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