1 // SPDX-License-Identifier: GPL-2.0+
3 * MaxLinear/Exar USB to Serial driver
5 * Copyright (c) 2020 Manivannan Sadhasivam <mani@kernel.org>
6 * Copyright (c) 2021 Johan Hovold <johan@kernel.org>
8 * Based on the initial driver written by Patong Yang:
10 * https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop
12 * Copyright (c) 2018 Patong Yang <patong.mxl@gmail.com>
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_RS485_TX_H 0x8
97 #define XR_GPIO_MODE_TX_TOGGLE 0x100
98 #define XR_GPIO_MODE_RX_TOGGLE 0x200
100 #define XR_FIFO_RESET 0x1
102 #define XR_CUSTOM_DRIVER_ACTIVE 0x1
104 static int xr21v141x_uart_enable(struct usb_serial_port
*port
);
105 static int xr21v141x_uart_disable(struct usb_serial_port
*port
);
106 static int xr21v141x_fifo_reset(struct usb_serial_port
*port
);
107 static void xr21v141x_set_line_settings(struct tty_struct
*tty
,
108 struct usb_serial_port
*port
,
109 const struct ktermios
*old_termios
);
131 bool have_5_6_bit_mode
;
132 bool have_xmit_toggle
;
134 int (*enable
)(struct usb_serial_port
*port
);
135 int (*disable
)(struct usb_serial_port
*port
);
136 int (*fifo_reset
)(struct usb_serial_port
*port
);
137 void (*set_line_settings
)(struct tty_struct
*tty
,
138 struct usb_serial_port
*port
,
139 const struct ktermios
*old_termios
);
150 static const struct xr_type xr_types
[] = {
153 .reg_recipient
= USB_RECIP_DEVICE
,
158 .flow_control
= 0x0c,
163 .gpio_direction
= 0x1b,
168 .enable
= xr21v141x_uart_enable
,
169 .disable
= xr21v141x_uart_disable
,
170 .fifo_reset
= xr21v141x_fifo_reset
,
171 .set_line_settings
= xr21v141x_set_line_settings
,
175 .reg_recipient
= USB_RECIP_INTERFACE
,
180 .flow_control
= 0x06,
185 .gpio_direction
= 0x0d,
189 .tx_fifo_reset
= 0x40,
190 .rx_fifo_reset
= 0x43,
191 .custom_driver
= 0x60,
193 .have_5_6_bit_mode
= true,
194 .have_xmit_toggle
= true,
198 .reg_recipient
= USB_RECIP_DEVICE
,
202 .uart_enable
= 0xc00,
203 .flow_control
= 0xc06,
208 .gpio_direction
= 0xc0d,
211 .gpio_status
= 0xc10,
212 .tx_fifo_reset
= 0xc80,
213 .rx_fifo_reset
= 0xcc0,
214 .custom_driver
= 0x20d,
218 .reg_recipient
= USB_RECIP_DEVICE
,
223 .flow_control
= 0x46,
228 .gpio_direction
= 0x4d,
232 .tx_fifo_reset
= 0x60,
233 .rx_fifo_reset
= 0x63,
234 .custom_driver
= 0x81,
239 const struct xr_type
*type
;
240 u8 channel
; /* zero-based index or interface number */
241 struct serial_rs485 rs485
;
244 static int xr_set_reg(struct usb_serial_port
*port
, u8 channel
, u16 reg
, u16 val
)
246 struct xr_data
*data
= usb_get_serial_port_data(port
);
247 const struct xr_type
*type
= data
->type
;
248 struct usb_serial
*serial
= port
->serial
;
251 ret
= usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
253 USB_DIR_OUT
| USB_TYPE_VENDOR
| type
->reg_recipient
,
254 val
, (channel
<< 8) | reg
, NULL
, 0,
255 USB_CTRL_SET_TIMEOUT
);
257 dev_err(&port
->dev
, "Failed to set reg 0x%02x: %d\n", reg
, ret
);
264 static int xr_get_reg(struct usb_serial_port
*port
, u8 channel
, u16 reg
, u16
*val
)
266 struct xr_data
*data
= usb_get_serial_port_data(port
);
267 const struct xr_type
*type
= data
->type
;
268 struct usb_serial
*serial
= port
->serial
;
272 if (type
->reg_width
== 8)
277 dmabuf
= kmalloc(len
, GFP_KERNEL
);
281 ret
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
283 USB_DIR_IN
| USB_TYPE_VENDOR
| type
->reg_recipient
,
284 0, (channel
<< 8) | reg
, dmabuf
, len
,
285 USB_CTRL_GET_TIMEOUT
);
288 *val
= le16_to_cpup((__le16
*)dmabuf
);
293 dev_err(&port
->dev
, "Failed to get reg 0x%02x: %d\n", reg
, ret
);
303 static int xr_set_reg_uart(struct usb_serial_port
*port
, u16 reg
, u16 val
)
305 struct xr_data
*data
= usb_get_serial_port_data(port
);
307 return xr_set_reg(port
, data
->channel
, reg
, val
);
310 static int xr_get_reg_uart(struct usb_serial_port
*port
, u16 reg
, u16
*val
)
312 struct xr_data
*data
= usb_get_serial_port_data(port
);
314 return xr_get_reg(port
, data
->channel
, reg
, val
);
317 static int xr_set_reg_um(struct usb_serial_port
*port
, u8 reg_base
, u8 val
)
319 struct xr_data
*data
= usb_get_serial_port_data(port
);
322 reg
= reg_base
+ data
->channel
;
324 return xr_set_reg(port
, XR21V141X_UM_REG_BLOCK
, reg
, val
);
327 static int __xr_uart_enable(struct usb_serial_port
*port
)
329 struct xr_data
*data
= usb_get_serial_port_data(port
);
331 return xr_set_reg_uart(port
, data
->type
->uart_enable
,
332 XR_UART_ENABLE_TX
| XR_UART_ENABLE_RX
);
335 static int __xr_uart_disable(struct usb_serial_port
*port
)
337 struct xr_data
*data
= usb_get_serial_port_data(port
);
339 return xr_set_reg_uart(port
, data
->type
->uart_enable
, 0);
343 * According to datasheet, below is the recommended sequence for enabling UART
344 * module in XR21V141X:
350 static int xr21v141x_uart_enable(struct usb_serial_port
*port
)
354 ret
= xr_set_reg_um(port
, XR21V141X_UM_FIFO_ENABLE_REG
,
355 XR21V141X_UM_ENABLE_TX_FIFO
);
359 ret
= __xr_uart_enable(port
);
363 ret
= xr_set_reg_um(port
, XR21V141X_UM_FIFO_ENABLE_REG
,
364 XR21V141X_UM_ENABLE_TX_FIFO
| XR21V141X_UM_ENABLE_RX_FIFO
);
366 __xr_uart_disable(port
);
371 static int xr21v141x_uart_disable(struct usb_serial_port
*port
)
375 ret
= __xr_uart_disable(port
);
379 ret
= xr_set_reg_um(port
, XR21V141X_UM_FIFO_ENABLE_REG
, 0);
384 static int xr_uart_enable(struct usb_serial_port
*port
)
386 struct xr_data
*data
= usb_get_serial_port_data(port
);
388 if (data
->type
->enable
)
389 return data
->type
->enable(port
);
391 return __xr_uart_enable(port
);
394 static int xr_uart_disable(struct usb_serial_port
*port
)
396 struct xr_data
*data
= usb_get_serial_port_data(port
);
398 if (data
->type
->disable
)
399 return data
->type
->disable(port
);
401 return __xr_uart_disable(port
);
404 static int xr21v141x_fifo_reset(struct usb_serial_port
*port
)
408 ret
= xr_set_reg_um(port
, XR21V141X_UM_TX_FIFO_RESET
, XR_FIFO_RESET
);
412 ret
= xr_set_reg_um(port
, XR21V141X_UM_RX_FIFO_RESET
, XR_FIFO_RESET
);
419 static int xr_fifo_reset(struct usb_serial_port
*port
)
421 struct xr_data
*data
= usb_get_serial_port_data(port
);
424 if (data
->type
->fifo_reset
)
425 return data
->type
->fifo_reset(port
);
427 ret
= xr_set_reg_uart(port
, data
->type
->tx_fifo_reset
, XR_FIFO_RESET
);
431 ret
= xr_set_reg_uart(port
, data
->type
->rx_fifo_reset
, XR_FIFO_RESET
);
438 static int xr_tiocmget(struct tty_struct
*tty
)
440 struct usb_serial_port
*port
= tty
->driver_data
;
441 struct xr_data
*data
= usb_get_serial_port_data(port
);
445 ret
= xr_get_reg_uart(port
, data
->type
->gpio_status
, &status
);
450 * Modem control pins are active low, so reading '0' means it is active
451 * and '1' means not active.
453 ret
= ((status
& XR_GPIO_DTR
) ? 0 : TIOCM_DTR
) |
454 ((status
& XR_GPIO_RTS
) ? 0 : TIOCM_RTS
) |
455 ((status
& XR_GPIO_CTS
) ? 0 : TIOCM_CTS
) |
456 ((status
& XR_GPIO_DSR
) ? 0 : TIOCM_DSR
) |
457 ((status
& XR_GPIO_RI
) ? 0 : TIOCM_RI
) |
458 ((status
& XR_GPIO_CD
) ? 0 : TIOCM_CD
);
463 static int xr_tiocmset_port(struct usb_serial_port
*port
,
464 unsigned int set
, unsigned int clear
)
466 struct xr_data
*data
= usb_get_serial_port_data(port
);
467 const struct xr_type
*type
= data
->type
;
472 /* Modem control pins are active low, so set & clr are swapped */
474 gpio_clr
|= XR_GPIO_RTS
;
476 gpio_clr
|= XR_GPIO_DTR
;
477 if (clear
& TIOCM_RTS
)
478 gpio_set
|= XR_GPIO_RTS
;
479 if (clear
& TIOCM_DTR
)
480 gpio_set
|= XR_GPIO_DTR
;
482 /* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */
484 ret
= xr_set_reg_uart(port
, type
->gpio_clear
, gpio_clr
);
487 ret
= xr_set_reg_uart(port
, type
->gpio_set
, gpio_set
);
492 static int xr_tiocmset(struct tty_struct
*tty
,
493 unsigned int set
, unsigned int clear
)
495 struct usb_serial_port
*port
= tty
->driver_data
;
497 return xr_tiocmset_port(port
, set
, clear
);
500 static void xr_dtr_rts(struct usb_serial_port
*port
, int on
)
503 xr_tiocmset_port(port
, TIOCM_DTR
| TIOCM_RTS
, 0);
505 xr_tiocmset_port(port
, 0, TIOCM_DTR
| TIOCM_RTS
);
508 static int xr_break_ctl(struct tty_struct
*tty
, int break_state
)
510 struct usb_serial_port
*port
= tty
->driver_data
;
511 struct xr_data
*data
= usb_get_serial_port_data(port
);
512 const struct xr_type
*type
= data
->type
;
515 if (break_state
== 0)
518 state
= GENMASK(type
->reg_width
- 1, 0);
520 dev_dbg(&port
->dev
, "Turning break %s\n", state
== 0 ? "off" : "on");
522 return xr_set_reg_uart(port
, type
->tx_break
, state
);
525 /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */
526 static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks
[] = {
527 { 0x000, 0x000, 0x000 },
528 { 0x000, 0x000, 0x000 },
529 { 0x100, 0x000, 0x100 },
530 { 0x020, 0x400, 0x020 },
531 { 0x010, 0x100, 0x010 },
532 { 0x208, 0x040, 0x208 },
533 { 0x104, 0x820, 0x108 },
534 { 0x844, 0x210, 0x884 },
535 { 0x444, 0x110, 0x444 },
536 { 0x122, 0x888, 0x224 },
537 { 0x912, 0x448, 0x924 },
538 { 0x492, 0x248, 0x492 },
539 { 0x252, 0x928, 0x292 },
540 { 0x94a, 0x4a4, 0xa52 },
541 { 0x52a, 0xaa4, 0x54a },
542 { 0xaaa, 0x954, 0x4aa },
543 { 0xaaa, 0x554, 0xaaa },
544 { 0x555, 0xad4, 0x5aa },
545 { 0xb55, 0xab4, 0x55a },
546 { 0x6b5, 0x5ac, 0xb56 },
547 { 0x5b5, 0xd6c, 0x6d6 },
548 { 0xb6d, 0xb6a, 0xdb6 },
549 { 0x76d, 0x6da, 0xbb6 },
550 { 0xedd, 0xdda, 0x76e },
551 { 0xddd, 0xbba, 0xeee },
552 { 0x7bb, 0xf7a, 0xdde },
553 { 0xf7b, 0xef6, 0x7de },
554 { 0xdf7, 0xbf6, 0xf7e },
555 { 0x7f7, 0xfee, 0xefe },
556 { 0xfdf, 0xfbe, 0x7fe },
557 { 0xf7f, 0xefe, 0xffe },
558 { 0xfff, 0xffe, 0xffd },
561 static int xr21v141x_set_baudrate(struct tty_struct
*tty
, struct usb_serial_port
*port
)
563 u32 divisor
, baud
, idx
;
564 u16 tx_mask
, rx_mask
;
567 baud
= tty
->termios
.c_ospeed
;
571 baud
= clamp(baud
, XR21V141X_MIN_SPEED
, XR21V141X_MAX_SPEED
);
572 divisor
= XR_INT_OSC_HZ
/ baud
;
573 idx
= ((32 * XR_INT_OSC_HZ
) / baud
) & 0x1f;
574 tx_mask
= xr21v141x_txrx_clk_masks
[idx
].tx
;
577 rx_mask
= xr21v141x_txrx_clk_masks
[idx
].rx1
;
579 rx_mask
= xr21v141x_txrx_clk_masks
[idx
].rx0
;
581 dev_dbg(&port
->dev
, "Setting baud rate: %u\n", baud
);
583 * XR21V141X uses fractional baud rate generator with 48MHz internal
584 * oscillator and 19-bit programmable divisor. So theoretically it can
585 * generate most commonly used baud rates with high accuracy.
587 ret
= xr_set_reg_uart(port
, XR21V141X_CLOCK_DIVISOR_0
,
592 ret
= xr_set_reg_uart(port
, XR21V141X_CLOCK_DIVISOR_1
,
593 (divisor
>> 8) & 0xff);
597 ret
= xr_set_reg_uart(port
, XR21V141X_CLOCK_DIVISOR_2
,
598 (divisor
>> 16) & 0xff);
602 ret
= xr_set_reg_uart(port
, XR21V141X_TX_CLOCK_MASK_0
,
607 ret
= xr_set_reg_uart(port
, XR21V141X_TX_CLOCK_MASK_1
,
608 (tx_mask
>> 8) & 0xff);
612 ret
= xr_set_reg_uart(port
, XR21V141X_RX_CLOCK_MASK_0
,
617 ret
= xr_set_reg_uart(port
, XR21V141X_RX_CLOCK_MASK_1
,
618 (rx_mask
>> 8) & 0xff);
622 tty_encode_baud_rate(tty
, baud
, baud
);
627 static void xr_set_flow_mode(struct tty_struct
*tty
,
628 struct usb_serial_port
*port
,
629 const struct ktermios
*old_termios
)
631 struct xr_data
*data
= usb_get_serial_port_data(port
);
632 const struct xr_type
*type
= data
->type
;
637 ret
= xr_get_reg_uart(port
, type
->gpio_mode
, &gpio_mode
);
642 * According to the datasheets, the UART needs to be disabled while
643 * writing to the FLOW_CONTROL register (XR21V141X), or any register
644 * but GPIO_SET, GPIO_CLEAR, TX_BREAK and ERROR_STATUS (XR21B142X).
646 xr_uart_disable(port
);
648 /* Set GPIO mode for controlling the pins manually by default. */
649 gpio_mode
&= ~XR_GPIO_MODE_SEL_MASK
;
651 rs485_enabled
= !!(data
->rs485
.flags
& SER_RS485_ENABLED
);
653 dev_dbg(&port
->dev
, "Enabling RS-485\n");
654 gpio_mode
|= XR_GPIO_MODE_SEL_RS485
;
655 if (data
->rs485
.flags
& SER_RS485_RTS_ON_SEND
)
656 gpio_mode
&= ~XR_GPIO_MODE_RS485_TX_H
;
658 gpio_mode
|= XR_GPIO_MODE_RS485_TX_H
;
661 if (C_CRTSCTS(tty
) && C_BAUD(tty
) != B0
&& !rs485_enabled
) {
662 dev_dbg(&port
->dev
, "Enabling hardware flow ctrl\n");
663 gpio_mode
|= XR_GPIO_MODE_SEL_RTS_CTS
;
664 flow
= XR_UART_FLOW_MODE_HW
;
665 } else if (I_IXON(tty
)) {
666 u8 start_char
= START_CHAR(tty
);
667 u8 stop_char
= STOP_CHAR(tty
);
669 dev_dbg(&port
->dev
, "Enabling sw flow ctrl\n");
670 flow
= XR_UART_FLOW_MODE_SW
;
672 xr_set_reg_uart(port
, type
->xon_char
, start_char
);
673 xr_set_reg_uart(port
, type
->xoff_char
, stop_char
);
675 dev_dbg(&port
->dev
, "Disabling flow ctrl\n");
676 flow
= XR_UART_FLOW_MODE_NONE
;
679 xr_set_reg_uart(port
, type
->flow_control
, flow
);
680 xr_set_reg_uart(port
, type
->gpio_mode
, gpio_mode
);
682 xr_uart_enable(port
);
684 if (C_BAUD(tty
) == B0
)
686 else if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
)
690 static void xr21v141x_set_line_settings(struct tty_struct
*tty
,
691 struct usb_serial_port
*port
,
692 const struct ktermios
*old_termios
)
694 struct ktermios
*termios
= &tty
->termios
;
698 if (!old_termios
|| (tty
->termios
.c_ospeed
!= old_termios
->c_ospeed
))
699 xr21v141x_set_baudrate(tty
, port
);
701 switch (C_CSIZE(tty
)) {
704 /* CS5 and CS6 are not supported, so just restore old setting */
705 termios
->c_cflag
&= ~CSIZE
;
707 termios
->c_cflag
|= old_termios
->c_cflag
& CSIZE
;
709 termios
->c_cflag
|= CS8
;
711 if (C_CSIZE(tty
) == CS7
)
712 bits
|= XR_UART_DATA_7
;
714 bits
|= XR_UART_DATA_8
;
717 bits
|= XR_UART_DATA_7
;
721 bits
|= XR_UART_DATA_8
;
728 bits
|= XR_UART_PARITY_MARK
;
730 bits
|= XR_UART_PARITY_SPACE
;
733 bits
|= XR_UART_PARITY_ODD
;
735 bits
|= XR_UART_PARITY_EVEN
;
740 bits
|= XR_UART_STOP_2
;
742 bits
|= XR_UART_STOP_1
;
744 ret
= xr_set_reg_uart(port
, XR21V141X_REG_FORMAT
, bits
);
749 static void xr_cdc_set_line_coding(struct tty_struct
*tty
,
750 struct usb_serial_port
*port
,
751 const struct ktermios
*old_termios
)
753 struct xr_data
*data
= usb_get_serial_port_data(port
);
754 struct usb_host_interface
*alt
= port
->serial
->interface
->cur_altsetting
;
755 struct usb_device
*udev
= port
->serial
->dev
;
756 struct usb_cdc_line_coding
*lc
;
759 lc
= kzalloc(sizeof(*lc
), GFP_KERNEL
);
763 if (tty
->termios
.c_ospeed
)
764 lc
->dwDTERate
= cpu_to_le32(tty
->termios
.c_ospeed
);
766 lc
->dwDTERate
= cpu_to_le32(9600);
769 lc
->bCharFormat
= USB_CDC_2_STOP_BITS
;
771 lc
->bCharFormat
= USB_CDC_1_STOP_BITS
;
776 lc
->bParityType
= USB_CDC_MARK_PARITY
;
778 lc
->bParityType
= USB_CDC_SPACE_PARITY
;
781 lc
->bParityType
= USB_CDC_ODD_PARITY
;
783 lc
->bParityType
= USB_CDC_EVEN_PARITY
;
786 lc
->bParityType
= USB_CDC_NO_PARITY
;
789 if (!data
->type
->have_5_6_bit_mode
&&
790 (C_CSIZE(tty
) == CS5
|| C_CSIZE(tty
) == CS6
)) {
791 tty
->termios
.c_cflag
&= ~CSIZE
;
793 tty
->termios
.c_cflag
|= old_termios
->c_cflag
& CSIZE
;
795 tty
->termios
.c_cflag
|= CS8
;
798 switch (C_CSIZE(tty
)) {
814 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
815 USB_CDC_REQ_SET_LINE_CODING
,
816 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
817 0, alt
->desc
.bInterfaceNumber
,
818 lc
, sizeof(*lc
), USB_CTRL_SET_TIMEOUT
);
820 dev_err(&port
->dev
, "Failed to set line coding: %d\n", ret
);
825 static void xr_sanitize_serial_rs485(struct serial_rs485
*rs485
)
827 if (!(rs485
->flags
& SER_RS485_ENABLED
)) {
828 memset(rs485
, 0, sizeof(*rs485
));
832 /* RTS always toggles after TX */
833 if (rs485
->flags
& SER_RS485_RTS_ON_SEND
)
834 rs485
->flags
&= ~SER_RS485_RTS_AFTER_SEND
;
836 rs485
->flags
|= SER_RS485_RTS_AFTER_SEND
;
838 /* Only the flags are implemented at the moment */
839 rs485
->flags
&= SER_RS485_ENABLED
| SER_RS485_RTS_ON_SEND
|
840 SER_RS485_RTS_AFTER_SEND
;
841 rs485
->delay_rts_before_send
= 0;
842 rs485
->delay_rts_after_send
= 0;
843 memset(rs485
->padding
, 0, sizeof(rs485
->padding
));
846 static int xr_get_rs485_config(struct tty_struct
*tty
,
847 struct serial_rs485 __user
*argp
)
849 struct usb_serial_port
*port
= tty
->driver_data
;
850 struct xr_data
*data
= usb_get_serial_port_data(port
);
852 down_read(&tty
->termios_rwsem
);
853 if (copy_to_user(argp
, &data
->rs485
, sizeof(data
->rs485
))) {
854 up_read(&tty
->termios_rwsem
);
857 up_read(&tty
->termios_rwsem
);
862 static int xr_set_rs485_config(struct tty_struct
*tty
,
863 struct serial_rs485 __user
*argp
)
865 struct usb_serial_port
*port
= tty
->driver_data
;
866 struct xr_data
*data
= usb_get_serial_port_data(port
);
867 struct serial_rs485 rs485
;
869 if (copy_from_user(&rs485
, argp
, sizeof(rs485
)))
871 xr_sanitize_serial_rs485(&rs485
);
873 down_write(&tty
->termios_rwsem
);
875 xr_set_flow_mode(tty
, port
, NULL
);
876 up_write(&tty
->termios_rwsem
);
878 if (copy_to_user(argp
, &rs485
, sizeof(rs485
)))
884 static int xr_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
886 void __user
*argp
= (void __user
*)arg
;
890 return xr_get_rs485_config(tty
, argp
);
892 return xr_set_rs485_config(tty
, argp
);
898 static void xr_set_termios(struct tty_struct
*tty
,
899 struct usb_serial_port
*port
,
900 const struct ktermios
*old_termios
)
902 struct xr_data
*data
= usb_get_serial_port_data(port
);
905 * XR21V141X does not have a CUSTOM_DRIVER flag and always enters CDC
906 * mode upon receiving CDC requests.
908 if (data
->type
->set_line_settings
)
909 data
->type
->set_line_settings(tty
, port
, old_termios
);
911 xr_cdc_set_line_coding(tty
, port
, old_termios
);
913 xr_set_flow_mode(tty
, port
, old_termios
);
916 static int xr_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
920 ret
= xr_fifo_reset(port
);
924 ret
= xr_uart_enable(port
);
926 dev_err(&port
->dev
, "Failed to enable UART\n");
932 xr_set_termios(tty
, port
, NULL
);
934 ret
= usb_serial_generic_open(tty
, port
);
936 xr_uart_disable(port
);
943 static void xr_close(struct usb_serial_port
*port
)
945 usb_serial_generic_close(port
);
947 xr_uart_disable(port
);
950 static int xr_probe(struct usb_serial
*serial
, const struct usb_device_id
*id
)
952 struct usb_interface
*control
= serial
->interface
;
953 struct usb_host_interface
*alt
= control
->cur_altsetting
;
954 struct usb_cdc_parsed_header hdrs
;
955 struct usb_cdc_union_desc
*desc
;
956 struct usb_interface
*data
;
959 ret
= cdc_parse_cdc_header(&hdrs
, control
, alt
->extra
, alt
->extralen
);
963 desc
= hdrs
.usb_cdc_union_desc
;
967 data
= usb_ifnum_to_if(serial
->dev
, desc
->bSlaveInterface0
);
971 ret
= usb_serial_claim_interface(serial
, data
);
975 usb_set_serial_data(serial
, (void *)id
->driver_info
);
980 static int xr_gpio_init(struct usb_serial_port
*port
, const struct xr_type
*type
)
986 * Configure all pins as GPIO except for Receive and Transmit Toggle.
989 if (type
->have_xmit_toggle
)
990 mode
|= XR_GPIO_MODE_RX_TOGGLE
| XR_GPIO_MODE_TX_TOGGLE
;
992 ret
= xr_set_reg_uart(port
, type
->gpio_mode
, mode
);
997 * Configure DTR and RTS as outputs and make sure they are deasserted
998 * (active low), and configure RI, CD, DSR and CTS as inputs.
1000 mask
= XR_GPIO_DTR
| XR_GPIO_RTS
;
1001 ret
= xr_set_reg_uart(port
, type
->gpio_direction
, mask
);
1005 ret
= xr_set_reg_uart(port
, type
->gpio_set
, mask
);
1012 static int xr_port_probe(struct usb_serial_port
*port
)
1014 struct usb_interface_descriptor
*desc
;
1015 const struct xr_type
*type
;
1016 struct xr_data
*data
;
1017 enum xr_type_id type_id
;
1020 type_id
= (int)(unsigned long)usb_get_serial_data(port
->serial
);
1021 type
= &xr_types
[type_id
];
1023 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1029 desc
= &port
->serial
->interface
->cur_altsetting
->desc
;
1030 if (type_id
== XR21V141X
)
1031 data
->channel
= desc
->bInterfaceNumber
/ 2;
1033 data
->channel
= desc
->bInterfaceNumber
;
1035 usb_set_serial_port_data(port
, data
);
1037 if (type
->custom_driver
) {
1038 ret
= xr_set_reg_uart(port
, type
->custom_driver
,
1039 XR_CUSTOM_DRIVER_ACTIVE
);
1044 ret
= xr_gpio_init(port
, type
);
1056 static void xr_port_remove(struct usb_serial_port
*port
)
1058 struct xr_data
*data
= usb_get_serial_port_data(port
);
1063 #define XR_DEVICE(vid, pid, type) \
1064 USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_COMM), \
1065 .driver_info = (type)
1067 static const struct usb_device_id id_table
[] = {
1068 { XR_DEVICE(0x04e2, 0x1400, XR2280X
) },
1069 { XR_DEVICE(0x04e2, 0x1401, XR2280X
) },
1070 { XR_DEVICE(0x04e2, 0x1402, XR2280X
) },
1071 { XR_DEVICE(0x04e2, 0x1403, XR2280X
) },
1072 { XR_DEVICE(0x04e2, 0x1410, XR21V141X
) },
1073 { XR_DEVICE(0x04e2, 0x1411, XR21B1411
) },
1074 { XR_DEVICE(0x04e2, 0x1412, XR21V141X
) },
1075 { XR_DEVICE(0x04e2, 0x1414, XR21V141X
) },
1076 { XR_DEVICE(0x04e2, 0x1420, XR21B142X
) },
1077 { XR_DEVICE(0x04e2, 0x1422, XR21B142X
) },
1078 { XR_DEVICE(0x04e2, 0x1424, XR21B142X
) },
1081 MODULE_DEVICE_TABLE(usb
, id_table
);
1083 static struct usb_serial_driver xr_device
= {
1085 .name
= "xr_serial",
1087 .id_table
= id_table
,
1090 .port_probe
= xr_port_probe
,
1091 .port_remove
= xr_port_remove
,
1094 .break_ctl
= xr_break_ctl
,
1095 .set_termios
= xr_set_termios
,
1096 .tiocmget
= xr_tiocmget
,
1097 .tiocmset
= xr_tiocmset
,
1099 .dtr_rts
= xr_dtr_rts
1102 static struct usb_serial_driver
* const serial_drivers
[] = {
1106 module_usb_serial_driver(serial_drivers
, id_table
);
1108 MODULE_AUTHOR("Manivannan Sadhasivam <mani@kernel.org>");
1109 MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver");
1110 MODULE_LICENSE("GPL");