mos7840.c 77 KB

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