mos7840.c 75 KB

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