GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / usb / serial / cp210x.c
1 /*
2  * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
3  *
4  * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License version
8  *      2 as published by the Free Software Foundation.
9  *
10  * Support to set flow control line levels using TIOCMGET and TIOCMSET
11  * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
12  * control thanks to Munir Nassar nassarmu@real-time.com
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/usb.h>
24 #include <linux/uaccess.h>
25 #include <linux/usb/serial.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_port_probe(struct usb_serial_port *);
48 static int cp210x_port_remove(struct usb_serial_port *);
49 static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
50
51 static const struct usb_device_id id_table[] = {
52         { USB_DEVICE(0x0404, 0x034C) }, /* NCR Retail IO Box */
53         { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
54         { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
55         { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
56         { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
57         { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
58         { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
59         { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
60         { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
61         { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
62         { USB_DEVICE(0x0988, 0x0578) }, /* Teraoka AD2000 */
63         { USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
64         { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
65         { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
66         { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
67         { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
68         { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
69         { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
70         { USB_DEVICE(0x106F, 0x0003) }, /* CPI / Money Controls Bulk Coin Recycler */
71         { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
72         { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
73         { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
74         { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
75         { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
76         { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
77         { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
78         { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
79         { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
80         { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
81         { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
82         { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
83         { USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
84         { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
85         { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
86         { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
87         { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
88         { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
89         { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
90         { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
91         { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
92         { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
93         { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
94         { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
95         { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
96         { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
97         { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
98         { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
99         { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
100         { USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
101         { USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
102         { USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
103         { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
104         { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
105         { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
106         { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
107         { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
108         { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
109         { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
110         { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
111         { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
112         { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
113         { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
114         { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
115         { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
116         { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
117         { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
118         { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
119         { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
120         { USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
121         { USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
122         { USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
123         { USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
124         { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
125         { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
126         { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
127         { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
128         { USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
129         { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
130         { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
131         { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
132         { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
133         { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
134         { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
135         { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
136         { USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
137         { USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
138         { USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
139         { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
140         { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
141         { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
142         { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
143         { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
144         { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
145         { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
146         { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
147         { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
148         { USB_DEVICE(0x10C4, 0x88D8) }, /* Acuity Brands nLight Air Adapter */
149         { USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
150         { USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
151         { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
152         { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
153         { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
154         { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
155         { USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
156         { USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
157         { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
158         { USB_DEVICE(0x10C4, 0x8A5B) }, /* CEL EM3588 ZigBee USB Stick */
159         { USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
160         { USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
161         { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
162         { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
163         { USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
164         { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
165         { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
166         { USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
167         { USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
168         { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
169         { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
170         { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
171         { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
172         { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
173         { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
174         { USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
175         { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
176         { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
177         { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
178         { USB_DEVICE(0x155A, 0x1006) }, /* ELDAT Easywave RX09 */
179         { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
180         { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
181         { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
182         { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
183         { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
184         { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
185         { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
186         { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
187         { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
188         { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
189         { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
190         { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
191         { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
192         { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
193         { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
194         { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
195         { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
196         { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
197         { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
198         { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
199         { USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
200         { USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
201         { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
202         { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
203         { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
204         { USB_DEVICE(0x1901, 0x0195) }, /* GE B850/B650/B450 CP2104 DP UART interface */
205         { USB_DEVICE(0x1901, 0x0196) }, /* GE B850 CP2105 DP UART interface */
206         { USB_DEVICE(0x1901, 0x0197) }, /* GE CS1000 M.2 Key E serial interface */
207         { USB_DEVICE(0x1901, 0x0198) }, /* GE CS1000 Display serial interface */
208         { USB_DEVICE(0x199B, 0xBA30) }, /* LORD WSDA-200-USB */
209         { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
210         { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
211         { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
212         { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
213         { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
214         { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
215         { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
216         { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
217         { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
218         { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
219         { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
220         { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
221         { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
222         { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
223         { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
224         { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
225         { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
226         { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
227         { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
228         { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
229         { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
230         { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
231         { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
232         { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
233         { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
234         { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
235         { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
236         { USB_DEVICE(0x2184, 0x0030) }, /* GW Instek GDM-834x Digital Multimeter */
237         { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
238         { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
239         { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
240         { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
241         { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
242         { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
243         { } /* Terminating Entry */
244 };
245
246 MODULE_DEVICE_TABLE(usb, id_table);
247
248 struct cp210x_port_private {
249         __u8                    bInterfaceNumber;
250         bool                    has_swapped_line_ctl;
251 };
252
253 static struct usb_serial_driver cp210x_device = {
254         .driver = {
255                 .owner =        THIS_MODULE,
256                 .name =         "cp210x",
257         },
258         .id_table               = id_table,
259         .num_ports              = 1,
260         .bulk_in_size           = 256,
261         .bulk_out_size          = 256,
262         .open                   = cp210x_open,
263         .close                  = cp210x_close,
264         .break_ctl              = cp210x_break_ctl,
265         .set_termios            = cp210x_set_termios,
266         .tx_empty               = cp210x_tx_empty,
267         .throttle               = usb_serial_generic_throttle,
268         .unthrottle             = usb_serial_generic_unthrottle,
269         .tiocmget               = cp210x_tiocmget,
270         .tiocmset               = cp210x_tiocmset,
271         .port_probe             = cp210x_port_probe,
272         .port_remove            = cp210x_port_remove,
273         .dtr_rts                = cp210x_dtr_rts
274 };
275
276 static struct usb_serial_driver * const serial_drivers[] = {
277         &cp210x_device, NULL
278 };
279
280 /* Config request types */
281 #define REQTYPE_HOST_TO_INTERFACE       0x41
282 #define REQTYPE_INTERFACE_TO_HOST       0xc1
283 #define REQTYPE_HOST_TO_DEVICE  0x40
284 #define REQTYPE_DEVICE_TO_HOST  0xc0
285
286 /* Config request codes */
287 #define CP210X_IFC_ENABLE       0x00
288 #define CP210X_SET_BAUDDIV      0x01
289 #define CP210X_GET_BAUDDIV      0x02
290 #define CP210X_SET_LINE_CTL     0x03
291 #define CP210X_GET_LINE_CTL     0x04
292 #define CP210X_SET_BREAK        0x05
293 #define CP210X_IMM_CHAR         0x06
294 #define CP210X_SET_MHS          0x07
295 #define CP210X_GET_MDMSTS       0x08
296 #define CP210X_SET_XON          0x09
297 #define CP210X_SET_XOFF         0x0A
298 #define CP210X_SET_EVENTMASK    0x0B
299 #define CP210X_GET_EVENTMASK    0x0C
300 #define CP210X_SET_CHAR         0x0D
301 #define CP210X_GET_CHARS        0x0E
302 #define CP210X_GET_PROPS        0x0F
303 #define CP210X_GET_COMM_STATUS  0x10
304 #define CP210X_RESET            0x11
305 #define CP210X_PURGE            0x12
306 #define CP210X_SET_FLOW         0x13
307 #define CP210X_GET_FLOW         0x14
308 #define CP210X_EMBED_EVENTS     0x15
309 #define CP210X_GET_EVENTSTATE   0x16
310 #define CP210X_SET_CHARS        0x19
311 #define CP210X_GET_BAUDRATE     0x1D
312 #define CP210X_SET_BAUDRATE     0x1E
313
314 /* CP210X_IFC_ENABLE */
315 #define UART_ENABLE             0x0001
316 #define UART_DISABLE            0x0000
317
318 /* CP210X_(SET|GET)_BAUDDIV */
319 #define BAUD_RATE_GEN_FREQ      0x384000
320
321 /* CP210X_(SET|GET)_LINE_CTL */
322 #define BITS_DATA_MASK          0X0f00
323 #define BITS_DATA_5             0X0500
324 #define BITS_DATA_6             0X0600
325 #define BITS_DATA_7             0X0700
326 #define BITS_DATA_8             0X0800
327 #define BITS_DATA_9             0X0900
328
329 #define BITS_PARITY_MASK        0x00f0
330 #define BITS_PARITY_NONE        0x0000
331 #define BITS_PARITY_ODD         0x0010
332 #define BITS_PARITY_EVEN        0x0020
333 #define BITS_PARITY_MARK        0x0030
334 #define BITS_PARITY_SPACE       0x0040
335
336 #define BITS_STOP_MASK          0x000f
337 #define BITS_STOP_1             0x0000
338 #define BITS_STOP_1_5           0x0001
339 #define BITS_STOP_2             0x0002
340
341 /* CP210X_SET_BREAK */
342 #define BREAK_ON                0x0001
343 #define BREAK_OFF               0x0000
344
345 /* CP210X_(SET_MHS|GET_MDMSTS) */
346 #define CONTROL_DTR             0x0001
347 #define CONTROL_RTS             0x0002
348 #define CONTROL_CTS             0x0010
349 #define CONTROL_DSR             0x0020
350 #define CONTROL_RING            0x0040
351 #define CONTROL_DCD             0x0080
352 #define CONTROL_WRITE_DTR       0x0100
353 #define CONTROL_WRITE_RTS       0x0200
354
355 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
356 struct cp210x_comm_status {
357         __le32   ulErrors;
358         __le32   ulHoldReasons;
359         __le32   ulAmountInInQueue;
360         __le32   ulAmountInOutQueue;
361         u8       bEofReceived;
362         u8       bWaitForImmediate;
363         u8       bReserved;
364 } __packed;
365
366 /*
367  * CP210X_PURGE - 16 bits passed in wValue of USB request.
368  * SiLabs app note AN571 gives a strange description of the 4 bits:
369  * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
370  * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
371  */
372 #define PURGE_ALL               0x000f
373
374 /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
375 struct cp210x_flow_ctl {
376         __le32  ulControlHandshake;
377         __le32  ulFlowReplace;
378         __le32  ulXonLimit;
379         __le32  ulXoffLimit;
380 } __packed;
381
382 /* cp210x_flow_ctl::ulControlHandshake */
383 #define CP210X_SERIAL_DTR_MASK          GENMASK(1, 0)
384 #define CP210X_SERIAL_DTR_SHIFT(_mode)  (_mode)
385 #define CP210X_SERIAL_CTS_HANDSHAKE     BIT(3)
386 #define CP210X_SERIAL_DSR_HANDSHAKE     BIT(4)
387 #define CP210X_SERIAL_DCD_HANDSHAKE     BIT(5)
388 #define CP210X_SERIAL_DSR_SENSITIVITY   BIT(6)
389
390 /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
391 #define CP210X_SERIAL_DTR_INACTIVE      0
392 #define CP210X_SERIAL_DTR_ACTIVE        1
393 #define CP210X_SERIAL_DTR_FLOW_CTL      2
394
395 /* cp210x_flow_ctl::ulFlowReplace */
396 #define CP210X_SERIAL_AUTO_TRANSMIT     BIT(0)
397 #define CP210X_SERIAL_AUTO_RECEIVE      BIT(1)
398 #define CP210X_SERIAL_ERROR_CHAR        BIT(2)
399 #define CP210X_SERIAL_NULL_STRIPPING    BIT(3)
400 #define CP210X_SERIAL_BREAK_CHAR        BIT(4)
401 #define CP210X_SERIAL_RTS_MASK          GENMASK(7, 6)
402 #define CP210X_SERIAL_RTS_SHIFT(_mode)  (_mode << 6)
403 #define CP210X_SERIAL_XOFF_CONTINUE     BIT(31)
404
405 /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
406 #define CP210X_SERIAL_RTS_INACTIVE      0
407 #define CP210X_SERIAL_RTS_ACTIVE        1
408 #define CP210X_SERIAL_RTS_FLOW_CTL      2
409
410 /*
411  * Reads a variable-sized block of CP210X_ registers, identified by req.
412  * Returns data into buf in native USB byte order.
413  */
414 static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
415                 void *buf, int bufsize)
416 {
417         struct usb_serial *serial = port->serial;
418         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
419         void *dmabuf;
420         int result;
421
422         dmabuf = kmalloc(bufsize, GFP_KERNEL);
423         if (!dmabuf) {
424                 /*
425                  * FIXME Some callers don't bother to check for error,
426                  * at least give them consistent junk until they are fixed
427                  */
428                 memset(buf, 0, bufsize);
429                 return -ENOMEM;
430         }
431
432         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
433                         req, REQTYPE_INTERFACE_TO_HOST, 0,
434                         port_priv->bInterfaceNumber, dmabuf, bufsize,
435                         USB_CTRL_SET_TIMEOUT);
436         if (result == bufsize) {
437                 memcpy(buf, dmabuf, bufsize);
438                 result = 0;
439         } else {
440                 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
441                                 req, bufsize, result);
442                 if (result >= 0)
443                         result = -EPROTO;
444
445                 /*
446                  * FIXME Some callers don't bother to check for error,
447                  * at least give them consistent junk until they are fixed
448                  */
449                 memset(buf, 0, bufsize);
450         }
451
452         kfree(dmabuf);
453
454         return result;
455 }
456
457 /*
458  * Reads any 32-bit CP210X_ register identified by req.
459  */
460 static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
461 {
462         __le32 le32_val;
463         int err;
464
465         err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
466         if (err) {
467                 /*
468                  * FIXME Some callers don't bother to check for error,
469                  * at least give them consistent junk until they are fixed
470                  */
471                 *val = 0;
472                 return err;
473         }
474
475         *val = le32_to_cpu(le32_val);
476
477         return 0;
478 }
479
480 /*
481  * Reads any 16-bit CP210X_ register identified by req.
482  */
483 static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
484 {
485         __le16 le16_val;
486         int err;
487
488         err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
489         if (err)
490                 return err;
491
492         *val = le16_to_cpu(le16_val);
493
494         return 0;
495 }
496
497 /*
498  * Reads any 8-bit CP210X_ register identified by req.
499  */
500 static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
501 {
502         return cp210x_read_reg_block(port, req, val, sizeof(*val));
503 }
504
505 /*
506  * Writes any 16-bit CP210X_ register (req) whose value is passed
507  * entirely in the wValue field of the USB request.
508  */
509 static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
510 {
511         struct usb_serial *serial = port->serial;
512         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
513         int result;
514
515         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
516                         req, REQTYPE_HOST_TO_INTERFACE, val,
517                         port_priv->bInterfaceNumber, NULL, 0,
518                         USB_CTRL_SET_TIMEOUT);
519         if (result < 0) {
520                 dev_err(&port->dev, "failed set request 0x%x status: %d\n",
521                                 req, result);
522         }
523
524         return result;
525 }
526
527 /*
528  * Writes a variable-sized block of CP210X_ registers, identified by req.
529  * Data in buf must be in native USB byte order.
530  */
531 static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
532                 void *buf, int bufsize)
533 {
534         struct usb_serial *serial = port->serial;
535         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
536         void *dmabuf;
537         int result;
538
539         dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
540         if (!dmabuf)
541                 return -ENOMEM;
542
543         result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
544                         req, REQTYPE_HOST_TO_INTERFACE, 0,
545                         port_priv->bInterfaceNumber, dmabuf, bufsize,
546                         USB_CTRL_SET_TIMEOUT);
547
548         kfree(dmabuf);
549
550         if (result == bufsize) {
551                 result = 0;
552         } else {
553                 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
554                                 req, bufsize, result);
555                 if (result >= 0)
556                         result = -EPROTO;
557         }
558
559         return result;
560 }
561
562 /*
563  * Writes any 32-bit CP210X_ register identified by req.
564  */
565 static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
566 {
567         __le32 le32_val;
568
569         le32_val = cpu_to_le32(val);
570
571         return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
572 }
573
574 /*
575  * Detect CP2108 GET_LINE_CTL bug and activate workaround.
576  * Write a known good value 0x800, read it back.
577  * If it comes back swapped the bug is detected.
578  * Preserve the original register value.
579  */
580 static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
581 {
582         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
583         u16 line_ctl_save;
584         u16 line_ctl_test;
585         int err;
586
587         err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
588         if (err)
589                 return err;
590
591         err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
592         if (err)
593                 return err;
594
595         err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
596         if (err)
597                 return err;
598
599         if (line_ctl_test == 8) {
600                 port_priv->has_swapped_line_ctl = true;
601                 line_ctl_save = swab16(line_ctl_save);
602         }
603
604         return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
605 }
606
607 /*
608  * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
609  * to workaround cp2108 bug and get correct value.
610  */
611 static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
612 {
613         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
614         int err;
615
616         err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
617         if (err)
618                 return err;
619
620         /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
621         if (port_priv->has_swapped_line_ctl)
622                 *ctl = swab16(*ctl);
623
624         return 0;
625 }
626
627 /*
628  * cp210x_quantise_baudrate
629  * Quantises the baud rate as per AN205 Table 1
630  */
631 static unsigned int cp210x_quantise_baudrate(unsigned int baud)
632 {
633         if (baud <= 300)
634                 baud = 300;
635         else if (baud <= 600)      baud = 600;
636         else if (baud <= 1200)     baud = 1200;
637         else if (baud <= 1800)     baud = 1800;
638         else if (baud <= 2400)     baud = 2400;
639         else if (baud <= 4000)     baud = 4000;
640         else if (baud <= 4803)     baud = 4800;
641         else if (baud <= 7207)     baud = 7200;
642         else if (baud <= 9612)     baud = 9600;
643         else if (baud <= 14428)    baud = 14400;
644         else if (baud <= 16062)    baud = 16000;
645         else if (baud <= 19250)    baud = 19200;
646         else if (baud <= 28912)    baud = 28800;
647         else if (baud <= 38601)    baud = 38400;
648         else if (baud <= 51558)    baud = 51200;
649         else if (baud <= 56280)    baud = 56000;
650         else if (baud <= 58053)    baud = 57600;
651         else if (baud <= 64111)    baud = 64000;
652         else if (baud <= 77608)    baud = 76800;
653         else if (baud <= 117028)   baud = 115200;
654         else if (baud <= 129347)   baud = 128000;
655         else if (baud <= 156868)   baud = 153600;
656         else if (baud <= 237832)   baud = 230400;
657         else if (baud <= 254234)   baud = 250000;
658         else if (baud <= 273066)   baud = 256000;
659         else if (baud <= 491520)   baud = 460800;
660         else if (baud <= 567138)   baud = 500000;
661         else if (baud <= 670254)   baud = 576000;
662         else if (baud < 1000000)
663                 baud = 921600;
664         else if (baud > 2000000)
665                 baud = 2000000;
666         return baud;
667 }
668
669 static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
670 {
671         int result;
672
673         result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
674         if (result) {
675                 dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
676                 return result;
677         }
678
679         /* Configure the termios structure */
680         cp210x_get_termios(tty, port);
681
682         /* The baud rate must be initialised on cp2104 */
683         if (tty)
684                 cp210x_change_speed(tty, port, NULL);
685
686         return usb_serial_generic_open(tty, port);
687 }
688
689 static void cp210x_close(struct usb_serial_port *port)
690 {
691         usb_serial_generic_close(port);
692
693         /* Clear both queues; cp2108 needs this to avoid an occasional hang */
694         cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
695
696         cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
697 }
698
699 /*
700  * Read how many bytes are waiting in the TX queue.
701  */
702 static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
703                 u32 *count)
704 {
705         struct usb_serial *serial = port->serial;
706         struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
707         struct cp210x_comm_status *sts;
708         int result;
709
710         sts = kmalloc(sizeof(*sts), GFP_KERNEL);
711         if (!sts)
712                 return -ENOMEM;
713
714         result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
715                         CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
716                         0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
717                         USB_CTRL_GET_TIMEOUT);
718         if (result == sizeof(*sts)) {
719                 *count = le32_to_cpu(sts->ulAmountInOutQueue);
720                 result = 0;
721         } else {
722                 dev_err(&port->dev, "failed to get comm status: %d\n", result);
723                 if (result >= 0)
724                         result = -EPROTO;
725         }
726
727         kfree(sts);
728
729         return result;
730 }
731
732 static bool cp210x_tx_empty(struct usb_serial_port *port)
733 {
734         int err;
735         u32 count;
736
737         err = cp210x_get_tx_queue_byte_count(port, &count);
738         if (err)
739                 return true;
740
741         return !count;
742 }
743
744 /*
745  * cp210x_get_termios
746  * Reads the baud rate, data bits, parity, stop bits and flow control mode
747  * from the device, corrects any unsupported values, and configures the
748  * termios structure to reflect the state of the device
749  */
750 static void cp210x_get_termios(struct tty_struct *tty,
751         struct usb_serial_port *port)
752 {
753         unsigned int baud;
754
755         if (tty) {
756                 cp210x_get_termios_port(tty->driver_data,
757                         &tty->termios.c_cflag, &baud);
758                 tty_encode_baud_rate(tty, baud, baud);
759         } else {
760                 tcflag_t cflag;
761                 cflag = 0;
762                 cp210x_get_termios_port(port, &cflag, &baud);
763         }
764 }
765
766 /*
767  * cp210x_get_termios_port
768  * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
769  */
770 static void cp210x_get_termios_port(struct usb_serial_port *port,
771         tcflag_t *cflagp, unsigned int *baudp)
772 {
773         struct device *dev = &port->dev;
774         tcflag_t cflag;
775         struct cp210x_flow_ctl flow_ctl;
776         u32 baud;
777         u16 bits;
778         u32 ctl_hs;
779         u32 flow_repl;
780
781         cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
782
783         dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
784         *baudp = baud;
785
786         cflag = *cflagp;
787
788         cp210x_get_line_ctl(port, &bits);
789         cflag &= ~CSIZE;
790         switch (bits & BITS_DATA_MASK) {
791         case BITS_DATA_5:
792                 dev_dbg(dev, "%s - data bits = 5\n", __func__);
793                 cflag |= CS5;
794                 break;
795         case BITS_DATA_6:
796                 dev_dbg(dev, "%s - data bits = 6\n", __func__);
797                 cflag |= CS6;
798                 break;
799         case BITS_DATA_7:
800                 dev_dbg(dev, "%s - data bits = 7\n", __func__);
801                 cflag |= CS7;
802                 break;
803         case BITS_DATA_8:
804                 dev_dbg(dev, "%s - data bits = 8\n", __func__);
805                 cflag |= CS8;
806                 break;
807         case BITS_DATA_9:
808                 dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
809                 cflag |= CS8;
810                 bits &= ~BITS_DATA_MASK;
811                 bits |= BITS_DATA_8;
812                 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
813                 break;
814         default:
815                 dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
816                 cflag |= CS8;
817                 bits &= ~BITS_DATA_MASK;
818                 bits |= BITS_DATA_8;
819                 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
820                 break;
821         }
822
823         switch (bits & BITS_PARITY_MASK) {
824         case BITS_PARITY_NONE:
825                 dev_dbg(dev, "%s - parity = NONE\n", __func__);
826                 cflag &= ~PARENB;
827                 break;
828         case BITS_PARITY_ODD:
829                 dev_dbg(dev, "%s - parity = ODD\n", __func__);
830                 cflag |= (PARENB|PARODD);
831                 break;
832         case BITS_PARITY_EVEN:
833                 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
834                 cflag &= ~PARODD;
835                 cflag |= PARENB;
836                 break;
837         case BITS_PARITY_MARK:
838                 dev_dbg(dev, "%s - parity = MARK\n", __func__);
839                 cflag |= (PARENB|PARODD|CMSPAR);
840                 break;
841         case BITS_PARITY_SPACE:
842                 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
843                 cflag &= ~PARODD;
844                 cflag |= (PARENB|CMSPAR);
845                 break;
846         default:
847                 dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
848                 cflag &= ~PARENB;
849                 bits &= ~BITS_PARITY_MASK;
850                 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
851                 break;
852         }
853
854         cflag &= ~CSTOPB;
855         switch (bits & BITS_STOP_MASK) {
856         case BITS_STOP_1:
857                 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
858                 break;
859         case BITS_STOP_1_5:
860                 dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
861                 bits &= ~BITS_STOP_MASK;
862                 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
863                 break;
864         case BITS_STOP_2:
865                 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
866                 cflag |= CSTOPB;
867                 break;
868         default:
869                 dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
870                 bits &= ~BITS_STOP_MASK;
871                 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
872                 break;
873         }
874
875         cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
876                         sizeof(flow_ctl));
877         ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
878         if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
879                 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
880                 /*
881                  * When the port is closed, the CP210x hardware disables
882                  * auto-RTS and RTS is deasserted but it leaves auto-CTS when
883                  * in hardware flow control mode. When re-opening the port, if
884                  * auto-CTS is enabled on the cp210x, then auto-RTS must be
885                  * re-enabled in the driver.
886                  */
887                 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
888                 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
889                 flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
890                 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
891                 cp210x_write_reg_block(port,
892                                 CP210X_SET_FLOW,
893                                 &flow_ctl,
894                                 sizeof(flow_ctl));
895
896                 cflag |= CRTSCTS;
897         } else {
898                 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
899                 cflag &= ~CRTSCTS;
900         }
901
902         *cflagp = cflag;
903 }
904
905 /*
906  * CP2101 supports the following baud rates:
907  *
908  *      300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
909  *      38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
910  *
911  * CP2102 and CP2103 support the following additional rates:
912  *
913  *      4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
914  *      576000
915  *
916  * The device will map a requested rate to a supported one, but the result
917  * of requests for rates greater than 1053257 is undefined (see AN205).
918  *
919  * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
920  * respectively, with an error less than 1%. The actual rates are determined
921  * by
922  *
923  *      div = round(freq / (2 x prescale x request))
924  *      actual = freq / (2 x prescale x div)
925  *
926  * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
927  * or 1 otherwise.
928  * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
929  * otherwise.
930  */
931 static void cp210x_change_speed(struct tty_struct *tty,
932                 struct usb_serial_port *port, struct ktermios *old_termios)
933 {
934         u32 baud;
935
936         baud = tty->termios.c_ospeed;
937
938         /* This maps the requested rate to a rate valid on cp2102 or cp2103,
939          * or to an arbitrary rate in [1M,2M].
940          *
941          * NOTE: B0 is not implemented.
942          */
943         baud = cp210x_quantise_baudrate(baud);
944
945         dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
946         if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
947                 dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
948                 if (old_termios)
949                         baud = old_termios->c_ospeed;
950                 else
951                         baud = 9600;
952         }
953
954         tty_encode_baud_rate(tty, baud, baud);
955 }
956
957 static void cp210x_set_termios(struct tty_struct *tty,
958                 struct usb_serial_port *port, struct ktermios *old_termios)
959 {
960         struct device *dev = &port->dev;
961         unsigned int cflag, old_cflag;
962         u16 bits;
963
964         cflag = tty->termios.c_cflag;
965         old_cflag = old_termios->c_cflag;
966
967         if (tty->termios.c_ospeed != old_termios->c_ospeed)
968                 cp210x_change_speed(tty, port, old_termios);
969
970         /* If the number of data bits is to be updated */
971         if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
972                 cp210x_get_line_ctl(port, &bits);
973                 bits &= ~BITS_DATA_MASK;
974                 switch (cflag & CSIZE) {
975                 case CS5:
976                         bits |= BITS_DATA_5;
977                         dev_dbg(dev, "%s - data bits = 5\n", __func__);
978                         break;
979                 case CS6:
980                         bits |= BITS_DATA_6;
981                         dev_dbg(dev, "%s - data bits = 6\n", __func__);
982                         break;
983                 case CS7:
984                         bits |= BITS_DATA_7;
985                         dev_dbg(dev, "%s - data bits = 7\n", __func__);
986                         break;
987                 case CS8:
988                         bits |= BITS_DATA_8;
989                         dev_dbg(dev, "%s - data bits = 8\n", __func__);
990                         break;
991                 /*case CS9:
992                         bits |= BITS_DATA_9;
993                         dev_dbg(dev, "%s - data bits = 9\n", __func__);
994                         break;*/
995                 default:
996                         dev_dbg(dev, "cp210x driver does not support the number of bits requested, using 8 bit mode\n");
997                         bits |= BITS_DATA_8;
998                         break;
999                 }
1000                 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1001                         dev_dbg(dev, "Number of data bits requested not supported by device\n");
1002         }
1003
1004         if ((cflag     & (PARENB|PARODD|CMSPAR)) !=
1005             (old_cflag & (PARENB|PARODD|CMSPAR))) {
1006                 cp210x_get_line_ctl(port, &bits);
1007                 bits &= ~BITS_PARITY_MASK;
1008                 if (cflag & PARENB) {
1009                         if (cflag & CMSPAR) {
1010                                 if (cflag & PARODD) {
1011                                         bits |= BITS_PARITY_MARK;
1012                                         dev_dbg(dev, "%s - parity = MARK\n", __func__);
1013                                 } else {
1014                                         bits |= BITS_PARITY_SPACE;
1015                                         dev_dbg(dev, "%s - parity = SPACE\n", __func__);
1016                                 }
1017                         } else {
1018                                 if (cflag & PARODD) {
1019                                         bits |= BITS_PARITY_ODD;
1020                                         dev_dbg(dev, "%s - parity = ODD\n", __func__);
1021                                 } else {
1022                                         bits |= BITS_PARITY_EVEN;
1023                                         dev_dbg(dev, "%s - parity = EVEN\n", __func__);
1024                                 }
1025                         }
1026                 }
1027                 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1028                         dev_dbg(dev, "Parity mode not supported by device\n");
1029         }
1030
1031         if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1032                 cp210x_get_line_ctl(port, &bits);
1033                 bits &= ~BITS_STOP_MASK;
1034                 if (cflag & CSTOPB) {
1035                         bits |= BITS_STOP_2;
1036                         dev_dbg(dev, "%s - stop bits = 2\n", __func__);
1037                 } else {
1038                         bits |= BITS_STOP_1;
1039                         dev_dbg(dev, "%s - stop bits = 1\n", __func__);
1040                 }
1041                 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1042                         dev_dbg(dev, "Number of stop bits requested not supported by device\n");
1043         }
1044
1045         if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
1046                 struct cp210x_flow_ctl flow_ctl;
1047                 u32 ctl_hs;
1048                 u32 flow_repl;
1049
1050                 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1051                                 sizeof(flow_ctl));
1052                 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1053                 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1054                 dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1055                                 __func__, ctl_hs, flow_repl);
1056
1057                 ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1058                 ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1059                 ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1060                 ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1061                 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
1062                 if (cflag & CRTSCTS) {
1063                         ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1064
1065                         flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1066                         flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1067                                         CP210X_SERIAL_RTS_FLOW_CTL);
1068                         dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1069                 } else {
1070                         ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1071
1072                         flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1073                         flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1074                                         CP210X_SERIAL_RTS_ACTIVE);
1075                         dev_dbg(dev, "%s - flow control = NONE\n", __func__);
1076                 }
1077
1078                 dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1079                                 __func__, ctl_hs, flow_repl);
1080                 flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1081                 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1082                 cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1083                                 sizeof(flow_ctl));
1084         }
1085
1086 }
1087
1088 static int cp210x_tiocmset(struct tty_struct *tty,
1089                 unsigned int set, unsigned int clear)
1090 {
1091         struct usb_serial_port *port = tty->driver_data;
1092         return cp210x_tiocmset_port(port, set, clear);
1093 }
1094
1095 static int cp210x_tiocmset_port(struct usb_serial_port *port,
1096                 unsigned int set, unsigned int clear)
1097 {
1098         u16 control = 0;
1099
1100         if (set & TIOCM_RTS) {
1101                 control |= CONTROL_RTS;
1102                 control |= CONTROL_WRITE_RTS;
1103         }
1104         if (set & TIOCM_DTR) {
1105                 control |= CONTROL_DTR;
1106                 control |= CONTROL_WRITE_DTR;
1107         }
1108         if (clear & TIOCM_RTS) {
1109                 control &= ~CONTROL_RTS;
1110                 control |= CONTROL_WRITE_RTS;
1111         }
1112         if (clear & TIOCM_DTR) {
1113                 control &= ~CONTROL_DTR;
1114                 control |= CONTROL_WRITE_DTR;
1115         }
1116
1117         dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
1118
1119         return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
1120 }
1121
1122 static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1123 {
1124         if (on)
1125                 cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
1126         else
1127                 cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
1128 }
1129
1130 static int cp210x_tiocmget(struct tty_struct *tty)
1131 {
1132         struct usb_serial_port *port = tty->driver_data;
1133         u8 control;
1134         int result;
1135
1136         result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1137         if (result)
1138                 return result;
1139
1140         result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
1141                 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
1142                 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
1143                 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
1144                 |((control & CONTROL_RING)? TIOCM_RI  : 0)
1145                 |((control & CONTROL_DCD) ? TIOCM_CD  : 0);
1146
1147         dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
1148
1149         return result;
1150 }
1151
1152 static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
1153 {
1154         struct usb_serial_port *port = tty->driver_data;
1155         u16 state;
1156
1157         if (break_state == 0)
1158                 state = BREAK_OFF;
1159         else
1160                 state = BREAK_ON;
1161         dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1162                 state == BREAK_OFF ? "off" : "on");
1163         cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
1164 }
1165
1166 static int cp210x_port_probe(struct usb_serial_port *port)
1167 {
1168         struct usb_serial *serial = port->serial;
1169         struct usb_host_interface *cur_altsetting;
1170         struct cp210x_port_private *port_priv;
1171         int ret;
1172
1173         port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1174         if (!port_priv)
1175                 return -ENOMEM;
1176
1177         cur_altsetting = serial->interface->cur_altsetting;
1178         port_priv->bInterfaceNumber = cur_altsetting->desc.bInterfaceNumber;
1179
1180         usb_set_serial_port_data(port, port_priv);
1181
1182         ret = cp210x_detect_swapped_line_ctl(port);
1183         if (ret) {
1184                 kfree(port_priv);
1185                 return ret;
1186         }
1187
1188         return 0;
1189 }
1190
1191 static int cp210x_port_remove(struct usb_serial_port *port)
1192 {
1193         struct cp210x_port_private *port_priv;
1194
1195         port_priv = usb_get_serial_port_data(port);
1196         kfree(port_priv);
1197
1198         return 0;
1199 }
1200
1201 module_usb_serial_driver(serial_drivers, id_table);
1202
1203 MODULE_DESCRIPTION(DRIVER_DESC);
1204 MODULE_LICENSE("GPL");