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 * Copyright (c) 2021 Johan Hovold <johan@kernel.org>
7 *
8 * Based on the initial driver written by Patong Yang:
9 *
10 * https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop
11 *
12 * Copyright (c) 2018 Patong Yang <patong.mxl@gmail.com>
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/uaccess.h>
20 #include <linux/usb.h>
21 #include <linux/usb/cdc.h>
22 #include <linux/usb/serial.h>
23
24 struct xr_txrx_clk_mask {
25 u16 tx;
26 u16 rx0;
27 u16 rx1;
28 };
29
30 #define XR_INT_OSC_HZ 48000000U
31 #define XR21V141X_MIN_SPEED 46U
32 #define XR21V141X_MAX_SPEED XR_INT_OSC_HZ
33
34 /* XR21V141X register blocks */
35 #define XR21V141X_UART_REG_BLOCK 0
36 #define XR21V141X_UM_REG_BLOCK 4
37 #define XR21V141X_UART_CUSTOM_BLOCK 0x66
38
39 /* XR21V141X UART registers */
40 #define XR21V141X_CLOCK_DIVISOR_0 0x04
41 #define XR21V141X_CLOCK_DIVISOR_1 0x05
42 #define XR21V141X_CLOCK_DIVISOR_2 0x06
43 #define XR21V141X_TX_CLOCK_MASK_0 0x07
44 #define XR21V141X_TX_CLOCK_MASK_1 0x08
45 #define XR21V141X_RX_CLOCK_MASK_0 0x09
46 #define XR21V141X_RX_CLOCK_MASK_1 0x0a
47 #define XR21V141X_REG_FORMAT 0x0b
48
49 /* XR21V141X UART Manager registers */
50 #define XR21V141X_UM_FIFO_ENABLE_REG 0x10
51 #define XR21V141X_UM_ENABLE_TX_FIFO 0x01
52 #define XR21V141X_UM_ENABLE_RX_FIFO 0x02
53
54 #define XR21V141X_UM_RX_FIFO_RESET 0x18
55 #define XR21V141X_UM_TX_FIFO_RESET 0x1c
56
57 #define XR_UART_ENABLE_TX 0x1
58 #define XR_UART_ENABLE_RX 0x2
59
60 #define XR_GPIO_RI BIT(0)
61 #define XR_GPIO_CD BIT(1)
62 #define XR_GPIO_DSR BIT(2)
63 #define XR_GPIO_DTR BIT(3)
64 #define XR_GPIO_CTS BIT(4)
65 #define XR_GPIO_RTS BIT(5)
66 #define XR_GPIO_CLK BIT(6)
67 #define XR_GPIO_XEN BIT(7)
68 #define XR_GPIO_TXT BIT(8)
69 #define XR_GPIO_RXT BIT(9)
70
71 #define XR_UART_DATA_MASK GENMASK(3, 0)
72 #define XR_UART_DATA_7 0x7
73 #define XR_UART_DATA_8 0x8
74
75 #define XR_UART_PARITY_MASK GENMASK(6, 4)
76 #define XR_UART_PARITY_SHIFT 4
77 #define XR_UART_PARITY_NONE (0x0 << XR_UART_PARITY_SHIFT)
78 #define XR_UART_PARITY_ODD (0x1 << XR_UART_PARITY_SHIFT)
79 #define XR_UART_PARITY_EVEN (0x2 << XR_UART_PARITY_SHIFT)
80 #define XR_UART_PARITY_MARK (0x3 << XR_UART_PARITY_SHIFT)
81 #define XR_UART_PARITY_SPACE (0x4 << XR_UART_PARITY_SHIFT)
82
83 #define XR_UART_STOP_MASK BIT(7)
84 #define XR_UART_STOP_SHIFT 7
85 #define XR_UART_STOP_1 (0x0 << XR_UART_STOP_SHIFT)
86 #define XR_UART_STOP_2 (0x1 << XR_UART_STOP_SHIFT)
87
88 #define XR_UART_FLOW_MODE_NONE 0x0
89 #define XR_UART_FLOW_MODE_HW 0x1
90 #define XR_UART_FLOW_MODE_SW 0x2
91
92 #define XR_GPIO_MODE_SEL_MASK GENMASK(2, 0)
93 #define XR_GPIO_MODE_SEL_RTS_CTS 0x1
94 #define XR_GPIO_MODE_SEL_DTR_DSR 0x2
95 #define XR_GPIO_MODE_SEL_RS485 0x3
96 #define XR_GPIO_MODE_SEL_RS485_ADDR 0x4
97 #define XR_GPIO_MODE_RS485_TX_H 0x8
98 #define XR_GPIO_MODE_TX_TOGGLE 0x100
99 #define XR_GPIO_MODE_RX_TOGGLE 0x200
100
101 #define XR_FIFO_RESET 0x1
102
103 #define XR_CUSTOM_DRIVER_ACTIVE 0x1
104
105 static int xr21v141x_uart_enable(struct usb_serial_port *port);
106 static int xr21v141x_uart_disable(struct usb_serial_port *port);
107 static int xr21v141x_fifo_reset(struct usb_serial_port *port);
108 static void xr21v141x_set_line_settings(struct tty_struct *tty,
109 struct usb_serial_port *port,
110 const struct ktermios *old_termios);
111
112 struct xr_type {
113 int reg_width;
114 u8 reg_recipient;
115 u8 set_reg;
116 u8 get_reg;
117
118 u16 uart_enable;
119 u16 flow_control;
120 u16 xon_char;
121 u16 xoff_char;
122 u16 tx_break;
123 u16 gpio_mode;
124 u16 gpio_direction;
125 u16 gpio_set;
126 u16 gpio_clear;
127 u16 gpio_status;
128 u16 tx_fifo_reset;
129 u16 rx_fifo_reset;
130 u16 custom_driver;
131
132 bool have_5_6_bit_mode;
133 bool have_xmit_toggle;
134
135 int (*enable)(struct usb_serial_port *port);
136 int (*disable)(struct usb_serial_port *port);
137 int (*fifo_reset)(struct usb_serial_port *port);
138 void (*set_line_settings)(struct tty_struct *tty,
139 struct usb_serial_port *port,
140 const struct ktermios *old_termios);
141 };
142
143 enum xr_type_id {
144 XR21V141X,
145 XR21B142X,
146 XR21B1411,
147 XR2280X,
148 XR_TYPE_COUNT,
149 };
150
151 static const struct xr_type xr_types[] = {
152 [XR21V141X] = {
153 .reg_width = 8,
154 .reg_recipient = USB_RECIP_DEVICE,
155 .set_reg = 0x00,
156 .get_reg = 0x01,
157
158 .uart_enable = 0x03,
159 .flow_control = 0x0c,
160 .xon_char = 0x10,
161 .xoff_char = 0x11,
162 .tx_break = 0x14,
163 .gpio_mode = 0x1a,
164 .gpio_direction = 0x1b,
165 .gpio_set = 0x1d,
166 .gpio_clear = 0x1e,
167 .gpio_status = 0x1f,
168
169 .enable = xr21v141x_uart_enable,
170 .disable = xr21v141x_uart_disable,
171 .fifo_reset = xr21v141x_fifo_reset,
172 .set_line_settings = xr21v141x_set_line_settings,
173 },
174 [XR21B142X] = {
175 .reg_width = 16,
176 .reg_recipient = USB_RECIP_INTERFACE,
177 .set_reg = 0x00,
178 .get_reg = 0x00,
179
180 .uart_enable = 0x00,
181 .flow_control = 0x06,
182 .xon_char = 0x07,
183 .xoff_char = 0x08,
184 .tx_break = 0x0a,
185 .gpio_mode = 0x0c,
186 .gpio_direction = 0x0d,
187 .gpio_set = 0x0e,
188 .gpio_clear = 0x0f,
189 .gpio_status = 0x10,
190 .tx_fifo_reset = 0x40,
191 .rx_fifo_reset = 0x43,
192 .custom_driver = 0x60,
193
194 .have_5_6_bit_mode = true,
195 .have_xmit_toggle = true,
196 },
197 [XR21B1411] = {
198 .reg_width = 12,
199 .reg_recipient = USB_RECIP_DEVICE,
200 .set_reg = 0x00,
201 .get_reg = 0x01,
202
203 .uart_enable = 0xc00,
204 .flow_control = 0xc06,
205 .xon_char = 0xc07,
206 .xoff_char = 0xc08,
207 .tx_break = 0xc0a,
208 .gpio_mode = 0xc0c,
209 .gpio_direction = 0xc0d,
210 .gpio_set = 0xc0e,
211 .gpio_clear = 0xc0f,
212 .gpio_status = 0xc10,
213 .tx_fifo_reset = 0xc80,
214 .rx_fifo_reset = 0xcc0,
215 .custom_driver = 0x20d,
216 },
217 [XR2280X] = {
218 .reg_width = 16,
219 .reg_recipient = USB_RECIP_DEVICE,
220 .set_reg = 0x05,
221 .get_reg = 0x05,
222
223 .uart_enable = 0x40,
224 .flow_control = 0x46,
225 .xon_char = 0x47,
226 .xoff_char = 0x48,
227 .tx_break = 0x4a,
228 .gpio_mode = 0x4c,
229 .gpio_direction = 0x4d,
230 .gpio_set = 0x4e,
231 .gpio_clear = 0x4f,
232 .gpio_status = 0x50,
233 .tx_fifo_reset = 0x60,
234 .rx_fifo_reset = 0x63,
235 .custom_driver = 0x81,
236 },
237 };
238
239 struct xr_data {
240 const struct xr_type *type;
241 u8 channel; /* zero-based index or interface number */
242 struct serial_rs485 rs485;
243 };
244
xr_set_reg(struct usb_serial_port * port,u8 channel,u16 reg,u16 val)245 static int xr_set_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 val)
246 {
247 struct xr_data *data = usb_get_serial_port_data(port);
248 const struct xr_type *type = data->type;
249 struct usb_serial *serial = port->serial;
250 int ret;
251
252 ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
253 type->set_reg,
254 USB_DIR_OUT | USB_TYPE_VENDOR | type->reg_recipient,
255 val, (channel << 8) | reg, NULL, 0,
256 USB_CTRL_SET_TIMEOUT);
257 if (ret < 0) {
258 dev_err(&port->dev, "Failed to set reg 0x%02x: %d\n", reg, ret);
259 return ret;
260 }
261
262 return 0;
263 }
264
xr_get_reg(struct usb_serial_port * port,u8 channel,u16 reg,u16 * val)265 static int xr_get_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 *val)
266 {
267 struct xr_data *data = usb_get_serial_port_data(port);
268 const struct xr_type *type = data->type;
269 struct usb_serial *serial = port->serial;
270 u8 *dmabuf;
271 int ret, len;
272
273 if (type->reg_width == 8)
274 len = 1;
275 else
276 len = 2;
277
278 dmabuf = kmalloc(len, GFP_KERNEL);
279 if (!dmabuf)
280 return -ENOMEM;
281
282 ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
283 type->get_reg,
284 USB_DIR_IN | USB_TYPE_VENDOR | type->reg_recipient,
285 0, (channel << 8) | reg, dmabuf, len,
286 USB_CTRL_GET_TIMEOUT);
287 if (ret == len) {
288 if (len == 2)
289 *val = le16_to_cpup((__le16 *)dmabuf);
290 else
291 *val = *dmabuf;
292 ret = 0;
293 } else {
294 dev_err(&port->dev, "Failed to get reg 0x%02x: %d\n", reg, ret);
295 if (ret >= 0)
296 ret = -EIO;
297 }
298
299 kfree(dmabuf);
300
301 return ret;
302 }
303
xr_set_reg_uart(struct usb_serial_port * port,u16 reg,u16 val)304 static int xr_set_reg_uart(struct usb_serial_port *port, u16 reg, u16 val)
305 {
306 struct xr_data *data = usb_get_serial_port_data(port);
307
308 return xr_set_reg(port, data->channel, reg, val);
309 }
310
xr_get_reg_uart(struct usb_serial_port * port,u16 reg,u16 * val)311 static int xr_get_reg_uart(struct usb_serial_port *port, u16 reg, u16 *val)
312 {
313 struct xr_data *data = usb_get_serial_port_data(port);
314
315 return xr_get_reg(port, data->channel, reg, val);
316 }
317
xr_set_reg_um(struct usb_serial_port * port,u8 reg_base,u8 val)318 static int xr_set_reg_um(struct usb_serial_port *port, u8 reg_base, u8 val)
319 {
320 struct xr_data *data = usb_get_serial_port_data(port);
321 u8 reg;
322
323 reg = reg_base + data->channel;
324
325 return xr_set_reg(port, XR21V141X_UM_REG_BLOCK, reg, val);
326 }
327
__xr_uart_enable(struct usb_serial_port * port)328 static int __xr_uart_enable(struct usb_serial_port *port)
329 {
330 struct xr_data *data = usb_get_serial_port_data(port);
331
332 return xr_set_reg_uart(port, data->type->uart_enable,
333 XR_UART_ENABLE_TX | XR_UART_ENABLE_RX);
334 }
335
__xr_uart_disable(struct usb_serial_port * port)336 static int __xr_uart_disable(struct usb_serial_port *port)
337 {
338 struct xr_data *data = usb_get_serial_port_data(port);
339
340 return xr_set_reg_uart(port, data->type->uart_enable, 0);
341 }
342
343 /*
344 * According to datasheet, below is the recommended sequence for enabling UART
345 * module in XR21V141X:
346 *
347 * Enable Tx FIFO
348 * Enable Tx and Rx
349 * Enable Rx FIFO
350 */
xr21v141x_uart_enable(struct usb_serial_port * port)351 static int xr21v141x_uart_enable(struct usb_serial_port *port)
352 {
353 int ret;
354
355 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
356 XR21V141X_UM_ENABLE_TX_FIFO);
357 if (ret)
358 return ret;
359
360 ret = __xr_uart_enable(port);
361 if (ret)
362 return ret;
363
364 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
365 XR21V141X_UM_ENABLE_TX_FIFO | XR21V141X_UM_ENABLE_RX_FIFO);
366 if (ret)
367 __xr_uart_disable(port);
368
369 return ret;
370 }
371
xr21v141x_uart_disable(struct usb_serial_port * port)372 static int xr21v141x_uart_disable(struct usb_serial_port *port)
373 {
374 int ret;
375
376 ret = __xr_uart_disable(port);
377 if (ret)
378 return ret;
379
380 ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, 0);
381
382 return ret;
383 }
384
xr_uart_enable(struct usb_serial_port * port)385 static int xr_uart_enable(struct usb_serial_port *port)
386 {
387 struct xr_data *data = usb_get_serial_port_data(port);
388
389 if (data->type->enable)
390 return data->type->enable(port);
391
392 return __xr_uart_enable(port);
393 }
394
xr_uart_disable(struct usb_serial_port * port)395 static int xr_uart_disable(struct usb_serial_port *port)
396 {
397 struct xr_data *data = usb_get_serial_port_data(port);
398
399 if (data->type->disable)
400 return data->type->disable(port);
401
402 return __xr_uart_disable(port);
403 }
404
xr21v141x_fifo_reset(struct usb_serial_port * port)405 static int xr21v141x_fifo_reset(struct usb_serial_port *port)
406 {
407 int ret;
408
409 ret = xr_set_reg_um(port, XR21V141X_UM_TX_FIFO_RESET, XR_FIFO_RESET);
410 if (ret)
411 return ret;
412
413 ret = xr_set_reg_um(port, XR21V141X_UM_RX_FIFO_RESET, XR_FIFO_RESET);
414 if (ret)
415 return ret;
416
417 return 0;
418 }
419
xr_fifo_reset(struct usb_serial_port * port)420 static int xr_fifo_reset(struct usb_serial_port *port)
421 {
422 struct xr_data *data = usb_get_serial_port_data(port);
423 int ret;
424
425 if (data->type->fifo_reset)
426 return data->type->fifo_reset(port);
427
428 ret = xr_set_reg_uart(port, data->type->tx_fifo_reset, XR_FIFO_RESET);
429 if (ret)
430 return ret;
431
432 ret = xr_set_reg_uart(port, data->type->rx_fifo_reset, XR_FIFO_RESET);
433 if (ret)
434 return ret;
435
436 return 0;
437 }
438
xr_tiocmget(struct tty_struct * tty)439 static int xr_tiocmget(struct tty_struct *tty)
440 {
441 struct usb_serial_port *port = tty->driver_data;
442 struct xr_data *data = usb_get_serial_port_data(port);
443 u16 status;
444 int ret;
445
446 ret = xr_get_reg_uart(port, data->type->gpio_status, &status);
447 if (ret)
448 return ret;
449
450 /*
451 * Modem control pins are active low, so reading '0' means it is active
452 * and '1' means not active.
453 */
454 ret = ((status & XR_GPIO_DTR) ? 0 : TIOCM_DTR) |
455 ((status & XR_GPIO_RTS) ? 0 : TIOCM_RTS) |
456 ((status & XR_GPIO_CTS) ? 0 : TIOCM_CTS) |
457 ((status & XR_GPIO_DSR) ? 0 : TIOCM_DSR) |
458 ((status & XR_GPIO_RI) ? 0 : TIOCM_RI) |
459 ((status & XR_GPIO_CD) ? 0 : TIOCM_CD);
460
461 return ret;
462 }
463
xr_tiocmset_port(struct usb_serial_port * port,unsigned int set,unsigned int clear)464 static int xr_tiocmset_port(struct usb_serial_port *port,
465 unsigned int set, unsigned int clear)
466 {
467 struct xr_data *data = usb_get_serial_port_data(port);
468 const struct xr_type *type = data->type;
469 u16 gpio_set = 0;
470 u16 gpio_clr = 0;
471 int ret = 0;
472
473 /* Modem control pins are active low, so set & clr are swapped */
474 if (set & TIOCM_RTS)
475 gpio_clr |= XR_GPIO_RTS;
476 if (set & TIOCM_DTR)
477 gpio_clr |= XR_GPIO_DTR;
478 if (clear & TIOCM_RTS)
479 gpio_set |= XR_GPIO_RTS;
480 if (clear & TIOCM_DTR)
481 gpio_set |= XR_GPIO_DTR;
482
483 /* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */
484 if (gpio_clr)
485 ret = xr_set_reg_uart(port, type->gpio_clear, gpio_clr);
486
487 if (gpio_set)
488 ret = xr_set_reg_uart(port, type->gpio_set, gpio_set);
489
490 return ret;
491 }
492
xr_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)493 static int xr_tiocmset(struct tty_struct *tty,
494 unsigned int set, unsigned int clear)
495 {
496 struct usb_serial_port *port = tty->driver_data;
497
498 return xr_tiocmset_port(port, set, clear);
499 }
500
xr_dtr_rts(struct usb_serial_port * port,int on)501 static void xr_dtr_rts(struct usb_serial_port *port, int on)
502 {
503 if (on)
504 xr_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0);
505 else
506 xr_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS);
507 }
508
xr_break_ctl(struct tty_struct * tty,int break_state)509 static int xr_break_ctl(struct tty_struct *tty, int break_state)
510 {
511 struct usb_serial_port *port = tty->driver_data;
512 struct xr_data *data = usb_get_serial_port_data(port);
513 const struct xr_type *type = data->type;
514 u16 state;
515
516 if (break_state == 0)
517 state = 0;
518 else
519 state = GENMASK(type->reg_width - 1, 0);
520
521 dev_dbg(&port->dev, "Turning break %s\n", state == 0 ? "off" : "on");
522
523 return xr_set_reg_uart(port, type->tx_break, state);
524 }
525
526 /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */
527 static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks[] = {
528 { 0x000, 0x000, 0x000 },
529 { 0x000, 0x000, 0x000 },
530 { 0x100, 0x000, 0x100 },
531 { 0x020, 0x400, 0x020 },
532 { 0x010, 0x100, 0x010 },
533 { 0x208, 0x040, 0x208 },
534 { 0x104, 0x820, 0x108 },
535 { 0x844, 0x210, 0x884 },
536 { 0x444, 0x110, 0x444 },
537 { 0x122, 0x888, 0x224 },
538 { 0x912, 0x448, 0x924 },
539 { 0x492, 0x248, 0x492 },
540 { 0x252, 0x928, 0x292 },
541 { 0x94a, 0x4a4, 0xa52 },
542 { 0x52a, 0xaa4, 0x54a },
543 { 0xaaa, 0x954, 0x4aa },
544 { 0xaaa, 0x554, 0xaaa },
545 { 0x555, 0xad4, 0x5aa },
546 { 0xb55, 0xab4, 0x55a },
547 { 0x6b5, 0x5ac, 0xb56 },
548 { 0x5b5, 0xd6c, 0x6d6 },
549 { 0xb6d, 0xb6a, 0xdb6 },
550 { 0x76d, 0x6da, 0xbb6 },
551 { 0xedd, 0xdda, 0x76e },
552 { 0xddd, 0xbba, 0xeee },
553 { 0x7bb, 0xf7a, 0xdde },
554 { 0xf7b, 0xef6, 0x7de },
555 { 0xdf7, 0xbf6, 0xf7e },
556 { 0x7f7, 0xfee, 0xefe },
557 { 0xfdf, 0xfbe, 0x7fe },
558 { 0xf7f, 0xefe, 0xffe },
559 { 0xfff, 0xffe, 0xffd },
560 };
561
xr21v141x_set_baudrate(struct tty_struct * tty,struct usb_serial_port * port)562 static int xr21v141x_set_baudrate(struct tty_struct *tty, struct usb_serial_port *port)
563 {
564 u32 divisor, baud, idx;
565 u16 tx_mask, rx_mask;
566 int ret;
567
568 baud = tty->termios.c_ospeed;
569 if (!baud)
570 return 0;
571
572 baud = clamp(baud, XR21V141X_MIN_SPEED, XR21V141X_MAX_SPEED);
573 divisor = XR_INT_OSC_HZ / baud;
574 idx = ((32 * XR_INT_OSC_HZ) / baud) & 0x1f;
575 tx_mask = xr21v141x_txrx_clk_masks[idx].tx;
576
577 if (divisor & 0x01)
578 rx_mask = xr21v141x_txrx_clk_masks[idx].rx1;
579 else
580 rx_mask = xr21v141x_txrx_clk_masks[idx].rx0;
581
582 dev_dbg(&port->dev, "Setting baud rate: %u\n", baud);
583 /*
584 * XR21V141X uses fractional baud rate generator with 48MHz internal
585 * oscillator and 19-bit programmable divisor. So theoretically it can
586 * generate most commonly used baud rates with high accuracy.
587 */
588 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_0,
589 divisor & 0xff);
590 if (ret)
591 return ret;
592
593 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_1,
594 (divisor >> 8) & 0xff);
595 if (ret)
596 return ret;
597
598 ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_2,
599 (divisor >> 16) & 0xff);
600 if (ret)
601 return ret;
602
603 ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_0,
604 tx_mask & 0xff);
605 if (ret)
606 return ret;
607
608 ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_1,
609 (tx_mask >> 8) & 0xff);
610 if (ret)
611 return ret;
612
613 ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_0,
614 rx_mask & 0xff);
615 if (ret)
616 return ret;
617
618 ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_1,
619 (rx_mask >> 8) & 0xff);
620 if (ret)
621 return ret;
622
623 tty_encode_baud_rate(tty, baud, baud);
624
625 return 0;
626 }
627
xr_set_flow_mode(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)628 static void xr_set_flow_mode(struct tty_struct *tty,
629 struct usb_serial_port *port,
630 const struct ktermios *old_termios)
631 {
632 struct xr_data *data = usb_get_serial_port_data(port);
633 const struct xr_type *type = data->type;
634 u16 flow, gpio_mode;
635 bool rs485_enabled;
636 int ret;
637
638 ret = xr_get_reg_uart(port, type->gpio_mode, &gpio_mode);
639 if (ret)
640 return;
641
642 /*
643 * According to the datasheets, the UART needs to be disabled while
644 * writing to the FLOW_CONTROL register (XR21V141X), or any register
645 * but GPIO_SET, GPIO_CLEAR, TX_BREAK and ERROR_STATUS (XR21B142X).
646 */
647 xr_uart_disable(port);
648
649 /* Set GPIO mode for controlling the pins manually by default. */
650 gpio_mode &= ~XR_GPIO_MODE_SEL_MASK;
651
652 rs485_enabled = !!(data->rs485.flags & SER_RS485_ENABLED);
653 if (rs485_enabled) {
654 dev_dbg(&port->dev, "Enabling RS-485\n");
655 gpio_mode |= XR_GPIO_MODE_SEL_RS485;
656 if (data->rs485.flags & SER_RS485_RTS_ON_SEND)
657 gpio_mode &= ~XR_GPIO_MODE_RS485_TX_H;
658 else
659 gpio_mode |= XR_GPIO_MODE_RS485_TX_H;
660 }
661
662 if (C_CRTSCTS(tty) && C_BAUD(tty) != B0 && !rs485_enabled) {
663 dev_dbg(&port->dev, "Enabling hardware flow ctrl\n");
664 gpio_mode |= XR_GPIO_MODE_SEL_RTS_CTS;
665 flow = XR_UART_FLOW_MODE_HW;
666 } else if (I_IXON(tty)) {
667 u8 start_char = START_CHAR(tty);
668 u8 stop_char = STOP_CHAR(tty);
669
670 dev_dbg(&port->dev, "Enabling sw flow ctrl\n");
671 flow = XR_UART_FLOW_MODE_SW;
672
673 xr_set_reg_uart(port, type->xon_char, start_char);
674 xr_set_reg_uart(port, type->xoff_char, stop_char);
675 } else {
676 dev_dbg(&port->dev, "Disabling flow ctrl\n");
677 flow = XR_UART_FLOW_MODE_NONE;
678 }
679
680 xr_set_reg_uart(port, type->flow_control, flow);
681 xr_set_reg_uart(port, type->gpio_mode, gpio_mode);
682
683 xr_uart_enable(port);
684
685 if (C_BAUD(tty) == B0)
686 xr_dtr_rts(port, 0);
687 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
688 xr_dtr_rts(port, 1);
689 }
690
xr21v141x_set_line_settings(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)691 static void xr21v141x_set_line_settings(struct tty_struct *tty,
692 struct usb_serial_port *port,
693 const struct ktermios *old_termios)
694 {
695 struct ktermios *termios = &tty->termios;
696 u8 bits = 0;
697 int ret;
698
699 if (!old_termios || (tty->termios.c_ospeed != old_termios->c_ospeed))
700 xr21v141x_set_baudrate(tty, port);
701
702 switch (C_CSIZE(tty)) {
703 case CS5:
704 case CS6:
705 /* CS5 and CS6 are not supported, so just restore old setting */
706 termios->c_cflag &= ~CSIZE;
707 if (old_termios)
708 termios->c_cflag |= old_termios->c_cflag & CSIZE;
709 else
710 termios->c_cflag |= CS8;
711
712 if (C_CSIZE(tty) == CS7)
713 bits |= XR_UART_DATA_7;
714 else
715 bits |= XR_UART_DATA_8;
716 break;
717 case CS7:
718 bits |= XR_UART_DATA_7;
719 break;
720 case CS8:
721 default:
722 bits |= XR_UART_DATA_8;
723 break;
724 }
725
726 if (C_PARENB(tty)) {
727 if (C_CMSPAR(tty)) {
728 if (C_PARODD(tty))
729 bits |= XR_UART_PARITY_MARK;
730 else
731 bits |= XR_UART_PARITY_SPACE;
732 } else {
733 if (C_PARODD(tty))
734 bits |= XR_UART_PARITY_ODD;
735 else
736 bits |= XR_UART_PARITY_EVEN;
737 }
738 }
739
740 if (C_CSTOPB(tty))
741 bits |= XR_UART_STOP_2;
742 else
743 bits |= XR_UART_STOP_1;
744
745 ret = xr_set_reg_uart(port, XR21V141X_REG_FORMAT, bits);
746 if (ret)
747 return;
748 }
749
xr_cdc_set_line_coding(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)750 static void xr_cdc_set_line_coding(struct tty_struct *tty,
751 struct usb_serial_port *port,
752 const struct ktermios *old_termios)
753 {
754 struct xr_data *data = usb_get_serial_port_data(port);
755 struct usb_host_interface *alt = port->serial->interface->cur_altsetting;
756 struct usb_device *udev = port->serial->dev;
757 struct usb_cdc_line_coding *lc;
758 int ret;
759
760 lc = kzalloc_obj(*lc);
761 if (!lc)
762 return;
763
764 if (tty->termios.c_ospeed)
765 lc->dwDTERate = cpu_to_le32(tty->termios.c_ospeed);
766 else
767 lc->dwDTERate = cpu_to_le32(9600);
768
769 if (C_CSTOPB(tty))
770 lc->bCharFormat = USB_CDC_2_STOP_BITS;
771 else
772 lc->bCharFormat = USB_CDC_1_STOP_BITS;
773
774 if (C_PARENB(tty)) {
775 if (C_CMSPAR(tty)) {
776 if (C_PARODD(tty))
777 lc->bParityType = USB_CDC_MARK_PARITY;
778 else
779 lc->bParityType = USB_CDC_SPACE_PARITY;
780 } else {
781 if (C_PARODD(tty))
782 lc->bParityType = USB_CDC_ODD_PARITY;
783 else
784 lc->bParityType = USB_CDC_EVEN_PARITY;
785 }
786 } else {
787 lc->bParityType = USB_CDC_NO_PARITY;
788 }
789
790 if (!data->type->have_5_6_bit_mode &&
791 (C_CSIZE(tty) == CS5 || C_CSIZE(tty) == CS6)) {
792 tty->termios.c_cflag &= ~CSIZE;
793 if (old_termios)
794 tty->termios.c_cflag |= old_termios->c_cflag & CSIZE;
795 else
796 tty->termios.c_cflag |= CS8;
797 }
798
799 switch (C_CSIZE(tty)) {
800 case CS5:
801 lc->bDataBits = 5;
802 break;
803 case CS6:
804 lc->bDataBits = 6;
805 break;
806 case CS7:
807 lc->bDataBits = 7;
808 break;
809 case CS8:
810 default:
811 lc->bDataBits = 8;
812 break;
813 }
814
815 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
816 USB_CDC_REQ_SET_LINE_CODING,
817 USB_TYPE_CLASS | USB_RECIP_INTERFACE,
818 0, alt->desc.bInterfaceNumber,
819 lc, sizeof(*lc), USB_CTRL_SET_TIMEOUT);
820 if (ret < 0)
821 dev_err(&port->dev, "Failed to set line coding: %d\n", ret);
822
823 kfree(lc);
824 }
825
xr_sanitize_serial_rs485(struct serial_rs485 * rs485)826 static void xr_sanitize_serial_rs485(struct serial_rs485 *rs485)
827 {
828 if (!(rs485->flags & SER_RS485_ENABLED)) {
829 memset(rs485, 0, sizeof(*rs485));
830 return;
831 }
832
833 /* RTS always toggles after TX */
834 if (rs485->flags & SER_RS485_RTS_ON_SEND)
835 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
836 else
837 rs485->flags |= SER_RS485_RTS_AFTER_SEND;
838
839 /* Only the flags are implemented at the moment */
840 rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
841 SER_RS485_RTS_AFTER_SEND;
842 rs485->delay_rts_before_send = 0;
843 rs485->delay_rts_after_send = 0;
844 memset(rs485->padding, 0, sizeof(rs485->padding));
845 }
846
xr_get_rs485_config(struct tty_struct * tty,struct serial_rs485 __user * argp)847 static int xr_get_rs485_config(struct tty_struct *tty,
848 struct serial_rs485 __user *argp)
849 {
850 struct usb_serial_port *port = tty->driver_data;
851 struct xr_data *data = usb_get_serial_port_data(port);
852
853 /* core holds port mutex */
854 if (copy_to_user(argp, &data->rs485, sizeof(data->rs485)))
855 return -EFAULT;
856
857 return 0;
858 }
859
xr_set_rs485_config(struct tty_struct * tty,struct serial_rs485 __user * argp)860 static int xr_set_rs485_config(struct tty_struct *tty,
861 struct serial_rs485 __user *argp)
862 {
863 struct usb_serial_port *port = tty->driver_data;
864 struct xr_data *data = usb_get_serial_port_data(port);
865 struct serial_rs485 rs485;
866
867 if (copy_from_user(&rs485, argp, sizeof(rs485)))
868 return -EFAULT;
869 xr_sanitize_serial_rs485(&rs485);
870
871 /* core holds termios rwsem and port mutex */
872 data->rs485 = rs485;
873 xr_set_flow_mode(tty, port, NULL);
874
875 if (copy_to_user(argp, &rs485, sizeof(rs485)))
876 return -EFAULT;
877
878 return 0;
879 }
880
xr_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)881 static int xr_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
882 {
883 void __user *argp = (void __user *)arg;
884
885 switch (cmd) {
886 case TIOCGRS485:
887 return xr_get_rs485_config(tty, argp);
888 case TIOCSRS485:
889 return xr_set_rs485_config(tty, argp);
890 }
891
892 return -ENOIOCTLCMD;
893 }
894
xr_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)895 static void xr_set_termios(struct tty_struct *tty,
896 struct usb_serial_port *port,
897 const struct ktermios *old_termios)
898 {
899 struct xr_data *data = usb_get_serial_port_data(port);
900
901 /*
902 * XR21V141X does not have a CUSTOM_DRIVER flag and always enters CDC
903 * mode upon receiving CDC requests.
904 */
905 if (data->type->set_line_settings)
906 data->type->set_line_settings(tty, port, old_termios);
907 else
908 xr_cdc_set_line_coding(tty, port, old_termios);
909
910 xr_set_flow_mode(tty, port, old_termios);
911 }
912
xr_open(struct tty_struct * tty,struct usb_serial_port * port)913 static int xr_open(struct tty_struct *tty, struct usb_serial_port *port)
914 {
915 int ret;
916
917 ret = xr_fifo_reset(port);
918 if (ret)
919 return ret;
920
921 ret = xr_uart_enable(port);
922 if (ret) {
923 dev_err(&port->dev, "Failed to enable UART\n");
924 return ret;
925 }
926
927 /* Setup termios */
928 if (tty)
929 xr_set_termios(tty, port, NULL);
930
931 ret = usb_serial_generic_open(tty, port);
932 if (ret) {
933 xr_uart_disable(port);
934 return ret;
935 }
936
937 return 0;
938 }
939
xr_close(struct usb_serial_port * port)940 static void xr_close(struct usb_serial_port *port)
941 {
942 usb_serial_generic_close(port);
943
944 xr_uart_disable(port);
945 }
946
xr_probe(struct usb_serial * serial,const struct usb_device_id * id)947 static int xr_probe(struct usb_serial *serial, const struct usb_device_id *id)
948 {
949 struct usb_interface *control = serial->interface;
950 struct usb_host_interface *alt = control->cur_altsetting;
951 struct usb_cdc_parsed_header hdrs;
952 struct usb_cdc_union_desc *desc;
953 struct usb_interface *data;
954 int ret;
955
956 ret = cdc_parse_cdc_header(&hdrs, control, alt->extra, alt->extralen);
957 if (ret < 0)
958 return -ENODEV;
959
960 desc = hdrs.usb_cdc_union_desc;
961 if (!desc)
962 return -ENODEV;
963
964 data = usb_ifnum_to_if(serial->dev, desc->bSlaveInterface0);
965 if (!data)
966 return -ENODEV;
967
968 ret = usb_serial_claim_interface(serial, data);
969 if (ret)
970 return ret;
971
972 usb_set_serial_data(serial, (void *)id->driver_info);
973
974 return 0;
975 }
976
xr_gpio_init(struct usb_serial_port * port,const struct xr_type * type)977 static int xr_gpio_init(struct usb_serial_port *port, const struct xr_type *type)
978 {
979 u16 mask, mode;
980 int ret;
981
982 /*
983 * Configure all pins as GPIO except for Receive and Transmit Toggle.
984 */
985 mode = 0;
986 if (type->have_xmit_toggle)
987 mode |= XR_GPIO_MODE_RX_TOGGLE | XR_GPIO_MODE_TX_TOGGLE;
988
989 ret = xr_set_reg_uart(port, type->gpio_mode, mode);
990 if (ret)
991 return ret;
992
993 /*
994 * Configure DTR and RTS as outputs and make sure they are deasserted
995 * (active low), and configure RI, CD, DSR and CTS as inputs.
996 */
997 mask = XR_GPIO_DTR | XR_GPIO_RTS;
998 ret = xr_set_reg_uart(port, type->gpio_direction, mask);
999 if (ret)
1000 return ret;
1001
1002 ret = xr_set_reg_uart(port, type->gpio_set, mask);
1003 if (ret)
1004 return ret;
1005
1006 return 0;
1007 }
1008
xr_port_probe(struct usb_serial_port * port)1009 static int xr_port_probe(struct usb_serial_port *port)
1010 {
1011 struct usb_interface_descriptor *desc;
1012 const struct xr_type *type;
1013 struct xr_data *data;
1014 enum xr_type_id type_id;
1015 int ret;
1016
1017 type_id = (int)(unsigned long)usb_get_serial_data(port->serial);
1018 type = &xr_types[type_id];
1019
1020 data = kzalloc_obj(*data);
1021 if (!data)
1022 return -ENOMEM;
1023
1024 data->type = type;
1025
1026 desc = &port->serial->interface->cur_altsetting->desc;
1027 if (type_id == XR21V141X)
1028 data->channel = desc->bInterfaceNumber / 2;
1029 else
1030 data->channel = desc->bInterfaceNumber;
1031
1032 usb_set_serial_port_data(port, data);
1033
1034 if (type->custom_driver) {
1035 ret = xr_set_reg_uart(port, type->custom_driver,
1036 XR_CUSTOM_DRIVER_ACTIVE);
1037 if (ret)
1038 goto err_free;
1039 }
1040
1041 ret = xr_gpio_init(port, type);
1042 if (ret)
1043 goto err_free;
1044
1045 return 0;
1046
1047 err_free:
1048 kfree(data);
1049
1050 return ret;
1051 }
1052
xr_port_remove(struct usb_serial_port * port)1053 static void xr_port_remove(struct usb_serial_port *port)
1054 {
1055 struct xr_data *data = usb_get_serial_port_data(port);
1056
1057 kfree(data);
1058 }
1059
1060 #define XR_DEVICE(vid, pid, type) \
1061 USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_COMM), \
1062 .driver_info = (type)
1063
1064 static const struct usb_device_id id_table[] = {
1065 { XR_DEVICE(0x04e2, 0x1400, XR2280X) },
1066 { XR_DEVICE(0x04e2, 0x1401, XR2280X) },
1067 { XR_DEVICE(0x04e2, 0x1402, XR2280X) },
1068 { XR_DEVICE(0x04e2, 0x1403, XR2280X) },
1069 { XR_DEVICE(0x04e2, 0x1410, XR21V141X) },
1070 { XR_DEVICE(0x04e2, 0x1411, XR21B1411) },
1071 { XR_DEVICE(0x04e2, 0x1412, XR21V141X) },
1072 { XR_DEVICE(0x04e2, 0x1414, XR21V141X) },
1073 { XR_DEVICE(0x04e2, 0x1420, XR21B142X) },
1074 { XR_DEVICE(0x04e2, 0x1422, XR21B142X) },
1075 { XR_DEVICE(0x04e2, 0x1424, XR21B142X) },
1076 { }
1077 };
1078 MODULE_DEVICE_TABLE(usb, id_table);
1079
1080 static struct usb_serial_driver xr_device = {
1081 .driver = {
1082 .name = "xr_serial",
1083 },
1084 .id_table = id_table,
1085 .num_ports = 1,
1086 .probe = xr_probe,
1087 .port_probe = xr_port_probe,
1088 .port_remove = xr_port_remove,
1089 .open = xr_open,
1090 .close = xr_close,
1091 .break_ctl = xr_break_ctl,
1092 .set_termios = xr_set_termios,
1093 .tiocmget = xr_tiocmget,
1094 .tiocmset = xr_tiocmset,
1095 .ioctl = xr_ioctl,
1096 .dtr_rts = xr_dtr_rts
1097 };
1098
1099 static struct usb_serial_driver * const serial_drivers[] = {
1100 &xr_device, NULL
1101 };
1102
1103 module_usb_serial_driver(serial_drivers, id_table);
1104
1105 MODULE_AUTHOR("Manivannan Sadhasivam <mani@kernel.org>");
1106 MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver");
1107 MODULE_LICENSE("GPL");
1108