mos7840.c 71 KB

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