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