mos7840.c 75 KB

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