xref: /linux/drivers/usb/serial/cp210x.c (revision 63a8eef70ccb5199534dec56fed9759d214bfe55)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
203ee2515SGreg Kroah-Hartman /*
34cc27bd6SCraig Shelley  * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
403ee2515SGreg Kroah-Hartman  *
503ee2515SGreg Kroah-Hartman  * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
690fa41eeSJohan Hovold  * Copyright (C) 2010-2021 Johan Hovold (johan@kernel.org)
703ee2515SGreg Kroah-Hartman  *
803ee2515SGreg Kroah-Hartman  * Support to set flow control line levels using TIOCMGET and TIOCMSET
903ee2515SGreg Kroah-Hartman  * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
1003ee2515SGreg Kroah-Hartman  * control thanks to Munir Nassar nassarmu@real-time.com
1103ee2515SGreg Kroah-Hartman  *
1203ee2515SGreg Kroah-Hartman  */
1303ee2515SGreg Kroah-Hartman 
1403ee2515SGreg Kroah-Hartman #include <linux/kernel.h>
1503ee2515SGreg Kroah-Hartman #include <linux/errno.h>
1603ee2515SGreg Kroah-Hartman #include <linux/slab.h>
1703ee2515SGreg Kroah-Hartman #include <linux/tty.h>
1803ee2515SGreg Kroah-Hartman #include <linux/tty_flip.h>
1903ee2515SGreg Kroah-Hartman #include <linux/module.h>
2003ee2515SGreg Kroah-Hartman #include <linux/usb.h>
2103ee2515SGreg Kroah-Hartman #include <linux/usb/serial.h>
22cf5276ceSMartyn Welch #include <linux/gpio/driver.h>
23cf5276ceSMartyn Welch #include <linux/bitops.h>
24cf5276ceSMartyn Welch #include <linux/mutex.h>
2503ee2515SGreg Kroah-Hartman 
264cc27bd6SCraig Shelley #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
2703ee2515SGreg Kroah-Hartman 
2803ee2515SGreg Kroah-Hartman /*
2903ee2515SGreg Kroah-Hartman  * Function Prototypes
3003ee2515SGreg Kroah-Hartman  */
31a509a7e4SAlan Cox static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
324cc27bd6SCraig Shelley static void cp210x_close(struct usb_serial_port *);
33e5990874SJohan Hovold static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
34e5990874SJohan Hovold 							struct ktermios *);
354cc27bd6SCraig Shelley static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
3603ee2515SGreg Kroah-Hartman 							struct ktermios*);
376f923a01SKonstantin Shkolnyy static bool cp210x_tx_empty(struct usb_serial_port *port);
3860b33c13SAlan Cox static int cp210x_tiocmget(struct tty_struct *);
3920b9d177SAlan Cox static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
4020b9d177SAlan Cox static int cp210x_tiocmset_port(struct usb_serial_port *port,
41d2ad67b3SVomLehn 		unsigned int, unsigned int);
424cc27bd6SCraig Shelley static void cp210x_break_ctl(struct tty_struct *, int);
43cf5276ceSMartyn Welch static int cp210x_attach(struct usb_serial *);
44cf5276ceSMartyn Welch static void cp210x_disconnect(struct usb_serial *);
45cf5276ceSMartyn Welch static void cp210x_release(struct usb_serial *);
46e2ae67a3SKonstantin Shkolnyy static int cp210x_port_probe(struct usb_serial_port *);
47c5d1448fSUwe Kleine-König static void cp210x_port_remove(struct usb_serial_port *);
48daa91919SJohan Hovold static void cp210x_dtr_rts(struct usb_serial_port *port, int on);
49a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb);
50a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port);
51a7207e98SJohan Hovold static void cp210x_disable_event_mode(struct usb_serial_port *port);
5203ee2515SGreg Kroah-Hartman 
537d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
542f1136d1SDJ Delorie 	{ USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
5503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
5603ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
575b22a32eSA E Lawrence 	{ USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
58b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
5990625070SLuiz Angelo Daros de Luca 	{ USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
6003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
61b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
62a6f03312SLennart Sorensen 	{ USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
6343377df7SChenxin Jin 	{ USB_DEVICE(0x0988, 0x0578) }, /* Teraoka AD2000 */
64dd9d3d86SIvan Mironov 	{ USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
65eefd9029SCraig Shelley 	{ USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
66eefd9029SCraig Shelley 	{ USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
6703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
6803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
6903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
708de7f4daSLuis Llorente Campo 	{ USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
7103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
7203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
7303ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
74b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
750601e116SAmit Kucheria 	{ USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
760601e116SAmit Kucheria 	{ USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
7703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
7803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
79eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
80eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
8103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
8203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
83a595ecddSGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
8403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
85eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
8603ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
875bbfa6f4SMikko Tuumanen 	{ USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
8803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
8903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
9003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
9103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
9203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
938bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
9403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
9503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
96a57e82a1SSteve Conklin 	{ USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
970bf7a81cSJason Detring 	{ USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
9803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
993fcc8f96SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
10024160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
10124160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
10224160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
103eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
10403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
10503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
10608e87d0dSMalte Schröder 	{ USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
10703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
108eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
10903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
110f487c54dSPeter Dedecker 	{ USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
11103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
11203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
113bd07c551SAlan Cox 	{ USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
114b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
11503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
11603ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
11725985edcSLucas De Marchi 	{ USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
11872b30079STristan Bruns 	{ USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
119c851e83fSFlorian Fainelli 	{ USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
12024160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
12124160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
12224160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
123613ac23aSJasem Mutlaq 	{ USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
124b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
12503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
1268bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
12703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
128347bc8cbSGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
129faea63f7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
1308bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
131faea63f7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
132d23bac9fSAlex Stephens 	{ USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
133decc5360SKyle Jones 	{ USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
134541e05ecSCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
135613ac23aSJasem Mutlaq 	{ USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
13624160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
13743074132SDiego Elio Pettenò 	{ USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
13824160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
1394eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
1404eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
141356fe44fSMarkus Becker 	{ USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
1424eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
1434eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
1441ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8856) },	/* CEL EM357 ZigBee USB Stick - LR */
1451ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8857) },	/* CEL EM357 ZigBee USB Stick */
14676811569SSami Rahman 	{ USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
14776811569SSami Rahman 	{ USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
148ca667a33SKaran Singhal 	{ USB_DEVICE(0x10C4, 0x88D8) }, /* Acuity Brands nLight Air Adapter */
14924160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
15024160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
151bfc2d7dfSJoe Savage 	{ USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
1522ab13292SPaul Jakma 	{ USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
1531ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8977) },	/* CEL MeshWorks DevKit Device */
154c735ed74SMark Edwards 	{ USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
15524160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
156367b160fSOlli Salonen 	{ USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
157df72d588SJohn D. Blair 	{ USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
158fd90f73aSJeremie Rapin 	{ USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
1599585e340SStefan Triller 	{ USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
16003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
16103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
1622f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
163a5360a53SPreston Fick 	{ USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
164eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
1652f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
1662f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
16703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
16803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
16903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
17003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
17103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
172b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
1731d377f4dSMike Manning 	{ USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
1741d377f4dSMike Manning 	{ USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
17503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
176b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
1771f1e82f7SJohan Hovold 	{ USB_DEVICE(0x155A, 0x1006) },	/* ELDAT Easywave RX09 */
1783fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
1793fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
18003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
1813fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
1823fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
1833fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
1843fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
185675af708SMichiel vd Garde 	{ USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
186675af708SMichiel vd Garde 	{ USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
18703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
188356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
189356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
190356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
191356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
192c6c1e449SBruno Thomsen 	{ USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
193c6c1e449SBruno Thomsen 	{ USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
194541e05ecSCraig Shelley 	{ USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
195541e05ecSCraig Shelley 	{ USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
196541e05ecSCraig Shelley 	{ USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
197f7d7f59aSOliver Freyermuth 	{ USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
198d14ac576SChristian Holl 	{ USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
199c496ad83SAndreas Engel 	{ USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
2006627ae19SKen Lin 	{ USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
2016627ae19SKen Lin 	{ USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
202cddc9434SMartyn Welch 	{ USB_DEVICE(0x1901, 0x0194) },	/* GE Healthcare Remote Alarm Box */
2039a593656SKen Lin 	{ USB_DEVICE(0x1901, 0x0195) },	/* GE B850/B650/B450 CP2104 DP UART interface */
2049a593656SKen Lin 	{ USB_DEVICE(0x1901, 0x0196) },	/* GE B850 CP2105 DP UART interface */
20542213a01SSebastian Reichel 	{ USB_DEVICE(0x1901, 0x0197) }, /* GE CS1000 Display serial interface */
20642213a01SSebastian Reichel 	{ USB_DEVICE(0x1901, 0x0198) }, /* GE CS1000 M.2 Key E serial interface */
2073c4f6ecdSPho Tran 	{ USB_DEVICE(0x199B, 0xBA30) }, /* LORD WSDA-200-USB */
2083c4c615dSVittorio Alfieri 	{ USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
209b579fa52SBarry Grussling 	{ USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
210b9326057SAndras Kovacs 	{ USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
21135cc83eaSNathaniel Ting 	{ USB_DEVICE(0x1BA4, 0x0002) },	/* Silicon Labs 358x factory default */
21293ad03d6SAnders Larsen 	{ USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
213dee80ad1SAndreas Bomholtz 	{ USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
2143fcc8f96SCraig Shelley 	{ USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
2153fcc8f96SCraig Shelley 	{ USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
216be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
217be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
218be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
219be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
220be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
221be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
222be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
223be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
224be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
225be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
226be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
227be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
228be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
229be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
230be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
231be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
232be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
233be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
234be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
235f98a7aa8SPeter Sanford 	{ USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
236791b7d7cSRenato Caldas 	{ USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
2373fcc8f96SCraig Shelley 	{ USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
2383fcc8f96SCraig Shelley 	{ USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
2391e23aaceSKyle Roeschley 	{ USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
240541e05ecSCraig Shelley 	{ USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
24103ee2515SGreg Kroah-Hartman 	{ } /* Terminating Entry */
24203ee2515SGreg Kroah-Hartman };
24303ee2515SGreg Kroah-Hartman 
24403ee2515SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
24503ee2515SGreg Kroah-Hartman 
246cf5276ceSMartyn Welch struct cp210x_serial_private {
247cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
248cf5276ceSMartyn Welch 	struct gpio_chip	gc;
2496b7271d2SJohan Hovold 	bool			gpio_registered;
250c8acfe0aSKaroly Pados 	u8			gpio_pushpull;
251c8acfe0aSKaroly Pados 	u8			gpio_altfunc;
252c8acfe0aSKaroly Pados 	u8			gpio_input;
253cf5276ceSMartyn Welch #endif
254cf5276ceSMartyn Welch 	u8			partnum;
255*63a8eef7SJohan Hovold 	u32			fw_version;
25685bc2d91SJohanna Abrahamsson 	speed_t			min_speed;
257d4706c05SJohan Hovold 	speed_t			max_speed;
2587aecd7fcSJohan Hovold 	bool			use_actual_rate;
259*63a8eef7SJohan Hovold 	bool			no_flow_control;
260cf5276ceSMartyn Welch };
261cf5276ceSMartyn Welch 
262a7207e98SJohan Hovold enum cp210x_event_state {
263a7207e98SJohan Hovold 	ES_DATA,
264a7207e98SJohan Hovold 	ES_ESCAPE,
265a7207e98SJohan Hovold 	ES_LSR,
266a7207e98SJohan Hovold 	ES_LSR_DATA_0,
267a7207e98SJohan Hovold 	ES_LSR_DATA_1,
268a7207e98SJohan Hovold 	ES_MSR
269a7207e98SJohan Hovold };
270a7207e98SJohan Hovold 
271e2ae67a3SKonstantin Shkolnyy struct cp210x_port_private {
27216045babSJohan Hovold 	u8			bInterfaceNumber;
273a7207e98SJohan Hovold 	bool			event_mode;
274a7207e98SJohan Hovold 	enum cp210x_event_state event_state;
275a7207e98SJohan Hovold 	u8			lsr;
2765951b850SJohan Hovold 
2775951b850SJohan Hovold 	struct mutex		mutex;
2785951b850SJohan Hovold 	bool			crtscts;
2798cce3bbfSJohan Hovold 	bool			dtr;
2808cce3bbfSJohan Hovold 	bool			rts;
281a5360a53SPreston Fick };
282a5360a53SPreston Fick 
2834cc27bd6SCraig Shelley static struct usb_serial_driver cp210x_device = {
28403ee2515SGreg Kroah-Hartman 	.driver = {
28503ee2515SGreg Kroah-Hartman 		.owner =	THIS_MODULE,
2864cc27bd6SCraig Shelley 		.name =		"cp210x",
28703ee2515SGreg Kroah-Hartman 	},
28803ee2515SGreg Kroah-Hartman 	.id_table		= id_table,
28903ee2515SGreg Kroah-Hartman 	.num_ports		= 1,
290aea006b9SJohan Hovold 	.bulk_in_size		= 256,
291d4e598f6SJohan Hovold 	.bulk_out_size		= 256,
2924cc27bd6SCraig Shelley 	.open			= cp210x_open,
2934cc27bd6SCraig Shelley 	.close			= cp210x_close,
2944cc27bd6SCraig Shelley 	.break_ctl		= cp210x_break_ctl,
2954cc27bd6SCraig Shelley 	.set_termios		= cp210x_set_termios,
2966f923a01SKonstantin Shkolnyy 	.tx_empty		= cp210x_tx_empty,
2974387b3dbSBrant Merryman 	.throttle		= usb_serial_generic_throttle,
2984387b3dbSBrant Merryman 	.unthrottle		= usb_serial_generic_unthrottle,
2994cc27bd6SCraig Shelley 	.tiocmget		= cp210x_tiocmget,
3004cc27bd6SCraig Shelley 	.tiocmset		= cp210x_tiocmset,
301de9c7e9fSJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
302cf5276ceSMartyn Welch 	.attach			= cp210x_attach,
303cf5276ceSMartyn Welch 	.disconnect		= cp210x_disconnect,
304cf5276ceSMartyn Welch 	.release		= cp210x_release,
305e2ae67a3SKonstantin Shkolnyy 	.port_probe		= cp210x_port_probe,
306e2ae67a3SKonstantin Shkolnyy 	.port_remove		= cp210x_port_remove,
307a7207e98SJohan Hovold 	.dtr_rts		= cp210x_dtr_rts,
308a7207e98SJohan Hovold 	.process_read_urb	= cp210x_process_read_urb,
30903ee2515SGreg Kroah-Hartman };
31003ee2515SGreg Kroah-Hartman 
31108a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
31208a4f6bcSAlan Stern 	&cp210x_device, NULL
31308a4f6bcSAlan Stern };
31408a4f6bcSAlan Stern 
31503ee2515SGreg Kroah-Hartman /* Config request types */
31632445605SPreston Fick #define REQTYPE_HOST_TO_INTERFACE	0x41
31732445605SPreston Fick #define REQTYPE_INTERFACE_TO_HOST	0xc1
31832445605SPreston Fick #define REQTYPE_HOST_TO_DEVICE	0x40
31932445605SPreston Fick #define REQTYPE_DEVICE_TO_HOST	0xc0
32003ee2515SGreg Kroah-Hartman 
32193ef1f1fSCraig Shelley /* Config request codes */
32293ef1f1fSCraig Shelley #define CP210X_IFC_ENABLE	0x00
32393ef1f1fSCraig Shelley #define CP210X_SET_BAUDDIV	0x01
32493ef1f1fSCraig Shelley #define CP210X_GET_BAUDDIV	0x02
32593ef1f1fSCraig Shelley #define CP210X_SET_LINE_CTL	0x03
32693ef1f1fSCraig Shelley #define CP210X_GET_LINE_CTL	0x04
32793ef1f1fSCraig Shelley #define CP210X_SET_BREAK	0x05
32893ef1f1fSCraig Shelley #define CP210X_IMM_CHAR		0x06
32993ef1f1fSCraig Shelley #define CP210X_SET_MHS		0x07
33093ef1f1fSCraig Shelley #define CP210X_GET_MDMSTS	0x08
33193ef1f1fSCraig Shelley #define CP210X_SET_XON		0x09
33293ef1f1fSCraig Shelley #define CP210X_SET_XOFF		0x0A
33393ef1f1fSCraig Shelley #define CP210X_SET_EVENTMASK	0x0B
33493ef1f1fSCraig Shelley #define CP210X_GET_EVENTMASK	0x0C
33593ef1f1fSCraig Shelley #define CP210X_SET_CHAR		0x0D
33693ef1f1fSCraig Shelley #define CP210X_GET_CHARS	0x0E
33793ef1f1fSCraig Shelley #define CP210X_GET_PROPS	0x0F
33893ef1f1fSCraig Shelley #define CP210X_GET_COMM_STATUS	0x10
33993ef1f1fSCraig Shelley #define CP210X_RESET		0x11
34093ef1f1fSCraig Shelley #define CP210X_PURGE		0x12
34193ef1f1fSCraig Shelley #define CP210X_SET_FLOW		0x13
34293ef1f1fSCraig Shelley #define CP210X_GET_FLOW		0x14
34393ef1f1fSCraig Shelley #define CP210X_EMBED_EVENTS	0x15
34493ef1f1fSCraig Shelley #define CP210X_GET_EVENTSTATE	0x16
34593ef1f1fSCraig Shelley #define CP210X_SET_CHARS	0x19
3467f482fc8SPreston Fick #define CP210X_GET_BAUDRATE	0x1D
3477f482fc8SPreston Fick #define CP210X_SET_BAUDRATE	0x1E
348cf5276ceSMartyn Welch #define CP210X_VENDOR_SPECIFIC	0xFF
34903ee2515SGreg Kroah-Hartman 
35093ef1f1fSCraig Shelley /* CP210X_IFC_ENABLE */
35103ee2515SGreg Kroah-Hartman #define UART_ENABLE		0x0001
35203ee2515SGreg Kroah-Hartman #define UART_DISABLE		0x0000
35303ee2515SGreg Kroah-Hartman 
35493ef1f1fSCraig Shelley /* CP210X_(SET|GET)_BAUDDIV */
35503ee2515SGreg Kroah-Hartman #define BAUD_RATE_GEN_FREQ	0x384000
35603ee2515SGreg Kroah-Hartman 
35793ef1f1fSCraig Shelley /* CP210X_(SET|GET)_LINE_CTL */
35803ee2515SGreg Kroah-Hartman #define BITS_DATA_MASK		0X0f00
35903ee2515SGreg Kroah-Hartman #define BITS_DATA_5		0X0500
36003ee2515SGreg Kroah-Hartman #define BITS_DATA_6		0X0600
36103ee2515SGreg Kroah-Hartman #define BITS_DATA_7		0X0700
36203ee2515SGreg Kroah-Hartman #define BITS_DATA_8		0X0800
36303ee2515SGreg Kroah-Hartman #define BITS_DATA_9		0X0900
36403ee2515SGreg Kroah-Hartman 
36503ee2515SGreg Kroah-Hartman #define BITS_PARITY_MASK	0x00f0
36603ee2515SGreg Kroah-Hartman #define BITS_PARITY_NONE	0x0000
36703ee2515SGreg Kroah-Hartman #define BITS_PARITY_ODD		0x0010
36803ee2515SGreg Kroah-Hartman #define BITS_PARITY_EVEN	0x0020
36903ee2515SGreg Kroah-Hartman #define BITS_PARITY_MARK	0x0030
37003ee2515SGreg Kroah-Hartman #define BITS_PARITY_SPACE	0x0040
37103ee2515SGreg Kroah-Hartman 
37203ee2515SGreg Kroah-Hartman #define BITS_STOP_MASK		0x000f
37303ee2515SGreg Kroah-Hartman #define BITS_STOP_1		0x0000
37403ee2515SGreg Kroah-Hartman #define BITS_STOP_1_5		0x0001
37503ee2515SGreg Kroah-Hartman #define BITS_STOP_2		0x0002
37603ee2515SGreg Kroah-Hartman 
37793ef1f1fSCraig Shelley /* CP210X_SET_BREAK */
37872916791SCraig Shelley #define BREAK_ON		0x0001
37972916791SCraig Shelley #define BREAK_OFF		0x0000
38003ee2515SGreg Kroah-Hartman 
38193ef1f1fSCraig Shelley /* CP210X_(SET_MHS|GET_MDMSTS) */
38203ee2515SGreg Kroah-Hartman #define CONTROL_DTR		0x0001
38303ee2515SGreg Kroah-Hartman #define CONTROL_RTS		0x0002
38403ee2515SGreg Kroah-Hartman #define CONTROL_CTS		0x0010
38503ee2515SGreg Kroah-Hartman #define CONTROL_DSR		0x0020
38603ee2515SGreg Kroah-Hartman #define CONTROL_RING		0x0040
38703ee2515SGreg Kroah-Hartman #define CONTROL_DCD		0x0080
38803ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_DTR	0x0100
38903ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_RTS	0x0200
39003ee2515SGreg Kroah-Hartman 
3917748feffSWang Sheng Long /* CP210X_(GET|SET)_CHARS */
3927748feffSWang Sheng Long struct cp210x_special_chars {
3937748feffSWang Sheng Long 	u8	bEofChar;
3947748feffSWang Sheng Long 	u8	bErrorChar;
3957748feffSWang Sheng Long 	u8	bBreakChar;
3967748feffSWang Sheng Long 	u8	bEventChar;
3977748feffSWang Sheng Long 	u8	bXonChar;
3987748feffSWang Sheng Long 	u8	bXoffChar;
3997748feffSWang Sheng Long };
4007748feffSWang Sheng Long 
401cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC values */
402c8acfe0aSKaroly Pados #define CP210X_READ_2NCONFIG	0x000E
403*63a8eef7SJohan Hovold #define CP210X_GET_FW_VER_2N	0x0010
404cf5276ceSMartyn Welch #define CP210X_READ_LATCH	0x00C2
405cf5276ceSMartyn Welch #define CP210X_GET_PARTNUM	0x370B
406cf5276ceSMartyn Welch #define CP210X_GET_PORTCONFIG	0x370C
407cf5276ceSMartyn Welch #define CP210X_GET_DEVICEMODE	0x3711
408cf5276ceSMartyn Welch #define CP210X_WRITE_LATCH	0x37E1
409cf5276ceSMartyn Welch 
410cf5276ceSMartyn Welch /* Part number definitions */
411cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2101	0x01
412cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2102	0x02
413cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2103	0x03
414cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2104	0x04
415cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2105	0x05
416cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2108	0x08
4176f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN28	0x20
4186f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN24	0x21
4196f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN20	0x22
4207eac35eaSSebastian Frei #define CP210X_PARTNUM_UNKNOWN	0xFF
421cf5276ceSMartyn Welch 
4226f923a01SKonstantin Shkolnyy /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
4236f923a01SKonstantin Shkolnyy struct cp210x_comm_status {
4246f923a01SKonstantin Shkolnyy 	__le32   ulErrors;
4256f923a01SKonstantin Shkolnyy 	__le32   ulHoldReasons;
4266f923a01SKonstantin Shkolnyy 	__le32   ulAmountInInQueue;
4276f923a01SKonstantin Shkolnyy 	__le32   ulAmountInOutQueue;
4286f923a01SKonstantin Shkolnyy 	u8       bEofReceived;
4296f923a01SKonstantin Shkolnyy 	u8       bWaitForImmediate;
4306f923a01SKonstantin Shkolnyy 	u8       bReserved;
4316f923a01SKonstantin Shkolnyy } __packed;
4326f923a01SKonstantin Shkolnyy 
43303ee2515SGreg Kroah-Hartman /*
434ebfb319bSKonstantin Shkolnyy  * CP210X_PURGE - 16 bits passed in wValue of USB request.
435ebfb319bSKonstantin Shkolnyy  * SiLabs app note AN571 gives a strange description of the 4 bits:
436ebfb319bSKonstantin Shkolnyy  * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
437ebfb319bSKonstantin Shkolnyy  * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
438ebfb319bSKonstantin Shkolnyy  */
439ebfb319bSKonstantin Shkolnyy #define PURGE_ALL		0x000f
440ebfb319bSKonstantin Shkolnyy 
441a7207e98SJohan Hovold /* CP210X_EMBED_EVENTS */
442a7207e98SJohan Hovold #define CP210X_ESCCHAR		0xec
443a7207e98SJohan Hovold 
444a7207e98SJohan Hovold #define CP210X_LSR_OVERRUN	BIT(1)
445a7207e98SJohan Hovold #define CP210X_LSR_PARITY	BIT(2)
446a7207e98SJohan Hovold #define CP210X_LSR_FRAME	BIT(3)
447a7207e98SJohan Hovold #define CP210X_LSR_BREAK	BIT(4)
448a7207e98SJohan Hovold 
449a7207e98SJohan Hovold 
4509034389cSKonstantin Shkolnyy /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
4519034389cSKonstantin Shkolnyy struct cp210x_flow_ctl {
4529034389cSKonstantin Shkolnyy 	__le32	ulControlHandshake;
4539034389cSKonstantin Shkolnyy 	__le32	ulFlowReplace;
4549034389cSKonstantin Shkolnyy 	__le32	ulXonLimit;
4559034389cSKonstantin Shkolnyy 	__le32	ulXoffLimit;
456ba84190eSJohan Hovold };
4579034389cSKonstantin Shkolnyy 
4589034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulControlHandshake */
4599034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_MASK		GENMASK(1, 0)
460568400b1SJohan Hovold #define CP210X_SERIAL_DTR_INACTIVE	(0 << 0)
461568400b1SJohan Hovold #define CP210X_SERIAL_DTR_ACTIVE	(1 << 0)
462568400b1SJohan Hovold #define CP210X_SERIAL_DTR_FLOW_CTL	(2 << 0)
4639034389cSKonstantin Shkolnyy #define CP210X_SERIAL_CTS_HANDSHAKE	BIT(3)
4649034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_HANDSHAKE	BIT(4)
4659034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DCD_HANDSHAKE	BIT(5)
4669034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_SENSITIVITY	BIT(6)
4679034389cSKonstantin Shkolnyy 
4689034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulFlowReplace */
4699034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_TRANSMIT	BIT(0)
4709034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_RECEIVE	BIT(1)
4719034389cSKonstantin Shkolnyy #define CP210X_SERIAL_ERROR_CHAR	BIT(2)
4729034389cSKonstantin Shkolnyy #define CP210X_SERIAL_NULL_STRIPPING	BIT(3)
4739034389cSKonstantin Shkolnyy #define CP210X_SERIAL_BREAK_CHAR	BIT(4)
4749034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_MASK		GENMASK(7, 6)
475568400b1SJohan Hovold #define CP210X_SERIAL_RTS_INACTIVE	(0 << 6)
476568400b1SJohan Hovold #define CP210X_SERIAL_RTS_ACTIVE	(1 << 6)
477568400b1SJohan Hovold #define CP210X_SERIAL_RTS_FLOW_CTL	(2 << 6)
4789034389cSKonstantin Shkolnyy #define CP210X_SERIAL_XOFF_CONTINUE	BIT(31)
4799034389cSKonstantin Shkolnyy 
480cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
481cf5276ceSMartyn Welch struct cp210x_pin_mode {
482cf5276ceSMartyn Welch 	u8	eci;
483cf5276ceSMartyn Welch 	u8	sci;
484ba84190eSJohan Hovold };
485cf5276ceSMartyn Welch 
486cf5276ceSMartyn Welch #define CP210X_PIN_MODE_MODEM		0
487cf5276ceSMartyn Welch #define CP210X_PIN_MODE_GPIO		BIT(0)
488cf5276ceSMartyn Welch 
489cf5276ceSMartyn Welch /*
49015fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
49115fb84b7SIcenowy Zheng  * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
492cf5276ceSMartyn Welch  */
49315fb84b7SIcenowy Zheng struct cp210x_dual_port_config {
494cf5276ceSMartyn Welch 	__le16	gpio_mode;
495cf5276ceSMartyn Welch 	u8	__pad0[2];
496cf5276ceSMartyn Welch 	__le16	reset_state;
497cf5276ceSMartyn Welch 	u8	__pad1[4];
498cf5276ceSMartyn Welch 	__le16	suspend_state;
499cf5276ceSMartyn Welch 	u8	sci_cfg;
500cf5276ceSMartyn Welch 	u8	eci_cfg;
501cf5276ceSMartyn Welch 	u8	device_cfg;
502cf5276ceSMartyn Welch } __packed;
503cf5276ceSMartyn Welch 
50415fb84b7SIcenowy Zheng /*
50515fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
50615fb84b7SIcenowy Zheng  * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
50715fb84b7SIcenowy Zheng  */
50815fb84b7SIcenowy Zheng struct cp210x_single_port_config {
50915fb84b7SIcenowy Zheng 	__le16	gpio_mode;
51015fb84b7SIcenowy Zheng 	u8	__pad0[2];
51115fb84b7SIcenowy Zheng 	__le16	reset_state;
51215fb84b7SIcenowy Zheng 	u8	__pad1[4];
51315fb84b7SIcenowy Zheng 	__le16	suspend_state;
51415fb84b7SIcenowy Zheng 	u8	device_cfg;
51515fb84b7SIcenowy Zheng } __packed;
51615fb84b7SIcenowy Zheng 
517cf5276ceSMartyn Welch /* GPIO modes */
518cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_OFFSET	9
519cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_MASK	GENMASK(11, 9)
520cf5276ceSMartyn Welch 
521cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_OFFSET	2
522cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_MASK	GENMASK(3, 2)
523cf5276ceSMartyn Welch 
52415fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_OFFSET		8
52515fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_MASK		GENMASK(11, 8)
52615fb84b7SIcenowy Zheng 
527cf5276ceSMartyn Welch /* CP2105 port configuration values */
528cf5276ceSMartyn Welch #define CP2105_GPIO0_TXLED_MODE		BIT(0)
529cf5276ceSMartyn Welch #define CP2105_GPIO1_RXLED_MODE		BIT(1)
530cf5276ceSMartyn Welch #define CP2105_GPIO1_RS485_MODE		BIT(2)
531cf5276ceSMartyn Welch 
53215fb84b7SIcenowy Zheng /* CP2104 port configuration values */
53315fb84b7SIcenowy Zheng #define CP2104_GPIO0_TXLED_MODE		BIT(0)
53415fb84b7SIcenowy Zheng #define CP2104_GPIO1_RXLED_MODE		BIT(1)
53515fb84b7SIcenowy Zheng #define CP2104_GPIO2_RS485_MODE		BIT(2)
53615fb84b7SIcenowy Zheng 
537c8acfe0aSKaroly Pados /* CP2102N configuration array indices */
538c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_CONFIG_VERSION_IDX	2
539c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_MODE_IDX		581
540c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX	587
541c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_CONTROL_IDX	600
542c8acfe0aSKaroly Pados 
5436f7ec77cSStefan Agner /* CP2102N QFN20 port configuration values */
5446f7ec77cSStefan Agner #define CP2102N_QFN20_GPIO2_TXLED_MODE		BIT(2)
5456f7ec77cSStefan Agner #define CP2102N_QFN20_GPIO3_RXLED_MODE		BIT(3)
5466f7ec77cSStefan Agner #define CP2102N_QFN20_GPIO1_RS485_MODE		BIT(4)
5476f7ec77cSStefan Agner #define CP2102N_QFN20_GPIO0_CLK_MODE		BIT(6)
5486f7ec77cSStefan Agner 
549cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
550cf5276ceSMartyn Welch struct cp210x_gpio_write {
551cf5276ceSMartyn Welch 	u8	mask;
552cf5276ceSMartyn Welch 	u8	state;
553ba84190eSJohan Hovold };
554cf5276ceSMartyn Welch 
555cf5276ceSMartyn Welch /*
556cf5276ceSMartyn Welch  * Helper to get interface number when we only have struct usb_serial.
557cf5276ceSMartyn Welch  */
558cf5276ceSMartyn Welch static u8 cp210x_interface_num(struct usb_serial *serial)
559cf5276ceSMartyn Welch {
560cf5276ceSMartyn Welch 	struct usb_host_interface *cur_altsetting;
561cf5276ceSMartyn Welch 
562cf5276ceSMartyn Welch 	cur_altsetting = serial->interface->cur_altsetting;
563cf5276ceSMartyn Welch 
564cf5276ceSMartyn Welch 	return cur_altsetting->desc.bInterfaceNumber;
565cf5276ceSMartyn Welch }
566cf5276ceSMartyn Welch 
56703ee2515SGreg Kroah-Hartman /*
56819165b2bSKonstantin Shkolnyy  * Reads a variable-sized block of CP210X_ registers, identified by req.
56919165b2bSKonstantin Shkolnyy  * Returns data into buf in native USB byte order.
57003ee2515SGreg Kroah-Hartman  */
57119165b2bSKonstantin Shkolnyy static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
57219165b2bSKonstantin Shkolnyy 		void *buf, int bufsize)
57303ee2515SGreg Kroah-Hartman {
57403ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
575e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
57619165b2bSKonstantin Shkolnyy 	void *dmabuf;
57719165b2bSKonstantin Shkolnyy 	int result;
57803ee2515SGreg Kroah-Hartman 
57919165b2bSKonstantin Shkolnyy 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
580ed921771SJohan Hovold 	if (!dmabuf)
58103ee2515SGreg Kroah-Hartman 		return -ENOMEM;
58203ee2515SGreg Kroah-Hartman 
58303ee2515SGreg Kroah-Hartman 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
58419165b2bSKonstantin Shkolnyy 			req, REQTYPE_INTERFACE_TO_HOST, 0,
58519165b2bSKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
58619165b2bSKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
58719165b2bSKonstantin Shkolnyy 	if (result == bufsize) {
58819165b2bSKonstantin Shkolnyy 		memcpy(buf, dmabuf, bufsize);
58919165b2bSKonstantin Shkolnyy 		result = 0;
59019165b2bSKonstantin Shkolnyy 	} else {
59119165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
59219165b2bSKonstantin Shkolnyy 				req, bufsize, result);
59319165b2bSKonstantin Shkolnyy 		if (result >= 0)
5940407f1ceSJohan Hovold 			result = -EIO;
59519165b2bSKonstantin Shkolnyy 	}
59619165b2bSKonstantin Shkolnyy 
59719165b2bSKonstantin Shkolnyy 	kfree(dmabuf);
59819165b2bSKonstantin Shkolnyy 
5992479e2a9SJohan Hovold 	return result;
60003ee2515SGreg Kroah-Hartman }
60103ee2515SGreg Kroah-Hartman 
60219165b2bSKonstantin Shkolnyy /*
603fe1b07e9SKonstantin Shkolnyy  * Reads any 8-bit CP210X_ register identified by req.
604fe1b07e9SKonstantin Shkolnyy  */
605fe1b07e9SKonstantin Shkolnyy static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
606fe1b07e9SKonstantin Shkolnyy {
607fe1b07e9SKonstantin Shkolnyy 	return cp210x_read_reg_block(port, req, val, sizeof(*val));
608fe1b07e9SKonstantin Shkolnyy }
609fe1b07e9SKonstantin Shkolnyy 
610fe1b07e9SKonstantin Shkolnyy /*
611cf5276ceSMartyn Welch  * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
612cf5276ceSMartyn Welch  * Returns data into buf in native USB byte order.
613cf5276ceSMartyn Welch  */
614cf5276ceSMartyn Welch static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
615cf5276ceSMartyn Welch 				    void *buf, int bufsize)
616cf5276ceSMartyn Welch {
617cf5276ceSMartyn Welch 	void *dmabuf;
618cf5276ceSMartyn Welch 	int result;
619cf5276ceSMartyn Welch 
620cf5276ceSMartyn Welch 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
621cf5276ceSMartyn Welch 	if (!dmabuf)
622cf5276ceSMartyn Welch 		return -ENOMEM;
623cf5276ceSMartyn Welch 
624cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
625cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
626cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
627cf5276ceSMartyn Welch 				 USB_CTRL_GET_TIMEOUT);
628cf5276ceSMartyn Welch 	if (result == bufsize) {
629cf5276ceSMartyn Welch 		memcpy(buf, dmabuf, bufsize);
630cf5276ceSMartyn Welch 		result = 0;
631cf5276ceSMartyn Welch 	} else {
632cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
633cf5276ceSMartyn Welch 			"failed to get vendor val 0x%04x size %d: %d\n", val,
634cf5276ceSMartyn Welch 			bufsize, result);
635cf5276ceSMartyn Welch 		if (result >= 0)
636cf5276ceSMartyn Welch 			result = -EIO;
637cf5276ceSMartyn Welch 	}
638cf5276ceSMartyn Welch 
639cf5276ceSMartyn Welch 	kfree(dmabuf);
640cf5276ceSMartyn Welch 
641cf5276ceSMartyn Welch 	return result;
642cf5276ceSMartyn Welch }
643cf5276ceSMartyn Welch 
644cf5276ceSMartyn Welch /*
64519165b2bSKonstantin Shkolnyy  * Writes any 16-bit CP210X_ register (req) whose value is passed
64619165b2bSKonstantin Shkolnyy  * entirely in the wValue field of the USB request.
64719165b2bSKonstantin Shkolnyy  */
64819165b2bSKonstantin Shkolnyy static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
64903ee2515SGreg Kroah-Hartman {
65003ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
651e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
65219165b2bSKonstantin Shkolnyy 	int result;
65303ee2515SGreg Kroah-Hartman 
65419165b2bSKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
65519165b2bSKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, val,
656e2ae67a3SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, NULL, 0,
6572d5733fcSYuri Matylitski 			USB_CTRL_SET_TIMEOUT);
65819165b2bSKonstantin Shkolnyy 	if (result < 0) {
65919165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed set request 0x%x status: %d\n",
66019165b2bSKonstantin Shkolnyy 				req, result);
66103ee2515SGreg Kroah-Hartman 	}
66203ee2515SGreg Kroah-Hartman 
6632479e2a9SJohan Hovold 	return result;
66403ee2515SGreg Kroah-Hartman }
66503ee2515SGreg Kroah-Hartman 
66603ee2515SGreg Kroah-Hartman /*
667fe1b07e9SKonstantin Shkolnyy  * Writes a variable-sized block of CP210X_ registers, identified by req.
668fe1b07e9SKonstantin Shkolnyy  * Data in buf must be in native USB byte order.
669fe1b07e9SKonstantin Shkolnyy  */
670fe1b07e9SKonstantin Shkolnyy static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
671fe1b07e9SKonstantin Shkolnyy 		void *buf, int bufsize)
672fe1b07e9SKonstantin Shkolnyy {
673fe1b07e9SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
674fe1b07e9SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
675fe1b07e9SKonstantin Shkolnyy 	void *dmabuf;
676fe1b07e9SKonstantin Shkolnyy 	int result;
677fe1b07e9SKonstantin Shkolnyy 
678d6c4dc3bSMuhammad Falak R Wani 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
679fe1b07e9SKonstantin Shkolnyy 	if (!dmabuf)
680fe1b07e9SKonstantin Shkolnyy 		return -ENOMEM;
681fe1b07e9SKonstantin Shkolnyy 
682fe1b07e9SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
683fe1b07e9SKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, 0,
684fe1b07e9SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
685fe1b07e9SKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
686fe1b07e9SKonstantin Shkolnyy 
687fe1b07e9SKonstantin Shkolnyy 	kfree(dmabuf);
688fe1b07e9SKonstantin Shkolnyy 
689dc5338fcSJohan Hovold 	if (result < 0) {
690fe1b07e9SKonstantin Shkolnyy 		dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
691fe1b07e9SKonstantin Shkolnyy 				req, bufsize, result);
692dc5338fcSJohan Hovold 		return result;
693fe1b07e9SKonstantin Shkolnyy 	}
694fe1b07e9SKonstantin Shkolnyy 
695dc5338fcSJohan Hovold 	return 0;
69603ee2515SGreg Kroah-Hartman }
69703ee2515SGreg Kroah-Hartman 
69803ee2515SGreg Kroah-Hartman /*
699fe1b07e9SKonstantin Shkolnyy  * Writes any 32-bit CP210X_ register identified by req.
70003ee2515SGreg Kroah-Hartman  */
701fe1b07e9SKonstantin Shkolnyy static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
70203ee2515SGreg Kroah-Hartman {
703fe1b07e9SKonstantin Shkolnyy 	__le32 le32_val;
704fe1b07e9SKonstantin Shkolnyy 
705fe1b07e9SKonstantin Shkolnyy 	le32_val = cpu_to_le32(val);
706fe1b07e9SKonstantin Shkolnyy 
707fe1b07e9SKonstantin Shkolnyy 	return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
70803ee2515SGreg Kroah-Hartman }
70903ee2515SGreg Kroah-Hartman 
710cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
711cf5276ceSMartyn Welch /*
712cf5276ceSMartyn Welch  * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
713cf5276ceSMartyn Welch  * Data in buf must be in native USB byte order.
714cf5276ceSMartyn Welch  */
715cf5276ceSMartyn Welch static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
716cf5276ceSMartyn Welch 				     u16 val, void *buf, int bufsize)
717cf5276ceSMartyn Welch {
718cf5276ceSMartyn Welch 	void *dmabuf;
719cf5276ceSMartyn Welch 	int result;
720cf5276ceSMartyn Welch 
721cf5276ceSMartyn Welch 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
722cf5276ceSMartyn Welch 	if (!dmabuf)
723cf5276ceSMartyn Welch 		return -ENOMEM;
724cf5276ceSMartyn Welch 
725cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
726cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
727cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
728cf5276ceSMartyn Welch 				 USB_CTRL_SET_TIMEOUT);
729cf5276ceSMartyn Welch 
730cf5276ceSMartyn Welch 	kfree(dmabuf);
731cf5276ceSMartyn Welch 
732dc5338fcSJohan Hovold 	if (result < 0) {
733cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
734cf5276ceSMartyn Welch 			"failed to set vendor val 0x%04x size %d: %d\n", val,
735cf5276ceSMartyn Welch 			bufsize, result);
736dc5338fcSJohan Hovold 		return result;
737cf5276ceSMartyn Welch 	}
738cf5276ceSMartyn Welch 
739dc5338fcSJohan Hovold 	return 0;
740cf5276ceSMartyn Welch }
741cf5276ceSMartyn Welch #endif
742cf5276ceSMartyn Welch 
743a509a7e4SAlan Cox static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
74403ee2515SGreg Kroah-Hartman {
745a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
74603ee2515SGreg Kroah-Hartman 	int result;
74703ee2515SGreg Kroah-Hartman 
74819165b2bSKonstantin Shkolnyy 	result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
7492479e2a9SJohan Hovold 	if (result) {
7502479e2a9SJohan Hovold 		dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
7512479e2a9SJohan Hovold 		return result;
75203ee2515SGreg Kroah-Hartman 	}
75303ee2515SGreg Kroah-Hartman 
75446827bdaSJohan Hovold 	if (tty)
75546827bdaSJohan Hovold 		cp210x_set_termios(tty, port, NULL);
756a7207e98SJohan Hovold 
757bcbb9d81SJohan Hovold 	result = usb_serial_generic_open(tty, port);
758bcbb9d81SJohan Hovold 	if (result)
759bcbb9d81SJohan Hovold 		goto err_disable;
760bcbb9d81SJohan Hovold 
761bcbb9d81SJohan Hovold 	return 0;
762bcbb9d81SJohan Hovold 
763bcbb9d81SJohan Hovold err_disable:
764bcbb9d81SJohan Hovold 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
765a7207e98SJohan Hovold 	port_priv->event_mode = false;
766bcbb9d81SJohan Hovold 
767bcbb9d81SJohan Hovold 	return result;
76803ee2515SGreg Kroah-Hartman }
76903ee2515SGreg Kroah-Hartman 
7704cc27bd6SCraig Shelley static void cp210x_close(struct usb_serial_port *port)
77103ee2515SGreg Kroah-Hartman {
772a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
773a7207e98SJohan Hovold 
774f26788daSJohan Hovold 	usb_serial_generic_close(port);
775ebfb319bSKonstantin Shkolnyy 
776ebfb319bSKonstantin Shkolnyy 	/* Clear both queues; cp2108 needs this to avoid an occasional hang */
77719165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
778ebfb319bSKonstantin Shkolnyy 
77919165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
780a7207e98SJohan Hovold 
781a7207e98SJohan Hovold 	/* Disabling the interface disables event-insertion mode. */
782a7207e98SJohan Hovold 	port_priv->event_mode = false;
783a7207e98SJohan Hovold }
784a7207e98SJohan Hovold 
785a7207e98SJohan Hovold static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
786a7207e98SJohan Hovold {
787a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_BREAK) {
788a7207e98SJohan Hovold 		port->icount.brk++;
789a7207e98SJohan Hovold 		*flag = TTY_BREAK;
790a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_PARITY) {
791a7207e98SJohan Hovold 		port->icount.parity++;
792a7207e98SJohan Hovold 		*flag = TTY_PARITY;
793a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_FRAME) {
794a7207e98SJohan Hovold 		port->icount.frame++;
795a7207e98SJohan Hovold 		*flag = TTY_FRAME;
796a7207e98SJohan Hovold 	}
797a7207e98SJohan Hovold 
798a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_OVERRUN) {
799a7207e98SJohan Hovold 		port->icount.overrun++;
800a7207e98SJohan Hovold 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
801a7207e98SJohan Hovold 	}
802a7207e98SJohan Hovold }
803a7207e98SJohan Hovold 
804a7207e98SJohan Hovold static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
805a7207e98SJohan Hovold {
806a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
807a7207e98SJohan Hovold 
808a7207e98SJohan Hovold 	switch (port_priv->event_state) {
809a7207e98SJohan Hovold 	case ES_DATA:
810a7207e98SJohan Hovold 		if (*ch == CP210X_ESCCHAR) {
811a7207e98SJohan Hovold 			port_priv->event_state = ES_ESCAPE;
812a7207e98SJohan Hovold 			break;
813a7207e98SJohan Hovold 		}
814a7207e98SJohan Hovold 		return false;
815a7207e98SJohan Hovold 	case ES_ESCAPE:
816a7207e98SJohan Hovold 		switch (*ch) {
817a7207e98SJohan Hovold 		case 0:
818a7207e98SJohan Hovold 			dev_dbg(&port->dev, "%s - escape char\n", __func__);
819a7207e98SJohan Hovold 			*ch = CP210X_ESCCHAR;
820a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
821a7207e98SJohan Hovold 			return false;
822a7207e98SJohan Hovold 		case 1:
823a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR_DATA_0;
824a7207e98SJohan Hovold 			break;
825a7207e98SJohan Hovold 		case 2:
826a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR;
827a7207e98SJohan Hovold 			break;
828a7207e98SJohan Hovold 		case 3:
829a7207e98SJohan Hovold 			port_priv->event_state = ES_MSR;
830a7207e98SJohan Hovold 			break;
831a7207e98SJohan Hovold 		default:
832a7207e98SJohan Hovold 			dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
833a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
834a7207e98SJohan Hovold 			break;
835a7207e98SJohan Hovold 		}
836a7207e98SJohan Hovold 		break;
837a7207e98SJohan Hovold 	case ES_LSR_DATA_0:
838a7207e98SJohan Hovold 		port_priv->lsr = *ch;
839a7207e98SJohan Hovold 		port_priv->event_state = ES_LSR_DATA_1;
840a7207e98SJohan Hovold 		break;
841a7207e98SJohan Hovold 	case ES_LSR_DATA_1:
842a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
843a7207e98SJohan Hovold 				__func__, port_priv->lsr, *ch);
844a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
845a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
846a7207e98SJohan Hovold 		return false;
847a7207e98SJohan Hovold 	case ES_LSR:
848a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
849a7207e98SJohan Hovold 		port_priv->lsr = *ch;
850a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
851a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
852a7207e98SJohan Hovold 		break;
853a7207e98SJohan Hovold 	case ES_MSR:
854a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
855a7207e98SJohan Hovold 		/* unimplemented */
856a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
857a7207e98SJohan Hovold 		break;
858a7207e98SJohan Hovold 	}
859a7207e98SJohan Hovold 
860a7207e98SJohan Hovold 	return true;
861a7207e98SJohan Hovold }
862a7207e98SJohan Hovold 
863a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb)
864a7207e98SJohan Hovold {
865a7207e98SJohan Hovold 	struct usb_serial_port *port = urb->context;
866a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
867a7207e98SJohan Hovold 	unsigned char *ch = urb->transfer_buffer;
868a7207e98SJohan Hovold 	char flag;
869a7207e98SJohan Hovold 	int i;
870a7207e98SJohan Hovold 
871a7207e98SJohan Hovold 	if (!urb->actual_length)
872a7207e98SJohan Hovold 		return;
873a7207e98SJohan Hovold 
874a7207e98SJohan Hovold 	if (port_priv->event_mode) {
875a7207e98SJohan Hovold 		for (i = 0; i < urb->actual_length; i++, ch++) {
876a7207e98SJohan Hovold 			flag = TTY_NORMAL;
877a7207e98SJohan Hovold 
878a7207e98SJohan Hovold 			if (cp210x_process_char(port, ch, &flag))
879a7207e98SJohan Hovold 				continue;
880a7207e98SJohan Hovold 
881a7207e98SJohan Hovold 			tty_insert_flip_char(&port->port, *ch, flag);
882a7207e98SJohan Hovold 		}
883a7207e98SJohan Hovold 	} else {
884a7207e98SJohan Hovold 		tty_insert_flip_string(&port->port, ch, urb->actual_length);
885a7207e98SJohan Hovold 	}
886a7207e98SJohan Hovold 	tty_flip_buffer_push(&port->port);
88703ee2515SGreg Kroah-Hartman }
88803ee2515SGreg Kroah-Hartman 
88903ee2515SGreg Kroah-Hartman /*
8906f923a01SKonstantin Shkolnyy  * Read how many bytes are waiting in the TX queue.
8916f923a01SKonstantin Shkolnyy  */
8926f923a01SKonstantin Shkolnyy static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
8936f923a01SKonstantin Shkolnyy 		u32 *count)
8946f923a01SKonstantin Shkolnyy {
8956f923a01SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
8966f923a01SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
8976f923a01SKonstantin Shkolnyy 	struct cp210x_comm_status *sts;
8986f923a01SKonstantin Shkolnyy 	int result;
8996f923a01SKonstantin Shkolnyy 
9006f923a01SKonstantin Shkolnyy 	sts = kmalloc(sizeof(*sts), GFP_KERNEL);
9016f923a01SKonstantin Shkolnyy 	if (!sts)
9026f923a01SKonstantin Shkolnyy 		return -ENOMEM;
9036f923a01SKonstantin Shkolnyy 
9046f923a01SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
9056f923a01SKonstantin Shkolnyy 			CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
9066f923a01SKonstantin Shkolnyy 			0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
9076f923a01SKonstantin Shkolnyy 			USB_CTRL_GET_TIMEOUT);
9086f923a01SKonstantin Shkolnyy 	if (result == sizeof(*sts)) {
9096f923a01SKonstantin Shkolnyy 		*count = le32_to_cpu(sts->ulAmountInOutQueue);
9106f923a01SKonstantin Shkolnyy 		result = 0;
9116f923a01SKonstantin Shkolnyy 	} else {
9126f923a01SKonstantin Shkolnyy 		dev_err(&port->dev, "failed to get comm status: %d\n", result);
9136f923a01SKonstantin Shkolnyy 		if (result >= 0)
9140407f1ceSJohan Hovold 			result = -EIO;
9156f923a01SKonstantin Shkolnyy 	}
9166f923a01SKonstantin Shkolnyy 
9176f923a01SKonstantin Shkolnyy 	kfree(sts);
9186f923a01SKonstantin Shkolnyy 
9196f923a01SKonstantin Shkolnyy 	return result;
9206f923a01SKonstantin Shkolnyy }
9216f923a01SKonstantin Shkolnyy 
9226f923a01SKonstantin Shkolnyy static bool cp210x_tx_empty(struct usb_serial_port *port)
9236f923a01SKonstantin Shkolnyy {
9246f923a01SKonstantin Shkolnyy 	int err;
9256f923a01SKonstantin Shkolnyy 	u32 count;
9266f923a01SKonstantin Shkolnyy 
9276f923a01SKonstantin Shkolnyy 	err = cp210x_get_tx_queue_byte_count(port, &count);
9286f923a01SKonstantin Shkolnyy 	if (err)
9296f923a01SKonstantin Shkolnyy 		return true;
9306f923a01SKonstantin Shkolnyy 
9316f923a01SKonstantin Shkolnyy 	return !count;
9326f923a01SKonstantin Shkolnyy }
9336f923a01SKonstantin Shkolnyy 
93495fd4f47SJohan Hovold struct cp210x_rate {
93595fd4f47SJohan Hovold 	speed_t rate;
93695fd4f47SJohan Hovold 	speed_t high;
93795fd4f47SJohan Hovold };
93895fd4f47SJohan Hovold 
93995fd4f47SJohan Hovold static const struct cp210x_rate cp210x_an205_table1[] = {
94095fd4f47SJohan Hovold 	{ 300, 300 },
94195fd4f47SJohan Hovold 	{ 600, 600 },
94295fd4f47SJohan Hovold 	{ 1200, 1200 },
94395fd4f47SJohan Hovold 	{ 1800, 1800 },
94495fd4f47SJohan Hovold 	{ 2400, 2400 },
94595fd4f47SJohan Hovold 	{ 4000, 4000 },
94695fd4f47SJohan Hovold 	{ 4800, 4803 },
94795fd4f47SJohan Hovold 	{ 7200, 7207 },
94895fd4f47SJohan Hovold 	{ 9600, 9612 },
94995fd4f47SJohan Hovold 	{ 14400, 14428 },
95095fd4f47SJohan Hovold 	{ 16000, 16062 },
95195fd4f47SJohan Hovold 	{ 19200, 19250 },
95295fd4f47SJohan Hovold 	{ 28800, 28912 },
95395fd4f47SJohan Hovold 	{ 38400, 38601 },
95495fd4f47SJohan Hovold 	{ 51200, 51558 },
95595fd4f47SJohan Hovold 	{ 56000, 56280 },
95695fd4f47SJohan Hovold 	{ 57600, 58053 },
95795fd4f47SJohan Hovold 	{ 64000, 64111 },
95895fd4f47SJohan Hovold 	{ 76800, 77608 },
95995fd4f47SJohan Hovold 	{ 115200, 117028 },
96095fd4f47SJohan Hovold 	{ 128000, 129347 },
96195fd4f47SJohan Hovold 	{ 153600, 156868 },
96295fd4f47SJohan Hovold 	{ 230400, 237832 },
96395fd4f47SJohan Hovold 	{ 250000, 254234 },
96495fd4f47SJohan Hovold 	{ 256000, 273066 },
96595fd4f47SJohan Hovold 	{ 460800, 491520 },
96695fd4f47SJohan Hovold 	{ 500000, 567138 },
96795fd4f47SJohan Hovold 	{ 576000, 670254 },
96895fd4f47SJohan Hovold 	{ 921600, UINT_MAX }
96995fd4f47SJohan Hovold };
97095fd4f47SJohan Hovold 
97195fd4f47SJohan Hovold /*
97295fd4f47SJohan Hovold  * Quantises the baud rate as per AN205 Table 1
97395fd4f47SJohan Hovold  */
97495fd4f47SJohan Hovold static speed_t cp210x_get_an205_rate(speed_t baud)
97595fd4f47SJohan Hovold {
97695fd4f47SJohan Hovold 	int i;
97795fd4f47SJohan Hovold 
97895fd4f47SJohan Hovold 	for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
97995fd4f47SJohan Hovold 		if (baud <= cp210x_an205_table1[i].high)
98095fd4f47SJohan Hovold 			break;
98195fd4f47SJohan Hovold 	}
98295fd4f47SJohan Hovold 
98395fd4f47SJohan Hovold 	return cp210x_an205_table1[i].rate;
98495fd4f47SJohan Hovold }
98595fd4f47SJohan Hovold 
98685bc2d91SJohanna Abrahamsson static speed_t cp210x_get_actual_rate(speed_t baud)
9877aecd7fcSJohan Hovold {
9887aecd7fcSJohan Hovold 	unsigned int prescale = 1;
9897aecd7fcSJohan Hovold 	unsigned int div;
9907aecd7fcSJohan Hovold 
9917aecd7fcSJohan Hovold 	if (baud <= 365)
9927aecd7fcSJohan Hovold 		prescale = 4;
9937aecd7fcSJohan Hovold 
9947aecd7fcSJohan Hovold 	div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
9957aecd7fcSJohan Hovold 	baud = 48000000 / (2 * prescale * div);
9967aecd7fcSJohan Hovold 
9977aecd7fcSJohan Hovold 	return baud;
9987aecd7fcSJohan Hovold }
9997aecd7fcSJohan Hovold 
1000e5990874SJohan Hovold /*
1001e5990874SJohan Hovold  * CP2101 supports the following baud rates:
1002e5990874SJohan Hovold  *
1003e5990874SJohan Hovold  *	300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1004e5990874SJohan Hovold  *	38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1005e5990874SJohan Hovold  *
1006e5990874SJohan Hovold  * CP2102 and CP2103 support the following additional rates:
1007e5990874SJohan Hovold  *
1008e5990874SJohan Hovold  *	4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1009e5990874SJohan Hovold  *	576000
1010e5990874SJohan Hovold  *
1011e5990874SJohan Hovold  * The device will map a requested rate to a supported one, but the result
1012e5990874SJohan Hovold  * of requests for rates greater than 1053257 is undefined (see AN205).
1013e5990874SJohan Hovold  *
1014e5990874SJohan Hovold  * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1015e5990874SJohan Hovold  * respectively, with an error less than 1%. The actual rates are determined
1016e5990874SJohan Hovold  * by
1017e5990874SJohan Hovold  *
1018e5990874SJohan Hovold  *	div = round(freq / (2 x prescale x request))
1019e5990874SJohan Hovold  *	actual = freq / (2 x prescale x div)
1020e5990874SJohan Hovold  *
1021e5990874SJohan Hovold  * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1022e5990874SJohan Hovold  * or 1 otherwise.
1023e5990874SJohan Hovold  * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1024e5990874SJohan Hovold  * otherwise.
1025e5990874SJohan Hovold  */
1026e5990874SJohan Hovold static void cp210x_change_speed(struct tty_struct *tty,
1027e5990874SJohan Hovold 		struct usb_serial_port *port, struct ktermios *old_termios)
1028e5990874SJohan Hovold {
10296f0bcf72SKaroly Pados 	struct usb_serial *serial = port->serial;
10306f0bcf72SKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1031e5990874SJohan Hovold 	u32 baud;
1032e5990874SJohan Hovold 
10336f0bcf72SKaroly Pados 	/*
10347aecd7fcSJohan Hovold 	 * This maps the requested rate to the actual rate, a valid rate on
10357aecd7fcSJohan Hovold 	 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
1036e5990874SJohan Hovold 	 *
1037e5990874SJohan Hovold 	 * NOTE: B0 is not implemented.
1038e5990874SJohan Hovold 	 */
103985bc2d91SJohanna Abrahamsson 	baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
104085bc2d91SJohanna Abrahamsson 
10417aecd7fcSJohan Hovold 	if (priv->use_actual_rate)
104285bc2d91SJohanna Abrahamsson 		baud = cp210x_get_actual_rate(baud);
10437aecd7fcSJohan Hovold 	else if (baud < 1000000)
104495fd4f47SJohan Hovold 		baud = cp210x_get_an205_rate(baud);
1045e5990874SJohan Hovold 
1046660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
1047fe1b07e9SKonstantin Shkolnyy 	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
1048e5990874SJohan Hovold 		dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1049e5990874SJohan Hovold 		if (old_termios)
1050e5990874SJohan Hovold 			baud = old_termios->c_ospeed;
1051e5990874SJohan Hovold 		else
1052e5990874SJohan Hovold 			baud = 9600;
1053e5990874SJohan Hovold 	}
1054e5990874SJohan Hovold 
1055e5990874SJohan Hovold 	tty_encode_baud_rate(tty, baud, baud);
1056e5990874SJohan Hovold }
1057e5990874SJohan Hovold 
1058a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port)
1059a7207e98SJohan Hovold {
1060a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1061a7207e98SJohan Hovold 	int ret;
1062a7207e98SJohan Hovold 
1063a7207e98SJohan Hovold 	if (port_priv->event_mode)
1064a7207e98SJohan Hovold 		return;
1065a7207e98SJohan Hovold 
1066a7207e98SJohan Hovold 	port_priv->event_state = ES_DATA;
1067a7207e98SJohan Hovold 	port_priv->event_mode = true;
1068a7207e98SJohan Hovold 
1069a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1070a7207e98SJohan Hovold 	if (ret) {
1071a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to enable events: %d\n", ret);
1072a7207e98SJohan Hovold 		port_priv->event_mode = false;
1073a7207e98SJohan Hovold 	}
1074a7207e98SJohan Hovold }
1075a7207e98SJohan Hovold 
1076a7207e98SJohan Hovold static void cp210x_disable_event_mode(struct usb_serial_port *port)
1077a7207e98SJohan Hovold {
1078a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1079a7207e98SJohan Hovold 	int ret;
1080a7207e98SJohan Hovold 
1081a7207e98SJohan Hovold 	if (!port_priv->event_mode)
1082a7207e98SJohan Hovold 		return;
1083a7207e98SJohan Hovold 
1084a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
1085a7207e98SJohan Hovold 	if (ret) {
1086a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to disable events: %d\n", ret);
1087a7207e98SJohan Hovold 		return;
1088a7207e98SJohan Hovold 	}
1089a7207e98SJohan Hovold 
1090a7207e98SJohan Hovold 	port_priv->event_mode = false;
1091a7207e98SJohan Hovold }
1092a7207e98SJohan Hovold 
10937748feffSWang Sheng Long static int cp210x_set_chars(struct usb_serial_port *port,
10947748feffSWang Sheng Long 		struct cp210x_special_chars *chars)
10957748feffSWang Sheng Long {
10967748feffSWang Sheng Long 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
10977748feffSWang Sheng Long 	struct usb_serial *serial = port->serial;
10987748feffSWang Sheng Long 	void *dmabuf;
10997748feffSWang Sheng Long 	int result;
11007748feffSWang Sheng Long 
11017748feffSWang Sheng Long 	dmabuf = kmemdup(chars, sizeof(*chars), GFP_KERNEL);
11027748feffSWang Sheng Long 	if (!dmabuf)
11037748feffSWang Sheng Long 		return -ENOMEM;
11047748feffSWang Sheng Long 
11057748feffSWang Sheng Long 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
11067748feffSWang Sheng Long 				CP210X_SET_CHARS, REQTYPE_HOST_TO_INTERFACE, 0,
11077748feffSWang Sheng Long 				port_priv->bInterfaceNumber,
11087748feffSWang Sheng Long 				dmabuf, sizeof(*chars), USB_CTRL_SET_TIMEOUT);
11097748feffSWang Sheng Long 
11107748feffSWang Sheng Long 	kfree(dmabuf);
11117748feffSWang Sheng Long 
11127748feffSWang Sheng Long 	if (result < 0) {
11137748feffSWang Sheng Long 		dev_err(&port->dev, "failed to set special chars: %d\n", result);
11147748feffSWang Sheng Long 		return result;
11157748feffSWang Sheng Long 	}
11167748feffSWang Sheng Long 
11177748feffSWang Sheng Long 	return 0;
11187748feffSWang Sheng Long }
11197748feffSWang Sheng Long 
112095168d62SJohan Hovold static bool cp210x_termios_change(const struct ktermios *a, const struct ktermios *b)
112195168d62SJohan Hovold {
112203f32d7cSJohan Hovold 	bool iflag_change, cc_change;
112395168d62SJohan Hovold 
11247748feffSWang Sheng Long 	iflag_change = ((a->c_iflag ^ b->c_iflag) & (INPCK | IXON | IXOFF));
112503f32d7cSJohan Hovold 	cc_change = a->c_cc[VSTART] != b->c_cc[VSTART] ||
112603f32d7cSJohan Hovold 			a->c_cc[VSTOP] != b->c_cc[VSTOP];
112795168d62SJohan Hovold 
112803f32d7cSJohan Hovold 	return tty_termios_hw_change(a, b) || iflag_change || cc_change;
112995168d62SJohan Hovold }
113095168d62SJohan Hovold 
1131ed921771SJohan Hovold static void cp210x_set_flow_control(struct tty_struct *tty,
1132ed921771SJohan Hovold 		struct usb_serial_port *port, struct ktermios *old_termios)
1133ed921771SJohan Hovold {
1134*63a8eef7SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
11355951b850SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
11367748feffSWang Sheng Long 	struct cp210x_special_chars chars;
1137ed921771SJohan Hovold 	struct cp210x_flow_ctl flow_ctl;
1138ed921771SJohan Hovold 	u32 flow_repl;
1139ed921771SJohan Hovold 	u32 ctl_hs;
1140ed921771SJohan Hovold 	int ret;
1141ed921771SJohan Hovold 
1142*63a8eef7SJohan Hovold 	/*
1143*63a8eef7SJohan Hovold 	 * Some CP2102N interpret ulXonLimit as ulFlowReplace (erratum
1144*63a8eef7SJohan Hovold 	 * CP2102N_E104). Report back that flow control is not supported.
1145*63a8eef7SJohan Hovold 	 */
1146*63a8eef7SJohan Hovold 	if (priv->no_flow_control) {
1147*63a8eef7SJohan Hovold 		tty->termios.c_cflag &= ~CRTSCTS;
1148*63a8eef7SJohan Hovold 		tty->termios.c_iflag &= ~(IXON | IXOFF);
1149*63a8eef7SJohan Hovold 	}
1150*63a8eef7SJohan Hovold 
11517748feffSWang Sheng Long 	if (old_termios &&
11527748feffSWang Sheng Long 			C_CRTSCTS(tty) == (old_termios->c_cflag & CRTSCTS) &&
11537748feffSWang Sheng Long 			I_IXON(tty) == (old_termios->c_iflag & IXON) &&
115403f32d7cSJohan Hovold 			I_IXOFF(tty) == (old_termios->c_iflag & IXOFF) &&
115503f32d7cSJohan Hovold 			START_CHAR(tty) == old_termios->c_cc[VSTART] &&
115603f32d7cSJohan Hovold 			STOP_CHAR(tty) == old_termios->c_cc[VSTOP]) {
1157ed921771SJohan Hovold 		return;
11587748feffSWang Sheng Long 	}
11597748feffSWang Sheng Long 
11607748feffSWang Sheng Long 	if (I_IXON(tty) || I_IXOFF(tty)) {
11617748feffSWang Sheng Long 		memset(&chars, 0, sizeof(chars));
11627748feffSWang Sheng Long 
11637748feffSWang Sheng Long 		chars.bXonChar = START_CHAR(tty);
11647748feffSWang Sheng Long 		chars.bXoffChar = STOP_CHAR(tty);
11657748feffSWang Sheng Long 
11667748feffSWang Sheng Long 		ret = cp210x_set_chars(port, &chars);
11677748feffSWang Sheng Long 		if (ret)
11687748feffSWang Sheng Long 			return;
11697748feffSWang Sheng Long 	}
1170ed921771SJohan Hovold 
11715951b850SJohan Hovold 	mutex_lock(&port_priv->mutex);
1172ed921771SJohan Hovold 
1173ed921771SJohan Hovold 	ret = cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1174ed921771SJohan Hovold 			sizeof(flow_ctl));
1175ed921771SJohan Hovold 	if (ret)
11765951b850SJohan Hovold 		goto out_unlock;
1177ed921771SJohan Hovold 
1178ed921771SJohan Hovold 	ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1179ed921771SJohan Hovold 	flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1180ed921771SJohan Hovold 
1181ed921771SJohan Hovold 	ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1182ed921771SJohan Hovold 	ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1183ed921771SJohan Hovold 	ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1184ed921771SJohan Hovold 	ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
11858cce3bbfSJohan Hovold 	if (port_priv->dtr)
1186568400b1SJohan Hovold 		ctl_hs |= CP210X_SERIAL_DTR_ACTIVE;
11878cce3bbfSJohan Hovold 	else
1188568400b1SJohan Hovold 		ctl_hs |= CP210X_SERIAL_DTR_INACTIVE;
1189ed921771SJohan Hovold 
1190e2f2dea3SJohan Hovold 	flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1191ed921771SJohan Hovold 	if (C_CRTSCTS(tty)) {
1192ed921771SJohan Hovold 		ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1193cf00ead0SJohan Hovold 		if (port_priv->rts)
1194568400b1SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_FLOW_CTL;
1195cf00ead0SJohan Hovold 		else
1196cf00ead0SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_INACTIVE;
11975951b850SJohan Hovold 		port_priv->crtscts = true;
1198ed921771SJohan Hovold 	} else {
1199ed921771SJohan Hovold 		ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
12008cce3bbfSJohan Hovold 		if (port_priv->rts)
1201568400b1SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_ACTIVE;
12028cce3bbfSJohan Hovold 		else
1203568400b1SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_INACTIVE;
12045951b850SJohan Hovold 		port_priv->crtscts = false;
1205ed921771SJohan Hovold 	}
1206ed921771SJohan Hovold 
1207*63a8eef7SJohan Hovold 	if (I_IXOFF(tty)) {
12087748feffSWang Sheng Long 		flow_repl |= CP210X_SERIAL_AUTO_RECEIVE;
1209*63a8eef7SJohan Hovold 
1210*63a8eef7SJohan Hovold 		flow_ctl.ulXonLimit = cpu_to_le32(128);
1211*63a8eef7SJohan Hovold 		flow_ctl.ulXoffLimit = cpu_to_le32(128);
1212*63a8eef7SJohan Hovold 	} else {
12137748feffSWang Sheng Long 		flow_repl &= ~CP210X_SERIAL_AUTO_RECEIVE;
1214*63a8eef7SJohan Hovold 	}
12157748feffSWang Sheng Long 
12167748feffSWang Sheng Long 	if (I_IXON(tty))
12177748feffSWang Sheng Long 		flow_repl |= CP210X_SERIAL_AUTO_TRANSMIT;
12187748feffSWang Sheng Long 	else
12197748feffSWang Sheng Long 		flow_repl &= ~CP210X_SERIAL_AUTO_TRANSMIT;
12207748feffSWang Sheng Long 
1221f191c637SJohan Hovold 	dev_dbg(&port->dev, "%s - ctrl = 0x%02x, flow = 0x%02x\n", __func__,
1222f191c637SJohan Hovold 			ctl_hs, flow_repl);
1223ed921771SJohan Hovold 
1224ed921771SJohan Hovold 	flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1225ed921771SJohan Hovold 	flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1226ed921771SJohan Hovold 
1227ed921771SJohan Hovold 	cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1228ed921771SJohan Hovold 			sizeof(flow_ctl));
12295951b850SJohan Hovold out_unlock:
12305951b850SJohan Hovold 	mutex_unlock(&port_priv->mutex);
1231ed921771SJohan Hovold }
1232ed921771SJohan Hovold 
12334cc27bd6SCraig Shelley static void cp210x_set_termios(struct tty_struct *tty,
123403ee2515SGreg Kroah-Hartman 		struct usb_serial_port *port, struct ktermios *old_termios)
123503ee2515SGreg Kroah-Hartman {
1236d4297629SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
123719165b2bSKonstantin Shkolnyy 	u16 bits;
1238d4297629SJohan Hovold 	int ret;
123903ee2515SGreg Kroah-Hartman 
124046827bdaSJohan Hovold 	if (old_termios && !cp210x_termios_change(&tty->termios, old_termios))
124195168d62SJohan Hovold 		return;
124295168d62SJohan Hovold 
124346827bdaSJohan Hovold 	if (!old_termios || tty->termios.c_ospeed != old_termios->c_ospeed)
1244e5990874SJohan Hovold 		cp210x_change_speed(tty, port, old_termios);
124503ee2515SGreg Kroah-Hartman 
1246d4297629SJohan Hovold 	/* CP2101 only supports CS8, 1 stop bit and non-stick parity. */
1247d4297629SJohan Hovold 	if (priv->partnum == CP210X_PARTNUM_CP2101) {
1248d4297629SJohan Hovold 		tty->termios.c_cflag &= ~(CSIZE | CSTOPB | CMSPAR);
1249d4297629SJohan Hovold 		tty->termios.c_cflag |= CS8;
1250d4297629SJohan Hovold 	}
1251d4297629SJohan Hovold 
1252d4297629SJohan Hovold 	bits = 0;
1253d4297629SJohan Hovold 
1254d4297629SJohan Hovold 	switch (C_CSIZE(tty)) {
125503ee2515SGreg Kroah-Hartman 	case CS5:
125603ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_5;
125703ee2515SGreg Kroah-Hartman 		break;
125803ee2515SGreg Kroah-Hartman 	case CS6:
125903ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_6;
126003ee2515SGreg Kroah-Hartman 		break;
126103ee2515SGreg Kroah-Hartman 	case CS7:
126203ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_7;
126303ee2515SGreg Kroah-Hartman 		break;
126403ee2515SGreg Kroah-Hartman 	case CS8:
1265c2b33559SJohan Hovold 	default:
126603ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_8;
126703ee2515SGreg Kroah-Hartman 		break;
126803ee2515SGreg Kroah-Hartman 	}
126903ee2515SGreg Kroah-Hartman 
1270d4297629SJohan Hovold 	if (C_PARENB(tty)) {
1271d4297629SJohan Hovold 		if (C_CMSPAR(tty)) {
1272d4297629SJohan Hovold 			if (C_PARODD(tty))
12734b6181caSRoland Koebler 				bits |= BITS_PARITY_MARK;
1274d4297629SJohan Hovold 			else
12754b6181caSRoland Koebler 				bits |= BITS_PARITY_SPACE;
12764b6181caSRoland Koebler 		} else {
1277d4297629SJohan Hovold 			if (C_PARODD(tty))
127803ee2515SGreg Kroah-Hartman 				bits |= BITS_PARITY_ODD;
1279d4297629SJohan Hovold 			else
128003ee2515SGreg Kroah-Hartman 				bits |= BITS_PARITY_EVEN;
128103ee2515SGreg Kroah-Hartman 		}
128203ee2515SGreg Kroah-Hartman 	}
128303ee2515SGreg Kroah-Hartman 
1284d4297629SJohan Hovold 	if (C_CSTOPB(tty))
128503ee2515SGreg Kroah-Hartman 		bits |= BITS_STOP_2;
1286d4297629SJohan Hovold 	else
128703ee2515SGreg Kroah-Hartman 		bits |= BITS_STOP_1;
1288d4297629SJohan Hovold 
1289d4297629SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1290d4297629SJohan Hovold 	if (ret)
1291d4297629SJohan Hovold 		dev_err(&port->dev, "failed to set line control: %d\n", ret);
129203ee2515SGreg Kroah-Hartman 
1293ed921771SJohan Hovold 	cp210x_set_flow_control(tty, port, old_termios);
129403ee2515SGreg Kroah-Hartman 
1295a7207e98SJohan Hovold 	/*
1296a7207e98SJohan Hovold 	 * Enable event-insertion mode only if input parity checking is
1297a7207e98SJohan Hovold 	 * enabled for now.
1298a7207e98SJohan Hovold 	 */
1299a7207e98SJohan Hovold 	if (I_INPCK(tty))
1300a7207e98SJohan Hovold 		cp210x_enable_event_mode(port);
1301a7207e98SJohan Hovold 	else
1302a7207e98SJohan Hovold 		cp210x_disable_event_mode(port);
130303ee2515SGreg Kroah-Hartman }
130403ee2515SGreg Kroah-Hartman 
130520b9d177SAlan Cox static int cp210x_tiocmset(struct tty_struct *tty,
130603ee2515SGreg Kroah-Hartman 		unsigned int set, unsigned int clear)
130703ee2515SGreg Kroah-Hartman {
130803ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
130920b9d177SAlan Cox 	return cp210x_tiocmset_port(port, set, clear);
1310d2ad67b3SVomLehn }
1311d2ad67b3SVomLehn 
131220b9d177SAlan Cox static int cp210x_tiocmset_port(struct usb_serial_port *port,
1313d2ad67b3SVomLehn 		unsigned int set, unsigned int clear)
1314d2ad67b3SVomLehn {
13155951b850SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1316cf00ead0SJohan Hovold 	struct cp210x_flow_ctl flow_ctl;
1317cf00ead0SJohan Hovold 	u32 ctl_hs, flow_repl;
131819165b2bSKonstantin Shkolnyy 	u16 control = 0;
13195951b850SJohan Hovold 	int ret;
132003ee2515SGreg Kroah-Hartman 
13218cce3bbfSJohan Hovold 	mutex_lock(&port_priv->mutex);
132203ee2515SGreg Kroah-Hartman 
132303ee2515SGreg Kroah-Hartman 	if (set & TIOCM_RTS) {
13248cce3bbfSJohan Hovold 		port_priv->rts = true;
132503ee2515SGreg Kroah-Hartman 		control |= CONTROL_RTS;
132603ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
132703ee2515SGreg Kroah-Hartman 	}
132803ee2515SGreg Kroah-Hartman 	if (set & TIOCM_DTR) {
13298cce3bbfSJohan Hovold 		port_priv->dtr = true;
133003ee2515SGreg Kroah-Hartman 		control |= CONTROL_DTR;
133103ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
133203ee2515SGreg Kroah-Hartman 	}
133303ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_RTS) {
13348cce3bbfSJohan Hovold 		port_priv->rts = false;
133503ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_RTS;
133603ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
133703ee2515SGreg Kroah-Hartman 	}
133803ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_DTR) {
13398cce3bbfSJohan Hovold 		port_priv->dtr = false;
134003ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_DTR;
134103ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
134203ee2515SGreg Kroah-Hartman 	}
134303ee2515SGreg Kroah-Hartman 
13445951b850SJohan Hovold 	/*
1345cf00ead0SJohan Hovold 	 * Use SET_FLOW to set DTR and enable/disable auto-RTS when hardware
1346cf00ead0SJohan Hovold 	 * flow control is enabled.
13475951b850SJohan Hovold 	 */
1348cf00ead0SJohan Hovold 	if (port_priv->crtscts && control & CONTROL_WRITE_RTS) {
1349cf00ead0SJohan Hovold 		ret = cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1350cf00ead0SJohan Hovold 				sizeof(flow_ctl));
1351cf00ead0SJohan Hovold 		if (ret)
1352cf00ead0SJohan Hovold 			goto out_unlock;
135303ee2515SGreg Kroah-Hartman 
1354cf00ead0SJohan Hovold 		ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1355cf00ead0SJohan Hovold 		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1356cf00ead0SJohan Hovold 
1357cf00ead0SJohan Hovold 		ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1358cf00ead0SJohan Hovold 		if (port_priv->dtr)
1359cf00ead0SJohan Hovold 			ctl_hs |= CP210X_SERIAL_DTR_ACTIVE;
1360cf00ead0SJohan Hovold 		else
1361cf00ead0SJohan Hovold 			ctl_hs |= CP210X_SERIAL_DTR_INACTIVE;
1362cf00ead0SJohan Hovold 
1363cf00ead0SJohan Hovold 		flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1364cf00ead0SJohan Hovold 		if (port_priv->rts)
1365cf00ead0SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_FLOW_CTL;
1366cf00ead0SJohan Hovold 		else
1367cf00ead0SJohan Hovold 			flow_repl |= CP210X_SERIAL_RTS_INACTIVE;
1368cf00ead0SJohan Hovold 
1369cf00ead0SJohan Hovold 		flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1370cf00ead0SJohan Hovold 		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1371cf00ead0SJohan Hovold 
1372cf00ead0SJohan Hovold 		dev_dbg(&port->dev, "%s - ctrl = 0x%02x, flow = 0x%02x\n",
1373cf00ead0SJohan Hovold 				__func__, ctl_hs, flow_repl);
1374cf00ead0SJohan Hovold 
1375cf00ead0SJohan Hovold 		ret = cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1376cf00ead0SJohan Hovold 				sizeof(flow_ctl));
1377cf00ead0SJohan Hovold 	} else {
13786b667274SJohan Hovold 		dev_dbg(&port->dev, "%s - control = 0x%04x\n", __func__, control);
137903ee2515SGreg Kroah-Hartman 
13805951b850SJohan Hovold 		ret = cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
1381cf00ead0SJohan Hovold 	}
1382cf00ead0SJohan Hovold out_unlock:
13835951b850SJohan Hovold 	mutex_unlock(&port_priv->mutex);
13845951b850SJohan Hovold 
13855951b850SJohan Hovold 	return ret;
138603ee2515SGreg Kroah-Hartman }
138703ee2515SGreg Kroah-Hartman 
1388daa91919SJohan Hovold static void cp210x_dtr_rts(struct usb_serial_port *port, int on)
1389d94c7bd4SAlan Cox {
1390d94c7bd4SAlan Cox 	if (on)
1391daa91919SJohan Hovold 		cp210x_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0);
1392d94c7bd4SAlan Cox 	else
1393daa91919SJohan Hovold 		cp210x_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS);
1394d94c7bd4SAlan Cox }
1395d94c7bd4SAlan Cox 
139660b33c13SAlan Cox static int cp210x_tiocmget(struct tty_struct *tty)
139703ee2515SGreg Kroah-Hartman {
139803ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
1399fe1b07e9SKonstantin Shkolnyy 	u8 control;
140003ee2515SGreg Kroah-Hartman 	int result;
140103ee2515SGreg Kroah-Hartman 
1402de24e0a1SJohan Hovold 	result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1403de24e0a1SJohan Hovold 	if (result)
1404de24e0a1SJohan Hovold 		return result;
140503ee2515SGreg Kroah-Hartman 
140603ee2515SGreg Kroah-Hartman 	result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
140703ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RTS) ? TIOCM_RTS : 0)
140803ee2515SGreg Kroah-Hartman 		|((control & CONTROL_CTS) ? TIOCM_CTS : 0)
140903ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DSR) ? TIOCM_DSR : 0)
141003ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RING)? TIOCM_RI  : 0)
141103ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DCD) ? TIOCM_CD  : 0);
141203ee2515SGreg Kroah-Hartman 
14136b667274SJohan Hovold 	dev_dbg(&port->dev, "%s - control = 0x%02x\n", __func__, control);
141403ee2515SGreg Kroah-Hartman 
141503ee2515SGreg Kroah-Hartman 	return result;
141603ee2515SGreg Kroah-Hartman }
141703ee2515SGreg Kroah-Hartman 
14184cc27bd6SCraig Shelley static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
141903ee2515SGreg Kroah-Hartman {
142003ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
142119165b2bSKonstantin Shkolnyy 	u16 state;
142203ee2515SGreg Kroah-Hartman 
142303ee2515SGreg Kroah-Hartman 	if (break_state == 0)
142403ee2515SGreg Kroah-Hartman 		state = BREAK_OFF;
142503ee2515SGreg Kroah-Hartman 	else
142603ee2515SGreg Kroah-Hartman 		state = BREAK_ON;
1427660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
142803ee2515SGreg Kroah-Hartman 		state == BREAK_OFF ? "off" : "on");
142919165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
143003ee2515SGreg Kroah-Hartman }
143103ee2515SGreg Kroah-Hartman 
1432cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
1433cf5276ceSMartyn Welch static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1434cf5276ceSMartyn Welch {
1435cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1436c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1437c8acfe0aSKaroly Pados 	u8 req_type = REQTYPE_DEVICE_TO_HOST;
1438cf5276ceSMartyn Welch 	int result;
1439cf5276ceSMartyn Welch 	u8 buf;
1440cf5276ceSMartyn Welch 
1441c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105)
1442c8acfe0aSKaroly Pados 		req_type = REQTYPE_INTERFACE_TO_HOST;
1443c8acfe0aSKaroly Pados 
14447b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
14457b0b644bSKaroly Pados 	if (result)
14467b0b644bSKaroly Pados 		return result;
14477b0b644bSKaroly Pados 
1448c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial, req_type,
1449cf5276ceSMartyn Welch 					  CP210X_READ_LATCH, &buf, sizeof(buf));
14507b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
1451cf5276ceSMartyn Welch 	if (result < 0)
1452cf5276ceSMartyn Welch 		return result;
1453cf5276ceSMartyn Welch 
1454cf5276ceSMartyn Welch 	return !!(buf & BIT(gpio));
1455cf5276ceSMartyn Welch }
1456cf5276ceSMartyn Welch 
1457cf5276ceSMartyn Welch static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1458cf5276ceSMartyn Welch {
1459cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1460c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1461cf5276ceSMartyn Welch 	struct cp210x_gpio_write buf;
1462c8acfe0aSKaroly Pados 	int result;
1463cf5276ceSMartyn Welch 
1464cf5276ceSMartyn Welch 	if (value == 1)
1465cf5276ceSMartyn Welch 		buf.state = BIT(gpio);
1466cf5276ceSMartyn Welch 	else
1467cf5276ceSMartyn Welch 		buf.state = 0;
1468cf5276ceSMartyn Welch 
1469cf5276ceSMartyn Welch 	buf.mask = BIT(gpio);
1470cf5276ceSMartyn Welch 
14717b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
14727b0b644bSKaroly Pados 	if (result)
14737b0b644bSKaroly Pados 		goto out;
14747b0b644bSKaroly Pados 
1475c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1476c8acfe0aSKaroly Pados 		result = cp210x_write_vendor_block(serial,
1477c8acfe0aSKaroly Pados 						   REQTYPE_HOST_TO_INTERFACE,
1478c8acfe0aSKaroly Pados 						   CP210X_WRITE_LATCH, &buf,
1479c8acfe0aSKaroly Pados 						   sizeof(buf));
1480c8acfe0aSKaroly Pados 	} else {
1481c8acfe0aSKaroly Pados 		u16 wIndex = buf.state << 8 | buf.mask;
1482c8acfe0aSKaroly Pados 
1483c8acfe0aSKaroly Pados 		result = usb_control_msg(serial->dev,
1484c8acfe0aSKaroly Pados 					 usb_sndctrlpipe(serial->dev, 0),
1485c8acfe0aSKaroly Pados 					 CP210X_VENDOR_SPECIFIC,
1486c8acfe0aSKaroly Pados 					 REQTYPE_HOST_TO_DEVICE,
1487c8acfe0aSKaroly Pados 					 CP210X_WRITE_LATCH,
1488c8acfe0aSKaroly Pados 					 wIndex,
1489c8acfe0aSKaroly Pados 					 NULL, 0, USB_CTRL_SET_TIMEOUT);
1490c8acfe0aSKaroly Pados 	}
1491c8acfe0aSKaroly Pados 
14927b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
14937b0b644bSKaroly Pados out:
1494c8acfe0aSKaroly Pados 	if (result < 0) {
1495c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
1496c8acfe0aSKaroly Pados 				result);
1497c8acfe0aSKaroly Pados 	}
1498cf5276ceSMartyn Welch }
1499cf5276ceSMartyn Welch 
1500cf5276ceSMartyn Welch static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1501cf5276ceSMartyn Welch {
1502c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1503c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1504c8acfe0aSKaroly Pados 
1505c8acfe0aSKaroly Pados 	return priv->gpio_input & BIT(gpio);
1506cf5276ceSMartyn Welch }
1507cf5276ceSMartyn Welch 
1508cf5276ceSMartyn Welch static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1509cf5276ceSMartyn Welch {
1510c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1511c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1512c8acfe0aSKaroly Pados 
1513c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1514c8acfe0aSKaroly Pados 		/* hardware does not support an input mode */
1515cf5276ceSMartyn Welch 		return -ENOTSUPP;
1516cf5276ceSMartyn Welch 	}
1517cf5276ceSMartyn Welch 
1518c8acfe0aSKaroly Pados 	/* push-pull pins cannot be changed to be inputs */
1519c8acfe0aSKaroly Pados 	if (priv->gpio_pushpull & BIT(gpio))
1520c8acfe0aSKaroly Pados 		return -EINVAL;
1521c8acfe0aSKaroly Pados 
1522c8acfe0aSKaroly Pados 	/* make sure to release pin if it is being driven low */
1523c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, 1);
1524c8acfe0aSKaroly Pados 
1525c8acfe0aSKaroly Pados 	priv->gpio_input |= BIT(gpio);
1526c8acfe0aSKaroly Pados 
1527c8acfe0aSKaroly Pados 	return 0;
1528c8acfe0aSKaroly Pados }
1529c8acfe0aSKaroly Pados 
1530cf5276ceSMartyn Welch static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1531cf5276ceSMartyn Welch 					int value)
1532cf5276ceSMartyn Welch {
1533c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1534c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1535c8acfe0aSKaroly Pados 
1536c8acfe0aSKaroly Pados 	priv->gpio_input &= ~BIT(gpio);
1537c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, value);
1538c8acfe0aSKaroly Pados 
1539cf5276ceSMartyn Welch 	return 0;
1540cf5276ceSMartyn Welch }
1541cf5276ceSMartyn Welch 
15422956b5d9SMika Westerberg static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
15432956b5d9SMika Westerberg 				  unsigned long config)
1544cf5276ceSMartyn Welch {
1545cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1546cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
15472956b5d9SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(config);
1548cf5276ceSMartyn Welch 
1549cf5276ceSMartyn Welch 	/* Succeed only if in correct mode (this can't be set at runtime) */
15502956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
1551c8acfe0aSKaroly Pados 	    (priv->gpio_pushpull & BIT(gpio)))
1552cf5276ceSMartyn Welch 		return 0;
1553cf5276ceSMartyn Welch 
15542956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
1555c8acfe0aSKaroly Pados 	    !(priv->gpio_pushpull & BIT(gpio)))
1556cf5276ceSMartyn Welch 		return 0;
1557cf5276ceSMartyn Welch 
1558cf5276ceSMartyn Welch 	return -ENOTSUPP;
1559cf5276ceSMartyn Welch }
1560cf5276ceSMartyn Welch 
1561b979248dSJohan Hovold static int cp210x_gpio_init_valid_mask(struct gpio_chip *gc,
1562b979248dSJohan Hovold 		unsigned long *valid_mask, unsigned int ngpios)
1563b979248dSJohan Hovold {
1564b979248dSJohan Hovold 	struct usb_serial *serial = gpiochip_get_data(gc);
1565b979248dSJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1566d0708227SJohan Hovold 	struct device *dev = &serial->interface->dev;
1567b979248dSJohan Hovold 	unsigned long altfunc_mask = priv->gpio_altfunc;
1568b979248dSJohan Hovold 
1569b979248dSJohan Hovold 	bitmap_complement(valid_mask, &altfunc_mask, ngpios);
1570b979248dSJohan Hovold 
1571d0708227SJohan Hovold 	if (bitmap_empty(valid_mask, ngpios))
1572d0708227SJohan Hovold 		dev_dbg(dev, "no pin configured for GPIO\n");
1573d0708227SJohan Hovold 	else
1574d0708227SJohan Hovold 		dev_dbg(dev, "GPIO.%*pbl configured for GPIO\n", ngpios,
1575d0708227SJohan Hovold 				valid_mask);
1576b979248dSJohan Hovold 	return 0;
1577b979248dSJohan Hovold }
1578b979248dSJohan Hovold 
1579cf5276ceSMartyn Welch /*
1580cf5276ceSMartyn Welch  * This function is for configuring GPIO using shared pins, where other signals
1581cf5276ceSMartyn Welch  * are made unavailable by configuring the use of GPIO. This is believed to be
1582cf5276ceSMartyn Welch  * only applicable to the cp2105 at this point, the other devices supported by
1583cf5276ceSMartyn Welch  * this driver that provide GPIO do so in a way that does not impact other
1584cf5276ceSMartyn Welch  * signals and are thus expected to have very different initialisation.
1585cf5276ceSMartyn Welch  */
1586c8acfe0aSKaroly Pados static int cp2105_gpioconf_init(struct usb_serial *serial)
1587cf5276ceSMartyn Welch {
1588cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1589cf5276ceSMartyn Welch 	struct cp210x_pin_mode mode;
159015fb84b7SIcenowy Zheng 	struct cp210x_dual_port_config config;
1591cf5276ceSMartyn Welch 	u8 intf_num = cp210x_interface_num(serial);
1592c8acfe0aSKaroly Pados 	u8 iface_config;
1593cf5276ceSMartyn Welch 	int result;
1594cf5276ceSMartyn Welch 
1595cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1596cf5276ceSMartyn Welch 					  CP210X_GET_DEVICEMODE, &mode,
1597cf5276ceSMartyn Welch 					  sizeof(mode));
1598cf5276ceSMartyn Welch 	if (result < 0)
1599cf5276ceSMartyn Welch 		return result;
1600cf5276ceSMartyn Welch 
1601cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1602cf5276ceSMartyn Welch 					  CP210X_GET_PORTCONFIG, &config,
1603cf5276ceSMartyn Welch 					  sizeof(config));
1604cf5276ceSMartyn Welch 	if (result < 0)
1605cf5276ceSMartyn Welch 		return result;
1606cf5276ceSMartyn Welch 
1607cf5276ceSMartyn Welch 	/*  2 banks of GPIO - One for the pins taken from each serial port */
1608cf5276ceSMartyn Welch 	if (intf_num == 0) {
1609c8acfe0aSKaroly Pados 		if (mode.eci == CP210X_PIN_MODE_MODEM) {
1610c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1611c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1612cf5276ceSMartyn Welch 			return 0;
1613c8acfe0aSKaroly Pados 		}
1614cf5276ceSMartyn Welch 
1615c8acfe0aSKaroly Pados 		iface_config = config.eci_cfg;
1616c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1617cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_MASK) >>
1618cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_OFFSET);
1619cf5276ceSMartyn Welch 		priv->gc.ngpio = 2;
1620cf5276ceSMartyn Welch 	} else if (intf_num == 1) {
1621c8acfe0aSKaroly Pados 		if (mode.sci == CP210X_PIN_MODE_MODEM) {
1622c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1623c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1624cf5276ceSMartyn Welch 			return 0;
1625c8acfe0aSKaroly Pados 		}
1626cf5276ceSMartyn Welch 
1627c8acfe0aSKaroly Pados 		iface_config = config.sci_cfg;
1628c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1629cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_MASK) >>
1630cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_OFFSET);
1631cf5276ceSMartyn Welch 		priv->gc.ngpio = 3;
1632cf5276ceSMartyn Welch 	} else {
1633cf5276ceSMartyn Welch 		return -ENODEV;
1634cf5276ceSMartyn Welch 	}
1635cf5276ceSMartyn Welch 
1636c8acfe0aSKaroly Pados 	/* mark all pins which are not in GPIO mode */
1637c8acfe0aSKaroly Pados 	if (iface_config & CP2105_GPIO0_TXLED_MODE)	/* GPIO 0 */
1638c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(0);
1639c8acfe0aSKaroly Pados 	if (iface_config & (CP2105_GPIO1_RXLED_MODE |	/* GPIO 1 */
1640c8acfe0aSKaroly Pados 			CP2105_GPIO1_RS485_MODE))
1641c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(1);
1642c8acfe0aSKaroly Pados 
1643c8acfe0aSKaroly Pados 	/* driver implementation for CP2105 only supports outputs */
1644c8acfe0aSKaroly Pados 	priv->gpio_input = 0;
1645c8acfe0aSKaroly Pados 
1646c8acfe0aSKaroly Pados 	return 0;
1647c8acfe0aSKaroly Pados }
1648c8acfe0aSKaroly Pados 
164915fb84b7SIcenowy Zheng static int cp2104_gpioconf_init(struct usb_serial *serial)
165015fb84b7SIcenowy Zheng {
165115fb84b7SIcenowy Zheng 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
165215fb84b7SIcenowy Zheng 	struct cp210x_single_port_config config;
165315fb84b7SIcenowy Zheng 	u8 iface_config;
165415fb84b7SIcenowy Zheng 	u8 gpio_latch;
165515fb84b7SIcenowy Zheng 	int result;
165615fb84b7SIcenowy Zheng 	u8 i;
165715fb84b7SIcenowy Zheng 
165815fb84b7SIcenowy Zheng 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
165915fb84b7SIcenowy Zheng 					  CP210X_GET_PORTCONFIG, &config,
166015fb84b7SIcenowy Zheng 					  sizeof(config));
166115fb84b7SIcenowy Zheng 	if (result < 0)
166215fb84b7SIcenowy Zheng 		return result;
166315fb84b7SIcenowy Zheng 
166415fb84b7SIcenowy Zheng 	priv->gc.ngpio = 4;
166515fb84b7SIcenowy Zheng 
166615fb84b7SIcenowy Zheng 	iface_config = config.device_cfg;
166715fb84b7SIcenowy Zheng 	priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
166815fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
166915fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
167015fb84b7SIcenowy Zheng 	gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
167115fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
167215fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
167315fb84b7SIcenowy Zheng 
167415fb84b7SIcenowy Zheng 	/* mark all pins which are not in GPIO mode */
167515fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO0_TXLED_MODE)	/* GPIO 0 */
167615fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(0);
167715fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO1_RXLED_MODE)	/* GPIO 1 */
167815fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(1);
167915fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO2_RS485_MODE)	/* GPIO 2 */
168015fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(2);
168115fb84b7SIcenowy Zheng 
168215fb84b7SIcenowy Zheng 	/*
168315fb84b7SIcenowy Zheng 	 * Like CP2102N, CP2104 has also no strict input and output pin
168415fb84b7SIcenowy Zheng 	 * modes.
168515fb84b7SIcenowy Zheng 	 * Do the same input mode emulation as CP2102N.
168615fb84b7SIcenowy Zheng 	 */
168715fb84b7SIcenowy Zheng 	for (i = 0; i < priv->gc.ngpio; ++i) {
168815fb84b7SIcenowy Zheng 		/*
168915fb84b7SIcenowy Zheng 		 * Set direction to "input" iff pin is open-drain and reset
169015fb84b7SIcenowy Zheng 		 * value is 1.
169115fb84b7SIcenowy Zheng 		 */
169215fb84b7SIcenowy Zheng 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
169315fb84b7SIcenowy Zheng 			priv->gpio_input |= BIT(i);
169415fb84b7SIcenowy Zheng 	}
169515fb84b7SIcenowy Zheng 
169615fb84b7SIcenowy Zheng 	return 0;
169715fb84b7SIcenowy Zheng }
169815fb84b7SIcenowy Zheng 
1699c8acfe0aSKaroly Pados static int cp2102n_gpioconf_init(struct usb_serial *serial)
1700c8acfe0aSKaroly Pados {
1701c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1702c8acfe0aSKaroly Pados 	const u16 config_size = 0x02a6;
1703c8acfe0aSKaroly Pados 	u8 gpio_rst_latch;
1704c8acfe0aSKaroly Pados 	u8 config_version;
1705c8acfe0aSKaroly Pados 	u8 gpio_pushpull;
1706c8acfe0aSKaroly Pados 	u8 *config_buf;
1707c8acfe0aSKaroly Pados 	u8 gpio_latch;
1708c8acfe0aSKaroly Pados 	u8 gpio_ctrl;
1709c8acfe0aSKaroly Pados 	int result;
1710c8acfe0aSKaroly Pados 	u8 i;
1711c8acfe0aSKaroly Pados 
1712c8acfe0aSKaroly Pados 	/*
1713c8acfe0aSKaroly Pados 	 * Retrieve device configuration from the device.
1714c8acfe0aSKaroly Pados 	 * The array received contains all customization settings done at the
1715c8acfe0aSKaroly Pados 	 * factory/manufacturer. Format of the array is documented at the
1716c8acfe0aSKaroly Pados 	 * time of writing at:
1717c8acfe0aSKaroly Pados 	 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
1718c8acfe0aSKaroly Pados 	 */
1719c8acfe0aSKaroly Pados 	config_buf = kmalloc(config_size, GFP_KERNEL);
1720c8acfe0aSKaroly Pados 	if (!config_buf)
1721c8acfe0aSKaroly Pados 		return -ENOMEM;
1722c8acfe0aSKaroly Pados 
1723c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial,
1724c8acfe0aSKaroly Pados 					  REQTYPE_DEVICE_TO_HOST,
1725c8acfe0aSKaroly Pados 					  CP210X_READ_2NCONFIG,
1726c8acfe0aSKaroly Pados 					  config_buf,
1727c8acfe0aSKaroly Pados 					  config_size);
1728c8acfe0aSKaroly Pados 	if (result < 0) {
1729c8acfe0aSKaroly Pados 		kfree(config_buf);
1730c8acfe0aSKaroly Pados 		return result;
1731c8acfe0aSKaroly Pados 	}
1732c8acfe0aSKaroly Pados 
1733c8acfe0aSKaroly Pados 	config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
1734c8acfe0aSKaroly Pados 	gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
1735c8acfe0aSKaroly Pados 	gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
1736c8acfe0aSKaroly Pados 	gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
1737c8acfe0aSKaroly Pados 
1738c8acfe0aSKaroly Pados 	kfree(config_buf);
1739c8acfe0aSKaroly Pados 
1740c8acfe0aSKaroly Pados 	/* Make sure this is a config format we understand. */
1741c8acfe0aSKaroly Pados 	if (config_version != 0x01)
1742c8acfe0aSKaroly Pados 		return -ENOTSUPP;
1743c8acfe0aSKaroly Pados 
1744c8acfe0aSKaroly Pados 	priv->gc.ngpio = 4;
1745c8acfe0aSKaroly Pados 
1746c8acfe0aSKaroly Pados 	/*
1747c8acfe0aSKaroly Pados 	 * Get default pin states after reset. Needed so we can determine
1748c8acfe0aSKaroly Pados 	 * the direction of an open-drain pin.
1749c8acfe0aSKaroly Pados 	 */
1750c8acfe0aSKaroly Pados 	gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
1751c8acfe0aSKaroly Pados 
1752c8acfe0aSKaroly Pados 	/* 0 indicates open-drain mode, 1 is push-pull */
1753c8acfe0aSKaroly Pados 	priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
1754c8acfe0aSKaroly Pados 
1755c8acfe0aSKaroly Pados 	/* 0 indicates GPIO mode, 1 is alternate function */
17566f7ec77cSStefan Agner 	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20) {
17576f7ec77cSStefan Agner 		/* QFN20 is special... */
17586f7ec77cSStefan Agner 		if (gpio_ctrl & CP2102N_QFN20_GPIO0_CLK_MODE)   /* GPIO 0 */
17596f7ec77cSStefan Agner 			priv->gpio_altfunc |= BIT(0);
17606f7ec77cSStefan Agner 		if (gpio_ctrl & CP2102N_QFN20_GPIO1_RS485_MODE) /* GPIO 1 */
17616f7ec77cSStefan Agner 			priv->gpio_altfunc |= BIT(1);
17626f7ec77cSStefan Agner 		if (gpio_ctrl & CP2102N_QFN20_GPIO2_TXLED_MODE) /* GPIO 2 */
17636f7ec77cSStefan Agner 			priv->gpio_altfunc |= BIT(2);
17646f7ec77cSStefan Agner 		if (gpio_ctrl & CP2102N_QFN20_GPIO3_RXLED_MODE) /* GPIO 3 */
17656f7ec77cSStefan Agner 			priv->gpio_altfunc |= BIT(3);
17666f7ec77cSStefan Agner 	} else {
1767c8acfe0aSKaroly Pados 		priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
17686f7ec77cSStefan Agner 	}
1769c8acfe0aSKaroly Pados 
1770a49e1abfSMans Rullgard 	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
1771a49e1abfSMans Rullgard 		/*
1772a49e1abfSMans Rullgard 		 * For the QFN28 package, GPIO4-6 are controlled by
1773a49e1abfSMans Rullgard 		 * the low three bits of the mode/latch fields.
1774a49e1abfSMans Rullgard 		 * Contrary to the document linked above, the bits for
1775a49e1abfSMans Rullgard 		 * the SUSPEND pins are elsewhere.  No alternate
1776a49e1abfSMans Rullgard 		 * function is available for these pins.
1777a49e1abfSMans Rullgard 		 */
1778a49e1abfSMans Rullgard 		priv->gc.ngpio = 7;
1779a49e1abfSMans Rullgard 		gpio_latch |= (gpio_rst_latch & 7) << 4;
1780a49e1abfSMans Rullgard 		priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
1781a49e1abfSMans Rullgard 	}
1782a49e1abfSMans Rullgard 
1783c8acfe0aSKaroly Pados 	/*
1784c8acfe0aSKaroly Pados 	 * The CP2102N does not strictly has input and output pin modes,
1785c8acfe0aSKaroly Pados 	 * it only knows open-drain and push-pull modes which is set at
1786c8acfe0aSKaroly Pados 	 * factory. An open-drain pin can function both as an
1787c8acfe0aSKaroly Pados 	 * input or an output. We emulate input mode for open-drain pins
1788c8acfe0aSKaroly Pados 	 * by making sure they are not driven low, and we do not allow
1789c8acfe0aSKaroly Pados 	 * push-pull pins to be set as an input.
1790c8acfe0aSKaroly Pados 	 */
1791c8acfe0aSKaroly Pados 	for (i = 0; i < priv->gc.ngpio; ++i) {
1792c8acfe0aSKaroly Pados 		/*
1793c8acfe0aSKaroly Pados 		 * Set direction to "input" iff pin is open-drain and reset
1794c8acfe0aSKaroly Pados 		 * value is 1.
1795c8acfe0aSKaroly Pados 		 */
1796c8acfe0aSKaroly Pados 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1797c8acfe0aSKaroly Pados 			priv->gpio_input |= BIT(i);
1798c8acfe0aSKaroly Pados 	}
1799c8acfe0aSKaroly Pados 
1800c8acfe0aSKaroly Pados 	return 0;
1801c8acfe0aSKaroly Pados }
1802c8acfe0aSKaroly Pados 
1803c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1804c8acfe0aSKaroly Pados {
1805c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1806c8acfe0aSKaroly Pados 	int result;
1807c8acfe0aSKaroly Pados 
1808c8acfe0aSKaroly Pados 	switch (priv->partnum) {
180915fb84b7SIcenowy Zheng 	case CP210X_PARTNUM_CP2104:
181015fb84b7SIcenowy Zheng 		result = cp2104_gpioconf_init(serial);
181115fb84b7SIcenowy Zheng 		break;
1812c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2105:
1813c8acfe0aSKaroly Pados 		result = cp2105_gpioconf_init(serial);
1814c8acfe0aSKaroly Pados 		break;
1815c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
1816c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
1817c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
1818c8acfe0aSKaroly Pados 		result = cp2102n_gpioconf_init(serial);
1819c8acfe0aSKaroly Pados 		break;
1820c8acfe0aSKaroly Pados 	default:
1821c8acfe0aSKaroly Pados 		return 0;
1822c8acfe0aSKaroly Pados 	}
1823c8acfe0aSKaroly Pados 
1824c8acfe0aSKaroly Pados 	if (result < 0)
1825c8acfe0aSKaroly Pados 		return result;
1826c8acfe0aSKaroly Pados 
1827cf5276ceSMartyn Welch 	priv->gc.label = "cp210x";
1828cf5276ceSMartyn Welch 	priv->gc.get_direction = cp210x_gpio_direction_get;
1829cf5276ceSMartyn Welch 	priv->gc.direction_input = cp210x_gpio_direction_input;
1830cf5276ceSMartyn Welch 	priv->gc.direction_output = cp210x_gpio_direction_output;
1831cf5276ceSMartyn Welch 	priv->gc.get = cp210x_gpio_get;
1832cf5276ceSMartyn Welch 	priv->gc.set = cp210x_gpio_set;
18332956b5d9SMika Westerberg 	priv->gc.set_config = cp210x_gpio_set_config;
1834b979248dSJohan Hovold 	priv->gc.init_valid_mask = cp210x_gpio_init_valid_mask;
1835cf5276ceSMartyn Welch 	priv->gc.owner = THIS_MODULE;
1836cf5276ceSMartyn Welch 	priv->gc.parent = &serial->interface->dev;
1837cf5276ceSMartyn Welch 	priv->gc.base = -1;
1838cf5276ceSMartyn Welch 	priv->gc.can_sleep = true;
1839cf5276ceSMartyn Welch 
1840cf5276ceSMartyn Welch 	result = gpiochip_add_data(&priv->gc, serial);
1841cf5276ceSMartyn Welch 	if (!result)
18426b7271d2SJohan Hovold 		priv->gpio_registered = true;
1843cf5276ceSMartyn Welch 
1844cf5276ceSMartyn Welch 	return result;
1845cf5276ceSMartyn Welch }
1846cf5276ceSMartyn Welch 
1847cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1848cf5276ceSMartyn Welch {
1849cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1850cf5276ceSMartyn Welch 
1851cf5276ceSMartyn Welch 	if (priv->gpio_registered) {
1852cf5276ceSMartyn Welch 		gpiochip_remove(&priv->gc);
18536b7271d2SJohan Hovold 		priv->gpio_registered = false;
1854cf5276ceSMartyn Welch 	}
1855cf5276ceSMartyn Welch }
1856cf5276ceSMartyn Welch 
1857cf5276ceSMartyn Welch #else
1858cf5276ceSMartyn Welch 
1859c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1860cf5276ceSMartyn Welch {
1861cf5276ceSMartyn Welch 	return 0;
1862cf5276ceSMartyn Welch }
1863cf5276ceSMartyn Welch 
1864cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1865cf5276ceSMartyn Welch {
1866cf5276ceSMartyn Welch 	/* Nothing to do */
1867cf5276ceSMartyn Welch }
1868cf5276ceSMartyn Welch 
1869cf5276ceSMartyn Welch #endif
1870cf5276ceSMartyn Welch 
1871e2ae67a3SKonstantin Shkolnyy static int cp210x_port_probe(struct usb_serial_port *port)
187203ee2515SGreg Kroah-Hartman {
1873e2ae67a3SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
1874e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
1875a5360a53SPreston Fick 
1876e2ae67a3SKonstantin Shkolnyy 	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1877e2ae67a3SKonstantin Shkolnyy 	if (!port_priv)
1878a5360a53SPreston Fick 		return -ENOMEM;
1879a5360a53SPreston Fick 
1880cf5276ceSMartyn Welch 	port_priv->bInterfaceNumber = cp210x_interface_num(serial);
18815951b850SJohan Hovold 	mutex_init(&port_priv->mutex);
1882a5360a53SPreston Fick 
1883e2ae67a3SKonstantin Shkolnyy 	usb_set_serial_port_data(port, port_priv);
1884a5360a53SPreston Fick 
188503ee2515SGreg Kroah-Hartman 	return 0;
188603ee2515SGreg Kroah-Hartman }
188703ee2515SGreg Kroah-Hartman 
1888c5d1448fSUwe Kleine-König static void cp210x_port_remove(struct usb_serial_port *port)
1889a5360a53SPreston Fick {
1890e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
1891a5360a53SPreston Fick 
1892e2ae67a3SKonstantin Shkolnyy 	port_priv = usb_get_serial_port_data(port);
1893e2ae67a3SKonstantin Shkolnyy 	kfree(port_priv);
1894a5360a53SPreston Fick }
1895a5360a53SPreston Fick 
1896d4706c05SJohan Hovold static void cp210x_init_max_speed(struct usb_serial *serial)
1897d4706c05SJohan Hovold {
1898d4706c05SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
18997aecd7fcSJohan Hovold 	bool use_actual_rate = false;
190085bc2d91SJohanna Abrahamsson 	speed_t min = 300;
1901d4706c05SJohan Hovold 	speed_t max;
1902d4706c05SJohan Hovold 
1903d4706c05SJohan Hovold 	switch (priv->partnum) {
1904d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2101:
1905d4706c05SJohan Hovold 		max = 921600;
1906d4706c05SJohan Hovold 		break;
1907d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2102:
1908d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2103:
1909d4706c05SJohan Hovold 		max = 1000000;
1910d4706c05SJohan Hovold 		break;
1911d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2104:
19125edb65a3SJohan Hovold 		use_actual_rate = true;
19135edb65a3SJohan Hovold 		max = 2000000;
19145edb65a3SJohan Hovold 		break;
1915d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2108:
1916d4706c05SJohan Hovold 		max = 2000000;
1917d4706c05SJohan Hovold 		break;
1918d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2105:
19195edb65a3SJohan Hovold 		if (cp210x_interface_num(serial) == 0) {
19205edb65a3SJohan Hovold 			use_actual_rate = true;
1921d4706c05SJohan Hovold 			max = 2000000;	/* ECI */
19225edb65a3SJohan Hovold 		} else {
192385bc2d91SJohanna Abrahamsson 			min = 2400;
1924d4706c05SJohan Hovold 			max = 921600;	/* SCI */
19255edb65a3SJohan Hovold 		}
1926d4706c05SJohan Hovold 		break;
19276f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
19286f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
19296f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
19307aecd7fcSJohan Hovold 		use_actual_rate = true;
19316f0bcf72SKaroly Pados 		max = 3000000;
19326f0bcf72SKaroly Pados 		break;
1933d4706c05SJohan Hovold 	default:
1934d4706c05SJohan Hovold 		max = 2000000;
1935d4706c05SJohan Hovold 		break;
1936d4706c05SJohan Hovold 	}
1937d4706c05SJohan Hovold 
193885bc2d91SJohanna Abrahamsson 	priv->min_speed = min;
1939d4706c05SJohan Hovold 	priv->max_speed = max;
19407aecd7fcSJohan Hovold 	priv->use_actual_rate = use_actual_rate;
1941d4706c05SJohan Hovold }
1942d4706c05SJohan Hovold 
1943*63a8eef7SJohan Hovold static int cp210x_get_fw_version(struct usb_serial *serial, u16 value)
1944*63a8eef7SJohan Hovold {
1945*63a8eef7SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1946*63a8eef7SJohan Hovold 	u8 ver[3];
1947*63a8eef7SJohan Hovold 	int ret;
1948*63a8eef7SJohan Hovold 
1949*63a8eef7SJohan Hovold 	ret = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST, value,
1950*63a8eef7SJohan Hovold 			ver, sizeof(ver));
1951*63a8eef7SJohan Hovold 	if (ret)
1952*63a8eef7SJohan Hovold 		return ret;
1953*63a8eef7SJohan Hovold 
1954*63a8eef7SJohan Hovold 	dev_dbg(&serial->interface->dev, "%s - %d.%d.%d\n", __func__,
1955*63a8eef7SJohan Hovold 			ver[0], ver[1], ver[2]);
1956*63a8eef7SJohan Hovold 
1957*63a8eef7SJohan Hovold 	priv->fw_version = ver[0] << 16 | ver[1] << 8 | ver[2];
1958*63a8eef7SJohan Hovold 
1959*63a8eef7SJohan Hovold 	return 0;
1960*63a8eef7SJohan Hovold }
1961*63a8eef7SJohan Hovold 
1962*63a8eef7SJohan Hovold static void cp210x_determine_quirks(struct usb_serial *serial)
1963*63a8eef7SJohan Hovold {
1964*63a8eef7SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1965*63a8eef7SJohan Hovold 	int ret;
1966*63a8eef7SJohan Hovold 
1967*63a8eef7SJohan Hovold 	switch (priv->partnum) {
1968*63a8eef7SJohan Hovold 	case CP210X_PARTNUM_CP2102N_QFN28:
1969*63a8eef7SJohan Hovold 	case CP210X_PARTNUM_CP2102N_QFN24:
1970*63a8eef7SJohan Hovold 	case CP210X_PARTNUM_CP2102N_QFN20:
1971*63a8eef7SJohan Hovold 		ret = cp210x_get_fw_version(serial, CP210X_GET_FW_VER_2N);
1972*63a8eef7SJohan Hovold 		if (ret)
1973*63a8eef7SJohan Hovold 			break;
1974*63a8eef7SJohan Hovold 		if (priv->fw_version <= 0x10004)
1975*63a8eef7SJohan Hovold 			priv->no_flow_control = true;
1976*63a8eef7SJohan Hovold 		break;
1977*63a8eef7SJohan Hovold 	default:
1978*63a8eef7SJohan Hovold 		break;
1979*63a8eef7SJohan Hovold 	}
1980*63a8eef7SJohan Hovold }
1981*63a8eef7SJohan Hovold 
1982cf5276ceSMartyn Welch static int cp210x_attach(struct usb_serial *serial)
1983cf5276ceSMartyn Welch {
1984cf5276ceSMartyn Welch 	int result;
1985cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv;
1986cf5276ceSMartyn Welch 
1987cf5276ceSMartyn Welch 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1988cf5276ceSMartyn Welch 	if (!priv)
1989cf5276ceSMartyn Welch 		return -ENOMEM;
1990cf5276ceSMartyn Welch 
1991cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1992cf5276ceSMartyn Welch 					  CP210X_GET_PARTNUM, &priv->partnum,
1993cf5276ceSMartyn Welch 					  sizeof(priv->partnum));
19947eac35eaSSebastian Frei 	if (result < 0) {
19957eac35eaSSebastian Frei 		dev_warn(&serial->interface->dev,
19967eac35eaSSebastian Frei 			 "querying part number failed\n");
19977eac35eaSSebastian Frei 		priv->partnum = CP210X_PARTNUM_UNKNOWN;
19987eac35eaSSebastian Frei 	}
1999cf5276ceSMartyn Welch 
2000cf5276ceSMartyn Welch 	usb_set_serial_data(serial, priv);
2001cf5276ceSMartyn Welch 
2002*63a8eef7SJohan Hovold 	cp210x_determine_quirks(serial);
2003d4706c05SJohan Hovold 	cp210x_init_max_speed(serial);
2004d4706c05SJohan Hovold 
2005c8acfe0aSKaroly Pados 	result = cp210x_gpio_init(serial);
2006cf5276ceSMartyn Welch 	if (result < 0) {
2007c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
2008c8acfe0aSKaroly Pados 				result);
2009cf5276ceSMartyn Welch 	}
2010cf5276ceSMartyn Welch 
2011cf5276ceSMartyn Welch 	return 0;
2012cf5276ceSMartyn Welch }
2013cf5276ceSMartyn Welch 
2014cf5276ceSMartyn Welch static void cp210x_disconnect(struct usb_serial *serial)
2015cf5276ceSMartyn Welch {
2016cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2017cf5276ceSMartyn Welch }
2018cf5276ceSMartyn Welch 
2019cf5276ceSMartyn Welch static void cp210x_release(struct usb_serial *serial)
2020cf5276ceSMartyn Welch {
2021cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2022cf5276ceSMartyn Welch 
2023cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2024cf5276ceSMartyn Welch 
2025cf5276ceSMartyn Welch 	kfree(priv);
2026cf5276ceSMartyn Welch }
2027cf5276ceSMartyn Welch 
202868e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
202903ee2515SGreg Kroah-Hartman 
203003ee2515SGreg Kroah-Hartman MODULE_DESCRIPTION(DRIVER_DESC);
2031627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
2032