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