mos7840.c 78 KB

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