mos7840.c 79 KB

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