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