xref: /linux/drivers/usb/serial/cp210x.c (revision 95168d624f3a8dee466525767200391a0fb006b9)
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)
603ee2515SGreg Kroah-Hartman  *
703ee2515SGreg Kroah-Hartman  * Support to set flow control line levels using TIOCMGET and TIOCMSET
803ee2515SGreg Kroah-Hartman  * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
903ee2515SGreg Kroah-Hartman  * control thanks to Munir Nassar nassarmu@real-time.com
1003ee2515SGreg Kroah-Hartman  *
1103ee2515SGreg Kroah-Hartman  */
1203ee2515SGreg Kroah-Hartman 
1303ee2515SGreg Kroah-Hartman #include <linux/kernel.h>
1403ee2515SGreg Kroah-Hartman #include <linux/errno.h>
1503ee2515SGreg Kroah-Hartman #include <linux/slab.h>
1603ee2515SGreg Kroah-Hartman #include <linux/tty.h>
1703ee2515SGreg Kroah-Hartman #include <linux/tty_flip.h>
1803ee2515SGreg Kroah-Hartman #include <linux/module.h>
1903ee2515SGreg Kroah-Hartman #include <linux/moduleparam.h>
2003ee2515SGreg Kroah-Hartman #include <linux/usb.h>
2103ee2515SGreg Kroah-Hartman #include <linux/uaccess.h>
2203ee2515SGreg Kroah-Hartman #include <linux/usb/serial.h>
23cf5276ceSMartyn Welch #include <linux/gpio/driver.h>
24cf5276ceSMartyn Welch #include <linux/bitops.h>
25cf5276ceSMartyn Welch #include <linux/mutex.h>
2603ee2515SGreg Kroah-Hartman 
274cc27bd6SCraig Shelley #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
2803ee2515SGreg Kroah-Hartman 
2903ee2515SGreg Kroah-Hartman /*
3003ee2515SGreg Kroah-Hartman  * Function Prototypes
3103ee2515SGreg Kroah-Hartman  */
32a509a7e4SAlan Cox static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
334cc27bd6SCraig Shelley static void cp210x_close(struct usb_serial_port *);
344f2ab888SJohan Hovold static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
354cc27bd6SCraig Shelley static void cp210x_get_termios_port(struct usb_serial_port *port,
36009615abSGeert Uytterhoeven 	tcflag_t *cflagp, unsigned int *baudp);
37e5990874SJohan Hovold static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
38e5990874SJohan Hovold 							struct ktermios *);
394cc27bd6SCraig Shelley static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
4003ee2515SGreg Kroah-Hartman 							struct ktermios*);
416f923a01SKonstantin Shkolnyy static bool cp210x_tx_empty(struct usb_serial_port *port);
4260b33c13SAlan Cox static int cp210x_tiocmget(struct tty_struct *);
4320b9d177SAlan Cox static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
4420b9d177SAlan Cox static int cp210x_tiocmset_port(struct usb_serial_port *port,
45d2ad67b3SVomLehn 		unsigned int, unsigned int);
464cc27bd6SCraig Shelley static void cp210x_break_ctl(struct tty_struct *, int);
47cf5276ceSMartyn Welch static int cp210x_attach(struct usb_serial *);
48cf5276ceSMartyn Welch static void cp210x_disconnect(struct usb_serial *);
49cf5276ceSMartyn Welch static void cp210x_release(struct usb_serial *);
50e2ae67a3SKonstantin Shkolnyy static int cp210x_port_probe(struct usb_serial_port *);
51e2ae67a3SKonstantin Shkolnyy static int cp210x_port_remove(struct usb_serial_port *);
52d94c7bd4SAlan Cox static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
53a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb);
54a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port);
55a7207e98SJohan Hovold static void cp210x_disable_event_mode(struct usb_serial_port *port);
5603ee2515SGreg Kroah-Hartman 
577d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
582f1136d1SDJ Delorie 	{ USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
5903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
6003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
615b22a32eSA E Lawrence 	{ USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
62b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
6390625070SLuiz Angelo Daros de Luca 	{ USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
6403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
65b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
66a6f03312SLennart Sorensen 	{ USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
67dd9d3d86SIvan Mironov 	{ USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
68eefd9029SCraig Shelley 	{ USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
69eefd9029SCraig Shelley 	{ USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
7003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
7103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
7203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
738de7f4daSLuis Llorente Campo 	{ USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
7403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
7503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
7603ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
77b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
780601e116SAmit Kucheria 	{ USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
790601e116SAmit Kucheria 	{ USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
8003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
8103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
82eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
83eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
8403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
8503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
86a595ecddSGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
8703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
88eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
8903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
905bbfa6f4SMikko Tuumanen 	{ USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
9103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
9203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
9303ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
9403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
9503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
968bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
9703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
9803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
99a57e82a1SSteve Conklin 	{ USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
1000bf7a81cSJason Detring 	{ USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
10103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
1023fcc8f96SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
10324160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
10424160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
10524160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
106eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
10703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
10803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
10908e87d0dSMalte Schröder 	{ USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
11003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
111eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
11203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
113f487c54dSPeter Dedecker 	{ USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
11403ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
11503ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
116bd07c551SAlan Cox 	{ USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
117b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
11803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
11903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
12025985edcSLucas De Marchi 	{ USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
12172b30079STristan Bruns 	{ USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
122c851e83fSFlorian Fainelli 	{ USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
12324160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
12424160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
12524160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
126613ac23aSJasem Mutlaq 	{ USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
127b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
12803ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
1298bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
13003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
131347bc8cbSGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
132faea63f7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
1338bf16ba7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
134faea63f7SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
135d23bac9fSAlex Stephens 	{ USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
136decc5360SKyle Jones 	{ USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
137541e05ecSCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
138613ac23aSJasem Mutlaq 	{ USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
13924160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
14043074132SDiego Elio Pettenò 	{ USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
14124160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
1424eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
1434eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
144356fe44fSMarkus Becker 	{ USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
1454eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
1464eff0b40SCraig Shelley 	{ USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
1471ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8856) },	/* CEL EM357 ZigBee USB Stick - LR */
1481ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8857) },	/* CEL EM357 ZigBee USB Stick */
14976811569SSami Rahman 	{ USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
15076811569SSami Rahman 	{ USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
15124160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
15224160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
153bfc2d7dfSJoe Savage 	{ USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
1542ab13292SPaul Jakma 	{ USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
1551ae78a48SDavid Peterson 	{ USB_DEVICE(0x10C4, 0x8977) },	/* CEL MeshWorks DevKit Device */
156c735ed74SMark Edwards 	{ USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
15724160628SJohan Hovold 	{ USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
158367b160fSOlli Salonen 	{ USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
159df72d588SJohn D. Blair 	{ USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
160fd90f73aSJeremie Rapin 	{ USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
1619585e340SStefan Triller 	{ USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
16203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
16303ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
1642f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
165a5360a53SPreston Fick 	{ USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
166eefd9029SCraig Shelley 	{ USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
1672f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
1682f839823SKaroly Pados 	{ USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
16903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
17003ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
17103ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
17203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
17303ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
174b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
1751d377f4dSMike Manning 	{ USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
1761d377f4dSMike Manning 	{ USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
17703ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
178b7c7cbc8SCraig Shelley 	{ USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
1791f1e82f7SJohan Hovold 	{ USB_DEVICE(0x155A, 0x1006) },	/* ELDAT Easywave RX09 */
1803fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
1813fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
18203ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
1833fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
1843fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
1853fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
1863fcc8f96SCraig Shelley 	{ USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
187675af708SMichiel vd Garde 	{ USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
188675af708SMichiel vd Garde 	{ USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
18903ee2515SGreg Kroah-Hartman 	{ USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
190356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
191356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
192356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
193356c5a48SAlessio Igor Bogani 	{ USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
194c6c1e449SBruno Thomsen 	{ USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
195c6c1e449SBruno Thomsen 	{ USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
196541e05ecSCraig Shelley 	{ USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
197541e05ecSCraig Shelley 	{ USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
198541e05ecSCraig Shelley 	{ USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
199f7d7f59aSOliver Freyermuth 	{ USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
200d14ac576SChristian Holl 	{ USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
201c496ad83SAndreas Engel 	{ USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
2026627ae19SKen Lin 	{ USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
2036627ae19SKen Lin 	{ USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
204cddc9434SMartyn Welch 	{ USB_DEVICE(0x1901, 0x0194) },	/* GE Healthcare Remote Alarm Box */
2059a593656SKen Lin 	{ USB_DEVICE(0x1901, 0x0195) },	/* GE B850/B650/B450 CP2104 DP UART interface */
2069a593656SKen Lin 	{ USB_DEVICE(0x1901, 0x0196) },	/* GE B850 CP2105 DP UART interface */
2073c4c615dSVittorio Alfieri 	{ USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
208b579fa52SBarry Grussling 	{ USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
209b9326057SAndras Kovacs 	{ USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
21035cc83eaSNathaniel Ting 	{ USB_DEVICE(0x1BA4, 0x0002) },	/* Silicon Labs 358x factory default */
21193ad03d6SAnders Larsen 	{ USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
212dee80ad1SAndreas Bomholtz 	{ USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
2133fcc8f96SCraig Shelley 	{ USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
2143fcc8f96SCraig Shelley 	{ USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
215be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
216be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
217be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
218be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
219be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
220be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
221be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
222be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
223be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
224be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
225be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
226be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
227be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
228be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
229be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
230be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
231be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
232be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
233be3101c2SMatwey V. Kornilov 	{ USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
234f98a7aa8SPeter Sanford 	{ USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
235791b7d7cSRenato Caldas 	{ USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
2363fcc8f96SCraig Shelley 	{ USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
2373fcc8f96SCraig Shelley 	{ USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
2381e23aaceSKyle Roeschley 	{ USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
239541e05ecSCraig Shelley 	{ USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
24003ee2515SGreg Kroah-Hartman 	{ } /* Terminating Entry */
24103ee2515SGreg Kroah-Hartman };
24203ee2515SGreg Kroah-Hartman 
24303ee2515SGreg Kroah-Hartman MODULE_DEVICE_TABLE(usb, id_table);
24403ee2515SGreg Kroah-Hartman 
245cf5276ceSMartyn Welch struct cp210x_serial_private {
246cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
247cf5276ceSMartyn Welch 	struct gpio_chip	gc;
2486b7271d2SJohan Hovold 	bool			gpio_registered;
249c8acfe0aSKaroly Pados 	u8			gpio_pushpull;
250c8acfe0aSKaroly Pados 	u8			gpio_altfunc;
251c8acfe0aSKaroly Pados 	u8			gpio_input;
252cf5276ceSMartyn Welch #endif
253cf5276ceSMartyn Welch 	u8			partnum;
25485bc2d91SJohanna Abrahamsson 	speed_t			min_speed;
255d4706c05SJohan Hovold 	speed_t			max_speed;
2567aecd7fcSJohan Hovold 	bool			use_actual_rate;
257cf5276ceSMartyn Welch };
258cf5276ceSMartyn Welch 
259a7207e98SJohan Hovold enum cp210x_event_state {
260a7207e98SJohan Hovold 	ES_DATA,
261a7207e98SJohan Hovold 	ES_ESCAPE,
262a7207e98SJohan Hovold 	ES_LSR,
263a7207e98SJohan Hovold 	ES_LSR_DATA_0,
264a7207e98SJohan Hovold 	ES_LSR_DATA_1,
265a7207e98SJohan Hovold 	ES_MSR
266a7207e98SJohan Hovold };
267a7207e98SJohan Hovold 
268e2ae67a3SKonstantin Shkolnyy struct cp210x_port_private {
26916045babSJohan Hovold 	u8			bInterfaceNumber;
270d0bf1ff0SKonstantin Shkolnyy 	bool			has_swapped_line_ctl;
271a7207e98SJohan Hovold 	bool			event_mode;
272a7207e98SJohan Hovold 	enum cp210x_event_state event_state;
273a7207e98SJohan Hovold 	u8 lsr;
274a5360a53SPreston Fick };
275a5360a53SPreston Fick 
2764cc27bd6SCraig Shelley static struct usb_serial_driver cp210x_device = {
27703ee2515SGreg Kroah-Hartman 	.driver = {
27803ee2515SGreg Kroah-Hartman 		.owner =	THIS_MODULE,
2794cc27bd6SCraig Shelley 		.name =		"cp210x",
28003ee2515SGreg Kroah-Hartman 	},
28103ee2515SGreg Kroah-Hartman 	.id_table		= id_table,
28203ee2515SGreg Kroah-Hartman 	.num_ports		= 1,
283aea006b9SJohan Hovold 	.bulk_in_size		= 256,
284d4e598f6SJohan Hovold 	.bulk_out_size		= 256,
2854cc27bd6SCraig Shelley 	.open			= cp210x_open,
2864cc27bd6SCraig Shelley 	.close			= cp210x_close,
2874cc27bd6SCraig Shelley 	.break_ctl		= cp210x_break_ctl,
2884cc27bd6SCraig Shelley 	.set_termios		= cp210x_set_termios,
2896f923a01SKonstantin Shkolnyy 	.tx_empty		= cp210x_tx_empty,
2904387b3dbSBrant Merryman 	.throttle		= usb_serial_generic_throttle,
2914387b3dbSBrant Merryman 	.unthrottle		= usb_serial_generic_unthrottle,
2924cc27bd6SCraig Shelley 	.tiocmget		= cp210x_tiocmget,
2934cc27bd6SCraig Shelley 	.tiocmset		= cp210x_tiocmset,
294de9c7e9fSJohan Hovold 	.get_icount		= usb_serial_generic_get_icount,
295cf5276ceSMartyn Welch 	.attach			= cp210x_attach,
296cf5276ceSMartyn Welch 	.disconnect		= cp210x_disconnect,
297cf5276ceSMartyn Welch 	.release		= cp210x_release,
298e2ae67a3SKonstantin Shkolnyy 	.port_probe		= cp210x_port_probe,
299e2ae67a3SKonstantin Shkolnyy 	.port_remove		= cp210x_port_remove,
300a7207e98SJohan Hovold 	.dtr_rts		= cp210x_dtr_rts,
301a7207e98SJohan Hovold 	.process_read_urb	= cp210x_process_read_urb,
30203ee2515SGreg Kroah-Hartman };
30303ee2515SGreg Kroah-Hartman 
30408a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
30508a4f6bcSAlan Stern 	&cp210x_device, NULL
30608a4f6bcSAlan Stern };
30708a4f6bcSAlan Stern 
30803ee2515SGreg Kroah-Hartman /* Config request types */
30932445605SPreston Fick #define REQTYPE_HOST_TO_INTERFACE	0x41
31032445605SPreston Fick #define REQTYPE_INTERFACE_TO_HOST	0xc1
31132445605SPreston Fick #define REQTYPE_HOST_TO_DEVICE	0x40
31232445605SPreston Fick #define REQTYPE_DEVICE_TO_HOST	0xc0
31303ee2515SGreg Kroah-Hartman 
31493ef1f1fSCraig Shelley /* Config request codes */
31593ef1f1fSCraig Shelley #define CP210X_IFC_ENABLE	0x00
31693ef1f1fSCraig Shelley #define CP210X_SET_BAUDDIV	0x01
31793ef1f1fSCraig Shelley #define CP210X_GET_BAUDDIV	0x02
31893ef1f1fSCraig Shelley #define CP210X_SET_LINE_CTL	0x03
31993ef1f1fSCraig Shelley #define CP210X_GET_LINE_CTL	0x04
32093ef1f1fSCraig Shelley #define CP210X_SET_BREAK	0x05
32193ef1f1fSCraig Shelley #define CP210X_IMM_CHAR		0x06
32293ef1f1fSCraig Shelley #define CP210X_SET_MHS		0x07
32393ef1f1fSCraig Shelley #define CP210X_GET_MDMSTS	0x08
32493ef1f1fSCraig Shelley #define CP210X_SET_XON		0x09
32593ef1f1fSCraig Shelley #define CP210X_SET_XOFF		0x0A
32693ef1f1fSCraig Shelley #define CP210X_SET_EVENTMASK	0x0B
32793ef1f1fSCraig Shelley #define CP210X_GET_EVENTMASK	0x0C
32893ef1f1fSCraig Shelley #define CP210X_SET_CHAR		0x0D
32993ef1f1fSCraig Shelley #define CP210X_GET_CHARS	0x0E
33093ef1f1fSCraig Shelley #define CP210X_GET_PROPS	0x0F
33193ef1f1fSCraig Shelley #define CP210X_GET_COMM_STATUS	0x10
33293ef1f1fSCraig Shelley #define CP210X_RESET		0x11
33393ef1f1fSCraig Shelley #define CP210X_PURGE		0x12
33493ef1f1fSCraig Shelley #define CP210X_SET_FLOW		0x13
33593ef1f1fSCraig Shelley #define CP210X_GET_FLOW		0x14
33693ef1f1fSCraig Shelley #define CP210X_EMBED_EVENTS	0x15
33793ef1f1fSCraig Shelley #define CP210X_GET_EVENTSTATE	0x16
33893ef1f1fSCraig Shelley #define CP210X_SET_CHARS	0x19
3397f482fc8SPreston Fick #define CP210X_GET_BAUDRATE	0x1D
3407f482fc8SPreston Fick #define CP210X_SET_BAUDRATE	0x1E
341cf5276ceSMartyn Welch #define CP210X_VENDOR_SPECIFIC	0xFF
34203ee2515SGreg Kroah-Hartman 
34393ef1f1fSCraig Shelley /* CP210X_IFC_ENABLE */
34403ee2515SGreg Kroah-Hartman #define UART_ENABLE		0x0001
34503ee2515SGreg Kroah-Hartman #define UART_DISABLE		0x0000
34603ee2515SGreg Kroah-Hartman 
34793ef1f1fSCraig Shelley /* CP210X_(SET|GET)_BAUDDIV */
34803ee2515SGreg Kroah-Hartman #define BAUD_RATE_GEN_FREQ	0x384000
34903ee2515SGreg Kroah-Hartman 
35093ef1f1fSCraig Shelley /* CP210X_(SET|GET)_LINE_CTL */
35103ee2515SGreg Kroah-Hartman #define BITS_DATA_MASK		0X0f00
35203ee2515SGreg Kroah-Hartman #define BITS_DATA_5		0X0500
35303ee2515SGreg Kroah-Hartman #define BITS_DATA_6		0X0600
35403ee2515SGreg Kroah-Hartman #define BITS_DATA_7		0X0700
35503ee2515SGreg Kroah-Hartman #define BITS_DATA_8		0X0800
35603ee2515SGreg Kroah-Hartman #define BITS_DATA_9		0X0900
35703ee2515SGreg Kroah-Hartman 
35803ee2515SGreg Kroah-Hartman #define BITS_PARITY_MASK	0x00f0
35903ee2515SGreg Kroah-Hartman #define BITS_PARITY_NONE	0x0000
36003ee2515SGreg Kroah-Hartman #define BITS_PARITY_ODD		0x0010
36103ee2515SGreg Kroah-Hartman #define BITS_PARITY_EVEN	0x0020
36203ee2515SGreg Kroah-Hartman #define BITS_PARITY_MARK	0x0030
36303ee2515SGreg Kroah-Hartman #define BITS_PARITY_SPACE	0x0040
36403ee2515SGreg Kroah-Hartman 
36503ee2515SGreg Kroah-Hartman #define BITS_STOP_MASK		0x000f
36603ee2515SGreg Kroah-Hartman #define BITS_STOP_1		0x0000
36703ee2515SGreg Kroah-Hartman #define BITS_STOP_1_5		0x0001
36803ee2515SGreg Kroah-Hartman #define BITS_STOP_2		0x0002
36903ee2515SGreg Kroah-Hartman 
37093ef1f1fSCraig Shelley /* CP210X_SET_BREAK */
37172916791SCraig Shelley #define BREAK_ON		0x0001
37272916791SCraig Shelley #define BREAK_OFF		0x0000
37303ee2515SGreg Kroah-Hartman 
37493ef1f1fSCraig Shelley /* CP210X_(SET_MHS|GET_MDMSTS) */
37503ee2515SGreg Kroah-Hartman #define CONTROL_DTR		0x0001
37603ee2515SGreg Kroah-Hartman #define CONTROL_RTS		0x0002
37703ee2515SGreg Kroah-Hartman #define CONTROL_CTS		0x0010
37803ee2515SGreg Kroah-Hartman #define CONTROL_DSR		0x0020
37903ee2515SGreg Kroah-Hartman #define CONTROL_RING		0x0040
38003ee2515SGreg Kroah-Hartman #define CONTROL_DCD		0x0080
38103ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_DTR	0x0100
38203ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_RTS	0x0200
38303ee2515SGreg Kroah-Hartman 
384cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC values */
385c8acfe0aSKaroly Pados #define CP210X_READ_2NCONFIG	0x000E
386cf5276ceSMartyn Welch #define CP210X_READ_LATCH	0x00C2
387cf5276ceSMartyn Welch #define CP210X_GET_PARTNUM	0x370B
388cf5276ceSMartyn Welch #define CP210X_GET_PORTCONFIG	0x370C
389cf5276ceSMartyn Welch #define CP210X_GET_DEVICEMODE	0x3711
390cf5276ceSMartyn Welch #define CP210X_WRITE_LATCH	0x37E1
391cf5276ceSMartyn Welch 
392cf5276ceSMartyn Welch /* Part number definitions */
393cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2101	0x01
394cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2102	0x02
395cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2103	0x03
396cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2104	0x04
397cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2105	0x05
398cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2108	0x08
3996f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN28	0x20
4006f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN24	0x21
4016f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN20	0x22
4027eac35eaSSebastian Frei #define CP210X_PARTNUM_UNKNOWN	0xFF
403cf5276ceSMartyn Welch 
4046f923a01SKonstantin Shkolnyy /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
4056f923a01SKonstantin Shkolnyy struct cp210x_comm_status {
4066f923a01SKonstantin Shkolnyy 	__le32   ulErrors;
4076f923a01SKonstantin Shkolnyy 	__le32   ulHoldReasons;
4086f923a01SKonstantin Shkolnyy 	__le32   ulAmountInInQueue;
4096f923a01SKonstantin Shkolnyy 	__le32   ulAmountInOutQueue;
4106f923a01SKonstantin Shkolnyy 	u8       bEofReceived;
4116f923a01SKonstantin Shkolnyy 	u8       bWaitForImmediate;
4126f923a01SKonstantin Shkolnyy 	u8       bReserved;
4136f923a01SKonstantin Shkolnyy } __packed;
4146f923a01SKonstantin Shkolnyy 
41503ee2515SGreg Kroah-Hartman /*
416ebfb319bSKonstantin Shkolnyy  * CP210X_PURGE - 16 bits passed in wValue of USB request.
417ebfb319bSKonstantin Shkolnyy  * SiLabs app note AN571 gives a strange description of the 4 bits:
418ebfb319bSKonstantin Shkolnyy  * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
419ebfb319bSKonstantin Shkolnyy  * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
420ebfb319bSKonstantin Shkolnyy  */
421ebfb319bSKonstantin Shkolnyy #define PURGE_ALL		0x000f
422ebfb319bSKonstantin Shkolnyy 
423a7207e98SJohan Hovold /* CP210X_EMBED_EVENTS */
424a7207e98SJohan Hovold #define CP210X_ESCCHAR		0xec
425a7207e98SJohan Hovold 
426a7207e98SJohan Hovold #define CP210X_LSR_OVERRUN	BIT(1)
427a7207e98SJohan Hovold #define CP210X_LSR_PARITY	BIT(2)
428a7207e98SJohan Hovold #define CP210X_LSR_FRAME	BIT(3)
429a7207e98SJohan Hovold #define CP210X_LSR_BREAK	BIT(4)
430a7207e98SJohan Hovold 
431a7207e98SJohan Hovold 
4329034389cSKonstantin Shkolnyy /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
4339034389cSKonstantin Shkolnyy struct cp210x_flow_ctl {
4349034389cSKonstantin Shkolnyy 	__le32	ulControlHandshake;
4359034389cSKonstantin Shkolnyy 	__le32	ulFlowReplace;
4369034389cSKonstantin Shkolnyy 	__le32	ulXonLimit;
4379034389cSKonstantin Shkolnyy 	__le32	ulXoffLimit;
438ba84190eSJohan Hovold };
4399034389cSKonstantin Shkolnyy 
4409034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulControlHandshake */
4419034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_MASK		GENMASK(1, 0)
4429034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_SHIFT(_mode)	(_mode)
4439034389cSKonstantin Shkolnyy #define CP210X_SERIAL_CTS_HANDSHAKE	BIT(3)
4449034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_HANDSHAKE	BIT(4)
4459034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DCD_HANDSHAKE	BIT(5)
4469034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_SENSITIVITY	BIT(6)
4479034389cSKonstantin Shkolnyy 
4489034389cSKonstantin Shkolnyy /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
4499034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_INACTIVE	0
4509034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_ACTIVE	1
4519034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_FLOW_CTL	2
4529034389cSKonstantin Shkolnyy 
4539034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulFlowReplace */
4549034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_TRANSMIT	BIT(0)
4559034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_RECEIVE	BIT(1)
4569034389cSKonstantin Shkolnyy #define CP210X_SERIAL_ERROR_CHAR	BIT(2)
4579034389cSKonstantin Shkolnyy #define CP210X_SERIAL_NULL_STRIPPING	BIT(3)
4589034389cSKonstantin Shkolnyy #define CP210X_SERIAL_BREAK_CHAR	BIT(4)
4599034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_MASK		GENMASK(7, 6)
4609034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_SHIFT(_mode)	(_mode << 6)
4619034389cSKonstantin Shkolnyy #define CP210X_SERIAL_XOFF_CONTINUE	BIT(31)
4629034389cSKonstantin Shkolnyy 
4639034389cSKonstantin Shkolnyy /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
4649034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_INACTIVE	0
4659034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_ACTIVE	1
4669034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_FLOW_CTL	2
4679034389cSKonstantin Shkolnyy 
468cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
469cf5276ceSMartyn Welch struct cp210x_pin_mode {
470cf5276ceSMartyn Welch 	u8	eci;
471cf5276ceSMartyn Welch 	u8	sci;
472ba84190eSJohan Hovold };
473cf5276ceSMartyn Welch 
474cf5276ceSMartyn Welch #define CP210X_PIN_MODE_MODEM		0
475cf5276ceSMartyn Welch #define CP210X_PIN_MODE_GPIO		BIT(0)
476cf5276ceSMartyn Welch 
477cf5276ceSMartyn Welch /*
47815fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
47915fb84b7SIcenowy Zheng  * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
480cf5276ceSMartyn Welch  */
48115fb84b7SIcenowy Zheng struct cp210x_dual_port_config {
482cf5276ceSMartyn Welch 	__le16	gpio_mode;
483cf5276ceSMartyn Welch 	u8	__pad0[2];
484cf5276ceSMartyn Welch 	__le16	reset_state;
485cf5276ceSMartyn Welch 	u8	__pad1[4];
486cf5276ceSMartyn Welch 	__le16	suspend_state;
487cf5276ceSMartyn Welch 	u8	sci_cfg;
488cf5276ceSMartyn Welch 	u8	eci_cfg;
489cf5276ceSMartyn Welch 	u8	device_cfg;
490cf5276ceSMartyn Welch } __packed;
491cf5276ceSMartyn Welch 
49215fb84b7SIcenowy Zheng /*
49315fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
49415fb84b7SIcenowy Zheng  * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
49515fb84b7SIcenowy Zheng  */
49615fb84b7SIcenowy Zheng struct cp210x_single_port_config {
49715fb84b7SIcenowy Zheng 	__le16	gpio_mode;
49815fb84b7SIcenowy Zheng 	u8	__pad0[2];
49915fb84b7SIcenowy Zheng 	__le16	reset_state;
50015fb84b7SIcenowy Zheng 	u8	__pad1[4];
50115fb84b7SIcenowy Zheng 	__le16	suspend_state;
50215fb84b7SIcenowy Zheng 	u8	device_cfg;
50315fb84b7SIcenowy Zheng } __packed;
50415fb84b7SIcenowy Zheng 
505cf5276ceSMartyn Welch /* GPIO modes */
506cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_OFFSET	9
507cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_MASK	GENMASK(11, 9)
508cf5276ceSMartyn Welch 
509cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_OFFSET	2
510cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_MASK	GENMASK(3, 2)
511cf5276ceSMartyn Welch 
51215fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_OFFSET		8
51315fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_MASK		GENMASK(11, 8)
51415fb84b7SIcenowy Zheng 
515cf5276ceSMartyn Welch /* CP2105 port configuration values */
516cf5276ceSMartyn Welch #define CP2105_GPIO0_TXLED_MODE		BIT(0)
517cf5276ceSMartyn Welch #define CP2105_GPIO1_RXLED_MODE		BIT(1)
518cf5276ceSMartyn Welch #define CP2105_GPIO1_RS485_MODE		BIT(2)
519cf5276ceSMartyn Welch 
52015fb84b7SIcenowy Zheng /* CP2104 port configuration values */
52115fb84b7SIcenowy Zheng #define CP2104_GPIO0_TXLED_MODE		BIT(0)
52215fb84b7SIcenowy Zheng #define CP2104_GPIO1_RXLED_MODE		BIT(1)
52315fb84b7SIcenowy Zheng #define CP2104_GPIO2_RS485_MODE		BIT(2)
52415fb84b7SIcenowy Zheng 
525c8acfe0aSKaroly Pados /* CP2102N configuration array indices */
526c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_CONFIG_VERSION_IDX	2
527c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_MODE_IDX		581
528c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX	587
529c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_CONTROL_IDX	600
530c8acfe0aSKaroly Pados 
531cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
532cf5276ceSMartyn Welch struct cp210x_gpio_write {
533cf5276ceSMartyn Welch 	u8	mask;
534cf5276ceSMartyn Welch 	u8	state;
535ba84190eSJohan Hovold };
536cf5276ceSMartyn Welch 
537cf5276ceSMartyn Welch /*
538cf5276ceSMartyn Welch  * Helper to get interface number when we only have struct usb_serial.
539cf5276ceSMartyn Welch  */
540cf5276ceSMartyn Welch static u8 cp210x_interface_num(struct usb_serial *serial)
541cf5276ceSMartyn Welch {
542cf5276ceSMartyn Welch 	struct usb_host_interface *cur_altsetting;
543cf5276ceSMartyn Welch 
544cf5276ceSMartyn Welch 	cur_altsetting = serial->interface->cur_altsetting;
545cf5276ceSMartyn Welch 
546cf5276ceSMartyn Welch 	return cur_altsetting->desc.bInterfaceNumber;
547cf5276ceSMartyn Welch }
548cf5276ceSMartyn Welch 
54903ee2515SGreg Kroah-Hartman /*
55019165b2bSKonstantin Shkolnyy  * Reads a variable-sized block of CP210X_ registers, identified by req.
55119165b2bSKonstantin Shkolnyy  * Returns data into buf in native USB byte order.
55203ee2515SGreg Kroah-Hartman  */
55319165b2bSKonstantin Shkolnyy static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
55419165b2bSKonstantin Shkolnyy 		void *buf, int bufsize)
55503ee2515SGreg Kroah-Hartman {
55603ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
557e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
55819165b2bSKonstantin Shkolnyy 	void *dmabuf;
55919165b2bSKonstantin Shkolnyy 	int result;
56003ee2515SGreg Kroah-Hartman 
56119165b2bSKonstantin Shkolnyy 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
56219165b2bSKonstantin Shkolnyy 	if (!dmabuf) {
56319165b2bSKonstantin Shkolnyy 		/*
56419165b2bSKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
56519165b2bSKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
56619165b2bSKonstantin Shkolnyy 		 */
56719165b2bSKonstantin Shkolnyy 		memset(buf, 0, bufsize);
56803ee2515SGreg Kroah-Hartman 		return -ENOMEM;
56919165b2bSKonstantin Shkolnyy 	}
57003ee2515SGreg Kroah-Hartman 
57103ee2515SGreg Kroah-Hartman 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
57219165b2bSKonstantin Shkolnyy 			req, REQTYPE_INTERFACE_TO_HOST, 0,
57319165b2bSKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
57419165b2bSKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
57519165b2bSKonstantin Shkolnyy 	if (result == bufsize) {
57619165b2bSKonstantin Shkolnyy 		memcpy(buf, dmabuf, bufsize);
57719165b2bSKonstantin Shkolnyy 		result = 0;
57819165b2bSKonstantin Shkolnyy 	} else {
57919165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
58019165b2bSKonstantin Shkolnyy 				req, bufsize, result);
58119165b2bSKonstantin Shkolnyy 		if (result >= 0)
5820407f1ceSJohan Hovold 			result = -EIO;
5832479e2a9SJohan Hovold 
58419165b2bSKonstantin Shkolnyy 		/*
58519165b2bSKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
58619165b2bSKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
58719165b2bSKonstantin Shkolnyy 		 */
58819165b2bSKonstantin Shkolnyy 		memset(buf, 0, bufsize);
58919165b2bSKonstantin Shkolnyy 	}
59019165b2bSKonstantin Shkolnyy 
59119165b2bSKonstantin Shkolnyy 	kfree(dmabuf);
59219165b2bSKonstantin Shkolnyy 
5932479e2a9SJohan Hovold 	return result;
59403ee2515SGreg Kroah-Hartman }
59503ee2515SGreg Kroah-Hartman 
59619165b2bSKonstantin Shkolnyy /*
597fe1b07e9SKonstantin Shkolnyy  * Reads any 32-bit CP210X_ register identified by req.
598fe1b07e9SKonstantin Shkolnyy  */
599fe1b07e9SKonstantin Shkolnyy static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
600fe1b07e9SKonstantin Shkolnyy {
601fe1b07e9SKonstantin Shkolnyy 	__le32 le32_val;
602fe1b07e9SKonstantin Shkolnyy 	int err;
603fe1b07e9SKonstantin Shkolnyy 
604fe1b07e9SKonstantin Shkolnyy 	err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
605fe1b07e9SKonstantin Shkolnyy 	if (err) {
606fe1b07e9SKonstantin Shkolnyy 		/*
607fe1b07e9SKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
608fe1b07e9SKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
609fe1b07e9SKonstantin Shkolnyy 		 */
610fe1b07e9SKonstantin Shkolnyy 		*val = 0;
611fe1b07e9SKonstantin Shkolnyy 		return err;
612fe1b07e9SKonstantin Shkolnyy 	}
613fe1b07e9SKonstantin Shkolnyy 
614fe1b07e9SKonstantin Shkolnyy 	*val = le32_to_cpu(le32_val);
615fe1b07e9SKonstantin Shkolnyy 
61603ee2515SGreg Kroah-Hartman 	return 0;
61703ee2515SGreg Kroah-Hartman }
61803ee2515SGreg Kroah-Hartman 
61903ee2515SGreg Kroah-Hartman /*
62019165b2bSKonstantin Shkolnyy  * Reads any 16-bit CP210X_ register identified by req.
62103ee2515SGreg Kroah-Hartman  */
62219165b2bSKonstantin Shkolnyy static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
62319165b2bSKonstantin Shkolnyy {
62419165b2bSKonstantin Shkolnyy 	__le16 le16_val;
62519165b2bSKonstantin Shkolnyy 	int err;
62619165b2bSKonstantin Shkolnyy 
62719165b2bSKonstantin Shkolnyy 	err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
62819165b2bSKonstantin Shkolnyy 	if (err)
62919165b2bSKonstantin Shkolnyy 		return err;
63019165b2bSKonstantin Shkolnyy 
63119165b2bSKonstantin Shkolnyy 	*val = le16_to_cpu(le16_val);
632fe1b07e9SKonstantin Shkolnyy 
63319165b2bSKonstantin Shkolnyy 	return 0;
63419165b2bSKonstantin Shkolnyy }
63519165b2bSKonstantin Shkolnyy 
63619165b2bSKonstantin Shkolnyy /*
637fe1b07e9SKonstantin Shkolnyy  * Reads any 8-bit CP210X_ register identified by req.
638fe1b07e9SKonstantin Shkolnyy  */
639fe1b07e9SKonstantin Shkolnyy static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
640fe1b07e9SKonstantin Shkolnyy {
641fe1b07e9SKonstantin Shkolnyy 	return cp210x_read_reg_block(port, req, val, sizeof(*val));
642fe1b07e9SKonstantin Shkolnyy }
643fe1b07e9SKonstantin Shkolnyy 
644fe1b07e9SKonstantin Shkolnyy /*
645cf5276ceSMartyn Welch  * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
646cf5276ceSMartyn Welch  * Returns data into buf in native USB byte order.
647cf5276ceSMartyn Welch  */
648cf5276ceSMartyn Welch static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
649cf5276ceSMartyn Welch 				    void *buf, int bufsize)
650cf5276ceSMartyn Welch {
651cf5276ceSMartyn Welch 	void *dmabuf;
652cf5276ceSMartyn Welch 	int result;
653cf5276ceSMartyn Welch 
654cf5276ceSMartyn Welch 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
655cf5276ceSMartyn Welch 	if (!dmabuf)
656cf5276ceSMartyn Welch 		return -ENOMEM;
657cf5276ceSMartyn Welch 
658cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
659cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
660cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
661cf5276ceSMartyn Welch 				 USB_CTRL_GET_TIMEOUT);
662cf5276ceSMartyn Welch 	if (result == bufsize) {
663cf5276ceSMartyn Welch 		memcpy(buf, dmabuf, bufsize);
664cf5276ceSMartyn Welch 		result = 0;
665cf5276ceSMartyn Welch 	} else {
666cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
667cf5276ceSMartyn Welch 			"failed to get vendor val 0x%04x size %d: %d\n", val,
668cf5276ceSMartyn Welch 			bufsize, result);
669cf5276ceSMartyn Welch 		if (result >= 0)
670cf5276ceSMartyn Welch 			result = -EIO;
671cf5276ceSMartyn Welch 	}
672cf5276ceSMartyn Welch 
673cf5276ceSMartyn Welch 	kfree(dmabuf);
674cf5276ceSMartyn Welch 
675cf5276ceSMartyn Welch 	return result;
676cf5276ceSMartyn Welch }
677cf5276ceSMartyn Welch 
678cf5276ceSMartyn Welch /*
67919165b2bSKonstantin Shkolnyy  * Writes any 16-bit CP210X_ register (req) whose value is passed
68019165b2bSKonstantin Shkolnyy  * entirely in the wValue field of the USB request.
68119165b2bSKonstantin Shkolnyy  */
68219165b2bSKonstantin Shkolnyy static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
68303ee2515SGreg Kroah-Hartman {
68403ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
685e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
68619165b2bSKonstantin Shkolnyy 	int result;
68703ee2515SGreg Kroah-Hartman 
68819165b2bSKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
68919165b2bSKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, val,
690e2ae67a3SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, NULL, 0,
6912d5733fcSYuri Matylitski 			USB_CTRL_SET_TIMEOUT);
69219165b2bSKonstantin Shkolnyy 	if (result < 0) {
69319165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed set request 0x%x status: %d\n",
69419165b2bSKonstantin Shkolnyy 				req, result);
69503ee2515SGreg Kroah-Hartman 	}
69603ee2515SGreg Kroah-Hartman 
6972479e2a9SJohan Hovold 	return result;
69803ee2515SGreg Kroah-Hartman }
69903ee2515SGreg Kroah-Hartman 
70003ee2515SGreg Kroah-Hartman /*
701fe1b07e9SKonstantin Shkolnyy  * Writes a variable-sized block of CP210X_ registers, identified by req.
702fe1b07e9SKonstantin Shkolnyy  * Data in buf must be in native USB byte order.
703fe1b07e9SKonstantin Shkolnyy  */
704fe1b07e9SKonstantin Shkolnyy static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
705fe1b07e9SKonstantin Shkolnyy 		void *buf, int bufsize)
706fe1b07e9SKonstantin Shkolnyy {
707fe1b07e9SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
708fe1b07e9SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
709fe1b07e9SKonstantin Shkolnyy 	void *dmabuf;
710fe1b07e9SKonstantin Shkolnyy 	int result;
711fe1b07e9SKonstantin Shkolnyy 
712d6c4dc3bSMuhammad Falak R Wani 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
713fe1b07e9SKonstantin Shkolnyy 	if (!dmabuf)
714fe1b07e9SKonstantin Shkolnyy 		return -ENOMEM;
715fe1b07e9SKonstantin Shkolnyy 
716fe1b07e9SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
717fe1b07e9SKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, 0,
718fe1b07e9SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
719fe1b07e9SKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
720fe1b07e9SKonstantin Shkolnyy 
721fe1b07e9SKonstantin Shkolnyy 	kfree(dmabuf);
722fe1b07e9SKonstantin Shkolnyy 
723fe1b07e9SKonstantin Shkolnyy 	if (result == bufsize) {
724fe1b07e9SKonstantin Shkolnyy 		result = 0;
725fe1b07e9SKonstantin Shkolnyy 	} else {
726fe1b07e9SKonstantin Shkolnyy 		dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
727fe1b07e9SKonstantin Shkolnyy 				req, bufsize, result);
728fe1b07e9SKonstantin Shkolnyy 		if (result >= 0)
7290407f1ceSJohan Hovold 			result = -EIO;
730fe1b07e9SKonstantin Shkolnyy 	}
731fe1b07e9SKonstantin Shkolnyy 
732fe1b07e9SKonstantin Shkolnyy 	return result;
73303ee2515SGreg Kroah-Hartman }
73403ee2515SGreg Kroah-Hartman 
73503ee2515SGreg Kroah-Hartman /*
736fe1b07e9SKonstantin Shkolnyy  * Writes any 32-bit CP210X_ register identified by req.
73703ee2515SGreg Kroah-Hartman  */
738fe1b07e9SKonstantin Shkolnyy static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
73903ee2515SGreg Kroah-Hartman {
740fe1b07e9SKonstantin Shkolnyy 	__le32 le32_val;
741fe1b07e9SKonstantin Shkolnyy 
742fe1b07e9SKonstantin Shkolnyy 	le32_val = cpu_to_le32(val);
743fe1b07e9SKonstantin Shkolnyy 
744fe1b07e9SKonstantin Shkolnyy 	return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
74503ee2515SGreg Kroah-Hartman }
74603ee2515SGreg Kroah-Hartman 
747cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
748cf5276ceSMartyn Welch /*
749cf5276ceSMartyn Welch  * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
750cf5276ceSMartyn Welch  * Data in buf must be in native USB byte order.
751cf5276ceSMartyn Welch  */
752cf5276ceSMartyn Welch static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
753cf5276ceSMartyn Welch 				     u16 val, void *buf, int bufsize)
754cf5276ceSMartyn Welch {
755cf5276ceSMartyn Welch 	void *dmabuf;
756cf5276ceSMartyn Welch 	int result;
757cf5276ceSMartyn Welch 
758cf5276ceSMartyn Welch 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
759cf5276ceSMartyn Welch 	if (!dmabuf)
760cf5276ceSMartyn Welch 		return -ENOMEM;
761cf5276ceSMartyn Welch 
762cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
763cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
764cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
765cf5276ceSMartyn Welch 				 USB_CTRL_SET_TIMEOUT);
766cf5276ceSMartyn Welch 
767cf5276ceSMartyn Welch 	kfree(dmabuf);
768cf5276ceSMartyn Welch 
769cf5276ceSMartyn Welch 	if (result == bufsize) {
770cf5276ceSMartyn Welch 		result = 0;
771cf5276ceSMartyn Welch 	} else {
772cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
773cf5276ceSMartyn Welch 			"failed to set vendor val 0x%04x size %d: %d\n", val,
774cf5276ceSMartyn Welch 			bufsize, result);
775cf5276ceSMartyn Welch 		if (result >= 0)
776cf5276ceSMartyn Welch 			result = -EIO;
777cf5276ceSMartyn Welch 	}
778cf5276ceSMartyn Welch 
779cf5276ceSMartyn Welch 	return result;
780cf5276ceSMartyn Welch }
781cf5276ceSMartyn Welch #endif
782cf5276ceSMartyn Welch 
78303ee2515SGreg Kroah-Hartman /*
784d0bf1ff0SKonstantin Shkolnyy  * Detect CP2108 GET_LINE_CTL bug and activate workaround.
785d0bf1ff0SKonstantin Shkolnyy  * Write a known good value 0x800, read it back.
786d0bf1ff0SKonstantin Shkolnyy  * If it comes back swapped the bug is detected.
787d0bf1ff0SKonstantin Shkolnyy  * Preserve the original register value.
788d0bf1ff0SKonstantin Shkolnyy  */
789d0bf1ff0SKonstantin Shkolnyy static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
790d0bf1ff0SKonstantin Shkolnyy {
791d0bf1ff0SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
79219165b2bSKonstantin Shkolnyy 	u16 line_ctl_save;
79319165b2bSKonstantin Shkolnyy 	u16 line_ctl_test;
794d0bf1ff0SKonstantin Shkolnyy 	int err;
795d0bf1ff0SKonstantin Shkolnyy 
79619165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
797d0bf1ff0SKonstantin Shkolnyy 	if (err)
798d0bf1ff0SKonstantin Shkolnyy 		return err;
799d0bf1ff0SKonstantin Shkolnyy 
80019165b2bSKonstantin Shkolnyy 	err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
801d0bf1ff0SKonstantin Shkolnyy 	if (err)
802d0bf1ff0SKonstantin Shkolnyy 		return err;
803d0bf1ff0SKonstantin Shkolnyy 
80419165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
805d0bf1ff0SKonstantin Shkolnyy 	if (err)
806d0bf1ff0SKonstantin Shkolnyy 		return err;
807d0bf1ff0SKonstantin Shkolnyy 
808d0bf1ff0SKonstantin Shkolnyy 	if (line_ctl_test == 8) {
809d0bf1ff0SKonstantin Shkolnyy 		port_priv->has_swapped_line_ctl = true;
81019165b2bSKonstantin Shkolnyy 		line_ctl_save = swab16(line_ctl_save);
811d0bf1ff0SKonstantin Shkolnyy 	}
812d0bf1ff0SKonstantin Shkolnyy 
81319165b2bSKonstantin Shkolnyy 	return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
814d0bf1ff0SKonstantin Shkolnyy }
815d0bf1ff0SKonstantin Shkolnyy 
816d0bf1ff0SKonstantin Shkolnyy /*
81719165b2bSKonstantin Shkolnyy  * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
818d0bf1ff0SKonstantin Shkolnyy  * to workaround cp2108 bug and get correct value.
819d0bf1ff0SKonstantin Shkolnyy  */
82019165b2bSKonstantin Shkolnyy static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
821d0bf1ff0SKonstantin Shkolnyy {
822d0bf1ff0SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
823d0bf1ff0SKonstantin Shkolnyy 	int err;
824d0bf1ff0SKonstantin Shkolnyy 
82519165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
826d0bf1ff0SKonstantin Shkolnyy 	if (err)
827d0bf1ff0SKonstantin Shkolnyy 		return err;
828d0bf1ff0SKonstantin Shkolnyy 
829d0bf1ff0SKonstantin Shkolnyy 	/* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
830d0bf1ff0SKonstantin Shkolnyy 	if (port_priv->has_swapped_line_ctl)
83119165b2bSKonstantin Shkolnyy 		*ctl = swab16(*ctl);
832d0bf1ff0SKonstantin Shkolnyy 
833d0bf1ff0SKonstantin Shkolnyy 	return 0;
834d0bf1ff0SKonstantin Shkolnyy }
835d0bf1ff0SKonstantin Shkolnyy 
836a509a7e4SAlan Cox static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
83703ee2515SGreg Kroah-Hartman {
838a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
83903ee2515SGreg Kroah-Hartman 	int result;
84003ee2515SGreg Kroah-Hartman 
84119165b2bSKonstantin Shkolnyy 	result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
8422479e2a9SJohan Hovold 	if (result) {
8432479e2a9SJohan Hovold 		dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
8442479e2a9SJohan Hovold 		return result;
84503ee2515SGreg Kroah-Hartman 	}
84603ee2515SGreg Kroah-Hartman 
84703ee2515SGreg Kroah-Hartman 	/* Configure the termios structure */
8484cc27bd6SCraig Shelley 	cp210x_get_termios(tty, port);
84955b2afbbSJohan Hovold 
850a7207e98SJohan Hovold 	if (tty) {
851cdc32fd6SJohan Hovold 		/* The baud rate must be initialised on cp2104 */
852cdc32fd6SJohan Hovold 		cp210x_change_speed(tty, port, NULL);
853cdc32fd6SJohan Hovold 
854a7207e98SJohan Hovold 		if (I_INPCK(tty))
855a7207e98SJohan Hovold 			cp210x_enable_event_mode(port);
856a7207e98SJohan Hovold 	}
857a7207e98SJohan Hovold 
858bcbb9d81SJohan Hovold 	result = usb_serial_generic_open(tty, port);
859bcbb9d81SJohan Hovold 	if (result)
860bcbb9d81SJohan Hovold 		goto err_disable;
861bcbb9d81SJohan Hovold 
862bcbb9d81SJohan Hovold 	return 0;
863bcbb9d81SJohan Hovold 
864bcbb9d81SJohan Hovold err_disable:
865bcbb9d81SJohan Hovold 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
866a7207e98SJohan Hovold 	port_priv->event_mode = false;
867bcbb9d81SJohan Hovold 
868bcbb9d81SJohan Hovold 	return result;
86903ee2515SGreg Kroah-Hartman }
87003ee2515SGreg Kroah-Hartman 
8714cc27bd6SCraig Shelley static void cp210x_close(struct usb_serial_port *port)
87203ee2515SGreg Kroah-Hartman {
873a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
874a7207e98SJohan Hovold 
875f26788daSJohan Hovold 	usb_serial_generic_close(port);
876ebfb319bSKonstantin Shkolnyy 
877ebfb319bSKonstantin Shkolnyy 	/* Clear both queues; cp2108 needs this to avoid an occasional hang */
87819165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
879ebfb319bSKonstantin Shkolnyy 
88019165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
881a7207e98SJohan Hovold 
882a7207e98SJohan Hovold 	/* Disabling the interface disables event-insertion mode. */
883a7207e98SJohan Hovold 	port_priv->event_mode = false;
884a7207e98SJohan Hovold }
885a7207e98SJohan Hovold 
886a7207e98SJohan Hovold static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
887a7207e98SJohan Hovold {
888a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_BREAK) {
889a7207e98SJohan Hovold 		port->icount.brk++;
890a7207e98SJohan Hovold 		*flag = TTY_BREAK;
891a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_PARITY) {
892a7207e98SJohan Hovold 		port->icount.parity++;
893a7207e98SJohan Hovold 		*flag = TTY_PARITY;
894a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_FRAME) {
895a7207e98SJohan Hovold 		port->icount.frame++;
896a7207e98SJohan Hovold 		*flag = TTY_FRAME;
897a7207e98SJohan Hovold 	}
898a7207e98SJohan Hovold 
899a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_OVERRUN) {
900a7207e98SJohan Hovold 		port->icount.overrun++;
901a7207e98SJohan Hovold 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
902a7207e98SJohan Hovold 	}
903a7207e98SJohan Hovold }
904a7207e98SJohan Hovold 
905a7207e98SJohan Hovold static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
906a7207e98SJohan Hovold {
907a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
908a7207e98SJohan Hovold 
909a7207e98SJohan Hovold 	switch (port_priv->event_state) {
910a7207e98SJohan Hovold 	case ES_DATA:
911a7207e98SJohan Hovold 		if (*ch == CP210X_ESCCHAR) {
912a7207e98SJohan Hovold 			port_priv->event_state = ES_ESCAPE;
913a7207e98SJohan Hovold 			break;
914a7207e98SJohan Hovold 		}
915a7207e98SJohan Hovold 		return false;
916a7207e98SJohan Hovold 	case ES_ESCAPE:
917a7207e98SJohan Hovold 		switch (*ch) {
918a7207e98SJohan Hovold 		case 0:
919a7207e98SJohan Hovold 			dev_dbg(&port->dev, "%s - escape char\n", __func__);
920a7207e98SJohan Hovold 			*ch = CP210X_ESCCHAR;
921a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
922a7207e98SJohan Hovold 			return false;
923a7207e98SJohan Hovold 		case 1:
924a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR_DATA_0;
925a7207e98SJohan Hovold 			break;
926a7207e98SJohan Hovold 		case 2:
927a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR;
928a7207e98SJohan Hovold 			break;
929a7207e98SJohan Hovold 		case 3:
930a7207e98SJohan Hovold 			port_priv->event_state = ES_MSR;
931a7207e98SJohan Hovold 			break;
932a7207e98SJohan Hovold 		default:
933a7207e98SJohan Hovold 			dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
934a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
935a7207e98SJohan Hovold 			break;
936a7207e98SJohan Hovold 		}
937a7207e98SJohan Hovold 		break;
938a7207e98SJohan Hovold 	case ES_LSR_DATA_0:
939a7207e98SJohan Hovold 		port_priv->lsr = *ch;
940a7207e98SJohan Hovold 		port_priv->event_state = ES_LSR_DATA_1;
941a7207e98SJohan Hovold 		break;
942a7207e98SJohan Hovold 	case ES_LSR_DATA_1:
943a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
944a7207e98SJohan Hovold 				__func__, port_priv->lsr, *ch);
945a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
946a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
947a7207e98SJohan Hovold 		return false;
948a7207e98SJohan Hovold 	case ES_LSR:
949a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
950a7207e98SJohan Hovold 		port_priv->lsr = *ch;
951a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
952a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
953a7207e98SJohan Hovold 		break;
954a7207e98SJohan Hovold 	case ES_MSR:
955a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
956a7207e98SJohan Hovold 		/* unimplemented */
957a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
958a7207e98SJohan Hovold 		break;
959a7207e98SJohan Hovold 	}
960a7207e98SJohan Hovold 
961a7207e98SJohan Hovold 	return true;
962a7207e98SJohan Hovold }
963a7207e98SJohan Hovold 
964a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb)
965a7207e98SJohan Hovold {
966a7207e98SJohan Hovold 	struct usb_serial_port *port = urb->context;
967a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
968a7207e98SJohan Hovold 	unsigned char *ch = urb->transfer_buffer;
969a7207e98SJohan Hovold 	char flag;
970a7207e98SJohan Hovold 	int i;
971a7207e98SJohan Hovold 
972a7207e98SJohan Hovold 	if (!urb->actual_length)
973a7207e98SJohan Hovold 		return;
974a7207e98SJohan Hovold 
975a7207e98SJohan Hovold 	if (port_priv->event_mode) {
976a7207e98SJohan Hovold 		for (i = 0; i < urb->actual_length; i++, ch++) {
977a7207e98SJohan Hovold 			flag = TTY_NORMAL;
978a7207e98SJohan Hovold 
979a7207e98SJohan Hovold 			if (cp210x_process_char(port, ch, &flag))
980a7207e98SJohan Hovold 				continue;
981a7207e98SJohan Hovold 
982a7207e98SJohan Hovold 			tty_insert_flip_char(&port->port, *ch, flag);
983a7207e98SJohan Hovold 		}
984a7207e98SJohan Hovold 	} else {
985a7207e98SJohan Hovold 		tty_insert_flip_string(&port->port, ch, urb->actual_length);
986a7207e98SJohan Hovold 	}
987a7207e98SJohan Hovold 	tty_flip_buffer_push(&port->port);
98803ee2515SGreg Kroah-Hartman }
98903ee2515SGreg Kroah-Hartman 
99003ee2515SGreg Kroah-Hartman /*
9916f923a01SKonstantin Shkolnyy  * Read how many bytes are waiting in the TX queue.
9926f923a01SKonstantin Shkolnyy  */
9936f923a01SKonstantin Shkolnyy static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
9946f923a01SKonstantin Shkolnyy 		u32 *count)
9956f923a01SKonstantin Shkolnyy {
9966f923a01SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
9976f923a01SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
9986f923a01SKonstantin Shkolnyy 	struct cp210x_comm_status *sts;
9996f923a01SKonstantin Shkolnyy 	int result;
10006f923a01SKonstantin Shkolnyy 
10016f923a01SKonstantin Shkolnyy 	sts = kmalloc(sizeof(*sts), GFP_KERNEL);
10026f923a01SKonstantin Shkolnyy 	if (!sts)
10036f923a01SKonstantin Shkolnyy 		return -ENOMEM;
10046f923a01SKonstantin Shkolnyy 
10056f923a01SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
10066f923a01SKonstantin Shkolnyy 			CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
10076f923a01SKonstantin Shkolnyy 			0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
10086f923a01SKonstantin Shkolnyy 			USB_CTRL_GET_TIMEOUT);
10096f923a01SKonstantin Shkolnyy 	if (result == sizeof(*sts)) {
10106f923a01SKonstantin Shkolnyy 		*count = le32_to_cpu(sts->ulAmountInOutQueue);
10116f923a01SKonstantin Shkolnyy 		result = 0;
10126f923a01SKonstantin Shkolnyy 	} else {
10136f923a01SKonstantin Shkolnyy 		dev_err(&port->dev, "failed to get comm status: %d\n", result);
10146f923a01SKonstantin Shkolnyy 		if (result >= 0)
10150407f1ceSJohan Hovold 			result = -EIO;
10166f923a01SKonstantin Shkolnyy 	}
10176f923a01SKonstantin Shkolnyy 
10186f923a01SKonstantin Shkolnyy 	kfree(sts);
10196f923a01SKonstantin Shkolnyy 
10206f923a01SKonstantin Shkolnyy 	return result;
10216f923a01SKonstantin Shkolnyy }
10226f923a01SKonstantin Shkolnyy 
10236f923a01SKonstantin Shkolnyy static bool cp210x_tx_empty(struct usb_serial_port *port)
10246f923a01SKonstantin Shkolnyy {
10256f923a01SKonstantin Shkolnyy 	int err;
10266f923a01SKonstantin Shkolnyy 	u32 count;
10276f923a01SKonstantin Shkolnyy 
10286f923a01SKonstantin Shkolnyy 	err = cp210x_get_tx_queue_byte_count(port, &count);
10296f923a01SKonstantin Shkolnyy 	if (err)
10306f923a01SKonstantin Shkolnyy 		return true;
10316f923a01SKonstantin Shkolnyy 
10326f923a01SKonstantin Shkolnyy 	return !count;
10336f923a01SKonstantin Shkolnyy }
10346f923a01SKonstantin Shkolnyy 
10356f923a01SKonstantin Shkolnyy /*
10364cc27bd6SCraig Shelley  * cp210x_get_termios
103703ee2515SGreg Kroah-Hartman  * Reads the baud rate, data bits, parity, stop bits and flow control mode
103803ee2515SGreg Kroah-Hartman  * from the device, corrects any unsupported values, and configures the
103903ee2515SGreg Kroah-Hartman  * termios structure to reflect the state of the device
104003ee2515SGreg Kroah-Hartman  */
10414cc27bd6SCraig Shelley static void cp210x_get_termios(struct tty_struct *tty,
1042d2ad67b3SVomLehn 	struct usb_serial_port *port)
104303ee2515SGreg Kroah-Hartman {
1044d2ad67b3SVomLehn 	unsigned int baud;
1045d2ad67b3SVomLehn 
1046d2ad67b3SVomLehn 	if (tty) {
10474cc27bd6SCraig Shelley 		cp210x_get_termios_port(tty->driver_data,
1048adc8d746SAlan Cox 			&tty->termios.c_cflag, &baud);
1049d2ad67b3SVomLehn 		tty_encode_baud_rate(tty, baud, baud);
10504f2ab888SJohan Hovold 	} else {
1051009615abSGeert Uytterhoeven 		tcflag_t cflag;
1052d2ad67b3SVomLehn 		cflag = 0;
10534cc27bd6SCraig Shelley 		cp210x_get_termios_port(port, &cflag, &baud);
1054d2ad67b3SVomLehn 	}
1055d2ad67b3SVomLehn }
1056d2ad67b3SVomLehn 
1057d2ad67b3SVomLehn /*
10584cc27bd6SCraig Shelley  * cp210x_get_termios_port
10594cc27bd6SCraig Shelley  * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
1060d2ad67b3SVomLehn  */
10614cc27bd6SCraig Shelley static void cp210x_get_termios_port(struct usb_serial_port *port,
1062009615abSGeert Uytterhoeven 	tcflag_t *cflagp, unsigned int *baudp)
1063d2ad67b3SVomLehn {
1064660f3b14SGreg Kroah-Hartman 	struct device *dev = &port->dev;
1065009615abSGeert Uytterhoeven 	tcflag_t cflag;
10669034389cSKonstantin Shkolnyy 	struct cp210x_flow_ctl flow_ctl;
1067fe1b07e9SKonstantin Shkolnyy 	u32 baud;
106819165b2bSKonstantin Shkolnyy 	u16 bits;
10699034389cSKonstantin Shkolnyy 	u32 ctl_hs;
1070c7614ff9SBrant Merryman 	u32 flow_repl;
107103ee2515SGreg Kroah-Hartman 
1072fe1b07e9SKonstantin Shkolnyy 	cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
107303ee2515SGreg Kroah-Hartman 
1074660f3b14SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
1075d2ad67b3SVomLehn 	*baudp = baud;
107603ee2515SGreg Kroah-Hartman 
1077d2ad67b3SVomLehn 	cflag = *cflagp;
107803ee2515SGreg Kroah-Hartman 
1079d0bf1ff0SKonstantin Shkolnyy 	cp210x_get_line_ctl(port, &bits);
108003ee2515SGreg Kroah-Hartman 	cflag &= ~CSIZE;
108103ee2515SGreg Kroah-Hartman 	switch (bits & BITS_DATA_MASK) {
108203ee2515SGreg Kroah-Hartman 	case BITS_DATA_5:
1083660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 5\n", __func__);
108403ee2515SGreg Kroah-Hartman 		cflag |= CS5;
108503ee2515SGreg Kroah-Hartman 		break;
108603ee2515SGreg Kroah-Hartman 	case BITS_DATA_6:
1087660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 6\n", __func__);
108803ee2515SGreg Kroah-Hartman 		cflag |= CS6;
108903ee2515SGreg Kroah-Hartman 		break;
109003ee2515SGreg Kroah-Hartman 	case BITS_DATA_7:
1091660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 7\n", __func__);
109203ee2515SGreg Kroah-Hartman 		cflag |= CS7;
109303ee2515SGreg Kroah-Hartman 		break;
109403ee2515SGreg Kroah-Hartman 	case BITS_DATA_8:
1095660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 8\n", __func__);
109603ee2515SGreg Kroah-Hartman 		cflag |= CS8;
109703ee2515SGreg Kroah-Hartman 		break;
109803ee2515SGreg Kroah-Hartman 	case BITS_DATA_9:
1099660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
110003ee2515SGreg Kroah-Hartman 		cflag |= CS8;
110103ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
110203ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_8;
110319165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
110403ee2515SGreg Kroah-Hartman 		break;
110503ee2515SGreg Kroah-Hartman 	default:
1106660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
110703ee2515SGreg Kroah-Hartman 		cflag |= CS8;
110803ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
110903ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_8;
111019165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
111103ee2515SGreg Kroah-Hartman 		break;
111203ee2515SGreg Kroah-Hartman 	}
111303ee2515SGreg Kroah-Hartman 
111403ee2515SGreg Kroah-Hartman 	switch (bits & BITS_PARITY_MASK) {
111503ee2515SGreg Kroah-Hartman 	case BITS_PARITY_NONE:
1116660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = NONE\n", __func__);
111703ee2515SGreg Kroah-Hartman 		cflag &= ~PARENB;
111803ee2515SGreg Kroah-Hartman 		break;
111903ee2515SGreg Kroah-Hartman 	case BITS_PARITY_ODD:
1120660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = ODD\n", __func__);
112103ee2515SGreg Kroah-Hartman 		cflag |= (PARENB|PARODD);
112203ee2515SGreg Kroah-Hartman 		break;
112303ee2515SGreg Kroah-Hartman 	case BITS_PARITY_EVEN:
1124660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = EVEN\n", __func__);
112503ee2515SGreg Kroah-Hartman 		cflag &= ~PARODD;
112603ee2515SGreg Kroah-Hartman 		cflag |= PARENB;
112703ee2515SGreg Kroah-Hartman 		break;
112803ee2515SGreg Kroah-Hartman 	case BITS_PARITY_MARK:
1129660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = MARK\n", __func__);
11304b6181caSRoland Koebler 		cflag |= (PARENB|PARODD|CMSPAR);
113103ee2515SGreg Kroah-Hartman 		break;
113203ee2515SGreg Kroah-Hartman 	case BITS_PARITY_SPACE:
1133660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = SPACE\n", __func__);
11344b6181caSRoland Koebler 		cflag &= ~PARODD;
11354b6181caSRoland Koebler 		cflag |= (PARENB|CMSPAR);
113603ee2515SGreg Kroah-Hartman 		break;
113703ee2515SGreg Kroah-Hartman 	default:
1138660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
113903ee2515SGreg Kroah-Hartman 		cflag &= ~PARENB;
114003ee2515SGreg Kroah-Hartman 		bits &= ~BITS_PARITY_MASK;
114119165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
114203ee2515SGreg Kroah-Hartman 		break;
114303ee2515SGreg Kroah-Hartman 	}
114403ee2515SGreg Kroah-Hartman 
114503ee2515SGreg Kroah-Hartman 	cflag &= ~CSTOPB;
114603ee2515SGreg Kroah-Hartman 	switch (bits & BITS_STOP_MASK) {
114703ee2515SGreg Kroah-Hartman 	case BITS_STOP_1:
1148660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
114903ee2515SGreg Kroah-Hartman 		break;
115003ee2515SGreg Kroah-Hartman 	case BITS_STOP_1_5:
1151660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
115203ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
115319165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
115403ee2515SGreg Kroah-Hartman 		break;
115503ee2515SGreg Kroah-Hartman 	case BITS_STOP_2:
1156660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
115703ee2515SGreg Kroah-Hartman 		cflag |= CSTOPB;
115803ee2515SGreg Kroah-Hartman 		break;
115903ee2515SGreg Kroah-Hartman 	default:
1160660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
116103ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
116219165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
116303ee2515SGreg Kroah-Hartman 		break;
116403ee2515SGreg Kroah-Hartman 	}
116503ee2515SGreg Kroah-Hartman 
11669034389cSKonstantin Shkolnyy 	cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
11679034389cSKonstantin Shkolnyy 			sizeof(flow_ctl));
11689034389cSKonstantin Shkolnyy 	ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
11699034389cSKonstantin Shkolnyy 	if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
1170660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1171c7614ff9SBrant Merryman 		/*
1172c7614ff9SBrant Merryman 		 * When the port is closed, the CP210x hardware disables
1173c7614ff9SBrant Merryman 		 * auto-RTS and RTS is deasserted but it leaves auto-CTS when
1174c7614ff9SBrant Merryman 		 * in hardware flow control mode. When re-opening the port, if
1175c7614ff9SBrant Merryman 		 * auto-CTS is enabled on the cp210x, then auto-RTS must be
1176c7614ff9SBrant Merryman 		 * re-enabled in the driver.
1177c7614ff9SBrant Merryman 		 */
1178c7614ff9SBrant Merryman 		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1179c7614ff9SBrant Merryman 		flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1180c7614ff9SBrant Merryman 		flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
1181c7614ff9SBrant Merryman 		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1182c7614ff9SBrant Merryman 		cp210x_write_reg_block(port,
1183c7614ff9SBrant Merryman 				CP210X_SET_FLOW,
1184c7614ff9SBrant Merryman 				&flow_ctl,
1185c7614ff9SBrant Merryman 				sizeof(flow_ctl));
1186c7614ff9SBrant Merryman 
118703ee2515SGreg Kroah-Hartman 		cflag |= CRTSCTS;
118803ee2515SGreg Kroah-Hartman 	} else {
1189660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - flow control = NONE\n", __func__);
119003ee2515SGreg Kroah-Hartman 		cflag &= ~CRTSCTS;
119103ee2515SGreg Kroah-Hartman 	}
119203ee2515SGreg Kroah-Hartman 
1193d2ad67b3SVomLehn 	*cflagp = cflag;
119403ee2515SGreg Kroah-Hartman }
119503ee2515SGreg Kroah-Hartman 
119695fd4f47SJohan Hovold struct cp210x_rate {
119795fd4f47SJohan Hovold 	speed_t rate;
119895fd4f47SJohan Hovold 	speed_t high;
119995fd4f47SJohan Hovold };
120095fd4f47SJohan Hovold 
120195fd4f47SJohan Hovold static const struct cp210x_rate cp210x_an205_table1[] = {
120295fd4f47SJohan Hovold 	{ 300, 300 },
120395fd4f47SJohan Hovold 	{ 600, 600 },
120495fd4f47SJohan Hovold 	{ 1200, 1200 },
120595fd4f47SJohan Hovold 	{ 1800, 1800 },
120695fd4f47SJohan Hovold 	{ 2400, 2400 },
120795fd4f47SJohan Hovold 	{ 4000, 4000 },
120895fd4f47SJohan Hovold 	{ 4800, 4803 },
120995fd4f47SJohan Hovold 	{ 7200, 7207 },
121095fd4f47SJohan Hovold 	{ 9600, 9612 },
121195fd4f47SJohan Hovold 	{ 14400, 14428 },
121295fd4f47SJohan Hovold 	{ 16000, 16062 },
121395fd4f47SJohan Hovold 	{ 19200, 19250 },
121495fd4f47SJohan Hovold 	{ 28800, 28912 },
121595fd4f47SJohan Hovold 	{ 38400, 38601 },
121695fd4f47SJohan Hovold 	{ 51200, 51558 },
121795fd4f47SJohan Hovold 	{ 56000, 56280 },
121895fd4f47SJohan Hovold 	{ 57600, 58053 },
121995fd4f47SJohan Hovold 	{ 64000, 64111 },
122095fd4f47SJohan Hovold 	{ 76800, 77608 },
122195fd4f47SJohan Hovold 	{ 115200, 117028 },
122295fd4f47SJohan Hovold 	{ 128000, 129347 },
122395fd4f47SJohan Hovold 	{ 153600, 156868 },
122495fd4f47SJohan Hovold 	{ 230400, 237832 },
122595fd4f47SJohan Hovold 	{ 250000, 254234 },
122695fd4f47SJohan Hovold 	{ 256000, 273066 },
122795fd4f47SJohan Hovold 	{ 460800, 491520 },
122895fd4f47SJohan Hovold 	{ 500000, 567138 },
122995fd4f47SJohan Hovold 	{ 576000, 670254 },
123095fd4f47SJohan Hovold 	{ 921600, UINT_MAX }
123195fd4f47SJohan Hovold };
123295fd4f47SJohan Hovold 
123395fd4f47SJohan Hovold /*
123495fd4f47SJohan Hovold  * Quantises the baud rate as per AN205 Table 1
123595fd4f47SJohan Hovold  */
123695fd4f47SJohan Hovold static speed_t cp210x_get_an205_rate(speed_t baud)
123795fd4f47SJohan Hovold {
123895fd4f47SJohan Hovold 	int i;
123995fd4f47SJohan Hovold 
124095fd4f47SJohan Hovold 	for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
124195fd4f47SJohan Hovold 		if (baud <= cp210x_an205_table1[i].high)
124295fd4f47SJohan Hovold 			break;
124395fd4f47SJohan Hovold 	}
124495fd4f47SJohan Hovold 
124595fd4f47SJohan Hovold 	return cp210x_an205_table1[i].rate;
124695fd4f47SJohan Hovold }
124795fd4f47SJohan Hovold 
124885bc2d91SJohanna Abrahamsson static speed_t cp210x_get_actual_rate(speed_t baud)
12497aecd7fcSJohan Hovold {
12507aecd7fcSJohan Hovold 	unsigned int prescale = 1;
12517aecd7fcSJohan Hovold 	unsigned int div;
12527aecd7fcSJohan Hovold 
12537aecd7fcSJohan Hovold 	if (baud <= 365)
12547aecd7fcSJohan Hovold 		prescale = 4;
12557aecd7fcSJohan Hovold 
12567aecd7fcSJohan Hovold 	div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
12577aecd7fcSJohan Hovold 	baud = 48000000 / (2 * prescale * div);
12587aecd7fcSJohan Hovold 
12597aecd7fcSJohan Hovold 	return baud;
12607aecd7fcSJohan Hovold }
12617aecd7fcSJohan Hovold 
1262e5990874SJohan Hovold /*
1263e5990874SJohan Hovold  * CP2101 supports the following baud rates:
1264e5990874SJohan Hovold  *
1265e5990874SJohan Hovold  *	300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1266e5990874SJohan Hovold  *	38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1267e5990874SJohan Hovold  *
1268e5990874SJohan Hovold  * CP2102 and CP2103 support the following additional rates:
1269e5990874SJohan Hovold  *
1270e5990874SJohan Hovold  *	4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1271e5990874SJohan Hovold  *	576000
1272e5990874SJohan Hovold  *
1273e5990874SJohan Hovold  * The device will map a requested rate to a supported one, but the result
1274e5990874SJohan Hovold  * of requests for rates greater than 1053257 is undefined (see AN205).
1275e5990874SJohan Hovold  *
1276e5990874SJohan Hovold  * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1277e5990874SJohan Hovold  * respectively, with an error less than 1%. The actual rates are determined
1278e5990874SJohan Hovold  * by
1279e5990874SJohan Hovold  *
1280e5990874SJohan Hovold  *	div = round(freq / (2 x prescale x request))
1281e5990874SJohan Hovold  *	actual = freq / (2 x prescale x div)
1282e5990874SJohan Hovold  *
1283e5990874SJohan Hovold  * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1284e5990874SJohan Hovold  * or 1 otherwise.
1285e5990874SJohan Hovold  * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1286e5990874SJohan Hovold  * otherwise.
1287e5990874SJohan Hovold  */
1288e5990874SJohan Hovold static void cp210x_change_speed(struct tty_struct *tty,
1289e5990874SJohan Hovold 		struct usb_serial_port *port, struct ktermios *old_termios)
1290e5990874SJohan Hovold {
12916f0bcf72SKaroly Pados 	struct usb_serial *serial = port->serial;
12926f0bcf72SKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1293e5990874SJohan Hovold 	u32 baud;
1294e5990874SJohan Hovold 
12956f0bcf72SKaroly Pados 	/*
12967aecd7fcSJohan Hovold 	 * This maps the requested rate to the actual rate, a valid rate on
12977aecd7fcSJohan Hovold 	 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
1298e5990874SJohan Hovold 	 *
1299e5990874SJohan Hovold 	 * NOTE: B0 is not implemented.
1300e5990874SJohan Hovold 	 */
130185bc2d91SJohanna Abrahamsson 	baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
130285bc2d91SJohanna Abrahamsson 
13037aecd7fcSJohan Hovold 	if (priv->use_actual_rate)
130485bc2d91SJohanna Abrahamsson 		baud = cp210x_get_actual_rate(baud);
13057aecd7fcSJohan Hovold 	else if (baud < 1000000)
130695fd4f47SJohan Hovold 		baud = cp210x_get_an205_rate(baud);
1307e5990874SJohan Hovold 
1308660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
1309fe1b07e9SKonstantin Shkolnyy 	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
1310e5990874SJohan Hovold 		dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1311e5990874SJohan Hovold 		if (old_termios)
1312e5990874SJohan Hovold 			baud = old_termios->c_ospeed;
1313e5990874SJohan Hovold 		else
1314e5990874SJohan Hovold 			baud = 9600;
1315e5990874SJohan Hovold 	}
1316e5990874SJohan Hovold 
1317e5990874SJohan Hovold 	tty_encode_baud_rate(tty, baud, baud);
1318e5990874SJohan Hovold }
1319e5990874SJohan Hovold 
1320a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port)
1321a7207e98SJohan Hovold {
1322a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1323a7207e98SJohan Hovold 	int ret;
1324a7207e98SJohan Hovold 
1325a7207e98SJohan Hovold 	if (port_priv->event_mode)
1326a7207e98SJohan Hovold 		return;
1327a7207e98SJohan Hovold 
1328a7207e98SJohan Hovold 	port_priv->event_state = ES_DATA;
1329a7207e98SJohan Hovold 	port_priv->event_mode = true;
1330a7207e98SJohan Hovold 
1331a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1332a7207e98SJohan Hovold 	if (ret) {
1333a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to enable events: %d\n", ret);
1334a7207e98SJohan Hovold 		port_priv->event_mode = false;
1335a7207e98SJohan Hovold 	}
1336a7207e98SJohan Hovold }
1337a7207e98SJohan Hovold 
1338a7207e98SJohan Hovold static void cp210x_disable_event_mode(struct usb_serial_port *port)
1339a7207e98SJohan Hovold {
1340a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1341a7207e98SJohan Hovold 	int ret;
1342a7207e98SJohan Hovold 
1343a7207e98SJohan Hovold 	if (!port_priv->event_mode)
1344a7207e98SJohan Hovold 		return;
1345a7207e98SJohan Hovold 
1346a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
1347a7207e98SJohan Hovold 	if (ret) {
1348a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to disable events: %d\n", ret);
1349a7207e98SJohan Hovold 		return;
1350a7207e98SJohan Hovold 	}
1351a7207e98SJohan Hovold 
1352a7207e98SJohan Hovold 	port_priv->event_mode = false;
1353a7207e98SJohan Hovold }
1354a7207e98SJohan Hovold 
1355*95168d62SJohan Hovold static bool cp210x_termios_change(const struct ktermios *a, const struct ktermios *b)
1356*95168d62SJohan Hovold {
1357*95168d62SJohan Hovold 	bool iflag_change;
1358*95168d62SJohan Hovold 
1359*95168d62SJohan Hovold 	iflag_change = ((a->c_iflag ^ b->c_iflag) & INPCK);
1360*95168d62SJohan Hovold 
1361*95168d62SJohan Hovold 	return tty_termios_hw_change(a, b) || iflag_change;
1362*95168d62SJohan Hovold }
1363*95168d62SJohan Hovold 
13644cc27bd6SCraig Shelley static void cp210x_set_termios(struct tty_struct *tty,
136503ee2515SGreg Kroah-Hartman 		struct usb_serial_port *port, struct ktermios *old_termios)
136603ee2515SGreg Kroah-Hartman {
1367660f3b14SGreg Kroah-Hartman 	struct device *dev = &port->dev;
136803ee2515SGreg Kroah-Hartman 	unsigned int cflag, old_cflag;
136919165b2bSKonstantin Shkolnyy 	u16 bits;
137003ee2515SGreg Kroah-Hartman 
1371*95168d62SJohan Hovold 	if (!cp210x_termios_change(&tty->termios, old_termios))
1372*95168d62SJohan Hovold 		return;
1373*95168d62SJohan Hovold 
1374adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
137503ee2515SGreg Kroah-Hartman 	old_cflag = old_termios->c_cflag;
137603ee2515SGreg Kroah-Hartman 
1377adc8d746SAlan Cox 	if (tty->termios.c_ospeed != old_termios->c_ospeed)
1378e5990874SJohan Hovold 		cp210x_change_speed(tty, port, old_termios);
137903ee2515SGreg Kroah-Hartman 
138003ee2515SGreg Kroah-Hartman 	/* If the number of data bits is to be updated */
138103ee2515SGreg Kroah-Hartman 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
1382d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
138303ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
138403ee2515SGreg Kroah-Hartman 		switch (cflag & CSIZE) {
138503ee2515SGreg Kroah-Hartman 		case CS5:
138603ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_5;
1387660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 5\n", __func__);
138803ee2515SGreg Kroah-Hartman 			break;
138903ee2515SGreg Kroah-Hartman 		case CS6:
139003ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_6;
1391660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 6\n", __func__);
139203ee2515SGreg Kroah-Hartman 			break;
139303ee2515SGreg Kroah-Hartman 		case CS7:
139403ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_7;
1395660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 7\n", __func__);
139603ee2515SGreg Kroah-Hartman 			break;
139703ee2515SGreg Kroah-Hartman 		case CS8:
1398c2b33559SJohan Hovold 		default:
139903ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_8;
1400660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 8\n", __func__);
140103ee2515SGreg Kroah-Hartman 			break;
140203ee2515SGreg Kroah-Hartman 		}
140319165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1404660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Number of data bits requested not supported by device\n");
140503ee2515SGreg Kroah-Hartman 	}
140603ee2515SGreg Kroah-Hartman 
14074b6181caSRoland Koebler 	if ((cflag     & (PARENB|PARODD|CMSPAR)) !=
14084b6181caSRoland Koebler 	    (old_cflag & (PARENB|PARODD|CMSPAR))) {
1409d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
141003ee2515SGreg Kroah-Hartman 		bits &= ~BITS_PARITY_MASK;
141103ee2515SGreg Kroah-Hartman 		if (cflag & PARENB) {
14124b6181caSRoland Koebler 			if (cflag & CMSPAR) {
14134b6181caSRoland Koebler 				if (cflag & PARODD) {
14144b6181caSRoland Koebler 					bits |= BITS_PARITY_MARK;
1415660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = MARK\n", __func__);
14164b6181caSRoland Koebler 				} else {
14174b6181caSRoland Koebler 					bits |= BITS_PARITY_SPACE;
1418660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = SPACE\n", __func__);
14194b6181caSRoland Koebler 				}
14204b6181caSRoland Koebler 			} else {
142103ee2515SGreg Kroah-Hartman 				if (cflag & PARODD) {
142203ee2515SGreg Kroah-Hartman 					bits |= BITS_PARITY_ODD;
1423660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = ODD\n", __func__);
142403ee2515SGreg Kroah-Hartman 				} else {
142503ee2515SGreg Kroah-Hartman 					bits |= BITS_PARITY_EVEN;
1426660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = EVEN\n", __func__);
142703ee2515SGreg Kroah-Hartman 				}
142803ee2515SGreg Kroah-Hartman 			}
14294b6181caSRoland Koebler 		}
143019165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1431660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Parity mode not supported by device\n");
143203ee2515SGreg Kroah-Hartman 	}
143303ee2515SGreg Kroah-Hartman 
143403ee2515SGreg Kroah-Hartman 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1435d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
143603ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
143703ee2515SGreg Kroah-Hartman 		if (cflag & CSTOPB) {
143803ee2515SGreg Kroah-Hartman 			bits |= BITS_STOP_2;
1439660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - stop bits = 2\n", __func__);
144003ee2515SGreg Kroah-Hartman 		} else {
144103ee2515SGreg Kroah-Hartman 			bits |= BITS_STOP_1;
1442660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - stop bits = 1\n", __func__);
144303ee2515SGreg Kroah-Hartman 		}
144419165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1445660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Number of stop bits requested not supported by device\n");
144603ee2515SGreg Kroah-Hartman 	}
144703ee2515SGreg Kroah-Hartman 
144803ee2515SGreg Kroah-Hartman 	if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
14499034389cSKonstantin Shkolnyy 		struct cp210x_flow_ctl flow_ctl;
14509034389cSKonstantin Shkolnyy 		u32 ctl_hs;
14519034389cSKonstantin Shkolnyy 		u32 flow_repl;
14527084fa86SKonstantin Shkolnyy 
14539034389cSKonstantin Shkolnyy 		cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
14549034389cSKonstantin Shkolnyy 				sizeof(flow_ctl));
14559034389cSKonstantin Shkolnyy 		ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
14569034389cSKonstantin Shkolnyy 		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
14579034389cSKonstantin Shkolnyy 		dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14589034389cSKonstantin Shkolnyy 				__func__, ctl_hs, flow_repl);
14597084fa86SKonstantin Shkolnyy 
1460ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1461ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1462ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1463ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1464ab5701adSKonstantin Shkolnyy 		ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
146503ee2515SGreg Kroah-Hartman 		if (cflag & CRTSCTS) {
14669034389cSKonstantin Shkolnyy 			ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1467ab5701adSKonstantin Shkolnyy 
1468ab5701adSKonstantin Shkolnyy 			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14699034389cSKonstantin Shkolnyy 			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14709034389cSKonstantin Shkolnyy 					CP210X_SERIAL_RTS_FLOW_CTL);
1471660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
147203ee2515SGreg Kroah-Hartman 		} else {
1473ab5701adSKonstantin Shkolnyy 			ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1474ab5701adSKonstantin Shkolnyy 
1475ab5701adSKonstantin Shkolnyy 			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14769034389cSKonstantin Shkolnyy 			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14779034389cSKonstantin Shkolnyy 					CP210X_SERIAL_RTS_ACTIVE);
1478660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - flow control = NONE\n", __func__);
147903ee2515SGreg Kroah-Hartman 		}
148003ee2515SGreg Kroah-Hartman 
14819034389cSKonstantin Shkolnyy 		dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14829034389cSKonstantin Shkolnyy 				__func__, ctl_hs, flow_repl);
14839034389cSKonstantin Shkolnyy 		flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
14849034389cSKonstantin Shkolnyy 		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
14859034389cSKonstantin Shkolnyy 		cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
14869034389cSKonstantin Shkolnyy 				sizeof(flow_ctl));
148703ee2515SGreg Kroah-Hartman 	}
148803ee2515SGreg Kroah-Hartman 
1489a7207e98SJohan Hovold 	/*
1490a7207e98SJohan Hovold 	 * Enable event-insertion mode only if input parity checking is
1491a7207e98SJohan Hovold 	 * enabled for now.
1492a7207e98SJohan Hovold 	 */
1493a7207e98SJohan Hovold 	if (I_INPCK(tty))
1494a7207e98SJohan Hovold 		cp210x_enable_event_mode(port);
1495a7207e98SJohan Hovold 	else
1496a7207e98SJohan Hovold 		cp210x_disable_event_mode(port);
149703ee2515SGreg Kroah-Hartman }
149803ee2515SGreg Kroah-Hartman 
149920b9d177SAlan Cox static int cp210x_tiocmset(struct tty_struct *tty,
150003ee2515SGreg Kroah-Hartman 		unsigned int set, unsigned int clear)
150103ee2515SGreg Kroah-Hartman {
150203ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
150320b9d177SAlan Cox 	return cp210x_tiocmset_port(port, set, clear);
1504d2ad67b3SVomLehn }
1505d2ad67b3SVomLehn 
150620b9d177SAlan Cox static int cp210x_tiocmset_port(struct usb_serial_port *port,
1507d2ad67b3SVomLehn 		unsigned int set, unsigned int clear)
1508d2ad67b3SVomLehn {
150919165b2bSKonstantin Shkolnyy 	u16 control = 0;
151003ee2515SGreg Kroah-Hartman 
151103ee2515SGreg Kroah-Hartman 	if (set & TIOCM_RTS) {
151203ee2515SGreg Kroah-Hartman 		control |= CONTROL_RTS;
151303ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
151403ee2515SGreg Kroah-Hartman 	}
151503ee2515SGreg Kroah-Hartman 	if (set & TIOCM_DTR) {
151603ee2515SGreg Kroah-Hartman 		control |= CONTROL_DTR;
151703ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
151803ee2515SGreg Kroah-Hartman 	}
151903ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_RTS) {
152003ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_RTS;
152103ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
152203ee2515SGreg Kroah-Hartman 	}
152303ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_DTR) {
152403ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_DTR;
152503ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
152603ee2515SGreg Kroah-Hartman 	}
152703ee2515SGreg Kroah-Hartman 
1528660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
152903ee2515SGreg Kroah-Hartman 
153019165b2bSKonstantin Shkolnyy 	return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
153103ee2515SGreg Kroah-Hartman }
153203ee2515SGreg Kroah-Hartman 
1533d94c7bd4SAlan Cox static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1534d94c7bd4SAlan Cox {
1535d94c7bd4SAlan Cox 	if (on)
153620b9d177SAlan Cox 		cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
1537d94c7bd4SAlan Cox 	else
153820b9d177SAlan Cox 		cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
1539d94c7bd4SAlan Cox }
1540d94c7bd4SAlan Cox 
154160b33c13SAlan Cox static int cp210x_tiocmget(struct tty_struct *tty)
154203ee2515SGreg Kroah-Hartman {
154303ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
1544fe1b07e9SKonstantin Shkolnyy 	u8 control;
154503ee2515SGreg Kroah-Hartman 	int result;
154603ee2515SGreg Kroah-Hartman 
1547de24e0a1SJohan Hovold 	result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1548de24e0a1SJohan Hovold 	if (result)
1549de24e0a1SJohan Hovold 		return result;
155003ee2515SGreg Kroah-Hartman 
155103ee2515SGreg Kroah-Hartman 	result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
155203ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RTS) ? TIOCM_RTS : 0)
155303ee2515SGreg Kroah-Hartman 		|((control & CONTROL_CTS) ? TIOCM_CTS : 0)
155403ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DSR) ? TIOCM_DSR : 0)
155503ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RING)? TIOCM_RI  : 0)
155603ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DCD) ? TIOCM_CD  : 0);
155703ee2515SGreg Kroah-Hartman 
1558660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
155903ee2515SGreg Kroah-Hartman 
156003ee2515SGreg Kroah-Hartman 	return result;
156103ee2515SGreg Kroah-Hartman }
156203ee2515SGreg Kroah-Hartman 
15634cc27bd6SCraig Shelley static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
156403ee2515SGreg Kroah-Hartman {
156503ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
156619165b2bSKonstantin Shkolnyy 	u16 state;
156703ee2515SGreg Kroah-Hartman 
156803ee2515SGreg Kroah-Hartman 	if (break_state == 0)
156903ee2515SGreg Kroah-Hartman 		state = BREAK_OFF;
157003ee2515SGreg Kroah-Hartman 	else
157103ee2515SGreg Kroah-Hartman 		state = BREAK_ON;
1572660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
157303ee2515SGreg Kroah-Hartman 		state == BREAK_OFF ? "off" : "on");
157419165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
157503ee2515SGreg Kroah-Hartman }
157603ee2515SGreg Kroah-Hartman 
1577cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
1578cf5276ceSMartyn Welch static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
1579cf5276ceSMartyn Welch {
1580cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1581cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1582cf5276ceSMartyn Welch 
1583c8acfe0aSKaroly Pados 	if (priv->gpio_altfunc & BIT(offset))
1584cf5276ceSMartyn Welch 		return -ENODEV;
1585cf5276ceSMartyn Welch 
1586cf5276ceSMartyn Welch 	return 0;
1587cf5276ceSMartyn Welch }
1588cf5276ceSMartyn Welch 
1589cf5276ceSMartyn Welch static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1590cf5276ceSMartyn Welch {
1591cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1592c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1593c8acfe0aSKaroly Pados 	u8 req_type = REQTYPE_DEVICE_TO_HOST;
1594cf5276ceSMartyn Welch 	int result;
1595cf5276ceSMartyn Welch 	u8 buf;
1596cf5276ceSMartyn Welch 
1597c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105)
1598c8acfe0aSKaroly Pados 		req_type = REQTYPE_INTERFACE_TO_HOST;
1599c8acfe0aSKaroly Pados 
16007b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
16017b0b644bSKaroly Pados 	if (result)
16027b0b644bSKaroly Pados 		return result;
16037b0b644bSKaroly Pados 
1604c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial, req_type,
1605cf5276ceSMartyn Welch 					  CP210X_READ_LATCH, &buf, sizeof(buf));
16067b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
1607cf5276ceSMartyn Welch 	if (result < 0)
1608cf5276ceSMartyn Welch 		return result;
1609cf5276ceSMartyn Welch 
1610cf5276ceSMartyn Welch 	return !!(buf & BIT(gpio));
1611cf5276ceSMartyn Welch }
1612cf5276ceSMartyn Welch 
1613cf5276ceSMartyn Welch static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1614cf5276ceSMartyn Welch {
1615cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1616c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1617cf5276ceSMartyn Welch 	struct cp210x_gpio_write buf;
1618c8acfe0aSKaroly Pados 	int result;
1619cf5276ceSMartyn Welch 
1620cf5276ceSMartyn Welch 	if (value == 1)
1621cf5276ceSMartyn Welch 		buf.state = BIT(gpio);
1622cf5276ceSMartyn Welch 	else
1623cf5276ceSMartyn Welch 		buf.state = 0;
1624cf5276ceSMartyn Welch 
1625cf5276ceSMartyn Welch 	buf.mask = BIT(gpio);
1626cf5276ceSMartyn Welch 
16277b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
16287b0b644bSKaroly Pados 	if (result)
16297b0b644bSKaroly Pados 		goto out;
16307b0b644bSKaroly Pados 
1631c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1632c8acfe0aSKaroly Pados 		result = cp210x_write_vendor_block(serial,
1633c8acfe0aSKaroly Pados 						   REQTYPE_HOST_TO_INTERFACE,
1634c8acfe0aSKaroly Pados 						   CP210X_WRITE_LATCH, &buf,
1635c8acfe0aSKaroly Pados 						   sizeof(buf));
1636c8acfe0aSKaroly Pados 	} else {
1637c8acfe0aSKaroly Pados 		u16 wIndex = buf.state << 8 | buf.mask;
1638c8acfe0aSKaroly Pados 
1639c8acfe0aSKaroly Pados 		result = usb_control_msg(serial->dev,
1640c8acfe0aSKaroly Pados 					 usb_sndctrlpipe(serial->dev, 0),
1641c8acfe0aSKaroly Pados 					 CP210X_VENDOR_SPECIFIC,
1642c8acfe0aSKaroly Pados 					 REQTYPE_HOST_TO_DEVICE,
1643c8acfe0aSKaroly Pados 					 CP210X_WRITE_LATCH,
1644c8acfe0aSKaroly Pados 					 wIndex,
1645c8acfe0aSKaroly Pados 					 NULL, 0, USB_CTRL_SET_TIMEOUT);
1646c8acfe0aSKaroly Pados 	}
1647c8acfe0aSKaroly Pados 
16487b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
16497b0b644bSKaroly Pados out:
1650c8acfe0aSKaroly Pados 	if (result < 0) {
1651c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
1652c8acfe0aSKaroly Pados 				result);
1653c8acfe0aSKaroly Pados 	}
1654cf5276ceSMartyn Welch }
1655cf5276ceSMartyn Welch 
1656cf5276ceSMartyn Welch static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1657cf5276ceSMartyn Welch {
1658c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1659c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1660c8acfe0aSKaroly Pados 
1661c8acfe0aSKaroly Pados 	return priv->gpio_input & BIT(gpio);
1662cf5276ceSMartyn Welch }
1663cf5276ceSMartyn Welch 
1664cf5276ceSMartyn Welch static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1665cf5276ceSMartyn Welch {
1666c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1667c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1668c8acfe0aSKaroly Pados 
1669c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1670c8acfe0aSKaroly Pados 		/* hardware does not support an input mode */
1671cf5276ceSMartyn Welch 		return -ENOTSUPP;
1672cf5276ceSMartyn Welch 	}
1673cf5276ceSMartyn Welch 
1674c8acfe0aSKaroly Pados 	/* push-pull pins cannot be changed to be inputs */
1675c8acfe0aSKaroly Pados 	if (priv->gpio_pushpull & BIT(gpio))
1676c8acfe0aSKaroly Pados 		return -EINVAL;
1677c8acfe0aSKaroly Pados 
1678c8acfe0aSKaroly Pados 	/* make sure to release pin if it is being driven low */
1679c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, 1);
1680c8acfe0aSKaroly Pados 
1681c8acfe0aSKaroly Pados 	priv->gpio_input |= BIT(gpio);
1682c8acfe0aSKaroly Pados 
1683c8acfe0aSKaroly Pados 	return 0;
1684c8acfe0aSKaroly Pados }
1685c8acfe0aSKaroly Pados 
1686cf5276ceSMartyn Welch static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1687cf5276ceSMartyn Welch 					int value)
1688cf5276ceSMartyn Welch {
1689c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1690c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1691c8acfe0aSKaroly Pados 
1692c8acfe0aSKaroly Pados 	priv->gpio_input &= ~BIT(gpio);
1693c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, value);
1694c8acfe0aSKaroly Pados 
1695cf5276ceSMartyn Welch 	return 0;
1696cf5276ceSMartyn Welch }
1697cf5276ceSMartyn Welch 
16982956b5d9SMika Westerberg static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
16992956b5d9SMika Westerberg 				  unsigned long config)
1700cf5276ceSMartyn Welch {
1701cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1702cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
17032956b5d9SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(config);
1704cf5276ceSMartyn Welch 
1705cf5276ceSMartyn Welch 	/* Succeed only if in correct mode (this can't be set at runtime) */
17062956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
1707c8acfe0aSKaroly Pados 	    (priv->gpio_pushpull & BIT(gpio)))
1708cf5276ceSMartyn Welch 		return 0;
1709cf5276ceSMartyn Welch 
17102956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
1711c8acfe0aSKaroly Pados 	    !(priv->gpio_pushpull & BIT(gpio)))
1712cf5276ceSMartyn Welch 		return 0;
1713cf5276ceSMartyn Welch 
1714cf5276ceSMartyn Welch 	return -ENOTSUPP;
1715cf5276ceSMartyn Welch }
1716cf5276ceSMartyn Welch 
1717cf5276ceSMartyn Welch /*
1718cf5276ceSMartyn Welch  * This function is for configuring GPIO using shared pins, where other signals
1719cf5276ceSMartyn Welch  * are made unavailable by configuring the use of GPIO. This is believed to be
1720cf5276ceSMartyn Welch  * only applicable to the cp2105 at this point, the other devices supported by
1721cf5276ceSMartyn Welch  * this driver that provide GPIO do so in a way that does not impact other
1722cf5276ceSMartyn Welch  * signals and are thus expected to have very different initialisation.
1723cf5276ceSMartyn Welch  */
1724c8acfe0aSKaroly Pados static int cp2105_gpioconf_init(struct usb_serial *serial)
1725cf5276ceSMartyn Welch {
1726cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1727cf5276ceSMartyn Welch 	struct cp210x_pin_mode mode;
172815fb84b7SIcenowy Zheng 	struct cp210x_dual_port_config config;
1729cf5276ceSMartyn Welch 	u8 intf_num = cp210x_interface_num(serial);
1730c8acfe0aSKaroly Pados 	u8 iface_config;
1731cf5276ceSMartyn Welch 	int result;
1732cf5276ceSMartyn Welch 
1733cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1734cf5276ceSMartyn Welch 					  CP210X_GET_DEVICEMODE, &mode,
1735cf5276ceSMartyn Welch 					  sizeof(mode));
1736cf5276ceSMartyn Welch 	if (result < 0)
1737cf5276ceSMartyn Welch 		return result;
1738cf5276ceSMartyn Welch 
1739cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1740cf5276ceSMartyn Welch 					  CP210X_GET_PORTCONFIG, &config,
1741cf5276ceSMartyn Welch 					  sizeof(config));
1742cf5276ceSMartyn Welch 	if (result < 0)
1743cf5276ceSMartyn Welch 		return result;
1744cf5276ceSMartyn Welch 
1745cf5276ceSMartyn Welch 	/*  2 banks of GPIO - One for the pins taken from each serial port */
1746cf5276ceSMartyn Welch 	if (intf_num == 0) {
1747c8acfe0aSKaroly Pados 		if (mode.eci == CP210X_PIN_MODE_MODEM) {
1748c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1749c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1750cf5276ceSMartyn Welch 			return 0;
1751c8acfe0aSKaroly Pados 		}
1752cf5276ceSMartyn Welch 
1753c8acfe0aSKaroly Pados 		iface_config = config.eci_cfg;
1754c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1755cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_MASK) >>
1756cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_OFFSET);
1757cf5276ceSMartyn Welch 		priv->gc.ngpio = 2;
1758cf5276ceSMartyn Welch 	} else if (intf_num == 1) {
1759c8acfe0aSKaroly Pados 		if (mode.sci == CP210X_PIN_MODE_MODEM) {
1760c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1761c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1762cf5276ceSMartyn Welch 			return 0;
1763c8acfe0aSKaroly Pados 		}
1764cf5276ceSMartyn Welch 
1765c8acfe0aSKaroly Pados 		iface_config = config.sci_cfg;
1766c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1767cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_MASK) >>
1768cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_OFFSET);
1769cf5276ceSMartyn Welch 		priv->gc.ngpio = 3;
1770cf5276ceSMartyn Welch 	} else {
1771cf5276ceSMartyn Welch 		return -ENODEV;
1772cf5276ceSMartyn Welch 	}
1773cf5276ceSMartyn Welch 
1774c8acfe0aSKaroly Pados 	/* mark all pins which are not in GPIO mode */
1775c8acfe0aSKaroly Pados 	if (iface_config & CP2105_GPIO0_TXLED_MODE)	/* GPIO 0 */
1776c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(0);
1777c8acfe0aSKaroly Pados 	if (iface_config & (CP2105_GPIO1_RXLED_MODE |	/* GPIO 1 */
1778c8acfe0aSKaroly Pados 			CP2105_GPIO1_RS485_MODE))
1779c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(1);
1780c8acfe0aSKaroly Pados 
1781c8acfe0aSKaroly Pados 	/* driver implementation for CP2105 only supports outputs */
1782c8acfe0aSKaroly Pados 	priv->gpio_input = 0;
1783c8acfe0aSKaroly Pados 
1784c8acfe0aSKaroly Pados 	return 0;
1785c8acfe0aSKaroly Pados }
1786c8acfe0aSKaroly Pados 
178715fb84b7SIcenowy Zheng static int cp2104_gpioconf_init(struct usb_serial *serial)
178815fb84b7SIcenowy Zheng {
178915fb84b7SIcenowy Zheng 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
179015fb84b7SIcenowy Zheng 	struct cp210x_single_port_config config;
179115fb84b7SIcenowy Zheng 	u8 iface_config;
179215fb84b7SIcenowy Zheng 	u8 gpio_latch;
179315fb84b7SIcenowy Zheng 	int result;
179415fb84b7SIcenowy Zheng 	u8 i;
179515fb84b7SIcenowy Zheng 
179615fb84b7SIcenowy Zheng 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
179715fb84b7SIcenowy Zheng 					  CP210X_GET_PORTCONFIG, &config,
179815fb84b7SIcenowy Zheng 					  sizeof(config));
179915fb84b7SIcenowy Zheng 	if (result < 0)
180015fb84b7SIcenowy Zheng 		return result;
180115fb84b7SIcenowy Zheng 
180215fb84b7SIcenowy Zheng 	priv->gc.ngpio = 4;
180315fb84b7SIcenowy Zheng 
180415fb84b7SIcenowy Zheng 	iface_config = config.device_cfg;
180515fb84b7SIcenowy Zheng 	priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
180615fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
180715fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
180815fb84b7SIcenowy Zheng 	gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
180915fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
181015fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
181115fb84b7SIcenowy Zheng 
181215fb84b7SIcenowy Zheng 	/* mark all pins which are not in GPIO mode */
181315fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO0_TXLED_MODE)	/* GPIO 0 */
181415fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(0);
181515fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO1_RXLED_MODE)	/* GPIO 1 */
181615fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(1);
181715fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO2_RS485_MODE)	/* GPIO 2 */
181815fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(2);
181915fb84b7SIcenowy Zheng 
182015fb84b7SIcenowy Zheng 	/*
182115fb84b7SIcenowy Zheng 	 * Like CP2102N, CP2104 has also no strict input and output pin
182215fb84b7SIcenowy Zheng 	 * modes.
182315fb84b7SIcenowy Zheng 	 * Do the same input mode emulation as CP2102N.
182415fb84b7SIcenowy Zheng 	 */
182515fb84b7SIcenowy Zheng 	for (i = 0; i < priv->gc.ngpio; ++i) {
182615fb84b7SIcenowy Zheng 		/*
182715fb84b7SIcenowy Zheng 		 * Set direction to "input" iff pin is open-drain and reset
182815fb84b7SIcenowy Zheng 		 * value is 1.
182915fb84b7SIcenowy Zheng 		 */
183015fb84b7SIcenowy Zheng 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
183115fb84b7SIcenowy Zheng 			priv->gpio_input |= BIT(i);
183215fb84b7SIcenowy Zheng 	}
183315fb84b7SIcenowy Zheng 
183415fb84b7SIcenowy Zheng 	return 0;
183515fb84b7SIcenowy Zheng }
183615fb84b7SIcenowy Zheng 
1837c8acfe0aSKaroly Pados static int cp2102n_gpioconf_init(struct usb_serial *serial)
1838c8acfe0aSKaroly Pados {
1839c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1840c8acfe0aSKaroly Pados 	const u16 config_size = 0x02a6;
1841c8acfe0aSKaroly Pados 	u8 gpio_rst_latch;
1842c8acfe0aSKaroly Pados 	u8 config_version;
1843c8acfe0aSKaroly Pados 	u8 gpio_pushpull;
1844c8acfe0aSKaroly Pados 	u8 *config_buf;
1845c8acfe0aSKaroly Pados 	u8 gpio_latch;
1846c8acfe0aSKaroly Pados 	u8 gpio_ctrl;
1847c8acfe0aSKaroly Pados 	int result;
1848c8acfe0aSKaroly Pados 	u8 i;
1849c8acfe0aSKaroly Pados 
1850c8acfe0aSKaroly Pados 	/*
1851c8acfe0aSKaroly Pados 	 * Retrieve device configuration from the device.
1852c8acfe0aSKaroly Pados 	 * The array received contains all customization settings done at the
1853c8acfe0aSKaroly Pados 	 * factory/manufacturer. Format of the array is documented at the
1854c8acfe0aSKaroly Pados 	 * time of writing at:
1855c8acfe0aSKaroly Pados 	 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
1856c8acfe0aSKaroly Pados 	 */
1857c8acfe0aSKaroly Pados 	config_buf = kmalloc(config_size, GFP_KERNEL);
1858c8acfe0aSKaroly Pados 	if (!config_buf)
1859c8acfe0aSKaroly Pados 		return -ENOMEM;
1860c8acfe0aSKaroly Pados 
1861c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial,
1862c8acfe0aSKaroly Pados 					  REQTYPE_DEVICE_TO_HOST,
1863c8acfe0aSKaroly Pados 					  CP210X_READ_2NCONFIG,
1864c8acfe0aSKaroly Pados 					  config_buf,
1865c8acfe0aSKaroly Pados 					  config_size);
1866c8acfe0aSKaroly Pados 	if (result < 0) {
1867c8acfe0aSKaroly Pados 		kfree(config_buf);
1868c8acfe0aSKaroly Pados 		return result;
1869c8acfe0aSKaroly Pados 	}
1870c8acfe0aSKaroly Pados 
1871c8acfe0aSKaroly Pados 	config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
1872c8acfe0aSKaroly Pados 	gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
1873c8acfe0aSKaroly Pados 	gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
1874c8acfe0aSKaroly Pados 	gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
1875c8acfe0aSKaroly Pados 
1876c8acfe0aSKaroly Pados 	kfree(config_buf);
1877c8acfe0aSKaroly Pados 
1878c8acfe0aSKaroly Pados 	/* Make sure this is a config format we understand. */
1879c8acfe0aSKaroly Pados 	if (config_version != 0x01)
1880c8acfe0aSKaroly Pados 		return -ENOTSUPP;
1881c8acfe0aSKaroly Pados 
1882c8acfe0aSKaroly Pados 	priv->gc.ngpio = 4;
1883c8acfe0aSKaroly Pados 
1884c8acfe0aSKaroly Pados 	/*
1885c8acfe0aSKaroly Pados 	 * Get default pin states after reset. Needed so we can determine
1886c8acfe0aSKaroly Pados 	 * the direction of an open-drain pin.
1887c8acfe0aSKaroly Pados 	 */
1888c8acfe0aSKaroly Pados 	gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
1889c8acfe0aSKaroly Pados 
1890c8acfe0aSKaroly Pados 	/* 0 indicates open-drain mode, 1 is push-pull */
1891c8acfe0aSKaroly Pados 	priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
1892c8acfe0aSKaroly Pados 
1893c8acfe0aSKaroly Pados 	/* 0 indicates GPIO mode, 1 is alternate function */
1894c8acfe0aSKaroly Pados 	priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
1895c8acfe0aSKaroly Pados 
1896a49e1abfSMans Rullgard 	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
1897a49e1abfSMans Rullgard 		/*
1898a49e1abfSMans Rullgard 		 * For the QFN28 package, GPIO4-6 are controlled by
1899a49e1abfSMans Rullgard 		 * the low three bits of the mode/latch fields.
1900a49e1abfSMans Rullgard 		 * Contrary to the document linked above, the bits for
1901a49e1abfSMans Rullgard 		 * the SUSPEND pins are elsewhere.  No alternate
1902a49e1abfSMans Rullgard 		 * function is available for these pins.
1903a49e1abfSMans Rullgard 		 */
1904a49e1abfSMans Rullgard 		priv->gc.ngpio = 7;
1905a49e1abfSMans Rullgard 		gpio_latch |= (gpio_rst_latch & 7) << 4;
1906a49e1abfSMans Rullgard 		priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
1907a49e1abfSMans Rullgard 	}
1908a49e1abfSMans Rullgard 
1909c8acfe0aSKaroly Pados 	/*
1910c8acfe0aSKaroly Pados 	 * The CP2102N does not strictly has input and output pin modes,
1911c8acfe0aSKaroly Pados 	 * it only knows open-drain and push-pull modes which is set at
1912c8acfe0aSKaroly Pados 	 * factory. An open-drain pin can function both as an
1913c8acfe0aSKaroly Pados 	 * input or an output. We emulate input mode for open-drain pins
1914c8acfe0aSKaroly Pados 	 * by making sure they are not driven low, and we do not allow
1915c8acfe0aSKaroly Pados 	 * push-pull pins to be set as an input.
1916c8acfe0aSKaroly Pados 	 */
1917c8acfe0aSKaroly Pados 	for (i = 0; i < priv->gc.ngpio; ++i) {
1918c8acfe0aSKaroly Pados 		/*
1919c8acfe0aSKaroly Pados 		 * Set direction to "input" iff pin is open-drain and reset
1920c8acfe0aSKaroly Pados 		 * value is 1.
1921c8acfe0aSKaroly Pados 		 */
1922c8acfe0aSKaroly Pados 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1923c8acfe0aSKaroly Pados 			priv->gpio_input |= BIT(i);
1924c8acfe0aSKaroly Pados 	}
1925c8acfe0aSKaroly Pados 
1926c8acfe0aSKaroly Pados 	return 0;
1927c8acfe0aSKaroly Pados }
1928c8acfe0aSKaroly Pados 
1929c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1930c8acfe0aSKaroly Pados {
1931c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1932c8acfe0aSKaroly Pados 	int result;
1933c8acfe0aSKaroly Pados 
1934c8acfe0aSKaroly Pados 	switch (priv->partnum) {
193515fb84b7SIcenowy Zheng 	case CP210X_PARTNUM_CP2104:
193615fb84b7SIcenowy Zheng 		result = cp2104_gpioconf_init(serial);
193715fb84b7SIcenowy Zheng 		break;
1938c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2105:
1939c8acfe0aSKaroly Pados 		result = cp2105_gpioconf_init(serial);
1940c8acfe0aSKaroly Pados 		break;
1941c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
1942c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
1943c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
1944c8acfe0aSKaroly Pados 		result = cp2102n_gpioconf_init(serial);
1945c8acfe0aSKaroly Pados 		break;
1946c8acfe0aSKaroly Pados 	default:
1947c8acfe0aSKaroly Pados 		return 0;
1948c8acfe0aSKaroly Pados 	}
1949c8acfe0aSKaroly Pados 
1950c8acfe0aSKaroly Pados 	if (result < 0)
1951c8acfe0aSKaroly Pados 		return result;
1952c8acfe0aSKaroly Pados 
1953cf5276ceSMartyn Welch 	priv->gc.label = "cp210x";
1954cf5276ceSMartyn Welch 	priv->gc.request = cp210x_gpio_request;
1955cf5276ceSMartyn Welch 	priv->gc.get_direction = cp210x_gpio_direction_get;
1956cf5276ceSMartyn Welch 	priv->gc.direction_input = cp210x_gpio_direction_input;
1957cf5276ceSMartyn Welch 	priv->gc.direction_output = cp210x_gpio_direction_output;
1958cf5276ceSMartyn Welch 	priv->gc.get = cp210x_gpio_get;
1959cf5276ceSMartyn Welch 	priv->gc.set = cp210x_gpio_set;
19602956b5d9SMika Westerberg 	priv->gc.set_config = cp210x_gpio_set_config;
1961cf5276ceSMartyn Welch 	priv->gc.owner = THIS_MODULE;
1962cf5276ceSMartyn Welch 	priv->gc.parent = &serial->interface->dev;
1963cf5276ceSMartyn Welch 	priv->gc.base = -1;
1964cf5276ceSMartyn Welch 	priv->gc.can_sleep = true;
1965cf5276ceSMartyn Welch 
1966cf5276ceSMartyn Welch 	result = gpiochip_add_data(&priv->gc, serial);
1967cf5276ceSMartyn Welch 	if (!result)
19686b7271d2SJohan Hovold 		priv->gpio_registered = true;
1969cf5276ceSMartyn Welch 
1970cf5276ceSMartyn Welch 	return result;
1971cf5276ceSMartyn Welch }
1972cf5276ceSMartyn Welch 
1973cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1974cf5276ceSMartyn Welch {
1975cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1976cf5276ceSMartyn Welch 
1977cf5276ceSMartyn Welch 	if (priv->gpio_registered) {
1978cf5276ceSMartyn Welch 		gpiochip_remove(&priv->gc);
19796b7271d2SJohan Hovold 		priv->gpio_registered = false;
1980cf5276ceSMartyn Welch 	}
1981cf5276ceSMartyn Welch }
1982cf5276ceSMartyn Welch 
1983cf5276ceSMartyn Welch #else
1984cf5276ceSMartyn Welch 
1985c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1986cf5276ceSMartyn Welch {
1987cf5276ceSMartyn Welch 	return 0;
1988cf5276ceSMartyn Welch }
1989cf5276ceSMartyn Welch 
1990cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1991cf5276ceSMartyn Welch {
1992cf5276ceSMartyn Welch 	/* Nothing to do */
1993cf5276ceSMartyn Welch }
1994cf5276ceSMartyn Welch 
1995cf5276ceSMartyn Welch #endif
1996cf5276ceSMartyn Welch 
1997e2ae67a3SKonstantin Shkolnyy static int cp210x_port_probe(struct usb_serial_port *port)
199803ee2515SGreg Kroah-Hartman {
1999e2ae67a3SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
2000e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
2001d0bf1ff0SKonstantin Shkolnyy 	int ret;
2002a5360a53SPreston Fick 
2003e2ae67a3SKonstantin Shkolnyy 	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
2004e2ae67a3SKonstantin Shkolnyy 	if (!port_priv)
2005a5360a53SPreston Fick 		return -ENOMEM;
2006a5360a53SPreston Fick 
2007cf5276ceSMartyn Welch 	port_priv->bInterfaceNumber = cp210x_interface_num(serial);
2008a5360a53SPreston Fick 
2009e2ae67a3SKonstantin Shkolnyy 	usb_set_serial_port_data(port, port_priv);
2010a5360a53SPreston Fick 
2011d0bf1ff0SKonstantin Shkolnyy 	ret = cp210x_detect_swapped_line_ctl(port);
2012d0bf1ff0SKonstantin Shkolnyy 	if (ret) {
2013d0bf1ff0SKonstantin Shkolnyy 		kfree(port_priv);
2014d0bf1ff0SKonstantin Shkolnyy 		return ret;
2015d0bf1ff0SKonstantin Shkolnyy 	}
201603ee2515SGreg Kroah-Hartman 
201703ee2515SGreg Kroah-Hartman 	return 0;
201803ee2515SGreg Kroah-Hartman }
201903ee2515SGreg Kroah-Hartman 
2020e2ae67a3SKonstantin Shkolnyy static int cp210x_port_remove(struct usb_serial_port *port)
2021a5360a53SPreston Fick {
2022e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
2023a5360a53SPreston Fick 
2024e2ae67a3SKonstantin Shkolnyy 	port_priv = usb_get_serial_port_data(port);
2025e2ae67a3SKonstantin Shkolnyy 	kfree(port_priv);
2026e2ae67a3SKonstantin Shkolnyy 
2027e2ae67a3SKonstantin Shkolnyy 	return 0;
2028a5360a53SPreston Fick }
2029a5360a53SPreston Fick 
2030d4706c05SJohan Hovold static void cp210x_init_max_speed(struct usb_serial *serial)
2031d4706c05SJohan Hovold {
2032d4706c05SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
20337aecd7fcSJohan Hovold 	bool use_actual_rate = false;
203485bc2d91SJohanna Abrahamsson 	speed_t min = 300;
2035d4706c05SJohan Hovold 	speed_t max;
2036d4706c05SJohan Hovold 
2037d4706c05SJohan Hovold 	switch (priv->partnum) {
2038d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2101:
2039d4706c05SJohan Hovold 		max = 921600;
2040d4706c05SJohan Hovold 		break;
2041d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2102:
2042d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2103:
2043d4706c05SJohan Hovold 		max = 1000000;
2044d4706c05SJohan Hovold 		break;
2045d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2104:
20465edb65a3SJohan Hovold 		use_actual_rate = true;
20475edb65a3SJohan Hovold 		max = 2000000;
20485edb65a3SJohan Hovold 		break;
2049d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2108:
2050d4706c05SJohan Hovold 		max = 2000000;
2051d4706c05SJohan Hovold 		break;
2052d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2105:
20535edb65a3SJohan Hovold 		if (cp210x_interface_num(serial) == 0) {
20545edb65a3SJohan Hovold 			use_actual_rate = true;
2055d4706c05SJohan Hovold 			max = 2000000;	/* ECI */
20565edb65a3SJohan Hovold 		} else {
205785bc2d91SJohanna Abrahamsson 			min = 2400;
2058d4706c05SJohan Hovold 			max = 921600;	/* SCI */
20595edb65a3SJohan Hovold 		}
2060d4706c05SJohan Hovold 		break;
20616f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
20626f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
20636f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
20647aecd7fcSJohan Hovold 		use_actual_rate = true;
20656f0bcf72SKaroly Pados 		max = 3000000;
20666f0bcf72SKaroly Pados 		break;
2067d4706c05SJohan Hovold 	default:
2068d4706c05SJohan Hovold 		max = 2000000;
2069d4706c05SJohan Hovold 		break;
2070d4706c05SJohan Hovold 	}
2071d4706c05SJohan Hovold 
207285bc2d91SJohanna Abrahamsson 	priv->min_speed = min;
2073d4706c05SJohan Hovold 	priv->max_speed = max;
20747aecd7fcSJohan Hovold 	priv->use_actual_rate = use_actual_rate;
2075d4706c05SJohan Hovold }
2076d4706c05SJohan Hovold 
2077cf5276ceSMartyn Welch static int cp210x_attach(struct usb_serial *serial)
2078cf5276ceSMartyn Welch {
2079cf5276ceSMartyn Welch 	int result;
2080cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv;
2081cf5276ceSMartyn Welch 
2082cf5276ceSMartyn Welch 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2083cf5276ceSMartyn Welch 	if (!priv)
2084cf5276ceSMartyn Welch 		return -ENOMEM;
2085cf5276ceSMartyn Welch 
2086cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
2087cf5276ceSMartyn Welch 					  CP210X_GET_PARTNUM, &priv->partnum,
2088cf5276ceSMartyn Welch 					  sizeof(priv->partnum));
20897eac35eaSSebastian Frei 	if (result < 0) {
20907eac35eaSSebastian Frei 		dev_warn(&serial->interface->dev,
20917eac35eaSSebastian Frei 			 "querying part number failed\n");
20927eac35eaSSebastian Frei 		priv->partnum = CP210X_PARTNUM_UNKNOWN;
20937eac35eaSSebastian Frei 	}
2094cf5276ceSMartyn Welch 
2095cf5276ceSMartyn Welch 	usb_set_serial_data(serial, priv);
2096cf5276ceSMartyn Welch 
2097d4706c05SJohan Hovold 	cp210x_init_max_speed(serial);
2098d4706c05SJohan Hovold 
2099c8acfe0aSKaroly Pados 	result = cp210x_gpio_init(serial);
2100cf5276ceSMartyn Welch 	if (result < 0) {
2101c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
2102c8acfe0aSKaroly Pados 				result);
2103cf5276ceSMartyn Welch 	}
2104cf5276ceSMartyn Welch 
2105cf5276ceSMartyn Welch 	return 0;
2106cf5276ceSMartyn Welch }
2107cf5276ceSMartyn Welch 
2108cf5276ceSMartyn Welch static void cp210x_disconnect(struct usb_serial *serial)
2109cf5276ceSMartyn Welch {
2110cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2111cf5276ceSMartyn Welch }
2112cf5276ceSMartyn Welch 
2113cf5276ceSMartyn Welch static void cp210x_release(struct usb_serial *serial)
2114cf5276ceSMartyn Welch {
2115cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2116cf5276ceSMartyn Welch 
2117cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2118cf5276ceSMartyn Welch 
2119cf5276ceSMartyn Welch 	kfree(priv);
2120cf5276ceSMartyn Welch }
2121cf5276ceSMartyn Welch 
212268e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
212303ee2515SGreg Kroah-Hartman 
212403ee2515SGreg Kroah-Hartman MODULE_DESCRIPTION(DRIVER_DESC);
2125627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
2126