xref: /linux/drivers/usb/serial/cp210x.c (revision a7207e9835a4f245c8c693170906fda0980273f3)
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);
53*a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb);
54*a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port);
55*a7207e98SJohan 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 
259*a7207e98SJohan Hovold enum cp210x_event_state {
260*a7207e98SJohan Hovold 	ES_DATA,
261*a7207e98SJohan Hovold 	ES_ESCAPE,
262*a7207e98SJohan Hovold 	ES_LSR,
263*a7207e98SJohan Hovold 	ES_LSR_DATA_0,
264*a7207e98SJohan Hovold 	ES_LSR_DATA_1,
265*a7207e98SJohan Hovold 	ES_MSR
266*a7207e98SJohan Hovold };
267*a7207e98SJohan Hovold 
268e2ae67a3SKonstantin Shkolnyy struct cp210x_port_private {
269a5360a53SPreston Fick 	__u8			bInterfaceNumber;
270d0bf1ff0SKonstantin Shkolnyy 	bool			has_swapped_line_ctl;
271*a7207e98SJohan Hovold 	bool			event_mode;
272*a7207e98SJohan Hovold 	enum cp210x_event_state event_state;
273*a7207e98SJohan 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,
294cf5276ceSMartyn Welch 	.attach			= cp210x_attach,
295cf5276ceSMartyn Welch 	.disconnect		= cp210x_disconnect,
296cf5276ceSMartyn Welch 	.release		= cp210x_release,
297e2ae67a3SKonstantin Shkolnyy 	.port_probe		= cp210x_port_probe,
298e2ae67a3SKonstantin Shkolnyy 	.port_remove		= cp210x_port_remove,
299*a7207e98SJohan Hovold 	.dtr_rts		= cp210x_dtr_rts,
300*a7207e98SJohan Hovold 	.process_read_urb	= cp210x_process_read_urb,
30103ee2515SGreg Kroah-Hartman };
30203ee2515SGreg Kroah-Hartman 
30308a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
30408a4f6bcSAlan Stern 	&cp210x_device, NULL
30508a4f6bcSAlan Stern };
30608a4f6bcSAlan Stern 
30703ee2515SGreg Kroah-Hartman /* Config request types */
30832445605SPreston Fick #define REQTYPE_HOST_TO_INTERFACE	0x41
30932445605SPreston Fick #define REQTYPE_INTERFACE_TO_HOST	0xc1
31032445605SPreston Fick #define REQTYPE_HOST_TO_DEVICE	0x40
31132445605SPreston Fick #define REQTYPE_DEVICE_TO_HOST	0xc0
31203ee2515SGreg Kroah-Hartman 
31393ef1f1fSCraig Shelley /* Config request codes */
31493ef1f1fSCraig Shelley #define CP210X_IFC_ENABLE	0x00
31593ef1f1fSCraig Shelley #define CP210X_SET_BAUDDIV	0x01
31693ef1f1fSCraig Shelley #define CP210X_GET_BAUDDIV	0x02
31793ef1f1fSCraig Shelley #define CP210X_SET_LINE_CTL	0x03
31893ef1f1fSCraig Shelley #define CP210X_GET_LINE_CTL	0x04
31993ef1f1fSCraig Shelley #define CP210X_SET_BREAK	0x05
32093ef1f1fSCraig Shelley #define CP210X_IMM_CHAR		0x06
32193ef1f1fSCraig Shelley #define CP210X_SET_MHS		0x07
32293ef1f1fSCraig Shelley #define CP210X_GET_MDMSTS	0x08
32393ef1f1fSCraig Shelley #define CP210X_SET_XON		0x09
32493ef1f1fSCraig Shelley #define CP210X_SET_XOFF		0x0A
32593ef1f1fSCraig Shelley #define CP210X_SET_EVENTMASK	0x0B
32693ef1f1fSCraig Shelley #define CP210X_GET_EVENTMASK	0x0C
32793ef1f1fSCraig Shelley #define CP210X_SET_CHAR		0x0D
32893ef1f1fSCraig Shelley #define CP210X_GET_CHARS	0x0E
32993ef1f1fSCraig Shelley #define CP210X_GET_PROPS	0x0F
33093ef1f1fSCraig Shelley #define CP210X_GET_COMM_STATUS	0x10
33193ef1f1fSCraig Shelley #define CP210X_RESET		0x11
33293ef1f1fSCraig Shelley #define CP210X_PURGE		0x12
33393ef1f1fSCraig Shelley #define CP210X_SET_FLOW		0x13
33493ef1f1fSCraig Shelley #define CP210X_GET_FLOW		0x14
33593ef1f1fSCraig Shelley #define CP210X_EMBED_EVENTS	0x15
33693ef1f1fSCraig Shelley #define CP210X_GET_EVENTSTATE	0x16
33793ef1f1fSCraig Shelley #define CP210X_SET_CHARS	0x19
3387f482fc8SPreston Fick #define CP210X_GET_BAUDRATE	0x1D
3397f482fc8SPreston Fick #define CP210X_SET_BAUDRATE	0x1E
340cf5276ceSMartyn Welch #define CP210X_VENDOR_SPECIFIC	0xFF
34103ee2515SGreg Kroah-Hartman 
34293ef1f1fSCraig Shelley /* CP210X_IFC_ENABLE */
34303ee2515SGreg Kroah-Hartman #define UART_ENABLE		0x0001
34403ee2515SGreg Kroah-Hartman #define UART_DISABLE		0x0000
34503ee2515SGreg Kroah-Hartman 
34693ef1f1fSCraig Shelley /* CP210X_(SET|GET)_BAUDDIV */
34703ee2515SGreg Kroah-Hartman #define BAUD_RATE_GEN_FREQ	0x384000
34803ee2515SGreg Kroah-Hartman 
34993ef1f1fSCraig Shelley /* CP210X_(SET|GET)_LINE_CTL */
35003ee2515SGreg Kroah-Hartman #define BITS_DATA_MASK		0X0f00
35103ee2515SGreg Kroah-Hartman #define BITS_DATA_5		0X0500
35203ee2515SGreg Kroah-Hartman #define BITS_DATA_6		0X0600
35303ee2515SGreg Kroah-Hartman #define BITS_DATA_7		0X0700
35403ee2515SGreg Kroah-Hartman #define BITS_DATA_8		0X0800
35503ee2515SGreg Kroah-Hartman #define BITS_DATA_9		0X0900
35603ee2515SGreg Kroah-Hartman 
35703ee2515SGreg Kroah-Hartman #define BITS_PARITY_MASK	0x00f0
35803ee2515SGreg Kroah-Hartman #define BITS_PARITY_NONE	0x0000
35903ee2515SGreg Kroah-Hartman #define BITS_PARITY_ODD		0x0010
36003ee2515SGreg Kroah-Hartman #define BITS_PARITY_EVEN	0x0020
36103ee2515SGreg Kroah-Hartman #define BITS_PARITY_MARK	0x0030
36203ee2515SGreg Kroah-Hartman #define BITS_PARITY_SPACE	0x0040
36303ee2515SGreg Kroah-Hartman 
36403ee2515SGreg Kroah-Hartman #define BITS_STOP_MASK		0x000f
36503ee2515SGreg Kroah-Hartman #define BITS_STOP_1		0x0000
36603ee2515SGreg Kroah-Hartman #define BITS_STOP_1_5		0x0001
36703ee2515SGreg Kroah-Hartman #define BITS_STOP_2		0x0002
36803ee2515SGreg Kroah-Hartman 
36993ef1f1fSCraig Shelley /* CP210X_SET_BREAK */
37072916791SCraig Shelley #define BREAK_ON		0x0001
37172916791SCraig Shelley #define BREAK_OFF		0x0000
37203ee2515SGreg Kroah-Hartman 
37393ef1f1fSCraig Shelley /* CP210X_(SET_MHS|GET_MDMSTS) */
37403ee2515SGreg Kroah-Hartman #define CONTROL_DTR		0x0001
37503ee2515SGreg Kroah-Hartman #define CONTROL_RTS		0x0002
37603ee2515SGreg Kroah-Hartman #define CONTROL_CTS		0x0010
37703ee2515SGreg Kroah-Hartman #define CONTROL_DSR		0x0020
37803ee2515SGreg Kroah-Hartman #define CONTROL_RING		0x0040
37903ee2515SGreg Kroah-Hartman #define CONTROL_DCD		0x0080
38003ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_DTR	0x0100
38103ee2515SGreg Kroah-Hartman #define CONTROL_WRITE_RTS	0x0200
38203ee2515SGreg Kroah-Hartman 
383cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC values */
384c8acfe0aSKaroly Pados #define CP210X_READ_2NCONFIG	0x000E
385cf5276ceSMartyn Welch #define CP210X_READ_LATCH	0x00C2
386cf5276ceSMartyn Welch #define CP210X_GET_PARTNUM	0x370B
387cf5276ceSMartyn Welch #define CP210X_GET_PORTCONFIG	0x370C
388cf5276ceSMartyn Welch #define CP210X_GET_DEVICEMODE	0x3711
389cf5276ceSMartyn Welch #define CP210X_WRITE_LATCH	0x37E1
390cf5276ceSMartyn Welch 
391cf5276ceSMartyn Welch /* Part number definitions */
392cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2101	0x01
393cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2102	0x02
394cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2103	0x03
395cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2104	0x04
396cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2105	0x05
397cf5276ceSMartyn Welch #define CP210X_PARTNUM_CP2108	0x08
3986f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN28	0x20
3996f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN24	0x21
4006f0bcf72SKaroly Pados #define CP210X_PARTNUM_CP2102N_QFN20	0x22
4017eac35eaSSebastian Frei #define CP210X_PARTNUM_UNKNOWN	0xFF
402cf5276ceSMartyn Welch 
4036f923a01SKonstantin Shkolnyy /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
4046f923a01SKonstantin Shkolnyy struct cp210x_comm_status {
4056f923a01SKonstantin Shkolnyy 	__le32   ulErrors;
4066f923a01SKonstantin Shkolnyy 	__le32   ulHoldReasons;
4076f923a01SKonstantin Shkolnyy 	__le32   ulAmountInInQueue;
4086f923a01SKonstantin Shkolnyy 	__le32   ulAmountInOutQueue;
4096f923a01SKonstantin Shkolnyy 	u8       bEofReceived;
4106f923a01SKonstantin Shkolnyy 	u8       bWaitForImmediate;
4116f923a01SKonstantin Shkolnyy 	u8       bReserved;
4126f923a01SKonstantin Shkolnyy } __packed;
4136f923a01SKonstantin Shkolnyy 
41403ee2515SGreg Kroah-Hartman /*
415ebfb319bSKonstantin Shkolnyy  * CP210X_PURGE - 16 bits passed in wValue of USB request.
416ebfb319bSKonstantin Shkolnyy  * SiLabs app note AN571 gives a strange description of the 4 bits:
417ebfb319bSKonstantin Shkolnyy  * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
418ebfb319bSKonstantin Shkolnyy  * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
419ebfb319bSKonstantin Shkolnyy  */
420ebfb319bSKonstantin Shkolnyy #define PURGE_ALL		0x000f
421ebfb319bSKonstantin Shkolnyy 
422*a7207e98SJohan Hovold /* CP210X_EMBED_EVENTS */
423*a7207e98SJohan Hovold #define CP210X_ESCCHAR		0xec
424*a7207e98SJohan Hovold 
425*a7207e98SJohan Hovold #define CP210X_LSR_OVERRUN	BIT(1)
426*a7207e98SJohan Hovold #define CP210X_LSR_PARITY	BIT(2)
427*a7207e98SJohan Hovold #define CP210X_LSR_FRAME	BIT(3)
428*a7207e98SJohan Hovold #define CP210X_LSR_BREAK	BIT(4)
429*a7207e98SJohan Hovold 
430*a7207e98SJohan Hovold 
4319034389cSKonstantin Shkolnyy /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
4329034389cSKonstantin Shkolnyy struct cp210x_flow_ctl {
4339034389cSKonstantin Shkolnyy 	__le32	ulControlHandshake;
4349034389cSKonstantin Shkolnyy 	__le32	ulFlowReplace;
4359034389cSKonstantin Shkolnyy 	__le32	ulXonLimit;
4369034389cSKonstantin Shkolnyy 	__le32	ulXoffLimit;
4379034389cSKonstantin Shkolnyy } __packed;
4389034389cSKonstantin Shkolnyy 
4399034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulControlHandshake */
4409034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_MASK		GENMASK(1, 0)
4419034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_SHIFT(_mode)	(_mode)
4429034389cSKonstantin Shkolnyy #define CP210X_SERIAL_CTS_HANDSHAKE	BIT(3)
4439034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_HANDSHAKE	BIT(4)
4449034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DCD_HANDSHAKE	BIT(5)
4459034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DSR_SENSITIVITY	BIT(6)
4469034389cSKonstantin Shkolnyy 
4479034389cSKonstantin Shkolnyy /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
4489034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_INACTIVE	0
4499034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_ACTIVE	1
4509034389cSKonstantin Shkolnyy #define CP210X_SERIAL_DTR_FLOW_CTL	2
4519034389cSKonstantin Shkolnyy 
4529034389cSKonstantin Shkolnyy /* cp210x_flow_ctl::ulFlowReplace */
4539034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_TRANSMIT	BIT(0)
4549034389cSKonstantin Shkolnyy #define CP210X_SERIAL_AUTO_RECEIVE	BIT(1)
4559034389cSKonstantin Shkolnyy #define CP210X_SERIAL_ERROR_CHAR	BIT(2)
4569034389cSKonstantin Shkolnyy #define CP210X_SERIAL_NULL_STRIPPING	BIT(3)
4579034389cSKonstantin Shkolnyy #define CP210X_SERIAL_BREAK_CHAR	BIT(4)
4589034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_MASK		GENMASK(7, 6)
4599034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_SHIFT(_mode)	(_mode << 6)
4609034389cSKonstantin Shkolnyy #define CP210X_SERIAL_XOFF_CONTINUE	BIT(31)
4619034389cSKonstantin Shkolnyy 
4629034389cSKonstantin Shkolnyy /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
4639034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_INACTIVE	0
4649034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_ACTIVE	1
4659034389cSKonstantin Shkolnyy #define CP210X_SERIAL_RTS_FLOW_CTL	2
4669034389cSKonstantin Shkolnyy 
467cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
468cf5276ceSMartyn Welch struct cp210x_pin_mode {
469cf5276ceSMartyn Welch 	u8	eci;
470cf5276ceSMartyn Welch 	u8	sci;
471cf5276ceSMartyn Welch } __packed;
472cf5276ceSMartyn Welch 
473cf5276ceSMartyn Welch #define CP210X_PIN_MODE_MODEM		0
474cf5276ceSMartyn Welch #define CP210X_PIN_MODE_GPIO		BIT(0)
475cf5276ceSMartyn Welch 
476cf5276ceSMartyn Welch /*
47715fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
47815fb84b7SIcenowy Zheng  * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
479cf5276ceSMartyn Welch  */
48015fb84b7SIcenowy Zheng struct cp210x_dual_port_config {
481cf5276ceSMartyn Welch 	__le16	gpio_mode;
482cf5276ceSMartyn Welch 	u8	__pad0[2];
483cf5276ceSMartyn Welch 	__le16	reset_state;
484cf5276ceSMartyn Welch 	u8	__pad1[4];
485cf5276ceSMartyn Welch 	__le16	suspend_state;
486cf5276ceSMartyn Welch 	u8	sci_cfg;
487cf5276ceSMartyn Welch 	u8	eci_cfg;
488cf5276ceSMartyn Welch 	u8	device_cfg;
489cf5276ceSMartyn Welch } __packed;
490cf5276ceSMartyn Welch 
49115fb84b7SIcenowy Zheng /*
49215fb84b7SIcenowy Zheng  * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
49315fb84b7SIcenowy Zheng  * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
49415fb84b7SIcenowy Zheng  */
49515fb84b7SIcenowy Zheng struct cp210x_single_port_config {
49615fb84b7SIcenowy Zheng 	__le16	gpio_mode;
49715fb84b7SIcenowy Zheng 	u8	__pad0[2];
49815fb84b7SIcenowy Zheng 	__le16	reset_state;
49915fb84b7SIcenowy Zheng 	u8	__pad1[4];
50015fb84b7SIcenowy Zheng 	__le16	suspend_state;
50115fb84b7SIcenowy Zheng 	u8	device_cfg;
50215fb84b7SIcenowy Zheng } __packed;
50315fb84b7SIcenowy Zheng 
504cf5276ceSMartyn Welch /* GPIO modes */
505cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_OFFSET	9
506cf5276ceSMartyn Welch #define CP210X_SCI_GPIO_MODE_MASK	GENMASK(11, 9)
507cf5276ceSMartyn Welch 
508cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_OFFSET	2
509cf5276ceSMartyn Welch #define CP210X_ECI_GPIO_MODE_MASK	GENMASK(3, 2)
510cf5276ceSMartyn Welch 
51115fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_OFFSET		8
51215fb84b7SIcenowy Zheng #define CP210X_GPIO_MODE_MASK		GENMASK(11, 8)
51315fb84b7SIcenowy Zheng 
514cf5276ceSMartyn Welch /* CP2105 port configuration values */
515cf5276ceSMartyn Welch #define CP2105_GPIO0_TXLED_MODE		BIT(0)
516cf5276ceSMartyn Welch #define CP2105_GPIO1_RXLED_MODE		BIT(1)
517cf5276ceSMartyn Welch #define CP2105_GPIO1_RS485_MODE		BIT(2)
518cf5276ceSMartyn Welch 
51915fb84b7SIcenowy Zheng /* CP2104 port configuration values */
52015fb84b7SIcenowy Zheng #define CP2104_GPIO0_TXLED_MODE		BIT(0)
52115fb84b7SIcenowy Zheng #define CP2104_GPIO1_RXLED_MODE		BIT(1)
52215fb84b7SIcenowy Zheng #define CP2104_GPIO2_RS485_MODE		BIT(2)
52315fb84b7SIcenowy Zheng 
524c8acfe0aSKaroly Pados /* CP2102N configuration array indices */
525c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_CONFIG_VERSION_IDX	2
526c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_MODE_IDX		581
527c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX	587
528c8acfe0aSKaroly Pados #define CP210X_2NCONFIG_GPIO_CONTROL_IDX	600
529c8acfe0aSKaroly Pados 
530cf5276ceSMartyn Welch /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
531cf5276ceSMartyn Welch struct cp210x_gpio_write {
532cf5276ceSMartyn Welch 	u8	mask;
533cf5276ceSMartyn Welch 	u8	state;
534cf5276ceSMartyn Welch } __packed;
535cf5276ceSMartyn Welch 
536cf5276ceSMartyn Welch /*
537cf5276ceSMartyn Welch  * Helper to get interface number when we only have struct usb_serial.
538cf5276ceSMartyn Welch  */
539cf5276ceSMartyn Welch static u8 cp210x_interface_num(struct usb_serial *serial)
540cf5276ceSMartyn Welch {
541cf5276ceSMartyn Welch 	struct usb_host_interface *cur_altsetting;
542cf5276ceSMartyn Welch 
543cf5276ceSMartyn Welch 	cur_altsetting = serial->interface->cur_altsetting;
544cf5276ceSMartyn Welch 
545cf5276ceSMartyn Welch 	return cur_altsetting->desc.bInterfaceNumber;
546cf5276ceSMartyn Welch }
547cf5276ceSMartyn Welch 
54803ee2515SGreg Kroah-Hartman /*
54919165b2bSKonstantin Shkolnyy  * Reads a variable-sized block of CP210X_ registers, identified by req.
55019165b2bSKonstantin Shkolnyy  * Returns data into buf in native USB byte order.
55103ee2515SGreg Kroah-Hartman  */
55219165b2bSKonstantin Shkolnyy static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
55319165b2bSKonstantin Shkolnyy 		void *buf, int bufsize)
55403ee2515SGreg Kroah-Hartman {
55503ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
556e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
55719165b2bSKonstantin Shkolnyy 	void *dmabuf;
55819165b2bSKonstantin Shkolnyy 	int result;
55903ee2515SGreg Kroah-Hartman 
56019165b2bSKonstantin Shkolnyy 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
56119165b2bSKonstantin Shkolnyy 	if (!dmabuf) {
56219165b2bSKonstantin Shkolnyy 		/*
56319165b2bSKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
56419165b2bSKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
56519165b2bSKonstantin Shkolnyy 		 */
56619165b2bSKonstantin Shkolnyy 		memset(buf, 0, bufsize);
56703ee2515SGreg Kroah-Hartman 		return -ENOMEM;
56819165b2bSKonstantin Shkolnyy 	}
56903ee2515SGreg Kroah-Hartman 
57003ee2515SGreg Kroah-Hartman 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
57119165b2bSKonstantin Shkolnyy 			req, REQTYPE_INTERFACE_TO_HOST, 0,
57219165b2bSKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
57319165b2bSKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
57419165b2bSKonstantin Shkolnyy 	if (result == bufsize) {
57519165b2bSKonstantin Shkolnyy 		memcpy(buf, dmabuf, bufsize);
57619165b2bSKonstantin Shkolnyy 		result = 0;
57719165b2bSKonstantin Shkolnyy 	} else {
57819165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
57919165b2bSKonstantin Shkolnyy 				req, bufsize, result);
58019165b2bSKonstantin Shkolnyy 		if (result >= 0)
5810407f1ceSJohan Hovold 			result = -EIO;
5822479e2a9SJohan Hovold 
58319165b2bSKonstantin Shkolnyy 		/*
58419165b2bSKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
58519165b2bSKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
58619165b2bSKonstantin Shkolnyy 		 */
58719165b2bSKonstantin Shkolnyy 		memset(buf, 0, bufsize);
58819165b2bSKonstantin Shkolnyy 	}
58919165b2bSKonstantin Shkolnyy 
59019165b2bSKonstantin Shkolnyy 	kfree(dmabuf);
59119165b2bSKonstantin Shkolnyy 
5922479e2a9SJohan Hovold 	return result;
59303ee2515SGreg Kroah-Hartman }
59403ee2515SGreg Kroah-Hartman 
59519165b2bSKonstantin Shkolnyy /*
596fe1b07e9SKonstantin Shkolnyy  * Reads any 32-bit CP210X_ register identified by req.
597fe1b07e9SKonstantin Shkolnyy  */
598fe1b07e9SKonstantin Shkolnyy static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
599fe1b07e9SKonstantin Shkolnyy {
600fe1b07e9SKonstantin Shkolnyy 	__le32 le32_val;
601fe1b07e9SKonstantin Shkolnyy 	int err;
602fe1b07e9SKonstantin Shkolnyy 
603fe1b07e9SKonstantin Shkolnyy 	err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
604fe1b07e9SKonstantin Shkolnyy 	if (err) {
605fe1b07e9SKonstantin Shkolnyy 		/*
606fe1b07e9SKonstantin Shkolnyy 		 * FIXME Some callers don't bother to check for error,
607fe1b07e9SKonstantin Shkolnyy 		 * at least give them consistent junk until they are fixed
608fe1b07e9SKonstantin Shkolnyy 		 */
609fe1b07e9SKonstantin Shkolnyy 		*val = 0;
610fe1b07e9SKonstantin Shkolnyy 		return err;
611fe1b07e9SKonstantin Shkolnyy 	}
612fe1b07e9SKonstantin Shkolnyy 
613fe1b07e9SKonstantin Shkolnyy 	*val = le32_to_cpu(le32_val);
614fe1b07e9SKonstantin Shkolnyy 
61503ee2515SGreg Kroah-Hartman 	return 0;
61603ee2515SGreg Kroah-Hartman }
61703ee2515SGreg Kroah-Hartman 
61803ee2515SGreg Kroah-Hartman /*
61919165b2bSKonstantin Shkolnyy  * Reads any 16-bit CP210X_ register identified by req.
62003ee2515SGreg Kroah-Hartman  */
62119165b2bSKonstantin Shkolnyy static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
62219165b2bSKonstantin Shkolnyy {
62319165b2bSKonstantin Shkolnyy 	__le16 le16_val;
62419165b2bSKonstantin Shkolnyy 	int err;
62519165b2bSKonstantin Shkolnyy 
62619165b2bSKonstantin Shkolnyy 	err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
62719165b2bSKonstantin Shkolnyy 	if (err)
62819165b2bSKonstantin Shkolnyy 		return err;
62919165b2bSKonstantin Shkolnyy 
63019165b2bSKonstantin Shkolnyy 	*val = le16_to_cpu(le16_val);
631fe1b07e9SKonstantin Shkolnyy 
63219165b2bSKonstantin Shkolnyy 	return 0;
63319165b2bSKonstantin Shkolnyy }
63419165b2bSKonstantin Shkolnyy 
63519165b2bSKonstantin Shkolnyy /*
636fe1b07e9SKonstantin Shkolnyy  * Reads any 8-bit CP210X_ register identified by req.
637fe1b07e9SKonstantin Shkolnyy  */
638fe1b07e9SKonstantin Shkolnyy static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
639fe1b07e9SKonstantin Shkolnyy {
640fe1b07e9SKonstantin Shkolnyy 	return cp210x_read_reg_block(port, req, val, sizeof(*val));
641fe1b07e9SKonstantin Shkolnyy }
642fe1b07e9SKonstantin Shkolnyy 
643fe1b07e9SKonstantin Shkolnyy /*
644cf5276ceSMartyn Welch  * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
645cf5276ceSMartyn Welch  * Returns data into buf in native USB byte order.
646cf5276ceSMartyn Welch  */
647cf5276ceSMartyn Welch static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
648cf5276ceSMartyn Welch 				    void *buf, int bufsize)
649cf5276ceSMartyn Welch {
650cf5276ceSMartyn Welch 	void *dmabuf;
651cf5276ceSMartyn Welch 	int result;
652cf5276ceSMartyn Welch 
653cf5276ceSMartyn Welch 	dmabuf = kmalloc(bufsize, GFP_KERNEL);
654cf5276ceSMartyn Welch 	if (!dmabuf)
655cf5276ceSMartyn Welch 		return -ENOMEM;
656cf5276ceSMartyn Welch 
657cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
658cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
659cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
660cf5276ceSMartyn Welch 				 USB_CTRL_GET_TIMEOUT);
661cf5276ceSMartyn Welch 	if (result == bufsize) {
662cf5276ceSMartyn Welch 		memcpy(buf, dmabuf, bufsize);
663cf5276ceSMartyn Welch 		result = 0;
664cf5276ceSMartyn Welch 	} else {
665cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
666cf5276ceSMartyn Welch 			"failed to get vendor val 0x%04x size %d: %d\n", val,
667cf5276ceSMartyn Welch 			bufsize, result);
668cf5276ceSMartyn Welch 		if (result >= 0)
669cf5276ceSMartyn Welch 			result = -EIO;
670cf5276ceSMartyn Welch 	}
671cf5276ceSMartyn Welch 
672cf5276ceSMartyn Welch 	kfree(dmabuf);
673cf5276ceSMartyn Welch 
674cf5276ceSMartyn Welch 	return result;
675cf5276ceSMartyn Welch }
676cf5276ceSMartyn Welch 
677cf5276ceSMartyn Welch /*
67819165b2bSKonstantin Shkolnyy  * Writes any 16-bit CP210X_ register (req) whose value is passed
67919165b2bSKonstantin Shkolnyy  * entirely in the wValue field of the USB request.
68019165b2bSKonstantin Shkolnyy  */
68119165b2bSKonstantin Shkolnyy static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
68203ee2515SGreg Kroah-Hartman {
68303ee2515SGreg Kroah-Hartman 	struct usb_serial *serial = port->serial;
684e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
68519165b2bSKonstantin Shkolnyy 	int result;
68603ee2515SGreg Kroah-Hartman 
68719165b2bSKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
68819165b2bSKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, val,
689e2ae67a3SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, NULL, 0,
6902d5733fcSYuri Matylitski 			USB_CTRL_SET_TIMEOUT);
69119165b2bSKonstantin Shkolnyy 	if (result < 0) {
69219165b2bSKonstantin Shkolnyy 		dev_err(&port->dev, "failed set request 0x%x status: %d\n",
69319165b2bSKonstantin Shkolnyy 				req, result);
69403ee2515SGreg Kroah-Hartman 	}
69503ee2515SGreg Kroah-Hartman 
6962479e2a9SJohan Hovold 	return result;
69703ee2515SGreg Kroah-Hartman }
69803ee2515SGreg Kroah-Hartman 
69903ee2515SGreg Kroah-Hartman /*
700fe1b07e9SKonstantin Shkolnyy  * Writes a variable-sized block of CP210X_ registers, identified by req.
701fe1b07e9SKonstantin Shkolnyy  * Data in buf must be in native USB byte order.
702fe1b07e9SKonstantin Shkolnyy  */
703fe1b07e9SKonstantin Shkolnyy static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
704fe1b07e9SKonstantin Shkolnyy 		void *buf, int bufsize)
705fe1b07e9SKonstantin Shkolnyy {
706fe1b07e9SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
707fe1b07e9SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
708fe1b07e9SKonstantin Shkolnyy 	void *dmabuf;
709fe1b07e9SKonstantin Shkolnyy 	int result;
710fe1b07e9SKonstantin Shkolnyy 
711d6c4dc3bSMuhammad Falak R Wani 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
712fe1b07e9SKonstantin Shkolnyy 	if (!dmabuf)
713fe1b07e9SKonstantin Shkolnyy 		return -ENOMEM;
714fe1b07e9SKonstantin Shkolnyy 
715fe1b07e9SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
716fe1b07e9SKonstantin Shkolnyy 			req, REQTYPE_HOST_TO_INTERFACE, 0,
717fe1b07e9SKonstantin Shkolnyy 			port_priv->bInterfaceNumber, dmabuf, bufsize,
718fe1b07e9SKonstantin Shkolnyy 			USB_CTRL_SET_TIMEOUT);
719fe1b07e9SKonstantin Shkolnyy 
720fe1b07e9SKonstantin Shkolnyy 	kfree(dmabuf);
721fe1b07e9SKonstantin Shkolnyy 
722fe1b07e9SKonstantin Shkolnyy 	if (result == bufsize) {
723fe1b07e9SKonstantin Shkolnyy 		result = 0;
724fe1b07e9SKonstantin Shkolnyy 	} else {
725fe1b07e9SKonstantin Shkolnyy 		dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
726fe1b07e9SKonstantin Shkolnyy 				req, bufsize, result);
727fe1b07e9SKonstantin Shkolnyy 		if (result >= 0)
7280407f1ceSJohan Hovold 			result = -EIO;
729fe1b07e9SKonstantin Shkolnyy 	}
730fe1b07e9SKonstantin Shkolnyy 
731fe1b07e9SKonstantin Shkolnyy 	return result;
73203ee2515SGreg Kroah-Hartman }
73303ee2515SGreg Kroah-Hartman 
73403ee2515SGreg Kroah-Hartman /*
735fe1b07e9SKonstantin Shkolnyy  * Writes any 32-bit CP210X_ register identified by req.
73603ee2515SGreg Kroah-Hartman  */
737fe1b07e9SKonstantin Shkolnyy static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
73803ee2515SGreg Kroah-Hartman {
739fe1b07e9SKonstantin Shkolnyy 	__le32 le32_val;
740fe1b07e9SKonstantin Shkolnyy 
741fe1b07e9SKonstantin Shkolnyy 	le32_val = cpu_to_le32(val);
742fe1b07e9SKonstantin Shkolnyy 
743fe1b07e9SKonstantin Shkolnyy 	return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
74403ee2515SGreg Kroah-Hartman }
74503ee2515SGreg Kroah-Hartman 
746cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
747cf5276ceSMartyn Welch /*
748cf5276ceSMartyn Welch  * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
749cf5276ceSMartyn Welch  * Data in buf must be in native USB byte order.
750cf5276ceSMartyn Welch  */
751cf5276ceSMartyn Welch static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
752cf5276ceSMartyn Welch 				     u16 val, void *buf, int bufsize)
753cf5276ceSMartyn Welch {
754cf5276ceSMartyn Welch 	void *dmabuf;
755cf5276ceSMartyn Welch 	int result;
756cf5276ceSMartyn Welch 
757cf5276ceSMartyn Welch 	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
758cf5276ceSMartyn Welch 	if (!dmabuf)
759cf5276ceSMartyn Welch 		return -ENOMEM;
760cf5276ceSMartyn Welch 
761cf5276ceSMartyn Welch 	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
762cf5276ceSMartyn Welch 				 CP210X_VENDOR_SPECIFIC, type, val,
763cf5276ceSMartyn Welch 				 cp210x_interface_num(serial), dmabuf, bufsize,
764cf5276ceSMartyn Welch 				 USB_CTRL_SET_TIMEOUT);
765cf5276ceSMartyn Welch 
766cf5276ceSMartyn Welch 	kfree(dmabuf);
767cf5276ceSMartyn Welch 
768cf5276ceSMartyn Welch 	if (result == bufsize) {
769cf5276ceSMartyn Welch 		result = 0;
770cf5276ceSMartyn Welch 	} else {
771cf5276ceSMartyn Welch 		dev_err(&serial->interface->dev,
772cf5276ceSMartyn Welch 			"failed to set vendor val 0x%04x size %d: %d\n", val,
773cf5276ceSMartyn Welch 			bufsize, result);
774cf5276ceSMartyn Welch 		if (result >= 0)
775cf5276ceSMartyn Welch 			result = -EIO;
776cf5276ceSMartyn Welch 	}
777cf5276ceSMartyn Welch 
778cf5276ceSMartyn Welch 	return result;
779cf5276ceSMartyn Welch }
780cf5276ceSMartyn Welch #endif
781cf5276ceSMartyn Welch 
78203ee2515SGreg Kroah-Hartman /*
783d0bf1ff0SKonstantin Shkolnyy  * Detect CP2108 GET_LINE_CTL bug and activate workaround.
784d0bf1ff0SKonstantin Shkolnyy  * Write a known good value 0x800, read it back.
785d0bf1ff0SKonstantin Shkolnyy  * If it comes back swapped the bug is detected.
786d0bf1ff0SKonstantin Shkolnyy  * Preserve the original register value.
787d0bf1ff0SKonstantin Shkolnyy  */
788d0bf1ff0SKonstantin Shkolnyy static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
789d0bf1ff0SKonstantin Shkolnyy {
790d0bf1ff0SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
79119165b2bSKonstantin Shkolnyy 	u16 line_ctl_save;
79219165b2bSKonstantin Shkolnyy 	u16 line_ctl_test;
793d0bf1ff0SKonstantin Shkolnyy 	int err;
794d0bf1ff0SKonstantin Shkolnyy 
79519165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
796d0bf1ff0SKonstantin Shkolnyy 	if (err)
797d0bf1ff0SKonstantin Shkolnyy 		return err;
798d0bf1ff0SKonstantin Shkolnyy 
79919165b2bSKonstantin Shkolnyy 	err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
800d0bf1ff0SKonstantin Shkolnyy 	if (err)
801d0bf1ff0SKonstantin Shkolnyy 		return err;
802d0bf1ff0SKonstantin Shkolnyy 
80319165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
804d0bf1ff0SKonstantin Shkolnyy 	if (err)
805d0bf1ff0SKonstantin Shkolnyy 		return err;
806d0bf1ff0SKonstantin Shkolnyy 
807d0bf1ff0SKonstantin Shkolnyy 	if (line_ctl_test == 8) {
808d0bf1ff0SKonstantin Shkolnyy 		port_priv->has_swapped_line_ctl = true;
80919165b2bSKonstantin Shkolnyy 		line_ctl_save = swab16(line_ctl_save);
810d0bf1ff0SKonstantin Shkolnyy 	}
811d0bf1ff0SKonstantin Shkolnyy 
81219165b2bSKonstantin Shkolnyy 	return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
813d0bf1ff0SKonstantin Shkolnyy }
814d0bf1ff0SKonstantin Shkolnyy 
815d0bf1ff0SKonstantin Shkolnyy /*
81619165b2bSKonstantin Shkolnyy  * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
817d0bf1ff0SKonstantin Shkolnyy  * to workaround cp2108 bug and get correct value.
818d0bf1ff0SKonstantin Shkolnyy  */
81919165b2bSKonstantin Shkolnyy static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
820d0bf1ff0SKonstantin Shkolnyy {
821d0bf1ff0SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
822d0bf1ff0SKonstantin Shkolnyy 	int err;
823d0bf1ff0SKonstantin Shkolnyy 
82419165b2bSKonstantin Shkolnyy 	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
825d0bf1ff0SKonstantin Shkolnyy 	if (err)
826d0bf1ff0SKonstantin Shkolnyy 		return err;
827d0bf1ff0SKonstantin Shkolnyy 
828d0bf1ff0SKonstantin Shkolnyy 	/* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
829d0bf1ff0SKonstantin Shkolnyy 	if (port_priv->has_swapped_line_ctl)
83019165b2bSKonstantin Shkolnyy 		*ctl = swab16(*ctl);
831d0bf1ff0SKonstantin Shkolnyy 
832d0bf1ff0SKonstantin Shkolnyy 	return 0;
833d0bf1ff0SKonstantin Shkolnyy }
834d0bf1ff0SKonstantin Shkolnyy 
835a509a7e4SAlan Cox static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
83603ee2515SGreg Kroah-Hartman {
837*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
83803ee2515SGreg Kroah-Hartman 	int result;
83903ee2515SGreg Kroah-Hartman 
84019165b2bSKonstantin Shkolnyy 	result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
8412479e2a9SJohan Hovold 	if (result) {
8422479e2a9SJohan Hovold 		dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
8432479e2a9SJohan Hovold 		return result;
84403ee2515SGreg Kroah-Hartman 	}
84503ee2515SGreg Kroah-Hartman 
84603ee2515SGreg Kroah-Hartman 	/* Configure the termios structure */
8474cc27bd6SCraig Shelley 	cp210x_get_termios(tty, port);
84855b2afbbSJohan Hovold 
849*a7207e98SJohan Hovold 	if (tty) {
850cdc32fd6SJohan Hovold 		/* The baud rate must be initialised on cp2104 */
851cdc32fd6SJohan Hovold 		cp210x_change_speed(tty, port, NULL);
852cdc32fd6SJohan Hovold 
853*a7207e98SJohan Hovold 		if (I_INPCK(tty))
854*a7207e98SJohan Hovold 			cp210x_enable_event_mode(port);
855*a7207e98SJohan Hovold 	}
856*a7207e98SJohan Hovold 
857bcbb9d81SJohan Hovold 	result = usb_serial_generic_open(tty, port);
858bcbb9d81SJohan Hovold 	if (result)
859bcbb9d81SJohan Hovold 		goto err_disable;
860bcbb9d81SJohan Hovold 
861bcbb9d81SJohan Hovold 	return 0;
862bcbb9d81SJohan Hovold 
863bcbb9d81SJohan Hovold err_disable:
864bcbb9d81SJohan Hovold 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
865*a7207e98SJohan Hovold 	port_priv->event_mode = false;
866bcbb9d81SJohan Hovold 
867bcbb9d81SJohan Hovold 	return result;
86803ee2515SGreg Kroah-Hartman }
86903ee2515SGreg Kroah-Hartman 
8704cc27bd6SCraig Shelley static void cp210x_close(struct usb_serial_port *port)
87103ee2515SGreg Kroah-Hartman {
872*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
873*a7207e98SJohan Hovold 
874f26788daSJohan Hovold 	usb_serial_generic_close(port);
875ebfb319bSKonstantin Shkolnyy 
876ebfb319bSKonstantin Shkolnyy 	/* Clear both queues; cp2108 needs this to avoid an occasional hang */
87719165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
878ebfb319bSKonstantin Shkolnyy 
87919165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
880*a7207e98SJohan Hovold 
881*a7207e98SJohan Hovold 	/* Disabling the interface disables event-insertion mode. */
882*a7207e98SJohan Hovold 	port_priv->event_mode = false;
883*a7207e98SJohan Hovold }
884*a7207e98SJohan Hovold 
885*a7207e98SJohan Hovold static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
886*a7207e98SJohan Hovold {
887*a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_BREAK) {
888*a7207e98SJohan Hovold 		port->icount.brk++;
889*a7207e98SJohan Hovold 		*flag = TTY_BREAK;
890*a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_PARITY) {
891*a7207e98SJohan Hovold 		port->icount.parity++;
892*a7207e98SJohan Hovold 		*flag = TTY_PARITY;
893*a7207e98SJohan Hovold 	} else if (lsr & CP210X_LSR_FRAME) {
894*a7207e98SJohan Hovold 		port->icount.frame++;
895*a7207e98SJohan Hovold 		*flag = TTY_FRAME;
896*a7207e98SJohan Hovold 	}
897*a7207e98SJohan Hovold 
898*a7207e98SJohan Hovold 	if (lsr & CP210X_LSR_OVERRUN) {
899*a7207e98SJohan Hovold 		port->icount.overrun++;
900*a7207e98SJohan Hovold 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
901*a7207e98SJohan Hovold 	}
902*a7207e98SJohan Hovold }
903*a7207e98SJohan Hovold 
904*a7207e98SJohan Hovold static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
905*a7207e98SJohan Hovold {
906*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
907*a7207e98SJohan Hovold 
908*a7207e98SJohan Hovold 	switch (port_priv->event_state) {
909*a7207e98SJohan Hovold 	case ES_DATA:
910*a7207e98SJohan Hovold 		if (*ch == CP210X_ESCCHAR) {
911*a7207e98SJohan Hovold 			port_priv->event_state = ES_ESCAPE;
912*a7207e98SJohan Hovold 			break;
913*a7207e98SJohan Hovold 		}
914*a7207e98SJohan Hovold 		return false;
915*a7207e98SJohan Hovold 	case ES_ESCAPE:
916*a7207e98SJohan Hovold 		switch (*ch) {
917*a7207e98SJohan Hovold 		case 0:
918*a7207e98SJohan Hovold 			dev_dbg(&port->dev, "%s - escape char\n", __func__);
919*a7207e98SJohan Hovold 			*ch = CP210X_ESCCHAR;
920*a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
921*a7207e98SJohan Hovold 			return false;
922*a7207e98SJohan Hovold 		case 1:
923*a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR_DATA_0;
924*a7207e98SJohan Hovold 			break;
925*a7207e98SJohan Hovold 		case 2:
926*a7207e98SJohan Hovold 			port_priv->event_state = ES_LSR;
927*a7207e98SJohan Hovold 			break;
928*a7207e98SJohan Hovold 		case 3:
929*a7207e98SJohan Hovold 			port_priv->event_state = ES_MSR;
930*a7207e98SJohan Hovold 			break;
931*a7207e98SJohan Hovold 		default:
932*a7207e98SJohan Hovold 			dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
933*a7207e98SJohan Hovold 			port_priv->event_state = ES_DATA;
934*a7207e98SJohan Hovold 			break;
935*a7207e98SJohan Hovold 		}
936*a7207e98SJohan Hovold 		break;
937*a7207e98SJohan Hovold 	case ES_LSR_DATA_0:
938*a7207e98SJohan Hovold 		port_priv->lsr = *ch;
939*a7207e98SJohan Hovold 		port_priv->event_state = ES_LSR_DATA_1;
940*a7207e98SJohan Hovold 		break;
941*a7207e98SJohan Hovold 	case ES_LSR_DATA_1:
942*a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
943*a7207e98SJohan Hovold 				__func__, port_priv->lsr, *ch);
944*a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
945*a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
946*a7207e98SJohan Hovold 		return false;
947*a7207e98SJohan Hovold 	case ES_LSR:
948*a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
949*a7207e98SJohan Hovold 		port_priv->lsr = *ch;
950*a7207e98SJohan Hovold 		cp210x_process_lsr(port, port_priv->lsr, flag);
951*a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
952*a7207e98SJohan Hovold 		break;
953*a7207e98SJohan Hovold 	case ES_MSR:
954*a7207e98SJohan Hovold 		dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
955*a7207e98SJohan Hovold 		/* unimplemented */
956*a7207e98SJohan Hovold 		port_priv->event_state = ES_DATA;
957*a7207e98SJohan Hovold 		break;
958*a7207e98SJohan Hovold 	}
959*a7207e98SJohan Hovold 
960*a7207e98SJohan Hovold 	return true;
961*a7207e98SJohan Hovold }
962*a7207e98SJohan Hovold 
963*a7207e98SJohan Hovold static void cp210x_process_read_urb(struct urb *urb)
964*a7207e98SJohan Hovold {
965*a7207e98SJohan Hovold 	struct usb_serial_port *port = urb->context;
966*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
967*a7207e98SJohan Hovold 	unsigned char *ch = urb->transfer_buffer;
968*a7207e98SJohan Hovold 	char flag;
969*a7207e98SJohan Hovold 	int i;
970*a7207e98SJohan Hovold 
971*a7207e98SJohan Hovold 	if (!urb->actual_length)
972*a7207e98SJohan Hovold 		return;
973*a7207e98SJohan Hovold 
974*a7207e98SJohan Hovold 	if (port_priv->event_mode) {
975*a7207e98SJohan Hovold 		for (i = 0; i < urb->actual_length; i++, ch++) {
976*a7207e98SJohan Hovold 			flag = TTY_NORMAL;
977*a7207e98SJohan Hovold 
978*a7207e98SJohan Hovold 			if (cp210x_process_char(port, ch, &flag))
979*a7207e98SJohan Hovold 				continue;
980*a7207e98SJohan Hovold 
981*a7207e98SJohan Hovold 			tty_insert_flip_char(&port->port, *ch, flag);
982*a7207e98SJohan Hovold 		}
983*a7207e98SJohan Hovold 	} else {
984*a7207e98SJohan Hovold 		tty_insert_flip_string(&port->port, ch, urb->actual_length);
985*a7207e98SJohan Hovold 	}
986*a7207e98SJohan Hovold 	tty_flip_buffer_push(&port->port);
98703ee2515SGreg Kroah-Hartman }
98803ee2515SGreg Kroah-Hartman 
98903ee2515SGreg Kroah-Hartman /*
9906f923a01SKonstantin Shkolnyy  * Read how many bytes are waiting in the TX queue.
9916f923a01SKonstantin Shkolnyy  */
9926f923a01SKonstantin Shkolnyy static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
9936f923a01SKonstantin Shkolnyy 		u32 *count)
9946f923a01SKonstantin Shkolnyy {
9956f923a01SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
9966f923a01SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
9976f923a01SKonstantin Shkolnyy 	struct cp210x_comm_status *sts;
9986f923a01SKonstantin Shkolnyy 	int result;
9996f923a01SKonstantin Shkolnyy 
10006f923a01SKonstantin Shkolnyy 	sts = kmalloc(sizeof(*sts), GFP_KERNEL);
10016f923a01SKonstantin Shkolnyy 	if (!sts)
10026f923a01SKonstantin Shkolnyy 		return -ENOMEM;
10036f923a01SKonstantin Shkolnyy 
10046f923a01SKonstantin Shkolnyy 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
10056f923a01SKonstantin Shkolnyy 			CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
10066f923a01SKonstantin Shkolnyy 			0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
10076f923a01SKonstantin Shkolnyy 			USB_CTRL_GET_TIMEOUT);
10086f923a01SKonstantin Shkolnyy 	if (result == sizeof(*sts)) {
10096f923a01SKonstantin Shkolnyy 		*count = le32_to_cpu(sts->ulAmountInOutQueue);
10106f923a01SKonstantin Shkolnyy 		result = 0;
10116f923a01SKonstantin Shkolnyy 	} else {
10126f923a01SKonstantin Shkolnyy 		dev_err(&port->dev, "failed to get comm status: %d\n", result);
10136f923a01SKonstantin Shkolnyy 		if (result >= 0)
10140407f1ceSJohan Hovold 			result = -EIO;
10156f923a01SKonstantin Shkolnyy 	}
10166f923a01SKonstantin Shkolnyy 
10176f923a01SKonstantin Shkolnyy 	kfree(sts);
10186f923a01SKonstantin Shkolnyy 
10196f923a01SKonstantin Shkolnyy 	return result;
10206f923a01SKonstantin Shkolnyy }
10216f923a01SKonstantin Shkolnyy 
10226f923a01SKonstantin Shkolnyy static bool cp210x_tx_empty(struct usb_serial_port *port)
10236f923a01SKonstantin Shkolnyy {
10246f923a01SKonstantin Shkolnyy 	int err;
10256f923a01SKonstantin Shkolnyy 	u32 count;
10266f923a01SKonstantin Shkolnyy 
10276f923a01SKonstantin Shkolnyy 	err = cp210x_get_tx_queue_byte_count(port, &count);
10286f923a01SKonstantin Shkolnyy 	if (err)
10296f923a01SKonstantin Shkolnyy 		return true;
10306f923a01SKonstantin Shkolnyy 
10316f923a01SKonstantin Shkolnyy 	return !count;
10326f923a01SKonstantin Shkolnyy }
10336f923a01SKonstantin Shkolnyy 
10346f923a01SKonstantin Shkolnyy /*
10354cc27bd6SCraig Shelley  * cp210x_get_termios
103603ee2515SGreg Kroah-Hartman  * Reads the baud rate, data bits, parity, stop bits and flow control mode
103703ee2515SGreg Kroah-Hartman  * from the device, corrects any unsupported values, and configures the
103803ee2515SGreg Kroah-Hartman  * termios structure to reflect the state of the device
103903ee2515SGreg Kroah-Hartman  */
10404cc27bd6SCraig Shelley static void cp210x_get_termios(struct tty_struct *tty,
1041d2ad67b3SVomLehn 	struct usb_serial_port *port)
104203ee2515SGreg Kroah-Hartman {
1043d2ad67b3SVomLehn 	unsigned int baud;
1044d2ad67b3SVomLehn 
1045d2ad67b3SVomLehn 	if (tty) {
10464cc27bd6SCraig Shelley 		cp210x_get_termios_port(tty->driver_data,
1047adc8d746SAlan Cox 			&tty->termios.c_cflag, &baud);
1048d2ad67b3SVomLehn 		tty_encode_baud_rate(tty, baud, baud);
10494f2ab888SJohan Hovold 	} else {
1050009615abSGeert Uytterhoeven 		tcflag_t cflag;
1051d2ad67b3SVomLehn 		cflag = 0;
10524cc27bd6SCraig Shelley 		cp210x_get_termios_port(port, &cflag, &baud);
1053d2ad67b3SVomLehn 	}
1054d2ad67b3SVomLehn }
1055d2ad67b3SVomLehn 
1056d2ad67b3SVomLehn /*
10574cc27bd6SCraig Shelley  * cp210x_get_termios_port
10584cc27bd6SCraig Shelley  * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
1059d2ad67b3SVomLehn  */
10604cc27bd6SCraig Shelley static void cp210x_get_termios_port(struct usb_serial_port *port,
1061009615abSGeert Uytterhoeven 	tcflag_t *cflagp, unsigned int *baudp)
1062d2ad67b3SVomLehn {
1063660f3b14SGreg Kroah-Hartman 	struct device *dev = &port->dev;
1064009615abSGeert Uytterhoeven 	tcflag_t cflag;
10659034389cSKonstantin Shkolnyy 	struct cp210x_flow_ctl flow_ctl;
1066fe1b07e9SKonstantin Shkolnyy 	u32 baud;
106719165b2bSKonstantin Shkolnyy 	u16 bits;
10689034389cSKonstantin Shkolnyy 	u32 ctl_hs;
1069c7614ff9SBrant Merryman 	u32 flow_repl;
107003ee2515SGreg Kroah-Hartman 
1071fe1b07e9SKonstantin Shkolnyy 	cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
107203ee2515SGreg Kroah-Hartman 
1073660f3b14SGreg Kroah-Hartman 	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
1074d2ad67b3SVomLehn 	*baudp = baud;
107503ee2515SGreg Kroah-Hartman 
1076d2ad67b3SVomLehn 	cflag = *cflagp;
107703ee2515SGreg Kroah-Hartman 
1078d0bf1ff0SKonstantin Shkolnyy 	cp210x_get_line_ctl(port, &bits);
107903ee2515SGreg Kroah-Hartman 	cflag &= ~CSIZE;
108003ee2515SGreg Kroah-Hartman 	switch (bits & BITS_DATA_MASK) {
108103ee2515SGreg Kroah-Hartman 	case BITS_DATA_5:
1082660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 5\n", __func__);
108303ee2515SGreg Kroah-Hartman 		cflag |= CS5;
108403ee2515SGreg Kroah-Hartman 		break;
108503ee2515SGreg Kroah-Hartman 	case BITS_DATA_6:
1086660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 6\n", __func__);
108703ee2515SGreg Kroah-Hartman 		cflag |= CS6;
108803ee2515SGreg Kroah-Hartman 		break;
108903ee2515SGreg Kroah-Hartman 	case BITS_DATA_7:
1090660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 7\n", __func__);
109103ee2515SGreg Kroah-Hartman 		cflag |= CS7;
109203ee2515SGreg Kroah-Hartman 		break;
109303ee2515SGreg Kroah-Hartman 	case BITS_DATA_8:
1094660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 8\n", __func__);
109503ee2515SGreg Kroah-Hartman 		cflag |= CS8;
109603ee2515SGreg Kroah-Hartman 		break;
109703ee2515SGreg Kroah-Hartman 	case BITS_DATA_9:
1098660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
109903ee2515SGreg Kroah-Hartman 		cflag |= CS8;
110003ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
110103ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_8;
110219165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
110303ee2515SGreg Kroah-Hartman 		break;
110403ee2515SGreg Kroah-Hartman 	default:
1105660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
110603ee2515SGreg Kroah-Hartman 		cflag |= CS8;
110703ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
110803ee2515SGreg Kroah-Hartman 		bits |= BITS_DATA_8;
110919165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
111003ee2515SGreg Kroah-Hartman 		break;
111103ee2515SGreg Kroah-Hartman 	}
111203ee2515SGreg Kroah-Hartman 
111303ee2515SGreg Kroah-Hartman 	switch (bits & BITS_PARITY_MASK) {
111403ee2515SGreg Kroah-Hartman 	case BITS_PARITY_NONE:
1115660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = NONE\n", __func__);
111603ee2515SGreg Kroah-Hartman 		cflag &= ~PARENB;
111703ee2515SGreg Kroah-Hartman 		break;
111803ee2515SGreg Kroah-Hartman 	case BITS_PARITY_ODD:
1119660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = ODD\n", __func__);
112003ee2515SGreg Kroah-Hartman 		cflag |= (PARENB|PARODD);
112103ee2515SGreg Kroah-Hartman 		break;
112203ee2515SGreg Kroah-Hartman 	case BITS_PARITY_EVEN:
1123660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = EVEN\n", __func__);
112403ee2515SGreg Kroah-Hartman 		cflag &= ~PARODD;
112503ee2515SGreg Kroah-Hartman 		cflag |= PARENB;
112603ee2515SGreg Kroah-Hartman 		break;
112703ee2515SGreg Kroah-Hartman 	case BITS_PARITY_MARK:
1128660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = MARK\n", __func__);
11294b6181caSRoland Koebler 		cflag |= (PARENB|PARODD|CMSPAR);
113003ee2515SGreg Kroah-Hartman 		break;
113103ee2515SGreg Kroah-Hartman 	case BITS_PARITY_SPACE:
1132660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - parity = SPACE\n", __func__);
11334b6181caSRoland Koebler 		cflag &= ~PARODD;
11344b6181caSRoland Koebler 		cflag |= (PARENB|CMSPAR);
113503ee2515SGreg Kroah-Hartman 		break;
113603ee2515SGreg Kroah-Hartman 	default:
1137660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
113803ee2515SGreg Kroah-Hartman 		cflag &= ~PARENB;
113903ee2515SGreg Kroah-Hartman 		bits &= ~BITS_PARITY_MASK;
114019165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
114103ee2515SGreg Kroah-Hartman 		break;
114203ee2515SGreg Kroah-Hartman 	}
114303ee2515SGreg Kroah-Hartman 
114403ee2515SGreg Kroah-Hartman 	cflag &= ~CSTOPB;
114503ee2515SGreg Kroah-Hartman 	switch (bits & BITS_STOP_MASK) {
114603ee2515SGreg Kroah-Hartman 	case BITS_STOP_1:
1147660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
114803ee2515SGreg Kroah-Hartman 		break;
114903ee2515SGreg Kroah-Hartman 	case BITS_STOP_1_5:
1150660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
115103ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
115219165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
115303ee2515SGreg Kroah-Hartman 		break;
115403ee2515SGreg Kroah-Hartman 	case BITS_STOP_2:
1155660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
115603ee2515SGreg Kroah-Hartman 		cflag |= CSTOPB;
115703ee2515SGreg Kroah-Hartman 		break;
115803ee2515SGreg Kroah-Hartman 	default:
1159660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
116003ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
116119165b2bSKonstantin Shkolnyy 		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
116203ee2515SGreg Kroah-Hartman 		break;
116303ee2515SGreg Kroah-Hartman 	}
116403ee2515SGreg Kroah-Hartman 
11659034389cSKonstantin Shkolnyy 	cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
11669034389cSKonstantin Shkolnyy 			sizeof(flow_ctl));
11679034389cSKonstantin Shkolnyy 	ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
11689034389cSKonstantin Shkolnyy 	if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
1169660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1170c7614ff9SBrant Merryman 		/*
1171c7614ff9SBrant Merryman 		 * When the port is closed, the CP210x hardware disables
1172c7614ff9SBrant Merryman 		 * auto-RTS and RTS is deasserted but it leaves auto-CTS when
1173c7614ff9SBrant Merryman 		 * in hardware flow control mode. When re-opening the port, if
1174c7614ff9SBrant Merryman 		 * auto-CTS is enabled on the cp210x, then auto-RTS must be
1175c7614ff9SBrant Merryman 		 * re-enabled in the driver.
1176c7614ff9SBrant Merryman 		 */
1177c7614ff9SBrant Merryman 		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1178c7614ff9SBrant Merryman 		flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1179c7614ff9SBrant Merryman 		flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
1180c7614ff9SBrant Merryman 		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1181c7614ff9SBrant Merryman 		cp210x_write_reg_block(port,
1182c7614ff9SBrant Merryman 				CP210X_SET_FLOW,
1183c7614ff9SBrant Merryman 				&flow_ctl,
1184c7614ff9SBrant Merryman 				sizeof(flow_ctl));
1185c7614ff9SBrant Merryman 
118603ee2515SGreg Kroah-Hartman 		cflag |= CRTSCTS;
118703ee2515SGreg Kroah-Hartman 	} else {
1188660f3b14SGreg Kroah-Hartman 		dev_dbg(dev, "%s - flow control = NONE\n", __func__);
118903ee2515SGreg Kroah-Hartman 		cflag &= ~CRTSCTS;
119003ee2515SGreg Kroah-Hartman 	}
119103ee2515SGreg Kroah-Hartman 
1192d2ad67b3SVomLehn 	*cflagp = cflag;
119303ee2515SGreg Kroah-Hartman }
119403ee2515SGreg Kroah-Hartman 
119595fd4f47SJohan Hovold struct cp210x_rate {
119695fd4f47SJohan Hovold 	speed_t rate;
119795fd4f47SJohan Hovold 	speed_t high;
119895fd4f47SJohan Hovold };
119995fd4f47SJohan Hovold 
120095fd4f47SJohan Hovold static const struct cp210x_rate cp210x_an205_table1[] = {
120195fd4f47SJohan Hovold 	{ 300, 300 },
120295fd4f47SJohan Hovold 	{ 600, 600 },
120395fd4f47SJohan Hovold 	{ 1200, 1200 },
120495fd4f47SJohan Hovold 	{ 1800, 1800 },
120595fd4f47SJohan Hovold 	{ 2400, 2400 },
120695fd4f47SJohan Hovold 	{ 4000, 4000 },
120795fd4f47SJohan Hovold 	{ 4800, 4803 },
120895fd4f47SJohan Hovold 	{ 7200, 7207 },
120995fd4f47SJohan Hovold 	{ 9600, 9612 },
121095fd4f47SJohan Hovold 	{ 14400, 14428 },
121195fd4f47SJohan Hovold 	{ 16000, 16062 },
121295fd4f47SJohan Hovold 	{ 19200, 19250 },
121395fd4f47SJohan Hovold 	{ 28800, 28912 },
121495fd4f47SJohan Hovold 	{ 38400, 38601 },
121595fd4f47SJohan Hovold 	{ 51200, 51558 },
121695fd4f47SJohan Hovold 	{ 56000, 56280 },
121795fd4f47SJohan Hovold 	{ 57600, 58053 },
121895fd4f47SJohan Hovold 	{ 64000, 64111 },
121995fd4f47SJohan Hovold 	{ 76800, 77608 },
122095fd4f47SJohan Hovold 	{ 115200, 117028 },
122195fd4f47SJohan Hovold 	{ 128000, 129347 },
122295fd4f47SJohan Hovold 	{ 153600, 156868 },
122395fd4f47SJohan Hovold 	{ 230400, 237832 },
122495fd4f47SJohan Hovold 	{ 250000, 254234 },
122595fd4f47SJohan Hovold 	{ 256000, 273066 },
122695fd4f47SJohan Hovold 	{ 460800, 491520 },
122795fd4f47SJohan Hovold 	{ 500000, 567138 },
122895fd4f47SJohan Hovold 	{ 576000, 670254 },
122995fd4f47SJohan Hovold 	{ 921600, UINT_MAX }
123095fd4f47SJohan Hovold };
123195fd4f47SJohan Hovold 
123295fd4f47SJohan Hovold /*
123395fd4f47SJohan Hovold  * Quantises the baud rate as per AN205 Table 1
123495fd4f47SJohan Hovold  */
123595fd4f47SJohan Hovold static speed_t cp210x_get_an205_rate(speed_t baud)
123695fd4f47SJohan Hovold {
123795fd4f47SJohan Hovold 	int i;
123895fd4f47SJohan Hovold 
123995fd4f47SJohan Hovold 	for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
124095fd4f47SJohan Hovold 		if (baud <= cp210x_an205_table1[i].high)
124195fd4f47SJohan Hovold 			break;
124295fd4f47SJohan Hovold 	}
124395fd4f47SJohan Hovold 
124495fd4f47SJohan Hovold 	return cp210x_an205_table1[i].rate;
124595fd4f47SJohan Hovold }
124695fd4f47SJohan Hovold 
124785bc2d91SJohanna Abrahamsson static speed_t cp210x_get_actual_rate(speed_t baud)
12487aecd7fcSJohan Hovold {
12497aecd7fcSJohan Hovold 	unsigned int prescale = 1;
12507aecd7fcSJohan Hovold 	unsigned int div;
12517aecd7fcSJohan Hovold 
12527aecd7fcSJohan Hovold 	if (baud <= 365)
12537aecd7fcSJohan Hovold 		prescale = 4;
12547aecd7fcSJohan Hovold 
12557aecd7fcSJohan Hovold 	div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
12567aecd7fcSJohan Hovold 	baud = 48000000 / (2 * prescale * div);
12577aecd7fcSJohan Hovold 
12587aecd7fcSJohan Hovold 	return baud;
12597aecd7fcSJohan Hovold }
12607aecd7fcSJohan Hovold 
1261e5990874SJohan Hovold /*
1262e5990874SJohan Hovold  * CP2101 supports the following baud rates:
1263e5990874SJohan Hovold  *
1264e5990874SJohan Hovold  *	300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1265e5990874SJohan Hovold  *	38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1266e5990874SJohan Hovold  *
1267e5990874SJohan Hovold  * CP2102 and CP2103 support the following additional rates:
1268e5990874SJohan Hovold  *
1269e5990874SJohan Hovold  *	4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1270e5990874SJohan Hovold  *	576000
1271e5990874SJohan Hovold  *
1272e5990874SJohan Hovold  * The device will map a requested rate to a supported one, but the result
1273e5990874SJohan Hovold  * of requests for rates greater than 1053257 is undefined (see AN205).
1274e5990874SJohan Hovold  *
1275e5990874SJohan Hovold  * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1276e5990874SJohan Hovold  * respectively, with an error less than 1%. The actual rates are determined
1277e5990874SJohan Hovold  * by
1278e5990874SJohan Hovold  *
1279e5990874SJohan Hovold  *	div = round(freq / (2 x prescale x request))
1280e5990874SJohan Hovold  *	actual = freq / (2 x prescale x div)
1281e5990874SJohan Hovold  *
1282e5990874SJohan Hovold  * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1283e5990874SJohan Hovold  * or 1 otherwise.
1284e5990874SJohan Hovold  * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1285e5990874SJohan Hovold  * otherwise.
1286e5990874SJohan Hovold  */
1287e5990874SJohan Hovold static void cp210x_change_speed(struct tty_struct *tty,
1288e5990874SJohan Hovold 		struct usb_serial_port *port, struct ktermios *old_termios)
1289e5990874SJohan Hovold {
12906f0bcf72SKaroly Pados 	struct usb_serial *serial = port->serial;
12916f0bcf72SKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1292e5990874SJohan Hovold 	u32 baud;
1293e5990874SJohan Hovold 
12946f0bcf72SKaroly Pados 	/*
12957aecd7fcSJohan Hovold 	 * This maps the requested rate to the actual rate, a valid rate on
12967aecd7fcSJohan Hovold 	 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
1297e5990874SJohan Hovold 	 *
1298e5990874SJohan Hovold 	 * NOTE: B0 is not implemented.
1299e5990874SJohan Hovold 	 */
130085bc2d91SJohanna Abrahamsson 	baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
130185bc2d91SJohanna Abrahamsson 
13027aecd7fcSJohan Hovold 	if (priv->use_actual_rate)
130385bc2d91SJohanna Abrahamsson 		baud = cp210x_get_actual_rate(baud);
13047aecd7fcSJohan Hovold 	else if (baud < 1000000)
130595fd4f47SJohan Hovold 		baud = cp210x_get_an205_rate(baud);
1306e5990874SJohan Hovold 
1307660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
1308fe1b07e9SKonstantin Shkolnyy 	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
1309e5990874SJohan Hovold 		dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1310e5990874SJohan Hovold 		if (old_termios)
1311e5990874SJohan Hovold 			baud = old_termios->c_ospeed;
1312e5990874SJohan Hovold 		else
1313e5990874SJohan Hovold 			baud = 9600;
1314e5990874SJohan Hovold 	}
1315e5990874SJohan Hovold 
1316e5990874SJohan Hovold 	tty_encode_baud_rate(tty, baud, baud);
1317e5990874SJohan Hovold }
1318e5990874SJohan Hovold 
1319*a7207e98SJohan Hovold static void cp210x_enable_event_mode(struct usb_serial_port *port)
1320*a7207e98SJohan Hovold {
1321*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1322*a7207e98SJohan Hovold 	int ret;
1323*a7207e98SJohan Hovold 
1324*a7207e98SJohan Hovold 	if (port_priv->event_mode)
1325*a7207e98SJohan Hovold 		return;
1326*a7207e98SJohan Hovold 
1327*a7207e98SJohan Hovold 	port_priv->event_state = ES_DATA;
1328*a7207e98SJohan Hovold 	port_priv->event_mode = true;
1329*a7207e98SJohan Hovold 
1330*a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1331*a7207e98SJohan Hovold 	if (ret) {
1332*a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to enable events: %d\n", ret);
1333*a7207e98SJohan Hovold 		port_priv->event_mode = false;
1334*a7207e98SJohan Hovold 	}
1335*a7207e98SJohan Hovold }
1336*a7207e98SJohan Hovold 
1337*a7207e98SJohan Hovold static void cp210x_disable_event_mode(struct usb_serial_port *port)
1338*a7207e98SJohan Hovold {
1339*a7207e98SJohan Hovold 	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1340*a7207e98SJohan Hovold 	int ret;
1341*a7207e98SJohan Hovold 
1342*a7207e98SJohan Hovold 	if (!port_priv->event_mode)
1343*a7207e98SJohan Hovold 		return;
1344*a7207e98SJohan Hovold 
1345*a7207e98SJohan Hovold 	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
1346*a7207e98SJohan Hovold 	if (ret) {
1347*a7207e98SJohan Hovold 		dev_err(&port->dev, "failed to disable events: %d\n", ret);
1348*a7207e98SJohan Hovold 		return;
1349*a7207e98SJohan Hovold 	}
1350*a7207e98SJohan Hovold 
1351*a7207e98SJohan Hovold 	port_priv->event_mode = false;
1352*a7207e98SJohan Hovold }
1353*a7207e98SJohan Hovold 
13544cc27bd6SCraig Shelley static void cp210x_set_termios(struct tty_struct *tty,
135503ee2515SGreg Kroah-Hartman 		struct usb_serial_port *port, struct ktermios *old_termios)
135603ee2515SGreg Kroah-Hartman {
1357660f3b14SGreg Kroah-Hartman 	struct device *dev = &port->dev;
135803ee2515SGreg Kroah-Hartman 	unsigned int cflag, old_cflag;
135919165b2bSKonstantin Shkolnyy 	u16 bits;
136003ee2515SGreg Kroah-Hartman 
1361adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
136203ee2515SGreg Kroah-Hartman 	old_cflag = old_termios->c_cflag;
136303ee2515SGreg Kroah-Hartman 
1364adc8d746SAlan Cox 	if (tty->termios.c_ospeed != old_termios->c_ospeed)
1365e5990874SJohan Hovold 		cp210x_change_speed(tty, port, old_termios);
136603ee2515SGreg Kroah-Hartman 
136703ee2515SGreg Kroah-Hartman 	/* If the number of data bits is to be updated */
136803ee2515SGreg Kroah-Hartman 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
1369d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
137003ee2515SGreg Kroah-Hartman 		bits &= ~BITS_DATA_MASK;
137103ee2515SGreg Kroah-Hartman 		switch (cflag & CSIZE) {
137203ee2515SGreg Kroah-Hartman 		case CS5:
137303ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_5;
1374660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 5\n", __func__);
137503ee2515SGreg Kroah-Hartman 			break;
137603ee2515SGreg Kroah-Hartman 		case CS6:
137703ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_6;
1378660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 6\n", __func__);
137903ee2515SGreg Kroah-Hartman 			break;
138003ee2515SGreg Kroah-Hartman 		case CS7:
138103ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_7;
1382660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 7\n", __func__);
138303ee2515SGreg Kroah-Hartman 			break;
138403ee2515SGreg Kroah-Hartman 		case CS8:
1385c2b33559SJohan Hovold 		default:
138603ee2515SGreg Kroah-Hartman 			bits |= BITS_DATA_8;
1387660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - data bits = 8\n", __func__);
138803ee2515SGreg Kroah-Hartman 			break;
138903ee2515SGreg Kroah-Hartman 		}
139019165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1391660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Number of data bits requested not supported by device\n");
139203ee2515SGreg Kroah-Hartman 	}
139303ee2515SGreg Kroah-Hartman 
13944b6181caSRoland Koebler 	if ((cflag     & (PARENB|PARODD|CMSPAR)) !=
13954b6181caSRoland Koebler 	    (old_cflag & (PARENB|PARODD|CMSPAR))) {
1396d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
139703ee2515SGreg Kroah-Hartman 		bits &= ~BITS_PARITY_MASK;
139803ee2515SGreg Kroah-Hartman 		if (cflag & PARENB) {
13994b6181caSRoland Koebler 			if (cflag & CMSPAR) {
14004b6181caSRoland Koebler 				if (cflag & PARODD) {
14014b6181caSRoland Koebler 					bits |= BITS_PARITY_MARK;
1402660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = MARK\n", __func__);
14034b6181caSRoland Koebler 				} else {
14044b6181caSRoland Koebler 					bits |= BITS_PARITY_SPACE;
1405660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = SPACE\n", __func__);
14064b6181caSRoland Koebler 				}
14074b6181caSRoland Koebler 			} else {
140803ee2515SGreg Kroah-Hartman 				if (cflag & PARODD) {
140903ee2515SGreg Kroah-Hartman 					bits |= BITS_PARITY_ODD;
1410660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = ODD\n", __func__);
141103ee2515SGreg Kroah-Hartman 				} else {
141203ee2515SGreg Kroah-Hartman 					bits |= BITS_PARITY_EVEN;
1413660f3b14SGreg Kroah-Hartman 					dev_dbg(dev, "%s - parity = EVEN\n", __func__);
141403ee2515SGreg Kroah-Hartman 				}
141503ee2515SGreg Kroah-Hartman 			}
14164b6181caSRoland Koebler 		}
141719165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1418660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Parity mode not supported by device\n");
141903ee2515SGreg Kroah-Hartman 	}
142003ee2515SGreg Kroah-Hartman 
142103ee2515SGreg Kroah-Hartman 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1422d0bf1ff0SKonstantin Shkolnyy 		cp210x_get_line_ctl(port, &bits);
142303ee2515SGreg Kroah-Hartman 		bits &= ~BITS_STOP_MASK;
142403ee2515SGreg Kroah-Hartman 		if (cflag & CSTOPB) {
142503ee2515SGreg Kroah-Hartman 			bits |= BITS_STOP_2;
1426660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - stop bits = 2\n", __func__);
142703ee2515SGreg Kroah-Hartman 		} else {
142803ee2515SGreg Kroah-Hartman 			bits |= BITS_STOP_1;
1429660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - stop bits = 1\n", __func__);
143003ee2515SGreg Kroah-Hartman 		}
143119165b2bSKonstantin Shkolnyy 		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1432660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "Number of stop bits requested not supported by device\n");
143303ee2515SGreg Kroah-Hartman 	}
143403ee2515SGreg Kroah-Hartman 
143503ee2515SGreg Kroah-Hartman 	if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
14369034389cSKonstantin Shkolnyy 		struct cp210x_flow_ctl flow_ctl;
14379034389cSKonstantin Shkolnyy 		u32 ctl_hs;
14389034389cSKonstantin Shkolnyy 		u32 flow_repl;
14397084fa86SKonstantin Shkolnyy 
14409034389cSKonstantin Shkolnyy 		cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
14419034389cSKonstantin Shkolnyy 				sizeof(flow_ctl));
14429034389cSKonstantin Shkolnyy 		ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
14439034389cSKonstantin Shkolnyy 		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
14449034389cSKonstantin Shkolnyy 		dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14459034389cSKonstantin Shkolnyy 				__func__, ctl_hs, flow_repl);
14467084fa86SKonstantin Shkolnyy 
1447ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1448ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1449ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1450ab5701adSKonstantin Shkolnyy 		ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1451ab5701adSKonstantin Shkolnyy 		ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
145203ee2515SGreg Kroah-Hartman 		if (cflag & CRTSCTS) {
14539034389cSKonstantin Shkolnyy 			ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1454ab5701adSKonstantin Shkolnyy 
1455ab5701adSKonstantin Shkolnyy 			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14569034389cSKonstantin Shkolnyy 			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14579034389cSKonstantin Shkolnyy 					CP210X_SERIAL_RTS_FLOW_CTL);
1458660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
145903ee2515SGreg Kroah-Hartman 		} else {
1460ab5701adSKonstantin Shkolnyy 			ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1461ab5701adSKonstantin Shkolnyy 
1462ab5701adSKonstantin Shkolnyy 			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14639034389cSKonstantin Shkolnyy 			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14649034389cSKonstantin Shkolnyy 					CP210X_SERIAL_RTS_ACTIVE);
1465660f3b14SGreg Kroah-Hartman 			dev_dbg(dev, "%s - flow control = NONE\n", __func__);
146603ee2515SGreg Kroah-Hartman 		}
146703ee2515SGreg Kroah-Hartman 
14689034389cSKonstantin Shkolnyy 		dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14699034389cSKonstantin Shkolnyy 				__func__, ctl_hs, flow_repl);
14709034389cSKonstantin Shkolnyy 		flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
14719034389cSKonstantin Shkolnyy 		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
14729034389cSKonstantin Shkolnyy 		cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
14739034389cSKonstantin Shkolnyy 				sizeof(flow_ctl));
147403ee2515SGreg Kroah-Hartman 	}
147503ee2515SGreg Kroah-Hartman 
1476*a7207e98SJohan Hovold 	/*
1477*a7207e98SJohan Hovold 	 * Enable event-insertion mode only if input parity checking is
1478*a7207e98SJohan Hovold 	 * enabled for now.
1479*a7207e98SJohan Hovold 	 */
1480*a7207e98SJohan Hovold 	if (I_INPCK(tty))
1481*a7207e98SJohan Hovold 		cp210x_enable_event_mode(port);
1482*a7207e98SJohan Hovold 	else
1483*a7207e98SJohan Hovold 		cp210x_disable_event_mode(port);
148403ee2515SGreg Kroah-Hartman }
148503ee2515SGreg Kroah-Hartman 
148620b9d177SAlan Cox static int cp210x_tiocmset(struct tty_struct *tty,
148703ee2515SGreg Kroah-Hartman 		unsigned int set, unsigned int clear)
148803ee2515SGreg Kroah-Hartman {
148903ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
149020b9d177SAlan Cox 	return cp210x_tiocmset_port(port, set, clear);
1491d2ad67b3SVomLehn }
1492d2ad67b3SVomLehn 
149320b9d177SAlan Cox static int cp210x_tiocmset_port(struct usb_serial_port *port,
1494d2ad67b3SVomLehn 		unsigned int set, unsigned int clear)
1495d2ad67b3SVomLehn {
149619165b2bSKonstantin Shkolnyy 	u16 control = 0;
149703ee2515SGreg Kroah-Hartman 
149803ee2515SGreg Kroah-Hartman 	if (set & TIOCM_RTS) {
149903ee2515SGreg Kroah-Hartman 		control |= CONTROL_RTS;
150003ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
150103ee2515SGreg Kroah-Hartman 	}
150203ee2515SGreg Kroah-Hartman 	if (set & TIOCM_DTR) {
150303ee2515SGreg Kroah-Hartman 		control |= CONTROL_DTR;
150403ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
150503ee2515SGreg Kroah-Hartman 	}
150603ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_RTS) {
150703ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_RTS;
150803ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_RTS;
150903ee2515SGreg Kroah-Hartman 	}
151003ee2515SGreg Kroah-Hartman 	if (clear & TIOCM_DTR) {
151103ee2515SGreg Kroah-Hartman 		control &= ~CONTROL_DTR;
151203ee2515SGreg Kroah-Hartman 		control |= CONTROL_WRITE_DTR;
151303ee2515SGreg Kroah-Hartman 	}
151403ee2515SGreg Kroah-Hartman 
1515660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
151603ee2515SGreg Kroah-Hartman 
151719165b2bSKonstantin Shkolnyy 	return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
151803ee2515SGreg Kroah-Hartman }
151903ee2515SGreg Kroah-Hartman 
1520d94c7bd4SAlan Cox static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1521d94c7bd4SAlan Cox {
1522d94c7bd4SAlan Cox 	if (on)
152320b9d177SAlan Cox 		cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
1524d94c7bd4SAlan Cox 	else
152520b9d177SAlan Cox 		cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
1526d94c7bd4SAlan Cox }
1527d94c7bd4SAlan Cox 
152860b33c13SAlan Cox static int cp210x_tiocmget(struct tty_struct *tty)
152903ee2515SGreg Kroah-Hartman {
153003ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
1531fe1b07e9SKonstantin Shkolnyy 	u8 control;
153203ee2515SGreg Kroah-Hartman 	int result;
153303ee2515SGreg Kroah-Hartman 
1534de24e0a1SJohan Hovold 	result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1535de24e0a1SJohan Hovold 	if (result)
1536de24e0a1SJohan Hovold 		return result;
153703ee2515SGreg Kroah-Hartman 
153803ee2515SGreg Kroah-Hartman 	result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
153903ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RTS) ? TIOCM_RTS : 0)
154003ee2515SGreg Kroah-Hartman 		|((control & CONTROL_CTS) ? TIOCM_CTS : 0)
154103ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DSR) ? TIOCM_DSR : 0)
154203ee2515SGreg Kroah-Hartman 		|((control & CONTROL_RING)? TIOCM_RI  : 0)
154303ee2515SGreg Kroah-Hartman 		|((control & CONTROL_DCD) ? TIOCM_CD  : 0);
154403ee2515SGreg Kroah-Hartman 
1545660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
154603ee2515SGreg Kroah-Hartman 
154703ee2515SGreg Kroah-Hartman 	return result;
154803ee2515SGreg Kroah-Hartman }
154903ee2515SGreg Kroah-Hartman 
15504cc27bd6SCraig Shelley static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
155103ee2515SGreg Kroah-Hartman {
155203ee2515SGreg Kroah-Hartman 	struct usb_serial_port *port = tty->driver_data;
155319165b2bSKonstantin Shkolnyy 	u16 state;
155403ee2515SGreg Kroah-Hartman 
155503ee2515SGreg Kroah-Hartman 	if (break_state == 0)
155603ee2515SGreg Kroah-Hartman 		state = BREAK_OFF;
155703ee2515SGreg Kroah-Hartman 	else
155803ee2515SGreg Kroah-Hartman 		state = BREAK_ON;
1559660f3b14SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
156003ee2515SGreg Kroah-Hartman 		state == BREAK_OFF ? "off" : "on");
156119165b2bSKonstantin Shkolnyy 	cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
156203ee2515SGreg Kroah-Hartman }
156303ee2515SGreg Kroah-Hartman 
1564cf5276ceSMartyn Welch #ifdef CONFIG_GPIOLIB
1565cf5276ceSMartyn Welch static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
1566cf5276ceSMartyn Welch {
1567cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1568cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1569cf5276ceSMartyn Welch 
1570c8acfe0aSKaroly Pados 	if (priv->gpio_altfunc & BIT(offset))
1571cf5276ceSMartyn Welch 		return -ENODEV;
1572cf5276ceSMartyn Welch 
1573cf5276ceSMartyn Welch 	return 0;
1574cf5276ceSMartyn Welch }
1575cf5276ceSMartyn Welch 
1576cf5276ceSMartyn Welch static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1577cf5276ceSMartyn Welch {
1578cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1579c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1580c8acfe0aSKaroly Pados 	u8 req_type = REQTYPE_DEVICE_TO_HOST;
1581cf5276ceSMartyn Welch 	int result;
1582cf5276ceSMartyn Welch 	u8 buf;
1583cf5276ceSMartyn Welch 
1584c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105)
1585c8acfe0aSKaroly Pados 		req_type = REQTYPE_INTERFACE_TO_HOST;
1586c8acfe0aSKaroly Pados 
15877b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
15887b0b644bSKaroly Pados 	if (result)
15897b0b644bSKaroly Pados 		return result;
15907b0b644bSKaroly Pados 
1591c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial, req_type,
1592cf5276ceSMartyn Welch 					  CP210X_READ_LATCH, &buf, sizeof(buf));
15937b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
1594cf5276ceSMartyn Welch 	if (result < 0)
1595cf5276ceSMartyn Welch 		return result;
1596cf5276ceSMartyn Welch 
1597cf5276ceSMartyn Welch 	return !!(buf & BIT(gpio));
1598cf5276ceSMartyn Welch }
1599cf5276ceSMartyn Welch 
1600cf5276ceSMartyn Welch static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1601cf5276ceSMartyn Welch {
1602cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1603c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1604cf5276ceSMartyn Welch 	struct cp210x_gpio_write buf;
1605c8acfe0aSKaroly Pados 	int result;
1606cf5276ceSMartyn Welch 
1607cf5276ceSMartyn Welch 	if (value == 1)
1608cf5276ceSMartyn Welch 		buf.state = BIT(gpio);
1609cf5276ceSMartyn Welch 	else
1610cf5276ceSMartyn Welch 		buf.state = 0;
1611cf5276ceSMartyn Welch 
1612cf5276ceSMartyn Welch 	buf.mask = BIT(gpio);
1613cf5276ceSMartyn Welch 
16147b0b644bSKaroly Pados 	result = usb_autopm_get_interface(serial->interface);
16157b0b644bSKaroly Pados 	if (result)
16167b0b644bSKaroly Pados 		goto out;
16177b0b644bSKaroly Pados 
1618c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1619c8acfe0aSKaroly Pados 		result = cp210x_write_vendor_block(serial,
1620c8acfe0aSKaroly Pados 						   REQTYPE_HOST_TO_INTERFACE,
1621c8acfe0aSKaroly Pados 						   CP210X_WRITE_LATCH, &buf,
1622c8acfe0aSKaroly Pados 						   sizeof(buf));
1623c8acfe0aSKaroly Pados 	} else {
1624c8acfe0aSKaroly Pados 		u16 wIndex = buf.state << 8 | buf.mask;
1625c8acfe0aSKaroly Pados 
1626c8acfe0aSKaroly Pados 		result = usb_control_msg(serial->dev,
1627c8acfe0aSKaroly Pados 					 usb_sndctrlpipe(serial->dev, 0),
1628c8acfe0aSKaroly Pados 					 CP210X_VENDOR_SPECIFIC,
1629c8acfe0aSKaroly Pados 					 REQTYPE_HOST_TO_DEVICE,
1630c8acfe0aSKaroly Pados 					 CP210X_WRITE_LATCH,
1631c8acfe0aSKaroly Pados 					 wIndex,
1632c8acfe0aSKaroly Pados 					 NULL, 0, USB_CTRL_SET_TIMEOUT);
1633c8acfe0aSKaroly Pados 	}
1634c8acfe0aSKaroly Pados 
16357b0b644bSKaroly Pados 	usb_autopm_put_interface(serial->interface);
16367b0b644bSKaroly Pados out:
1637c8acfe0aSKaroly Pados 	if (result < 0) {
1638c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
1639c8acfe0aSKaroly Pados 				result);
1640c8acfe0aSKaroly Pados 	}
1641cf5276ceSMartyn Welch }
1642cf5276ceSMartyn Welch 
1643cf5276ceSMartyn Welch static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1644cf5276ceSMartyn Welch {
1645c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1646c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1647c8acfe0aSKaroly Pados 
1648c8acfe0aSKaroly Pados 	return priv->gpio_input & BIT(gpio);
1649cf5276ceSMartyn Welch }
1650cf5276ceSMartyn Welch 
1651cf5276ceSMartyn Welch static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1652cf5276ceSMartyn Welch {
1653c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1654c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1655c8acfe0aSKaroly Pados 
1656c8acfe0aSKaroly Pados 	if (priv->partnum == CP210X_PARTNUM_CP2105) {
1657c8acfe0aSKaroly Pados 		/* hardware does not support an input mode */
1658cf5276ceSMartyn Welch 		return -ENOTSUPP;
1659cf5276ceSMartyn Welch 	}
1660cf5276ceSMartyn Welch 
1661c8acfe0aSKaroly Pados 	/* push-pull pins cannot be changed to be inputs */
1662c8acfe0aSKaroly Pados 	if (priv->gpio_pushpull & BIT(gpio))
1663c8acfe0aSKaroly Pados 		return -EINVAL;
1664c8acfe0aSKaroly Pados 
1665c8acfe0aSKaroly Pados 	/* make sure to release pin if it is being driven low */
1666c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, 1);
1667c8acfe0aSKaroly Pados 
1668c8acfe0aSKaroly Pados 	priv->gpio_input |= BIT(gpio);
1669c8acfe0aSKaroly Pados 
1670c8acfe0aSKaroly Pados 	return 0;
1671c8acfe0aSKaroly Pados }
1672c8acfe0aSKaroly Pados 
1673cf5276ceSMartyn Welch static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1674cf5276ceSMartyn Welch 					int value)
1675cf5276ceSMartyn Welch {
1676c8acfe0aSKaroly Pados 	struct usb_serial *serial = gpiochip_get_data(gc);
1677c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1678c8acfe0aSKaroly Pados 
1679c8acfe0aSKaroly Pados 	priv->gpio_input &= ~BIT(gpio);
1680c8acfe0aSKaroly Pados 	cp210x_gpio_set(gc, gpio, value);
1681c8acfe0aSKaroly Pados 
1682cf5276ceSMartyn Welch 	return 0;
1683cf5276ceSMartyn Welch }
1684cf5276ceSMartyn Welch 
16852956b5d9SMika Westerberg static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
16862956b5d9SMika Westerberg 				  unsigned long config)
1687cf5276ceSMartyn Welch {
1688cf5276ceSMartyn Welch 	struct usb_serial *serial = gpiochip_get_data(gc);
1689cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
16902956b5d9SMika Westerberg 	enum pin_config_param param = pinconf_to_config_param(config);
1691cf5276ceSMartyn Welch 
1692cf5276ceSMartyn Welch 	/* Succeed only if in correct mode (this can't be set at runtime) */
16932956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
1694c8acfe0aSKaroly Pados 	    (priv->gpio_pushpull & BIT(gpio)))
1695cf5276ceSMartyn Welch 		return 0;
1696cf5276ceSMartyn Welch 
16972956b5d9SMika Westerberg 	if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
1698c8acfe0aSKaroly Pados 	    !(priv->gpio_pushpull & BIT(gpio)))
1699cf5276ceSMartyn Welch 		return 0;
1700cf5276ceSMartyn Welch 
1701cf5276ceSMartyn Welch 	return -ENOTSUPP;
1702cf5276ceSMartyn Welch }
1703cf5276ceSMartyn Welch 
1704cf5276ceSMartyn Welch /*
1705cf5276ceSMartyn Welch  * This function is for configuring GPIO using shared pins, where other signals
1706cf5276ceSMartyn Welch  * are made unavailable by configuring the use of GPIO. This is believed to be
1707cf5276ceSMartyn Welch  * only applicable to the cp2105 at this point, the other devices supported by
1708cf5276ceSMartyn Welch  * this driver that provide GPIO do so in a way that does not impact other
1709cf5276ceSMartyn Welch  * signals and are thus expected to have very different initialisation.
1710cf5276ceSMartyn Welch  */
1711c8acfe0aSKaroly Pados static int cp2105_gpioconf_init(struct usb_serial *serial)
1712cf5276ceSMartyn Welch {
1713cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1714cf5276ceSMartyn Welch 	struct cp210x_pin_mode mode;
171515fb84b7SIcenowy Zheng 	struct cp210x_dual_port_config config;
1716cf5276ceSMartyn Welch 	u8 intf_num = cp210x_interface_num(serial);
1717c8acfe0aSKaroly Pados 	u8 iface_config;
1718cf5276ceSMartyn Welch 	int result;
1719cf5276ceSMartyn Welch 
1720cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1721cf5276ceSMartyn Welch 					  CP210X_GET_DEVICEMODE, &mode,
1722cf5276ceSMartyn Welch 					  sizeof(mode));
1723cf5276ceSMartyn Welch 	if (result < 0)
1724cf5276ceSMartyn Welch 		return result;
1725cf5276ceSMartyn Welch 
1726cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1727cf5276ceSMartyn Welch 					  CP210X_GET_PORTCONFIG, &config,
1728cf5276ceSMartyn Welch 					  sizeof(config));
1729cf5276ceSMartyn Welch 	if (result < 0)
1730cf5276ceSMartyn Welch 		return result;
1731cf5276ceSMartyn Welch 
1732cf5276ceSMartyn Welch 	/*  2 banks of GPIO - One for the pins taken from each serial port */
1733cf5276ceSMartyn Welch 	if (intf_num == 0) {
1734c8acfe0aSKaroly Pados 		if (mode.eci == CP210X_PIN_MODE_MODEM) {
1735c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1736c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1737cf5276ceSMartyn Welch 			return 0;
1738c8acfe0aSKaroly Pados 		}
1739cf5276ceSMartyn Welch 
1740c8acfe0aSKaroly Pados 		iface_config = config.eci_cfg;
1741c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1742cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_MASK) >>
1743cf5276ceSMartyn Welch 						CP210X_ECI_GPIO_MODE_OFFSET);
1744cf5276ceSMartyn Welch 		priv->gc.ngpio = 2;
1745cf5276ceSMartyn Welch 	} else if (intf_num == 1) {
1746c8acfe0aSKaroly Pados 		if (mode.sci == CP210X_PIN_MODE_MODEM) {
1747c8acfe0aSKaroly Pados 			/* mark all GPIOs of this interface as reserved */
1748c8acfe0aSKaroly Pados 			priv->gpio_altfunc = 0xff;
1749cf5276ceSMartyn Welch 			return 0;
1750c8acfe0aSKaroly Pados 		}
1751cf5276ceSMartyn Welch 
1752c8acfe0aSKaroly Pados 		iface_config = config.sci_cfg;
1753c8acfe0aSKaroly Pados 		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1754cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_MASK) >>
1755cf5276ceSMartyn Welch 						CP210X_SCI_GPIO_MODE_OFFSET);
1756cf5276ceSMartyn Welch 		priv->gc.ngpio = 3;
1757cf5276ceSMartyn Welch 	} else {
1758cf5276ceSMartyn Welch 		return -ENODEV;
1759cf5276ceSMartyn Welch 	}
1760cf5276ceSMartyn Welch 
1761c8acfe0aSKaroly Pados 	/* mark all pins which are not in GPIO mode */
1762c8acfe0aSKaroly Pados 	if (iface_config & CP2105_GPIO0_TXLED_MODE)	/* GPIO 0 */
1763c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(0);
1764c8acfe0aSKaroly Pados 	if (iface_config & (CP2105_GPIO1_RXLED_MODE |	/* GPIO 1 */
1765c8acfe0aSKaroly Pados 			CP2105_GPIO1_RS485_MODE))
1766c8acfe0aSKaroly Pados 		priv->gpio_altfunc |= BIT(1);
1767c8acfe0aSKaroly Pados 
1768c8acfe0aSKaroly Pados 	/* driver implementation for CP2105 only supports outputs */
1769c8acfe0aSKaroly Pados 	priv->gpio_input = 0;
1770c8acfe0aSKaroly Pados 
1771c8acfe0aSKaroly Pados 	return 0;
1772c8acfe0aSKaroly Pados }
1773c8acfe0aSKaroly Pados 
177415fb84b7SIcenowy Zheng static int cp2104_gpioconf_init(struct usb_serial *serial)
177515fb84b7SIcenowy Zheng {
177615fb84b7SIcenowy Zheng 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
177715fb84b7SIcenowy Zheng 	struct cp210x_single_port_config config;
177815fb84b7SIcenowy Zheng 	u8 iface_config;
177915fb84b7SIcenowy Zheng 	u8 gpio_latch;
178015fb84b7SIcenowy Zheng 	int result;
178115fb84b7SIcenowy Zheng 	u8 i;
178215fb84b7SIcenowy Zheng 
178315fb84b7SIcenowy Zheng 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
178415fb84b7SIcenowy Zheng 					  CP210X_GET_PORTCONFIG, &config,
178515fb84b7SIcenowy Zheng 					  sizeof(config));
178615fb84b7SIcenowy Zheng 	if (result < 0)
178715fb84b7SIcenowy Zheng 		return result;
178815fb84b7SIcenowy Zheng 
178915fb84b7SIcenowy Zheng 	priv->gc.ngpio = 4;
179015fb84b7SIcenowy Zheng 
179115fb84b7SIcenowy Zheng 	iface_config = config.device_cfg;
179215fb84b7SIcenowy Zheng 	priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
179315fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
179415fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
179515fb84b7SIcenowy Zheng 	gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
179615fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_MASK) >>
179715fb84b7SIcenowy Zheng 					CP210X_GPIO_MODE_OFFSET);
179815fb84b7SIcenowy Zheng 
179915fb84b7SIcenowy Zheng 	/* mark all pins which are not in GPIO mode */
180015fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO0_TXLED_MODE)	/* GPIO 0 */
180115fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(0);
180215fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO1_RXLED_MODE)	/* GPIO 1 */
180315fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(1);
180415fb84b7SIcenowy Zheng 	if (iface_config & CP2104_GPIO2_RS485_MODE)	/* GPIO 2 */
180515fb84b7SIcenowy Zheng 		priv->gpio_altfunc |= BIT(2);
180615fb84b7SIcenowy Zheng 
180715fb84b7SIcenowy Zheng 	/*
180815fb84b7SIcenowy Zheng 	 * Like CP2102N, CP2104 has also no strict input and output pin
180915fb84b7SIcenowy Zheng 	 * modes.
181015fb84b7SIcenowy Zheng 	 * Do the same input mode emulation as CP2102N.
181115fb84b7SIcenowy Zheng 	 */
181215fb84b7SIcenowy Zheng 	for (i = 0; i < priv->gc.ngpio; ++i) {
181315fb84b7SIcenowy Zheng 		/*
181415fb84b7SIcenowy Zheng 		 * Set direction to "input" iff pin is open-drain and reset
181515fb84b7SIcenowy Zheng 		 * value is 1.
181615fb84b7SIcenowy Zheng 		 */
181715fb84b7SIcenowy Zheng 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
181815fb84b7SIcenowy Zheng 			priv->gpio_input |= BIT(i);
181915fb84b7SIcenowy Zheng 	}
182015fb84b7SIcenowy Zheng 
182115fb84b7SIcenowy Zheng 	return 0;
182215fb84b7SIcenowy Zheng }
182315fb84b7SIcenowy Zheng 
1824c8acfe0aSKaroly Pados static int cp2102n_gpioconf_init(struct usb_serial *serial)
1825c8acfe0aSKaroly Pados {
1826c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1827c8acfe0aSKaroly Pados 	const u16 config_size = 0x02a6;
1828c8acfe0aSKaroly Pados 	u8 gpio_rst_latch;
1829c8acfe0aSKaroly Pados 	u8 config_version;
1830c8acfe0aSKaroly Pados 	u8 gpio_pushpull;
1831c8acfe0aSKaroly Pados 	u8 *config_buf;
1832c8acfe0aSKaroly Pados 	u8 gpio_latch;
1833c8acfe0aSKaroly Pados 	u8 gpio_ctrl;
1834c8acfe0aSKaroly Pados 	int result;
1835c8acfe0aSKaroly Pados 	u8 i;
1836c8acfe0aSKaroly Pados 
1837c8acfe0aSKaroly Pados 	/*
1838c8acfe0aSKaroly Pados 	 * Retrieve device configuration from the device.
1839c8acfe0aSKaroly Pados 	 * The array received contains all customization settings done at the
1840c8acfe0aSKaroly Pados 	 * factory/manufacturer. Format of the array is documented at the
1841c8acfe0aSKaroly Pados 	 * time of writing at:
1842c8acfe0aSKaroly Pados 	 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
1843c8acfe0aSKaroly Pados 	 */
1844c8acfe0aSKaroly Pados 	config_buf = kmalloc(config_size, GFP_KERNEL);
1845c8acfe0aSKaroly Pados 	if (!config_buf)
1846c8acfe0aSKaroly Pados 		return -ENOMEM;
1847c8acfe0aSKaroly Pados 
1848c8acfe0aSKaroly Pados 	result = cp210x_read_vendor_block(serial,
1849c8acfe0aSKaroly Pados 					  REQTYPE_DEVICE_TO_HOST,
1850c8acfe0aSKaroly Pados 					  CP210X_READ_2NCONFIG,
1851c8acfe0aSKaroly Pados 					  config_buf,
1852c8acfe0aSKaroly Pados 					  config_size);
1853c8acfe0aSKaroly Pados 	if (result < 0) {
1854c8acfe0aSKaroly Pados 		kfree(config_buf);
1855c8acfe0aSKaroly Pados 		return result;
1856c8acfe0aSKaroly Pados 	}
1857c8acfe0aSKaroly Pados 
1858c8acfe0aSKaroly Pados 	config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
1859c8acfe0aSKaroly Pados 	gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
1860c8acfe0aSKaroly Pados 	gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
1861c8acfe0aSKaroly Pados 	gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
1862c8acfe0aSKaroly Pados 
1863c8acfe0aSKaroly Pados 	kfree(config_buf);
1864c8acfe0aSKaroly Pados 
1865c8acfe0aSKaroly Pados 	/* Make sure this is a config format we understand. */
1866c8acfe0aSKaroly Pados 	if (config_version != 0x01)
1867c8acfe0aSKaroly Pados 		return -ENOTSUPP;
1868c8acfe0aSKaroly Pados 
1869c8acfe0aSKaroly Pados 	priv->gc.ngpio = 4;
1870c8acfe0aSKaroly Pados 
1871c8acfe0aSKaroly Pados 	/*
1872c8acfe0aSKaroly Pados 	 * Get default pin states after reset. Needed so we can determine
1873c8acfe0aSKaroly Pados 	 * the direction of an open-drain pin.
1874c8acfe0aSKaroly Pados 	 */
1875c8acfe0aSKaroly Pados 	gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
1876c8acfe0aSKaroly Pados 
1877c8acfe0aSKaroly Pados 	/* 0 indicates open-drain mode, 1 is push-pull */
1878c8acfe0aSKaroly Pados 	priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
1879c8acfe0aSKaroly Pados 
1880c8acfe0aSKaroly Pados 	/* 0 indicates GPIO mode, 1 is alternate function */
1881c8acfe0aSKaroly Pados 	priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
1882c8acfe0aSKaroly Pados 
1883a49e1abfSMans Rullgard 	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
1884a49e1abfSMans Rullgard 		/*
1885a49e1abfSMans Rullgard 		 * For the QFN28 package, GPIO4-6 are controlled by
1886a49e1abfSMans Rullgard 		 * the low three bits of the mode/latch fields.
1887a49e1abfSMans Rullgard 		 * Contrary to the document linked above, the bits for
1888a49e1abfSMans Rullgard 		 * the SUSPEND pins are elsewhere.  No alternate
1889a49e1abfSMans Rullgard 		 * function is available for these pins.
1890a49e1abfSMans Rullgard 		 */
1891a49e1abfSMans Rullgard 		priv->gc.ngpio = 7;
1892a49e1abfSMans Rullgard 		gpio_latch |= (gpio_rst_latch & 7) << 4;
1893a49e1abfSMans Rullgard 		priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
1894a49e1abfSMans Rullgard 	}
1895a49e1abfSMans Rullgard 
1896c8acfe0aSKaroly Pados 	/*
1897c8acfe0aSKaroly Pados 	 * The CP2102N does not strictly has input and output pin modes,
1898c8acfe0aSKaroly Pados 	 * it only knows open-drain and push-pull modes which is set at
1899c8acfe0aSKaroly Pados 	 * factory. An open-drain pin can function both as an
1900c8acfe0aSKaroly Pados 	 * input or an output. We emulate input mode for open-drain pins
1901c8acfe0aSKaroly Pados 	 * by making sure they are not driven low, and we do not allow
1902c8acfe0aSKaroly Pados 	 * push-pull pins to be set as an input.
1903c8acfe0aSKaroly Pados 	 */
1904c8acfe0aSKaroly Pados 	for (i = 0; i < priv->gc.ngpio; ++i) {
1905c8acfe0aSKaroly Pados 		/*
1906c8acfe0aSKaroly Pados 		 * Set direction to "input" iff pin is open-drain and reset
1907c8acfe0aSKaroly Pados 		 * value is 1.
1908c8acfe0aSKaroly Pados 		 */
1909c8acfe0aSKaroly Pados 		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1910c8acfe0aSKaroly Pados 			priv->gpio_input |= BIT(i);
1911c8acfe0aSKaroly Pados 	}
1912c8acfe0aSKaroly Pados 
1913c8acfe0aSKaroly Pados 	return 0;
1914c8acfe0aSKaroly Pados }
1915c8acfe0aSKaroly Pados 
1916c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1917c8acfe0aSKaroly Pados {
1918c8acfe0aSKaroly Pados 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1919c8acfe0aSKaroly Pados 	int result;
1920c8acfe0aSKaroly Pados 
1921c8acfe0aSKaroly Pados 	switch (priv->partnum) {
192215fb84b7SIcenowy Zheng 	case CP210X_PARTNUM_CP2104:
192315fb84b7SIcenowy Zheng 		result = cp2104_gpioconf_init(serial);
192415fb84b7SIcenowy Zheng 		break;
1925c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2105:
1926c8acfe0aSKaroly Pados 		result = cp2105_gpioconf_init(serial);
1927c8acfe0aSKaroly Pados 		break;
1928c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
1929c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
1930c8acfe0aSKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
1931c8acfe0aSKaroly Pados 		result = cp2102n_gpioconf_init(serial);
1932c8acfe0aSKaroly Pados 		break;
1933c8acfe0aSKaroly Pados 	default:
1934c8acfe0aSKaroly Pados 		return 0;
1935c8acfe0aSKaroly Pados 	}
1936c8acfe0aSKaroly Pados 
1937c8acfe0aSKaroly Pados 	if (result < 0)
1938c8acfe0aSKaroly Pados 		return result;
1939c8acfe0aSKaroly Pados 
1940cf5276ceSMartyn Welch 	priv->gc.label = "cp210x";
1941cf5276ceSMartyn Welch 	priv->gc.request = cp210x_gpio_request;
1942cf5276ceSMartyn Welch 	priv->gc.get_direction = cp210x_gpio_direction_get;
1943cf5276ceSMartyn Welch 	priv->gc.direction_input = cp210x_gpio_direction_input;
1944cf5276ceSMartyn Welch 	priv->gc.direction_output = cp210x_gpio_direction_output;
1945cf5276ceSMartyn Welch 	priv->gc.get = cp210x_gpio_get;
1946cf5276ceSMartyn Welch 	priv->gc.set = cp210x_gpio_set;
19472956b5d9SMika Westerberg 	priv->gc.set_config = cp210x_gpio_set_config;
1948cf5276ceSMartyn Welch 	priv->gc.owner = THIS_MODULE;
1949cf5276ceSMartyn Welch 	priv->gc.parent = &serial->interface->dev;
1950cf5276ceSMartyn Welch 	priv->gc.base = -1;
1951cf5276ceSMartyn Welch 	priv->gc.can_sleep = true;
1952cf5276ceSMartyn Welch 
1953cf5276ceSMartyn Welch 	result = gpiochip_add_data(&priv->gc, serial);
1954cf5276ceSMartyn Welch 	if (!result)
19556b7271d2SJohan Hovold 		priv->gpio_registered = true;
1956cf5276ceSMartyn Welch 
1957cf5276ceSMartyn Welch 	return result;
1958cf5276ceSMartyn Welch }
1959cf5276ceSMartyn Welch 
1960cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1961cf5276ceSMartyn Welch {
1962cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1963cf5276ceSMartyn Welch 
1964cf5276ceSMartyn Welch 	if (priv->gpio_registered) {
1965cf5276ceSMartyn Welch 		gpiochip_remove(&priv->gc);
19666b7271d2SJohan Hovold 		priv->gpio_registered = false;
1967cf5276ceSMartyn Welch 	}
1968cf5276ceSMartyn Welch }
1969cf5276ceSMartyn Welch 
1970cf5276ceSMartyn Welch #else
1971cf5276ceSMartyn Welch 
1972c8acfe0aSKaroly Pados static int cp210x_gpio_init(struct usb_serial *serial)
1973cf5276ceSMartyn Welch {
1974cf5276ceSMartyn Welch 	return 0;
1975cf5276ceSMartyn Welch }
1976cf5276ceSMartyn Welch 
1977cf5276ceSMartyn Welch static void cp210x_gpio_remove(struct usb_serial *serial)
1978cf5276ceSMartyn Welch {
1979cf5276ceSMartyn Welch 	/* Nothing to do */
1980cf5276ceSMartyn Welch }
1981cf5276ceSMartyn Welch 
1982cf5276ceSMartyn Welch #endif
1983cf5276ceSMartyn Welch 
1984e2ae67a3SKonstantin Shkolnyy static int cp210x_port_probe(struct usb_serial_port *port)
198503ee2515SGreg Kroah-Hartman {
1986e2ae67a3SKonstantin Shkolnyy 	struct usb_serial *serial = port->serial;
1987e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
1988d0bf1ff0SKonstantin Shkolnyy 	int ret;
1989a5360a53SPreston Fick 
1990e2ae67a3SKonstantin Shkolnyy 	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1991e2ae67a3SKonstantin Shkolnyy 	if (!port_priv)
1992a5360a53SPreston Fick 		return -ENOMEM;
1993a5360a53SPreston Fick 
1994cf5276ceSMartyn Welch 	port_priv->bInterfaceNumber = cp210x_interface_num(serial);
1995a5360a53SPreston Fick 
1996e2ae67a3SKonstantin Shkolnyy 	usb_set_serial_port_data(port, port_priv);
1997a5360a53SPreston Fick 
1998d0bf1ff0SKonstantin Shkolnyy 	ret = cp210x_detect_swapped_line_ctl(port);
1999d0bf1ff0SKonstantin Shkolnyy 	if (ret) {
2000d0bf1ff0SKonstantin Shkolnyy 		kfree(port_priv);
2001d0bf1ff0SKonstantin Shkolnyy 		return ret;
2002d0bf1ff0SKonstantin Shkolnyy 	}
200303ee2515SGreg Kroah-Hartman 
200403ee2515SGreg Kroah-Hartman 	return 0;
200503ee2515SGreg Kroah-Hartman }
200603ee2515SGreg Kroah-Hartman 
2007e2ae67a3SKonstantin Shkolnyy static int cp210x_port_remove(struct usb_serial_port *port)
2008a5360a53SPreston Fick {
2009e2ae67a3SKonstantin Shkolnyy 	struct cp210x_port_private *port_priv;
2010a5360a53SPreston Fick 
2011e2ae67a3SKonstantin Shkolnyy 	port_priv = usb_get_serial_port_data(port);
2012e2ae67a3SKonstantin Shkolnyy 	kfree(port_priv);
2013e2ae67a3SKonstantin Shkolnyy 
2014e2ae67a3SKonstantin Shkolnyy 	return 0;
2015a5360a53SPreston Fick }
2016a5360a53SPreston Fick 
2017d4706c05SJohan Hovold static void cp210x_init_max_speed(struct usb_serial *serial)
2018d4706c05SJohan Hovold {
2019d4706c05SJohan Hovold 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
20207aecd7fcSJohan Hovold 	bool use_actual_rate = false;
202185bc2d91SJohanna Abrahamsson 	speed_t min = 300;
2022d4706c05SJohan Hovold 	speed_t max;
2023d4706c05SJohan Hovold 
2024d4706c05SJohan Hovold 	switch (priv->partnum) {
2025d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2101:
2026d4706c05SJohan Hovold 		max = 921600;
2027d4706c05SJohan Hovold 		break;
2028d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2102:
2029d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2103:
2030d4706c05SJohan Hovold 		max = 1000000;
2031d4706c05SJohan Hovold 		break;
2032d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2104:
20335edb65a3SJohan Hovold 		use_actual_rate = true;
20345edb65a3SJohan Hovold 		max = 2000000;
20355edb65a3SJohan Hovold 		break;
2036d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2108:
2037d4706c05SJohan Hovold 		max = 2000000;
2038d4706c05SJohan Hovold 		break;
2039d4706c05SJohan Hovold 	case CP210X_PARTNUM_CP2105:
20405edb65a3SJohan Hovold 		if (cp210x_interface_num(serial) == 0) {
20415edb65a3SJohan Hovold 			use_actual_rate = true;
2042d4706c05SJohan Hovold 			max = 2000000;	/* ECI */
20435edb65a3SJohan Hovold 		} else {
204485bc2d91SJohanna Abrahamsson 			min = 2400;
2045d4706c05SJohan Hovold 			max = 921600;	/* SCI */
20465edb65a3SJohan Hovold 		}
2047d4706c05SJohan Hovold 		break;
20486f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN28:
20496f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN24:
20506f0bcf72SKaroly Pados 	case CP210X_PARTNUM_CP2102N_QFN20:
20517aecd7fcSJohan Hovold 		use_actual_rate = true;
20526f0bcf72SKaroly Pados 		max = 3000000;
20536f0bcf72SKaroly Pados 		break;
2054d4706c05SJohan Hovold 	default:
2055d4706c05SJohan Hovold 		max = 2000000;
2056d4706c05SJohan Hovold 		break;
2057d4706c05SJohan Hovold 	}
2058d4706c05SJohan Hovold 
205985bc2d91SJohanna Abrahamsson 	priv->min_speed = min;
2060d4706c05SJohan Hovold 	priv->max_speed = max;
20617aecd7fcSJohan Hovold 	priv->use_actual_rate = use_actual_rate;
2062d4706c05SJohan Hovold }
2063d4706c05SJohan Hovold 
2064cf5276ceSMartyn Welch static int cp210x_attach(struct usb_serial *serial)
2065cf5276ceSMartyn Welch {
2066cf5276ceSMartyn Welch 	int result;
2067cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv;
2068cf5276ceSMartyn Welch 
2069cf5276ceSMartyn Welch 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2070cf5276ceSMartyn Welch 	if (!priv)
2071cf5276ceSMartyn Welch 		return -ENOMEM;
2072cf5276ceSMartyn Welch 
2073cf5276ceSMartyn Welch 	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
2074cf5276ceSMartyn Welch 					  CP210X_GET_PARTNUM, &priv->partnum,
2075cf5276ceSMartyn Welch 					  sizeof(priv->partnum));
20767eac35eaSSebastian Frei 	if (result < 0) {
20777eac35eaSSebastian Frei 		dev_warn(&serial->interface->dev,
20787eac35eaSSebastian Frei 			 "querying part number failed\n");
20797eac35eaSSebastian Frei 		priv->partnum = CP210X_PARTNUM_UNKNOWN;
20807eac35eaSSebastian Frei 	}
2081cf5276ceSMartyn Welch 
2082cf5276ceSMartyn Welch 	usb_set_serial_data(serial, priv);
2083cf5276ceSMartyn Welch 
2084d4706c05SJohan Hovold 	cp210x_init_max_speed(serial);
2085d4706c05SJohan Hovold 
2086c8acfe0aSKaroly Pados 	result = cp210x_gpio_init(serial);
2087cf5276ceSMartyn Welch 	if (result < 0) {
2088c8acfe0aSKaroly Pados 		dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
2089c8acfe0aSKaroly Pados 				result);
2090cf5276ceSMartyn Welch 	}
2091cf5276ceSMartyn Welch 
2092cf5276ceSMartyn Welch 	return 0;
2093cf5276ceSMartyn Welch }
2094cf5276ceSMartyn Welch 
2095cf5276ceSMartyn Welch static void cp210x_disconnect(struct usb_serial *serial)
2096cf5276ceSMartyn Welch {
2097cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2098cf5276ceSMartyn Welch }
2099cf5276ceSMartyn Welch 
2100cf5276ceSMartyn Welch static void cp210x_release(struct usb_serial *serial)
2101cf5276ceSMartyn Welch {
2102cf5276ceSMartyn Welch 	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2103cf5276ceSMartyn Welch 
2104cf5276ceSMartyn Welch 	cp210x_gpio_remove(serial);
2105cf5276ceSMartyn Welch 
2106cf5276ceSMartyn Welch 	kfree(priv);
2107cf5276ceSMartyn Welch }
2108cf5276ceSMartyn Welch 
210968e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
211003ee2515SGreg Kroah-Hartman 
211103ee2515SGreg Kroah-Hartman MODULE_DESCRIPTION(DRIVER_DESC);
2112627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
2113