mos7840.c 78 KB

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