mos7840.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Clean ups from Moschip version and a few ioctl implementations by:
  17. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/serial.h>
  35. #include <asm/uaccess.h>
  36. /*
  37. * Version Information
  38. */
  39. #define DRIVER_VERSION "1.3.1"
  40. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  41. /*
  42. * 16C50 UART register defines
  43. */
  44. #define LCR_BITS_5 0x00 /* 5 bits/char */
  45. #define LCR_BITS_6 0x01 /* 6 bits/char */
  46. #define LCR_BITS_7 0x02 /* 7 bits/char */
  47. #define LCR_BITS_8 0x03 /* 8 bits/char */
  48. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  49. #define LCR_STOP_1 0x00 /* 1 stop bit */
  50. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  51. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  52. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  53. #define LCR_PAR_NONE 0x00 /* No parity */
  54. #define LCR_PAR_ODD 0x08 /* Odd parity */
  55. #define LCR_PAR_EVEN 0x18 /* Even parity */
  56. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  57. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  58. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  59. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  60. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  61. #define MCR_DTR 0x01 /* Assert DTR */
  62. #define MCR_RTS 0x02 /* Assert RTS */
  63. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  64. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  65. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  66. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  67. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  68. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  69. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  70. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  71. /*
  72. * Defines used for sending commands to port
  73. */
  74. #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
  75. #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
  76. #define MOS_PORT1 0x0200
  77. #define MOS_PORT2 0x0300
  78. #define MOS_VENREG 0x0000
  79. #define MOS_MAX_PORT 0x02
  80. #define MOS_WRITE 0x0E
  81. #define MOS_READ 0x0D
  82. /* Requests */
  83. #define MCS_RD_RTYPE 0xC0
  84. #define MCS_WR_RTYPE 0x40
  85. #define MCS_RDREQ 0x0D
  86. #define MCS_WRREQ 0x0E
  87. #define MCS_CTRL_TIMEOUT 500
  88. #define VENDOR_READ_LENGTH (0x01)
  89. #define MAX_NAME_LEN 64
  90. #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
  91. #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
  92. /* For higher baud Rates use TIOCEXBAUD */
  93. #define TIOCEXBAUD 0x5462
  94. /* vendor id and device id defines */
  95. #define USB_VENDOR_ID_MOSCHIP 0x9710
  96. #define MOSCHIP_DEVICE_ID_7840 0x7840
  97. #define MOSCHIP_DEVICE_ID_7820 0x7820
  98. /* Interrupt Rotinue Defines */
  99. #define SERIAL_IIR_RLS 0x06
  100. #define SERIAL_IIR_MS 0x00
  101. /*
  102. * Emulation of the bit mask on the LINE STATUS REGISTER.
  103. */
  104. #define SERIAL_LSR_DR 0x0001
  105. #define SERIAL_LSR_OE 0x0002
  106. #define SERIAL_LSR_PE 0x0004
  107. #define SERIAL_LSR_FE 0x0008
  108. #define SERIAL_LSR_BI 0x0010
  109. #define MOS_MSR_DELTA_CTS 0x10
  110. #define MOS_MSR_DELTA_DSR 0x20
  111. #define MOS_MSR_DELTA_RI 0x40
  112. #define MOS_MSR_DELTA_CD 0x80
  113. // Serial Port register Address
  114. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  115. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  116. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  117. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  118. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  119. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  120. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  121. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  122. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  123. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  124. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  125. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  126. /*
  127. * URB POOL related defines
  128. */
  129. #define NUM_URBS 16 /* URB Count */
  130. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  131. static struct usb_device_id moschip_port_id_table[] = {
  132. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  133. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  134. {} /* terminating entry */
  135. };
  136. static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
  137. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  138. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  139. {} /* terminating entry */
  140. };
  141. MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
  142. /* This structure holds all of the local port information */
  143. struct moschip_port {
  144. int port_num; /*Actual port number in the device(1,2,etc) */
  145. struct urb *write_urb; /* write URB for this port */
  146. struct urb *read_urb; /* read URB for this port */
  147. struct urb *int_urb;
  148. __u8 shadowLCR; /* last LCR value received */
  149. __u8 shadowMCR; /* last MCR value received */
  150. char open;
  151. char open_ports;
  152. char zombie;
  153. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  154. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  155. int delta_msr_cond;
  156. struct async_icount icount;
  157. struct usb_serial_port *port; /* loop back to the owner of this object */
  158. /*Offsets */
  159. __u8 SpRegOffset;
  160. __u8 ControlRegOffset;
  161. __u8 DcrRegOffset;
  162. //for processing control URBS in interrupt context
  163. struct urb *control_urb;
  164. struct usb_ctrlrequest *dr;
  165. char *ctrl_buf;
  166. int MsrLsr;
  167. spinlock_t pool_lock;
  168. struct urb *write_urb_pool[NUM_URBS];
  169. char busy[NUM_URBS];
  170. };
  171. static int debug;
  172. /*
  173. * mos7840_set_reg_sync
  174. * To set the Control register by calling usb_fill_control_urb function
  175. * by passing usb_sndctrlpipe function as parameter.
  176. */
  177. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  178. __u16 val)
  179. {
  180. struct usb_device *dev = port->serial->dev;
  181. val = val & 0x00ff;
  182. dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  183. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  184. MCS_WR_RTYPE, val, reg, NULL, 0,
  185. MOS_WDR_TIMEOUT);
  186. }
  187. /*
  188. * mos7840_get_reg_sync
  189. * To set the Uart register by calling usb_fill_control_urb function by
  190. * passing usb_rcvctrlpipe function as parameter.
  191. */
  192. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  193. __u16 * val)
  194. {
  195. struct usb_device *dev = port->serial->dev;
  196. int ret = 0;
  197. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  198. MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
  199. MOS_WDR_TIMEOUT);
  200. dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
  201. *val = (*val) & 0x00ff;
  202. return ret;
  203. }
  204. /*
  205. * mos7840_set_uart_reg
  206. * To set the Uart register by calling usb_fill_control_urb function by
  207. * passing usb_sndctrlpipe function as parameter.
  208. */
  209. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  210. __u16 val)
  211. {
  212. struct usb_device *dev = port->serial->dev;
  213. val = val & 0x00ff;
  214. // For the UART control registers, the application number need to be Or'ed
  215. if (port->serial->num_ports == 4) {
  216. val |=
  217. (((__u16) port->number - (__u16) (port->serial->minor)) +
  218. 1) << 8;
  219. dbg("mos7840_set_uart_reg application number is %x\n", val);
  220. } else {
  221. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  222. val |=
  223. (((__u16) port->number -
  224. (__u16) (port->serial->minor)) + 1) << 8;
  225. dbg("mos7840_set_uart_reg application number is %x\n",
  226. val);
  227. } else {
  228. val |=
  229. (((__u16) port->number -
  230. (__u16) (port->serial->minor)) + 2) << 8;
  231. dbg("mos7840_set_uart_reg application number is %x\n",
  232. val);
  233. }
  234. }
  235. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  236. MCS_WR_RTYPE, val, reg, NULL, 0,
  237. MOS_WDR_TIMEOUT);
  238. }
  239. /*
  240. * mos7840_get_uart_reg
  241. * To set the Control register by calling usb_fill_control_urb function
  242. * by passing usb_rcvctrlpipe function as parameter.
  243. */
  244. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  245. __u16 * val)
  246. {
  247. struct usb_device *dev = port->serial->dev;
  248. int ret = 0;
  249. __u16 Wval;
  250. //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
  251. /*Wval is same as application number */
  252. if (port->serial->num_ports == 4) {
  253. Wval =
  254. (((__u16) port->number - (__u16) (port->serial->minor)) +
  255. 1) << 8;
  256. dbg("mos7840_get_uart_reg application number is %x\n", Wval);
  257. } else {
  258. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  259. Wval =
  260. (((__u16) port->number -
  261. (__u16) (port->serial->minor)) + 1) << 8;
  262. dbg("mos7840_get_uart_reg application number is %x\n",
  263. Wval);
  264. } else {
  265. Wval =
  266. (((__u16) port->number -
  267. (__u16) (port->serial->minor)) + 2) << 8;
  268. dbg("mos7840_get_uart_reg application number is %x\n",
  269. Wval);
  270. }
  271. }
  272. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  273. MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
  274. MOS_WDR_TIMEOUT);
  275. *val = (*val) & 0x00ff;
  276. return ret;
  277. }
  278. static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
  279. {
  280. dbg("***************************************\n");
  281. dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  282. dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
  283. dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
  284. dbg("***************************************\n");
  285. }
  286. /************************************************************************/
  287. /************************************************************************/
  288. /* I N T E R F A C E F U N C T I O N S */
  289. /* I N T E R F A C E F U N C T I O N S */
  290. /************************************************************************/
  291. /************************************************************************/
  292. static inline void mos7840_set_port_private(struct usb_serial_port *port,
  293. struct moschip_port *data)
  294. {
  295. usb_set_serial_port_data(port, (void *)data);
  296. }
  297. static inline struct moschip_port *mos7840_get_port_private(struct
  298. usb_serial_port
  299. *port)
  300. {
  301. return (struct moschip_port *)usb_get_serial_port_data(port);
  302. }
  303. static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
  304. {
  305. struct moschip_port *mos7840_port;
  306. struct async_icount *icount;
  307. mos7840_port = port;
  308. icount = &mos7840_port->icount;
  309. if (new_msr &
  310. (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
  311. MOS_MSR_DELTA_CD)) {
  312. icount = &mos7840_port->icount;
  313. /* update input line counters */
  314. if (new_msr & MOS_MSR_DELTA_CTS) {
  315. icount->cts++;
  316. smp_wmb();
  317. }
  318. if (new_msr & MOS_MSR_DELTA_DSR) {
  319. icount->dsr++;
  320. smp_wmb();
  321. }
  322. if (new_msr & MOS_MSR_DELTA_CD) {
  323. icount->dcd++;
  324. smp_wmb();
  325. }
  326. if (new_msr & MOS_MSR_DELTA_RI) {
  327. icount->rng++;
  328. smp_wmb();
  329. }
  330. }
  331. }
  332. static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
  333. {
  334. struct async_icount *icount;
  335. dbg("%s - %02x", __FUNCTION__, new_lsr);
  336. if (new_lsr & SERIAL_LSR_BI) {
  337. //
  338. // Parity and Framing errors only count if they
  339. // occur exclusive of a break being
  340. // received.
  341. //
  342. new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
  343. }
  344. /* update input line counters */
  345. icount = &port->icount;
  346. if (new_lsr & SERIAL_LSR_BI) {
  347. icount->brk++;
  348. smp_wmb();
  349. }
  350. if (new_lsr & SERIAL_LSR_OE) {
  351. icount->overrun++;
  352. smp_wmb();
  353. }
  354. if (new_lsr & SERIAL_LSR_PE) {
  355. icount->parity++;
  356. smp_wmb();
  357. }
  358. if (new_lsr & SERIAL_LSR_FE) {
  359. icount->frame++;
  360. smp_wmb();
  361. }
  362. }
  363. /************************************************************************/
  364. /************************************************************************/
  365. /* U S B C A L L B A C K F U N C T I O N S */
  366. /* U S B C A L L B A C K F U N C T I O N S */
  367. /************************************************************************/
  368. /************************************************************************/
  369. static void mos7840_control_callback(struct urb *urb)
  370. {
  371. unsigned char *data;
  372. struct moschip_port *mos7840_port;
  373. __u8 regval = 0x0;
  374. int result = 0;
  375. int status = urb->status;
  376. if (!urb) {
  377. dbg("%s", "Invalid Pointer !!!!:\n");
  378. return;
  379. }
  380. mos7840_port = (struct moschip_port *)urb->context;
  381. switch (status) {
  382. case 0:
  383. /* success */
  384. break;
  385. case -ECONNRESET:
  386. case -ENOENT:
  387. case -ESHUTDOWN:
  388. /* this urb is terminated, clean up */
  389. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  390. status);
  391. return;
  392. default:
  393. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  394. status);
  395. goto exit;
  396. }
  397. dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
  398. dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
  399. mos7840_port->MsrLsr, mos7840_port->port_num);
  400. data = urb->transfer_buffer;
  401. regval = (__u8) data[0];
  402. dbg("%s data is %x\n", __FUNCTION__, regval);
  403. if (mos7840_port->MsrLsr == 0)
  404. mos7840_handle_new_msr(mos7840_port, regval);
  405. else if (mos7840_port->MsrLsr == 1)
  406. mos7840_handle_new_lsr(mos7840_port, regval);
  407. exit:
  408. spin_lock(&mos7840_port->pool_lock);
  409. if (!mos7840_port->zombie)
  410. result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
  411. spin_unlock(&mos7840_port->pool_lock);
  412. if (result) {
  413. dev_err(&urb->dev->dev,
  414. "%s - Error %d submitting interrupt urb\n",
  415. __FUNCTION__, result);
  416. }
  417. }
  418. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  419. __u16 * val)
  420. {
  421. struct usb_device *dev = mcs->port->serial->dev;
  422. struct usb_ctrlrequest *dr = mcs->dr;
  423. unsigned char *buffer = mcs->ctrl_buf;
  424. int ret;
  425. dr->bRequestType = MCS_RD_RTYPE;
  426. dr->bRequest = MCS_RDREQ;
  427. dr->wValue = cpu_to_le16(Wval); //0;
  428. dr->wIndex = cpu_to_le16(reg);
  429. dr->wLength = cpu_to_le16(2);
  430. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  431. (unsigned char *)dr, buffer, 2,
  432. mos7840_control_callback, mcs);
  433. mcs->control_urb->transfer_buffer_length = 2;
  434. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  435. return ret;
  436. }
  437. /*****************************************************************************
  438. * mos7840_interrupt_callback
  439. * this is the callback function for when we have received data on the
  440. * interrupt endpoint.
  441. *****************************************************************************/
  442. static void mos7840_interrupt_callback(struct urb *urb)
  443. {
  444. int result;
  445. int length;
  446. struct moschip_port *mos7840_port;
  447. struct usb_serial *serial;
  448. __u16 Data;
  449. unsigned char *data;
  450. __u8 sp[5], st;
  451. int i, rv = 0;
  452. __u16 wval, wreg = 0;
  453. int status = urb->status;
  454. dbg("%s", " : Entering\n");
  455. if (!urb) {
  456. dbg("%s", "Invalid Pointer !!!!:\n");
  457. return;
  458. }
  459. switch (status) {
  460. case 0:
  461. /* success */
  462. break;
  463. case -ECONNRESET:
  464. case -ENOENT:
  465. case -ESHUTDOWN:
  466. /* this urb is terminated, clean up */
  467. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  468. status);
  469. return;
  470. default:
  471. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  472. status);
  473. goto exit;
  474. }
  475. length = urb->actual_length;
  476. data = urb->transfer_buffer;
  477. serial = (struct usb_serial *)urb->context;
  478. /* Moschip get 5 bytes
  479. * Byte 1 IIR Port 1 (port.number is 0)
  480. * Byte 2 IIR Port 2 (port.number is 1)
  481. * Byte 3 IIR Port 3 (port.number is 2)
  482. * Byte 4 IIR Port 4 (port.number is 3)
  483. * Byte 5 FIFO status for both */
  484. if (length && length > 5) {
  485. dbg("%s \n", "Wrong data !!!");
  486. return;
  487. }
  488. sp[0] = (__u8) data[0];
  489. sp[1] = (__u8) data[1];
  490. sp[2] = (__u8) data[2];
  491. sp[3] = (__u8) data[3];
  492. st = (__u8) data[4];
  493. for (i = 0; i < serial->num_ports; i++) {
  494. mos7840_port = mos7840_get_port_private(serial->port[i]);
  495. wval =
  496. (((__u16) serial->port[i]->number -
  497. (__u16) (serial->minor)) + 1) << 8;
  498. if (mos7840_port->open) {
  499. if (sp[i] & 0x01) {
  500. dbg("SP%d No Interrupt !!!\n", i);
  501. } else {
  502. switch (sp[i] & 0x0f) {
  503. case SERIAL_IIR_RLS:
  504. dbg("Serial Port %d: Receiver status error or ", i);
  505. dbg("address bit detected in 9-bit mode\n");
  506. mos7840_port->MsrLsr = 1;
  507. wreg = LINE_STATUS_REGISTER;
  508. break;
  509. case SERIAL_IIR_MS:
  510. dbg("Serial Port %d: Modem status change\n", i);
  511. mos7840_port->MsrLsr = 0;
  512. wreg = MODEM_STATUS_REGISTER;
  513. break;
  514. }
  515. spin_lock(&mos7840_port->pool_lock);
  516. if (!mos7840_port->zombie) {
  517. rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
  518. } else {
  519. spin_unlock(&mos7840_port->pool_lock);
  520. return;
  521. }
  522. spin_unlock(&mos7840_port->pool_lock);
  523. }
  524. }
  525. }
  526. if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
  527. return;
  528. exit:
  529. result = usb_submit_urb(urb, GFP_ATOMIC);
  530. if (result) {
  531. dev_err(&urb->dev->dev,
  532. "%s - Error %d submitting interrupt urb\n",
  533. __FUNCTION__, result);
  534. }
  535. }
  536. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  537. const char *function)
  538. {
  539. if (!port) {
  540. dbg("%s - port == NULL", function);
  541. return -1;
  542. }
  543. if (!port->serial) {
  544. dbg("%s - port->serial == NULL", function);
  545. return -1;
  546. }
  547. return 0;
  548. }
  549. /* Inline functions to check the sanity of a pointer that is passed to us */
  550. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  551. const char *function)
  552. {
  553. if (!serial) {
  554. dbg("%s - serial == NULL", function);
  555. return -1;
  556. }
  557. if (!serial->type) {
  558. dbg("%s - serial->type == NULL!", function);
  559. return -1;
  560. }
  561. return 0;
  562. }
  563. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  564. const char *function)
  565. {
  566. /* if no port was specified, or it fails a paranoia check */
  567. if (!port ||
  568. mos7840_port_paranoia_check(port, function) ||
  569. mos7840_serial_paranoia_check(port->serial, function)) {
  570. /* then say that we don't have a valid usb_serial thing, which will
  571. * end up genrating -ENODEV return values */
  572. return NULL;
  573. }
  574. return port->serial;
  575. }
  576. /*****************************************************************************
  577. * mos7840_bulk_in_callback
  578. * this is the callback function for when we have received data on the
  579. * bulk in endpoint.
  580. *****************************************************************************/
  581. static void mos7840_bulk_in_callback(struct urb *urb)
  582. {
  583. int retval;
  584. unsigned char *data;
  585. struct usb_serial *serial;
  586. struct usb_serial_port *port;
  587. struct moschip_port *mos7840_port;
  588. struct tty_struct *tty;
  589. int status = urb->status;
  590. if (!urb) {
  591. dbg("%s", "Invalid Pointer !!!!:\n");
  592. return;
  593. }
  594. if (status) {
  595. dbg("nonzero read bulk status received: %d", status);
  596. return;
  597. }
  598. mos7840_port = (struct moschip_port *)urb->context;
  599. if (!mos7840_port) {
  600. dbg("%s", "NULL mos7840_port pointer \n");
  601. return;
  602. }
  603. port = (struct usb_serial_port *)mos7840_port->port;
  604. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  605. dbg("%s", "Port Paranoia failed \n");
  606. return;
  607. }
  608. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  609. if (!serial) {
  610. dbg("%s\n", "Bad serial pointer ");
  611. return;
  612. }
  613. dbg("%s\n", "Entering... \n");
  614. data = urb->transfer_buffer;
  615. dbg("%s", "Entering ........... \n");
  616. if (urb->actual_length) {
  617. tty = mos7840_port->port->tty;
  618. if (tty) {
  619. tty_buffer_request_room(tty, urb->actual_length);
  620. tty_insert_flip_string(tty, data, urb->actual_length);
  621. dbg(" %s \n", data);
  622. tty_flip_buffer_push(tty);
  623. }
  624. mos7840_port->icount.rx += urb->actual_length;
  625. smp_wmb();
  626. dbg("mos7840_port->icount.rx is %d:\n",
  627. mos7840_port->icount.rx);
  628. }
  629. if (!mos7840_port->read_urb) {
  630. dbg("%s", "URB KILLED !!!\n");
  631. return;
  632. }
  633. mos7840_port->read_urb->dev = serial->dev;
  634. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  635. if (retval) {
  636. dbg(" usb_submit_urb(read bulk) failed, retval = %d",
  637. retval);
  638. }
  639. }
  640. /*****************************************************************************
  641. * mos7840_bulk_out_data_callback
  642. * this is the callback function for when we have finished sending serial data
  643. * on the bulk out endpoint.
  644. *****************************************************************************/
  645. static void mos7840_bulk_out_data_callback(struct urb *urb)
  646. {
  647. struct moschip_port *mos7840_port;
  648. struct tty_struct *tty;
  649. int status = urb->status;
  650. int i;
  651. if (!urb) {
  652. dbg("%s", "Invalid Pointer !!!!:\n");
  653. return;
  654. }
  655. mos7840_port = (struct moschip_port *)urb->context;
  656. spin_lock(&mos7840_port->pool_lock);
  657. for (i = 0; i < NUM_URBS; i++) {
  658. if (urb == mos7840_port->write_urb_pool[i]) {
  659. mos7840_port->busy[i] = 0;
  660. break;
  661. }
  662. }
  663. spin_unlock(&mos7840_port->pool_lock);
  664. if (status) {
  665. dbg("nonzero write bulk status received:%d\n", status);
  666. return;
  667. }
  668. if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
  669. dbg("%s", "Port Paranoia failed \n");
  670. return;
  671. }
  672. dbg("%s \n", "Entering .........");
  673. tty = mos7840_port->port->tty;
  674. if (tty && mos7840_port->open)
  675. tty_wakeup(tty);
  676. }
  677. /************************************************************************/
  678. /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
  679. /************************************************************************/
  680. #ifdef MCSSerialProbe
  681. static int mos7840_serial_probe(struct usb_serial *serial,
  682. const struct usb_device_id *id)
  683. {
  684. /*need to implement the mode_reg reading and updating\
  685. structures usb_serial_ device_type\
  686. (i.e num_ports, num_bulkin,bulkout etc) */
  687. /* Also we can update the changes attach */
  688. return 1;
  689. }
  690. #endif
  691. /*****************************************************************************
  692. * mos7840_open
  693. * this function is called by the tty driver when a port is opened
  694. * If successful, we return 0
  695. * Otherwise we return a negative error number.
  696. *****************************************************************************/
  697. static int mos7840_open(struct usb_serial_port *port, struct file *filp)
  698. {
  699. int response;
  700. int j;
  701. struct usb_serial *serial;
  702. struct urb *urb;
  703. __u16 Data;
  704. int status;
  705. struct moschip_port *mos7840_port;
  706. struct moschip_port *port0;
  707. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  708. dbg("%s", "Port Paranoia failed \n");
  709. return -ENODEV;
  710. }
  711. serial = port->serial;
  712. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  713. dbg("%s", "Serial Paranoia failed \n");
  714. return -ENODEV;
  715. }
  716. mos7840_port = mos7840_get_port_private(port);
  717. port0 = mos7840_get_port_private(serial->port[0]);
  718. if (mos7840_port == NULL || port0 == NULL)
  719. return -ENODEV;
  720. usb_clear_halt(serial->dev, port->write_urb->pipe);
  721. usb_clear_halt(serial->dev, port->read_urb->pipe);
  722. port0->open_ports++;
  723. /* Initialising the write urb pool */
  724. for (j = 0; j < NUM_URBS; ++j) {
  725. urb = usb_alloc_urb(0, GFP_KERNEL);
  726. mos7840_port->write_urb_pool[j] = urb;
  727. if (urb == NULL) {
  728. err("No more urbs???");
  729. continue;
  730. }
  731. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  732. if (!urb->transfer_buffer) {
  733. usb_free_urb(urb);
  734. mos7840_port->write_urb_pool[j] = NULL;
  735. err("%s-out of memory for urb buffers.", __FUNCTION__);
  736. continue;
  737. }
  738. }
  739. /*****************************************************************************
  740. * Initialize MCS7840 -- Write Init values to corresponding Registers
  741. *
  742. * Register Index
  743. * 1 : IER
  744. * 2 : FCR
  745. * 3 : LCR
  746. * 4 : MCR
  747. *
  748. * 0x08 : SP1/2 Control Reg
  749. *****************************************************************************/
  750. //NEED to check the following Block
  751. status = 0;
  752. Data = 0x0;
  753. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  754. if (status < 0) {
  755. dbg("Reading Spreg failed\n");
  756. return -1;
  757. }
  758. Data |= 0x80;
  759. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  760. if (status < 0) {
  761. dbg("writing Spreg failed\n");
  762. return -1;
  763. }
  764. Data &= ~0x80;
  765. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  766. if (status < 0) {
  767. dbg("writing Spreg failed\n");
  768. return -1;
  769. }
  770. //End of block to be checked
  771. status = 0;
  772. Data = 0x0;
  773. status =
  774. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  775. if (status < 0) {
  776. dbg("Reading Controlreg failed\n");
  777. return -1;
  778. }
  779. Data |= 0x08; //Driver done bit
  780. Data |= 0x20; //rx_disable
  781. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  782. if (status < 0) {
  783. dbg("writing Controlreg failed\n");
  784. return -1;
  785. }
  786. //do register settings here
  787. // Set all regs to the device default values.
  788. ////////////////////////////////////
  789. // First Disable all interrupts.
  790. ////////////////////////////////////
  791. Data = 0x00;
  792. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  793. if (status < 0) {
  794. dbg("disableing interrupts failed\n");
  795. return -1;
  796. }
  797. // Set FIFO_CONTROL_REGISTER to the default value
  798. Data = 0x00;
  799. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  800. if (status < 0) {
  801. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  802. return -1;
  803. }
  804. Data = 0xcf;
  805. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  806. if (status < 0) {
  807. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  808. return -1;
  809. }
  810. Data = 0x03;
  811. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  812. mos7840_port->shadowLCR = Data;
  813. Data = 0x0b;
  814. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  815. mos7840_port->shadowMCR = Data;
  816. Data = 0x00;
  817. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  818. mos7840_port->shadowLCR = Data;
  819. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  820. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  821. Data = 0x0c;
  822. status = 0;
  823. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  824. Data = 0x0;
  825. status = 0;
  826. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  827. Data = 0x00;
  828. status = 0;
  829. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  830. Data = Data & ~SERIAL_LCR_DLAB;
  831. status = 0;
  832. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  833. mos7840_port->shadowLCR = Data;
  834. //clearing Bulkin and Bulkout Fifo
  835. Data = 0x0;
  836. status = 0;
  837. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  838. Data = Data | 0x0c;
  839. status = 0;
  840. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  841. Data = Data & ~0x0c;
  842. status = 0;
  843. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  844. //Finally enable all interrupts
  845. Data = 0x0;
  846. Data = 0x0c;
  847. status = 0;
  848. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  849. //clearing rx_disable
  850. Data = 0x0;
  851. status = 0;
  852. status =
  853. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  854. Data = Data & ~0x20;
  855. status = 0;
  856. status =
  857. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  858. // rx_negate
  859. Data = 0x0;
  860. status = 0;
  861. status =
  862. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  863. Data = Data | 0x10;
  864. status = 0;
  865. status =
  866. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  867. /* force low_latency on so that our tty_push actually forces *
  868. * the data through,otherwise it is scheduled, and with *
  869. * high data rates (like with OHCI) data can get lost. */
  870. if (port->tty)
  871. port->tty->low_latency = 1;
  872. /* Check to see if we've set up our endpoint info yet *
  873. * (can't set it up in mos7840_startup as the structures *
  874. * were not set up at that time.) */
  875. if (port0->open_ports == 1) {
  876. if (serial->port[0]->interrupt_in_buffer == NULL) {
  877. /* set up interrupt urb */
  878. usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
  879. serial->dev,
  880. usb_rcvintpipe(serial->dev,
  881. serial->port[0]->
  882. interrupt_in_endpointAddress),
  883. serial->port[0]->interrupt_in_buffer,
  884. serial->port[0]->interrupt_in_urb->
  885. transfer_buffer_length,
  886. mos7840_interrupt_callback,
  887. serial,
  888. serial->port[0]->interrupt_in_urb->
  889. interval);
  890. /* start interrupt read for mos7840 *
  891. * will continue as long as mos7840 is connected */
  892. response =
  893. usb_submit_urb(serial->port[0]->interrupt_in_urb,
  894. GFP_KERNEL);
  895. if (response) {
  896. err("%s - Error %d submitting interrupt urb",
  897. __FUNCTION__, response);
  898. }
  899. }
  900. }
  901. /* see if we've set up our endpoint info yet *
  902. * (can't set it up in mos7840_startup as the *
  903. * structures were not set up at that time.) */
  904. dbg("port number is %d \n", port->number);
  905. dbg("serial number is %d \n", port->serial->minor);
  906. dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
  907. dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
  908. dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
  909. dbg("port's number in the device is %d\n", mos7840_port->port_num);
  910. mos7840_port->read_urb = port->read_urb;
  911. /* set up our bulk in urb */
  912. usb_fill_bulk_urb(mos7840_port->read_urb,
  913. serial->dev,
  914. usb_rcvbulkpipe(serial->dev,
  915. port->bulk_in_endpointAddress),
  916. port->bulk_in_buffer,
  917. mos7840_port->read_urb->transfer_buffer_length,
  918. mos7840_bulk_in_callback, mos7840_port);
  919. dbg("mos7840_open: bulkin endpoint is %d\n",
  920. port->bulk_in_endpointAddress);
  921. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  922. if (response) {
  923. err("%s - Error %d submitting control urb", __FUNCTION__,
  924. response);
  925. }
  926. /* initialize our wait queues */
  927. init_waitqueue_head(&mos7840_port->wait_chase);
  928. init_waitqueue_head(&mos7840_port->delta_msr_wait);
  929. /* initialize our icount structure */
  930. memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
  931. /* initialize our port settings */
  932. mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
  933. /* send a open port command */
  934. mos7840_port->open = 1;
  935. //mos7840_change_port_settings(mos7840_port,old_termios);
  936. mos7840_port->icount.tx = 0;
  937. mos7840_port->icount.rx = 0;
  938. dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
  939. return 0;
  940. }
  941. /*****************************************************************************
  942. * mos7840_chars_in_buffer
  943. * this function is called by the tty driver when it wants to know how many
  944. * bytes of data we currently have outstanding in the port (data that has
  945. * been written, but hasn't made it out the port yet)
  946. * If successful, we return the number of bytes left to be written in the
  947. * system,
  948. * Otherwise we return a negative error number.
  949. *****************************************************************************/
  950. static int mos7840_chars_in_buffer(struct usb_serial_port *port)
  951. {
  952. int i;
  953. int chars = 0;
  954. unsigned long flags;
  955. struct moschip_port *mos7840_port;
  956. dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
  957. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  958. dbg("%s", "Invalid port \n");
  959. return -1;
  960. }
  961. mos7840_port = mos7840_get_port_private(port);
  962. if (mos7840_port == NULL) {
  963. dbg("%s \n", "mos7840_break:leaving ...........");
  964. return -1;
  965. }
  966. spin_lock_irqsave(&mos7840_port->pool_lock,flags);
  967. for (i = 0; i < NUM_URBS; ++i) {
  968. if (mos7840_port->busy[i]) {
  969. chars += URB_TRANSFER_BUFFER_SIZE;
  970. }
  971. }
  972. spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
  973. dbg("%s - returns %d", __FUNCTION__, chars);
  974. return chars;
  975. }
  976. /************************************************************************
  977. *
  978. * mos7840_block_until_tx_empty
  979. *
  980. * This function will block the close until one of the following:
  981. * 1. TX count are 0
  982. * 2. The mos7840 has stopped
  983. * 3. A timout of 3 seconds without activity has expired
  984. *
  985. ************************************************************************/
  986. static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
  987. {
  988. int timeout = HZ / 10;
  989. int wait = 30;
  990. int count;
  991. while (1) {
  992. count = mos7840_chars_in_buffer(mos7840_port->port);
  993. /* Check for Buffer status */
  994. if (count <= 0) {
  995. return;
  996. }
  997. /* Block the thread for a while */
  998. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  999. timeout);
  1000. /* No activity.. count down section */
  1001. wait--;
  1002. if (wait == 0) {
  1003. dbg("%s - TIMEOUT", __FUNCTION__);
  1004. return;
  1005. } else {
  1006. /* Reset timout value back to seconds */
  1007. wait = 30;
  1008. }
  1009. }
  1010. }
  1011. /*****************************************************************************
  1012. * mos7840_close
  1013. * this function is called by the tty driver when a port is closed
  1014. *****************************************************************************/
  1015. static void mos7840_close(struct usb_serial_port *port, struct file *filp)
  1016. {
  1017. struct usb_serial *serial;
  1018. struct moschip_port *mos7840_port;
  1019. struct moschip_port *port0;
  1020. int j;
  1021. __u16 Data;
  1022. dbg("%s\n", "mos7840_close:entering...");
  1023. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1024. dbg("%s", "Port Paranoia failed \n");
  1025. return;
  1026. }
  1027. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  1028. if (!serial) {
  1029. dbg("%s", "Serial Paranoia failed \n");
  1030. return;
  1031. }
  1032. mos7840_port = mos7840_get_port_private(port);
  1033. port0 = mos7840_get_port_private(serial->port[0]);
  1034. if (mos7840_port == NULL || port0 == NULL)
  1035. return;
  1036. for (j = 0; j < NUM_URBS; ++j)
  1037. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  1038. /* Freeing Write URBs */
  1039. for (j = 0; j < NUM_URBS; ++j) {
  1040. if (mos7840_port->write_urb_pool[j]) {
  1041. if (mos7840_port->write_urb_pool[j]->transfer_buffer)
  1042. kfree(mos7840_port->write_urb_pool[j]->
  1043. transfer_buffer);
  1044. usb_free_urb(mos7840_port->write_urb_pool[j]);
  1045. }
  1046. }
  1047. if (serial->dev) {
  1048. /* flush and block until tx is empty */
  1049. mos7840_block_until_tx_empty(mos7840_port);
  1050. }
  1051. /* While closing port, shutdown all bulk read, write *
  1052. * and interrupt read if they exists */
  1053. if (serial->dev) {
  1054. if (mos7840_port->write_urb) {
  1055. dbg("%s", "Shutdown bulk write\n");
  1056. usb_kill_urb(mos7840_port->write_urb);
  1057. }
  1058. if (mos7840_port->read_urb) {
  1059. dbg("%s", "Shutdown bulk read\n");
  1060. usb_kill_urb(mos7840_port->read_urb);
  1061. }
  1062. if ((&mos7840_port->control_urb)) {
  1063. dbg("%s", "Shutdown control read\n");
  1064. // usb_kill_urb (mos7840_port->control_urb);
  1065. }
  1066. }
  1067. // if(mos7840_port->ctrl_buf != NULL)
  1068. // kfree(mos7840_port->ctrl_buf);
  1069. port0->open_ports--;
  1070. dbg("mos7840_num_open_ports in close%d:in port%d\n",
  1071. port0->open_ports, port->number);
  1072. if (port0->open_ports == 0) {
  1073. if (serial->port[0]->interrupt_in_urb) {
  1074. dbg("%s", "Shutdown interrupt_in_urb\n");
  1075. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1076. }
  1077. }
  1078. if (mos7840_port->write_urb) {
  1079. /* if this urb had a transfer buffer already (old tx) free it */
  1080. if (mos7840_port->write_urb->transfer_buffer != NULL) {
  1081. kfree(mos7840_port->write_urb->transfer_buffer);
  1082. }
  1083. usb_free_urb(mos7840_port->write_urb);
  1084. }
  1085. Data = 0x0;
  1086. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1087. Data = 0x00;
  1088. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1089. mos7840_port->open = 0;
  1090. dbg("%s \n", "Leaving ............");
  1091. }
  1092. /************************************************************************
  1093. *
  1094. * mos7840_block_until_chase_response
  1095. *
  1096. * This function will block the close until one of the following:
  1097. * 1. Response to our Chase comes from mos7840
  1098. * 2. A timout of 10 seconds without activity has expired
  1099. * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
  1100. *
  1101. ************************************************************************/
  1102. static void mos7840_block_until_chase_response(struct moschip_port
  1103. *mos7840_port)
  1104. {
  1105. int timeout = 1 * HZ;
  1106. int wait = 10;
  1107. int count;
  1108. while (1) {
  1109. count = mos7840_chars_in_buffer(mos7840_port->port);
  1110. /* Check for Buffer status */
  1111. if (count <= 0) {
  1112. return;
  1113. }
  1114. /* Block the thread for a while */
  1115. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  1116. timeout);
  1117. /* No activity.. count down section */
  1118. wait--;
  1119. if (wait == 0) {
  1120. dbg("%s - TIMEOUT", __FUNCTION__);
  1121. return;
  1122. } else {
  1123. /* Reset timout value back to seconds */
  1124. wait = 10;
  1125. }
  1126. }
  1127. }
  1128. /*****************************************************************************
  1129. * mos7840_break
  1130. * this function sends a break to the port
  1131. *****************************************************************************/
  1132. static void mos7840_break(struct usb_serial_port *port, int break_state)
  1133. {
  1134. unsigned char data;
  1135. struct usb_serial *serial;
  1136. struct moschip_port *mos7840_port;
  1137. dbg("%s \n", "Entering ...........");
  1138. dbg("mos7840_break: Start\n");
  1139. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1140. dbg("%s", "Port Paranoia failed \n");
  1141. return;
  1142. }
  1143. serial = mos7840_get_usb_serial(port, __FUNCTION__);
  1144. if (!serial) {
  1145. dbg("%s", "Serial Paranoia failed \n");
  1146. return;
  1147. }
  1148. mos7840_port = mos7840_get_port_private(port);
  1149. if (mos7840_port == NULL) {
  1150. return;
  1151. }
  1152. if (serial->dev) {
  1153. /* flush and block until tx is empty */
  1154. mos7840_block_until_chase_response(mos7840_port);
  1155. }
  1156. if (break_state == -1) {
  1157. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  1158. } else {
  1159. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  1160. }
  1161. mos7840_port->shadowLCR = data;
  1162. dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
  1163. mos7840_port->shadowLCR);
  1164. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  1165. mos7840_port->shadowLCR);
  1166. return;
  1167. }
  1168. /*****************************************************************************
  1169. * mos7840_write_room
  1170. * this function is called by the tty driver when it wants to know how many
  1171. * bytes of data we can accept for a specific port.
  1172. * If successful, we return the amount of room that we have for this port
  1173. * Otherwise we return a negative error number.
  1174. *****************************************************************************/
  1175. static int mos7840_write_room(struct usb_serial_port *port)
  1176. {
  1177. int i;
  1178. int room = 0;
  1179. unsigned long flags;
  1180. struct moschip_port *mos7840_port;
  1181. dbg("%s \n", " mos7840_write_room:entering ...........");
  1182. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1183. dbg("%s", "Invalid port \n");
  1184. dbg("%s \n", " mos7840_write_room:leaving ...........");
  1185. return -1;
  1186. }
  1187. mos7840_port = mos7840_get_port_private(port);
  1188. if (mos7840_port == NULL) {
  1189. dbg("%s \n", "mos7840_break:leaving ...........");
  1190. return -1;
  1191. }
  1192. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1193. for (i = 0; i < NUM_URBS; ++i) {
  1194. if (!mos7840_port->busy[i]) {
  1195. room += URB_TRANSFER_BUFFER_SIZE;
  1196. }
  1197. }
  1198. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1199. room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
  1200. dbg("%s - returns %d", __FUNCTION__, room);
  1201. return room;
  1202. }
  1203. /*****************************************************************************
  1204. * mos7840_write
  1205. * this function is called by the tty driver when data should be written to
  1206. * the port.
  1207. * If successful, we return the number of bytes written, otherwise we
  1208. * return a negative error number.
  1209. *****************************************************************************/
  1210. static int mos7840_write(struct usb_serial_port *port,
  1211. const unsigned char *data, int count)
  1212. {
  1213. int status;
  1214. int i;
  1215. int bytes_sent = 0;
  1216. int transfer_size;
  1217. unsigned long flags;
  1218. struct moschip_port *mos7840_port;
  1219. struct usb_serial *serial;
  1220. struct urb *urb;
  1221. //__u16 Data;
  1222. const unsigned char *current_position = data;
  1223. unsigned char *data1;
  1224. dbg("%s \n", "entering ...........");
  1225. //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
  1226. #ifdef NOTMOS7840
  1227. Data = 0x00;
  1228. status = 0;
  1229. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  1230. mos7840_port->shadowLCR = Data;
  1231. dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
  1232. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1233. mos7840_port->shadowLCR);
  1234. //Data = 0x03;
  1235. //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
  1236. //mos7840_port->shadowLCR=Data;//Need to add later
  1237. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  1238. status = 0;
  1239. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1240. //Data = 0x0c;
  1241. //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
  1242. Data = 0x00;
  1243. status = 0;
  1244. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
  1245. dbg("mos7840_write:DLL value is %x\n", Data);
  1246. Data = 0x0;
  1247. status = 0;
  1248. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
  1249. dbg("mos7840_write:DLM value is %x\n", Data);
  1250. Data = Data & ~SERIAL_LCR_DLAB;
  1251. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1252. mos7840_port->shadowLCR);
  1253. status = 0;
  1254. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1255. #endif
  1256. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1257. dbg("%s", "Port Paranoia failed \n");
  1258. return -1;
  1259. }
  1260. serial = port->serial;
  1261. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  1262. dbg("%s", "Serial Paranoia failed \n");
  1263. return -1;
  1264. }
  1265. mos7840_port = mos7840_get_port_private(port);
  1266. if (mos7840_port == NULL) {
  1267. dbg("%s", "mos7840_port is NULL\n");
  1268. return -1;
  1269. }
  1270. /* try to find a free urb in the list */
  1271. urb = NULL;
  1272. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1273. for (i = 0; i < NUM_URBS; ++i) {
  1274. if (!mos7840_port->busy[i]) {
  1275. mos7840_port->busy[i] = 1;
  1276. urb = mos7840_port->write_urb_pool[i];
  1277. dbg("\nURB:%d", i);
  1278. break;
  1279. }
  1280. }
  1281. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1282. if (urb == NULL) {
  1283. dbg("%s - no more free urbs", __FUNCTION__);
  1284. goto exit;
  1285. }
  1286. if (urb->transfer_buffer == NULL) {
  1287. urb->transfer_buffer =
  1288. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  1289. if (urb->transfer_buffer == NULL) {
  1290. err("%s no more kernel memory...", __FUNCTION__);
  1291. goto exit;
  1292. }
  1293. }
  1294. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1295. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1296. /* fill urb with data and submit */
  1297. usb_fill_bulk_urb(urb,
  1298. serial->dev,
  1299. usb_sndbulkpipe(serial->dev,
  1300. port->bulk_out_endpointAddress),
  1301. urb->transfer_buffer,
  1302. transfer_size,
  1303. mos7840_bulk_out_data_callback, mos7840_port);
  1304. data1 = urb->transfer_buffer;
  1305. dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
  1306. /* send it down the pipe */
  1307. status = usb_submit_urb(urb, GFP_ATOMIC);
  1308. if (status) {
  1309. mos7840_port->busy[i] = 0;
  1310. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  1311. __FUNCTION__, status);
  1312. bytes_sent = status;
  1313. goto exit;
  1314. }
  1315. bytes_sent = transfer_size;
  1316. mos7840_port->icount.tx += transfer_size;
  1317. smp_wmb();
  1318. dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
  1319. exit:
  1320. return bytes_sent;
  1321. }
  1322. /*****************************************************************************
  1323. * mos7840_throttle
  1324. * this function is called by the tty driver when it wants to stop the data
  1325. * being read from the port.
  1326. *****************************************************************************/
  1327. static void mos7840_throttle(struct usb_serial_port *port)
  1328. {
  1329. struct moschip_port *mos7840_port;
  1330. struct tty_struct *tty;
  1331. int status;
  1332. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1333. dbg("%s", "Invalid port \n");
  1334. return;
  1335. }
  1336. dbg("- port %d\n", port->number);
  1337. mos7840_port = mos7840_get_port_private(port);
  1338. if (mos7840_port == NULL)
  1339. return;
  1340. if (!mos7840_port->open) {
  1341. dbg("%s\n", "port not opened");
  1342. return;
  1343. }
  1344. dbg("%s", "Entering .......... \n");
  1345. tty = port->tty;
  1346. if (!tty) {
  1347. dbg("%s - no tty available", __FUNCTION__);
  1348. return;
  1349. }
  1350. /* if we are implementing XON/XOFF, send the stop character */
  1351. if (I_IXOFF(tty)) {
  1352. unsigned char stop_char = STOP_CHAR(tty);
  1353. status = mos7840_write(port, &stop_char, 1);
  1354. if (status <= 0) {
  1355. return;
  1356. }
  1357. }
  1358. /* if we are implementing RTS/CTS, toggle that line */
  1359. if (tty->termios->c_cflag & CRTSCTS) {
  1360. mos7840_port->shadowMCR &= ~MCR_RTS;
  1361. status = 0;
  1362. status =
  1363. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1364. mos7840_port->shadowMCR);
  1365. if (status < 0) {
  1366. return;
  1367. }
  1368. }
  1369. return;
  1370. }
  1371. /*****************************************************************************
  1372. * mos7840_unthrottle
  1373. * this function is called by the tty driver when it wants to resume the data
  1374. * being read from the port (called after SerialThrottle is called)
  1375. *****************************************************************************/
  1376. static void mos7840_unthrottle(struct usb_serial_port *port)
  1377. {
  1378. struct tty_struct *tty;
  1379. int status;
  1380. struct moschip_port *mos7840_port = mos7840_get_port_private(port);
  1381. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1382. dbg("%s", "Invalid port \n");
  1383. return;
  1384. }
  1385. if (mos7840_port == NULL)
  1386. return;
  1387. if (!mos7840_port->open) {
  1388. dbg("%s - port not opened", __FUNCTION__);
  1389. return;
  1390. }
  1391. dbg("%s", "Entering .......... \n");
  1392. tty = port->tty;
  1393. if (!tty) {
  1394. dbg("%s - no tty available", __FUNCTION__);
  1395. return;
  1396. }
  1397. /* if we are implementing XON/XOFF, send the start character */
  1398. if (I_IXOFF(tty)) {
  1399. unsigned char start_char = START_CHAR(tty);
  1400. status = mos7840_write(port, &start_char, 1);
  1401. if (status <= 0) {
  1402. return;
  1403. }
  1404. }
  1405. /* if we are implementing RTS/CTS, toggle that line */
  1406. if (tty->termios->c_cflag & CRTSCTS) {
  1407. mos7840_port->shadowMCR |= MCR_RTS;
  1408. status = 0;
  1409. status =
  1410. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1411. mos7840_port->shadowMCR);
  1412. if (status < 0) {
  1413. return;
  1414. }
  1415. }
  1416. return;
  1417. }
  1418. static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
  1419. {
  1420. struct moschip_port *mos7840_port;
  1421. unsigned int result;
  1422. __u16 msr;
  1423. __u16 mcr;
  1424. int status = 0;
  1425. mos7840_port = mos7840_get_port_private(port);
  1426. dbg("%s - port %d", __FUNCTION__, port->number);
  1427. if (mos7840_port == NULL)
  1428. return -ENODEV;
  1429. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  1430. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1431. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  1432. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  1433. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  1434. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  1435. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  1436. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  1437. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  1438. dbg("%s - 0x%04X", __FUNCTION__, result);
  1439. return result;
  1440. }
  1441. static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
  1442. unsigned int set, unsigned int clear)
  1443. {
  1444. struct moschip_port *mos7840_port;
  1445. unsigned int mcr;
  1446. unsigned int status;
  1447. dbg("%s - port %d", __FUNCTION__, port->number);
  1448. mos7840_port = mos7840_get_port_private(port);
  1449. if (mos7840_port == NULL)
  1450. return -ENODEV;
  1451. mcr = mos7840_port->shadowMCR;
  1452. if (clear & TIOCM_RTS)
  1453. mcr &= ~MCR_RTS;
  1454. if (clear & TIOCM_DTR)
  1455. mcr &= ~MCR_DTR;
  1456. if (clear & TIOCM_LOOP)
  1457. mcr &= ~MCR_LOOPBACK;
  1458. if (set & TIOCM_RTS)
  1459. mcr |= MCR_RTS;
  1460. if (set & TIOCM_DTR)
  1461. mcr |= MCR_DTR;
  1462. if (set & TIOCM_LOOP)
  1463. mcr |= MCR_LOOPBACK;
  1464. mos7840_port->shadowMCR = mcr;
  1465. status = 0;
  1466. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  1467. if (status < 0) {
  1468. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1469. return -1;
  1470. }
  1471. return 0;
  1472. }
  1473. /*****************************************************************************
  1474. * mos7840_calc_baud_rate_divisor
  1475. * this function calculates the proper baud rate divisor for the specified
  1476. * baud rate.
  1477. *****************************************************************************/
  1478. static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
  1479. __u16 * clk_sel_val)
  1480. {
  1481. dbg("%s - %d", __FUNCTION__, baudRate);
  1482. if (baudRate <= 115200) {
  1483. *divisor = 115200 / baudRate;
  1484. *clk_sel_val = 0x0;
  1485. }
  1486. if ((baudRate > 115200) && (baudRate <= 230400)) {
  1487. *divisor = 230400 / baudRate;
  1488. *clk_sel_val = 0x10;
  1489. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  1490. *divisor = 403200 / baudRate;
  1491. *clk_sel_val = 0x20;
  1492. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  1493. *divisor = 460800 / baudRate;
  1494. *clk_sel_val = 0x30;
  1495. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  1496. *divisor = 806400 / baudRate;
  1497. *clk_sel_val = 0x40;
  1498. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  1499. *divisor = 921600 / baudRate;
  1500. *clk_sel_val = 0x50;
  1501. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  1502. *divisor = 1572864 / baudRate;
  1503. *clk_sel_val = 0x60;
  1504. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  1505. *divisor = 3145728 / baudRate;
  1506. *clk_sel_val = 0x70;
  1507. }
  1508. return 0;
  1509. #ifdef NOTMCS7840
  1510. for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
  1511. if (mos7840_divisor_table[i].BaudRate == baudrate) {
  1512. *divisor = mos7840_divisor_table[i].Divisor;
  1513. return 0;
  1514. }
  1515. }
  1516. /* After trying for all the standard baud rates *
  1517. * Try calculating the divisor for this baud rate */
  1518. if (baudrate > 75 && baudrate < 230400) {
  1519. /* get the divisor */
  1520. custom = (__u16) (230400L / baudrate);
  1521. /* Check for round off */
  1522. round1 = (__u16) (2304000L / baudrate);
  1523. round = (__u16) (round1 - (custom * 10));
  1524. if (round > 4) {
  1525. custom++;
  1526. }
  1527. *divisor = custom;
  1528. dbg(" Baud %d = %d\n", baudrate, custom);
  1529. return 0;
  1530. }
  1531. dbg("%s\n", " Baud calculation Failed...");
  1532. return -1;
  1533. #endif
  1534. }
  1535. /*****************************************************************************
  1536. * mos7840_send_cmd_write_baud_rate
  1537. * this function sends the proper command to change the baud rate of the
  1538. * specified port.
  1539. *****************************************************************************/
  1540. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  1541. int baudRate)
  1542. {
  1543. int divisor = 0;
  1544. int status;
  1545. __u16 Data;
  1546. unsigned char number;
  1547. __u16 clk_sel_val;
  1548. struct usb_serial_port *port;
  1549. if (mos7840_port == NULL)
  1550. return -1;
  1551. port = (struct usb_serial_port *)mos7840_port->port;
  1552. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1553. dbg("%s", "Invalid port \n");
  1554. return -1;
  1555. }
  1556. if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
  1557. dbg("%s", "Invalid Serial \n");
  1558. return -1;
  1559. }
  1560. dbg("%s", "Entering .......... \n");
  1561. number = mos7840_port->port->number - mos7840_port->port->serial->minor;
  1562. dbg("%s - port = %d, baud = %d", __FUNCTION__,
  1563. mos7840_port->port->number, baudRate);
  1564. //reset clk_uart_sel in spregOffset
  1565. if (baudRate > 115200) {
  1566. #ifdef HW_flow_control
  1567. //NOTE: need to see the pther register to modify
  1568. //setting h/w flow control bit to 1;
  1569. status = 0;
  1570. Data = 0x2b;
  1571. mos7840_port->shadowMCR = Data;
  1572. status =
  1573. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1574. if (status < 0) {
  1575. dbg("Writing spreg failed in set_serial_baud\n");
  1576. return -1;
  1577. }
  1578. #endif
  1579. } else {
  1580. #ifdef HW_flow_control
  1581. //setting h/w flow control bit to 0;
  1582. status = 0;
  1583. Data = 0xb;
  1584. mos7840_port->shadowMCR = Data;
  1585. status =
  1586. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1587. if (status < 0) {
  1588. dbg("Writing spreg failed in set_serial_baud\n");
  1589. return -1;
  1590. }
  1591. #endif
  1592. }
  1593. if (1) //baudRate <= 115200)
  1594. {
  1595. clk_sel_val = 0x0;
  1596. Data = 0x0;
  1597. status = 0;
  1598. status =
  1599. mos7840_calc_baud_rate_divisor(baudRate, &divisor,
  1600. &clk_sel_val);
  1601. status =
  1602. mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  1603. &Data);
  1604. if (status < 0) {
  1605. dbg("reading spreg failed in set_serial_baud\n");
  1606. return -1;
  1607. }
  1608. Data = (Data & 0x8f) | clk_sel_val;
  1609. status = 0;
  1610. status =
  1611. mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  1612. if (status < 0) {
  1613. dbg("Writing spreg failed in set_serial_baud\n");
  1614. return -1;
  1615. }
  1616. /* Calculate the Divisor */
  1617. if (status) {
  1618. err("%s - bad baud rate", __FUNCTION__);
  1619. dbg("%s\n", "bad baud rate");
  1620. return status;
  1621. }
  1622. /* Enable access to divisor latch */
  1623. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  1624. mos7840_port->shadowLCR = Data;
  1625. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1626. /* Write the divisor */
  1627. Data = (unsigned char)(divisor & 0xff);
  1628. dbg("set_serial_baud Value to write DLL is %x\n", Data);
  1629. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  1630. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1631. dbg("set_serial_baud Value to write DLM is %x\n", Data);
  1632. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1633. /* Disable access to divisor latch */
  1634. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1635. mos7840_port->shadowLCR = Data;
  1636. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1637. }
  1638. return status;
  1639. }
  1640. /*****************************************************************************
  1641. * mos7840_change_port_settings
  1642. * This routine is called to set the UART on the device to match
  1643. * the specified new settings.
  1644. *****************************************************************************/
  1645. static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
  1646. struct ktermios *old_termios)
  1647. {
  1648. struct tty_struct *tty;
  1649. int baud;
  1650. unsigned cflag;
  1651. unsigned iflag;
  1652. __u8 lData;
  1653. __u8 lParity;
  1654. __u8 lStop;
  1655. int status;
  1656. __u16 Data;
  1657. struct usb_serial_port *port;
  1658. struct usb_serial *serial;
  1659. if (mos7840_port == NULL)
  1660. return;
  1661. port = (struct usb_serial_port *)mos7840_port->port;
  1662. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1663. dbg("%s", "Invalid port \n");
  1664. return;
  1665. }
  1666. if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
  1667. dbg("%s", "Invalid Serial \n");
  1668. return;
  1669. }
  1670. serial = port->serial;
  1671. dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
  1672. if (!mos7840_port->open) {
  1673. dbg("%s - port not opened", __FUNCTION__);
  1674. return;
  1675. }
  1676. tty = mos7840_port->port->tty;
  1677. if ((!tty) || (!tty->termios)) {
  1678. dbg("%s - no tty structures", __FUNCTION__);
  1679. return;
  1680. }
  1681. dbg("%s", "Entering .......... \n");
  1682. lData = LCR_BITS_8;
  1683. lStop = LCR_STOP_1;
  1684. lParity = LCR_PAR_NONE;
  1685. cflag = tty->termios->c_cflag;
  1686. iflag = tty->termios->c_iflag;
  1687. /* Change the number of bits */
  1688. if (cflag & CSIZE) {
  1689. switch (cflag & CSIZE) {
  1690. case CS5:
  1691. lData = LCR_BITS_5;
  1692. break;
  1693. case CS6:
  1694. lData = LCR_BITS_6;
  1695. break;
  1696. case CS7:
  1697. lData = LCR_BITS_7;
  1698. break;
  1699. default:
  1700. case CS8:
  1701. lData = LCR_BITS_8;
  1702. break;
  1703. }
  1704. }
  1705. /* Change the Parity bit */
  1706. if (cflag & PARENB) {
  1707. if (cflag & PARODD) {
  1708. lParity = LCR_PAR_ODD;
  1709. dbg("%s - parity = odd", __FUNCTION__);
  1710. } else {
  1711. lParity = LCR_PAR_EVEN;
  1712. dbg("%s - parity = even", __FUNCTION__);
  1713. }
  1714. } else {
  1715. dbg("%s - parity = none", __FUNCTION__);
  1716. }
  1717. if (cflag & CMSPAR) {
  1718. lParity = lParity | 0x20;
  1719. }
  1720. /* Change the Stop bit */
  1721. if (cflag & CSTOPB) {
  1722. lStop = LCR_STOP_2;
  1723. dbg("%s - stop bits = 2", __FUNCTION__);
  1724. } else {
  1725. lStop = LCR_STOP_1;
  1726. dbg("%s - stop bits = 1", __FUNCTION__);
  1727. }
  1728. /* Update the LCR with the correct value */
  1729. mos7840_port->shadowLCR &=
  1730. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1731. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1732. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
  1733. mos7840_port->shadowLCR);
  1734. /* Disable Interrupts */
  1735. Data = 0x00;
  1736. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1737. Data = 0x00;
  1738. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1739. Data = 0xcf;
  1740. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1741. /* Send the updated LCR value to the mos7840 */
  1742. Data = mos7840_port->shadowLCR;
  1743. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1744. Data = 0x00b;
  1745. mos7840_port->shadowMCR = Data;
  1746. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1747. Data = 0x00b;
  1748. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1749. /* set up the MCR register and send it to the mos7840 */
  1750. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1751. if (cflag & CBAUD) {
  1752. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1753. }
  1754. if (cflag & CRTSCTS) {
  1755. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1756. } else {
  1757. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1758. }
  1759. Data = mos7840_port->shadowMCR;
  1760. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1761. /* Determine divisor based on baud rate */
  1762. baud = tty_get_baud_rate(tty);
  1763. if (!baud) {
  1764. /* pick a default, any default... */
  1765. dbg("%s\n", "Picked default baud...");
  1766. baud = 9600;
  1767. }
  1768. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  1769. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1770. /* Enable Interrupts */
  1771. Data = 0x0c;
  1772. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1773. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1774. mos7840_port->read_urb->dev = serial->dev;
  1775. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1776. if (status) {
  1777. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1778. status);
  1779. }
  1780. }
  1781. wake_up(&mos7840_port->delta_msr_wait);
  1782. mos7840_port->delta_msr_cond = 1;
  1783. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
  1784. mos7840_port->shadowLCR);
  1785. return;
  1786. }
  1787. /*****************************************************************************
  1788. * mos7840_set_termios
  1789. * this function is called by the tty driver when it wants to change
  1790. * the termios structure
  1791. *****************************************************************************/
  1792. static void mos7840_set_termios(struct usb_serial_port *port,
  1793. struct ktermios *old_termios)
  1794. {
  1795. int status;
  1796. unsigned int cflag;
  1797. struct usb_serial *serial;
  1798. struct moschip_port *mos7840_port;
  1799. struct tty_struct *tty;
  1800. dbg("mos7840_set_termios: START\n");
  1801. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1802. dbg("%s", "Invalid port \n");
  1803. return;
  1804. }
  1805. serial = port->serial;
  1806. if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
  1807. dbg("%s", "Invalid Serial \n");
  1808. return;
  1809. }
  1810. mos7840_port = mos7840_get_port_private(port);
  1811. if (mos7840_port == NULL)
  1812. return;
  1813. tty = port->tty;
  1814. if (!port->tty || !port->tty->termios) {
  1815. dbg("%s - no tty or termios", __FUNCTION__);
  1816. return;
  1817. }
  1818. if (!mos7840_port->open) {
  1819. dbg("%s - port not opened", __FUNCTION__);
  1820. return;
  1821. }
  1822. dbg("%s\n", "setting termios - ");
  1823. cflag = tty->termios->c_cflag;
  1824. if (!cflag) {
  1825. dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
  1826. return;
  1827. }
  1828. dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
  1829. tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
  1830. if (old_termios) {
  1831. dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
  1832. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1833. }
  1834. dbg("%s - port %d", __FUNCTION__, port->number);
  1835. /* change the port settings to the new ones specified */
  1836. mos7840_change_port_settings(mos7840_port, old_termios);
  1837. if (!mos7840_port->read_urb) {
  1838. dbg("%s", "URB KILLED !!!!!\n");
  1839. return;
  1840. }
  1841. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1842. mos7840_port->read_urb->dev = serial->dev;
  1843. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1844. if (status) {
  1845. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1846. status);
  1847. }
  1848. }
  1849. return;
  1850. }
  1851. /*****************************************************************************
  1852. * mos7840_get_lsr_info - get line status register info
  1853. *
  1854. * Purpose: Let user call ioctl() to get info when the UART physically
  1855. * is emptied. On bus types like RS485, the transmitter must
  1856. * release the bus after transmitting. This must be done when
  1857. * the transmit shift register is empty, not be done when the
  1858. * transmit holding register is empty. This functionality
  1859. * allows an RS485 driver to be written in user space.
  1860. *****************************************************************************/
  1861. static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
  1862. unsigned int __user *value)
  1863. {
  1864. int count;
  1865. unsigned int result = 0;
  1866. count = mos7840_chars_in_buffer(mos7840_port->port);
  1867. if (count == 0) {
  1868. dbg("%s -- Empty", __FUNCTION__);
  1869. result = TIOCSER_TEMT;
  1870. }
  1871. if (copy_to_user(value, &result, sizeof(int)))
  1872. return -EFAULT;
  1873. return 0;
  1874. }
  1875. /*****************************************************************************
  1876. * mos7840_set_modem_info
  1877. * function to set modem info
  1878. *****************************************************************************/
  1879. static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
  1880. unsigned int cmd, unsigned int __user *value)
  1881. {
  1882. unsigned int mcr;
  1883. unsigned int arg;
  1884. __u16 Data;
  1885. int status;
  1886. struct usb_serial_port *port;
  1887. if (mos7840_port == NULL)
  1888. return -1;
  1889. port = (struct usb_serial_port *)mos7840_port->port;
  1890. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1891. dbg("%s", "Invalid port \n");
  1892. return -1;
  1893. }
  1894. mcr = mos7840_port->shadowMCR;
  1895. if (copy_from_user(&arg, value, sizeof(int)))
  1896. return -EFAULT;
  1897. switch (cmd) {
  1898. case TIOCMBIS:
  1899. if (arg & TIOCM_RTS)
  1900. mcr |= MCR_RTS;
  1901. if (arg & TIOCM_DTR)
  1902. mcr |= MCR_RTS;
  1903. if (arg & TIOCM_LOOP)
  1904. mcr |= MCR_LOOPBACK;
  1905. break;
  1906. case TIOCMBIC:
  1907. if (arg & TIOCM_RTS)
  1908. mcr &= ~MCR_RTS;
  1909. if (arg & TIOCM_DTR)
  1910. mcr &= ~MCR_RTS;
  1911. if (arg & TIOCM_LOOP)
  1912. mcr &= ~MCR_LOOPBACK;
  1913. break;
  1914. case TIOCMSET:
  1915. /* turn off the RTS and DTR and LOOPBACK
  1916. * and then only turn on what was asked to */
  1917. mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
  1918. mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
  1919. mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
  1920. mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
  1921. break;
  1922. }
  1923. mos7840_port->shadowMCR = mcr;
  1924. Data = mos7840_port->shadowMCR;
  1925. status = 0;
  1926. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1927. if (status < 0) {
  1928. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1929. return -1;
  1930. }
  1931. return 0;
  1932. }
  1933. /*****************************************************************************
  1934. * mos7840_get_modem_info
  1935. * function to get modem info
  1936. *****************************************************************************/
  1937. static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
  1938. unsigned int __user *value)
  1939. {
  1940. unsigned int result = 0;
  1941. __u16 msr;
  1942. unsigned int mcr = mos7840_port->shadowMCR;
  1943. int status = 0;
  1944. status =
  1945. mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
  1946. &msr);
  1947. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1948. |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1949. |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1950. |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
  1951. |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1952. |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1953. dbg("%s -- %x", __FUNCTION__, result);
  1954. if (copy_to_user(value, &result, sizeof(int)))
  1955. return -EFAULT;
  1956. return 0;
  1957. }
  1958. /*****************************************************************************
  1959. * mos7840_get_serial_info
  1960. * function to get information about serial port
  1961. *****************************************************************************/
  1962. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1963. struct serial_struct __user *retinfo)
  1964. {
  1965. struct serial_struct tmp;
  1966. if (mos7840_port == NULL)
  1967. return -1;
  1968. if (!retinfo)
  1969. return -EFAULT;
  1970. memset(&tmp, 0, sizeof(tmp));
  1971. tmp.type = PORT_16550A;
  1972. tmp.line = mos7840_port->port->serial->minor;
  1973. tmp.port = mos7840_port->port->number;
  1974. tmp.irq = 0;
  1975. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1976. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1977. tmp.baud_base = 9600;
  1978. tmp.close_delay = 5 * HZ;
  1979. tmp.closing_wait = 30 * HZ;
  1980. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1981. return -EFAULT;
  1982. return 0;
  1983. }
  1984. /*****************************************************************************
  1985. * SerialIoctl
  1986. * this function handles any ioctl calls to the driver
  1987. *****************************************************************************/
  1988. static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
  1989. unsigned int cmd, unsigned long arg)
  1990. {
  1991. void __user *argp = (void __user *)arg;
  1992. struct moschip_port *mos7840_port;
  1993. struct tty_struct *tty;
  1994. struct async_icount cnow;
  1995. struct async_icount cprev;
  1996. struct serial_icounter_struct icount;
  1997. int mosret = 0;
  1998. if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
  1999. dbg("%s", "Invalid port \n");
  2000. return -1;
  2001. }
  2002. mos7840_port = mos7840_get_port_private(port);
  2003. if (mos7840_port == NULL)
  2004. return -1;
  2005. tty = mos7840_port->port->tty;
  2006. dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
  2007. switch (cmd) {
  2008. /* return number of bytes available */
  2009. case TIOCSERGETLSR:
  2010. dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
  2011. return mos7840_get_lsr_info(mos7840_port, argp);
  2012. return 0;
  2013. case TIOCMBIS:
  2014. case TIOCMBIC:
  2015. case TIOCMSET:
  2016. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
  2017. port->number);
  2018. mosret =
  2019. mos7840_set_modem_info(mos7840_port, cmd, argp);
  2020. return mosret;
  2021. case TIOCMGET:
  2022. dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
  2023. return mos7840_get_modem_info(mos7840_port, argp);
  2024. case TIOCGSERIAL:
  2025. dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
  2026. return mos7840_get_serial_info(mos7840_port, argp);
  2027. case TIOCSSERIAL:
  2028. dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
  2029. break;
  2030. case TIOCMIWAIT:
  2031. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  2032. cprev = mos7840_port->icount;
  2033. while (1) {
  2034. //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
  2035. mos7840_port->delta_msr_cond = 0;
  2036. wait_event_interruptible(mos7840_port->delta_msr_wait,
  2037. (mos7840_port->
  2038. delta_msr_cond == 1));
  2039. /* see if a signal did it */
  2040. if (signal_pending(current))
  2041. return -ERESTARTSYS;
  2042. cnow = mos7840_port->icount;
  2043. smp_rmb();
  2044. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2045. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  2046. return -EIO; /* no change => error */
  2047. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  2048. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  2049. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  2050. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  2051. return 0;
  2052. }
  2053. cprev = cnow;
  2054. }
  2055. /* NOTREACHED */
  2056. break;
  2057. case TIOCGICOUNT:
  2058. cnow = mos7840_port->icount;
  2059. smp_rmb();
  2060. icount.cts = cnow.cts;
  2061. icount.dsr = cnow.dsr;
  2062. icount.rng = cnow.rng;
  2063. icount.dcd = cnow.dcd;
  2064. icount.rx = cnow.rx;
  2065. icount.tx = cnow.tx;
  2066. icount.frame = cnow.frame;
  2067. icount.overrun = cnow.overrun;
  2068. icount.parity = cnow.parity;
  2069. icount.brk = cnow.brk;
  2070. icount.buf_overrun = cnow.buf_overrun;
  2071. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
  2072. port->number, icount.rx, icount.tx);
  2073. if (copy_to_user(argp, &icount, sizeof(icount)))
  2074. return -EFAULT;
  2075. return 0;
  2076. case TIOCEXBAUD:
  2077. return 0;
  2078. default:
  2079. break;
  2080. }
  2081. return -ENOIOCTLCMD;
  2082. }
  2083. static int mos7840_calc_num_ports(struct usb_serial *serial)
  2084. {
  2085. int mos7840_num_ports = 0;
  2086. dbg("numberofendpoints: %d \n",
  2087. (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
  2088. dbg("numberofendpoints: %d \n",
  2089. (int)serial->interface->altsetting->desc.bNumEndpoints);
  2090. if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
  2091. mos7840_num_ports = serial->num_ports = 2;
  2092. } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
  2093. serial->num_bulk_in = 4;
  2094. serial->num_bulk_out = 4;
  2095. mos7840_num_ports = serial->num_ports = 4;
  2096. }
  2097. return mos7840_num_ports;
  2098. }
  2099. /****************************************************************************
  2100. * mos7840_startup
  2101. ****************************************************************************/
  2102. static int mos7840_startup(struct usb_serial *serial)
  2103. {
  2104. struct moschip_port *mos7840_port;
  2105. struct usb_device *dev;
  2106. int i, status;
  2107. __u16 Data;
  2108. dbg("%s \n", " mos7840_startup :entering..........");
  2109. if (!serial) {
  2110. dbg("%s\n", "Invalid Handler");
  2111. return -1;
  2112. }
  2113. dev = serial->dev;
  2114. dbg("%s\n", "Entering...");
  2115. /* we set up the pointers to the endpoints in the mos7840_open *
  2116. * function, as the structures aren't created yet. */
  2117. /* set up port private structures */
  2118. for (i = 0; i < serial->num_ports; ++i) {
  2119. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  2120. if (mos7840_port == NULL) {
  2121. err("%s - Out of memory", __FUNCTION__);
  2122. status = -ENOMEM;
  2123. i--; /* don't follow NULL pointer cleaning up */
  2124. goto error;
  2125. }
  2126. /* Initialize all port interrupt end point to port 0 int endpoint *
  2127. * Our device has only one interrupt end point comman to all port */
  2128. mos7840_port->port = serial->port[i];
  2129. mos7840_set_port_private(serial->port[i], mos7840_port);
  2130. spin_lock_init(&mos7840_port->pool_lock);
  2131. mos7840_port->port_num = ((serial->port[i]->number -
  2132. (serial->port[i]->serial->minor)) +
  2133. 1);
  2134. if (mos7840_port->port_num == 1) {
  2135. mos7840_port->SpRegOffset = 0x0;
  2136. mos7840_port->ControlRegOffset = 0x1;
  2137. mos7840_port->DcrRegOffset = 0x4;
  2138. } else if ((mos7840_port->port_num == 2)
  2139. && (serial->num_ports == 4)) {
  2140. mos7840_port->SpRegOffset = 0x8;
  2141. mos7840_port->ControlRegOffset = 0x9;
  2142. mos7840_port->DcrRegOffset = 0x16;
  2143. } else if ((mos7840_port->port_num == 2)
  2144. && (serial->num_ports == 2)) {
  2145. mos7840_port->SpRegOffset = 0xa;
  2146. mos7840_port->ControlRegOffset = 0xb;
  2147. mos7840_port->DcrRegOffset = 0x19;
  2148. } else if ((mos7840_port->port_num == 3)
  2149. && (serial->num_ports == 4)) {
  2150. mos7840_port->SpRegOffset = 0xa;
  2151. mos7840_port->ControlRegOffset = 0xb;
  2152. mos7840_port->DcrRegOffset = 0x19;
  2153. } else if ((mos7840_port->port_num == 4)
  2154. && (serial->num_ports == 4)) {
  2155. mos7840_port->SpRegOffset = 0xc;
  2156. mos7840_port->ControlRegOffset = 0xd;
  2157. mos7840_port->DcrRegOffset = 0x1c;
  2158. }
  2159. mos7840_dump_serial_port(mos7840_port);
  2160. mos7840_set_port_private(serial->port[i], mos7840_port);
  2161. //enable rx_disable bit in control register
  2162. status =
  2163. mos7840_get_reg_sync(serial->port[i],
  2164. mos7840_port->ControlRegOffset, &Data);
  2165. if (status < 0) {
  2166. dbg("Reading ControlReg failed status-0x%x\n", status);
  2167. break;
  2168. } else
  2169. dbg("ControlReg Reading success val is %x, status%d\n",
  2170. Data, status);
  2171. Data |= 0x08; //setting driver done bit
  2172. Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
  2173. //Data |= 0x20; //rx_disable bit
  2174. status = 0;
  2175. status =
  2176. mos7840_set_reg_sync(serial->port[i],
  2177. mos7840_port->ControlRegOffset, Data);
  2178. if (status < 0) {
  2179. dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  2180. break;
  2181. } else
  2182. dbg("ControlReg Writing success(rx_disable) status%d\n",
  2183. status);
  2184. //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
  2185. Data = 0x01;
  2186. status = 0;
  2187. status =
  2188. mos7840_set_reg_sync(serial->port[i],
  2189. (__u16) (mos7840_port->DcrRegOffset +
  2190. 0), Data);
  2191. if (status < 0) {
  2192. dbg("Writing DCR0 failed status-0x%x\n", status);
  2193. break;
  2194. } else
  2195. dbg("DCR0 Writing success status%d\n", status);
  2196. Data = 0x05;
  2197. status = 0;
  2198. status =
  2199. mos7840_set_reg_sync(serial->port[i],
  2200. (__u16) (mos7840_port->DcrRegOffset +
  2201. 1), Data);
  2202. if (status < 0) {
  2203. dbg("Writing DCR1 failed status-0x%x\n", status);
  2204. break;
  2205. } else
  2206. dbg("DCR1 Writing success status%d\n", status);
  2207. Data = 0x24;
  2208. status = 0;
  2209. status =
  2210. mos7840_set_reg_sync(serial->port[i],
  2211. (__u16) (mos7840_port->DcrRegOffset +
  2212. 2), Data);
  2213. if (status < 0) {
  2214. dbg("Writing DCR2 failed status-0x%x\n", status);
  2215. break;
  2216. } else
  2217. dbg("DCR2 Writing success status%d\n", status);
  2218. // write values in clkstart0x0 and clkmulti 0x20
  2219. Data = 0x0;
  2220. status = 0;
  2221. status =
  2222. mos7840_set_reg_sync(serial->port[i],
  2223. CLK_START_VALUE_REGISTER, Data);
  2224. if (status < 0) {
  2225. dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  2226. break;
  2227. } else
  2228. dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  2229. Data = 0x20;
  2230. status =
  2231. mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
  2232. Data);
  2233. if (status < 0) {
  2234. dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
  2235. status);
  2236. goto error;
  2237. } else
  2238. dbg("CLK_MULTI_REGISTER Writing success status%d\n",
  2239. status);
  2240. //write value 0x0 to scratchpad register
  2241. Data = 0x00;
  2242. status =
  2243. mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
  2244. Data);
  2245. if (status < 0) {
  2246. dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
  2247. status);
  2248. break;
  2249. } else
  2250. dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
  2251. status);
  2252. //Zero Length flag register
  2253. if ((mos7840_port->port_num != 1)
  2254. && (serial->num_ports == 2)) {
  2255. Data = 0xff;
  2256. status = 0;
  2257. status = mos7840_set_reg_sync(serial->port[i],
  2258. (__u16) (ZLP_REG1 +
  2259. ((__u16)
  2260. mos7840_port->
  2261. port_num)),
  2262. Data);
  2263. dbg("ZLIP offset%x\n",
  2264. (__u16) (ZLP_REG1 +
  2265. ((__u16) mos7840_port->port_num)));
  2266. if (status < 0) {
  2267. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2268. i + 2, status);
  2269. break;
  2270. } else
  2271. dbg("ZLP_REG%d Writing success status%d\n",
  2272. i + 2, status);
  2273. } else {
  2274. Data = 0xff;
  2275. status = 0;
  2276. status = mos7840_set_reg_sync(serial->port[i],
  2277. (__u16) (ZLP_REG1 +
  2278. ((__u16)
  2279. mos7840_port->
  2280. port_num) -
  2281. 0x1), Data);
  2282. dbg("ZLIP offset%x\n",
  2283. (__u16) (ZLP_REG1 +
  2284. ((__u16) mos7840_port->port_num) - 0x1));
  2285. if (status < 0) {
  2286. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2287. i + 1, status);
  2288. break;
  2289. } else
  2290. dbg("ZLP_REG%d Writing success status%d\n",
  2291. i + 1, status);
  2292. }
  2293. mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  2294. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  2295. mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  2296. if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
  2297. status = -ENOMEM;
  2298. goto error;
  2299. }
  2300. }
  2301. //Zero Length flag enable
  2302. Data = 0x0f;
  2303. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  2304. if (status < 0) {
  2305. dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
  2306. return -1;
  2307. } else
  2308. dbg("ZLP_REG5 Writing success status%d\n", status);
  2309. /* setting configuration feature to one */
  2310. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  2311. (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
  2312. return 0;
  2313. error:
  2314. for (/* nothing */; i >= 0; i--) {
  2315. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2316. kfree(mos7840_port->dr);
  2317. kfree(mos7840_port->ctrl_buf);
  2318. usb_free_urb(mos7840_port->control_urb);
  2319. kfree(mos7840_port);
  2320. serial->port[i] = NULL;
  2321. }
  2322. return status;
  2323. }
  2324. /****************************************************************************
  2325. * mos7840_shutdown
  2326. * This function is called whenever the device is removed from the usb bus.
  2327. ****************************************************************************/
  2328. static void mos7840_shutdown(struct usb_serial *serial)
  2329. {
  2330. int i;
  2331. unsigned long flags;
  2332. struct moschip_port *mos7840_port;
  2333. dbg("%s \n", " shutdown :entering..........");
  2334. if (!serial) {
  2335. dbg("%s", "Invalid Handler \n");
  2336. return;
  2337. }
  2338. /* check for the ports to be closed,close the ports and disconnect */
  2339. /* free private structure allocated for serial port *
  2340. * stop reads and writes on all ports */
  2341. for (i = 0; i < serial->num_ports; ++i) {
  2342. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2343. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  2344. mos7840_port->zombie = 1;
  2345. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  2346. usb_kill_urb(mos7840_port->control_urb);
  2347. kfree(mos7840_port->ctrl_buf);
  2348. kfree(mos7840_port->dr);
  2349. kfree(mos7840_port);
  2350. mos7840_set_port_private(serial->port[i], NULL);
  2351. }
  2352. dbg("%s\n", "Thank u :: ");
  2353. }
  2354. static struct usb_driver io_driver = {
  2355. .name = "mos7840",
  2356. .probe = usb_serial_probe,
  2357. .disconnect = usb_serial_disconnect,
  2358. .id_table = moschip_id_table_combined,
  2359. .no_dynamic_id = 1,
  2360. };
  2361. static struct usb_serial_driver moschip7840_4port_device = {
  2362. .driver = {
  2363. .owner = THIS_MODULE,
  2364. .name = "mos7840",
  2365. },
  2366. .description = DRIVER_DESC,
  2367. .usb_driver = &io_driver,
  2368. .id_table = moschip_port_id_table,
  2369. .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
  2370. #ifdef check
  2371. .num_bulk_in = 4,
  2372. .num_bulk_out = 4,
  2373. .num_ports = 4,
  2374. #endif
  2375. .open = mos7840_open,
  2376. .close = mos7840_close,
  2377. .write = mos7840_write,
  2378. .write_room = mos7840_write_room,
  2379. .chars_in_buffer = mos7840_chars_in_buffer,
  2380. .throttle = mos7840_throttle,
  2381. .unthrottle = mos7840_unthrottle,
  2382. .calc_num_ports = mos7840_calc_num_ports,
  2383. #ifdef MCSSerialProbe
  2384. .probe = mos7840_serial_probe,
  2385. #endif
  2386. .ioctl = mos7840_ioctl,
  2387. .set_termios = mos7840_set_termios,
  2388. .break_ctl = mos7840_break,
  2389. .tiocmget = mos7840_tiocmget,
  2390. .tiocmset = mos7840_tiocmset,
  2391. .attach = mos7840_startup,
  2392. .shutdown = mos7840_shutdown,
  2393. .read_bulk_callback = mos7840_bulk_in_callback,
  2394. .read_int_callback = mos7840_interrupt_callback,
  2395. };
  2396. /****************************************************************************
  2397. * moschip7840_init
  2398. * This is called by the module subsystem, or on startup to initialize us
  2399. ****************************************************************************/
  2400. static int __init moschip7840_init(void)
  2401. {
  2402. int retval;
  2403. dbg("%s \n", " mos7840_init :entering..........");
  2404. /* Register with the usb serial */
  2405. retval = usb_serial_register(&moschip7840_4port_device);
  2406. if (retval)
  2407. goto failed_port_device_register;
  2408. dbg("%s\n", "Entring...");
  2409. info(DRIVER_DESC " " DRIVER_VERSION);
  2410. /* Register with the usb */
  2411. retval = usb_register(&io_driver);
  2412. if (retval)
  2413. goto failed_usb_register;
  2414. if (retval == 0) {
  2415. dbg("%s\n", "Leaving...");
  2416. return 0;
  2417. }
  2418. failed_usb_register:
  2419. usb_serial_deregister(&moschip7840_4port_device);
  2420. failed_port_device_register:
  2421. return retval;
  2422. }
  2423. /****************************************************************************
  2424. * moschip7840_exit
  2425. * Called when the driver is about to be unloaded.
  2426. ****************************************************************************/
  2427. static void __exit moschip7840_exit(void)
  2428. {
  2429. dbg("%s \n", " mos7840_exit :entering..........");
  2430. usb_deregister(&io_driver);
  2431. usb_serial_deregister(&moschip7840_4port_device);
  2432. dbg("%s\n", "Entring...");
  2433. }
  2434. module_init(moschip7840_init);
  2435. module_exit(moschip7840_exit);
  2436. /* Module information */
  2437. MODULE_DESCRIPTION(DRIVER_DESC);
  2438. MODULE_LICENSE("GPL");
  2439. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2440. MODULE_PARM_DESC(debug, "Debug enabled or not");