mos7840.c 76 KB

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