cp210x.c 64 KB

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