mos7840.c 72 KB

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