blob: 02e4acb2823bdc9364397ddcd48fcc72c4aea35d [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Craig Shelley4cc27bd2009-06-11 13:54:40 +01003 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
6 *
Craig Shelley39a66b82005-05-27 00:09:56 +01007 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Alan Coxff7eb602008-07-22 11:10:17 +010021#include <linux/uaccess.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070022#include <linux/usb/serial.h>
Martyn Welchcf5276c2016-10-20 15:13:54 +010023#include <linux/gpio/driver.h>
24#include <linux/bitops.h>
25#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Craig Shelley4cc27bd2009-06-11 13:54:40 +010027#define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Function Prototypes
31 */
Alan Coxa509a7e2009-09-19 13:13:26 -070032static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
Craig Shelley4cc27bd2009-06-11 13:54:40 +010033static void cp210x_close(struct usb_serial_port *);
Johan Hovold4f2ab882012-10-29 10:56:19 +010034static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
Craig Shelley4cc27bd2009-06-11 13:54:40 +010035static void cp210x_get_termios_port(struct usb_serial_port *port,
Geert Uytterhoeven009615ab2016-11-21 13:19:31 +010036 tcflag_t *cflagp, unsigned int *baudp);
Johan Hovolde5990872012-01-16 00:36:51 +010037static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
38 struct ktermios *);
Craig Shelley4cc27bd2009-06-11 13:54:40 +010039static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
Alan Cox95da3102008-07-22 11:09:07 +010040 struct ktermios*);
Konstantin Shkolnyy6f923a02015-11-24 16:28:41 -060041static bool cp210x_tx_empty(struct usb_serial_port *port);
Alan Cox60b33c12011-02-14 16:26:14 +000042static int cp210x_tiocmget(struct tty_struct *);
Alan Cox20b9d172011-02-14 16:26:50 +000043static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
44static int cp210x_tiocmset_port(struct usb_serial_port *port,
VomLehnd2ad67b2009-03-12 14:37:42 -070045 unsigned int, unsigned int);
Craig Shelley4cc27bd2009-06-11 13:54:40 +010046static void cp210x_break_ctl(struct tty_struct *, int);
Martyn Welchcf5276c2016-10-20 15:13:54 +010047static int cp210x_attach(struct usb_serial *);
48static void cp210x_disconnect(struct usb_serial *);
49static void cp210x_release(struct usb_serial *);
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -050050static int cp210x_port_probe(struct usb_serial_port *);
51static int cp210x_port_remove(struct usb_serial_port *);
Alan Coxd94c7bd2009-10-28 21:12:33 +010052static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
Johan Hovolda7207e92020-07-13 12:55:14 +020053static void cp210x_process_read_urb(struct urb *urb);
54static void cp210x_enable_event_mode(struct usb_serial_port *port);
55static void cp210x_disable_event_mode(struct usb_serial_port *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Németh Márton7d40d7e2010-01-10 15:34:24 +010057static const struct usb_device_id id_table[] = {
DJ Delorie2f1136d2010-09-17 11:09:06 -040058 { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
M Kondrin5091b582008-11-14 13:02:45 +030059 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
Craig Shelley59fba742008-04-12 16:15:54 +010060 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
A E Lawrence5b22a322010-08-29 21:51:52 +010061 { USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +010062 { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
Luiz Angelo Daros de Luca90625072013-07-01 23:56:25 -030063 { USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
Edouard Lafargue1f4f7142007-09-25 10:15:36 +020064 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +010065 { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
Lennart Sorensena6f03312015-01-21 15:24:27 -050066 { USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
Ivan Mironovdd9d3d82019-02-06 21:14:13 +050067 { USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
Craig Shelleyeefd9022010-05-15 13:36:38 +010068 { USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
69 { USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
Craig Shelley198b9512005-08-28 09:51:15 +010070 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
Martin Kusserow014840e2007-12-21 12:02:17 +010071 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
Craig Shelley59fba742008-04-12 16:15:54 +010072 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
Luis Llorente Campo8de7f4d2013-01-31 13:37:05 +010073 { USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
Craig Shelley198b9512005-08-28 09:51:15 +010074 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
Craig Shelleye988fc82006-01-20 00:06:19 +000075 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
76 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +010077 { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
Amit Kucheria0601e112009-07-06 14:19:59 +030078 { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
79 { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
Craig Shelley5de8b3a2008-01-03 12:45:59 +000080 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
Craig Shelley59224f52006-03-11 11:29:02 +000081 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
Craig Shelleyeefd9022010-05-15 13:36:38 +010082 { USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
83 { USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
Craig Shelley92f5cae22007-04-20 14:37:17 +010084 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
Damir N Abdullin9a18e752008-10-30 13:52:38 -070085 { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
Greg Kroah-Hartmana595ecdd2019-03-27 10:11:14 +090086 { USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
Vitja Makarov212a4b42006-05-31 00:40:06 +040087 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
Craig Shelleyeefd9022010-05-15 13:36:38 +010088 { USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
Craig Shelleye988fc82006-01-20 00:06:19 +000089 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
Mikko Tuumanen5bbfa6f2012-06-01 11:28:55 +030090 { USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
Craig Shelleye988fc82006-01-20 00:06:19 +000091 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
Craig Shelley92f5cae22007-04-20 14:37:17 +010092 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
Craig Shelleye988fc82006-01-20 00:06:19 +000093 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
Craig Shelleyba0f3322008-09-04 21:28:21 +010094 { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
Craig Shelleye988fc82006-01-20 00:06:19 +000095 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
Craig Shelley8bf16ba2009-07-12 21:52:33 +010096 { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
Jon K Hellanfc4bd642007-02-28 23:04:33 +010097 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
98 { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
Steve Conklina57e82a2013-03-07 17:19:33 -060099 { USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
Jason Detring0bf7a812010-08-26 15:08:54 -0500100 { USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
Craig Shelleye988fc82006-01-20 00:06:19 +0000101 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
Craig Shelley3fcc8f92012-06-26 23:20:04 +0100102 { USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
Johan Hovold24160622018-06-18 10:24:03 +0200103 { USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
104 { USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
105 { USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
Craig Shelleyeefd9022010-05-15 13:36:38 +0100106 { USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
Robert M. Kenneyc6535662009-02-26 14:58:39 -0500107 { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
Bernhard Beck966236972008-05-11 09:23:13 -0700108 { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
Malte Schröder08e87d02012-01-05 20:34:40 +0100109 { USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
Craig Shelley59fba742008-04-12 16:15:54 +0100110 { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
Craig Shelleyeefd9022010-05-15 13:36:38 +0100111 { USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
Bjorn Schneider78001e32006-10-28 12:42:04 +0200112 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
Peter Dedeckerf487c542016-01-08 12:34:41 +0100113 { USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
Bjorn Schneider78001e32006-10-28 12:42:04 +0200114 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
Jeff Long8be27c22007-11-28 11:02:09 -0500115 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
Alan Coxbd07c552010-02-08 10:10:44 +0000116 { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +0100117 { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
Bjorn Schneider78001e32006-10-28 12:42:04 +0200118 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
Tomasz K877e2622009-01-04 12:47:11 +0100119 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300120 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
Tristan Bruns72b30072014-04-13 23:57:16 +0200121 { USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
Florian Fainellic851e832011-05-10 11:17:33 +0200122 { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
Johan Hovold24160622018-06-18 10:24:03 +0200123 { USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
124 { USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
125 { USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
Jasem Mutlaq613ac232016-04-19 10:38:27 +0300126 { USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +0100127 { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
Craig Shelleyba0f3322008-09-04 21:28:21 +0100128 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
Craig Shelley8bf16ba2009-07-12 21:52:33 +0100129 { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
Craig Shelley881e3c92008-11-09 20:17:54 +0000130 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
Greg Kroah-Hartman347bc8c2019-11-18 10:21:19 +0100131 { USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
Craig Shelleyfaea63f2011-01-02 21:51:46 +0000132 { USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
Craig Shelley8bf16ba2009-07-12 21:52:33 +0100133 { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
Craig Shelleyfaea63f2011-01-02 21:51:46 +0000134 { USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
Alex Stephensd23bac92009-03-17 00:06:19 +0000135 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
Kyle Jonesdecc5362016-09-23 13:28:37 -0500136 { USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
Craig Shelley541e05e2010-08-23 20:50:57 +0100137 { USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
Jasem Mutlaq613ac232016-04-19 10:38:27 +0300138 { USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
Johan Hovold24160622018-06-18 10:24:03 +0200139 { USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
Diego Elio Pettenò43074132017-12-29 09:54:25 +0000140 { USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
Johan Hovold24160622018-06-18 10:24:03 +0200141 { USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
Craig Shelley4eff0b42011-03-20 13:51:13 +0000142 { USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
143 { USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
Markus Becker356fe442012-11-22 09:41:23 +0100144 { USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
Craig Shelley4eff0b42011-03-20 13:51:13 +0000145 { USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
146 { USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
David Peterson1ae78a42015-01-06 15:00:52 +0000147 { USB_DEVICE(0x10C4, 0x8856) }, /* CEL EM357 ZigBee USB Stick - LR */
148 { USB_DEVICE(0x10C4, 0x8857) }, /* CEL EM357 ZigBee USB Stick */
Sami Rahman76811562013-07-08 14:28:55 -0400149 { USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
150 { USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
Johan Hovold24160622018-06-18 10:24:03 +0200151 { USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
152 { USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
Joe Savagebfc2d7d2014-09-20 08:01:16 -0500153 { USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
Paul Jakma2ab13292016-11-16 10:13:49 +0000154 { USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
David Peterson1ae78a42015-01-06 15:00:52 +0000155 { USB_DEVICE(0x10C4, 0x8977) }, /* CEL MeshWorks DevKit Device */
Mark Edwardsc735ed72015-04-14 08:52:34 -0400156 { USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
Johan Hovold24160622018-06-18 10:24:03 +0200157 { USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
Olli Salonen367b1602018-07-04 14:07:42 +0300158 { USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
John D. Blairdf72d582015-06-04 13:18:19 -0700159 { USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
Jeremie Rapinfd90f732017-06-28 18:23:25 +0200160 { USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
Stefan Triller9585e342017-06-30 14:44:03 +0200161 { USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
Craig Shelleye988fc82006-01-20 00:06:19 +0000162 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
Craig Shelley61926b92006-10-12 22:09:56 +0100163 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
Karoly Pados2f839822018-06-09 13:26:08 +0200164 { USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
Preston Ficka5360a52012-02-24 13:42:39 -0600165 { USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
Craig Shelleyeefd9022010-05-15 13:36:38 +0100166 { USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
Karoly Pados2f839822018-06-09 13:26:08 +0200167 { USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
168 { USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
Craig Shelley5de8b3a2008-01-03 12:45:59 +0000169 { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
170 { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
171 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
172 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
Pozsar Balazsebaf4942007-02-16 12:47:49 +0100173 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +0100174 { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
Mike Manning1d377f42016-04-18 12:13:23 +0000175 { USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
176 { USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
Johannes Hoelzle05998d2006-12-02 16:54:27 +0100177 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
Craig Shelleyb7c7cbc2009-06-11 13:52:31 +0100178 { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
Johan Hovold1f1e82f2018-03-06 09:32:43 +0100179 { USB_DEVICE(0x155A, 0x1006) }, /* ELDAT Easywave RX09 */
Craig Shelley3fcc8f92012-06-26 23:20:04 +0100180 { USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
181 { USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
Craig Shelley5de8b3a2008-01-03 12:45:59 +0000182 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
Craig Shelley3fcc8f92012-06-26 23:20:04 +0100183 { USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
184 { USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
185 { USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
186 { USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
Michiel vd Garde675af702015-02-27 02:08:29 +0100187 { USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
188 { USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
Josef Balatkab0ce84d2005-11-17 09:47:24 -0800189 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
Alessio Igor Bogani356c5a42010-07-27 23:05:14 +0200190 { USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
191 { USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
192 { USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
193 { USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
Bruno Thomsenc6c1e442012-02-21 23:41:37 +0100194 { USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
195 { USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
Craig Shelley541e05e2010-08-23 20:50:57 +0100196 { USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
197 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
198 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
Oliver Freyermuthf7d7f592015-12-28 18:37:38 +0100199 { USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
Christian Holld14ac572018-01-03 19:53:02 +0100200 { USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
Andreas Engelc496ad82017-09-18 21:11:57 +0200201 { USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
Ken Lin6627ae12016-02-01 14:57:25 -0500202 { USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
203 { USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
Martyn Welchcddc9432016-03-29 17:47:29 +0100204 { USB_DEVICE(0x1901, 0x0194) }, /* GE Healthcare Remote Alarm Box */
Ken Lin9a593652017-02-04 04:00:24 +0800205 { USB_DEVICE(0x1901, 0x0195) }, /* GE B850/B650/B450 CP2104 DP UART interface */
206 { USB_DEVICE(0x1901, 0x0196) }, /* GE B850 CP2105 DP UART interface */
Vittorio Alfieri3c4c6152016-02-28 14:40:24 +0100207 { USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
Barry Grusslingb579fa52013-07-19 14:46:12 -0700208 { USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
Andras Kovacsb9326052014-06-27 14:50:11 +0200209 { USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
Nathaniel Ting35cc83e2014-10-03 12:01:20 -0400210 { USB_DEVICE(0x1BA4, 0x0002) }, /* Silicon Labs 358x factory default */
Anders Larsen93ad03d2010-10-06 23:46:25 +0200211 { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
Andreas Bomholtzdee80ad2014-09-22 09:50:43 +0200212 { USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
Craig Shelley3fcc8f92012-06-26 23:20:04 +0100213 { USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
214 { USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
Matwey V. Kornilovbe3101c2013-03-09 13:57:32 +0400215 { USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
216 { USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
217 { USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
218 { USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
219 { USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
220 { USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
221 { USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
222 { USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
223 { USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
224 { USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
225 { USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
226 { USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
227 { USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
228 { USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
229 { USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
230 { USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
231 { USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
232 { USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
233 { USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
Peter Sanfordf98a7aa2015-06-25 17:40:05 -0700234 { USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
Renato Caldas791b7d72012-01-06 15:20:51 +0000235 { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
Craig Shelley3fcc8f92012-06-26 23:20:04 +0100236 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
237 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
Kyle Roeschley1e23aac2018-04-09 10:23:55 -0500238 { USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
Craig Shelley541e05e2010-08-23 20:50:57 +0100239 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
Craig Shelley39a66b82005-05-27 00:09:56 +0100240 { } /* Terminating Entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
Alan Coxff7eb602008-07-22 11:10:17 +0100243MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Martyn Welchcf5276c2016-10-20 15:13:54 +0100245struct cp210x_serial_private {
246#ifdef CONFIG_GPIOLIB
247 struct gpio_chip gc;
Johan Hovold6b7271d22016-10-24 11:58:12 +0200248 bool gpio_registered;
Karoly Padosc8acfe02018-07-20 12:52:40 +0200249 u8 gpio_pushpull;
250 u8 gpio_altfunc;
251 u8 gpio_input;
Martyn Welchcf5276c2016-10-20 15:13:54 +0100252#endif
253 u8 partnum;
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +0100254 speed_t min_speed;
Johan Hovoldd4706c052018-07-18 14:24:58 +0200255 speed_t max_speed;
Johan Hovold7aecd7f2018-07-18 14:25:00 +0200256 bool use_actual_rate;
Martyn Welchcf5276c2016-10-20 15:13:54 +0100257};
258
Johan Hovolda7207e92020-07-13 12:55:14 +0200259enum cp210x_event_state {
260 ES_DATA,
261 ES_ESCAPE,
262 ES_LSR,
263 ES_LSR_DATA_0,
264 ES_LSR_DATA_1,
265 ES_MSR
266};
267
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -0500268struct cp210x_port_private {
Preston Ficka5360a52012-02-24 13:42:39 -0600269 __u8 bInterfaceNumber;
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500270 bool has_swapped_line_ctl;
Johan Hovolda7207e92020-07-13 12:55:14 +0200271 bool event_mode;
272 enum cp210x_event_state event_state;
273 u8 lsr;
Preston Ficka5360a52012-02-24 13:42:39 -0600274};
275
Craig Shelley4cc27bd2009-06-11 13:54:40 +0100276static struct usb_serial_driver cp210x_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700277 .driver = {
278 .owner = THIS_MODULE,
Johan Hovold4f2ab882012-10-29 10:56:19 +0100279 .name = "cp210x",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700280 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .num_ports = 1,
Johan Hovoldaea006b2010-03-17 23:00:39 +0100283 .bulk_in_size = 256,
Johan Hovoldd4e598f2010-03-17 23:00:38 +0100284 .bulk_out_size = 256,
Craig Shelley4cc27bd2009-06-11 13:54:40 +0100285 .open = cp210x_open,
286 .close = cp210x_close,
287 .break_ctl = cp210x_break_ctl,
288 .set_termios = cp210x_set_termios,
Konstantin Shkolnyy6f923a02015-11-24 16:28:41 -0600289 .tx_empty = cp210x_tx_empty,
Brant Merryman4387b3d2020-06-26 04:22:58 +0000290 .throttle = usb_serial_generic_throttle,
291 .unthrottle = usb_serial_generic_unthrottle,
Johan Hovold4f2ab882012-10-29 10:56:19 +0100292 .tiocmget = cp210x_tiocmget,
Craig Shelley4cc27bd2009-06-11 13:54:40 +0100293 .tiocmset = cp210x_tiocmset,
Martyn Welchcf5276c2016-10-20 15:13:54 +0100294 .attach = cp210x_attach,
295 .disconnect = cp210x_disconnect,
296 .release = cp210x_release,
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -0500297 .port_probe = cp210x_port_probe,
298 .port_remove = cp210x_port_remove,
Johan Hovolda7207e92020-07-13 12:55:14 +0200299 .dtr_rts = cp210x_dtr_rts,
300 .process_read_urb = cp210x_process_read_urb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Alan Stern08a4f6b2012-02-23 14:56:17 -0500303static struct usb_serial_driver * const serial_drivers[] = {
304 &cp210x_device, NULL
305};
306
Craig Shelley39a66b82005-05-27 00:09:56 +0100307/* Config request types */
Preston Fick32445602012-04-30 23:06:48 -0500308#define REQTYPE_HOST_TO_INTERFACE 0x41
309#define REQTYPE_INTERFACE_TO_HOST 0xc1
310#define REQTYPE_HOST_TO_DEVICE 0x40
311#define REQTYPE_DEVICE_TO_HOST 0xc0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Craig Shelley93ef1f12009-06-11 13:53:30 +0100313/* Config request codes */
314#define CP210X_IFC_ENABLE 0x00
315#define CP210X_SET_BAUDDIV 0x01
316#define CP210X_GET_BAUDDIV 0x02
317#define CP210X_SET_LINE_CTL 0x03
318#define CP210X_GET_LINE_CTL 0x04
319#define CP210X_SET_BREAK 0x05
320#define CP210X_IMM_CHAR 0x06
321#define CP210X_SET_MHS 0x07
322#define CP210X_GET_MDMSTS 0x08
323#define CP210X_SET_XON 0x09
324#define CP210X_SET_XOFF 0x0A
325#define CP210X_SET_EVENTMASK 0x0B
326#define CP210X_GET_EVENTMASK 0x0C
327#define CP210X_SET_CHAR 0x0D
328#define CP210X_GET_CHARS 0x0E
329#define CP210X_GET_PROPS 0x0F
330#define CP210X_GET_COMM_STATUS 0x10
331#define CP210X_RESET 0x11
332#define CP210X_PURGE 0x12
333#define CP210X_SET_FLOW 0x13
334#define CP210X_GET_FLOW 0x14
335#define CP210X_EMBED_EVENTS 0x15
336#define CP210X_GET_EVENTSTATE 0x16
337#define CP210X_SET_CHARS 0x19
Preston Fick7f482fc2012-01-16 18:14:09 -0600338#define CP210X_GET_BAUDRATE 0x1D
339#define CP210X_SET_BAUDRATE 0x1E
Martyn Welchcf5276c2016-10-20 15:13:54 +0100340#define CP210X_VENDOR_SPECIFIC 0xFF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Craig Shelley93ef1f12009-06-11 13:53:30 +0100342/* CP210X_IFC_ENABLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#define UART_ENABLE 0x0001
344#define UART_DISABLE 0x0000
345
Craig Shelley93ef1f12009-06-11 13:53:30 +0100346/* CP210X_(SET|GET)_BAUDDIV */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#define BAUD_RATE_GEN_FREQ 0x384000
348
Craig Shelley93ef1f12009-06-11 13:53:30 +0100349/* CP210X_(SET|GET)_LINE_CTL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#define BITS_DATA_MASK 0X0f00
Craig Shelley39a66b82005-05-27 00:09:56 +0100351#define BITS_DATA_5 0X0500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#define BITS_DATA_6 0X0600
353#define BITS_DATA_7 0X0700
354#define BITS_DATA_8 0X0800
355#define BITS_DATA_9 0X0900
356
357#define BITS_PARITY_MASK 0x00f0
358#define BITS_PARITY_NONE 0x0000
359#define BITS_PARITY_ODD 0x0010
360#define BITS_PARITY_EVEN 0x0020
361#define BITS_PARITY_MARK 0x0030
362#define BITS_PARITY_SPACE 0x0040
363
364#define BITS_STOP_MASK 0x000f
365#define BITS_STOP_1 0x0000
366#define BITS_STOP_1_5 0x0001
367#define BITS_STOP_2 0x0002
Craig Shelley39a66b82005-05-27 00:09:56 +0100368
Craig Shelley93ef1f12009-06-11 13:53:30 +0100369/* CP210X_SET_BREAK */
Craig Shelley72916792010-08-18 22:13:39 +0100370#define BREAK_ON 0x0001
371#define BREAK_OFF 0x0000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Craig Shelley93ef1f12009-06-11 13:53:30 +0100373/* CP210X_(SET_MHS|GET_MDMSTS) */
Craig Shelley39a66b82005-05-27 00:09:56 +0100374#define CONTROL_DTR 0x0001
375#define CONTROL_RTS 0x0002
376#define CONTROL_CTS 0x0010
377#define CONTROL_DSR 0x0020
378#define CONTROL_RING 0x0040
379#define CONTROL_DCD 0x0080
380#define CONTROL_WRITE_DTR 0x0100
381#define CONTROL_WRITE_RTS 0x0200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Martyn Welchcf5276c2016-10-20 15:13:54 +0100383/* CP210X_VENDOR_SPECIFIC values */
Karoly Padosc8acfe02018-07-20 12:52:40 +0200384#define CP210X_READ_2NCONFIG 0x000E
Martyn Welchcf5276c2016-10-20 15:13:54 +0100385#define CP210X_READ_LATCH 0x00C2
386#define CP210X_GET_PARTNUM 0x370B
387#define CP210X_GET_PORTCONFIG 0x370C
388#define CP210X_GET_DEVICEMODE 0x3711
389#define CP210X_WRITE_LATCH 0x37E1
390
391/* Part number definitions */
392#define CP210X_PARTNUM_CP2101 0x01
393#define CP210X_PARTNUM_CP2102 0x02
394#define CP210X_PARTNUM_CP2103 0x03
395#define CP210X_PARTNUM_CP2104 0x04
396#define CP210X_PARTNUM_CP2105 0x05
397#define CP210X_PARTNUM_CP2108 0x08
Karoly Pados6f0bcf72018-07-18 14:24:59 +0200398#define CP210X_PARTNUM_CP2102N_QFN28 0x20
399#define CP210X_PARTNUM_CP2102N_QFN24 0x21
400#define CP210X_PARTNUM_CP2102N_QFN20 0x22
Sebastian Frei7eac35e2017-09-12 09:50:59 +0200401#define CP210X_PARTNUM_UNKNOWN 0xFF
Martyn Welchcf5276c2016-10-20 15:13:54 +0100402
Konstantin Shkolnyy6f923a02015-11-24 16:28:41 -0600403/* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
404struct cp210x_comm_status {
405 __le32 ulErrors;
406 __le32 ulHoldReasons;
407 __le32 ulAmountInInQueue;
408 __le32 ulAmountInOutQueue;
409 u8 bEofReceived;
410 u8 bWaitForImmediate;
411 u8 bReserved;
412} __packed;
413
Craig Shelley39a66b82005-05-27 00:09:56 +0100414/*
Konstantin Shkolnyyebfb3192015-10-28 16:02:03 -0500415 * CP210X_PURGE - 16 bits passed in wValue of USB request.
416 * SiLabs app note AN571 gives a strange description of the 4 bits:
417 * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
418 * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
419 */
420#define PURGE_ALL 0x000f
421
Johan Hovolda7207e92020-07-13 12:55:14 +0200422/* CP210X_EMBED_EVENTS */
423#define CP210X_ESCCHAR 0xec
424
425#define CP210X_LSR_OVERRUN BIT(1)
426#define CP210X_LSR_PARITY BIT(2)
427#define CP210X_LSR_FRAME BIT(3)
428#define CP210X_LSR_BREAK BIT(4)
429
430
Konstantin Shkolnyy90343892016-05-04 16:57:02 -0500431/* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
432struct cp210x_flow_ctl {
433 __le32 ulControlHandshake;
434 __le32 ulFlowReplace;
435 __le32 ulXonLimit;
436 __le32 ulXoffLimit;
437} __packed;
438
439/* cp210x_flow_ctl::ulControlHandshake */
440#define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
441#define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
442#define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
443#define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
444#define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
445#define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
446
447/* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
448#define CP210X_SERIAL_DTR_INACTIVE 0
449#define CP210X_SERIAL_DTR_ACTIVE 1
450#define CP210X_SERIAL_DTR_FLOW_CTL 2
451
452/* cp210x_flow_ctl::ulFlowReplace */
453#define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
454#define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
455#define CP210X_SERIAL_ERROR_CHAR BIT(2)
456#define CP210X_SERIAL_NULL_STRIPPING BIT(3)
457#define CP210X_SERIAL_BREAK_CHAR BIT(4)
458#define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
459#define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
460#define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
461
462/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
463#define CP210X_SERIAL_RTS_INACTIVE 0
464#define CP210X_SERIAL_RTS_ACTIVE 1
465#define CP210X_SERIAL_RTS_FLOW_CTL 2
466
Martyn Welchcf5276c2016-10-20 15:13:54 +0100467/* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
468struct cp210x_pin_mode {
469 u8 eci;
470 u8 sci;
471} __packed;
472
473#define CP210X_PIN_MODE_MODEM 0
474#define CP210X_PIN_MODE_GPIO BIT(0)
475
476/*
Icenowy Zheng15fb84b2019-01-28 13:57:17 +0800477 * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
478 * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
Martyn Welchcf5276c2016-10-20 15:13:54 +0100479 */
Icenowy Zheng15fb84b2019-01-28 13:57:17 +0800480struct cp210x_dual_port_config {
Martyn Welchcf5276c2016-10-20 15:13:54 +0100481 __le16 gpio_mode;
482 u8 __pad0[2];
483 __le16 reset_state;
484 u8 __pad1[4];
485 __le16 suspend_state;
486 u8 sci_cfg;
487 u8 eci_cfg;
488 u8 device_cfg;
489} __packed;
490
Icenowy Zheng15fb84b2019-01-28 13:57:17 +0800491/*
492 * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
493 * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
494 */
495struct cp210x_single_port_config {
496 __le16 gpio_mode;
497 u8 __pad0[2];
498 __le16 reset_state;
499 u8 __pad1[4];
500 __le16 suspend_state;
501 u8 device_cfg;
502} __packed;
503
Martyn Welchcf5276c2016-10-20 15:13:54 +0100504/* GPIO modes */
505#define CP210X_SCI_GPIO_MODE_OFFSET 9
506#define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9)
507
508#define CP210X_ECI_GPIO_MODE_OFFSET 2
509#define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2)
510
Icenowy Zheng15fb84b2019-01-28 13:57:17 +0800511#define CP210X_GPIO_MODE_OFFSET 8
512#define CP210X_GPIO_MODE_MASK GENMASK(11, 8)
513
Martyn Welchcf5276c2016-10-20 15:13:54 +0100514/* CP2105 port configuration values */
515#define CP2105_GPIO0_TXLED_MODE BIT(0)
516#define CP2105_GPIO1_RXLED_MODE BIT(1)
517#define CP2105_GPIO1_RS485_MODE BIT(2)
518
Icenowy Zheng15fb84b2019-01-28 13:57:17 +0800519/* CP2104 port configuration values */
520#define CP2104_GPIO0_TXLED_MODE BIT(0)
521#define CP2104_GPIO1_RXLED_MODE BIT(1)
522#define CP2104_GPIO2_RS485_MODE BIT(2)
523
Karoly Padosc8acfe02018-07-20 12:52:40 +0200524/* CP2102N configuration array indices */
525#define CP210X_2NCONFIG_CONFIG_VERSION_IDX 2
526#define CP210X_2NCONFIG_GPIO_MODE_IDX 581
527#define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX 587
528#define CP210X_2NCONFIG_GPIO_CONTROL_IDX 600
529
Martyn Welchcf5276c2016-10-20 15:13:54 +0100530/* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
531struct cp210x_gpio_write {
532 u8 mask;
533 u8 state;
534} __packed;
535
536/*
537 * Helper to get interface number when we only have struct usb_serial.
538 */
539static u8 cp210x_interface_num(struct usb_serial *serial)
540{
541 struct usb_host_interface *cur_altsetting;
542
543 cur_altsetting = serial->interface->cur_altsetting;
544
545 return cur_altsetting->desc.bInterfaceNumber;
546}
547
Craig Shelley39a66b82005-05-27 00:09:56 +0100548/*
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600549 * Reads a variable-sized block of CP210X_ registers, identified by req.
550 * Returns data into buf in native USB byte order.
Craig Shelley39a66b82005-05-27 00:09:56 +0100551 */
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600552static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
553 void *buf, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct usb_serial *serial = port->serial;
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -0500556 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600557 void *dmabuf;
558 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600560 dmabuf = kmalloc(bufsize, GFP_KERNEL);
561 if (!dmabuf) {
562 /*
563 * FIXME Some callers don't bother to check for error,
564 * at least give them consistent junk until they are fixed
565 */
566 memset(buf, 0, bufsize);
Craig Shelley39a66b82005-05-27 00:09:56 +0100567 return -ENOMEM;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600568 }
Craig Shelley39a66b82005-05-27 00:09:56 +0100569
Alan Coxff7eb602008-07-22 11:10:17 +0100570 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600571 req, REQTYPE_INTERFACE_TO_HOST, 0,
572 port_priv->bInterfaceNumber, dmabuf, bufsize,
573 USB_CTRL_SET_TIMEOUT);
574 if (result == bufsize) {
575 memcpy(buf, dmabuf, bufsize);
576 result = 0;
577 } else {
578 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
579 req, bufsize, result);
580 if (result >= 0)
Johan Hovold0407f1c2016-10-19 15:46:39 +0200581 result = -EIO;
Johan Hovold2479e2a2011-11-10 14:58:29 +0100582
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600583 /*
584 * FIXME Some callers don't bother to check for error,
585 * at least give them consistent junk until they are fixed
586 */
587 memset(buf, 0, bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600590 kfree(dmabuf);
591
592 return result;
593}
594
595/*
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600596 * Reads any 32-bit CP210X_ register identified by req.
597 */
598static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
599{
600 __le32 le32_val;
601 int err;
602
603 err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
604 if (err) {
605 /*
606 * FIXME Some callers don't bother to check for error,
607 * at least give them consistent junk until they are fixed
608 */
609 *val = 0;
610 return err;
611 }
612
613 *val = le32_to_cpu(le32_val);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return 0;
616}
617
Craig Shelley39a66b82005-05-27 00:09:56 +0100618/*
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600619 * Reads any 16-bit CP210X_ register identified by req.
Craig Shelley39a66b82005-05-27 00:09:56 +0100620 */
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600621static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
622{
623 __le16 le16_val;
624 int err;
625
626 err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
627 if (err)
628 return err;
629
630 *val = le16_to_cpu(le16_val);
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600631
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600632 return 0;
633}
634
635/*
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600636 * Reads any 8-bit CP210X_ register identified by req.
637 */
638static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
639{
640 return cp210x_read_reg_block(port, req, val, sizeof(*val));
641}
642
643/*
Martyn Welchcf5276c2016-10-20 15:13:54 +0100644 * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
645 * Returns data into buf in native USB byte order.
646 */
647static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
648 void *buf, int bufsize)
649{
650 void *dmabuf;
651 int result;
652
653 dmabuf = kmalloc(bufsize, GFP_KERNEL);
654 if (!dmabuf)
655 return -ENOMEM;
656
657 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
658 CP210X_VENDOR_SPECIFIC, type, val,
659 cp210x_interface_num(serial), dmabuf, bufsize,
660 USB_CTRL_GET_TIMEOUT);
661 if (result == bufsize) {
662 memcpy(buf, dmabuf, bufsize);
663 result = 0;
664 } else {
665 dev_err(&serial->interface->dev,
666 "failed to get vendor val 0x%04x size %d: %d\n", val,
667 bufsize, result);
668 if (result >= 0)
669 result = -EIO;
670 }
671
672 kfree(dmabuf);
673
674 return result;
675}
676
677/*
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600678 * Writes any 16-bit CP210X_ register (req) whose value is passed
679 * entirely in the wValue field of the USB request.
680 */
681static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
Craig Shelley39a66b82005-05-27 00:09:56 +0100682{
683 struct usb_serial *serial = port->serial;
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -0500684 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600685 int result;
Craig Shelley39a66b82005-05-27 00:09:56 +0100686
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600687 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
688 req, REQTYPE_HOST_TO_INTERFACE, val,
689 port_priv->bInterfaceNumber, NULL, 0,
690 USB_CTRL_SET_TIMEOUT);
691 if (result < 0) {
692 dev_err(&port->dev, "failed set request 0x%x status: %d\n",
693 req, result);
Craig Shelley39a66b82005-05-27 00:09:56 +0100694 }
695
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600696 return result;
Craig Shelley39a66b82005-05-27 00:09:56 +0100697}
698
699/*
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600700 * Writes a variable-sized block of CP210X_ registers, identified by req.
701 * Data in buf must be in native USB byte order.
Craig Shelley39a66b82005-05-27 00:09:56 +0100702 */
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600703static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
704 void *buf, int bufsize)
Craig Shelley39a66b82005-05-27 00:09:56 +0100705{
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600706 struct usb_serial *serial = port->serial;
707 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
708 void *dmabuf;
709 int result;
710
Muhammad Falak R Wanid6c4dc32016-05-19 19:34:36 +0530711 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600712 if (!dmabuf)
713 return -ENOMEM;
714
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600715 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
716 req, REQTYPE_HOST_TO_INTERFACE, 0,
717 port_priv->bInterfaceNumber, dmabuf, bufsize,
718 USB_CTRL_SET_TIMEOUT);
719
720 kfree(dmabuf);
721
722 if (result == bufsize) {
723 result = 0;
724 } else {
725 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
726 req, bufsize, result);
727 if (result >= 0)
Johan Hovold0407f1c2016-10-19 15:46:39 +0200728 result = -EIO;
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -0600729 }
730
731 return result;
732}
733
734/*
735 * Writes any 32-bit CP210X_ register identified by req.
736 */
737static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
738{
739 __le32 le32_val;
740
741 le32_val = cpu_to_le32(val);
742
743 return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
Craig Shelley39a66b82005-05-27 00:09:56 +0100744}
745
Martyn Welchcf5276c2016-10-20 15:13:54 +0100746#ifdef CONFIG_GPIOLIB
747/*
748 * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
749 * Data in buf must be in native USB byte order.
750 */
751static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
752 u16 val, void *buf, int bufsize)
753{
754 void *dmabuf;
755 int result;
756
757 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
758 if (!dmabuf)
759 return -ENOMEM;
760
761 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
762 CP210X_VENDOR_SPECIFIC, type, val,
763 cp210x_interface_num(serial), dmabuf, bufsize,
764 USB_CTRL_SET_TIMEOUT);
765
766 kfree(dmabuf);
767
768 if (result == bufsize) {
769 result = 0;
770 } else {
771 dev_err(&serial->interface->dev,
772 "failed to set vendor val 0x%04x size %d: %d\n", val,
773 bufsize, result);
774 if (result >= 0)
775 result = -EIO;
776 }
777
778 return result;
779}
780#endif
781
Craig Shelleyef8b6bc2009-02-26 22:19:22 +0000782/*
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500783 * Detect CP2108 GET_LINE_CTL bug and activate workaround.
784 * Write a known good value 0x800, read it back.
785 * If it comes back swapped the bug is detected.
786 * Preserve the original register value.
787 */
788static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
789{
790 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600791 u16 line_ctl_save;
792 u16 line_ctl_test;
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500793 int err;
794
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600795 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500796 if (err)
797 return err;
798
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600799 err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500800 if (err)
801 return err;
802
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600803 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500804 if (err)
805 return err;
806
807 if (line_ctl_test == 8) {
808 port_priv->has_swapped_line_ctl = true;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600809 line_ctl_save = swab16(line_ctl_save);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500810 }
811
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600812 return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500813}
814
815/*
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600816 * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500817 * to workaround cp2108 bug and get correct value.
818 */
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600819static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500820{
821 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
822 int err;
823
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600824 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500825 if (err)
826 return err;
827
828 /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
829 if (port_priv->has_swapped_line_ctl)
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600830 *ctl = swab16(*ctl);
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -0500831
832 return 0;
833}
834
Alan Coxa509a7e2009-09-19 13:13:26 -0700835static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Johan Hovolda7207e92020-07-13 12:55:14 +0200837 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int result;
839
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600840 result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
Johan Hovold2479e2a2011-11-10 14:58:29 +0100841 if (result) {
842 dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
843 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Craig Shelley39a66b82005-05-27 00:09:56 +0100846 /* Configure the termios structure */
Craig Shelley4cc27bd2009-06-11 13:54:40 +0100847 cp210x_get_termios(tty, port);
Johan Hovold55b2afb2012-01-16 00:36:48 +0100848
Johan Hovolda7207e92020-07-13 12:55:14 +0200849 if (tty) {
850 /* The baud rate must be initialised on cp2104 */
Johan Hovoldcdc32fd2012-01-16 00:36:52 +0100851 cp210x_change_speed(tty, port, NULL);
852
Johan Hovolda7207e92020-07-13 12:55:14 +0200853 if (I_INPCK(tty))
854 cp210x_enable_event_mode(port);
855 }
856
Johan Hovoldbcbb9d82020-07-13 12:55:13 +0200857 result = usb_serial_generic_open(tty, port);
858 if (result)
859 goto err_disable;
860
861 return 0;
862
863err_disable:
864 cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
Johan Hovolda7207e92020-07-13 12:55:14 +0200865 port_priv->event_mode = false;
Johan Hovoldbcbb9d82020-07-13 12:55:13 +0200866
867 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Craig Shelley4cc27bd2009-06-11 13:54:40 +0100870static void cp210x_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Johan Hovolda7207e92020-07-13 12:55:14 +0200872 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
873
Johan Hovoldf26788d2010-03-17 23:00:45 +0100874 usb_serial_generic_close(port);
Konstantin Shkolnyyebfb3192015-10-28 16:02:03 -0500875
876 /* Clear both queues; cp2108 needs this to avoid an occasional hang */
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600877 cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
Konstantin Shkolnyyebfb3192015-10-28 16:02:03 -0500878
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -0600879 cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
Johan Hovolda7207e92020-07-13 12:55:14 +0200880
881 /* Disabling the interface disables event-insertion mode. */
882 port_priv->event_mode = false;
883}
884
885static void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
886{
887 if (lsr & CP210X_LSR_BREAK) {
888 port->icount.brk++;
889 *flag = TTY_BREAK;
890 } else if (lsr & CP210X_LSR_PARITY) {
891 port->icount.parity++;
892 *flag = TTY_PARITY;
893 } else if (lsr & CP210X_LSR_FRAME) {
894 port->icount.frame++;
895 *flag = TTY_FRAME;
896 }
897
898 if (lsr & CP210X_LSR_OVERRUN) {
899 port->icount.overrun++;
900 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
901 }
902}
903
904static bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
905{
906 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
907
908 switch (port_priv->event_state) {
909 case ES_DATA:
910 if (*ch == CP210X_ESCCHAR) {
911 port_priv->event_state = ES_ESCAPE;
912 break;
913 }
914 return false;
915 case ES_ESCAPE:
916 switch (*ch) {
917 case 0:
918 dev_dbg(&port->dev, "%s - escape char\n", __func__);
919 *ch = CP210X_ESCCHAR;
920 port_priv->event_state = ES_DATA;
921 return false;
922 case 1:
923 port_priv->event_state = ES_LSR_DATA_0;
924 break;
925 case 2:
926 port_priv->event_state = ES_LSR;
927 break;
928 case 3:
929 port_priv->event_state = ES_MSR;
930 break;
931 default:
932 dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
933 port_priv->event_state = ES_DATA;
934 break;
935 }
936 break;
937 case ES_LSR_DATA_0:
938 port_priv->lsr = *ch;
939 port_priv->event_state = ES_LSR_DATA_1;
940 break;
941 case ES_LSR_DATA_1:
942 dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
943 __func__, port_priv->lsr, *ch);
944 cp210x_process_lsr(port, port_priv->lsr, flag);
945 port_priv->event_state = ES_DATA;
946 return false;
947 case ES_LSR:
948 dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
949 port_priv->lsr = *ch;
950 cp210x_process_lsr(port, port_priv->lsr, flag);
951 port_priv->event_state = ES_DATA;
952 break;
953 case ES_MSR:
954 dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
955 /* unimplemented */
956 port_priv->event_state = ES_DATA;
957 break;
958 }
959
960 return true;
961}
962
963static void cp210x_process_read_urb(struct urb *urb)
964{
965 struct usb_serial_port *port = urb->context;
966 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
967 unsigned char *ch = urb->transfer_buffer;
968 char flag;
969 int i;
970
971 if (!urb->actual_length)
972 return;
973
974 if (port_priv->event_mode) {
975 for (i = 0; i < urb->actual_length; i++, ch++) {
976 flag = TTY_NORMAL;
977
978 if (cp210x_process_char(port, ch, &flag))
979 continue;
980
981 tty_insert_flip_char(&port->port, *ch, flag);
982 }
983 } else {
984 tty_insert_flip_string(&port->port, ch, urb->actual_length);
985 }
986 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Craig Shelley39a66b82005-05-27 00:09:56 +0100989/*
Konstantin Shkolnyy6f923a02015-11-24 16:28:41 -0600990 * Read how many bytes are waiting in the TX queue.
991 */
992static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
993 u32 *count)
994{
995 struct usb_serial *serial = port->serial;
996 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
997 struct cp210x_comm_status *sts;
998 int result;
999
1000 sts = kmalloc(sizeof(*sts), GFP_KERNEL);
1001 if (!sts)
1002 return -ENOMEM;
1003
1004 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1005 CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
1006 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
1007 USB_CTRL_GET_TIMEOUT);
1008 if (result == sizeof(*sts)) {
1009 *count = le32_to_cpu(sts->ulAmountInOutQueue);
1010 result = 0;
1011 } else {
1012 dev_err(&port->dev, "failed to get comm status: %d\n", result);
1013 if (result >= 0)
Johan Hovold0407f1c2016-10-19 15:46:39 +02001014 result = -EIO;
Konstantin Shkolnyy6f923a02015-11-24 16:28:41 -06001015 }
1016
1017 kfree(sts);
1018
1019 return result;
1020}
1021
1022static bool cp210x_tx_empty(struct usb_serial_port *port)
1023{
1024 int err;
1025 u32 count;
1026
1027 err = cp210x_get_tx_queue_byte_count(port, &count);
1028 if (err)
1029 return true;
1030
1031 return !count;
1032}
1033
1034/*
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001035 * cp210x_get_termios
Craig Shelley39a66b82005-05-27 00:09:56 +01001036 * Reads the baud rate, data bits, parity, stop bits and flow control mode
1037 * from the device, corrects any unsupported values, and configures the
1038 * termios structure to reflect the state of the device
1039 */
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001040static void cp210x_get_termios(struct tty_struct *tty,
VomLehnd2ad67b2009-03-12 14:37:42 -07001041 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
VomLehnd2ad67b2009-03-12 14:37:42 -07001043 unsigned int baud;
1044
1045 if (tty) {
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001046 cp210x_get_termios_port(tty->driver_data,
Alan Coxadc8d742012-07-14 15:31:47 +01001047 &tty->termios.c_cflag, &baud);
VomLehnd2ad67b2009-03-12 14:37:42 -07001048 tty_encode_baud_rate(tty, baud, baud);
Johan Hovold4f2ab882012-10-29 10:56:19 +01001049 } else {
Geert Uytterhoeven009615ab2016-11-21 13:19:31 +01001050 tcflag_t cflag;
VomLehnd2ad67b2009-03-12 14:37:42 -07001051 cflag = 0;
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001052 cp210x_get_termios_port(port, &cflag, &baud);
VomLehnd2ad67b2009-03-12 14:37:42 -07001053 }
1054}
1055
1056/*
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001057 * cp210x_get_termios_port
1058 * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
VomLehnd2ad67b2009-03-12 14:37:42 -07001059 */
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001060static void cp210x_get_termios_port(struct usb_serial_port *port,
Geert Uytterhoeven009615ab2016-11-21 13:19:31 +01001061 tcflag_t *cflagp, unsigned int *baudp)
VomLehnd2ad67b2009-03-12 14:37:42 -07001062{
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001063 struct device *dev = &port->dev;
Geert Uytterhoeven009615ab2016-11-21 13:19:31 +01001064 tcflag_t cflag;
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001065 struct cp210x_flow_ctl flow_ctl;
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -06001066 u32 baud;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001067 u16 bits;
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001068 u32 ctl_hs;
Brant Merrymanc7614ff2020-06-26 04:24:20 +00001069 u32 flow_repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -06001071 cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001073 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
VomLehnd2ad67b2009-03-12 14:37:42 -07001074 *baudp = baud;
Alan Cox9ab0f202007-10-18 01:24:24 -07001075
VomLehnd2ad67b2009-03-12 14:37:42 -07001076 cflag = *cflagp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001078 cp210x_get_line_ctl(port, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 cflag &= ~CSIZE;
Alan Coxff7eb602008-07-22 11:10:17 +01001080 switch (bits & BITS_DATA_MASK) {
1081 case BITS_DATA_5:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001082 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001083 cflag |= CS5;
1084 break;
1085 case BITS_DATA_6:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001086 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001087 cflag |= CS6;
1088 break;
1089 case BITS_DATA_7:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001090 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001091 cflag |= CS7;
1092 break;
1093 case BITS_DATA_8:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001094 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001095 cflag |= CS8;
1096 break;
1097 case BITS_DATA_9:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001098 dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001099 cflag |= CS8;
1100 bits &= ~BITS_DATA_MASK;
1101 bits |= BITS_DATA_8;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001102 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
Alan Coxff7eb602008-07-22 11:10:17 +01001103 break;
1104 default:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001105 dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001106 cflag |= CS8;
1107 bits &= ~BITS_DATA_MASK;
1108 bits |= BITS_DATA_8;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001109 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
Alan Coxff7eb602008-07-22 11:10:17 +01001110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112
Alan Coxff7eb602008-07-22 11:10:17 +01001113 switch (bits & BITS_PARITY_MASK) {
1114 case BITS_PARITY_NONE:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001115 dev_dbg(dev, "%s - parity = NONE\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001116 cflag &= ~PARENB;
1117 break;
1118 case BITS_PARITY_ODD:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001119 dev_dbg(dev, "%s - parity = ODD\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001120 cflag |= (PARENB|PARODD);
1121 break;
1122 case BITS_PARITY_EVEN:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001123 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001124 cflag &= ~PARODD;
1125 cflag |= PARENB;
1126 break;
1127 case BITS_PARITY_MARK:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001128 dev_dbg(dev, "%s - parity = MARK\n", __func__);
Roland Koebler4b6181c2011-11-09 19:37:08 +01001129 cflag |= (PARENB|PARODD|CMSPAR);
Alan Coxff7eb602008-07-22 11:10:17 +01001130 break;
1131 case BITS_PARITY_SPACE:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001132 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
Roland Koebler4b6181c2011-11-09 19:37:08 +01001133 cflag &= ~PARODD;
1134 cflag |= (PARENB|CMSPAR);
Alan Coxff7eb602008-07-22 11:10:17 +01001135 break;
1136 default:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001137 dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001138 cflag &= ~PARENB;
1139 bits &= ~BITS_PARITY_MASK;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001140 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
Alan Coxff7eb602008-07-22 11:10:17 +01001141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 cflag &= ~CSTOPB;
Alan Coxff7eb602008-07-22 11:10:17 +01001145 switch (bits & BITS_STOP_MASK) {
1146 case BITS_STOP_1:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001147 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001148 break;
1149 case BITS_STOP_1_5:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001150 dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001151 bits &= ~BITS_STOP_MASK;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001152 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
Alan Coxff7eb602008-07-22 11:10:17 +01001153 break;
1154 case BITS_STOP_2:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001155 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001156 cflag |= CSTOPB;
1157 break;
1158 default:
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001159 dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001160 bits &= ~BITS_STOP_MASK;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001161 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
Alan Coxff7eb602008-07-22 11:10:17 +01001162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001165 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1166 sizeof(flow_ctl));
1167 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1168 if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001169 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
Brant Merrymanc7614ff2020-06-26 04:24:20 +00001170 /*
1171 * When the port is closed, the CP210x hardware disables
1172 * auto-RTS and RTS is deasserted but it leaves auto-CTS when
1173 * in hardware flow control mode. When re-opening the port, if
1174 * auto-CTS is enabled on the cp210x, then auto-RTS must be
1175 * re-enabled in the driver.
1176 */
1177 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1178 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1179 flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
1180 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1181 cp210x_write_reg_block(port,
1182 CP210X_SET_FLOW,
1183 &flow_ctl,
1184 sizeof(flow_ctl));
1185
Craig Shelley39a66b82005-05-27 00:09:56 +01001186 cflag |= CRTSCTS;
1187 } else {
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001188 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
Craig Shelley39a66b82005-05-27 00:09:56 +01001189 cflag &= ~CRTSCTS;
1190 }
1191
VomLehnd2ad67b2009-03-12 14:37:42 -07001192 *cflagp = cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Johan Hovold95fd4f42018-07-18 14:24:57 +02001195struct cp210x_rate {
1196 speed_t rate;
1197 speed_t high;
1198};
1199
1200static const struct cp210x_rate cp210x_an205_table1[] = {
1201 { 300, 300 },
1202 { 600, 600 },
1203 { 1200, 1200 },
1204 { 1800, 1800 },
1205 { 2400, 2400 },
1206 { 4000, 4000 },
1207 { 4800, 4803 },
1208 { 7200, 7207 },
1209 { 9600, 9612 },
1210 { 14400, 14428 },
1211 { 16000, 16062 },
1212 { 19200, 19250 },
1213 { 28800, 28912 },
1214 { 38400, 38601 },
1215 { 51200, 51558 },
1216 { 56000, 56280 },
1217 { 57600, 58053 },
1218 { 64000, 64111 },
1219 { 76800, 77608 },
1220 { 115200, 117028 },
1221 { 128000, 129347 },
1222 { 153600, 156868 },
1223 { 230400, 237832 },
1224 { 250000, 254234 },
1225 { 256000, 273066 },
1226 { 460800, 491520 },
1227 { 500000, 567138 },
1228 { 576000, 670254 },
1229 { 921600, UINT_MAX }
1230};
1231
1232/*
1233 * Quantises the baud rate as per AN205 Table 1
1234 */
1235static speed_t cp210x_get_an205_rate(speed_t baud)
1236{
1237 int i;
1238
1239 for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
1240 if (baud <= cp210x_an205_table1[i].high)
1241 break;
1242 }
1243
1244 return cp210x_an205_table1[i].rate;
1245}
1246
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01001247static speed_t cp210x_get_actual_rate(speed_t baud)
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001248{
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001249 unsigned int prescale = 1;
1250 unsigned int div;
1251
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001252 if (baud <= 365)
1253 prescale = 4;
1254
1255 div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
1256 baud = 48000000 / (2 * prescale * div);
1257
1258 return baud;
1259}
1260
Johan Hovolde5990872012-01-16 00:36:51 +01001261/*
1262 * CP2101 supports the following baud rates:
1263 *
1264 * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1265 * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1266 *
1267 * CP2102 and CP2103 support the following additional rates:
1268 *
1269 * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1270 * 576000
1271 *
1272 * The device will map a requested rate to a supported one, but the result
1273 * of requests for rates greater than 1053257 is undefined (see AN205).
1274 *
1275 * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1276 * respectively, with an error less than 1%. The actual rates are determined
1277 * by
1278 *
1279 * div = round(freq / (2 x prescale x request))
1280 * actual = freq / (2 x prescale x div)
1281 *
1282 * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1283 * or 1 otherwise.
1284 * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1285 * otherwise.
1286 */
1287static void cp210x_change_speed(struct tty_struct *tty,
1288 struct usb_serial_port *port, struct ktermios *old_termios)
1289{
Karoly Pados6f0bcf72018-07-18 14:24:59 +02001290 struct usb_serial *serial = port->serial;
1291 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
Johan Hovolde5990872012-01-16 00:36:51 +01001292 u32 baud;
1293
Karoly Pados6f0bcf72018-07-18 14:24:59 +02001294 /*
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001295 * This maps the requested rate to the actual rate, a valid rate on
1296 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
Johan Hovolde5990872012-01-16 00:36:51 +01001297 *
1298 * NOTE: B0 is not implemented.
1299 */
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01001300 baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
1301
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001302 if (priv->use_actual_rate)
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01001303 baud = cp210x_get_actual_rate(baud);
Johan Hovold7aecd7f2018-07-18 14:25:00 +02001304 else if (baud < 1000000)
Johan Hovold95fd4f42018-07-18 14:24:57 +02001305 baud = cp210x_get_an205_rate(baud);
Johan Hovolde5990872012-01-16 00:36:51 +01001306
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001307 dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -06001308 if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
Johan Hovolde5990872012-01-16 00:36:51 +01001309 dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1310 if (old_termios)
1311 baud = old_termios->c_ospeed;
1312 else
1313 baud = 9600;
1314 }
1315
1316 tty_encode_baud_rate(tty, baud, baud);
1317}
1318
Johan Hovolda7207e92020-07-13 12:55:14 +02001319static void cp210x_enable_event_mode(struct usb_serial_port *port)
1320{
1321 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1322 int ret;
1323
1324 if (port_priv->event_mode)
1325 return;
1326
1327 port_priv->event_state = ES_DATA;
1328 port_priv->event_mode = true;
1329
1330 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
1331 if (ret) {
1332 dev_err(&port->dev, "failed to enable events: %d\n", ret);
1333 port_priv->event_mode = false;
1334 }
1335}
1336
1337static void cp210x_disable_event_mode(struct usb_serial_port *port)
1338{
1339 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
1340 int ret;
1341
1342 if (!port_priv->event_mode)
1343 return;
1344
1345 ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
1346 if (ret) {
1347 dev_err(&port->dev, "failed to disable events: %d\n", ret);
1348 return;
1349 }
1350
1351 port_priv->event_mode = false;
1352}
1353
Craig Shelley4cc27bd2009-06-11 13:54:40 +01001354static void cp210x_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +01001355 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001357 struct device *dev = &port->dev;
Alan Cox9ab0f202007-10-18 01:24:24 -07001358 unsigned int cflag, old_cflag;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001359 u16 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Alan Coxadc8d742012-07-14 15:31:47 +01001361 cflag = tty->termios.c_cflag;
Alan Cox01e96d22007-07-26 18:54:12 +01001362 old_cflag = old_termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Alan Coxadc8d742012-07-14 15:31:47 +01001364 if (tty->termios.c_ospeed != old_termios->c_ospeed)
Johan Hovolde5990872012-01-16 00:36:51 +01001365 cp210x_change_speed(tty, port, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Craig Shelley39a66b82005-05-27 00:09:56 +01001367 /* If the number of data bits is to be updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001369 cp210x_get_line_ctl(port, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 bits &= ~BITS_DATA_MASK;
1371 switch (cflag & CSIZE) {
Alan Coxff7eb602008-07-22 11:10:17 +01001372 case CS5:
1373 bits |= BITS_DATA_5;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001374 dev_dbg(dev, "%s - data bits = 5\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001375 break;
1376 case CS6:
1377 bits |= BITS_DATA_6;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001378 dev_dbg(dev, "%s - data bits = 6\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001379 break;
1380 case CS7:
1381 bits |= BITS_DATA_7;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001382 dev_dbg(dev, "%s - data bits = 7\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001383 break;
1384 case CS8:
Johan Hovoldc2b33552016-10-19 15:46:38 +02001385 default:
Alan Coxff7eb602008-07-22 11:10:17 +01001386 bits |= BITS_DATA_8;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001387 dev_dbg(dev, "%s - data bits = 8\n", __func__);
Alan Coxff7eb602008-07-22 11:10:17 +01001388 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001390 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001391 dev_dbg(dev, "Number of data bits requested not supported by device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Roland Koebler4b6181c2011-11-09 19:37:08 +01001394 if ((cflag & (PARENB|PARODD|CMSPAR)) !=
1395 (old_cflag & (PARENB|PARODD|CMSPAR))) {
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001396 cp210x_get_line_ctl(port, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 bits &= ~BITS_PARITY_MASK;
1398 if (cflag & PARENB) {
Roland Koebler4b6181c2011-11-09 19:37:08 +01001399 if (cflag & CMSPAR) {
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001400 if (cflag & PARODD) {
1401 bits |= BITS_PARITY_MARK;
1402 dev_dbg(dev, "%s - parity = MARK\n", __func__);
1403 } else {
1404 bits |= BITS_PARITY_SPACE;
1405 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 } else {
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001408 if (cflag & PARODD) {
1409 bits |= BITS_PARITY_ODD;
1410 dev_dbg(dev, "%s - parity = ODD\n", __func__);
1411 } else {
1412 bits |= BITS_PARITY_EVEN;
1413 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 }
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001417 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001418 dev_dbg(dev, "Parity mode not supported by device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
1421 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001422 cp210x_get_line_ctl(port, &bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 bits &= ~BITS_STOP_MASK;
1424 if (cflag & CSTOPB) {
1425 bits |= BITS_STOP_2;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001426 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 } else {
1428 bits |= BITS_STOP_1;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001429 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001431 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001432 dev_dbg(dev, "Number of stop bits requested not supported by device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Craig Shelley39a66b82005-05-27 00:09:56 +01001434
1435 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001436 struct cp210x_flow_ctl flow_ctl;
1437 u32 ctl_hs;
1438 u32 flow_repl;
Konstantin Shkolnyy7084fa82016-02-28 15:51:56 -06001439
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001440 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1441 sizeof(flow_ctl));
1442 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1443 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1444 dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1445 __func__, ctl_hs, flow_repl);
Konstantin Shkolnyy7084fa82016-02-28 15:51:56 -06001446
Konstantin Shkolnyyab5701a2016-05-04 16:57:11 -05001447 ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1448 ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1449 ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1450 ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1451 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
Craig Shelley39a66b82005-05-27 00:09:56 +01001452 if (cflag & CRTSCTS) {
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001453 ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
Konstantin Shkolnyyab5701a2016-05-04 16:57:11 -05001454
1455 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001456 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1457 CP210X_SERIAL_RTS_FLOW_CTL);
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001458 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
Craig Shelley39a66b82005-05-27 00:09:56 +01001459 } else {
Konstantin Shkolnyyab5701a2016-05-04 16:57:11 -05001460 ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1461
1462 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001463 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1464 CP210X_SERIAL_RTS_ACTIVE);
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001465 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
Craig Shelley39a66b82005-05-27 00:09:56 +01001466 }
1467
Konstantin Shkolnyy90343892016-05-04 16:57:02 -05001468 dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1469 __func__, ctl_hs, flow_repl);
1470 flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1471 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1472 cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1473 sizeof(flow_ctl));
Craig Shelley39a66b82005-05-27 00:09:56 +01001474 }
1475
Johan Hovolda7207e92020-07-13 12:55:14 +02001476 /*
1477 * Enable event-insertion mode only if input parity checking is
1478 * enabled for now.
1479 */
1480 if (I_INPCK(tty))
1481 cp210x_enable_event_mode(port);
1482 else
1483 cp210x_disable_event_mode(port);
Craig Shelley39a66b82005-05-27 00:09:56 +01001484}
1485
Johan Hovold4f2ab882012-10-29 10:56:19 +01001486static int cp210x_tiocmset(struct tty_struct *tty,
Craig Shelley39a66b82005-05-27 00:09:56 +01001487 unsigned int set, unsigned int clear)
1488{
Alan Cox95da3102008-07-22 11:09:07 +01001489 struct usb_serial_port *port = tty->driver_data;
Alan Cox20b9d172011-02-14 16:26:50 +00001490 return cp210x_tiocmset_port(port, set, clear);
VomLehnd2ad67b2009-03-12 14:37:42 -07001491}
1492
Alan Cox20b9d172011-02-14 16:26:50 +00001493static int cp210x_tiocmset_port(struct usb_serial_port *port,
VomLehnd2ad67b2009-03-12 14:37:42 -07001494 unsigned int set, unsigned int clear)
1495{
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001496 u16 control = 0;
Craig Shelley39a66b82005-05-27 00:09:56 +01001497
Craig Shelley39a66b82005-05-27 00:09:56 +01001498 if (set & TIOCM_RTS) {
1499 control |= CONTROL_RTS;
1500 control |= CONTROL_WRITE_RTS;
1501 }
1502 if (set & TIOCM_DTR) {
1503 control |= CONTROL_DTR;
1504 control |= CONTROL_WRITE_DTR;
1505 }
1506 if (clear & TIOCM_RTS) {
1507 control &= ~CONTROL_RTS;
1508 control |= CONTROL_WRITE_RTS;
1509 }
1510 if (clear & TIOCM_DTR) {
1511 control &= ~CONTROL_DTR;
1512 control |= CONTROL_WRITE_DTR;
1513 }
1514
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001515 dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
Craig Shelley39a66b82005-05-27 00:09:56 +01001516
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001517 return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
Craig Shelley39a66b82005-05-27 00:09:56 +01001518}
1519
Alan Coxd94c7bd2009-10-28 21:12:33 +01001520static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1521{
1522 if (on)
Alan Cox20b9d172011-02-14 16:26:50 +00001523 cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
Alan Coxd94c7bd2009-10-28 21:12:33 +01001524 else
Alan Cox20b9d172011-02-14 16:26:50 +00001525 cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
Alan Coxd94c7bd2009-10-28 21:12:33 +01001526}
1527
Johan Hovold4f2ab882012-10-29 10:56:19 +01001528static int cp210x_tiocmget(struct tty_struct *tty)
Craig Shelley39a66b82005-05-27 00:09:56 +01001529{
Alan Cox95da3102008-07-22 11:09:07 +01001530 struct usb_serial_port *port = tty->driver_data;
Konstantin Shkolnyyfe1b07e2016-02-28 15:51:41 -06001531 u8 control;
Harvey Harrisonb2bdd1f2008-05-30 10:29:55 -07001532 int result;
Craig Shelley39a66b82005-05-27 00:09:56 +01001533
Johan Hovoldde24e0a2016-10-19 15:45:07 +02001534 result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1535 if (result)
1536 return result;
Craig Shelley39a66b82005-05-27 00:09:56 +01001537
1538 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
1539 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
1540 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
1541 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
1542 |((control & CONTROL_RING)? TIOCM_RI : 0)
1543 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
1544
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001545 dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
Craig Shelley39a66b82005-05-27 00:09:56 +01001546
1547 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Johan Hovold4f2ab882012-10-29 10:56:19 +01001550static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Alan Cox95da3102008-07-22 11:09:07 +01001552 struct usb_serial_port *port = tty->driver_data;
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001553 u16 state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (break_state == 0)
1556 state = BREAK_OFF;
1557 else
1558 state = BREAK_ON;
Greg Kroah-Hartman660f3b12012-09-13 17:18:13 -07001559 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1560 state == BREAK_OFF ? "off" : "on");
Konstantin Shkolnyy19165b22016-02-28 15:51:30 -06001561 cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Martyn Welchcf5276c2016-10-20 15:13:54 +01001564#ifdef CONFIG_GPIOLIB
1565static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
1566{
1567 struct usb_serial *serial = gpiochip_get_data(gc);
1568 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1569
Karoly Padosc8acfe02018-07-20 12:52:40 +02001570 if (priv->gpio_altfunc & BIT(offset))
1571 return -ENODEV;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001572
1573 return 0;
1574}
1575
1576static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1577{
1578 struct usb_serial *serial = gpiochip_get_data(gc);
Karoly Padosc8acfe02018-07-20 12:52:40 +02001579 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1580 u8 req_type = REQTYPE_DEVICE_TO_HOST;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001581 int result;
1582 u8 buf;
1583
Karoly Padosc8acfe02018-07-20 12:52:40 +02001584 if (priv->partnum == CP210X_PARTNUM_CP2105)
1585 req_type = REQTYPE_INTERFACE_TO_HOST;
1586
Karoly Pados7b0b6442019-02-17 18:59:01 +01001587 result = usb_autopm_get_interface(serial->interface);
1588 if (result)
1589 return result;
1590
Karoly Padosc8acfe02018-07-20 12:52:40 +02001591 result = cp210x_read_vendor_block(serial, req_type,
Martyn Welchcf5276c2016-10-20 15:13:54 +01001592 CP210X_READ_LATCH, &buf, sizeof(buf));
Karoly Pados7b0b6442019-02-17 18:59:01 +01001593 usb_autopm_put_interface(serial->interface);
Martyn Welchcf5276c2016-10-20 15:13:54 +01001594 if (result < 0)
1595 return result;
1596
1597 return !!(buf & BIT(gpio));
1598}
1599
1600static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1601{
1602 struct usb_serial *serial = gpiochip_get_data(gc);
Karoly Padosc8acfe02018-07-20 12:52:40 +02001603 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
Martyn Welchcf5276c2016-10-20 15:13:54 +01001604 struct cp210x_gpio_write buf;
Karoly Padosc8acfe02018-07-20 12:52:40 +02001605 int result;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001606
1607 if (value == 1)
1608 buf.state = BIT(gpio);
1609 else
1610 buf.state = 0;
1611
1612 buf.mask = BIT(gpio);
1613
Karoly Pados7b0b6442019-02-17 18:59:01 +01001614 result = usb_autopm_get_interface(serial->interface);
1615 if (result)
1616 goto out;
1617
Karoly Padosc8acfe02018-07-20 12:52:40 +02001618 if (priv->partnum == CP210X_PARTNUM_CP2105) {
1619 result = cp210x_write_vendor_block(serial,
1620 REQTYPE_HOST_TO_INTERFACE,
1621 CP210X_WRITE_LATCH, &buf,
1622 sizeof(buf));
1623 } else {
1624 u16 wIndex = buf.state << 8 | buf.mask;
1625
1626 result = usb_control_msg(serial->dev,
1627 usb_sndctrlpipe(serial->dev, 0),
1628 CP210X_VENDOR_SPECIFIC,
1629 REQTYPE_HOST_TO_DEVICE,
1630 CP210X_WRITE_LATCH,
1631 wIndex,
1632 NULL, 0, USB_CTRL_SET_TIMEOUT);
1633 }
1634
Karoly Pados7b0b6442019-02-17 18:59:01 +01001635 usb_autopm_put_interface(serial->interface);
1636out:
Karoly Padosc8acfe02018-07-20 12:52:40 +02001637 if (result < 0) {
1638 dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
1639 result);
1640 }
Martyn Welchcf5276c2016-10-20 15:13:54 +01001641}
1642
1643static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1644{
Karoly Padosc8acfe02018-07-20 12:52:40 +02001645 struct usb_serial *serial = gpiochip_get_data(gc);
1646 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1647
1648 return priv->gpio_input & BIT(gpio);
Martyn Welchcf5276c2016-10-20 15:13:54 +01001649}
1650
1651static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1652{
Karoly Padosc8acfe02018-07-20 12:52:40 +02001653 struct usb_serial *serial = gpiochip_get_data(gc);
1654 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1655
1656 if (priv->partnum == CP210X_PARTNUM_CP2105) {
1657 /* hardware does not support an input mode */
1658 return -ENOTSUPP;
1659 }
1660
1661 /* push-pull pins cannot be changed to be inputs */
1662 if (priv->gpio_pushpull & BIT(gpio))
1663 return -EINVAL;
1664
1665 /* make sure to release pin if it is being driven low */
1666 cp210x_gpio_set(gc, gpio, 1);
1667
1668 priv->gpio_input |= BIT(gpio);
1669
1670 return 0;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001671}
1672
1673static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1674 int value)
1675{
Karoly Padosc8acfe02018-07-20 12:52:40 +02001676 struct usb_serial *serial = gpiochip_get_data(gc);
1677 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1678
1679 priv->gpio_input &= ~BIT(gpio);
1680 cp210x_gpio_set(gc, gpio, value);
1681
Martyn Welchcf5276c2016-10-20 15:13:54 +01001682 return 0;
1683}
1684
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001685static int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
1686 unsigned long config)
Martyn Welchcf5276c2016-10-20 15:13:54 +01001687{
1688 struct usb_serial *serial = gpiochip_get_data(gc);
1689 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001690 enum pin_config_param param = pinconf_to_config_param(config);
Martyn Welchcf5276c2016-10-20 15:13:54 +01001691
1692 /* Succeed only if in correct mode (this can't be set at runtime) */
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001693 if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
Karoly Padosc8acfe02018-07-20 12:52:40 +02001694 (priv->gpio_pushpull & BIT(gpio)))
Martyn Welchcf5276c2016-10-20 15:13:54 +01001695 return 0;
1696
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001697 if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
Karoly Padosc8acfe02018-07-20 12:52:40 +02001698 !(priv->gpio_pushpull & BIT(gpio)))
Martyn Welchcf5276c2016-10-20 15:13:54 +01001699 return 0;
1700
1701 return -ENOTSUPP;
1702}
1703
1704/*
1705 * This function is for configuring GPIO using shared pins, where other signals
1706 * are made unavailable by configuring the use of GPIO. This is believed to be
1707 * only applicable to the cp2105 at this point, the other devices supported by
1708 * this driver that provide GPIO do so in a way that does not impact other
1709 * signals and are thus expected to have very different initialisation.
1710 */
Karoly Padosc8acfe02018-07-20 12:52:40 +02001711static int cp2105_gpioconf_init(struct usb_serial *serial)
Martyn Welchcf5276c2016-10-20 15:13:54 +01001712{
1713 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1714 struct cp210x_pin_mode mode;
Icenowy Zheng15fb84b2019-01-28 13:57:17 +08001715 struct cp210x_dual_port_config config;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001716 u8 intf_num = cp210x_interface_num(serial);
Karoly Padosc8acfe02018-07-20 12:52:40 +02001717 u8 iface_config;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001718 int result;
1719
1720 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1721 CP210X_GET_DEVICEMODE, &mode,
1722 sizeof(mode));
1723 if (result < 0)
1724 return result;
1725
1726 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1727 CP210X_GET_PORTCONFIG, &config,
1728 sizeof(config));
1729 if (result < 0)
1730 return result;
1731
1732 /* 2 banks of GPIO - One for the pins taken from each serial port */
1733 if (intf_num == 0) {
Karoly Padosc8acfe02018-07-20 12:52:40 +02001734 if (mode.eci == CP210X_PIN_MODE_MODEM) {
1735 /* mark all GPIOs of this interface as reserved */
1736 priv->gpio_altfunc = 0xff;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001737 return 0;
Karoly Padosc8acfe02018-07-20 12:52:40 +02001738 }
Martyn Welchcf5276c2016-10-20 15:13:54 +01001739
Karoly Padosc8acfe02018-07-20 12:52:40 +02001740 iface_config = config.eci_cfg;
1741 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
Martyn Welchcf5276c2016-10-20 15:13:54 +01001742 CP210X_ECI_GPIO_MODE_MASK) >>
1743 CP210X_ECI_GPIO_MODE_OFFSET);
1744 priv->gc.ngpio = 2;
1745 } else if (intf_num == 1) {
Karoly Padosc8acfe02018-07-20 12:52:40 +02001746 if (mode.sci == CP210X_PIN_MODE_MODEM) {
1747 /* mark all GPIOs of this interface as reserved */
1748 priv->gpio_altfunc = 0xff;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001749 return 0;
Karoly Padosc8acfe02018-07-20 12:52:40 +02001750 }
Martyn Welchcf5276c2016-10-20 15:13:54 +01001751
Karoly Padosc8acfe02018-07-20 12:52:40 +02001752 iface_config = config.sci_cfg;
1753 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
Martyn Welchcf5276c2016-10-20 15:13:54 +01001754 CP210X_SCI_GPIO_MODE_MASK) >>
1755 CP210X_SCI_GPIO_MODE_OFFSET);
1756 priv->gc.ngpio = 3;
1757 } else {
1758 return -ENODEV;
1759 }
1760
Karoly Padosc8acfe02018-07-20 12:52:40 +02001761 /* mark all pins which are not in GPIO mode */
1762 if (iface_config & CP2105_GPIO0_TXLED_MODE) /* GPIO 0 */
1763 priv->gpio_altfunc |= BIT(0);
1764 if (iface_config & (CP2105_GPIO1_RXLED_MODE | /* GPIO 1 */
1765 CP2105_GPIO1_RS485_MODE))
1766 priv->gpio_altfunc |= BIT(1);
1767
1768 /* driver implementation for CP2105 only supports outputs */
1769 priv->gpio_input = 0;
1770
1771 return 0;
1772}
1773
Icenowy Zheng15fb84b2019-01-28 13:57:17 +08001774static int cp2104_gpioconf_init(struct usb_serial *serial)
1775{
1776 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1777 struct cp210x_single_port_config config;
1778 u8 iface_config;
1779 u8 gpio_latch;
1780 int result;
1781 u8 i;
1782
1783 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1784 CP210X_GET_PORTCONFIG, &config,
1785 sizeof(config));
1786 if (result < 0)
1787 return result;
1788
1789 priv->gc.ngpio = 4;
1790
1791 iface_config = config.device_cfg;
1792 priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
1793 CP210X_GPIO_MODE_MASK) >>
1794 CP210X_GPIO_MODE_OFFSET);
1795 gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
1796 CP210X_GPIO_MODE_MASK) >>
1797 CP210X_GPIO_MODE_OFFSET);
1798
1799 /* mark all pins which are not in GPIO mode */
1800 if (iface_config & CP2104_GPIO0_TXLED_MODE) /* GPIO 0 */
1801 priv->gpio_altfunc |= BIT(0);
1802 if (iface_config & CP2104_GPIO1_RXLED_MODE) /* GPIO 1 */
1803 priv->gpio_altfunc |= BIT(1);
1804 if (iface_config & CP2104_GPIO2_RS485_MODE) /* GPIO 2 */
1805 priv->gpio_altfunc |= BIT(2);
1806
1807 /*
1808 * Like CP2102N, CP2104 has also no strict input and output pin
1809 * modes.
1810 * Do the same input mode emulation as CP2102N.
1811 */
1812 for (i = 0; i < priv->gc.ngpio; ++i) {
1813 /*
1814 * Set direction to "input" iff pin is open-drain and reset
1815 * value is 1.
1816 */
1817 if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1818 priv->gpio_input |= BIT(i);
1819 }
1820
1821 return 0;
1822}
1823
Karoly Padosc8acfe02018-07-20 12:52:40 +02001824static int cp2102n_gpioconf_init(struct usb_serial *serial)
1825{
1826 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1827 const u16 config_size = 0x02a6;
1828 u8 gpio_rst_latch;
1829 u8 config_version;
1830 u8 gpio_pushpull;
1831 u8 *config_buf;
1832 u8 gpio_latch;
1833 u8 gpio_ctrl;
1834 int result;
1835 u8 i;
1836
1837 /*
1838 * Retrieve device configuration from the device.
1839 * The array received contains all customization settings done at the
1840 * factory/manufacturer. Format of the array is documented at the
1841 * time of writing at:
1842 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
1843 */
1844 config_buf = kmalloc(config_size, GFP_KERNEL);
1845 if (!config_buf)
1846 return -ENOMEM;
1847
1848 result = cp210x_read_vendor_block(serial,
1849 REQTYPE_DEVICE_TO_HOST,
1850 CP210X_READ_2NCONFIG,
1851 config_buf,
1852 config_size);
1853 if (result < 0) {
1854 kfree(config_buf);
1855 return result;
1856 }
1857
1858 config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
1859 gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
1860 gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
1861 gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
1862
1863 kfree(config_buf);
1864
1865 /* Make sure this is a config format we understand. */
1866 if (config_version != 0x01)
1867 return -ENOTSUPP;
1868
Karoly Padosc8acfe02018-07-20 12:52:40 +02001869 priv->gc.ngpio = 4;
1870
1871 /*
1872 * Get default pin states after reset. Needed so we can determine
1873 * the direction of an open-drain pin.
1874 */
1875 gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
1876
1877 /* 0 indicates open-drain mode, 1 is push-pull */
1878 priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
1879
1880 /* 0 indicates GPIO mode, 1 is alternate function */
1881 priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
1882
Mans Rullgarda49e1ab2019-01-24 13:48:39 +00001883 if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
1884 /*
1885 * For the QFN28 package, GPIO4-6 are controlled by
1886 * the low three bits of the mode/latch fields.
1887 * Contrary to the document linked above, the bits for
1888 * the SUSPEND pins are elsewhere. No alternate
1889 * function is available for these pins.
1890 */
1891 priv->gc.ngpio = 7;
1892 gpio_latch |= (gpio_rst_latch & 7) << 4;
1893 priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
1894 }
1895
Karoly Padosc8acfe02018-07-20 12:52:40 +02001896 /*
1897 * The CP2102N does not strictly has input and output pin modes,
1898 * it only knows open-drain and push-pull modes which is set at
1899 * factory. An open-drain pin can function both as an
1900 * input or an output. We emulate input mode for open-drain pins
1901 * by making sure they are not driven low, and we do not allow
1902 * push-pull pins to be set as an input.
1903 */
1904 for (i = 0; i < priv->gc.ngpio; ++i) {
1905 /*
1906 * Set direction to "input" iff pin is open-drain and reset
1907 * value is 1.
1908 */
1909 if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
1910 priv->gpio_input |= BIT(i);
1911 }
1912
1913 return 0;
1914}
1915
1916static int cp210x_gpio_init(struct usb_serial *serial)
1917{
1918 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1919 int result;
1920
1921 switch (priv->partnum) {
Icenowy Zheng15fb84b2019-01-28 13:57:17 +08001922 case CP210X_PARTNUM_CP2104:
1923 result = cp2104_gpioconf_init(serial);
1924 break;
Karoly Padosc8acfe02018-07-20 12:52:40 +02001925 case CP210X_PARTNUM_CP2105:
1926 result = cp2105_gpioconf_init(serial);
1927 break;
1928 case CP210X_PARTNUM_CP2102N_QFN28:
1929 case CP210X_PARTNUM_CP2102N_QFN24:
1930 case CP210X_PARTNUM_CP2102N_QFN20:
1931 result = cp2102n_gpioconf_init(serial);
1932 break;
1933 default:
1934 return 0;
1935 }
1936
1937 if (result < 0)
1938 return result;
1939
Martyn Welchcf5276c2016-10-20 15:13:54 +01001940 priv->gc.label = "cp210x";
1941 priv->gc.request = cp210x_gpio_request;
1942 priv->gc.get_direction = cp210x_gpio_direction_get;
1943 priv->gc.direction_input = cp210x_gpio_direction_input;
1944 priv->gc.direction_output = cp210x_gpio_direction_output;
1945 priv->gc.get = cp210x_gpio_get;
1946 priv->gc.set = cp210x_gpio_set;
Mika Westerberg2956b5d2017-01-23 15:34:34 +03001947 priv->gc.set_config = cp210x_gpio_set_config;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001948 priv->gc.owner = THIS_MODULE;
1949 priv->gc.parent = &serial->interface->dev;
1950 priv->gc.base = -1;
1951 priv->gc.can_sleep = true;
1952
1953 result = gpiochip_add_data(&priv->gc, serial);
1954 if (!result)
Johan Hovold6b7271d22016-10-24 11:58:12 +02001955 priv->gpio_registered = true;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001956
1957 return result;
1958}
1959
1960static void cp210x_gpio_remove(struct usb_serial *serial)
1961{
1962 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1963
1964 if (priv->gpio_registered) {
1965 gpiochip_remove(&priv->gc);
Johan Hovold6b7271d22016-10-24 11:58:12 +02001966 priv->gpio_registered = false;
Martyn Welchcf5276c2016-10-20 15:13:54 +01001967 }
1968}
1969
1970#else
1971
Karoly Padosc8acfe02018-07-20 12:52:40 +02001972static int cp210x_gpio_init(struct usb_serial *serial)
Martyn Welchcf5276c2016-10-20 15:13:54 +01001973{
1974 return 0;
1975}
1976
1977static void cp210x_gpio_remove(struct usb_serial *serial)
1978{
1979 /* Nothing to do */
1980}
1981
1982#endif
1983
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05001984static int cp210x_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05001986 struct usb_serial *serial = port->serial;
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05001987 struct cp210x_port_private *port_priv;
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001988 int ret;
Preston Ficka5360a52012-02-24 13:42:39 -06001989
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05001990 port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1991 if (!port_priv)
Johan Hovold4295fe72012-10-15 15:47:20 +02001992 return -ENOMEM;
Preston Ficka5360a52012-02-24 13:42:39 -06001993
Martyn Welchcf5276c2016-10-20 15:13:54 +01001994 port_priv->bInterfaceNumber = cp210x_interface_num(serial);
Preston Ficka5360a52012-02-24 13:42:39 -06001995
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05001996 usb_set_serial_port_data(port, port_priv);
Preston Ficka5360a52012-02-24 13:42:39 -06001997
Konstantin Shkolnyyd0bf1ff2015-10-28 16:02:54 -05001998 ret = cp210x_detect_swapped_line_ctl(port);
1999 if (ret) {
2000 kfree(port_priv);
2001 return ret;
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 return 0;
2005}
2006
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05002007static int cp210x_port_remove(struct usb_serial_port *port)
Preston Ficka5360a52012-02-24 13:42:39 -06002008{
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05002009 struct cp210x_port_private *port_priv;
Preston Ficka5360a52012-02-24 13:42:39 -06002010
Konstantin Shkolnyye2ae67a2015-10-28 16:02:34 -05002011 port_priv = usb_get_serial_port_data(port);
2012 kfree(port_priv);
2013
2014 return 0;
Preston Ficka5360a52012-02-24 13:42:39 -06002015}
2016
Johan Hovoldd4706c052018-07-18 14:24:58 +02002017static void cp210x_init_max_speed(struct usb_serial *serial)
2018{
2019 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
Johan Hovold7aecd7f2018-07-18 14:25:00 +02002020 bool use_actual_rate = false;
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01002021 speed_t min = 300;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002022 speed_t max;
2023
2024 switch (priv->partnum) {
2025 case CP210X_PARTNUM_CP2101:
2026 max = 921600;
2027 break;
2028 case CP210X_PARTNUM_CP2102:
2029 case CP210X_PARTNUM_CP2103:
2030 max = 1000000;
2031 break;
2032 case CP210X_PARTNUM_CP2104:
Johan Hovold5edb65a2018-07-18 14:25:01 +02002033 use_actual_rate = true;
2034 max = 2000000;
2035 break;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002036 case CP210X_PARTNUM_CP2108:
2037 max = 2000000;
2038 break;
2039 case CP210X_PARTNUM_CP2105:
Johan Hovold5edb65a2018-07-18 14:25:01 +02002040 if (cp210x_interface_num(serial) == 0) {
2041 use_actual_rate = true;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002042 max = 2000000; /* ECI */
Johan Hovold5edb65a2018-07-18 14:25:01 +02002043 } else {
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01002044 min = 2400;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002045 max = 921600; /* SCI */
Johan Hovold5edb65a2018-07-18 14:25:01 +02002046 }
Johan Hovoldd4706c052018-07-18 14:24:58 +02002047 break;
Karoly Pados6f0bcf72018-07-18 14:24:59 +02002048 case CP210X_PARTNUM_CP2102N_QFN28:
2049 case CP210X_PARTNUM_CP2102N_QFN24:
2050 case CP210X_PARTNUM_CP2102N_QFN20:
Johan Hovold7aecd7f2018-07-18 14:25:00 +02002051 use_actual_rate = true;
Karoly Pados6f0bcf72018-07-18 14:24:59 +02002052 max = 3000000;
2053 break;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002054 default:
2055 max = 2000000;
2056 break;
2057 }
2058
Johanna Abrahamsson85bc2d92019-02-06 09:56:15 +01002059 priv->min_speed = min;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002060 priv->max_speed = max;
Johan Hovold7aecd7f2018-07-18 14:25:00 +02002061 priv->use_actual_rate = use_actual_rate;
Johan Hovoldd4706c052018-07-18 14:24:58 +02002062}
2063
Martyn Welchcf5276c2016-10-20 15:13:54 +01002064static int cp210x_attach(struct usb_serial *serial)
2065{
2066 int result;
2067 struct cp210x_serial_private *priv;
2068
2069 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2070 if (!priv)
2071 return -ENOMEM;
2072
2073 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
2074 CP210X_GET_PARTNUM, &priv->partnum,
2075 sizeof(priv->partnum));
Sebastian Frei7eac35e2017-09-12 09:50:59 +02002076 if (result < 0) {
2077 dev_warn(&serial->interface->dev,
2078 "querying part number failed\n");
2079 priv->partnum = CP210X_PARTNUM_UNKNOWN;
2080 }
Martyn Welchcf5276c2016-10-20 15:13:54 +01002081
2082 usb_set_serial_data(serial, priv);
2083
Johan Hovoldd4706c052018-07-18 14:24:58 +02002084 cp210x_init_max_speed(serial);
2085
Karoly Padosc8acfe02018-07-20 12:52:40 +02002086 result = cp210x_gpio_init(serial);
2087 if (result < 0) {
2088 dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
2089 result);
Martyn Welchcf5276c2016-10-20 15:13:54 +01002090 }
2091
2092 return 0;
Martyn Welchcf5276c2016-10-20 15:13:54 +01002093}
2094
2095static void cp210x_disconnect(struct usb_serial *serial)
2096{
2097 cp210x_gpio_remove(serial);
2098}
2099
2100static void cp210x_release(struct usb_serial *serial)
2101{
2102 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
2103
2104 cp210x_gpio_remove(serial);
2105
2106 kfree(priv);
2107}
2108
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07002109module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111MODULE_DESCRIPTION(DRIVER_DESC);
Johan Hovold627cfa82017-11-03 18:12:08 +01002112MODULE_LICENSE("GPL v2");