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