From 19302bad0d615e230d12012987ca7b7e7aa2c756 Mon Sep 17 00:00:00 2001 From: Wen-chien Jesse Sung Date: Fri, 2 Dec 2016 17:11:02 +0800 Subject: [PATCH] UBUNTU: SAUCE: xr-usb-serial: Driver for Exar USB serial ports BugLink: https://launchpad.net/bugs/1645591 Import USB UART driver from https://www.exar.com/design-tools/software-drivers Product Family: USB UART Part Numbers: XR21V1410, XR21V1412, XR21V1414, XR21B1411, XR21B1420, XR21B1422, XR21B1424, XR22801, XR22802, XR22804 Operating system: Linux 3.6.x and newer Driver version: 1A Release date: January 2015 Signed-off-by: Wen-chien Jesse Sung Signed-off-by: Tim Gardner --- ubuntu/Makefile | 4 + ubuntu/xr-usb-serial/Makefile | 15 + ubuntu/xr-usb-serial/README.txt | 50 + ubuntu/xr-usb-serial/xr_usb_serial_common.c | 1754 +++++++++++++++++++ ubuntu/xr-usb-serial/xr_usb_serial_common.h | 186 ++ ubuntu/xr-usb-serial/xr_usb_serial_hal.c | 717 ++++++++ ubuntu/xr-usb-serial/xr_usb_serial_ioctl.h | 31 + 7 files changed, 2757 insertions(+) create mode 100644 ubuntu/xr-usb-serial/Makefile create mode 100644 ubuntu/xr-usb-serial/README.txt create mode 100644 ubuntu/xr-usb-serial/xr_usb_serial_common.c create mode 100644 ubuntu/xr-usb-serial/xr_usb_serial_common.h create mode 100644 ubuntu/xr-usb-serial/xr_usb_serial_hal.c create mode 100644 ubuntu/xr-usb-serial/xr_usb_serial_ioctl.h diff --git a/ubuntu/Makefile b/ubuntu/Makefile index 18a29876c5e3..5892f627f70e 100644 --- a/ubuntu/Makefile +++ b/ubuntu/Makefile @@ -19,6 +19,10 @@ obj-$(CONFIG_HIO) += hio/ ## ## ## +obj-y += xr-usb-serial/ +## +## +## ## ## ## diff --git a/ubuntu/xr-usb-serial/Makefile b/ubuntu/xr-usb-serial/Makefile new file mode 100644 index 000000000000..1a323a62b348 --- /dev/null +++ b/ubuntu/xr-usb-serial/Makefile @@ -0,0 +1,15 @@ +obj-m := xr_usb_serial_common.o + +KERNELDIR ?= /lib/modules/$(shell uname -r)/build +PWD := $(shell pwd) + +EXTRA_CFLAGS := -DDEBUG=0 + +all: + $(MAKE) -C $(KERNELDIR) M=$(PWD) + +modules_install: + $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install + +clean: + rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions vtty diff --git a/ubuntu/xr-usb-serial/README.txt b/ubuntu/xr-usb-serial/README.txt new file mode 100644 index 000000000000..eb88f40f2080 --- /dev/null +++ b/ubuntu/xr-usb-serial/README.txt @@ -0,0 +1,50 @@ +Exar USB Serial Driver +====================== +Version 1A, 1/9/2015 + +This driver will work with any USB UART function in these Exar devices: + XR21V1410/1412/1414 + XR21B1411 + XR21B1420/1422/1424 + XR22801/802/804 + +The source code has been tested on various Linux kernels from 3.6.x to 3.17.x. +This may also work with newer kernels as well. + + +Installation +------------ + +* Compile and install the common usb serial driver module + + # make + # insmod ./xr_usb_serial_common.ko + + +* Plug the device into the USB host. You should see up to four devices created, + typically /dev/ttyXRUSB[0-3]. + + +Tips for Debugging +------------------ + +* Check that the USB UART is detected by the system + + # lsusb + +* Check that the CDC-ACM driver was not installed for the Exar USB UART + + # ls /dev/tty* + + To remove the CDC-ACM driver and install the driver: + + # rmmod cdc-acm + # modprobe -r usbserial + # modprobe usbserial + # insmod ./xr_usb_serial_common.ko + + +Technical Support +----------------- +Send any technical questions/issues to uarttechsupport@exar.com. + diff --git a/ubuntu/xr-usb-serial/xr_usb_serial_common.c b/ubuntu/xr-usb-serial/xr_usb_serial_common.c new file mode 100644 index 000000000000..0225dc704744 --- /dev/null +++ b/ubuntu/xr-usb-serial/xr_usb_serial_common.c @@ -0,0 +1,1754 @@ +/* + * 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 + */ + + /* + * Copyright (c) 2015 Exar Corporation, Inc. + * + * This driver will work with any USB UART function in these Exar devices: + * XR21V1410/1412/1414 + * XR21B1411 + * XR21B1420/1422/1424 + * XR22801/802/804 + * + * The driver has been tested on various kernel versions from 3.6.x to 3.17.x. + * This driver may work on newer versions as well. There is a different driver available + * from www.exar.com that will work with kernel versions 2.6.18 to 3.4.x. + * + * ChangeLog: + * Version 1A - Initial released version. + */ + +//#undef DEBUG +#undef VERBOSE_DEBUG + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "linux/version.h" + +#include "xr_usb_serial_common.h" +#include "xr_usb_serial_ioctl.h" + + +#define DRIVER_AUTHOR "" +#define DRIVER_DESC "Exar USB UART (serial port) driver" + +static struct usb_driver xr_usb_serial_driver; +static struct tty_driver *xr_usb_serial_tty_driver; +static struct xr_usb_serial *xr_usb_serial_table[XR_USB_SERIAL_TTY_MINORS]; + +static DEFINE_MUTEX(xr_usb_serial_table_lock); + +/* + * xr_usb_serial_table accessors + */ + +/* + * Look up an XR_USB_SERIAL structure by index. If found and not disconnected, increment + * its refcount and return it with its mutex held. + */ +static struct xr_usb_serial *xr_usb_serial_get_by_index(unsigned index) +{ + struct xr_usb_serial *xr_usb_serial; + + mutex_lock(&xr_usb_serial_table_lock); + xr_usb_serial = xr_usb_serial_table[index]; + if (xr_usb_serial) { + mutex_lock(&xr_usb_serial->mutex); + if (xr_usb_serial->disconnected) { + mutex_unlock(&xr_usb_serial->mutex); + xr_usb_serial = NULL; + } else { + tty_port_get(&xr_usb_serial->port); + mutex_unlock(&xr_usb_serial->mutex); + } + } + mutex_unlock(&xr_usb_serial_table_lock); + return xr_usb_serial; +} + +/* + * Try to find an available minor number and if found, associate it with 'xr_usb_serial'. + */ +static int xr_usb_serial_alloc_minor(struct xr_usb_serial *xr_usb_serial) +{ + int minor; + + mutex_lock(&xr_usb_serial_table_lock); + for (minor = 0; minor < XR_USB_SERIAL_TTY_MINORS; minor++) { + if (!xr_usb_serial_table[minor]) { + xr_usb_serial_table[minor] = xr_usb_serial; + break; + } + } + mutex_unlock(&xr_usb_serial_table_lock); + + return minor; +} + +/* Release the minor number associated with 'xr_usb_serial'. */ +static void xr_usb_serial_release_minor(struct xr_usb_serial *xr_usb_serial) +{ + mutex_lock(&xr_usb_serial_table_lock); + xr_usb_serial_table[xr_usb_serial->minor] = NULL; + mutex_unlock(&xr_usb_serial_table_lock); +} + +/* + * Functions for XR_USB_SERIAL control messages. + */ + +static int xr_usb_serial_ctrl_msg(struct xr_usb_serial *xr_usb_serial, int request, int value, + void *buf, int len) +{ + int retval = usb_control_msg(xr_usb_serial->dev, usb_sndctrlpipe(xr_usb_serial->dev, 0), + request, USB_RT_XR_USB_SERIAL, value, + xr_usb_serial->control->altsetting[0].desc.bInterfaceNumber, + buf, len, 5000); + dev_dbg(&xr_usb_serial->control->dev, + "%s - rq 0x%02x, val %#x, len %#x, result %d\n", + __func__, request, value, len, retval); + return retval < 0 ? retval : 0; +} + +#include "xr_usb_serial_hal.c" + + +/* + * Write buffer management. + * All of these assume proper locks taken by the caller. + */ + +static int xr_usb_serial_wb_alloc(struct xr_usb_serial *xr_usb_serial) +{ + int i, wbn; + struct xr_usb_serial_wb *wb; + + wbn = 0; + i = 0; + for (;;) { + wb = &xr_usb_serial->wb[wbn]; + if (!wb->use) { + wb->use = 1; + return wbn; + } + wbn = (wbn + 1) % XR_USB_SERIAL_NW; + if (++i >= XR_USB_SERIAL_NW) + return -1; + } +} + +static int xr_usb_serial_wb_is_avail(struct xr_usb_serial *xr_usb_serial) +{ + int i, n; + unsigned long flags; + + n = XR_USB_SERIAL_NW; + spin_lock_irqsave(&xr_usb_serial->write_lock, flags); + for (i = 0; i < XR_USB_SERIAL_NW; i++) + n -= xr_usb_serial->wb[i].use; + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + return n; +} + +/* + * Finish write. Caller must hold xr_usb_serial->write_lock + */ +static void xr_usb_serial_write_done(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb) +{ + wb->use = 0; + xr_usb_serial->transmitting--; + usb_autopm_put_interface_async(xr_usb_serial->control); +} + +/* + * Poke write. + * + * the caller is responsible for locking + */ + +static int xr_usb_serial_start_wb(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb) +{ + int rc; + + xr_usb_serial->transmitting++; + + wb->urb->transfer_buffer = wb->buf; + wb->urb->transfer_dma = wb->dmah; + wb->urb->transfer_buffer_length = wb->len; + wb->urb->dev = xr_usb_serial->dev; + + rc = usb_submit_urb(wb->urb, GFP_ATOMIC); + if (rc < 0) { + dev_err(&xr_usb_serial->data->dev, + "%s - usb_submit_urb(write bulk) failed: %d\n", + __func__, rc); + xr_usb_serial_write_done(xr_usb_serial, wb); + } + return rc; +} + +/* + * attributes exported through sysfs + */ +static ssize_t show_caps +(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct usb_interface *intf = to_usb_interface(dev); + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + + return sprintf(buf, "%d", xr_usb_serial->ctrl_caps); +} +static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL); + +static ssize_t show_country_codes +(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct usb_interface *intf = to_usb_interface(dev); + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + + memcpy(buf, xr_usb_serial->country_codes, xr_usb_serial->country_code_size); + return xr_usb_serial->country_code_size; +} + +static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL); + +static ssize_t show_country_rel_date +(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct usb_interface *intf = to_usb_interface(dev); + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + + return sprintf(buf, "%d", xr_usb_serial->country_rel_date); +} + +static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); +/* + * Interrupt handlers for various XR_USB_SERIAL device responses + */ + +/* control interface reports status changes with "interrupt" transfers */ +static void xr_usb_serial_ctrl_irq(struct urb *urb) +{ + struct xr_usb_serial *xr_usb_serial = urb->context; + struct usb_cdc_notification *dr = urb->transfer_buffer; + struct tty_struct *tty; + unsigned char *data; + int newctrl; + int retval; + int status = urb->status; + int i; + unsigned char *p; + + switch (status) { + case 0: + p = (unsigned char *)(urb->transfer_buffer); + for(i=0;iactual_length;i++) + { + dev_dbg(&xr_usb_serial->control->dev,"0x%02x\n",p[i]); + } + /* success */ + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + /* this urb is terminated, clean up */ + dev_dbg(&xr_usb_serial->control->dev, + "%s - urb shutting down with status: %d\n", + __func__, status); + return; + default: + dev_dbg(&xr_usb_serial->control->dev, + "%s - nonzero urb status received: %d\n", + __func__, status); + goto exit; + } + + usb_mark_last_busy(xr_usb_serial->dev); + + data = (unsigned char *)(dr + 1); + switch (dr->bNotificationType) { + case USB_CDC_NOTIFY_NETWORK_CONNECTION: + dev_dbg(&xr_usb_serial->control->dev, "%s - network connection: %d\n", + __func__, dr->wValue); + break; + + case USB_CDC_NOTIFY_SERIAL_STATE: +#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0) + newctrl = get_unaligned_le16(data); + if (!xr_usb_serial->clocal && (xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) { + dev_dbg(&xr_usb_serial->control->dev, "%s - calling hangup\n", + __func__); + tty_port_tty_hangup(&xr_usb_serial->port, false); + } +#else + tty = tty_port_tty_get(&xr_usb_serial->port); + newctrl = get_unaligned_le16(data); + if (tty) + { + if (!xr_usb_serial->clocal && + (xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) { + dev_dbg(&xr_usb_serial->control->dev, + "%s - calling hangup\n", __func__); + tty_hangup(tty); + } + tty_kref_put(tty); + } +#endif + xr_usb_serial->ctrlin = newctrl; + + dev_dbg(&xr_usb_serial->control->dev, + "%s - input control lines: dcd%c dsr%c break%c " + "ring%c framing%c parity%c overrun%c\n", + __func__, + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DCD ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DSR ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_BRK ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_RI ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_FRAMING ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_PARITY ? '+' : '-', + xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_OVERRUN ? '+' : '-'); + break; + + default: + dev_dbg(&xr_usb_serial->control->dev, + "%s - unknown notification %d received: index %d " + "len %d data0 %d data1 %d\n", + __func__, + dr->bNotificationType, dr->wIndex, + dr->wLength, data[0], data[1]); + break; + } +exit: + retval = usb_submit_urb(urb, GFP_ATOMIC); + if (retval) + dev_err(&xr_usb_serial->control->dev, "%s - usb_submit_urb failed: %d\n", + __func__, retval); +} + +static int xr_usb_serial_submit_read_urb(struct xr_usb_serial *xr_usb_serial, int index, gfp_t mem_flags) +{ + int res; + + if (!test_and_clear_bit(index, &xr_usb_serial->read_urbs_free)) + return 0; + + dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d\n", __func__, index); + + res = usb_submit_urb(xr_usb_serial->read_urbs[index], mem_flags); + if (res) { + if (res != -EPERM) { + dev_err(&xr_usb_serial->data->dev, + "%s - usb_submit_urb failed: %d\n", + __func__, res); + } + set_bit(index, &xr_usb_serial->read_urbs_free); + return res; + } + + return 0; +} + +static int xr_usb_serial_submit_read_urbs(struct xr_usb_serial *xr_usb_serial, gfp_t mem_flags) +{ + int res; + int i; + + for (i = 0; i < xr_usb_serial->rx_buflimit; ++i) { + res = xr_usb_serial_submit_read_urb(xr_usb_serial, i, mem_flags); + if (res) + return res; + } + + return 0; +} +static void xr_usb_serial_process_read_urb(struct xr_usb_serial *xr_usb_serial, struct urb *urb) +{ + struct tty_struct *tty; + if (!urb->actual_length) + return; +#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0) + tty_insert_flip_string(&xr_usb_serial->port, urb->transfer_buffer, + urb->actual_length); + tty_flip_buffer_push(&xr_usb_serial->port); +#else + tty = tty_port_tty_get(&xr_usb_serial->port); + if (!tty) + return; + tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length); + tty_flip_buffer_push(tty); + + tty_kref_put(tty); +#endif +} + +static void xr_usb_serial_read_bulk_callback(struct urb *urb) +{ + struct xr_usb_serial_rb *rb = urb->context; + struct xr_usb_serial *xr_usb_serial = rb->instance; + unsigned long flags; + + dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d, len %d\n", __func__, + rb->index, urb->actual_length); + set_bit(rb->index, &xr_usb_serial->read_urbs_free); + + if (!xr_usb_serial->dev) { + dev_dbg(&xr_usb_serial->data->dev, "%s - disconnected\n", __func__); + return; + } + usb_mark_last_busy(xr_usb_serial->dev); + + if (urb->status) { + dev_dbg(&xr_usb_serial->data->dev, "%s - non-zero urb status: %d\n", + __func__, urb->status); + return; + } + xr_usb_serial_process_read_urb(xr_usb_serial, urb); + + /* throttle device if requested by tty */ + spin_lock_irqsave(&xr_usb_serial->read_lock, flags); + xr_usb_serial->throttled = xr_usb_serial->throttle_req; + if (!xr_usb_serial->throttled && !xr_usb_serial->susp_count) { + spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags); + xr_usb_serial_submit_read_urb(xr_usb_serial, rb->index, GFP_ATOMIC); + } else { + spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags); + } +} + +/* data interface wrote those outgoing bytes */ +static void xr_usb_serial_write_bulk(struct urb *urb) +{ + struct xr_usb_serial_wb *wb = urb->context; + struct xr_usb_serial *xr_usb_serial = wb->instance; + unsigned long flags; + + if (urb->status || (urb->actual_length != urb->transfer_buffer_length)) + dev_vdbg(&xr_usb_serial->data->dev, "%s - len %d/%d, status %d\n", + __func__, + urb->actual_length, + urb->transfer_buffer_length, + urb->status); + + spin_lock_irqsave(&xr_usb_serial->write_lock, flags); + xr_usb_serial_write_done(xr_usb_serial, wb); + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + schedule_work(&xr_usb_serial->work); +} + +static void xr_usb_serial_softint(struct work_struct *work) +{ + struct xr_usb_serial *xr_usb_serial = container_of(work, struct xr_usb_serial, work); + struct tty_struct *tty; + + dev_vdbg(&xr_usb_serial->data->dev, "%s\n", __func__); +#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0) + tty_port_tty_wakeup(&xr_usb_serial->port); +#else + tty = tty_port_tty_get(&xr_usb_serial->port); + if (!tty) + return; + tty_wakeup(tty); + tty_kref_put(tty); +#endif +} + +/* + * TTY handlers + */ + +static int xr_usb_serial_tty_install(struct tty_driver *driver, struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial; + int retval; + + dev_dbg(tty->dev, "%s\n", __func__); + + xr_usb_serial = xr_usb_serial_get_by_index(tty->index); + if (!xr_usb_serial) + return -ENODEV; + + retval = tty_standard_install(driver, tty); + if (retval) + goto error_init_termios; + + tty->driver_data = xr_usb_serial; + + return 0; + +error_init_termios: + tty_port_put(&xr_usb_serial->port); + return retval; +} + +static int xr_usb_serial_tty_open(struct tty_struct *tty, struct file *filp) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + + dev_dbg(tty->dev, "%s\n", __func__); + + return tty_port_open(&xr_usb_serial->port, tty, filp); +} + +static int xr_usb_serial_port_activate(struct tty_port *port, struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port); + int retval = -ENODEV; + + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + + mutex_lock(&xr_usb_serial->mutex); + if (xr_usb_serial->disconnected) + goto disconnected; + + retval = usb_autopm_get_interface(xr_usb_serial->control); + if (retval) + goto error_get_interface; + + /* + * FIXME: Why do we need this? Allocating 64K of physically contiguous + * memory is really nasty... + */ + set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); + xr_usb_serial->control->needs_remote_wakeup = 1; + + xr_usb_serial->ctrlurb->dev = xr_usb_serial->dev; + if (usb_submit_urb(xr_usb_serial->ctrlurb, GFP_KERNEL)) { + dev_err(&xr_usb_serial->control->dev, + "%s - usb_submit_urb(ctrl irq) failed\n", __func__); + goto error_submit_urb; + } + + xr_usb_serial->ctrlout = XR_USB_SERIAL_CTRL_DTR | XR_USB_SERIAL_CTRL_RTS; + if (xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout) < 0 && + (xr_usb_serial->ctrl_caps & USB_CDC_CAP_LINE)) + goto error_set_control; + + usb_autopm_put_interface(xr_usb_serial->control); + + /* + * Unthrottle device in case the TTY was closed while throttled. + */ + spin_lock_irq(&xr_usb_serial->read_lock); + xr_usb_serial->throttled = 0; + xr_usb_serial->throttle_req = 0; + spin_unlock_irq(&xr_usb_serial->read_lock); + + if (xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL)) + goto error_submit_read_urbs; + + mutex_unlock(&xr_usb_serial->mutex); + + return 0; + +error_submit_read_urbs: + xr_usb_serial->ctrlout = 0; + xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout); +error_set_control: + usb_kill_urb(xr_usb_serial->ctrlurb); +error_submit_urb: + usb_autopm_put_interface(xr_usb_serial->control); +error_get_interface: +disconnected: + mutex_unlock(&xr_usb_serial->mutex); + return retval; +} + +static void xr_usb_serial_port_destruct(struct tty_port *port) +{ + struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port); + + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + tty_unregister_device(xr_usb_serial_tty_driver, xr_usb_serial->minor); + #endif + xr_usb_serial_release_minor(xr_usb_serial); + usb_put_intf(xr_usb_serial->control); + kfree(xr_usb_serial->country_codes); + kfree(xr_usb_serial); +} + +static void xr_usb_serial_port_shutdown(struct tty_port *port) +{ + struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port); + int i; + + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + + mutex_lock(&xr_usb_serial->mutex); + if (!xr_usb_serial->disconnected) { + usb_autopm_get_interface(xr_usb_serial->control); + xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout = 0); + usb_kill_urb(xr_usb_serial->ctrlurb); + for (i = 0; i < XR_USB_SERIAL_NW; i++) + usb_kill_urb(xr_usb_serial->wb[i].urb); + for (i = 0; i < xr_usb_serial->rx_buflimit; i++) + usb_kill_urb(xr_usb_serial->read_urbs[i]); + xr_usb_serial->control->needs_remote_wakeup = 0; + usb_autopm_put_interface(xr_usb_serial->control); + } + mutex_unlock(&xr_usb_serial->mutex); +} + +static void xr_usb_serial_tty_cleanup(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + tty_port_put(&xr_usb_serial->port); +} + +static void xr_usb_serial_tty_hangup(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + tty_port_hangup(&xr_usb_serial->port); +} + +static void xr_usb_serial_tty_close(struct tty_struct *tty, struct file *filp) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + tty_port_close(&xr_usb_serial->port, tty, filp); +} + +static int xr_usb_serial_tty_write(struct tty_struct *tty, + const unsigned char *buf, int count) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + int stat; + unsigned long flags; + int wbn; + struct xr_usb_serial_wb *wb; + + if (!count) + return 0; + + dev_vdbg(&xr_usb_serial->data->dev, "%s - count %d\n", __func__, count); + + spin_lock_irqsave(&xr_usb_serial->write_lock, flags); + wbn = xr_usb_serial_wb_alloc(xr_usb_serial); + if (wbn < 0) { + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + return 0; + } + wb = &xr_usb_serial->wb[wbn]; + + if (!xr_usb_serial->dev) { + wb->use = 0; + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + return -ENODEV; + } + + count = (count > xr_usb_serial->writesize) ? xr_usb_serial->writesize : count; + dev_vdbg(&xr_usb_serial->data->dev, "%s - write %d\n", __func__, count); + memcpy(wb->buf, buf, count); + wb->len = count; + + usb_autopm_get_interface_async(xr_usb_serial->control); + if (xr_usb_serial->susp_count) { + if (!xr_usb_serial->delayed_wb) + xr_usb_serial->delayed_wb = wb; + else + usb_autopm_put_interface_async(xr_usb_serial->control); + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + return count; /* A white lie */ + } + usb_mark_last_busy(xr_usb_serial->dev); + + stat = xr_usb_serial_start_wb(xr_usb_serial, wb); + spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags); + + if (stat < 0) + return stat; + return count; +} + +static int xr_usb_serial_tty_write_room(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + /* + * Do not let the line discipline to know that we have a reserve, + * or it might get too enthusiastic. + */ + return xr_usb_serial_wb_is_avail(xr_usb_serial) ? xr_usb_serial->writesize : 0; +} + +static int xr_usb_serial_tty_chars_in_buffer(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + /* + * if the device was unplugged then any remaining characters fell out + * of the connector ;) + */ + if (xr_usb_serial->disconnected) + return 0; + /* + * This is inaccurate (overcounts), but it works. + */ + return (XR_USB_SERIAL_NW - xr_usb_serial_wb_is_avail(xr_usb_serial)) * xr_usb_serial->writesize; +} + +static void xr_usb_serial_tty_throttle(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + + spin_lock_irq(&xr_usb_serial->read_lock); + xr_usb_serial->throttle_req = 1; + spin_unlock_irq(&xr_usb_serial->read_lock); +} + +static void xr_usb_serial_tty_unthrottle(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + unsigned int was_throttled; + + spin_lock_irq(&xr_usb_serial->read_lock); + was_throttled = xr_usb_serial->throttled; + xr_usb_serial->throttled = 0; + xr_usb_serial->throttle_req = 0; + spin_unlock_irq(&xr_usb_serial->read_lock); + + if (was_throttled) + xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL); +} + +static int xr_usb_serial_tty_break_ctl(struct tty_struct *tty, int state) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + int retval; + + retval = xr_usb_serial_send_break(xr_usb_serial, state ? 0xffff : 0); + if (retval < 0) + dev_dbg(&xr_usb_serial->control->dev, "%s - send break failed\n", + __func__); + return retval; +} + +static int xr_usb_serial_tty_tiocmget(struct tty_struct *tty) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmget\n"); + return xr_usb_serial_tiocmget(xr_usb_serial); + +} + +static int xr_usb_serial_tty_tiocmset(struct tty_struct *tty, + unsigned int set, unsigned int clear) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmset set=0x%x clear=0x%x\n",set,clear); + return xr_usb_serial_tiocmset(xr_usb_serial,set,clear); + +} + +static int get_serial_info(struct xr_usb_serial *xr_usb_serial, struct serial_struct __user *info) +{ + struct serial_struct tmp; + + if (!info) + return -EINVAL; + + memset(&tmp, 0, sizeof(tmp)); + tmp.flags = ASYNC_LOW_LATENCY; + tmp.xmit_fifo_size = xr_usb_serial->writesize; + tmp.baud_base = le32_to_cpu(xr_usb_serial->line.dwDTERate); + tmp.close_delay = xr_usb_serial->port.close_delay / 10; + tmp.closing_wait = xr_usb_serial->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? + ASYNC_CLOSING_WAIT_NONE : + xr_usb_serial->port.closing_wait / 10; + + if (copy_to_user(info, &tmp, sizeof(tmp))) + return -EFAULT; + else + return 0; +} + +static int set_serial_info(struct xr_usb_serial *xr_usb_serial, + struct serial_struct __user *newinfo) +{ + struct serial_struct new_serial; + unsigned int closing_wait, close_delay; + int retval = 0; + + if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) + return -EFAULT; + + close_delay = new_serial.close_delay * 10; + closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? + ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; + + mutex_lock(&xr_usb_serial->port.mutex); + + if (!capable(CAP_SYS_ADMIN)) { + if ((close_delay != xr_usb_serial->port.close_delay) || + (closing_wait != xr_usb_serial->port.closing_wait)) + retval = -EPERM; + else + retval = -EOPNOTSUPP; + } else { + xr_usb_serial->port.close_delay = close_delay; + xr_usb_serial->port.closing_wait = closing_wait; + } + + mutex_unlock(&xr_usb_serial->port.mutex); + return retval; +} + +static int xr_usb_serial_tty_ioctl(struct tty_struct *tty, + unsigned int cmd, unsigned long arg) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; + int rv = -ENOIOCTLCMD; + unsigned int channel, reg, val; + + short *data; + switch (cmd) { + case TIOCGSERIAL: /* gets serial port data */ + rv = get_serial_info(xr_usb_serial, (struct serial_struct __user *) arg); + break; + case TIOCSSERIAL: + rv = set_serial_info(xr_usb_serial, (struct serial_struct __user *) arg); + break; + case XR_USB_SERIAL_GET_REG: + if (get_user(channel, (int __user *)arg)) + return -EFAULT; + if (get_user(reg, (int __user *)(arg + sizeof(int)))) + return -EFAULT; + + data = kmalloc(2, GFP_KERNEL); + if (data == NULL) { + dev_err(&xr_usb_serial->control->dev, "%s - Cannot allocate USB buffer.\n", __func__); + return -ENOMEM; + } + + if (channel == -1) + { + rv = xr_usb_serial_get_reg(xr_usb_serial,reg, data); + } + else + { + rv = xr_usb_serial_get_reg_ext(xr_usb_serial,channel,reg, data); + } + if (rv != 1) { + dev_err(&xr_usb_serial->control->dev, "Cannot get register (%d)\n", rv); + kfree(data); + return -EFAULT; + } + if (put_user(le16_to_cpu(*data), (int __user *)(arg + 2 * sizeof(int)))) + { + dev_err(&xr_usb_serial->control->dev, "Cannot put user result\n"); + kfree(data); + return -EFAULT; + } + rv = 0; + kfree(data); + break; + + case XR_USB_SERIAL_SET_REG: + if (get_user(channel, (int __user *)arg)) + return -EFAULT; + if (get_user(reg, (int __user *)(arg + sizeof(int)))) + return -EFAULT; + if (get_user(val, (int __user *)(arg + 2 * sizeof(int)))) + return -EFAULT; + + if (channel == -1) + { + rv = xr_usb_serial_set_reg(xr_usb_serial,reg, val); + } + else + { + rv = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,reg, val); + + } + if (rv < 0) + return -EFAULT; + rv = 0; + break; + case XR_USB_SERIAL_LOOPBACK: + if (get_user(channel, (int __user *)arg)) + return -EFAULT; + if (channel == -1) + channel = xr_usb_serial->channel; + rv = xr_usb_serial_set_loopback(xr_usb_serial,channel); + if (rv < 0) + return -EFAULT; + rv = 0; + break; + + } + + return rv; +} + +static void xr_usb_serial_tty_set_termios(struct tty_struct *tty, + struct ktermios *termios_old) +{ + struct xr_usb_serial *xr_usb_serial = tty->driver_data; +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + struct ktermios *termios = tty->termios; +#else + struct ktermios *termios = &tty->termios; +#endif + unsigned int cflag = termios->c_cflag; + struct usb_cdc_line_coding newline; + int newctrl = xr_usb_serial->ctrlout; + xr_usb_serial_disable(xr_usb_serial); + newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty)); + newline.bCharFormat = termios->c_cflag & CSTOPB ? 1 : 0; + newline.bParityType = termios->c_cflag & PARENB ? + (termios->c_cflag & PARODD ? 1 : 2) + + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; + switch (termios->c_cflag & CSIZE) { + case CS5:/*using CS5 replace of the 9 bit data mode*/ + newline.bDataBits = 9; + break; + case CS6: + newline.bDataBits = 6; + break; + case CS7: + newline.bDataBits = 7; + break; + case CS8: + default: + newline.bDataBits = 8; + break; + } + /* FIXME: Needs to clear unsupported bits in the termios */ + xr_usb_serial->clocal = ((termios->c_cflag & CLOCAL) != 0); + + if (!newline.dwDTERate) { + newline.dwDTERate = xr_usb_serial->line.dwDTERate; + newctrl &= ~XR_USB_SERIAL_CTRL_DTR; + } else + newctrl |= XR_USB_SERIAL_CTRL_DTR; + + if (newctrl != xr_usb_serial->ctrlout) + xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout = newctrl); + + xr_usb_serial_set_flow_mode(xr_usb_serial,tty,cflag);/*set the serial flow mode*/ + + if (memcmp(&xr_usb_serial->line, &newline, sizeof newline)) + { + memcpy(&xr_usb_serial->line, &newline, sizeof newline); + dev_dbg(&xr_usb_serial->control->dev, "%s - set line: %d %d %d %d\n", + __func__, + le32_to_cpu(newline.dwDTERate), + newline.bCharFormat, newline.bParityType, + newline.bDataBits); + xr_usb_serial_set_line(xr_usb_serial, &xr_usb_serial->line); + } + xr_usb_serial_enable(xr_usb_serial); +} + +static const struct tty_port_operations xr_usb_serial_port_ops = { + .shutdown = xr_usb_serial_port_shutdown, + .activate = xr_usb_serial_port_activate, + .destruct = xr_usb_serial_port_destruct, +}; + +/* + * USB probe and disconnect routines. + */ + +/* Little helpers: write/read buffers free */ +static void xr_usb_serial_write_buffers_free(struct xr_usb_serial *xr_usb_serial) +{ + int i; + struct xr_usb_serial_wb *wb; + struct usb_device *usb_dev = interface_to_usbdev(xr_usb_serial->control); + + for (wb = &xr_usb_serial->wb[0], i = 0; i < XR_USB_SERIAL_NW; i++, wb++) + usb_free_coherent(usb_dev, xr_usb_serial->writesize, wb->buf, wb->dmah); +} + +static void xr_usb_serial_read_buffers_free(struct xr_usb_serial *xr_usb_serial) +{ + struct usb_device *usb_dev = interface_to_usbdev(xr_usb_serial->control); + int i; + + for (i = 0; i < xr_usb_serial->rx_buflimit; i++) + usb_free_coherent(usb_dev, xr_usb_serial->readsize, + xr_usb_serial->read_buffers[i].base, xr_usb_serial->read_buffers[i].dma); +} + +/* Little helper: write buffers allocate */ +static int xr_usb_serial_write_buffers_alloc(struct xr_usb_serial *xr_usb_serial) +{ + int i; + struct xr_usb_serial_wb *wb; + + for (wb = &xr_usb_serial->wb[0], i = 0; i < XR_USB_SERIAL_NW; i++, wb++) { + wb->buf = usb_alloc_coherent(xr_usb_serial->dev, xr_usb_serial->writesize, GFP_KERNEL, + &wb->dmah); + if (!wb->buf) { + while (i != 0) { + --i; + --wb; + usb_free_coherent(xr_usb_serial->dev, xr_usb_serial->writesize, + wb->buf, wb->dmah); + } + return -ENOMEM; + } + } + return 0; +} + +static int xr_usb_serial_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct usb_cdc_union_desc *union_header = NULL; + struct usb_cdc_country_functional_desc *cfd = NULL; + unsigned char *buffer = intf->altsetting->extra; + int buflen = intf->altsetting->extralen; + struct usb_interface *control_interface; + struct usb_interface *data_interface; + struct usb_endpoint_descriptor *epctrl = NULL; + struct usb_endpoint_descriptor *epread = NULL; + struct usb_endpoint_descriptor *epwrite = NULL; + struct usb_device *usb_dev = interface_to_usbdev(intf); + struct xr_usb_serial *xr_usb_serial; + int minor; + int ctrlsize, readsize; + u8 *buf; + u8 ac_management_function = 0; + u8 call_management_function = 0; + int call_interface_num = -1; + int data_interface_num = -1; + unsigned long quirks; + int num_rx_buf; + int i; + int combined_interfaces = 0; + struct device *tty_dev; + int rv = -ENOMEM; + + /* normal quirks */ + quirks = (unsigned long)id->driver_info; + + if (quirks == IGNORE_DEVICE) + return -ENODEV; + + num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : XR_USB_SERIAL_NR; + + dev_dbg(&intf->dev, "USB_device_id idVendor:%04x, idProduct %04x\n",id->idVendor,id->idProduct); + + /* handle quirks deadly to normal probing*/ + if (quirks == NO_UNION_NORMAL) { + data_interface = usb_ifnum_to_if(usb_dev, 1); + control_interface = usb_ifnum_to_if(usb_dev, 0); + goto skip_normal_probe; + } + + /* normal probing*/ + if (!buffer) { + dev_err(&intf->dev, "Weird descriptor references\n"); + return -EINVAL; + } + + if (!buflen) { + if (intf->cur_altsetting->endpoint && + intf->cur_altsetting->endpoint->extralen && + intf->cur_altsetting->endpoint->extra) { + dev_dbg(&intf->dev, + "Seeking extra descriptors on endpoint\n"); + buflen = intf->cur_altsetting->endpoint->extralen; + buffer = intf->cur_altsetting->endpoint->extra; + } else { + dev_err(&intf->dev, + "Zero length descriptor references\n"); + return -EINVAL; + } + } + + while (buflen > 0) { + if (buffer[1] != USB_DT_CS_INTERFACE) { + dev_err(&intf->dev, "skipping garbage\n"); + goto next_desc; + } + + switch (buffer[2]) { + case USB_CDC_UNION_TYPE: /* we've found it */ + if (union_header) { + dev_err(&intf->dev, "More than one " + "union descriptor, skipping ...\n"); + goto next_desc; + } + union_header = (struct usb_cdc_union_desc *)buffer; + break; + case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/ + cfd = (struct usb_cdc_country_functional_desc *)buffer; + break; + case USB_CDC_HEADER_TYPE: /* maybe check version */ + break; /* for now we ignore it */ + case USB_CDC_ACM_TYPE: + ac_management_function = buffer[3]; + break; + case USB_CDC_CALL_MANAGEMENT_TYPE: + call_management_function = buffer[3]; + call_interface_num = buffer[4]; + if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3) + dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n"); + break; + default: + /* there are LOTS more CDC descriptors that + * could legitimately be found here. + */ + dev_dbg(&intf->dev, "Ignoring descriptor: " + "type %02x, length %d\n", + buffer[2], buffer[0]); + break; + } +next_desc: + buflen -= buffer[0]; + buffer += buffer[0]; + } + + if (!union_header) { + if (call_interface_num > 0) { + dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n"); + /* quirks for Droids MuIn LCD */ + if (quirks & NO_DATA_INTERFACE) + data_interface = usb_ifnum_to_if(usb_dev, 0); + else + data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); + control_interface = intf; + } else { + if (intf->cur_altsetting->desc.bNumEndpoints != 3) { + dev_dbg(&intf->dev,"No union descriptor, giving up\n"); + return -ENODEV; + } else { + dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n"); + combined_interfaces = 1; + control_interface = data_interface = intf; + goto look_for_collapsed_interface; + } + } + } else { + control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); + data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); + if (!control_interface || !data_interface) { + dev_dbg(&intf->dev, "no interfaces\n"); + return -ENODEV; + } + } + + if (data_interface_num != call_interface_num) + dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n"); + + if (control_interface == data_interface) { + /* some broken devices designed for windows work this way */ + dev_warn(&intf->dev,"Control and data interfaces are not separated!\n"); + combined_interfaces = 1; + /* a popular other OS doesn't use it */ + quirks |= NO_CAP_LINE; + if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) { + dev_err(&intf->dev, "This needs exactly 3 endpoints\n"); + return -EINVAL; + } +look_for_collapsed_interface: + for (i = 0; i < 3; i++) { + struct usb_endpoint_descriptor *ep; + ep = &data_interface->cur_altsetting->endpoint[i].desc; + + if (usb_endpoint_is_int_in(ep)) + epctrl = ep; + else if (usb_endpoint_is_bulk_out(ep)) + epwrite = ep; + else if (usb_endpoint_is_bulk_in(ep)) + epread = ep; + else + return -EINVAL; + } + if (!epctrl || !epread || !epwrite) + return -ENODEV; + else + goto made_compressed_probe; + } + +skip_normal_probe: + + /*workaround for switched interfaces */ + if (data_interface->cur_altsetting->desc.bInterfaceClass + != CDC_DATA_INTERFACE_TYPE) { + if (control_interface->cur_altsetting->desc.bInterfaceClass + == CDC_DATA_INTERFACE_TYPE) { + struct usb_interface *t; + dev_dbg(&intf->dev, + "Your device has switched interfaces.\n"); + t = control_interface; + control_interface = data_interface; + data_interface = t; + } else { + return -EINVAL; + } + } + + /* Accept probe requests only for the control interface */ + if (!combined_interfaces && intf != control_interface) + return -ENODEV; + + if (!combined_interfaces && usb_interface_claimed(data_interface)) { + /* valid in this context */ + dev_dbg(&intf->dev, "The data interface isn't available\n"); + return -EBUSY; + } + + + if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 || + control_interface->cur_altsetting->desc.bNumEndpoints == 0) + return -EINVAL; + + epctrl = &control_interface->cur_altsetting->endpoint[0].desc; + epread = &data_interface->cur_altsetting->endpoint[0].desc; + epwrite = &data_interface->cur_altsetting->endpoint[1].desc; + + + /* workaround for switched endpoints */ + if (!usb_endpoint_dir_in(epread)) { + /* descriptors are swapped */ + struct usb_endpoint_descriptor *t; + dev_dbg(&intf->dev, + "The data interface has switched endpoints\n"); + t = epread; + epread = epwrite; + epwrite = t; + } +made_compressed_probe: + dev_dbg(&intf->dev, "interfaces are valid\n"); + + xr_usb_serial = kzalloc(sizeof(struct xr_usb_serial), GFP_KERNEL); + if (xr_usb_serial == NULL) { + dev_err(&intf->dev, "out of memory (xr_usb_serial kzalloc)\n"); + goto alloc_fail; + } + + minor = xr_usb_serial_alloc_minor(xr_usb_serial); + if (minor == XR_USB_SERIAL_TTY_MINORS) { + dev_err(&intf->dev, "no more free xr_usb_serial devices\n"); + kfree(xr_usb_serial); + return -ENODEV; + } + + ctrlsize = usb_endpoint_maxp(epctrl); + readsize = usb_endpoint_maxp(epread) * + (quirks == SINGLE_RX_URB ? 1 : 2); + xr_usb_serial->combined_interfaces = combined_interfaces; + xr_usb_serial->writesize = usb_endpoint_maxp(epwrite) * 20; + xr_usb_serial->control = control_interface; + xr_usb_serial->data = data_interface; + xr_usb_serial->minor = minor; + xr_usb_serial->dev = usb_dev; + xr_usb_serial->ctrl_caps = ac_management_function; + if (quirks & NO_CAP_LINE) + xr_usb_serial->ctrl_caps &= ~USB_CDC_CAP_LINE; + xr_usb_serial->ctrlsize = ctrlsize; + xr_usb_serial->readsize = readsize; + xr_usb_serial->rx_buflimit = num_rx_buf; + INIT_WORK(&xr_usb_serial->work, xr_usb_serial_softint); + spin_lock_init(&xr_usb_serial->write_lock); + spin_lock_init(&xr_usb_serial->read_lock); + mutex_init(&xr_usb_serial->mutex); + xr_usb_serial->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); + xr_usb_serial->is_int_ep = usb_endpoint_xfer_int(epread); + if (xr_usb_serial->is_int_ep) + xr_usb_serial->bInterval = epread->bInterval; + tty_port_init(&xr_usb_serial->port); + xr_usb_serial->port.ops = &xr_usb_serial_port_ops; + xr_usb_serial->DeviceVendor = id->idVendor; + xr_usb_serial->DeviceProduct = id->idProduct; + #if 0 + if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1410) + {//map the serial port A B C D to blocknum 0 1 2 3 for the xr21v141x device + xr_usb_serial->channel = epwrite->bEndpointAddress - 1; + } + else if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1420) + {//map the serial port A B C D to blocknum 0 2 4 6 for the xr21B142x device + xr_usb_serial->channel = (epwrite->bEndpointAddress - 4)*2; + } + else + { + xr_usb_serial->channel = epwrite->bEndpointAddress; + } + #else + xr_usb_serial->channel = epwrite->bEndpointAddress; + dev_dbg(&intf->dev, "epwrite->bEndpointAddress =%d\n",epwrite->bEndpointAddress); + #endif + buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &xr_usb_serial->ctrl_dma); + if (!buf) { + dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n"); + goto alloc_fail2; + } + xr_usb_serial->ctrl_buffer = buf; + + if (xr_usb_serial_write_buffers_alloc(xr_usb_serial) < 0) { + dev_err(&intf->dev, "out of memory (write buffer alloc)\n"); + goto alloc_fail4; + } + + xr_usb_serial->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); + if (!xr_usb_serial->ctrlurb) { + dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); + goto alloc_fail5; + } + for (i = 0; i < num_rx_buf; i++) { + struct xr_usb_serial_rb *rb = &(xr_usb_serial->read_buffers[i]); + struct urb *urb; + + rb->base = usb_alloc_coherent(xr_usb_serial->dev, readsize, GFP_KERNEL, + &rb->dma); + if (!rb->base) { + dev_err(&intf->dev, "out of memory " + "(read bufs usb_alloc_coherent)\n"); + goto alloc_fail6; + } + rb->index = i; + rb->instance = xr_usb_serial; + + urb = usb_alloc_urb(0, GFP_KERNEL); + if (!urb) { + dev_err(&intf->dev, + "out of memory (read urbs usb_alloc_urb)\n"); + goto alloc_fail6; + } + urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + urb->transfer_dma = rb->dma; + if (xr_usb_serial->is_int_ep) { + usb_fill_int_urb(urb, xr_usb_serial->dev, + xr_usb_serial->rx_endpoint, + rb->base, + xr_usb_serial->readsize, + xr_usb_serial_read_bulk_callback, rb, + xr_usb_serial->bInterval); + } else { + usb_fill_bulk_urb(urb, xr_usb_serial->dev, + xr_usb_serial->rx_endpoint, + rb->base, + xr_usb_serial->readsize, + xr_usb_serial_read_bulk_callback, rb); + } + + xr_usb_serial->read_urbs[i] = urb; + __set_bit(i, &xr_usb_serial->read_urbs_free); + } + for (i = 0; i < XR_USB_SERIAL_NW; i++) { + struct xr_usb_serial_wb *snd = &(xr_usb_serial->wb[i]); + + snd->urb = usb_alloc_urb(0, GFP_KERNEL); + if (snd->urb == NULL) { + dev_err(&intf->dev, + "out of memory (write urbs usb_alloc_urb)\n"); + goto alloc_fail7; + } + + if (usb_endpoint_xfer_int(epwrite)) + usb_fill_int_urb(snd->urb, usb_dev, +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), +#else + usb_sndintpipe(usb_dev, epwrite->bEndpointAddress), +#endif + NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd, epwrite->bInterval); + else + usb_fill_bulk_urb(snd->urb, usb_dev, + usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), + NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd); + snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + snd->instance = xr_usb_serial; + } + + usb_set_intfdata(intf, xr_usb_serial); + + i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); + if (i < 0) + goto alloc_fail7; + + if (cfd) { /* export the country data */ + xr_usb_serial->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL); + if (!xr_usb_serial->country_codes) + goto skip_countries; + xr_usb_serial->country_code_size = cfd->bLength - 4; + memcpy(xr_usb_serial->country_codes, (u8 *)&cfd->wCountyCode0, + cfd->bLength - 4); + xr_usb_serial->country_rel_date = cfd->iCountryCodeRelDate; + + i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); + if (i < 0) { + kfree(xr_usb_serial->country_codes); + xr_usb_serial->country_codes = NULL; + xr_usb_serial->country_code_size = 0; + goto skip_countries; + } + + i = device_create_file(&intf->dev, + &dev_attr_iCountryCodeRelDate); + if (i < 0) { + device_remove_file(&intf->dev, &dev_attr_wCountryCodes); + kfree(xr_usb_serial->country_codes); + xr_usb_serial->country_codes = NULL; + xr_usb_serial->country_code_size = 0; + goto skip_countries; + } + } + +skip_countries: + usb_fill_int_urb(xr_usb_serial->ctrlurb, usb_dev, + usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), + xr_usb_serial->ctrl_buffer, ctrlsize, xr_usb_serial_ctrl_irq, xr_usb_serial, + /* works around buggy devices */ + epctrl->bInterval ? epctrl->bInterval : 0xff); + xr_usb_serial->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + xr_usb_serial->ctrlurb->transfer_dma = xr_usb_serial->ctrl_dma; + + dev_info(&intf->dev, "ttyXR_USB_SERIAL%d: USB XR_USB_SERIAL device\n", minor); + + xr_usb_serial_pre_setup(xr_usb_serial); + + xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout); + + xr_usb_serial->line.dwDTERate = cpu_to_le32(9600); + xr_usb_serial->line.bDataBits = 8; + xr_usb_serial_set_line(xr_usb_serial, &xr_usb_serial->line); + + usb_driver_claim_interface(&xr_usb_serial_driver, data_interface, xr_usb_serial); + usb_set_intfdata(data_interface, xr_usb_serial); + + usb_get_intf(control_interface); +#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) + tty_register_device(xr_usb_serial_tty_driver, minor, &control_interface->dev); +#else + tty_dev = tty_port_register_device(&xr_usb_serial->port, xr_usb_serial_tty_driver, minor, + &control_interface->dev); + if (IS_ERR(tty_dev)) { + rv = PTR_ERR(tty_dev); + goto alloc_fail8; + } +#endif + + return 0; +alloc_fail8: + if (xr_usb_serial->country_codes) { + device_remove_file(&xr_usb_serial->control->dev, + &dev_attr_wCountryCodes); + device_remove_file(&xr_usb_serial->control->dev, + &dev_attr_iCountryCodeRelDate); + } + device_remove_file(&xr_usb_serial->control->dev, &dev_attr_bmCapabilities); +alloc_fail7: + usb_set_intfdata(intf, NULL); + for (i = 0; i < XR_USB_SERIAL_NW; i++) + usb_free_urb(xr_usb_serial->wb[i].urb); +alloc_fail6: + for (i = 0; i < num_rx_buf; i++) + usb_free_urb(xr_usb_serial->read_urbs[i]); + xr_usb_serial_read_buffers_free(xr_usb_serial); + usb_free_urb(xr_usb_serial->ctrlurb); +alloc_fail5: + xr_usb_serial_write_buffers_free(xr_usb_serial); +alloc_fail4: + usb_free_coherent(usb_dev, ctrlsize, xr_usb_serial->ctrl_buffer, xr_usb_serial->ctrl_dma); +alloc_fail2: + xr_usb_serial_release_minor(xr_usb_serial); + kfree(xr_usb_serial); +alloc_fail: + return rv; +} + +static void stop_data_traffic(struct xr_usb_serial *xr_usb_serial) +{ + int i; + + dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__); + + usb_kill_urb(xr_usb_serial->ctrlurb); + for (i = 0; i < XR_USB_SERIAL_NW; i++) + usb_kill_urb(xr_usb_serial->wb[i].urb); + for (i = 0; i < xr_usb_serial->rx_buflimit; i++) + usb_kill_urb(xr_usb_serial->read_urbs[i]); + + cancel_work_sync(&xr_usb_serial->work); +} + +static void xr_usb_serial_disconnect(struct usb_interface *intf) +{ + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + struct usb_device *usb_dev = interface_to_usbdev(intf); + struct tty_struct *tty; + int i; + + dev_dbg(&intf->dev, "%s\n", __func__); + + /* sibling interface is already cleaning up */ + if (!xr_usb_serial) + return; + + mutex_lock(&xr_usb_serial->mutex); + xr_usb_serial->disconnected = true; + if (xr_usb_serial->country_codes) { + device_remove_file(&xr_usb_serial->control->dev, + &dev_attr_wCountryCodes); + device_remove_file(&xr_usb_serial->control->dev, + &dev_attr_iCountryCodeRelDate); + } + device_remove_file(&xr_usb_serial->control->dev, &dev_attr_bmCapabilities); + usb_set_intfdata(xr_usb_serial->control, NULL); + usb_set_intfdata(xr_usb_serial->data, NULL); + mutex_unlock(&xr_usb_serial->mutex); + + tty = tty_port_tty_get(&xr_usb_serial->port); + if (tty) { + tty_vhangup(tty); + tty_kref_put(tty); + } + stop_data_traffic(xr_usb_serial); +#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 0) + tty_unregister_device(xr_usb_serial_tty_driver, xr_usb_serial->minor); +#endif + + usb_free_urb(xr_usb_serial->ctrlurb); + for (i = 0; i < XR_USB_SERIAL_NW; i++) + usb_free_urb(xr_usb_serial->wb[i].urb); + for (i = 0; i < xr_usb_serial->rx_buflimit; i++) + usb_free_urb(xr_usb_serial->read_urbs[i]); + xr_usb_serial_write_buffers_free(xr_usb_serial); + usb_free_coherent(usb_dev, xr_usb_serial->ctrlsize, xr_usb_serial->ctrl_buffer, xr_usb_serial->ctrl_dma); + xr_usb_serial_read_buffers_free(xr_usb_serial); + + if (!xr_usb_serial->combined_interfaces) + usb_driver_release_interface(&xr_usb_serial_driver, intf == xr_usb_serial->control ? + xr_usb_serial->data : xr_usb_serial->control); + + tty_port_put(&xr_usb_serial->port); +} + +#ifdef CONFIG_PM +static int xr_usb_serial_suspend(struct usb_interface *intf, pm_message_t message) +{ + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + int cnt; + + if (PMSG_IS_AUTO(message)) { + int b; + + spin_lock_irq(&xr_usb_serial->write_lock); + b = xr_usb_serial->transmitting; + spin_unlock_irq(&xr_usb_serial->write_lock); + if (b) + return -EBUSY; + } + + spin_lock_irq(&xr_usb_serial->read_lock); + spin_lock(&xr_usb_serial->write_lock); + cnt = xr_usb_serial->susp_count++; + spin_unlock(&xr_usb_serial->write_lock); + spin_unlock_irq(&xr_usb_serial->read_lock); + + if (cnt) + return 0; + + if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags)) + stop_data_traffic(xr_usb_serial); + + return 0; +} + +static int xr_usb_serial_resume(struct usb_interface *intf) +{ + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + struct xr_usb_serial_wb *wb; + int rv = 0; + int cnt; + + spin_lock_irq(&xr_usb_serial->read_lock); + xr_usb_serial->susp_count -= 1; + cnt = xr_usb_serial->susp_count; + spin_unlock_irq(&xr_usb_serial->read_lock); + + if (cnt) + return 0; + + if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags)) { + rv = usb_submit_urb(xr_usb_serial->ctrlurb, GFP_NOIO); + + spin_lock_irq(&xr_usb_serial->write_lock); + if (xr_usb_serial->delayed_wb) { + wb = xr_usb_serial->delayed_wb; + xr_usb_serial->delayed_wb = NULL; + spin_unlock_irq(&xr_usb_serial->write_lock); + xr_usb_serial_start_wb(xr_usb_serial, wb); + } else { + spin_unlock_irq(&xr_usb_serial->write_lock); + } + + /* + * delayed error checking because we must + * do the write path at all cost + */ + if (rv < 0) + goto err_out; + + rv = xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_NOIO); + } + +err_out: + return rv; +} + +static int xr_usb_serial_reset_resume(struct usb_interface *intf) +{ + struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf); + struct tty_struct *tty; + if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags)){ +#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0) + tty_port_tty_hangup(&xr_usb_serial->port, false); +#else + tty = tty_port_tty_get(&xr_usb_serial->port); + if (tty) { + tty_hangup(tty); + tty_kref_put(tty); + } +#endif + } + return xr_usb_serial_resume(intf); +} + +#endif /* CONFIG_PM */ + +/* + * USB driver structure. + */ +static const struct usb_device_id xr_usb_serial_ids[] = { + { USB_DEVICE(0x04e2, 0x1410)}, + { USB_DEVICE(0x04e2, 0x1411)}, + { USB_DEVICE(0x04e2, 0x1412)}, + { USB_DEVICE(0x04e2, 0x1414)}, + { USB_DEVICE(0x04e2, 0x1420)}, + { USB_DEVICE(0x04e2, 0x1421)}, + { USB_DEVICE(0x04e2, 0x1422)}, + { USB_DEVICE(0x04e2, 0x1424)}, + { USB_DEVICE(0x04e2, 0x1400)}, + { USB_DEVICE(0x04e2, 0x1401)}, + { USB_DEVICE(0x04e2, 0x1402)}, + { USB_DEVICE(0x04e2, 0x1403)}, + { } +}; + +MODULE_DEVICE_TABLE(usb, xr_usb_serial_ids); + +static struct usb_driver xr_usb_serial_driver = { + .name = "cdc_xr_usb_serial", + .probe = xr_usb_serial_probe, + .disconnect = xr_usb_serial_disconnect, +#ifdef CONFIG_PM + .suspend = xr_usb_serial_suspend, + .resume = xr_usb_serial_resume, + .reset_resume = xr_usb_serial_reset_resume, +#endif + .id_table = xr_usb_serial_ids, +#ifdef CONFIG_PM + .supports_autosuspend = 1, +#endif + .disable_hub_initiated_lpm = 1, +}; + +/* + * TTY driver structures. + */ + +static const struct tty_operations xr_usb_serial_ops = { + .install = xr_usb_serial_tty_install, + .open = xr_usb_serial_tty_open, + .close = xr_usb_serial_tty_close, + .cleanup = xr_usb_serial_tty_cleanup, + .hangup = xr_usb_serial_tty_hangup, + .write = xr_usb_serial_tty_write, + .write_room = xr_usb_serial_tty_write_room, + .ioctl = xr_usb_serial_tty_ioctl, + .throttle = xr_usb_serial_tty_throttle, + .unthrottle = xr_usb_serial_tty_unthrottle, + .chars_in_buffer = xr_usb_serial_tty_chars_in_buffer, + .break_ctl = xr_usb_serial_tty_break_ctl, + .set_termios = xr_usb_serial_tty_set_termios, + .tiocmget = xr_usb_serial_tty_tiocmget, + .tiocmset = xr_usb_serial_tty_tiocmset, +}; + +/* + * Init / exit. + */ + +static int __init xr_usb_serial_init(void) +{ + int retval; + xr_usb_serial_tty_driver = alloc_tty_driver(XR_USB_SERIAL_TTY_MINORS); + if (!xr_usb_serial_tty_driver) + return -ENOMEM; + xr_usb_serial_tty_driver->driver_name = "xr_usb_serial", + xr_usb_serial_tty_driver->name = "ttyXRUSB", + xr_usb_serial_tty_driver->major = XR_USB_SERIAL_TTY_MAJOR, + xr_usb_serial_tty_driver->minor_start = 0, + xr_usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, + xr_usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL, + xr_usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; + xr_usb_serial_tty_driver->init_termios = tty_std_termios; + xr_usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | + HUPCL | CLOCAL; + tty_set_operations(xr_usb_serial_tty_driver, &xr_usb_serial_ops); + + retval = tty_register_driver(xr_usb_serial_tty_driver); + if (retval) { + put_tty_driver(xr_usb_serial_tty_driver); + return retval; + } + + retval = usb_register(&xr_usb_serial_driver); + if (retval) { + tty_unregister_driver(xr_usb_serial_tty_driver); + put_tty_driver(xr_usb_serial_tty_driver); + return retval; + } + + printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n"); + + return 0; +} + +static void __exit xr_usb_serial_exit(void) +{ + usb_deregister(&xr_usb_serial_driver); + tty_unregister_driver(xr_usb_serial_tty_driver); + put_tty_driver(xr_usb_serial_tty_driver); +} + +module_init(xr_usb_serial_init); +module_exit(xr_usb_serial_exit); + +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_LICENSE("GPL"); +MODULE_ALIAS_CHARDEV_MAJOR(XR_USB_SERIAL_TTY_MAJOR); diff --git a/ubuntu/xr-usb-serial/xr_usb_serial_common.h b/ubuntu/xr-usb-serial/xr_usb_serial_common.h new file mode 100644 index 000000000000..4ba22d947999 --- /dev/null +++ b/ubuntu/xr-usb-serial/xr_usb_serial_common.h @@ -0,0 +1,186 @@ +/* + * 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 + +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; + unsigned short DeviceVendor; + unsigned short DeviceProduct; + struct reg_addr_map reg_map; +}; + +#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 + + + + diff --git a/ubuntu/xr-usb-serial/xr_usb_serial_hal.c b/ubuntu/xr-usb-serial/xr_usb_serial_hal.c new file mode 100644 index 000000000000..0fcc580002bd --- /dev/null +++ b/ubuntu/xr-usb-serial/xr_usb_serial_hal.c @@ -0,0 +1,717 @@ +/* + * 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_dbg(&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_dbg(&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_dbg(&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_dbg(&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; + + 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 */ + value, /* data */ + 2, /* 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_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 */ + value, /* data */ + 1, /* size */ + 5000); /* timeout */ + } + 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 */ + value, /* data */ + 2, /* 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_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 */ + value, /* data */ + 2, /* size */ + 5000); /* timeout */ + } + else + { + result = -1; + } + + if(result < 0) + dev_dbg(&xr_usb_serial->control->dev, "%s channel:%d Reg 0x%x Error:%d\n", __func__,channel,regnum,result); + else + dev_dbg(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value); + + 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; + 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 */ + value, /* data */ + 2, /* size */ + 5000); /* timeout */ + + + + } + else if((xr_usb_serial->DeviceProduct == 0x1410) || + (xr_usb_serial->DeviceProduct == 0x1412) || + (xr_usb_serial->DeviceProduct == 0x1414)) + { + unsigned char reg_value = 0; + 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 */ + ®_value, /* data */ + 1, /* size */ + 5000); /* timeout */ + dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_get_reg_ext reg:%x\n",reg_value); + *value = reg_value; + } + 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 */ + value, /* data */ + 2, /* 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_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 */ + value, /* data */ + 2, /* size */ + 5000); /* timeout */ + } + else + { + result = -1; + } + + if(result < 0) + dev_dbg(&xr_usb_serial->control->dev, "%s Error:%d\n", __func__,result); + else + dev_dbg(&xr_usb_serial->control->dev, "%s channel:%d 0x%x = 0x%04x\n", __func__,channel,regnum, *value); + + 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_dbg(&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,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; + } + 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 + +int xr_usb_serial_enable(struct xr_usb_serial *xr_usb_serial) +{ + int ret = 0; + int channel = xr_usb_serial->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_disable(struct xr_usb_serial *xr_usb_serial) +{ + int ret = 0; + int channel = xr_usb_serial->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); + 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; +} + + +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_dbg(&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; + xr21b140x_reg_map.tx_break_addr = 0x0a; + xr21b140x_reg_map.uart_custom_driver = 0x60; + xr21b140x_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; + +} + + + diff --git a/ubuntu/xr-usb-serial/xr_usb_serial_ioctl.h b/ubuntu/xr-usb-serial/xr_usb_serial_ioctl.h new file mode 100644 index 000000000000..c28b6bdc0b16 --- /dev/null +++ b/ubuntu/xr-usb-serial/xr_usb_serial_ioctl.h @@ -0,0 +1,31 @@ +/* + * 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 + +#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 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 -- 2.39.5