Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * MaxLinear/Exar USB to Serial driver
0004  *
0005  * Copyright (c) 2020 Manivannan Sadhasivam <mani@kernel.org>
0006  * Copyright (c) 2021 Johan Hovold <johan@kernel.org>
0007  *
0008  * Based on the initial driver written by Patong Yang:
0009  *
0010  *   https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop
0011  *
0012  *   Copyright (c) 2018 Patong Yang <patong.mxl@gmail.com>
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/tty.h>
0019 #include <linux/usb.h>
0020 #include <linux/usb/cdc.h>
0021 #include <linux/usb/serial.h>
0022 
0023 struct xr_txrx_clk_mask {
0024     u16 tx;
0025     u16 rx0;
0026     u16 rx1;
0027 };
0028 
0029 #define XR_INT_OSC_HZ           48000000U
0030 #define XR21V141X_MIN_SPEED     46U
0031 #define XR21V141X_MAX_SPEED     XR_INT_OSC_HZ
0032 
0033 /* XR21V141X register blocks */
0034 #define XR21V141X_UART_REG_BLOCK    0
0035 #define XR21V141X_UM_REG_BLOCK      4
0036 #define XR21V141X_UART_CUSTOM_BLOCK 0x66
0037 
0038 /* XR21V141X UART registers */
0039 #define XR21V141X_CLOCK_DIVISOR_0   0x04
0040 #define XR21V141X_CLOCK_DIVISOR_1   0x05
0041 #define XR21V141X_CLOCK_DIVISOR_2   0x06
0042 #define XR21V141X_TX_CLOCK_MASK_0   0x07
0043 #define XR21V141X_TX_CLOCK_MASK_1   0x08
0044 #define XR21V141X_RX_CLOCK_MASK_0   0x09
0045 #define XR21V141X_RX_CLOCK_MASK_1   0x0a
0046 #define XR21V141X_REG_FORMAT        0x0b
0047 
0048 /* XR21V141X UART Manager registers */
0049 #define XR21V141X_UM_FIFO_ENABLE_REG    0x10
0050 #define XR21V141X_UM_ENABLE_TX_FIFO 0x01
0051 #define XR21V141X_UM_ENABLE_RX_FIFO 0x02
0052 
0053 #define XR21V141X_UM_RX_FIFO_RESET  0x18
0054 #define XR21V141X_UM_TX_FIFO_RESET  0x1c
0055 
0056 #define XR_UART_ENABLE_TX       0x1
0057 #define XR_UART_ENABLE_RX       0x2
0058 
0059 #define XR_GPIO_RI          BIT(0)
0060 #define XR_GPIO_CD          BIT(1)
0061 #define XR_GPIO_DSR         BIT(2)
0062 #define XR_GPIO_DTR         BIT(3)
0063 #define XR_GPIO_CTS         BIT(4)
0064 #define XR_GPIO_RTS         BIT(5)
0065 #define XR_GPIO_CLK         BIT(6)
0066 #define XR_GPIO_XEN         BIT(7)
0067 #define XR_GPIO_TXT         BIT(8)
0068 #define XR_GPIO_RXT         BIT(9)
0069 
0070 #define XR_UART_DATA_MASK       GENMASK(3, 0)
0071 #define XR_UART_DATA_7          0x7
0072 #define XR_UART_DATA_8          0x8
0073 
0074 #define XR_UART_PARITY_MASK     GENMASK(6, 4)
0075 #define XR_UART_PARITY_SHIFT        4
0076 #define XR_UART_PARITY_NONE     (0x0 << XR_UART_PARITY_SHIFT)
0077 #define XR_UART_PARITY_ODD      (0x1 << XR_UART_PARITY_SHIFT)
0078 #define XR_UART_PARITY_EVEN     (0x2 << XR_UART_PARITY_SHIFT)
0079 #define XR_UART_PARITY_MARK     (0x3 << XR_UART_PARITY_SHIFT)
0080 #define XR_UART_PARITY_SPACE        (0x4 << XR_UART_PARITY_SHIFT)
0081 
0082 #define XR_UART_STOP_MASK       BIT(7)
0083 #define XR_UART_STOP_SHIFT      7
0084 #define XR_UART_STOP_1          (0x0 << XR_UART_STOP_SHIFT)
0085 #define XR_UART_STOP_2          (0x1 << XR_UART_STOP_SHIFT)
0086 
0087 #define XR_UART_FLOW_MODE_NONE      0x0
0088 #define XR_UART_FLOW_MODE_HW        0x1
0089 #define XR_UART_FLOW_MODE_SW        0x2
0090 
0091 #define XR_GPIO_MODE_SEL_MASK       GENMASK(2, 0)
0092 #define XR_GPIO_MODE_SEL_RTS_CTS    0x1
0093 #define XR_GPIO_MODE_SEL_DTR_DSR    0x2
0094 #define XR_GPIO_MODE_SEL_RS485      0x3
0095 #define XR_GPIO_MODE_SEL_RS485_ADDR 0x4
0096 #define XR_GPIO_MODE_TX_TOGGLE      0x100
0097 #define XR_GPIO_MODE_RX_TOGGLE      0x200
0098 
0099 #define XR_FIFO_RESET           0x1
0100 
0101 #define XR_CUSTOM_DRIVER_ACTIVE     0x1
0102 
0103 static int xr21v141x_uart_enable(struct usb_serial_port *port);
0104 static int xr21v141x_uart_disable(struct usb_serial_port *port);
0105 static int xr21v141x_fifo_reset(struct usb_serial_port *port);
0106 static void xr21v141x_set_line_settings(struct tty_struct *tty,
0107         struct usb_serial_port *port, struct ktermios *old_termios);
0108 
0109 struct xr_type {
0110     int reg_width;
0111     u8 reg_recipient;
0112     u8 set_reg;
0113     u8 get_reg;
0114 
0115     u16 uart_enable;
0116     u16 flow_control;
0117     u16 xon_char;
0118     u16 xoff_char;
0119     u16 tx_break;
0120     u16 gpio_mode;
0121     u16 gpio_direction;
0122     u16 gpio_set;
0123     u16 gpio_clear;
0124     u16 gpio_status;
0125     u16 tx_fifo_reset;
0126     u16 rx_fifo_reset;
0127     u16 custom_driver;
0128 
0129     bool have_5_6_bit_mode;
0130     bool have_xmit_toggle;
0131 
0132     int (*enable)(struct usb_serial_port *port);
0133     int (*disable)(struct usb_serial_port *port);
0134     int (*fifo_reset)(struct usb_serial_port *port);
0135     void (*set_line_settings)(struct tty_struct *tty,
0136             struct usb_serial_port *port,
0137             struct ktermios *old_termios);
0138 };
0139 
0140 enum xr_type_id {
0141     XR21V141X,
0142     XR21B142X,
0143     XR21B1411,
0144     XR2280X,
0145     XR_TYPE_COUNT,
0146 };
0147 
0148 static const struct xr_type xr_types[] = {
0149     [XR21V141X] = {
0150         .reg_width  = 8,
0151         .reg_recipient  = USB_RECIP_DEVICE,
0152         .set_reg    = 0x00,
0153         .get_reg    = 0x01,
0154 
0155         .uart_enable    = 0x03,
0156         .flow_control   = 0x0c,
0157         .xon_char   = 0x10,
0158         .xoff_char  = 0x11,
0159         .tx_break   = 0x14,
0160         .gpio_mode  = 0x1a,
0161         .gpio_direction = 0x1b,
0162         .gpio_set   = 0x1d,
0163         .gpio_clear = 0x1e,
0164         .gpio_status    = 0x1f,
0165 
0166         .enable         = xr21v141x_uart_enable,
0167         .disable        = xr21v141x_uart_disable,
0168         .fifo_reset     = xr21v141x_fifo_reset,
0169         .set_line_settings  = xr21v141x_set_line_settings,
0170     },
0171     [XR21B142X] = {
0172         .reg_width  = 16,
0173         .reg_recipient  = USB_RECIP_INTERFACE,
0174         .set_reg    = 0x00,
0175         .get_reg    = 0x00,
0176 
0177         .uart_enable    = 0x00,
0178         .flow_control   = 0x06,
0179         .xon_char   = 0x07,
0180         .xoff_char  = 0x08,
0181         .tx_break   = 0x0a,
0182         .gpio_mode  = 0x0c,
0183         .gpio_direction = 0x0d,
0184         .gpio_set   = 0x0e,
0185         .gpio_clear = 0x0f,
0186         .gpio_status    = 0x10,
0187         .tx_fifo_reset  = 0x40,
0188         .rx_fifo_reset  = 0x43,
0189         .custom_driver  = 0x60,
0190 
0191         .have_5_6_bit_mode  = true,
0192         .have_xmit_toggle   = true,
0193     },
0194     [XR21B1411] = {
0195         .reg_width  = 12,
0196         .reg_recipient  = USB_RECIP_DEVICE,
0197         .set_reg    = 0x00,
0198         .get_reg    = 0x01,
0199 
0200         .uart_enable    = 0xc00,
0201         .flow_control   = 0xc06,
0202         .xon_char   = 0xc07,
0203         .xoff_char  = 0xc08,
0204         .tx_break   = 0xc0a,
0205         .gpio_mode  = 0xc0c,
0206         .gpio_direction = 0xc0d,
0207         .gpio_set   = 0xc0e,
0208         .gpio_clear = 0xc0f,
0209         .gpio_status    = 0xc10,
0210         .tx_fifo_reset  = 0xc80,
0211         .rx_fifo_reset  = 0xcc0,
0212         .custom_driver  = 0x20d,
0213     },
0214     [XR2280X] = {
0215         .reg_width  = 16,
0216         .reg_recipient  = USB_RECIP_DEVICE,
0217         .set_reg    = 0x05,
0218         .get_reg    = 0x05,
0219 
0220         .uart_enable    = 0x40,
0221         .flow_control   = 0x46,
0222         .xon_char   = 0x47,
0223         .xoff_char  = 0x48,
0224         .tx_break   = 0x4a,
0225         .gpio_mode  = 0x4c,
0226         .gpio_direction = 0x4d,
0227         .gpio_set   = 0x4e,
0228         .gpio_clear = 0x4f,
0229         .gpio_status    = 0x50,
0230         .tx_fifo_reset  = 0x60,
0231         .rx_fifo_reset  = 0x63,
0232         .custom_driver  = 0x81,
0233     },
0234 };
0235 
0236 struct xr_data {
0237     const struct xr_type *type;
0238     u8 channel;         /* zero-based index or interface number */
0239 };
0240 
0241 static int xr_set_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 val)
0242 {
0243     struct xr_data *data = usb_get_serial_port_data(port);
0244     const struct xr_type *type = data->type;
0245     struct usb_serial *serial = port->serial;
0246     int ret;
0247 
0248     ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
0249             type->set_reg,
0250             USB_DIR_OUT | USB_TYPE_VENDOR | type->reg_recipient,
0251             val, (channel << 8) | reg, NULL, 0,
0252             USB_CTRL_SET_TIMEOUT);
0253     if (ret < 0) {
0254         dev_err(&port->dev, "Failed to set reg 0x%02x: %d\n", reg, ret);
0255         return ret;
0256     }
0257 
0258     return 0;
0259 }
0260 
0261 static int xr_get_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 *val)
0262 {
0263     struct xr_data *data = usb_get_serial_port_data(port);
0264     const struct xr_type *type = data->type;
0265     struct usb_serial *serial = port->serial;
0266     u8 *dmabuf;
0267     int ret, len;
0268 
0269     if (type->reg_width == 8)
0270         len = 1;
0271     else
0272         len = 2;
0273 
0274     dmabuf = kmalloc(len, GFP_KERNEL);
0275     if (!dmabuf)
0276         return -ENOMEM;
0277 
0278     ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
0279             type->get_reg,
0280             USB_DIR_IN | USB_TYPE_VENDOR | type->reg_recipient,
0281             0, (channel << 8) | reg, dmabuf, len,
0282             USB_CTRL_GET_TIMEOUT);
0283     if (ret == len) {
0284         if (len == 2)
0285             *val = le16_to_cpup((__le16 *)dmabuf);
0286         else
0287             *val = *dmabuf;
0288         ret = 0;
0289     } else {
0290         dev_err(&port->dev, "Failed to get reg 0x%02x: %d\n", reg, ret);
0291         if (ret >= 0)
0292             ret = -EIO;
0293     }
0294 
0295     kfree(dmabuf);
0296 
0297     return ret;
0298 }
0299 
0300 static int xr_set_reg_uart(struct usb_serial_port *port, u16 reg, u16 val)
0301 {
0302     struct xr_data *data = usb_get_serial_port_data(port);
0303 
0304     return xr_set_reg(port, data->channel, reg, val);
0305 }
0306 
0307 static int xr_get_reg_uart(struct usb_serial_port *port, u16 reg, u16 *val)
0308 {
0309     struct xr_data *data = usb_get_serial_port_data(port);
0310 
0311     return xr_get_reg(port, data->channel, reg, val);
0312 }
0313 
0314 static int xr_set_reg_um(struct usb_serial_port *port, u8 reg_base, u8 val)
0315 {
0316     struct xr_data *data = usb_get_serial_port_data(port);
0317     u8 reg;
0318 
0319     reg = reg_base + data->channel;
0320 
0321     return xr_set_reg(port, XR21V141X_UM_REG_BLOCK, reg, val);
0322 }
0323 
0324 static int __xr_uart_enable(struct usb_serial_port *port)
0325 {
0326     struct xr_data *data = usb_get_serial_port_data(port);
0327 
0328     return xr_set_reg_uart(port, data->type->uart_enable,
0329             XR_UART_ENABLE_TX | XR_UART_ENABLE_RX);
0330 }
0331 
0332 static int __xr_uart_disable(struct usb_serial_port *port)
0333 {
0334     struct xr_data *data = usb_get_serial_port_data(port);
0335 
0336     return xr_set_reg_uart(port, data->type->uart_enable, 0);
0337 }
0338 
0339 /*
0340  * According to datasheet, below is the recommended sequence for enabling UART
0341  * module in XR21V141X:
0342  *
0343  * Enable Tx FIFO
0344  * Enable Tx and Rx
0345  * Enable Rx FIFO
0346  */
0347 static int xr21v141x_uart_enable(struct usb_serial_port *port)
0348 {
0349     int ret;
0350 
0351     ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
0352                 XR21V141X_UM_ENABLE_TX_FIFO);
0353     if (ret)
0354         return ret;
0355 
0356     ret = __xr_uart_enable(port);
0357     if (ret)
0358         return ret;
0359 
0360     ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
0361                 XR21V141X_UM_ENABLE_TX_FIFO | XR21V141X_UM_ENABLE_RX_FIFO);
0362     if (ret)
0363         __xr_uart_disable(port);
0364 
0365     return ret;
0366 }
0367 
0368 static int xr21v141x_uart_disable(struct usb_serial_port *port)
0369 {
0370     int ret;
0371 
0372     ret = __xr_uart_disable(port);
0373     if (ret)
0374         return ret;
0375 
0376     ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, 0);
0377 
0378     return ret;
0379 }
0380 
0381 static int xr_uart_enable(struct usb_serial_port *port)
0382 {
0383     struct xr_data *data = usb_get_serial_port_data(port);
0384 
0385     if (data->type->enable)
0386         return data->type->enable(port);
0387 
0388     return __xr_uart_enable(port);
0389 }
0390 
0391 static int xr_uart_disable(struct usb_serial_port *port)
0392 {
0393     struct xr_data *data = usb_get_serial_port_data(port);
0394 
0395     if (data->type->disable)
0396         return data->type->disable(port);
0397 
0398     return __xr_uart_disable(port);
0399 }
0400 
0401 static int xr21v141x_fifo_reset(struct usb_serial_port *port)
0402 {
0403     int ret;
0404 
0405     ret = xr_set_reg_um(port, XR21V141X_UM_TX_FIFO_RESET, XR_FIFO_RESET);
0406     if (ret)
0407         return ret;
0408 
0409     ret = xr_set_reg_um(port, XR21V141X_UM_RX_FIFO_RESET, XR_FIFO_RESET);
0410     if (ret)
0411         return ret;
0412 
0413     return 0;
0414 }
0415 
0416 static int xr_fifo_reset(struct usb_serial_port *port)
0417 {
0418     struct xr_data *data = usb_get_serial_port_data(port);
0419     int ret;
0420 
0421     if (data->type->fifo_reset)
0422         return data->type->fifo_reset(port);
0423 
0424     ret = xr_set_reg_uart(port, data->type->tx_fifo_reset, XR_FIFO_RESET);
0425     if (ret)
0426         return ret;
0427 
0428     ret = xr_set_reg_uart(port, data->type->rx_fifo_reset, XR_FIFO_RESET);
0429     if (ret)
0430         return ret;
0431 
0432     return 0;
0433 }
0434 
0435 static int xr_tiocmget(struct tty_struct *tty)
0436 {
0437     struct usb_serial_port *port = tty->driver_data;
0438     struct xr_data *data = usb_get_serial_port_data(port);
0439     u16 status;
0440     int ret;
0441 
0442     ret = xr_get_reg_uart(port, data->type->gpio_status, &status);
0443     if (ret)
0444         return ret;
0445 
0446     /*
0447      * Modem control pins are active low, so reading '0' means it is active
0448      * and '1' means not active.
0449      */
0450     ret = ((status & XR_GPIO_DTR) ? 0 : TIOCM_DTR) |
0451           ((status & XR_GPIO_RTS) ? 0 : TIOCM_RTS) |
0452           ((status & XR_GPIO_CTS) ? 0 : TIOCM_CTS) |
0453           ((status & XR_GPIO_DSR) ? 0 : TIOCM_DSR) |
0454           ((status & XR_GPIO_RI) ? 0 : TIOCM_RI) |
0455           ((status & XR_GPIO_CD) ? 0 : TIOCM_CD);
0456 
0457     return ret;
0458 }
0459 
0460 static int xr_tiocmset_port(struct usb_serial_port *port,
0461                 unsigned int set, unsigned int clear)
0462 {
0463     struct xr_data *data = usb_get_serial_port_data(port);
0464     const struct xr_type *type = data->type;
0465     u16 gpio_set = 0;
0466     u16 gpio_clr = 0;
0467     int ret = 0;
0468 
0469     /* Modem control pins are active low, so set & clr are swapped */
0470     if (set & TIOCM_RTS)
0471         gpio_clr |= XR_GPIO_RTS;
0472     if (set & TIOCM_DTR)
0473         gpio_clr |= XR_GPIO_DTR;
0474     if (clear & TIOCM_RTS)
0475         gpio_set |= XR_GPIO_RTS;
0476     if (clear & TIOCM_DTR)
0477         gpio_set |= XR_GPIO_DTR;
0478 
0479     /* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */
0480     if (gpio_clr)
0481         ret = xr_set_reg_uart(port, type->gpio_clear, gpio_clr);
0482 
0483     if (gpio_set)
0484         ret = xr_set_reg_uart(port, type->gpio_set, gpio_set);
0485 
0486     return ret;
0487 }
0488 
0489 static int xr_tiocmset(struct tty_struct *tty,
0490                unsigned int set, unsigned int clear)
0491 {
0492     struct usb_serial_port *port = tty->driver_data;
0493 
0494     return xr_tiocmset_port(port, set, clear);
0495 }
0496 
0497 static void xr_dtr_rts(struct usb_serial_port *port, int on)
0498 {
0499     if (on)
0500         xr_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0);
0501     else
0502         xr_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS);
0503 }
0504 
0505 static void xr_break_ctl(struct tty_struct *tty, int break_state)
0506 {
0507     struct usb_serial_port *port = tty->driver_data;
0508     struct xr_data *data = usb_get_serial_port_data(port);
0509     const struct xr_type *type = data->type;
0510     u16 state;
0511 
0512     if (break_state == 0)
0513         state = 0;
0514     else
0515         state = GENMASK(type->reg_width - 1, 0);
0516 
0517     dev_dbg(&port->dev, "Turning break %s\n", state == 0 ? "off" : "on");
0518 
0519     xr_set_reg_uart(port, type->tx_break, state);
0520 }
0521 
0522 /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */
0523 static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks[] = {
0524     { 0x000, 0x000, 0x000 },
0525     { 0x000, 0x000, 0x000 },
0526     { 0x100, 0x000, 0x100 },
0527     { 0x020, 0x400, 0x020 },
0528     { 0x010, 0x100, 0x010 },
0529     { 0x208, 0x040, 0x208 },
0530     { 0x104, 0x820, 0x108 },
0531     { 0x844, 0x210, 0x884 },
0532     { 0x444, 0x110, 0x444 },
0533     { 0x122, 0x888, 0x224 },
0534     { 0x912, 0x448, 0x924 },
0535     { 0x492, 0x248, 0x492 },
0536     { 0x252, 0x928, 0x292 },
0537     { 0x94a, 0x4a4, 0xa52 },
0538     { 0x52a, 0xaa4, 0x54a },
0539     { 0xaaa, 0x954, 0x4aa },
0540     { 0xaaa, 0x554, 0xaaa },
0541     { 0x555, 0xad4, 0x5aa },
0542     { 0xb55, 0xab4, 0x55a },
0543     { 0x6b5, 0x5ac, 0xb56 },
0544     { 0x5b5, 0xd6c, 0x6d6 },
0545     { 0xb6d, 0xb6a, 0xdb6 },
0546     { 0x76d, 0x6da, 0xbb6 },
0547     { 0xedd, 0xdda, 0x76e },
0548     { 0xddd, 0xbba, 0xeee },
0549     { 0x7bb, 0xf7a, 0xdde },
0550     { 0xf7b, 0xef6, 0x7de },
0551     { 0xdf7, 0xbf6, 0xf7e },
0552     { 0x7f7, 0xfee, 0xefe },
0553     { 0xfdf, 0xfbe, 0x7fe },
0554     { 0xf7f, 0xefe, 0xffe },
0555     { 0xfff, 0xffe, 0xffd },
0556 };
0557 
0558 static int xr21v141x_set_baudrate(struct tty_struct *tty, struct usb_serial_port *port)
0559 {
0560     u32 divisor, baud, idx;
0561     u16 tx_mask, rx_mask;
0562     int ret;
0563 
0564     baud = tty->termios.c_ospeed;
0565     if (!baud)
0566         return 0;
0567 
0568     baud = clamp(baud, XR21V141X_MIN_SPEED, XR21V141X_MAX_SPEED);
0569     divisor = XR_INT_OSC_HZ / baud;
0570     idx = ((32 * XR_INT_OSC_HZ) / baud) & 0x1f;
0571     tx_mask = xr21v141x_txrx_clk_masks[idx].tx;
0572 
0573     if (divisor & 0x01)
0574         rx_mask = xr21v141x_txrx_clk_masks[idx].rx1;
0575     else
0576         rx_mask = xr21v141x_txrx_clk_masks[idx].rx0;
0577 
0578     dev_dbg(&port->dev, "Setting baud rate: %u\n", baud);
0579     /*
0580      * XR21V141X uses fractional baud rate generator with 48MHz internal
0581      * oscillator and 19-bit programmable divisor. So theoretically it can
0582      * generate most commonly used baud rates with high accuracy.
0583      */
0584     ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_0,
0585                   divisor & 0xff);
0586     if (ret)
0587         return ret;
0588 
0589     ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_1,
0590                   (divisor >>  8) & 0xff);
0591     if (ret)
0592         return ret;
0593 
0594     ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_2,
0595                   (divisor >> 16) & 0xff);
0596     if (ret)
0597         return ret;
0598 
0599     ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_0,
0600                   tx_mask & 0xff);
0601     if (ret)
0602         return ret;
0603 
0604     ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_1,
0605                   (tx_mask >>  8) & 0xff);
0606     if (ret)
0607         return ret;
0608 
0609     ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_0,
0610                   rx_mask & 0xff);
0611     if (ret)
0612         return ret;
0613 
0614     ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_1,
0615                   (rx_mask >>  8) & 0xff);
0616     if (ret)
0617         return ret;
0618 
0619     tty_encode_baud_rate(tty, baud, baud);
0620 
0621     return 0;
0622 }
0623 
0624 static void xr_set_flow_mode(struct tty_struct *tty,
0625                  struct usb_serial_port *port,
0626                  struct ktermios *old_termios)
0627 {
0628     struct xr_data *data = usb_get_serial_port_data(port);
0629     const struct xr_type *type = data->type;
0630     u16 flow, gpio_mode;
0631     int ret;
0632 
0633     ret = xr_get_reg_uart(port, type->gpio_mode, &gpio_mode);
0634     if (ret)
0635         return;
0636 
0637     /*
0638      * According to the datasheets, the UART needs to be disabled while
0639      * writing to the FLOW_CONTROL register (XR21V141X), or any register
0640      * but GPIO_SET, GPIO_CLEAR, TX_BREAK and ERROR_STATUS (XR21B142X).
0641      */
0642     xr_uart_disable(port);
0643 
0644     /* Set GPIO mode for controlling the pins manually by default. */
0645     gpio_mode &= ~XR_GPIO_MODE_SEL_MASK;
0646 
0647     if (C_CRTSCTS(tty) && C_BAUD(tty) != B0) {
0648         dev_dbg(&port->dev, "Enabling hardware flow ctrl\n");
0649         gpio_mode |= XR_GPIO_MODE_SEL_RTS_CTS;
0650         flow = XR_UART_FLOW_MODE_HW;
0651     } else if (I_IXON(tty)) {
0652         u8 start_char = START_CHAR(tty);
0653         u8 stop_char = STOP_CHAR(tty);
0654 
0655         dev_dbg(&port->dev, "Enabling sw flow ctrl\n");
0656         flow = XR_UART_FLOW_MODE_SW;
0657 
0658         xr_set_reg_uart(port, type->xon_char, start_char);
0659         xr_set_reg_uart(port, type->xoff_char, stop_char);
0660     } else {
0661         dev_dbg(&port->dev, "Disabling flow ctrl\n");
0662         flow = XR_UART_FLOW_MODE_NONE;
0663     }
0664 
0665     xr_set_reg_uart(port, type->flow_control, flow);
0666     xr_set_reg_uart(port, type->gpio_mode, gpio_mode);
0667 
0668     xr_uart_enable(port);
0669 
0670     if (C_BAUD(tty) == B0)
0671         xr_dtr_rts(port, 0);
0672     else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
0673         xr_dtr_rts(port, 1);
0674 }
0675 
0676 static void xr21v141x_set_line_settings(struct tty_struct *tty,
0677         struct usb_serial_port *port, struct ktermios *old_termios)
0678 {
0679     struct ktermios *termios = &tty->termios;
0680     u8 bits = 0;
0681     int ret;
0682 
0683     if (!old_termios || (tty->termios.c_ospeed != old_termios->c_ospeed))
0684         xr21v141x_set_baudrate(tty, port);
0685 
0686     switch (C_CSIZE(tty)) {
0687     case CS5:
0688     case CS6:
0689         /* CS5 and CS6 are not supported, so just restore old setting */
0690         termios->c_cflag &= ~CSIZE;
0691         if (old_termios)
0692             termios->c_cflag |= old_termios->c_cflag & CSIZE;
0693         else
0694             termios->c_cflag |= CS8;
0695 
0696         if (C_CSIZE(tty) == CS7)
0697             bits |= XR_UART_DATA_7;
0698         else
0699             bits |= XR_UART_DATA_8;
0700         break;
0701     case CS7:
0702         bits |= XR_UART_DATA_7;
0703         break;
0704     case CS8:
0705     default:
0706         bits |= XR_UART_DATA_8;
0707         break;
0708     }
0709 
0710     if (C_PARENB(tty)) {
0711         if (C_CMSPAR(tty)) {
0712             if (C_PARODD(tty))
0713                 bits |= XR_UART_PARITY_MARK;
0714             else
0715                 bits |= XR_UART_PARITY_SPACE;
0716         } else {
0717             if (C_PARODD(tty))
0718                 bits |= XR_UART_PARITY_ODD;
0719             else
0720                 bits |= XR_UART_PARITY_EVEN;
0721         }
0722     }
0723 
0724     if (C_CSTOPB(tty))
0725         bits |= XR_UART_STOP_2;
0726     else
0727         bits |= XR_UART_STOP_1;
0728 
0729     ret = xr_set_reg_uart(port, XR21V141X_REG_FORMAT, bits);
0730     if (ret)
0731         return;
0732 }
0733 
0734 static void xr_cdc_set_line_coding(struct tty_struct *tty,
0735         struct usb_serial_port *port, struct ktermios *old_termios)
0736 {
0737     struct xr_data *data = usb_get_serial_port_data(port);
0738     struct usb_host_interface *alt = port->serial->interface->cur_altsetting;
0739     struct usb_device *udev = port->serial->dev;
0740     struct usb_cdc_line_coding *lc;
0741     int ret;
0742 
0743     lc = kzalloc(sizeof(*lc), GFP_KERNEL);
0744     if (!lc)
0745         return;
0746 
0747     if (tty->termios.c_ospeed)
0748         lc->dwDTERate = cpu_to_le32(tty->termios.c_ospeed);
0749     else if (old_termios)
0750         lc->dwDTERate = cpu_to_le32(old_termios->c_ospeed);
0751     else
0752         lc->dwDTERate = cpu_to_le32(9600);
0753 
0754     if (C_CSTOPB(tty))
0755         lc->bCharFormat = USB_CDC_2_STOP_BITS;
0756     else
0757         lc->bCharFormat = USB_CDC_1_STOP_BITS;
0758 
0759     if (C_PARENB(tty)) {
0760         if (C_CMSPAR(tty)) {
0761             if (C_PARODD(tty))
0762                 lc->bParityType = USB_CDC_MARK_PARITY;
0763             else
0764                 lc->bParityType = USB_CDC_SPACE_PARITY;
0765         } else {
0766             if (C_PARODD(tty))
0767                 lc->bParityType = USB_CDC_ODD_PARITY;
0768             else
0769                 lc->bParityType = USB_CDC_EVEN_PARITY;
0770         }
0771     } else {
0772         lc->bParityType = USB_CDC_NO_PARITY;
0773     }
0774 
0775     if (!data->type->have_5_6_bit_mode &&
0776             (C_CSIZE(tty) == CS5 || C_CSIZE(tty) == CS6)) {
0777         tty->termios.c_cflag &= ~CSIZE;
0778         if (old_termios)
0779             tty->termios.c_cflag |= old_termios->c_cflag & CSIZE;
0780         else
0781             tty->termios.c_cflag |= CS8;
0782     }
0783 
0784     switch (C_CSIZE(tty)) {
0785     case CS5:
0786         lc->bDataBits = 5;
0787         break;
0788     case CS6:
0789         lc->bDataBits = 6;
0790         break;
0791     case CS7:
0792         lc->bDataBits = 7;
0793         break;
0794     case CS8:
0795     default:
0796         lc->bDataBits = 8;
0797         break;
0798     }
0799 
0800     ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
0801             USB_CDC_REQ_SET_LINE_CODING,
0802             USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0803             0, alt->desc.bInterfaceNumber,
0804             lc, sizeof(*lc), USB_CTRL_SET_TIMEOUT);
0805     if (ret < 0)
0806         dev_err(&port->dev, "Failed to set line coding: %d\n", ret);
0807 
0808     kfree(lc);
0809 }
0810 
0811 static void xr_set_termios(struct tty_struct *tty,
0812         struct usb_serial_port *port, struct ktermios *old_termios)
0813 {
0814     struct xr_data *data = usb_get_serial_port_data(port);
0815 
0816     /*
0817      * XR21V141X does not have a CUSTOM_DRIVER flag and always enters CDC
0818      * mode upon receiving CDC requests.
0819      */
0820     if (data->type->set_line_settings)
0821         data->type->set_line_settings(tty, port, old_termios);
0822     else
0823         xr_cdc_set_line_coding(tty, port, old_termios);
0824 
0825     xr_set_flow_mode(tty, port, old_termios);
0826 }
0827 
0828 static int xr_open(struct tty_struct *tty, struct usb_serial_port *port)
0829 {
0830     int ret;
0831 
0832     ret = xr_fifo_reset(port);
0833     if (ret)
0834         return ret;
0835 
0836     ret = xr_uart_enable(port);
0837     if (ret) {
0838         dev_err(&port->dev, "Failed to enable UART\n");
0839         return ret;
0840     }
0841 
0842     /* Setup termios */
0843     if (tty)
0844         xr_set_termios(tty, port, NULL);
0845 
0846     ret = usb_serial_generic_open(tty, port);
0847     if (ret) {
0848         xr_uart_disable(port);
0849         return ret;
0850     }
0851 
0852     return 0;
0853 }
0854 
0855 static void xr_close(struct usb_serial_port *port)
0856 {
0857     usb_serial_generic_close(port);
0858 
0859     xr_uart_disable(port);
0860 }
0861 
0862 static int xr_probe(struct usb_serial *serial, const struct usb_device_id *id)
0863 {
0864     struct usb_interface *control = serial->interface;
0865     struct usb_host_interface *alt = control->cur_altsetting;
0866     struct usb_cdc_parsed_header hdrs;
0867     struct usb_cdc_union_desc *desc;
0868     struct usb_interface *data;
0869     int ret;
0870 
0871     ret = cdc_parse_cdc_header(&hdrs, control, alt->extra, alt->extralen);
0872     if (ret < 0)
0873         return -ENODEV;
0874 
0875     desc = hdrs.usb_cdc_union_desc;
0876     if (!desc)
0877         return -ENODEV;
0878 
0879     data = usb_ifnum_to_if(serial->dev, desc->bSlaveInterface0);
0880     if (!data)
0881         return -ENODEV;
0882 
0883     ret = usb_serial_claim_interface(serial, data);
0884     if (ret)
0885         return ret;
0886 
0887     usb_set_serial_data(serial, (void *)id->driver_info);
0888 
0889     return 0;
0890 }
0891 
0892 static int xr_gpio_init(struct usb_serial_port *port, const struct xr_type *type)
0893 {
0894     u16 mask, mode;
0895     int ret;
0896 
0897     /*
0898      * Configure all pins as GPIO except for Receive and Transmit Toggle.
0899      */
0900     mode = 0;
0901     if (type->have_xmit_toggle)
0902         mode |= XR_GPIO_MODE_RX_TOGGLE | XR_GPIO_MODE_TX_TOGGLE;
0903 
0904     ret = xr_set_reg_uart(port, type->gpio_mode, mode);
0905     if (ret)
0906         return ret;
0907 
0908     /*
0909      * Configure DTR and RTS as outputs and make sure they are deasserted
0910      * (active low), and configure RI, CD, DSR and CTS as inputs.
0911      */
0912     mask = XR_GPIO_DTR | XR_GPIO_RTS;
0913     ret = xr_set_reg_uart(port, type->gpio_direction, mask);
0914     if (ret)
0915         return ret;
0916 
0917     ret = xr_set_reg_uart(port, type->gpio_set, mask);
0918     if (ret)
0919         return ret;
0920 
0921     return 0;
0922 }
0923 
0924 static int xr_port_probe(struct usb_serial_port *port)
0925 {
0926     struct usb_interface_descriptor *desc;
0927     const struct xr_type *type;
0928     struct xr_data *data;
0929     enum xr_type_id type_id;
0930     int ret;
0931 
0932     type_id = (int)(unsigned long)usb_get_serial_data(port->serial);
0933     type = &xr_types[type_id];
0934 
0935     data = kzalloc(sizeof(*data), GFP_KERNEL);
0936     if (!data)
0937         return -ENOMEM;
0938 
0939     data->type = type;
0940 
0941     desc = &port->serial->interface->cur_altsetting->desc;
0942     if (type_id == XR21V141X)
0943         data->channel = desc->bInterfaceNumber / 2;
0944     else
0945         data->channel = desc->bInterfaceNumber;
0946 
0947     usb_set_serial_port_data(port, data);
0948 
0949     if (type->custom_driver) {
0950         ret = xr_set_reg_uart(port, type->custom_driver,
0951                 XR_CUSTOM_DRIVER_ACTIVE);
0952         if (ret)
0953             goto err_free;
0954     }
0955 
0956     ret = xr_gpio_init(port, type);
0957     if (ret)
0958         goto err_free;
0959 
0960     return 0;
0961 
0962 err_free:
0963     kfree(data);
0964 
0965     return ret;
0966 }
0967 
0968 static void xr_port_remove(struct usb_serial_port *port)
0969 {
0970     struct xr_data *data = usb_get_serial_port_data(port);
0971 
0972     kfree(data);
0973 }
0974 
0975 #define XR_DEVICE(vid, pid, type)                   \
0976     USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_COMM),   \
0977     .driver_info = (type)
0978 
0979 static const struct usb_device_id id_table[] = {
0980     { XR_DEVICE(0x04e2, 0x1400, XR2280X) },
0981     { XR_DEVICE(0x04e2, 0x1401, XR2280X) },
0982     { XR_DEVICE(0x04e2, 0x1402, XR2280X) },
0983     { XR_DEVICE(0x04e2, 0x1403, XR2280X) },
0984     { XR_DEVICE(0x04e2, 0x1410, XR21V141X) },
0985     { XR_DEVICE(0x04e2, 0x1411, XR21B1411) },
0986     { XR_DEVICE(0x04e2, 0x1412, XR21V141X) },
0987     { XR_DEVICE(0x04e2, 0x1414, XR21V141X) },
0988     { XR_DEVICE(0x04e2, 0x1420, XR21B142X) },
0989     { XR_DEVICE(0x04e2, 0x1422, XR21B142X) },
0990     { XR_DEVICE(0x04e2, 0x1424, XR21B142X) },
0991     { }
0992 };
0993 MODULE_DEVICE_TABLE(usb, id_table);
0994 
0995 static struct usb_serial_driver xr_device = {
0996     .driver = {
0997         .owner = THIS_MODULE,
0998         .name = "xr_serial",
0999     },
1000     .id_table       = id_table,
1001     .num_ports      = 1,
1002     .probe          = xr_probe,
1003     .port_probe     = xr_port_probe,
1004     .port_remove        = xr_port_remove,
1005     .open           = xr_open,
1006     .close          = xr_close,
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
1012 };
1013 
1014 static struct usb_serial_driver * const serial_drivers[] = {
1015     &xr_device, NULL
1016 };
1017 
1018 module_usb_serial_driver(serial_drivers, id_table);
1019 
1020 MODULE_AUTHOR("Manivannan Sadhasivam <mani@kernel.org>");
1021 MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver");
1022 MODULE_LICENSE("GPL");