xref: /openbmc/linux/drivers/usb/serial/f81232.c (revision b868a02e)
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,
607 			       const struct ktermios *old_termios)
608 {
609 	struct f81232_private *priv = usb_get_serial_port_data(port);
610 	u8 new_lcr = 0;
611 	int status = 0;
612 	speed_t baudrate;
613 	speed_t old_baud;
614 
615 	/* Don't change anything if nothing has changed */
616 	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
617 		return;
618 
619 	if (C_BAUD(tty) == B0)
620 		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
621 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
622 		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
623 
624 	baudrate = tty_get_baud_rate(tty);
625 	if (baudrate > 0) {
626 		if (old_termios)
627 			old_baud = tty_termios_baud_rate(old_termios);
628 		else
629 			old_baud = F81232_DEF_BAUDRATE;
630 
631 		f81232_set_baudrate(tty, port, baudrate, old_baud);
632 	}
633 
634 	if (C_PARENB(tty)) {
635 		new_lcr |= UART_LCR_PARITY;
636 
637 		if (!C_PARODD(tty))
638 			new_lcr |= UART_LCR_EPAR;
639 
640 		if (C_CMSPAR(tty))
641 			new_lcr |= UART_LCR_SPAR;
642 	}
643 
644 	if (C_CSTOPB(tty))
645 		new_lcr |= UART_LCR_STOP;
646 
647 	new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
648 
649 	mutex_lock(&priv->lock);
650 
651 	new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
652 	status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
653 	if (status) {
654 		dev_err(&port->dev, "%s failed to set LCR: %d\n",
655 			__func__, status);
656 	}
657 
658 	priv->shadow_lcr = new_lcr;
659 
660 	mutex_unlock(&priv->lock);
661 }
662 
663 static int f81232_tiocmget(struct tty_struct *tty)
664 {
665 	int r;
666 	struct usb_serial_port *port = tty->driver_data;
667 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
668 	u8 mcr, msr;
669 
670 	/* force get current MSR changed state */
671 	f81232_read_msr(port);
672 
673 	mutex_lock(&port_priv->lock);
674 	mcr = port_priv->modem_control;
675 	msr = port_priv->modem_status;
676 	mutex_unlock(&port_priv->lock);
677 
678 	r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
679 		(mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
680 		(msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
681 		(msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
682 		(msr & UART_MSR_RI ? TIOCM_RI : 0) |
683 		(msr & UART_MSR_DSR ? TIOCM_DSR : 0);
684 
685 	return r;
686 }
687 
688 static int f81232_tiocmset(struct tty_struct *tty,
689 			unsigned int set, unsigned int clear)
690 {
691 	struct usb_serial_port *port = tty->driver_data;
692 
693 	return f81232_set_mctrl(port, set, clear);
694 }
695 
696 static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
697 {
698 	int result;
699 
700 	result = f81232_port_enable(port);
701 	if (result)
702 		return result;
703 
704 	/* Setup termios */
705 	if (tty)
706 		f81232_set_termios(tty, port, NULL);
707 
708 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
709 	if (result) {
710 		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
711 			" error %d\n", __func__, result);
712 		return result;
713 	}
714 
715 	result = usb_serial_generic_open(tty, port);
716 	if (result) {
717 		usb_kill_urb(port->interrupt_in_urb);
718 		return result;
719 	}
720 
721 	return 0;
722 }
723 
724 static int f81534a_open(struct tty_struct *tty, struct usb_serial_port *port)
725 {
726 	int status;
727 	u8 mask;
728 	u8 val;
729 
730 	val = F81534A_TRIGGER_MULTIPLE_4X | F81534A_FIFO_128BYTE;
731 	mask = F81534A_TRIGGER_MASK | F81534A_FIFO_128BYTE;
732 
733 	status = f81232_set_mask_register(port, F81534A_MODE_REG, mask, val);
734 	if (status) {
735 		dev_err(&port->dev, "failed to set MODE_REG: %d\n", status);
736 		return status;
737 	}
738 
739 	return f81232_open(tty, port);
740 }
741 
742 static void f81232_close(struct usb_serial_port *port)
743 {
744 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
745 
746 	f81232_port_disable(port);
747 	usb_serial_generic_close(port);
748 	usb_kill_urb(port->interrupt_in_urb);
749 	flush_work(&port_priv->interrupt_work);
750 	flush_work(&port_priv->lsr_work);
751 }
752 
753 static void f81232_dtr_rts(struct usb_serial_port *port, int on)
754 {
755 	if (on)
756 		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
757 	else
758 		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
759 }
760 
761 static bool f81232_tx_empty(struct usb_serial_port *port)
762 {
763 	int status;
764 	u8 tmp;
765 
766 	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
767 	if (!status) {
768 		if ((tmp & UART_LSR_TEMT) != UART_LSR_TEMT)
769 			return false;
770 	}
771 
772 	return true;
773 }
774 
775 static int f81232_carrier_raised(struct usb_serial_port *port)
776 {
777 	u8 msr;
778 	struct f81232_private *priv = usb_get_serial_port_data(port);
779 
780 	mutex_lock(&priv->lock);
781 	msr = priv->modem_status;
782 	mutex_unlock(&priv->lock);
783 
784 	if (msr & UART_MSR_DCD)
785 		return 1;
786 	return 0;
787 }
788 
789 static void f81232_get_serial(struct tty_struct *tty, struct serial_struct *ss)
790 {
791 	struct usb_serial_port *port = tty->driver_data;
792 	struct f81232_private *priv = usb_get_serial_port_data(port);
793 
794 	ss->baud_base = priv->baud_base;
795 }
796 
797 static void  f81232_interrupt_work(struct work_struct *work)
798 {
799 	struct f81232_private *priv =
800 		container_of(work, struct f81232_private, interrupt_work);
801 
802 	f81232_read_msr(priv->port);
803 }
804 
805 static void f81232_lsr_worker(struct work_struct *work)
806 {
807 	struct f81232_private *priv;
808 	struct usb_serial_port *port;
809 	int status;
810 	u8 tmp;
811 
812 	priv = container_of(work, struct f81232_private, lsr_work);
813 	port = priv->port;
814 
815 	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
816 	if (status)
817 		dev_warn(&port->dev, "read LSR failed: %d\n", status);
818 }
819 
820 static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
821 					u16 size, void *val)
822 {
823 	struct usb_device *dev = interface_to_usbdev(intf);
824 	int retry = F81534A_ACCESS_REG_RETRY;
825 	int status;
826 
827 	while (retry--) {
828 		status = usb_control_msg_send(dev,
829 					      0,
830 					      F81232_REGISTER_REQUEST,
831 					      F81232_SET_REGISTER,
832 					      reg,
833 					      0,
834 					      val,
835 					      size,
836 					      USB_CTRL_SET_TIMEOUT,
837 					      GFP_KERNEL);
838 		if (status) {
839 			status = usb_translate_errors(status);
840 			if (status == -EIO)
841 				continue;
842 		}
843 
844 		break;
845 	}
846 
847 	if (status) {
848 		dev_err(&intf->dev, "failed to set register 0x%x: %d\n",
849 				reg, status);
850 	}
851 
852 	return status;
853 }
854 
855 static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
856 {
857 	unsigned char enable[2] = {0};
858 	int status;
859 
860 	/*
861 	 * Enable all available serial ports, define as following:
862 	 * bit 15	: Reset behavior (when HUB got soft reset)
863 	 *			0: maintain all serial port enabled state.
864 	 *			1: disable all serial port.
865 	 * bit 0~11	: Serial port enable bit.
866 	 */
867 	if (en) {
868 		enable[0] = 0xff;
869 		enable[1] = 0x8f;
870 	}
871 
872 	status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
873 			sizeof(enable), enable);
874 	if (status)
875 		dev_err(&intf->dev, "failed to enable ports: %d\n", status);
876 
877 	return status;
878 }
879 
880 static int f81534a_ctrl_probe(struct usb_interface *intf,
881 				const struct usb_device_id *id)
882 {
883 	return f81534a_ctrl_enable_all_ports(intf, true);
884 }
885 
886 static void f81534a_ctrl_disconnect(struct usb_interface *intf)
887 {
888 	f81534a_ctrl_enable_all_ports(intf, false);
889 }
890 
891 static int f81534a_ctrl_resume(struct usb_interface *intf)
892 {
893 	return f81534a_ctrl_enable_all_ports(intf, true);
894 }
895 
896 static int f81232_port_probe(struct usb_serial_port *port)
897 {
898 	struct f81232_private *priv;
899 
900 	priv = devm_kzalloc(&port->dev, sizeof(*priv), GFP_KERNEL);
901 	if (!priv)
902 		return -ENOMEM;
903 
904 	mutex_init(&priv->lock);
905 	INIT_WORK(&priv->interrupt_work,  f81232_interrupt_work);
906 	INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
907 
908 	usb_set_serial_port_data(port, priv);
909 
910 	priv->port = port;
911 
912 	return 0;
913 }
914 
915 static int f81534a_port_probe(struct usb_serial_port *port)
916 {
917 	int status;
918 
919 	/* tri-state with pull-high, default RS232 Mode */
920 	status = f81232_set_register(port, F81534A_GPIO_REG,
921 					F81534A_GPIO_MODE2_DIR);
922 	if (status)
923 		return status;
924 
925 	return f81232_port_probe(port);
926 }
927 
928 static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
929 {
930 	struct usb_serial_port *port = serial->port[0];
931 	struct f81232_private *port_priv = usb_get_serial_port_data(port);
932 	int i;
933 
934 	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
935 		usb_kill_urb(port->read_urbs[i]);
936 
937 	usb_kill_urb(port->interrupt_in_urb);
938 
939 	if (port_priv) {
940 		flush_work(&port_priv->interrupt_work);
941 		flush_work(&port_priv->lsr_work);
942 	}
943 
944 	return 0;
945 }
946 
947 static int f81232_resume(struct usb_serial *serial)
948 {
949 	struct usb_serial_port *port = serial->port[0];
950 	int result;
951 
952 	if (tty_port_initialized(&port->port)) {
953 		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
954 		if (result) {
955 			dev_err(&port->dev, "submit interrupt urb failed: %d\n",
956 					result);
957 			return result;
958 		}
959 	}
960 
961 	return usb_serial_generic_resume(serial);
962 }
963 
964 static struct usb_serial_driver f81232_device = {
965 	.driver = {
966 		.owner =	THIS_MODULE,
967 		.name =		"f81232",
968 	},
969 	.id_table =		f81232_id_table,
970 	.num_ports =		1,
971 	.bulk_in_size =		256,
972 	.bulk_out_size =	256,
973 	.open =			f81232_open,
974 	.close =		f81232_close,
975 	.dtr_rts =		f81232_dtr_rts,
976 	.carrier_raised =	f81232_carrier_raised,
977 	.get_serial =		f81232_get_serial,
978 	.break_ctl =		f81232_break_ctl,
979 	.set_termios =		f81232_set_termios,
980 	.tiocmget =		f81232_tiocmget,
981 	.tiocmset =		f81232_tiocmset,
982 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
983 	.tx_empty =		f81232_tx_empty,
984 	.process_read_urb =	f81232_process_read_urb,
985 	.read_int_callback =	f81232_read_int_callback,
986 	.port_probe =		f81232_port_probe,
987 	.suspend =		f81232_suspend,
988 	.resume =		f81232_resume,
989 };
990 
991 static struct usb_serial_driver f81534a_device = {
992 	.driver = {
993 		.owner =	THIS_MODULE,
994 		.name =		"f81534a",
995 	},
996 	.id_table =		f81534a_id_table,
997 	.num_ports =		1,
998 	.open =			f81534a_open,
999 	.close =		f81232_close,
1000 	.dtr_rts =		f81232_dtr_rts,
1001 	.carrier_raised =	f81232_carrier_raised,
1002 	.get_serial =		f81232_get_serial,
1003 	.break_ctl =		f81232_break_ctl,
1004 	.set_termios =		f81232_set_termios,
1005 	.tiocmget =		f81232_tiocmget,
1006 	.tiocmset =		f81232_tiocmset,
1007 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1008 	.tx_empty =		f81232_tx_empty,
1009 	.process_read_urb =	f81534a_process_read_urb,
1010 	.read_int_callback =	f81232_read_int_callback,
1011 	.port_probe =		f81534a_port_probe,
1012 	.suspend =		f81232_suspend,
1013 	.resume =		f81232_resume,
1014 };
1015 
1016 static struct usb_serial_driver * const serial_drivers[] = {
1017 	&f81232_device,
1018 	&f81534a_device,
1019 	NULL,
1020 };
1021 
1022 static struct usb_driver f81534a_ctrl_driver = {
1023 	.name =		"f81534a_ctrl",
1024 	.id_table =	f81534a_ctrl_id_table,
1025 	.probe =	f81534a_ctrl_probe,
1026 	.disconnect =	f81534a_ctrl_disconnect,
1027 	.resume =	f81534a_ctrl_resume,
1028 };
1029 
1030 static int __init f81232_init(void)
1031 {
1032 	int status;
1033 
1034 	status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
1035 			KBUILD_MODNAME);
1036 	if (status)
1037 		return status;
1038 
1039 	status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
1040 			combined_id_table);
1041 	if (status) {
1042 		usb_deregister(&f81534a_ctrl_driver);
1043 		return status;
1044 	}
1045 
1046 	return 0;
1047 }
1048 
1049 static void __exit f81232_exit(void)
1050 {
1051 	usb_serial_deregister_drivers(serial_drivers);
1052 	usb_deregister(&f81534a_ctrl_driver);
1053 }
1054 
1055 module_init(f81232_init);
1056 module_exit(f81232_exit);
1057 
1058 MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
1059 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
1060 MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
1061 MODULE_LICENSE("GPL v2");
1062