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