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