mos7840.c 73 KB

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