mos7840.c 78 KB

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