Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Ours Technology Inc. OTi-6858 USB to serial adapter driver. |
| 3 | * |
| 4 | * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20) |
| 5 | * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail) |
| 6 | * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) |
| 7 | * Copyright (C) 2003 IBM Corp. |
| 8 | * |
| 9 | * Many thanks to the authors of pl2303 driver: all functions in this file |
| 10 | * are heavily based on pl2303 code, buffering code is a 1-to-1 copy. |
| 11 | * |
| 12 | * Warning! You use this driver on your own risk! The only official |
| 13 | * description of this device I have is datasheet from manufacturer, |
| 14 | * and it doesn't contain almost any information needed to write a driver. |
| 15 | * Almost all knowlegde used while writing this driver was gathered by: |
| 16 | * - analyzing traffic between device and the M$ Windows 2000 driver, |
| 17 | * - trying different bit combinations and checking pin states |
| 18 | * with a voltmeter, |
| 19 | * - receiving malformed frames and producing buffer overflows |
| 20 | * to learn how errors are reported, |
| 21 | * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE |
| 22 | * CONNECTED TO IT! |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License as published by |
| 26 | * the Free Software Foundation; either version 2 of the License. |
| 27 | * |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 28 | * See Documentation/usb/usb-serial.txt for more information on using this |
| 29 | * driver |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 30 | * |
| 31 | * TODO: |
| 32 | * - implement correct flushing for ioctls and oti6858_close() |
| 33 | * - check how errors (rx overflow, parity error, framing error) are reported |
| 34 | * - implement oti6858_break_ctl() |
| 35 | * - implement more ioctls |
| 36 | * - test/implement flow control |
| 37 | * - allow setting custom baud rates |
| 38 | */ |
| 39 | |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/tty.h> |
| 45 | #include <linux/tty_driver.h> |
| 46 | #include <linux/tty_flip.h> |
| 47 | #include <linux/serial.h> |
| 48 | #include <linux/module.h> |
| 49 | #include <linux/moduleparam.h> |
| 50 | #include <linux/spinlock.h> |
| 51 | #include <linux/usb.h> |
| 52 | #include <linux/usb/serial.h> |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 53 | #include <linux/uaccess.h> |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 54 | #include "oti6858.h" |
| 55 | |
| 56 | #define OTI6858_DESCRIPTION \ |
| 57 | "Ours Technology Inc. OTi-6858 USB to serial adapter driver" |
| 58 | #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>" |
| 59 | #define OTI6858_VERSION "0.1" |
| 60 | |
Németh Márton | 7d40d7e | 2010-01-10 15:34:24 +0100 | [diff] [blame^] | 61 | static const struct usb_device_id id_table[] = { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 62 | { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) }, |
| 63 | { } |
| 64 | }; |
| 65 | |
| 66 | MODULE_DEVICE_TABLE(usb, id_table); |
| 67 | |
| 68 | static struct usb_driver oti6858_driver = { |
| 69 | .name = "oti6858", |
| 70 | .probe = usb_serial_probe, |
| 71 | .disconnect = usb_serial_disconnect, |
| 72 | .id_table = id_table, |
| 73 | .no_dynamic_id = 1, |
| 74 | }; |
| 75 | |
| 76 | static int debug; |
| 77 | |
| 78 | |
| 79 | /* buffering code, copied from pl2303 driver */ |
| 80 | #define PL2303_BUF_SIZE 1024 |
| 81 | #define PL2303_TMP_BUF_SIZE 1024 |
| 82 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 83 | struct oti6858_buf { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 84 | unsigned int buf_size; |
| 85 | char *buf_buf; |
| 86 | char *buf_get; |
| 87 | char *buf_put; |
| 88 | }; |
| 89 | |
| 90 | /* requests */ |
| 91 | #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00) |
| 92 | #define OTI6858_REQ_T_GET_STATUS 0x01 |
| 93 | |
| 94 | #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00) |
| 95 | #define OTI6858_REQ_T_SET_LINE 0x00 |
| 96 | |
| 97 | #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01) |
| 98 | #define OTI6858_REQ_T_CHECK_TXBUFF 0x00 |
| 99 | |
| 100 | /* format of the control packet */ |
| 101 | struct oti6858_control_pkt { |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 102 | __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 103 | #define OTI6858_MAX_BAUD_RATE 3000000 |
| 104 | u8 frame_fmt; |
| 105 | #define FMT_STOP_BITS_MASK 0xc0 |
| 106 | #define FMT_STOP_BITS_1 0x00 |
| 107 | #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */ |
| 108 | #define FMT_PARITY_MASK 0x38 |
| 109 | #define FMT_PARITY_NONE 0x00 |
| 110 | #define FMT_PARITY_ODD 0x08 |
| 111 | #define FMT_PARITY_EVEN 0x18 |
| 112 | #define FMT_PARITY_MARK 0x28 |
| 113 | #define FMT_PARITY_SPACE 0x38 |
| 114 | #define FMT_DATA_BITS_MASK 0x03 |
| 115 | #define FMT_DATA_BITS_5 0x00 |
| 116 | #define FMT_DATA_BITS_6 0x01 |
| 117 | #define FMT_DATA_BITS_7 0x02 |
| 118 | #define FMT_DATA_BITS_8 0x03 |
| 119 | u8 something; /* always equals 0x43 */ |
| 120 | u8 control; /* settings of flow control lines */ |
| 121 | #define CONTROL_MASK 0x0c |
| 122 | #define CONTROL_DTR_HIGH 0x08 |
| 123 | #define CONTROL_RTS_HIGH 0x04 |
| 124 | u8 tx_status; |
| 125 | #define TX_BUFFER_EMPTIED 0x09 |
| 126 | u8 pin_state; |
| 127 | #define PIN_MASK 0x3f |
| 128 | #define PIN_RTS 0x20 /* output pin */ |
| 129 | #define PIN_CTS 0x10 /* input pin, active low */ |
| 130 | #define PIN_DSR 0x08 /* input pin, active low */ |
| 131 | #define PIN_DTR 0x04 /* output pin */ |
| 132 | #define PIN_RI 0x02 /* input pin, active low */ |
| 133 | #define PIN_DCD 0x01 /* input pin, active low */ |
| 134 | u8 rx_bytes_avail; /* number of bytes in rx buffer */; |
| 135 | }; |
| 136 | |
| 137 | #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt) |
| 138 | #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \ |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 139 | (((a)->divisor == (priv)->pending_setup.divisor) \ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 140 | && ((a)->control == (priv)->pending_setup.control) \ |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 141 | && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt)) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 142 | |
| 143 | /* function prototypes */ |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 144 | static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port); |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 145 | static void oti6858_close(struct usb_serial_port *port); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 146 | static void oti6858_set_termios(struct tty_struct *tty, |
| 147 | struct usb_serial_port *port, struct ktermios *old); |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 148 | static void oti6858_init_termios(struct tty_struct *tty); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 149 | static int oti6858_ioctl(struct tty_struct *tty, struct file *file, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 150 | unsigned int cmd, unsigned long arg); |
| 151 | static void oti6858_read_int_callback(struct urb *urb); |
| 152 | static void oti6858_read_bulk_callback(struct urb *urb); |
| 153 | static void oti6858_write_bulk_callback(struct urb *urb); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 154 | static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 155 | const unsigned char *buf, int count); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 156 | static int oti6858_write_room(struct tty_struct *tty); |
| 157 | static int oti6858_chars_in_buffer(struct tty_struct *tty); |
| 158 | static int oti6858_tiocmget(struct tty_struct *tty, struct file *file); |
| 159 | static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 160 | unsigned int set, unsigned int clear); |
| 161 | static int oti6858_startup(struct usb_serial *serial); |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 162 | static void oti6858_release(struct usb_serial *serial); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 163 | |
| 164 | /* functions operating on buffers */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 165 | static struct oti6858_buf *oti6858_buf_alloc(unsigned int size); |
| 166 | static void oti6858_buf_free(struct oti6858_buf *pb); |
| 167 | static void oti6858_buf_clear(struct oti6858_buf *pb); |
| 168 | static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb); |
| 169 | static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb); |
| 170 | static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 171 | unsigned int count); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 172 | static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 173 | unsigned int count); |
| 174 | |
| 175 | |
| 176 | /* device info */ |
| 177 | static struct usb_serial_driver oti6858_device = { |
| 178 | .driver = { |
| 179 | .owner = THIS_MODULE, |
| 180 | .name = "oti6858", |
| 181 | }, |
| 182 | .id_table = id_table, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 183 | .num_ports = 1, |
| 184 | .open = oti6858_open, |
| 185 | .close = oti6858_close, |
| 186 | .write = oti6858_write, |
| 187 | .ioctl = oti6858_ioctl, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 188 | .set_termios = oti6858_set_termios, |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 189 | .init_termios = oti6858_init_termios, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 190 | .tiocmget = oti6858_tiocmget, |
| 191 | .tiocmset = oti6858_tiocmset, |
| 192 | .read_bulk_callback = oti6858_read_bulk_callback, |
| 193 | .read_int_callback = oti6858_read_int_callback, |
| 194 | .write_bulk_callback = oti6858_write_bulk_callback, |
| 195 | .write_room = oti6858_write_room, |
| 196 | .chars_in_buffer = oti6858_chars_in_buffer, |
| 197 | .attach = oti6858_startup, |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 198 | .release = oti6858_release, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | struct oti6858_private { |
| 202 | spinlock_t lock; |
| 203 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 204 | struct oti6858_buf *buf; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 205 | struct oti6858_control_pkt status; |
| 206 | |
| 207 | struct { |
| 208 | u8 read_urb_in_use; |
| 209 | u8 write_urb_in_use; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 210 | } flags; |
| 211 | struct delayed_work delayed_write_work; |
| 212 | |
| 213 | struct { |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 214 | __le16 divisor; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 215 | u8 frame_fmt; |
| 216 | u8 control; |
| 217 | } pending_setup; |
| 218 | u8 transient; |
| 219 | u8 setup_done; |
| 220 | struct delayed_work delayed_setup_work; |
| 221 | |
| 222 | wait_queue_head_t intr_wait; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 223 | struct usb_serial_port *port; /* USB port with which associated */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 224 | }; |
| 225 | |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 226 | static void setup_line(struct work_struct *work) |
| 227 | { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 228 | struct oti6858_private *priv = container_of(work, |
| 229 | struct oti6858_private, delayed_setup_work.work); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 230 | struct usb_serial_port *port = priv->port; |
| 231 | struct oti6858_control_pkt *new_setup; |
| 232 | unsigned long flags; |
| 233 | int result; |
| 234 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 235 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 236 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 237 | new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL); |
| 238 | if (new_setup == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 239 | dev_err(&port->dev, "%s(): out of memory!\n", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 240 | /* we will try again */ |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 241 | schedule_delayed_work(&priv->delayed_setup_work, |
| 242 | msecs_to_jiffies(2)); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
| 246 | result = usb_control_msg(port->serial->dev, |
| 247 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 248 | OTI6858_REQ_T_GET_STATUS, |
| 249 | OTI6858_REQ_GET_STATUS, |
| 250 | 0, 0, |
| 251 | new_setup, OTI6858_CTRL_PKT_SIZE, |
| 252 | 100); |
| 253 | |
| 254 | if (result != OTI6858_CTRL_PKT_SIZE) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 255 | dev_err(&port->dev, "%s(): error reading status\n", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 256 | kfree(new_setup); |
| 257 | /* we will try again */ |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 258 | schedule_delayed_work(&priv->delayed_setup_work, |
| 259 | msecs_to_jiffies(2)); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | spin_lock_irqsave(&priv->lock, flags); |
| 264 | if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) { |
| 265 | new_setup->divisor = priv->pending_setup.divisor; |
| 266 | new_setup->control = priv->pending_setup.control; |
| 267 | new_setup->frame_fmt = priv->pending_setup.frame_fmt; |
| 268 | |
| 269 | spin_unlock_irqrestore(&priv->lock, flags); |
| 270 | result = usb_control_msg(port->serial->dev, |
| 271 | usb_sndctrlpipe(port->serial->dev, 0), |
| 272 | OTI6858_REQ_T_SET_LINE, |
| 273 | OTI6858_REQ_SET_LINE, |
| 274 | 0, 0, |
| 275 | new_setup, OTI6858_CTRL_PKT_SIZE, |
| 276 | 100); |
| 277 | } else { |
| 278 | spin_unlock_irqrestore(&priv->lock, flags); |
| 279 | result = 0; |
| 280 | } |
| 281 | kfree(new_setup); |
| 282 | |
| 283 | spin_lock_irqsave(&priv->lock, flags); |
| 284 | if (result != OTI6858_CTRL_PKT_SIZE) |
| 285 | priv->transient = 0; |
| 286 | priv->setup_done = 1; |
| 287 | spin_unlock_irqrestore(&priv->lock, flags); |
| 288 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 289 | dbg("%s(): submitting interrupt urb", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 290 | port->interrupt_in_urb->dev = port->serial->dev; |
Oliver Neukum | 40d2858 | 2009-10-07 18:07:10 +0200 | [diff] [blame] | 291 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 292 | if (result != 0) { |
| 293 | dev_err(&port->dev, "%s(): usb_submit_urb() failed" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 294 | " with error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | void send_data(struct work_struct *work) |
| 299 | { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 300 | struct oti6858_private *priv = container_of(work, |
| 301 | struct oti6858_private, delayed_write_work.work); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 302 | struct usb_serial_port *port = priv->port; |
| 303 | int count = 0, result; |
| 304 | unsigned long flags; |
Johan Hovold | d212632 | 2009-12-28 23:01:56 +0100 | [diff] [blame] | 305 | u8 *allow; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 306 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 307 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 308 | |
| 309 | spin_lock_irqsave(&priv->lock, flags); |
| 310 | if (priv->flags.write_urb_in_use) { |
| 311 | spin_unlock_irqrestore(&priv->lock, flags); |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 312 | schedule_delayed_work(&priv->delayed_write_work, |
| 313 | msecs_to_jiffies(2)); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | priv->flags.write_urb_in_use = 1; |
| 317 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 318 | count = oti6858_buf_data_avail(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 319 | spin_unlock_irqrestore(&priv->lock, flags); |
| 320 | if (count > port->bulk_out_size) |
| 321 | count = port->bulk_out_size; |
| 322 | |
| 323 | if (count != 0) { |
Johan Hovold | d212632 | 2009-12-28 23:01:56 +0100 | [diff] [blame] | 324 | allow = kmalloc(1, GFP_KERNEL); |
| 325 | if (!allow) { |
| 326 | dev_err(&port->dev, "%s(): kmalloc failed\n", |
| 327 | __func__); |
| 328 | return; |
| 329 | } |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 330 | result = usb_control_msg(port->serial->dev, |
| 331 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 332 | OTI6858_REQ_T_CHECK_TXBUFF, |
| 333 | OTI6858_REQ_CHECK_TXBUFF, |
Johan Hovold | d212632 | 2009-12-28 23:01:56 +0100 | [diff] [blame] | 334 | count, 0, allow, 1, 100); |
| 335 | if (result != 1 || *allow != 0) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 336 | count = 0; |
Johan Hovold | d212632 | 2009-12-28 23:01:56 +0100 | [diff] [blame] | 337 | kfree(allow); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (count == 0) { |
| 341 | priv->flags.write_urb_in_use = 0; |
| 342 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 343 | dbg("%s(): submitting interrupt urb", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 344 | port->interrupt_in_urb->dev = port->serial->dev; |
Oliver Neukum | 40d2858 | 2009-10-07 18:07:10 +0200 | [diff] [blame] | 345 | result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 346 | if (result != 0) { |
| 347 | dev_err(&port->dev, "%s(): usb_submit_urb() failed" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 348 | " with error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 349 | } |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | spin_lock_irqsave(&priv->lock, flags); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 354 | oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 355 | spin_unlock_irqrestore(&priv->lock, flags); |
| 356 | |
| 357 | port->write_urb->transfer_buffer_length = count; |
| 358 | port->write_urb->dev = port->serial->dev; |
Oliver Neukum | 40d2858 | 2009-10-07 18:07:10 +0200 | [diff] [blame] | 359 | result = usb_submit_urb(port->write_urb, GFP_NOIO); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 360 | if (result != 0) { |
| 361 | dev_err(&port->dev, "%s(): usb_submit_urb() failed" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 362 | " with error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 363 | priv->flags.write_urb_in_use = 0; |
| 364 | } |
| 365 | |
| 366 | usb_serial_port_softint(port); |
| 367 | } |
| 368 | |
| 369 | static int oti6858_startup(struct usb_serial *serial) |
| 370 | { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 371 | struct usb_serial_port *port = serial->port[0]; |
| 372 | struct oti6858_private *priv; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 373 | int i; |
| 374 | |
| 375 | for (i = 0; i < serial->num_ports; ++i) { |
| 376 | priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL); |
| 377 | if (!priv) |
| 378 | break; |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 379 | priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 380 | if (priv->buf == NULL) { |
| 381 | kfree(priv); |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | spin_lock_init(&priv->lock); |
| 386 | init_waitqueue_head(&priv->intr_wait); |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 387 | /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */ |
| 388 | /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 389 | priv->port = port; |
| 390 | INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line); |
| 391 | INIT_DELAYED_WORK(&priv->delayed_write_work, send_data); |
| 392 | |
| 393 | usb_set_serial_port_data(serial->port[i], priv); |
| 394 | } |
| 395 | if (i == serial->num_ports) |
| 396 | return 0; |
| 397 | |
| 398 | for (--i; i >= 0; --i) { |
| 399 | priv = usb_get_serial_port_data(serial->port[i]); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 400 | oti6858_buf_free(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 401 | kfree(priv); |
| 402 | usb_set_serial_port_data(serial->port[i], NULL); |
| 403 | } |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 407 | static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 408 | const unsigned char *buf, int count) |
| 409 | { |
| 410 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 411 | unsigned long flags; |
| 412 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 413 | dbg("%s(port = %d, count = %d)", __func__, port->number, count); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 414 | |
| 415 | if (!count) |
| 416 | return count; |
| 417 | |
| 418 | spin_lock_irqsave(&priv->lock, flags); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 419 | count = oti6858_buf_put(priv->buf, buf, count); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 420 | spin_unlock_irqrestore(&priv->lock, flags); |
| 421 | |
| 422 | return count; |
| 423 | } |
| 424 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 425 | static int oti6858_write_room(struct tty_struct *tty) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 426 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 427 | struct usb_serial_port *port = tty->driver_data; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 428 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 429 | int room = 0; |
| 430 | unsigned long flags; |
| 431 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 432 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 433 | |
| 434 | spin_lock_irqsave(&priv->lock, flags); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 435 | room = oti6858_buf_space_avail(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 436 | spin_unlock_irqrestore(&priv->lock, flags); |
| 437 | |
| 438 | return room; |
| 439 | } |
| 440 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 441 | static int oti6858_chars_in_buffer(struct tty_struct *tty) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 442 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 443 | struct usb_serial_port *port = tty->driver_data; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 444 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 445 | int chars = 0; |
| 446 | unsigned long flags; |
| 447 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 448 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 449 | |
| 450 | spin_lock_irqsave(&priv->lock, flags); |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 451 | chars = oti6858_buf_data_avail(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 452 | spin_unlock_irqrestore(&priv->lock, flags); |
| 453 | |
| 454 | return chars; |
| 455 | } |
| 456 | |
Alan Cox | fe1ae7f | 2009-09-19 13:13:33 -0700 | [diff] [blame] | 457 | static void oti6858_init_termios(struct tty_struct *tty) |
| 458 | { |
| 459 | *(tty->termios) = tty_std_termios; |
| 460 | tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL; |
| 461 | tty->termios->c_ispeed = 38400; |
| 462 | tty->termios->c_ospeed = 38400; |
| 463 | } |
| 464 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 465 | static void oti6858_set_termios(struct tty_struct *tty, |
| 466 | struct usb_serial_port *port, struct ktermios *old_termios) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 467 | { |
| 468 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 469 | unsigned long flags; |
| 470 | unsigned int cflag; |
| 471 | u8 frame_fmt, control; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 472 | __le16 divisor; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 473 | int br; |
| 474 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 475 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 476 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 477 | if (!tty) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 478 | dbg("%s(): no tty structures", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 482 | cflag = tty->termios->c_cflag; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 483 | |
| 484 | spin_lock_irqsave(&priv->lock, flags); |
| 485 | divisor = priv->pending_setup.divisor; |
| 486 | frame_fmt = priv->pending_setup.frame_fmt; |
| 487 | control = priv->pending_setup.control; |
| 488 | spin_unlock_irqrestore(&priv->lock, flags); |
| 489 | |
| 490 | frame_fmt &= ~FMT_DATA_BITS_MASK; |
| 491 | switch (cflag & CSIZE) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 492 | case CS5: |
| 493 | frame_fmt |= FMT_DATA_BITS_5; |
| 494 | break; |
| 495 | case CS6: |
| 496 | frame_fmt |= FMT_DATA_BITS_6; |
| 497 | break; |
| 498 | case CS7: |
| 499 | frame_fmt |= FMT_DATA_BITS_7; |
| 500 | break; |
| 501 | default: |
| 502 | case CS8: |
| 503 | frame_fmt |= FMT_DATA_BITS_8; |
| 504 | break; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* manufacturer claims that this device can work with baud rates |
| 508 | * up to 3 Mbps; I've tested it only on 115200 bps, so I can't |
| 509 | * guarantee that any other baud rate will work (especially |
| 510 | * the higher ones) |
| 511 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 512 | br = tty_get_baud_rate(tty); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 513 | if (br == 0) { |
| 514 | divisor = 0; |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 515 | } else { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 516 | int real_br; |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 517 | int new_divisor; |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 518 | br = min(br, OTI6858_MAX_BAUD_RATE); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 519 | |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 520 | new_divisor = (96000000 + 8 * br) / (16 * br); |
| 521 | real_br = 96000000 / (16 * new_divisor); |
| 522 | divisor = cpu_to_le16(new_divisor); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 523 | tty_encode_baud_rate(tty, real_br, real_br); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | frame_fmt &= ~FMT_STOP_BITS_MASK; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 527 | if ((cflag & CSTOPB) != 0) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 528 | frame_fmt |= FMT_STOP_BITS_2; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 529 | else |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 530 | frame_fmt |= FMT_STOP_BITS_1; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 531 | |
| 532 | frame_fmt &= ~FMT_PARITY_MASK; |
| 533 | if ((cflag & PARENB) != 0) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 534 | if ((cflag & PARODD) != 0) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 535 | frame_fmt |= FMT_PARITY_ODD; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 536 | else |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 537 | frame_fmt |= FMT_PARITY_EVEN; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 538 | } else { |
| 539 | frame_fmt |= FMT_PARITY_NONE; |
| 540 | } |
| 541 | |
| 542 | control &= ~CONTROL_MASK; |
| 543 | if ((cflag & CRTSCTS) != 0) |
| 544 | control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH); |
| 545 | |
| 546 | /* change control lines if we are switching to or from B0 */ |
| 547 | /* FIXME: |
| 548 | spin_lock_irqsave(&priv->lock, flags); |
| 549 | control = priv->line_control; |
| 550 | if ((cflag & CBAUD) == B0) |
| 551 | priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS); |
| 552 | else |
| 553 | priv->line_control |= (CONTROL_DTR | CONTROL_RTS); |
| 554 | if (control != priv->line_control) { |
| 555 | control = priv->line_control; |
| 556 | spin_unlock_irqrestore(&priv->lock, flags); |
| 557 | set_control_lines(serial->dev, control); |
| 558 | } else { |
| 559 | spin_unlock_irqrestore(&priv->lock, flags); |
| 560 | } |
| 561 | */ |
| 562 | |
| 563 | spin_lock_irqsave(&priv->lock, flags); |
| 564 | if (divisor != priv->pending_setup.divisor |
| 565 | || control != priv->pending_setup.control |
| 566 | || frame_fmt != priv->pending_setup.frame_fmt) { |
| 567 | priv->pending_setup.divisor = divisor; |
| 568 | priv->pending_setup.control = control; |
| 569 | priv->pending_setup.frame_fmt = frame_fmt; |
| 570 | } |
| 571 | spin_unlock_irqrestore(&priv->lock, flags); |
| 572 | } |
| 573 | |
Alan Cox | a509a7e | 2009-09-19 13:13:26 -0700 | [diff] [blame] | 574 | static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 575 | { |
| 576 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 577 | struct ktermios tmp_termios; |
| 578 | struct usb_serial *serial = port->serial; |
| 579 | struct oti6858_control_pkt *buf; |
| 580 | unsigned long flags; |
| 581 | int result; |
| 582 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 583 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 584 | |
| 585 | usb_clear_halt(serial->dev, port->write_urb->pipe); |
| 586 | usb_clear_halt(serial->dev, port->read_urb->pipe); |
| 587 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 588 | if (port->port.count != 1) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 589 | return 0; |
| 590 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 591 | buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL); |
| 592 | if (buf == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 593 | dev_err(&port->dev, "%s(): out of memory!\n", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 594 | return -ENOMEM; |
| 595 | } |
| 596 | |
| 597 | result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 598 | OTI6858_REQ_T_GET_STATUS, |
| 599 | OTI6858_REQ_GET_STATUS, |
| 600 | 0, 0, |
| 601 | buf, OTI6858_CTRL_PKT_SIZE, |
| 602 | 100); |
| 603 | if (result != OTI6858_CTRL_PKT_SIZE) { |
| 604 | /* assume default (after power-on reset) values */ |
| 605 | buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */ |
| 606 | buf->frame_fmt = 0x03; /* 8N1 */ |
| 607 | buf->something = 0x43; |
| 608 | buf->control = 0x4c; /* DTR, RTS */ |
| 609 | buf->tx_status = 0x00; |
| 610 | buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */ |
| 611 | buf->rx_bytes_avail = 0x00; |
| 612 | } |
| 613 | |
| 614 | spin_lock_irqsave(&priv->lock, flags); |
| 615 | memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE); |
| 616 | priv->pending_setup.divisor = buf->divisor; |
| 617 | priv->pending_setup.frame_fmt = buf->frame_fmt; |
| 618 | priv->pending_setup.control = buf->control; |
| 619 | spin_unlock_irqrestore(&priv->lock, flags); |
| 620 | kfree(buf); |
| 621 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 622 | dbg("%s(): submitting interrupt urb", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 623 | port->interrupt_in_urb->dev = serial->dev; |
| 624 | result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 625 | if (result != 0) { |
| 626 | dev_err(&port->dev, "%s(): usb_submit_urb() failed" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 627 | " with error %d\n", __func__, result); |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 628 | oti6858_close(port); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 629 | return -EPROTO; |
| 630 | } |
| 631 | |
| 632 | /* setup termios */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 633 | if (tty) |
| 634 | oti6858_set_termios(tty, port, &tmp_termios); |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 635 | port->port.drain_delay = 256; /* FIXME: check the FIFO length */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 639 | static void oti6858_close(struct usb_serial_port *port) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 640 | { |
| 641 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 642 | unsigned long flags; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 643 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 644 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 645 | |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 646 | spin_lock_irqsave(&priv->lock, flags); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 647 | /* clear out any remaining data in the buffer */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 648 | oti6858_buf_clear(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 649 | spin_unlock_irqrestore(&priv->lock, flags); |
| 650 | |
Alan Cox | 335f851 | 2009-06-11 12:26:29 +0100 | [diff] [blame] | 651 | dbg("%s(): after buf_clear()", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 652 | |
| 653 | /* cancel scheduled setup */ |
| 654 | cancel_delayed_work(&priv->delayed_setup_work); |
| 655 | cancel_delayed_work(&priv->delayed_write_work); |
| 656 | flush_scheduled_work(); |
| 657 | |
| 658 | /* shutdown our urbs */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 659 | dbg("%s(): shutting down urbs", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 660 | usb_kill_urb(port->write_urb); |
| 661 | usb_kill_urb(port->read_urb); |
| 662 | usb_kill_urb(port->interrupt_in_urb); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 665 | static int oti6858_tiocmset(struct tty_struct *tty, struct file *file, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 666 | unsigned int set, unsigned int clear) |
| 667 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 668 | struct usb_serial_port *port = tty->driver_data; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 669 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 670 | unsigned long flags; |
| 671 | u8 control; |
| 672 | |
| 673 | dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 674 | __func__, port->number, set, clear); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 675 | |
| 676 | if (!usb_get_intfdata(port->serial->interface)) |
| 677 | return -ENODEV; |
| 678 | |
| 679 | /* FIXME: check if this is correct (active high/low) */ |
| 680 | spin_lock_irqsave(&priv->lock, flags); |
| 681 | control = priv->pending_setup.control; |
| 682 | if ((set & TIOCM_RTS) != 0) |
| 683 | control |= CONTROL_RTS_HIGH; |
| 684 | if ((set & TIOCM_DTR) != 0) |
| 685 | control |= CONTROL_DTR_HIGH; |
| 686 | if ((clear & TIOCM_RTS) != 0) |
| 687 | control &= ~CONTROL_RTS_HIGH; |
| 688 | if ((clear & TIOCM_DTR) != 0) |
| 689 | control &= ~CONTROL_DTR_HIGH; |
| 690 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 691 | if (control != priv->pending_setup.control) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 692 | priv->pending_setup.control = control; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 693 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 694 | spin_unlock_irqrestore(&priv->lock, flags); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 698 | static int oti6858_tiocmget(struct tty_struct *tty, struct file *file) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 699 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 700 | struct usb_serial_port *port = tty->driver_data; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 701 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 702 | unsigned long flags; |
| 703 | unsigned pin_state; |
| 704 | unsigned result = 0; |
| 705 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 706 | dbg("%s(port = %d)", __func__, port->number); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 707 | |
| 708 | if (!usb_get_intfdata(port->serial->interface)) |
| 709 | return -ENODEV; |
| 710 | |
| 711 | spin_lock_irqsave(&priv->lock, flags); |
| 712 | pin_state = priv->status.pin_state & PIN_MASK; |
| 713 | spin_unlock_irqrestore(&priv->lock, flags); |
| 714 | |
| 715 | /* FIXME: check if this is correct (active high/low) */ |
| 716 | if ((pin_state & PIN_RTS) != 0) |
| 717 | result |= TIOCM_RTS; |
| 718 | if ((pin_state & PIN_CTS) != 0) |
| 719 | result |= TIOCM_CTS; |
| 720 | if ((pin_state & PIN_DSR) != 0) |
| 721 | result |= TIOCM_DSR; |
| 722 | if ((pin_state & PIN_DTR) != 0) |
| 723 | result |= TIOCM_DTR; |
| 724 | if ((pin_state & PIN_RI) != 0) |
| 725 | result |= TIOCM_RI; |
| 726 | if ((pin_state & PIN_DCD) != 0) |
| 727 | result |= TIOCM_CD; |
| 728 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 729 | dbg("%s() = 0x%08x", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 730 | |
| 731 | return result; |
| 732 | } |
| 733 | |
| 734 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) |
| 735 | { |
| 736 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 737 | unsigned long flags; |
| 738 | unsigned int prev, status; |
| 739 | unsigned int changed; |
| 740 | |
| 741 | spin_lock_irqsave(&priv->lock, flags); |
| 742 | prev = priv->status.pin_state; |
| 743 | spin_unlock_irqrestore(&priv->lock, flags); |
| 744 | |
| 745 | while (1) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 746 | wait_event_interruptible(priv->intr_wait, |
| 747 | priv->status.pin_state != prev); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 748 | if (signal_pending(current)) |
| 749 | return -ERESTARTSYS; |
| 750 | |
| 751 | spin_lock_irqsave(&priv->lock, flags); |
| 752 | status = priv->status.pin_state & PIN_MASK; |
| 753 | spin_unlock_irqrestore(&priv->lock, flags); |
| 754 | |
| 755 | changed = prev ^ status; |
| 756 | /* FIXME: check if this is correct (active high/low) */ |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 757 | if (((arg & TIOCM_RNG) && (changed & PIN_RI)) || |
| 758 | ((arg & TIOCM_DSR) && (changed & PIN_DSR)) || |
| 759 | ((arg & TIOCM_CD) && (changed & PIN_DCD)) || |
| 760 | ((arg & TIOCM_CTS) && (changed & PIN_CTS))) |
| 761 | return 0; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 762 | prev = status; |
| 763 | } |
| 764 | |
| 765 | /* NOTREACHED */ |
| 766 | return 0; |
| 767 | } |
| 768 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 769 | static int oti6858_ioctl(struct tty_struct *tty, struct file *file, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 770 | unsigned int cmd, unsigned long arg) |
| 771 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 772 | struct usb_serial_port *port = tty->driver_data; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 773 | |
| 774 | dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 775 | __func__, port->number, cmd, arg); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 776 | |
| 777 | switch (cmd) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 778 | case TIOCMIWAIT: |
| 779 | dbg("%s(): TIOCMIWAIT", __func__); |
| 780 | return wait_modem_info(port, arg); |
| 781 | default: |
| 782 | dbg("%s(): 0x%04x not supported", __func__, cmd); |
| 783 | break; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 784 | } |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 785 | return -ENOIOCTLCMD; |
| 786 | } |
| 787 | |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 788 | |
Alan Stern | f9c99bb | 2009-06-02 11:53:55 -0400 | [diff] [blame] | 789 | static void oti6858_release(struct usb_serial *serial) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 790 | { |
| 791 | struct oti6858_private *priv; |
| 792 | int i; |
| 793 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 794 | dbg("%s()", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 795 | |
| 796 | for (i = 0; i < serial->num_ports; ++i) { |
| 797 | priv = usb_get_serial_port_data(serial->port[i]); |
| 798 | if (priv) { |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 799 | oti6858_buf_free(priv->buf); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 800 | kfree(priv); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | static void oti6858_read_int_callback(struct urb *urb) |
| 806 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 807 | struct usb_serial_port *port = urb->context; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 808 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 809 | int transient = 0, can_recv = 0, resubmit = 1; |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 810 | int status = urb->status; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 811 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 812 | dbg("%s(port = %d, status = %d)", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 813 | __func__, port->number, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 814 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 815 | switch (status) { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 816 | case 0: |
| 817 | /* success */ |
| 818 | break; |
| 819 | case -ECONNRESET: |
| 820 | case -ENOENT: |
| 821 | case -ESHUTDOWN: |
| 822 | /* this urb is terminated, clean up */ |
| 823 | dbg("%s(): urb shutting down with status: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 824 | __func__, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 825 | return; |
| 826 | default: |
| 827 | dbg("%s(): nonzero urb status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 828 | __func__, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 829 | break; |
| 830 | } |
| 831 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 832 | if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 833 | struct oti6858_control_pkt *xs = urb->transfer_buffer; |
| 834 | unsigned long flags; |
| 835 | |
| 836 | spin_lock_irqsave(&priv->lock, flags); |
| 837 | |
| 838 | if (!priv->transient) { |
| 839 | if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) { |
| 840 | if (xs->rx_bytes_avail == 0) { |
| 841 | priv->transient = 4; |
| 842 | priv->setup_done = 0; |
| 843 | resubmit = 0; |
| 844 | dbg("%s(): scheduling setup_line()", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 845 | __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 846 | schedule_delayed_work(&priv->delayed_setup_work, 0); |
| 847 | } |
| 848 | } |
| 849 | } else { |
| 850 | if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) { |
| 851 | priv->transient = 0; |
| 852 | } else if (!priv->setup_done) { |
| 853 | resubmit = 0; |
| 854 | } else if (--priv->transient == 0) { |
| 855 | if (xs->rx_bytes_avail == 0) { |
| 856 | priv->transient = 4; |
| 857 | priv->setup_done = 0; |
| 858 | resubmit = 0; |
| 859 | dbg("%s(): scheduling setup_line()", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 860 | __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 861 | schedule_delayed_work(&priv->delayed_setup_work, 0); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | if (!priv->transient) { |
| 867 | if (xs->pin_state != priv->status.pin_state) |
| 868 | wake_up_interruptible(&priv->intr_wait); |
| 869 | memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE); |
| 870 | } |
| 871 | |
| 872 | if (!priv->transient && xs->rx_bytes_avail != 0) { |
| 873 | can_recv = xs->rx_bytes_avail; |
| 874 | priv->flags.read_urb_in_use = 1; |
| 875 | } |
| 876 | |
| 877 | transient = priv->transient; |
| 878 | spin_unlock_irqrestore(&priv->lock, flags); |
| 879 | } |
| 880 | |
| 881 | if (can_recv) { |
| 882 | int result; |
| 883 | |
| 884 | port->read_urb->dev = port->serial->dev; |
| 885 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 886 | if (result != 0) { |
| 887 | priv->flags.read_urb_in_use = 0; |
| 888 | dev_err(&port->dev, "%s(): usb_submit_urb() failed," |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 889 | " error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 890 | } else { |
| 891 | resubmit = 0; |
| 892 | } |
| 893 | } else if (!transient) { |
| 894 | unsigned long flags; |
| 895 | |
| 896 | spin_lock_irqsave(&priv->lock, flags); |
| 897 | if (priv->flags.write_urb_in_use == 0 |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 898 | && oti6858_buf_data_avail(priv->buf) != 0) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 899 | schedule_delayed_work(&priv->delayed_write_work, 0); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 900 | resubmit = 0; |
| 901 | } |
| 902 | spin_unlock_irqrestore(&priv->lock, flags); |
| 903 | } |
| 904 | |
| 905 | if (resubmit) { |
| 906 | int result; |
| 907 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 908 | /* dbg("%s(): submitting interrupt urb", __func__); */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 909 | urb->dev = port->serial->dev; |
| 910 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 911 | if (result != 0) { |
| 912 | dev_err(&urb->dev->dev, |
| 913 | "%s(): usb_submit_urb() failed with" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 914 | " error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | static void oti6858_read_bulk_callback(struct urb *urb) |
| 920 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 921 | struct usb_serial_port *port = urb->context; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 922 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
| 923 | struct tty_struct *tty; |
| 924 | unsigned char *data = urb->transfer_buffer; |
| 925 | unsigned long flags; |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 926 | int status = urb->status; |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 927 | int result; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 928 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 929 | dbg("%s(port = %d, status = %d)", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 930 | __func__, port->number, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 931 | |
| 932 | spin_lock_irqsave(&priv->lock, flags); |
| 933 | priv->flags.read_urb_in_use = 0; |
| 934 | spin_unlock_irqrestore(&priv->lock, flags); |
| 935 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 936 | if (status != 0) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 937 | if (!port->port.count) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 938 | dbg("%s(): port is closed, exiting", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 939 | return; |
| 940 | } |
| 941 | /* |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 942 | if (status == -EPROTO) { |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 943 | * PL2303 mysteriously fails with -EPROTO reschedule |
| 944 | the read * |
| 945 | dbg("%s - caught -EPROTO, resubmitting the urb", |
| 946 | __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 947 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 948 | if (result) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 949 | dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 950 | return; |
| 951 | } |
| 952 | */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 953 | dbg("%s(): unable to handle the error, exiting", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 954 | return; |
| 955 | } |
| 956 | |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 957 | tty = tty_port_tty_get(&port->port); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 958 | if (tty != NULL && urb->actual_length > 0) { |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 959 | tty_insert_flip_string(tty, data, urb->actual_length); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 960 | tty_flip_buffer_push(tty); |
| 961 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 962 | tty_kref_put(tty); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 963 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 964 | /* schedule the interrupt urb if we are still open */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 965 | if (port->port.count != 0) { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 966 | port->interrupt_in_urb->dev = port->serial->dev; |
| 967 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
| 968 | if (result != 0) { |
| 969 | dev_err(&port->dev, "%s(): usb_submit_urb() failed," |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 970 | " error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | static void oti6858_write_bulk_callback(struct urb *urb) |
| 976 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 977 | struct usb_serial_port *port = urb->context; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 978 | struct oti6858_private *priv = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 979 | int status = urb->status; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 980 | int result; |
| 981 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 982 | dbg("%s(port = %d, status = %d)", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 983 | __func__, port->number, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 984 | |
Greg Kroah-Hartman | 78c26ae | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 985 | switch (status) { |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 986 | case 0: |
| 987 | /* success */ |
| 988 | break; |
| 989 | case -ECONNRESET: |
| 990 | case -ENOENT: |
| 991 | case -ESHUTDOWN: |
| 992 | /* this urb is terminated, clean up */ |
| 993 | dbg("%s(): urb shutting down with status: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 994 | __func__, status); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 995 | priv->flags.write_urb_in_use = 0; |
| 996 | return; |
| 997 | default: |
| 998 | /* error in the urb, so we have to resubmit it */ |
| 999 | dbg("%s(): nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1000 | __func__, status); |
| 1001 | dbg("%s(): overflow in write", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1002 | |
| 1003 | port->write_urb->transfer_buffer_length = 1; |
| 1004 | port->write_urb->dev = port->serial->dev; |
| 1005 | result = usb_submit_urb(port->write_urb, GFP_ATOMIC); |
| 1006 | if (result) { |
| 1007 | dev_err(&port->dev, "%s(): usb_submit_urb() failed," |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1008 | " error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1009 | } else { |
| 1010 | return; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | priv->flags.write_urb_in_use = 0; |
| 1015 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 1016 | /* schedule the interrupt urb if we are still open */ |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1017 | port->interrupt_in_urb->dev = port->serial->dev; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1018 | dbg("%s(): submitting interrupt urb", __func__); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1019 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
| 1020 | if (result != 0) { |
| 1021 | dev_err(&port->dev, "%s(): failed submitting int urb," |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1022 | " error %d\n", __func__, result); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1028 | * oti6858_buf_alloc |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1029 | * |
| 1030 | * Allocate a circular buffer and all associated memory. |
| 1031 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1032 | static struct oti6858_buf *oti6858_buf_alloc(unsigned int size) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1033 | { |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1034 | struct oti6858_buf *pb; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1035 | |
| 1036 | if (size == 0) |
| 1037 | return NULL; |
| 1038 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1039 | pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1040 | if (pb == NULL) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | pb->buf_buf = kmalloc(size, GFP_KERNEL); |
| 1044 | if (pb->buf_buf == NULL) { |
| 1045 | kfree(pb); |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
| 1049 | pb->buf_size = size; |
| 1050 | pb->buf_get = pb->buf_put = pb->buf_buf; |
| 1051 | |
| 1052 | return pb; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1056 | * oti6858_buf_free |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1057 | * |
| 1058 | * Free the buffer and all associated memory. |
| 1059 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1060 | static void oti6858_buf_free(struct oti6858_buf *pb) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1061 | { |
| 1062 | if (pb) { |
| 1063 | kfree(pb->buf_buf); |
| 1064 | kfree(pb); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1069 | * oti6858_buf_clear |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1070 | * |
| 1071 | * Clear out all data in the circular buffer. |
| 1072 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1073 | static void oti6858_buf_clear(struct oti6858_buf *pb) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1074 | { |
| 1075 | if (pb != NULL) { |
| 1076 | /* equivalent to a get of all data available */ |
| 1077 | pb->buf_get = pb->buf_put; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1082 | * oti6858_buf_data_avail |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1083 | * |
| 1084 | * Return the number of bytes of data available in the circular |
| 1085 | * buffer. |
| 1086 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1087 | static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1088 | { |
| 1089 | if (pb == NULL) |
| 1090 | return 0; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 1091 | return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1095 | * oti6858_buf_space_avail |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1096 | * |
| 1097 | * Return the number of bytes of space available in the circular |
| 1098 | * buffer. |
| 1099 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1100 | static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1101 | { |
| 1102 | if (pb == NULL) |
| 1103 | return 0; |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 1104 | return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size; |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1108 | * oti6858_buf_put |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1109 | * |
| 1110 | * Copy data data from a user buffer and put it into the circular buffer. |
| 1111 | * Restrict to the amount of space available. |
| 1112 | * |
| 1113 | * Return the number of bytes copied. |
| 1114 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1115 | static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1116 | unsigned int count) |
| 1117 | { |
| 1118 | unsigned int len; |
| 1119 | |
| 1120 | if (pb == NULL) |
| 1121 | return 0; |
| 1122 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1123 | len = oti6858_buf_space_avail(pb); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1124 | if (count > len) |
| 1125 | count = len; |
| 1126 | |
| 1127 | if (count == 0) |
| 1128 | return 0; |
| 1129 | |
| 1130 | len = pb->buf_buf + pb->buf_size - pb->buf_put; |
| 1131 | if (count > len) { |
| 1132 | memcpy(pb->buf_put, buf, len); |
| 1133 | memcpy(pb->buf_buf, buf+len, count - len); |
| 1134 | pb->buf_put = pb->buf_buf + count - len; |
| 1135 | } else { |
| 1136 | memcpy(pb->buf_put, buf, count); |
| 1137 | if (count < len) |
| 1138 | pb->buf_put += count; |
| 1139 | else /* count == len */ |
| 1140 | pb->buf_put = pb->buf_buf; |
| 1141 | } |
| 1142 | |
| 1143 | return count; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1147 | * oti6858_buf_get |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1148 | * |
| 1149 | * Get data from the circular buffer and copy to the given buffer. |
| 1150 | * Restrict to the amount of data available. |
| 1151 | * |
| 1152 | * Return the number of bytes copied. |
| 1153 | */ |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1154 | static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf, |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1155 | unsigned int count) |
| 1156 | { |
| 1157 | unsigned int len; |
| 1158 | |
| 1159 | if (pb == NULL) |
| 1160 | return 0; |
| 1161 | |
Alan Cox | b0a239d | 2008-01-19 16:02:37 +0000 | [diff] [blame] | 1162 | len = oti6858_buf_data_avail(pb); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1163 | if (count > len) |
| 1164 | count = len; |
| 1165 | |
| 1166 | if (count == 0) |
| 1167 | return 0; |
| 1168 | |
| 1169 | len = pb->buf_buf + pb->buf_size - pb->buf_get; |
| 1170 | if (count > len) { |
| 1171 | memcpy(buf, pb->buf_get, len); |
| 1172 | memcpy(buf+len, pb->buf_buf, count - len); |
| 1173 | pb->buf_get = pb->buf_buf + count - len; |
| 1174 | } else { |
| 1175 | memcpy(buf, pb->buf_get, count); |
| 1176 | if (count < len) |
| 1177 | pb->buf_get += count; |
| 1178 | else /* count == len */ |
| 1179 | pb->buf_get = pb->buf_buf; |
| 1180 | } |
| 1181 | |
| 1182 | return count; |
| 1183 | } |
| 1184 | |
| 1185 | /* module description and (de)initialization */ |
| 1186 | |
| 1187 | static int __init oti6858_init(void) |
| 1188 | { |
| 1189 | int retval; |
| 1190 | |
Alan Cox | 2a77c81 | 2008-07-22 11:15:36 +0100 | [diff] [blame] | 1191 | retval = usb_serial_register(&oti6858_device); |
| 1192 | if (retval == 0) { |
| 1193 | retval = usb_register(&oti6858_driver); |
| 1194 | if (retval) |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1195 | usb_serial_deregister(&oti6858_device); |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1196 | } |
Kees Lemmens | 49cdee0 | 2007-03-27 12:34:30 +0200 | [diff] [blame] | 1197 | return retval; |
| 1198 | } |
| 1199 | |
| 1200 | static void __exit oti6858_exit(void) |
| 1201 | { |
| 1202 | usb_deregister(&oti6858_driver); |
| 1203 | usb_serial_deregister(&oti6858_device); |
| 1204 | } |
| 1205 | |
| 1206 | module_init(oti6858_init); |
| 1207 | module_exit(oti6858_exit); |
| 1208 | |
| 1209 | MODULE_DESCRIPTION(OTI6858_DESCRIPTION); |
| 1210 | MODULE_AUTHOR(OTI6858_AUTHOR); |
| 1211 | MODULE_VERSION(OTI6858_VERSION); |
| 1212 | MODULE_LICENSE("GPL"); |
| 1213 | |
| 1214 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1215 | MODULE_PARM_DESC(debug, "enable debug output"); |
| 1216 | |