Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * F81532/F81534 USB to Serial Ports Bridge
0004  *
0005  * F81532 => 2 Serial Ports
0006  * F81534 => 4 Serial Ports
0007  *
0008  * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
0009  * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
0010  * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
0011  *
0012  * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
0013  * for all serial port TX and 1 endpoint bulk-in for all serial port read in
0014  * (Read Data/MSR/LSR).
0015  *
0016  * Write URB is fixed with 512bytes, per serial port used 128Bytes.
0017  * It can be described by f81534_prepare_write_buffer()
0018  *
0019  * Read URB is 512Bytes max, per serial port used 128Bytes.
0020  * It can be described by f81534_process_read_urb() and maybe received with
0021  * 128x1,2,3,4 bytes.
0022  *
0023  */
0024 #include <linux/slab.h>
0025 #include <linux/tty.h>
0026 #include <linux/tty_flip.h>
0027 #include <linux/usb.h>
0028 #include <linux/usb/serial.h>
0029 #include <linux/serial_reg.h>
0030 #include <linux/module.h>
0031 #include <linux/uaccess.h>
0032 
0033 /* Serial Port register Address */
0034 #define F81534_UART_BASE_ADDRESS    0x1200
0035 #define F81534_UART_OFFSET      0x10
0036 #define F81534_DIVISOR_LSB_REG      (0x00 + F81534_UART_BASE_ADDRESS)
0037 #define F81534_DIVISOR_MSB_REG      (0x01 + F81534_UART_BASE_ADDRESS)
0038 #define F81534_INTERRUPT_ENABLE_REG (0x01 + F81534_UART_BASE_ADDRESS)
0039 #define F81534_FIFO_CONTROL_REG     (0x02 + F81534_UART_BASE_ADDRESS)
0040 #define F81534_LINE_CONTROL_REG     (0x03 + F81534_UART_BASE_ADDRESS)
0041 #define F81534_MODEM_CONTROL_REG    (0x04 + F81534_UART_BASE_ADDRESS)
0042 #define F81534_LINE_STATUS_REG      (0x05 + F81534_UART_BASE_ADDRESS)
0043 #define F81534_MODEM_STATUS_REG     (0x06 + F81534_UART_BASE_ADDRESS)
0044 #define F81534_CLOCK_REG        (0x08 + F81534_UART_BASE_ADDRESS)
0045 #define F81534_CONFIG1_REG      (0x09 + F81534_UART_BASE_ADDRESS)
0046 
0047 #define F81534_DEF_CONF_ADDRESS_START   0x3000
0048 #define F81534_DEF_CONF_SIZE        12
0049 
0050 #define F81534_CUSTOM_ADDRESS_START 0x2f00
0051 #define F81534_CUSTOM_DATA_SIZE     0x10
0052 #define F81534_CUSTOM_NO_CUSTOM_DATA    0xff
0053 #define F81534_CUSTOM_VALID_TOKEN   0xf0
0054 #define F81534_CONF_OFFSET      1
0055 #define F81534_CONF_INIT_GPIO_OFFSET    4
0056 #define F81534_CONF_WORK_GPIO_OFFSET    8
0057 #define F81534_CONF_GPIO_SHUTDOWN   7
0058 #define F81534_CONF_GPIO_RS232      1
0059 
0060 #define F81534_MAX_DATA_BLOCK       64
0061 #define F81534_MAX_BUS_RETRY        20
0062 
0063 /* Default URB timeout for USB operations */
0064 #define F81534_USB_MAX_RETRY        10
0065 #define F81534_USB_TIMEOUT      2000
0066 #define F81534_SET_GET_REGISTER     0xA0
0067 
0068 #define F81534_NUM_PORT         4
0069 #define F81534_UNUSED_PORT      0xff
0070 #define F81534_WRITE_BUFFER_SIZE    512
0071 
0072 #define DRIVER_DESC         "Fintek F81532/F81534"
0073 #define FINTEK_VENDOR_ID_1      0x1934
0074 #define FINTEK_VENDOR_ID_2      0x2C42
0075 #define FINTEK_DEVICE_ID        0x1202
0076 #define F81534_MAX_TX_SIZE      124
0077 #define F81534_MAX_RX_SIZE      124
0078 #define F81534_RECEIVE_BLOCK_SIZE   128
0079 #define F81534_MAX_RECEIVE_BLOCK_SIZE   512
0080 
0081 #define F81534_TOKEN_RECEIVE        0x01
0082 #define F81534_TOKEN_WRITE      0x02
0083 #define F81534_TOKEN_TX_EMPTY       0x03
0084 #define F81534_TOKEN_MSR_CHANGE     0x04
0085 
0086 /*
0087  * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
0088  * idle if we performed any command.
0089  *
0090  * SPI Bus status register: F81534_BUS_REG_STATUS
0091  *  Bit 0/1 : BUSY
0092  *  Bit 2   : IDLE
0093  */
0094 #define F81534_BUS_BUSY         (BIT(0) | BIT(1))
0095 #define F81534_BUS_IDLE         BIT(2)
0096 #define F81534_BUS_READ_DATA        0x1004
0097 #define F81534_BUS_REG_STATUS       0x1003
0098 #define F81534_BUS_REG_START        0x1002
0099 #define F81534_BUS_REG_END      0x1001
0100 
0101 #define F81534_CMD_READ         0x03
0102 
0103 #define F81534_DEFAULT_BAUD_RATE    9600
0104 
0105 #define F81534_PORT_CONF_RS232      0
0106 #define F81534_PORT_CONF_RS485      BIT(0)
0107 #define F81534_PORT_CONF_RS485_INVERT   (BIT(0) | BIT(1))
0108 #define F81534_PORT_CONF_MODE_MASK  GENMASK(1, 0)
0109 #define F81534_PORT_CONF_DISABLE_PORT   BIT(3)
0110 #define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
0111 #define F81534_PORT_UNAVAILABLE     \
0112     (F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
0113 
0114 
0115 #define F81534_1X_RXTRIGGER     0xc3
0116 #define F81534_8X_RXTRIGGER     0xcf
0117 
0118 /*
0119  * F81532/534 Clock registers (offset +08h)
0120  *
0121  * Bit0:    UART Enable (always on)
0122  * Bit2-1:  Clock source selector
0123  *          00: 1.846MHz.
0124  *          01: 18.46MHz.
0125  *          10: 24MHz.
0126  *          11: 14.77MHz.
0127  * Bit4:    Auto direction(RTS) control (RTS pin Low when TX)
0128  * Bit5:    Invert direction(RTS) when Bit4 enabled (RTS pin high when TX)
0129  */
0130 
0131 #define F81534_UART_EN          BIT(0)
0132 #define F81534_CLK_1_846_MHZ        0
0133 #define F81534_CLK_18_46_MHZ        BIT(1)
0134 #define F81534_CLK_24_MHZ       BIT(2)
0135 #define F81534_CLK_14_77_MHZ        (BIT(1) | BIT(2))
0136 #define F81534_CLK_MASK         GENMASK(2, 1)
0137 #define F81534_CLK_TX_DELAY_1BIT    BIT(3)
0138 #define F81534_CLK_RS485_MODE       BIT(4)
0139 #define F81534_CLK_RS485_INVERT     BIT(5)
0140 
0141 static const struct usb_device_id f81534_id_table[] = {
0142     { USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
0143     { USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
0144     {}          /* Terminating entry */
0145 };
0146 
0147 #define F81534_TX_EMPTY_BIT     0
0148 
0149 struct f81534_serial_private {
0150     u8 conf_data[F81534_DEF_CONF_SIZE];
0151     int tty_idx[F81534_NUM_PORT];
0152     u8 setting_idx;
0153     int opened_port;
0154     struct mutex urb_mutex;
0155 };
0156 
0157 struct f81534_port_private {
0158     struct mutex mcr_mutex;
0159     struct mutex lcr_mutex;
0160     struct work_struct lsr_work;
0161     struct usb_serial_port *port;
0162     unsigned long tx_empty;
0163     spinlock_t msr_lock;
0164     u32 baud_base;
0165     u8 shadow_mcr;
0166     u8 shadow_lcr;
0167     u8 shadow_msr;
0168     u8 shadow_clk;
0169     u8 phy_num;
0170 };
0171 
0172 struct f81534_pin_data {
0173     const u16 reg_addr;
0174     const u8 reg_mask;
0175 };
0176 
0177 struct f81534_port_out_pin {
0178     struct f81534_pin_data pin[3];
0179 };
0180 
0181 /* Pin output value for M2/M1/M0(SD) */
0182 static const struct f81534_port_out_pin f81534_port_out_pins[] = {
0183      { { { 0x2ae8, BIT(7) }, { 0x2a90, BIT(5) }, { 0x2a90, BIT(4) } } },
0184      { { { 0x2ae8, BIT(6) }, { 0x2ae8, BIT(0) }, { 0x2ae8, BIT(3) } } },
0185      { { { 0x2a90, BIT(0) }, { 0x2ae8, BIT(2) }, { 0x2a80, BIT(6) } } },
0186      { { { 0x2a90, BIT(3) }, { 0x2a90, BIT(2) }, { 0x2a90, BIT(1) } } },
0187 };
0188 
0189 static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
0190 static u8 const clock_table[] = { F81534_CLK_1_846_MHZ, F81534_CLK_14_77_MHZ,
0191                 F81534_CLK_18_46_MHZ, F81534_CLK_24_MHZ };
0192 
0193 static int f81534_logic_to_phy_port(struct usb_serial *serial,
0194                     struct usb_serial_port *port)
0195 {
0196     struct f81534_serial_private *serial_priv =
0197             usb_get_serial_data(port->serial);
0198     int count = 0;
0199     int i;
0200 
0201     for (i = 0; i < F81534_NUM_PORT; ++i) {
0202         if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
0203             continue;
0204 
0205         if (port->port_number == count)
0206             return i;
0207 
0208         ++count;
0209     }
0210 
0211     return -ENODEV;
0212 }
0213 
0214 static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
0215 {
0216     struct usb_interface *interface = serial->interface;
0217     struct usb_device *dev = serial->dev;
0218     size_t count = F81534_USB_MAX_RETRY;
0219     int status;
0220     u8 *tmp;
0221 
0222     tmp = kmalloc(sizeof(u8), GFP_KERNEL);
0223     if (!tmp)
0224         return -ENOMEM;
0225 
0226     *tmp = data;
0227 
0228     /*
0229      * Our device maybe not reply when heavily loading, We'll retry for
0230      * F81534_USB_MAX_RETRY times.
0231      */
0232     while (count--) {
0233         status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
0234                      F81534_SET_GET_REGISTER,
0235                      USB_TYPE_VENDOR | USB_DIR_OUT,
0236                      reg, 0, tmp, sizeof(u8),
0237                      F81534_USB_TIMEOUT);
0238         if (status == sizeof(u8)) {
0239             status = 0;
0240             break;
0241         }
0242     }
0243 
0244     if (status < 0) {
0245         dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
0246                 __func__, reg, data, status);
0247     }
0248 
0249     kfree(tmp);
0250     return status;
0251 }
0252 
0253 static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
0254 {
0255     struct usb_interface *interface = serial->interface;
0256     struct usb_device *dev = serial->dev;
0257     size_t count = F81534_USB_MAX_RETRY;
0258     int status;
0259     u8 *tmp;
0260 
0261     tmp = kmalloc(sizeof(u8), GFP_KERNEL);
0262     if (!tmp)
0263         return -ENOMEM;
0264 
0265     /*
0266      * Our device maybe not reply when heavily loading, We'll retry for
0267      * F81534_USB_MAX_RETRY times.
0268      */
0269     while (count--) {
0270         status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0271                      F81534_SET_GET_REGISTER,
0272                      USB_TYPE_VENDOR | USB_DIR_IN,
0273                      reg, 0, tmp, sizeof(u8),
0274                      F81534_USB_TIMEOUT);
0275         if (status > 0) {
0276             status = 0;
0277             break;
0278         } else if (status == 0) {
0279             status = -EIO;
0280         }
0281     }
0282 
0283     if (status < 0) {
0284         dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
0285                 reg, status);
0286         goto end;
0287     }
0288 
0289     *data = *tmp;
0290 
0291 end:
0292     kfree(tmp);
0293     return status;
0294 }
0295 
0296 static int f81534_set_mask_register(struct usb_serial *serial, u16 reg,
0297                     u8 mask, u8 data)
0298 {
0299     int status;
0300     u8 tmp;
0301 
0302     status = f81534_get_register(serial, reg, &tmp);
0303     if (status)
0304         return status;
0305 
0306     tmp &= ~mask;
0307     tmp |= (mask & data);
0308 
0309     return f81534_set_register(serial, reg, tmp);
0310 }
0311 
0312 static int f81534_set_phy_port_register(struct usb_serial *serial, int phy,
0313                     u16 reg, u8 data)
0314 {
0315     return f81534_set_register(serial, reg + F81534_UART_OFFSET * phy,
0316                     data);
0317 }
0318 
0319 static int f81534_get_phy_port_register(struct usb_serial *serial, int phy,
0320                     u16 reg, u8 *data)
0321 {
0322     return f81534_get_register(serial, reg + F81534_UART_OFFSET * phy,
0323                     data);
0324 }
0325 
0326 static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
0327                     u8 data)
0328 {
0329     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0330 
0331     return f81534_set_register(port->serial,
0332             reg + port_priv->phy_num * F81534_UART_OFFSET, data);
0333 }
0334 
0335 static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
0336                     u8 *data)
0337 {
0338     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0339 
0340     return f81534_get_register(port->serial,
0341             reg + port_priv->phy_num * F81534_UART_OFFSET, data);
0342 }
0343 
0344 /*
0345  * If we try to access the internal flash via SPI bus, we should check the bus
0346  * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
0347  */
0348 static int f81534_wait_for_spi_idle(struct usb_serial *serial)
0349 {
0350     size_t count = F81534_MAX_BUS_RETRY;
0351     u8 tmp;
0352     int status;
0353 
0354     do {
0355         status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
0356                         &tmp);
0357         if (status)
0358             return status;
0359 
0360         if (tmp & F81534_BUS_BUSY)
0361             continue;
0362 
0363         if (tmp & F81534_BUS_IDLE)
0364             break;
0365 
0366     } while (--count);
0367 
0368     if (!count) {
0369         dev_err(&serial->interface->dev,
0370                 "%s: timed out waiting for idle SPI bus\n",
0371                 __func__);
0372         return -EIO;
0373     }
0374 
0375     return f81534_set_register(serial, F81534_BUS_REG_STATUS,
0376                 tmp & ~F81534_BUS_IDLE);
0377 }
0378 
0379 static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
0380                     u8 *data)
0381 {
0382     int status;
0383 
0384     status = f81534_get_register(serial, reg, data);
0385     if (status)
0386         return status;
0387 
0388     return f81534_wait_for_spi_idle(serial);
0389 }
0390 
0391 static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
0392 {
0393     int status;
0394 
0395     status = f81534_set_register(serial, reg, data);
0396     if (status)
0397         return status;
0398 
0399     return f81534_wait_for_spi_idle(serial);
0400 }
0401 
0402 static int f81534_read_flash(struct usb_serial *serial, u32 address,
0403                 size_t size, u8 *buf)
0404 {
0405     u8 tmp_buf[F81534_MAX_DATA_BLOCK];
0406     size_t block = 0;
0407     size_t read_size;
0408     size_t count;
0409     int status;
0410     int offset;
0411     u16 reg_tmp;
0412 
0413     status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
0414                     F81534_CMD_READ);
0415     if (status)
0416         return status;
0417 
0418     status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
0419                     (address >> 16) & 0xff);
0420     if (status)
0421         return status;
0422 
0423     status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
0424                     (address >> 8) & 0xff);
0425     if (status)
0426         return status;
0427 
0428     status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
0429                     (address >> 0) & 0xff);
0430     if (status)
0431         return status;
0432 
0433     /* Continuous read mode */
0434     do {
0435         read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
0436 
0437         for (count = 0; count < read_size; ++count) {
0438             /* To write F81534_BUS_REG_END when final byte */
0439             if (size <= F81534_MAX_DATA_BLOCK &&
0440                     read_size == count + 1)
0441                 reg_tmp = F81534_BUS_REG_END;
0442             else
0443                 reg_tmp = F81534_BUS_REG_START;
0444 
0445             /*
0446              * Dummy code, force IC to generate a read pulse, the
0447              * set of value 0xf1 is dont care (any value is ok)
0448              */
0449             status = f81534_set_spi_register(serial, reg_tmp,
0450                     0xf1);
0451             if (status)
0452                 return status;
0453 
0454             status = f81534_get_spi_register(serial,
0455                         F81534_BUS_READ_DATA,
0456                         &tmp_buf[count]);
0457             if (status)
0458                 return status;
0459 
0460             offset = count + block * F81534_MAX_DATA_BLOCK;
0461             buf[offset] = tmp_buf[count];
0462         }
0463 
0464         size -= read_size;
0465         ++block;
0466     } while (size);
0467 
0468     return 0;
0469 }
0470 
0471 static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
0472 {
0473     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0474     int phy_num = port_priv->phy_num;
0475     u8 tx_len;
0476     int i;
0477 
0478     /*
0479      * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
0480      * index 0: port phy idx (e.g., 0,1,2,3)
0481      * index 1: only F81534_TOKEN_WRITE
0482      * index 2: serial TX out length
0483      * index 3: fix to 0
0484      * index 4~127: serial out data block
0485      */
0486     for (i = 0; i < F81534_NUM_PORT; ++i) {
0487         buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
0488         buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
0489         buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
0490         buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
0491     }
0492 
0493     tx_len = kfifo_out_locked(&port->write_fifo,
0494                 &buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
0495                 F81534_MAX_TX_SIZE, &port->lock);
0496 
0497     buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
0498 }
0499 
0500 static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
0501 {
0502     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0503     struct urb *urb;
0504     unsigned long flags;
0505     int result;
0506 
0507     /* Check is any data in write_fifo */
0508     spin_lock_irqsave(&port->lock, flags);
0509 
0510     if (kfifo_is_empty(&port->write_fifo)) {
0511         spin_unlock_irqrestore(&port->lock, flags);
0512         return 0;
0513     }
0514 
0515     spin_unlock_irqrestore(&port->lock, flags);
0516 
0517     /* Check H/W is TXEMPTY */
0518     if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
0519         return 0;
0520 
0521     urb = port->write_urbs[0];
0522     f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
0523     urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
0524 
0525     result = usb_submit_urb(urb, mem_flags);
0526     if (result) {
0527         set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
0528         dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
0529                 result);
0530         return result;
0531     }
0532 
0533     usb_serial_port_softint(port);
0534     return 0;
0535 }
0536 
0537 static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
0538 {
0539     if (!baudrate)
0540         return 0;
0541 
0542     /* Round to nearest divisor */
0543     return DIV_ROUND_CLOSEST(clockrate, baudrate);
0544 }
0545 
0546 static int f81534_find_clk(u32 baudrate)
0547 {
0548     int idx;
0549 
0550     for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
0551         if (baudrate <= baudrate_table[idx] &&
0552                 baudrate_table[idx] % baudrate == 0)
0553             return idx;
0554     }
0555 
0556     return -EINVAL;
0557 }
0558 
0559 static int f81534_set_port_config(struct usb_serial_port *port,
0560         struct tty_struct *tty, u32 baudrate, u32 old_baudrate, u8 lcr)
0561 {
0562     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0563     u32 divisor;
0564     int status;
0565     int i;
0566     int idx;
0567     u8 value;
0568     u32 baud_list[] = {baudrate, old_baudrate, F81534_DEFAULT_BAUD_RATE};
0569 
0570     for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
0571         idx = f81534_find_clk(baud_list[i]);
0572         if (idx >= 0) {
0573             baudrate = baud_list[i];
0574             tty_encode_baud_rate(tty, baudrate, baudrate);
0575             break;
0576         }
0577     }
0578 
0579     if (idx < 0)
0580         return -EINVAL;
0581 
0582     port_priv->baud_base = baudrate_table[idx];
0583     port_priv->shadow_clk &= ~F81534_CLK_MASK;
0584     port_priv->shadow_clk |= clock_table[idx];
0585 
0586     status = f81534_set_port_register(port, F81534_CLOCK_REG,
0587             port_priv->shadow_clk);
0588     if (status) {
0589         dev_err(&port->dev, "CLOCK_REG setting failed\n");
0590         return status;
0591     }
0592 
0593     if (baudrate <= 1200)
0594         value = F81534_1X_RXTRIGGER;    /* 128 FIFO & TL: 1x */
0595     else
0596         value = F81534_8X_RXTRIGGER;    /* 128 FIFO & TL: 8x */
0597 
0598     status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
0599     if (status) {
0600         dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
0601         return status;
0602     }
0603 
0604     if (baudrate <= 1200)
0605         value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
0606     else
0607         value = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO; /* TL: 8 */
0608 
0609     status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
0610                         value);
0611     if (status) {
0612         dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
0613         return status;
0614     }
0615 
0616     divisor = f81534_calc_baud_divisor(baudrate, port_priv->baud_base);
0617 
0618     mutex_lock(&port_priv->lcr_mutex);
0619 
0620     value = UART_LCR_DLAB;
0621     status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
0622                         value);
0623     if (status) {
0624         dev_err(&port->dev, "%s: set LCR failed\n", __func__);
0625         goto out_unlock;
0626     }
0627 
0628     value = divisor & 0xff;
0629     status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
0630     if (status) {
0631         dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
0632         goto out_unlock;
0633     }
0634 
0635     value = (divisor >> 8) & 0xff;
0636     status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
0637     if (status) {
0638         dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
0639         goto out_unlock;
0640     }
0641 
0642     value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
0643     status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
0644                         value);
0645     if (status) {
0646         dev_err(&port->dev, "%s: set LCR failed\n", __func__);
0647         goto out_unlock;
0648     }
0649 
0650     port_priv->shadow_lcr = value;
0651 out_unlock:
0652     mutex_unlock(&port_priv->lcr_mutex);
0653 
0654     return status;
0655 }
0656 
0657 static void f81534_break_ctl(struct tty_struct *tty, int break_state)
0658 {
0659     struct usb_serial_port *port = tty->driver_data;
0660     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0661     int status;
0662 
0663     mutex_lock(&port_priv->lcr_mutex);
0664 
0665     if (break_state)
0666         port_priv->shadow_lcr |= UART_LCR_SBC;
0667     else
0668         port_priv->shadow_lcr &= ~UART_LCR_SBC;
0669 
0670     status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
0671                     port_priv->shadow_lcr);
0672     if (status)
0673         dev_err(&port->dev, "set break failed: %d\n", status);
0674 
0675     mutex_unlock(&port_priv->lcr_mutex);
0676 }
0677 
0678 static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
0679                 unsigned int clear)
0680 {
0681     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
0682     int status;
0683     u8 tmp;
0684 
0685     if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
0686         return 0;   /* no change */
0687 
0688     mutex_lock(&port_priv->mcr_mutex);
0689 
0690     /* 'Set' takes precedence over 'Clear' */
0691     clear &= ~set;
0692 
0693     /* Always enable UART_MCR_OUT2 */
0694     tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
0695 
0696     if (clear & TIOCM_DTR)
0697         tmp &= ~UART_MCR_DTR;
0698 
0699     if (clear & TIOCM_RTS)
0700         tmp &= ~UART_MCR_RTS;
0701 
0702     if (set & TIOCM_DTR)
0703         tmp |= UART_MCR_DTR;
0704 
0705     if (set & TIOCM_RTS)
0706         tmp |= UART_MCR_RTS;
0707 
0708     status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
0709     if (status < 0) {
0710         dev_err(&port->dev, "%s: MCR write failed\n", __func__);
0711         mutex_unlock(&port_priv->mcr_mutex);
0712         return status;
0713     }
0714 
0715     port_priv->shadow_mcr = tmp;
0716     mutex_unlock(&port_priv->mcr_mutex);
0717     return 0;
0718 }
0719 
0720 /*
0721  * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
0722  * for latest configuration index. If nothing found
0723  * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
0724  * F81534_DEF_CONF_ADDRESS_START section.
0725  *
0726  * Due to we only use block0 to save data, so *index should be 0 or
0727  * F81534_CUSTOM_NO_CUSTOM_DATA.
0728  */
0729 static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
0730 {
0731     u8 tmp;
0732     int status;
0733 
0734     status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
0735                     &tmp);
0736     if (status) {
0737         dev_err(&serial->interface->dev, "%s: read failed: %d\n",
0738                 __func__, status);
0739         return status;
0740     }
0741 
0742     /* We'll use the custom data when the data is valid. */
0743     if (tmp == F81534_CUSTOM_VALID_TOKEN)
0744         *index = 0;
0745     else
0746         *index = F81534_CUSTOM_NO_CUSTOM_DATA;
0747 
0748     return 0;
0749 }
0750 
0751 /*
0752  * The F81532/534 will not report serial port to USB serial subsystem when
0753  * H/W DCD/DSR/CTS/RI/RX pin connected to ground.
0754  *
0755  * To detect RX pin status, we'll enable MCR interal loopback, disable it and
0756  * delayed for 60ms. It connected to ground If LSR register report UART_LSR_BI.
0757  */
0758 static bool f81534_check_port_hw_disabled(struct usb_serial *serial, int phy)
0759 {
0760     int status;
0761     u8 old_mcr;
0762     u8 msr;
0763     u8 lsr;
0764     u8 msr_mask;
0765 
0766     msr_mask = UART_MSR_DCD | UART_MSR_RI | UART_MSR_DSR | UART_MSR_CTS;
0767 
0768     status = f81534_get_phy_port_register(serial, phy,
0769                 F81534_MODEM_STATUS_REG, &msr);
0770     if (status)
0771         return false;
0772 
0773     if ((msr & msr_mask) != msr_mask)
0774         return false;
0775 
0776     status = f81534_set_phy_port_register(serial, phy,
0777                 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
0778                 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
0779     if (status)
0780         return false;
0781 
0782     status = f81534_get_phy_port_register(serial, phy,
0783                 F81534_MODEM_CONTROL_REG, &old_mcr);
0784     if (status)
0785         return false;
0786 
0787     status = f81534_set_phy_port_register(serial, phy,
0788                 F81534_MODEM_CONTROL_REG, UART_MCR_LOOP);
0789     if (status)
0790         return false;
0791 
0792     status = f81534_set_phy_port_register(serial, phy,
0793                 F81534_MODEM_CONTROL_REG, 0x0);
0794     if (status)
0795         return false;
0796 
0797     msleep(60);
0798 
0799     status = f81534_get_phy_port_register(serial, phy,
0800                 F81534_LINE_STATUS_REG, &lsr);
0801     if (status)
0802         return false;
0803 
0804     status = f81534_set_phy_port_register(serial, phy,
0805                 F81534_MODEM_CONTROL_REG, old_mcr);
0806     if (status)
0807         return false;
0808 
0809     if ((lsr & UART_LSR_BI) == UART_LSR_BI)
0810         return true;
0811 
0812     return false;
0813 }
0814 
0815 /*
0816  * We had 2 generation of F81532/534 IC. All has an internal storage.
0817  *
0818  * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
0819  * internal data will used. All mode and gpio control should manually set
0820  * by AP or Driver and all storage space value are 0xff. The
0821  * f81534_calc_num_ports() will run to final we marked as "oldest version"
0822  * for this IC.
0823  *
0824  * 2rd is designed to more generic to use any transceiver and this is our
0825  * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
0826  * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
0827  * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
0828  * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
0829  * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
0830  * The f81534_calc_num_ports() will run to "new style" with checking
0831  * F81534_PORT_UNAVAILABLE section.
0832  */
0833 static int f81534_calc_num_ports(struct usb_serial *serial,
0834                     struct usb_serial_endpoints *epds)
0835 {
0836     struct f81534_serial_private *serial_priv;
0837     struct device *dev = &serial->interface->dev;
0838     int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
0839     int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
0840     u8 num_port = 0;
0841     int index = 0;
0842     int status;
0843     int i;
0844 
0845     if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
0846             size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
0847         dev_err(dev, "unsupported endpoint max packet size\n");
0848         return -ENODEV;
0849     }
0850 
0851     serial_priv = devm_kzalloc(&serial->interface->dev,
0852                     sizeof(*serial_priv), GFP_KERNEL);
0853     if (!serial_priv)
0854         return -ENOMEM;
0855 
0856     usb_set_serial_data(serial, serial_priv);
0857     mutex_init(&serial_priv->urb_mutex);
0858 
0859     /* Check had custom setting */
0860     status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
0861     if (status) {
0862         dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
0863                 __func__, status);
0864         return status;
0865     }
0866 
0867     /*
0868      * We'll read custom data only when data available, otherwise we'll
0869      * read default value instead.
0870      */
0871     if (serial_priv->setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
0872         status = f81534_read_flash(serial,
0873                         F81534_CUSTOM_ADDRESS_START +
0874                         F81534_CONF_OFFSET,
0875                         sizeof(serial_priv->conf_data),
0876                         serial_priv->conf_data);
0877         if (status) {
0878             dev_err(&serial->interface->dev,
0879                     "%s: get custom data failed: %d\n",
0880                     __func__, status);
0881             return status;
0882         }
0883 
0884         dev_dbg(&serial->interface->dev,
0885                 "%s: read config from block: %d\n", __func__,
0886                 serial_priv->setting_idx);
0887     } else {
0888         /* Read default board setting */
0889         status = f81534_read_flash(serial,
0890                 F81534_DEF_CONF_ADDRESS_START,
0891                 sizeof(serial_priv->conf_data),
0892                 serial_priv->conf_data);
0893         if (status) {
0894             dev_err(&serial->interface->dev,
0895                     "%s: read failed: %d\n", __func__,
0896                     status);
0897             return status;
0898         }
0899 
0900         dev_dbg(&serial->interface->dev, "%s: read default config\n",
0901                 __func__);
0902     }
0903 
0904     /* New style, find all possible ports */
0905     for (i = 0; i < F81534_NUM_PORT; ++i) {
0906         if (f81534_check_port_hw_disabled(serial, i))
0907             serial_priv->conf_data[i] |= F81534_PORT_UNAVAILABLE;
0908 
0909         if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
0910             continue;
0911 
0912         ++num_port;
0913     }
0914 
0915     if (!num_port) {
0916         dev_warn(&serial->interface->dev,
0917             "no config found, assuming 4 ports\n");
0918         num_port = 4;       /* Nothing found, oldest version IC */
0919     }
0920 
0921     /* Assign phy-to-logic mapping */
0922     for (i = 0; i < F81534_NUM_PORT; ++i) {
0923         if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
0924             continue;
0925 
0926         serial_priv->tty_idx[i] = index++;
0927         dev_dbg(&serial->interface->dev,
0928                 "%s: phy_num: %d, tty_idx: %d\n", __func__, i,
0929                 serial_priv->tty_idx[i]);
0930     }
0931 
0932     /*
0933      * Setup bulk-out endpoint multiplexing. All ports share the same
0934      * bulk-out endpoint.
0935      */
0936     BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
0937 
0938     for (i = 1; i < num_port; ++i)
0939         epds->bulk_out[i] = epds->bulk_out[0];
0940 
0941     epds->num_bulk_out = num_port;
0942 
0943     return num_port;
0944 }
0945 
0946 static void f81534_set_termios(struct tty_struct *tty,
0947                 struct usb_serial_port *port,
0948                 struct ktermios *old_termios)
0949 {
0950     u8 new_lcr = 0;
0951     int status;
0952     u32 baud;
0953     u32 old_baud;
0954 
0955     if (C_BAUD(tty) == B0)
0956         f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
0957     else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
0958         f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
0959 
0960     if (C_PARENB(tty)) {
0961         new_lcr |= UART_LCR_PARITY;
0962 
0963         if (!C_PARODD(tty))
0964             new_lcr |= UART_LCR_EPAR;
0965 
0966         if (C_CMSPAR(tty))
0967             new_lcr |= UART_LCR_SPAR;
0968     }
0969 
0970     if (C_CSTOPB(tty))
0971         new_lcr |= UART_LCR_STOP;
0972 
0973     new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
0974 
0975     baud = tty_get_baud_rate(tty);
0976     if (!baud)
0977         return;
0978 
0979     if (old_termios)
0980         old_baud = tty_termios_baud_rate(old_termios);
0981     else
0982         old_baud = F81534_DEFAULT_BAUD_RATE;
0983 
0984     dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
0985 
0986     status = f81534_set_port_config(port, tty, baud, old_baud, new_lcr);
0987     if (status < 0) {
0988         dev_err(&port->dev, "%s: set port config failed: %d\n",
0989                 __func__, status);
0990     }
0991 }
0992 
0993 static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
0994 {
0995     return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
0996 }
0997 
0998 static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
0999 {
1000     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1001     struct tty_struct *tty;
1002     unsigned long flags;
1003     u8 old_msr;
1004 
1005     if (!(msr & UART_MSR_ANY_DELTA))
1006         return;
1007 
1008     spin_lock_irqsave(&port_priv->msr_lock, flags);
1009     old_msr = port_priv->shadow_msr;
1010     port_priv->shadow_msr = msr;
1011     spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1012 
1013     dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
1014             msr);
1015 
1016     /* Update input line counters */
1017     if (msr & UART_MSR_DCTS)
1018         port->icount.cts++;
1019     if (msr & UART_MSR_DDSR)
1020         port->icount.dsr++;
1021     if (msr & UART_MSR_DDCD)
1022         port->icount.dcd++;
1023     if (msr & UART_MSR_TERI)
1024         port->icount.rng++;
1025 
1026     wake_up_interruptible(&port->port.delta_msr_wait);
1027 
1028     if (!(msr & UART_MSR_DDCD))
1029         return;
1030 
1031     dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
1032             __func__, port_priv->phy_num, old_msr, msr);
1033 
1034     tty = tty_port_tty_get(&port->port);
1035     if (!tty)
1036         return;
1037 
1038     usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
1039     tty_kref_put(tty);
1040 }
1041 
1042 static int f81534_read_msr(struct usb_serial_port *port)
1043 {
1044     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1045     unsigned long flags;
1046     int status;
1047     u8 msr;
1048 
1049     /* Get MSR initial value */
1050     status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1051     if (status)
1052         return status;
1053 
1054     /* Force update current state */
1055     spin_lock_irqsave(&port_priv->msr_lock, flags);
1056     port_priv->shadow_msr = msr;
1057     spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1058 
1059     return 0;
1060 }
1061 
1062 static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
1063 {
1064     struct f81534_serial_private *serial_priv =
1065             usb_get_serial_data(port->serial);
1066     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1067     int status;
1068 
1069     status = f81534_set_port_register(port,
1070                 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
1071                 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
1072     if (status) {
1073         dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
1074                 status);
1075         return status;
1076     }
1077 
1078     if (tty)
1079         f81534_set_termios(tty, port, NULL);
1080 
1081     status = f81534_read_msr(port);
1082     if (status)
1083         return status;
1084 
1085     mutex_lock(&serial_priv->urb_mutex);
1086 
1087     /* Submit Read URBs for first port opened */
1088     if (!serial_priv->opened_port) {
1089         status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
1090         if (status)
1091             goto exit;
1092     }
1093 
1094     serial_priv->opened_port++;
1095 
1096 exit:
1097     mutex_unlock(&serial_priv->urb_mutex);
1098 
1099     set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1100     return status;
1101 }
1102 
1103 static void f81534_close(struct usb_serial_port *port)
1104 {
1105     struct f81534_serial_private *serial_priv =
1106             usb_get_serial_data(port->serial);
1107     struct usb_serial_port *port0 = port->serial->port[0];
1108     unsigned long flags;
1109     size_t i;
1110 
1111     usb_kill_urb(port->write_urbs[0]);
1112 
1113     spin_lock_irqsave(&port->lock, flags);
1114     kfifo_reset_out(&port->write_fifo);
1115     spin_unlock_irqrestore(&port->lock, flags);
1116 
1117     /* Kill Read URBs when final port closed */
1118     mutex_lock(&serial_priv->urb_mutex);
1119     serial_priv->opened_port--;
1120 
1121     if (!serial_priv->opened_port) {
1122         for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
1123             usb_kill_urb(port0->read_urbs[i]);
1124     }
1125 
1126     mutex_unlock(&serial_priv->urb_mutex);
1127 }
1128 
1129 static void f81534_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
1130 {
1131     struct usb_serial_port *port = tty->driver_data;
1132     struct f81534_port_private *port_priv;
1133 
1134     port_priv = usb_get_serial_port_data(port);
1135 
1136     ss->baud_base = port_priv->baud_base;
1137 }
1138 
1139 static void f81534_process_per_serial_block(struct usb_serial_port *port,
1140         u8 *data)
1141 {
1142     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1143     int phy_num = data[0];
1144     size_t read_size = 0;
1145     size_t i;
1146     char tty_flag;
1147     int status;
1148     u8 lsr;
1149 
1150     /*
1151      * The block layout is 128 Bytes
1152      * index 0: port phy idx (e.g., 0,1,2,3),
1153      * index 1: It's could be
1154      *          F81534_TOKEN_RECEIVE
1155      *          F81534_TOKEN_TX_EMPTY
1156      *          F81534_TOKEN_MSR_CHANGE
1157      * index 2: serial in size (data+lsr, must be even)
1158      *          meaningful for F81534_TOKEN_RECEIVE only
1159      * index 3: current MSR with this device
1160      * index 4~127: serial in data block (data+lsr, must be even)
1161      */
1162     switch (data[1]) {
1163     case F81534_TOKEN_TX_EMPTY:
1164         set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1165 
1166         /* Try to submit writer */
1167         status = f81534_submit_writer(port, GFP_ATOMIC);
1168         if (status)
1169             dev_err(&port->dev, "%s: submit failed\n", __func__);
1170         return;
1171 
1172     case F81534_TOKEN_MSR_CHANGE:
1173         f81534_msr_changed(port, data[3]);
1174         return;
1175 
1176     case F81534_TOKEN_RECEIVE:
1177         read_size = data[2];
1178         if (read_size > F81534_MAX_RX_SIZE) {
1179             dev_err(&port->dev,
1180                 "%s: phy: %d read_size: %zu larger than: %d\n",
1181                 __func__, phy_num, read_size,
1182                 F81534_MAX_RX_SIZE);
1183             return;
1184         }
1185 
1186         break;
1187 
1188     default:
1189         dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
1190                 data[1]);
1191         return;
1192     }
1193 
1194     for (i = 4; i < 4 + read_size; i += 2) {
1195         tty_flag = TTY_NORMAL;
1196         lsr = data[i + 1];
1197 
1198         if (lsr & UART_LSR_BRK_ERROR_BITS) {
1199             if (lsr & UART_LSR_BI) {
1200                 tty_flag = TTY_BREAK;
1201                 port->icount.brk++;
1202                 usb_serial_handle_break(port);
1203             } else if (lsr & UART_LSR_PE) {
1204                 tty_flag = TTY_PARITY;
1205                 port->icount.parity++;
1206             } else if (lsr & UART_LSR_FE) {
1207                 tty_flag = TTY_FRAME;
1208                 port->icount.frame++;
1209             }
1210 
1211             if (lsr & UART_LSR_OE) {
1212                 port->icount.overrun++;
1213                 tty_insert_flip_char(&port->port, 0,
1214                         TTY_OVERRUN);
1215             }
1216 
1217             schedule_work(&port_priv->lsr_work);
1218         }
1219 
1220         if (port->sysrq) {
1221             if (usb_serial_handle_sysrq_char(port, data[i]))
1222                 continue;
1223         }
1224 
1225         tty_insert_flip_char(&port->port, data[i], tty_flag);
1226     }
1227 
1228     tty_flip_buffer_push(&port->port);
1229 }
1230 
1231 static void f81534_process_read_urb(struct urb *urb)
1232 {
1233     struct f81534_serial_private *serial_priv;
1234     struct usb_serial_port *port;
1235     struct usb_serial *serial;
1236     u8 *buf;
1237     int phy_port_num;
1238     int tty_port_num;
1239     size_t i;
1240 
1241     if (!urb->actual_length ||
1242             urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1243         return;
1244     }
1245 
1246     port = urb->context;
1247     serial = port->serial;
1248     buf = urb->transfer_buffer;
1249     serial_priv = usb_get_serial_data(serial);
1250 
1251     for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
1252         phy_port_num = buf[i];
1253         if (phy_port_num >= F81534_NUM_PORT) {
1254             dev_err(&port->dev,
1255                 "%s: phy_port_num: %d larger than: %d\n",
1256                 __func__, phy_port_num, F81534_NUM_PORT);
1257             continue;
1258         }
1259 
1260         tty_port_num = serial_priv->tty_idx[phy_port_num];
1261         port = serial->port[tty_port_num];
1262 
1263         if (tty_port_initialized(&port->port))
1264             f81534_process_per_serial_block(port, &buf[i]);
1265     }
1266 }
1267 
1268 static void f81534_write_usb_callback(struct urb *urb)
1269 {
1270     struct usb_serial_port *port = urb->context;
1271 
1272     switch (urb->status) {
1273     case 0:
1274         break;
1275     case -ENOENT:
1276     case -ECONNRESET:
1277     case -ESHUTDOWN:
1278         dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1279                 __func__, urb->status);
1280         return;
1281     case -EPIPE:
1282         dev_err(&port->dev, "%s - urb stopped: %d\n",
1283                 __func__, urb->status);
1284         return;
1285     default:
1286         dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1287                 __func__, urb->status);
1288         break;
1289     }
1290 }
1291 
1292 static void f81534_lsr_worker(struct work_struct *work)
1293 {
1294     struct f81534_port_private *port_priv;
1295     struct usb_serial_port *port;
1296     int status;
1297     u8 tmp;
1298 
1299     port_priv = container_of(work, struct f81534_port_private, lsr_work);
1300     port = port_priv->port;
1301 
1302     status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
1303     if (status)
1304         dev_warn(&port->dev, "read LSR failed: %d\n", status);
1305 }
1306 
1307 static int f81534_set_port_output_pin(struct usb_serial_port *port)
1308 {
1309     struct f81534_serial_private *serial_priv;
1310     struct f81534_port_private *port_priv;
1311     struct usb_serial *serial;
1312     const struct f81534_port_out_pin *pins;
1313     int status;
1314     int i;
1315     u8 value;
1316     u8 idx;
1317 
1318     serial = port->serial;
1319     serial_priv = usb_get_serial_data(serial);
1320     port_priv = usb_get_serial_port_data(port);
1321 
1322     idx = F81534_CONF_INIT_GPIO_OFFSET + port_priv->phy_num;
1323     value = serial_priv->conf_data[idx];
1324     if (value >= F81534_CONF_GPIO_SHUTDOWN) {
1325         /*
1326          * Newer IC configure will make transceiver in shutdown mode on
1327          * initial power on. We need enable it before using UARTs.
1328          */
1329         idx = F81534_CONF_WORK_GPIO_OFFSET + port_priv->phy_num;
1330         value = serial_priv->conf_data[idx];
1331         if (value >= F81534_CONF_GPIO_SHUTDOWN)
1332             value = F81534_CONF_GPIO_RS232;
1333     }
1334 
1335     pins = &f81534_port_out_pins[port_priv->phy_num];
1336 
1337     for (i = 0; i < ARRAY_SIZE(pins->pin); ++i) {
1338         status = f81534_set_mask_register(serial,
1339                 pins->pin[i].reg_addr, pins->pin[i].reg_mask,
1340                 value & BIT(i) ? pins->pin[i].reg_mask : 0);
1341         if (status)
1342             return status;
1343     }
1344 
1345     dev_dbg(&port->dev, "Output pin (M0/M1/M2): %d\n", value);
1346     return 0;
1347 }
1348 
1349 static int f81534_port_probe(struct usb_serial_port *port)
1350 {
1351     struct f81534_serial_private *serial_priv;
1352     struct f81534_port_private *port_priv;
1353     int ret;
1354     u8 value;
1355 
1356     serial_priv = usb_get_serial_data(port->serial);
1357     port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
1358     if (!port_priv)
1359         return -ENOMEM;
1360 
1361     /*
1362      * We'll make tx frame error when baud rate from 384~500kps. So we'll
1363      * delay all tx data frame with 1bit.
1364      */
1365     port_priv->shadow_clk = F81534_UART_EN | F81534_CLK_TX_DELAY_1BIT;
1366     spin_lock_init(&port_priv->msr_lock);
1367     mutex_init(&port_priv->mcr_mutex);
1368     mutex_init(&port_priv->lcr_mutex);
1369     INIT_WORK(&port_priv->lsr_work, f81534_lsr_worker);
1370 
1371     /* Assign logic-to-phy mapping */
1372     ret = f81534_logic_to_phy_port(port->serial, port);
1373     if (ret < 0)
1374         return ret;
1375 
1376     port_priv->phy_num = ret;
1377     port_priv->port = port;
1378     usb_set_serial_port_data(port, port_priv);
1379     dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
1380             port->port_number, port_priv->phy_num);
1381 
1382     /*
1383      * The F81532/534 will hang-up when enable LSR interrupt in IER and
1384      * occur data overrun. So we'll disable the LSR interrupt in probe()
1385      * and submit the LSR worker to clear LSR state when reported LSR error
1386      * bit with bulk-in data in f81534_process_per_serial_block().
1387      */
1388     ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
1389             UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
1390     if (ret)
1391         return ret;
1392 
1393     value = serial_priv->conf_data[port_priv->phy_num];
1394     switch (value & F81534_PORT_CONF_MODE_MASK) {
1395     case F81534_PORT_CONF_RS485_INVERT:
1396         port_priv->shadow_clk |= F81534_CLK_RS485_MODE |
1397                     F81534_CLK_RS485_INVERT;
1398         dev_dbg(&port->dev, "RS485 invert mode\n");
1399         break;
1400     case F81534_PORT_CONF_RS485:
1401         port_priv->shadow_clk |= F81534_CLK_RS485_MODE;
1402         dev_dbg(&port->dev, "RS485 mode\n");
1403         break;
1404 
1405     default:
1406     case F81534_PORT_CONF_RS232:
1407         dev_dbg(&port->dev, "RS232 mode\n");
1408         break;
1409     }
1410 
1411     return f81534_set_port_output_pin(port);
1412 }
1413 
1414 static void f81534_port_remove(struct usb_serial_port *port)
1415 {
1416     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1417 
1418     flush_work(&port_priv->lsr_work);
1419 }
1420 
1421 static int f81534_tiocmget(struct tty_struct *tty)
1422 {
1423     struct usb_serial_port *port = tty->driver_data;
1424     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1425     int status;
1426     int r;
1427     u8 msr;
1428     u8 mcr;
1429 
1430     /* Read current MSR from device */
1431     status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1432     if (status)
1433         return status;
1434 
1435     mutex_lock(&port_priv->mcr_mutex);
1436     mcr = port_priv->shadow_mcr;
1437     mutex_unlock(&port_priv->mcr_mutex);
1438 
1439     r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
1440         (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
1441         (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
1442         (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
1443         (msr & UART_MSR_RI ? TIOCM_RI : 0) |
1444         (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
1445 
1446     return r;
1447 }
1448 
1449 static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1450                 unsigned int clear)
1451 {
1452     struct usb_serial_port *port = tty->driver_data;
1453 
1454     return f81534_update_mctrl(port, set, clear);
1455 }
1456 
1457 static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1458 {
1459     if (on)
1460         f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1461     else
1462         f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1463 }
1464 
1465 static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1466             const u8 *buf, int count)
1467 {
1468     int bytes_out, status;
1469 
1470     if (!count)
1471         return 0;
1472 
1473     bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1474                     &port->lock);
1475 
1476     status = f81534_submit_writer(port, GFP_ATOMIC);
1477     if (status) {
1478         dev_err(&port->dev, "%s: submit failed\n", __func__);
1479         return status;
1480     }
1481 
1482     return bytes_out;
1483 }
1484 
1485 static bool f81534_tx_empty(struct usb_serial_port *port)
1486 {
1487     struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1488 
1489     return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1490 }
1491 
1492 static int f81534_resume(struct usb_serial *serial)
1493 {
1494     struct f81534_serial_private *serial_priv =
1495             usb_get_serial_data(serial);
1496     struct usb_serial_port *port;
1497     int error = 0;
1498     int status;
1499     size_t i;
1500 
1501     /*
1502      * We'll register port 0 bulkin when port had opened, It'll take all
1503      * port received data, MSR register change and TX_EMPTY information.
1504      */
1505     mutex_lock(&serial_priv->urb_mutex);
1506 
1507     if (serial_priv->opened_port) {
1508         status = f81534_submit_read_urb(serial, GFP_NOIO);
1509         if (status) {
1510             mutex_unlock(&serial_priv->urb_mutex);
1511             return status;
1512         }
1513     }
1514 
1515     mutex_unlock(&serial_priv->urb_mutex);
1516 
1517     for (i = 0; i < serial->num_ports; i++) {
1518         port = serial->port[i];
1519         if (!tty_port_initialized(&port->port))
1520             continue;
1521 
1522         status = f81534_submit_writer(port, GFP_NOIO);
1523         if (status) {
1524             dev_err(&port->dev, "%s: submit failed\n", __func__);
1525             ++error;
1526         }
1527     }
1528 
1529     if (error)
1530         return -EIO;
1531 
1532     return 0;
1533 }
1534 
1535 static struct usb_serial_driver f81534_device = {
1536     .driver = {
1537            .owner = THIS_MODULE,
1538            .name = "f81534",
1539     },
1540     .description =      DRIVER_DESC,
1541     .id_table =     f81534_id_table,
1542     .num_bulk_in =      1,
1543     .num_bulk_out =     1,
1544     .open =         f81534_open,
1545     .close =        f81534_close,
1546     .write =        f81534_write,
1547     .tx_empty =     f81534_tx_empty,
1548     .calc_num_ports =   f81534_calc_num_ports,
1549     .port_probe =       f81534_port_probe,
1550     .port_remove =      f81534_port_remove,
1551     .break_ctl =        f81534_break_ctl,
1552     .dtr_rts =      f81534_dtr_rts,
1553     .process_read_urb = f81534_process_read_urb,
1554     .get_serial =       f81534_get_serial_info,
1555     .tiocmget =     f81534_tiocmget,
1556     .tiocmset =     f81534_tiocmset,
1557     .write_bulk_callback =  f81534_write_usb_callback,
1558     .set_termios =      f81534_set_termios,
1559     .resume =       f81534_resume,
1560 };
1561 
1562 static struct usb_serial_driver *const serial_drivers[] = {
1563     &f81534_device, NULL
1564 };
1565 
1566 module_usb_serial_driver(serial_drivers, f81534_id_table);
1567 
1568 MODULE_DEVICE_TABLE(usb, f81534_id_table);
1569 MODULE_DESCRIPTION(DRIVER_DESC);
1570 MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
1571 MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
1572 MODULE_LICENSE("GPL");