149cdee0eSKees Lemmens /* 249cdee0eSKees Lemmens * Ours Technology Inc. OTi-6858 USB to serial adapter driver. 349cdee0eSKees Lemmens * 449cdee0eSKees Lemmens * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20) 549cdee0eSKees Lemmens * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail) 649cdee0eSKees Lemmens * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) 749cdee0eSKees Lemmens * Copyright (C) 2003 IBM Corp. 849cdee0eSKees Lemmens * 949cdee0eSKees Lemmens * Many thanks to the authors of pl2303 driver: all functions in this file 1049cdee0eSKees Lemmens * are heavily based on pl2303 code, buffering code is a 1-to-1 copy. 1149cdee0eSKees Lemmens * 1249cdee0eSKees Lemmens * Warning! You use this driver on your own risk! The only official 1349cdee0eSKees Lemmens * description of this device I have is datasheet from manufacturer, 1449cdee0eSKees Lemmens * and it doesn't contain almost any information needed to write a driver. 1549cdee0eSKees Lemmens * Almost all knowlegde used while writing this driver was gathered by: 1649cdee0eSKees Lemmens * - analyzing traffic between device and the M$ Windows 2000 driver, 1749cdee0eSKees Lemmens * - trying different bit combinations and checking pin states 1849cdee0eSKees Lemmens * with a voltmeter, 1949cdee0eSKees Lemmens * - receiving malformed frames and producing buffer overflows 2049cdee0eSKees Lemmens * to learn how errors are reported, 2149cdee0eSKees Lemmens * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE 2249cdee0eSKees Lemmens * CONNECTED TO IT! 2349cdee0eSKees Lemmens * 2449cdee0eSKees Lemmens * This program is free software; you can redistribute it and/or modify 2549cdee0eSKees Lemmens * it under the terms of the GNU General Public License as published by 2649cdee0eSKees Lemmens * the Free Software Foundation; either version 2 of the License. 2749cdee0eSKees Lemmens * 282a77c814SAlan Cox * See Documentation/usb/usb-serial.txt for more information on using this 292a77c814SAlan Cox * driver 3049cdee0eSKees Lemmens * 3149cdee0eSKees Lemmens * TODO: 3249cdee0eSKees Lemmens * - implement correct flushing for ioctls and oti6858_close() 3349cdee0eSKees Lemmens * - check how errors (rx overflow, parity error, framing error) are reported 3449cdee0eSKees Lemmens * - implement oti6858_break_ctl() 3549cdee0eSKees Lemmens * - implement more ioctls 3649cdee0eSKees Lemmens * - test/implement flow control 3749cdee0eSKees Lemmens * - allow setting custom baud rates 3849cdee0eSKees Lemmens */ 3949cdee0eSKees Lemmens 4049cdee0eSKees Lemmens #include <linux/kernel.h> 4149cdee0eSKees Lemmens #include <linux/errno.h> 4249cdee0eSKees Lemmens #include <linux/init.h> 4349cdee0eSKees Lemmens #include <linux/slab.h> 4449cdee0eSKees Lemmens #include <linux/tty.h> 4549cdee0eSKees Lemmens #include <linux/tty_driver.h> 4649cdee0eSKees Lemmens #include <linux/tty_flip.h> 4749cdee0eSKees Lemmens #include <linux/serial.h> 4849cdee0eSKees Lemmens #include <linux/module.h> 4949cdee0eSKees Lemmens #include <linux/moduleparam.h> 5049cdee0eSKees Lemmens #include <linux/spinlock.h> 5149cdee0eSKees Lemmens #include <linux/usb.h> 5249cdee0eSKees Lemmens #include <linux/usb/serial.h> 532a77c814SAlan Cox #include <linux/uaccess.h> 54e3c1803fSJohan Hovold #include <linux/kfifo.h> 5549cdee0eSKees Lemmens #include "oti6858.h" 5649cdee0eSKees Lemmens 5749cdee0eSKees Lemmens #define OTI6858_DESCRIPTION \ 5849cdee0eSKees Lemmens "Ours Technology Inc. OTi-6858 USB to serial adapter driver" 5949cdee0eSKees Lemmens #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>" 60e3c1803fSJohan Hovold #define OTI6858_VERSION "0.2" 6149cdee0eSKees Lemmens 627d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = { 6349cdee0eSKees Lemmens { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) }, 6449cdee0eSKees Lemmens { } 6549cdee0eSKees Lemmens }; 6649cdee0eSKees Lemmens 6749cdee0eSKees Lemmens MODULE_DEVICE_TABLE(usb, id_table); 6849cdee0eSKees Lemmens 6949cdee0eSKees Lemmens static struct usb_driver oti6858_driver = { 7049cdee0eSKees Lemmens .name = "oti6858", 7149cdee0eSKees Lemmens .probe = usb_serial_probe, 7249cdee0eSKees Lemmens .disconnect = usb_serial_disconnect, 7349cdee0eSKees Lemmens .id_table = id_table, 7449cdee0eSKees Lemmens .no_dynamic_id = 1, 7549cdee0eSKees Lemmens }; 7649cdee0eSKees Lemmens 7749cdee0eSKees Lemmens static int debug; 7849cdee0eSKees Lemmens 7949cdee0eSKees Lemmens /* requests */ 8049cdee0eSKees Lemmens #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00) 8149cdee0eSKees Lemmens #define OTI6858_REQ_T_GET_STATUS 0x01 8249cdee0eSKees Lemmens 8349cdee0eSKees Lemmens #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00) 8449cdee0eSKees Lemmens #define OTI6858_REQ_T_SET_LINE 0x00 8549cdee0eSKees Lemmens 8649cdee0eSKees Lemmens #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01) 8749cdee0eSKees Lemmens #define OTI6858_REQ_T_CHECK_TXBUFF 0x00 8849cdee0eSKees Lemmens 8949cdee0eSKees Lemmens /* format of the control packet */ 9049cdee0eSKees Lemmens struct oti6858_control_pkt { 91fd05e720SAl Viro __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */ 9249cdee0eSKees Lemmens #define OTI6858_MAX_BAUD_RATE 3000000 9349cdee0eSKees Lemmens u8 frame_fmt; 9449cdee0eSKees Lemmens #define FMT_STOP_BITS_MASK 0xc0 9549cdee0eSKees Lemmens #define FMT_STOP_BITS_1 0x00 9649cdee0eSKees Lemmens #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */ 9749cdee0eSKees Lemmens #define FMT_PARITY_MASK 0x38 9849cdee0eSKees Lemmens #define FMT_PARITY_NONE 0x00 9949cdee0eSKees Lemmens #define FMT_PARITY_ODD 0x08 10049cdee0eSKees Lemmens #define FMT_PARITY_EVEN 0x18 10149cdee0eSKees Lemmens #define FMT_PARITY_MARK 0x28 10249cdee0eSKees Lemmens #define FMT_PARITY_SPACE 0x38 10349cdee0eSKees Lemmens #define FMT_DATA_BITS_MASK 0x03 10449cdee0eSKees Lemmens #define FMT_DATA_BITS_5 0x00 10549cdee0eSKees Lemmens #define FMT_DATA_BITS_6 0x01 10649cdee0eSKees Lemmens #define FMT_DATA_BITS_7 0x02 10749cdee0eSKees Lemmens #define FMT_DATA_BITS_8 0x03 10849cdee0eSKees Lemmens u8 something; /* always equals 0x43 */ 10949cdee0eSKees Lemmens u8 control; /* settings of flow control lines */ 11049cdee0eSKees Lemmens #define CONTROL_MASK 0x0c 11149cdee0eSKees Lemmens #define CONTROL_DTR_HIGH 0x08 11249cdee0eSKees Lemmens #define CONTROL_RTS_HIGH 0x04 11349cdee0eSKees Lemmens u8 tx_status; 11449cdee0eSKees Lemmens #define TX_BUFFER_EMPTIED 0x09 11549cdee0eSKees Lemmens u8 pin_state; 11649cdee0eSKees Lemmens #define PIN_MASK 0x3f 11749cdee0eSKees Lemmens #define PIN_RTS 0x20 /* output pin */ 11849cdee0eSKees Lemmens #define PIN_CTS 0x10 /* input pin, active low */ 11949cdee0eSKees Lemmens #define PIN_DSR 0x08 /* input pin, active low */ 12049cdee0eSKees Lemmens #define PIN_DTR 0x04 /* output pin */ 12149cdee0eSKees Lemmens #define PIN_RI 0x02 /* input pin, active low */ 12249cdee0eSKees Lemmens #define PIN_DCD 0x01 /* input pin, active low */ 12349cdee0eSKees Lemmens u8 rx_bytes_avail; /* number of bytes in rx buffer */; 12449cdee0eSKees Lemmens }; 12549cdee0eSKees Lemmens 12649cdee0eSKees Lemmens #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt) 12749cdee0eSKees Lemmens #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \ 12849cdee0eSKees Lemmens (((a)->divisor == (priv)->pending_setup.divisor) \ 12949cdee0eSKees Lemmens && ((a)->control == (priv)->pending_setup.control) \ 13049cdee0eSKees Lemmens && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt)) 13149cdee0eSKees Lemmens 13249cdee0eSKees Lemmens /* function prototypes */ 133a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port); 134335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port); 13595da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty, 13695da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old); 137fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty); 13895da310eSAlan Cox static int oti6858_ioctl(struct tty_struct *tty, struct file *file, 13949cdee0eSKees Lemmens unsigned int cmd, unsigned long arg); 14049cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb); 14149cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb); 14249cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb); 14395da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port, 14449cdee0eSKees Lemmens const unsigned char *buf, int count); 14595da310eSAlan Cox static int oti6858_write_room(struct tty_struct *tty); 14695da310eSAlan Cox static int oti6858_chars_in_buffer(struct tty_struct *tty); 14795da310eSAlan Cox static int oti6858_tiocmget(struct tty_struct *tty, struct file *file); 14895da310eSAlan Cox static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, 14949cdee0eSKees Lemmens unsigned int set, unsigned int clear); 15049cdee0eSKees Lemmens static int oti6858_startup(struct usb_serial *serial); 151f9c99bb8SAlan Stern static void oti6858_release(struct usb_serial *serial); 15249cdee0eSKees Lemmens 15349cdee0eSKees Lemmens /* device info */ 15449cdee0eSKees Lemmens static struct usb_serial_driver oti6858_device = { 15549cdee0eSKees Lemmens .driver = { 15649cdee0eSKees Lemmens .owner = THIS_MODULE, 15749cdee0eSKees Lemmens .name = "oti6858", 15849cdee0eSKees Lemmens }, 15949cdee0eSKees Lemmens .id_table = id_table, 16049cdee0eSKees Lemmens .num_ports = 1, 16149cdee0eSKees Lemmens .open = oti6858_open, 16249cdee0eSKees Lemmens .close = oti6858_close, 16349cdee0eSKees Lemmens .write = oti6858_write, 16449cdee0eSKees Lemmens .ioctl = oti6858_ioctl, 16549cdee0eSKees Lemmens .set_termios = oti6858_set_termios, 166fe1ae7fdSAlan Cox .init_termios = oti6858_init_termios, 16749cdee0eSKees Lemmens .tiocmget = oti6858_tiocmget, 16849cdee0eSKees Lemmens .tiocmset = oti6858_tiocmset, 16949cdee0eSKees Lemmens .read_bulk_callback = oti6858_read_bulk_callback, 17049cdee0eSKees Lemmens .read_int_callback = oti6858_read_int_callback, 17149cdee0eSKees Lemmens .write_bulk_callback = oti6858_write_bulk_callback, 17249cdee0eSKees Lemmens .write_room = oti6858_write_room, 17349cdee0eSKees Lemmens .chars_in_buffer = oti6858_chars_in_buffer, 17449cdee0eSKees Lemmens .attach = oti6858_startup, 175f9c99bb8SAlan Stern .release = oti6858_release, 17649cdee0eSKees Lemmens }; 17749cdee0eSKees Lemmens 17849cdee0eSKees Lemmens struct oti6858_private { 17949cdee0eSKees Lemmens spinlock_t lock; 18049cdee0eSKees Lemmens 18149cdee0eSKees Lemmens struct oti6858_control_pkt status; 18249cdee0eSKees Lemmens 18349cdee0eSKees Lemmens struct { 18449cdee0eSKees Lemmens u8 read_urb_in_use; 18549cdee0eSKees Lemmens u8 write_urb_in_use; 18649cdee0eSKees Lemmens } flags; 18749cdee0eSKees Lemmens struct delayed_work delayed_write_work; 18849cdee0eSKees Lemmens 18949cdee0eSKees Lemmens struct { 190fd05e720SAl Viro __le16 divisor; 19149cdee0eSKees Lemmens u8 frame_fmt; 19249cdee0eSKees Lemmens u8 control; 19349cdee0eSKees Lemmens } pending_setup; 19449cdee0eSKees Lemmens u8 transient; 19549cdee0eSKees Lemmens u8 setup_done; 19649cdee0eSKees Lemmens struct delayed_work delayed_setup_work; 19749cdee0eSKees Lemmens 19849cdee0eSKees Lemmens wait_queue_head_t intr_wait; 19949cdee0eSKees Lemmens struct usb_serial_port *port; /* USB port with which associated */ 20049cdee0eSKees Lemmens }; 20149cdee0eSKees Lemmens 20249cdee0eSKees Lemmens static void setup_line(struct work_struct *work) 20349cdee0eSKees Lemmens { 2042a77c814SAlan Cox struct oti6858_private *priv = container_of(work, 2052a77c814SAlan Cox struct oti6858_private, delayed_setup_work.work); 20649cdee0eSKees Lemmens struct usb_serial_port *port = priv->port; 20749cdee0eSKees Lemmens struct oti6858_control_pkt *new_setup; 20849cdee0eSKees Lemmens unsigned long flags; 20949cdee0eSKees Lemmens int result; 21049cdee0eSKees Lemmens 211441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 21249cdee0eSKees Lemmens 2132a77c814SAlan Cox new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL); 2142a77c814SAlan Cox if (new_setup == NULL) { 215441b62c1SHarvey Harrison dev_err(&port->dev, "%s(): out of memory!\n", __func__); 21649cdee0eSKees Lemmens /* we will try again */ 2172a77c814SAlan Cox schedule_delayed_work(&priv->delayed_setup_work, 2182a77c814SAlan Cox msecs_to_jiffies(2)); 21949cdee0eSKees Lemmens return; 22049cdee0eSKees Lemmens } 22149cdee0eSKees Lemmens 22249cdee0eSKees Lemmens result = usb_control_msg(port->serial->dev, 22349cdee0eSKees Lemmens usb_rcvctrlpipe(port->serial->dev, 0), 22449cdee0eSKees Lemmens OTI6858_REQ_T_GET_STATUS, 22549cdee0eSKees Lemmens OTI6858_REQ_GET_STATUS, 22649cdee0eSKees Lemmens 0, 0, 22749cdee0eSKees Lemmens new_setup, OTI6858_CTRL_PKT_SIZE, 22849cdee0eSKees Lemmens 100); 22949cdee0eSKees Lemmens 23049cdee0eSKees Lemmens if (result != OTI6858_CTRL_PKT_SIZE) { 231441b62c1SHarvey Harrison dev_err(&port->dev, "%s(): error reading status\n", __func__); 23249cdee0eSKees Lemmens kfree(new_setup); 23349cdee0eSKees Lemmens /* we will try again */ 2342a77c814SAlan Cox schedule_delayed_work(&priv->delayed_setup_work, 2352a77c814SAlan Cox msecs_to_jiffies(2)); 23649cdee0eSKees Lemmens return; 23749cdee0eSKees Lemmens } 23849cdee0eSKees Lemmens 23949cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 24049cdee0eSKees Lemmens if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) { 24149cdee0eSKees Lemmens new_setup->divisor = priv->pending_setup.divisor; 24249cdee0eSKees Lemmens new_setup->control = priv->pending_setup.control; 24349cdee0eSKees Lemmens new_setup->frame_fmt = priv->pending_setup.frame_fmt; 24449cdee0eSKees Lemmens 24549cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 24649cdee0eSKees Lemmens result = usb_control_msg(port->serial->dev, 24749cdee0eSKees Lemmens usb_sndctrlpipe(port->serial->dev, 0), 24849cdee0eSKees Lemmens OTI6858_REQ_T_SET_LINE, 24949cdee0eSKees Lemmens OTI6858_REQ_SET_LINE, 25049cdee0eSKees Lemmens 0, 0, 25149cdee0eSKees Lemmens new_setup, OTI6858_CTRL_PKT_SIZE, 25249cdee0eSKees Lemmens 100); 25349cdee0eSKees Lemmens } else { 25449cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 25549cdee0eSKees Lemmens result = 0; 25649cdee0eSKees Lemmens } 25749cdee0eSKees Lemmens kfree(new_setup); 25849cdee0eSKees Lemmens 25949cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 26049cdee0eSKees Lemmens if (result != OTI6858_CTRL_PKT_SIZE) 26149cdee0eSKees Lemmens priv->transient = 0; 26249cdee0eSKees Lemmens priv->setup_done = 1; 26349cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 26449cdee0eSKees Lemmens 265441b62c1SHarvey Harrison dbg("%s(): submitting interrupt urb", __func__); 26649cdee0eSKees Lemmens port->interrupt_in_urb->dev = port->serial->dev; 26740d28582SOliver Neukum result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 26849cdee0eSKees Lemmens if (result != 0) { 26949cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed" 270441b62c1SHarvey Harrison " with error %d\n", __func__, result); 27149cdee0eSKees Lemmens } 27249cdee0eSKees Lemmens } 27349cdee0eSKees Lemmens 2747d7917bcSBill Pemberton static void send_data(struct work_struct *work) 27549cdee0eSKees Lemmens { 2762a77c814SAlan Cox struct oti6858_private *priv = container_of(work, 2772a77c814SAlan Cox struct oti6858_private, delayed_write_work.work); 27849cdee0eSKees Lemmens struct usb_serial_port *port = priv->port; 27949cdee0eSKees Lemmens int count = 0, result; 28049cdee0eSKees Lemmens unsigned long flags; 281d2126326SJohan Hovold u8 *allow; 28249cdee0eSKees Lemmens 283441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 28449cdee0eSKees Lemmens 28549cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 28649cdee0eSKees Lemmens if (priv->flags.write_urb_in_use) { 28749cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 2882a77c814SAlan Cox schedule_delayed_work(&priv->delayed_write_work, 2892a77c814SAlan Cox msecs_to_jiffies(2)); 29049cdee0eSKees Lemmens return; 29149cdee0eSKees Lemmens } 29249cdee0eSKees Lemmens priv->flags.write_urb_in_use = 1; 29349cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 29428f27dcbSJohan Hovold 29528f27dcbSJohan Hovold spin_lock_irqsave(&port->lock, flags); 29628f27dcbSJohan Hovold count = kfifo_len(&port->write_fifo); 29728f27dcbSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 29828f27dcbSJohan Hovold 29949cdee0eSKees Lemmens if (count > port->bulk_out_size) 30049cdee0eSKees Lemmens count = port->bulk_out_size; 30149cdee0eSKees Lemmens 30249cdee0eSKees Lemmens if (count != 0) { 303d2126326SJohan Hovold allow = kmalloc(1, GFP_KERNEL); 304d2126326SJohan Hovold if (!allow) { 305d2126326SJohan Hovold dev_err(&port->dev, "%s(): kmalloc failed\n", 306d2126326SJohan Hovold __func__); 307d2126326SJohan Hovold return; 308d2126326SJohan Hovold } 30949cdee0eSKees Lemmens result = usb_control_msg(port->serial->dev, 31049cdee0eSKees Lemmens usb_rcvctrlpipe(port->serial->dev, 0), 31149cdee0eSKees Lemmens OTI6858_REQ_T_CHECK_TXBUFF, 31249cdee0eSKees Lemmens OTI6858_REQ_CHECK_TXBUFF, 313d2126326SJohan Hovold count, 0, allow, 1, 100); 314d2126326SJohan Hovold if (result != 1 || *allow != 0) 31549cdee0eSKees Lemmens count = 0; 316d2126326SJohan Hovold kfree(allow); 31749cdee0eSKees Lemmens } 31849cdee0eSKees Lemmens 31949cdee0eSKees Lemmens if (count == 0) { 32049cdee0eSKees Lemmens priv->flags.write_urb_in_use = 0; 32149cdee0eSKees Lemmens 322441b62c1SHarvey Harrison dbg("%s(): submitting interrupt urb", __func__); 32349cdee0eSKees Lemmens port->interrupt_in_urb->dev = port->serial->dev; 32440d28582SOliver Neukum result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 32549cdee0eSKees Lemmens if (result != 0) { 32649cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed" 327441b62c1SHarvey Harrison " with error %d\n", __func__, result); 32849cdee0eSKees Lemmens } 32949cdee0eSKees Lemmens return; 33049cdee0eSKees Lemmens } 33149cdee0eSKees Lemmens 33228f27dcbSJohan Hovold count = kfifo_out_locked(&port->write_fifo, 333e3c1803fSJohan Hovold port->write_urb->transfer_buffer, 33428f27dcbSJohan Hovold count, &port->lock); 33549cdee0eSKees Lemmens port->write_urb->transfer_buffer_length = count; 33649cdee0eSKees Lemmens port->write_urb->dev = port->serial->dev; 33740d28582SOliver Neukum result = usb_submit_urb(port->write_urb, GFP_NOIO); 33849cdee0eSKees Lemmens if (result != 0) { 33949cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed" 340441b62c1SHarvey Harrison " with error %d\n", __func__, result); 34149cdee0eSKees Lemmens priv->flags.write_urb_in_use = 0; 34249cdee0eSKees Lemmens } 34349cdee0eSKees Lemmens 34449cdee0eSKees Lemmens usb_serial_port_softint(port); 34549cdee0eSKees Lemmens } 34649cdee0eSKees Lemmens 34749cdee0eSKees Lemmens static int oti6858_startup(struct usb_serial *serial) 34849cdee0eSKees Lemmens { 34949cdee0eSKees Lemmens struct usb_serial_port *port = serial->port[0]; 35049cdee0eSKees Lemmens struct oti6858_private *priv; 35149cdee0eSKees Lemmens int i; 35249cdee0eSKees Lemmens 35349cdee0eSKees Lemmens for (i = 0; i < serial->num_ports; ++i) { 35449cdee0eSKees Lemmens priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL); 35549cdee0eSKees Lemmens if (!priv) 35649cdee0eSKees Lemmens break; 35749cdee0eSKees Lemmens 35849cdee0eSKees Lemmens spin_lock_init(&priv->lock); 35949cdee0eSKees Lemmens init_waitqueue_head(&priv->intr_wait); 3602a77c814SAlan Cox /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */ 3612a77c814SAlan Cox /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */ 36249cdee0eSKees Lemmens priv->port = port; 36349cdee0eSKees Lemmens INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line); 36449cdee0eSKees Lemmens INIT_DELAYED_WORK(&priv->delayed_write_work, send_data); 36549cdee0eSKees Lemmens 36649cdee0eSKees Lemmens usb_set_serial_port_data(serial->port[i], priv); 36749cdee0eSKees Lemmens } 36849cdee0eSKees Lemmens if (i == serial->num_ports) 36949cdee0eSKees Lemmens return 0; 37049cdee0eSKees Lemmens 37149cdee0eSKees Lemmens for (--i; i >= 0; --i) { 37249cdee0eSKees Lemmens priv = usb_get_serial_port_data(serial->port[i]); 37349cdee0eSKees Lemmens kfree(priv); 37449cdee0eSKees Lemmens usb_set_serial_port_data(serial->port[i], NULL); 37549cdee0eSKees Lemmens } 37649cdee0eSKees Lemmens return -ENOMEM; 37749cdee0eSKees Lemmens } 37849cdee0eSKees Lemmens 37995da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port, 38049cdee0eSKees Lemmens const unsigned char *buf, int count) 38149cdee0eSKees Lemmens { 382441b62c1SHarvey Harrison dbg("%s(port = %d, count = %d)", __func__, port->number, count); 38349cdee0eSKees Lemmens 38449cdee0eSKees Lemmens if (!count) 38549cdee0eSKees Lemmens return count; 38649cdee0eSKees Lemmens 38728f27dcbSJohan Hovold count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); 38849cdee0eSKees Lemmens 38949cdee0eSKees Lemmens return count; 39049cdee0eSKees Lemmens } 39149cdee0eSKees Lemmens 39295da310eSAlan Cox static int oti6858_write_room(struct tty_struct *tty) 39349cdee0eSKees Lemmens { 39495da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 39549cdee0eSKees Lemmens int room = 0; 39649cdee0eSKees Lemmens unsigned long flags; 39749cdee0eSKees Lemmens 398441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 39949cdee0eSKees Lemmens 40028f27dcbSJohan Hovold spin_lock_irqsave(&port->lock, flags); 40128f27dcbSJohan Hovold room = kfifo_avail(&port->write_fifo); 40228f27dcbSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 40349cdee0eSKees Lemmens 40449cdee0eSKees Lemmens return room; 40549cdee0eSKees Lemmens } 40649cdee0eSKees Lemmens 40795da310eSAlan Cox static int oti6858_chars_in_buffer(struct tty_struct *tty) 40849cdee0eSKees Lemmens { 40995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 41049cdee0eSKees Lemmens int chars = 0; 41149cdee0eSKees Lemmens unsigned long flags; 41249cdee0eSKees Lemmens 413441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 41449cdee0eSKees Lemmens 41528f27dcbSJohan Hovold spin_lock_irqsave(&port->lock, flags); 41628f27dcbSJohan Hovold chars = kfifo_len(&port->write_fifo); 41728f27dcbSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 41849cdee0eSKees Lemmens 41949cdee0eSKees Lemmens return chars; 42049cdee0eSKees Lemmens } 42149cdee0eSKees Lemmens 422fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty) 423fe1ae7fdSAlan Cox { 424fe1ae7fdSAlan Cox *(tty->termios) = tty_std_termios; 425fe1ae7fdSAlan Cox tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL; 426fe1ae7fdSAlan Cox tty->termios->c_ispeed = 38400; 427fe1ae7fdSAlan Cox tty->termios->c_ospeed = 38400; 428fe1ae7fdSAlan Cox } 429fe1ae7fdSAlan Cox 43095da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty, 43195da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 43249cdee0eSKees Lemmens { 43349cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 43449cdee0eSKees Lemmens unsigned long flags; 43549cdee0eSKees Lemmens unsigned int cflag; 43649cdee0eSKees Lemmens u8 frame_fmt, control; 437fd05e720SAl Viro __le16 divisor; 43849cdee0eSKees Lemmens int br; 43949cdee0eSKees Lemmens 440441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 44149cdee0eSKees Lemmens 44295da310eSAlan Cox if (!tty) { 443441b62c1SHarvey Harrison dbg("%s(): no tty structures", __func__); 44449cdee0eSKees Lemmens return; 44549cdee0eSKees Lemmens } 44649cdee0eSKees Lemmens 44795da310eSAlan Cox cflag = tty->termios->c_cflag; 44849cdee0eSKees Lemmens 44949cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 45049cdee0eSKees Lemmens divisor = priv->pending_setup.divisor; 45149cdee0eSKees Lemmens frame_fmt = priv->pending_setup.frame_fmt; 45249cdee0eSKees Lemmens control = priv->pending_setup.control; 45349cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 45449cdee0eSKees Lemmens 45549cdee0eSKees Lemmens frame_fmt &= ~FMT_DATA_BITS_MASK; 45649cdee0eSKees Lemmens switch (cflag & CSIZE) { 45749cdee0eSKees Lemmens case CS5: 45849cdee0eSKees Lemmens frame_fmt |= FMT_DATA_BITS_5; 45949cdee0eSKees Lemmens break; 46049cdee0eSKees Lemmens case CS6: 46149cdee0eSKees Lemmens frame_fmt |= FMT_DATA_BITS_6; 46249cdee0eSKees Lemmens break; 46349cdee0eSKees Lemmens case CS7: 46449cdee0eSKees Lemmens frame_fmt |= FMT_DATA_BITS_7; 46549cdee0eSKees Lemmens break; 46649cdee0eSKees Lemmens default: 46749cdee0eSKees Lemmens case CS8: 46849cdee0eSKees Lemmens frame_fmt |= FMT_DATA_BITS_8; 46949cdee0eSKees Lemmens break; 47049cdee0eSKees Lemmens } 47149cdee0eSKees Lemmens 47249cdee0eSKees Lemmens /* manufacturer claims that this device can work with baud rates 47349cdee0eSKees Lemmens * up to 3 Mbps; I've tested it only on 115200 bps, so I can't 47449cdee0eSKees Lemmens * guarantee that any other baud rate will work (especially 47549cdee0eSKees Lemmens * the higher ones) 47649cdee0eSKees Lemmens */ 47795da310eSAlan Cox br = tty_get_baud_rate(tty); 47849cdee0eSKees Lemmens if (br == 0) { 47949cdee0eSKees Lemmens divisor = 0; 480b0a239daSAlan Cox } else { 48149cdee0eSKees Lemmens int real_br; 482fd05e720SAl Viro int new_divisor; 483b0a239daSAlan Cox br = min(br, OTI6858_MAX_BAUD_RATE); 48449cdee0eSKees Lemmens 485fd05e720SAl Viro new_divisor = (96000000 + 8 * br) / (16 * br); 486fd05e720SAl Viro real_br = 96000000 / (16 * new_divisor); 487fd05e720SAl Viro divisor = cpu_to_le16(new_divisor); 48895da310eSAlan Cox tty_encode_baud_rate(tty, real_br, real_br); 48949cdee0eSKees Lemmens } 49049cdee0eSKees Lemmens 49149cdee0eSKees Lemmens frame_fmt &= ~FMT_STOP_BITS_MASK; 4922a77c814SAlan Cox if ((cflag & CSTOPB) != 0) 49349cdee0eSKees Lemmens frame_fmt |= FMT_STOP_BITS_2; 4942a77c814SAlan Cox else 49549cdee0eSKees Lemmens frame_fmt |= FMT_STOP_BITS_1; 49649cdee0eSKees Lemmens 49749cdee0eSKees Lemmens frame_fmt &= ~FMT_PARITY_MASK; 49849cdee0eSKees Lemmens if ((cflag & PARENB) != 0) { 4992a77c814SAlan Cox if ((cflag & PARODD) != 0) 50049cdee0eSKees Lemmens frame_fmt |= FMT_PARITY_ODD; 5012a77c814SAlan Cox else 50249cdee0eSKees Lemmens frame_fmt |= FMT_PARITY_EVEN; 50349cdee0eSKees Lemmens } else { 50449cdee0eSKees Lemmens frame_fmt |= FMT_PARITY_NONE; 50549cdee0eSKees Lemmens } 50649cdee0eSKees Lemmens 50749cdee0eSKees Lemmens control &= ~CONTROL_MASK; 50849cdee0eSKees Lemmens if ((cflag & CRTSCTS) != 0) 50949cdee0eSKees Lemmens control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH); 51049cdee0eSKees Lemmens 51149cdee0eSKees Lemmens /* change control lines if we are switching to or from B0 */ 51249cdee0eSKees Lemmens /* FIXME: 51349cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 51449cdee0eSKees Lemmens control = priv->line_control; 51549cdee0eSKees Lemmens if ((cflag & CBAUD) == B0) 51649cdee0eSKees Lemmens priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); 51749cdee0eSKees Lemmens else 51849cdee0eSKees Lemmens priv->line_control |= (CONTROL_DTR | CONTROL_RTS); 51949cdee0eSKees Lemmens if (control != priv->line_control) { 52049cdee0eSKees Lemmens control = priv->line_control; 52149cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 52249cdee0eSKees Lemmens set_control_lines(serial->dev, control); 52349cdee0eSKees Lemmens } else { 52449cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 52549cdee0eSKees Lemmens } 52649cdee0eSKees Lemmens */ 52749cdee0eSKees Lemmens 52849cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 52949cdee0eSKees Lemmens if (divisor != priv->pending_setup.divisor 53049cdee0eSKees Lemmens || control != priv->pending_setup.control 53149cdee0eSKees Lemmens || frame_fmt != priv->pending_setup.frame_fmt) { 53249cdee0eSKees Lemmens priv->pending_setup.divisor = divisor; 53349cdee0eSKees Lemmens priv->pending_setup.control = control; 53449cdee0eSKees Lemmens priv->pending_setup.frame_fmt = frame_fmt; 53549cdee0eSKees Lemmens } 53649cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 53749cdee0eSKees Lemmens } 53849cdee0eSKees Lemmens 539a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port) 54049cdee0eSKees Lemmens { 54149cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 54249cdee0eSKees Lemmens struct ktermios tmp_termios; 54349cdee0eSKees Lemmens struct usb_serial *serial = port->serial; 54449cdee0eSKees Lemmens struct oti6858_control_pkt *buf; 54549cdee0eSKees Lemmens unsigned long flags; 54649cdee0eSKees Lemmens int result; 54749cdee0eSKees Lemmens 548441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 54949cdee0eSKees Lemmens 55049cdee0eSKees Lemmens usb_clear_halt(serial->dev, port->write_urb->pipe); 55149cdee0eSKees Lemmens usb_clear_halt(serial->dev, port->read_urb->pipe); 55249cdee0eSKees Lemmens 5532a77c814SAlan Cox buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL); 5542a77c814SAlan Cox if (buf == NULL) { 555441b62c1SHarvey Harrison dev_err(&port->dev, "%s(): out of memory!\n", __func__); 55649cdee0eSKees Lemmens return -ENOMEM; 55749cdee0eSKees Lemmens } 55849cdee0eSKees Lemmens 55949cdee0eSKees Lemmens result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 56049cdee0eSKees Lemmens OTI6858_REQ_T_GET_STATUS, 56149cdee0eSKees Lemmens OTI6858_REQ_GET_STATUS, 56249cdee0eSKees Lemmens 0, 0, 56349cdee0eSKees Lemmens buf, OTI6858_CTRL_PKT_SIZE, 56449cdee0eSKees Lemmens 100); 56549cdee0eSKees Lemmens if (result != OTI6858_CTRL_PKT_SIZE) { 56649cdee0eSKees Lemmens /* assume default (after power-on reset) values */ 56749cdee0eSKees Lemmens buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */ 56849cdee0eSKees Lemmens buf->frame_fmt = 0x03; /* 8N1 */ 56949cdee0eSKees Lemmens buf->something = 0x43; 57049cdee0eSKees Lemmens buf->control = 0x4c; /* DTR, RTS */ 57149cdee0eSKees Lemmens buf->tx_status = 0x00; 57249cdee0eSKees Lemmens buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */ 57349cdee0eSKees Lemmens buf->rx_bytes_avail = 0x00; 57449cdee0eSKees Lemmens } 57549cdee0eSKees Lemmens 57649cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 57749cdee0eSKees Lemmens memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE); 57849cdee0eSKees Lemmens priv->pending_setup.divisor = buf->divisor; 57949cdee0eSKees Lemmens priv->pending_setup.frame_fmt = buf->frame_fmt; 58049cdee0eSKees Lemmens priv->pending_setup.control = buf->control; 58149cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 58249cdee0eSKees Lemmens kfree(buf); 58349cdee0eSKees Lemmens 584441b62c1SHarvey Harrison dbg("%s(): submitting interrupt urb", __func__); 58549cdee0eSKees Lemmens port->interrupt_in_urb->dev = serial->dev; 58649cdee0eSKees Lemmens result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 58749cdee0eSKees Lemmens if (result != 0) { 58849cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed" 589441b62c1SHarvey Harrison " with error %d\n", __func__, result); 590335f8514SAlan Cox oti6858_close(port); 59149cdee0eSKees Lemmens return -EPROTO; 59249cdee0eSKees Lemmens } 59349cdee0eSKees Lemmens 59449cdee0eSKees Lemmens /* setup termios */ 59595da310eSAlan Cox if (tty) 59695da310eSAlan Cox oti6858_set_termios(tty, port, &tmp_termios); 597335f8514SAlan Cox port->port.drain_delay = 256; /* FIXME: check the FIFO length */ 59849cdee0eSKees Lemmens return 0; 59949cdee0eSKees Lemmens } 60049cdee0eSKees Lemmens 601335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port) 60249cdee0eSKees Lemmens { 60349cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 60449cdee0eSKees Lemmens unsigned long flags; 60549cdee0eSKees Lemmens 606441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 60749cdee0eSKees Lemmens 60828f27dcbSJohan Hovold spin_lock_irqsave(&port->lock, flags); 60949cdee0eSKees Lemmens /* clear out any remaining data in the buffer */ 61028f27dcbSJohan Hovold kfifo_reset_out(&port->write_fifo); 61128f27dcbSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 61249cdee0eSKees Lemmens 613335f8514SAlan Cox dbg("%s(): after buf_clear()", __func__); 61449cdee0eSKees Lemmens 61549cdee0eSKees Lemmens /* cancel scheduled setup */ 616*569ff2deSTejun Heo cancel_delayed_work_sync(&priv->delayed_setup_work); 617*569ff2deSTejun Heo cancel_delayed_work_sync(&priv->delayed_write_work); 61849cdee0eSKees Lemmens 61949cdee0eSKees Lemmens /* shutdown our urbs */ 620441b62c1SHarvey Harrison dbg("%s(): shutting down urbs", __func__); 62149cdee0eSKees Lemmens usb_kill_urb(port->write_urb); 62249cdee0eSKees Lemmens usb_kill_urb(port->read_urb); 62349cdee0eSKees Lemmens usb_kill_urb(port->interrupt_in_urb); 62449cdee0eSKees Lemmens } 62549cdee0eSKees Lemmens 62695da310eSAlan Cox static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, 62749cdee0eSKees Lemmens unsigned int set, unsigned int clear) 62849cdee0eSKees Lemmens { 62995da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 63049cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 63149cdee0eSKees Lemmens unsigned long flags; 63249cdee0eSKees Lemmens u8 control; 63349cdee0eSKees Lemmens 63449cdee0eSKees Lemmens dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)", 635441b62c1SHarvey Harrison __func__, port->number, set, clear); 63649cdee0eSKees Lemmens 63749cdee0eSKees Lemmens if (!usb_get_intfdata(port->serial->interface)) 63849cdee0eSKees Lemmens return -ENODEV; 63949cdee0eSKees Lemmens 64049cdee0eSKees Lemmens /* FIXME: check if this is correct (active high/low) */ 64149cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 64249cdee0eSKees Lemmens control = priv->pending_setup.control; 64349cdee0eSKees Lemmens if ((set & TIOCM_RTS) != 0) 64449cdee0eSKees Lemmens control |= CONTROL_RTS_HIGH; 64549cdee0eSKees Lemmens if ((set & TIOCM_DTR) != 0) 64649cdee0eSKees Lemmens control |= CONTROL_DTR_HIGH; 64749cdee0eSKees Lemmens if ((clear & TIOCM_RTS) != 0) 64849cdee0eSKees Lemmens control &= ~CONTROL_RTS_HIGH; 64949cdee0eSKees Lemmens if ((clear & TIOCM_DTR) != 0) 65049cdee0eSKees Lemmens control &= ~CONTROL_DTR_HIGH; 65149cdee0eSKees Lemmens 6522a77c814SAlan Cox if (control != priv->pending_setup.control) 65349cdee0eSKees Lemmens priv->pending_setup.control = control; 65449cdee0eSKees Lemmens 6552a77c814SAlan Cox spin_unlock_irqrestore(&priv->lock, flags); 65649cdee0eSKees Lemmens return 0; 65749cdee0eSKees Lemmens } 65849cdee0eSKees Lemmens 65995da310eSAlan Cox static int oti6858_tiocmget(struct tty_struct *tty, struct file *file) 66049cdee0eSKees Lemmens { 66195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 66249cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 66349cdee0eSKees Lemmens unsigned long flags; 66449cdee0eSKees Lemmens unsigned pin_state; 66549cdee0eSKees Lemmens unsigned result = 0; 66649cdee0eSKees Lemmens 667441b62c1SHarvey Harrison dbg("%s(port = %d)", __func__, port->number); 66849cdee0eSKees Lemmens 66949cdee0eSKees Lemmens if (!usb_get_intfdata(port->serial->interface)) 67049cdee0eSKees Lemmens return -ENODEV; 67149cdee0eSKees Lemmens 67249cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 67349cdee0eSKees Lemmens pin_state = priv->status.pin_state & PIN_MASK; 67449cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 67549cdee0eSKees Lemmens 67649cdee0eSKees Lemmens /* FIXME: check if this is correct (active high/low) */ 67749cdee0eSKees Lemmens if ((pin_state & PIN_RTS) != 0) 67849cdee0eSKees Lemmens result |= TIOCM_RTS; 67949cdee0eSKees Lemmens if ((pin_state & PIN_CTS) != 0) 68049cdee0eSKees Lemmens result |= TIOCM_CTS; 68149cdee0eSKees Lemmens if ((pin_state & PIN_DSR) != 0) 68249cdee0eSKees Lemmens result |= TIOCM_DSR; 68349cdee0eSKees Lemmens if ((pin_state & PIN_DTR) != 0) 68449cdee0eSKees Lemmens result |= TIOCM_DTR; 68549cdee0eSKees Lemmens if ((pin_state & PIN_RI) != 0) 68649cdee0eSKees Lemmens result |= TIOCM_RI; 68749cdee0eSKees Lemmens if ((pin_state & PIN_DCD) != 0) 68849cdee0eSKees Lemmens result |= TIOCM_CD; 68949cdee0eSKees Lemmens 690441b62c1SHarvey Harrison dbg("%s() = 0x%08x", __func__, result); 69149cdee0eSKees Lemmens 69249cdee0eSKees Lemmens return result; 69349cdee0eSKees Lemmens } 69449cdee0eSKees Lemmens 69549cdee0eSKees Lemmens static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 69649cdee0eSKees Lemmens { 69749cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 69849cdee0eSKees Lemmens unsigned long flags; 69949cdee0eSKees Lemmens unsigned int prev, status; 70049cdee0eSKees Lemmens unsigned int changed; 70149cdee0eSKees Lemmens 70249cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 70349cdee0eSKees Lemmens prev = priv->status.pin_state; 70449cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 70549cdee0eSKees Lemmens 70649cdee0eSKees Lemmens while (1) { 7072a77c814SAlan Cox wait_event_interruptible(priv->intr_wait, 7082a77c814SAlan Cox priv->status.pin_state != prev); 70949cdee0eSKees Lemmens if (signal_pending(current)) 71049cdee0eSKees Lemmens return -ERESTARTSYS; 71149cdee0eSKees Lemmens 71249cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 71349cdee0eSKees Lemmens status = priv->status.pin_state & PIN_MASK; 71449cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 71549cdee0eSKees Lemmens 71649cdee0eSKees Lemmens changed = prev ^ status; 71749cdee0eSKees Lemmens /* FIXME: check if this is correct (active high/low) */ 71849cdee0eSKees Lemmens if (((arg & TIOCM_RNG) && (changed & PIN_RI)) || 71949cdee0eSKees Lemmens ((arg & TIOCM_DSR) && (changed & PIN_DSR)) || 72049cdee0eSKees Lemmens ((arg & TIOCM_CD) && (changed & PIN_DCD)) || 7212a77c814SAlan Cox ((arg & TIOCM_CTS) && (changed & PIN_CTS))) 72249cdee0eSKees Lemmens return 0; 72349cdee0eSKees Lemmens prev = status; 72449cdee0eSKees Lemmens } 72549cdee0eSKees Lemmens 72649cdee0eSKees Lemmens /* NOTREACHED */ 72749cdee0eSKees Lemmens return 0; 72849cdee0eSKees Lemmens } 72949cdee0eSKees Lemmens 73095da310eSAlan Cox static int oti6858_ioctl(struct tty_struct *tty, struct file *file, 73149cdee0eSKees Lemmens unsigned int cmd, unsigned long arg) 73249cdee0eSKees Lemmens { 73395da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 73449cdee0eSKees Lemmens 73549cdee0eSKees Lemmens dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)", 736441b62c1SHarvey Harrison __func__, port->number, cmd, arg); 73749cdee0eSKees Lemmens 73849cdee0eSKees Lemmens switch (cmd) { 73949cdee0eSKees Lemmens case TIOCMIWAIT: 740441b62c1SHarvey Harrison dbg("%s(): TIOCMIWAIT", __func__); 74149cdee0eSKees Lemmens return wait_modem_info(port, arg); 74249cdee0eSKees Lemmens default: 743441b62c1SHarvey Harrison dbg("%s(): 0x%04x not supported", __func__, cmd); 74449cdee0eSKees Lemmens break; 74549cdee0eSKees Lemmens } 74649cdee0eSKees Lemmens return -ENOIOCTLCMD; 74749cdee0eSKees Lemmens } 74849cdee0eSKees Lemmens 74949cdee0eSKees Lemmens 750f9c99bb8SAlan Stern static void oti6858_release(struct usb_serial *serial) 75149cdee0eSKees Lemmens { 75249cdee0eSKees Lemmens int i; 75349cdee0eSKees Lemmens 754441b62c1SHarvey Harrison dbg("%s()", __func__); 75549cdee0eSKees Lemmens 75628f27dcbSJohan Hovold for (i = 0; i < serial->num_ports; ++i) 75728f27dcbSJohan Hovold kfree(usb_get_serial_port_data(serial->port[i])); 75849cdee0eSKees Lemmens } 75949cdee0eSKees Lemmens 76049cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb) 76149cdee0eSKees Lemmens { 762cdc97792SMing Lei struct usb_serial_port *port = urb->context; 76349cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 76449cdee0eSKees Lemmens int transient = 0, can_recv = 0, resubmit = 1; 76578c26aebSGreg Kroah-Hartman int status = urb->status; 76649cdee0eSKees Lemmens 76778c26aebSGreg Kroah-Hartman dbg("%s(port = %d, status = %d)", 768441b62c1SHarvey Harrison __func__, port->number, status); 76949cdee0eSKees Lemmens 77078c26aebSGreg Kroah-Hartman switch (status) { 77149cdee0eSKees Lemmens case 0: 77249cdee0eSKees Lemmens /* success */ 77349cdee0eSKees Lemmens break; 77449cdee0eSKees Lemmens case -ECONNRESET: 77549cdee0eSKees Lemmens case -ENOENT: 77649cdee0eSKees Lemmens case -ESHUTDOWN: 77749cdee0eSKees Lemmens /* this urb is terminated, clean up */ 77849cdee0eSKees Lemmens dbg("%s(): urb shutting down with status: %d", 779441b62c1SHarvey Harrison __func__, status); 78049cdee0eSKees Lemmens return; 78149cdee0eSKees Lemmens default: 78249cdee0eSKees Lemmens dbg("%s(): nonzero urb status received: %d", 783441b62c1SHarvey Harrison __func__, status); 78449cdee0eSKees Lemmens break; 78549cdee0eSKees Lemmens } 78649cdee0eSKees Lemmens 78778c26aebSGreg Kroah-Hartman if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) { 78849cdee0eSKees Lemmens struct oti6858_control_pkt *xs = urb->transfer_buffer; 78949cdee0eSKees Lemmens unsigned long flags; 79049cdee0eSKees Lemmens 79149cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 79249cdee0eSKees Lemmens 79349cdee0eSKees Lemmens if (!priv->transient) { 79449cdee0eSKees Lemmens if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) { 79549cdee0eSKees Lemmens if (xs->rx_bytes_avail == 0) { 79649cdee0eSKees Lemmens priv->transient = 4; 79749cdee0eSKees Lemmens priv->setup_done = 0; 79849cdee0eSKees Lemmens resubmit = 0; 79949cdee0eSKees Lemmens dbg("%s(): scheduling setup_line()", 800441b62c1SHarvey Harrison __func__); 80149cdee0eSKees Lemmens schedule_delayed_work(&priv->delayed_setup_work, 0); 80249cdee0eSKees Lemmens } 80349cdee0eSKees Lemmens } 80449cdee0eSKees Lemmens } else { 80549cdee0eSKees Lemmens if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) { 80649cdee0eSKees Lemmens priv->transient = 0; 80749cdee0eSKees Lemmens } else if (!priv->setup_done) { 80849cdee0eSKees Lemmens resubmit = 0; 80949cdee0eSKees Lemmens } else if (--priv->transient == 0) { 81049cdee0eSKees Lemmens if (xs->rx_bytes_avail == 0) { 81149cdee0eSKees Lemmens priv->transient = 4; 81249cdee0eSKees Lemmens priv->setup_done = 0; 81349cdee0eSKees Lemmens resubmit = 0; 81449cdee0eSKees Lemmens dbg("%s(): scheduling setup_line()", 815441b62c1SHarvey Harrison __func__); 81649cdee0eSKees Lemmens schedule_delayed_work(&priv->delayed_setup_work, 0); 81749cdee0eSKees Lemmens } 81849cdee0eSKees Lemmens } 81949cdee0eSKees Lemmens } 82049cdee0eSKees Lemmens 82149cdee0eSKees Lemmens if (!priv->transient) { 82249cdee0eSKees Lemmens if (xs->pin_state != priv->status.pin_state) 82349cdee0eSKees Lemmens wake_up_interruptible(&priv->intr_wait); 82449cdee0eSKees Lemmens memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE); 82549cdee0eSKees Lemmens } 82649cdee0eSKees Lemmens 82749cdee0eSKees Lemmens if (!priv->transient && xs->rx_bytes_avail != 0) { 82849cdee0eSKees Lemmens can_recv = xs->rx_bytes_avail; 82949cdee0eSKees Lemmens priv->flags.read_urb_in_use = 1; 83049cdee0eSKees Lemmens } 83149cdee0eSKees Lemmens 83249cdee0eSKees Lemmens transient = priv->transient; 83349cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 83449cdee0eSKees Lemmens } 83549cdee0eSKees Lemmens 83649cdee0eSKees Lemmens if (can_recv) { 83749cdee0eSKees Lemmens int result; 83849cdee0eSKees Lemmens 83949cdee0eSKees Lemmens port->read_urb->dev = port->serial->dev; 84049cdee0eSKees Lemmens result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 84149cdee0eSKees Lemmens if (result != 0) { 84249cdee0eSKees Lemmens priv->flags.read_urb_in_use = 0; 84349cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed," 844441b62c1SHarvey Harrison " error %d\n", __func__, result); 84549cdee0eSKees Lemmens } else { 84649cdee0eSKees Lemmens resubmit = 0; 84749cdee0eSKees Lemmens } 84849cdee0eSKees Lemmens } else if (!transient) { 84949cdee0eSKees Lemmens unsigned long flags; 85028f27dcbSJohan Hovold int count; 85128f27dcbSJohan Hovold 85228f27dcbSJohan Hovold spin_lock_irqsave(&port->lock, flags); 85328f27dcbSJohan Hovold count = kfifo_len(&port->write_fifo); 85428f27dcbSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 85549cdee0eSKees Lemmens 85649cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 85728f27dcbSJohan Hovold if (priv->flags.write_urb_in_use == 0 && count != 0) { 85849cdee0eSKees Lemmens schedule_delayed_work(&priv->delayed_write_work, 0); 85949cdee0eSKees Lemmens resubmit = 0; 86049cdee0eSKees Lemmens } 86149cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 86249cdee0eSKees Lemmens } 86349cdee0eSKees Lemmens 86449cdee0eSKees Lemmens if (resubmit) { 86549cdee0eSKees Lemmens int result; 86649cdee0eSKees Lemmens 8672a77c814SAlan Cox /* dbg("%s(): submitting interrupt urb", __func__); */ 86849cdee0eSKees Lemmens urb->dev = port->serial->dev; 86949cdee0eSKees Lemmens result = usb_submit_urb(urb, GFP_ATOMIC); 87049cdee0eSKees Lemmens if (result != 0) { 87149cdee0eSKees Lemmens dev_err(&urb->dev->dev, 87249cdee0eSKees Lemmens "%s(): usb_submit_urb() failed with" 873441b62c1SHarvey Harrison " error %d\n", __func__, result); 87449cdee0eSKees Lemmens } 87549cdee0eSKees Lemmens } 87649cdee0eSKees Lemmens } 87749cdee0eSKees Lemmens 87849cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb) 87949cdee0eSKees Lemmens { 880cdc97792SMing Lei struct usb_serial_port *port = urb->context; 88149cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 88249cdee0eSKees Lemmens struct tty_struct *tty; 88349cdee0eSKees Lemmens unsigned char *data = urb->transfer_buffer; 88449cdee0eSKees Lemmens unsigned long flags; 88578c26aebSGreg Kroah-Hartman int status = urb->status; 886b0a239daSAlan Cox int result; 88749cdee0eSKees Lemmens 88878c26aebSGreg Kroah-Hartman dbg("%s(port = %d, status = %d)", 889441b62c1SHarvey Harrison __func__, port->number, status); 89049cdee0eSKees Lemmens 89149cdee0eSKees Lemmens spin_lock_irqsave(&priv->lock, flags); 89249cdee0eSKees Lemmens priv->flags.read_urb_in_use = 0; 89349cdee0eSKees Lemmens spin_unlock_irqrestore(&priv->lock, flags); 89449cdee0eSKees Lemmens 89578c26aebSGreg Kroah-Hartman if (status != 0) { 89649cdee0eSKees Lemmens /* 89778c26aebSGreg Kroah-Hartman if (status == -EPROTO) { 8982a77c814SAlan Cox * PL2303 mysteriously fails with -EPROTO reschedule 8992a77c814SAlan Cox the read * 9002a77c814SAlan Cox dbg("%s - caught -EPROTO, resubmitting the urb", 9012a77c814SAlan Cox __func__); 90249cdee0eSKees Lemmens result = usb_submit_urb(urb, GFP_ATOMIC); 90349cdee0eSKees Lemmens if (result) 904441b62c1SHarvey Harrison dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result); 90549cdee0eSKees Lemmens return; 90649cdee0eSKees Lemmens } 90749cdee0eSKees Lemmens */ 908441b62c1SHarvey Harrison dbg("%s(): unable to handle the error, exiting", __func__); 90949cdee0eSKees Lemmens return; 91049cdee0eSKees Lemmens } 91149cdee0eSKees Lemmens 9124a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 91349cdee0eSKees Lemmens if (tty != NULL && urb->actual_length > 0) { 914b0a239daSAlan Cox tty_insert_flip_string(tty, data, urb->actual_length); 91549cdee0eSKees Lemmens tty_flip_buffer_push(tty); 91649cdee0eSKees Lemmens } 9174a90f09bSAlan Cox tty_kref_put(tty); 91849cdee0eSKees Lemmens 9191f87158eSAlan Stern /* schedule the interrupt urb */ 92049cdee0eSKees Lemmens port->interrupt_in_urb->dev = port->serial->dev; 92149cdee0eSKees Lemmens result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 9221f87158eSAlan Stern if (result != 0 && result != -EPERM) { 92349cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed," 924441b62c1SHarvey Harrison " error %d\n", __func__, result); 92549cdee0eSKees Lemmens } 92649cdee0eSKees Lemmens } 92749cdee0eSKees Lemmens 92849cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb) 92949cdee0eSKees Lemmens { 930cdc97792SMing Lei struct usb_serial_port *port = urb->context; 93149cdee0eSKees Lemmens struct oti6858_private *priv = usb_get_serial_port_data(port); 93278c26aebSGreg Kroah-Hartman int status = urb->status; 93349cdee0eSKees Lemmens int result; 93449cdee0eSKees Lemmens 93578c26aebSGreg Kroah-Hartman dbg("%s(port = %d, status = %d)", 936441b62c1SHarvey Harrison __func__, port->number, status); 93749cdee0eSKees Lemmens 93878c26aebSGreg Kroah-Hartman switch (status) { 93949cdee0eSKees Lemmens case 0: 94049cdee0eSKees Lemmens /* success */ 94149cdee0eSKees Lemmens break; 94249cdee0eSKees Lemmens case -ECONNRESET: 94349cdee0eSKees Lemmens case -ENOENT: 94449cdee0eSKees Lemmens case -ESHUTDOWN: 94549cdee0eSKees Lemmens /* this urb is terminated, clean up */ 94649cdee0eSKees Lemmens dbg("%s(): urb shutting down with status: %d", 947441b62c1SHarvey Harrison __func__, status); 94849cdee0eSKees Lemmens priv->flags.write_urb_in_use = 0; 94949cdee0eSKees Lemmens return; 95049cdee0eSKees Lemmens default: 95149cdee0eSKees Lemmens /* error in the urb, so we have to resubmit it */ 95249cdee0eSKees Lemmens dbg("%s(): nonzero write bulk status received: %d", 953441b62c1SHarvey Harrison __func__, status); 954441b62c1SHarvey Harrison dbg("%s(): overflow in write", __func__); 95549cdee0eSKees Lemmens 95649cdee0eSKees Lemmens port->write_urb->transfer_buffer_length = 1; 95749cdee0eSKees Lemmens port->write_urb->dev = port->serial->dev; 95849cdee0eSKees Lemmens result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 95949cdee0eSKees Lemmens if (result) { 96049cdee0eSKees Lemmens dev_err(&port->dev, "%s(): usb_submit_urb() failed," 961441b62c1SHarvey Harrison " error %d\n", __func__, result); 96249cdee0eSKees Lemmens } else { 96349cdee0eSKees Lemmens return; 96449cdee0eSKees Lemmens } 96549cdee0eSKees Lemmens } 96649cdee0eSKees Lemmens 96749cdee0eSKees Lemmens priv->flags.write_urb_in_use = 0; 96849cdee0eSKees Lemmens 9692a77c814SAlan Cox /* schedule the interrupt urb if we are still open */ 97049cdee0eSKees Lemmens port->interrupt_in_urb->dev = port->serial->dev; 971441b62c1SHarvey Harrison dbg("%s(): submitting interrupt urb", __func__); 97249cdee0eSKees Lemmens result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 97349cdee0eSKees Lemmens if (result != 0) { 97449cdee0eSKees Lemmens dev_err(&port->dev, "%s(): failed submitting int urb," 975441b62c1SHarvey Harrison " error %d\n", __func__, result); 97649cdee0eSKees Lemmens } 97749cdee0eSKees Lemmens } 97849cdee0eSKees Lemmens 97949cdee0eSKees Lemmens /* module description and (de)initialization */ 98049cdee0eSKees Lemmens 98149cdee0eSKees Lemmens static int __init oti6858_init(void) 98249cdee0eSKees Lemmens { 98349cdee0eSKees Lemmens int retval; 98449cdee0eSKees Lemmens 9852a77c814SAlan Cox retval = usb_serial_register(&oti6858_device); 9862a77c814SAlan Cox if (retval == 0) { 9872a77c814SAlan Cox retval = usb_register(&oti6858_driver); 9882a77c814SAlan Cox if (retval) 98949cdee0eSKees Lemmens usb_serial_deregister(&oti6858_device); 99049cdee0eSKees Lemmens } 99149cdee0eSKees Lemmens return retval; 99249cdee0eSKees Lemmens } 99349cdee0eSKees Lemmens 99449cdee0eSKees Lemmens static void __exit oti6858_exit(void) 99549cdee0eSKees Lemmens { 99649cdee0eSKees Lemmens usb_deregister(&oti6858_driver); 99749cdee0eSKees Lemmens usb_serial_deregister(&oti6858_device); 99849cdee0eSKees Lemmens } 99949cdee0eSKees Lemmens 100049cdee0eSKees Lemmens module_init(oti6858_init); 100149cdee0eSKees Lemmens module_exit(oti6858_exit); 100249cdee0eSKees Lemmens 100349cdee0eSKees Lemmens MODULE_DESCRIPTION(OTI6858_DESCRIPTION); 100449cdee0eSKees Lemmens MODULE_AUTHOR(OTI6858_AUTHOR); 100549cdee0eSKees Lemmens MODULE_VERSION(OTI6858_VERSION); 100649cdee0eSKees Lemmens MODULE_LICENSE("GPL"); 100749cdee0eSKees Lemmens 100849cdee0eSKees Lemmens module_param(debug, bool, S_IRUGO | S_IWUSR); 100949cdee0eSKees Lemmens MODULE_PARM_DESC(debug, "enable debug output"); 101049cdee0eSKees Lemmens 1011