mos7840.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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. dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
  404. mos7840_port->MsrLsr, mos7840_port->port_num);
  405. data = urb->transfer_buffer;
  406. regval = (__u8) data[0];
  407. dev_dbg(dev, "%s data is %x\n", __func__, regval);
  408. if (mos7840_port->MsrLsr == 0)
  409. mos7840_handle_new_msr(mos7840_port, regval);
  410. else if (mos7840_port->MsrLsr == 1)
  411. mos7840_handle_new_lsr(mos7840_port, regval);
  412. out:
  413. clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
  414. }
  415. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  416. __u16 *val)
  417. {
  418. struct usb_device *dev = mcs->port->serial->dev;
  419. struct usb_ctrlrequest *dr = mcs->dr;
  420. unsigned char *buffer = mcs->ctrl_buf;
  421. int ret;
  422. if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
  423. return -EBUSY;
  424. dr->bRequestType = MCS_RD_RTYPE;
  425. dr->bRequest = MCS_RDREQ;
  426. dr->wValue = cpu_to_le16(Wval); /* 0 */
  427. dr->wIndex = cpu_to_le16(reg);
  428. dr->wLength = cpu_to_le16(2);
  429. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  430. (unsigned char *)dr, buffer, 2,
  431. mos7840_control_callback, mcs);
  432. mcs->control_urb->transfer_buffer_length = 2;
  433. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  434. if (ret)
  435. clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
  436. return ret;
  437. }
  438. static void mos7840_set_led_callback(struct urb *urb)
  439. {
  440. switch (urb->status) {
  441. case 0:
  442. /* Success */
  443. break;
  444. case -ECONNRESET:
  445. case -ENOENT:
  446. case -ESHUTDOWN:
  447. /* This urb is terminated, clean up */
  448. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  449. __func__, urb->status);
  450. break;
  451. default:
  452. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  453. __func__, urb->status);
  454. }
  455. }
  456. static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
  457. __u16 reg)
  458. {
  459. struct usb_device *dev = mcs->port->serial->dev;
  460. struct usb_ctrlrequest *dr = mcs->led_dr;
  461. dr->bRequestType = MCS_WR_RTYPE;
  462. dr->bRequest = MCS_WRREQ;
  463. dr->wValue = cpu_to_le16(wval);
  464. dr->wIndex = cpu_to_le16(reg);
  465. dr->wLength = cpu_to_le16(0);
  466. usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
  467. (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
  468. usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
  469. }
  470. static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
  471. __u16 val)
  472. {
  473. struct usb_device *dev = port->serial->dev;
  474. usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
  475. val, reg, NULL, 0, MOS_WDR_TIMEOUT);
  476. }
  477. static void mos7840_led_off(struct timer_list *t)
  478. {
  479. struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
  480. /* Turn off LED */
  481. mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
  482. mod_timer(&mcs->led_timer2,
  483. jiffies + msecs_to_jiffies(LED_OFF_MS));
  484. }
  485. static void mos7840_led_flag_off(struct timer_list *t)
  486. {
  487. struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
  488. clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
  489. }
  490. static void mos7840_led_activity(struct usb_serial_port *port)
  491. {
  492. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  493. if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
  494. return;
  495. mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
  496. mod_timer(&mos7840_port->led_timer1,
  497. jiffies + msecs_to_jiffies(LED_ON_MS));
  498. }
  499. /*****************************************************************************
  500. * mos7840_interrupt_callback
  501. * this is the callback function for when we have received data on the
  502. * interrupt endpoint.
  503. *****************************************************************************/
  504. static void mos7840_interrupt_callback(struct urb *urb)
  505. {
  506. int result;
  507. int length;
  508. struct moschip_port *mos7840_port;
  509. struct usb_serial *serial;
  510. __u16 Data;
  511. unsigned char *data;
  512. __u8 sp[5], st;
  513. int i, rv = 0;
  514. __u16 wval, wreg = 0;
  515. int status = urb->status;
  516. switch (status) {
  517. case 0:
  518. /* success */
  519. break;
  520. case -ECONNRESET:
  521. case -ENOENT:
  522. case -ESHUTDOWN:
  523. /* this urb is terminated, clean up */
  524. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  525. __func__, status);
  526. return;
  527. default:
  528. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
  529. __func__, status);
  530. goto exit;
  531. }
  532. length = urb->actual_length;
  533. data = urb->transfer_buffer;
  534. serial = urb->context;
  535. /* Moschip get 5 bytes
  536. * Byte 1 IIR Port 1 (port.number is 0)
  537. * Byte 2 IIR Port 2 (port.number is 1)
  538. * Byte 3 IIR Port 3 (port.number is 2)
  539. * Byte 4 IIR Port 4 (port.number is 3)
  540. * Byte 5 FIFO status for both */
  541. if (length > 5) {
  542. dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
  543. return;
  544. }
  545. sp[0] = (__u8) data[0];
  546. sp[1] = (__u8) data[1];
  547. sp[2] = (__u8) data[2];
  548. sp[3] = (__u8) data[3];
  549. st = (__u8) data[4];
  550. for (i = 0; i < serial->num_ports; i++) {
  551. mos7840_port = mos7840_get_port_private(serial->port[i]);
  552. wval = ((__u16)serial->port[i]->port_number + 1) << 8;
  553. if (mos7840_port->open) {
  554. if (sp[i] & 0x01) {
  555. dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
  556. } else {
  557. switch (sp[i] & 0x0f) {
  558. case SERIAL_IIR_RLS:
  559. dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
  560. dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
  561. mos7840_port->MsrLsr = 1;
  562. wreg = LINE_STATUS_REGISTER;
  563. break;
  564. case SERIAL_IIR_MS:
  565. dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
  566. mos7840_port->MsrLsr = 0;
  567. wreg = MODEM_STATUS_REGISTER;
  568. break;
  569. }
  570. rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
  571. }
  572. }
  573. }
  574. if (!(rv < 0))
  575. /* the completion handler for the control urb will resubmit */
  576. return;
  577. exit:
  578. result = usb_submit_urb(urb, GFP_ATOMIC);
  579. if (result) {
  580. dev_err(&urb->dev->dev,
  581. "%s - Error %d submitting interrupt urb\n",
  582. __func__, result);
  583. }
  584. }
  585. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  586. const char *function)
  587. {
  588. if (!port) {
  589. pr_debug("%s - port == NULL\n", function);
  590. return -1;
  591. }
  592. if (!port->serial) {
  593. pr_debug("%s - port->serial == NULL\n", function);
  594. return -1;
  595. }
  596. return 0;
  597. }
  598. /* Inline functions to check the sanity of a pointer that is passed to us */
  599. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  600. const char *function)
  601. {
  602. if (!serial) {
  603. pr_debug("%s - serial == NULL\n", function);
  604. return -1;
  605. }
  606. if (!serial->type) {
  607. pr_debug("%s - serial->type == NULL!\n", function);
  608. return -1;
  609. }
  610. return 0;
  611. }
  612. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  613. const char *function)
  614. {
  615. /* if no port was specified, or it fails a paranoia check */
  616. if (!port ||
  617. mos7840_port_paranoia_check(port, function) ||
  618. mos7840_serial_paranoia_check(port->serial, function)) {
  619. /* then say that we don't have a valid usb_serial thing,
  620. * which will end up genrating -ENODEV return values */
  621. return NULL;
  622. }
  623. return port->serial;
  624. }
  625. /*****************************************************************************
  626. * mos7840_bulk_in_callback
  627. * this is the callback function for when we have received data on the
  628. * bulk in endpoint.
  629. *****************************************************************************/
  630. static void mos7840_bulk_in_callback(struct urb *urb)
  631. {
  632. int retval;
  633. unsigned char *data;
  634. struct usb_serial *serial;
  635. struct usb_serial_port *port;
  636. struct moschip_port *mos7840_port;
  637. int status = urb->status;
  638. mos7840_port = urb->context;
  639. if (!mos7840_port)
  640. return;
  641. if (status) {
  642. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  643. mos7840_port->read_urb_busy = false;
  644. return;
  645. }
  646. port = mos7840_port->port;
  647. if (mos7840_port_paranoia_check(port, __func__)) {
  648. mos7840_port->read_urb_busy = false;
  649. return;
  650. }
  651. serial = mos7840_get_usb_serial(port, __func__);
  652. if (!serial) {
  653. mos7840_port->read_urb_busy = false;
  654. return;
  655. }
  656. data = urb->transfer_buffer;
  657. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  658. if (urb->actual_length) {
  659. struct tty_port *tport = &mos7840_port->port->port;
  660. tty_insert_flip_string(tport, data, urb->actual_length);
  661. tty_flip_buffer_push(tport);
  662. port->icount.rx += urb->actual_length;
  663. dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
  664. }
  665. if (!mos7840_port->read_urb) {
  666. dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
  667. mos7840_port->read_urb_busy = false;
  668. return;
  669. }
  670. if (mos7840_port->has_led)
  671. mos7840_led_activity(port);
  672. mos7840_port->read_urb_busy = true;
  673. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  674. if (retval) {
  675. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  676. mos7840_port->read_urb_busy = false;
  677. }
  678. }
  679. /*****************************************************************************
  680. * mos7840_bulk_out_data_callback
  681. * this is the callback function for when we have finished sending
  682. * serial data on the bulk out endpoint.
  683. *****************************************************************************/
  684. static void mos7840_bulk_out_data_callback(struct urb *urb)
  685. {
  686. struct moschip_port *mos7840_port;
  687. struct usb_serial_port *port;
  688. int status = urb->status;
  689. int i;
  690. mos7840_port = urb->context;
  691. port = mos7840_port->port;
  692. spin_lock(&mos7840_port->pool_lock);
  693. for (i = 0; i < NUM_URBS; i++) {
  694. if (urb == mos7840_port->write_urb_pool[i]) {
  695. mos7840_port->busy[i] = 0;
  696. break;
  697. }
  698. }
  699. spin_unlock(&mos7840_port->pool_lock);
  700. if (status) {
  701. dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
  702. return;
  703. }
  704. if (mos7840_port_paranoia_check(port, __func__))
  705. return;
  706. if (mos7840_port->open)
  707. tty_port_tty_wakeup(&port->port);
  708. }
  709. /************************************************************************/
  710. /* 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 */
  711. /************************************************************************/
  712. /*****************************************************************************
  713. * mos7840_open
  714. * this function is called by the tty driver when a port is opened
  715. * If successful, we return 0
  716. * Otherwise we return a negative error number.
  717. *****************************************************************************/
  718. static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
  719. {
  720. int response;
  721. int j;
  722. struct usb_serial *serial;
  723. struct urb *urb;
  724. __u16 Data;
  725. int status;
  726. struct moschip_port *mos7840_port;
  727. struct moschip_port *port0;
  728. if (mos7840_port_paranoia_check(port, __func__))
  729. return -ENODEV;
  730. serial = port->serial;
  731. if (mos7840_serial_paranoia_check(serial, __func__))
  732. return -ENODEV;
  733. mos7840_port = mos7840_get_port_private(port);
  734. port0 = mos7840_get_port_private(serial->port[0]);
  735. if (mos7840_port == NULL || port0 == NULL)
  736. return -ENODEV;
  737. usb_clear_halt(serial->dev, port->write_urb->pipe);
  738. usb_clear_halt(serial->dev, port->read_urb->pipe);
  739. port0->open_ports++;
  740. /* Initialising the write urb pool */
  741. for (j = 0; j < NUM_URBS; ++j) {
  742. urb = usb_alloc_urb(0, GFP_KERNEL);
  743. mos7840_port->write_urb_pool[j] = urb;
  744. if (!urb)
  745. continue;
  746. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  747. GFP_KERNEL);
  748. if (!urb->transfer_buffer) {
  749. usb_free_urb(urb);
  750. mos7840_port->write_urb_pool[j] = NULL;
  751. continue;
  752. }
  753. }
  754. /*****************************************************************************
  755. * Initialize MCS7840 -- Write Init values to corresponding Registers
  756. *
  757. * Register Index
  758. * 1 : IER
  759. * 2 : FCR
  760. * 3 : LCR
  761. * 4 : MCR
  762. *
  763. * 0x08 : SP1/2 Control Reg
  764. *****************************************************************************/
  765. /* NEED to check the following Block */
  766. Data = 0x0;
  767. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  768. if (status < 0) {
  769. dev_dbg(&port->dev, "Reading Spreg failed\n");
  770. goto err;
  771. }
  772. Data |= 0x80;
  773. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  774. if (status < 0) {
  775. dev_dbg(&port->dev, "writing 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. /* End of block to be checked */
  785. Data = 0x0;
  786. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  787. &Data);
  788. if (status < 0) {
  789. dev_dbg(&port->dev, "Reading Controlreg failed\n");
  790. goto err;
  791. }
  792. Data |= 0x08; /* Driver done bit */
  793. Data |= 0x20; /* rx_disable */
  794. status = mos7840_set_reg_sync(port,
  795. mos7840_port->ControlRegOffset, Data);
  796. if (status < 0) {
  797. dev_dbg(&port->dev, "writing Controlreg failed\n");
  798. goto err;
  799. }
  800. /* do register settings here */
  801. /* Set all regs to the device default values. */
  802. /***********************************
  803. * First Disable all interrupts.
  804. ***********************************/
  805. Data = 0x00;
  806. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  807. if (status < 0) {
  808. dev_dbg(&port->dev, "disabling interrupts failed\n");
  809. goto err;
  810. }
  811. /* Set FIFO_CONTROL_REGISTER to the default value */
  812. Data = 0x00;
  813. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  814. if (status < 0) {
  815. dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
  816. goto err;
  817. }
  818. Data = 0xcf;
  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 = 0x03;
  825. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  826. mos7840_port->shadowLCR = Data;
  827. Data = 0x0b;
  828. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  829. mos7840_port->shadowMCR = Data;
  830. Data = 0x00;
  831. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  832. mos7840_port->shadowLCR = Data;
  833. Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
  834. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  835. Data = 0x0c;
  836. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  837. Data = 0x0;
  838. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  839. Data = 0x00;
  840. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  841. Data = Data & ~SERIAL_LCR_DLAB;
  842. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  843. mos7840_port->shadowLCR = Data;
  844. /* clearing Bulkin and Bulkout Fifo */
  845. Data = 0x0;
  846. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  847. Data = Data | 0x0c;
  848. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  849. Data = Data & ~0x0c;
  850. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  851. /* Finally enable all interrupts */
  852. Data = 0x0c;
  853. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  854. /* clearing rx_disable */
  855. Data = 0x0;
  856. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  857. &Data);
  858. Data = Data & ~0x20;
  859. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  860. Data);
  861. /* rx_negate */
  862. Data = 0x0;
  863. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  864. &Data);
  865. Data = Data | 0x10;
  866. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  867. Data);
  868. /* Check to see if we've set up our endpoint info yet *
  869. * (can't set it up in mos7840_startup as the structures *
  870. * were not set up at that time.) */
  871. if (port0->open_ports == 1) {
  872. /* FIXME: Buffer never NULL, so URB is not submitted. */
  873. if (serial->port[0]->interrupt_in_buffer == NULL) {
  874. /* set up interrupt urb */
  875. usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
  876. serial->dev,
  877. usb_rcvintpipe(serial->dev,
  878. serial->port[0]->interrupt_in_endpointAddress),
  879. serial->port[0]->interrupt_in_buffer,
  880. serial->port[0]->interrupt_in_urb->
  881. transfer_buffer_length,
  882. mos7840_interrupt_callback,
  883. serial,
  884. serial->port[0]->interrupt_in_urb->interval);
  885. /* start interrupt read for mos7840 */
  886. response =
  887. usb_submit_urb(serial->port[0]->interrupt_in_urb,
  888. GFP_KERNEL);
  889. if (response) {
  890. dev_err(&port->dev, "%s - Error %d submitting "
  891. "interrupt urb\n", __func__, response);
  892. }
  893. }
  894. }
  895. /* see if we've set up our endpoint info yet *
  896. * (can't set it up in mos7840_startup as the *
  897. * structures were not set up at that time.) */
  898. dev_dbg(&port->dev, "port number is %d\n", port->port_number);
  899. dev_dbg(&port->dev, "minor number is %d\n", port->minor);
  900. dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
  901. dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
  902. dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
  903. dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
  904. mos7840_port->read_urb = port->read_urb;
  905. /* set up our bulk in urb */
  906. if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
  907. usb_fill_bulk_urb(mos7840_port->read_urb,
  908. serial->dev,
  909. usb_rcvbulkpipe(serial->dev,
  910. (port->bulk_in_endpointAddress) + 2),
  911. port->bulk_in_buffer,
  912. mos7840_port->read_urb->transfer_buffer_length,
  913. mos7840_bulk_in_callback, mos7840_port);
  914. } else {
  915. usb_fill_bulk_urb(mos7840_port->read_urb,
  916. serial->dev,
  917. usb_rcvbulkpipe(serial->dev,
  918. port->bulk_in_endpointAddress),
  919. port->bulk_in_buffer,
  920. mos7840_port->read_urb->transfer_buffer_length,
  921. mos7840_bulk_in_callback, mos7840_port);
  922. }
  923. dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
  924. mos7840_port->read_urb_busy = true;
  925. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  926. if (response) {
  927. dev_err(&port->dev, "%s - Error %d submitting control urb\n",
  928. __func__, response);
  929. mos7840_port->read_urb_busy = false;
  930. }
  931. /* initialize our port settings */
  932. /* Must set to enable ints! */
  933. mos7840_port->shadowMCR = MCR_MASTER_IE;
  934. /* send a open port command */
  935. mos7840_port->open = 1;
  936. /* mos7840_change_port_settings(mos7840_port,old_termios); */
  937. return 0;
  938. err:
  939. for (j = 0; j < NUM_URBS; ++j) {
  940. urb = mos7840_port->write_urb_pool[j];
  941. if (!urb)
  942. continue;
  943. kfree(urb->transfer_buffer);
  944. usb_free_urb(urb);
  945. }
  946. return status;
  947. }
  948. /*****************************************************************************
  949. * mos7840_chars_in_buffer
  950. * this function is called by the tty driver when it wants to know how many
  951. * bytes of data we currently have outstanding in the port (data that has
  952. * been written, but hasn't made it out the port yet)
  953. * If successful, we return the number of bytes left to be written in the
  954. * system,
  955. * Otherwise we return zero.
  956. *****************************************************************************/
  957. static int mos7840_chars_in_buffer(struct tty_struct *tty)
  958. {
  959. struct usb_serial_port *port = tty->driver_data;
  960. int i;
  961. int chars = 0;
  962. unsigned long flags;
  963. struct moschip_port *mos7840_port;
  964. if (mos7840_port_paranoia_check(port, __func__))
  965. return 0;
  966. mos7840_port = mos7840_get_port_private(port);
  967. if (mos7840_port == NULL)
  968. return 0;
  969. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  970. for (i = 0; i < NUM_URBS; ++i) {
  971. if (mos7840_port->busy[i]) {
  972. struct urb *urb = mos7840_port->write_urb_pool[i];
  973. chars += urb->transfer_buffer_length;
  974. }
  975. }
  976. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  977. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  978. return chars;
  979. }
  980. /*****************************************************************************
  981. * mos7840_close
  982. * this function is called by the tty driver when a port is closed
  983. *****************************************************************************/
  984. static void mos7840_close(struct usb_serial_port *port)
  985. {
  986. struct usb_serial *serial;
  987. struct moschip_port *mos7840_port;
  988. struct moschip_port *port0;
  989. int j;
  990. __u16 Data;
  991. if (mos7840_port_paranoia_check(port, __func__))
  992. return;
  993. serial = mos7840_get_usb_serial(port, __func__);
  994. if (!serial)
  995. return;
  996. mos7840_port = mos7840_get_port_private(port);
  997. port0 = mos7840_get_port_private(serial->port[0]);
  998. if (mos7840_port == NULL || port0 == NULL)
  999. return;
  1000. for (j = 0; j < NUM_URBS; ++j)
  1001. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  1002. /* Freeing Write URBs */
  1003. for (j = 0; j < NUM_URBS; ++j) {
  1004. if (mos7840_port->write_urb_pool[j]) {
  1005. kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
  1006. usb_free_urb(mos7840_port->write_urb_pool[j]);
  1007. }
  1008. }
  1009. usb_kill_urb(mos7840_port->read_urb);
  1010. mos7840_port->read_urb_busy = false;
  1011. port0->open_ports--;
  1012. dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
  1013. if (port0->open_ports == 0) {
  1014. if (serial->port[0]->interrupt_in_urb) {
  1015. dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
  1016. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1017. }
  1018. }
  1019. Data = 0x0;
  1020. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1021. Data = 0x00;
  1022. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1023. mos7840_port->open = 0;
  1024. }
  1025. /*****************************************************************************
  1026. * mos7840_break
  1027. * this function sends a break to the port
  1028. *****************************************************************************/
  1029. static void mos7840_break(struct tty_struct *tty, int break_state)
  1030. {
  1031. struct usb_serial_port *port = tty->driver_data;
  1032. unsigned char data;
  1033. struct usb_serial *serial;
  1034. struct moschip_port *mos7840_port;
  1035. if (mos7840_port_paranoia_check(port, __func__))
  1036. return;
  1037. serial = mos7840_get_usb_serial(port, __func__);
  1038. if (!serial)
  1039. return;
  1040. mos7840_port = mos7840_get_port_private(port);
  1041. if (mos7840_port == NULL)
  1042. return;
  1043. if (break_state == -1)
  1044. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  1045. else
  1046. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  1047. /* FIXME: no locking on shadowLCR anywhere in driver */
  1048. mos7840_port->shadowLCR = data;
  1049. dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
  1050. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  1051. mos7840_port->shadowLCR);
  1052. }
  1053. /*****************************************************************************
  1054. * mos7840_write_room
  1055. * this function is called by the tty driver when it wants to know how many
  1056. * bytes of data we can accept for a specific port.
  1057. * If successful, we return the amount of room that we have for this port
  1058. * Otherwise we return a negative error number.
  1059. *****************************************************************************/
  1060. static int mos7840_write_room(struct tty_struct *tty)
  1061. {
  1062. struct usb_serial_port *port = tty->driver_data;
  1063. int i;
  1064. int room = 0;
  1065. unsigned long flags;
  1066. struct moschip_port *mos7840_port;
  1067. if (mos7840_port_paranoia_check(port, __func__))
  1068. return -1;
  1069. mos7840_port = mos7840_get_port_private(port);
  1070. if (mos7840_port == NULL)
  1071. return -1;
  1072. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1073. for (i = 0; i < NUM_URBS; ++i) {
  1074. if (!mos7840_port->busy[i])
  1075. room += URB_TRANSFER_BUFFER_SIZE;
  1076. }
  1077. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1078. room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
  1079. dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
  1080. return room;
  1081. }
  1082. /*****************************************************************************
  1083. * mos7840_write
  1084. * this function is called by the tty driver when data should be written to
  1085. * the port.
  1086. * If successful, we return the number of bytes written, otherwise we
  1087. * return a negative error number.
  1088. *****************************************************************************/
  1089. static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
  1090. const unsigned char *data, int count)
  1091. {
  1092. int status;
  1093. int i;
  1094. int bytes_sent = 0;
  1095. int transfer_size;
  1096. unsigned long flags;
  1097. struct moschip_port *mos7840_port;
  1098. struct usb_serial *serial;
  1099. struct urb *urb;
  1100. /* __u16 Data; */
  1101. const unsigned char *current_position = data;
  1102. unsigned char *data1;
  1103. if (mos7840_port_paranoia_check(port, __func__))
  1104. return -1;
  1105. serial = port->serial;
  1106. if (mos7840_serial_paranoia_check(serial, __func__))
  1107. return -1;
  1108. mos7840_port = mos7840_get_port_private(port);
  1109. if (mos7840_port == NULL)
  1110. return -1;
  1111. /* try to find a free urb in the list */
  1112. urb = NULL;
  1113. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1114. for (i = 0; i < NUM_URBS; ++i) {
  1115. if (!mos7840_port->busy[i]) {
  1116. mos7840_port->busy[i] = 1;
  1117. urb = mos7840_port->write_urb_pool[i];
  1118. dev_dbg(&port->dev, "URB:%d\n", i);
  1119. break;
  1120. }
  1121. }
  1122. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1123. if (urb == NULL) {
  1124. dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
  1125. goto exit;
  1126. }
  1127. if (urb->transfer_buffer == NULL) {
  1128. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  1129. GFP_ATOMIC);
  1130. if (!urb->transfer_buffer)
  1131. goto exit;
  1132. }
  1133. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1134. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1135. /* fill urb with data and submit */
  1136. if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
  1137. usb_fill_bulk_urb(urb,
  1138. serial->dev,
  1139. usb_sndbulkpipe(serial->dev,
  1140. (port->bulk_out_endpointAddress) + 2),
  1141. urb->transfer_buffer,
  1142. transfer_size,
  1143. mos7840_bulk_out_data_callback, mos7840_port);
  1144. } else {
  1145. usb_fill_bulk_urb(urb,
  1146. serial->dev,
  1147. usb_sndbulkpipe(serial->dev,
  1148. port->bulk_out_endpointAddress),
  1149. urb->transfer_buffer,
  1150. transfer_size,
  1151. mos7840_bulk_out_data_callback, mos7840_port);
  1152. }
  1153. data1 = urb->transfer_buffer;
  1154. dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
  1155. if (mos7840_port->has_led)
  1156. mos7840_led_activity(port);
  1157. /* send it down the pipe */
  1158. status = usb_submit_urb(urb, GFP_ATOMIC);
  1159. if (status) {
  1160. mos7840_port->busy[i] = 0;
  1161. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  1162. "with status = %d\n", __func__, status);
  1163. bytes_sent = status;
  1164. goto exit;
  1165. }
  1166. bytes_sent = transfer_size;
  1167. port->icount.tx += transfer_size;
  1168. dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
  1169. exit:
  1170. return bytes_sent;
  1171. }
  1172. /*****************************************************************************
  1173. * mos7840_throttle
  1174. * this function is called by the tty driver when it wants to stop the data
  1175. * being read from the port.
  1176. *****************************************************************************/
  1177. static void mos7840_throttle(struct tty_struct *tty)
  1178. {
  1179. struct usb_serial_port *port = tty->driver_data;
  1180. struct moschip_port *mos7840_port;
  1181. int status;
  1182. if (mos7840_port_paranoia_check(port, __func__))
  1183. return;
  1184. mos7840_port = mos7840_get_port_private(port);
  1185. if (mos7840_port == NULL)
  1186. return;
  1187. if (!mos7840_port->open) {
  1188. dev_dbg(&port->dev, "%s", "port not opened\n");
  1189. return;
  1190. }
  1191. /* if we are implementing XON/XOFF, send the stop character */
  1192. if (I_IXOFF(tty)) {
  1193. unsigned char stop_char = STOP_CHAR(tty);
  1194. status = mos7840_write(tty, port, &stop_char, 1);
  1195. if (status <= 0)
  1196. return;
  1197. }
  1198. /* if we are implementing RTS/CTS, toggle that line */
  1199. if (C_CRTSCTS(tty)) {
  1200. mos7840_port->shadowMCR &= ~MCR_RTS;
  1201. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1202. mos7840_port->shadowMCR);
  1203. if (status < 0)
  1204. return;
  1205. }
  1206. }
  1207. /*****************************************************************************
  1208. * mos7840_unthrottle
  1209. * this function is called by the tty driver when it wants to resume
  1210. * the data being read from the port (called after mos7840_throttle is
  1211. * called)
  1212. *****************************************************************************/
  1213. static void mos7840_unthrottle(struct tty_struct *tty)
  1214. {
  1215. struct usb_serial_port *port = tty->driver_data;
  1216. int status;
  1217. struct moschip_port *mos7840_port = mos7840_get_port_private(port);
  1218. if (mos7840_port_paranoia_check(port, __func__))
  1219. return;
  1220. if (mos7840_port == NULL)
  1221. return;
  1222. if (!mos7840_port->open) {
  1223. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1224. return;
  1225. }
  1226. /* if we are implementing XON/XOFF, send the start character */
  1227. if (I_IXOFF(tty)) {
  1228. unsigned char start_char = START_CHAR(tty);
  1229. status = mos7840_write(tty, port, &start_char, 1);
  1230. if (status <= 0)
  1231. return;
  1232. }
  1233. /* if we are implementing RTS/CTS, toggle that line */
  1234. if (C_CRTSCTS(tty)) {
  1235. mos7840_port->shadowMCR |= MCR_RTS;
  1236. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1237. mos7840_port->shadowMCR);
  1238. if (status < 0)
  1239. return;
  1240. }
  1241. }
  1242. static int mos7840_tiocmget(struct tty_struct *tty)
  1243. {
  1244. struct usb_serial_port *port = tty->driver_data;
  1245. struct moschip_port *mos7840_port;
  1246. unsigned int result;
  1247. __u16 msr;
  1248. __u16 mcr;
  1249. int status;
  1250. mos7840_port = mos7840_get_port_private(port);
  1251. if (mos7840_port == NULL)
  1252. return -ENODEV;
  1253. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  1254. if (status < 0)
  1255. return -EIO;
  1256. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1257. if (status < 0)
  1258. return -EIO;
  1259. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  1260. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  1261. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  1262. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  1263. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  1264. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  1265. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  1266. dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
  1267. return result;
  1268. }
  1269. static int mos7840_tiocmset(struct tty_struct *tty,
  1270. unsigned int set, unsigned int clear)
  1271. {
  1272. struct usb_serial_port *port = tty->driver_data;
  1273. struct moschip_port *mos7840_port;
  1274. unsigned int mcr;
  1275. int status;
  1276. mos7840_port = mos7840_get_port_private(port);
  1277. if (mos7840_port == NULL)
  1278. return -ENODEV;
  1279. /* FIXME: What locks the port registers ? */
  1280. mcr = mos7840_port->shadowMCR;
  1281. if (clear & TIOCM_RTS)
  1282. mcr &= ~MCR_RTS;
  1283. if (clear & TIOCM_DTR)
  1284. mcr &= ~MCR_DTR;
  1285. if (clear & TIOCM_LOOP)
  1286. mcr &= ~MCR_LOOPBACK;
  1287. if (set & TIOCM_RTS)
  1288. mcr |= MCR_RTS;
  1289. if (set & TIOCM_DTR)
  1290. mcr |= MCR_DTR;
  1291. if (set & TIOCM_LOOP)
  1292. mcr |= MCR_LOOPBACK;
  1293. mos7840_port->shadowMCR = mcr;
  1294. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  1295. if (status < 0) {
  1296. dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
  1297. return status;
  1298. }
  1299. return 0;
  1300. }
  1301. /*****************************************************************************
  1302. * mos7840_calc_baud_rate_divisor
  1303. * this function calculates the proper baud rate divisor for the specified
  1304. * baud rate.
  1305. *****************************************************************************/
  1306. static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
  1307. int baudRate, int *divisor,
  1308. __u16 *clk_sel_val)
  1309. {
  1310. dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
  1311. if (baudRate <= 115200) {
  1312. *divisor = 115200 / baudRate;
  1313. *clk_sel_val = 0x0;
  1314. }
  1315. if ((baudRate > 115200) && (baudRate <= 230400)) {
  1316. *divisor = 230400 / baudRate;
  1317. *clk_sel_val = 0x10;
  1318. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  1319. *divisor = 403200 / baudRate;
  1320. *clk_sel_val = 0x20;
  1321. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  1322. *divisor = 460800 / baudRate;
  1323. *clk_sel_val = 0x30;
  1324. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  1325. *divisor = 806400 / baudRate;
  1326. *clk_sel_val = 0x40;
  1327. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  1328. *divisor = 921600 / baudRate;
  1329. *clk_sel_val = 0x50;
  1330. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  1331. *divisor = 1572864 / baudRate;
  1332. *clk_sel_val = 0x60;
  1333. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  1334. *divisor = 3145728 / baudRate;
  1335. *clk_sel_val = 0x70;
  1336. }
  1337. return 0;
  1338. }
  1339. /*****************************************************************************
  1340. * mos7840_send_cmd_write_baud_rate
  1341. * this function sends the proper command to change the baud rate of the
  1342. * specified port.
  1343. *****************************************************************************/
  1344. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  1345. int baudRate)
  1346. {
  1347. int divisor = 0;
  1348. int status;
  1349. __u16 Data;
  1350. unsigned char number;
  1351. __u16 clk_sel_val;
  1352. struct usb_serial_port *port;
  1353. if (mos7840_port == NULL)
  1354. return -1;
  1355. port = mos7840_port->port;
  1356. if (mos7840_port_paranoia_check(port, __func__))
  1357. return -1;
  1358. if (mos7840_serial_paranoia_check(port->serial, __func__))
  1359. return -1;
  1360. number = mos7840_port->port->port_number;
  1361. dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
  1362. /* reset clk_uart_sel in spregOffset */
  1363. if (baudRate > 115200) {
  1364. #ifdef HW_flow_control
  1365. /* NOTE: need to see the pther register to modify */
  1366. /* setting h/w flow control bit to 1 */
  1367. Data = 0x2b;
  1368. mos7840_port->shadowMCR = Data;
  1369. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1370. Data);
  1371. if (status < 0) {
  1372. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  1373. return -1;
  1374. }
  1375. #endif
  1376. } else {
  1377. #ifdef HW_flow_control
  1378. /* setting h/w flow control bit to 0 */
  1379. Data = 0xb;
  1380. mos7840_port->shadowMCR = Data;
  1381. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1382. Data);
  1383. if (status < 0) {
  1384. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  1385. return -1;
  1386. }
  1387. #endif
  1388. }
  1389. if (1) { /* baudRate <= 115200) */
  1390. clk_sel_val = 0x0;
  1391. Data = 0x0;
  1392. status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
  1393. &clk_sel_val);
  1394. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  1395. &Data);
  1396. if (status < 0) {
  1397. dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
  1398. return -1;
  1399. }
  1400. Data = (Data & 0x8f) | clk_sel_val;
  1401. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
  1402. Data);
  1403. if (status < 0) {
  1404. dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
  1405. return -1;
  1406. }
  1407. /* Calculate the Divisor */
  1408. if (status) {
  1409. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1410. return status;
  1411. }
  1412. /* Enable access to divisor latch */
  1413. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  1414. mos7840_port->shadowLCR = Data;
  1415. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1416. /* Write the divisor */
  1417. Data = (unsigned char)(divisor & 0xff);
  1418. dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
  1419. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  1420. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1421. dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
  1422. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1423. /* Disable access to divisor latch */
  1424. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1425. mos7840_port->shadowLCR = Data;
  1426. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1427. }
  1428. return status;
  1429. }
  1430. /*****************************************************************************
  1431. * mos7840_change_port_settings
  1432. * This routine is called to set the UART on the device to match
  1433. * the specified new settings.
  1434. *****************************************************************************/
  1435. static void mos7840_change_port_settings(struct tty_struct *tty,
  1436. struct moschip_port *mos7840_port, struct ktermios *old_termios)
  1437. {
  1438. int baud;
  1439. unsigned cflag;
  1440. unsigned iflag;
  1441. __u8 lData;
  1442. __u8 lParity;
  1443. __u8 lStop;
  1444. int status;
  1445. __u16 Data;
  1446. struct usb_serial_port *port;
  1447. struct usb_serial *serial;
  1448. if (mos7840_port == NULL)
  1449. return;
  1450. port = mos7840_port->port;
  1451. if (mos7840_port_paranoia_check(port, __func__))
  1452. return;
  1453. if (mos7840_serial_paranoia_check(port->serial, __func__))
  1454. return;
  1455. serial = port->serial;
  1456. if (!mos7840_port->open) {
  1457. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1458. return;
  1459. }
  1460. lData = LCR_BITS_8;
  1461. lStop = LCR_STOP_1;
  1462. lParity = LCR_PAR_NONE;
  1463. cflag = tty->termios.c_cflag;
  1464. iflag = tty->termios.c_iflag;
  1465. /* Change the number of bits */
  1466. switch (cflag & CSIZE) {
  1467. case CS5:
  1468. lData = LCR_BITS_5;
  1469. break;
  1470. case CS6:
  1471. lData = LCR_BITS_6;
  1472. break;
  1473. case CS7:
  1474. lData = LCR_BITS_7;
  1475. break;
  1476. default:
  1477. case CS8:
  1478. lData = LCR_BITS_8;
  1479. break;
  1480. }
  1481. /* Change the Parity bit */
  1482. if (cflag & PARENB) {
  1483. if (cflag & PARODD) {
  1484. lParity = LCR_PAR_ODD;
  1485. dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
  1486. } else {
  1487. lParity = LCR_PAR_EVEN;
  1488. dev_dbg(&port->dev, "%s - parity = even\n", __func__);
  1489. }
  1490. } else {
  1491. dev_dbg(&port->dev, "%s - parity = none\n", __func__);
  1492. }
  1493. if (cflag & CMSPAR)
  1494. lParity = lParity | 0x20;
  1495. /* Change the Stop bit */
  1496. if (cflag & CSTOPB) {
  1497. lStop = LCR_STOP_2;
  1498. dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
  1499. } else {
  1500. lStop = LCR_STOP_1;
  1501. dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
  1502. }
  1503. /* Update the LCR with the correct value */
  1504. mos7840_port->shadowLCR &=
  1505. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1506. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1507. dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
  1508. mos7840_port->shadowLCR);
  1509. /* Disable Interrupts */
  1510. Data = 0x00;
  1511. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1512. Data = 0x00;
  1513. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1514. Data = 0xcf;
  1515. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1516. /* Send the updated LCR value to the mos7840 */
  1517. Data = mos7840_port->shadowLCR;
  1518. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1519. Data = 0x00b;
  1520. mos7840_port->shadowMCR = Data;
  1521. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1522. Data = 0x00b;
  1523. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1524. /* set up the MCR register and send it to the mos7840 */
  1525. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1526. if (cflag & CBAUD)
  1527. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1528. if (cflag & CRTSCTS)
  1529. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1530. else
  1531. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1532. Data = mos7840_port->shadowMCR;
  1533. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1534. /* Determine divisor based on baud rate */
  1535. baud = tty_get_baud_rate(tty);
  1536. if (!baud) {
  1537. /* pick a default, any default... */
  1538. dev_dbg(&port->dev, "%s", "Picked default baud...\n");
  1539. baud = 9600;
  1540. }
  1541. dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
  1542. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1543. /* Enable Interrupts */
  1544. Data = 0x0c;
  1545. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1546. if (!mos7840_port->read_urb_busy) {
  1547. mos7840_port->read_urb_busy = true;
  1548. status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  1549. if (status) {
  1550. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
  1551. status);
  1552. mos7840_port->read_urb_busy = false;
  1553. }
  1554. }
  1555. dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
  1556. mos7840_port->shadowLCR);
  1557. }
  1558. /*****************************************************************************
  1559. * mos7840_set_termios
  1560. * this function is called by the tty driver when it wants to change
  1561. * the termios structure
  1562. *****************************************************************************/
  1563. static void mos7840_set_termios(struct tty_struct *tty,
  1564. struct usb_serial_port *port,
  1565. struct ktermios *old_termios)
  1566. {
  1567. int status;
  1568. struct usb_serial *serial;
  1569. struct moschip_port *mos7840_port;
  1570. if (mos7840_port_paranoia_check(port, __func__))
  1571. return;
  1572. serial = port->serial;
  1573. if (mos7840_serial_paranoia_check(serial, __func__))
  1574. return;
  1575. mos7840_port = mos7840_get_port_private(port);
  1576. if (mos7840_port == NULL)
  1577. return;
  1578. if (!mos7840_port->open) {
  1579. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1580. return;
  1581. }
  1582. /* change the port settings to the new ones specified */
  1583. mos7840_change_port_settings(tty, mos7840_port, old_termios);
  1584. if (!mos7840_port->read_urb) {
  1585. dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
  1586. return;
  1587. }
  1588. if (!mos7840_port->read_urb_busy) {
  1589. mos7840_port->read_urb_busy = true;
  1590. status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  1591. if (status) {
  1592. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
  1593. status);
  1594. mos7840_port->read_urb_busy = false;
  1595. }
  1596. }
  1597. }
  1598. /*****************************************************************************
  1599. * mos7840_get_lsr_info - get line status register info
  1600. *
  1601. * Purpose: Let user call ioctl() to get info when the UART physically
  1602. * is emptied. On bus types like RS485, the transmitter must
  1603. * release the bus after transmitting. This must be done when
  1604. * the transmit shift register is empty, not be done when the
  1605. * transmit holding register is empty. This functionality
  1606. * allows an RS485 driver to be written in user space.
  1607. *****************************************************************************/
  1608. static int mos7840_get_lsr_info(struct tty_struct *tty,
  1609. unsigned int __user *value)
  1610. {
  1611. int count;
  1612. unsigned int result = 0;
  1613. count = mos7840_chars_in_buffer(tty);
  1614. if (count == 0)
  1615. result = TIOCSER_TEMT;
  1616. if (copy_to_user(value, &result, sizeof(int)))
  1617. return -EFAULT;
  1618. return 0;
  1619. }
  1620. /*****************************************************************************
  1621. * mos7840_get_serial_info
  1622. * function to get information about serial port
  1623. *****************************************************************************/
  1624. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1625. struct serial_struct __user *retinfo)
  1626. {
  1627. struct serial_struct tmp;
  1628. if (mos7840_port == NULL)
  1629. return -1;
  1630. memset(&tmp, 0, sizeof(tmp));
  1631. tmp.type = PORT_16550A;
  1632. tmp.line = mos7840_port->port->minor;
  1633. tmp.port = mos7840_port->port->port_number;
  1634. tmp.irq = 0;
  1635. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1636. tmp.baud_base = 9600;
  1637. tmp.close_delay = 5 * HZ;
  1638. tmp.closing_wait = 30 * HZ;
  1639. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1640. return -EFAULT;
  1641. return 0;
  1642. }
  1643. /*****************************************************************************
  1644. * SerialIoctl
  1645. * this function handles any ioctl calls to the driver
  1646. *****************************************************************************/
  1647. static int mos7840_ioctl(struct tty_struct *tty,
  1648. unsigned int cmd, unsigned long arg)
  1649. {
  1650. struct usb_serial_port *port = tty->driver_data;
  1651. void __user *argp = (void __user *)arg;
  1652. struct moschip_port *mos7840_port;
  1653. if (mos7840_port_paranoia_check(port, __func__))
  1654. return -1;
  1655. mos7840_port = mos7840_get_port_private(port);
  1656. if (mos7840_port == NULL)
  1657. return -1;
  1658. switch (cmd) {
  1659. /* return number of bytes available */
  1660. case TIOCSERGETLSR:
  1661. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1662. return mos7840_get_lsr_info(tty, argp);
  1663. case TIOCGSERIAL:
  1664. dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
  1665. return mos7840_get_serial_info(mos7840_port, argp);
  1666. case TIOCSSERIAL:
  1667. dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
  1668. break;
  1669. default:
  1670. break;
  1671. }
  1672. return -ENOIOCTLCMD;
  1673. }
  1674. static int mos7810_check(struct usb_serial *serial)
  1675. {
  1676. int i, pass_count = 0;
  1677. u8 *buf;
  1678. __u16 data = 0, mcr_data = 0;
  1679. __u16 test_pattern = 0x55AA;
  1680. int res;
  1681. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1682. if (!buf)
  1683. return 0; /* failed to identify 7810 */
  1684. /* Store MCR setting */
  1685. res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1686. MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
  1687. buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1688. if (res == VENDOR_READ_LENGTH)
  1689. mcr_data = *buf;
  1690. for (i = 0; i < 16; i++) {
  1691. /* Send the 1-bit test pattern out to MCS7810 test pin */
  1692. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1693. MCS_WRREQ, MCS_WR_RTYPE,
  1694. (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
  1695. MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
  1696. /* Read the test pattern back */
  1697. res = usb_control_msg(serial->dev,
  1698. usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
  1699. MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1700. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1701. if (res == VENDOR_READ_LENGTH)
  1702. data = *buf;
  1703. /* If this is a MCS7810 device, both test patterns must match */
  1704. if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
  1705. break;
  1706. pass_count++;
  1707. }
  1708. /* Restore MCR setting */
  1709. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
  1710. MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
  1711. 0, MOS_WDR_TIMEOUT);
  1712. kfree(buf);
  1713. if (pass_count == 16)
  1714. return 1;
  1715. return 0;
  1716. }
  1717. static int mos7840_probe(struct usb_serial *serial,
  1718. const struct usb_device_id *id)
  1719. {
  1720. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1721. u8 *buf;
  1722. int device_type;
  1723. if (product == MOSCHIP_DEVICE_ID_7810 ||
  1724. product == MOSCHIP_DEVICE_ID_7820) {
  1725. device_type = product;
  1726. goto out;
  1727. }
  1728. buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1729. if (!buf)
  1730. return -ENOMEM;
  1731. usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1732. MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1733. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1734. /* For a MCS7840 device GPIO0 must be set to 1 */
  1735. if (buf[0] & 0x01)
  1736. device_type = MOSCHIP_DEVICE_ID_7840;
  1737. else if (mos7810_check(serial))
  1738. device_type = MOSCHIP_DEVICE_ID_7810;
  1739. else
  1740. device_type = MOSCHIP_DEVICE_ID_7820;
  1741. kfree(buf);
  1742. out:
  1743. usb_set_serial_data(serial, (void *)(unsigned long)device_type);
  1744. return 0;
  1745. }
  1746. static int mos7840_calc_num_ports(struct usb_serial *serial,
  1747. struct usb_serial_endpoints *epds)
  1748. {
  1749. int device_type = (unsigned long)usb_get_serial_data(serial);
  1750. int num_ports;
  1751. num_ports = (device_type >> 4) & 0x000F;
  1752. /*
  1753. * num_ports is currently never zero as device_type is one of
  1754. * MOSCHIP_DEVICE_ID_78{1,2,4}0.
  1755. */
  1756. if (num_ports == 0)
  1757. return -ENODEV;
  1758. if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
  1759. dev_err(&serial->interface->dev, "missing endpoints\n");
  1760. return -ENODEV;
  1761. }
  1762. return num_ports;
  1763. }
  1764. static int mos7840_port_probe(struct usb_serial_port *port)
  1765. {
  1766. struct usb_serial *serial = port->serial;
  1767. int device_type = (unsigned long)usb_get_serial_data(serial);
  1768. struct moschip_port *mos7840_port;
  1769. int status;
  1770. int pnum;
  1771. __u16 Data;
  1772. /* we set up the pointers to the endpoints in the mos7840_open *
  1773. * function, as the structures aren't created yet. */
  1774. pnum = port->port_number;
  1775. dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
  1776. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1777. if (!mos7840_port)
  1778. return -ENOMEM;
  1779. /* Initialize all port interrupt end point to port 0 int
  1780. * endpoint. Our device has only one interrupt end point
  1781. * common to all port */
  1782. mos7840_port->port = port;
  1783. mos7840_set_port_private(port, mos7840_port);
  1784. spin_lock_init(&mos7840_port->pool_lock);
  1785. /* minor is not initialised until later by
  1786. * usb-serial.c:get_free_serial() and cannot therefore be used
  1787. * to index device instances */
  1788. mos7840_port->port_num = pnum + 1;
  1789. dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
  1790. dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
  1791. if (mos7840_port->port_num == 1) {
  1792. mos7840_port->SpRegOffset = 0x0;
  1793. mos7840_port->ControlRegOffset = 0x1;
  1794. mos7840_port->DcrRegOffset = 0x4;
  1795. } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
  1796. mos7840_port->SpRegOffset = 0x8;
  1797. mos7840_port->ControlRegOffset = 0x9;
  1798. mos7840_port->DcrRegOffset = 0x16;
  1799. } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
  1800. mos7840_port->SpRegOffset = 0xa;
  1801. mos7840_port->ControlRegOffset = 0xb;
  1802. mos7840_port->DcrRegOffset = 0x19;
  1803. } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
  1804. mos7840_port->SpRegOffset = 0xa;
  1805. mos7840_port->ControlRegOffset = 0xb;
  1806. mos7840_port->DcrRegOffset = 0x19;
  1807. } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
  1808. mos7840_port->SpRegOffset = 0xc;
  1809. mos7840_port->ControlRegOffset = 0xd;
  1810. mos7840_port->DcrRegOffset = 0x1c;
  1811. }
  1812. mos7840_dump_serial_port(port, mos7840_port);
  1813. mos7840_set_port_private(port, mos7840_port);
  1814. /* enable rx_disable bit in control register */
  1815. status = mos7840_get_reg_sync(port,
  1816. mos7840_port->ControlRegOffset, &Data);
  1817. if (status < 0) {
  1818. dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
  1819. goto out;
  1820. } else
  1821. dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
  1822. Data |= 0x08; /* setting driver done bit */
  1823. Data |= 0x04; /* sp1_bit to have cts change reflect in
  1824. modem status reg */
  1825. /* Data |= 0x20; //rx_disable bit */
  1826. status = mos7840_set_reg_sync(port,
  1827. mos7840_port->ControlRegOffset, Data);
  1828. if (status < 0) {
  1829. dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  1830. goto out;
  1831. } else
  1832. dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
  1833. /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
  1834. and 0x24 in DCR3 */
  1835. Data = 0x01;
  1836. status = mos7840_set_reg_sync(port,
  1837. (__u16) (mos7840_port->DcrRegOffset + 0), Data);
  1838. if (status < 0) {
  1839. dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
  1840. goto out;
  1841. } else
  1842. dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
  1843. Data = 0x05;
  1844. status = mos7840_set_reg_sync(port,
  1845. (__u16) (mos7840_port->DcrRegOffset + 1), Data);
  1846. if (status < 0) {
  1847. dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
  1848. goto out;
  1849. } else
  1850. dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
  1851. Data = 0x24;
  1852. status = mos7840_set_reg_sync(port,
  1853. (__u16) (mos7840_port->DcrRegOffset + 2), Data);
  1854. if (status < 0) {
  1855. dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
  1856. goto out;
  1857. } else
  1858. dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
  1859. /* write values in clkstart0x0 and clkmulti 0x20 */
  1860. Data = 0x0;
  1861. status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
  1862. if (status < 0) {
  1863. dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  1864. goto out;
  1865. } else
  1866. dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  1867. Data = 0x20;
  1868. status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
  1869. if (status < 0) {
  1870. dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
  1871. goto error;
  1872. } else
  1873. dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
  1874. /* write value 0x0 to scratchpad register */
  1875. Data = 0x00;
  1876. status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
  1877. if (status < 0) {
  1878. dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
  1879. goto out;
  1880. } else
  1881. dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
  1882. /* Zero Length flag register */
  1883. if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
  1884. Data = 0xff;
  1885. status = mos7840_set_reg_sync(port,
  1886. (__u16) (ZLP_REG1 +
  1887. ((__u16)mos7840_port->port_num)), Data);
  1888. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1889. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
  1890. if (status < 0) {
  1891. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
  1892. goto out;
  1893. } else
  1894. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
  1895. } else {
  1896. Data = 0xff;
  1897. status = mos7840_set_reg_sync(port,
  1898. (__u16) (ZLP_REG1 +
  1899. ((__u16)mos7840_port->port_num) - 0x1), Data);
  1900. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1901. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
  1902. if (status < 0) {
  1903. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
  1904. goto out;
  1905. } else
  1906. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
  1907. }
  1908. mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  1909. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  1910. mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
  1911. GFP_KERNEL);
  1912. if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
  1913. !mos7840_port->dr) {
  1914. status = -ENOMEM;
  1915. goto error;
  1916. }
  1917. mos7840_port->has_led = false;
  1918. /* Initialize LED timers */
  1919. if (device_type == MOSCHIP_DEVICE_ID_7810) {
  1920. mos7840_port->has_led = true;
  1921. mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
  1922. mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
  1923. GFP_KERNEL);
  1924. if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
  1925. status = -ENOMEM;
  1926. goto error;
  1927. }
  1928. timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
  1929. mos7840_port->led_timer1.expires =
  1930. jiffies + msecs_to_jiffies(LED_ON_MS);
  1931. timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
  1932. 0);
  1933. mos7840_port->led_timer2.expires =
  1934. jiffies + msecs_to_jiffies(LED_OFF_MS);
  1935. /* Turn off LED */
  1936. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1937. }
  1938. out:
  1939. if (pnum == serial->num_ports - 1) {
  1940. /* Zero Length flag enable */
  1941. Data = 0x0f;
  1942. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  1943. if (status < 0) {
  1944. dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
  1945. goto error;
  1946. } else
  1947. dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
  1948. /* setting configuration feature to one */
  1949. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1950. 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
  1951. MOS_WDR_TIMEOUT);
  1952. }
  1953. return 0;
  1954. error:
  1955. kfree(mos7840_port->led_dr);
  1956. usb_free_urb(mos7840_port->led_urb);
  1957. kfree(mos7840_port->dr);
  1958. kfree(mos7840_port->ctrl_buf);
  1959. usb_free_urb(mos7840_port->control_urb);
  1960. kfree(mos7840_port);
  1961. return status;
  1962. }
  1963. static int mos7840_port_remove(struct usb_serial_port *port)
  1964. {
  1965. struct moschip_port *mos7840_port;
  1966. mos7840_port = mos7840_get_port_private(port);
  1967. if (mos7840_port->has_led) {
  1968. /* Turn off LED */
  1969. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1970. del_timer_sync(&mos7840_port->led_timer1);
  1971. del_timer_sync(&mos7840_port->led_timer2);
  1972. usb_kill_urb(mos7840_port->led_urb);
  1973. usb_free_urb(mos7840_port->led_urb);
  1974. kfree(mos7840_port->led_dr);
  1975. }
  1976. usb_kill_urb(mos7840_port->control_urb);
  1977. usb_free_urb(mos7840_port->control_urb);
  1978. kfree(mos7840_port->ctrl_buf);
  1979. kfree(mos7840_port->dr);
  1980. kfree(mos7840_port);
  1981. return 0;
  1982. }
  1983. static struct usb_serial_driver moschip7840_4port_device = {
  1984. .driver = {
  1985. .owner = THIS_MODULE,
  1986. .name = "mos7840",
  1987. },
  1988. .description = DRIVER_DESC,
  1989. .id_table = id_table,
  1990. .num_interrupt_in = 1,
  1991. .open = mos7840_open,
  1992. .close = mos7840_close,
  1993. .write = mos7840_write,
  1994. .write_room = mos7840_write_room,
  1995. .chars_in_buffer = mos7840_chars_in_buffer,
  1996. .throttle = mos7840_throttle,
  1997. .unthrottle = mos7840_unthrottle,
  1998. .calc_num_ports = mos7840_calc_num_ports,
  1999. .probe = mos7840_probe,
  2000. .ioctl = mos7840_ioctl,
  2001. .set_termios = mos7840_set_termios,
  2002. .break_ctl = mos7840_break,
  2003. .tiocmget = mos7840_tiocmget,
  2004. .tiocmset = mos7840_tiocmset,
  2005. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2006. .get_icount = usb_serial_generic_get_icount,
  2007. .port_probe = mos7840_port_probe,
  2008. .port_remove = mos7840_port_remove,
  2009. .read_bulk_callback = mos7840_bulk_in_callback,
  2010. .read_int_callback = mos7840_interrupt_callback,
  2011. };
  2012. static struct usb_serial_driver * const serial_drivers[] = {
  2013. &moschip7840_4port_device, NULL
  2014. };
  2015. module_usb_serial_driver(serial_drivers, id_table);
  2016. MODULE_DESCRIPTION(DRIVER_DESC);
  2017. MODULE_LICENSE("GPL");