mos7840.c 68 KB

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