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