cp210x.c 47 KB

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