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