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