cp210x.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
  4. *
  5. * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
  6. *
  7. * Support to set flow control line levels using TIOCMGET and TIOCMSET
  8. * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
  9. * control thanks to Munir Nassar nassarmu@real-time.com
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/usb.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/usb/serial.h>
  22. #include <linux/gpio/driver.h>
  23. #include <linux/bitops.h>
  24. #include <linux/mutex.h>
  25. #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
  26. /*
  27. * Function Prototypes
  28. */
  29. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
  30. static void cp210x_close(struct usb_serial_port *);
  31. static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
  32. static void cp210x_get_termios_port(struct usb_serial_port *port,
  33. tcflag_t *cflagp, unsigned int *baudp);
  34. static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
  35. struct ktermios *);
  36. static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
  37. struct ktermios*);
  38. static bool cp210x_tx_empty(struct usb_serial_port *port);
  39. static int cp210x_tiocmget(struct tty_struct *);
  40. static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
  41. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  42. unsigned int, unsigned int);
  43. static void cp210x_break_ctl(struct tty_struct *, int);
  44. static int cp210x_attach(struct usb_serial *);
  45. static void cp210x_disconnect(struct usb_serial *);
  46. static void cp210x_release(struct usb_serial *);
  47. static int cp210x_port_probe(struct usb_serial_port *);
  48. static int cp210x_port_remove(struct usb_serial_port *);
  49. static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
  50. static const struct usb_device_id id_table[] = {
  51. { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
  52. { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
  53. { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  54. { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
  55. { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
  56. { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
  57. { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
  58. { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
  59. { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
  60. { USB_DEVICE(0x0988, 0x0578) }, /* Teraoka AD2000 */
  61. { USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
  62. { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
  63. { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
  64. { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
  65. { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
  66. { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
  67. { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
  68. { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
  69. { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
  70. { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
  71. { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
  72. { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
  73. { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
  74. { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
  75. { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
  76. { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
  77. { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
  78. { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
  79. { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
  80. { USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
  81. { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
  82. { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
  83. { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
  84. { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
  85. { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
  86. { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
  87. { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
  88. { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
  89. { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
  90. { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
  91. { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
  92. { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
  93. { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
  94. { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
  95. { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
  96. { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
  97. { USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
  98. { USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
  99. { USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
  100. { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
  101. { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
  102. { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
  103. { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
  104. { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
  105. { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
  106. { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
  107. { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
  108. { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
  109. { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
  110. { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
  111. { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
  112. { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
  113. { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
  114. { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
  115. { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
  116. { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
  117. { USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
  118. { USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
  119. { USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
  120. { USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
  121. { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
  122. { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
  123. { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
  124. { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
  125. { USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
  126. { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
  127. { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
  128. { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
  129. { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
  130. { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
  131. { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
  132. { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
  133. { USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
  134. { USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
  135. { USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
  136. { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
  137. { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
  138. { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
  139. { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
  140. { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
  141. { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
  142. { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
  143. { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
  144. { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
  145. { USB_DEVICE(0x10C4, 0x88D8) }, /* Acuity Brands nLight Air Adapter */
  146. { USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
  147. { USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
  148. { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
  149. { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
  150. { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
  151. { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
  152. { USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
  153. { USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
  154. { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
  155. { USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
  156. { USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
  157. { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
  158. { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
  159. { USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
  160. { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
  161. { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
  162. { USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
  163. { USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
  164. { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
  165. { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
  166. { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
  167. { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
  168. { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
  169. { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
  170. { USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
  171. { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
  172. { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
  173. { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
  174. { USB_DEVICE(0x155A, 0x1006) }, /* ELDAT Easywave RX09 */
  175. { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
  176. { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
  177. { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
  178. { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
  179. { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
  180. { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
  181. { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
  182. { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
  183. { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
  184. { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
  185. { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
  186. { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
  187. { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
  188. { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
  189. { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
  190. { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
  191. { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
  192. { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
  193. { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
  194. { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
  195. { USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
  196. { USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
  197. { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
  198. { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
  199. { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
  200. { USB_DEVICE(0x1901, 0x0195) }, /* GE B850/B650/B450 CP2104 DP UART interface */
  201. { USB_DEVICE(0x1901, 0x0196) }, /* GE B850 CP2105 DP UART interface */
  202. { USB_DEVICE(0x1901, 0x0197) }, /* GE CS1000 Display serial interface */
  203. { USB_DEVICE(0x1901, 0x0198) }, /* GE CS1000 M.2 Key E serial interface */
  204. { USB_DEVICE(0x199B, 0xBA30) }, /* LORD WSDA-200-USB */
  205. { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
  206. { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
  207. { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
  208. { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
  209. { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
  210. { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
  211. { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
  212. { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
  213. { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
  214. { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
  215. { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
  216. { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
  217. { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
  218. { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
  219. { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
  220. { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
  221. { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
  222. { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
  223. { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
  224. { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
  225. { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
  226. { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
  227. { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
  228. { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
  229. { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
  230. { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
  231. { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
  232. { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
  233. { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
  234. { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
  235. { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
  236. { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
  237. { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
  238. { } /* Terminating Entry */
  239. };
  240. MODULE_DEVICE_TABLE(usb, id_table);
  241. struct cp210x_serial_private {
  242. #ifdef CONFIG_GPIOLIB
  243. struct gpio_chip gc;
  244. bool gpio_registered;
  245. u8 gpio_pushpull;
  246. u8 gpio_altfunc;
  247. u8 gpio_input;
  248. #endif
  249. u8 partnum;
  250. speed_t max_speed;
  251. bool use_actual_rate;
  252. };
  253. struct cp210x_port_private {
  254. __u8 bInterfaceNumber;
  255. bool has_swapped_line_ctl;
  256. };
  257. static struct usb_serial_driver cp210x_device = {
  258. .driver = {
  259. .owner = THIS_MODULE,
  260. .name = "cp210x",
  261. },
  262. .id_table = id_table,
  263. .num_ports = 1,
  264. .bulk_in_size = 256,
  265. .bulk_out_size = 256,
  266. .open = cp210x_open,
  267. .close = cp210x_close,
  268. .break_ctl = cp210x_break_ctl,
  269. .set_termios = cp210x_set_termios,
  270. .tx_empty = cp210x_tx_empty,
  271. .throttle = usb_serial_generic_throttle,
  272. .unthrottle = usb_serial_generic_unthrottle,
  273. .tiocmget = cp210x_tiocmget,
  274. .tiocmset = cp210x_tiocmset,
  275. .attach = cp210x_attach,
  276. .disconnect = cp210x_disconnect,
  277. .release = cp210x_release,
  278. .port_probe = cp210x_port_probe,
  279. .port_remove = cp210x_port_remove,
  280. .dtr_rts = cp210x_dtr_rts
  281. };
  282. static struct usb_serial_driver * const serial_drivers[] = {
  283. &cp210x_device, NULL
  284. };
  285. /* Config request types */
  286. #define REQTYPE_HOST_TO_INTERFACE 0x41
  287. #define REQTYPE_INTERFACE_TO_HOST 0xc1
  288. #define REQTYPE_HOST_TO_DEVICE 0x40
  289. #define REQTYPE_DEVICE_TO_HOST 0xc0
  290. /* Config request codes */
  291. #define CP210X_IFC_ENABLE 0x00
  292. #define CP210X_SET_BAUDDIV 0x01
  293. #define CP210X_GET_BAUDDIV 0x02
  294. #define CP210X_SET_LINE_CTL 0x03
  295. #define CP210X_GET_LINE_CTL 0x04
  296. #define CP210X_SET_BREAK 0x05
  297. #define CP210X_IMM_CHAR 0x06
  298. #define CP210X_SET_MHS 0x07
  299. #define CP210X_GET_MDMSTS 0x08
  300. #define CP210X_SET_XON 0x09
  301. #define CP210X_SET_XOFF 0x0A
  302. #define CP210X_SET_EVENTMASK 0x0B
  303. #define CP210X_GET_EVENTMASK 0x0C
  304. #define CP210X_SET_CHAR 0x0D
  305. #define CP210X_GET_CHARS 0x0E
  306. #define CP210X_GET_PROPS 0x0F
  307. #define CP210X_GET_COMM_STATUS 0x10
  308. #define CP210X_RESET 0x11
  309. #define CP210X_PURGE 0x12
  310. #define CP210X_SET_FLOW 0x13
  311. #define CP210X_GET_FLOW 0x14
  312. #define CP210X_EMBED_EVENTS 0x15
  313. #define CP210X_GET_EVENTSTATE 0x16
  314. #define CP210X_SET_CHARS 0x19
  315. #define CP210X_GET_BAUDRATE 0x1D
  316. #define CP210X_SET_BAUDRATE 0x1E
  317. #define CP210X_VENDOR_SPECIFIC 0xFF
  318. /* CP210X_IFC_ENABLE */
  319. #define UART_ENABLE 0x0001
  320. #define UART_DISABLE 0x0000
  321. /* CP210X_(SET|GET)_BAUDDIV */
  322. #define BAUD_RATE_GEN_FREQ 0x384000
  323. /* CP210X_(SET|GET)_LINE_CTL */
  324. #define BITS_DATA_MASK 0X0f00
  325. #define BITS_DATA_5 0X0500
  326. #define BITS_DATA_6 0X0600
  327. #define BITS_DATA_7 0X0700
  328. #define BITS_DATA_8 0X0800
  329. #define BITS_DATA_9 0X0900
  330. #define BITS_PARITY_MASK 0x00f0
  331. #define BITS_PARITY_NONE 0x0000
  332. #define BITS_PARITY_ODD 0x0010
  333. #define BITS_PARITY_EVEN 0x0020
  334. #define BITS_PARITY_MARK 0x0030
  335. #define BITS_PARITY_SPACE 0x0040
  336. #define BITS_STOP_MASK 0x000f
  337. #define BITS_STOP_1 0x0000
  338. #define BITS_STOP_1_5 0x0001
  339. #define BITS_STOP_2 0x0002
  340. /* CP210X_SET_BREAK */
  341. #define BREAK_ON 0x0001
  342. #define BREAK_OFF 0x0000
  343. /* CP210X_(SET_MHS|GET_MDMSTS) */
  344. #define CONTROL_DTR 0x0001
  345. #define CONTROL_RTS 0x0002
  346. #define CONTROL_CTS 0x0010
  347. #define CONTROL_DSR 0x0020
  348. #define CONTROL_RING 0x0040
  349. #define CONTROL_DCD 0x0080
  350. #define CONTROL_WRITE_DTR 0x0100
  351. #define CONTROL_WRITE_RTS 0x0200
  352. /* CP210X_VENDOR_SPECIFIC values */
  353. #define CP210X_READ_2NCONFIG 0x000E
  354. #define CP210X_READ_LATCH 0x00C2
  355. #define CP210X_GET_PARTNUM 0x370B
  356. #define CP210X_GET_PORTCONFIG 0x370C
  357. #define CP210X_GET_DEVICEMODE 0x3711
  358. #define CP210X_WRITE_LATCH 0x37E1
  359. /* Part number definitions */
  360. #define CP210X_PARTNUM_CP2101 0x01
  361. #define CP210X_PARTNUM_CP2102 0x02
  362. #define CP210X_PARTNUM_CP2103 0x03
  363. #define CP210X_PARTNUM_CP2104 0x04
  364. #define CP210X_PARTNUM_CP2105 0x05
  365. #define CP210X_PARTNUM_CP2108 0x08
  366. #define CP210X_PARTNUM_CP2102N_QFN28 0x20
  367. #define CP210X_PARTNUM_CP2102N_QFN24 0x21
  368. #define CP210X_PARTNUM_CP2102N_QFN20 0x22
  369. #define CP210X_PARTNUM_UNKNOWN 0xFF
  370. /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
  371. struct cp210x_comm_status {
  372. __le32 ulErrors;
  373. __le32 ulHoldReasons;
  374. __le32 ulAmountInInQueue;
  375. __le32 ulAmountInOutQueue;
  376. u8 bEofReceived;
  377. u8 bWaitForImmediate;
  378. u8 bReserved;
  379. } __packed;
  380. /*
  381. * CP210X_PURGE - 16 bits passed in wValue of USB request.
  382. * SiLabs app note AN571 gives a strange description of the 4 bits:
  383. * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
  384. * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
  385. */
  386. #define PURGE_ALL 0x000f
  387. /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
  388. struct cp210x_flow_ctl {
  389. __le32 ulControlHandshake;
  390. __le32 ulFlowReplace;
  391. __le32 ulXonLimit;
  392. __le32 ulXoffLimit;
  393. } __packed;
  394. /* cp210x_flow_ctl::ulControlHandshake */
  395. #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
  396. #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
  397. #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
  398. #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
  399. #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
  400. #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
  401. /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
  402. #define CP210X_SERIAL_DTR_INACTIVE 0
  403. #define CP210X_SERIAL_DTR_ACTIVE 1
  404. #define CP210X_SERIAL_DTR_FLOW_CTL 2
  405. /* cp210x_flow_ctl::ulFlowReplace */
  406. #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
  407. #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
  408. #define CP210X_SERIAL_ERROR_CHAR BIT(2)
  409. #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
  410. #define CP210X_SERIAL_BREAK_CHAR BIT(4)
  411. #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
  412. #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
  413. #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
  414. /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
  415. #define CP210X_SERIAL_RTS_INACTIVE 0
  416. #define CP210X_SERIAL_RTS_ACTIVE 1
  417. #define CP210X_SERIAL_RTS_FLOW_CTL 2
  418. /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
  419. struct cp210x_pin_mode {
  420. u8 eci;
  421. u8 sci;
  422. } __packed;
  423. #define CP210X_PIN_MODE_MODEM 0
  424. #define CP210X_PIN_MODE_GPIO BIT(0)
  425. /*
  426. * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes.
  427. * Structure needs padding due to unused/unspecified bytes.
  428. */
  429. struct cp210x_config {
  430. __le16 gpio_mode;
  431. u8 __pad0[2];
  432. __le16 reset_state;
  433. u8 __pad1[4];
  434. __le16 suspend_state;
  435. u8 sci_cfg;
  436. u8 eci_cfg;
  437. u8 device_cfg;
  438. } __packed;
  439. /* GPIO modes */
  440. #define CP210X_SCI_GPIO_MODE_OFFSET 9
  441. #define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9)
  442. #define CP210X_ECI_GPIO_MODE_OFFSET 2
  443. #define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2)
  444. /* CP2105 port configuration values */
  445. #define CP2105_GPIO0_TXLED_MODE BIT(0)
  446. #define CP2105_GPIO1_RXLED_MODE BIT(1)
  447. #define CP2105_GPIO1_RS485_MODE BIT(2)
  448. /* CP2102N configuration array indices */
  449. #define CP210X_2NCONFIG_CONFIG_VERSION_IDX 2
  450. #define CP210X_2NCONFIG_GPIO_MODE_IDX 581
  451. #define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX 587
  452. #define CP210X_2NCONFIG_GPIO_CONTROL_IDX 600
  453. /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
  454. struct cp210x_gpio_write {
  455. u8 mask;
  456. u8 state;
  457. } __packed;
  458. /*
  459. * Helper to get interface number when we only have struct usb_serial.
  460. */
  461. static u8 cp210x_interface_num(struct usb_serial *serial)
  462. {
  463. struct usb_host_interface *cur_altsetting;
  464. cur_altsetting = serial->interface->cur_altsetting;
  465. return cur_altsetting->desc.bInterfaceNumber;
  466. }
  467. /*
  468. * Reads a variable-sized block of CP210X_ registers, identified by req.
  469. * Returns data into buf in native USB byte order.
  470. */
  471. static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
  472. void *buf, int bufsize)
  473. {
  474. struct usb_serial *serial = port->serial;
  475. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  476. void *dmabuf;
  477. int result;
  478. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  479. if (!dmabuf) {
  480. /*
  481. * FIXME Some callers don't bother to check for error,
  482. * at least give them consistent junk until they are fixed
  483. */
  484. memset(buf, 0, bufsize);
  485. return -ENOMEM;
  486. }
  487. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  488. req, REQTYPE_INTERFACE_TO_HOST, 0,
  489. port_priv->bInterfaceNumber, dmabuf, bufsize,
  490. USB_CTRL_SET_TIMEOUT);
  491. if (result == bufsize) {
  492. memcpy(buf, dmabuf, bufsize);
  493. result = 0;
  494. } else {
  495. dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
  496. req, bufsize, result);
  497. if (result >= 0)
  498. result = -EIO;
  499. /*
  500. * FIXME Some callers don't bother to check for error,
  501. * at least give them consistent junk until they are fixed
  502. */
  503. memset(buf, 0, bufsize);
  504. }
  505. kfree(dmabuf);
  506. return result;
  507. }
  508. /*
  509. * Reads any 32-bit CP210X_ register identified by req.
  510. */
  511. static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
  512. {
  513. __le32 le32_val;
  514. int err;
  515. err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
  516. if (err) {
  517. /*
  518. * FIXME Some callers don't bother to check for error,
  519. * at least give them consistent junk until they are fixed
  520. */
  521. *val = 0;
  522. return err;
  523. }
  524. *val = le32_to_cpu(le32_val);
  525. return 0;
  526. }
  527. /*
  528. * Reads any 16-bit CP210X_ register identified by req.
  529. */
  530. static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
  531. {
  532. __le16 le16_val;
  533. int err;
  534. err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
  535. if (err)
  536. return err;
  537. *val = le16_to_cpu(le16_val);
  538. return 0;
  539. }
  540. /*
  541. * Reads any 8-bit CP210X_ register identified by req.
  542. */
  543. static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
  544. {
  545. return cp210x_read_reg_block(port, req, val, sizeof(*val));
  546. }
  547. /*
  548. * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
  549. * Returns data into buf in native USB byte order.
  550. */
  551. static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
  552. void *buf, int bufsize)
  553. {
  554. void *dmabuf;
  555. int result;
  556. dmabuf = kmalloc(bufsize, GFP_KERNEL);
  557. if (!dmabuf)
  558. return -ENOMEM;
  559. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  560. CP210X_VENDOR_SPECIFIC, type, val,
  561. cp210x_interface_num(serial), dmabuf, bufsize,
  562. USB_CTRL_GET_TIMEOUT);
  563. if (result == bufsize) {
  564. memcpy(buf, dmabuf, bufsize);
  565. result = 0;
  566. } else {
  567. dev_err(&serial->interface->dev,
  568. "failed to get vendor val 0x%04x size %d: %d\n", val,
  569. bufsize, result);
  570. if (result >= 0)
  571. result = -EIO;
  572. }
  573. kfree(dmabuf);
  574. return result;
  575. }
  576. /*
  577. * Writes any 16-bit CP210X_ register (req) whose value is passed
  578. * entirely in the wValue field of the USB request.
  579. */
  580. static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
  581. {
  582. struct usb_serial *serial = port->serial;
  583. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  584. int result;
  585. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  586. req, REQTYPE_HOST_TO_INTERFACE, val,
  587. port_priv->bInterfaceNumber, NULL, 0,
  588. USB_CTRL_SET_TIMEOUT);
  589. if (result < 0) {
  590. dev_err(&port->dev, "failed set request 0x%x status: %d\n",
  591. req, result);
  592. }
  593. return result;
  594. }
  595. /*
  596. * Writes a variable-sized block of CP210X_ registers, identified by req.
  597. * Data in buf must be in native USB byte order.
  598. */
  599. static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
  600. void *buf, int bufsize)
  601. {
  602. struct usb_serial *serial = port->serial;
  603. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  604. void *dmabuf;
  605. int result;
  606. dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
  607. if (!dmabuf)
  608. return -ENOMEM;
  609. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  610. req, REQTYPE_HOST_TO_INTERFACE, 0,
  611. port_priv->bInterfaceNumber, dmabuf, bufsize,
  612. USB_CTRL_SET_TIMEOUT);
  613. kfree(dmabuf);
  614. if (result == bufsize) {
  615. result = 0;
  616. } else {
  617. dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
  618. req, bufsize, result);
  619. if (result >= 0)
  620. result = -EIO;
  621. }
  622. return result;
  623. }
  624. /*
  625. * Writes any 32-bit CP210X_ register identified by req.
  626. */
  627. static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
  628. {
  629. __le32 le32_val;
  630. le32_val = cpu_to_le32(val);
  631. return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
  632. }
  633. #ifdef CONFIG_GPIOLIB
  634. /*
  635. * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
  636. * Data in buf must be in native USB byte order.
  637. */
  638. static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
  639. u16 val, void *buf, int bufsize)
  640. {
  641. void *dmabuf;
  642. int result;
  643. dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
  644. if (!dmabuf)
  645. return -ENOMEM;
  646. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  647. CP210X_VENDOR_SPECIFIC, type, val,
  648. cp210x_interface_num(serial), dmabuf, bufsize,
  649. USB_CTRL_SET_TIMEOUT);
  650. kfree(dmabuf);
  651. if (result == bufsize) {
  652. result = 0;
  653. } else {
  654. dev_err(&serial->interface->dev,
  655. "failed to set vendor val 0x%04x size %d: %d\n", val,
  656. bufsize, result);
  657. if (result >= 0)
  658. result = -EIO;
  659. }
  660. return result;
  661. }
  662. #endif
  663. /*
  664. * Detect CP2108 GET_LINE_CTL bug and activate workaround.
  665. * Write a known good value 0x800, read it back.
  666. * If it comes back swapped the bug is detected.
  667. * Preserve the original register value.
  668. */
  669. static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
  670. {
  671. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  672. u16 line_ctl_save;
  673. u16 line_ctl_test;
  674. int err;
  675. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
  676. if (err)
  677. return err;
  678. err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
  679. if (err)
  680. return err;
  681. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
  682. if (err)
  683. return err;
  684. if (line_ctl_test == 8) {
  685. port_priv->has_swapped_line_ctl = true;
  686. line_ctl_save = swab16(line_ctl_save);
  687. }
  688. return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
  689. }
  690. /*
  691. * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
  692. * to workaround cp2108 bug and get correct value.
  693. */
  694. static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
  695. {
  696. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  697. int err;
  698. err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
  699. if (err)
  700. return err;
  701. /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
  702. if (port_priv->has_swapped_line_ctl)
  703. *ctl = swab16(*ctl);
  704. return 0;
  705. }
  706. static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
  707. {
  708. int result;
  709. result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
  710. if (result) {
  711. dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
  712. return result;
  713. }
  714. /* Configure the termios structure */
  715. cp210x_get_termios(tty, port);
  716. /* The baud rate must be initialised on cp2104 */
  717. if (tty)
  718. cp210x_change_speed(tty, port, NULL);
  719. return usb_serial_generic_open(tty, port);
  720. }
  721. static void cp210x_close(struct usb_serial_port *port)
  722. {
  723. usb_serial_generic_close(port);
  724. /* Clear both queues; cp2108 needs this to avoid an occasional hang */
  725. cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
  726. cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
  727. }
  728. /*
  729. * Read how many bytes are waiting in the TX queue.
  730. */
  731. static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
  732. u32 *count)
  733. {
  734. struct usb_serial *serial = port->serial;
  735. struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
  736. struct cp210x_comm_status *sts;
  737. int result;
  738. sts = kmalloc(sizeof(*sts), GFP_KERNEL);
  739. if (!sts)
  740. return -ENOMEM;
  741. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  742. CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
  743. 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
  744. USB_CTRL_GET_TIMEOUT);
  745. if (result == sizeof(*sts)) {
  746. *count = le32_to_cpu(sts->ulAmountInOutQueue);
  747. result = 0;
  748. } else {
  749. dev_err(&port->dev, "failed to get comm status: %d\n", result);
  750. if (result >= 0)
  751. result = -EIO;
  752. }
  753. kfree(sts);
  754. return result;
  755. }
  756. static bool cp210x_tx_empty(struct usb_serial_port *port)
  757. {
  758. int err;
  759. u32 count;
  760. err = cp210x_get_tx_queue_byte_count(port, &count);
  761. if (err)
  762. return true;
  763. return !count;
  764. }
  765. /*
  766. * cp210x_get_termios
  767. * Reads the baud rate, data bits, parity, stop bits and flow control mode
  768. * from the device, corrects any unsupported values, and configures the
  769. * termios structure to reflect the state of the device
  770. */
  771. static void cp210x_get_termios(struct tty_struct *tty,
  772. struct usb_serial_port *port)
  773. {
  774. unsigned int baud;
  775. if (tty) {
  776. cp210x_get_termios_port(tty->driver_data,
  777. &tty->termios.c_cflag, &baud);
  778. tty_encode_baud_rate(tty, baud, baud);
  779. } else {
  780. tcflag_t cflag;
  781. cflag = 0;
  782. cp210x_get_termios_port(port, &cflag, &baud);
  783. }
  784. }
  785. /*
  786. * cp210x_get_termios_port
  787. * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
  788. */
  789. static void cp210x_get_termios_port(struct usb_serial_port *port,
  790. tcflag_t *cflagp, unsigned int *baudp)
  791. {
  792. struct device *dev = &port->dev;
  793. tcflag_t cflag;
  794. struct cp210x_flow_ctl flow_ctl;
  795. u32 baud;
  796. u16 bits;
  797. u32 ctl_hs;
  798. u32 flow_repl;
  799. cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
  800. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  801. *baudp = baud;
  802. cflag = *cflagp;
  803. cp210x_get_line_ctl(port, &bits);
  804. cflag &= ~CSIZE;
  805. switch (bits & BITS_DATA_MASK) {
  806. case BITS_DATA_5:
  807. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  808. cflag |= CS5;
  809. break;
  810. case BITS_DATA_6:
  811. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  812. cflag |= CS6;
  813. break;
  814. case BITS_DATA_7:
  815. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  816. cflag |= CS7;
  817. break;
  818. case BITS_DATA_8:
  819. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  820. cflag |= CS8;
  821. break;
  822. case BITS_DATA_9:
  823. dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
  824. cflag |= CS8;
  825. bits &= ~BITS_DATA_MASK;
  826. bits |= BITS_DATA_8;
  827. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  828. break;
  829. default:
  830. dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
  831. cflag |= CS8;
  832. bits &= ~BITS_DATA_MASK;
  833. bits |= BITS_DATA_8;
  834. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  835. break;
  836. }
  837. switch (bits & BITS_PARITY_MASK) {
  838. case BITS_PARITY_NONE:
  839. dev_dbg(dev, "%s - parity = NONE\n", __func__);
  840. cflag &= ~PARENB;
  841. break;
  842. case BITS_PARITY_ODD:
  843. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  844. cflag |= (PARENB|PARODD);
  845. break;
  846. case BITS_PARITY_EVEN:
  847. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  848. cflag &= ~PARODD;
  849. cflag |= PARENB;
  850. break;
  851. case BITS_PARITY_MARK:
  852. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  853. cflag |= (PARENB|PARODD|CMSPAR);
  854. break;
  855. case BITS_PARITY_SPACE:
  856. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  857. cflag &= ~PARODD;
  858. cflag |= (PARENB|CMSPAR);
  859. break;
  860. default:
  861. dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
  862. cflag &= ~PARENB;
  863. bits &= ~BITS_PARITY_MASK;
  864. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  865. break;
  866. }
  867. cflag &= ~CSTOPB;
  868. switch (bits & BITS_STOP_MASK) {
  869. case BITS_STOP_1:
  870. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  871. break;
  872. case BITS_STOP_1_5:
  873. dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
  874. bits &= ~BITS_STOP_MASK;
  875. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  876. break;
  877. case BITS_STOP_2:
  878. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  879. cflag |= CSTOPB;
  880. break;
  881. default:
  882. dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
  883. bits &= ~BITS_STOP_MASK;
  884. cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
  885. break;
  886. }
  887. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  888. sizeof(flow_ctl));
  889. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  890. if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
  891. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  892. /*
  893. * When the port is closed, the CP210x hardware disables
  894. * auto-RTS and RTS is deasserted but it leaves auto-CTS when
  895. * in hardware flow control mode. When re-opening the port, if
  896. * auto-CTS is enabled on the cp210x, then auto-RTS must be
  897. * re-enabled in the driver.
  898. */
  899. flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
  900. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  901. flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
  902. flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
  903. cp210x_write_reg_block(port,
  904. CP210X_SET_FLOW,
  905. &flow_ctl,
  906. sizeof(flow_ctl));
  907. cflag |= CRTSCTS;
  908. } else {
  909. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  910. cflag &= ~CRTSCTS;
  911. }
  912. *cflagp = cflag;
  913. }
  914. struct cp210x_rate {
  915. speed_t rate;
  916. speed_t high;
  917. };
  918. static const struct cp210x_rate cp210x_an205_table1[] = {
  919. { 300, 300 },
  920. { 600, 600 },
  921. { 1200, 1200 },
  922. { 1800, 1800 },
  923. { 2400, 2400 },
  924. { 4000, 4000 },
  925. { 4800, 4803 },
  926. { 7200, 7207 },
  927. { 9600, 9612 },
  928. { 14400, 14428 },
  929. { 16000, 16062 },
  930. { 19200, 19250 },
  931. { 28800, 28912 },
  932. { 38400, 38601 },
  933. { 51200, 51558 },
  934. { 56000, 56280 },
  935. { 57600, 58053 },
  936. { 64000, 64111 },
  937. { 76800, 77608 },
  938. { 115200, 117028 },
  939. { 128000, 129347 },
  940. { 153600, 156868 },
  941. { 230400, 237832 },
  942. { 250000, 254234 },
  943. { 256000, 273066 },
  944. { 460800, 491520 },
  945. { 500000, 567138 },
  946. { 576000, 670254 },
  947. { 921600, UINT_MAX }
  948. };
  949. /*
  950. * Quantises the baud rate as per AN205 Table 1
  951. */
  952. static speed_t cp210x_get_an205_rate(speed_t baud)
  953. {
  954. int i;
  955. for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
  956. if (baud <= cp210x_an205_table1[i].high)
  957. break;
  958. }
  959. return cp210x_an205_table1[i].rate;
  960. }
  961. static speed_t cp210x_get_actual_rate(struct usb_serial *serial, speed_t baud)
  962. {
  963. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  964. unsigned int prescale = 1;
  965. unsigned int div;
  966. baud = clamp(baud, 300u, priv->max_speed);
  967. if (baud <= 365)
  968. prescale = 4;
  969. div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
  970. baud = 48000000 / (2 * prescale * div);
  971. return baud;
  972. }
  973. /*
  974. * CP2101 supports the following baud rates:
  975. *
  976. * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
  977. * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
  978. *
  979. * CP2102 and CP2103 support the following additional rates:
  980. *
  981. * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
  982. * 576000
  983. *
  984. * The device will map a requested rate to a supported one, but the result
  985. * of requests for rates greater than 1053257 is undefined (see AN205).
  986. *
  987. * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
  988. * respectively, with an error less than 1%. The actual rates are determined
  989. * by
  990. *
  991. * div = round(freq / (2 x prescale x request))
  992. * actual = freq / (2 x prescale x div)
  993. *
  994. * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
  995. * or 1 otherwise.
  996. * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
  997. * otherwise.
  998. */
  999. static void cp210x_change_speed(struct tty_struct *tty,
  1000. struct usb_serial_port *port, struct ktermios *old_termios)
  1001. {
  1002. struct usb_serial *serial = port->serial;
  1003. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1004. u32 baud;
  1005. baud = tty->termios.c_ospeed;
  1006. /*
  1007. * This maps the requested rate to the actual rate, a valid rate on
  1008. * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
  1009. *
  1010. * NOTE: B0 is not implemented.
  1011. */
  1012. if (priv->use_actual_rate)
  1013. baud = cp210x_get_actual_rate(serial, baud);
  1014. else if (baud < 1000000)
  1015. baud = cp210x_get_an205_rate(baud);
  1016. else if (baud > priv->max_speed)
  1017. baud = priv->max_speed;
  1018. dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
  1019. if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
  1020. dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
  1021. if (old_termios)
  1022. baud = old_termios->c_ospeed;
  1023. else
  1024. baud = 9600;
  1025. }
  1026. tty_encode_baud_rate(tty, baud, baud);
  1027. }
  1028. static void cp210x_set_termios(struct tty_struct *tty,
  1029. struct usb_serial_port *port, struct ktermios *old_termios)
  1030. {
  1031. struct device *dev = &port->dev;
  1032. unsigned int cflag, old_cflag;
  1033. u16 bits;
  1034. cflag = tty->termios.c_cflag;
  1035. old_cflag = old_termios->c_cflag;
  1036. if (tty->termios.c_ospeed != old_termios->c_ospeed)
  1037. cp210x_change_speed(tty, port, old_termios);
  1038. /* If the number of data bits is to be updated */
  1039. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  1040. cp210x_get_line_ctl(port, &bits);
  1041. bits &= ~BITS_DATA_MASK;
  1042. switch (cflag & CSIZE) {
  1043. case CS5:
  1044. bits |= BITS_DATA_5;
  1045. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  1046. break;
  1047. case CS6:
  1048. bits |= BITS_DATA_6;
  1049. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  1050. break;
  1051. case CS7:
  1052. bits |= BITS_DATA_7;
  1053. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  1054. break;
  1055. case CS8:
  1056. default:
  1057. bits |= BITS_DATA_8;
  1058. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  1059. break;
  1060. }
  1061. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1062. dev_dbg(dev, "Number of data bits requested not supported by device\n");
  1063. }
  1064. if ((cflag & (PARENB|PARODD|CMSPAR)) !=
  1065. (old_cflag & (PARENB|PARODD|CMSPAR))) {
  1066. cp210x_get_line_ctl(port, &bits);
  1067. bits &= ~BITS_PARITY_MASK;
  1068. if (cflag & PARENB) {
  1069. if (cflag & CMSPAR) {
  1070. if (cflag & PARODD) {
  1071. bits |= BITS_PARITY_MARK;
  1072. dev_dbg(dev, "%s - parity = MARK\n", __func__);
  1073. } else {
  1074. bits |= BITS_PARITY_SPACE;
  1075. dev_dbg(dev, "%s - parity = SPACE\n", __func__);
  1076. }
  1077. } else {
  1078. if (cflag & PARODD) {
  1079. bits |= BITS_PARITY_ODD;
  1080. dev_dbg(dev, "%s - parity = ODD\n", __func__);
  1081. } else {
  1082. bits |= BITS_PARITY_EVEN;
  1083. dev_dbg(dev, "%s - parity = EVEN\n", __func__);
  1084. }
  1085. }
  1086. }
  1087. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1088. dev_dbg(dev, "Parity mode not supported by device\n");
  1089. }
  1090. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  1091. cp210x_get_line_ctl(port, &bits);
  1092. bits &= ~BITS_STOP_MASK;
  1093. if (cflag & CSTOPB) {
  1094. bits |= BITS_STOP_2;
  1095. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1096. } else {
  1097. bits |= BITS_STOP_1;
  1098. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1099. }
  1100. if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
  1101. dev_dbg(dev, "Number of stop bits requested not supported by device\n");
  1102. }
  1103. if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  1104. struct cp210x_flow_ctl flow_ctl;
  1105. u32 ctl_hs;
  1106. u32 flow_repl;
  1107. cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
  1108. sizeof(flow_ctl));
  1109. ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
  1110. flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
  1111. dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1112. __func__, ctl_hs, flow_repl);
  1113. ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
  1114. ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
  1115. ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
  1116. ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
  1117. ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
  1118. if (cflag & CRTSCTS) {
  1119. ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
  1120. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1121. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1122. CP210X_SERIAL_RTS_FLOW_CTL);
  1123. dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
  1124. } else {
  1125. ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
  1126. flow_repl &= ~CP210X_SERIAL_RTS_MASK;
  1127. flow_repl |= CP210X_SERIAL_RTS_SHIFT(
  1128. CP210X_SERIAL_RTS_ACTIVE);
  1129. dev_dbg(dev, "%s - flow control = NONE\n", __func__);
  1130. }
  1131. dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
  1132. __func__, ctl_hs, flow_repl);
  1133. flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
  1134. flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
  1135. cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
  1136. sizeof(flow_ctl));
  1137. }
  1138. }
  1139. static int cp210x_tiocmset(struct tty_struct *tty,
  1140. unsigned int set, unsigned int clear)
  1141. {
  1142. struct usb_serial_port *port = tty->driver_data;
  1143. return cp210x_tiocmset_port(port, set, clear);
  1144. }
  1145. static int cp210x_tiocmset_port(struct usb_serial_port *port,
  1146. unsigned int set, unsigned int clear)
  1147. {
  1148. u16 control = 0;
  1149. if (set & TIOCM_RTS) {
  1150. control |= CONTROL_RTS;
  1151. control |= CONTROL_WRITE_RTS;
  1152. }
  1153. if (set & TIOCM_DTR) {
  1154. control |= CONTROL_DTR;
  1155. control |= CONTROL_WRITE_DTR;
  1156. }
  1157. if (clear & TIOCM_RTS) {
  1158. control &= ~CONTROL_RTS;
  1159. control |= CONTROL_WRITE_RTS;
  1160. }
  1161. if (clear & TIOCM_DTR) {
  1162. control &= ~CONTROL_DTR;
  1163. control |= CONTROL_WRITE_DTR;
  1164. }
  1165. dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
  1166. return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
  1167. }
  1168. static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
  1169. {
  1170. if (on)
  1171. cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
  1172. else
  1173. cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
  1174. }
  1175. static int cp210x_tiocmget(struct tty_struct *tty)
  1176. {
  1177. struct usb_serial_port *port = tty->driver_data;
  1178. u8 control;
  1179. int result;
  1180. result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
  1181. if (result)
  1182. return result;
  1183. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  1184. |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  1185. |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
  1186. |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
  1187. |((control & CONTROL_RING)? TIOCM_RI : 0)
  1188. |((control & CONTROL_DCD) ? TIOCM_CD : 0);
  1189. dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
  1190. return result;
  1191. }
  1192. static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
  1193. {
  1194. struct usb_serial_port *port = tty->driver_data;
  1195. u16 state;
  1196. if (break_state == 0)
  1197. state = BREAK_OFF;
  1198. else
  1199. state = BREAK_ON;
  1200. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  1201. state == BREAK_OFF ? "off" : "on");
  1202. cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
  1203. }
  1204. #ifdef CONFIG_GPIOLIB
  1205. static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
  1206. {
  1207. struct usb_serial *serial = gpiochip_get_data(gc);
  1208. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1209. if (priv->gpio_altfunc & BIT(offset))
  1210. return -ENODEV;
  1211. return 0;
  1212. }
  1213. static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  1214. {
  1215. struct usb_serial *serial = gpiochip_get_data(gc);
  1216. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1217. u8 req_type = REQTYPE_DEVICE_TO_HOST;
  1218. int result;
  1219. u8 buf;
  1220. if (priv->partnum == CP210X_PARTNUM_CP2105)
  1221. req_type = REQTYPE_INTERFACE_TO_HOST;
  1222. result = usb_autopm_get_interface(serial->interface);
  1223. if (result)
  1224. return result;
  1225. result = cp210x_read_vendor_block(serial, req_type,
  1226. CP210X_READ_LATCH, &buf, sizeof(buf));
  1227. usb_autopm_put_interface(serial->interface);
  1228. if (result < 0)
  1229. return result;
  1230. return !!(buf & BIT(gpio));
  1231. }
  1232. static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
  1233. {
  1234. struct usb_serial *serial = gpiochip_get_data(gc);
  1235. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1236. struct cp210x_gpio_write buf;
  1237. int result;
  1238. if (value == 1)
  1239. buf.state = BIT(gpio);
  1240. else
  1241. buf.state = 0;
  1242. buf.mask = BIT(gpio);
  1243. result = usb_autopm_get_interface(serial->interface);
  1244. if (result)
  1245. goto out;
  1246. if (priv->partnum == CP210X_PARTNUM_CP2105) {
  1247. result = cp210x_write_vendor_block(serial,
  1248. REQTYPE_HOST_TO_INTERFACE,
  1249. CP210X_WRITE_LATCH, &buf,
  1250. sizeof(buf));
  1251. } else {
  1252. u16 wIndex = buf.state << 8 | buf.mask;
  1253. result = usb_control_msg(serial->dev,
  1254. usb_sndctrlpipe(serial->dev, 0),
  1255. CP210X_VENDOR_SPECIFIC,
  1256. REQTYPE_HOST_TO_DEVICE,
  1257. CP210X_WRITE_LATCH,
  1258. wIndex,
  1259. NULL, 0, USB_CTRL_SET_TIMEOUT);
  1260. }
  1261. usb_autopm_put_interface(serial->interface);
  1262. out:
  1263. if (result < 0) {
  1264. dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
  1265. result);
  1266. }
  1267. }
  1268. static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
  1269. {
  1270. struct usb_serial *serial = gpiochip_get_data(gc);
  1271. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1272. return priv->gpio_input & BIT(gpio);
  1273. }
  1274. static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
  1275. {
  1276. struct usb_serial *serial = gpiochip_get_data(gc);
  1277. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1278. if (priv->partnum == CP210X_PARTNUM_CP2105) {
  1279. /* hardware does not support an input mode */
  1280. return -ENOTSUPP;
  1281. }
  1282. /* push-pull pins cannot be changed to be inputs */
  1283. if (priv->gpio_pushpull & BIT(gpio))
  1284. return -EINVAL;
  1285. /* make sure to release pin if it is being driven low */
  1286. cp210x_gpio_set(gc, gpio, 1);
  1287. priv->gpio_input |= BIT(gpio);
  1288. return 0;
  1289. }
  1290. static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
  1291. int value)
  1292. {
  1293. struct usb_serial *serial = gpiochip_get_data(gc);
  1294. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1295. priv->gpio_input &= ~BIT(gpio);
  1296. cp210x_gpio_set(gc, gpio, value);
  1297. return 0;
  1298. }
  1299. static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
  1300. unsigned long config)
  1301. {
  1302. struct usb_serial *serial = gpiochip_get_data(gc);
  1303. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1304. enum pin_config_param param = pinconf_to_config_param(config);
  1305. /* Succeed only if in correct mode (this can't be set at runtime) */
  1306. if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
  1307. (priv->gpio_pushpull & BIT(gpio)))
  1308. return 0;
  1309. if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
  1310. !(priv->gpio_pushpull & BIT(gpio)))
  1311. return 0;
  1312. return -ENOTSUPP;
  1313. }
  1314. /*
  1315. * This function is for configuring GPIO using shared pins, where other signals
  1316. * are made unavailable by configuring the use of GPIO. This is believed to be
  1317. * only applicable to the cp2105 at this point, the other devices supported by
  1318. * this driver that provide GPIO do so in a way that does not impact other
  1319. * signals and are thus expected to have very different initialisation.
  1320. */
  1321. static int cp2105_gpioconf_init(struct usb_serial *serial)
  1322. {
  1323. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1324. struct cp210x_pin_mode mode;
  1325. struct cp210x_config config;
  1326. u8 intf_num = cp210x_interface_num(serial);
  1327. u8 iface_config;
  1328. int result;
  1329. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1330. CP210X_GET_DEVICEMODE, &mode,
  1331. sizeof(mode));
  1332. if (result < 0)
  1333. return result;
  1334. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1335. CP210X_GET_PORTCONFIG, &config,
  1336. sizeof(config));
  1337. if (result < 0)
  1338. return result;
  1339. /* 2 banks of GPIO - One for the pins taken from each serial port */
  1340. if (intf_num == 0) {
  1341. if (mode.eci == CP210X_PIN_MODE_MODEM) {
  1342. /* mark all GPIOs of this interface as reserved */
  1343. priv->gpio_altfunc = 0xff;
  1344. return 0;
  1345. }
  1346. iface_config = config.eci_cfg;
  1347. priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
  1348. CP210X_ECI_GPIO_MODE_MASK) >>
  1349. CP210X_ECI_GPIO_MODE_OFFSET);
  1350. priv->gc.ngpio = 2;
  1351. } else if (intf_num == 1) {
  1352. if (mode.sci == CP210X_PIN_MODE_MODEM) {
  1353. /* mark all GPIOs of this interface as reserved */
  1354. priv->gpio_altfunc = 0xff;
  1355. return 0;
  1356. }
  1357. iface_config = config.sci_cfg;
  1358. priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
  1359. CP210X_SCI_GPIO_MODE_MASK) >>
  1360. CP210X_SCI_GPIO_MODE_OFFSET);
  1361. priv->gc.ngpio = 3;
  1362. } else {
  1363. return -ENODEV;
  1364. }
  1365. /* mark all pins which are not in GPIO mode */
  1366. if (iface_config & CP2105_GPIO0_TXLED_MODE) /* GPIO 0 */
  1367. priv->gpio_altfunc |= BIT(0);
  1368. if (iface_config & (CP2105_GPIO1_RXLED_MODE | /* GPIO 1 */
  1369. CP2105_GPIO1_RS485_MODE))
  1370. priv->gpio_altfunc |= BIT(1);
  1371. /* driver implementation for CP2105 only supports outputs */
  1372. priv->gpio_input = 0;
  1373. return 0;
  1374. }
  1375. static int cp2102n_gpioconf_init(struct usb_serial *serial)
  1376. {
  1377. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1378. const u16 config_size = 0x02a6;
  1379. u8 gpio_rst_latch;
  1380. u8 config_version;
  1381. u8 gpio_pushpull;
  1382. u8 *config_buf;
  1383. u8 gpio_latch;
  1384. u8 gpio_ctrl;
  1385. int result;
  1386. u8 i;
  1387. /*
  1388. * Retrieve device configuration from the device.
  1389. * The array received contains all customization settings done at the
  1390. * factory/manufacturer. Format of the array is documented at the
  1391. * time of writing at:
  1392. * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
  1393. */
  1394. config_buf = kmalloc(config_size, GFP_KERNEL);
  1395. if (!config_buf)
  1396. return -ENOMEM;
  1397. result = cp210x_read_vendor_block(serial,
  1398. REQTYPE_DEVICE_TO_HOST,
  1399. CP210X_READ_2NCONFIG,
  1400. config_buf,
  1401. config_size);
  1402. if (result < 0) {
  1403. kfree(config_buf);
  1404. return result;
  1405. }
  1406. config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
  1407. gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
  1408. gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
  1409. gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
  1410. kfree(config_buf);
  1411. /* Make sure this is a config format we understand. */
  1412. if (config_version != 0x01)
  1413. return -ENOTSUPP;
  1414. /*
  1415. * We only support 4 GPIOs even on the QFN28 package, because
  1416. * config locations of GPIOs 4-6 determined using reverse
  1417. * engineering revealed conflicting offsets with other
  1418. * documented functions. So we'll just play it safe for now.
  1419. */
  1420. priv->gc.ngpio = 4;
  1421. /*
  1422. * Get default pin states after reset. Needed so we can determine
  1423. * the direction of an open-drain pin.
  1424. */
  1425. gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
  1426. /* 0 indicates open-drain mode, 1 is push-pull */
  1427. priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
  1428. /* 0 indicates GPIO mode, 1 is alternate function */
  1429. priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
  1430. /*
  1431. * The CP2102N does not strictly has input and output pin modes,
  1432. * it only knows open-drain and push-pull modes which is set at
  1433. * factory. An open-drain pin can function both as an
  1434. * input or an output. We emulate input mode for open-drain pins
  1435. * by making sure they are not driven low, and we do not allow
  1436. * push-pull pins to be set as an input.
  1437. */
  1438. for (i = 0; i < priv->gc.ngpio; ++i) {
  1439. /*
  1440. * Set direction to "input" iff pin is open-drain and reset
  1441. * value is 1.
  1442. */
  1443. if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
  1444. priv->gpio_input |= BIT(i);
  1445. }
  1446. return 0;
  1447. }
  1448. static int cp210x_gpio_init(struct usb_serial *serial)
  1449. {
  1450. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1451. int result;
  1452. switch (priv->partnum) {
  1453. case CP210X_PARTNUM_CP2105:
  1454. result = cp2105_gpioconf_init(serial);
  1455. break;
  1456. case CP210X_PARTNUM_CP2102N_QFN28:
  1457. case CP210X_PARTNUM_CP2102N_QFN24:
  1458. case CP210X_PARTNUM_CP2102N_QFN20:
  1459. result = cp2102n_gpioconf_init(serial);
  1460. break;
  1461. default:
  1462. return 0;
  1463. }
  1464. if (result < 0)
  1465. return result;
  1466. priv->gc.label = "cp210x";
  1467. priv->gc.request = cp210x_gpio_request;
  1468. priv->gc.get_direction = cp210x_gpio_direction_get;
  1469. priv->gc.direction_input = cp210x_gpio_direction_input;
  1470. priv->gc.direction_output = cp210x_gpio_direction_output;
  1471. priv->gc.get = cp210x_gpio_get;
  1472. priv->gc.set = cp210x_gpio_set;
  1473. priv->gc.set_config = cp210x_gpio_set_config;
  1474. priv->gc.owner = THIS_MODULE;
  1475. priv->gc.parent = &serial->interface->dev;
  1476. priv->gc.base = -1;
  1477. priv->gc.can_sleep = true;
  1478. result = gpiochip_add_data(&priv->gc, serial);
  1479. if (!result)
  1480. priv->gpio_registered = true;
  1481. return result;
  1482. }
  1483. static void cp210x_gpio_remove(struct usb_serial *serial)
  1484. {
  1485. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1486. if (priv->gpio_registered) {
  1487. gpiochip_remove(&priv->gc);
  1488. priv->gpio_registered = false;
  1489. }
  1490. }
  1491. #else
  1492. static int cp210x_gpio_init(struct usb_serial *serial)
  1493. {
  1494. return 0;
  1495. }
  1496. static void cp210x_gpio_remove(struct usb_serial *serial)
  1497. {
  1498. /* Nothing to do */
  1499. }
  1500. #endif
  1501. static int cp210x_port_probe(struct usb_serial_port *port)
  1502. {
  1503. struct usb_serial *serial = port->serial;
  1504. struct cp210x_port_private *port_priv;
  1505. int ret;
  1506. port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
  1507. if (!port_priv)
  1508. return -ENOMEM;
  1509. port_priv->bInterfaceNumber = cp210x_interface_num(serial);
  1510. usb_set_serial_port_data(port, port_priv);
  1511. ret = cp210x_detect_swapped_line_ctl(port);
  1512. if (ret) {
  1513. kfree(port_priv);
  1514. return ret;
  1515. }
  1516. return 0;
  1517. }
  1518. static int cp210x_port_remove(struct usb_serial_port *port)
  1519. {
  1520. struct cp210x_port_private *port_priv;
  1521. port_priv = usb_get_serial_port_data(port);
  1522. kfree(port_priv);
  1523. return 0;
  1524. }
  1525. static void cp210x_init_max_speed(struct usb_serial *serial)
  1526. {
  1527. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1528. bool use_actual_rate = false;
  1529. speed_t max;
  1530. switch (priv->partnum) {
  1531. case CP210X_PARTNUM_CP2101:
  1532. max = 921600;
  1533. break;
  1534. case CP210X_PARTNUM_CP2102:
  1535. case CP210X_PARTNUM_CP2103:
  1536. max = 1000000;
  1537. break;
  1538. case CP210X_PARTNUM_CP2104:
  1539. use_actual_rate = true;
  1540. max = 2000000;
  1541. break;
  1542. case CP210X_PARTNUM_CP2108:
  1543. max = 2000000;
  1544. break;
  1545. case CP210X_PARTNUM_CP2105:
  1546. if (cp210x_interface_num(serial) == 0) {
  1547. use_actual_rate = true;
  1548. max = 2000000; /* ECI */
  1549. } else {
  1550. max = 921600; /* SCI */
  1551. }
  1552. break;
  1553. case CP210X_PARTNUM_CP2102N_QFN28:
  1554. case CP210X_PARTNUM_CP2102N_QFN24:
  1555. case CP210X_PARTNUM_CP2102N_QFN20:
  1556. use_actual_rate = true;
  1557. max = 3000000;
  1558. break;
  1559. default:
  1560. max = 2000000;
  1561. break;
  1562. }
  1563. priv->max_speed = max;
  1564. priv->use_actual_rate = use_actual_rate;
  1565. }
  1566. static int cp210x_attach(struct usb_serial *serial)
  1567. {
  1568. int result;
  1569. struct cp210x_serial_private *priv;
  1570. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1571. if (!priv)
  1572. return -ENOMEM;
  1573. result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
  1574. CP210X_GET_PARTNUM, &priv->partnum,
  1575. sizeof(priv->partnum));
  1576. if (result < 0) {
  1577. dev_warn(&serial->interface->dev,
  1578. "querying part number failed\n");
  1579. priv->partnum = CP210X_PARTNUM_UNKNOWN;
  1580. }
  1581. usb_set_serial_data(serial, priv);
  1582. cp210x_init_max_speed(serial);
  1583. result = cp210x_gpio_init(serial);
  1584. if (result < 0) {
  1585. dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
  1586. result);
  1587. }
  1588. return 0;
  1589. }
  1590. static void cp210x_disconnect(struct usb_serial *serial)
  1591. {
  1592. cp210x_gpio_remove(serial);
  1593. }
  1594. static void cp210x_release(struct usb_serial *serial)
  1595. {
  1596. struct cp210x_serial_private *priv = usb_get_serial_data(serial);
  1597. cp210x_gpio_remove(serial);
  1598. kfree(priv);
  1599. }
  1600. module_usb_serial_driver(serial_drivers, id_table);
  1601. MODULE_DESCRIPTION(DRIVER_DESC);
  1602. MODULE_LICENSE("GPL v2");