mos7840.c 68 KB

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