1 // SPDX-License-Identifier: GPL-2.0+
3 * MaxLinear/Exar USB to Serial driver
8 * Based on the initial driver written by Patong Yang:
10 * https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/usb.h>
20 #include <linux/usb/cdc.h>
21 #include <linux/usb/serial.h>
23 struct xr_txrx_clk_mask {
29 #define XR_INT_OSC_HZ 48000000U
30 #define XR21V141X_MIN_SPEED 46U
31 #define XR21V141X_MAX_SPEED XR_INT_OSC_HZ
33 /* XR21V141X register blocks */
34 #define XR21V141X_UART_REG_BLOCK 0
35 #define XR21V141X_UM_REG_BLOCK 4
36 #define XR21V141X_UART_CUSTOM_BLOCK 0x66
38 /* XR21V141X UART registers */
39 #define XR21V141X_CLOCK_DIVISOR_0 0x04
40 #define XR21V141X_CLOCK_DIVISOR_1 0x05
41 #define XR21V141X_CLOCK_DIVISOR_2 0x06
42 #define XR21V141X_TX_CLOCK_MASK_0 0x07
43 #define XR21V141X_TX_CLOCK_MASK_1 0x08
44 #define XR21V141X_RX_CLOCK_MASK_0 0x09
45 #define XR21V141X_RX_CLOCK_MASK_1 0x0a
46 #define XR21V141X_REG_FORMAT 0x0b
48 /* XR21V141X UART Manager registers */
49 #define XR21V141X_UM_FIFO_ENABLE_REG 0x10
50 #define XR21V141X_UM_ENABLE_TX_FIFO 0x01
51 #define XR21V141X_UM_ENABLE_RX_FIFO 0x02
53 #define XR21V141X_UM_RX_FIFO_RESET 0x18
54 #define XR21V141X_UM_TX_FIFO_RESET 0x1c
56 #define XR_UART_ENABLE_TX 0x1
57 #define XR_UART_ENABLE_RX 0x2
59 #define XR_GPIO_RI BIT(0)
60 #define XR_GPIO_CD BIT(1)
61 #define XR_GPIO_DSR BIT(2)
62 #define XR_GPIO_DTR BIT(3)
63 #define XR_GPIO_CTS BIT(4)
64 #define XR_GPIO_RTS BIT(5)
65 #define XR_GPIO_CLK BIT(6)
66 #define XR_GPIO_XEN BIT(7)
67 #define XR_GPIO_TXT BIT(8)
68 #define XR_GPIO_RXT BIT(9)
70 #define XR_UART_DATA_MASK GENMASK(3, 0)
71 #define XR_UART_DATA_7 0x7
72 #define XR_UART_DATA_8 0x8
74 #define XR_UART_PARITY_MASK GENMASK(6, 4)
75 #define XR_UART_PARITY_SHIFT 4
76 #define XR_UART_PARITY_NONE (0x0 << XR_UART_PARITY_SHIFT)
77 #define XR_UART_PARITY_ODD (0x1 << XR_UART_PARITY_SHIFT)
78 #define XR_UART_PARITY_EVEN (0x2 << XR_UART_PARITY_SHIFT)
79 #define XR_UART_PARITY_MARK (0x3 << XR_UART_PARITY_SHIFT)
80 #define XR_UART_PARITY_SPACE (0x4 << XR_UART_PARITY_SHIFT)
82 #define XR_UART_STOP_MASK BIT(7)
83 #define XR_UART_STOP_SHIFT 7
84 #define XR_UART_STOP_1 (0x0 << XR_UART_STOP_SHIFT)
85 #define XR_UART_STOP_2 (0x1 << XR_UART_STOP_SHIFT)
87 #define XR_UART_FLOW_MODE_NONE 0x0
88 #define XR_UART_FLOW_MODE_HW 0x1
89 #define XR_UART_FLOW_MODE_SW 0x2
91 #define XR_GPIO_MODE_SEL_MASK GENMASK(2, 0)
92 #define XR_GPIO_MODE_SEL_RTS_CTS 0x1
93 #define XR_GPIO_MODE_SEL_DTR_DSR 0x2
94 #define XR_GPIO_MODE_SEL_RS485 0x3
95 #define XR_GPIO_MODE_SEL_RS485_ADDR 0x4
96 #define XR_GPIO_MODE_TX_TOGGLE 0x100
97 #define XR_GPIO_MODE_RX_TOGGLE 0x200
99 #define XR_FIFO_RESET 0x1
101 #define XR_CUSTOM_DRIVER_ACTIVE 0x1
103 static int xr21v141x_uart_enable(struct usb_serial_port *port);
104 static int xr21v141x_uart_disable(struct usb_serial_port *port);
105 static int xr21v141x_fifo_reset(struct usb_serial_port *port);
106 static void xr21v141x_set_line_settings(struct tty_struct *tty,
107 struct usb_serial_port *port, struct ktermios *old_termios);
129 bool have_5_6_bit_mode;
130 bool have_xmit_toggle;
132 int (*enable)(struct usb_serial_port *port);
133 int (*disable)(struct usb_serial_port *port);
134 int (*fifo_reset)(struct usb_serial_port *port);
135 void (*set_line_settings)(struct tty_struct *tty,
136 struct usb_serial_port *port,
137 struct ktermios *old_termios);
148 static const struct xr_type xr_types[] = {
151 .reg_recipient = USB_RECIP_DEVICE,
156 .flow_control = 0x0c,
161 .gpio_direction = 0x1b,
166 .enable = xr21v141x_uart_enable,
167 .disable = xr21v141x_uart_disable,
168 .fifo_reset = xr21v141x_fifo_reset,
169 .set_line_settings = xr21v141x_set_line_settings,
173 .reg_recipient = USB_RECIP_INTERFACE,
178 .flow_control = 0x06,
183 .gpio_direction = 0x0d,
187 .tx_fifo_reset = 0x40,
188 .rx_fifo_reset = 0x43,
189 .custom_driver = 0x60,
191 .have_5_6_bit_mode = true,
192 .have_xmit_toggle = true,
196 .reg_recipient = USB_RECIP_DEVICE,
200 .uart_enable = 0xc00,
201 .flow_control = 0xc06,
206 .gpio_direction = 0xc0d,
209 .gpio_status = 0xc10,
210 .tx_fifo_reset = 0xc80,
211 .rx_fifo_reset = 0xcc0,
212 .custom_driver = 0x20d,
216 .reg_recipient = USB_RECIP_DEVICE,
221 .flow_control = 0x46,
226 .gpio_direction = 0x4d,
230 .tx_fifo_reset = 0x60,
231 .rx_fifo_reset = 0x63,
232 .custom_driver = 0x81,
237 const struct xr_type *type;
238 u8 channel; /* zero-based index or interface number */
241 static int xr_set_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 val)
243 struct xr_data *data = usb_get_serial_port_data(port);
244 const struct xr_type *type = data->type;
245 struct usb_serial *serial = port->serial;
248 ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
250 USB_DIR_OUT | USB_TYPE_VENDOR | type->reg_recipient,
251 val, (channel << 8) | reg, NULL, 0,
252 USB_CTRL_SET_TIMEOUT);
254 dev_err(&port->dev, "Failed to set reg 0x%02x: %d\n", reg, ret);
261 static int xr_get_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 *val)
263 struct xr_data *data = usb_get_serial_port_data(port);
264 const struct xr_type *type = data->type;
265 struct usb_serial *serial = port->serial;
269 if (type->reg_width == 8)
274 dmabuf = kmalloc(len, GFP_KERNEL);
278 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
280 USB_DIR_IN | USB_TYPE_VENDOR | type->reg_recipient,
281 0, (channel << 8) | reg, dmabuf, len,
282 USB_CTRL_GET_TIMEOUT);
285 *val = le16_to_cpup((__le16 *)dmabuf);
290 dev_err(&port->dev, "Failed to get reg 0x%02x: %d\n", reg, ret);
300 static int xr_set_reg_uart(struct usb_serial_port *port, u16 reg, u16 val)
302 struct xr_data *data = usb_get_serial_port_data(port);
304 return xr_set_reg(port, data->channel, reg, val);
307 static int xr_get_reg_uart(struct usb_serial_port *port, u16 reg, u16 *val)
309 struct xr_data *data = usb_get_serial_port_data(port);
311 return xr_get_reg(port, data->channel, reg, val);
314 static int xr_set_reg_um(struct usb_serial_port *port, u8 reg_base, u8 val)
316 struct xr_data *data = usb_get_serial_port_data(port);
319 reg = reg_base + data->channel;
321 return xr_set_reg(port, XR21V141X_UM_REG_BLOCK, reg, val);
324 static int __xr_uart_enable(struct usb_serial_port *port)
326 struct xr_data *data = usb_get_serial_port_data(port);
328 return xr_set_reg_uart(port, data->type->uart_enable,
329 XR_UART_ENABLE_TX | XR_UART_ENABLE_RX);
332 static int __xr_uart_disable(struct usb_serial_port *port)
334 struct xr_data *data = usb_get_serial_port_data(port);
336 return xr_set_reg_uart(port, data->type->uart_enable, 0);
340 * According to datasheet, below is the recommended sequence for enabling UART
341 * module in XR21V141X:
347 static int xr21v141x_uart_enable(struct usb_serial_port *port)
351 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
352 XR21V141X_UM_ENABLE_TX_FIFO);
356 ret = __xr_uart_enable(port);
360 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
361 XR21V141X_UM_ENABLE_TX_FIFO | XR21V141X_UM_ENABLE_RX_FIFO);
363 __xr_uart_disable(port);
368 static int xr21v141x_uart_disable(struct usb_serial_port *port)
372 ret = __xr_uart_disable(port);
376 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, 0);
381 static int xr_uart_enable(struct usb_serial_port *port)
383 struct xr_data *data = usb_get_serial_port_data(port);
385 if (data->type->enable)
386 return data->type->enable(port);
388 return __xr_uart_enable(port);
391 static int xr_uart_disable(struct usb_serial_port *port)
393 struct xr_data *data = usb_get_serial_port_data(port);
395 if (data->type->disable)
396 return data->type->disable(port);
398 return __xr_uart_disable(port);
401 static int xr21v141x_fifo_reset(struct usb_serial_port *port)
405 ret = xr_set_reg_um(port, XR21V141X_UM_TX_FIFO_RESET, XR_FIFO_RESET);
409 ret = xr_set_reg_um(port, XR21V141X_UM_RX_FIFO_RESET, XR_FIFO_RESET);
416 static int xr_fifo_reset(struct usb_serial_port *port)
418 struct xr_data *data = usb_get_serial_port_data(port);
421 if (data->type->fifo_reset)
422 return data->type->fifo_reset(port);
424 ret = xr_set_reg_uart(port, data->type->tx_fifo_reset, XR_FIFO_RESET);
428 ret = xr_set_reg_uart(port, data->type->rx_fifo_reset, XR_FIFO_RESET);
435 static int xr_tiocmget(struct tty_struct *tty)
437 struct usb_serial_port *port = tty->driver_data;
438 struct xr_data *data = usb_get_serial_port_data(port);
442 ret = xr_get_reg_uart(port, data->type->gpio_status, &status);
447 * Modem control pins are active low, so reading '0' means it is active
448 * and '1' means not active.
450 ret = ((status & XR_GPIO_DTR) ? 0 : TIOCM_DTR) |
451 ((status & XR_GPIO_RTS) ? 0 : TIOCM_RTS) |
452 ((status & XR_GPIO_CTS) ? 0 : TIOCM_CTS) |
453 ((status & XR_GPIO_DSR) ? 0 : TIOCM_DSR) |
454 ((status & XR_GPIO_RI) ? 0 : TIOCM_RI) |
455 ((status & XR_GPIO_CD) ? 0 : TIOCM_CD);
460 static int xr_tiocmset_port(struct usb_serial_port *port,
461 unsigned int set, unsigned int clear)
463 struct xr_data *data = usb_get_serial_port_data(port);
464 const struct xr_type *type = data->type;
469 /* Modem control pins are active low, so set & clr are swapped */
471 gpio_clr |= XR_GPIO_RTS;
473 gpio_clr |= XR_GPIO_DTR;
474 if (clear & TIOCM_RTS)
475 gpio_set |= XR_GPIO_RTS;
476 if (clear & TIOCM_DTR)
477 gpio_set |= XR_GPIO_DTR;
479 /* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */
481 ret = xr_set_reg_uart(port, type->gpio_clear, gpio_clr);
484 ret = xr_set_reg_uart(port, type->gpio_set, gpio_set);
489 static int xr_tiocmset(struct tty_struct *tty,
490 unsigned int set, unsigned int clear)
492 struct usb_serial_port *port = tty->driver_data;
494 return xr_tiocmset_port(port, set, clear);
497 static void xr_dtr_rts(struct usb_serial_port *port, int on)
500 xr_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0);
502 xr_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS);
505 static void xr_break_ctl(struct tty_struct *tty, int break_state)
507 struct usb_serial_port *port = tty->driver_data;
508 struct xr_data *data = usb_get_serial_port_data(port);
509 const struct xr_type *type = data->type;
512 if (break_state == 0)
515 state = GENMASK(type->reg_width - 1, 0);
517 dev_dbg(&port->dev, "Turning break %s\n", state == 0 ? "off" : "on");
519 xr_set_reg_uart(port, type->tx_break, state);
522 /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */
523 static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks[] = {
524 { 0x000, 0x000, 0x000 },
525 { 0x000, 0x000, 0x000 },
526 { 0x100, 0x000, 0x100 },
527 { 0x020, 0x400, 0x020 },
528 { 0x010, 0x100, 0x010 },
529 { 0x208, 0x040, 0x208 },
530 { 0x104, 0x820, 0x108 },
531 { 0x844, 0x210, 0x884 },
532 { 0x444, 0x110, 0x444 },
533 { 0x122, 0x888, 0x224 },
534 { 0x912, 0x448, 0x924 },
535 { 0x492, 0x248, 0x492 },
536 { 0x252, 0x928, 0x292 },
537 { 0x94a, 0x4a4, 0xa52 },
538 { 0x52a, 0xaa4, 0x54a },
539 { 0xaaa, 0x954, 0x4aa },
540 { 0xaaa, 0x554, 0xaaa },
541 { 0x555, 0xad4, 0x5aa },
542 { 0xb55, 0xab4, 0x55a },
543 { 0x6b5, 0x5ac, 0xb56 },
544 { 0x5b5, 0xd6c, 0x6d6 },
545 { 0xb6d, 0xb6a, 0xdb6 },
546 { 0x76d, 0x6da, 0xbb6 },
547 { 0xedd, 0xdda, 0x76e },
548 { 0xddd, 0xbba, 0xeee },
549 { 0x7bb, 0xf7a, 0xdde },
550 { 0xf7b, 0xef6, 0x7de },
551 { 0xdf7, 0xbf6, 0xf7e },
552 { 0x7f7, 0xfee, 0xefe },
553 { 0xfdf, 0xfbe, 0x7fe },
554 { 0xf7f, 0xefe, 0xffe },
555 { 0xfff, 0xffe, 0xffd },
558 static int xr21v141x_set_baudrate(struct tty_struct *tty, struct usb_serial_port *port)
560 u32 divisor, baud, idx;
561 u16 tx_mask, rx_mask;
564 baud = tty->termios.c_ospeed;
568 baud = clamp(baud, XR21V141X_MIN_SPEED, XR21V141X_MAX_SPEED);
569 divisor = XR_INT_OSC_HZ / baud;
570 idx = ((32 * XR_INT_OSC_HZ) / baud) & 0x1f;
571 tx_mask = xr21v141x_txrx_clk_masks[idx].tx;
574 rx_mask = xr21v141x_txrx_clk_masks[idx].rx1;
576 rx_mask = xr21v141x_txrx_clk_masks[idx].rx0;
578 dev_dbg(&port->dev, "Setting baud rate: %u\n", baud);
580 * XR21V141X uses fractional baud rate generator with 48MHz internal
581 * oscillator and 19-bit programmable divisor. So theoretically it can
582 * generate most commonly used baud rates with high accuracy.
584 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_0,
589 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_1,
590 (divisor >> 8) & 0xff);
594 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_2,
595 (divisor >> 16) & 0xff);
599 ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_0,
604 ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_1,
605 (tx_mask >> 8) & 0xff);
609 ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_0,
614 ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_1,
615 (rx_mask >> 8) & 0xff);
619 tty_encode_baud_rate(tty, baud, baud);
624 static void xr_set_flow_mode(struct tty_struct *tty,
625 struct usb_serial_port *port,
626 struct ktermios *old_termios)
628 struct xr_data *data = usb_get_serial_port_data(port);
629 const struct xr_type *type = data->type;
633 ret = xr_get_reg_uart(port, type->gpio_mode, &gpio_mode);
638 * According to the datasheets, the UART needs to be disabled while
639 * writing to the FLOW_CONTROL register (XR21V141X), or any register
640 * but GPIO_SET, GPIO_CLEAR, TX_BREAK and ERROR_STATUS (XR21B142X).
642 xr_uart_disable(port);
644 /* Set GPIO mode for controlling the pins manually by default. */
645 gpio_mode &= ~XR_GPIO_MODE_SEL_MASK;
647 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0) {
648 dev_dbg(&port->dev, "Enabling hardware flow ctrl\n");
649 gpio_mode |= XR_GPIO_MODE_SEL_RTS_CTS;
650 flow = XR_UART_FLOW_MODE_HW;
651 } else if (I_IXON(tty)) {
652 u8 start_char = START_CHAR(tty);
653 u8 stop_char = STOP_CHAR(tty);
655 dev_dbg(&port->dev, "Enabling sw flow ctrl\n");
656 flow = XR_UART_FLOW_MODE_SW;
658 xr_set_reg_uart(port, type->xon_char, start_char);
659 xr_set_reg_uart(port, type->xoff_char, stop_char);
661 dev_dbg(&port->dev, "Disabling flow ctrl\n");
662 flow = XR_UART_FLOW_MODE_NONE;
665 xr_set_reg_uart(port, type->flow_control, flow);
666 xr_set_reg_uart(port, type->gpio_mode, gpio_mode);
668 xr_uart_enable(port);
670 if (C_BAUD(tty) == B0)
672 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
676 static void xr21v141x_set_line_settings(struct tty_struct *tty,
677 struct usb_serial_port *port, struct ktermios *old_termios)
679 struct ktermios *termios = &tty->termios;
683 if (!old_termios || (tty->termios.c_ospeed != old_termios->c_ospeed))
684 xr21v141x_set_baudrate(tty, port);
686 switch (C_CSIZE(tty)) {
689 /* CS5 and CS6 are not supported, so just restore old setting */
690 termios->c_cflag &= ~CSIZE;
692 termios->c_cflag |= old_termios->c_cflag & CSIZE;
694 termios->c_cflag |= CS8;
696 if (C_CSIZE(tty) == CS7)
697 bits |= XR_UART_DATA_7;
699 bits |= XR_UART_DATA_8;
702 bits |= XR_UART_DATA_7;
706 bits |= XR_UART_DATA_8;
713 bits |= XR_UART_PARITY_MARK;
715 bits |= XR_UART_PARITY_SPACE;
718 bits |= XR_UART_PARITY_ODD;
720 bits |= XR_UART_PARITY_EVEN;
725 bits |= XR_UART_STOP_2;
727 bits |= XR_UART_STOP_1;
729 ret = xr_set_reg_uart(port, XR21V141X_REG_FORMAT, bits);
734 static void xr_cdc_set_line_coding(struct tty_struct *tty,
735 struct usb_serial_port *port, struct ktermios *old_termios)
737 struct xr_data *data = usb_get_serial_port_data(port);
738 struct usb_host_interface *alt = port->serial->interface->cur_altsetting;
739 struct usb_device *udev = port->serial->dev;
740 struct usb_cdc_line_coding *lc;
743 lc = kzalloc(sizeof(*lc), GFP_KERNEL);
747 if (tty->termios.c_ospeed)
748 lc->dwDTERate = cpu_to_le32(tty->termios.c_ospeed);
749 else if (old_termios)
750 lc->dwDTERate = cpu_to_le32(old_termios->c_ospeed);
752 lc->dwDTERate = cpu_to_le32(9600);
755 lc->bCharFormat = USB_CDC_2_STOP_BITS;
757 lc->bCharFormat = USB_CDC_1_STOP_BITS;
762 lc->bParityType = USB_CDC_MARK_PARITY;
764 lc->bParityType = USB_CDC_SPACE_PARITY;
767 lc->bParityType = USB_CDC_ODD_PARITY;
769 lc->bParityType = USB_CDC_EVEN_PARITY;
772 lc->bParityType = USB_CDC_NO_PARITY;
775 if (!data->type->have_5_6_bit_mode &&
776 (C_CSIZE(tty) == CS5 || C_CSIZE(tty) == CS6)) {
777 tty->termios.c_cflag &= ~CSIZE;
779 tty->termios.c_cflag |= old_termios->c_cflag & CSIZE;
781 tty->termios.c_cflag |= CS8;
784 switch (C_CSIZE(tty)) {
800 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
801 USB_CDC_REQ_SET_LINE_CODING,
802 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
803 0, alt->desc.bInterfaceNumber,
804 lc, sizeof(*lc), USB_CTRL_SET_TIMEOUT);
806 dev_err(&port->dev, "Failed to set line coding: %d\n", ret);
811 static void xr_set_termios(struct tty_struct *tty,
812 struct usb_serial_port *port, struct ktermios *old_termios)
814 struct xr_data *data = usb_get_serial_port_data(port);
817 * XR21V141X does not have a CUSTOM_DRIVER flag and always enters CDC
818 * mode upon receiving CDC requests.
820 if (data->type->set_line_settings)
821 data->type->set_line_settings(tty, port, old_termios);
823 xr_cdc_set_line_coding(tty, port, old_termios);
825 xr_set_flow_mode(tty, port, old_termios);
828 static int xr_open(struct tty_struct *tty, struct usb_serial_port *port)
832 ret = xr_fifo_reset(port);
836 ret = xr_uart_enable(port);
838 dev_err(&port->dev, "Failed to enable UART\n");
844 xr_set_termios(tty, port, NULL);
846 ret = usb_serial_generic_open(tty, port);
848 xr_uart_disable(port);
855 static void xr_close(struct usb_serial_port *port)
857 usb_serial_generic_close(port);
859 xr_uart_disable(port);
862 static int xr_probe(struct usb_serial *serial, const struct usb_device_id *id)
864 struct usb_interface *control = serial->interface;
865 struct usb_host_interface *alt = control->cur_altsetting;
866 struct usb_cdc_parsed_header hdrs;
867 struct usb_cdc_union_desc *desc;
868 struct usb_interface *data;
871 ret = cdc_parse_cdc_header(&hdrs, control, alt->extra, alt->extralen);
875 desc = hdrs.usb_cdc_union_desc;
879 data = usb_ifnum_to_if(serial->dev, desc->bSlaveInterface0);
883 ret = usb_serial_claim_interface(serial, data);
887 usb_set_serial_data(serial, (void *)id->driver_info);
892 static int xr_gpio_init(struct usb_serial_port *port, const struct xr_type *type)
898 * Configure all pins as GPIO except for Receive and Transmit Toggle.
901 if (type->have_xmit_toggle)
902 mode |= XR_GPIO_MODE_RX_TOGGLE | XR_GPIO_MODE_TX_TOGGLE;
904 ret = xr_set_reg_uart(port, type->gpio_mode, mode);
909 * Configure DTR and RTS as outputs and make sure they are deasserted
910 * (active low), and configure RI, CD, DSR and CTS as inputs.
912 mask = XR_GPIO_DTR | XR_GPIO_RTS;
913 ret = xr_set_reg_uart(port, type->gpio_direction, mask);
917 ret = xr_set_reg_uart(port, type->gpio_set, mask);
924 static int xr_port_probe(struct usb_serial_port *port)
926 struct usb_interface_descriptor *desc;
927 const struct xr_type *type;
928 struct xr_data *data;
929 enum xr_type_id type_id;
932 type_id = (int)(unsigned long)usb_get_serial_data(port->serial);
933 type = &xr_types[type_id];
935 data = kzalloc(sizeof(*data), GFP_KERNEL);
941 desc = &port->serial->interface->cur_altsetting->desc;
942 if (type_id == XR21V141X)
943 data->channel = desc->bInterfaceNumber / 2;
945 data->channel = desc->bInterfaceNumber;
947 usb_set_serial_port_data(port, data);
949 if (type->custom_driver) {
950 ret = xr_set_reg_uart(port, type->custom_driver,
951 XR_CUSTOM_DRIVER_ACTIVE);
956 ret = xr_gpio_init(port, type);
968 static void xr_port_remove(struct usb_serial_port *port)
970 struct xr_data *data = usb_get_serial_port_data(port);
975 #define XR_DEVICE(vid, pid, type) \
976 USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_COMM), \
977 .driver_info = (type)
979 static const struct usb_device_id id_table[] = {
980 { XR_DEVICE(0x04e2, 0x1400, XR2280X) },
981 { XR_DEVICE(0x04e2, 0x1401, XR2280X) },
982 { XR_DEVICE(0x04e2, 0x1402, XR2280X) },
983 { XR_DEVICE(0x04e2, 0x1403, XR2280X) },
984 { XR_DEVICE(0x04e2, 0x1410, XR21V141X) },
985 { XR_DEVICE(0x04e2, 0x1411, XR21B1411) },
986 { XR_DEVICE(0x04e2, 0x1412, XR21V141X) },
987 { XR_DEVICE(0x04e2, 0x1414, XR21V141X) },
988 { XR_DEVICE(0x04e2, 0x1420, XR21B142X) },
989 { XR_DEVICE(0x04e2, 0x1422, XR21B142X) },
990 { XR_DEVICE(0x04e2, 0x1424, XR21B142X) },
993 MODULE_DEVICE_TABLE(usb, id_table);
995 static struct usb_serial_driver xr_device = {
997 .owner = THIS_MODULE,
1000 .id_table = id_table,
1003 .port_probe = xr_port_probe,
1004 .port_remove = xr_port_remove,
1007 .break_ctl = xr_break_ctl,
1008 .set_termios = xr_set_termios,
1009 .tiocmget = xr_tiocmget,
1010 .tiocmset = xr_tiocmset,
1011 .dtr_rts = xr_dtr_rts
1014 static struct usb_serial_driver * const serial_drivers[] = {
1018 module_usb_serial_driver(serial_drivers, id_table);
1021 MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver");
1022 MODULE_LICENSE("GPL");