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