cp210x.c 54 KB

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