mos7840.c 72 KB

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