mos7840.c 71 KB

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