18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Support to set flow control line levels using TIOCMGET and TIOCMSET
88c2ecf20Sopenharmony_ci * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
98c2ecf20Sopenharmony_ci * control thanks to Munir Nassar nassarmu@real-time.com
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/errno.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/tty.h>
178c2ecf20Sopenharmony_ci#include <linux/tty_flip.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
208c2ecf20Sopenharmony_ci#include <linux/usb.h>
218c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
228c2ecf20Sopenharmony_ci#include <linux/usb/serial.h>
238c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
248c2ecf20Sopenharmony_ci#include <linux/bitops.h>
258c2ecf20Sopenharmony_ci#include <linux/mutex.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Function Prototypes
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_cistatic int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
338c2ecf20Sopenharmony_cistatic void cp210x_close(struct usb_serial_port *);
348c2ecf20Sopenharmony_cistatic void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
358c2ecf20Sopenharmony_cistatic void cp210x_get_termios_port(struct usb_serial_port *port,
368c2ecf20Sopenharmony_ci	tcflag_t *cflagp, unsigned int *baudp);
378c2ecf20Sopenharmony_cistatic void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
388c2ecf20Sopenharmony_ci							struct ktermios *);
398c2ecf20Sopenharmony_cistatic void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
408c2ecf20Sopenharmony_ci							struct ktermios*);
418c2ecf20Sopenharmony_cistatic bool cp210x_tx_empty(struct usb_serial_port *port);
428c2ecf20Sopenharmony_cistatic int cp210x_tiocmget(struct tty_struct *);
438c2ecf20Sopenharmony_cistatic int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
448c2ecf20Sopenharmony_cistatic int cp210x_tiocmset_port(struct usb_serial_port *port,
458c2ecf20Sopenharmony_ci		unsigned int, unsigned int);
468c2ecf20Sopenharmony_cistatic void cp210x_break_ctl(struct tty_struct *, int);
478c2ecf20Sopenharmony_cistatic int cp210x_attach(struct usb_serial *);
488c2ecf20Sopenharmony_cistatic void cp210x_disconnect(struct usb_serial *);
498c2ecf20Sopenharmony_cistatic void cp210x_release(struct usb_serial *);
508c2ecf20Sopenharmony_cistatic int cp210x_port_probe(struct usb_serial_port *);
518c2ecf20Sopenharmony_cistatic int cp210x_port_remove(struct usb_serial_port *);
528c2ecf20Sopenharmony_cistatic void cp210x_dtr_rts(struct usb_serial_port *p, int on);
538c2ecf20Sopenharmony_cistatic void cp210x_process_read_urb(struct urb *urb);
548c2ecf20Sopenharmony_cistatic void cp210x_enable_event_mode(struct usb_serial_port *port);
558c2ecf20Sopenharmony_cistatic void cp210x_disable_event_mode(struct usb_serial_port *port);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table[] = {
588c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0404, 0x034C) },	/* NCR Retail IO Box */
598c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
608c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
618c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
628c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0489, 0xE003) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
638c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
648c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0846, 0x1100) }, /* NetGear Managed Switch M4100 series, M5300 series, M7100 series */
658c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
668c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
678c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0908, 0x0070) }, /* Siemens SCALANCE LPE-9000 USB Serial Console */
688c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0908, 0x01FF) }, /* Siemens RUGGEDCOM USB Serial Console */
698c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0988, 0x0578) }, /* Teraoka AD2000 */
708c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0B00, 0x3070) }, /* Ingenico 3070 */
718c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0BED, 0x1100) }, /* MEI (TM) Cashflow-SC Bill/Voucher Acceptor */
728c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0BED, 0x1101) }, /* MEI series 2000 Combo Acceptor */
738c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
748c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
758c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
768c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x0FDE, 0xCA05) }, /* OWL Wireless Electricity Monitor CM-160 */
778c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x106F, 0x0003) },	/* CPI / Money Controls Bulk Coin Recycler */
788c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
798c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
808c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
818c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
828c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
838c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
848c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
858c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
868c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8044) }, /* Cygnal Debug Adapter */
878c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x804E) }, /* Software Bisque Paramount ME build-in converter */
888c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
898c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
908c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8056) }, /* Lorenz Messtechnik devices */
918c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
928c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x806F) }, /* IMS USB to RS422 Converter Cable */
938c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
948c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x80C4) }, /* Cygnal Integrated Products, Inc., Optris infrared thermometer */
958c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
968c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
978c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
988c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
998c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
1008c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
1018c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
1028c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
1038c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2405, 0x0003) }, /* West Mountain Radio RIGblaster Advantage */
1048c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8156) }, /* B&G H3000 link cable */
1058c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
1068c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x815F) }, /* Timewave HamLinkUSB */
1078c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x817C) }, /* CESINEL MEDCAL N Power Quality Monitor */
1088c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x817D) }, /* CESINEL MEDCAL NT Power Quality Monitor */
1098c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x817E) }, /* CESINEL MEDCAL S Power Quality Monitor */
1108c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x818B) }, /* AVIT Research USB to TTL */
1118c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
1128c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
1138c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81A9) }, /* Multiplex RC Interface */
1148c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
1158c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81AD) }, /* INSYS USB Modem */
1168c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
1178c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81D7) }, /* IAI Corp. RCB-CV-USB USB to RS485 Adaptor */
1188c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
1198c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
1208c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
1218c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
1228c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
1238c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
1248c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demonstration module */
1258c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8281) }, /* Nanotec Plug & Drive */
1268c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8293) }, /* Telegesis ETRX2USB */
1278c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82AA) }, /* Silicon Labs IFS-USB-DATACABLE used with Quint UPS */
1288c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82EF) }, /* CESINEL FALCO 6105 AC Power Supply */
1298c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82F1) }, /* CESINEL MEDCAL EFD Earth Fault Detector */
1308c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82F2) }, /* CESINEL MEDCAL ST Network Analyzer */
1318c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82F4) }, /* Starizona MicroTouch */
1328c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
1338c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
1348c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
1358c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
1368c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x83AA) }, /* Mark-10 Digital Force Gauge */
1378c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x83D8) }, /* DekTec DTA Plus VHF/UHF Booster/Attenuator */
1388c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
1398c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8414) }, /* Decagon USB Cable Adapter */
1408c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8418) }, /* IRZ Automation Teleport SG-10 GSM/GPRS Modem */
1418c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
1428c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8470) }, /* Juniper Networks BX Series System Console */
1438c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8477) }, /* Balluff RFID */
1448c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x84B6) }, /* Starizona Hyperion */
1458c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x851E) }, /* CESINEL MEDCAL PT Network Analyzer */
1468c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x85A7) }, /* LifeScan OneTouch Verio IQ */
1478c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x85B8) }, /* CESINEL ReCon T Energy Logger */
1488c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x85EA) }, /* AC-Services IBUS-IF */
1498c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x85EB) }, /* AC-Services CIS-IBUS */
1508c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x85F8) }, /* Virtenio Preon32 */
1518c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8664) }, /* AC-Services CAN-IF */
1528c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8665) }, /* AC-Services OBD-IF */
1538c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x87ED) }, /* IMST USB-Stick for Smart Meter */
1548c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8856) },	/* CEL EM357 ZigBee USB Stick - LR */
1558c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8857) },	/* CEL EM357 ZigBee USB Stick */
1568c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x88A4) }, /* MMB Networks ZigBee USB Device */
1578c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x88A5) }, /* Planet Innovation Ingeni ZigBee USB Device */
1588c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x88D8) }, /* Acuity Brands nLight Air Adapter */
1598c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x88FB) }, /* CESINEL MEDCAL STII Network Analyzer */
1608c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8938) }, /* CESINEL MEDCAL S II Network Analyzer */
1618c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8946) }, /* Ketra N1 Wireless Interface */
1628c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8962) }, /* Brim Brothers charging dock */
1638c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8977) },	/* CEL MeshWorks DevKit Device */
1648c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8998) }, /* KCF Technologies PRN */
1658c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x89A4) }, /* CESINEL FTBC Flexible Thyristor Bridge Controller */
1668c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x89FB) }, /* Qivicon ZigBee USB Radio Stick */
1678c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8A2A) }, /* HubZ dual ZigBee and Z-Wave dongle */
1688c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8A5B) }, /* CEL EM3588 ZigBee USB Stick */
1698c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8A5E) }, /* CEL EM3588 ZigBee USB Stick Long Range */
1708c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0x8B34) }, /* Qivicon ZigBee USB Radio Stick */
1718c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
1728c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
1738c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA63) }, /* Silicon Labs Windows Update (CP2101-4/CP2102N) */
1748c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA70) }, /* Silicon Labs factory default */
1758c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA71) }, /* Infinity GPS-MIC-1 Radio Monophone */
1768c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA7A) }, /* Silicon Labs Windows Update (CP2105) */
1778c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xEA7B) }, /* Silicon Labs Windows Update (CP2108) */
1788c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
1798c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
1808c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
1818c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
1828c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
1838c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
1848c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x12B8, 0xEC60) }, /* Link G4 ECU */
1858c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x12B8, 0xEC62) }, /* Link G4+ ECU */
1868c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
1878c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
1888c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x155A, 0x1006) },	/* ELDAT Easywave RX09 */
1898c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0201) }, /* Clipsal 5500PACA C-Bus Pascal Automation Controller */
1908c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0301) }, /* Clipsal 5800PC C-Bus Wireless PC Interface */
1918c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
1928c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0304) }, /* Clipsal 5000CT2 C-Bus Black and White Touchscreen */
1938c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0305) }, /* Clipsal C-5000CT2 C-Bus Spectrum Colour Touchscreen */
1948c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0401) }, /* Clipsal L51xx C-Bus Architectural Dimmer */
1958c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x166A, 0x0101) }, /* Clipsal 5560884 C-Bus Multi-room Audio Matrix Switcher */
1968c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16C0, 0x09B0) }, /* Lunatico Seletek */
1978c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16C0, 0x09B1) }, /* Lunatico Seletek */
1988c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
1998c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16DC, 0x0010) }, /* W-IE-NE-R Plein & Baus GmbH PL512 Power Supply */
2008c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16DC, 0x0011) }, /* W-IE-NE-R Plein & Baus GmbH RCM Remote Control for MARATON Power Supply */
2018c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16DC, 0x0012) }, /* W-IE-NE-R Plein & Baus GmbH MPOD Multi Channel Power Supply */
2028c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x16DC, 0x0015) }, /* W-IE-NE-R Plein & Baus GmbH CML Control, Monitoring and Data Logger */
2038c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0001) }, /* Kamstrup Optical Eye/3-wire */
2048c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0005) }, /* Kamstrup M-Bus Master MultiPort 250D */
2058c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0011) }, /* Kamstrup 444 MHz RF sniffer */
2068c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0013) }, /* Kamstrup 870 MHz RF sniffer */
2078c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0101) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Int Ant) */
2088c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17A8, 0x0102) }, /* Kamstrup 868 MHz wM-Bus C-Mode Meter Reader (Ext Ant) */
2098c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x17F4, 0xAAAA) }, /* Wavesense Jazz blood glucose meter */
2108c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
2118c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
2128c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x18EF, 0xE025) }, /* ELV Marble Sound Board 1 */
2138c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x18EF, 0xE030) }, /* ELV ALC 8xxx Battery Charger */
2148c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x18EF, 0xE032) }, /* ELV TFD500 Data Logger */
2158c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0190) }, /* GE B850 CP2105 Recorder interface */
2168c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0193) }, /* GE B650 CP2104 PMC interface */
2178c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0194) },	/* GE Healthcare Remote Alarm Box */
2188c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0195) },	/* GE B850/B650/B450 CP2104 DP UART interface */
2198c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0196) },	/* GE B850 CP2105 DP UART interface */
2208c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0197) }, /* GE CS1000 M.2 Key E serial interface */
2218c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1901, 0x0198) }, /* GE CS1000 Display serial interface */
2228c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x199B, 0xBA30) }, /* LORD WSDA-200-USB */
2238c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x19CF, 0x3000) }, /* Parrot NMEA GPS Flight Recorder */
2248c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1ADB, 0x0001) }, /* Schweitzer Engineering C662 Cable */
2258c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1B1C, 0x1C00) }, /* Corsair USB Dongle */
2268c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1BA4, 0x0002) },	/* Silicon Labs 358x factory default */
2278c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
2288c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1D6F, 0x0010) }, /* Seluxit ApS RF Dongle */
2298c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1E29, 0x0102) }, /* Festo CPX-USB */
2308c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1E29, 0x0501) }, /* Festo CMSP */
2318c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0100) }, /* Lake Shore Model 121 Current Source */
2328c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0200) }, /* Lake Shore Model 218A Temperature Monitor */
2338c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0201) }, /* Lake Shore Model 219 Temperature Monitor */
2348c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0202) }, /* Lake Shore Model 233 Temperature Transmitter */
2358c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0203) }, /* Lake Shore Model 235 Temperature Transmitter */
2368c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0300) }, /* Lake Shore Model 335 Temperature Controller */
2378c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0301) }, /* Lake Shore Model 336 Temperature Controller */
2388c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0302) }, /* Lake Shore Model 350 Temperature Controller */
2398c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0303) }, /* Lake Shore Model 371 AC Bridge */
2408c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0400) }, /* Lake Shore Model 411 Handheld Gaussmeter */
2418c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0401) }, /* Lake Shore Model 425 Gaussmeter */
2428c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0402) }, /* Lake Shore Model 455A Gaussmeter */
2438c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0403) }, /* Lake Shore Model 475A Gaussmeter */
2448c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0404) }, /* Lake Shore Model 465 Three Axis Gaussmeter */
2458c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0600) }, /* Lake Shore Model 625A Superconducting MPS */
2468c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0601) }, /* Lake Shore Model 642A Magnet Power Supply */
2478c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0602) }, /* Lake Shore Model 648 Magnet Power Supply */
2488c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0700) }, /* Lake Shore Model 737 VSM Controller */
2498c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x1FB9, 0x0701) }, /* Lake Shore Model 776 Hall Matrix */
2508c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2184, 0x0030) }, /* GW Instek GDM-834x Digital Multimeter */
2518c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x2626, 0xEA60) }, /* Aruba Networks 7xxx USB Serial Console */
2528c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
2538c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
2548c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
2558c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x3923, 0x7A0B) }, /* National Instruments USB Serial Console */
2568c2ecf20Sopenharmony_ci	{ USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
2578c2ecf20Sopenharmony_ci	{ } /* Terminating Entry */
2588c2ecf20Sopenharmony_ci};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistruct cp210x_serial_private {
2638c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
2648c2ecf20Sopenharmony_ci	struct gpio_chip	gc;
2658c2ecf20Sopenharmony_ci	bool			gpio_registered;
2668c2ecf20Sopenharmony_ci	u8			gpio_pushpull;
2678c2ecf20Sopenharmony_ci	u8			gpio_altfunc;
2688c2ecf20Sopenharmony_ci	u8			gpio_input;
2698c2ecf20Sopenharmony_ci#endif
2708c2ecf20Sopenharmony_ci	u8			partnum;
2718c2ecf20Sopenharmony_ci	speed_t			min_speed;
2728c2ecf20Sopenharmony_ci	speed_t			max_speed;
2738c2ecf20Sopenharmony_ci	bool			use_actual_rate;
2748c2ecf20Sopenharmony_ci	bool			no_event_mode;
2758c2ecf20Sopenharmony_ci};
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cienum cp210x_event_state {
2788c2ecf20Sopenharmony_ci	ES_DATA,
2798c2ecf20Sopenharmony_ci	ES_ESCAPE,
2808c2ecf20Sopenharmony_ci	ES_LSR,
2818c2ecf20Sopenharmony_ci	ES_LSR_DATA_0,
2828c2ecf20Sopenharmony_ci	ES_LSR_DATA_1,
2838c2ecf20Sopenharmony_ci	ES_MSR
2848c2ecf20Sopenharmony_ci};
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistruct cp210x_port_private {
2878c2ecf20Sopenharmony_ci	u8			bInterfaceNumber;
2888c2ecf20Sopenharmony_ci	bool			has_swapped_line_ctl;
2898c2ecf20Sopenharmony_ci	bool			event_mode;
2908c2ecf20Sopenharmony_ci	enum cp210x_event_state event_state;
2918c2ecf20Sopenharmony_ci	u8 lsr;
2928c2ecf20Sopenharmony_ci};
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic struct usb_serial_driver cp210x_device = {
2958c2ecf20Sopenharmony_ci	.driver = {
2968c2ecf20Sopenharmony_ci		.owner =	THIS_MODULE,
2978c2ecf20Sopenharmony_ci		.name =		"cp210x",
2988c2ecf20Sopenharmony_ci	},
2998c2ecf20Sopenharmony_ci	.id_table		= id_table,
3008c2ecf20Sopenharmony_ci	.num_ports		= 1,
3018c2ecf20Sopenharmony_ci	.bulk_in_size		= 256,
3028c2ecf20Sopenharmony_ci	.bulk_out_size		= 256,
3038c2ecf20Sopenharmony_ci	.open			= cp210x_open,
3048c2ecf20Sopenharmony_ci	.close			= cp210x_close,
3058c2ecf20Sopenharmony_ci	.break_ctl		= cp210x_break_ctl,
3068c2ecf20Sopenharmony_ci	.set_termios		= cp210x_set_termios,
3078c2ecf20Sopenharmony_ci	.tx_empty		= cp210x_tx_empty,
3088c2ecf20Sopenharmony_ci	.throttle		= usb_serial_generic_throttle,
3098c2ecf20Sopenharmony_ci	.unthrottle		= usb_serial_generic_unthrottle,
3108c2ecf20Sopenharmony_ci	.tiocmget		= cp210x_tiocmget,
3118c2ecf20Sopenharmony_ci	.tiocmset		= cp210x_tiocmset,
3128c2ecf20Sopenharmony_ci	.get_icount		= usb_serial_generic_get_icount,
3138c2ecf20Sopenharmony_ci	.attach			= cp210x_attach,
3148c2ecf20Sopenharmony_ci	.disconnect		= cp210x_disconnect,
3158c2ecf20Sopenharmony_ci	.release		= cp210x_release,
3168c2ecf20Sopenharmony_ci	.port_probe		= cp210x_port_probe,
3178c2ecf20Sopenharmony_ci	.port_remove		= cp210x_port_remove,
3188c2ecf20Sopenharmony_ci	.dtr_rts		= cp210x_dtr_rts,
3198c2ecf20Sopenharmony_ci	.process_read_urb	= cp210x_process_read_urb,
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
3238c2ecf20Sopenharmony_ci	&cp210x_device, NULL
3248c2ecf20Sopenharmony_ci};
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci/* Config request types */
3278c2ecf20Sopenharmony_ci#define REQTYPE_HOST_TO_INTERFACE	0x41
3288c2ecf20Sopenharmony_ci#define REQTYPE_INTERFACE_TO_HOST	0xc1
3298c2ecf20Sopenharmony_ci#define REQTYPE_HOST_TO_DEVICE	0x40
3308c2ecf20Sopenharmony_ci#define REQTYPE_DEVICE_TO_HOST	0xc0
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci/* Config request codes */
3338c2ecf20Sopenharmony_ci#define CP210X_IFC_ENABLE	0x00
3348c2ecf20Sopenharmony_ci#define CP210X_SET_BAUDDIV	0x01
3358c2ecf20Sopenharmony_ci#define CP210X_GET_BAUDDIV	0x02
3368c2ecf20Sopenharmony_ci#define CP210X_SET_LINE_CTL	0x03
3378c2ecf20Sopenharmony_ci#define CP210X_GET_LINE_CTL	0x04
3388c2ecf20Sopenharmony_ci#define CP210X_SET_BREAK	0x05
3398c2ecf20Sopenharmony_ci#define CP210X_IMM_CHAR		0x06
3408c2ecf20Sopenharmony_ci#define CP210X_SET_MHS		0x07
3418c2ecf20Sopenharmony_ci#define CP210X_GET_MDMSTS	0x08
3428c2ecf20Sopenharmony_ci#define CP210X_SET_XON		0x09
3438c2ecf20Sopenharmony_ci#define CP210X_SET_XOFF		0x0A
3448c2ecf20Sopenharmony_ci#define CP210X_SET_EVENTMASK	0x0B
3458c2ecf20Sopenharmony_ci#define CP210X_GET_EVENTMASK	0x0C
3468c2ecf20Sopenharmony_ci#define CP210X_SET_CHAR		0x0D
3478c2ecf20Sopenharmony_ci#define CP210X_GET_CHARS	0x0E
3488c2ecf20Sopenharmony_ci#define CP210X_GET_PROPS	0x0F
3498c2ecf20Sopenharmony_ci#define CP210X_GET_COMM_STATUS	0x10
3508c2ecf20Sopenharmony_ci#define CP210X_RESET		0x11
3518c2ecf20Sopenharmony_ci#define CP210X_PURGE		0x12
3528c2ecf20Sopenharmony_ci#define CP210X_SET_FLOW		0x13
3538c2ecf20Sopenharmony_ci#define CP210X_GET_FLOW		0x14
3548c2ecf20Sopenharmony_ci#define CP210X_EMBED_EVENTS	0x15
3558c2ecf20Sopenharmony_ci#define CP210X_GET_EVENTSTATE	0x16
3568c2ecf20Sopenharmony_ci#define CP210X_SET_CHARS	0x19
3578c2ecf20Sopenharmony_ci#define CP210X_GET_BAUDRATE	0x1D
3588c2ecf20Sopenharmony_ci#define CP210X_SET_BAUDRATE	0x1E
3598c2ecf20Sopenharmony_ci#define CP210X_VENDOR_SPECIFIC	0xFF
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci/* CP210X_IFC_ENABLE */
3628c2ecf20Sopenharmony_ci#define UART_ENABLE		0x0001
3638c2ecf20Sopenharmony_ci#define UART_DISABLE		0x0000
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/* CP210X_(SET|GET)_BAUDDIV */
3668c2ecf20Sopenharmony_ci#define BAUD_RATE_GEN_FREQ	0x384000
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/* CP210X_(SET|GET)_LINE_CTL */
3698c2ecf20Sopenharmony_ci#define BITS_DATA_MASK		0X0f00
3708c2ecf20Sopenharmony_ci#define BITS_DATA_5		0X0500
3718c2ecf20Sopenharmony_ci#define BITS_DATA_6		0X0600
3728c2ecf20Sopenharmony_ci#define BITS_DATA_7		0X0700
3738c2ecf20Sopenharmony_ci#define BITS_DATA_8		0X0800
3748c2ecf20Sopenharmony_ci#define BITS_DATA_9		0X0900
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci#define BITS_PARITY_MASK	0x00f0
3778c2ecf20Sopenharmony_ci#define BITS_PARITY_NONE	0x0000
3788c2ecf20Sopenharmony_ci#define BITS_PARITY_ODD		0x0010
3798c2ecf20Sopenharmony_ci#define BITS_PARITY_EVEN	0x0020
3808c2ecf20Sopenharmony_ci#define BITS_PARITY_MARK	0x0030
3818c2ecf20Sopenharmony_ci#define BITS_PARITY_SPACE	0x0040
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci#define BITS_STOP_MASK		0x000f
3848c2ecf20Sopenharmony_ci#define BITS_STOP_1		0x0000
3858c2ecf20Sopenharmony_ci#define BITS_STOP_1_5		0x0001
3868c2ecf20Sopenharmony_ci#define BITS_STOP_2		0x0002
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/* CP210X_SET_BREAK */
3898c2ecf20Sopenharmony_ci#define BREAK_ON		0x0001
3908c2ecf20Sopenharmony_ci#define BREAK_OFF		0x0000
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci/* CP210X_(SET_MHS|GET_MDMSTS) */
3938c2ecf20Sopenharmony_ci#define CONTROL_DTR		0x0001
3948c2ecf20Sopenharmony_ci#define CONTROL_RTS		0x0002
3958c2ecf20Sopenharmony_ci#define CONTROL_CTS		0x0010
3968c2ecf20Sopenharmony_ci#define CONTROL_DSR		0x0020
3978c2ecf20Sopenharmony_ci#define CONTROL_RING		0x0040
3988c2ecf20Sopenharmony_ci#define CONTROL_DCD		0x0080
3998c2ecf20Sopenharmony_ci#define CONTROL_WRITE_DTR	0x0100
4008c2ecf20Sopenharmony_ci#define CONTROL_WRITE_RTS	0x0200
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/* CP210X_VENDOR_SPECIFIC values */
4038c2ecf20Sopenharmony_ci#define CP210X_READ_2NCONFIG	0x000E
4048c2ecf20Sopenharmony_ci#define CP210X_READ_LATCH	0x00C2
4058c2ecf20Sopenharmony_ci#define CP210X_GET_PARTNUM	0x370B
4068c2ecf20Sopenharmony_ci#define CP210X_GET_PORTCONFIG	0x370C
4078c2ecf20Sopenharmony_ci#define CP210X_GET_DEVICEMODE	0x3711
4088c2ecf20Sopenharmony_ci#define CP210X_WRITE_LATCH	0x37E1
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci/* Part number definitions */
4118c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2101	0x01
4128c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2102	0x02
4138c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2103	0x03
4148c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2104	0x04
4158c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2105	0x05
4168c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2108	0x08
4178c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2102N_QFN28	0x20
4188c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2102N_QFN24	0x21
4198c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_CP2102N_QFN20	0x22
4208c2ecf20Sopenharmony_ci#define CP210X_PARTNUM_UNKNOWN	0xFF
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci/* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
4238c2ecf20Sopenharmony_cistruct cp210x_comm_status {
4248c2ecf20Sopenharmony_ci	__le32   ulErrors;
4258c2ecf20Sopenharmony_ci	__le32   ulHoldReasons;
4268c2ecf20Sopenharmony_ci	__le32   ulAmountInInQueue;
4278c2ecf20Sopenharmony_ci	__le32   ulAmountInOutQueue;
4288c2ecf20Sopenharmony_ci	u8       bEofReceived;
4298c2ecf20Sopenharmony_ci	u8       bWaitForImmediate;
4308c2ecf20Sopenharmony_ci	u8       bReserved;
4318c2ecf20Sopenharmony_ci} __packed;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci/*
4348c2ecf20Sopenharmony_ci * CP210X_PURGE - 16 bits passed in wValue of USB request.
4358c2ecf20Sopenharmony_ci * SiLabs app note AN571 gives a strange description of the 4 bits:
4368c2ecf20Sopenharmony_ci * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
4378c2ecf20Sopenharmony_ci * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
4388c2ecf20Sopenharmony_ci */
4398c2ecf20Sopenharmony_ci#define PURGE_ALL		0x000f
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci/* CP210X_EMBED_EVENTS */
4428c2ecf20Sopenharmony_ci#define CP210X_ESCCHAR		0xec
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci#define CP210X_LSR_OVERRUN	BIT(1)
4458c2ecf20Sopenharmony_ci#define CP210X_LSR_PARITY	BIT(2)
4468c2ecf20Sopenharmony_ci#define CP210X_LSR_FRAME	BIT(3)
4478c2ecf20Sopenharmony_ci#define CP210X_LSR_BREAK	BIT(4)
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci/* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
4518c2ecf20Sopenharmony_cistruct cp210x_flow_ctl {
4528c2ecf20Sopenharmony_ci	__le32	ulControlHandshake;
4538c2ecf20Sopenharmony_ci	__le32	ulFlowReplace;
4548c2ecf20Sopenharmony_ci	__le32	ulXonLimit;
4558c2ecf20Sopenharmony_ci	__le32	ulXoffLimit;
4568c2ecf20Sopenharmony_ci};
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/* cp210x_flow_ctl::ulControlHandshake */
4598c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DTR_MASK		GENMASK(1, 0)
4608c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DTR_SHIFT(_mode)	(_mode)
4618c2ecf20Sopenharmony_ci#define CP210X_SERIAL_CTS_HANDSHAKE	BIT(3)
4628c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DSR_HANDSHAKE	BIT(4)
4638c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DCD_HANDSHAKE	BIT(5)
4648c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DSR_SENSITIVITY	BIT(6)
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
4678c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DTR_INACTIVE	0
4688c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DTR_ACTIVE	1
4698c2ecf20Sopenharmony_ci#define CP210X_SERIAL_DTR_FLOW_CTL	2
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci/* cp210x_flow_ctl::ulFlowReplace */
4728c2ecf20Sopenharmony_ci#define CP210X_SERIAL_AUTO_TRANSMIT	BIT(0)
4738c2ecf20Sopenharmony_ci#define CP210X_SERIAL_AUTO_RECEIVE	BIT(1)
4748c2ecf20Sopenharmony_ci#define CP210X_SERIAL_ERROR_CHAR	BIT(2)
4758c2ecf20Sopenharmony_ci#define CP210X_SERIAL_NULL_STRIPPING	BIT(3)
4768c2ecf20Sopenharmony_ci#define CP210X_SERIAL_BREAK_CHAR	BIT(4)
4778c2ecf20Sopenharmony_ci#define CP210X_SERIAL_RTS_MASK		GENMASK(7, 6)
4788c2ecf20Sopenharmony_ci#define CP210X_SERIAL_RTS_SHIFT(_mode)	(_mode << 6)
4798c2ecf20Sopenharmony_ci#define CP210X_SERIAL_XOFF_CONTINUE	BIT(31)
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
4828c2ecf20Sopenharmony_ci#define CP210X_SERIAL_RTS_INACTIVE	0
4838c2ecf20Sopenharmony_ci#define CP210X_SERIAL_RTS_ACTIVE	1
4848c2ecf20Sopenharmony_ci#define CP210X_SERIAL_RTS_FLOW_CTL	2
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
4878c2ecf20Sopenharmony_cistruct cp210x_pin_mode {
4888c2ecf20Sopenharmony_ci	u8	eci;
4898c2ecf20Sopenharmony_ci	u8	sci;
4908c2ecf20Sopenharmony_ci};
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci#define CP210X_PIN_MODE_MODEM		0
4938c2ecf20Sopenharmony_ci#define CP210X_PIN_MODE_GPIO		BIT(0)
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci/*
4968c2ecf20Sopenharmony_ci * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes
4978c2ecf20Sopenharmony_ci * on a CP2105 chip. Structure needs padding due to unused/unspecified bytes.
4988c2ecf20Sopenharmony_ci */
4998c2ecf20Sopenharmony_cistruct cp210x_dual_port_config {
5008c2ecf20Sopenharmony_ci	__le16	gpio_mode;
5018c2ecf20Sopenharmony_ci	u8	__pad0[2];
5028c2ecf20Sopenharmony_ci	__le16	reset_state;
5038c2ecf20Sopenharmony_ci	u8	__pad1[4];
5048c2ecf20Sopenharmony_ci	__le16	suspend_state;
5058c2ecf20Sopenharmony_ci	u8	sci_cfg;
5068c2ecf20Sopenharmony_ci	u8	eci_cfg;
5078c2ecf20Sopenharmony_ci	u8	device_cfg;
5088c2ecf20Sopenharmony_ci} __packed;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/*
5118c2ecf20Sopenharmony_ci * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xd bytes
5128c2ecf20Sopenharmony_ci * on a CP2104 chip. Structure needs padding due to unused/unspecified bytes.
5138c2ecf20Sopenharmony_ci */
5148c2ecf20Sopenharmony_cistruct cp210x_single_port_config {
5158c2ecf20Sopenharmony_ci	__le16	gpio_mode;
5168c2ecf20Sopenharmony_ci	u8	__pad0[2];
5178c2ecf20Sopenharmony_ci	__le16	reset_state;
5188c2ecf20Sopenharmony_ci	u8	__pad1[4];
5198c2ecf20Sopenharmony_ci	__le16	suspend_state;
5208c2ecf20Sopenharmony_ci	u8	device_cfg;
5218c2ecf20Sopenharmony_ci} __packed;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci/* GPIO modes */
5248c2ecf20Sopenharmony_ci#define CP210X_SCI_GPIO_MODE_OFFSET	9
5258c2ecf20Sopenharmony_ci#define CP210X_SCI_GPIO_MODE_MASK	GENMASK(11, 9)
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci#define CP210X_ECI_GPIO_MODE_OFFSET	2
5288c2ecf20Sopenharmony_ci#define CP210X_ECI_GPIO_MODE_MASK	GENMASK(3, 2)
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci#define CP210X_GPIO_MODE_OFFSET		8
5318c2ecf20Sopenharmony_ci#define CP210X_GPIO_MODE_MASK		GENMASK(11, 8)
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci/* CP2105 port configuration values */
5348c2ecf20Sopenharmony_ci#define CP2105_GPIO0_TXLED_MODE		BIT(0)
5358c2ecf20Sopenharmony_ci#define CP2105_GPIO1_RXLED_MODE		BIT(1)
5368c2ecf20Sopenharmony_ci#define CP2105_GPIO1_RS485_MODE		BIT(2)
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/* CP2104 port configuration values */
5398c2ecf20Sopenharmony_ci#define CP2104_GPIO0_TXLED_MODE		BIT(0)
5408c2ecf20Sopenharmony_ci#define CP2104_GPIO1_RXLED_MODE		BIT(1)
5418c2ecf20Sopenharmony_ci#define CP2104_GPIO2_RS485_MODE		BIT(2)
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci/* CP2102N configuration array indices */
5448c2ecf20Sopenharmony_ci#define CP210X_2NCONFIG_CONFIG_VERSION_IDX	2
5458c2ecf20Sopenharmony_ci#define CP210X_2NCONFIG_GPIO_MODE_IDX		581
5468c2ecf20Sopenharmony_ci#define CP210X_2NCONFIG_GPIO_RSTLATCH_IDX	587
5478c2ecf20Sopenharmony_ci#define CP210X_2NCONFIG_GPIO_CONTROL_IDX	600
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci/* CP2102N QFN20 port configuration values */
5508c2ecf20Sopenharmony_ci#define CP2102N_QFN20_GPIO2_TXLED_MODE		BIT(2)
5518c2ecf20Sopenharmony_ci#define CP2102N_QFN20_GPIO3_RXLED_MODE		BIT(3)
5528c2ecf20Sopenharmony_ci#define CP2102N_QFN20_GPIO1_RS485_MODE		BIT(4)
5538c2ecf20Sopenharmony_ci#define CP2102N_QFN20_GPIO0_CLK_MODE		BIT(6)
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci/* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
5568c2ecf20Sopenharmony_cistruct cp210x_gpio_write {
5578c2ecf20Sopenharmony_ci	u8	mask;
5588c2ecf20Sopenharmony_ci	u8	state;
5598c2ecf20Sopenharmony_ci};
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/*
5628c2ecf20Sopenharmony_ci * Helper to get interface number when we only have struct usb_serial.
5638c2ecf20Sopenharmony_ci */
5648c2ecf20Sopenharmony_cistatic u8 cp210x_interface_num(struct usb_serial *serial)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	struct usb_host_interface *cur_altsetting;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	cur_altsetting = serial->interface->cur_altsetting;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	return cur_altsetting->desc.bInterfaceNumber;
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/*
5748c2ecf20Sopenharmony_ci * Reads a variable-sized block of CP210X_ registers, identified by req.
5758c2ecf20Sopenharmony_ci * Returns data into buf in native USB byte order.
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_cistatic int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
5788c2ecf20Sopenharmony_ci		void *buf, int bufsize)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
5818c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
5828c2ecf20Sopenharmony_ci	void *dmabuf;
5838c2ecf20Sopenharmony_ci	int result;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	dmabuf = kmalloc(bufsize, GFP_KERNEL);
5868c2ecf20Sopenharmony_ci	if (!dmabuf) {
5878c2ecf20Sopenharmony_ci		/*
5888c2ecf20Sopenharmony_ci		 * FIXME Some callers don't bother to check for error,
5898c2ecf20Sopenharmony_ci		 * at least give them consistent junk until they are fixed
5908c2ecf20Sopenharmony_ci		 */
5918c2ecf20Sopenharmony_ci		memset(buf, 0, bufsize);
5928c2ecf20Sopenharmony_ci		return -ENOMEM;
5938c2ecf20Sopenharmony_ci	}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
5968c2ecf20Sopenharmony_ci			req, REQTYPE_INTERFACE_TO_HOST, 0,
5978c2ecf20Sopenharmony_ci			port_priv->bInterfaceNumber, dmabuf, bufsize,
5988c2ecf20Sopenharmony_ci			USB_CTRL_SET_TIMEOUT);
5998c2ecf20Sopenharmony_ci	if (result == bufsize) {
6008c2ecf20Sopenharmony_ci		memcpy(buf, dmabuf, bufsize);
6018c2ecf20Sopenharmony_ci		result = 0;
6028c2ecf20Sopenharmony_ci	} else {
6038c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
6048c2ecf20Sopenharmony_ci				req, bufsize, result);
6058c2ecf20Sopenharmony_ci		if (result >= 0)
6068c2ecf20Sopenharmony_ci			result = -EIO;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		/*
6098c2ecf20Sopenharmony_ci		 * FIXME Some callers don't bother to check for error,
6108c2ecf20Sopenharmony_ci		 * at least give them consistent junk until they are fixed
6118c2ecf20Sopenharmony_ci		 */
6128c2ecf20Sopenharmony_ci		memset(buf, 0, bufsize);
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	kfree(dmabuf);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	return result;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci/*
6218c2ecf20Sopenharmony_ci * Reads any 32-bit CP210X_ register identified by req.
6228c2ecf20Sopenharmony_ci */
6238c2ecf20Sopenharmony_cistatic int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	__le32 le32_val;
6268c2ecf20Sopenharmony_ci	int err;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
6298c2ecf20Sopenharmony_ci	if (err) {
6308c2ecf20Sopenharmony_ci		/*
6318c2ecf20Sopenharmony_ci		 * FIXME Some callers don't bother to check for error,
6328c2ecf20Sopenharmony_ci		 * at least give them consistent junk until they are fixed
6338c2ecf20Sopenharmony_ci		 */
6348c2ecf20Sopenharmony_ci		*val = 0;
6358c2ecf20Sopenharmony_ci		return err;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	*val = le32_to_cpu(le32_val);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	return 0;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci/*
6448c2ecf20Sopenharmony_ci * Reads any 16-bit CP210X_ register identified by req.
6458c2ecf20Sopenharmony_ci */
6468c2ecf20Sopenharmony_cistatic int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
6478c2ecf20Sopenharmony_ci{
6488c2ecf20Sopenharmony_ci	__le16 le16_val;
6498c2ecf20Sopenharmony_ci	int err;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
6528c2ecf20Sopenharmony_ci	if (err)
6538c2ecf20Sopenharmony_ci		return err;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	*val = le16_to_cpu(le16_val);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	return 0;
6588c2ecf20Sopenharmony_ci}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci/*
6618c2ecf20Sopenharmony_ci * Reads any 8-bit CP210X_ register identified by req.
6628c2ecf20Sopenharmony_ci */
6638c2ecf20Sopenharmony_cistatic int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	return cp210x_read_reg_block(port, req, val, sizeof(*val));
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci/*
6698c2ecf20Sopenharmony_ci * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
6708c2ecf20Sopenharmony_ci * Returns data into buf in native USB byte order.
6718c2ecf20Sopenharmony_ci */
6728c2ecf20Sopenharmony_cistatic int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
6738c2ecf20Sopenharmony_ci				    void *buf, int bufsize)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	void *dmabuf;
6768c2ecf20Sopenharmony_ci	int result;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	dmabuf = kmalloc(bufsize, GFP_KERNEL);
6798c2ecf20Sopenharmony_ci	if (!dmabuf)
6808c2ecf20Sopenharmony_ci		return -ENOMEM;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
6838c2ecf20Sopenharmony_ci				 CP210X_VENDOR_SPECIFIC, type, val,
6848c2ecf20Sopenharmony_ci				 cp210x_interface_num(serial), dmabuf, bufsize,
6858c2ecf20Sopenharmony_ci				 USB_CTRL_GET_TIMEOUT);
6868c2ecf20Sopenharmony_ci	if (result == bufsize) {
6878c2ecf20Sopenharmony_ci		memcpy(buf, dmabuf, bufsize);
6888c2ecf20Sopenharmony_ci		result = 0;
6898c2ecf20Sopenharmony_ci	} else {
6908c2ecf20Sopenharmony_ci		dev_err(&serial->interface->dev,
6918c2ecf20Sopenharmony_ci			"failed to get vendor val 0x%04x size %d: %d\n", val,
6928c2ecf20Sopenharmony_ci			bufsize, result);
6938c2ecf20Sopenharmony_ci		if (result >= 0)
6948c2ecf20Sopenharmony_ci			result = -EIO;
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	kfree(dmabuf);
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	return result;
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci/*
7038c2ecf20Sopenharmony_ci * Writes any 16-bit CP210X_ register (req) whose value is passed
7048c2ecf20Sopenharmony_ci * entirely in the wValue field of the USB request.
7058c2ecf20Sopenharmony_ci */
7068c2ecf20Sopenharmony_cistatic int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
7098c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
7108c2ecf20Sopenharmony_ci	int result;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
7138c2ecf20Sopenharmony_ci			req, REQTYPE_HOST_TO_INTERFACE, val,
7148c2ecf20Sopenharmony_ci			port_priv->bInterfaceNumber, NULL, 0,
7158c2ecf20Sopenharmony_ci			USB_CTRL_SET_TIMEOUT);
7168c2ecf20Sopenharmony_ci	if (result < 0) {
7178c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed set request 0x%x status: %d\n",
7188c2ecf20Sopenharmony_ci				req, result);
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return result;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci/*
7258c2ecf20Sopenharmony_ci * Writes a variable-sized block of CP210X_ registers, identified by req.
7268c2ecf20Sopenharmony_ci * Data in buf must be in native USB byte order.
7278c2ecf20Sopenharmony_ci */
7288c2ecf20Sopenharmony_cistatic int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
7298c2ecf20Sopenharmony_ci		void *buf, int bufsize)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
7328c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
7338c2ecf20Sopenharmony_ci	void *dmabuf;
7348c2ecf20Sopenharmony_ci	int result;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
7378c2ecf20Sopenharmony_ci	if (!dmabuf)
7388c2ecf20Sopenharmony_ci		return -ENOMEM;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
7418c2ecf20Sopenharmony_ci			req, REQTYPE_HOST_TO_INTERFACE, 0,
7428c2ecf20Sopenharmony_ci			port_priv->bInterfaceNumber, dmabuf, bufsize,
7438c2ecf20Sopenharmony_ci			USB_CTRL_SET_TIMEOUT);
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	kfree(dmabuf);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	if (result == bufsize) {
7488c2ecf20Sopenharmony_ci		result = 0;
7498c2ecf20Sopenharmony_ci	} else {
7508c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
7518c2ecf20Sopenharmony_ci				req, bufsize, result);
7528c2ecf20Sopenharmony_ci		if (result >= 0)
7538c2ecf20Sopenharmony_ci			result = -EIO;
7548c2ecf20Sopenharmony_ci	}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	return result;
7578c2ecf20Sopenharmony_ci}
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci/*
7608c2ecf20Sopenharmony_ci * Writes any 32-bit CP210X_ register identified by req.
7618c2ecf20Sopenharmony_ci */
7628c2ecf20Sopenharmony_cistatic int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	__le32 le32_val;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	le32_val = cpu_to_le32(val);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
7728c2ecf20Sopenharmony_ci/*
7738c2ecf20Sopenharmony_ci * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
7748c2ecf20Sopenharmony_ci * Data in buf must be in native USB byte order.
7758c2ecf20Sopenharmony_ci */
7768c2ecf20Sopenharmony_cistatic int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
7778c2ecf20Sopenharmony_ci				     u16 val, void *buf, int bufsize)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	void *dmabuf;
7808c2ecf20Sopenharmony_ci	int result;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
7838c2ecf20Sopenharmony_ci	if (!dmabuf)
7848c2ecf20Sopenharmony_ci		return -ENOMEM;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
7878c2ecf20Sopenharmony_ci				 CP210X_VENDOR_SPECIFIC, type, val,
7888c2ecf20Sopenharmony_ci				 cp210x_interface_num(serial), dmabuf, bufsize,
7898c2ecf20Sopenharmony_ci				 USB_CTRL_SET_TIMEOUT);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	kfree(dmabuf);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	if (result == bufsize) {
7948c2ecf20Sopenharmony_ci		result = 0;
7958c2ecf20Sopenharmony_ci	} else {
7968c2ecf20Sopenharmony_ci		dev_err(&serial->interface->dev,
7978c2ecf20Sopenharmony_ci			"failed to set vendor val 0x%04x size %d: %d\n", val,
7988c2ecf20Sopenharmony_ci			bufsize, result);
7998c2ecf20Sopenharmony_ci		if (result >= 0)
8008c2ecf20Sopenharmony_ci			result = -EIO;
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	return result;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci#endif
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci/*
8088c2ecf20Sopenharmony_ci * Detect CP2108 GET_LINE_CTL bug and activate workaround.
8098c2ecf20Sopenharmony_ci * Write a known good value 0x800, read it back.
8108c2ecf20Sopenharmony_ci * If it comes back swapped the bug is detected.
8118c2ecf20Sopenharmony_ci * Preserve the original register value.
8128c2ecf20Sopenharmony_ci */
8138c2ecf20Sopenharmony_cistatic int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
8168c2ecf20Sopenharmony_ci	u16 line_ctl_save;
8178c2ecf20Sopenharmony_ci	u16 line_ctl_test;
8188c2ecf20Sopenharmony_ci	int err;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
8218c2ecf20Sopenharmony_ci	if (err)
8228c2ecf20Sopenharmony_ci		return err;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
8258c2ecf20Sopenharmony_ci	if (err)
8268c2ecf20Sopenharmony_ci		return err;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
8298c2ecf20Sopenharmony_ci	if (err)
8308c2ecf20Sopenharmony_ci		return err;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	if (line_ctl_test == 8) {
8338c2ecf20Sopenharmony_ci		port_priv->has_swapped_line_ctl = true;
8348c2ecf20Sopenharmony_ci		line_ctl_save = swab16(line_ctl_save);
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
8388c2ecf20Sopenharmony_ci}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci/*
8418c2ecf20Sopenharmony_ci * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
8428c2ecf20Sopenharmony_ci * to workaround cp2108 bug and get correct value.
8438c2ecf20Sopenharmony_ci */
8448c2ecf20Sopenharmony_cistatic int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
8458c2ecf20Sopenharmony_ci{
8468c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
8478c2ecf20Sopenharmony_ci	int err;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
8508c2ecf20Sopenharmony_ci	if (err)
8518c2ecf20Sopenharmony_ci		return err;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	/* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
8548c2ecf20Sopenharmony_ci	if (port_priv->has_swapped_line_ctl)
8558c2ecf20Sopenharmony_ci		*ctl = swab16(*ctl);
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	return 0;
8588c2ecf20Sopenharmony_ci}
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_cistatic int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
8638c2ecf20Sopenharmony_ci	int result;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
8668c2ecf20Sopenharmony_ci	if (result) {
8678c2ecf20Sopenharmony_ci		dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
8688c2ecf20Sopenharmony_ci		return result;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	/* Configure the termios structure */
8728c2ecf20Sopenharmony_ci	cp210x_get_termios(tty, port);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	if (tty) {
8758c2ecf20Sopenharmony_ci		/* The baud rate must be initialised on cp2104 */
8768c2ecf20Sopenharmony_ci		cp210x_change_speed(tty, port, NULL);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci		if (I_INPCK(tty))
8798c2ecf20Sopenharmony_ci			cp210x_enable_event_mode(port);
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	result = usb_serial_generic_open(tty, port);
8838c2ecf20Sopenharmony_ci	if (result)
8848c2ecf20Sopenharmony_ci		goto err_disable;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	return 0;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cierr_disable:
8898c2ecf20Sopenharmony_ci	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
8908c2ecf20Sopenharmony_ci	port_priv->event_mode = false;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	return result;
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistatic void cp210x_close(struct usb_serial_port *port)
8968c2ecf20Sopenharmony_ci{
8978c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	usb_serial_generic_close(port);
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	/* Clear both queues; cp2108 needs this to avoid an occasional hang */
9028c2ecf20Sopenharmony_ci	cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	/* Disabling the interface disables event-insertion mode. */
9078c2ecf20Sopenharmony_ci	port_priv->event_mode = false;
9088c2ecf20Sopenharmony_ci}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_cistatic void cp210x_process_lsr(struct usb_serial_port *port, unsigned char lsr, char *flag)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	if (lsr & CP210X_LSR_BREAK) {
9138c2ecf20Sopenharmony_ci		port->icount.brk++;
9148c2ecf20Sopenharmony_ci		*flag = TTY_BREAK;
9158c2ecf20Sopenharmony_ci	} else if (lsr & CP210X_LSR_PARITY) {
9168c2ecf20Sopenharmony_ci		port->icount.parity++;
9178c2ecf20Sopenharmony_ci		*flag = TTY_PARITY;
9188c2ecf20Sopenharmony_ci	} else if (lsr & CP210X_LSR_FRAME) {
9198c2ecf20Sopenharmony_ci		port->icount.frame++;
9208c2ecf20Sopenharmony_ci		*flag = TTY_FRAME;
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (lsr & CP210X_LSR_OVERRUN) {
9248c2ecf20Sopenharmony_ci		port->icount.overrun++;
9258c2ecf20Sopenharmony_ci		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_cistatic bool cp210x_process_char(struct usb_serial_port *port, unsigned char *ch, char *flag)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	switch (port_priv->event_state) {
9348c2ecf20Sopenharmony_ci	case ES_DATA:
9358c2ecf20Sopenharmony_ci		if (*ch == CP210X_ESCCHAR) {
9368c2ecf20Sopenharmony_ci			port_priv->event_state = ES_ESCAPE;
9378c2ecf20Sopenharmony_ci			break;
9388c2ecf20Sopenharmony_ci		}
9398c2ecf20Sopenharmony_ci		return false;
9408c2ecf20Sopenharmony_ci	case ES_ESCAPE:
9418c2ecf20Sopenharmony_ci		switch (*ch) {
9428c2ecf20Sopenharmony_ci		case 0:
9438c2ecf20Sopenharmony_ci			dev_dbg(&port->dev, "%s - escape char\n", __func__);
9448c2ecf20Sopenharmony_ci			*ch = CP210X_ESCCHAR;
9458c2ecf20Sopenharmony_ci			port_priv->event_state = ES_DATA;
9468c2ecf20Sopenharmony_ci			return false;
9478c2ecf20Sopenharmony_ci		case 1:
9488c2ecf20Sopenharmony_ci			port_priv->event_state = ES_LSR_DATA_0;
9498c2ecf20Sopenharmony_ci			break;
9508c2ecf20Sopenharmony_ci		case 2:
9518c2ecf20Sopenharmony_ci			port_priv->event_state = ES_LSR;
9528c2ecf20Sopenharmony_ci			break;
9538c2ecf20Sopenharmony_ci		case 3:
9548c2ecf20Sopenharmony_ci			port_priv->event_state = ES_MSR;
9558c2ecf20Sopenharmony_ci			break;
9568c2ecf20Sopenharmony_ci		default:
9578c2ecf20Sopenharmony_ci			dev_err(&port->dev, "malformed event 0x%02x\n", *ch);
9588c2ecf20Sopenharmony_ci			port_priv->event_state = ES_DATA;
9598c2ecf20Sopenharmony_ci			break;
9608c2ecf20Sopenharmony_ci		}
9618c2ecf20Sopenharmony_ci		break;
9628c2ecf20Sopenharmony_ci	case ES_LSR_DATA_0:
9638c2ecf20Sopenharmony_ci		port_priv->lsr = *ch;
9648c2ecf20Sopenharmony_ci		port_priv->event_state = ES_LSR_DATA_1;
9658c2ecf20Sopenharmony_ci		break;
9668c2ecf20Sopenharmony_ci	case ES_LSR_DATA_1:
9678c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - lsr = 0x%02x, data = 0x%02x\n",
9688c2ecf20Sopenharmony_ci				__func__, port_priv->lsr, *ch);
9698c2ecf20Sopenharmony_ci		cp210x_process_lsr(port, port_priv->lsr, flag);
9708c2ecf20Sopenharmony_ci		port_priv->event_state = ES_DATA;
9718c2ecf20Sopenharmony_ci		return false;
9728c2ecf20Sopenharmony_ci	case ES_LSR:
9738c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - lsr = 0x%02x\n", __func__, *ch);
9748c2ecf20Sopenharmony_ci		port_priv->lsr = *ch;
9758c2ecf20Sopenharmony_ci		cp210x_process_lsr(port, port_priv->lsr, flag);
9768c2ecf20Sopenharmony_ci		port_priv->event_state = ES_DATA;
9778c2ecf20Sopenharmony_ci		break;
9788c2ecf20Sopenharmony_ci	case ES_MSR:
9798c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s - msr = 0x%02x\n", __func__, *ch);
9808c2ecf20Sopenharmony_ci		/* unimplemented */
9818c2ecf20Sopenharmony_ci		port_priv->event_state = ES_DATA;
9828c2ecf20Sopenharmony_ci		break;
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	return true;
9868c2ecf20Sopenharmony_ci}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_cistatic void cp210x_process_read_urb(struct urb *urb)
9898c2ecf20Sopenharmony_ci{
9908c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
9918c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
9928c2ecf20Sopenharmony_ci	unsigned char *ch = urb->transfer_buffer;
9938c2ecf20Sopenharmony_ci	char flag;
9948c2ecf20Sopenharmony_ci	int i;
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	if (!urb->actual_length)
9978c2ecf20Sopenharmony_ci		return;
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	if (port_priv->event_mode) {
10008c2ecf20Sopenharmony_ci		for (i = 0; i < urb->actual_length; i++, ch++) {
10018c2ecf20Sopenharmony_ci			flag = TTY_NORMAL;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci			if (cp210x_process_char(port, ch, &flag))
10048c2ecf20Sopenharmony_ci				continue;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci			tty_insert_flip_char(&port->port, *ch, flag);
10078c2ecf20Sopenharmony_ci		}
10088c2ecf20Sopenharmony_ci	} else {
10098c2ecf20Sopenharmony_ci		tty_insert_flip_string(&port->port, ch, urb->actual_length);
10108c2ecf20Sopenharmony_ci	}
10118c2ecf20Sopenharmony_ci	tty_flip_buffer_push(&port->port);
10128c2ecf20Sopenharmony_ci}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci/*
10158c2ecf20Sopenharmony_ci * Read how many bytes are waiting in the TX queue.
10168c2ecf20Sopenharmony_ci */
10178c2ecf20Sopenharmony_cistatic int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
10188c2ecf20Sopenharmony_ci		u32 *count)
10198c2ecf20Sopenharmony_ci{
10208c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
10218c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
10228c2ecf20Sopenharmony_ci	struct cp210x_comm_status *sts;
10238c2ecf20Sopenharmony_ci	int result;
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	sts = kmalloc(sizeof(*sts), GFP_KERNEL);
10268c2ecf20Sopenharmony_ci	if (!sts)
10278c2ecf20Sopenharmony_ci		return -ENOMEM;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
10308c2ecf20Sopenharmony_ci			CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
10318c2ecf20Sopenharmony_ci			0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
10328c2ecf20Sopenharmony_ci			USB_CTRL_GET_TIMEOUT);
10338c2ecf20Sopenharmony_ci	if (result == sizeof(*sts)) {
10348c2ecf20Sopenharmony_ci		*count = le32_to_cpu(sts->ulAmountInOutQueue);
10358c2ecf20Sopenharmony_ci		result = 0;
10368c2ecf20Sopenharmony_ci	} else {
10378c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed to get comm status: %d\n", result);
10388c2ecf20Sopenharmony_ci		if (result >= 0)
10398c2ecf20Sopenharmony_ci			result = -EIO;
10408c2ecf20Sopenharmony_ci	}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	kfree(sts);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	return result;
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_cistatic bool cp210x_tx_empty(struct usb_serial_port *port)
10488c2ecf20Sopenharmony_ci{
10498c2ecf20Sopenharmony_ci	int err;
10508c2ecf20Sopenharmony_ci	u32 count;
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	err = cp210x_get_tx_queue_byte_count(port, &count);
10538c2ecf20Sopenharmony_ci	if (err)
10548c2ecf20Sopenharmony_ci		return true;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	return !count;
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci/*
10608c2ecf20Sopenharmony_ci * cp210x_get_termios
10618c2ecf20Sopenharmony_ci * Reads the baud rate, data bits, parity, stop bits and flow control mode
10628c2ecf20Sopenharmony_ci * from the device, corrects any unsupported values, and configures the
10638c2ecf20Sopenharmony_ci * termios structure to reflect the state of the device
10648c2ecf20Sopenharmony_ci */
10658c2ecf20Sopenharmony_cistatic void cp210x_get_termios(struct tty_struct *tty,
10668c2ecf20Sopenharmony_ci	struct usb_serial_port *port)
10678c2ecf20Sopenharmony_ci{
10688c2ecf20Sopenharmony_ci	unsigned int baud;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	if (tty) {
10718c2ecf20Sopenharmony_ci		cp210x_get_termios_port(tty->driver_data,
10728c2ecf20Sopenharmony_ci			&tty->termios.c_cflag, &baud);
10738c2ecf20Sopenharmony_ci		tty_encode_baud_rate(tty, baud, baud);
10748c2ecf20Sopenharmony_ci	} else {
10758c2ecf20Sopenharmony_ci		tcflag_t cflag;
10768c2ecf20Sopenharmony_ci		cflag = 0;
10778c2ecf20Sopenharmony_ci		cp210x_get_termios_port(port, &cflag, &baud);
10788c2ecf20Sopenharmony_ci	}
10798c2ecf20Sopenharmony_ci}
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci/*
10828c2ecf20Sopenharmony_ci * cp210x_get_termios_port
10838c2ecf20Sopenharmony_ci * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
10848c2ecf20Sopenharmony_ci */
10858c2ecf20Sopenharmony_cistatic void cp210x_get_termios_port(struct usb_serial_port *port,
10868c2ecf20Sopenharmony_ci	tcflag_t *cflagp, unsigned int *baudp)
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci	struct device *dev = &port->dev;
10898c2ecf20Sopenharmony_ci	tcflag_t cflag;
10908c2ecf20Sopenharmony_ci	struct cp210x_flow_ctl flow_ctl;
10918c2ecf20Sopenharmony_ci	u32 baud;
10928c2ecf20Sopenharmony_ci	u16 bits;
10938c2ecf20Sopenharmony_ci	u32 ctl_hs;
10948c2ecf20Sopenharmony_ci	u32 flow_repl;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
10998c2ecf20Sopenharmony_ci	*baudp = baud;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	cflag = *cflagp;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	cp210x_get_line_ctl(port, &bits);
11048c2ecf20Sopenharmony_ci	cflag &= ~CSIZE;
11058c2ecf20Sopenharmony_ci	switch (bits & BITS_DATA_MASK) {
11068c2ecf20Sopenharmony_ci	case BITS_DATA_5:
11078c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - data bits = 5\n", __func__);
11088c2ecf20Sopenharmony_ci		cflag |= CS5;
11098c2ecf20Sopenharmony_ci		break;
11108c2ecf20Sopenharmony_ci	case BITS_DATA_6:
11118c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - data bits = 6\n", __func__);
11128c2ecf20Sopenharmony_ci		cflag |= CS6;
11138c2ecf20Sopenharmony_ci		break;
11148c2ecf20Sopenharmony_ci	case BITS_DATA_7:
11158c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - data bits = 7\n", __func__);
11168c2ecf20Sopenharmony_ci		cflag |= CS7;
11178c2ecf20Sopenharmony_ci		break;
11188c2ecf20Sopenharmony_ci	case BITS_DATA_8:
11198c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - data bits = 8\n", __func__);
11208c2ecf20Sopenharmony_ci		cflag |= CS8;
11218c2ecf20Sopenharmony_ci		break;
11228c2ecf20Sopenharmony_ci	case BITS_DATA_9:
11238c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
11248c2ecf20Sopenharmony_ci		cflag |= CS8;
11258c2ecf20Sopenharmony_ci		bits &= ~BITS_DATA_MASK;
11268c2ecf20Sopenharmony_ci		bits |= BITS_DATA_8;
11278c2ecf20Sopenharmony_ci		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
11288c2ecf20Sopenharmony_ci		break;
11298c2ecf20Sopenharmony_ci	default:
11308c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
11318c2ecf20Sopenharmony_ci		cflag |= CS8;
11328c2ecf20Sopenharmony_ci		bits &= ~BITS_DATA_MASK;
11338c2ecf20Sopenharmony_ci		bits |= BITS_DATA_8;
11348c2ecf20Sopenharmony_ci		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
11358c2ecf20Sopenharmony_ci		break;
11368c2ecf20Sopenharmony_ci	}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	switch (bits & BITS_PARITY_MASK) {
11398c2ecf20Sopenharmony_ci	case BITS_PARITY_NONE:
11408c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - parity = NONE\n", __func__);
11418c2ecf20Sopenharmony_ci		cflag &= ~PARENB;
11428c2ecf20Sopenharmony_ci		break;
11438c2ecf20Sopenharmony_ci	case BITS_PARITY_ODD:
11448c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - parity = ODD\n", __func__);
11458c2ecf20Sopenharmony_ci		cflag |= (PARENB|PARODD);
11468c2ecf20Sopenharmony_ci		break;
11478c2ecf20Sopenharmony_ci	case BITS_PARITY_EVEN:
11488c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - parity = EVEN\n", __func__);
11498c2ecf20Sopenharmony_ci		cflag &= ~PARODD;
11508c2ecf20Sopenharmony_ci		cflag |= PARENB;
11518c2ecf20Sopenharmony_ci		break;
11528c2ecf20Sopenharmony_ci	case BITS_PARITY_MARK:
11538c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - parity = MARK\n", __func__);
11548c2ecf20Sopenharmony_ci		cflag |= (PARENB|PARODD|CMSPAR);
11558c2ecf20Sopenharmony_ci		break;
11568c2ecf20Sopenharmony_ci	case BITS_PARITY_SPACE:
11578c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - parity = SPACE\n", __func__);
11588c2ecf20Sopenharmony_ci		cflag &= ~PARODD;
11598c2ecf20Sopenharmony_ci		cflag |= (PARENB|CMSPAR);
11608c2ecf20Sopenharmony_ci		break;
11618c2ecf20Sopenharmony_ci	default:
11628c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
11638c2ecf20Sopenharmony_ci		cflag &= ~PARENB;
11648c2ecf20Sopenharmony_ci		bits &= ~BITS_PARITY_MASK;
11658c2ecf20Sopenharmony_ci		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
11668c2ecf20Sopenharmony_ci		break;
11678c2ecf20Sopenharmony_ci	}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	cflag &= ~CSTOPB;
11708c2ecf20Sopenharmony_ci	switch (bits & BITS_STOP_MASK) {
11718c2ecf20Sopenharmony_ci	case BITS_STOP_1:
11728c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - stop bits = 1\n", __func__);
11738c2ecf20Sopenharmony_ci		break;
11748c2ecf20Sopenharmony_ci	case BITS_STOP_1_5:
11758c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
11768c2ecf20Sopenharmony_ci		bits &= ~BITS_STOP_MASK;
11778c2ecf20Sopenharmony_ci		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
11788c2ecf20Sopenharmony_ci		break;
11798c2ecf20Sopenharmony_ci	case BITS_STOP_2:
11808c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - stop bits = 2\n", __func__);
11818c2ecf20Sopenharmony_ci		cflag |= CSTOPB;
11828c2ecf20Sopenharmony_ci		break;
11838c2ecf20Sopenharmony_ci	default:
11848c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
11858c2ecf20Sopenharmony_ci		bits &= ~BITS_STOP_MASK;
11868c2ecf20Sopenharmony_ci		cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
11878c2ecf20Sopenharmony_ci		break;
11888c2ecf20Sopenharmony_ci	}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
11918c2ecf20Sopenharmony_ci			sizeof(flow_ctl));
11928c2ecf20Sopenharmony_ci	ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
11938c2ecf20Sopenharmony_ci	if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
11948c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
11958c2ecf20Sopenharmony_ci		/*
11968c2ecf20Sopenharmony_ci		 * When the port is closed, the CP210x hardware disables
11978c2ecf20Sopenharmony_ci		 * auto-RTS and RTS is deasserted but it leaves auto-CTS when
11988c2ecf20Sopenharmony_ci		 * in hardware flow control mode. When re-opening the port, if
11998c2ecf20Sopenharmony_ci		 * auto-CTS is enabled on the cp210x, then auto-RTS must be
12008c2ecf20Sopenharmony_ci		 * re-enabled in the driver.
12018c2ecf20Sopenharmony_ci		 */
12028c2ecf20Sopenharmony_ci		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
12038c2ecf20Sopenharmony_ci		flow_repl &= ~CP210X_SERIAL_RTS_MASK;
12048c2ecf20Sopenharmony_ci		flow_repl |= CP210X_SERIAL_RTS_SHIFT(CP210X_SERIAL_RTS_FLOW_CTL);
12058c2ecf20Sopenharmony_ci		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
12068c2ecf20Sopenharmony_ci		cp210x_write_reg_block(port,
12078c2ecf20Sopenharmony_ci				CP210X_SET_FLOW,
12088c2ecf20Sopenharmony_ci				&flow_ctl,
12098c2ecf20Sopenharmony_ci				sizeof(flow_ctl));
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci		cflag |= CRTSCTS;
12128c2ecf20Sopenharmony_ci	} else {
12138c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - flow control = NONE\n", __func__);
12148c2ecf20Sopenharmony_ci		cflag &= ~CRTSCTS;
12158c2ecf20Sopenharmony_ci	}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	*cflagp = cflag;
12188c2ecf20Sopenharmony_ci}
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_cistruct cp210x_rate {
12218c2ecf20Sopenharmony_ci	speed_t rate;
12228c2ecf20Sopenharmony_ci	speed_t high;
12238c2ecf20Sopenharmony_ci};
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_cistatic const struct cp210x_rate cp210x_an205_table1[] = {
12268c2ecf20Sopenharmony_ci	{ 300, 300 },
12278c2ecf20Sopenharmony_ci	{ 600, 600 },
12288c2ecf20Sopenharmony_ci	{ 1200, 1200 },
12298c2ecf20Sopenharmony_ci	{ 1800, 1800 },
12308c2ecf20Sopenharmony_ci	{ 2400, 2400 },
12318c2ecf20Sopenharmony_ci	{ 4000, 4000 },
12328c2ecf20Sopenharmony_ci	{ 4800, 4803 },
12338c2ecf20Sopenharmony_ci	{ 7200, 7207 },
12348c2ecf20Sopenharmony_ci	{ 9600, 9612 },
12358c2ecf20Sopenharmony_ci	{ 14400, 14428 },
12368c2ecf20Sopenharmony_ci	{ 16000, 16062 },
12378c2ecf20Sopenharmony_ci	{ 19200, 19250 },
12388c2ecf20Sopenharmony_ci	{ 28800, 28912 },
12398c2ecf20Sopenharmony_ci	{ 38400, 38601 },
12408c2ecf20Sopenharmony_ci	{ 51200, 51558 },
12418c2ecf20Sopenharmony_ci	{ 56000, 56280 },
12428c2ecf20Sopenharmony_ci	{ 57600, 58053 },
12438c2ecf20Sopenharmony_ci	{ 64000, 64111 },
12448c2ecf20Sopenharmony_ci	{ 76800, 77608 },
12458c2ecf20Sopenharmony_ci	{ 115200, 117028 },
12468c2ecf20Sopenharmony_ci	{ 128000, 129347 },
12478c2ecf20Sopenharmony_ci	{ 153600, 156868 },
12488c2ecf20Sopenharmony_ci	{ 230400, 237832 },
12498c2ecf20Sopenharmony_ci	{ 250000, 254234 },
12508c2ecf20Sopenharmony_ci	{ 256000, 273066 },
12518c2ecf20Sopenharmony_ci	{ 460800, 491520 },
12528c2ecf20Sopenharmony_ci	{ 500000, 567138 },
12538c2ecf20Sopenharmony_ci	{ 576000, 670254 },
12548c2ecf20Sopenharmony_ci	{ 921600, UINT_MAX }
12558c2ecf20Sopenharmony_ci};
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci/*
12588c2ecf20Sopenharmony_ci * Quantises the baud rate as per AN205 Table 1
12598c2ecf20Sopenharmony_ci */
12608c2ecf20Sopenharmony_cistatic speed_t cp210x_get_an205_rate(speed_t baud)
12618c2ecf20Sopenharmony_ci{
12628c2ecf20Sopenharmony_ci	int i;
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(cp210x_an205_table1); ++i) {
12658c2ecf20Sopenharmony_ci		if (baud <= cp210x_an205_table1[i].high)
12668c2ecf20Sopenharmony_ci			break;
12678c2ecf20Sopenharmony_ci	}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	return cp210x_an205_table1[i].rate;
12708c2ecf20Sopenharmony_ci}
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_cistatic speed_t cp210x_get_actual_rate(speed_t baud)
12738c2ecf20Sopenharmony_ci{
12748c2ecf20Sopenharmony_ci	unsigned int prescale = 1;
12758c2ecf20Sopenharmony_ci	unsigned int div;
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	if (baud <= 365)
12788c2ecf20Sopenharmony_ci		prescale = 4;
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	div = DIV_ROUND_CLOSEST(48000000, 2 * prescale * baud);
12818c2ecf20Sopenharmony_ci	baud = 48000000 / (2 * prescale * div);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	return baud;
12848c2ecf20Sopenharmony_ci}
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci/*
12878c2ecf20Sopenharmony_ci * CP2101 supports the following baud rates:
12888c2ecf20Sopenharmony_ci *
12898c2ecf20Sopenharmony_ci *	300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
12908c2ecf20Sopenharmony_ci *	38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
12918c2ecf20Sopenharmony_ci *
12928c2ecf20Sopenharmony_ci * CP2102 and CP2103 support the following additional rates:
12938c2ecf20Sopenharmony_ci *
12948c2ecf20Sopenharmony_ci *	4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
12958c2ecf20Sopenharmony_ci *	576000
12968c2ecf20Sopenharmony_ci *
12978c2ecf20Sopenharmony_ci * The device will map a requested rate to a supported one, but the result
12988c2ecf20Sopenharmony_ci * of requests for rates greater than 1053257 is undefined (see AN205).
12998c2ecf20Sopenharmony_ci *
13008c2ecf20Sopenharmony_ci * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
13018c2ecf20Sopenharmony_ci * respectively, with an error less than 1%. The actual rates are determined
13028c2ecf20Sopenharmony_ci * by
13038c2ecf20Sopenharmony_ci *
13048c2ecf20Sopenharmony_ci *	div = round(freq / (2 x prescale x request))
13058c2ecf20Sopenharmony_ci *	actual = freq / (2 x prescale x div)
13068c2ecf20Sopenharmony_ci *
13078c2ecf20Sopenharmony_ci * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
13088c2ecf20Sopenharmony_ci * or 1 otherwise.
13098c2ecf20Sopenharmony_ci * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
13108c2ecf20Sopenharmony_ci * otherwise.
13118c2ecf20Sopenharmony_ci */
13128c2ecf20Sopenharmony_cistatic void cp210x_change_speed(struct tty_struct *tty,
13138c2ecf20Sopenharmony_ci		struct usb_serial_port *port, struct ktermios *old_termios)
13148c2ecf20Sopenharmony_ci{
13158c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
13168c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
13178c2ecf20Sopenharmony_ci	u32 baud;
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	/*
13208c2ecf20Sopenharmony_ci	 * This maps the requested rate to the actual rate, a valid rate on
13218c2ecf20Sopenharmony_ci	 * cp2102 or cp2103, or to an arbitrary rate in [1M, max_speed].
13228c2ecf20Sopenharmony_ci	 *
13238c2ecf20Sopenharmony_ci	 * NOTE: B0 is not implemented.
13248c2ecf20Sopenharmony_ci	 */
13258c2ecf20Sopenharmony_ci	baud = clamp(tty->termios.c_ospeed, priv->min_speed, priv->max_speed);
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	if (priv->use_actual_rate)
13288c2ecf20Sopenharmony_ci		baud = cp210x_get_actual_rate(baud);
13298c2ecf20Sopenharmony_ci	else if (baud < 1000000)
13308c2ecf20Sopenharmony_ci		baud = cp210x_get_an205_rate(baud);
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
13338c2ecf20Sopenharmony_ci	if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
13348c2ecf20Sopenharmony_ci		dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
13358c2ecf20Sopenharmony_ci		if (old_termios)
13368c2ecf20Sopenharmony_ci			baud = old_termios->c_ospeed;
13378c2ecf20Sopenharmony_ci		else
13388c2ecf20Sopenharmony_ci			baud = 9600;
13398c2ecf20Sopenharmony_ci	}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	tty_encode_baud_rate(tty, baud, baud);
13428c2ecf20Sopenharmony_ci}
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_cistatic void cp210x_enable_event_mode(struct usb_serial_port *port)
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(port->serial);
13478c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
13488c2ecf20Sopenharmony_ci	int ret;
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	if (port_priv->event_mode)
13518c2ecf20Sopenharmony_ci		return;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	if (priv->no_event_mode)
13548c2ecf20Sopenharmony_ci		return;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	port_priv->event_state = ES_DATA;
13578c2ecf20Sopenharmony_ci	port_priv->event_mode = true;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, CP210X_ESCCHAR);
13608c2ecf20Sopenharmony_ci	if (ret) {
13618c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed to enable events: %d\n", ret);
13628c2ecf20Sopenharmony_ci		port_priv->event_mode = false;
13638c2ecf20Sopenharmony_ci	}
13648c2ecf20Sopenharmony_ci}
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_cistatic void cp210x_disable_event_mode(struct usb_serial_port *port)
13678c2ecf20Sopenharmony_ci{
13688c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
13698c2ecf20Sopenharmony_ci	int ret;
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	if (!port_priv->event_mode)
13728c2ecf20Sopenharmony_ci		return;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	ret = cp210x_write_u16_reg(port, CP210X_EMBED_EVENTS, 0);
13758c2ecf20Sopenharmony_ci	if (ret) {
13768c2ecf20Sopenharmony_ci		dev_err(&port->dev, "failed to disable events: %d\n", ret);
13778c2ecf20Sopenharmony_ci		return;
13788c2ecf20Sopenharmony_ci	}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	port_priv->event_mode = false;
13818c2ecf20Sopenharmony_ci}
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_cistatic void cp210x_set_termios(struct tty_struct *tty,
13848c2ecf20Sopenharmony_ci		struct usb_serial_port *port, struct ktermios *old_termios)
13858c2ecf20Sopenharmony_ci{
13868c2ecf20Sopenharmony_ci	struct device *dev = &port->dev;
13878c2ecf20Sopenharmony_ci	unsigned int cflag, old_cflag;
13888c2ecf20Sopenharmony_ci	u16 bits;
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	cflag = tty->termios.c_cflag;
13918c2ecf20Sopenharmony_ci	old_cflag = old_termios->c_cflag;
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	if (tty->termios.c_ospeed != old_termios->c_ospeed)
13948c2ecf20Sopenharmony_ci		cp210x_change_speed(tty, port, old_termios);
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	/* If the number of data bits is to be updated */
13978c2ecf20Sopenharmony_ci	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
13988c2ecf20Sopenharmony_ci		cp210x_get_line_ctl(port, &bits);
13998c2ecf20Sopenharmony_ci		bits &= ~BITS_DATA_MASK;
14008c2ecf20Sopenharmony_ci		switch (cflag & CSIZE) {
14018c2ecf20Sopenharmony_ci		case CS5:
14028c2ecf20Sopenharmony_ci			bits |= BITS_DATA_5;
14038c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - data bits = 5\n", __func__);
14048c2ecf20Sopenharmony_ci			break;
14058c2ecf20Sopenharmony_ci		case CS6:
14068c2ecf20Sopenharmony_ci			bits |= BITS_DATA_6;
14078c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - data bits = 6\n", __func__);
14088c2ecf20Sopenharmony_ci			break;
14098c2ecf20Sopenharmony_ci		case CS7:
14108c2ecf20Sopenharmony_ci			bits |= BITS_DATA_7;
14118c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - data bits = 7\n", __func__);
14128c2ecf20Sopenharmony_ci			break;
14138c2ecf20Sopenharmony_ci		case CS8:
14148c2ecf20Sopenharmony_ci		default:
14158c2ecf20Sopenharmony_ci			bits |= BITS_DATA_8;
14168c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - data bits = 8\n", __func__);
14178c2ecf20Sopenharmony_ci			break;
14188c2ecf20Sopenharmony_ci		}
14198c2ecf20Sopenharmony_ci		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
14208c2ecf20Sopenharmony_ci			dev_dbg(dev, "Number of data bits requested not supported by device\n");
14218c2ecf20Sopenharmony_ci	}
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	if ((cflag     & (PARENB|PARODD|CMSPAR)) !=
14248c2ecf20Sopenharmony_ci	    (old_cflag & (PARENB|PARODD|CMSPAR))) {
14258c2ecf20Sopenharmony_ci		cp210x_get_line_ctl(port, &bits);
14268c2ecf20Sopenharmony_ci		bits &= ~BITS_PARITY_MASK;
14278c2ecf20Sopenharmony_ci		if (cflag & PARENB) {
14288c2ecf20Sopenharmony_ci			if (cflag & CMSPAR) {
14298c2ecf20Sopenharmony_ci				if (cflag & PARODD) {
14308c2ecf20Sopenharmony_ci					bits |= BITS_PARITY_MARK;
14318c2ecf20Sopenharmony_ci					dev_dbg(dev, "%s - parity = MARK\n", __func__);
14328c2ecf20Sopenharmony_ci				} else {
14338c2ecf20Sopenharmony_ci					bits |= BITS_PARITY_SPACE;
14348c2ecf20Sopenharmony_ci					dev_dbg(dev, "%s - parity = SPACE\n", __func__);
14358c2ecf20Sopenharmony_ci				}
14368c2ecf20Sopenharmony_ci			} else {
14378c2ecf20Sopenharmony_ci				if (cflag & PARODD) {
14388c2ecf20Sopenharmony_ci					bits |= BITS_PARITY_ODD;
14398c2ecf20Sopenharmony_ci					dev_dbg(dev, "%s - parity = ODD\n", __func__);
14408c2ecf20Sopenharmony_ci				} else {
14418c2ecf20Sopenharmony_ci					bits |= BITS_PARITY_EVEN;
14428c2ecf20Sopenharmony_ci					dev_dbg(dev, "%s - parity = EVEN\n", __func__);
14438c2ecf20Sopenharmony_ci				}
14448c2ecf20Sopenharmony_ci			}
14458c2ecf20Sopenharmony_ci		}
14468c2ecf20Sopenharmony_ci		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
14478c2ecf20Sopenharmony_ci			dev_dbg(dev, "Parity mode not supported by device\n");
14488c2ecf20Sopenharmony_ci	}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
14518c2ecf20Sopenharmony_ci		cp210x_get_line_ctl(port, &bits);
14528c2ecf20Sopenharmony_ci		bits &= ~BITS_STOP_MASK;
14538c2ecf20Sopenharmony_ci		if (cflag & CSTOPB) {
14548c2ecf20Sopenharmony_ci			bits |= BITS_STOP_2;
14558c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - stop bits = 2\n", __func__);
14568c2ecf20Sopenharmony_ci		} else {
14578c2ecf20Sopenharmony_ci			bits |= BITS_STOP_1;
14588c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - stop bits = 1\n", __func__);
14598c2ecf20Sopenharmony_ci		}
14608c2ecf20Sopenharmony_ci		if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
14618c2ecf20Sopenharmony_ci			dev_dbg(dev, "Number of stop bits requested not supported by device\n");
14628c2ecf20Sopenharmony_ci	}
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
14658c2ecf20Sopenharmony_ci		struct cp210x_flow_ctl flow_ctl;
14668c2ecf20Sopenharmony_ci		u32 ctl_hs;
14678c2ecf20Sopenharmony_ci		u32 flow_repl;
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci		cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
14708c2ecf20Sopenharmony_ci				sizeof(flow_ctl));
14718c2ecf20Sopenharmony_ci		ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
14728c2ecf20Sopenharmony_ci		flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
14738c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14748c2ecf20Sopenharmony_ci				__func__, ctl_hs, flow_repl);
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci		ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
14778c2ecf20Sopenharmony_ci		ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
14788c2ecf20Sopenharmony_ci		ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
14798c2ecf20Sopenharmony_ci		ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
14808c2ecf20Sopenharmony_ci		ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
14818c2ecf20Sopenharmony_ci		if (cflag & CRTSCTS) {
14828c2ecf20Sopenharmony_ci			ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14858c2ecf20Sopenharmony_ci			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14868c2ecf20Sopenharmony_ci					CP210X_SERIAL_RTS_FLOW_CTL);
14878c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
14888c2ecf20Sopenharmony_ci		} else {
14898c2ecf20Sopenharmony_ci			ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci			flow_repl &= ~CP210X_SERIAL_RTS_MASK;
14928c2ecf20Sopenharmony_ci			flow_repl |= CP210X_SERIAL_RTS_SHIFT(
14938c2ecf20Sopenharmony_ci					CP210X_SERIAL_RTS_ACTIVE);
14948c2ecf20Sopenharmony_ci			dev_dbg(dev, "%s - flow control = NONE\n", __func__);
14958c2ecf20Sopenharmony_ci		}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_ci		dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
14988c2ecf20Sopenharmony_ci				__func__, ctl_hs, flow_repl);
14998c2ecf20Sopenharmony_ci		flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
15008c2ecf20Sopenharmony_ci		flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
15018c2ecf20Sopenharmony_ci		cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
15028c2ecf20Sopenharmony_ci				sizeof(flow_ctl));
15038c2ecf20Sopenharmony_ci	}
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	/*
15068c2ecf20Sopenharmony_ci	 * Enable event-insertion mode only if input parity checking is
15078c2ecf20Sopenharmony_ci	 * enabled for now.
15088c2ecf20Sopenharmony_ci	 */
15098c2ecf20Sopenharmony_ci	if (I_INPCK(tty))
15108c2ecf20Sopenharmony_ci		cp210x_enable_event_mode(port);
15118c2ecf20Sopenharmony_ci	else
15128c2ecf20Sopenharmony_ci		cp210x_disable_event_mode(port);
15138c2ecf20Sopenharmony_ci}
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_cistatic int cp210x_tiocmset(struct tty_struct *tty,
15168c2ecf20Sopenharmony_ci		unsigned int set, unsigned int clear)
15178c2ecf20Sopenharmony_ci{
15188c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
15198c2ecf20Sopenharmony_ci	return cp210x_tiocmset_port(port, set, clear);
15208c2ecf20Sopenharmony_ci}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_cistatic int cp210x_tiocmset_port(struct usb_serial_port *port,
15238c2ecf20Sopenharmony_ci		unsigned int set, unsigned int clear)
15248c2ecf20Sopenharmony_ci{
15258c2ecf20Sopenharmony_ci	u16 control = 0;
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci	if (set & TIOCM_RTS) {
15288c2ecf20Sopenharmony_ci		control |= CONTROL_RTS;
15298c2ecf20Sopenharmony_ci		control |= CONTROL_WRITE_RTS;
15308c2ecf20Sopenharmony_ci	}
15318c2ecf20Sopenharmony_ci	if (set & TIOCM_DTR) {
15328c2ecf20Sopenharmony_ci		control |= CONTROL_DTR;
15338c2ecf20Sopenharmony_ci		control |= CONTROL_WRITE_DTR;
15348c2ecf20Sopenharmony_ci	}
15358c2ecf20Sopenharmony_ci	if (clear & TIOCM_RTS) {
15368c2ecf20Sopenharmony_ci		control &= ~CONTROL_RTS;
15378c2ecf20Sopenharmony_ci		control |= CONTROL_WRITE_RTS;
15388c2ecf20Sopenharmony_ci	}
15398c2ecf20Sopenharmony_ci	if (clear & TIOCM_DTR) {
15408c2ecf20Sopenharmony_ci		control &= ~CONTROL_DTR;
15418c2ecf20Sopenharmony_ci		control |= CONTROL_WRITE_DTR;
15428c2ecf20Sopenharmony_ci	}
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
15478c2ecf20Sopenharmony_ci}
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_cistatic void cp210x_dtr_rts(struct usb_serial_port *p, int on)
15508c2ecf20Sopenharmony_ci{
15518c2ecf20Sopenharmony_ci	if (on)
15528c2ecf20Sopenharmony_ci		cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
15538c2ecf20Sopenharmony_ci	else
15548c2ecf20Sopenharmony_ci		cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
15558c2ecf20Sopenharmony_ci}
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_cistatic int cp210x_tiocmget(struct tty_struct *tty)
15588c2ecf20Sopenharmony_ci{
15598c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
15608c2ecf20Sopenharmony_ci	u8 control;
15618c2ecf20Sopenharmony_ci	int result;
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
15648c2ecf20Sopenharmony_ci	if (result)
15658c2ecf20Sopenharmony_ci		return result;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
15688c2ecf20Sopenharmony_ci		|((control & CONTROL_RTS) ? TIOCM_RTS : 0)
15698c2ecf20Sopenharmony_ci		|((control & CONTROL_CTS) ? TIOCM_CTS : 0)
15708c2ecf20Sopenharmony_ci		|((control & CONTROL_DSR) ? TIOCM_DSR : 0)
15718c2ecf20Sopenharmony_ci		|((control & CONTROL_RING)? TIOCM_RI  : 0)
15728c2ecf20Sopenharmony_ci		|((control & CONTROL_DCD) ? TIOCM_CD  : 0);
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci	return result;
15778c2ecf20Sopenharmony_ci}
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_cistatic void cp210x_break_ctl(struct tty_struct *tty, int break_state)
15808c2ecf20Sopenharmony_ci{
15818c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
15828c2ecf20Sopenharmony_ci	u16 state;
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	if (break_state == 0)
15858c2ecf20Sopenharmony_ci		state = BREAK_OFF;
15868c2ecf20Sopenharmony_ci	else
15878c2ecf20Sopenharmony_ci		state = BREAK_ON;
15888c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
15898c2ecf20Sopenharmony_ci		state == BREAK_OFF ? "off" : "on");
15908c2ecf20Sopenharmony_ci	cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
15918c2ecf20Sopenharmony_ci}
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
15948c2ecf20Sopenharmony_cistatic int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
15958c2ecf20Sopenharmony_ci{
15968c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
15978c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	if (priv->gpio_altfunc & BIT(offset))
16008c2ecf20Sopenharmony_ci		return -ENODEV;
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci	return 0;
16038c2ecf20Sopenharmony_ci}
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_cistatic int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
16068c2ecf20Sopenharmony_ci{
16078c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
16088c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
16098c2ecf20Sopenharmony_ci	u8 req_type = REQTYPE_DEVICE_TO_HOST;
16108c2ecf20Sopenharmony_ci	int result;
16118c2ecf20Sopenharmony_ci	u8 buf;
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	if (priv->partnum == CP210X_PARTNUM_CP2105)
16148c2ecf20Sopenharmony_ci		req_type = REQTYPE_INTERFACE_TO_HOST;
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci	result = usb_autopm_get_interface(serial->interface);
16178c2ecf20Sopenharmony_ci	if (result)
16188c2ecf20Sopenharmony_ci		return result;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial, req_type,
16218c2ecf20Sopenharmony_ci					  CP210X_READ_LATCH, &buf, sizeof(buf));
16228c2ecf20Sopenharmony_ci	usb_autopm_put_interface(serial->interface);
16238c2ecf20Sopenharmony_ci	if (result < 0)
16248c2ecf20Sopenharmony_ci		return result;
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	return !!(buf & BIT(gpio));
16278c2ecf20Sopenharmony_ci}
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_cistatic void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
16308c2ecf20Sopenharmony_ci{
16318c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
16328c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
16338c2ecf20Sopenharmony_ci	struct cp210x_gpio_write buf;
16348c2ecf20Sopenharmony_ci	int result;
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	if (value == 1)
16378c2ecf20Sopenharmony_ci		buf.state = BIT(gpio);
16388c2ecf20Sopenharmony_ci	else
16398c2ecf20Sopenharmony_ci		buf.state = 0;
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	buf.mask = BIT(gpio);
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	result = usb_autopm_get_interface(serial->interface);
16448c2ecf20Sopenharmony_ci	if (result)
16458c2ecf20Sopenharmony_ci		goto out;
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	if (priv->partnum == CP210X_PARTNUM_CP2105) {
16488c2ecf20Sopenharmony_ci		result = cp210x_write_vendor_block(serial,
16498c2ecf20Sopenharmony_ci						   REQTYPE_HOST_TO_INTERFACE,
16508c2ecf20Sopenharmony_ci						   CP210X_WRITE_LATCH, &buf,
16518c2ecf20Sopenharmony_ci						   sizeof(buf));
16528c2ecf20Sopenharmony_ci	} else {
16538c2ecf20Sopenharmony_ci		u16 wIndex = buf.state << 8 | buf.mask;
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci		result = usb_control_msg(serial->dev,
16568c2ecf20Sopenharmony_ci					 usb_sndctrlpipe(serial->dev, 0),
16578c2ecf20Sopenharmony_ci					 CP210X_VENDOR_SPECIFIC,
16588c2ecf20Sopenharmony_ci					 REQTYPE_HOST_TO_DEVICE,
16598c2ecf20Sopenharmony_ci					 CP210X_WRITE_LATCH,
16608c2ecf20Sopenharmony_ci					 wIndex,
16618c2ecf20Sopenharmony_ci					 NULL, 0, USB_CTRL_SET_TIMEOUT);
16628c2ecf20Sopenharmony_ci	}
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ci	usb_autopm_put_interface(serial->interface);
16658c2ecf20Sopenharmony_ciout:
16668c2ecf20Sopenharmony_ci	if (result < 0) {
16678c2ecf20Sopenharmony_ci		dev_err(&serial->interface->dev, "failed to set GPIO value: %d\n",
16688c2ecf20Sopenharmony_ci				result);
16698c2ecf20Sopenharmony_ci	}
16708c2ecf20Sopenharmony_ci}
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_cistatic int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
16738c2ecf20Sopenharmony_ci{
16748c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
16758c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	return priv->gpio_input & BIT(gpio);
16788c2ecf20Sopenharmony_ci}
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_cistatic int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
16818c2ecf20Sopenharmony_ci{
16828c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
16838c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
16848c2ecf20Sopenharmony_ci
16858c2ecf20Sopenharmony_ci	if (priv->partnum == CP210X_PARTNUM_CP2105) {
16868c2ecf20Sopenharmony_ci		/* hardware does not support an input mode */
16878c2ecf20Sopenharmony_ci		return -ENOTSUPP;
16888c2ecf20Sopenharmony_ci	}
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	/* push-pull pins cannot be changed to be inputs */
16918c2ecf20Sopenharmony_ci	if (priv->gpio_pushpull & BIT(gpio))
16928c2ecf20Sopenharmony_ci		return -EINVAL;
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	/* make sure to release pin if it is being driven low */
16958c2ecf20Sopenharmony_ci	cp210x_gpio_set(gc, gpio, 1);
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci	priv->gpio_input |= BIT(gpio);
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci	return 0;
17008c2ecf20Sopenharmony_ci}
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_cistatic int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
17038c2ecf20Sopenharmony_ci					int value)
17048c2ecf20Sopenharmony_ci{
17058c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
17068c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	priv->gpio_input &= ~BIT(gpio);
17098c2ecf20Sopenharmony_ci	cp210x_gpio_set(gc, gpio, value);
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci	return 0;
17128c2ecf20Sopenharmony_ci}
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_cistatic int cp210x_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
17158c2ecf20Sopenharmony_ci				  unsigned long config)
17168c2ecf20Sopenharmony_ci{
17178c2ecf20Sopenharmony_ci	struct usb_serial *serial = gpiochip_get_data(gc);
17188c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
17198c2ecf20Sopenharmony_ci	enum pin_config_param param = pinconf_to_config_param(config);
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	/* Succeed only if in correct mode (this can't be set at runtime) */
17228c2ecf20Sopenharmony_ci	if ((param == PIN_CONFIG_DRIVE_PUSH_PULL) &&
17238c2ecf20Sopenharmony_ci	    (priv->gpio_pushpull & BIT(gpio)))
17248c2ecf20Sopenharmony_ci		return 0;
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci	if ((param == PIN_CONFIG_DRIVE_OPEN_DRAIN) &&
17278c2ecf20Sopenharmony_ci	    !(priv->gpio_pushpull & BIT(gpio)))
17288c2ecf20Sopenharmony_ci		return 0;
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	return -ENOTSUPP;
17318c2ecf20Sopenharmony_ci}
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci/*
17348c2ecf20Sopenharmony_ci * This function is for configuring GPIO using shared pins, where other signals
17358c2ecf20Sopenharmony_ci * are made unavailable by configuring the use of GPIO. This is believed to be
17368c2ecf20Sopenharmony_ci * only applicable to the cp2105 at this point, the other devices supported by
17378c2ecf20Sopenharmony_ci * this driver that provide GPIO do so in a way that does not impact other
17388c2ecf20Sopenharmony_ci * signals and are thus expected to have very different initialisation.
17398c2ecf20Sopenharmony_ci */
17408c2ecf20Sopenharmony_cistatic int cp2105_gpioconf_init(struct usb_serial *serial)
17418c2ecf20Sopenharmony_ci{
17428c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
17438c2ecf20Sopenharmony_ci	struct cp210x_pin_mode mode;
17448c2ecf20Sopenharmony_ci	struct cp210x_dual_port_config config;
17458c2ecf20Sopenharmony_ci	u8 intf_num = cp210x_interface_num(serial);
17468c2ecf20Sopenharmony_ci	u8 iface_config;
17478c2ecf20Sopenharmony_ci	int result;
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
17508c2ecf20Sopenharmony_ci					  CP210X_GET_DEVICEMODE, &mode,
17518c2ecf20Sopenharmony_ci					  sizeof(mode));
17528c2ecf20Sopenharmony_ci	if (result < 0)
17538c2ecf20Sopenharmony_ci		return result;
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
17568c2ecf20Sopenharmony_ci					  CP210X_GET_PORTCONFIG, &config,
17578c2ecf20Sopenharmony_ci					  sizeof(config));
17588c2ecf20Sopenharmony_ci	if (result < 0)
17598c2ecf20Sopenharmony_ci		return result;
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	/*  2 banks of GPIO - One for the pins taken from each serial port */
17628c2ecf20Sopenharmony_ci	if (intf_num == 0) {
17638c2ecf20Sopenharmony_ci		priv->gc.ngpio = 2;
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci		if (mode.eci == CP210X_PIN_MODE_MODEM) {
17668c2ecf20Sopenharmony_ci			/* mark all GPIOs of this interface as reserved */
17678c2ecf20Sopenharmony_ci			priv->gpio_altfunc = 0xff;
17688c2ecf20Sopenharmony_ci			return 0;
17698c2ecf20Sopenharmony_ci		}
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci		iface_config = config.eci_cfg;
17728c2ecf20Sopenharmony_ci		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
17738c2ecf20Sopenharmony_ci						CP210X_ECI_GPIO_MODE_MASK) >>
17748c2ecf20Sopenharmony_ci						CP210X_ECI_GPIO_MODE_OFFSET);
17758c2ecf20Sopenharmony_ci	} else if (intf_num == 1) {
17768c2ecf20Sopenharmony_ci		priv->gc.ngpio = 3;
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci		if (mode.sci == CP210X_PIN_MODE_MODEM) {
17798c2ecf20Sopenharmony_ci			/* mark all GPIOs of this interface as reserved */
17808c2ecf20Sopenharmony_ci			priv->gpio_altfunc = 0xff;
17818c2ecf20Sopenharmony_ci			return 0;
17828c2ecf20Sopenharmony_ci		}
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci		iface_config = config.sci_cfg;
17858c2ecf20Sopenharmony_ci		priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
17868c2ecf20Sopenharmony_ci						CP210X_SCI_GPIO_MODE_MASK) >>
17878c2ecf20Sopenharmony_ci						CP210X_SCI_GPIO_MODE_OFFSET);
17888c2ecf20Sopenharmony_ci	} else {
17898c2ecf20Sopenharmony_ci		return -ENODEV;
17908c2ecf20Sopenharmony_ci	}
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci	/* mark all pins which are not in GPIO mode */
17938c2ecf20Sopenharmony_ci	if (iface_config & CP2105_GPIO0_TXLED_MODE)	/* GPIO 0 */
17948c2ecf20Sopenharmony_ci		priv->gpio_altfunc |= BIT(0);
17958c2ecf20Sopenharmony_ci	if (iface_config & (CP2105_GPIO1_RXLED_MODE |	/* GPIO 1 */
17968c2ecf20Sopenharmony_ci			CP2105_GPIO1_RS485_MODE))
17978c2ecf20Sopenharmony_ci		priv->gpio_altfunc |= BIT(1);
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci	/* driver implementation for CP2105 only supports outputs */
18008c2ecf20Sopenharmony_ci	priv->gpio_input = 0;
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	return 0;
18038c2ecf20Sopenharmony_ci}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_cistatic int cp2104_gpioconf_init(struct usb_serial *serial)
18068c2ecf20Sopenharmony_ci{
18078c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
18088c2ecf20Sopenharmony_ci	struct cp210x_single_port_config config;
18098c2ecf20Sopenharmony_ci	u8 iface_config;
18108c2ecf20Sopenharmony_ci	u8 gpio_latch;
18118c2ecf20Sopenharmony_ci	int result;
18128c2ecf20Sopenharmony_ci	u8 i;
18138c2ecf20Sopenharmony_ci
18148c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
18158c2ecf20Sopenharmony_ci					  CP210X_GET_PORTCONFIG, &config,
18168c2ecf20Sopenharmony_ci					  sizeof(config));
18178c2ecf20Sopenharmony_ci	if (result < 0)
18188c2ecf20Sopenharmony_ci		return result;
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ci	priv->gc.ngpio = 4;
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci	iface_config = config.device_cfg;
18238c2ecf20Sopenharmony_ci	priv->gpio_pushpull = (u8)((le16_to_cpu(config.gpio_mode) &
18248c2ecf20Sopenharmony_ci					CP210X_GPIO_MODE_MASK) >>
18258c2ecf20Sopenharmony_ci					CP210X_GPIO_MODE_OFFSET);
18268c2ecf20Sopenharmony_ci	gpio_latch = (u8)((le16_to_cpu(config.reset_state) &
18278c2ecf20Sopenharmony_ci					CP210X_GPIO_MODE_MASK) >>
18288c2ecf20Sopenharmony_ci					CP210X_GPIO_MODE_OFFSET);
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	/* mark all pins which are not in GPIO mode */
18318c2ecf20Sopenharmony_ci	if (iface_config & CP2104_GPIO0_TXLED_MODE)	/* GPIO 0 */
18328c2ecf20Sopenharmony_ci		priv->gpio_altfunc |= BIT(0);
18338c2ecf20Sopenharmony_ci	if (iface_config & CP2104_GPIO1_RXLED_MODE)	/* GPIO 1 */
18348c2ecf20Sopenharmony_ci		priv->gpio_altfunc |= BIT(1);
18358c2ecf20Sopenharmony_ci	if (iface_config & CP2104_GPIO2_RS485_MODE)	/* GPIO 2 */
18368c2ecf20Sopenharmony_ci		priv->gpio_altfunc |= BIT(2);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	/*
18398c2ecf20Sopenharmony_ci	 * Like CP2102N, CP2104 has also no strict input and output pin
18408c2ecf20Sopenharmony_ci	 * modes.
18418c2ecf20Sopenharmony_ci	 * Do the same input mode emulation as CP2102N.
18428c2ecf20Sopenharmony_ci	 */
18438c2ecf20Sopenharmony_ci	for (i = 0; i < priv->gc.ngpio; ++i) {
18448c2ecf20Sopenharmony_ci		/*
18458c2ecf20Sopenharmony_ci		 * Set direction to "input" iff pin is open-drain and reset
18468c2ecf20Sopenharmony_ci		 * value is 1.
18478c2ecf20Sopenharmony_ci		 */
18488c2ecf20Sopenharmony_ci		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
18498c2ecf20Sopenharmony_ci			priv->gpio_input |= BIT(i);
18508c2ecf20Sopenharmony_ci	}
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	return 0;
18538c2ecf20Sopenharmony_ci}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_cistatic int cp2102n_gpioconf_init(struct usb_serial *serial)
18568c2ecf20Sopenharmony_ci{
18578c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
18588c2ecf20Sopenharmony_ci	const u16 config_size = 0x02a6;
18598c2ecf20Sopenharmony_ci	u8 gpio_rst_latch;
18608c2ecf20Sopenharmony_ci	u8 config_version;
18618c2ecf20Sopenharmony_ci	u8 gpio_pushpull;
18628c2ecf20Sopenharmony_ci	u8 *config_buf;
18638c2ecf20Sopenharmony_ci	u8 gpio_latch;
18648c2ecf20Sopenharmony_ci	u8 gpio_ctrl;
18658c2ecf20Sopenharmony_ci	int result;
18668c2ecf20Sopenharmony_ci	u8 i;
18678c2ecf20Sopenharmony_ci
18688c2ecf20Sopenharmony_ci	/*
18698c2ecf20Sopenharmony_ci	 * Retrieve device configuration from the device.
18708c2ecf20Sopenharmony_ci	 * The array received contains all customization settings done at the
18718c2ecf20Sopenharmony_ci	 * factory/manufacturer. Format of the array is documented at the
18728c2ecf20Sopenharmony_ci	 * time of writing at:
18738c2ecf20Sopenharmony_ci	 * https://www.silabs.com/community/interface/knowledge-base.entry.html/2017/03/31/cp2102n_setconfig-xsfa
18748c2ecf20Sopenharmony_ci	 */
18758c2ecf20Sopenharmony_ci	config_buf = kmalloc(config_size, GFP_KERNEL);
18768c2ecf20Sopenharmony_ci	if (!config_buf)
18778c2ecf20Sopenharmony_ci		return -ENOMEM;
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial,
18808c2ecf20Sopenharmony_ci					  REQTYPE_DEVICE_TO_HOST,
18818c2ecf20Sopenharmony_ci					  CP210X_READ_2NCONFIG,
18828c2ecf20Sopenharmony_ci					  config_buf,
18838c2ecf20Sopenharmony_ci					  config_size);
18848c2ecf20Sopenharmony_ci	if (result < 0) {
18858c2ecf20Sopenharmony_ci		kfree(config_buf);
18868c2ecf20Sopenharmony_ci		return result;
18878c2ecf20Sopenharmony_ci	}
18888c2ecf20Sopenharmony_ci
18898c2ecf20Sopenharmony_ci	config_version = config_buf[CP210X_2NCONFIG_CONFIG_VERSION_IDX];
18908c2ecf20Sopenharmony_ci	gpio_pushpull = config_buf[CP210X_2NCONFIG_GPIO_MODE_IDX];
18918c2ecf20Sopenharmony_ci	gpio_ctrl = config_buf[CP210X_2NCONFIG_GPIO_CONTROL_IDX];
18928c2ecf20Sopenharmony_ci	gpio_rst_latch = config_buf[CP210X_2NCONFIG_GPIO_RSTLATCH_IDX];
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci	kfree(config_buf);
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	/* Make sure this is a config format we understand. */
18978c2ecf20Sopenharmony_ci	if (config_version != 0x01)
18988c2ecf20Sopenharmony_ci		return -ENOTSUPP;
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ci	priv->gc.ngpio = 4;
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci	/*
19038c2ecf20Sopenharmony_ci	 * Get default pin states after reset. Needed so we can determine
19048c2ecf20Sopenharmony_ci	 * the direction of an open-drain pin.
19058c2ecf20Sopenharmony_ci	 */
19068c2ecf20Sopenharmony_ci	gpio_latch = (gpio_rst_latch >> 3) & 0x0f;
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci	/* 0 indicates open-drain mode, 1 is push-pull */
19098c2ecf20Sopenharmony_ci	priv->gpio_pushpull = (gpio_pushpull >> 3) & 0x0f;
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	/* 0 indicates GPIO mode, 1 is alternate function */
19128c2ecf20Sopenharmony_ci	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN20) {
19138c2ecf20Sopenharmony_ci		/* QFN20 is special... */
19148c2ecf20Sopenharmony_ci		if (gpio_ctrl & CP2102N_QFN20_GPIO0_CLK_MODE)   /* GPIO 0 */
19158c2ecf20Sopenharmony_ci			priv->gpio_altfunc |= BIT(0);
19168c2ecf20Sopenharmony_ci		if (gpio_ctrl & CP2102N_QFN20_GPIO1_RS485_MODE) /* GPIO 1 */
19178c2ecf20Sopenharmony_ci			priv->gpio_altfunc |= BIT(1);
19188c2ecf20Sopenharmony_ci		if (gpio_ctrl & CP2102N_QFN20_GPIO2_TXLED_MODE) /* GPIO 2 */
19198c2ecf20Sopenharmony_ci			priv->gpio_altfunc |= BIT(2);
19208c2ecf20Sopenharmony_ci		if (gpio_ctrl & CP2102N_QFN20_GPIO3_RXLED_MODE) /* GPIO 3 */
19218c2ecf20Sopenharmony_ci			priv->gpio_altfunc |= BIT(3);
19228c2ecf20Sopenharmony_ci	} else {
19238c2ecf20Sopenharmony_ci		priv->gpio_altfunc = (gpio_ctrl >> 2) & 0x0f;
19248c2ecf20Sopenharmony_ci	}
19258c2ecf20Sopenharmony_ci
19268c2ecf20Sopenharmony_ci	if (priv->partnum == CP210X_PARTNUM_CP2102N_QFN28) {
19278c2ecf20Sopenharmony_ci		/*
19288c2ecf20Sopenharmony_ci		 * For the QFN28 package, GPIO4-6 are controlled by
19298c2ecf20Sopenharmony_ci		 * the low three bits of the mode/latch fields.
19308c2ecf20Sopenharmony_ci		 * Contrary to the document linked above, the bits for
19318c2ecf20Sopenharmony_ci		 * the SUSPEND pins are elsewhere.  No alternate
19328c2ecf20Sopenharmony_ci		 * function is available for these pins.
19338c2ecf20Sopenharmony_ci		 */
19348c2ecf20Sopenharmony_ci		priv->gc.ngpio = 7;
19358c2ecf20Sopenharmony_ci		gpio_latch |= (gpio_rst_latch & 7) << 4;
19368c2ecf20Sopenharmony_ci		priv->gpio_pushpull |= (gpio_pushpull & 7) << 4;
19378c2ecf20Sopenharmony_ci	}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_ci	/*
19408c2ecf20Sopenharmony_ci	 * The CP2102N does not strictly has input and output pin modes,
19418c2ecf20Sopenharmony_ci	 * it only knows open-drain and push-pull modes which is set at
19428c2ecf20Sopenharmony_ci	 * factory. An open-drain pin can function both as an
19438c2ecf20Sopenharmony_ci	 * input or an output. We emulate input mode for open-drain pins
19448c2ecf20Sopenharmony_ci	 * by making sure they are not driven low, and we do not allow
19458c2ecf20Sopenharmony_ci	 * push-pull pins to be set as an input.
19468c2ecf20Sopenharmony_ci	 */
19478c2ecf20Sopenharmony_ci	for (i = 0; i < priv->gc.ngpio; ++i) {
19488c2ecf20Sopenharmony_ci		/*
19498c2ecf20Sopenharmony_ci		 * Set direction to "input" iff pin is open-drain and reset
19508c2ecf20Sopenharmony_ci		 * value is 1.
19518c2ecf20Sopenharmony_ci		 */
19528c2ecf20Sopenharmony_ci		if (!(priv->gpio_pushpull & BIT(i)) && (gpio_latch & BIT(i)))
19538c2ecf20Sopenharmony_ci			priv->gpio_input |= BIT(i);
19548c2ecf20Sopenharmony_ci	}
19558c2ecf20Sopenharmony_ci
19568c2ecf20Sopenharmony_ci	return 0;
19578c2ecf20Sopenharmony_ci}
19588c2ecf20Sopenharmony_ci
19598c2ecf20Sopenharmony_cistatic int cp210x_gpio_init(struct usb_serial *serial)
19608c2ecf20Sopenharmony_ci{
19618c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
19628c2ecf20Sopenharmony_ci	int result;
19638c2ecf20Sopenharmony_ci
19648c2ecf20Sopenharmony_ci	switch (priv->partnum) {
19658c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2104:
19668c2ecf20Sopenharmony_ci		result = cp2104_gpioconf_init(serial);
19678c2ecf20Sopenharmony_ci		break;
19688c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2105:
19698c2ecf20Sopenharmony_ci		result = cp2105_gpioconf_init(serial);
19708c2ecf20Sopenharmony_ci		break;
19718c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN28:
19728c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN24:
19738c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN20:
19748c2ecf20Sopenharmony_ci		result = cp2102n_gpioconf_init(serial);
19758c2ecf20Sopenharmony_ci		break;
19768c2ecf20Sopenharmony_ci	default:
19778c2ecf20Sopenharmony_ci		return 0;
19788c2ecf20Sopenharmony_ci	}
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci	if (result < 0)
19818c2ecf20Sopenharmony_ci		return result;
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci	priv->gc.label = "cp210x";
19848c2ecf20Sopenharmony_ci	priv->gc.request = cp210x_gpio_request;
19858c2ecf20Sopenharmony_ci	priv->gc.get_direction = cp210x_gpio_direction_get;
19868c2ecf20Sopenharmony_ci	priv->gc.direction_input = cp210x_gpio_direction_input;
19878c2ecf20Sopenharmony_ci	priv->gc.direction_output = cp210x_gpio_direction_output;
19888c2ecf20Sopenharmony_ci	priv->gc.get = cp210x_gpio_get;
19898c2ecf20Sopenharmony_ci	priv->gc.set = cp210x_gpio_set;
19908c2ecf20Sopenharmony_ci	priv->gc.set_config = cp210x_gpio_set_config;
19918c2ecf20Sopenharmony_ci	priv->gc.owner = THIS_MODULE;
19928c2ecf20Sopenharmony_ci	priv->gc.parent = &serial->interface->dev;
19938c2ecf20Sopenharmony_ci	priv->gc.base = -1;
19948c2ecf20Sopenharmony_ci	priv->gc.can_sleep = true;
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	result = gpiochip_add_data(&priv->gc, serial);
19978c2ecf20Sopenharmony_ci	if (!result)
19988c2ecf20Sopenharmony_ci		priv->gpio_registered = true;
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_ci	return result;
20018c2ecf20Sopenharmony_ci}
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_cistatic void cp210x_gpio_remove(struct usb_serial *serial)
20048c2ecf20Sopenharmony_ci{
20058c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	if (priv->gpio_registered) {
20088c2ecf20Sopenharmony_ci		gpiochip_remove(&priv->gc);
20098c2ecf20Sopenharmony_ci		priv->gpio_registered = false;
20108c2ecf20Sopenharmony_ci	}
20118c2ecf20Sopenharmony_ci}
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci#else
20148c2ecf20Sopenharmony_ci
20158c2ecf20Sopenharmony_cistatic int cp210x_gpio_init(struct usb_serial *serial)
20168c2ecf20Sopenharmony_ci{
20178c2ecf20Sopenharmony_ci	return 0;
20188c2ecf20Sopenharmony_ci}
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_cistatic void cp210x_gpio_remove(struct usb_serial *serial)
20218c2ecf20Sopenharmony_ci{
20228c2ecf20Sopenharmony_ci	/* Nothing to do */
20238c2ecf20Sopenharmony_ci}
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci#endif
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_cistatic int cp210x_port_probe(struct usb_serial_port *port)
20288c2ecf20Sopenharmony_ci{
20298c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
20308c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv;
20318c2ecf20Sopenharmony_ci	int ret;
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
20348c2ecf20Sopenharmony_ci	if (!port_priv)
20358c2ecf20Sopenharmony_ci		return -ENOMEM;
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ci	port_priv->bInterfaceNumber = cp210x_interface_num(serial);
20388c2ecf20Sopenharmony_ci
20398c2ecf20Sopenharmony_ci	usb_set_serial_port_data(port, port_priv);
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci	ret = cp210x_detect_swapped_line_ctl(port);
20428c2ecf20Sopenharmony_ci	if (ret) {
20438c2ecf20Sopenharmony_ci		kfree(port_priv);
20448c2ecf20Sopenharmony_ci		return ret;
20458c2ecf20Sopenharmony_ci	}
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_ci	return 0;
20488c2ecf20Sopenharmony_ci}
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_cistatic int cp210x_port_remove(struct usb_serial_port *port)
20518c2ecf20Sopenharmony_ci{
20528c2ecf20Sopenharmony_ci	struct cp210x_port_private *port_priv;
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_ci	port_priv = usb_get_serial_port_data(port);
20558c2ecf20Sopenharmony_ci	kfree(port_priv);
20568c2ecf20Sopenharmony_ci
20578c2ecf20Sopenharmony_ci	return 0;
20588c2ecf20Sopenharmony_ci}
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_cistatic void cp210x_init_max_speed(struct usb_serial *serial)
20618c2ecf20Sopenharmony_ci{
20628c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
20638c2ecf20Sopenharmony_ci	bool use_actual_rate = false;
20648c2ecf20Sopenharmony_ci	speed_t min = 300;
20658c2ecf20Sopenharmony_ci	speed_t max;
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_ci	switch (priv->partnum) {
20688c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2101:
20698c2ecf20Sopenharmony_ci		max = 921600;
20708c2ecf20Sopenharmony_ci		break;
20718c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102:
20728c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2103:
20738c2ecf20Sopenharmony_ci		max = 1000000;
20748c2ecf20Sopenharmony_ci		break;
20758c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2104:
20768c2ecf20Sopenharmony_ci		use_actual_rate = true;
20778c2ecf20Sopenharmony_ci		max = 2000000;
20788c2ecf20Sopenharmony_ci		break;
20798c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2108:
20808c2ecf20Sopenharmony_ci		max = 2000000;
20818c2ecf20Sopenharmony_ci		break;
20828c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2105:
20838c2ecf20Sopenharmony_ci		if (cp210x_interface_num(serial) == 0) {
20848c2ecf20Sopenharmony_ci			use_actual_rate = true;
20858c2ecf20Sopenharmony_ci			max = 2000000;	/* ECI */
20868c2ecf20Sopenharmony_ci		} else {
20878c2ecf20Sopenharmony_ci			min = 2400;
20888c2ecf20Sopenharmony_ci			max = 921600;	/* SCI */
20898c2ecf20Sopenharmony_ci		}
20908c2ecf20Sopenharmony_ci		break;
20918c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN28:
20928c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN24:
20938c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102N_QFN20:
20948c2ecf20Sopenharmony_ci		use_actual_rate = true;
20958c2ecf20Sopenharmony_ci		max = 3000000;
20968c2ecf20Sopenharmony_ci		break;
20978c2ecf20Sopenharmony_ci	default:
20988c2ecf20Sopenharmony_ci		max = 2000000;
20998c2ecf20Sopenharmony_ci		break;
21008c2ecf20Sopenharmony_ci	}
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	priv->min_speed = min;
21038c2ecf20Sopenharmony_ci	priv->max_speed = max;
21048c2ecf20Sopenharmony_ci	priv->use_actual_rate = use_actual_rate;
21058c2ecf20Sopenharmony_ci}
21068c2ecf20Sopenharmony_ci
21078c2ecf20Sopenharmony_cistatic void cp2102_determine_quirks(struct usb_serial *serial)
21088c2ecf20Sopenharmony_ci{
21098c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
21108c2ecf20Sopenharmony_ci	u8 *buf;
21118c2ecf20Sopenharmony_ci	int ret;
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	buf = kmalloc(2, GFP_KERNEL);
21148c2ecf20Sopenharmony_ci	if (!buf)
21158c2ecf20Sopenharmony_ci		return;
21168c2ecf20Sopenharmony_ci	/*
21178c2ecf20Sopenharmony_ci	 * Some (possibly counterfeit) CP2102 do not support event-insertion
21188c2ecf20Sopenharmony_ci	 * mode and respond differently to malformed vendor requests.
21198c2ecf20Sopenharmony_ci	 * Specifically, they return one instead of two bytes when sent a
21208c2ecf20Sopenharmony_ci	 * two-byte part-number request.
21218c2ecf20Sopenharmony_ci	 */
21228c2ecf20Sopenharmony_ci	ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
21238c2ecf20Sopenharmony_ci			CP210X_VENDOR_SPECIFIC, REQTYPE_DEVICE_TO_HOST,
21248c2ecf20Sopenharmony_ci			CP210X_GET_PARTNUM, 0, buf, 2, USB_CTRL_GET_TIMEOUT);
21258c2ecf20Sopenharmony_ci	if (ret == 1) {
21268c2ecf20Sopenharmony_ci		dev_dbg(&serial->interface->dev,
21278c2ecf20Sopenharmony_ci				"device does not support event-insertion mode\n");
21288c2ecf20Sopenharmony_ci		priv->no_event_mode = true;
21298c2ecf20Sopenharmony_ci	}
21308c2ecf20Sopenharmony_ci
21318c2ecf20Sopenharmony_ci	kfree(buf);
21328c2ecf20Sopenharmony_ci}
21338c2ecf20Sopenharmony_ci
21348c2ecf20Sopenharmony_cistatic void cp210x_determine_quirks(struct usb_serial *serial)
21358c2ecf20Sopenharmony_ci{
21368c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
21378c2ecf20Sopenharmony_ci
21388c2ecf20Sopenharmony_ci	switch (priv->partnum) {
21398c2ecf20Sopenharmony_ci	case CP210X_PARTNUM_CP2102:
21408c2ecf20Sopenharmony_ci		cp2102_determine_quirks(serial);
21418c2ecf20Sopenharmony_ci		break;
21428c2ecf20Sopenharmony_ci	default:
21438c2ecf20Sopenharmony_ci		break;
21448c2ecf20Sopenharmony_ci	}
21458c2ecf20Sopenharmony_ci}
21468c2ecf20Sopenharmony_ci
21478c2ecf20Sopenharmony_cistatic int cp210x_attach(struct usb_serial *serial)
21488c2ecf20Sopenharmony_ci{
21498c2ecf20Sopenharmony_ci	int result;
21508c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv;
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
21538c2ecf20Sopenharmony_ci	if (!priv)
21548c2ecf20Sopenharmony_ci		return -ENOMEM;
21558c2ecf20Sopenharmony_ci
21568c2ecf20Sopenharmony_ci	result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
21578c2ecf20Sopenharmony_ci					  CP210X_GET_PARTNUM, &priv->partnum,
21588c2ecf20Sopenharmony_ci					  sizeof(priv->partnum));
21598c2ecf20Sopenharmony_ci	if (result < 0) {
21608c2ecf20Sopenharmony_ci		dev_warn(&serial->interface->dev,
21618c2ecf20Sopenharmony_ci			 "querying part number failed\n");
21628c2ecf20Sopenharmony_ci		priv->partnum = CP210X_PARTNUM_UNKNOWN;
21638c2ecf20Sopenharmony_ci	}
21648c2ecf20Sopenharmony_ci
21658c2ecf20Sopenharmony_ci	usb_set_serial_data(serial, priv);
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	cp210x_determine_quirks(serial);
21688c2ecf20Sopenharmony_ci	cp210x_init_max_speed(serial);
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	result = cp210x_gpio_init(serial);
21718c2ecf20Sopenharmony_ci	if (result < 0) {
21728c2ecf20Sopenharmony_ci		dev_err(&serial->interface->dev, "GPIO initialisation failed: %d\n",
21738c2ecf20Sopenharmony_ci				result);
21748c2ecf20Sopenharmony_ci	}
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	return 0;
21778c2ecf20Sopenharmony_ci}
21788c2ecf20Sopenharmony_ci
21798c2ecf20Sopenharmony_cistatic void cp210x_disconnect(struct usb_serial *serial)
21808c2ecf20Sopenharmony_ci{
21818c2ecf20Sopenharmony_ci	cp210x_gpio_remove(serial);
21828c2ecf20Sopenharmony_ci}
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_cistatic void cp210x_release(struct usb_serial *serial)
21858c2ecf20Sopenharmony_ci{
21868c2ecf20Sopenharmony_ci	struct cp210x_serial_private *priv = usb_get_serial_data(serial);
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_ci	cp210x_gpio_remove(serial);
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	kfree(priv);
21918c2ecf20Sopenharmony_ci}
21928c2ecf20Sopenharmony_ci
21938c2ecf20Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
21968c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2197