cp210x.c 65 KB

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