2 * F81532/F81534 USB to Serial Ports Bridge
4 * F81532 => 2 Serial Ports
5 * F81534 => 4 Serial Ports
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
13 * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
14 * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
16 * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
17 * for all serial port TX and 1 endpoint bulk-in for all serial port read in
18 * (Read Data/MSR/LSR).
20 * Write URB is fixed with 512bytes, per serial port used 128Bytes.
21 * It can be described by f81534_prepare_write_buffer()
23 * Read URB is 512Bytes max, per serial port used 128Bytes.
24 * It can be described by f81534_process_read_urb() and maybe received with
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/usb.h>
32 #include <linux/usb/serial.h>
33 #include <linux/serial_reg.h>
34 #include <linux/module.h>
35 #include <linux/uaccess.h>
37 /* Serial Port register Address */
38 #define F81534_UART_BASE_ADDRESS 0x1200
39 #define F81534_UART_OFFSET 0x10
40 #define F81534_DIVISOR_LSB_REG (0x00 + F81534_UART_BASE_ADDRESS)
41 #define F81534_DIVISOR_MSB_REG (0x01 + F81534_UART_BASE_ADDRESS)
42 #define F81534_FIFO_CONTROL_REG (0x02 + F81534_UART_BASE_ADDRESS)
43 #define F81534_LINE_CONTROL_REG (0x03 + F81534_UART_BASE_ADDRESS)
44 #define F81534_MODEM_CONTROL_REG (0x04 + F81534_UART_BASE_ADDRESS)
45 #define F81534_MODEM_STATUS_REG (0x06 + F81534_UART_BASE_ADDRESS)
46 #define F81534_CONFIG1_REG (0x09 + F81534_UART_BASE_ADDRESS)
48 #define F81534_DEF_CONF_ADDRESS_START 0x3000
49 #define F81534_DEF_CONF_SIZE 8
51 #define F81534_CUSTOM_ADDRESS_START 0x2f00
52 #define F81534_CUSTOM_DATA_SIZE 0x10
53 #define F81534_CUSTOM_NO_CUSTOM_DATA 0xff
54 #define F81534_CUSTOM_VALID_TOKEN 0xf0
55 #define F81534_CONF_OFFSET 1
57 #define F81534_MAX_DATA_BLOCK 64
58 #define F81534_MAX_BUS_RETRY 20
60 /* Default URB timeout for USB operations */
61 #define F81534_USB_MAX_RETRY 10
62 #define F81534_USB_TIMEOUT 1000
63 #define F81534_SET_GET_REGISTER 0xA0
65 #define F81534_NUM_PORT 4
66 #define F81534_UNUSED_PORT 0xff
67 #define F81534_WRITE_BUFFER_SIZE 512
69 #define DRIVER_DESC "Fintek F81532/F81534"
70 #define FINTEK_VENDOR_ID_1 0x1934
71 #define FINTEK_VENDOR_ID_2 0x2C42
72 #define FINTEK_DEVICE_ID 0x1202
73 #define F81534_MAX_TX_SIZE 124
74 #define F81534_MAX_RX_SIZE 124
75 #define F81534_RECEIVE_BLOCK_SIZE 128
76 #define F81534_MAX_RECEIVE_BLOCK_SIZE 512
78 #define F81534_TOKEN_RECEIVE 0x01
79 #define F81534_TOKEN_WRITE 0x02
80 #define F81534_TOKEN_TX_EMPTY 0x03
81 #define F81534_TOKEN_MSR_CHANGE 0x04
84 * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
85 * idle if we performed any command.
87 * SPI Bus status register: F81534_BUS_REG_STATUS
91 #define F81534_BUS_BUSY (BIT(0) | BIT(1))
92 #define F81534_BUS_IDLE BIT(2)
93 #define F81534_BUS_READ_DATA 0x1004
94 #define F81534_BUS_REG_STATUS 0x1003
95 #define F81534_BUS_REG_START 0x1002
96 #define F81534_BUS_REG_END 0x1001
98 #define F81534_CMD_READ 0x03
100 #define F81534_DEFAULT_BAUD_RATE 9600
101 #define F81534_MAX_BAUDRATE 115200
103 #define F81534_PORT_CONF_DISABLE_PORT BIT(3)
104 #define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
105 #define F81534_PORT_UNAVAILABLE \
106 (F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
108 #define F81534_1X_RXTRIGGER 0xc3
109 #define F81534_8X_RXTRIGGER 0xcf
111 static const struct usb_device_id f81534_id_table[] = {
112 { USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
113 { USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
114 {} /* Terminating entry */
117 #define F81534_TX_EMPTY_BIT 0
119 struct f81534_serial_private {
120 u8 conf_data[F81534_DEF_CONF_SIZE];
121 int tty_idx[F81534_NUM_PORT];
124 struct mutex urb_mutex;
127 struct f81534_port_private {
128 struct mutex mcr_mutex;
129 unsigned long tx_empty;
136 static int f81534_logic_to_phy_port(struct usb_serial *serial,
137 struct usb_serial_port *port)
139 struct f81534_serial_private *serial_priv =
140 usb_get_serial_data(port->serial);
144 for (i = 0; i < F81534_NUM_PORT; ++i) {
145 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
148 if (port->port_number == count)
157 static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
159 struct usb_interface *interface = serial->interface;
160 struct usb_device *dev = serial->dev;
161 size_t count = F81534_USB_MAX_RETRY;
165 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
172 * Our device maybe not reply when heavily loading, We'll retry for
173 * F81534_USB_MAX_RETRY times.
176 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
177 F81534_SET_GET_REGISTER,
178 USB_TYPE_VENDOR | USB_DIR_OUT,
179 reg, 0, tmp, sizeof(u8),
184 } else if (status == 0) {
190 dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
191 __func__, reg, data, status);
198 static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
200 struct usb_interface *interface = serial->interface;
201 struct usb_device *dev = serial->dev;
202 size_t count = F81534_USB_MAX_RETRY;
206 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
211 * Our device maybe not reply when heavily loading, We'll retry for
212 * F81534_USB_MAX_RETRY times.
215 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
216 F81534_SET_GET_REGISTER,
217 USB_TYPE_VENDOR | USB_DIR_IN,
218 reg, 0, tmp, sizeof(u8),
223 } else if (status == 0) {
229 dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
241 static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
244 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
246 return f81534_set_register(port->serial,
247 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
250 static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
253 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
255 return f81534_get_register(port->serial,
256 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
260 * If we try to access the internal flash via SPI bus, we should check the bus
261 * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
263 static int f81534_wait_for_spi_idle(struct usb_serial *serial)
265 size_t count = F81534_MAX_BUS_RETRY;
270 status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
275 if (tmp & F81534_BUS_BUSY)
278 if (tmp & F81534_BUS_IDLE)
284 dev_err(&serial->interface->dev,
285 "%s: timed out waiting for idle SPI bus\n",
290 return f81534_set_register(serial, F81534_BUS_REG_STATUS,
291 tmp & ~F81534_BUS_IDLE);
294 static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
299 status = f81534_get_register(serial, reg, data);
303 return f81534_wait_for_spi_idle(serial);
306 static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
310 status = f81534_set_register(serial, reg, data);
314 return f81534_wait_for_spi_idle(serial);
317 static int f81534_read_flash(struct usb_serial *serial, u32 address,
318 size_t size, u8 *buf)
320 u8 tmp_buf[F81534_MAX_DATA_BLOCK];
328 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
333 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
334 (address >> 16) & 0xff);
338 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
339 (address >> 8) & 0xff);
343 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
344 (address >> 0) & 0xff);
348 /* Continuous read mode */
350 read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
352 for (count = 0; count < read_size; ++count) {
353 /* To write F81534_BUS_REG_END when final byte */
354 if (size <= F81534_MAX_DATA_BLOCK &&
355 read_size == count + 1)
356 reg_tmp = F81534_BUS_REG_END;
358 reg_tmp = F81534_BUS_REG_START;
361 * Dummy code, force IC to generate a read pulse, the
362 * set of value 0xf1 is dont care (any value is ok)
364 status = f81534_set_spi_register(serial, reg_tmp,
369 status = f81534_get_spi_register(serial,
370 F81534_BUS_READ_DATA,
375 offset = count + block * F81534_MAX_DATA_BLOCK;
376 buf[offset] = tmp_buf[count];
386 static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
388 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
389 int phy_num = port_priv->phy_num;
394 * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
395 * index 0: port phy idx (e.g., 0,1,2,3)
396 * index 1: only F81534_TOKEN_WRITE
397 * index 2: serial TX out length
399 * index 4~127: serial out data block
401 for (i = 0; i < F81534_NUM_PORT; ++i) {
402 buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
403 buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
404 buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
405 buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
408 tx_len = kfifo_out_locked(&port->write_fifo,
409 &buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
410 F81534_MAX_TX_SIZE, &port->lock);
412 buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
415 static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
417 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
422 /* Check is any data in write_fifo */
423 spin_lock_irqsave(&port->lock, flags);
425 if (kfifo_is_empty(&port->write_fifo)) {
426 spin_unlock_irqrestore(&port->lock, flags);
430 spin_unlock_irqrestore(&port->lock, flags);
432 /* Check H/W is TXEMPTY */
433 if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
436 urb = port->write_urbs[0];
437 f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
438 urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
440 result = usb_submit_urb(urb, mem_flags);
442 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
443 dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
448 usb_serial_port_softint(port);
452 static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
457 /* Round to nearest divisor */
458 return DIV_ROUND_CLOSEST(clockrate, baudrate);
461 static int f81534_set_port_config(struct usb_serial_port *port, u32 baudrate,
468 if (baudrate <= 1200)
469 value = F81534_1X_RXTRIGGER; /* 128 FIFO & TL: 1x */
471 value = F81534_8X_RXTRIGGER; /* 128 FIFO & TL: 8x */
473 status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
475 dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
479 if (baudrate <= 1200)
480 value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
482 value = UART_FCR_R_TRIG_11 | UART_FCR_ENABLE_FIFO; /* TL: 14 */
484 status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
487 dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
491 divisor = f81534_calc_baud_divisor(baudrate, F81534_MAX_BAUDRATE);
492 value = UART_LCR_DLAB;
493 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
496 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
500 value = divisor & 0xff;
501 status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
503 dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
507 value = (divisor >> 8) & 0xff;
508 status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
510 dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
514 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG, lcr);
516 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
523 static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
526 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
530 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
531 return 0; /* no change */
533 mutex_lock(&port_priv->mcr_mutex);
535 /* 'Set' takes precedence over 'Clear' */
538 /* Always enable UART_MCR_OUT2 */
539 tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
541 if (clear & TIOCM_DTR)
542 tmp &= ~UART_MCR_DTR;
544 if (clear & TIOCM_RTS)
545 tmp &= ~UART_MCR_RTS;
553 status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
555 dev_err(&port->dev, "%s: MCR write failed\n", __func__);
556 mutex_unlock(&port_priv->mcr_mutex);
560 port_priv->shadow_mcr = tmp;
561 mutex_unlock(&port_priv->mcr_mutex);
566 * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
567 * for latest configuration index. If nothing found
568 * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
569 * F81534_DEF_CONF_ADDRESS_START section.
571 * Due to we only use block0 to save data, so *index should be 0 or
572 * F81534_CUSTOM_NO_CUSTOM_DATA.
574 static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
579 status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
582 dev_err(&serial->interface->dev, "%s: read failed: %d\n",
587 /* We'll use the custom data when the data is valid. */
588 if (tmp == F81534_CUSTOM_VALID_TOKEN)
591 *index = F81534_CUSTOM_NO_CUSTOM_DATA;
597 * We had 2 generation of F81532/534 IC. All has an internal storage.
599 * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
600 * internal data will used. All mode and gpio control should manually set
601 * by AP or Driver and all storage space value are 0xff. The
602 * f81534_calc_num_ports() will run to final we marked as "oldest version"
605 * 2rd is designed to more generic to use any transceiver and this is our
606 * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
607 * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
608 * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
609 * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
610 * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
611 * The f81534_calc_num_ports() will run to "new style" with checking
612 * F81534_PORT_UNAVAILABLE section.
614 static int f81534_calc_num_ports(struct usb_serial *serial,
615 struct usb_serial_endpoints *epds)
617 struct device *dev = &serial->interface->dev;
618 int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
619 int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
620 u8 setting[F81534_CUSTOM_DATA_SIZE];
626 if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
627 size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
628 dev_err(dev, "unsupported endpoint max packet size\n");
632 /* Check had custom setting */
633 status = f81534_find_config_idx(serial, &setting_idx);
635 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
641 * We'll read custom data only when data available, otherwise we'll
642 * read default value instead.
644 if (setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
645 status = f81534_read_flash(serial,
646 F81534_CUSTOM_ADDRESS_START +
648 sizeof(setting), setting);
650 dev_err(&serial->interface->dev,
651 "%s: get custom data failed: %d\n",
656 dev_dbg(&serial->interface->dev,
657 "%s: read config from block: %d\n", __func__,
660 /* Read default board setting */
661 status = f81534_read_flash(serial,
662 F81534_DEF_CONF_ADDRESS_START, F81534_NUM_PORT,
666 dev_err(&serial->interface->dev,
667 "%s: read failed: %d\n", __func__,
672 dev_dbg(&serial->interface->dev, "%s: read default config\n",
676 /* New style, find all possible ports */
677 for (i = 0; i < F81534_NUM_PORT; ++i) {
678 if (setting[i] & F81534_PORT_UNAVAILABLE)
685 dev_warn(&serial->interface->dev,
686 "no config found, assuming 4 ports\n");
687 num_port = 4; /* Nothing found, oldest version IC */
691 * Setup bulk-out endpoint multiplexing. All ports share the same
694 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
696 for (i = 1; i < num_port; ++i)
697 epds->bulk_out[i] = epds->bulk_out[0];
699 epds->num_bulk_out = num_port;
704 static void f81534_set_termios(struct tty_struct *tty,
705 struct usb_serial_port *port,
706 struct ktermios *old_termios)
712 if (C_BAUD(tty) == B0)
713 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
714 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
715 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
718 new_lcr |= UART_LCR_PARITY;
721 new_lcr |= UART_LCR_EPAR;
724 new_lcr |= UART_LCR_SPAR;
728 new_lcr |= UART_LCR_STOP;
730 switch (C_CSIZE(tty)) {
732 new_lcr |= UART_LCR_WLEN5;
735 new_lcr |= UART_LCR_WLEN6;
738 new_lcr |= UART_LCR_WLEN7;
742 new_lcr |= UART_LCR_WLEN8;
746 baud = tty_get_baud_rate(tty);
750 if (baud > F81534_MAX_BAUDRATE) {
752 baud = tty_termios_baud_rate(old_termios);
754 baud = F81534_DEFAULT_BAUD_RATE;
756 tty_encode_baud_rate(tty, baud, baud);
759 dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
761 status = f81534_set_port_config(port, baud, new_lcr);
763 dev_err(&port->dev, "%s: set port config failed: %d\n",
768 static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
770 return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
773 static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
775 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
776 struct tty_struct *tty;
780 if (!(msr & UART_MSR_ANY_DELTA))
783 spin_lock_irqsave(&port_priv->msr_lock, flags);
784 old_msr = port_priv->shadow_msr;
785 port_priv->shadow_msr = msr;
786 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
788 dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
791 /* Update input line counters */
792 if (msr & UART_MSR_DCTS)
794 if (msr & UART_MSR_DDSR)
796 if (msr & UART_MSR_DDCD)
798 if (msr & UART_MSR_TERI)
801 wake_up_interruptible(&port->port.delta_msr_wait);
803 if (!(msr & UART_MSR_DDCD))
806 dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
807 __func__, port_priv->phy_num, old_msr, msr);
809 tty = tty_port_tty_get(&port->port);
813 usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
817 static int f81534_read_msr(struct usb_serial_port *port)
819 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
824 /* Get MSR initial value */
825 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
829 /* Force update current state */
830 spin_lock_irqsave(&port_priv->msr_lock, flags);
831 port_priv->shadow_msr = msr;
832 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
837 static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
839 struct f81534_serial_private *serial_priv =
840 usb_get_serial_data(port->serial);
841 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
844 status = f81534_set_port_register(port,
845 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
846 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
848 dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
854 f81534_set_termios(tty, port, NULL);
856 status = f81534_read_msr(port);
860 mutex_lock(&serial_priv->urb_mutex);
862 /* Submit Read URBs for first port opened */
863 if (!serial_priv->opened_port) {
864 status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
869 serial_priv->opened_port++;
872 mutex_unlock(&serial_priv->urb_mutex);
874 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
878 static void f81534_close(struct usb_serial_port *port)
880 struct f81534_serial_private *serial_priv =
881 usb_get_serial_data(port->serial);
882 struct usb_serial_port *port0 = port->serial->port[0];
886 usb_kill_urb(port->write_urbs[0]);
888 spin_lock_irqsave(&port->lock, flags);
889 kfifo_reset_out(&port->write_fifo);
890 spin_unlock_irqrestore(&port->lock, flags);
892 /* Kill Read URBs when final port closed */
893 mutex_lock(&serial_priv->urb_mutex);
894 serial_priv->opened_port--;
896 if (!serial_priv->opened_port) {
897 for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
898 usb_kill_urb(port0->read_urbs[i]);
901 mutex_unlock(&serial_priv->urb_mutex);
904 static int f81534_get_serial_info(struct usb_serial_port *port,
905 struct serial_struct __user *retinfo)
907 struct f81534_port_private *port_priv;
908 struct serial_struct tmp;
910 port_priv = usb_get_serial_port_data(port);
912 memset(&tmp, 0, sizeof(tmp));
914 tmp.type = PORT_16550A;
915 tmp.port = port->port_number;
916 tmp.line = port->minor;
917 tmp.baud_base = F81534_MAX_BAUDRATE;
919 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
925 static int f81534_ioctl(struct tty_struct *tty, unsigned int cmd,
928 struct usb_serial_port *port = tty->driver_data;
929 struct serial_struct __user *buf = (struct serial_struct __user *)arg;
933 return f81534_get_serial_info(port, buf);
941 static void f81534_process_per_serial_block(struct usb_serial_port *port,
944 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
945 int phy_num = data[0];
946 size_t read_size = 0;
953 * The block layout is 128 Bytes
954 * index 0: port phy idx (e.g., 0,1,2,3),
955 * index 1: It's could be
956 * F81534_TOKEN_RECEIVE
957 * F81534_TOKEN_TX_EMPTY
958 * F81534_TOKEN_MSR_CHANGE
959 * index 2: serial in size (data+lsr, must be even)
960 * meaningful for F81534_TOKEN_RECEIVE only
961 * index 3: current MSR with this device
962 * index 4~127: serial in data block (data+lsr, must be even)
965 case F81534_TOKEN_TX_EMPTY:
966 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
968 /* Try to submit writer */
969 status = f81534_submit_writer(port, GFP_ATOMIC);
971 dev_err(&port->dev, "%s: submit failed\n", __func__);
974 case F81534_TOKEN_MSR_CHANGE:
975 f81534_msr_changed(port, data[3]);
978 case F81534_TOKEN_RECEIVE:
980 if (read_size > F81534_MAX_RX_SIZE) {
982 "%s: phy: %d read_size: %zu larger than: %d\n",
983 __func__, phy_num, read_size,
991 dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
996 for (i = 4; i < 4 + read_size; i += 2) {
997 tty_flag = TTY_NORMAL;
1000 if (lsr & UART_LSR_BRK_ERROR_BITS) {
1001 if (lsr & UART_LSR_BI) {
1002 tty_flag = TTY_BREAK;
1004 usb_serial_handle_break(port);
1005 } else if (lsr & UART_LSR_PE) {
1006 tty_flag = TTY_PARITY;
1007 port->icount.parity++;
1008 } else if (lsr & UART_LSR_FE) {
1009 tty_flag = TTY_FRAME;
1010 port->icount.frame++;
1013 if (lsr & UART_LSR_OE) {
1014 port->icount.overrun++;
1015 tty_insert_flip_char(&port->port, 0,
1020 if (port->port.console && port->sysrq) {
1021 if (usb_serial_handle_sysrq_char(port, data[i]))
1025 tty_insert_flip_char(&port->port, data[i], tty_flag);
1028 tty_flip_buffer_push(&port->port);
1031 static void f81534_process_read_urb(struct urb *urb)
1033 struct f81534_serial_private *serial_priv;
1034 struct usb_serial_port *port;
1035 struct usb_serial *serial;
1041 if (!urb->actual_length ||
1042 urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1046 port = urb->context;
1047 serial = port->serial;
1048 buf = urb->transfer_buffer;
1049 serial_priv = usb_get_serial_data(serial);
1051 for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
1052 phy_port_num = buf[i];
1053 if (phy_port_num >= F81534_NUM_PORT) {
1055 "%s: phy_port_num: %d larger than: %d\n",
1056 __func__, phy_port_num, F81534_NUM_PORT);
1060 tty_port_num = serial_priv->tty_idx[phy_port_num];
1061 port = serial->port[tty_port_num];
1063 if (tty_port_initialized(&port->port))
1064 f81534_process_per_serial_block(port, &buf[i]);
1068 static void f81534_write_usb_callback(struct urb *urb)
1070 struct usb_serial_port *port = urb->context;
1072 switch (urb->status) {
1078 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1079 __func__, urb->status);
1082 dev_err(&port->dev, "%s - urb stopped: %d\n",
1083 __func__, urb->status);
1086 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1087 __func__, urb->status);
1092 static int f81534_attach(struct usb_serial *serial)
1094 struct f81534_serial_private *serial_priv;
1099 serial_priv = devm_kzalloc(&serial->interface->dev,
1100 sizeof(*serial_priv), GFP_KERNEL);
1104 usb_set_serial_data(serial, serial_priv);
1106 mutex_init(&serial_priv->urb_mutex);
1108 /* Check had custom setting */
1109 status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
1111 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
1117 * We'll read custom data only when data available, otherwise we'll
1118 * read default value instead.
1120 if (serial_priv->setting_idx == F81534_CUSTOM_NO_CUSTOM_DATA) {
1122 * The default configuration layout:
1123 * byte 0/1/2/3: uart setting
1125 status = f81534_read_flash(serial,
1126 F81534_DEF_CONF_ADDRESS_START,
1127 F81534_DEF_CONF_SIZE,
1128 serial_priv->conf_data);
1130 dev_err(&serial->interface->dev,
1131 "%s: read reserve data failed: %d\n",
1136 /* Only read 8 bytes for mode & GPIO */
1137 status = f81534_read_flash(serial,
1138 F81534_CUSTOM_ADDRESS_START +
1140 sizeof(serial_priv->conf_data),
1141 serial_priv->conf_data);
1143 dev_err(&serial->interface->dev,
1144 "%s: idx: %d get data failed: %d\n",
1145 __func__, serial_priv->setting_idx,
1151 /* Assign phy-to-logic mapping */
1152 for (i = 0; i < F81534_NUM_PORT; ++i) {
1153 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
1156 serial_priv->tty_idx[i] = index++;
1157 dev_dbg(&serial->interface->dev,
1158 "%s: phy_num: %d, tty_idx: %d\n", __func__, i,
1159 serial_priv->tty_idx[i]);
1165 static int f81534_port_probe(struct usb_serial_port *port)
1167 struct f81534_port_private *port_priv;
1170 port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
1174 spin_lock_init(&port_priv->msr_lock);
1175 mutex_init(&port_priv->mcr_mutex);
1177 /* Assign logic-to-phy mapping */
1178 ret = f81534_logic_to_phy_port(port->serial, port);
1182 port_priv->phy_num = ret;
1183 usb_set_serial_port_data(port, port_priv);
1184 dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
1185 port->port_number, port_priv->phy_num);
1190 static int f81534_tiocmget(struct tty_struct *tty)
1192 struct usb_serial_port *port = tty->driver_data;
1193 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1199 /* Read current MSR from device */
1200 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1204 mutex_lock(&port_priv->mcr_mutex);
1205 mcr = port_priv->shadow_mcr;
1206 mutex_unlock(&port_priv->mcr_mutex);
1208 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
1209 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
1210 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
1211 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
1212 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
1213 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
1218 static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1221 struct usb_serial_port *port = tty->driver_data;
1223 return f81534_update_mctrl(port, set, clear);
1226 static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1229 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1231 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1234 static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1235 const u8 *buf, int count)
1237 int bytes_out, status;
1242 bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1245 status = f81534_submit_writer(port, GFP_ATOMIC);
1247 dev_err(&port->dev, "%s: submit failed\n", __func__);
1254 static bool f81534_tx_empty(struct usb_serial_port *port)
1256 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1258 return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1261 static int f81534_resume(struct usb_serial *serial)
1263 struct f81534_serial_private *serial_priv =
1264 usb_get_serial_data(serial);
1265 struct usb_serial_port *port;
1271 * We'll register port 0 bulkin when port had opened, It'll take all
1272 * port received data, MSR register change and TX_EMPTY information.
1274 mutex_lock(&serial_priv->urb_mutex);
1276 if (serial_priv->opened_port) {
1277 status = f81534_submit_read_urb(serial, GFP_NOIO);
1279 mutex_unlock(&serial_priv->urb_mutex);
1284 mutex_unlock(&serial_priv->urb_mutex);
1286 for (i = 0; i < serial->num_ports; i++) {
1287 port = serial->port[i];
1288 if (!tty_port_initialized(&port->port))
1291 status = f81534_submit_writer(port, GFP_NOIO);
1293 dev_err(&port->dev, "%s: submit failed\n", __func__);
1304 static struct usb_serial_driver f81534_device = {
1306 .owner = THIS_MODULE,
1309 .description = DRIVER_DESC,
1310 .id_table = f81534_id_table,
1313 .open = f81534_open,
1314 .close = f81534_close,
1315 .write = f81534_write,
1316 .tx_empty = f81534_tx_empty,
1317 .calc_num_ports = f81534_calc_num_ports,
1318 .attach = f81534_attach,
1319 .port_probe = f81534_port_probe,
1320 .dtr_rts = f81534_dtr_rts,
1321 .process_read_urb = f81534_process_read_urb,
1322 .ioctl = f81534_ioctl,
1323 .tiocmget = f81534_tiocmget,
1324 .tiocmset = f81534_tiocmset,
1325 .write_bulk_callback = f81534_write_usb_callback,
1326 .set_termios = f81534_set_termios,
1327 .resume = f81534_resume,
1330 static struct usb_serial_driver *const serial_drivers[] = {
1331 &f81534_device, NULL
1334 module_usb_serial_driver(serial_drivers, f81534_id_table);
1336 MODULE_DEVICE_TABLE(usb, f81534_id_table);
1337 MODULE_DESCRIPTION(DRIVER_DESC);
1338 MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
1339 MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
1340 MODULE_LICENSE("GPL");