add support for XR21V141x usb serial chip (#9514)

This commit is contained in:
Chen Yijun 2022-06-12 03:29:12 +08:00 committed by GitHub
parent 76a4c855a2
commit 54a3998646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 3181 additions and 0 deletions

View File

@ -0,0 +1,32 @@
#
# Copyright (C) Chen Yijun
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=xr-usb-serial
PKG_RELEASE:=1
include $(INCLUDE_DIR)/package.mk
define KernelPackage/$(PKG_NAME)
SUBMENU:=USB Support
DEPENDS:=@GPIO_SUPPORT +kmod-usb-serial +kmod-usb2
TITLE:=Driver for XR21V141x (used in HUAWEI UPS)
AUTOLOAD:=$(call AutoLoad,30,xr_usb_serial_common,1)
FILES:=$(PKG_BUILD_DIR)/xr_usb_serial_common.ko
endef
define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
$(KERNEL_MAKE_FLAGS) \
M="$(PKG_BUILD_DIR)" \
EXTRA_CFLAGS="$(BUILDFLAGS)" \
modules
endef
$(eval $(call KernelPackage,$(PKG_NAME)))

View File

@ -0,0 +1,10 @@
#
# Copyright (C) Chen Yijun
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
obj-m := xr_usb_serial_common.o
EXTRA_CFLAGS := -DDEBUG=0

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,197 @@
/*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* CMSPAR, some architectures can't have space and mark parity.
*/
#ifndef CMSPAR
#define CMSPAR 0
#endif
/*
* Major and minor numbers.
*/
#define XR_USB_SERIAL_TTY_MAJOR 266
#define XR_USB_SERIAL_TTY_MINORS 32
/*
* Requests.
*/
#define USB_RT_XR_USB_SERIAL (USB_TYPE_CLASS | USB_RECIP_INTERFACE)
/*
* Output control lines.
*/
#define XR_USB_SERIAL_CTRL_DTR 0x01
#define XR_USB_SERIAL_CTRL_RTS 0x02
/*
* Input control lines and line errors.
*/
#define XR_USB_SERIAL_CTRL_DCD 0x01
#define XR_USB_SERIAL_CTRL_DSR 0x02
#define XR_USB_SERIAL_CTRL_BRK 0x04
#define XR_USB_SERIAL_CTRL_RI 0x08
#define XR_USB_SERIAL_CTRL_FRAMING 0x10
#define XR_USB_SERIAL_CTRL_PARITY 0x20
#define XR_USB_SERIAL_CTRL_OVERRUN 0x40
/*
* Internal driver structures.
*/
/*
* The only reason to have several buffers is to accommodate assumptions
* in line disciplines. They ask for empty space amount, receive our URB size,
* and proceed to issue several 1-character writes, assuming they will fit.
* The very first write takes a complete URB. Fortunately, this only happens
* when processing onlcr, so we only need 2 buffers. These values must be
* powers of 2.
*/
#define XR_USB_SERIAL_NW 16
#define XR_USB_SERIAL_NR 16
#define RAMCTL_BUFFER_PARITY 0x1
#define RAMCTL_BUFFER_BREAK 0x2
#define RAMCTL_BUFFER_FRAME 0x4
#define RAMCTL_BUFFER_OVERRUN 0x8
struct xr_usb_serial_wb {
unsigned char *buf;
dma_addr_t dmah;
int len;
int use;
struct urb *urb;
struct xr_usb_serial *instance;
};
struct xr_usb_serial_rb {
int size;
unsigned char *base;
dma_addr_t dma;
int index;
struct xr_usb_serial *instance;
};
struct reg_addr_map {
unsigned int uart_enable_addr;
unsigned int uart_format_addr;
unsigned int uart_flow_addr;
unsigned int uart_loopback_addr;
unsigned int uart_xon_char_addr;
unsigned int uart_xoff_char_addr;
unsigned int uart_gpio_mode_addr;
unsigned int uart_gpio_dir_addr;
unsigned int uart_gpio_set_addr;
unsigned int uart_gpio_clr_addr;
unsigned int uart_gpio_status_addr;
unsigned int tx_break_addr;
unsigned int uart_custom_driver;
unsigned int uart_low_latency;
};
struct xr_usb_serial {
struct usb_device *dev; /* the corresponding usb device */
struct usb_interface *control; /* control interface */
struct usb_interface *data; /* data interface */
struct tty_port port; /* our tty port data */
struct urb *ctrlurb; /* urbs */
u8 *ctrl_buffer; /* buffers of urbs */
dma_addr_t ctrl_dma; /* dma handles of buffers */
u8 *country_codes; /* country codes from device */
unsigned int country_code_size; /* size of this buffer */
unsigned int country_rel_date; /* release date of version */
struct xr_usb_serial_wb wb[XR_USB_SERIAL_NW];
unsigned long read_urbs_free;
struct urb *read_urbs[XR_USB_SERIAL_NR];
struct xr_usb_serial_rb read_buffers[XR_USB_SERIAL_NR];
int rx_buflimit;
int rx_endpoint;
spinlock_t read_lock;
int write_used; /* number of non-empty write buffers */
int transmitting;
spinlock_t write_lock;
struct mutex mutex;
bool disconnected;
struct usb_cdc_line_coding line; /* bits, stop, parity */
struct work_struct work; /* work queue entry for line discipline waking up */
unsigned int ctrlin; /* input control lines (DCD, DSR, RI, break, overruns) */
unsigned int ctrlout; /* output control lines (DTR, RTS) */
unsigned int writesize; /* max packet size for the output bulk endpoint */
unsigned int readsize,ctrlsize; /* buffer sizes for freeing */
unsigned int minor; /* xr_usb_serial minor number */
unsigned char clocal; /* termios CLOCAL */
unsigned int ctrl_caps; /* control capabilities from the class specific header */
unsigned int susp_count; /* number of suspended interfaces */
unsigned int combined_interfaces:1; /* control and data collapsed */
unsigned int is_int_ep:1; /* interrupt endpoints contrary to spec used */
unsigned int throttled:1; /* actually throttled */
unsigned int throttle_req:1; /* throttle requested */
u8 bInterval;
struct xr_usb_serial_wb *delayed_wb; /* write queued for a device about to be woken */
unsigned int channel;
int preciseflags; /* USB: wide mode, TTY: flags per character */
int trans9; /* USB: wide mode, serial 9N1 */
int have_extra_byte;
int extra_byte;
unsigned short DeviceVendor;
unsigned short DeviceProduct;
struct reg_addr_map reg_map;
#ifdef CONFIG_GPIOLIB
struct gpio_chip xr_gpio;
int rv_gpio_created;
#endif
};
#define CDC_DATA_INTERFACE_TYPE 0x0a
/* constants describing various quirks and errors */
#define NO_UNION_NORMAL 1
#define SINGLE_RX_URB 2
#define NO_CAP_LINE 4
#define NOT_A_MODEM 8
#define NO_DATA_INTERFACE 16
#define IGNORE_DEVICE 32
#define UART_ENABLE_TX 1
#define UART_ENABLE_RX 2
#define UART_GPIO_CLR_DTR 0x8
#define UART_GPIO_SET_DTR 0x8
#define UART_GPIO_CLR_RTS 0x20
#define UART_GPIO_SET_RTS 0x20
#define LOOPBACK_ENABLE_TX_RX 1
#define LOOPBACK_ENABLE_RTS_CTS 2
#define LOOPBACK_ENABLE_DTR_DSR 4
#define UART_FLOW_MODE_NONE 0x0
#define UART_FLOW_MODE_HW 0x1
#define UART_FLOW_MODE_SW 0x2
#define UART_GPIO_MODE_SEL_GPIO 0x0
#define UART_GPIO_MODE_SEL_RTS_CTS 0x1
#define XR2280x_FUNC_MGR_OFFSET 0x40

View File

@ -0,0 +1,821 @@
/*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define XR_SET_MAP_XR2280X 5
#define XR_GET_MAP_XR2280X 5
#define XR_SET_MAP_XR21B142X 0
#define XR_GET_MAP_XR21B142X 0
#define XR_SET_MAP_XR21V141X 0
#define XR_GET_MAP_XR21V141X 1
#define XR_SET_MAP_XR21B1411 0
#define XR_GET_MAP_XR21B1411 1
int xr_usb_serial_set_reg(struct xr_usb_serial *xr_usb_serial,int regnum, int value)
{
int result;
int channel = 0;
//dev_info(&xr_usb_serial->control->dev, "%s Channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR2280X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
XR2280xaddr, /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(xr_usb_serial->channel)
channel = xr_usb_serial->channel - 1;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21V141X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B1411, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum , /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
channel = (xr_usb_serial->channel - 4)*2;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B142X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR | 1, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
return result;
}
int xr_usb_serial_set_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, int value)
{
int result;
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%02x = 0x%02x\n", __func__,channel,regnum, value);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR2280X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
XR2280xaddr, /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21V141X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B1411, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR , /* request_type */
value, /* request value */
regnum , /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_sndctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_SET_MAP_XR21B142X, /* request */
USB_DIR_OUT | USB_TYPE_VENDOR | 1, /* request_type */
value, /* request value */
regnum | (channel << 8), /* index */
NULL, /* data */
0, /* size */
5000); /* timeout */
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
return result;
}
int xr_usb_serial_get_reg(struct xr_usb_serial *xr_usb_serial,int regnum, short *value)
{
int result;
int channel = 0;
short *dmadata;
/* Use dynamic memory instead of statically allocated buffer
This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
dmadata = kmalloc(2, GFP_KERNEL);
if (!dmadata)
{
dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d] error no memory allocated!! \n", __func__,__LINE__);
return -ENOMEM;
}
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR2280X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
XR2280xaddr, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(xr_usb_serial->channel)
channel = xr_usb_serial->channel -1;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21V141X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
1, /* size */
5000); /* timeout */
memcpy(value, dmadata, 1);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B1411, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
channel = (xr_usb_serial->channel -4)*2;
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B142X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR | 1, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s channel:%d Reg 0x%x Error:%d\n", __func__,channel,regnum,result);
//else
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
kfree(dmadata);
return result;
}
int xr_usb_serial_get_reg_ext(struct xr_usb_serial *xr_usb_serial,int channel,int regnum, short *value)
{
int result;
int XR2280xaddr = XR2280x_FUNC_MGR_OFFSET + regnum;
short *dmadata;
/* Use dynamic memory instead of statically allocated buffer
This is to avoid Error -11 (EAGAIN) for Kernel no longer accept static allocated buffer after 4.9 */
dmadata = kmalloc(2, GFP_KERNEL);
if (!dmadata)
{
dev_err(&xr_usb_serial->control->dev, "[func:%s,line:%d] error no memory allocated!! \n", __func__,__LINE__);
return -ENOMEM;
}
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR2280X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
XR2280xaddr, /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21V141X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
1, /* size */
5000); /* timeout */
memcpy(value, dmadata, 1);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B1411, /* request */
USB_DIR_IN | USB_TYPE_VENDOR , /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
result = usb_control_msg(xr_usb_serial->dev, /* usb device */
usb_rcvctrlpipe(xr_usb_serial->dev, 0), /* endpoint pipe */
XR_GET_MAP_XR21B142X, /* request */
USB_DIR_IN | USB_TYPE_VENDOR | 1, /* request_type */
0, /* request value */
regnum | (channel << 8), /* index */
dmadata, /* data */
2, /* size */
5000); /* timeout */
memcpy(value, dmadata, 2);
}
else
{
result = -1;
}
if(result < 0)
dev_err(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result);
//else
//dev_info(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value);
kfree(dmadata);
return result;
}
struct xr21v141x_baud_rate
{
unsigned int tx;
unsigned int rx0;
unsigned int rx1;
};
static struct xr21v141x_baud_rate xr21v141x_baud_rates[] = {
{ 0x000, 0x000, 0x000 },
{ 0x000, 0x000, 0x000 },
{ 0x100, 0x000, 0x100 },
{ 0x020, 0x400, 0x020 },
{ 0x010, 0x100, 0x010 },
{ 0x208, 0x040, 0x208 },
{ 0x104, 0x820, 0x108 },
{ 0x844, 0x210, 0x884 },
{ 0x444, 0x110, 0x444 },
{ 0x122, 0x888, 0x224 },
{ 0x912, 0x448, 0x924 },
{ 0x492, 0x248, 0x492 },
{ 0x252, 0x928, 0x292 },
{ 0X94A, 0X4A4, 0XA52 },
{ 0X52A, 0XAA4, 0X54A },
{ 0XAAA, 0x954, 0X4AA },
{ 0XAAA, 0x554, 0XAAA },
{ 0x555, 0XAD4, 0X5AA },
{ 0XB55, 0XAB4, 0X55A },
{ 0X6B5, 0X5AC, 0XB56 },
{ 0X5B5, 0XD6C, 0X6D6 },
{ 0XB6D, 0XB6A, 0XDB6 },
{ 0X76D, 0X6DA, 0XBB6 },
{ 0XEDD, 0XDDA, 0X76E },
{ 0XDDD, 0XBBA, 0XEEE },
{ 0X7BB, 0XF7A, 0XDDE },
{ 0XF7B, 0XEF6, 0X7DE },
{ 0XDF7, 0XBF6, 0XF7E },
{ 0X7F7, 0XFEE, 0XEFE },
{ 0XFDF, 0XFBE, 0X7FE },
{ 0XF7F, 0XEFE, 0XFFE },
{ 0XFFF, 0XFFE, 0XFFD },
};
#define UART_CLOCK_DIVISOR_0 0x004
#define UART_CLOCK_DIVISOR_1 0x005
#define UART_CLOCK_DIVISOR_2 0x006
#define UART_TX_CLOCK_MASK_0 0x007
#define UART_TX_CLOCK_MASK_1 0x008
#define UART_RX_CLOCK_MASK_0 0x009
#define UART_RX_CLOCK_MASK_1 0x00a
static int xr21v141x_set_baud_rate(struct xr_usb_serial *xr_usb_serial, unsigned int rate)
{
unsigned int divisor = 48000000 / rate;
unsigned int i = ((32 * 48000000) / rate) & 0x1f;
unsigned int tx_mask = xr21v141x_baud_rates[i].tx;
unsigned int rx_mask = (divisor & 1) ? xr21v141x_baud_rates[i].rx1 : xr21v141x_baud_rates[i].rx0;
//dev_info(&xr_usb_serial->control->dev, "Setting baud rate to %d: i=%u div=%u tx=%03x rx=%03x\n", rate, i, divisor, tx_mask, rx_mask);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_0, (divisor >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_1, (divisor >> 8) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_CLOCK_DIVISOR_2, (divisor >> 16) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_0, (tx_mask >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_TX_CLOCK_MASK_1, (tx_mask >> 8) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_0, (rx_mask >> 0) & 0xff);
xr_usb_serial_set_reg(xr_usb_serial,UART_RX_CLOCK_MASK_1, (rx_mask >> 8) & 0xff);
return 0;
}
/* devices aren't required to support these requests.
* the cdc xr_usb_serial descriptor tells whether they do...
*/
int xr_usb_serial_set_control(struct xr_usb_serial *xr_usb_serial, unsigned int control)
{
int ret = 0;
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if (control & XR_USB_SERIAL_CTRL_DTR)
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
else
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
if (control & XR_USB_SERIAL_CTRL_RTS)
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
else
xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0);
}
return ret;
}
int xr_usb_serial_set_line(struct xr_usb_serial *xr_usb_serial, struct usb_cdc_line_coding* line)
{
int ret = 0;
unsigned int format_size;
unsigned int format_parity;
unsigned int format_stop;
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
xr21v141x_set_baud_rate(xr_usb_serial,le32_to_cpu(line->dwDTERate));
format_size = line->bDataBits;
format_parity = line->bParityType;
format_stop = line->bCharFormat;
xr_usb_serial_set_reg(xr_usb_serial,
xr_usb_serial->reg_map.uart_format_addr,
(format_size << 0) | (format_parity << 4) | (format_stop << 7) );
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line));
}
return ret;
}
int xr_usb_serial_set_flow_mode(struct xr_usb_serial *xr_usb_serial, struct tty_struct *tty, unsigned int cflag)
{
unsigned int flow;
unsigned int gpio_mode;
if (cflag & CRTSCTS)
{
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:hardware\n");
flow = UART_FLOW_MODE_HW;
gpio_mode = UART_GPIO_MODE_SEL_RTS_CTS;
}
else if (I_IXOFF(tty) || I_IXON(tty))
{
unsigned char start_char = START_CHAR(tty);
unsigned char stop_char = STOP_CHAR(tty);
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:software\n");
flow = UART_FLOW_MODE_SW;
gpio_mode = UART_GPIO_MODE_SEL_GPIO;
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xon_char_addr, start_char);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_xoff_char_addr, stop_char);
}
else
{
//dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_set_flow_mode:none\n");
flow = UART_FLOW_MODE_NONE;
gpio_mode = UART_GPIO_MODE_SEL_GPIO;
}
if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
//Add support for the TXT and RXT function for 0x1420, 0x1422, 0x1424, by setting GPIO_MODE [9:8] = '11'
gpio_mode |= 0x300;
}
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_flow_addr, flow);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, gpio_mode);
return 0;
}
int xr_usb_serial_send_break(struct xr_usb_serial *xr_usb_serial, int state)
{
int ret = 0;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(state)
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0xffff);
else
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.tx_break_addr,0);
}
else
{
ret = xr_usb_serial_ctrl_msg(xr_usb_serial, USB_CDC_REQ_SEND_BREAK, state, NULL, 0);
}
return ret;
}
#define URM_REG_BLOCK 4
#define URM_ENABLE_BASE 0x010
#define URM_ENABLE_0_TX 0x001
#define URM_ENABLE_0_RX 0x002
#define URM_RESET_RX_FIFO_BASE 0x018
#define URM_RESET_TX_FIFO_BASE 0x01C
int xr_usb_serial_enable(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_enable channel=%d\n",channel);
if(channel)
channel--;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX | URM_ENABLE_0_RX);
}
else
{
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,UART_ENABLE_TX | UART_ENABLE_RX);
}
return ret;
}
int xr_usb_serial_fifo_reset(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
if(channel) channel--;
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_RX_FIFO_BASE + channel,0xff);
ret |= xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_RESET_TX_FIFO_BASE + channel,0xff);
}
return ret;
}
int xr_usb_serial_disable(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
int channel = xr_usb_serial->channel;
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_disable channel=%d\n",channel);
if(channel) channel--;
ret = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_enable_addr,0);
if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,URM_REG_BLOCK,URM_ENABLE_BASE + channel,URM_ENABLE_0_TX);
}
return ret;
}
int xr_usb_serial_set_loopback(struct xr_usb_serial *xr_usb_serial, int channel)
{
int ret = 0;
xr_usb_serial_disable(xr_usb_serial);
if((xr_usb_serial->DeviceProduct == 0x1410) ||
(xr_usb_serial->DeviceProduct == 0x1412) ||
(xr_usb_serial->DeviceProduct == 0x1414))
{
switch (channel)
{
case 0:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x40);
break;
case 1:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x41);
break;
case 2:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x42);
break;
case 3:
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x43);
break;
default:
break;
}
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
ret = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,
xr_usb_serial->reg_map.uart_loopback_addr,0x07);
}
xr_usb_serial_enable(xr_usb_serial);
return ret;
}
#define XR21V1414_WIDE_MODE_OFFSET 3
#define XR21B142X_WIDE_MODE_TX_OFFSET 0x42
#define XR21B142X_WIDE_MODE_RX_OFFSET 0x45
/* XR2280x_FUNC_MGR_OFFSET will be included in xr_usb_serial_set_reg */
#define XR21B140X_WIDE_MODE_TX_OFFSET 0x22
#define XR21B140X_WIDE_MODE_RX_OFFSET 0x25
int xr_usb_serial_set_wide_mode(struct xr_usb_serial *xr_usb_serial, int preciseflags)
{
int ret = 0;
int channel = xr_usb_serial->channel;
xr_usb_serial_disable(xr_usb_serial);
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_TX_OFFSET, preciseflags);
xr_usb_serial_set_reg(xr_usb_serial, XR21B140X_WIDE_MODE_RX_OFFSET, preciseflags);
}
else if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
if(channel) channel--;
xr_usb_serial_set_reg_ext(xr_usb_serial, 0x66, channel*8 + XR21V1414_WIDE_MODE_OFFSET, preciseflags);
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
xr_usb_serial_set_reg(xr_usb_serial,0xd02, preciseflags);
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_TX_OFFSET, preciseflags);
xr_usb_serial_set_reg(xr_usb_serial, XR21B142X_WIDE_MODE_RX_OFFSET, preciseflags);
}
xr_usb_serial_enable(xr_usb_serial);
return ret;
}
static int xr_usb_serial_tiocmget(struct xr_usb_serial *xr_usb_serial)
{
short data;
int result;
result = xr_usb_serial_get_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_status_addr, &data);
//dev_info(&xr_usb_serial->control->dev, "xr_usb_serial_tiocmget uart_gpio_status_addr:0x%04x\n",data);
if (result)
return ((data & 0x8) ? 0: TIOCM_DTR) | ((data & 0x20) ? 0:TIOCM_RTS ) | ((data & 0x4) ? 0:TIOCM_DSR) | ((data & 0x1) ? 0 : TIOCM_RI) | ((data & 0x2) ? 0:TIOCM_CD) | ((data & 0x10) ? 0 : TIOCM_CTS);
else
return -EFAULT;
}
static int xr_usb_serial_tiocmset(struct xr_usb_serial *xr_usb_serial,
unsigned int set, unsigned int clear)
{
unsigned int newctrl = 0;
newctrl = xr_usb_serial->ctrlout;
set = (set & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (set & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
clear = (clear & TIOCM_DTR ? XR_USB_SERIAL_CTRL_DTR : 0) | (clear & TIOCM_RTS ? XR_USB_SERIAL_CTRL_RTS : 0);
newctrl = (newctrl & ~clear) | set;
if (xr_usb_serial->ctrlout == newctrl)
return 0;
xr_usb_serial->ctrlout = newctrl;
if (newctrl & XR_USB_SERIAL_CTRL_DTR)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x08);
else
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x08);
if (newctrl & XR_USB_SERIAL_CTRL_RTS)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, 0x20);
else
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, 0x20);
return 0;
}
static struct reg_addr_map xr21b140x_reg_map;
static struct reg_addr_map xr21b1411_reg_map;
static struct reg_addr_map xr21v141x_reg_map;
static struct reg_addr_map xr21b142x_reg_map;
static void init_xr21b140x_reg_map(void)
{
xr21b140x_reg_map.uart_enable_addr = 0x00;
xr21b140x_reg_map.uart_format_addr = 0x05;
xr21b140x_reg_map.uart_flow_addr = 0x06;
xr21b140x_reg_map.uart_loopback_addr = 0x16;
xr21b140x_reg_map.uart_xon_char_addr = 0x07;
xr21b140x_reg_map.uart_xoff_char_addr = 0x08;
xr21b140x_reg_map.uart_gpio_mode_addr = 0x0c;
xr21b140x_reg_map.uart_gpio_dir_addr = 0x0d;
xr21b140x_reg_map.uart_gpio_set_addr = 0x0e;
xr21b140x_reg_map.uart_gpio_clr_addr = 0x0f;
xr21b140x_reg_map.uart_gpio_status_addr = 0x10;
xr21b140x_reg_map.tx_break_addr = 0x0a;
xr21b140x_reg_map.uart_custom_driver = 0x41;
}
static void init_xr21b1411_reg_map(void)
{
xr21b1411_reg_map.uart_enable_addr = 0xc00;
xr21b1411_reg_map.uart_flow_addr = 0xc06;
xr21b1411_reg_map.uart_loopback_addr = 0xc16;
xr21b1411_reg_map.uart_xon_char_addr = 0xc07;
xr21b1411_reg_map.uart_xoff_char_addr = 0xc08;
xr21b1411_reg_map.uart_gpio_mode_addr = 0xc0c;
xr21b1411_reg_map.uart_gpio_dir_addr = 0xc0d;
xr21b1411_reg_map.uart_gpio_set_addr = 0xc0e;
xr21b1411_reg_map.uart_gpio_clr_addr = 0xc0f;
xr21b1411_reg_map.uart_gpio_status_addr = 0xc10;
xr21b1411_reg_map.tx_break_addr = 0xc0a;
xr21b1411_reg_map.uart_custom_driver = 0x20d;
}
static void init_xr21v141x_reg_map(void)
{
xr21v141x_reg_map.uart_enable_addr = 0x03;
xr21v141x_reg_map.uart_format_addr = 0x0b;
xr21v141x_reg_map.uart_flow_addr = 0x0c;
xr21v141x_reg_map.uart_loopback_addr = 0x12;
xr21v141x_reg_map.uart_xon_char_addr = 0x10;
xr21v141x_reg_map.uart_xoff_char_addr = 0x11;
xr21v141x_reg_map.uart_gpio_mode_addr = 0x1a;
xr21v141x_reg_map.uart_gpio_dir_addr = 0x1b;
xr21v141x_reg_map.uart_gpio_set_addr = 0x1d;
xr21v141x_reg_map.uart_gpio_clr_addr = 0x1e;
xr21v141x_reg_map.uart_gpio_status_addr = 0x1f;
xr21v141x_reg_map.tx_break_addr = 0x14;
}
static void init_xr21b142x_reg_map(void)
{
xr21b142x_reg_map.uart_enable_addr = 0x00;
xr21b142x_reg_map.uart_flow_addr = 0x06;
xr21b142x_reg_map.uart_loopback_addr = 0x16;
xr21b142x_reg_map.uart_xon_char_addr = 0x07;
xr21b142x_reg_map.uart_xoff_char_addr = 0x08;
xr21b142x_reg_map.uart_gpio_mode_addr = 0x0c;
xr21b142x_reg_map.uart_gpio_dir_addr = 0x0d;
xr21b142x_reg_map.uart_gpio_set_addr = 0x0e;
xr21b142x_reg_map.uart_gpio_clr_addr = 0x0f;
xr21b142x_reg_map.uart_gpio_status_addr = 0x10;
xr21b142x_reg_map.tx_break_addr = 0x0a;
xr21b142x_reg_map.uart_custom_driver = 0x60;
xr21b142x_reg_map.uart_low_latency = 0x46;
}
int xr_usb_serial_pre_setup(struct xr_usb_serial *xr_usb_serial)
{
int ret = 0;
init_xr21b140x_reg_map();
init_xr21b1411_reg_map();
init_xr21v141x_reg_map();
init_xr21b142x_reg_map();
if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1400)
{
memcpy(&(xr_usb_serial->reg_map),&xr21b140x_reg_map,sizeof(struct reg_addr_map));
}
else if(xr_usb_serial->DeviceProduct == 0x1411)
{
memcpy(&(xr_usb_serial->reg_map),&xr21b1411_reg_map,sizeof(struct reg_addr_map));
}
else if((xr_usb_serial->DeviceProduct == 0x1410)||
(xr_usb_serial->DeviceProduct == 0x1412)||
(xr_usb_serial->DeviceProduct == 0x1414))
{
memcpy(&(xr_usb_serial->reg_map),&xr21v141x_reg_map,sizeof(struct reg_addr_map));
}
else if((xr_usb_serial->DeviceProduct == 0x1420)||
(xr_usb_serial->DeviceProduct == 0x1422)||
(xr_usb_serial->DeviceProduct == 0x1424))
{
memcpy(&(xr_usb_serial->reg_map),&xr21b142x_reg_map,sizeof(struct reg_addr_map));
}
else
{
ret = -1;
}
if(xr_usb_serial->reg_map.uart_custom_driver)
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_custom_driver, 1);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, 0);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, 0x28);
xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, UART_GPIO_SET_DTR | UART_GPIO_SET_RTS);
return ret;
}

View File

@ -0,0 +1,35 @@
/*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/ioctl.h>
#define XR_USB_SERIAL_IOC_MAGIC 'v'
#define XR_USB_SERIAL_GET_REG _IOWR(XR_USB_SERIAL_IOC_MAGIC, 1, int)
#define XR_USB_SERIAL_SET_REG _IOWR(XR_USB_SERIAL_IOC_MAGIC, 2, int)
#define XR_USB_SERIAL_SET_ADDRESS_MATCH _IO(XR_USB_SERIAL_IOC_MAGIC, 3)
#define XR_USB_SERIAL_SET_PRECISE_FLAGS _IO(XR_USB_SERIAL_IOC_MAGIC, 4)
#define XR_USB_SERIAL_TEST_MODE _IO(XR_USB_SERIAL_IOC_MAGIC, 5)
#define XR_USB_SERIAL_LOOPBACK _IO(XR_USB_SERIAL_IOC_MAGIC, 6)
#define XR_USB_SERIAL_SET_GPIO_MODE_REG _IO(XR_USB_SERIAL_IOC_MAGIC, 9)
#define XR_USB_SERIAL_GET_GPIO_MODE_REG _IO(XR_USB_SERIAL_IOC_MAGIC, 10)
#define XRIOC_SET_ANY_BAUD_RATE _IO(XR_USB_SERIAL_IOC_MAGIC, 11)
#define XRIOC_SET_PRECISE_FLAGS _IO(XR_USB_SERIAL_IOC_MAGIC, 12)
#define VZ_ADDRESS_UNICAST_S 0
#define VZ_ADDRESS_BROADCAST_S 8
#define VZ_ADDRESS_MATCH(U, B) (0x8000000 | ((B) << VZ_ADDRESS_BROADCAST_S) | ((U) << VZ_ADDRESS_UNICAST_S))
#define VZ_ADDRESS_MATCH_DISABLE 0