xref: /openbmc/linux/drivers/usb/serial/f81232.c (revision 3a83e4e6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Fintek F81232 USB to serial adaptor driver
4  * Fintek F81532A/534A/535/536 USB to 2/4/8/12 serial adaptor driver
5  *
6  * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
7  * Copyright (C) 2012 Linux Foundation
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/tty.h>
14 #include <linux/tty_driver.h>
15 #include <linux/tty_flip.h>
16 #include <linux/serial.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/mutex.h>
20 #include <linux/uaccess.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/serial_reg.h>
24 
25 #define F81232_ID		\
26 	{ USB_DEVICE(0x1934, 0x0706) }	/* 1 port UART device */
27 
28 #define F81534A_SERIES_ID	\
29 	{ USB_DEVICE(0x2c42, 0x1602) },	/* In-Box 2 port UART device */	\
30 	{ USB_DEVICE(0x2c42, 0x1604) },	/* In-Box 4 port UART device */	\
31 	{ USB_DEVICE(0x2c42, 0x1605) },	/* In-Box 8 port UART device */	\
32 	{ USB_DEVICE(0x2c42, 0x1606) },	/* In-Box 12 port UART device */ \
33 	{ USB_DEVICE(0x2c42, 0x1608) },	/* Non-Flash type */ \
34 	{ USB_DEVICE(0x2c42, 0x1632) },	/* 2 port UART device */ \
35 	{ USB_DEVICE(0x2c42, 0x1634) },	/* 4 port UART device */ \
36 	{ USB_DEVICE(0x2c42, 0x1635) },	/* 8 port UART device */ \
37 	{ USB_DEVICE(0x2c42, 0x1636) }	/* 12 port UART device */
38 
39 #define F81534A_CTRL_ID		\
40 	{ USB_DEVICE(0x2c42, 0x16f8) }	/* Global control device */
41 
42 static const struct usb_device_id f81232_id_table[] = {
43 	F81232_ID,
44 	{ }					/* Terminating entry */
45 };
46 
47 static const struct usb_device_id f81534a_id_table[] = {
48 	F81534A_SERIES_ID,
49 	{ }					/* Terminating entry */
50 };
51 
52 static const struct usb_device_id f81534a_ctrl_id_table[] = {
53 	F81534A_CTRL_ID,
54 	{ }					/* Terminating entry */
55 };
56 
57 static const struct usb_device_id combined_id_table[] = {
58 	F81232_ID,
59 	F81534A_SERIES_ID,
60 	F81534A_CTRL_ID,
61 	{ }					/* Terminating entry */
62 };
63 MODULE_DEVICE_TABLE(usb, combined_id_table);
64 
65 /* Maximum baudrate for F81232 */
66 #define F81232_MAX_BAUDRATE		1500000
67 #define F81232_DEF_BAUDRATE		9600
68 
69 /* USB Control EP parameter */
70 #define F81232_REGISTER_REQUEST		0xa0
71 #define F81232_GET_REGISTER		0xc0
72 #define F81232_SET_REGISTER		0x40
73 #define F81534A_ACCESS_REG_RETRY	2
74 
75 #define SERIAL_BASE_ADDRESS		0x0120
76 #define RECEIVE_BUFFER_REGISTER		(0x00 + SERIAL_BASE_ADDRESS)
77 #define INTERRUPT_ENABLE_REGISTER	(0x01 + SERIAL_BASE_ADDRESS)
78 #define FIFO_CONTROL_REGISTER		(0x02 + SERIAL_BASE_ADDRESS)
79 #define LINE_CONTROL_REGISTER		(0x03 + SERIAL_BASE_ADDRESS)
80 #define MODEM_CONTROL_REGISTER		(0x04 + SERIAL_BASE_ADDRESS)
81 #define LINE_STATUS_REGISTER		(0x05 + SERIAL_BASE_ADDRESS)
82 #define MODEM_STATUS_REGISTER		(0x06 + SERIAL_BASE_ADDRESS)
83 
84 /*
85  * F81232 Clock registers (106h)
86  *
87  * Bit1-0:	Clock source selector
88  *			00: 1.846MHz.
89  *			01: 18.46MHz.
90  *			10: 24MHz.
91  *			11: 14.77MHz.
92  */
93 #define F81232_CLK_REGISTER		0x106
94 #define F81232_CLK_1_846_MHZ		0
95 #define F81232_CLK_18_46_MHZ		BIT(0)
96 #define F81232_CLK_24_MHZ		BIT(1)
97 #define F81232_CLK_14_77_MHZ		(BIT(1) | BIT(0))
98 #define F81232_CLK_MASK			GENMASK(1, 0)
99 
100 #define F81534A_MODE_REG		0x107
101 #define F81534A_TRIGGER_MASK		GENMASK(3, 2)
102 #define F81534A_TRIGGER_MULTIPLE_4X	BIT(3)
103 #define F81534A_FIFO_128BYTE		(BIT(1) | BIT(0))
104 
105 /* Serial port self GPIO control, 2bytes [control&output data][input data] */
106 #define F81534A_GPIO_REG		0x10e
107 #define F81534A_GPIO_MODE2_DIR		BIT(6) /* 1: input, 0: output */
108 #define F81534A_GPIO_MODE1_DIR		BIT(5)
109 #define F81534A_GPIO_MODE0_DIR		BIT(4)
110 #define F81534A_GPIO_MODE2_OUTPUT	BIT(2)
111 #define F81534A_GPIO_MODE1_OUTPUT	BIT(1)
112 #define F81534A_GPIO_MODE0_OUTPUT	BIT(0)
113 
114 #define F81534A_CTRL_CMD_ENABLE_PORT	0x116
115 
116 struct f81232_private {
117 	struct mutex lock;
118 	u8 modem_control;
119 	u8 modem_status;
120 	u8 shadow_lcr;
121 	speed_t baud_base;
122 	struct work_struct lsr_work;
123 	struct work_struct interrupt_work;
124 	struct usb_serial_port *port;
125 };
126 
127 static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
128 static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
129 				F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
130 
131 static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
132 {
133 	if (!baudrate)
134 		return 0;
135 
136 	return DIV_ROUND_CLOSEST(clockrate, baudrate);
137 }
138 
139 static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
140 {
141 	int status;
142 	u8 *tmp;
143 	struct usb_device *dev = port->serial->dev;
144 
145 	tmp = kmalloc(sizeof(*val), GFP_KERNEL);
146 	if (!tmp)
147 		return -ENOMEM;
148 
149 	status = usb_control_msg(dev,
150 				usb_rcvctrlpipe(dev, 0),
151 				F81232_REGISTER_REQUEST,
152 				F81232_GET_REGISTER,
153 				reg,
154 				0,
155 				tmp,
156 				sizeof(*val),
157 				USB_CTRL_GET_TIMEOUT);
158 	if (status != sizeof(*val)) {
159 		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
160 
161 		if (status < 0)
162 			status = usb_translate_errors(status);
163 		else
164 			status = -EIO;
165 	} else {
166 		status = 0;
167 		*val = *tmp;
168 	}
169 
170 	kfree(tmp);
171 	return status;
172 }
173 
174 static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
175 {
176 	int status;
177 	u8 *tmp;
178 	struct usb_device *dev = port->serial->dev;
179 
180 	tmp = kmalloc(sizeof(val), GFP_KERNEL);
181 	if (!tmp)
182 		return -ENOMEM;
183 
184 	*tmp = val;
185 
186 	status = usb_control_msg(dev,
187 				usb_sndctrlpipe(dev, 0),
188 				F81232_REGISTER_REQUEST,
189 				F81232_SET_REGISTER,
190 				reg,
191 				0,
192 				tmp,
193 				sizeof(val),
194 				USB_CTRL_SET_TIMEOUT);
195 	if (status != sizeof(val)) {
196 		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
197 
198 		if (status < 0)
199 			status = usb_translate_errors(status);
200 		else
201 			status = -EIO;
202 	} else {
203 		status = 0;
204 	}
205 
206 	kfree(tmp);
207 	return status;
208 }
209 
210 static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
211 					u8 mask, u8 val)
212 {
213 	int status;
214 	u8 tmp;
215 
216 	status = f81232_get_register(port, reg, &tmp);
217 	if (status)
218 		return status;
219 
220 	tmp = (tmp & ~mask) | (val & mask);
221 
222 	return f81232_set_register(port, reg, tmp);
223 }
224 
225 static void f81232_read_msr(struct usb_serial_port *port)
226 {
227 	int status;
228 	u8 current_msr;
229 	struct tty_struct *tty;
230 	struct f81232_private *priv = usb_get_serial_port_data(port);
231 
232 	mutex_lock(&priv->lock);
233 	status = f81232_get_register(port, MODEM_STATUS_REGISTER,
234 			&current_msr);
235 	if (status) {
236 		dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
237 		mutex_unlock(&priv->lock);
238 		return;
239 	}
240 
241 	if (!(current_msr & UART_MSR_ANY_DELTA)) {
242 		mutex_unlock(&priv->lock);
243 		return;
244 	}
245 
246 	priv->modem_status = current_msr;
247 
248 	if (current_msr & UART_MSR_DCTS)
249 		port->icount.cts++;
250 	if (current_msr & UART_MSR_DDSR)
251 		port->icount.dsr++;
252 	if (current_msr & UART_MSR_TERI)
253 		port->icount.rng++;
254 	if (current_msr & UART_MSR_DDCD) {
255 		port->icount.dcd++;
256 		tty = tty_port_tty_get(&port->port);
257 		if (tty) {
258 			usb_serial_handle_dcd_change(port, tty,
259 					current_msr & UART_MSR_DCD);
260 
261 			tty_kref_put(tty);
262 		}
263 	}
264 
265 	wake_up_interruptible(&port->port.delta_msr_wait);
266 	mutex_unlock(&priv->lock);
267 }
268 
269 static int f81232_set_mctrl(struct usb_serial_port *port,
270 					   unsigned int set, unsigned int clear)
271 {
272 	u8 val;
273 	int status;
274 	struct f81232_private *priv = usb_get_serial_port_data(port);
275 
276 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
277 		return 0;	/* no change */
278 
279 	/* 'set' takes precedence over 'clear' */
280 	clear &= ~set;
281 
282 	/* force enable interrupt with OUT2 */
283 	mutex_lock(&priv->lock);
284 	val = UART_MCR_OUT2 | priv->modem_control;
285 
286 	if (clear & TIOCM_DTR)
287 		val &= ~UART_MCR_DTR;
288 
289 	if (clear & TIOCM_RTS)
290 		val &= ~UART_MCR_RTS;
291 
292 	if (set & TIOCM_DTR)
293 		val |= UART_MCR_DTR;
294 
295 	if (set & TIOCM_RTS)
296 		val |= UART_MCR_RTS;
297 
298 	dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
299 			val, priv->modem_control);
300 
301 	status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
302 	if (status) {
303 		dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
304 		mutex_unlock(&priv->lock);
305 		return status;
306 	}
307 
308 	priv->modem_control = val;
309 	mutex_unlock(&priv->lock);
310 
311 	return 0;
312 }
313 
314 static void f81232_update_line_status(struct usb_serial_port *port,
315 				      unsigned char *data,
316 				      size_t actual_length)
317 {
318 	struct f81232_private *priv = usb_get_serial_port_data(port);
319 
320 	if (!actual_length)
321 		return;
322 
323 	switch (data[0] & 0x07) {
324 	case 0x00: /* msr change */
325 		dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
326 		schedule_work(&priv->interrupt_work);
327 		break;
328 	case 0x02: /* tx-empty */
329 		break;
330 	case 0x04: /* rx data available */
331 		break;
332 	case 0x06: /* lsr change */
333 		/* we can forget it. the LSR will read from bulk-in */
334 		dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
335 		break;
336 	}
337 }
338 
339 static void f81232_read_int_callback(struct urb *urb)
340 {
341 	struct usb_serial_port *port =  urb->context;
342 	unsigned char *data = urb->transfer_buffer;
343 	unsigned int actual_length = urb->actual_length;
344 	int status = urb->status;
345 	int retval;
346 
347 	switch (status) {
348 	case 0:
349 		/* success */
350 		break;
351 	case -ECONNRESET:
352 	case -ENOENT:
353 	case -ESHUTDOWN:
354 		/* this urb is terminated, clean up */
355 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
356 			__func__, status);
357 		return;
358 	default:
359 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
360 			__func__, status);
361 		goto exit;
362 	}
363 
364 	usb_serial_debug_data(&port->dev, __func__,
365 			      urb->actual_length, urb->transfer_buffer);
366 
367 	f81232_update_line_status(port, data, actual_length);
368 
369 exit:
370 	retval = usb_submit_urb(urb, GFP_ATOMIC);
371 	if (retval)
372 		dev_err(&urb->dev->dev,
373 			"%s - usb_submit_urb failed with result %d\n",
374 			__func__, retval);
375 }
376 
377 static char f81232_handle_lsr(struct usb_serial_port *port, u8 lsr)
378 {
379 	struct f81232_private *priv = usb_get_serial_port_data(port);
380 	char tty_flag = TTY_NORMAL;
381 
382 	if (!(lsr & UART_LSR_BRK_ERROR_BITS))
383 		return tty_flag;
384 
385 	if (lsr & UART_LSR_BI) {
386 		tty_flag = TTY_BREAK;
387 		port->icount.brk++;
388 		usb_serial_handle_break(port);
389 	} else if (lsr & UART_LSR_PE) {
390 		tty_flag = TTY_PARITY;
391 		port->icount.parity++;
392 	} else if (lsr & UART_LSR_FE) {
393 		tty_flag = TTY_FRAME;
394 		port->icount.frame++;
395 	}
396 
397 	if (lsr & UART_LSR_OE) {
398 		port->icount.overrun++;
399 		schedule_work(&priv->lsr_work);
400 		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
401 	}
402 
403 	return tty_flag;
404 }
405 
406 static void f81232_process_read_urb(struct urb *urb)
407 {
408 	struct usb_serial_port *port = urb->context;
409 	unsigned char *data = urb->transfer_buffer;
410 	char tty_flag;
411 	unsigned int i;
412 	u8 lsr;
413 
414 	/*
415 	 * When opening the port we get a 1-byte packet with the current LSR,
416 	 * which we discard.
417 	 */
418 	if ((urb->actual_length < 2) || (urb->actual_length % 2))
419 		return;
420 
421 	/* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
422 
423 	for (i = 0; i < urb->actual_length; i += 2) {
424 		lsr = data[i];
425 		tty_flag = f81232_handle_lsr(port, lsr);
426 
427 		if (port->sysrq) {
428 			if (usb_serial_handle_sysrq_char(port, data[i + 1]))
429 				continue;
430 		}
431 
432 		tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
433 	}
434 
435 	tty_flip_buffer_push(&port->port);
436 }
437 
438 static void f81534a_process_read_urb(struct urb *urb)
439 {
440 	struct usb_serial_port *port = urb->context;
441 	unsigned char *data = urb->transfer_buffer;
442 	char tty_flag;
443 	unsigned int i;
444 	u8 lsr;
445 	u8 len;
446 
447 	if (urb->actual_length < 3) {
448 		dev_err(&port->dev, "short message received: %d\n",
449 				urb->actual_length);
450 		return;
451 	}
452 
453 	len = data[0];
454 	if (len != urb->actual_length) {
455 		dev_err(&port->dev, "malformed message received: %d (%d)\n",
456 				urb->actual_length, len);
457 		return;
458 	}
459 
460 	/* bulk-in data: [LEN][Data.....][LSR] */
461 	lsr = data[len - 1];
462 	tty_flag = f81232_handle_lsr(port, lsr);
463 
464 	if (port->sysrq) {
465 		for (i = 1; i < len - 1; ++i) {
466 			if (!usb_serial_handle_sysrq_char(port, data[i])) {
467 				tty_insert_flip_char(&port->port, data[i],
468 						tty_flag);
469 			}
470 		}
471 	} else {
472 		tty_insert_flip_string_fixed_flag(&port->port, &data[1],
473 							tty_flag, len - 2);
474 	}
475 
476 	tty_flip_buffer_push(&port->port);
477 }
478 
479 static void f81232_break_ctl(struct tty_struct *tty, int break_state)
480 {
481 	struct usb_serial_port *port = tty->driver_data;
482 	struct f81232_private *priv = usb_get_serial_port_data(port);
483 	int status;
484 
485 	mutex_lock(&priv->lock);
486 
487 	if (break_state)
488 		priv->shadow_lcr |= UART_LCR_SBC;
489 	else
490 		priv->shadow_lcr &= ~UART_LCR_SBC;
491 
492 	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
493 					priv->shadow_lcr);
494 	if (status)
495 		dev_err(&port->dev, "set break failed: %d\n", status);
496 
497 	mutex_unlock(&priv->lock);
498 }
499 
500 static int f81232_find_clk(speed_t baudrate)
501 {
502 	int idx;
503 
504 	for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
505 		if (baudrate <= baudrate_table[idx] &&
506 				baudrate_table[idx] % baudrate == 0)
507 			return idx;
508 	}
509 
510 	return -EINVAL;
511 }
512 
513 static void f81232_set_baudrate(struct tty_struct *tty,
514 				struct usb_serial_port *port, speed_t baudrate,
515 				speed_t old_baudrate)
516 {
517 	struct f81232_private *priv = usb_get_serial_port_data(port);
518 	u8 lcr;
519 	int divisor;
520 	int status = 0;
521 	int i;
522 	int idx;
523 	speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
524 
525 	for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
526 		idx = f81232_find_clk(baud_list[i]);
527 		if (idx >= 0) {
528 			baudrate = baud_list[i];
529 			tty_encode_baud_rate(tty, baudrate, baudrate);
530 			break;
531 		}
532 	}
533 
534 	if (idx < 0)
535 		return;
536 
537 	priv->baud_base = baudrate_table[idx];
538 	divisor = calc_baud_divisor(baudrate, priv->baud_base);
539 
540 	status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
541 			F81232_CLK_MASK, clock_table[idx]);
542 	if (status) {
543 		dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
544 			__func__, status);
545 		return;
546 	}
547 
548 	status = f81232_get_register(port, LINE_CONTROL_REGISTER,
549 			 &lcr); /* get LCR */
550 	if (status) {
551 		dev_err(&port->dev, "%s failed to get LCR: %d\n",
552 			__func__, status);
553 		return;
554 	}
555 
556 	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
557 			 lcr | UART_LCR_DLAB); /* Enable DLAB */
558 	if (status) {
559 		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
560 			__func__, status);
561 		return;
562 	}
563 
564 	status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
565 			 divisor & 0x00ff); /* low */
566 	if (status) {
567 		dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
568 			__func__, status);
569 		goto reapply_lcr;
570 	}
571 
572 	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
573 			 (divisor & 0xff00) >> 8); /* high */
574 	if (status) {
575 		dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
576 			__func__, status);
577 	}
578 
579 reapply_lcr:
580 	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
581 			lcr & ~UART_LCR_DLAB);
582 	if (status) {
583 		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
584 			__func__, status);
585 	}
586 }
587 
588 static int f81232_port_enable(struct usb_serial_port *port)
589 {
590 	u8 val;
591 	int status;
592 
593 	/* fifo on, trigger8, clear TX/RX*/
594 	val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
595 			UART_FCR_CLEAR_XMIT;
596 
597 	status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
598 	if (status) {
599 		dev_err(&port->dev, "%s failed to set FCR: %d\n",
600 			__func__, status);
601 		return status;
602 	}
603 
604 	/* MSR Interrupt only, LSR will read from Bulk-in odd byte */
605 	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
606 			UART_IER_MSI);
607 	if (status) {
608 		dev_err(&port->dev, "%s failed to set IER: %d\n",
609 			__func__, status);
610 		return status;
611 	}
612 
613 	return 0;
614 }
615 
616 static int f81232_port_disable(struct usb_serial_port *port)
617 {
618 	int status;
619 
620 	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
621 	if (status) {
622 		dev_err(&port->dev, "%s failed to set IER: %d\n",
623 			__func__, status);
624 		return status;
625 	}
626 
627 	return 0;
628 }
629 
630 static void f81232_set_termios(struct tty_struct *tty,
631 		struct usb_serial_port *port, struct ktermios *old_termios)
632 {
633 	struct f81232_private *priv = usb_get_serial_port_data(port);
634 	u8 new_lcr = 0;
635 	int status = 0;
636 	speed_t baudrate;
637 	speed_t old_baud;
638 
639 	/* Don't change anything if nothing has changed */
640 	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
641 		return;
642 
643 	if (C_BAUD(tty) == B0)
644 		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
645 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
646 		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
647 
648 	baudrate = tty_get_baud_rate(tty);
649 	if (baudrate > 0) {
650 		if (old_termios)
651 			old_baud = tty_termios_baud_rate(old_termios);
652 		else
653 			old_baud = F81232_DEF_BAUDRATE;
654 
655 		f81232_set_baudrate(tty, port, baudrate, old_baud);
656 	}
657 
658 	if (C_PARENB(tty)) {
659 		new_lcr |= UART_LCR_PARITY;
660 
661 		if (!C_PARODD(tty))
662 			new_lcr |= UART_LCR_EPAR;
663 
664 		if (C_CMSPAR(tty))
665 			new_lcr |= UART_LCR_SPAR;
666 	}
667 
668 	if (C_CSTOPB(tty))
669 		new_lcr |= UART_LCR_STOP;
670 
671 	switch (C_CSIZE(tty)) {
672 	case CS5:
673 		new_lcr |= UART_LCR_WLEN5;
674 		break;
675 	case CS6:
676 		new_lcr |= UART_LCR_WLEN6;
677 		break;
678 	case CS7:
679 		new_lcr |= UART_LCR_WLEN7;
680 		break;
681 	default:
682 	case CS8:
683 		new_lcr |= UART_LCR_WLEN8;
684 		break;
685 	}
686 
687 	mutex_lock(&priv->lock);
688 
689 	new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
690 	status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
691 	if (status) {
692 		dev_err(&port->dev, "%s failed to set LCR: %d\n",
693 			__func__, status);
694 	}
695 
696 	priv->shadow_lcr = new_lcr;
697 
698 	mutex_unlock(&priv->lock);
699 }
700 
701 static int f81232_tiocmget(struct tty_struct *tty)
702 {
703 	int r;
704 	struct usb_serial_port *port = tty->driver_data;
705 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
706 	u8 mcr, msr;
707 
708 	/* force get current MSR changed state */
709 	f81232_read_msr(port);
710 
711 	mutex_lock(&port_priv->lock);
712 	mcr = port_priv->modem_control;
713 	msr = port_priv->modem_status;
714 	mutex_unlock(&port_priv->lock);
715 
716 	r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
717 		(mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
718 		(msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
719 		(msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
720 		(msr & UART_MSR_RI ? TIOCM_RI : 0) |
721 		(msr & UART_MSR_DSR ? TIOCM_DSR : 0);
722 
723 	return r;
724 }
725 
726 static int f81232_tiocmset(struct tty_struct *tty,
727 			unsigned int set, unsigned int clear)
728 {
729 	struct usb_serial_port *port = tty->driver_data;
730 
731 	return f81232_set_mctrl(port, set, clear);
732 }
733 
734 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
735 {
736 	int result;
737 
738 	result = f81232_port_enable(port);
739 	if (result)
740 		return result;
741 
742 	/* Setup termios */
743 	if (tty)
744 		f81232_set_termios(tty, port, NULL);
745 
746 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
747 	if (result) {
748 		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
749 			" error %d\n", __func__, result);
750 		return result;
751 	}
752 
753 	result = usb_serial_generic_open(tty, port);
754 	if (result) {
755 		usb_kill_urb(port->interrupt_in_urb);
756 		return result;
757 	}
758 
759 	return 0;
760 }
761 
762 static int f81534a_open(struct tty_struct *tty, struct usb_serial_port *port)
763 {
764 	int status;
765 	u8 mask;
766 	u8 val;
767 
768 	val = F81534A_TRIGGER_MULTIPLE_4X | F81534A_FIFO_128BYTE;
769 	mask = F81534A_TRIGGER_MASK | F81534A_FIFO_128BYTE;
770 
771 	status = f81232_set_mask_register(port, F81534A_MODE_REG, mask, val);
772 	if (status) {
773 		dev_err(&port->dev, "failed to set MODE_REG: %d\n", status);
774 		return status;
775 	}
776 
777 	return f81232_open(tty, port);
778 }
779 
780 static void f81232_close(struct usb_serial_port *port)
781 {
782 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
783 
784 	f81232_port_disable(port);
785 	usb_serial_generic_close(port);
786 	usb_kill_urb(port->interrupt_in_urb);
787 	flush_work(&port_priv->interrupt_work);
788 	flush_work(&port_priv->lsr_work);
789 }
790 
791 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
792 {
793 	if (on)
794 		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
795 	else
796 		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
797 }
798 
799 static bool f81232_tx_empty(struct usb_serial_port *port)
800 {
801 	int status;
802 	u8 tmp;
803 
804 	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
805 	if (!status) {
806 		if ((tmp & UART_LSR_TEMT) != UART_LSR_TEMT)
807 			return false;
808 	}
809 
810 	return true;
811 }
812 
813 static int f81232_carrier_raised(struct usb_serial_port *port)
814 {
815 	u8 msr;
816 	struct f81232_private *priv = usb_get_serial_port_data(port);
817 
818 	mutex_lock(&priv->lock);
819 	msr = priv->modem_status;
820 	mutex_unlock(&priv->lock);
821 
822 	if (msr & UART_MSR_DCD)
823 		return 1;
824 	return 0;
825 }
826 
827 static int f81232_get_serial_info(struct tty_struct *tty,
828 		struct serial_struct *ss)
829 {
830 	struct usb_serial_port *port = tty->driver_data;
831 	struct f81232_private *priv = usb_get_serial_port_data(port);
832 
833 	ss->type = PORT_16550A;
834 	ss->line = port->minor;
835 	ss->port = port->port_number;
836 	ss->baud_base = priv->baud_base;
837 	return 0;
838 }
839 
840 static void  f81232_interrupt_work(struct work_struct *work)
841 {
842 	struct f81232_private *priv =
843 		container_of(work, struct f81232_private, interrupt_work);
844 
845 	f81232_read_msr(priv->port);
846 }
847 
848 static void f81232_lsr_worker(struct work_struct *work)
849 {
850 	struct f81232_private *priv;
851 	struct usb_serial_port *port;
852 	int status;
853 	u8 tmp;
854 
855 	priv = container_of(work, struct f81232_private, lsr_work);
856 	port = priv->port;
857 
858 	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
859 	if (status)
860 		dev_warn(&port->dev, "read LSR failed: %d\n", status);
861 }
862 
863 static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
864 					u16 size, void *val)
865 {
866 	struct usb_device *dev = interface_to_usbdev(intf);
867 	int retry = F81534A_ACCESS_REG_RETRY;
868 	int status;
869 	u8 *tmp;
870 
871 	tmp = kmemdup(val, size, GFP_KERNEL);
872 	if (!tmp)
873 		return -ENOMEM;
874 
875 	while (retry--) {
876 		status = usb_control_msg(dev,
877 					usb_sndctrlpipe(dev, 0),
878 					F81232_REGISTER_REQUEST,
879 					F81232_SET_REGISTER,
880 					reg,
881 					0,
882 					tmp,
883 					size,
884 					USB_CTRL_SET_TIMEOUT);
885 		if (status < 0) {
886 			status = usb_translate_errors(status);
887 			if (status == -EIO)
888 				continue;
889 		} else if (status != size) {
890 			/* Retry on short transfers */
891 			status = -EIO;
892 			continue;
893 		} else {
894 			status = 0;
895 		}
896 
897 		break;
898 	}
899 
900 	if (status) {
901 		dev_err(&intf->dev, "failed to set register 0x%x: %d\n",
902 				reg, status);
903 	}
904 
905 	kfree(tmp);
906 	return status;
907 }
908 
909 static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
910 {
911 	unsigned char enable[2] = {0};
912 	int status;
913 
914 	/*
915 	 * Enable all available serial ports, define as following:
916 	 * bit 15	: Reset behavior (when HUB got soft reset)
917 	 *			0: maintain all serial port enabled state.
918 	 *			1: disable all serial port.
919 	 * bit 0~11	: Serial port enable bit.
920 	 */
921 	if (en) {
922 		enable[0] = 0xff;
923 		enable[1] = 0x8f;
924 	}
925 
926 	status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
927 			sizeof(enable), enable);
928 	if (status)
929 		dev_err(&intf->dev, "failed to enable ports: %d\n", status);
930 
931 	return status;
932 }
933 
934 static int f81534a_ctrl_probe(struct usb_interface *intf,
935 				const struct usb_device_id *id)
936 {
937 	return f81534a_ctrl_enable_all_ports(intf, true);
938 }
939 
940 static void f81534a_ctrl_disconnect(struct usb_interface *intf)
941 {
942 	f81534a_ctrl_enable_all_ports(intf, false);
943 }
944 
945 static int f81534a_ctrl_resume(struct usb_interface *intf)
946 {
947 	return f81534a_ctrl_enable_all_ports(intf, true);
948 }
949 
950 static int f81232_port_probe(struct usb_serial_port *port)
951 {
952 	struct f81232_private *priv;
953 
954 	priv = devm_kzalloc(&port->dev, sizeof(*priv), GFP_KERNEL);
955 	if (!priv)
956 		return -ENOMEM;
957 
958 	mutex_init(&priv->lock);
959 	INIT_WORK(&priv->interrupt_work,  f81232_interrupt_work);
960 	INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
961 
962 	usb_set_serial_port_data(port, priv);
963 
964 	port->port.drain_delay = 256;
965 	priv->port = port;
966 
967 	return 0;
968 }
969 
970 static int f81534a_port_probe(struct usb_serial_port *port)
971 {
972 	int status;
973 
974 	/* tri-state with pull-high, default RS232 Mode */
975 	status = f81232_set_register(port, F81534A_GPIO_REG,
976 					F81534A_GPIO_MODE2_DIR);
977 	if (status)
978 		return status;
979 
980 	return f81232_port_probe(port);
981 }
982 
983 static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
984 {
985 	struct usb_serial_port *port = serial->port[0];
986 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
987 	int i;
988 
989 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
990 		usb_kill_urb(port->read_urbs[i]);
991 
992 	usb_kill_urb(port->interrupt_in_urb);
993 
994 	if (port_priv) {
995 		flush_work(&port_priv->interrupt_work);
996 		flush_work(&port_priv->lsr_work);
997 	}
998 
999 	return 0;
1000 }
1001 
1002 static int f81232_resume(struct usb_serial *serial)
1003 {
1004 	struct usb_serial_port *port = serial->port[0];
1005 	int result;
1006 
1007 	if (tty_port_initialized(&port->port)) {
1008 		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
1009 		if (result) {
1010 			dev_err(&port->dev, "submit interrupt urb failed: %d\n",
1011 					result);
1012 			return result;
1013 		}
1014 	}
1015 
1016 	return usb_serial_generic_resume(serial);
1017 }
1018 
1019 static struct usb_serial_driver f81232_device = {
1020 	.driver = {
1021 		.owner =	THIS_MODULE,
1022 		.name =		"f81232",
1023 	},
1024 	.id_table =		f81232_id_table,
1025 	.num_ports =		1,
1026 	.bulk_in_size =		256,
1027 	.bulk_out_size =	256,
1028 	.open =			f81232_open,
1029 	.close =		f81232_close,
1030 	.dtr_rts =		f81232_dtr_rts,
1031 	.carrier_raised =	f81232_carrier_raised,
1032 	.get_serial =		f81232_get_serial_info,
1033 	.break_ctl =		f81232_break_ctl,
1034 	.set_termios =		f81232_set_termios,
1035 	.tiocmget =		f81232_tiocmget,
1036 	.tiocmset =		f81232_tiocmset,
1037 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1038 	.tx_empty =		f81232_tx_empty,
1039 	.process_read_urb =	f81232_process_read_urb,
1040 	.read_int_callback =	f81232_read_int_callback,
1041 	.port_probe =		f81232_port_probe,
1042 	.suspend =		f81232_suspend,
1043 	.resume =		f81232_resume,
1044 };
1045 
1046 static struct usb_serial_driver f81534a_device = {
1047 	.driver = {
1048 		.owner =	THIS_MODULE,
1049 		.name =		"f81534a",
1050 	},
1051 	.id_table =		f81534a_id_table,
1052 	.num_ports =		1,
1053 	.open =			f81534a_open,
1054 	.close =		f81232_close,
1055 	.dtr_rts =		f81232_dtr_rts,
1056 	.carrier_raised =	f81232_carrier_raised,
1057 	.get_serial =		f81232_get_serial_info,
1058 	.break_ctl =		f81232_break_ctl,
1059 	.set_termios =		f81232_set_termios,
1060 	.tiocmget =		f81232_tiocmget,
1061 	.tiocmset =		f81232_tiocmset,
1062 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1063 	.tx_empty =		f81232_tx_empty,
1064 	.process_read_urb =	f81534a_process_read_urb,
1065 	.read_int_callback =	f81232_read_int_callback,
1066 	.port_probe =		f81534a_port_probe,
1067 	.suspend =		f81232_suspend,
1068 	.resume =		f81232_resume,
1069 };
1070 
1071 static struct usb_serial_driver * const serial_drivers[] = {
1072 	&f81232_device,
1073 	&f81534a_device,
1074 	NULL,
1075 };
1076 
1077 static struct usb_driver f81534a_ctrl_driver = {
1078 	.name =		"f81534a_ctrl",
1079 	.id_table =	f81534a_ctrl_id_table,
1080 	.probe =	f81534a_ctrl_probe,
1081 	.disconnect =	f81534a_ctrl_disconnect,
1082 	.resume =	f81534a_ctrl_resume,
1083 };
1084 
1085 static int __init f81232_init(void)
1086 {
1087 	int status;
1088 
1089 	status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
1090 			KBUILD_MODNAME);
1091 	if (status)
1092 		return status;
1093 
1094 	status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
1095 			combined_id_table);
1096 	if (status) {
1097 		usb_deregister(&f81534a_ctrl_driver);
1098 		return status;
1099 	}
1100 
1101 	return 0;
1102 }
1103 
1104 static void __exit f81232_exit(void)
1105 {
1106 	usb_serial_deregister_drivers(serial_drivers);
1107 	usb_deregister(&f81534a_ctrl_driver);
1108 }
1109 
1110 module_init(f81232_init);
1111 module_exit(f81232_exit);
1112 
1113 MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
1114 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
1115 MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
1116 MODULE_LICENSE("GPL v2");
1117