mos7840.c 74 KB

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