mos7840.c 77 KB

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