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