mos7840.c 77 KB

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