1 // SPDX-License-Identifier: GPL-2.0
3 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
5 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
7 * Support to set flow control line levels using TIOCMGET and TIOCMSET
8 * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
9 * control thanks to Munir Nassar nassarmu@real-time.com
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/usb.h>
21 #include <linux/uaccess.h>
22 #include <linux/usb/serial.h>
24 #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
29 static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
30 static void cp210x_close(struct usb_serial_port *);
31 static int cp210x_ioctl(struct tty_struct *tty,
32 unsigned int cmd, unsigned long arg);
33 static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *);
34 static void cp210x_get_termios_port(struct usb_serial_port *port,
35 tcflag_t *cflagp, unsigned int *baudp);
36 static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
38 static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
40 static bool cp210x_tx_empty(struct usb_serial_port *port);
41 static int cp210x_tiocmget(struct tty_struct *);
42 static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
43 static int cp210x_tiocmset_port(struct usb_serial_port *port,
44 unsigned int, unsigned int);
45 static void cp210x_break_ctl(struct tty_struct *, int);
46 static int cp210x_port_probe(struct usb_serial_port *);
47 static void cp210x_port_remove(struct usb_serial_port *);
48 static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
50 static const struct usb_device_id id_table[] = {
51 { USB_DEVICE(0x04E8, 0x20C6) }, /* SmartThings link WW (Zigbee Only) */
52 { USB_DEVICE(0x04E8, 0x20C7) }, /* SmartThings link US */
53 { USB_DEVICE(0x04E8, 0x20C8) }, /* SmartThings link EU */
54 { USB_DEVICE(0x04E8, 0x20C9) }, /* SmartThings link KR */
55 { } /* Terminating Entry */
58 MODULE_DEVICE_TABLE(usb, id_table);
60 struct cp210x_port_private {
62 __u8 bInterfaceNumber;
63 bool has_swapped_line_ctl;
67 static struct usb_serial_driver cp210x_device = {
77 .close = cp210x_close,
78 .ioctl = cp210x_ioctl,
79 .break_ctl = cp210x_break_ctl,
80 .set_termios = cp210x_set_termios,
81 .tx_empty = cp210x_tx_empty,
82 .tiocmget = cp210x_tiocmget,
83 .tiocmset = cp210x_tiocmset,
84 .port_probe = cp210x_port_probe,
85 .port_remove = cp210x_port_remove,
86 .dtr_rts = cp210x_dtr_rts
90 #define IOCTL_GPIOGET 0x8000
91 #define IOCTL_GPIOSET 0x8001
93 static struct usb_serial_driver * const serial_drivers[] = {
97 /* Config request types */
98 #define REQTYPE_HOST_TO_INTERFACE 0x41
99 #define REQTYPE_INTERFACE_TO_HOST 0xc1
100 #define REQTYPE_HOST_TO_DEVICE 0x40
101 #define REQTYPE_DEVICE_TO_HOST 0xc0
103 /* Config request codes */
104 #define CP210X_IFC_ENABLE 0x00
105 #define CP210X_SET_BAUDDIV 0x01
106 #define CP210X_GET_BAUDDIV 0x02
107 #define CP210X_SET_LINE_CTL 0x03
108 #define CP210X_GET_LINE_CTL 0x04
109 #define CP210X_SET_BREAK 0x05
110 #define CP210X_IMM_CHAR 0x06
111 #define CP210X_SET_MHS 0x07
112 #define CP210X_GET_MDMSTS 0x08
113 #define CP210X_SET_XON 0x09
114 #define CP210X_SET_XOFF 0x0A
115 #define CP210X_SET_EVENTMASK 0x0B
116 #define CP210X_GET_EVENTMASK 0x0C
117 #define CP210X_SET_CHAR 0x0D
118 #define CP210X_GET_CHARS 0x0E
119 #define CP210X_GET_PROPS 0x0F
120 #define CP210X_GET_COMM_STATUS 0x10
121 #define CP210X_RESET 0x11
122 #define CP210X_PURGE 0x12
123 #define CP210X_SET_FLOW 0x13
124 #define CP210X_GET_FLOW 0x14
125 #define CP210X_EMBED_EVENTS 0x15
126 #define CP210X_GET_EVENTSTATE 0x16
127 #define CP210X_SET_CHARS 0x19
128 #define CP210X_GET_BAUDRATE 0x1D
129 #define CP210X_SET_BAUDRATE 0x1E
130 #define CP210X_VENDOR_SPECIFIC 0xFF
132 /* CP210X_IFC_ENABLE */
133 #define UART_ENABLE 0x0001
134 #define UART_DISABLE 0x0000
136 /* CP210X_(SET|GET)_BAUDDIV */
137 #define BAUD_RATE_GEN_FREQ 0x384000
139 /* CP210X_(SET|GET)_LINE_CTL */
140 #define BITS_DATA_MASK 0X0f00
141 #define BITS_DATA_5 0X0500
142 #define BITS_DATA_6 0X0600
143 #define BITS_DATA_7 0X0700
144 #define BITS_DATA_8 0X0800
145 #define BITS_DATA_9 0X0900
147 #define BITS_PARITY_MASK 0x00f0
148 #define BITS_PARITY_NONE 0x0000
149 #define BITS_PARITY_ODD 0x0010
150 #define BITS_PARITY_EVEN 0x0020
151 #define BITS_PARITY_MARK 0x0030
152 #define BITS_PARITY_SPACE 0x0040
154 #define BITS_STOP_MASK 0x000f
155 #define BITS_STOP_1 0x0000
156 #define BITS_STOP_1_5 0x0001
157 #define BITS_STOP_2 0x0002
159 /* CP210X_SET_BREAK */
160 #define BREAK_ON 0x0001
161 #define BREAK_OFF 0x0000
163 /* CP210X_(SET_MHS|GET_MDMSTS) */
164 #define CONTROL_DTR 0x0001
165 #define CONTROL_RTS 0x0002
166 #define CONTROL_CTS 0x0010
167 #define CONTROL_DSR 0x0020
168 #define CONTROL_RING 0x0040
169 #define CONTROL_DCD 0x0080
170 #define CONTROL_WRITE_DTR 0x0100
171 #define CONTROL_WRITE_RTS 0x0200
173 /* CP210X_VENDOR_SPECIFIC sub-commands passed in wValue */
174 #define CP210X_WRITE_LATCH 0x37E1
175 #define CP210X_READ_LATCH 0x00C2
176 #define CP210X_GET_PARTNUM 0x370B
177 #define CP2102N_GET_FW_VERS 0x0010
179 /* CP210X_GET_PARTNUM returns one of these */
180 #define CP2101_PARTNUM 0x01
181 #define CP2102_PARTNUM 0x02
182 #define CP2103_PARTNUM 0x03
183 #define CP2104_PARTNUM 0x04
184 #define CP2105_PARTNUM 0x05
185 #define CP2108_PARTNUM 0x08
186 #define CP210x_PARTNUM_CP2102N_QFN28 0x20
187 #define CP210x_PARTNUM_CP2102N_QFN24 0x21
188 #define CP210x_PARTNUM_CP2102N_QFN20 0x22
190 /* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
191 struct cp210x_comm_status {
193 __le32 ulHoldReasons;
194 __le32 ulAmountInInQueue;
195 __le32 ulAmountInOutQueue;
197 u8 bWaitForImmediate;
202 * CP210X_PURGE - 16 bits passed in wValue of USB request.
203 * SiLabs app note AN571 gives a strange description of the 4 bits:
204 * bit 0 or bit 2 clears the transmit queue and 1 or 3 receive.
205 * writing 1 to all, however, purges cp2108 well enough to avoid the hang.
207 #define PURGE_ALL 0x000f
209 /* CP210X_GET_FLOW/CP210X_SET_FLOW read/write these 0x10 bytes */
210 struct cp210x_flow_ctl {
211 __le32 ulControlHandshake;
212 __le32 ulFlowReplace;
217 /* cp210x_flow_ctl::ulControlHandshake */
218 #define CP210X_SERIAL_DTR_MASK GENMASK(1, 0)
219 #define CP210X_SERIAL_DTR_SHIFT(_mode) (_mode)
220 #define CP210X_SERIAL_CTS_HANDSHAKE BIT(3)
221 #define CP210X_SERIAL_DSR_HANDSHAKE BIT(4)
222 #define CP210X_SERIAL_DCD_HANDSHAKE BIT(5)
223 #define CP210X_SERIAL_DSR_SENSITIVITY BIT(6)
225 /* values for cp210x_flow_ctl::ulControlHandshake::CP210X_SERIAL_DTR_MASK */
226 #define CP210X_SERIAL_DTR_INACTIVE 0
227 #define CP210X_SERIAL_DTR_ACTIVE 1
228 #define CP210X_SERIAL_DTR_FLOW_CTL 2
230 /* cp210x_flow_ctl::ulFlowReplace */
231 #define CP210X_SERIAL_AUTO_TRANSMIT BIT(0)
232 #define CP210X_SERIAL_AUTO_RECEIVE BIT(1)
233 #define CP210X_SERIAL_ERROR_CHAR BIT(2)
234 #define CP210X_SERIAL_NULL_STRIPPING BIT(3)
235 #define CP210X_SERIAL_BREAK_CHAR BIT(4)
236 #define CP210X_SERIAL_RTS_MASK GENMASK(7, 6)
237 #define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
238 #define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
240 /* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
241 #define CP210X_SERIAL_RTS_INACTIVE 0
242 #define CP210X_SERIAL_RTS_ACTIVE 1
243 #define CP210X_SERIAL_RTS_FLOW_CTL 2
246 * Reads a variable-sized block of CP210X_ registers, identified by req.
247 * Returns data into buf in native USB byte order.
249 static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
250 void *buf, int bufsize)
252 struct usb_serial *serial = port->serial;
253 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
257 dmabuf = kmalloc(bufsize, GFP_KERNEL);
260 * FIXME Some callers don't bother to check for error,
261 * at least give them consistent junk until they are fixed
263 memset(buf, 0, bufsize);
267 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
268 req, REQTYPE_INTERFACE_TO_HOST, 0,
269 port_priv->bInterfaceNumber, dmabuf, bufsize,
270 USB_CTRL_SET_TIMEOUT);
271 if (result == bufsize) {
272 memcpy(buf, dmabuf, bufsize);
275 dev_err(&port->dev, "failed get req 0x%x size %d status: %d\n",
276 req, bufsize, result);
281 * FIXME Some callers don't bother to check for error,
282 * at least give them consistent junk until they are fixed
284 memset(buf, 0, bufsize);
293 * Reads any 32-bit CP210X_ register identified by req.
295 static int cp210x_read_u32_reg(struct usb_serial_port *port, u8 req, u32 *val)
300 err = cp210x_read_reg_block(port, req, &le32_val, sizeof(le32_val));
303 * FIXME Some callers don't bother to check for error,
304 * at least give them consistent junk until they are fixed
310 *val = le32_to_cpu(le32_val);
316 * Reads any 16-bit CP210X_ register identified by req.
318 static int cp210x_read_u16_reg(struct usb_serial_port *port, u8 req, u16 *val)
323 err = cp210x_read_reg_block(port, req, &le16_val, sizeof(le16_val));
327 *val = le16_to_cpu(le16_val);
333 * Reads any 8-bit CP210X_ register identified by req.
335 static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
337 return cp210x_read_reg_block(port, req, val, sizeof(*val));
341 * Writes any 16-bit CP210X_ register (req) whose value is passed
342 * entirely in the wValue field of the USB request.
344 static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
346 struct usb_serial *serial = port->serial;
347 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
350 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
351 req, REQTYPE_HOST_TO_INTERFACE, val,
352 port_priv->bInterfaceNumber, NULL, 0,
353 USB_CTRL_SET_TIMEOUT);
355 dev_err(&port->dev, "failed set request 0x%x status: %d\n",
363 * Writes a variable-sized block of CP210X_ registers, identified by req.
364 * Data in buf must be in native USB byte order.
366 static int cp210x_write_reg_block(struct usb_serial_port *port, u8 req,
367 void *buf, int bufsize)
369 struct usb_serial *serial = port->serial;
370 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
374 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
378 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
379 req, REQTYPE_HOST_TO_INTERFACE, 0,
380 port_priv->bInterfaceNumber, dmabuf, bufsize,
381 USB_CTRL_SET_TIMEOUT);
385 if (result == bufsize) {
388 dev_err(&port->dev, "failed set req 0x%x size %d status: %d\n",
389 req, bufsize, result);
398 * Writes any 32-bit CP210X_ register identified by req.
400 static int cp210x_write_u32_reg(struct usb_serial_port *port, u8 req, u32 val)
404 le32_val = cpu_to_le32(val);
406 return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
410 * Detect CP2108 GET_LINE_CTL bug and activate workaround.
411 * Write a known good value 0x800, read it back.
412 * If it comes back swapped the bug is detected.
413 * Preserve the original register value.
415 static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
417 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
422 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_save);
426 err = cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, 0x800);
430 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, &line_ctl_test);
434 if (line_ctl_test == 8) {
435 port_priv->has_swapped_line_ctl = true;
436 line_ctl_save = swab16(line_ctl_save);
439 return cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, line_ctl_save);
443 * Must always be called instead of cp210x_read_u16_reg(CP210X_GET_LINE_CTL)
444 * to workaround cp2108 bug and get correct value.
446 static int cp210x_get_line_ctl(struct usb_serial_port *port, u16 *ctl)
448 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
451 err = cp210x_read_u16_reg(port, CP210X_GET_LINE_CTL, ctl);
455 /* Workaround swapped bytes in 16-bit value from CP210X_GET_LINE_CTL */
456 if (port_priv->has_swapped_line_ctl)
463 * cp210x_quantise_baudrate
464 * Quantises the baud rate as per AN205 Table 1
466 static unsigned int cp210x_quantise_baudrate(unsigned int baud)
470 else if (baud <= 600) baud = 600;
471 else if (baud <= 1200) baud = 1200;
472 else if (baud <= 1800) baud = 1800;
473 else if (baud <= 2400) baud = 2400;
474 else if (baud <= 4000) baud = 4000;
475 else if (baud <= 4803) baud = 4800;
476 else if (baud <= 7207) baud = 7200;
477 else if (baud <= 9612) baud = 9600;
478 else if (baud <= 14428) baud = 14400;
479 else if (baud <= 16062) baud = 16000;
480 else if (baud <= 19250) baud = 19200;
481 else if (baud <= 28912) baud = 28800;
482 else if (baud <= 38601) baud = 38400;
483 else if (baud <= 51558) baud = 51200;
484 else if (baud <= 56280) baud = 56000;
485 else if (baud <= 58053) baud = 57600;
486 else if (baud <= 64111) baud = 64000;
487 else if (baud <= 77608) baud = 76800;
488 else if (baud <= 117028) baud = 115200;
489 else if (baud <= 129347) baud = 128000;
490 else if (baud <= 156868) baud = 153600;
491 else if (baud <= 237832) baud = 230400;
492 else if (baud <= 254234) baud = 250000;
493 else if (baud <= 273066) baud = 256000;
494 else if (baud <= 491520) baud = 460800;
495 else if (baud <= 567138) baud = 500000;
496 else if (baud <= 670254) baud = 576000;
497 else if (baud < 1000000)
499 else if (baud > 2000000)
504 static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
508 result = cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_ENABLE);
510 dev_err(&port->dev, "%s - Unable to enable UART\n", __func__);
514 /* Configure the termios structure */
515 cp210x_get_termios(tty, port);
517 /* The baud rate must be initialised on cp2104 */
519 cp210x_change_speed(tty, port, NULL);
521 return usb_serial_generic_open(tty, port);
524 static void cp210x_close(struct usb_serial_port *port)
526 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
528 usb_serial_generic_close(port);
530 /* Clear both queues; cp2108 needs this to avoid an occasional hang */
531 cp210x_write_u16_reg(port, CP210X_PURGE, PURGE_ALL);
533 cp210x_write_u16_reg(port, CP210X_IFC_ENABLE, UART_DISABLE);
535 if (port_priv->is_cp2102n_a01) {
538 struct usb_serial *serial = port->serial;
540 result = usb_control_msg(serial->dev,
541 usb_sndctrlpipe(serial->dev, 0),
545 port_priv->bInterfaceNumber,
548 USB_CTRL_SET_TIMEOUT);
552 dev_err(&port->dev, "failed usb_control_msg (1) request 0x09 status: %d\n",
556 result = usb_control_msg(serial->dev,
557 usb_sndctrlpipe(serial->dev, 0),
561 port_priv->bInterfaceNumber,
564 USB_CTRL_SET_TIMEOUT);
568 dev_err(&port->dev, "failed usb_control_msg (2) request 0x09 status: %d\n",
573 result = usb_control_msg(serial->dev,
574 usb_sndctrlpipe(serial->dev, 0),
581 USB_CTRL_SET_TIMEOUT);
585 dev_err(&port->dev, "failed usb_control_msg (1) request 0x01 status: %d\n",
589 result = usb_control_msg(serial->dev,
590 usb_sndctrlpipe(serial->dev, 0),
597 USB_CTRL_SET_TIMEOUT);
601 dev_err(&port->dev, "failed usb_control_msg (2) request 0x01 status: %d\n",
609 * Reads a variable-sized vendor-specific register identified by wValue
610 * Returns data into buf in native USB byte order.
612 static int cp210x_read_vs_reg_block(struct usb_serial_port *port, u8 bmRequestType,
613 u16 wValue, void *buf, int bufsize)
615 struct usb_serial *serial = port->serial;
616 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
620 dmabuf = kmalloc(bufsize, GFP_KERNEL);
624 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
625 CP210X_VENDOR_SPECIFIC, bmRequestType, wValue,
626 port_priv->bInterfaceNumber, dmabuf, bufsize,
627 USB_CTRL_SET_TIMEOUT);
628 if (result == bufsize) {
629 memcpy(buf, dmabuf, bufsize);
632 dev_err(&port->dev, "failed get VENDOR_SPECIFIC wValue 0x%x size %d status: %d\n",
633 wValue, bufsize, result);
643 /* GPIO register read from single-interface CP210x */
644 static int cp210x_read_device_gpio_u8(struct usb_serial_port *port, u8 *val)
646 return cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST, CP210X_READ_LATCH, val, 1);
649 /* GPIO register read from CP2105 */
650 static int cp210x_read_interface_gpio_u8(struct usb_serial_port *port, u8 *val)
652 return cp210x_read_vs_reg_block(port, REQTYPE_INTERFACE_TO_HOST, CP210X_READ_LATCH, val, 1);
655 /* GPIO register read from CP2108 */
656 static int cp210x_read_device_gpio_u16(struct usb_serial_port *port, u16 *val)
659 int err = cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST, CP210X_READ_LATCH, &le16_val, 2);
663 *val = le16_to_cpu(le16_val);
667 /* GPIO register write to single-interface CP210x */
668 static int cp210x_write_device_gpio_u16(struct usb_serial_port *port, u16 val)
672 result = usb_control_msg(port->serial->dev,
673 usb_sndctrlpipe(port->serial->dev, 0),
674 CP210X_VENDOR_SPECIFIC,
675 REQTYPE_HOST_TO_DEVICE,
676 CP210X_WRITE_LATCH, /* wValue */
678 NULL, 0, USB_CTRL_SET_TIMEOUT);
680 dev_err(&port->dev, "failed set WRITE_LATCH status: %d\n",
689 * Writes a variable-sized block of CP210X_ registers, identified by req.
690 * Data in buf must be in native USB byte order.
692 static int cp210x_write_gpio_reg_block(struct usb_serial_port *port,
693 u8 bmRequestType, void *buf, int bufsize)
695 struct usb_serial *serial = port->serial;
696 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
700 dmabuf = kmalloc(bufsize, GFP_KERNEL);
704 memcpy(dmabuf, buf, bufsize);
706 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
707 CP210X_VENDOR_SPECIFIC, bmRequestType,
708 CP210X_WRITE_LATCH, /* wValue */
709 port_priv->bInterfaceNumber, /* wIndex */
711 USB_CTRL_SET_TIMEOUT);
715 if (result == bufsize) {
718 dev_err(&port->dev, "failed set WRITE_LATCH size %d status: %d\n",
727 /* GPIO register write to CP2105 */
728 static int cp210x_write_interface_gpio_u16(struct usb_serial_port *port, u16 val)
730 __le16 le16_val = cpu_to_le16(val);
732 return cp210x_write_gpio_reg_block(port, REQTYPE_HOST_TO_INTERFACE, &le16_val, 2);
735 /* GPIO register write to CP2108 */
736 static int cp210x_write_device_gpio_u32(struct usb_serial_port *port, u32 val)
738 __le32 le32_val = cpu_to_le32(val);
740 return cp210x_write_gpio_reg_block(port, REQTYPE_HOST_TO_DEVICE, &le32_val, 4);
743 static int cp210x_ioctl(struct tty_struct *tty,
744 unsigned int cmd, unsigned long arg)
746 struct usb_serial_port *port = tty->driver_data;
747 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
751 if ((port_priv->bPartNumber == CP2103_PARTNUM) ||
752 (port_priv->bPartNumber == CP2104_PARTNUM) ||
753 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN28) ||
754 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN24) ||
755 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN20)) {
757 int err = cp210x_read_device_gpio_u8(port, &gpio);
760 if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
764 else if (port_priv->bPartNumber == CP2105_PARTNUM) {
766 int err = cp210x_read_interface_gpio_u8(port, &gpio);
769 if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
773 else if (port_priv->bPartNumber == CP2108_PARTNUM) {
775 int err = cp210x_read_device_gpio_u16(port, &gpio);
778 if (copy_to_user((void*)arg, &gpio, sizeof(gpio)))
787 if ((port_priv->bPartNumber == CP2103_PARTNUM) ||
788 (port_priv->bPartNumber == CP2104_PARTNUM) ||
789 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN28) ||
790 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN24) ||
791 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN20)) {
793 if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
795 return cp210x_write_device_gpio_u16(port, gpio);
797 else if (port_priv->bPartNumber == CP2105_PARTNUM) {
799 if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
801 return cp210x_write_interface_gpio_u16(port, gpio);
803 else if (port_priv->bPartNumber == CP2108_PARTNUM) {
805 if (copy_from_user(&gpio, (void*)arg, sizeof(gpio)))
807 return cp210x_write_device_gpio_u32(port, gpio);
822 * Read how many bytes are waiting in the TX queue.
824 static int cp210x_get_tx_queue_byte_count(struct usb_serial_port *port,
827 struct usb_serial *serial = port->serial;
828 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
829 struct cp210x_comm_status *sts;
832 sts = kmalloc(sizeof(*sts), GFP_KERNEL);
836 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
837 CP210X_GET_COMM_STATUS, REQTYPE_INTERFACE_TO_HOST,
838 0, port_priv->bInterfaceNumber, sts, sizeof(*sts),
839 USB_CTRL_GET_TIMEOUT);
840 if (result == sizeof(*sts)) {
841 *count = le32_to_cpu(sts->ulAmountInOutQueue);
844 dev_err(&port->dev, "failed to get comm status: %d\n", result);
854 static bool cp210x_tx_empty(struct usb_serial_port *port)
859 err = cp210x_get_tx_queue_byte_count(port, &count);
868 * Reads the baud rate, data bits, parity, stop bits and flow control mode
869 * from the device, corrects any unsupported values, and configures the
870 * termios structure to reflect the state of the device
872 static void cp210x_get_termios(struct tty_struct *tty,
873 struct usb_serial_port *port)
878 cp210x_get_termios_port(tty->driver_data,
879 &tty->termios.c_cflag, &baud);
880 tty_encode_baud_rate(tty, baud, baud);
884 cp210x_get_termios_port(port, &cflag, &baud);
889 * cp210x_get_termios_port
890 * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
892 static void cp210x_get_termios_port(struct usb_serial_port *port,
893 tcflag_t *cflagp, unsigned int *baudp)
895 struct device *dev = &port->dev;
897 struct cp210x_flow_ctl flow_ctl;
902 cp210x_read_u32_reg(port, CP210X_GET_BAUDRATE, &baud);
904 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
909 cp210x_get_line_ctl(port, &bits);
911 switch (bits & BITS_DATA_MASK) {
913 dev_dbg(dev, "%s - data bits = 5\n", __func__);
917 dev_dbg(dev, "%s - data bits = 6\n", __func__);
921 dev_dbg(dev, "%s - data bits = 7\n", __func__);
925 dev_dbg(dev, "%s - data bits = 8\n", __func__);
929 dev_dbg(dev, "%s - data bits = 9 (not supported, using 8 data bits)\n", __func__);
931 bits &= ~BITS_DATA_MASK;
933 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
936 dev_dbg(dev, "%s - Unknown number of data bits, using 8\n", __func__);
938 bits &= ~BITS_DATA_MASK;
940 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
944 switch (bits & BITS_PARITY_MASK) {
945 case BITS_PARITY_NONE:
946 dev_dbg(dev, "%s - parity = NONE\n", __func__);
949 case BITS_PARITY_ODD:
950 dev_dbg(dev, "%s - parity = ODD\n", __func__);
951 cflag |= (PARENB|PARODD);
953 case BITS_PARITY_EVEN:
954 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
958 case BITS_PARITY_MARK:
959 dev_dbg(dev, "%s - parity = MARK\n", __func__);
960 cflag |= (PARENB|PARODD|CMSPAR);
962 case BITS_PARITY_SPACE:
963 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
965 cflag |= (PARENB|CMSPAR);
968 dev_dbg(dev, "%s - Unknown parity mode, disabling parity\n", __func__);
970 bits &= ~BITS_PARITY_MASK;
971 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
976 switch (bits & BITS_STOP_MASK) {
978 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
981 dev_dbg(dev, "%s - stop bits = 1.5 (not supported, using 1 stop bit)\n", __func__);
982 bits &= ~BITS_STOP_MASK;
983 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
986 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
990 dev_dbg(dev, "%s - Unknown number of stop bits, using 1 stop bit\n", __func__);
991 bits &= ~BITS_STOP_MASK;
992 cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits);
996 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
998 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
999 if (ctl_hs & CP210X_SERIAL_CTS_HANDSHAKE) {
1000 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1003 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
1011 * CP2101 supports the following baud rates:
1013 * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
1014 * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
1016 * CP2102 and CP2103 support the following additional rates:
1018 * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
1021 * The device will map a requested rate to a supported one, but the result
1022 * of requests for rates greater than 1053257 is undefined (see AN205).
1024 * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
1025 * respectively, with an error less than 1%. The actual rates are determined
1028 * div = round(freq / (2 x prescale x request))
1029 * actual = freq / (2 x prescale x div)
1031 * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
1033 * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
1036 static void cp210x_change_speed(struct tty_struct *tty,
1037 struct usb_serial_port *port, struct ktermios *old_termios)
1041 baud = tty->termios.c_ospeed;
1043 /* This maps the requested rate to a rate valid on cp2102 or cp2103,
1044 * or to an arbitrary rate in [1M,2M].
1046 * NOTE: B0 is not implemented.
1048 baud = cp210x_quantise_baudrate(baud);
1050 dev_dbg(&port->dev, "%s - setting baud rate to %u\n", __func__, baud);
1051 if (cp210x_write_u32_reg(port, CP210X_SET_BAUDRATE, baud)) {
1052 dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
1054 baud = old_termios->c_ospeed;
1059 tty_encode_baud_rate(tty, baud, baud);
1062 static void cp210x_set_termios(struct tty_struct *tty,
1063 struct usb_serial_port *port, struct ktermios *old_termios)
1065 struct device *dev = &port->dev;
1066 unsigned int cflag, old_cflag;
1069 cflag = tty->termios.c_cflag;
1070 old_cflag = old_termios->c_cflag;
1072 if (tty->termios.c_ospeed != old_termios->c_ospeed)
1073 cp210x_change_speed(tty, port, old_termios);
1075 /* If the number of data bits is to be updated */
1076 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
1077 cp210x_get_line_ctl(port, &bits);
1078 bits &= ~BITS_DATA_MASK;
1079 switch (cflag & CSIZE) {
1081 bits |= BITS_DATA_5;
1082 dev_dbg(dev, "%s - data bits = 5\n", __func__);
1085 bits |= BITS_DATA_6;
1086 dev_dbg(dev, "%s - data bits = 6\n", __func__);
1089 bits |= BITS_DATA_7;
1090 dev_dbg(dev, "%s - data bits = 7\n", __func__);
1093 bits |= BITS_DATA_8;
1094 dev_dbg(dev, "%s - data bits = 8\n", __func__);
1097 dev_dbg(dev, "cp210x driver does not support the number of bits requested, using 8 bit mode\n");
1098 bits |= BITS_DATA_8;
1101 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1102 dev_dbg(dev, "Number of data bits requested not supported by device\n");
1105 if ((cflag & (PARENB|PARODD|CMSPAR)) !=
1106 (old_cflag & (PARENB|PARODD|CMSPAR))) {
1107 cp210x_get_line_ctl(port, &bits);
1108 bits &= ~BITS_PARITY_MASK;
1109 if (cflag & PARENB) {
1110 if (cflag & CMSPAR) {
1111 if (cflag & PARODD) {
1112 bits |= BITS_PARITY_MARK;
1113 dev_dbg(dev, "%s - parity = MARK\n", __func__);
1115 bits |= BITS_PARITY_SPACE;
1116 dev_dbg(dev, "%s - parity = SPACE\n", __func__);
1119 if (cflag & PARODD) {
1120 bits |= BITS_PARITY_ODD;
1121 dev_dbg(dev, "%s - parity = ODD\n", __func__);
1123 bits |= BITS_PARITY_EVEN;
1124 dev_dbg(dev, "%s - parity = EVEN\n", __func__);
1128 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1129 dev_dbg(dev, "Parity mode not supported by device\n");
1132 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1133 cp210x_get_line_ctl(port, &bits);
1134 bits &= ~BITS_STOP_MASK;
1135 if (cflag & CSTOPB) {
1136 bits |= BITS_STOP_2;
1137 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
1139 bits |= BITS_STOP_1;
1140 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
1142 if (cp210x_write_u16_reg(port, CP210X_SET_LINE_CTL, bits))
1143 dev_dbg(dev, "Number of stop bits requested not supported by device\n");
1146 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
1147 struct cp210x_flow_ctl flow_ctl;
1151 cp210x_read_reg_block(port, CP210X_GET_FLOW, &flow_ctl,
1153 ctl_hs = le32_to_cpu(flow_ctl.ulControlHandshake);
1154 flow_repl = le32_to_cpu(flow_ctl.ulFlowReplace);
1155 dev_dbg(dev, "%s - read ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1156 __func__, ctl_hs, flow_repl);
1158 ctl_hs &= ~CP210X_SERIAL_DSR_HANDSHAKE;
1159 ctl_hs &= ~CP210X_SERIAL_DCD_HANDSHAKE;
1160 ctl_hs &= ~CP210X_SERIAL_DSR_SENSITIVITY;
1161 ctl_hs &= ~CP210X_SERIAL_DTR_MASK;
1162 ctl_hs |= CP210X_SERIAL_DTR_SHIFT(CP210X_SERIAL_DTR_ACTIVE);
1163 if (cflag & CRTSCTS) {
1164 ctl_hs |= CP210X_SERIAL_CTS_HANDSHAKE;
1166 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1167 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1168 CP210X_SERIAL_RTS_FLOW_CTL);
1169 dev_dbg(dev, "%s - flow control = CRTSCTS\n", __func__);
1171 ctl_hs &= ~CP210X_SERIAL_CTS_HANDSHAKE;
1173 flow_repl &= ~CP210X_SERIAL_RTS_MASK;
1174 flow_repl |= CP210X_SERIAL_RTS_SHIFT(
1175 CP210X_SERIAL_RTS_ACTIVE);
1176 dev_dbg(dev, "%s - flow control = NONE\n", __func__);
1179 dev_dbg(dev, "%s - write ulControlHandshake=0x%08x, ulFlowReplace=0x%08x\n",
1180 __func__, ctl_hs, flow_repl);
1181 flow_ctl.ulControlHandshake = cpu_to_le32(ctl_hs);
1182 flow_ctl.ulFlowReplace = cpu_to_le32(flow_repl);
1183 cp210x_write_reg_block(port, CP210X_SET_FLOW, &flow_ctl,
1189 static int cp210x_tiocmset(struct tty_struct *tty,
1190 unsigned int set, unsigned int clear)
1192 struct usb_serial_port *port = tty->driver_data;
1193 return cp210x_tiocmset_port(port, set, clear);
1196 static int cp210x_tiocmset_port(struct usb_serial_port *port,
1197 unsigned int set, unsigned int clear)
1201 if (set & TIOCM_RTS) {
1202 control |= CONTROL_RTS;
1203 control |= CONTROL_WRITE_RTS;
1205 if (set & TIOCM_DTR) {
1206 control |= CONTROL_DTR;
1207 control |= CONTROL_WRITE_DTR;
1209 if (clear & TIOCM_RTS) {
1210 control &= ~CONTROL_RTS;
1211 control |= CONTROL_WRITE_RTS;
1213 if (clear & TIOCM_DTR) {
1214 control &= ~CONTROL_DTR;
1215 control |= CONTROL_WRITE_DTR;
1218 dev_dbg(&port->dev, "%s - control = 0x%.4x\n", __func__, control);
1220 return cp210x_write_u16_reg(port, CP210X_SET_MHS, control);
1223 static void cp210x_dtr_rts(struct usb_serial_port *p, int on)
1226 cp210x_tiocmset_port(p, TIOCM_DTR|TIOCM_RTS, 0);
1228 cp210x_tiocmset_port(p, 0, TIOCM_DTR|TIOCM_RTS);
1231 static int cp210x_tiocmget(struct tty_struct *tty)
1233 struct usb_serial_port *port = tty->driver_data;
1237 result = cp210x_read_u8_reg(port, CP210X_GET_MDMSTS, &control);
1241 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
1242 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
1243 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
1244 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
1245 |((control & CONTROL_RING)? TIOCM_RI : 0)
1246 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
1248 dev_dbg(&port->dev, "%s - control = 0x%.2x\n", __func__, control);
1253 static void cp210x_break_ctl(struct tty_struct *tty, int break_state)
1255 struct usb_serial_port *port = tty->driver_data;
1258 if (break_state == 0)
1262 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1263 state == BREAK_OFF ? "off" : "on");
1264 cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
1267 static int cp210x_port_probe(struct usb_serial_port *port)
1269 struct usb_serial *serial = port->serial;
1270 struct usb_host_interface *cur_altsetting;
1271 struct cp210x_port_private *port_priv;
1274 port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1278 cur_altsetting = serial->interface->cur_altsetting;
1279 port_priv->bInterfaceNumber = cur_altsetting->desc.bInterfaceNumber;
1280 port_priv->is_cp2102n_a01 = false;
1282 usb_set_serial_port_data(port, port_priv);
1284 ret = cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST,
1285 CP210X_GET_PARTNUM, &port_priv->bPartNumber, 1);
1291 if ((port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN28) ||
1292 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN24) ||
1293 (port_priv->bPartNumber == CP210x_PARTNUM_CP2102N_QFN20)) {
1294 // check the firmware version on the CP2102N.
1296 ret = cp210x_read_vs_reg_block(port, REQTYPE_DEVICE_TO_HOST, CP2102N_GET_FW_VERS, fwversion, sizeof(fwversion));
1298 if (1 == fwversion[0] && 0 == fwversion[1] && fwversion[2] < 8) {
1299 port_priv->is_cp2102n_a01 = true;
1305 ret = cp210x_detect_swapped_line_ctl(port);
1314 static void cp210x_port_remove(struct usb_serial_port *port)
1316 struct cp210x_port_private *port_priv;
1318 port_priv = usb_get_serial_port_data(port);
1322 module_usb_serial_driver(serial_drivers, id_table);
1324 MODULE_DESCRIPTION(DRIVER_DESC);
1325 MODULE_LICENSE("GPL v2");