1 // SPDX-License-Identifier: GPL-2.0+
3 * F81532/F81534 USB to Serial Ports Bridge
5 * F81532 => 2 Serial Ports
6 * F81534 => 4 Serial Ports
8 * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
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).
16 * Write URB is fixed with 512bytes, per serial port used 128Bytes.
17 * It can be described by f81534_prepare_write_buffer()
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
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>
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)
46 #define F81534_DEF_CONF_ADDRESS_START 0x3000
47 #define F81534_DEF_CONF_SIZE 8
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
55 #define F81534_MAX_DATA_BLOCK 64
56 #define F81534_MAX_BUS_RETRY 20
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
63 #define F81534_NUM_PORT 4
64 #define F81534_UNUSED_PORT 0xff
65 #define F81534_WRITE_BUFFER_SIZE 512
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
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
82 * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
83 * idle if we performed any command.
85 * SPI Bus status register: F81534_BUS_REG_STATUS
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
96 #define F81534_CMD_READ 0x03
98 #define F81534_DEFAULT_BAUD_RATE 9600
99 #define F81534_MAX_BAUDRATE 115200
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)
106 #define F81534_1X_RXTRIGGER 0xc3
107 #define F81534_8X_RXTRIGGER 0xcf
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 */
115 #define F81534_TX_EMPTY_BIT 0
117 struct f81534_serial_private {
118 u8 conf_data[F81534_DEF_CONF_SIZE];
119 int tty_idx[F81534_NUM_PORT];
122 struct mutex urb_mutex;
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;
138 static int f81534_logic_to_phy_port(struct usb_serial *serial,
139 struct usb_serial_port *port)
141 struct f81534_serial_private *serial_priv =
142 usb_get_serial_data(port->serial);
146 for (i = 0; i < F81534_NUM_PORT; ++i) {
147 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
150 if (port->port_number == count)
159 static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
161 struct usb_interface *interface = serial->interface;
162 struct usb_device *dev = serial->dev;
163 size_t count = F81534_USB_MAX_RETRY;
167 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
174 * Our device maybe not reply when heavily loading, We'll retry for
175 * F81534_USB_MAX_RETRY times.
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),
186 } else if (status == 0) {
192 dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
193 __func__, reg, data, status);
200 static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
202 struct usb_interface *interface = serial->interface;
203 struct usb_device *dev = serial->dev;
204 size_t count = F81534_USB_MAX_RETRY;
208 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
213 * Our device maybe not reply when heavily loading, We'll retry for
214 * F81534_USB_MAX_RETRY times.
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),
225 } else if (status == 0) {
231 dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
243 static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
246 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
248 return f81534_set_register(port->serial,
249 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
252 static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
255 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
257 return f81534_get_register(port->serial,
258 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
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
265 static int f81534_wait_for_spi_idle(struct usb_serial *serial)
267 size_t count = F81534_MAX_BUS_RETRY;
272 status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
277 if (tmp & F81534_BUS_BUSY)
280 if (tmp & F81534_BUS_IDLE)
286 dev_err(&serial->interface->dev,
287 "%s: timed out waiting for idle SPI bus\n",
292 return f81534_set_register(serial, F81534_BUS_REG_STATUS,
293 tmp & ~F81534_BUS_IDLE);
296 static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
301 status = f81534_get_register(serial, reg, data);
305 return f81534_wait_for_spi_idle(serial);
308 static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
312 status = f81534_set_register(serial, reg, data);
316 return f81534_wait_for_spi_idle(serial);
319 static int f81534_read_flash(struct usb_serial *serial, u32 address,
320 size_t size, u8 *buf)
322 u8 tmp_buf[F81534_MAX_DATA_BLOCK];
330 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
335 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
336 (address >> 16) & 0xff);
340 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
341 (address >> 8) & 0xff);
345 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
346 (address >> 0) & 0xff);
350 /* Continuous read mode */
352 read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
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;
360 reg_tmp = F81534_BUS_REG_START;
363 * Dummy code, force IC to generate a read pulse, the
364 * set of value 0xf1 is dont care (any value is ok)
366 status = f81534_set_spi_register(serial, reg_tmp,
371 status = f81534_get_spi_register(serial,
372 F81534_BUS_READ_DATA,
377 offset = count + block * F81534_MAX_DATA_BLOCK;
378 buf[offset] = tmp_buf[count];
388 static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
390 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
391 int phy_num = port_priv->phy_num;
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
401 * index 4~127: serial out data block
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;
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);
414 buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
417 static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
419 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
424 /* Check is any data in write_fifo */
425 spin_lock_irqsave(&port->lock, flags);
427 if (kfifo_is_empty(&port->write_fifo)) {
428 spin_unlock_irqrestore(&port->lock, flags);
432 spin_unlock_irqrestore(&port->lock, flags);
434 /* Check H/W is TXEMPTY */
435 if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
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;
442 result = usb_submit_urb(urb, mem_flags);
444 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
445 dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
450 usb_serial_port_softint(port);
454 static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
459 /* Round to nearest divisor */
460 return DIV_ROUND_CLOSEST(clockrate, baudrate);
463 static int f81534_set_port_config(struct usb_serial_port *port, u32 baudrate,
466 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
471 if (baudrate <= 1200)
472 value = F81534_1X_RXTRIGGER; /* 128 FIFO & TL: 1x */
474 value = F81534_8X_RXTRIGGER; /* 128 FIFO & TL: 8x */
476 status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
478 dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
482 if (baudrate <= 1200)
483 value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
485 value = UART_FCR_R_TRIG_11 | UART_FCR_ENABLE_FIFO; /* TL: 14 */
487 status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
490 dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
494 divisor = f81534_calc_baud_divisor(baudrate, F81534_MAX_BAUDRATE);
496 mutex_lock(&port_priv->lcr_mutex);
498 value = UART_LCR_DLAB;
499 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
502 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
506 value = divisor & 0xff;
507 status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
509 dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
513 value = (divisor >> 8) & 0xff;
514 status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
516 dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
520 value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
521 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
524 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
528 port_priv->shadow_lcr = value;
530 mutex_unlock(&port_priv->lcr_mutex);
535 static void f81534_break_ctl(struct tty_struct *tty, int break_state)
537 struct usb_serial_port *port = tty->driver_data;
538 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
541 mutex_lock(&port_priv->lcr_mutex);
544 port_priv->shadow_lcr |= UART_LCR_SBC;
546 port_priv->shadow_lcr &= ~UART_LCR_SBC;
548 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
549 port_priv->shadow_lcr);
551 dev_err(&port->dev, "set break failed: %d\n", status);
553 mutex_unlock(&port_priv->lcr_mutex);
556 static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
559 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
563 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
564 return 0; /* no change */
566 mutex_lock(&port_priv->mcr_mutex);
568 /* 'Set' takes precedence over 'Clear' */
571 /* Always enable UART_MCR_OUT2 */
572 tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
574 if (clear & TIOCM_DTR)
575 tmp &= ~UART_MCR_DTR;
577 if (clear & TIOCM_RTS)
578 tmp &= ~UART_MCR_RTS;
586 status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
588 dev_err(&port->dev, "%s: MCR write failed\n", __func__);
589 mutex_unlock(&port_priv->mcr_mutex);
593 port_priv->shadow_mcr = tmp;
594 mutex_unlock(&port_priv->mcr_mutex);
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.
604 * Due to we only use block0 to save data, so *index should be 0 or
605 * F81534_CUSTOM_NO_CUSTOM_DATA.
607 static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
612 status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
615 dev_err(&serial->interface->dev, "%s: read failed: %d\n",
620 /* We'll use the custom data when the data is valid. */
621 if (tmp == F81534_CUSTOM_VALID_TOKEN)
624 *index = F81534_CUSTOM_NO_CUSTOM_DATA;
630 * We had 2 generation of F81532/534 IC. All has an internal storage.
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"
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.
647 static int f81534_calc_num_ports(struct usb_serial *serial,
648 struct usb_serial_endpoints *epds)
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];
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");
665 /* Check had custom setting */
666 status = f81534_find_config_idx(serial, &setting_idx);
668 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
674 * We'll read custom data only when data available, otherwise we'll
675 * read default value instead.
677 if (setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
678 status = f81534_read_flash(serial,
679 F81534_CUSTOM_ADDRESS_START +
681 sizeof(setting), setting);
683 dev_err(&serial->interface->dev,
684 "%s: get custom data failed: %d\n",
689 dev_dbg(&serial->interface->dev,
690 "%s: read config from block: %d\n", __func__,
693 /* Read default board setting */
694 status = f81534_read_flash(serial,
695 F81534_DEF_CONF_ADDRESS_START, F81534_NUM_PORT,
699 dev_err(&serial->interface->dev,
700 "%s: read failed: %d\n", __func__,
705 dev_dbg(&serial->interface->dev, "%s: read default config\n",
709 /* New style, find all possible ports */
710 for (i = 0; i < F81534_NUM_PORT; ++i) {
711 if (setting[i] & F81534_PORT_UNAVAILABLE)
718 dev_warn(&serial->interface->dev,
719 "no config found, assuming 4 ports\n");
720 num_port = 4; /* Nothing found, oldest version IC */
724 * Setup bulk-out endpoint multiplexing. All ports share the same
727 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
729 for (i = 1; i < num_port; ++i)
730 epds->bulk_out[i] = epds->bulk_out[0];
732 epds->num_bulk_out = num_port;
737 static void f81534_set_termios(struct tty_struct *tty,
738 struct usb_serial_port *port,
739 struct ktermios *old_termios)
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);
751 new_lcr |= UART_LCR_PARITY;
754 new_lcr |= UART_LCR_EPAR;
757 new_lcr |= UART_LCR_SPAR;
761 new_lcr |= UART_LCR_STOP;
763 switch (C_CSIZE(tty)) {
765 new_lcr |= UART_LCR_WLEN5;
768 new_lcr |= UART_LCR_WLEN6;
771 new_lcr |= UART_LCR_WLEN7;
775 new_lcr |= UART_LCR_WLEN8;
779 baud = tty_get_baud_rate(tty);
783 if (baud > F81534_MAX_BAUDRATE) {
785 baud = tty_termios_baud_rate(old_termios);
787 baud = F81534_DEFAULT_BAUD_RATE;
789 tty_encode_baud_rate(tty, baud, baud);
792 dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
794 status = f81534_set_port_config(port, baud, new_lcr);
796 dev_err(&port->dev, "%s: set port config failed: %d\n",
801 static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
803 return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
806 static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
808 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
809 struct tty_struct *tty;
813 if (!(msr & UART_MSR_ANY_DELTA))
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);
821 dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
824 /* Update input line counters */
825 if (msr & UART_MSR_DCTS)
827 if (msr & UART_MSR_DDSR)
829 if (msr & UART_MSR_DDCD)
831 if (msr & UART_MSR_TERI)
834 wake_up_interruptible(&port->port.delta_msr_wait);
836 if (!(msr & UART_MSR_DDCD))
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);
842 tty = tty_port_tty_get(&port->port);
846 usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
850 static int f81534_read_msr(struct usb_serial_port *port)
852 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
857 /* Get MSR initial value */
858 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
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);
870 static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
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);
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);
881 dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
887 f81534_set_termios(tty, port, NULL);
889 status = f81534_read_msr(port);
893 mutex_lock(&serial_priv->urb_mutex);
895 /* Submit Read URBs for first port opened */
896 if (!serial_priv->opened_port) {
897 status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
902 serial_priv->opened_port++;
905 mutex_unlock(&serial_priv->urb_mutex);
907 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
911 static void f81534_close(struct usb_serial_port *port)
913 struct f81534_serial_private *serial_priv =
914 usb_get_serial_data(port->serial);
915 struct usb_serial_port *port0 = port->serial->port[0];
919 usb_kill_urb(port->write_urbs[0]);
921 spin_lock_irqsave(&port->lock, flags);
922 kfifo_reset_out(&port->write_fifo);
923 spin_unlock_irqrestore(&port->lock, flags);
925 /* Kill Read URBs when final port closed */
926 mutex_lock(&serial_priv->urb_mutex);
927 serial_priv->opened_port--;
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]);
934 mutex_unlock(&serial_priv->urb_mutex);
937 static int f81534_get_serial_info(struct usb_serial_port *port,
938 struct serial_struct __user *retinfo)
940 struct f81534_port_private *port_priv;
941 struct serial_struct tmp;
943 port_priv = usb_get_serial_port_data(port);
945 memset(&tmp, 0, sizeof(tmp));
947 tmp.type = PORT_16550A;
948 tmp.port = port->port_number;
949 tmp.line = port->minor;
950 tmp.baud_base = F81534_MAX_BAUDRATE;
952 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
958 static int f81534_ioctl(struct tty_struct *tty, unsigned int cmd,
961 struct usb_serial_port *port = tty->driver_data;
962 struct serial_struct __user *buf = (struct serial_struct __user *)arg;
966 return f81534_get_serial_info(port, buf);
974 static void f81534_process_per_serial_block(struct usb_serial_port *port,
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;
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)
998 case F81534_TOKEN_TX_EMPTY:
999 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1001 /* Try to submit writer */
1002 status = f81534_submit_writer(port, GFP_ATOMIC);
1004 dev_err(&port->dev, "%s: submit failed\n", __func__);
1007 case F81534_TOKEN_MSR_CHANGE:
1008 f81534_msr_changed(port, data[3]);
1011 case F81534_TOKEN_RECEIVE:
1012 read_size = data[2];
1013 if (read_size > F81534_MAX_RX_SIZE) {
1015 "%s: phy: %d read_size: %zu larger than: %d\n",
1016 __func__, phy_num, read_size,
1017 F81534_MAX_RX_SIZE);
1024 dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
1029 for (i = 4; i < 4 + read_size; i += 2) {
1030 tty_flag = TTY_NORMAL;
1033 if (lsr & UART_LSR_BRK_ERROR_BITS) {
1034 if (lsr & UART_LSR_BI) {
1035 tty_flag = TTY_BREAK;
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++;
1046 if (lsr & UART_LSR_OE) {
1047 port->icount.overrun++;
1048 tty_insert_flip_char(&port->port, 0,
1052 schedule_work(&port_priv->lsr_work);
1055 if (port->port.console && port->sysrq) {
1056 if (usb_serial_handle_sysrq_char(port, data[i]))
1060 tty_insert_flip_char(&port->port, data[i], tty_flag);
1063 tty_flip_buffer_push(&port->port);
1066 static void f81534_process_read_urb(struct urb *urb)
1068 struct f81534_serial_private *serial_priv;
1069 struct usb_serial_port *port;
1070 struct usb_serial *serial;
1076 if (!urb->actual_length ||
1077 urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1081 port = urb->context;
1082 serial = port->serial;
1083 buf = urb->transfer_buffer;
1084 serial_priv = usb_get_serial_data(serial);
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) {
1090 "%s: phy_port_num: %d larger than: %d\n",
1091 __func__, phy_port_num, F81534_NUM_PORT);
1095 tty_port_num = serial_priv->tty_idx[phy_port_num];
1096 port = serial->port[tty_port_num];
1098 if (tty_port_initialized(&port->port))
1099 f81534_process_per_serial_block(port, &buf[i]);
1103 static void f81534_write_usb_callback(struct urb *urb)
1105 struct usb_serial_port *port = urb->context;
1107 switch (urb->status) {
1113 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1114 __func__, urb->status);
1117 dev_err(&port->dev, "%s - urb stopped: %d\n",
1118 __func__, urb->status);
1121 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1122 __func__, urb->status);
1127 static int f81534_attach(struct usb_serial *serial)
1129 struct f81534_serial_private *serial_priv;
1134 serial_priv = devm_kzalloc(&serial->interface->dev,
1135 sizeof(*serial_priv), GFP_KERNEL);
1139 usb_set_serial_data(serial, serial_priv);
1141 mutex_init(&serial_priv->urb_mutex);
1143 /* Check had custom setting */
1144 status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
1146 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
1152 * We'll read custom data only when data available, otherwise we'll
1153 * read default value instead.
1155 if (serial_priv->setting_idx == F81534_CUSTOM_NO_CUSTOM_DATA) {
1157 * The default configuration layout:
1158 * byte 0/1/2/3: uart setting
1160 status = f81534_read_flash(serial,
1161 F81534_DEF_CONF_ADDRESS_START,
1162 F81534_DEF_CONF_SIZE,
1163 serial_priv->conf_data);
1165 dev_err(&serial->interface->dev,
1166 "%s: read reserve data failed: %d\n",
1171 /* Only read 8 bytes for mode & GPIO */
1172 status = f81534_read_flash(serial,
1173 F81534_CUSTOM_ADDRESS_START +
1175 sizeof(serial_priv->conf_data),
1176 serial_priv->conf_data);
1178 dev_err(&serial->interface->dev,
1179 "%s: idx: %d get data failed: %d\n",
1180 __func__, serial_priv->setting_idx,
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)
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]);
1200 static void f81534_lsr_worker(struct work_struct *work)
1202 struct f81534_port_private *port_priv;
1203 struct usb_serial_port *port;
1207 port_priv = container_of(work, struct f81534_port_private, lsr_work);
1208 port = port_priv->port;
1210 status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
1212 dev_warn(&port->dev, "read LSR failed: %d\n", status);
1215 static int f81534_port_probe(struct usb_serial_port *port)
1217 struct f81534_port_private *port_priv;
1220 port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
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);
1229 /* Assign logic-to-phy mapping */
1230 ret = f81534_logic_to_phy_port(port->serial, port);
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);
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().
1246 ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
1247 UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
1254 static int f81534_port_remove(struct usb_serial_port *port)
1256 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1258 flush_work(&port_priv->lsr_work);
1262 static int f81534_tiocmget(struct tty_struct *tty)
1264 struct usb_serial_port *port = tty->driver_data;
1265 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1271 /* Read current MSR from device */
1272 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1276 mutex_lock(&port_priv->mcr_mutex);
1277 mcr = port_priv->shadow_mcr;
1278 mutex_unlock(&port_priv->mcr_mutex);
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);
1290 static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1293 struct usb_serial_port *port = tty->driver_data;
1295 return f81534_update_mctrl(port, set, clear);
1298 static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1301 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1303 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1306 static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1307 const u8 *buf, int count)
1309 int bytes_out, status;
1314 bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1317 status = f81534_submit_writer(port, GFP_ATOMIC);
1319 dev_err(&port->dev, "%s: submit failed\n", __func__);
1326 static bool f81534_tx_empty(struct usb_serial_port *port)
1328 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1330 return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1333 static int f81534_resume(struct usb_serial *serial)
1335 struct f81534_serial_private *serial_priv =
1336 usb_get_serial_data(serial);
1337 struct usb_serial_port *port;
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.
1346 mutex_lock(&serial_priv->urb_mutex);
1348 if (serial_priv->opened_port) {
1349 status = f81534_submit_read_urb(serial, GFP_NOIO);
1351 mutex_unlock(&serial_priv->urb_mutex);
1356 mutex_unlock(&serial_priv->urb_mutex);
1358 for (i = 0; i < serial->num_ports; i++) {
1359 port = serial->port[i];
1360 if (!tty_port_initialized(&port->port))
1363 status = f81534_submit_writer(port, GFP_NOIO);
1365 dev_err(&port->dev, "%s: submit failed\n", __func__);
1376 static struct usb_serial_driver f81534_device = {
1378 .owner = THIS_MODULE,
1381 .description = DRIVER_DESC,
1382 .id_table = f81534_id_table,
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,
1404 static struct usb_serial_driver *const serial_drivers[] = {
1405 &f81534_device, NULL
1408 module_usb_serial_driver(serial_drivers, f81534_id_table);
1410 MODULE_DEVICE_TABLE(usb, f81534_id_table);
1411 MODULE_DESCRIPTION(DRIVER_DESC);
1414 MODULE_LICENSE("GPL");