mos7840.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Clean ups from Moschip version and a few ioctl implementations by:
  17. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/serial.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/serial.h>
  34. #include <linux/uaccess.h>
  35. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  36. /*
  37. * 16C50 UART register defines
  38. */
  39. #define LCR_BITS_5 0x00 /* 5 bits/char */
  40. #define LCR_BITS_6 0x01 /* 6 bits/char */
  41. #define LCR_BITS_7 0x02 /* 7 bits/char */
  42. #define LCR_BITS_8 0x03 /* 8 bits/char */
  43. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  44. #define LCR_STOP_1 0x00 /* 1 stop bit */
  45. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  46. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  47. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  48. #define LCR_PAR_NONE 0x00 /* No parity */
  49. #define LCR_PAR_ODD 0x08 /* Odd parity */
  50. #define LCR_PAR_EVEN 0x18 /* Even parity */
  51. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  52. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  53. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  54. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  55. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  56. #define MCR_DTR 0x01 /* Assert DTR */
  57. #define MCR_RTS 0x02 /* Assert RTS */
  58. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  59. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  60. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  61. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  62. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  63. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  64. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  65. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  66. /*
  67. * Defines used for sending commands to port
  68. */
  69. #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
  70. #define MOS_PORT1 0x0200
  71. #define MOS_PORT2 0x0300
  72. #define MOS_VENREG 0x0000
  73. #define MOS_MAX_PORT 0x02
  74. #define MOS_WRITE 0x0E
  75. #define MOS_READ 0x0D
  76. /* Requests */
  77. #define MCS_RD_RTYPE 0xC0
  78. #define MCS_WR_RTYPE 0x40
  79. #define MCS_RDREQ 0x0D
  80. #define MCS_WRREQ 0x0E
  81. #define MCS_CTRL_TIMEOUT 500
  82. #define VENDOR_READ_LENGTH (0x01)
  83. #define MAX_NAME_LEN 64
  84. #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
  85. #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
  86. /* For higher baud Rates use TIOCEXBAUD */
  87. #define TIOCEXBAUD 0x5462
  88. /* vendor id and device id defines */
  89. /* The native mos7840/7820 component */
  90. #define USB_VENDOR_ID_MOSCHIP 0x9710
  91. #define MOSCHIP_DEVICE_ID_7840 0x7840
  92. #define MOSCHIP_DEVICE_ID_7820 0x7820
  93. #define MOSCHIP_DEVICE_ID_7810 0x7810
  94. /* The native component can have its vendor/device id's overridden
  95. * in vendor-specific implementations. Such devices can be handled
  96. * by making a change here, in id_table.
  97. */
  98. #define USB_VENDOR_ID_BANDB 0x0856
  99. #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
  100. #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
  101. #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
  102. #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
  103. #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
  104. #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
  105. #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
  106. #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
  107. #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
  108. #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
  109. #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
  110. #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
  111. #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
  112. /* This driver also supports
  113. * ATEN UC2324 device using Moschip MCS7840
  114. * ATEN UC2322 device using Moschip MCS7820
  115. */
  116. #define USB_VENDOR_ID_ATENINTL 0x0557
  117. #define ATENINTL_DEVICE_ID_UC2324 0x2011
  118. #define ATENINTL_DEVICE_ID_UC2322 0x7820
  119. /* Interrupt Routine Defines */
  120. #define SERIAL_IIR_RLS 0x06
  121. #define SERIAL_IIR_MS 0x00
  122. /*
  123. * Emulation of the bit mask on the LINE STATUS REGISTER.
  124. */
  125. #define SERIAL_LSR_DR 0x0001
  126. #define SERIAL_LSR_OE 0x0002
  127. #define SERIAL_LSR_PE 0x0004
  128. #define SERIAL_LSR_FE 0x0008
  129. #define SERIAL_LSR_BI 0x0010
  130. #define MOS_MSR_DELTA_CTS 0x10
  131. #define MOS_MSR_DELTA_DSR 0x20
  132. #define MOS_MSR_DELTA_RI 0x40
  133. #define MOS_MSR_DELTA_CD 0x80
  134. /* Serial Port register Address */
  135. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  136. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  137. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  138. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  139. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  140. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  141. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  142. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  143. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  144. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  145. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  146. #define GPIO_REGISTER ((__u16)(0x07))
  147. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  148. /*
  149. * URB POOL related defines
  150. */
  151. #define NUM_URBS 16 /* URB Count */
  152. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  153. /* LED on/off milliseconds*/
  154. #define LED_ON_MS 500
  155. #define LED_OFF_MS 500
  156. enum mos7840_flag {
  157. MOS7840_FLAG_CTRL_BUSY,
  158. MOS7840_FLAG_LED_BUSY,
  159. };
  160. static const struct usb_device_id id_table[] = {
  161. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  162. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  163. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
  164. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
  165. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
  166. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
  167. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
  168. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
  169. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
  170. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
  171. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
  172. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
  173. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
  174. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
  175. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
  176. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
  177. {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
  178. {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
  179. {} /* terminating entry */
  180. };
  181. MODULE_DEVICE_TABLE(usb, id_table);
  182. /* This structure holds all of the local port information */
  183. struct moschip_port {
  184. int port_num; /*Actual port number in the device(1,2,etc) */
  185. struct urb *read_urb; /* read URB for this port */
  186. __u8 shadowLCR; /* last LCR value received */
  187. __u8 shadowMCR; /* last MCR value received */
  188. char open;
  189. char open_ports;
  190. struct usb_serial_port *port; /* loop back to the owner of this object */
  191. /* Offsets */
  192. __u8 SpRegOffset;
  193. __u8 ControlRegOffset;
  194. __u8 DcrRegOffset;
  195. /* for processing control URBS in interrupt context */
  196. struct urb *control_urb;
  197. struct usb_ctrlrequest *dr;
  198. char *ctrl_buf;
  199. int MsrLsr;
  200. spinlock_t pool_lock;
  201. struct urb *write_urb_pool[NUM_URBS];
  202. char busy[NUM_URBS];
  203. bool read_urb_busy;
  204. /* For device(s) with LED indicator */
  205. bool has_led;
  206. struct timer_list led_timer1; /* Timer for LED on */
  207. struct timer_list led_timer2; /* Timer for LED off */
  208. struct urb *led_urb;
  209. struct usb_ctrlrequest *led_dr;
  210. unsigned long flags;
  211. };
  212. /*
  213. * mos7840_set_reg_sync
  214. * To set the Control register by calling usb_fill_control_urb function
  215. * by passing usb_sndctrlpipe function as parameter.
  216. */
  217. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  218. __u16 val)
  219. {
  220. struct usb_device *dev = port->serial->dev;
  221. val = val & 0x00ff;
  222. dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  223. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  224. MCS_WR_RTYPE, val, reg, NULL, 0,
  225. MOS_WDR_TIMEOUT);
  226. }
  227. /*
  228. * mos7840_get_reg_sync
  229. * To set the Uart register by calling usb_fill_control_urb function by
  230. * passing usb_rcvctrlpipe function as parameter.
  231. */
  232. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  233. __u16 *val)
  234. {
  235. struct usb_device *dev = port->serial->dev;
  236. int ret = 0;
  237. u8 *buf;
  238. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  239. if (!buf)
  240. return -ENOMEM;
  241. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  242. MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
  243. MOS_WDR_TIMEOUT);
  244. *val = buf[0];
  245. dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
  246. kfree(buf);
  247. return ret;
  248. }
  249. /*
  250. * mos7840_set_uart_reg
  251. * To set the Uart register by calling usb_fill_control_urb function by
  252. * passing usb_sndctrlpipe function as parameter.
  253. */
  254. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  255. __u16 val)
  256. {
  257. struct usb_device *dev = port->serial->dev;
  258. val = val & 0x00ff;
  259. /* For the UART control registers, the application number need
  260. to be Or'ed */
  261. if (port->serial->num_ports == 4) {
  262. val |= ((__u16)port->port_number + 1) << 8;
  263. } else {
  264. if (port->port_number == 0) {
  265. val |= ((__u16)port->port_number + 1) << 8;
  266. } else {
  267. val |= ((__u16)port->port_number + 2) << 8;
  268. }
  269. }
  270. dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
  271. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  272. MCS_WR_RTYPE, val, reg, NULL, 0,
  273. MOS_WDR_TIMEOUT);
  274. }
  275. /*
  276. * mos7840_get_uart_reg
  277. * To set the Control register by calling usb_fill_control_urb function
  278. * by passing usb_rcvctrlpipe function as parameter.
  279. */
  280. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  281. __u16 *val)
  282. {
  283. struct usb_device *dev = port->serial->dev;
  284. int ret = 0;
  285. __u16 Wval;
  286. u8 *buf;
  287. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  288. if (!buf)
  289. return -ENOMEM;
  290. /* Wval is same as application number */
  291. if (port->serial->num_ports == 4) {
  292. Wval = ((__u16)port->port_number + 1) << 8;
  293. } else {
  294. if (port->port_number == 0) {
  295. Wval = ((__u16)port->port_number + 1) << 8;
  296. } else {
  297. Wval = ((__u16)port->port_number + 2) << 8;
  298. }
  299. }
  300. dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
  301. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  302. MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
  303. MOS_WDR_TIMEOUT);
  304. *val = buf[0];
  305. kfree(buf);
  306. return ret;
  307. }
  308. static void mos7840_dump_serial_port(struct usb_serial_port *port,
  309. struct moschip_port *mos7840_port)
  310. {
  311. dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  312. dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
  313. dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
  314. }
  315. /************************************************************************/
  316. /************************************************************************/
  317. /* I N T E R F A C E F U N C T I O N S */
  318. /* I N T E R F A C E F U N C T I O N S */
  319. /************************************************************************/
  320. /************************************************************************/
  321. static inline void mos7840_set_port_private(struct usb_serial_port *port,
  322. struct moschip_port *data)
  323. {
  324. usb_set_serial_port_data(port, (void *)data);
  325. }
  326. static inline struct moschip_port *mos7840_get_port_private(struct
  327. usb_serial_port
  328. *port)
  329. {
  330. return (struct moschip_port *)usb_get_serial_port_data(port);
  331. }
  332. static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
  333. {
  334. struct moschip_port *mos7840_port;
  335. struct async_icount *icount;
  336. mos7840_port = port;
  337. if (new_msr &
  338. (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
  339. MOS_MSR_DELTA_CD)) {
  340. icount = &mos7840_port->port->icount;
  341. /* update input line counters */
  342. if (new_msr & MOS_MSR_DELTA_CTS)
  343. icount->cts++;
  344. if (new_msr & MOS_MSR_DELTA_DSR)
  345. icount->dsr++;
  346. if (new_msr & MOS_MSR_DELTA_CD)
  347. icount->dcd++;
  348. if (new_msr & MOS_MSR_DELTA_RI)
  349. icount->rng++;
  350. wake_up_interruptible(&port->port->port.delta_msr_wait);
  351. }
  352. }
  353. static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
  354. {
  355. struct async_icount *icount;
  356. if (new_lsr & SERIAL_LSR_BI) {
  357. /*
  358. * Parity and Framing errors only count if they
  359. * occur exclusive of a break being
  360. * received.
  361. */
  362. new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
  363. }
  364. /* update input line counters */
  365. icount = &port->port->icount;
  366. if (new_lsr & SERIAL_LSR_BI)
  367. icount->brk++;
  368. if (new_lsr & SERIAL_LSR_OE)
  369. icount->overrun++;
  370. if (new_lsr & SERIAL_LSR_PE)
  371. icount->parity++;
  372. if (new_lsr & SERIAL_LSR_FE)
  373. icount->frame++;
  374. }
  375. /************************************************************************/
  376. /************************************************************************/
  377. /* U S B C A L L B A C K F U N C T I O N S */
  378. /* U S B C A L L B A C K F U N C T I O N S */
  379. /************************************************************************/
  380. /************************************************************************/
  381. static void mos7840_control_callback(struct urb *urb)
  382. {
  383. unsigned char *data;
  384. struct moschip_port *mos7840_port;
  385. struct device *dev = &urb->dev->dev;
  386. __u8 regval = 0x0;
  387. int status = urb->status;
  388. mos7840_port = urb->context;
  389. switch (status) {
  390. case 0:
  391. /* success */
  392. break;
  393. case -ECONNRESET:
  394. case -ENOENT:
  395. case -ESHUTDOWN:
  396. /* this urb is terminated, clean up */
  397. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  398. goto out;
  399. default:
  400. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  401. goto out;
  402. }
  403. dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
  404. dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
  405. mos7840_port->MsrLsr, mos7840_port->port_num);
  406. data = urb->transfer_buffer;
  407. regval = (__u8) data[0];
  408. dev_dbg(dev, "%s data is %x\n", __func__, regval);
  409. if (mos7840_port->MsrLsr == 0)
  410. mos7840_handle_new_msr(mos7840_port, regval);
  411. else if (mos7840_port->MsrLsr == 1)
  412. mos7840_handle_new_lsr(mos7840_port, regval);
  413. out:
  414. clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
  415. }
  416. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  417. __u16 *val)
  418. {
  419. struct usb_device *dev = mcs->port->serial->dev;
  420. struct usb_ctrlrequest *dr = mcs->dr;
  421. unsigned char *buffer = mcs->ctrl_buf;
  422. int ret;
  423. if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
  424. return -EBUSY;
  425. dr->bRequestType = MCS_RD_RTYPE;
  426. dr->bRequest = MCS_RDREQ;
  427. dr->wValue = cpu_to_le16(Wval); /* 0 */
  428. dr->wIndex = cpu_to_le16(reg);
  429. dr->wLength = cpu_to_le16(2);
  430. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  431. (unsigned char *)dr, buffer, 2,
  432. mos7840_control_callback, mcs);
  433. mcs->control_urb->transfer_buffer_length = 2;
  434. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  435. if (ret)
  436. clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
  437. return ret;
  438. }
  439. static void mos7840_set_led_callback(struct urb *urb)
  440. {
  441. switch (urb->status) {
  442. case 0:
  443. /* Success */
  444. break;
  445. case -ECONNRESET:
  446. case -ENOENT:
  447. case -ESHUTDOWN:
  448. /* This urb is terminated, clean up */
  449. dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
  450. __func__, urb->status);
  451. break;
  452. default:
  453. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
  454. __func__, urb->status);
  455. }
  456. }
  457. static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
  458. __u16 reg)
  459. {
  460. struct usb_device *dev = mcs->port->serial->dev;
  461. struct usb_ctrlrequest *dr = mcs->led_dr;
  462. dr->bRequestType = MCS_WR_RTYPE;
  463. dr->bRequest = MCS_WRREQ;
  464. dr->wValue = cpu_to_le16(wval);
  465. dr->wIndex = cpu_to_le16(reg);
  466. dr->wLength = cpu_to_le16(0);
  467. usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
  468. (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
  469. usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
  470. }
  471. static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
  472. __u16 val)
  473. {
  474. struct usb_device *dev = port->serial->dev;
  475. usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
  476. val, reg, NULL, 0, MOS_WDR_TIMEOUT);
  477. }
  478. static void mos7840_led_off(unsigned long arg)
  479. {
  480. struct moschip_port *mcs = (struct moschip_port *) arg;
  481. /* Turn off LED */
  482. mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
  483. mod_timer(&mcs->led_timer2,
  484. jiffies + msecs_to_jiffies(LED_OFF_MS));
  485. }
  486. static void mos7840_led_flag_off(unsigned long arg)
  487. {
  488. struct moschip_port *mcs = (struct moschip_port *) arg;
  489. clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
  490. }
  491. static void mos7840_led_activity(struct usb_serial_port *port)
  492. {
  493. struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
  494. if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
  495. return;
  496. mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
  497. mod_timer(&mos7840_port->led_timer1,
  498. jiffies + msecs_to_jiffies(LED_ON_MS));
  499. }
  500. /*****************************************************************************
  501. * mos7840_interrupt_callback
  502. * this is the callback function for when we have received data on the
  503. * interrupt endpoint.
  504. *****************************************************************************/
  505. static void mos7840_interrupt_callback(struct urb *urb)
  506. {
  507. int result;
  508. int length;
  509. struct moschip_port *mos7840_port;
  510. struct usb_serial *serial;
  511. __u16 Data;
  512. unsigned char *data;
  513. __u8 sp[5], st;
  514. int i, rv = 0;
  515. __u16 wval, wreg = 0;
  516. int status = urb->status;
  517. switch (status) {
  518. case 0:
  519. /* success */
  520. break;
  521. case -ECONNRESET:
  522. case -ENOENT:
  523. case -ESHUTDOWN:
  524. /* this urb is terminated, clean up */
  525. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  526. __func__, status);
  527. return;
  528. default:
  529. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
  530. __func__, status);
  531. goto exit;
  532. }
  533. length = urb->actual_length;
  534. data = urb->transfer_buffer;
  535. serial = urb->context;
  536. /* Moschip get 5 bytes
  537. * Byte 1 IIR Port 1 (port.number is 0)
  538. * Byte 2 IIR Port 2 (port.number is 1)
  539. * Byte 3 IIR Port 3 (port.number is 2)
  540. * Byte 4 IIR Port 4 (port.number is 3)
  541. * Byte 5 FIFO status for both */
  542. if (length > 5) {
  543. dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
  544. return;
  545. }
  546. sp[0] = (__u8) data[0];
  547. sp[1] = (__u8) data[1];
  548. sp[2] = (__u8) data[2];
  549. sp[3] = (__u8) data[3];
  550. st = (__u8) data[4];
  551. for (i = 0; i < serial->num_ports; i++) {
  552. mos7840_port = mos7840_get_port_private(serial->port[i]);
  553. wval = ((__u16)serial->port[i]->port_number + 1) << 8;
  554. if (mos7840_port->open) {
  555. if (sp[i] & 0x01) {
  556. dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
  557. } else {
  558. switch (sp[i] & 0x0f) {
  559. case SERIAL_IIR_RLS:
  560. dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
  561. dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
  562. mos7840_port->MsrLsr = 1;
  563. wreg = LINE_STATUS_REGISTER;
  564. break;
  565. case SERIAL_IIR_MS:
  566. dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
  567. mos7840_port->MsrLsr = 0;
  568. wreg = MODEM_STATUS_REGISTER;
  569. break;
  570. }
  571. rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
  572. }
  573. }
  574. }
  575. if (!(rv < 0))
  576. /* the completion handler for the control urb will resubmit */
  577. return;
  578. exit:
  579. result = usb_submit_urb(urb, GFP_ATOMIC);
  580. if (result) {
  581. dev_err(&urb->dev->dev,
  582. "%s - Error %d submitting interrupt urb\n",
  583. __func__, result);
  584. }
  585. }
  586. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  587. const char *function)
  588. {
  589. if (!port) {
  590. pr_debug("%s - port == NULL\n", function);
  591. return -1;
  592. }
  593. if (!port->serial) {
  594. pr_debug("%s - port->serial == NULL\n", function);
  595. return -1;
  596. }
  597. return 0;
  598. }
  599. /* Inline functions to check the sanity of a pointer that is passed to us */
  600. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  601. const char *function)
  602. {
  603. if (!serial) {
  604. pr_debug("%s - serial == NULL\n", function);
  605. return -1;
  606. }
  607. if (!serial->type) {
  608. pr_debug("%s - serial->type == NULL!\n", function);
  609. return -1;
  610. }
  611. return 0;
  612. }
  613. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  614. const char *function)
  615. {
  616. /* if no port was specified, or it fails a paranoia check */
  617. if (!port ||
  618. mos7840_port_paranoia_check(port, function) ||
  619. mos7840_serial_paranoia_check(port->serial, function)) {
  620. /* then say that we don't have a valid usb_serial thing,
  621. * which will end up genrating -ENODEV return values */
  622. return NULL;
  623. }
  624. return port->serial;
  625. }
  626. /*****************************************************************************
  627. * mos7840_bulk_in_callback
  628. * this is the callback function for when we have received data on the
  629. * bulk in endpoint.
  630. *****************************************************************************/
  631. static void mos7840_bulk_in_callback(struct urb *urb)
  632. {
  633. int retval;
  634. unsigned char *data;
  635. struct usb_serial *serial;
  636. struct usb_serial_port *port;
  637. struct moschip_port *mos7840_port;
  638. int status = urb->status;
  639. mos7840_port = urb->context;
  640. if (!mos7840_port)
  641. return;
  642. if (status) {
  643. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  644. mos7840_port->read_urb_busy = false;
  645. return;
  646. }
  647. port = mos7840_port->port;
  648. if (mos7840_port_paranoia_check(port, __func__)) {
  649. mos7840_port->read_urb_busy = false;
  650. return;
  651. }
  652. serial = mos7840_get_usb_serial(port, __func__);
  653. if (!serial) {
  654. mos7840_port->read_urb_busy = false;
  655. return;
  656. }
  657. data = urb->transfer_buffer;
  658. usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
  659. if (urb->actual_length) {
  660. struct tty_port *tport = &mos7840_port->port->port;
  661. tty_insert_flip_string(tport, data, urb->actual_length);
  662. tty_flip_buffer_push(tport);
  663. port->icount.rx += urb->actual_length;
  664. dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
  665. }
  666. if (!mos7840_port->read_urb) {
  667. dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
  668. mos7840_port->read_urb_busy = false;
  669. return;
  670. }
  671. if (mos7840_port->has_led)
  672. mos7840_led_activity(port);
  673. mos7840_port->read_urb_busy = true;
  674. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  675. if (retval) {
  676. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  677. mos7840_port->read_urb_busy = false;
  678. }
  679. }
  680. /*****************************************************************************
  681. * mos7840_bulk_out_data_callback
  682. * this is the callback function for when we have finished sending
  683. * serial data on the bulk out endpoint.
  684. *****************************************************************************/
  685. static void mos7840_bulk_out_data_callback(struct urb *urb)
  686. {
  687. struct moschip_port *mos7840_port;
  688. struct usb_serial_port *port;
  689. int status = urb->status;
  690. int i;
  691. mos7840_port = urb->context;
  692. port = mos7840_port->port;
  693. spin_lock(&mos7840_port->pool_lock);
  694. for (i = 0; i < NUM_URBS; i++) {
  695. if (urb == mos7840_port->write_urb_pool[i]) {
  696. mos7840_port->busy[i] = 0;
  697. break;
  698. }
  699. }
  700. spin_unlock(&mos7840_port->pool_lock);
  701. if (status) {
  702. dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
  703. return;
  704. }
  705. if (mos7840_port_paranoia_check(port, __func__))
  706. return;
  707. if (mos7840_port->open)
  708. tty_port_tty_wakeup(&port->port);
  709. }
  710. /************************************************************************/
  711. /* 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 */
  712. /************************************************************************/
  713. /*****************************************************************************
  714. * mos7840_open
  715. * this function is called by the tty driver when a port is opened
  716. * If successful, we return 0
  717. * Otherwise we return a negative error number.
  718. *****************************************************************************/
  719. static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
  720. {
  721. int response;
  722. int j;
  723. struct usb_serial *serial;
  724. struct urb *urb;
  725. __u16 Data;
  726. int status;
  727. struct moschip_port *mos7840_port;
  728. struct moschip_port *port0;
  729. if (mos7840_port_paranoia_check(port, __func__))
  730. return -ENODEV;
  731. serial = port->serial;
  732. if (mos7840_serial_paranoia_check(serial, __func__))
  733. return -ENODEV;
  734. mos7840_port = mos7840_get_port_private(port);
  735. port0 = mos7840_get_port_private(serial->port[0]);
  736. if (mos7840_port == NULL || port0 == NULL)
  737. return -ENODEV;
  738. usb_clear_halt(serial->dev, port->write_urb->pipe);
  739. usb_clear_halt(serial->dev, port->read_urb->pipe);
  740. port0->open_ports++;
  741. /* Initialising the write urb pool */
  742. for (j = 0; j < NUM_URBS; ++j) {
  743. urb = usb_alloc_urb(0, GFP_KERNEL);
  744. mos7840_port->write_urb_pool[j] = urb;
  745. if (!urb)
  746. continue;
  747. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  748. GFP_KERNEL);
  749. if (!urb->transfer_buffer) {
  750. usb_free_urb(urb);
  751. mos7840_port->write_urb_pool[j] = NULL;
  752. continue;
  753. }
  754. }
  755. /*****************************************************************************
  756. * Initialize MCS7840 -- Write Init values to corresponding Registers
  757. *
  758. * Register Index
  759. * 1 : IER
  760. * 2 : FCR
  761. * 3 : LCR
  762. * 4 : MCR
  763. *
  764. * 0x08 : SP1/2 Control Reg
  765. *****************************************************************************/
  766. /* NEED to check the following Block */
  767. Data = 0x0;
  768. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  769. if (status < 0) {
  770. dev_dbg(&port->dev, "Reading Spreg failed\n");
  771. goto err;
  772. }
  773. Data |= 0x80;
  774. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  775. if (status < 0) {
  776. dev_dbg(&port->dev, "writing 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. /* End of block to be checked */
  786. Data = 0x0;
  787. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  788. &Data);
  789. if (status < 0) {
  790. dev_dbg(&port->dev, "Reading Controlreg failed\n");
  791. goto err;
  792. }
  793. Data |= 0x08; /* Driver done bit */
  794. Data |= 0x20; /* rx_disable */
  795. status = mos7840_set_reg_sync(port,
  796. mos7840_port->ControlRegOffset, Data);
  797. if (status < 0) {
  798. dev_dbg(&port->dev, "writing Controlreg failed\n");
  799. goto err;
  800. }
  801. /* do register settings here */
  802. /* Set all regs to the device default values. */
  803. /***********************************
  804. * First Disable all interrupts.
  805. ***********************************/
  806. Data = 0x00;
  807. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  808. if (status < 0) {
  809. dev_dbg(&port->dev, "disabling interrupts failed\n");
  810. goto err;
  811. }
  812. /* Set FIFO_CONTROL_REGISTER to the default value */
  813. Data = 0x00;
  814. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  815. if (status < 0) {
  816. dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
  817. goto err;
  818. }
  819. Data = 0xcf;
  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 = 0x03;
  826. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  827. mos7840_port->shadowLCR = Data;
  828. Data = 0x0b;
  829. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  830. mos7840_port->shadowMCR = Data;
  831. Data = 0x00;
  832. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  833. mos7840_port->shadowLCR = Data;
  834. Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
  835. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  836. Data = 0x0c;
  837. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  838. Data = 0x0;
  839. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  840. Data = 0x00;
  841. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  842. Data = Data & ~SERIAL_LCR_DLAB;
  843. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  844. mos7840_port->shadowLCR = Data;
  845. /* clearing Bulkin and Bulkout Fifo */
  846. Data = 0x0;
  847. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  848. Data = Data | 0x0c;
  849. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  850. Data = Data & ~0x0c;
  851. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  852. /* Finally enable all interrupts */
  853. Data = 0x0c;
  854. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  855. /* clearing rx_disable */
  856. Data = 0x0;
  857. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  858. &Data);
  859. Data = Data & ~0x20;
  860. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  861. Data);
  862. /* rx_negate */
  863. Data = 0x0;
  864. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  865. &Data);
  866. Data = Data | 0x10;
  867. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  868. Data);
  869. /* Check to see if we've set up our endpoint info yet *
  870. * (can't set it up in mos7840_startup as the structures *
  871. * were not set up at that time.) */
  872. if (port0->open_ports == 1) {
  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 != 1)
  1255. return -EIO;
  1256. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1257. if (status != 1)
  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. unsigned int cflag;
  1569. struct usb_serial *serial;
  1570. struct moschip_port *mos7840_port;
  1571. if (mos7840_port_paranoia_check(port, __func__))
  1572. return;
  1573. serial = port->serial;
  1574. if (mos7840_serial_paranoia_check(serial, __func__))
  1575. return;
  1576. mos7840_port = mos7840_get_port_private(port);
  1577. if (mos7840_port == NULL)
  1578. return;
  1579. if (!mos7840_port->open) {
  1580. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1581. return;
  1582. }
  1583. dev_dbg(&port->dev, "%s", "setting termios - \n");
  1584. cflag = tty->termios.c_cflag;
  1585. dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
  1586. tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
  1587. dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
  1588. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1589. /* change the port settings to the new ones specified */
  1590. mos7840_change_port_settings(tty, mos7840_port, old_termios);
  1591. if (!mos7840_port->read_urb) {
  1592. dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
  1593. return;
  1594. }
  1595. if (!mos7840_port->read_urb_busy) {
  1596. mos7840_port->read_urb_busy = true;
  1597. status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  1598. if (status) {
  1599. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
  1600. status);
  1601. mos7840_port->read_urb_busy = false;
  1602. }
  1603. }
  1604. }
  1605. /*****************************************************************************
  1606. * mos7840_get_lsr_info - get line status register info
  1607. *
  1608. * Purpose: Let user call ioctl() to get info when the UART physically
  1609. * is emptied. On bus types like RS485, the transmitter must
  1610. * release the bus after transmitting. This must be done when
  1611. * the transmit shift register is empty, not be done when the
  1612. * transmit holding register is empty. This functionality
  1613. * allows an RS485 driver to be written in user space.
  1614. *****************************************************************************/
  1615. static int mos7840_get_lsr_info(struct tty_struct *tty,
  1616. unsigned int __user *value)
  1617. {
  1618. int count;
  1619. unsigned int result = 0;
  1620. count = mos7840_chars_in_buffer(tty);
  1621. if (count == 0)
  1622. result = TIOCSER_TEMT;
  1623. if (copy_to_user(value, &result, sizeof(int)))
  1624. return -EFAULT;
  1625. return 0;
  1626. }
  1627. /*****************************************************************************
  1628. * mos7840_get_serial_info
  1629. * function to get information about serial port
  1630. *****************************************************************************/
  1631. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1632. struct serial_struct __user *retinfo)
  1633. {
  1634. struct serial_struct tmp;
  1635. if (mos7840_port == NULL)
  1636. return -1;
  1637. memset(&tmp, 0, sizeof(tmp));
  1638. tmp.type = PORT_16550A;
  1639. tmp.line = mos7840_port->port->minor;
  1640. tmp.port = mos7840_port->port->port_number;
  1641. tmp.irq = 0;
  1642. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1643. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1644. tmp.baud_base = 9600;
  1645. tmp.close_delay = 5 * HZ;
  1646. tmp.closing_wait = 30 * HZ;
  1647. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1648. return -EFAULT;
  1649. return 0;
  1650. }
  1651. /*****************************************************************************
  1652. * SerialIoctl
  1653. * this function handles any ioctl calls to the driver
  1654. *****************************************************************************/
  1655. static int mos7840_ioctl(struct tty_struct *tty,
  1656. unsigned int cmd, unsigned long arg)
  1657. {
  1658. struct usb_serial_port *port = tty->driver_data;
  1659. void __user *argp = (void __user *)arg;
  1660. struct moschip_port *mos7840_port;
  1661. if (mos7840_port_paranoia_check(port, __func__))
  1662. return -1;
  1663. mos7840_port = mos7840_get_port_private(port);
  1664. if (mos7840_port == NULL)
  1665. return -1;
  1666. switch (cmd) {
  1667. /* return number of bytes available */
  1668. case TIOCSERGETLSR:
  1669. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1670. return mos7840_get_lsr_info(tty, argp);
  1671. case TIOCGSERIAL:
  1672. dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
  1673. return mos7840_get_serial_info(mos7840_port, argp);
  1674. case TIOCSSERIAL:
  1675. dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
  1676. break;
  1677. default:
  1678. break;
  1679. }
  1680. return -ENOIOCTLCMD;
  1681. }
  1682. static int mos7810_check(struct usb_serial *serial)
  1683. {
  1684. int i, pass_count = 0;
  1685. u8 *buf;
  1686. __u16 data = 0, mcr_data = 0;
  1687. __u16 test_pattern = 0x55AA;
  1688. int res;
  1689. buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1690. if (!buf)
  1691. return 0; /* failed to identify 7810 */
  1692. /* Store MCR setting */
  1693. res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1694. MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
  1695. buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1696. if (res == VENDOR_READ_LENGTH)
  1697. mcr_data = *buf;
  1698. for (i = 0; i < 16; i++) {
  1699. /* Send the 1-bit test pattern out to MCS7810 test pin */
  1700. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1701. MCS_WRREQ, MCS_WR_RTYPE,
  1702. (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
  1703. MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
  1704. /* Read the test pattern back */
  1705. res = usb_control_msg(serial->dev,
  1706. usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
  1707. MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1708. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1709. if (res == VENDOR_READ_LENGTH)
  1710. data = *buf;
  1711. /* If this is a MCS7810 device, both test patterns must match */
  1712. if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
  1713. break;
  1714. pass_count++;
  1715. }
  1716. /* Restore MCR setting */
  1717. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
  1718. MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
  1719. 0, MOS_WDR_TIMEOUT);
  1720. kfree(buf);
  1721. if (pass_count == 16)
  1722. return 1;
  1723. return 0;
  1724. }
  1725. static int mos7840_probe(struct usb_serial *serial,
  1726. const struct usb_device_id *id)
  1727. {
  1728. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1729. u8 *buf;
  1730. int device_type;
  1731. if (product == MOSCHIP_DEVICE_ID_7810 ||
  1732. product == MOSCHIP_DEVICE_ID_7820) {
  1733. device_type = product;
  1734. goto out;
  1735. }
  1736. buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
  1737. if (!buf)
  1738. return -ENOMEM;
  1739. usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  1740. MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
  1741. VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
  1742. /* For a MCS7840 device GPIO0 must be set to 1 */
  1743. if (buf[0] & 0x01)
  1744. device_type = MOSCHIP_DEVICE_ID_7840;
  1745. else if (mos7810_check(serial))
  1746. device_type = MOSCHIP_DEVICE_ID_7810;
  1747. else
  1748. device_type = MOSCHIP_DEVICE_ID_7820;
  1749. kfree(buf);
  1750. out:
  1751. usb_set_serial_data(serial, (void *)(unsigned long)device_type);
  1752. return 0;
  1753. }
  1754. static int mos7840_calc_num_ports(struct usb_serial *serial)
  1755. {
  1756. int device_type = (unsigned long)usb_get_serial_data(serial);
  1757. int mos7840_num_ports;
  1758. mos7840_num_ports = (device_type >> 4) & 0x000F;
  1759. return mos7840_num_ports;
  1760. }
  1761. static int mos7840_attach(struct usb_serial *serial)
  1762. {
  1763. if (serial->num_bulk_in < serial->num_ports ||
  1764. serial->num_bulk_out < serial->num_ports) {
  1765. dev_err(&serial->interface->dev, "missing endpoints\n");
  1766. return -ENODEV;
  1767. }
  1768. return 0;
  1769. }
  1770. static int mos7840_port_probe(struct usb_serial_port *port)
  1771. {
  1772. struct usb_serial *serial = port->serial;
  1773. int device_type = (unsigned long)usb_get_serial_data(serial);
  1774. struct moschip_port *mos7840_port;
  1775. int status;
  1776. int pnum;
  1777. __u16 Data;
  1778. /* we set up the pointers to the endpoints in the mos7840_open *
  1779. * function, as the structures aren't created yet. */
  1780. pnum = port->port_number;
  1781. dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
  1782. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1783. if (!mos7840_port)
  1784. return -ENOMEM;
  1785. /* Initialize all port interrupt end point to port 0 int
  1786. * endpoint. Our device has only one interrupt end point
  1787. * common to all port */
  1788. mos7840_port->port = port;
  1789. mos7840_set_port_private(port, mos7840_port);
  1790. spin_lock_init(&mos7840_port->pool_lock);
  1791. /* minor is not initialised until later by
  1792. * usb-serial.c:get_free_serial() and cannot therefore be used
  1793. * to index device instances */
  1794. mos7840_port->port_num = pnum + 1;
  1795. dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
  1796. dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
  1797. if (mos7840_port->port_num == 1) {
  1798. mos7840_port->SpRegOffset = 0x0;
  1799. mos7840_port->ControlRegOffset = 0x1;
  1800. mos7840_port->DcrRegOffset = 0x4;
  1801. } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
  1802. mos7840_port->SpRegOffset = 0x8;
  1803. mos7840_port->ControlRegOffset = 0x9;
  1804. mos7840_port->DcrRegOffset = 0x16;
  1805. } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
  1806. mos7840_port->SpRegOffset = 0xa;
  1807. mos7840_port->ControlRegOffset = 0xb;
  1808. mos7840_port->DcrRegOffset = 0x19;
  1809. } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
  1810. mos7840_port->SpRegOffset = 0xa;
  1811. mos7840_port->ControlRegOffset = 0xb;
  1812. mos7840_port->DcrRegOffset = 0x19;
  1813. } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
  1814. mos7840_port->SpRegOffset = 0xc;
  1815. mos7840_port->ControlRegOffset = 0xd;
  1816. mos7840_port->DcrRegOffset = 0x1c;
  1817. }
  1818. mos7840_dump_serial_port(port, mos7840_port);
  1819. mos7840_set_port_private(port, mos7840_port);
  1820. /* enable rx_disable bit in control register */
  1821. status = mos7840_get_reg_sync(port,
  1822. mos7840_port->ControlRegOffset, &Data);
  1823. if (status < 0) {
  1824. dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
  1825. goto out;
  1826. } else
  1827. dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
  1828. Data |= 0x08; /* setting driver done bit */
  1829. Data |= 0x04; /* sp1_bit to have cts change reflect in
  1830. modem status reg */
  1831. /* Data |= 0x20; //rx_disable bit */
  1832. status = mos7840_set_reg_sync(port,
  1833. mos7840_port->ControlRegOffset, Data);
  1834. if (status < 0) {
  1835. dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  1836. goto out;
  1837. } else
  1838. dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
  1839. /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
  1840. and 0x24 in DCR3 */
  1841. Data = 0x01;
  1842. status = mos7840_set_reg_sync(port,
  1843. (__u16) (mos7840_port->DcrRegOffset + 0), Data);
  1844. if (status < 0) {
  1845. dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
  1846. goto out;
  1847. } else
  1848. dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
  1849. Data = 0x05;
  1850. status = mos7840_set_reg_sync(port,
  1851. (__u16) (mos7840_port->DcrRegOffset + 1), Data);
  1852. if (status < 0) {
  1853. dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
  1854. goto out;
  1855. } else
  1856. dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
  1857. Data = 0x24;
  1858. status = mos7840_set_reg_sync(port,
  1859. (__u16) (mos7840_port->DcrRegOffset + 2), Data);
  1860. if (status < 0) {
  1861. dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
  1862. goto out;
  1863. } else
  1864. dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
  1865. /* write values in clkstart0x0 and clkmulti 0x20 */
  1866. Data = 0x0;
  1867. status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
  1868. if (status < 0) {
  1869. dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  1870. goto out;
  1871. } else
  1872. dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  1873. Data = 0x20;
  1874. status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
  1875. if (status < 0) {
  1876. dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
  1877. goto error;
  1878. } else
  1879. dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
  1880. /* write value 0x0 to scratchpad register */
  1881. Data = 0x00;
  1882. status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
  1883. if (status < 0) {
  1884. dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
  1885. goto out;
  1886. } else
  1887. dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
  1888. /* Zero Length flag register */
  1889. if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
  1890. Data = 0xff;
  1891. status = mos7840_set_reg_sync(port,
  1892. (__u16) (ZLP_REG1 +
  1893. ((__u16)mos7840_port->port_num)), Data);
  1894. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1895. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
  1896. if (status < 0) {
  1897. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
  1898. goto out;
  1899. } else
  1900. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
  1901. } else {
  1902. Data = 0xff;
  1903. status = mos7840_set_reg_sync(port,
  1904. (__u16) (ZLP_REG1 +
  1905. ((__u16)mos7840_port->port_num) - 0x1), Data);
  1906. dev_dbg(&port->dev, "ZLIP offset %x\n",
  1907. (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
  1908. if (status < 0) {
  1909. dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
  1910. goto out;
  1911. } else
  1912. dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
  1913. }
  1914. mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  1915. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  1916. mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
  1917. GFP_KERNEL);
  1918. if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
  1919. !mos7840_port->dr) {
  1920. status = -ENOMEM;
  1921. goto error;
  1922. }
  1923. mos7840_port->has_led = false;
  1924. /* Initialize LED timers */
  1925. if (device_type == MOSCHIP_DEVICE_ID_7810) {
  1926. mos7840_port->has_led = true;
  1927. mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
  1928. mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
  1929. GFP_KERNEL);
  1930. if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
  1931. status = -ENOMEM;
  1932. goto error;
  1933. }
  1934. setup_timer(&mos7840_port->led_timer1, mos7840_led_off,
  1935. (unsigned long)mos7840_port);
  1936. mos7840_port->led_timer1.expires =
  1937. jiffies + msecs_to_jiffies(LED_ON_MS);
  1938. setup_timer(&mos7840_port->led_timer2, mos7840_led_flag_off,
  1939. (unsigned long)mos7840_port);
  1940. mos7840_port->led_timer2.expires =
  1941. jiffies + msecs_to_jiffies(LED_OFF_MS);
  1942. /* Turn off LED */
  1943. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1944. }
  1945. out:
  1946. if (pnum == serial->num_ports - 1) {
  1947. /* Zero Length flag enable */
  1948. Data = 0x0f;
  1949. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  1950. if (status < 0) {
  1951. dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
  1952. goto error;
  1953. } else
  1954. dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
  1955. /* setting configuration feature to one */
  1956. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1957. 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
  1958. MOS_WDR_TIMEOUT);
  1959. }
  1960. return 0;
  1961. error:
  1962. kfree(mos7840_port->led_dr);
  1963. usb_free_urb(mos7840_port->led_urb);
  1964. kfree(mos7840_port->dr);
  1965. kfree(mos7840_port->ctrl_buf);
  1966. usb_free_urb(mos7840_port->control_urb);
  1967. kfree(mos7840_port);
  1968. return status;
  1969. }
  1970. static int mos7840_port_remove(struct usb_serial_port *port)
  1971. {
  1972. struct moschip_port *mos7840_port;
  1973. mos7840_port = mos7840_get_port_private(port);
  1974. if (mos7840_port->has_led) {
  1975. /* Turn off LED */
  1976. mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
  1977. del_timer_sync(&mos7840_port->led_timer1);
  1978. del_timer_sync(&mos7840_port->led_timer2);
  1979. usb_kill_urb(mos7840_port->led_urb);
  1980. usb_free_urb(mos7840_port->led_urb);
  1981. kfree(mos7840_port->led_dr);
  1982. }
  1983. usb_kill_urb(mos7840_port->control_urb);
  1984. usb_free_urb(mos7840_port->control_urb);
  1985. kfree(mos7840_port->ctrl_buf);
  1986. kfree(mos7840_port->dr);
  1987. kfree(mos7840_port);
  1988. return 0;
  1989. }
  1990. static struct usb_serial_driver moschip7840_4port_device = {
  1991. .driver = {
  1992. .owner = THIS_MODULE,
  1993. .name = "mos7840",
  1994. },
  1995. .description = DRIVER_DESC,
  1996. .id_table = id_table,
  1997. .num_ports = 4,
  1998. .open = mos7840_open,
  1999. .close = mos7840_close,
  2000. .write = mos7840_write,
  2001. .write_room = mos7840_write_room,
  2002. .chars_in_buffer = mos7840_chars_in_buffer,
  2003. .throttle = mos7840_throttle,
  2004. .unthrottle = mos7840_unthrottle,
  2005. .calc_num_ports = mos7840_calc_num_ports,
  2006. .probe = mos7840_probe,
  2007. .ioctl = mos7840_ioctl,
  2008. .set_termios = mos7840_set_termios,
  2009. .break_ctl = mos7840_break,
  2010. .tiocmget = mos7840_tiocmget,
  2011. .tiocmset = mos7840_tiocmset,
  2012. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2013. .get_icount = usb_serial_generic_get_icount,
  2014. .attach = mos7840_attach,
  2015. .port_probe = mos7840_port_probe,
  2016. .port_remove = mos7840_port_remove,
  2017. .read_bulk_callback = mos7840_bulk_in_callback,
  2018. .read_int_callback = mos7840_interrupt_callback,
  2019. };
  2020. static struct usb_serial_driver * const serial_drivers[] = {
  2021. &moschip7840_4port_device, NULL
  2022. };
  2023. module_usb_serial_driver(serial_drivers, id_table);
  2024. MODULE_DESCRIPTION(DRIVER_DESC);
  2025. MODULE_LICENSE("GPL");