/*
* F81532/F81534 USB to Serial Ports Bridge
*
* F81532 => 2 Serial Ports
* F81534 => 4 Serial Ports
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
* Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
* Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
*
* The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
* for all serial port TX and 1 endpoint bulk-in for all serial port read in
* (Read Data/MSR/LSR).
*
* Write URB is fixed with 512bytes, per serial port used 128Bytes.
* It can be described by f81534_prepare_write_buffer()
*
* Read URB is 512Bytes max, per serial port used 128Bytes.
* It can be described by f81534_process_read_urb() and maybe received with
* 128x1,2,3,4 bytes.
*
*/
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#include <linux/serial_reg.h>
#include <linux/module.h>
#include <linux/uaccess.h>
/* Serial Port register Address */
#define F81534_UART_BASE_ADDRESS 0x1200
#define F81534_UART_OFFSET 0x10
#define F81534_DIVISOR_LSB_REG (0x00 + F81534_UART_BASE_ADDRESS)
#define F81534_DIVISOR_MSB_REG (0x01 + F81534_UART_BASE_ADDRESS)
#define F81534_FIFO_CONTROL_REG (0x02 + F81534_UART_BASE_ADDRESS)
#define F81534_LINE_CONTROL_REG (0x03 + F81534_UART_BASE_ADDRESS)
#define F81534_MODEM_CONTROL_REG (0x04 + F81534_UART_BASE_ADDRESS)
#define F81534_MODEM_STATUS_REG (0x06 + F81534_UART_BASE_ADDRESS)
#define F81534_CONFIG1_REG (0x09 + F81534_UART_BASE_ADDRESS)
#define F81534_DEF_CONF_ADDRESS_START 0x3000
#define F81534_DEF_CONF_SIZE 8
#define F81534_CUSTOM_ADDRESS_START 0x2f00
#define F81534_CUSTOM_DATA_SIZE 0x10
#define F81534_CUSTOM_NO_CUSTOM_DATA 0xff
#define F81534_CUSTOM_VALID_TOKEN 0xf0
#define F81534_CONF_OFFSET 1
#define F81534_MAX_DATA_BLOCK 64
#define F81534_MAX_BUS_RETRY 20
/* Default URB timeout for USB operations */
#define F81534_USB_MAX_RETRY 10
#define F81534_USB_TIMEOUT 1000
#define F81534_SET_GET_REGISTER 0xA0
#define F81534_NUM_PORT 4
#define F81534_UNUSED_PORT 0xff
#define F81534_WRITE_BUFFER_SIZE 512
#define DRIVER_DESC "Fintek F81532/F81534"
#define FINTEK_VENDOR_ID_1 0x1934
#define FINTEK_VENDOR_ID_2 0x2C42
#define FINTEK_DEVICE_ID 0x1202
#define F81534_MAX_TX_SIZE 124
#define F81534_MAX_RX_SIZE 124
#define F81534_RECEIVE_BLOCK_SIZE 128
#define F81534_MAX_RECEIVE_BLOCK_SIZE 512
#define F81534_TOKEN_RECEIVE 0x01
#define F81534_TOKEN_WRITE 0x02
#define F81534_TOKEN_TX_EMPTY 0x03
#define F81534_TOKEN_MSR_CHANGE 0x04
/*
* We used interal SPI bus to access FLASH section. We must wait the SPI bus to
* idle if we performed any command.
*
* SPI Bus status register: F81534_BUS_REG_STATUS
* Bit 0/1 : BUSY
* Bit 2 : IDLE
*/
#define F81534_BUS_BUSY (BIT(0) | BIT(1))
#define F81534_BUS_IDLE BIT(2)
#define F81534_BUS_READ_DATA 0x1004
#define F81534_BUS_REG_STATUS 0x1003
#define F81534_BUS_REG_START 0x1002
#define F81534_BUS_REG_END 0x1001
#define F81534_CMD_READ 0x03
#define F81534_DEFAULT_BAUD_RATE 9600
#define F81534_MAX_BAUDRATE 115200
#define F81534_PORT_CONF_DISABLE_PORT BIT(3)
#define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
#define F81534_PORT_UNAVAILABLE \
(F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
#define F81534_1X_RXTRIGGER 0xc3
#define F81534_8X_RXTRIGGER 0xcf
static const struct usb_device_id f81534_id_table[] = {
{ USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
{ USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
{} /* Terminating entry */
};
#define F81534_TX_