xref: /linux/drivers/usb/serial/f81534.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * F81532/F81534 USB to Serial Ports Bridge
4  *
5  * F81532 => 2 Serial Ports
6  * F81534 => 4 Serial Ports
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
14  * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
15  * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
16  *
17  * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
18  * for all serial port TX and 1 endpoint bulk-in for all serial port read in
19  * (Read Data/MSR/LSR).
20  *
21  * Write URB is fixed with 512bytes, per serial port used 128Bytes.
22  * It can be described by f81534_prepare_write_buffer()
23  *
24  * Read URB is 512Bytes max, per serial port used 128Bytes.
25  * It can be described by f81534_process_read_urb() and maybe received with
26  * 128x1,2,3,4 bytes.
27  *
28  */
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
34 #include <linux/serial_reg.h>
35 #include <linux/module.h>
36 #include <linux/uaccess.h>
37 
38 /* Serial Port register Address */
39 #define F81534_UART_BASE_ADDRESS	0x1200
40 #define F81534_UART_OFFSET		0x10
41 #define F81534_DIVISOR_LSB_REG		(0x00 + F81534_UART_BASE_ADDRESS)
42 #define F81534_DIVISOR_MSB_REG		(0x01 + F81534_UART_BASE_ADDRESS)
43 #define F81534_INTERRUPT_ENABLE_REG	(0x01 + F81534_UART_BASE_ADDRESS)
44 #define F81534_FIFO_CONTROL_REG		(0x02 + F81534_UART_BASE_ADDRESS)
45 #define F81534_LINE_CONTROL_REG		(0x03 + F81534_UART_BASE_ADDRESS)
46 #define F81534_MODEM_CONTROL_REG	(0x04 + F81534_UART_BASE_ADDRESS)
47 #define F81534_LINE_STATUS_REG		(0x05 + F81534_UART_BASE_ADDRESS)
48 #define F81534_MODEM_STATUS_REG		(0x06 + F81534_UART_BASE_ADDRESS)
49 #define F81534_CONFIG1_REG		(0x09 + F81534_UART_BASE_ADDRESS)
50 
51 #define F81534_DEF_CONF_ADDRESS_START	0x3000
52 #define F81534_DEF_CONF_SIZE		8
53 
54 #define F81534_CUSTOM_ADDRESS_START	0x2f00
55 #define F81534_CUSTOM_DATA_SIZE		0x10
56 #define F81534_CUSTOM_NO_CUSTOM_DATA	0xff
57 #define F81534_CUSTOM_VALID_TOKEN	0xf0
58 #define F81534_CONF_OFFSET		1
59 
60 #define F81534_MAX_DATA_BLOCK		64
61 #define F81534_MAX_BUS_RETRY		20
62 
63 /* Default URB timeout for USB operations */
64 #define F81534_USB_MAX_RETRY		10
65 #define F81534_USB_TIMEOUT		1000
66 #define F81534_SET_GET_REGISTER		0xA0
67 
68 #define F81534_NUM_PORT			4
69 #define F81534_UNUSED_PORT		0xff
70 #define F81534_WRITE_BUFFER_SIZE	512
71 
72 #define DRIVER_DESC			"Fintek F81532/F81534"
73 #define FINTEK_VENDOR_ID_1		0x1934
74 #define FINTEK_VENDOR_ID_2		0x2C42
75 #define FINTEK_DEVICE_ID		0x1202
76 #define F81534_MAX_TX_SIZE		124
77 #define F81534_MAX_RX_SIZE		124
78 #define F81534_RECEIVE_BLOCK_SIZE	128
79 #define F81534_MAX_RECEIVE_BLOCK_SIZE	512
80 
81 #define F81534_TOKEN_RECEIVE		0x01
82 #define F81534_TOKEN_WRITE		0x02
83 #define F81534_TOKEN_TX_EMPTY		0x03
84 #define F81534_TOKEN_MSR_CHANGE		0x04
85 
86 /*
87  * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
88  * idle if we performed any command.
89  *
90  * SPI Bus status register: F81534_BUS_REG_STATUS
91  *	Bit 0/1	: BUSY
92  *	Bit 2	: IDLE
93  */
94 #define F81534_BUS_BUSY			(BIT(0) | BIT(1))
95 #define F81534_BUS_IDLE			BIT(2)
96 #define F81534_BUS_READ_DATA		0x1004
97 #define F81534_BUS_REG_STATUS		0x1003
98 #define F81534_BUS_REG_START		0x1002
99 #define F81534_BUS_REG_END		0x1001
100 
101 #define F81534_CMD_READ			0x03
102 
103 #define F81534_DEFAULT_BAUD_RATE	9600
104 #define F81534_MAX_BAUDRATE		115200
105 
106 #define F81534_PORT_CONF_DISABLE_PORT	BIT(3)
107 #define F81534_PORT_CONF_NOT_EXIST_PORT	BIT(7)
108 #define F81534_PORT_UNAVAILABLE		\
109 	(F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
110 
111 #define F81534_1X_RXTRIGGER		0xc3
112 #define F81534_8X_RXTRIGGER		0xcf
113 
114 static const struct usb_device_id f81534_id_table[] = {
115 	{ USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
116 	{ USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
117 	{}			/* Terminating entry */
118 };
119 
120 #define F81534_TX_EMPTY_BIT		0
121 
122 struct f81534_serial_private {
123 	u8 conf_data[F81534_DEF_CONF_SIZE];
124 	int tty_idx[F81534_NUM_PORT];
125 	u8 setting_idx;
126 	int opened_port;
127 	struct mutex urb_mutex;
128 };
129 
130 struct f81534_port_private {
131 	struct mutex mcr_mutex;
132 	struct mutex lcr_mutex;
133 	struct work_struct lsr_work;
134 	struct usb_serial_port *port;
135 	unsigned long tx_empty;
136 	spinlock_t msr_lock;
137 	u8 shadow_mcr;
138 	u8 shadow_lcr;
139 	u8 shadow_msr;
140 	u8 phy_num;
141 };
142 
143 static int f81534_logic_to_phy_port(struct usb_serial *serial,
144 					struct usb_serial_port *port)
145 {
146 	struct f81534_serial_private *serial_priv =
147 			usb_get_serial_data(port->serial);
148 	int count = 0;
149 	int i;
150 
151 	for (i = 0; i < F81534_NUM_PORT; ++i) {
152 		if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
153 			continue;
154 
155 		if (port->port_number == count)
156 			return i;
157 
158 		++count;
159 	}
160 
161 	return -ENODEV;
162 }
163 
164 static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
165 {
166 	struct usb_interface *interface = serial->interface;
167 	struct usb_device *dev = serial->dev;
168 	size_t count = F81534_USB_MAX_RETRY;
169 	int status;
170 	u8 *tmp;
171 
172 	tmp = kmalloc(sizeof(u8), GFP_KERNEL);
173 	if (!tmp)
174 		return -ENOMEM;
175 
176 	*tmp = data;
177 
178 	/*
179 	 * Our device maybe not reply when heavily loading, We'll retry for
180 	 * F81534_USB_MAX_RETRY times.
181 	 */
182 	while (count--) {
183 		status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
184 					 F81534_SET_GET_REGISTER,
185 					 USB_TYPE_VENDOR | USB_DIR_OUT,
186 					 reg, 0, tmp, sizeof(u8),
187 					 F81534_USB_TIMEOUT);
188 		if (status > 0) {
189 			status = 0;
190 			break;
191 		} else if (status == 0) {
192 			status = -EIO;
193 		}
194 	}
195 
196 	if (status < 0) {
197 		dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
198 				__func__, reg, data, status);
199 	}
200 
201 	kfree(tmp);
202 	return status;
203 }
204 
205 static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
206 {
207 	struct usb_interface *interface = serial->interface;
208 	struct usb_device *dev = serial->dev;
209 	size_t count = F81534_USB_MAX_RETRY;
210 	int status;
211 	u8 *tmp;
212 
213 	tmp = kmalloc(sizeof(u8), GFP_KERNEL);
214 	if (!tmp)
215 		return -ENOMEM;
216 
217 	/*
218 	 * Our device maybe not reply when heavily loading, We'll retry for
219 	 * F81534_USB_MAX_RETRY times.
220 	 */
221 	while (count--) {
222 		status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
223 					 F81534_SET_GET_REGISTER,
224 					 USB_TYPE_VENDOR | USB_DIR_IN,
225 					 reg, 0, tmp, sizeof(u8),
226 					 F81534_USB_TIMEOUT);
227 		if (status > 0) {
228 			status = 0;
229 			break;
230 		} else if (status == 0) {
231 			status = -EIO;
232 		}
233 	}
234 
235 	if (status < 0) {
236 		dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
237 				reg, status);
238 		goto end;
239 	}
240 
241 	*data = *tmp;
242 
243 end:
244 	kfree(tmp);
245 	return status;
246 }
247 
248 static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
249 					u8 data)
250 {
251 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
252 
253 	return f81534_set_register(port->serial,
254 			reg + port_priv->phy_num * F81534_UART_OFFSET, data);
255 }
256 
257 static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
258 					u8 *data)
259 {
260 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
261 
262 	return f81534_get_register(port->serial,
263 			reg + port_priv->phy_num * F81534_UART_OFFSET, data);
264 }
265 
266 /*
267  * If we try to access the internal flash via SPI bus, we should check the bus
268  * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
269  */
270 static int f81534_wait_for_spi_idle(struct usb_serial *serial)
271 {
272 	size_t count = F81534_MAX_BUS_RETRY;
273 	u8 tmp;
274 	int status;
275 
276 	do {
277 		status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
278 						&tmp);
279 		if (status)
280 			return status;
281 
282 		if (tmp & F81534_BUS_BUSY)
283 			continue;
284 
285 		if (tmp & F81534_BUS_IDLE)
286 			break;
287 
288 	} while (--count);
289 
290 	if (!count) {
291 		dev_err(&serial->interface->dev,
292 				"%s: timed out waiting for idle SPI bus\n",
293 				__func__);
294 		return -EIO;
295 	}
296 
297 	return f81534_set_register(serial, F81534_BUS_REG_STATUS,
298 				tmp & ~F81534_BUS_IDLE);
299 }
300 
301 static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
302 					u8 *data)
303 {
304 	int status;
305 
306 	status = f81534_get_register(serial, reg, data);
307 	if (status)
308 		return status;
309 
310 	return f81534_wait_for_spi_idle(serial);
311 }
312 
313 static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
314 {
315 	int status;
316 
317 	status = f81534_set_register(serial, reg, data);
318 	if (status)
319 		return status;
320 
321 	return f81534_wait_for_spi_idle(serial);
322 }
323 
324 static int f81534_read_flash(struct usb_serial *serial, u32 address,
325 				size_t size, u8 *buf)
326 {
327 	u8 tmp_buf[F81534_MAX_DATA_BLOCK];
328 	size_t block = 0;
329 	size_t read_size;
330 	size_t count;
331 	int status;
332 	int offset;
333 	u16 reg_tmp;
334 
335 	status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
336 					F81534_CMD_READ);
337 	if (status)
338 		return status;
339 
340 	status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
341 					(address >> 16) & 0xff);
342 	if (status)
343 		return status;
344 
345 	status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
346 					(address >> 8) & 0xff);
347 	if (status)
348 		return status;
349 
350 	status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
351 					(address >> 0) & 0xff);
352 	if (status)
353 		return status;
354 
355 	/* Continuous read mode */
356 	do {
357 		read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
358 
359 		for (count = 0; count < read_size; ++count) {
360 			/* To write F81534_BUS_REG_END when final byte */
361 			if (size <= F81534_MAX_DATA_BLOCK &&
362 					read_size == count + 1)
363 				reg_tmp = F81534_BUS_REG_END;
364 			else
365 				reg_tmp = F81534_BUS_REG_START;
366 
367 			/*
368 			 * Dummy code, force IC to generate a read pulse, the
369 			 * set of value 0xf1 is dont care (any value is ok)
370 			 */
371 			status = f81534_set_spi_register(serial, reg_tmp,
372 					0xf1);
373 			if (status)
374 				return status;
375 
376 			status = f81534_get_spi_register(serial,
377 						F81534_BUS_READ_DATA,
378 						&tmp_buf[count]);
379 			if (status)
380 				return status;
381 
382 			offset = count + block * F81534_MAX_DATA_BLOCK;
383 			buf[offset] = tmp_buf[count];
384 		}
385 
386 		size -= read_size;
387 		++block;
388 	} while (size);
389 
390 	return 0;
391 }
392 
393 static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
394 {
395 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
396 	int phy_num = port_priv->phy_num;
397 	u8 tx_len;
398 	int i;
399 
400 	/*
401 	 * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
402 	 * index 0: port phy idx (e.g., 0,1,2,3)
403 	 * index 1: only F81534_TOKEN_WRITE
404 	 * index 2: serial TX out length
405 	 * index 3: fix to 0
406 	 * index 4~127: serial out data block
407 	 */
408 	for (i = 0; i < F81534_NUM_PORT; ++i) {
409 		buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
410 		buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
411 		buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
412 		buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
413 	}
414 
415 	tx_len = kfifo_out_locked(&port->write_fifo,
416 				&buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
417 				F81534_MAX_TX_SIZE, &port->lock);
418 
419 	buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
420 }
421 
422 static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
423 {
424 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
425 	struct urb *urb;
426 	unsigned long flags;
427 	int result;
428 
429 	/* Check is any data in write_fifo */
430 	spin_lock_irqsave(&port->lock, flags);
431 
432 	if (kfifo_is_empty(&port->write_fifo)) {
433 		spin_unlock_irqrestore(&port->lock, flags);
434 		return 0;
435 	}
436 
437 	spin_unlock_irqrestore(&port->lock, flags);
438 
439 	/* Check H/W is TXEMPTY */
440 	if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
441 		return 0;
442 
443 	urb = port->write_urbs[0];
444 	f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
445 	urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
446 
447 	result = usb_submit_urb(urb, mem_flags);
448 	if (result) {
449 		set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
450 		dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
451 				result);
452 		return result;
453 	}
454 
455 	usb_serial_port_softint(port);
456 	return 0;
457 }
458 
459 static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
460 {
461 	if (!baudrate)
462 		return 0;
463 
464 	/* Round to nearest divisor */
465 	return DIV_ROUND_CLOSEST(clockrate, baudrate);
466 }
467 
468 static int f81534_set_port_config(struct usb_serial_port *port, u32 baudrate,
469 					u8 lcr)
470 {
471 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
472 	u32 divisor;
473 	int status;
474 	u8 value;
475 
476 	if (baudrate <= 1200)
477 		value = F81534_1X_RXTRIGGER;	/* 128 FIFO & TL: 1x */
478 	else
479 		value = F81534_8X_RXTRIGGER;	/* 128 FIFO & TL: 8x */
480 
481 	status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
482 	if (status) {
483 		dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
484 		return status;
485 	}
486 
487 	if (baudrate <= 1200)
488 		value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
489 	else
490 		value = UART_FCR_R_TRIG_11 | UART_FCR_ENABLE_FIFO; /* TL: 14 */
491 
492 	status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
493 						value);
494 	if (status) {
495 		dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
496 		return status;
497 	}
498 
499 	divisor = f81534_calc_baud_divisor(baudrate, F81534_MAX_BAUDRATE);
500 
501 	mutex_lock(&port_priv->lcr_mutex);
502 
503 	value = UART_LCR_DLAB;
504 	status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
505 						value);
506 	if (status) {
507 		dev_err(&port->dev, "%s: set LCR failed\n", __func__);
508 		goto out_unlock;
509 	}
510 
511 	value = divisor & 0xff;
512 	status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
513 	if (status) {
514 		dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
515 		goto out_unlock;
516 	}
517 
518 	value = (divisor >> 8) & 0xff;
519 	status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
520 	if (status) {
521 		dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
522 		goto out_unlock;
523 	}
524 
525 	value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
526 	status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
527 						value);
528 	if (status) {
529 		dev_err(&port->dev, "%s: set LCR failed\n", __func__);
530 		goto out_unlock;
531 	}
532 
533 	port_priv->shadow_lcr = value;
534 out_unlock:
535 	mutex_unlock(&port_priv->lcr_mutex);
536 
537 	return status;
538 }
539 
540 static void f81534_break_ctl(struct tty_struct *tty, int break_state)
541 {
542 	struct usb_serial_port *port = tty->driver_data;
543 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
544 	int status;
545 
546 	mutex_lock(&port_priv->lcr_mutex);
547 
548 	if (break_state)
549 		port_priv->shadow_lcr |= UART_LCR_SBC;
550 	else
551 		port_priv->shadow_lcr &= ~UART_LCR_SBC;
552 
553 	status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
554 					port_priv->shadow_lcr);
555 	if (status)
556 		dev_err(&port->dev, "set break failed: %d\n", status);
557 
558 	mutex_unlock(&port_priv->lcr_mutex);
559 }
560 
561 static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
562 				unsigned int clear)
563 {
564 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
565 	int status;
566 	u8 tmp;
567 
568 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
569 		return 0;	/* no change */
570 
571 	mutex_lock(&port_priv->mcr_mutex);
572 
573 	/* 'Set' takes precedence over 'Clear' */
574 	clear &= ~set;
575 
576 	/* Always enable UART_MCR_OUT2 */
577 	tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
578 
579 	if (clear & TIOCM_DTR)
580 		tmp &= ~UART_MCR_DTR;
581 
582 	if (clear & TIOCM_RTS)
583 		tmp &= ~UART_MCR_RTS;
584 
585 	if (set & TIOCM_DTR)
586 		tmp |= UART_MCR_DTR;
587 
588 	if (set & TIOCM_RTS)
589 		tmp |= UART_MCR_RTS;
590 
591 	status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
592 	if (status < 0) {
593 		dev_err(&port->dev, "%s: MCR write failed\n", __func__);
594 		mutex_unlock(&port_priv->mcr_mutex);
595 		return status;
596 	}
597 
598 	port_priv->shadow_mcr = tmp;
599 	mutex_unlock(&port_priv->mcr_mutex);
600 	return 0;
601 }
602 
603 /*
604  * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
605  * for latest configuration index. If nothing found
606  * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
607  * F81534_DEF_CONF_ADDRESS_START section.
608  *
609  * Due to we only use block0 to save data, so *index should be 0 or
610  * F81534_CUSTOM_NO_CUSTOM_DATA.
611  */
612 static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
613 {
614 	u8 tmp;
615 	int status;
616 
617 	status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
618 					&tmp);
619 	if (status) {
620 		dev_err(&serial->interface->dev, "%s: read failed: %d\n",
621 				__func__, status);
622 		return status;
623 	}
624 
625 	/* We'll use the custom data when the data is valid. */
626 	if (tmp == F81534_CUSTOM_VALID_TOKEN)
627 		*index = 0;
628 	else
629 		*index = F81534_CUSTOM_NO_CUSTOM_DATA;
630 
631 	return 0;
632 }
633 
634 /*
635  * We had 2 generation of F81532/534 IC. All has an internal storage.
636  *
637  * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
638  * internal data will used. All mode and gpio control should manually set
639  * by AP or Driver and all storage space value are 0xff. The
640  * f81534_calc_num_ports() will run to final we marked as "oldest version"
641  * for this IC.
642  *
643  * 2rd is designed to more generic to use any transceiver and this is our
644  * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
645  * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
646  * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
647  * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
648  * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
649  * The f81534_calc_num_ports() will run to "new style" with checking
650  * F81534_PORT_UNAVAILABLE section.
651  */
652 static int f81534_calc_num_ports(struct usb_serial *serial,
653 					struct usb_serial_endpoints *epds)
654 {
655 	struct device *dev = &serial->interface->dev;
656 	int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
657 	int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
658 	u8 setting[F81534_CUSTOM_DATA_SIZE];
659 	u8 setting_idx;
660 	u8 num_port = 0;
661 	int status;
662 	size_t i;
663 
664 	if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
665 			size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
666 		dev_err(dev, "unsupported endpoint max packet size\n");
667 		return -ENODEV;
668 	}
669 
670 	/* Check had custom setting */
671 	status = f81534_find_config_idx(serial, &setting_idx);
672 	if (status) {
673 		dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
674 				__func__, status);
675 		return status;
676 	}
677 
678 	/*
679 	 * We'll read custom data only when data available, otherwise we'll
680 	 * read default value instead.
681 	 */
682 	if (setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
683 		status = f81534_read_flash(serial,
684 						F81534_CUSTOM_ADDRESS_START +
685 						F81534_CONF_OFFSET,
686 						sizeof(setting), setting);
687 		if (status) {
688 			dev_err(&serial->interface->dev,
689 					"%s: get custom data failed: %d\n",
690 					__func__, status);
691 			return status;
692 		}
693 
694 		dev_dbg(&serial->interface->dev,
695 				"%s: read config from block: %d\n", __func__,
696 				setting_idx);
697 	} else {
698 		/* Read default board setting */
699 		status = f81534_read_flash(serial,
700 				F81534_DEF_CONF_ADDRESS_START, F81534_NUM_PORT,
701 				setting);
702 
703 		if (status) {
704 			dev_err(&serial->interface->dev,
705 					"%s: read failed: %d\n", __func__,
706 					status);
707 			return status;
708 		}
709 
710 		dev_dbg(&serial->interface->dev, "%s: read default config\n",
711 				__func__);
712 	}
713 
714 	/* New style, find all possible ports */
715 	for (i = 0; i < F81534_NUM_PORT; ++i) {
716 		if (setting[i] & F81534_PORT_UNAVAILABLE)
717 			continue;
718 
719 		++num_port;
720 	}
721 
722 	if (!num_port) {
723 		dev_warn(&serial->interface->dev,
724 			"no config found, assuming 4 ports\n");
725 		num_port = 4;		/* Nothing found, oldest version IC */
726 	}
727 
728 	/*
729 	 * Setup bulk-out endpoint multiplexing. All ports share the same
730 	 * bulk-out endpoint.
731 	 */
732 	BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
733 
734 	for (i = 1; i < num_port; ++i)
735 		epds->bulk_out[i] = epds->bulk_out[0];
736 
737 	epds->num_bulk_out = num_port;
738 
739 	return num_port;
740 }
741 
742 static void f81534_set_termios(struct tty_struct *tty,
743 				struct usb_serial_port *port,
744 				struct ktermios *old_termios)
745 {
746 	u8 new_lcr = 0;
747 	int status;
748 	u32 baud;
749 
750 	if (C_BAUD(tty) == B0)
751 		f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
752 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
753 		f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
754 
755 	if (C_PARENB(tty)) {
756 		new_lcr |= UART_LCR_PARITY;
757 
758 		if (!C_PARODD(tty))
759 			new_lcr |= UART_LCR_EPAR;
760 
761 		if (C_CMSPAR(tty))
762 			new_lcr |= UART_LCR_SPAR;
763 	}
764 
765 	if (C_CSTOPB(tty))
766 		new_lcr |= UART_LCR_STOP;
767 
768 	switch (C_CSIZE(tty)) {
769 	case CS5:
770 		new_lcr |= UART_LCR_WLEN5;
771 		break;
772 	case CS6:
773 		new_lcr |= UART_LCR_WLEN6;
774 		break;
775 	case CS7:
776 		new_lcr |= UART_LCR_WLEN7;
777 		break;
778 	default:
779 	case CS8:
780 		new_lcr |= UART_LCR_WLEN8;
781 		break;
782 	}
783 
784 	baud = tty_get_baud_rate(tty);
785 	if (!baud)
786 		return;
787 
788 	if (baud > F81534_MAX_BAUDRATE) {
789 		if (old_termios)
790 			baud = tty_termios_baud_rate(old_termios);
791 		else
792 			baud = F81534_DEFAULT_BAUD_RATE;
793 
794 		tty_encode_baud_rate(tty, baud, baud);
795 	}
796 
797 	dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
798 
799 	status = f81534_set_port_config(port, baud, new_lcr);
800 	if (status < 0) {
801 		dev_err(&port->dev, "%s: set port config failed: %d\n",
802 				__func__, status);
803 	}
804 }
805 
806 static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
807 {
808 	return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
809 }
810 
811 static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
812 {
813 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
814 	struct tty_struct *tty;
815 	unsigned long flags;
816 	u8 old_msr;
817 
818 	if (!(msr & UART_MSR_ANY_DELTA))
819 		return;
820 
821 	spin_lock_irqsave(&port_priv->msr_lock, flags);
822 	old_msr = port_priv->shadow_msr;
823 	port_priv->shadow_msr = msr;
824 	spin_unlock_irqrestore(&port_priv->msr_lock, flags);
825 
826 	dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
827 			msr);
828 
829 	/* Update input line counters */
830 	if (msr & UART_MSR_DCTS)
831 		port->icount.cts++;
832 	if (msr & UART_MSR_DDSR)
833 		port->icount.dsr++;
834 	if (msr & UART_MSR_DDCD)
835 		port->icount.dcd++;
836 	if (msr & UART_MSR_TERI)
837 		port->icount.rng++;
838 
839 	wake_up_interruptible(&port->port.delta_msr_wait);
840 
841 	if (!(msr & UART_MSR_DDCD))
842 		return;
843 
844 	dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
845 			__func__, port_priv->phy_num, old_msr, msr);
846 
847 	tty = tty_port_tty_get(&port->port);
848 	if (!tty)
849 		return;
850 
851 	usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
852 	tty_kref_put(tty);
853 }
854 
855 static int f81534_read_msr(struct usb_serial_port *port)
856 {
857 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
858 	unsigned long flags;
859 	int status;
860 	u8 msr;
861 
862 	/* Get MSR initial value */
863 	status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
864 	if (status)
865 		return status;
866 
867 	/* Force update current state */
868 	spin_lock_irqsave(&port_priv->msr_lock, flags);
869 	port_priv->shadow_msr = msr;
870 	spin_unlock_irqrestore(&port_priv->msr_lock, flags);
871 
872 	return 0;
873 }
874 
875 static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
876 {
877 	struct f81534_serial_private *serial_priv =
878 			usb_get_serial_data(port->serial);
879 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
880 	int status;
881 
882 	status = f81534_set_port_register(port,
883 				F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
884 				UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
885 	if (status) {
886 		dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
887 				status);
888 		return status;
889 	}
890 
891 	if (tty)
892 		f81534_set_termios(tty, port, NULL);
893 
894 	status = f81534_read_msr(port);
895 	if (status)
896 		return status;
897 
898 	mutex_lock(&serial_priv->urb_mutex);
899 
900 	/* Submit Read URBs for first port opened */
901 	if (!serial_priv->opened_port) {
902 		status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
903 		if (status)
904 			goto exit;
905 	}
906 
907 	serial_priv->opened_port++;
908 
909 exit:
910 	mutex_unlock(&serial_priv->urb_mutex);
911 
912 	set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
913 	return status;
914 }
915 
916 static void f81534_close(struct usb_serial_port *port)
917 {
918 	struct f81534_serial_private *serial_priv =
919 			usb_get_serial_data(port->serial);
920 	struct usb_serial_port *port0 = port->serial->port[0];
921 	unsigned long flags;
922 	size_t i;
923 
924 	usb_kill_urb(port->write_urbs[0]);
925 
926 	spin_lock_irqsave(&port->lock, flags);
927 	kfifo_reset_out(&port->write_fifo);
928 	spin_unlock_irqrestore(&port->lock, flags);
929 
930 	/* Kill Read URBs when final port closed */
931 	mutex_lock(&serial_priv->urb_mutex);
932 	serial_priv->opened_port--;
933 
934 	if (!serial_priv->opened_port) {
935 		for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
936 			usb_kill_urb(port0->read_urbs[i]);
937 	}
938 
939 	mutex_unlock(&serial_priv->urb_mutex);
940 }
941 
942 static int f81534_get_serial_info(struct usb_serial_port *port,
943 				  struct serial_struct __user *retinfo)
944 {
945 	struct f81534_port_private *port_priv;
946 	struct serial_struct tmp;
947 
948 	port_priv = usb_get_serial_port_data(port);
949 
950 	memset(&tmp, 0, sizeof(tmp));
951 
952 	tmp.type = PORT_16550A;
953 	tmp.port = port->port_number;
954 	tmp.line = port->minor;
955 	tmp.baud_base = F81534_MAX_BAUDRATE;
956 
957 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
958 		return -EFAULT;
959 
960 	return 0;
961 }
962 
963 static int f81534_ioctl(struct tty_struct *tty, unsigned int cmd,
964 			unsigned long arg)
965 {
966 	struct usb_serial_port *port = tty->driver_data;
967 	struct serial_struct __user *buf = (struct serial_struct __user *)arg;
968 
969 	switch (cmd) {
970 	case TIOCGSERIAL:
971 		return f81534_get_serial_info(port, buf);
972 	default:
973 		break;
974 	}
975 
976 	return -ENOIOCTLCMD;
977 }
978 
979 static void f81534_process_per_serial_block(struct usb_serial_port *port,
980 		u8 *data)
981 {
982 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
983 	int phy_num = data[0];
984 	size_t read_size = 0;
985 	size_t i;
986 	char tty_flag;
987 	int status;
988 	u8 lsr;
989 
990 	/*
991 	 * The block layout is 128 Bytes
992 	 * index 0: port phy idx (e.g., 0,1,2,3),
993 	 * index 1: It's could be
994 	 *			F81534_TOKEN_RECEIVE
995 	 *			F81534_TOKEN_TX_EMPTY
996 	 *			F81534_TOKEN_MSR_CHANGE
997 	 * index 2: serial in size (data+lsr, must be even)
998 	 *			meaningful for F81534_TOKEN_RECEIVE only
999 	 * index 3: current MSR with this device
1000 	 * index 4~127: serial in data block (data+lsr, must be even)
1001 	 */
1002 	switch (data[1]) {
1003 	case F81534_TOKEN_TX_EMPTY:
1004 		set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1005 
1006 		/* Try to submit writer */
1007 		status = f81534_submit_writer(port, GFP_ATOMIC);
1008 		if (status)
1009 			dev_err(&port->dev, "%s: submit failed\n", __func__);
1010 		return;
1011 
1012 	case F81534_TOKEN_MSR_CHANGE:
1013 		f81534_msr_changed(port, data[3]);
1014 		return;
1015 
1016 	case F81534_TOKEN_RECEIVE:
1017 		read_size = data[2];
1018 		if (read_size > F81534_MAX_RX_SIZE) {
1019 			dev_err(&port->dev,
1020 				"%s: phy: %d read_size: %zu larger than: %d\n",
1021 				__func__, phy_num, read_size,
1022 				F81534_MAX_RX_SIZE);
1023 			return;
1024 		}
1025 
1026 		break;
1027 
1028 	default:
1029 		dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
1030 				data[1]);
1031 		return;
1032 	}
1033 
1034 	for (i = 4; i < 4 + read_size; i += 2) {
1035 		tty_flag = TTY_NORMAL;
1036 		lsr = data[i + 1];
1037 
1038 		if (lsr & UART_LSR_BRK_ERROR_BITS) {
1039 			if (lsr & UART_LSR_BI) {
1040 				tty_flag = TTY_BREAK;
1041 				port->icount.brk++;
1042 				usb_serial_handle_break(port);
1043 			} else if (lsr & UART_LSR_PE) {
1044 				tty_flag = TTY_PARITY;
1045 				port->icount.parity++;
1046 			} else if (lsr & UART_LSR_FE) {
1047 				tty_flag = TTY_FRAME;
1048 				port->icount.frame++;
1049 			}
1050 
1051 			if (lsr & UART_LSR_OE) {
1052 				port->icount.overrun++;
1053 				tty_insert_flip_char(&port->port, 0,
1054 						TTY_OVERRUN);
1055 			}
1056 
1057 			schedule_work(&port_priv->lsr_work);
1058 		}
1059 
1060 		if (port->port.console && port->sysrq) {
1061 			if (usb_serial_handle_sysrq_char(port, data[i]))
1062 				continue;
1063 		}
1064 
1065 		tty_insert_flip_char(&port->port, data[i], tty_flag);
1066 	}
1067 
1068 	tty_flip_buffer_push(&port->port);
1069 }
1070 
1071 static void f81534_process_read_urb(struct urb *urb)
1072 {
1073 	struct f81534_serial_private *serial_priv;
1074 	struct usb_serial_port *port;
1075 	struct usb_serial *serial;
1076 	u8 *buf;
1077 	int phy_port_num;
1078 	int tty_port_num;
1079 	size_t i;
1080 
1081 	if (!urb->actual_length ||
1082 			urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1083 		return;
1084 	}
1085 
1086 	port = urb->context;
1087 	serial = port->serial;
1088 	buf = urb->transfer_buffer;
1089 	serial_priv = usb_get_serial_data(serial);
1090 
1091 	for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
1092 		phy_port_num = buf[i];
1093 		if (phy_port_num >= F81534_NUM_PORT) {
1094 			dev_err(&port->dev,
1095 				"%s: phy_port_num: %d larger than: %d\n",
1096 				__func__, phy_port_num, F81534_NUM_PORT);
1097 			continue;
1098 		}
1099 
1100 		tty_port_num = serial_priv->tty_idx[phy_port_num];
1101 		port = serial->port[tty_port_num];
1102 
1103 		if (tty_port_initialized(&port->port))
1104 			f81534_process_per_serial_block(port, &buf[i]);
1105 	}
1106 }
1107 
1108 static void f81534_write_usb_callback(struct urb *urb)
1109 {
1110 	struct usb_serial_port *port = urb->context;
1111 
1112 	switch (urb->status) {
1113 	case 0:
1114 		break;
1115 	case -ENOENT:
1116 	case -ECONNRESET:
1117 	case -ESHUTDOWN:
1118 		dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1119 				__func__, urb->status);
1120 		return;
1121 	case -EPIPE:
1122 		dev_err(&port->dev, "%s - urb stopped: %d\n",
1123 				__func__, urb->status);
1124 		return;
1125 	default:
1126 		dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1127 				__func__, urb->status);
1128 		break;
1129 	}
1130 }
1131 
1132 static int f81534_attach(struct usb_serial *serial)
1133 {
1134 	struct f81534_serial_private *serial_priv;
1135 	int index = 0;
1136 	int status;
1137 	int i;
1138 
1139 	serial_priv = devm_kzalloc(&serial->interface->dev,
1140 					sizeof(*serial_priv), GFP_KERNEL);
1141 	if (!serial_priv)
1142 		return -ENOMEM;
1143 
1144 	usb_set_serial_data(serial, serial_priv);
1145 
1146 	mutex_init(&serial_priv->urb_mutex);
1147 
1148 	/* Check had custom setting */
1149 	status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
1150 	if (status) {
1151 		dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
1152 				__func__, status);
1153 		return status;
1154 	}
1155 
1156 	/*
1157 	 * We'll read custom data only when data available, otherwise we'll
1158 	 * read default value instead.
1159 	 */
1160 	if (serial_priv->setting_idx == F81534_CUSTOM_NO_CUSTOM_DATA) {
1161 		/*
1162 		 * The default configuration layout:
1163 		 *	byte 0/1/2/3: uart setting
1164 		 */
1165 		status = f81534_read_flash(serial,
1166 					F81534_DEF_CONF_ADDRESS_START,
1167 					F81534_DEF_CONF_SIZE,
1168 					serial_priv->conf_data);
1169 		if (status) {
1170 			dev_err(&serial->interface->dev,
1171 					"%s: read reserve data failed: %d\n",
1172 					__func__, status);
1173 			return status;
1174 		}
1175 	} else {
1176 		/* Only read 8 bytes for mode & GPIO */
1177 		status = f81534_read_flash(serial,
1178 						F81534_CUSTOM_ADDRESS_START +
1179 						F81534_CONF_OFFSET,
1180 						sizeof(serial_priv->conf_data),
1181 						serial_priv->conf_data);
1182 		if (status) {
1183 			dev_err(&serial->interface->dev,
1184 					"%s: idx: %d get data failed: %d\n",
1185 					__func__, serial_priv->setting_idx,
1186 					status);
1187 			return status;
1188 		}
1189 	}
1190 
1191 	/* Assign phy-to-logic mapping */
1192 	for (i = 0; i < F81534_NUM_PORT; ++i) {
1193 		if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
1194 			continue;
1195 
1196 		serial_priv->tty_idx[i] = index++;
1197 		dev_dbg(&serial->interface->dev,
1198 				"%s: phy_num: %d, tty_idx: %d\n", __func__, i,
1199 				serial_priv->tty_idx[i]);
1200 	}
1201 
1202 	return 0;
1203 }
1204 
1205 static void f81534_lsr_worker(struct work_struct *work)
1206 {
1207 	struct f81534_port_private *port_priv;
1208 	struct usb_serial_port *port;
1209 	int status;
1210 	u8 tmp;
1211 
1212 	port_priv = container_of(work, struct f81534_port_private, lsr_work);
1213 	port = port_priv->port;
1214 
1215 	status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
1216 	if (status)
1217 		dev_warn(&port->dev, "read LSR failed: %d\n", status);
1218 }
1219 
1220 static int f81534_port_probe(struct usb_serial_port *port)
1221 {
1222 	struct f81534_port_private *port_priv;
1223 	int ret;
1224 
1225 	port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
1226 	if (!port_priv)
1227 		return -ENOMEM;
1228 
1229 	spin_lock_init(&port_priv->msr_lock);
1230 	mutex_init(&port_priv->mcr_mutex);
1231 	mutex_init(&port_priv->lcr_mutex);
1232 	INIT_WORK(&port_priv->lsr_work, f81534_lsr_worker);
1233 
1234 	/* Assign logic-to-phy mapping */
1235 	ret = f81534_logic_to_phy_port(port->serial, port);
1236 	if (ret < 0)
1237 		return ret;
1238 
1239 	port_priv->phy_num = ret;
1240 	port_priv->port = port;
1241 	usb_set_serial_port_data(port, port_priv);
1242 	dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
1243 			port->port_number, port_priv->phy_num);
1244 
1245 	/*
1246 	 * The F81532/534 will hang-up when enable LSR interrupt in IER and
1247 	 * occur data overrun. So we'll disable the LSR interrupt in probe()
1248 	 * and submit the LSR worker to clear LSR state when reported LSR error
1249 	 * bit with bulk-in data in f81534_process_per_serial_block().
1250 	 */
1251 	ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
1252 			UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
1253 	if (ret)
1254 		return ret;
1255 
1256 	return 0;
1257 }
1258 
1259 static int f81534_port_remove(struct usb_serial_port *port)
1260 {
1261 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1262 
1263 	flush_work(&port_priv->lsr_work);
1264 	return 0;
1265 }
1266 
1267 static int f81534_tiocmget(struct tty_struct *tty)
1268 {
1269 	struct usb_serial_port *port = tty->driver_data;
1270 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1271 	int status;
1272 	int r;
1273 	u8 msr;
1274 	u8 mcr;
1275 
1276 	/* Read current MSR from device */
1277 	status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1278 	if (status)
1279 		return status;
1280 
1281 	mutex_lock(&port_priv->mcr_mutex);
1282 	mcr = port_priv->shadow_mcr;
1283 	mutex_unlock(&port_priv->mcr_mutex);
1284 
1285 	r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
1286 	    (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
1287 	    (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
1288 	    (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
1289 	    (msr & UART_MSR_RI ? TIOCM_RI : 0) |
1290 	    (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
1291 
1292 	return r;
1293 }
1294 
1295 static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1296 				unsigned int clear)
1297 {
1298 	struct usb_serial_port *port = tty->driver_data;
1299 
1300 	return f81534_update_mctrl(port, set, clear);
1301 }
1302 
1303 static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1304 {
1305 	if (on)
1306 		f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1307 	else
1308 		f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1309 }
1310 
1311 static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1312 			const u8 *buf, int count)
1313 {
1314 	int bytes_out, status;
1315 
1316 	if (!count)
1317 		return 0;
1318 
1319 	bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1320 					&port->lock);
1321 
1322 	status = f81534_submit_writer(port, GFP_ATOMIC);
1323 	if (status) {
1324 		dev_err(&port->dev, "%s: submit failed\n", __func__);
1325 		return status;
1326 	}
1327 
1328 	return bytes_out;
1329 }
1330 
1331 static bool f81534_tx_empty(struct usb_serial_port *port)
1332 {
1333 	struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1334 
1335 	return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1336 }
1337 
1338 static int f81534_resume(struct usb_serial *serial)
1339 {
1340 	struct f81534_serial_private *serial_priv =
1341 			usb_get_serial_data(serial);
1342 	struct usb_serial_port *port;
1343 	int error = 0;
1344 	int status;
1345 	size_t i;
1346 
1347 	/*
1348 	 * We'll register port 0 bulkin when port had opened, It'll take all
1349 	 * port received data, MSR register change and TX_EMPTY information.
1350 	 */
1351 	mutex_lock(&serial_priv->urb_mutex);
1352 
1353 	if (serial_priv->opened_port) {
1354 		status = f81534_submit_read_urb(serial, GFP_NOIO);
1355 		if (status) {
1356 			mutex_unlock(&serial_priv->urb_mutex);
1357 			return status;
1358 		}
1359 	}
1360 
1361 	mutex_unlock(&serial_priv->urb_mutex);
1362 
1363 	for (i = 0; i < serial->num_ports; i++) {
1364 		port = serial->port[i];
1365 		if (!tty_port_initialized(&port->port))
1366 			continue;
1367 
1368 		status = f81534_submit_writer(port, GFP_NOIO);
1369 		if (status) {
1370 			dev_err(&port->dev, "%s: submit failed\n", __func__);
1371 			++error;
1372 		}
1373 	}
1374 
1375 	if (error)
1376 		return -EIO;
1377 
1378 	return 0;
1379 }
1380 
1381 static struct usb_serial_driver f81534_device = {
1382 	.driver = {
1383 		   .owner = THIS_MODULE,
1384 		   .name = "f81534",
1385 	},
1386 	.description =		DRIVER_DESC,
1387 	.id_table =		f81534_id_table,
1388 	.num_bulk_in =		1,
1389 	.num_bulk_out =		1,
1390 	.open =			f81534_open,
1391 	.close =		f81534_close,
1392 	.write =		f81534_write,
1393 	.tx_empty =		f81534_tx_empty,
1394 	.calc_num_ports =	f81534_calc_num_ports,
1395 	.attach =		f81534_attach,
1396 	.port_probe =		f81534_port_probe,
1397 	.port_remove =		f81534_port_remove,
1398 	.break_ctl =		f81534_break_ctl,
1399 	.dtr_rts =		f81534_dtr_rts,
1400 	.process_read_urb =	f81534_process_read_urb,
1401 	.ioctl =		f81534_ioctl,
1402 	.tiocmget =		f81534_tiocmget,
1403 	.tiocmset =		f81534_tiocmset,
1404 	.write_bulk_callback =	f81534_write_usb_callback,
1405 	.set_termios =		f81534_set_termios,
1406 	.resume =		f81534_resume,
1407 };
1408 
1409 static struct usb_serial_driver *const serial_drivers[] = {
1410 	&f81534_device, NULL
1411 };
1412 
1413 module_usb_serial_driver(serial_drivers, f81534_id_table);
1414 
1415 MODULE_DEVICE_TABLE(usb, f81534_id_table);
1416 MODULE_DESCRIPTION(DRIVER_DESC);
1417 MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
1418 MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
1419 MODULE_LICENSE("GPL");
1420