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