mos7840.c 76 KB

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