Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * MaxLinear/Exar USB to Serial driver |
| 4 | * |
| 5 | * Copyright (c) 2020 Manivannan Sadhasivam <mani@kernel.org> |
| 6 | * |
| 7 | * Based on the initial driver written by Patong Yang: |
| 8 | * |
| 9 | * https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop |
| 10 | * |
| 11 | * Copyright (c) 2018 Patong Yang <patong.mxl@gmail.com> |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/tty.h> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/usb/serial.h> |
| 20 | |
| 21 | struct xr_txrx_clk_mask { |
| 22 | u16 tx; |
| 23 | u16 rx0; |
| 24 | u16 rx1; |
| 25 | }; |
| 26 | |
| 27 | #define XR_INT_OSC_HZ 48000000U |
| 28 | #define XR21V141X_MIN_SPEED 46U |
| 29 | #define XR21V141X_MAX_SPEED XR_INT_OSC_HZ |
| 30 | |
| 31 | /* USB Requests */ |
| 32 | #define XR21V141X_SET_REQ 0 |
| 33 | #define XR21V141X_GET_REQ 1 |
| 34 | |
| 35 | #define XR21V141X_CLOCK_DIVISOR_0 0x04 |
| 36 | #define XR21V141X_CLOCK_DIVISOR_1 0x05 |
| 37 | #define XR21V141X_CLOCK_DIVISOR_2 0x06 |
| 38 | #define XR21V141X_TX_CLOCK_MASK_0 0x07 |
| 39 | #define XR21V141X_TX_CLOCK_MASK_1 0x08 |
| 40 | #define XR21V141X_RX_CLOCK_MASK_0 0x09 |
| 41 | #define XR21V141X_RX_CLOCK_MASK_1 0x0a |
| 42 | |
| 43 | /* XR21V141X register blocks */ |
| 44 | #define XR21V141X_UART_REG_BLOCK 0 |
| 45 | #define XR21V141X_UM_REG_BLOCK 4 |
| 46 | #define XR21V141X_UART_CUSTOM_BLOCK 0x66 |
| 47 | |
| 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 |
| 52 | |
| 53 | #define XR21V141X_UM_RX_FIFO_RESET 0x18 |
| 54 | #define XR21V141X_UM_TX_FIFO_RESET 0x1c |
| 55 | |
| 56 | #define XR21V141X_UART_ENABLE_TX 0x1 |
| 57 | #define XR21V141X_UART_ENABLE_RX 0x2 |
| 58 | |
| 59 | #define XR21V141X_UART_MODE_RI BIT(0) |
| 60 | #define XR21V141X_UART_MODE_CD BIT(1) |
| 61 | #define XR21V141X_UART_MODE_DSR BIT(2) |
| 62 | #define XR21V141X_UART_MODE_DTR BIT(3) |
| 63 | #define XR21V141X_UART_MODE_CTS BIT(4) |
| 64 | #define XR21V141X_UART_MODE_RTS BIT(5) |
| 65 | |
| 66 | #define XR21V141X_UART_BREAK_ON 0xff |
| 67 | #define XR21V141X_UART_BREAK_OFF 0 |
| 68 | |
| 69 | #define XR21V141X_UART_DATA_MASK GENMASK(3, 0) |
| 70 | #define XR21V141X_UART_DATA_7 0x7 |
| 71 | #define XR21V141X_UART_DATA_8 0x8 |
| 72 | |
| 73 | #define XR21V141X_UART_PARITY_MASK GENMASK(6, 4) |
| 74 | #define XR21V141X_UART_PARITY_SHIFT 0x4 |
| 75 | #define XR21V141X_UART_PARITY_NONE 0x0 |
| 76 | #define XR21V141X_UART_PARITY_ODD 0x1 |
| 77 | #define XR21V141X_UART_PARITY_EVEN 0x2 |
| 78 | #define XR21V141X_UART_PARITY_MARK 0x3 |
| 79 | #define XR21V141X_UART_PARITY_SPACE 0x4 |
| 80 | |
| 81 | #define XR21V141X_UART_STOP_MASK BIT(7) |
| 82 | #define XR21V141X_UART_STOP_SHIFT 0x7 |
| 83 | #define XR21V141X_UART_STOP_1 0x0 |
| 84 | #define XR21V141X_UART_STOP_2 0x1 |
| 85 | |
| 86 | #define XR21V141X_UART_FLOW_MODE_NONE 0x0 |
| 87 | #define XR21V141X_UART_FLOW_MODE_HW 0x1 |
| 88 | #define XR21V141X_UART_FLOW_MODE_SW 0x2 |
| 89 | |
| 90 | #define XR21V141X_UART_MODE_GPIO_MASK GENMASK(2, 0) |
| 91 | #define XR21V141X_UART_MODE_RTS_CTS 0x1 |
| 92 | #define XR21V141X_UART_MODE_DTR_DSR 0x2 |
| 93 | #define XR21V141X_UART_MODE_RS485 0x3 |
| 94 | #define XR21V141X_UART_MODE_RS485_ADDR 0x4 |
| 95 | |
| 96 | #define XR21V141X_REG_ENABLE 0x03 |
| 97 | #define XR21V141X_REG_FORMAT 0x0b |
| 98 | #define XR21V141X_REG_FLOW_CTRL 0x0c |
| 99 | #define XR21V141X_REG_XON_CHAR 0x10 |
| 100 | #define XR21V141X_REG_XOFF_CHAR 0x11 |
| 101 | #define XR21V141X_REG_LOOPBACK 0x12 |
| 102 | #define XR21V141X_REG_TX_BREAK 0x14 |
| 103 | #define XR21V141X_REG_RS845_DELAY 0x15 |
| 104 | #define XR21V141X_REG_GPIO_MODE 0x1a |
| 105 | #define XR21V141X_REG_GPIO_DIR 0x1b |
| 106 | #define XR21V141X_REG_GPIO_INT_MASK 0x1c |
| 107 | #define XR21V141X_REG_GPIO_SET 0x1d |
| 108 | #define XR21V141X_REG_GPIO_CLR 0x1e |
| 109 | #define XR21V141X_REG_GPIO_STATUS 0x1f |
| 110 | |
| 111 | static int xr_set_reg(struct usb_serial_port *port, u8 block, u8 reg, u8 val) |
| 112 | { |
| 113 | struct usb_serial *serial = port->serial; |
| 114 | int ret; |
| 115 | |
| 116 | ret = usb_control_msg(serial->dev, |
| 117 | usb_sndctrlpipe(serial->dev, 0), |
| 118 | XR21V141X_SET_REQ, |
| 119 | USB_DIR_OUT | USB_TYPE_VENDOR, val, |
| 120 | reg | (block << 8), NULL, 0, |
| 121 | USB_CTRL_SET_TIMEOUT); |
| 122 | if (ret < 0) { |
| 123 | dev_err(&port->dev, "Failed to set reg 0x%02x: %d\n", reg, ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int xr_get_reg(struct usb_serial_port *port, u8 block, u8 reg, u8 *val) |
| 131 | { |
| 132 | struct usb_serial *serial = port->serial; |
| 133 | u8 *dmabuf; |
| 134 | int ret; |
| 135 | |
| 136 | dmabuf = kmalloc(1, GFP_KERNEL); |
| 137 | if (!dmabuf) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | ret = usb_control_msg(serial->dev, |
| 141 | usb_rcvctrlpipe(serial->dev, 0), |
| 142 | XR21V141X_GET_REQ, |
| 143 | USB_DIR_IN | USB_TYPE_VENDOR, 0, |
| 144 | reg | (block << 8), dmabuf, 1, |
| 145 | USB_CTRL_GET_TIMEOUT); |
| 146 | if (ret == 1) { |
| 147 | *val = *dmabuf; |
| 148 | ret = 0; |
| 149 | } else { |
| 150 | dev_err(&port->dev, "Failed to get reg 0x%02x: %d\n", reg, ret); |
| 151 | if (ret >= 0) |
| 152 | ret = -EIO; |
| 153 | } |
| 154 | |
| 155 | kfree(dmabuf); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static int xr_set_reg_uart(struct usb_serial_port *port, u8 reg, u8 val) |
| 161 | { |
| 162 | return xr_set_reg(port, XR21V141X_UART_REG_BLOCK, reg, val); |
| 163 | } |
| 164 | |
| 165 | static int xr_get_reg_uart(struct usb_serial_port *port, u8 reg, u8 *val) |
| 166 | { |
| 167 | return xr_get_reg(port, XR21V141X_UART_REG_BLOCK, reg, val); |
| 168 | } |
| 169 | |
| 170 | static int xr_set_reg_um(struct usb_serial_port *port, u8 reg, u8 val) |
| 171 | { |
| 172 | return xr_set_reg(port, XR21V141X_UM_REG_BLOCK, reg, val); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * According to datasheet, below is the recommended sequence for enabling UART |
| 177 | * module in XR21V141X: |
| 178 | * |
| 179 | * Enable Tx FIFO |
| 180 | * Enable Tx and Rx |
| 181 | * Enable Rx FIFO |
| 182 | */ |
| 183 | static int xr_uart_enable(struct usb_serial_port *port) |
| 184 | { |
| 185 | int ret; |
| 186 | |
| 187 | ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, |
| 188 | XR21V141X_UM_ENABLE_TX_FIFO); |
| 189 | if (ret) |
| 190 | return ret; |
| 191 | |
| 192 | ret = xr_set_reg_uart(port, XR21V141X_REG_ENABLE, |
| 193 | XR21V141X_UART_ENABLE_TX | XR21V141X_UART_ENABLE_RX); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | |
| 197 | ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, |
| 198 | XR21V141X_UM_ENABLE_TX_FIFO | XR21V141X_UM_ENABLE_RX_FIFO); |
| 199 | |
| 200 | if (ret) |
| 201 | xr_set_reg_uart(port, XR21V141X_REG_ENABLE, 0); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static int xr_uart_disable(struct usb_serial_port *port) |
| 207 | { |
| 208 | int ret; |
| 209 | |
| 210 | ret = xr_set_reg_uart(port, XR21V141X_REG_ENABLE, 0); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, 0); |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int xr_tiocmget(struct tty_struct *tty) |
| 220 | { |
| 221 | struct usb_serial_port *port = tty->driver_data; |
| 222 | u8 status; |
| 223 | int ret; |
| 224 | |
| 225 | ret = xr_get_reg_uart(port, XR21V141X_REG_GPIO_STATUS, &status); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
| 229 | /* |
| 230 | * Modem control pins are active low, so reading '0' means it is active |
| 231 | * and '1' means not active. |
| 232 | */ |
| 233 | ret = ((status & XR21V141X_UART_MODE_DTR) ? 0 : TIOCM_DTR) | |
| 234 | ((status & XR21V141X_UART_MODE_RTS) ? 0 : TIOCM_RTS) | |
| 235 | ((status & XR21V141X_UART_MODE_CTS) ? 0 : TIOCM_CTS) | |
| 236 | ((status & XR21V141X_UART_MODE_DSR) ? 0 : TIOCM_DSR) | |
| 237 | ((status & XR21V141X_UART_MODE_RI) ? 0 : TIOCM_RI) | |
| 238 | ((status & XR21V141X_UART_MODE_CD) ? 0 : TIOCM_CD); |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static int xr_tiocmset_port(struct usb_serial_port *port, |
| 244 | unsigned int set, unsigned int clear) |
| 245 | { |
| 246 | u8 gpio_set = 0; |
| 247 | u8 gpio_clr = 0; |
| 248 | int ret = 0; |
| 249 | |
| 250 | /* Modem control pins are active low, so set & clr are swapped */ |
| 251 | if (set & TIOCM_RTS) |
| 252 | gpio_clr |= XR21V141X_UART_MODE_RTS; |
| 253 | if (set & TIOCM_DTR) |
| 254 | gpio_clr |= XR21V141X_UART_MODE_DTR; |
| 255 | if (clear & TIOCM_RTS) |
| 256 | gpio_set |= XR21V141X_UART_MODE_RTS; |
| 257 | if (clear & TIOCM_DTR) |
| 258 | gpio_set |= XR21V141X_UART_MODE_DTR; |
| 259 | |
| 260 | /* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */ |
| 261 | if (gpio_clr) |
| 262 | ret = xr_set_reg_uart(port, XR21V141X_REG_GPIO_CLR, gpio_clr); |
| 263 | |
| 264 | if (gpio_set) |
| 265 | ret = xr_set_reg_uart(port, XR21V141X_REG_GPIO_SET, gpio_set); |
| 266 | |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int xr_tiocmset(struct tty_struct *tty, |
| 271 | unsigned int set, unsigned int clear) |
| 272 | { |
| 273 | struct usb_serial_port *port = tty->driver_data; |
| 274 | |
| 275 | return xr_tiocmset_port(port, set, clear); |
| 276 | } |
| 277 | |
| 278 | static void xr_dtr_rts(struct usb_serial_port *port, int on) |
| 279 | { |
| 280 | if (on) |
| 281 | xr_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0); |
| 282 | else |
| 283 | xr_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS); |
| 284 | } |
| 285 | |
| 286 | static void xr_break_ctl(struct tty_struct *tty, int break_state) |
| 287 | { |
| 288 | struct usb_serial_port *port = tty->driver_data; |
| 289 | u8 state; |
| 290 | |
| 291 | if (break_state == 0) |
| 292 | state = XR21V141X_UART_BREAK_OFF; |
| 293 | else |
| 294 | state = XR21V141X_UART_BREAK_ON; |
| 295 | |
| 296 | dev_dbg(&port->dev, "Turning break %s\n", |
| 297 | state == XR21V141X_UART_BREAK_OFF ? "off" : "on"); |
| 298 | xr_set_reg_uart(port, XR21V141X_REG_TX_BREAK, state); |
| 299 | } |
| 300 | |
| 301 | /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */ |
| 302 | static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks[] = { |
| 303 | { 0x000, 0x000, 0x000 }, |
| 304 | { 0x000, 0x000, 0x000 }, |
| 305 | { 0x100, 0x000, 0x100 }, |
| 306 | { 0x020, 0x400, 0x020 }, |
| 307 | { 0x010, 0x100, 0x010 }, |
| 308 | { 0x208, 0x040, 0x208 }, |
| 309 | { 0x104, 0x820, 0x108 }, |
| 310 | { 0x844, 0x210, 0x884 }, |
| 311 | { 0x444, 0x110, 0x444 }, |
| 312 | { 0x122, 0x888, 0x224 }, |
| 313 | { 0x912, 0x448, 0x924 }, |
| 314 | { 0x492, 0x248, 0x492 }, |
| 315 | { 0x252, 0x928, 0x292 }, |
| 316 | { 0x94a, 0x4a4, 0xa52 }, |
| 317 | { 0x52a, 0xaa4, 0x54a }, |
| 318 | { 0xaaa, 0x954, 0x4aa }, |
| 319 | { 0xaaa, 0x554, 0xaaa }, |
| 320 | { 0x555, 0xad4, 0x5aa }, |
| 321 | { 0xb55, 0xab4, 0x55a }, |
| 322 | { 0x6b5, 0x5ac, 0xb56 }, |
| 323 | { 0x5b5, 0xd6c, 0x6d6 }, |
| 324 | { 0xb6d, 0xb6a, 0xdb6 }, |
| 325 | { 0x76d, 0x6da, 0xbb6 }, |
| 326 | { 0xedd, 0xdda, 0x76e }, |
| 327 | { 0xddd, 0xbba, 0xeee }, |
| 328 | { 0x7bb, 0xf7a, 0xdde }, |
| 329 | { 0xf7b, 0xef6, 0x7de }, |
| 330 | { 0xdf7, 0xbf6, 0xf7e }, |
| 331 | { 0x7f7, 0xfee, 0xefe }, |
| 332 | { 0xfdf, 0xfbe, 0x7fe }, |
| 333 | { 0xf7f, 0xefe, 0xffe }, |
| 334 | { 0xfff, 0xffe, 0xffd }, |
| 335 | }; |
| 336 | |
| 337 | static int xr_set_baudrate(struct tty_struct *tty, |
| 338 | struct usb_serial_port *port) |
| 339 | { |
| 340 | u32 divisor, baud, idx; |
| 341 | u16 tx_mask, rx_mask; |
| 342 | int ret; |
| 343 | |
| 344 | baud = clamp(tty->termios.c_ospeed, XR21V141X_MIN_SPEED, |
| 345 | XR21V141X_MAX_SPEED); |
| 346 | divisor = XR_INT_OSC_HZ / baud; |
| 347 | idx = ((32 * XR_INT_OSC_HZ) / baud) & 0x1f; |
| 348 | tx_mask = xr21v141x_txrx_clk_masks[idx].tx; |
| 349 | |
| 350 | if (divisor & 0x01) |
| 351 | rx_mask = xr21v141x_txrx_clk_masks[idx].rx1; |
| 352 | else |
| 353 | rx_mask = xr21v141x_txrx_clk_masks[idx].rx0; |
| 354 | |
| 355 | dev_dbg(&port->dev, "Setting baud rate: %u\n", baud); |
| 356 | /* |
| 357 | * XR21V141X uses fractional baud rate generator with 48MHz internal |
| 358 | * oscillator and 19-bit programmable divisor. So theoretically it can |
| 359 | * generate most commonly used baud rates with high accuracy. |
| 360 | */ |
| 361 | ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_0, |
| 362 | divisor & 0xff); |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | |
| 366 | ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_1, |
| 367 | (divisor >> 8) & 0xff); |
| 368 | if (ret) |
| 369 | return ret; |
| 370 | |
| 371 | ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_2, |
| 372 | (divisor >> 16) & 0xff); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_0, |
| 377 | tx_mask & 0xff); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | |
| 381 | ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_1, |
| 382 | (tx_mask >> 8) & 0xff); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
| 386 | ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_0, |
| 387 | rx_mask & 0xff); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | |
| 391 | ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_1, |
| 392 | (rx_mask >> 8) & 0xff); |
| 393 | if (ret) |
| 394 | return ret; |
| 395 | |
| 396 | tty_encode_baud_rate(tty, baud, baud); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static void xr_set_flow_mode(struct tty_struct *tty, |
| 402 | struct usb_serial_port *port) |
| 403 | { |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 404 | u8 flow, gpio_mode; |
| 405 | int ret; |
| 406 | |
| 407 | ret = xr_get_reg_uart(port, XR21V141X_REG_GPIO_MODE, &gpio_mode); |
| 408 | if (ret) |
| 409 | return; |
| 410 | |
Johan Hovold | 5c5d9af | 2021-01-21 11:29:16 +0100 | [diff] [blame^] | 411 | if (C_CRTSCTS(tty)) { |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 412 | dev_dbg(&port->dev, "Enabling hardware flow ctrl\n"); |
| 413 | |
| 414 | /* |
| 415 | * RTS/CTS is the default flow control mode, so set GPIO mode |
| 416 | * for controlling the pins manually by default. |
| 417 | */ |
| 418 | gpio_mode &= ~XR21V141X_UART_MODE_GPIO_MASK; |
| 419 | gpio_mode |= XR21V141X_UART_MODE_RTS_CTS; |
| 420 | flow = XR21V141X_UART_FLOW_MODE_HW; |
| 421 | } else if (I_IXON(tty)) { |
| 422 | u8 start_char = START_CHAR(tty); |
| 423 | u8 stop_char = STOP_CHAR(tty); |
| 424 | |
| 425 | dev_dbg(&port->dev, "Enabling sw flow ctrl\n"); |
| 426 | flow = XR21V141X_UART_FLOW_MODE_SW; |
| 427 | |
| 428 | xr_set_reg_uart(port, XR21V141X_REG_XON_CHAR, start_char); |
| 429 | xr_set_reg_uart(port, XR21V141X_REG_XOFF_CHAR, stop_char); |
| 430 | } else { |
| 431 | dev_dbg(&port->dev, "Disabling flow ctrl\n"); |
| 432 | flow = XR21V141X_UART_FLOW_MODE_NONE; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * As per the datasheet, UART needs to be disabled while writing to |
| 437 | * FLOW_CONTROL register. |
| 438 | */ |
| 439 | xr_uart_disable(port); |
| 440 | xr_set_reg_uart(port, XR21V141X_REG_FLOW_CTRL, flow); |
| 441 | xr_uart_enable(port); |
| 442 | |
| 443 | xr_set_reg_uart(port, XR21V141X_REG_GPIO_MODE, gpio_mode); |
| 444 | } |
| 445 | |
| 446 | static void xr_set_termios(struct tty_struct *tty, |
| 447 | struct usb_serial_port *port, |
| 448 | struct ktermios *old_termios) |
| 449 | { |
| 450 | struct ktermios *termios = &tty->termios; |
| 451 | u8 bits = 0; |
| 452 | int ret; |
| 453 | |
| 454 | if ((old_termios && tty->termios.c_ospeed != old_termios->c_ospeed) || |
| 455 | !old_termios) |
| 456 | xr_set_baudrate(tty, port); |
| 457 | |
| 458 | switch (C_CSIZE(tty)) { |
| 459 | case CS5: |
| 460 | case CS6: |
| 461 | /* CS5 and CS6 are not supported, so just restore old setting */ |
| 462 | termios->c_cflag &= ~CSIZE; |
| 463 | if (old_termios) |
| 464 | termios->c_cflag |= old_termios->c_cflag & CSIZE; |
| 465 | else |
| 466 | bits |= XR21V141X_UART_DATA_8; |
| 467 | break; |
| 468 | case CS7: |
| 469 | bits |= XR21V141X_UART_DATA_7; |
| 470 | break; |
| 471 | case CS8: |
| 472 | default: |
| 473 | bits |= XR21V141X_UART_DATA_8; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (C_PARENB(tty)) { |
| 478 | if (C_CMSPAR(tty)) { |
| 479 | if (C_PARODD(tty)) |
| 480 | bits |= XR21V141X_UART_PARITY_MARK << |
| 481 | XR21V141X_UART_PARITY_SHIFT; |
| 482 | else |
| 483 | bits |= XR21V141X_UART_PARITY_SPACE << |
| 484 | XR21V141X_UART_PARITY_SHIFT; |
| 485 | } else { |
| 486 | if (C_PARODD(tty)) |
| 487 | bits |= XR21V141X_UART_PARITY_ODD << |
| 488 | XR21V141X_UART_PARITY_SHIFT; |
| 489 | else |
| 490 | bits |= XR21V141X_UART_PARITY_EVEN << |
| 491 | XR21V141X_UART_PARITY_SHIFT; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | if (C_CSTOPB(tty)) |
| 496 | bits |= XR21V141X_UART_STOP_2 << XR21V141X_UART_STOP_SHIFT; |
| 497 | else |
| 498 | bits |= XR21V141X_UART_STOP_1 << XR21V141X_UART_STOP_SHIFT; |
| 499 | |
| 500 | ret = xr_set_reg_uart(port, XR21V141X_REG_FORMAT, bits); |
| 501 | if (ret) |
| 502 | return; |
| 503 | |
| 504 | /* If baud rate is B0, clear DTR and RTS */ |
| 505 | if (C_BAUD(tty) == B0) |
| 506 | xr_dtr_rts(port, 0); |
| 507 | |
| 508 | xr_set_flow_mode(tty, port); |
| 509 | } |
| 510 | |
| 511 | static int xr_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 512 | { |
| 513 | int ret; |
| 514 | |
| 515 | ret = xr_uart_enable(port); |
| 516 | if (ret) { |
| 517 | dev_err(&port->dev, "Failed to enable UART\n"); |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | /* Setup termios */ |
| 522 | if (tty) |
| 523 | xr_set_termios(tty, port, NULL); |
| 524 | |
| 525 | ret = usb_serial_generic_open(tty, port); |
| 526 | if (ret) { |
| 527 | xr_uart_disable(port); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static void xr_close(struct usb_serial_port *port) |
| 535 | { |
| 536 | usb_serial_generic_close(port); |
| 537 | |
| 538 | xr_uart_disable(port); |
| 539 | } |
| 540 | |
| 541 | static int xr_probe(struct usb_serial *serial, const struct usb_device_id *id) |
| 542 | { |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 543 | struct usb_driver *driver = serial->type->usb_driver; |
| 544 | struct usb_interface *control_interface; |
| 545 | int ret; |
| 546 | |
| 547 | /* Don't bind to control interface */ |
| 548 | if (serial->interface->cur_altsetting->desc.bInterfaceNumber == 0) |
| 549 | return -ENODEV; |
| 550 | |
| 551 | /* But claim the control interface during data interface probe */ |
Johan Hovold | 9ffa6ec | 2021-01-21 11:29:15 +0100 | [diff] [blame] | 552 | control_interface = usb_ifnum_to_if(serial->dev, 0); |
Johan Hovold | a38d214 | 2021-01-21 11:29:13 +0100 | [diff] [blame] | 553 | if (!control_interface) |
| 554 | return -ENODEV; |
| 555 | |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 556 | ret = usb_driver_claim_interface(driver, control_interface, NULL); |
| 557 | if (ret) { |
| 558 | dev_err(&serial->interface->dev, "Failed to claim control interface\n"); |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
Johan Hovold | 54c98d9 | 2021-01-21 11:29:14 +0100 | [diff] [blame] | 565 | static void xr_disconnect(struct usb_serial *serial) |
| 566 | { |
| 567 | struct usb_driver *driver = serial->type->usb_driver; |
| 568 | struct usb_interface *control_interface; |
| 569 | |
| 570 | control_interface = usb_ifnum_to_if(serial->dev, 0); |
| 571 | usb_driver_release_interface(driver, control_interface); |
| 572 | } |
| 573 | |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 574 | static const struct usb_device_id id_table[] = { |
| 575 | { USB_DEVICE(0x04e2, 0x1410) }, /* XR21V141X */ |
| 576 | { } |
| 577 | }; |
| 578 | MODULE_DEVICE_TABLE(usb, id_table); |
| 579 | |
| 580 | static struct usb_serial_driver xr_device = { |
| 581 | .driver = { |
| 582 | .owner = THIS_MODULE, |
| 583 | .name = "xr_serial", |
| 584 | }, |
| 585 | .id_table = id_table, |
| 586 | .num_ports = 1, |
| 587 | .probe = xr_probe, |
Johan Hovold | 54c98d9 | 2021-01-21 11:29:14 +0100 | [diff] [blame] | 588 | .disconnect = xr_disconnect, |
Manivannan Sadhasivam | c2d405a | 2020-11-22 22:38:20 +0530 | [diff] [blame] | 589 | .open = xr_open, |
| 590 | .close = xr_close, |
| 591 | .break_ctl = xr_break_ctl, |
| 592 | .set_termios = xr_set_termios, |
| 593 | .tiocmget = xr_tiocmget, |
| 594 | .tiocmset = xr_tiocmset, |
| 595 | .dtr_rts = xr_dtr_rts |
| 596 | }; |
| 597 | |
| 598 | static struct usb_serial_driver * const serial_drivers[] = { |
| 599 | &xr_device, NULL |
| 600 | }; |
| 601 | |
| 602 | module_usb_serial_driver(serial_drivers, id_table); |
| 603 | |
| 604 | MODULE_AUTHOR("Manivannan Sadhasivam <mani@kernel.org>"); |
| 605 | MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver"); |
| 606 | MODULE_LICENSE("GPL"); |