mos7840.c 78 KB

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