1 // SPDX-License-Identifier: GPL-2.0
3 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
5 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
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
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>
27 #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
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 *,
39 static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
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);
57 static const struct usb_device_id id_table[] = {
58 { USB_DEVICE(0x0404, 0x034C) }, /* NCR Retail IO Box */
59 { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
60 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
61 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
62 { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
63 { USB_DEVICE(0x04BF, 0x1301) }, /* TDK Corporation NC0110013M - Network Controller */
64 { USB_DEVICE(0x04BF, 0x1303) }, /* TDK Corporation MM0110113M - i3 Micro Module */
65 { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
66 { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
67 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
68 { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
69 { USB_DEVICE(0x0908, 0x0070) }, /* Siemens SCALANCE LPE-9000 USB Serial Console */
70 { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
71 { USB_DEVICE(0x0988, 0x0578) }, /* Teraoka AD2000 */
72 { USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
73 { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
74 { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
75 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
76 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
77 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
78 { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
79 { USB_DEVICE(0x106F, 0x0003) }, /* CPI / Money Controls Bulk Coin Recycler */
80 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
81 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
82 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
83 { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
84 { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
85 { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
86 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
87 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
88 { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
89 { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
90 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
91 { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
92 { USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
93 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
94 { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
95 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
96 { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
97 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
98 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
99 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
100 { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
101 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
102 { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
103 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
104 { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
105 { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
106 { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
107 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
108 { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
109 { USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
110 { USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
111 { USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
112 { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
113 { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
114 { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
115 { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
116 { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
117 { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
118 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
119 { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
120 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
121 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
122 { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
123 { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
124 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
125 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
126 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
127 { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
128 { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
129 { USB_DEVICE(0x10C4, 0x82AA) }, /* Silicon Labs IFS-USB-DATACABLE used with Quint UPS */
130 { USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
131 { USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
132 { USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
133 { USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
134 { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
135 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
136 { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
137 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
138 { USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
139 { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
140 { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
141 { USB_DEVICE(0x10C4, 0x8414) }, /* Decagon USB Cable Adapter */
142 { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
143 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
144 { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
145 { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
146 { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
147 { USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
148 { USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
149 { USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
150 { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
151 { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
152 { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
153 { USB_DEVICE(0x10C4, 0x863C) }, /* MGP Instruments PDS100 */
154 { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
155 { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
156 { USB_DEVICE(0x10C4, 0x87ED) }, /* IMST USB-Stick for Smart Meter */
157 { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
158 { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
159 { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
160 { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
161 { USB_DEVICE(0x10C4, 0x88D8) }, /* Acuity Brands nLight Air Adapter */
162 { USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
163 { USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
164 { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
165 { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
166 { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
167 { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
168 { USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
169 { USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
170 { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
171 { USB_DEVICE(0x10C4, 0x8A5B) }, /* CEL EM3588 ZigBee USB Stick */
172 { USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
173 { USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
174 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
175 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
176 { USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
177 { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
178 { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
179 { USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
180 { USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
181 { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
182 { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
183 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
184 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
185 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
186 { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
187 { USB_DEVICE(0x11CA, 0x0212) }, /* Verifone USB to Printer (UART, CP2102) */
188 { USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
189 { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
190 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
191 { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
192 { USB_DEVICE(0x155A, 0x1006) }, /* ELDAT Easywave RX09 */
193 { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
194 { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
195 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
196 { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
197 { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
198 { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
199 { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
200 { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
201 { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
202 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
203 { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
204 { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
205 { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
206 { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
207 { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
208 { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
209 { USB_DEVICE(0x17A8, 0x0011) }, /* Kamstrup 444 MHz RF sniffer */
210 { USB_DEVICE(0x17A8, 0x0013) }, /* Kamstrup 870 MHz RF sniffer */
211 { USB_DEVICE(0x17A8, 0x0101) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Int Ant) */
212 { USB_DEVICE(0x17A8, 0x0102) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Ext Ant) */
213 { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
214 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
215 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
216 { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
217 { USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
218 { USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
219 { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
220 { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
221 { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
222 { USB_DEVICE(0x1901, 0x0195) }, /* GE B850/B650/B450 CP2104 DP UART interface */
223 { USB_DEVICE(0x1901, 0x0196) }, /* GE B850 CP2105 DP UART interface */
224 { USB_DEVICE(0x1901, 0x0197) }, /* GE CS1000 M.2 Key E serial interface */
225 { USB_DEVICE(0x1901, 0x0198) }, /* GE CS1000 Display serial interface */
226 { USB_DEVICE(0x199B, 0xBA30) }, /* LORD WSDA-200-USB */
227 { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
228 { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
229 { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
230 { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
231 { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
232 { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
233 { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
234 { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
235 { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
236 { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
237 { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
238 { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
239 { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
240 { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
241 { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
242 { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
243 { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
244 { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
245 { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
246 { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
247 { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
248 { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
249 { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
250 { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
251 { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
252 { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
253 { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
254 { USB_DEVICE(0x2184, 0x0030) }, /* GW Instek GDM-834x Digital Multimeter */
255 { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
256 { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
257 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
258 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
259 { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
260 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
261 { } /* Terminating Entry */
264 MODULE_DEVICE_TABLE(usb, id_table);
266 struct cp210x_serial_private {
267 #ifdef CONFIG_GPIOLIB
269 bool gpio_registered;
277 bool use_actual_rate;
281 enum cp210x_event_state {
290 struct cp210x_port_private {
292 bool has_swapped_line_ctl;
294 enum cp210x_event_state event_state;
298 static struct usb_serial_driver cp210x_device = {
300 .owner = THIS_MODULE,
303 .id_table = id_table,
306 .bulk_out_size = 256,
308 .close = cp210x_close,
309 .break_ctl = cp210x_break_ctl,
310 .set_termios = cp210x_set_termios,
311 .tx_empty = cp210x_tx_empty,
312 .throttle = usb_serial_generic_throttle,
313 .unthrottle = usb_serial_generic_unthrottle,
314 .tiocmget = cp210x_tiocmget,
315 .tiocmset = cp210x_tiocmset,
316 .get_icount = usb_serial_generic_get_icount,
317 .attach = cp210x_attach,
318 .disconnect = cp210x_disconnect,
319 .release = cp210x_release,
320 .port_probe = cp210x_port_probe,
321 .port_remove = cp210x_port_remove,
322 .dtr_rts = cp210x_dtr_rts,
323 .process_read_urb = cp210x_process_read_urb,
326 static struct usb_serial_driver * const serial_drivers[] = {
330 /* Config request types */
331 #define REQTYPE_HOST_TO_INTERFACE 0x41
332 #define REQTYPE_INTERFACE_TO_HOST 0xc1
333 #define REQTYPE_HOST_TO_DEVICE 0x40
334 #define REQTYPE_DEVICE_TO_HOST 0xc0
336 /* Config request codes */
337 #define CP210X_IFC_ENABLE 0x00
338 #define CP210X_SET_BAUDDIV 0x01
339 #define CP210X_GET_BAUDDIV 0x02
340 #define CP210X_SET_LINE_CTL 0x03
341 #define CP210X_GET_LINE_CTL 0x04
342 #define CP210X_SET_BREAK 0x05
343 #define CP210X_IMM_CHAR 0x06
344 #define CP210X_SET_MHS 0x07
345 #define CP210X_GET_MDMSTS 0x08
346 #define CP210X_SET_XON 0x09
347 #define CP210X_SET_XOFF 0x0A
348 #define CP210X_SET_EVENTMASK 0x0B
349 #define CP210X_GET_EVENTMASK 0x0C
350 #define CP210X_SET_CHAR 0x0D
351 #define CP210X_GET_CHARS 0x0E
352 #define CP210X_GET_PROPS 0x0F
353 #define CP210X_GET_COMM_STATUS 0x10
354 #define CP210X_RESET 0x11
355 #define CP210X_PURGE 0x12
356 #define CP210X_SET_FLOW 0x13
357 #define CP210X_GET_FLOW 0x14
358 #define CP210X_EMBED_EVENTS 0x15
359 #define CP210X_GET_EVENTSTATE 0x16
360 #define CP210X_SET_CHARS 0x19
361 #define CP210X_GET_BAUDRATE 0x1D
362 #define CP210X_SET_BAUDRATE 0x1E
363 #define CP210X_VENDOR_SPECIFIC 0xFF
365 /* CP210X_IFC_ENABLE */
366 #define UART_ENABLE 0x0001
367 #define UART_DISABLE 0x0000
369 /* CP210X_(SET|GET)_BAUDDIV */
370 #define BAUD_RATE_GEN_FREQ 0x384000
372 /* CP210X_(SET|GET)_LINE_CTL */
373 #define BITS_DATA_MASK 0X0f00
374 #define BITS_DATA_5 0X0500
375 #define BITS_DATA_6 0X0600
376 #define BITS_DATA_7 0X0700
377 #define BITS_DATA_8 0X0800
378 #define BITS_DATA_9 0X0900
380 #define BITS_PARITY_MASK 0x00f0
381 #define BITS_PARITY_NONE 0x0000
382 #define BITS_PARITY_ODD 0x0010
383 #define BITS_PARITY_EVEN 0x0020
384 #define BITS_PARITY_MARK 0x0030
385 #define BITS_PARITY_SPACE 0x0040
387 #define BITS_STOP_MASK 0x000f
388 #define BITS_STOP_1 0x0000
389 #define BITS_STOP_1_5 0x0001
390 #define BITS_STOP_2 0x0002
392 /* CP210X_SET_BREAK */
393 #define BREAK_ON 0x0001
394 #define BREAK_OFF 0x0000
396 /* CP210X_(SET_MHS|GET_MDMSTS) */
397 #define CONTROL_DTR 0x0001
398 #define CONTROL_RTS 0x0002
399 #define CONTROL_CTS 0x0010
400 #define CONTROL_DSR 0x0020
401 #define CONTROL_RING 0x0040
402 #define CONTROL_DCD 0x0080
403 #define CONTROL_WRITE_DTR 0x0100
404 #define CONTROL_WRITE_RTS 0x0200
406 /* CP210X_VENDOR_SPECIFIC values */
407 #define CP210X_READ_2NCONFIG 0x000E
408 #define CP210X_READ_LATCH 0x00C2
409 #define CP210X_GET_PARTNUM 0x370B
410 #define CP210X_GET_PORTCONFIG 0x370C
411 #define CP210X_GET_DEVICEMODE 0x3711
412 #define CP210X_WRITE_LATCH 0x37E1
414 /* Part number definitions */
415 #define CP210X_PARTNUM_CP2101 0x01
416 #define CP210X_PARTNUM_CP2102 0x02
417 #define CP210X_PARTNUM_CP2103 0x03
418 #define CP210X_PARTNUM_CP2104 0x04
419 #define CP210X_PARTNUM_CP2105 0x05
420 #define CP210X_PARTNUM_CP2108 0x08
421 #define CP210X_PARTNUM_CP2102N_QFN28 0x20
422 #define CP210X_PARTNUM_CP2102N_QFN24 0x21
423 #define CP210X_PARTNUM_CP2102N_QFN20 0x22
424 #define CP210X_PARTNUM_UNKNOWN 0xFF
426 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
427 struct cp210x_comm_status {
429 __le32 ulHoldReasons;
430 __le32 ulAmountInInQueue;
431 __le32 ulAmountInOutQueue;
433 u8 bWaitForImmediate;
438 * CP210X_PURGE - 16 bits passed in wValue of USB request.
439 * SiLabs app note AN571 gives a strange description of the 4 bits:
440 * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
441 * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
443 #define PURGE_ALL 0x000f
445 /* CP210X_EMBED_EVENTS */
446 #define CP210X_ESCCHAR 0xec
448 #define CP210X_LSR_OVERRUN BIT(1)
449 #define CP210X_LSR_PARITY BIT(2)
450 #define CP210X_LSR_FRAME BIT(3)
451 #define CP210X_LSR_BREAK BIT(4)
454 /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
455 struct cp210x_flow_ctl {
456 __le32 ulControlHandshake;
457 __le32 ulFlowReplace;
462 /* cp210x_flow_ctl::ulControlHandshake */
463 #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
464 #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
465 #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
466 #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
467 #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
468 #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
470 /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
471 #define CP210X_SERIAL_DTR_INACTIVE 0
472 #define CP210X_SERIAL_DTR_ACTIVE 1
473 #define CP210X_SERIAL_DTR_FLOW_CTL 2
475 /* cp210x_flow_ctl::ulFlowReplace */
476 #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
477 #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
478 #define CP210X_SERIAL_ERROR_CHAR BIT(2)
479 #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
480 #define CP210X_SERIAL_BREAK_CHAR BIT(4)
481 #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
482 #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
483 #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
485 /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
486 #define CP210X_SERIAL_RTS_INACTIVE 0
487 #define CP210X_SERIAL_RTS_ACTIVE 1
488 #define CP210X_SERIAL_RTS_FLOW_CTL 2
490 /* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
491 struct cp210x_pin_mode {
496 #define CP210X_PIN_MODE_MODEM 0
497 #define CP210X_PIN_MODE_GPIO BIT(0)
500 * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
501 * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
503 struct cp210x_dual_port_config {
508 __le16 suspend_state;
515 * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
516 * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
518 struct cp210x_single_port_config {
523 __le16 suspend_state;
528 #define CP210X_SCI_GPIO_MODE_OFFSET 9
529 #define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9)
531 #define CP210X_ECI_GPIO_MODE_OFFSET 2
532 #define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2)
534 #define CP210X_GPIO_MODE_OFFSET 8
535 #define CP210X_GPIO_MODE_MASK GENMASK(11, 8)
537 /* CP2105 port configuration values */
538 #define CP2105_GPIO0_TXLED_MODE BIT(0)
539 #define CP2105_GPIO1_RXLED_MODE BIT(1)
540 #define CP2105_GPIO1_RS485_MODE BIT(2)
542 /* CP2104 port configuration values */
543 #define CP2104_GPIO0_TXLED_MODE BIT(0)
544 #define CP2104_GPIO1_RXLED_MODE BIT(1)
545 #define CP2104_GPIO2_RS485_MODE BIT(2)
547 /* CP2102N configuration array indices */
548 #define CP210X_2NCONFIG_CONFIG_VERSION_IDX 2
549 #define CP210X_2NCONFIG_GPIO_MODE_IDX 581
550 #define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX 587
551 #define CP210X_2NCONFIG_GPIO_CONTROL_IDX 600
553 /* CP2102N QFN20 port configuration values */
554 #define CP2102N_QFN20_GPIO2_TXLED_MODE BIT(2)
555 #define CP2102N_QFN20_GPIO3_RXLED_MODE BIT(3)
556 #define CP2102N_QFN20_GPIO1_RS485_MODE BIT(4)
557 #define CP2102N_QFN20_GPIO0_CLK_MODE BIT(6)
559 /* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
560 struct cp210x_gpio_write {
566 * Helper to get interface number when we only have struct usb_serial.
568 static u8 cp210x_interface_num(struct usb_serial *serial)
570 struct usb_host_interface *cur_altsetting;
572 cur_altsetting = serial->interface->cur_altsetting;
574 return cur_altsetting->desc.bInterfaceNumber;
578 * Reads a variable-sized block of CP210X_ registers, identified by req.
579 * Returns data into buf in native USB byte order.
581 static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
582 void *buf, int bufsize)
584 struct usb_serial *serial = port->serial;
585 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
589 dmabuf = kmalloc(bufsize, GFP_KERNEL);
592 * FIXME Some callers don't bother to check for error,
593 * at least give them consistent junk until they are fixed
595 memset(buf, 0, bufsize);
599 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
600 req, REQTYPE_INTERFACE_TO_HOST, 0,
601 port_priv->bInterfaceNumber, dmabuf, bufsize,
602 USB_CTRL_SET_TIMEOUT);
603 if (result == bufsize) {
604 memcpy(buf, dmabuf, bufsize);
607 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
608 req, bufsize, result);
613 * FIXME Some callers don't bother to check for error,
614 * at least give them consistent junk until they are fixed
616 memset(buf, 0, bufsize);
625 * Reads any 32-bit CP210X_ register identified by req.
627 static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
632 err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
635 * FIXME Some callers don't bother to check for error,
636 * at least give them consistent junk until they are fixed
642 *val = le32_to_cpu(le32_val);
648 * Reads any 16-bit CP210X_ register identified by req.
650 static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
655 err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
659 *val = le16_to_cpu(le16_val);
665 * Reads any 8-bit CP210X_ register identified by req.
667 static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
669 return cp210x_read_reg_block(port, req, val, sizeof(*val));
673 * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
674 * Returns data into buf in native USB byte order.
676 static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
677 void *buf, int bufsize)
682 dmabuf = kmalloc(bufsize, GFP_KERNEL);
686 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
687 CP210X_VENDOR_SPECIFIC, type, val,
688 cp210x_interface_num(serial), dmabuf, bufsize,
689 USB_CTRL_GET_TIMEOUT);
690 if (result == bufsize) {
691 memcpy(buf, dmabuf, bufsize);
694 dev_err(&serial->interface->dev,
695 "failed to get vendor val 0x%04x size %d: %d\n", val,
707 * Writes any 16-bit CP210X_ register (req) whose value is passed
708 * entirely in the wValue field of the USB request.
710 static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
712 struct usb_serial *serial = port->serial;
713 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
716 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
717 req, REQTYPE_HOST_TO_INTERFACE, val,
718 port_priv->bInterfaceNumber, NULL, 0,
719 USB_CTRL_SET_TIMEOUT);
721 dev_err(&port->dev, "failed set request 0x%x status: %d\n",
729 * Writes a variable-sized block of CP210X_ registers, identified by req.
730 * Data in buf must be in native USB byte order.
732 static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
733 void *buf, int bufsize)
735 struct usb_serial *serial = port->serial;
736 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
740 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
744 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
745 req, REQTYPE_HOST_TO_INTERFACE, 0,
746 port_priv->bInterfaceNumber, dmabuf, bufsize,
747 USB_CTRL_SET_TIMEOUT);
751 if (result == bufsize) {
754 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
755 req, bufsize, result);
764 * Writes any 32-bit CP210X_ register identified by req.
766 static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
770 le32_val = cpu_to_le32(val);
772 return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
775 #ifdef CONFIG_GPIOLIB
777 * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
778 * Data in buf must be in native USB byte order.
780 static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
781 u16 val, void *buf, int bufsize)
786 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
790 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
791 CP210X_VENDOR_SPECIFIC, type, val,
792 cp210x_interface_num(serial), dmabuf, bufsize,
793 USB_CTRL_SET_TIMEOUT);
797 if (result == bufsize) {
800 dev_err(&serial->interface->dev,
801 "failed to set vendor val 0x%04x size %d: %d\n", val,
812 * Detect CP2108 GET_LINE_CTL bug and activate workaround.
813 * Write a known good value 0x800, read it back.
814 * If it comes back swapped the bug is detected.
815 * Preserve the original register value.
817 static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
819 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
824 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
828 err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
832 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
836 if (line_ctl_test == 8) {
837 port_priv->has_swapped_line_ctl = true;
838 line_ctl_save = swab16(line_ctl_save);
841 return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
845 * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
846 * to workaround cp2108 bug and get correct value.
848 static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
850 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
853 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
857 /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
858 if (port_priv->has_swapped_line_ctl)
864 static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
866 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
869 result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
871 dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
875 /* Configure the termios structure */
876 cp210x_get_termios(tty, port);
879 /* The baud rate must be initialised on cp2104 */
880 cp210x_change_speed(tty, port, NULL);
883 cp210x_enable_event_mode(port);
886 result = usb_serial_generic_open(tty, port);
893 cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
894 port_priv->event_mode = false;
899 static void cp210x_close(struct usb_serial_port *port)
901 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
903 usb_serial_generic_close(port);
905 /* Clear both queues; cp2108 needs this to avoid an occasional hang */
906 cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
908 cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
910 /* Disabling the interface disables event-insertion mode. */
911 port_priv->event_mode = false;
914 static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
916 if (lsr & CP210X_LSR_BREAK) {
919 } else if (lsr & CP210X_LSR_PARITY) {
920 port->icount.parity++;
922 } else if (lsr & CP210X_LSR_FRAME) {
923 port->icount.frame++;
927 if (lsr & CP210X_LSR_OVERRUN) {
928 port->icount.overrun++;
929 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
933 static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
935 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
937 switch (port_priv->event_state) {
939 if (*ch == CP210X_ESCCHAR) {
940 port_priv->event_state = ES_ESCAPE;
947 dev_dbg(&port->dev, "%s - escape char\n", __func__);
948 *ch = CP210X_ESCCHAR;
949 port_priv->event_state = ES_DATA;
952 port_priv->event_state = ES_LSR_DATA_0;
955 port_priv->event_state = ES_LSR;
958 port_priv->event_state = ES_MSR;
961 dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
962 port_priv->event_state = ES_DATA;
967 port_priv->lsr = *ch;
968 port_priv->event_state = ES_LSR_DATA_1;
971 dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
972 __func__, port_priv->lsr, *ch);
973 cp210x_process_lsr(port, port_priv->lsr, flag);
974 port_priv->event_state = ES_DATA;
977 dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
978 port_priv->lsr = *ch;
979 cp210x_process_lsr(port, port_priv->lsr, flag);
980 port_priv->event_state = ES_DATA;
983 dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
985 port_priv->event_state = ES_DATA;
992 static void cp210x_process_read_urb(struct urb *urb)
994 struct usb_serial_port *port = urb->context;
995 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
996 unsigned char *ch = urb->transfer_buffer;
1000 if (!urb->actual_length)
1003 if (port_priv->event_mode) {
1004 for (i = 0; i < urb->actual_length; i++, ch++) {
1007 if (cp210x_process_char(port, ch, &flag))
1010 tty_insert_flip_char(&port->port, *ch, flag);
1013 tty_insert_flip_string(&port->port, ch, urb->actual_length);
1015 tty_flip_buffer_push(&port->port);
1019 * Read how many bytes are waiting in the TX queue.
1021 static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
1024 struct usb_serial *serial = port->serial;
1025 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1026 struct cp210x_comm_status *sts;
1029 sts = kmalloc(sizeof(*sts), GFP_KERNEL);
1033 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1034 CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
1035 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
1036 USB_CTRL_GET_TIMEOUT);
1037 if (result == sizeof(*sts)) {
1038 *count = le32_to_cpu(sts->ulAmountInOutQueue);
1041 dev_err(&port->dev, "failed to get comm status: %d\n", result);
1051 static bool cp210x_tx_empty(struct usb_serial_port *port)
1056 err = cp210x_get_tx_queue_byte_count(port, &count);
1064 * cp210x_get_termios
1065 * Reads the baud rate, data bits, parity, stop bits and flow control mode
1066 * from the device, corrects any unsupported values, and configures the
1067 * termios structure to reflect the state of the device
1069 static void cp210x_get_termios(struct tty_struct *tty,
1070 struct usb_serial_port *port)
1075 cp210x_get_termios_port(tty->driver_data,
1076 &tty->termios.c_cflag, &baud);
1077 tty_encode_baud_rate(tty, baud, baud);
1081 cp210x_get_termios_port(port, &cflag, &baud);
1086 * cp210x_get_termios_port
1087 * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
1089 static void cp210x_get_termios_port(struct usb_serial_port *port,
1090 tcflag_t *cflagp, unsigned int *baudp)
1092 struct device *dev = &port->dev;
1094 struct cp210x_flow_ctl flow_ctl;
1100 cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
1102 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
1107 cp210x_get_line_ctl(port, &bits);
1109 switch (bits & BITS_DATA_MASK) {
1111 dev_dbg(dev, "%s - data bits = 5\n", __func__);
1115 dev_dbg(dev, "%s - data bits = 6\n", __func__);
1119 dev_dbg(dev, "%s - data bits = 7\n", __func__);
1123 dev_dbg(dev, "%s - data bits = 8\n", __func__);
1127 dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
1129 bits &= ~BITS_DATA_MASK;
1130 bits |= BITS_DATA_8;
1131 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1134 dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
1136 bits &= ~BITS_DATA_MASK;
1137 bits |= BITS_DATA_8;
1138 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1142 switch (bits & BITS_PARITY_MASK) {
1143 case BITS_PARITY_NONE:
1144 dev_dbg(dev, "%s - parity = NONE\n", __func__);
1147 case BITS_PARITY_ODD:
1148 dev_dbg(dev, "%s - parity = ODD\n", __func__);
1149 cflag |= (PARENB|PARODD);
1151 case BITS_PARITY_EVEN:
1152 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
1156 case BITS_PARITY_MARK:
1157 dev_dbg(dev, "%s - parity = MARK\n", __func__);
1158 cflag |= (PARENB|PARODD|CMSPAR);
1160 case BITS_PARITY_SPACE:
1161 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
1163 cflag |= (PARENB|CMSPAR);
1166 dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
1168 bits &= ~BITS_PARITY_MASK;
1169 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1174 switch (bits & BITS_STOP_MASK) {
1176 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
1179 dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
1180 bits &= ~BITS_STOP_MASK;
1181 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1184 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
1188 dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
1189 bits &= ~BITS_STOP_MASK;
1190 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
1194 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1196 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1197 if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
1198 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1200 * When the port is closed, the CP210x hardware disables
1201 * auto-RTS and RTS is deasserted but it leaves auto-CTS when
1202 * in hardware flow control mode. When re-opening the port, if
1203 * auto-CTS is enabled on the cp210x, then auto-RTS must be
1204 * re-enabled in the driver.
1206 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1207 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1208 flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
1209 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1210 cp210x_write_reg_block(port,
1217 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
1224 struct cp210x_rate {
1229 static const struct cp210x_rate cp210x_an205_table1[] = {
1258 { 921600, UINT_MAX }
1262 * Quantises the baud rate as per AN205 Table 1
1264 static speed_t cp210x_get_an205_rate(speed_t baud)
1268 for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
1269 if (baud <= cp210x_an205_table1[i].high)
1273 return cp210x_an205_table1[i].rate;
1276 static speed_t cp210x_get_actual_rate(speed_t baud)
1278 unsigned int prescale = 1;
1284 div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
1285 baud = 48000000 / (2 * prescale * div);
1291 * CP2101 supports the following baud rates:
1293 * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1294 * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1296 * CP2102 and CP2103 support the following additional rates:
1298 * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1301 * The device will map a requested rate to a supported one, but the result
1302 * of requests for rates greater than 1053257 is undefined (see AN205).
1304 * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1305 * respectively, with an error less than 1%. The actual rates are determined
1308 * div = round(freq / (2 x prescale x request))
1309 * actual = freq / (2 x prescale x div)
1311 * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1313 * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1316 static void cp210x_change_speed(struct tty_struct *tty,
1317 struct usb_serial_port *port, struct ktermios *old_termios)
1319 struct usb_serial *serial = port->serial;
1320 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1324 * This maps the requested rate to the actual rate, a valid rate on
1325 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
1327 * NOTE: B0 is not implemented.
1329 baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
1331 if (priv->use_actual_rate)
1332 baud = cp210x_get_actual_rate(baud);
1333 else if (baud < 1000000)
1334 baud = cp210x_get_an205_rate(baud);
1336 dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
1337 if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
1338 dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1340 baud = old_termios->c_ospeed;
1345 tty_encode_baud_rate(tty, baud, baud);
1348 static void cp210x_enable_event_mode(struct usb_serial_port *port)
1350 struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
1351 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1354 if (port_priv->event_mode)
1357 if (priv->no_event_mode)
1360 port_priv->event_state = ES_DATA;
1361 port_priv->event_mode = true;
1363 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1365 dev_err(&port->dev, "failed to enable events: %d\n", ret);
1366 port_priv->event_mode = false;
1370 static void cp210x_disable_event_mode(struct usb_serial_port *port)
1372 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1375 if (!port_priv->event_mode)
1378 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
1380 dev_err(&port->dev, "failed to disable events: %d\n", ret);
1384 port_priv->event_mode = false;
1387 static void cp210x_set_termios(struct tty_struct *tty,
1388 struct usb_serial_port *port, struct ktermios *old_termios)
1390 struct device *dev = &port->dev;
1391 unsigned int cflag, old_cflag;
1394 cflag = tty->termios.c_cflag;
1395 old_cflag = old_termios->c_cflag;
1397 if (tty->termios.c_ospeed != old_termios->c_ospeed)
1398 cp210x_change_speed(tty, port, old_termios);
1400 /* If the number of data bits is to be updated */
1401 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
1402 cp210x_get_line_ctl(port, &bits);
1403 bits &= ~BITS_DATA_MASK;
1404 switch (cflag & CSIZE) {
1406 bits |= BITS_DATA_5;
1407 dev_dbg(dev, "%s - data bits = 5\n", __func__);
1410 bits |= BITS_DATA_6;
1411 dev_dbg(dev, "%s - data bits = 6\n", __func__);
1414 bits |= BITS_DATA_7;
1415 dev_dbg(dev, "%s - data bits = 7\n", __func__);
1419 bits |= BITS_DATA_8;
1420 dev_dbg(dev, "%s - data bits = 8\n", __func__);
1423 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1424 dev_dbg(dev, "Number of data bits requested not supported by device\n");
1427 if ((cflag & (PARENB|PARODD|CMSPAR)) !=
1428 (old_cflag & (PARENB|PARODD|CMSPAR))) {
1429 cp210x_get_line_ctl(port, &bits);
1430 bits &= ~BITS_PARITY_MASK;
1431 if (cflag & PARENB) {
1432 if (cflag & CMSPAR) {
1433 if (cflag & PARODD) {
1434 bits |= BITS_PARITY_MARK;
1435 dev_dbg(dev, "%s - parity = MARK\n", __func__);
1437 bits |= BITS_PARITY_SPACE;
1438 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
1441 if (cflag & PARODD) {
1442 bits |= BITS_PARITY_ODD;
1443 dev_dbg(dev, "%s - parity = ODD\n", __func__);
1445 bits |= BITS_PARITY_EVEN;
1446 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
1450 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1451 dev_dbg(dev, "Parity mode not supported by device\n");
1454 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1455 cp210x_get_line_ctl(port, &bits);
1456 bits &= ~BITS_STOP_MASK;
1457 if (cflag & CSTOPB) {
1458 bits |= BITS_STOP_2;
1459 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
1461 bits |= BITS_STOP_1;
1462 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
1464 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1465 dev_dbg(dev, "Number of stop bits requested not supported by device\n");
1468 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
1469 struct cp210x_flow_ctl flow_ctl;
1473 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1475 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1476 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1477 dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1478 __func__, ctl_hs, flow_repl);
1480 ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1481 ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1482 ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1483 ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1484 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
1485 if (cflag & CRTSCTS) {
1486 ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1488 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1489 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1490 CP210X_SERIAL_RTS_FLOW_CTL);
1491 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1493 ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1495 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1496 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1497 CP210X_SERIAL_RTS_ACTIVE);
1498 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
1501 dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1502 __func__, ctl_hs, flow_repl);
1503 flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1504 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1505 cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1510 * Enable event-insertion mode only if input parity checking is
1514 cp210x_enable_event_mode(port);
1516 cp210x_disable_event_mode(port);
1519 static int cp210x_tiocmset(struct tty_struct *tty,
1520 unsigned int set, unsigned int clear)
1522 struct usb_serial_port *port = tty->driver_data;
1523 return cp210x_tiocmset_port(port, set, clear);
1526 static int cp210x_tiocmset_port(struct usb_serial_port *port,
1527 unsigned int set, unsigned int clear)
1531 if (set & TIOCM_RTS) {
1532 control |= CONTROL_RTS;
1533 control |= CONTROL_WRITE_RTS;
1535 if (set & TIOCM_DTR) {
1536 control |= CONTROL_DTR;
1537 control |= CONTROL_WRITE_DTR;
1539 if (clear & TIOCM_RTS) {
1540 control &= ~CONTROL_RTS;
1541 control |= CONTROL_WRITE_RTS;
1543 if (clear & TIOCM_DTR) {
1544 control &= ~CONTROL_DTR;
1545 control |= CONTROL_WRITE_DTR;
1548 dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
1550 return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
1553 static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1556 cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
1558 cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
1561 static int cp210x_tiocmget(struct tty_struct *tty)
1563 struct usb_serial_port *port = tty->driver_data;
1567 result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1571 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
1572 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
1573 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
1574 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
1575 |((control & CONTROL_RING)? TIOCM_RI : 0)
1576 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
1578 dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
1583 static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
1585 struct usb_serial_port *port = tty->driver_data;
1588 if (break_state == 0)
1592 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1593 state == BREAK_OFF ? "off" : "on");
1594 cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
1597 #ifdef CONFIG_GPIOLIB
1598 static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
1600 struct usb_serial *serial = gpiochip_get_data(gc);
1601 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1603 if (priv->gpio_altfunc & BIT(offset))
1609 static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1611 struct usb_serial *serial = gpiochip_get_data(gc);
1612 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1613 u8 req_type = REQTYPE_DEVICE_TO_HOST;
1617 if (priv->partnum == CP210X_PARTNUM_CP2105)
1618 req_type = REQTYPE_INTERFACE_TO_HOST;
1620 result = usb_autopm_get_interface(serial->interface);
1624 result = cp210x_read_vendor_block(serial, req_type,
1625 CP210X_READ_LATCH, &buf, sizeof(buf));
1626 usb_autopm_put_interface(serial->interface);
1630 return !!(buf & BIT(gpio));
1633 static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1635 struct usb_serial *serial = gpiochip_get_data(gc);
1636 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1637 struct cp210x_gpio_write buf;
1641 buf.state = BIT(gpio);
1645 buf.mask = BIT(gpio);
1647 result = usb_autopm_get_interface(serial->interface);
1651 if (priv->partnum == CP210X_PARTNUM_CP2105) {
1652 result = cp210x_write_vendor_block(serial,
1653 REQTYPE_HOST_TO_INTERFACE,
1654 CP210X_WRITE_LATCH, &buf,
1657 u16 wIndex = buf.state << 8 | buf.mask;
1659 result = usb_control_msg(serial->dev,
1660 usb_sndctrlpipe(serial->dev, 0),
1661 CP210X_VENDOR_SPECIFIC,
1662 REQTYPE_HOST_TO_DEVICE,
1665 NULL, 0, USB_CTRL_SET_TIMEOUT);
1668 usb_autopm_put_interface(serial->interface);
1671 dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
1676 static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1678 struct usb_serial *serial = gpiochip_get_data(gc);
1679 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1681 return priv->gpio_input & BIT(gpio);
1684 static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1686 struct usb_serial *serial = gpiochip_get_data(gc);
1687 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1689 if (priv->partnum == CP210X_PARTNUM_CP2105) {
1690 /* hardware does not support an input mode */
1694 /* push-pull pins cannot be changed to be inputs */
1695 if (priv->gpio_pushpull & BIT(gpio))
1698 /* make sure to release pin if it is being driven low */
1699 cp210x_gpio_set(gc, gpio, 1);
1701 priv->gpio_input |= BIT(gpio);
1706 static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1709 struct usb_serial *serial = gpiochip_get_data(gc);
1710 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1712 priv->gpio_input &= ~BIT(gpio);
1713 cp210x_gpio_set(gc, gpio, value);
1718 static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
1719 unsigned long config)
1721 struct usb_serial *serial = gpiochip_get_data(gc);
1722 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1723 enum pin_config_param param = pinconf_to_config_param(config);
1725 /* Succeed only if in correct mode (this can't be set at runtime) */
1726 if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
1727 (priv->gpio_pushpull & BIT(gpio)))
1730 if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
1731 !(priv->gpio_pushpull & BIT(gpio)))
1738 * This function is for configuring GPIO using shared pins, where other signals
1739 * are made unavailable by configuring the use of GPIO. This is believed to be
1740 * only applicable to the cp2105 at this point, the other devices supported by
1741 * this driver that provide GPIO do so in a way that does not impact other
1742 * signals and are thus expected to have very different initialisation.
1744 static int cp2105_gpioconf_init(struct usb_serial *serial)
1746 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1747 struct cp210x_pin_mode mode;
1748 struct cp210x_dual_port_config config;
1749 u8 intf_num = cp210x_interface_num(serial);
1753 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1754 CP210X_GET_DEVICEMODE, &mode,
1759 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1760 CP210X_GET_PORTCONFIG, &config,
1765 /* 2 banks of GPIO - One for the pins taken from each serial port */
1766 if (intf_num == 0) {
1769 if (mode.eci == CP210X_PIN_MODE_MODEM) {
1770 /* mark all GPIOs of this interface as reserved */
1771 priv->gpio_altfunc = 0xff;
1775 iface_config = config.eci_cfg;
1776 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1777 CP210X_ECI_GPIO_MODE_MASK) >>
1778 CP210X_ECI_GPIO_MODE_OFFSET);
1779 } else if (intf_num == 1) {
1782 if (mode.sci == CP210X_PIN_MODE_MODEM) {
1783 /* mark all GPIOs of this interface as reserved */
1784 priv->gpio_altfunc = 0xff;
1788 iface_config = config.sci_cfg;
1789 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1790 CP210X_SCI_GPIO_MODE_MASK) >>
1791 CP210X_SCI_GPIO_MODE_OFFSET);
1796 /* mark all pins which are not in GPIO mode */
1797 if (iface_config & CP2105_GPIO0_TXLED_MODE) /* GPIO 0 */
1798 priv->gpio_altfunc |= BIT(0);
1799 if (iface_config & (CP2105_GPIO1_RXLED_MODE | /* GPIO 1 */
1800 CP2105_GPIO1_RS485_MODE))
1801 priv->gpio_altfunc |= BIT(1);
1803 /* driver implementation for CP2105 only supports outputs */
1804 priv->gpio_input = 0;
1809 static int cp2104_gpioconf_init(struct usb_serial *serial)
1811 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1812 struct cp210x_single_port_config config;
1818 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1819 CP210X_GET_PORTCONFIG, &config,
1826 iface_config = config.device_cfg;
1827 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1828 CP210X_GPIO_MODE_MASK) >>
1829 CP210X_GPIO_MODE_OFFSET);
1830 gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
1831 CP210X_GPIO_MODE_MASK) >>
1832 CP210X_GPIO_MODE_OFFSET);
1834 /* mark all pins which are not in GPIO mode */
1835 if (iface_config & CP2104_GPIO0_TXLED_MODE) /* GPIO 0 */
1836 priv->gpio_altfunc |= BIT(0);
1837 if (iface_config & CP2104_GPIO1_RXLED_MODE) /* GPIO 1 */
1838 priv->gpio_altfunc |= BIT(1);
1839 if (iface_config & CP2104_GPIO2_RS485_MODE) /* GPIO 2 */
1840 priv->gpio_altfunc |= BIT(2);
1843 * Like CP2102N, CP2104 has also no strict input and output pin
1845 * Do the same input mode emulation as CP2102N.
1847 for (i = 0; i < priv->gc.ngpio; ++i) {
1849 * Set direction to "input" iff pin is open-drain and reset
1852 if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1853 priv->gpio_input |= BIT(i);
1859 static int cp2102n_gpioconf_init(struct usb_serial *serial)
1861 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1862 const u16 config_size = 0x02a6;
1873 * Retrieve device configuration from the device.
1874 * The array received contains all customization settings done at the
1875 * factory/manufacturer. Format of the array is documented at the
1876 * time of writing at:
1877 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
1879 config_buf = kmalloc(config_size, GFP_KERNEL);
1883 result = cp210x_read_vendor_block(serial,
1884 REQTYPE_DEVICE_TO_HOST,
1885 CP210X_READ_2NCONFIG,
1893 config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
1894 gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
1895 gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
1896 gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
1900 /* Make sure this is a config format we understand. */
1901 if (config_version != 0x01)
1907 * Get default pin states after reset. Needed so we can determine
1908 * the direction of an open-drain pin.
1910 gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
1912 /* 0 indicates open-drain mode, 1 is push-pull */
1913 priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
1915 /* 0 indicates GPIO mode, 1 is alternate function */
1916 if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20) {
1917 /* QFN20 is special... */
1918 if (gpio_ctrl & CP2102N_QFN20_GPIO0_CLK_MODE) /* GPIO 0 */
1919 priv->gpio_altfunc |= BIT(0);
1920 if (gpio_ctrl & CP2102N_QFN20_GPIO1_RS485_MODE) /* GPIO 1 */
1921 priv->gpio_altfunc |= BIT(1);
1922 if (gpio_ctrl & CP2102N_QFN20_GPIO2_TXLED_MODE) /* GPIO 2 */
1923 priv->gpio_altfunc |= BIT(2);
1924 if (gpio_ctrl & CP2102N_QFN20_GPIO3_RXLED_MODE) /* GPIO 3 */
1925 priv->gpio_altfunc |= BIT(3);
1927 priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
1930 if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
1932 * For the QFN28 package, GPIO4-6 are controlled by
1933 * the low three bits of the mode/latch fields.
1934 * Contrary to the document linked above, the bits for
1935 * the SUSPEND pins are elsewhere. No alternate
1936 * function is available for these pins.
1939 gpio_latch |= (gpio_rst_latch & 7) << 4;
1940 priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
1944 * The CP2102N does not strictly has input and output pin modes,
1945 * it only knows open-drain and push-pull modes which is set at
1946 * factory. An open-drain pin can function both as an
1947 * input or an output. We emulate input mode for open-drain pins
1948 * by making sure they are not driven low, and we do not allow
1949 * push-pull pins to be set as an input.
1951 for (i = 0; i < priv->gc.ngpio; ++i) {
1953 * Set direction to "input" iff pin is open-drain and reset
1956 if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1957 priv->gpio_input |= BIT(i);
1963 static int cp210x_gpio_init(struct usb_serial *serial)
1965 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1968 switch (priv->partnum) {
1969 case CP210X_PARTNUM_CP2104:
1970 result = cp2104_gpioconf_init(serial);
1972 case CP210X_PARTNUM_CP2105:
1973 result = cp2105_gpioconf_init(serial);
1975 case CP210X_PARTNUM_CP2102N_QFN28:
1976 case CP210X_PARTNUM_CP2102N_QFN24:
1977 case CP210X_PARTNUM_CP2102N_QFN20:
1978 result = cp2102n_gpioconf_init(serial);
1987 priv->gc.label = "cp210x";
1988 priv->gc.request = cp210x_gpio_request;
1989 priv->gc.get_direction = cp210x_gpio_direction_get;
1990 priv->gc.direction_input = cp210x_gpio_direction_input;
1991 priv->gc.direction_output = cp210x_gpio_direction_output;
1992 priv->gc.get = cp210x_gpio_get;
1993 priv->gc.set = cp210x_gpio_set;
1994 priv->gc.set_config = cp210x_gpio_set_config;
1995 priv->gc.owner = THIS_MODULE;
1996 priv->gc.parent = &serial->interface->dev;
1998 priv->gc.can_sleep = true;
2000 result = gpiochip_add_data(&priv->gc, serial);
2002 priv->gpio_registered = true;
2007 static void cp210x_gpio_remove(struct usb_serial *serial)
2009 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2011 if (priv->gpio_registered) {
2012 gpiochip_remove(&priv->gc);
2013 priv->gpio_registered = false;
2019 static int cp210x_gpio_init(struct usb_serial *serial)
2024 static void cp210x_gpio_remove(struct usb_serial *serial)
2031 static int cp210x_port_probe(struct usb_serial_port *port)
2033 struct usb_serial *serial = port->serial;
2034 struct cp210x_port_private *port_priv;
2037 port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
2041 port_priv->bInterfaceNumber = cp210x_interface_num(serial);
2043 usb_set_serial_port_data(port, port_priv);
2045 ret = cp210x_detect_swapped_line_ctl(port);
2054 static int cp210x_port_remove(struct usb_serial_port *port)
2056 struct cp210x_port_private *port_priv;
2058 port_priv = usb_get_serial_port_data(port);
2064 static void cp210x_init_max_speed(struct usb_serial *serial)
2066 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2067 bool use_actual_rate = false;
2071 switch (priv->partnum) {
2072 case CP210X_PARTNUM_CP2101:
2075 case CP210X_PARTNUM_CP2102:
2076 case CP210X_PARTNUM_CP2103:
2079 case CP210X_PARTNUM_CP2104:
2080 use_actual_rate = true;
2083 case CP210X_PARTNUM_CP2108:
2086 case CP210X_PARTNUM_CP2105:
2087 if (cp210x_interface_num(serial) == 0) {
2088 use_actual_rate = true;
2089 max = 2000000; /* ECI */
2092 max = 921600; /* SCI */
2095 case CP210X_PARTNUM_CP2102N_QFN28:
2096 case CP210X_PARTNUM_CP2102N_QFN24:
2097 case CP210X_PARTNUM_CP2102N_QFN20:
2098 use_actual_rate = true;
2106 priv->min_speed = min;
2107 priv->max_speed = max;
2108 priv->use_actual_rate = use_actual_rate;
2111 static void cp2102_determine_quirks(struct usb_serial *serial)
2113 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2117 buf = kmalloc(2, GFP_KERNEL);
2121 * Some (possibly counterfeit) CP2102 do not support event-insertion
2122 * mode and respond differently to malformed vendor requests.
2123 * Specifically, they return one instead of two bytes when sent a
2124 * two-byte part-number request.
2126 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2127 CP210X_VENDOR_SPECIFIC, REQTYPE_DEVICE_TO_HOST,
2128 CP210X_GET_PARTNUM, 0, buf, 2, USB_CTRL_GET_TIMEOUT);
2130 dev_dbg(&serial->interface->dev,
2131 "device does not support event-insertion mode\n");
2132 priv->no_event_mode = true;
2138 static void cp210x_determine_quirks(struct usb_serial *serial)
2140 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2142 switch (priv->partnum) {
2143 case CP210X_PARTNUM_CP2102:
2144 cp2102_determine_quirks(serial);
2151 static int cp210x_attach(struct usb_serial *serial)
2154 struct cp210x_serial_private *priv;
2156 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2160 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
2161 CP210X_GET_PARTNUM, &priv->partnum,
2162 sizeof(priv->partnum));
2164 dev_warn(&serial->interface->dev,
2165 "querying part number failed\n");
2166 priv->partnum = CP210X_PARTNUM_UNKNOWN;
2169 usb_set_serial_data(serial, priv);
2171 cp210x_determine_quirks(serial);
2172 cp210x_init_max_speed(serial);
2174 result = cp210x_gpio_init(serial);
2176 dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
2183 static void cp210x_disconnect(struct usb_serial *serial)
2185 cp210x_gpio_remove(serial);
2188 static void cp210x_release(struct usb_serial *serial)
2190 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2192 cp210x_gpio_remove(serial);
2197 module_usb_serial_driver(serial_drivers, id_table);
2199 MODULE_DESCRIPTION(DRIVER_DESC);
2200 MODULE_LICENSE("GPL v2");