2 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
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.
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.
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
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
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.
28 * See Documentation/usb/usb-serial.txt for more information on using this driver
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
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>
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"
60 static struct usb_device_id id_table [] = {
61 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
65 MODULE_DEVICE_TABLE(usb, id_table);
67 static struct usb_driver oti6858_driver = {
69 .probe = usb_serial_probe,
70 .disconnect = usb_serial_disconnect,
78 /* buffering code, copied from pl2303 driver */
79 #define PL2303_BUF_SIZE 1024
80 #define PL2303_TMP_BUF_SIZE 1024
83 unsigned int buf_size;
90 #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
91 #define OTI6858_REQ_T_GET_STATUS 0x01
93 #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
94 #define OTI6858_REQ_T_SET_LINE 0x00
96 #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
97 #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
99 /* format of the control packet */
100 struct oti6858_control_pkt {
101 u16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
102 #define OTI6858_MAX_BAUD_RATE 3000000
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
124 #define TX_BUFFER_EMPTIED 0x09
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 */;
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) )
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);
163 /* functions operating on buffers */
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,
171 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
176 static struct usb_serial_driver oti6858_device = {
178 .owner = THIS_MODULE,
181 .id_table = id_table,
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,
200 struct oti6858_private {
203 struct oti6858_buf *buf;
204 struct oti6858_control_pkt status;
209 u8 termios_initialized;
211 struct delayed_work delayed_write_work;
220 struct delayed_work delayed_setup_work;
222 wait_queue_head_t intr_wait;
223 struct usb_serial_port *port; /* USB port with which associated */
227 /* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
228 #define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
230 static void setup_line(struct work_struct *work)
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;
238 dbg("%s(port = %d)", __func__, port->number);
240 if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
241 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
242 /* we will try again */
243 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
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,
252 new_setup, OTI6858_CTRL_PKT_SIZE,
255 if (result != OTI6858_CTRL_PKT_SIZE) {
256 dev_err(&port->dev, "%s(): error reading status\n", __func__);
258 /* we will try again */
259 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
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;
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,
275 new_setup, OTI6858_CTRL_PKT_SIZE,
278 spin_unlock_irqrestore(&priv->lock, flags);
283 spin_lock_irqsave(&priv->lock, flags);
284 if (result != OTI6858_CTRL_PKT_SIZE)
286 priv->setup_done = 1;
287 spin_unlock_irqrestore(&priv->lock, flags);
289 dbg("%s(): submitting interrupt urb", __func__);
290 port->interrupt_in_urb->dev = port->serial->dev;
291 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
293 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
294 " with error %d\n", __func__, result);
298 void send_data(struct work_struct *work)
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;
306 dbg("%s(port = %d)", __func__, port->number);
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));
314 priv->flags.write_urb_in_use = 1;
316 count = oti6858_buf_data_avail(priv->buf);
317 spin_unlock_irqrestore(&priv->lock, flags);
318 if (count > port->bulk_out_size)
319 count = port->bulk_out_size;
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)
332 priv->flags.write_urb_in_use = 0;
334 dbg("%s(): submitting interrupt urb", __func__);
335 port->interrupt_in_urb->dev = port->serial->dev;
336 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
338 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
339 " with error %d\n", __func__, result);
344 spin_lock_irqsave(&priv->lock, flags);
345 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
346 spin_unlock_irqrestore(&priv->lock, flags);
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);
352 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
353 " with error %d\n", __func__, result);
354 priv->flags.write_urb_in_use = 0;
357 usb_serial_port_softint(port);
360 static int oti6858_startup(struct usb_serial *serial)
362 struct usb_serial_port *port = serial->port[0];
363 struct oti6858_private *priv;
366 for (i = 0; i < serial->num_ports; ++i) {
367 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
370 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
371 if (priv->buf == NULL) {
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]);
381 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
382 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
384 usb_set_serial_port_data(serial->port[i], priv);
386 if (i == serial->num_ports)
389 for (--i; i >= 0; --i) {
390 priv = usb_get_serial_port_data(serial->port[i]);
391 oti6858_buf_free(priv->buf);
393 usb_set_serial_port_data(serial->port[i], NULL);
398 static int oti6858_write(struct usb_serial_port *port,
399 const unsigned char *buf, int count)
401 struct oti6858_private *priv = usb_get_serial_port_data(port);
404 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
409 spin_lock_irqsave(&priv->lock, flags);
410 count = oti6858_buf_put(priv->buf, buf, count);
411 spin_unlock_irqrestore(&priv->lock, flags);
416 static int oti6858_write_room(struct usb_serial_port *port)
418 struct oti6858_private *priv = usb_get_serial_port_data(port);
422 dbg("%s(port = %d)", __func__, port->number);
424 spin_lock_irqsave(&priv->lock, flags);
425 room = oti6858_buf_space_avail(priv->buf);
426 spin_unlock_irqrestore(&priv->lock, flags);
431 static int oti6858_chars_in_buffer(struct usb_serial_port *port)
433 struct oti6858_private *priv = usb_get_serial_port_data(port);
437 dbg("%s(port = %d)", __func__, port->number);
439 spin_lock_irqsave(&priv->lock, flags);
440 chars = oti6858_buf_data_avail(priv->buf);
441 spin_unlock_irqrestore(&priv->lock, flags);
446 static void oti6858_set_termios(struct usb_serial_port *port,
447 struct ktermios *old_termios)
449 struct oti6858_private *priv = usb_get_serial_port_data(port);
452 u8 frame_fmt, control;
456 dbg("%s(port = %d)", __func__, port->number);
458 if (!port->tty || !port->tty->termios) {
459 dbg("%s(): no tty structures", __func__);
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;
468 port->tty->termios->c_ispeed = 38400;
469 port->tty->termios->c_ospeed = 38400;
471 spin_unlock_irqrestore(&priv->lock, flags);
473 cflag = port->tty->termios->c_cflag;
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);
481 frame_fmt &= ~FMT_DATA_BITS_MASK;
482 switch (cflag & CSIZE) {
484 frame_fmt |= FMT_DATA_BITS_5;
487 frame_fmt |= FMT_DATA_BITS_6;
490 frame_fmt |= FMT_DATA_BITS_7;
494 frame_fmt |= FMT_DATA_BITS_8;
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
503 br = tty_get_baud_rate(port->tty);
508 br = min(br, OTI6858_MAX_BAUD_RATE);
510 divisor = (96000000 + 8 * br) / (16 * br);
511 real_br = 96000000 / (16 * divisor);
512 divisor = cpu_to_le16(divisor);
513 tty_encode_baud_rate(port->tty, real_br, real_br);
516 frame_fmt &= ~FMT_STOP_BITS_MASK;
517 if ((cflag & CSTOPB) != 0) {
518 frame_fmt |= FMT_STOP_BITS_2;
520 frame_fmt |= FMT_STOP_BITS_1;
523 frame_fmt &= ~FMT_PARITY_MASK;
524 if ((cflag & PARENB) != 0) {
525 if ((cflag & PARODD) != 0) {
526 frame_fmt |= FMT_PARITY_ODD;
528 frame_fmt |= FMT_PARITY_EVEN;
531 frame_fmt |= FMT_PARITY_NONE;
534 control &= ~CONTROL_MASK;
535 if ((cflag & CRTSCTS) != 0)
536 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
538 /* change control lines if we are switching to or from B0 */
540 spin_lock_irqsave(&priv->lock, flags);
541 control = priv->line_control;
542 if ((cflag & CBAUD) == B0)
543 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
545 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
546 if (control != priv->line_control) {
547 control = priv->line_control;
548 spin_unlock_irqrestore(&priv->lock, flags);
549 set_control_lines(serial->dev, control);
551 spin_unlock_irqrestore(&priv->lock, flags);
555 spin_lock_irqsave(&priv->lock, flags);
556 if (divisor != priv->pending_setup.divisor
557 || control != priv->pending_setup.control
558 || frame_fmt != priv->pending_setup.frame_fmt) {
559 priv->pending_setup.divisor = divisor;
560 priv->pending_setup.control = control;
561 priv->pending_setup.frame_fmt = frame_fmt;
563 spin_unlock_irqrestore(&priv->lock, flags);
566 static int oti6858_open(struct usb_serial_port *port, struct file *filp)
568 struct oti6858_private *priv = usb_get_serial_port_data(port);
569 struct ktermios tmp_termios;
570 struct usb_serial *serial = port->serial;
571 struct oti6858_control_pkt *buf;
575 dbg("%s(port = %d)", __func__, port->number);
577 usb_clear_halt(serial->dev, port->write_urb->pipe);
578 usb_clear_halt(serial->dev, port->read_urb->pipe);
580 if (port->open_count != 1)
583 if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
584 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
588 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
589 OTI6858_REQ_T_GET_STATUS,
590 OTI6858_REQ_GET_STATUS,
592 buf, OTI6858_CTRL_PKT_SIZE,
594 if (result != OTI6858_CTRL_PKT_SIZE) {
595 /* assume default (after power-on reset) values */
596 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
597 buf->frame_fmt = 0x03; /* 8N1 */
598 buf->something = 0x43;
599 buf->control = 0x4c; /* DTR, RTS */
600 buf->tx_status = 0x00;
601 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
602 buf->rx_bytes_avail = 0x00;
605 spin_lock_irqsave(&priv->lock, flags);
606 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
607 priv->pending_setup.divisor = buf->divisor;
608 priv->pending_setup.frame_fmt = buf->frame_fmt;
609 priv->pending_setup.control = buf->control;
610 spin_unlock_irqrestore(&priv->lock, flags);
613 dbg("%s(): submitting interrupt urb", __func__);
614 port->interrupt_in_urb->dev = serial->dev;
615 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
617 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
618 " with error %d\n", __func__, result);
619 oti6858_close(port, NULL);
625 oti6858_set_termios(port, &tmp_termios);
630 static void oti6858_close(struct usb_serial_port *port, struct file *filp)
632 struct oti6858_private *priv = usb_get_serial_port_data(port);
637 dbg("%s(port = %d)", __func__, port->number);
639 /* wait for data to drain from the buffer */
640 spin_lock_irqsave(&priv->lock, flags);
641 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
642 init_waitqueue_entry(&wait, current);
643 add_wait_queue(&port->tty->write_wait, &wait);
644 dbg("%s(): entering wait loop", __func__);
646 set_current_state(TASK_INTERRUPTIBLE);
647 if (oti6858_buf_data_avail(priv->buf) == 0
648 || timeout == 0 || signal_pending(current)
649 || port->serial->disconnected)
651 spin_unlock_irqrestore(&priv->lock, flags);
652 timeout = schedule_timeout(timeout);
653 spin_lock_irqsave(&priv->lock, flags);
655 set_current_state(TASK_RUNNING);
656 remove_wait_queue(&port->tty->write_wait, &wait);
657 dbg("%s(): after wait loop", __func__);
659 /* clear out any remaining data in the buffer */
660 oti6858_buf_clear(priv->buf);
661 spin_unlock_irqrestore(&priv->lock, flags);
663 /* wait for characters to drain from the device */
664 /* (this is long enough for the entire 256 byte */
665 /* pl2303 hardware buffer to drain with no flow */
666 /* control for data rates of 1200 bps or more, */
667 /* for lower rates we should really know how much */
668 /* data is in the buffer to compute a delay */
669 /* that is not unnecessarily long) */
671 bps = tty_get_baud_rate(port->tty);
673 timeout = max((HZ*2560)/bps,HZ/10);
677 schedule_timeout_interruptible(timeout);
678 dbg("%s(): after schedule_timeout_interruptible()", __func__);
680 /* cancel scheduled setup */
681 cancel_delayed_work(&priv->delayed_setup_work);
682 cancel_delayed_work(&priv->delayed_write_work);
683 flush_scheduled_work();
685 /* shutdown our urbs */
686 dbg("%s(): shutting down urbs", __func__);
687 usb_kill_urb(port->write_urb);
688 usb_kill_urb(port->read_urb);
689 usb_kill_urb(port->interrupt_in_urb);
692 if (port->tty && (port->tty->termios->c_cflag) & HUPCL) {
694 spin_lock_irqsave(&priv->lock, flags);
695 priv->pending_setup.control &= ~CONTROL_MASK;
696 spin_unlock_irqrestore(&priv->lock, flags);
701 static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
702 unsigned int set, unsigned int clear)
704 struct oti6858_private *priv = usb_get_serial_port_data(port);
708 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
709 __func__, port->number, set, clear);
711 if (!usb_get_intfdata(port->serial->interface))
714 /* FIXME: check if this is correct (active high/low) */
715 spin_lock_irqsave(&priv->lock, flags);
716 control = priv->pending_setup.control;
717 if ((set & TIOCM_RTS) != 0)
718 control |= CONTROL_RTS_HIGH;
719 if ((set & TIOCM_DTR) != 0)
720 control |= CONTROL_DTR_HIGH;
721 if ((clear & TIOCM_RTS) != 0)
722 control &= ~CONTROL_RTS_HIGH;
723 if ((clear & TIOCM_DTR) != 0)
724 control &= ~CONTROL_DTR_HIGH;
726 if (control != priv->pending_setup.control) {
727 priv->pending_setup.control = control;
729 spin_unlock_irqrestore(&priv->lock, flags);
734 static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file)
736 struct oti6858_private *priv = usb_get_serial_port_data(port);
741 dbg("%s(port = %d)", __func__, port->number);
743 if (!usb_get_intfdata(port->serial->interface))
746 spin_lock_irqsave(&priv->lock, flags);
747 pin_state = priv->status.pin_state & PIN_MASK;
748 spin_unlock_irqrestore(&priv->lock, flags);
750 /* FIXME: check if this is correct (active high/low) */
751 if ((pin_state & PIN_RTS) != 0)
753 if ((pin_state & PIN_CTS) != 0)
755 if ((pin_state & PIN_DSR) != 0)
757 if ((pin_state & PIN_DTR) != 0)
759 if ((pin_state & PIN_RI) != 0)
761 if ((pin_state & PIN_DCD) != 0)
764 dbg("%s() = 0x%08x", __func__, result);
769 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
771 struct oti6858_private *priv = usb_get_serial_port_data(port);
773 unsigned int prev, status;
774 unsigned int changed;
776 spin_lock_irqsave(&priv->lock, flags);
777 prev = priv->status.pin_state;
778 spin_unlock_irqrestore(&priv->lock, flags);
781 wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
782 if (signal_pending(current))
785 spin_lock_irqsave(&priv->lock, flags);
786 status = priv->status.pin_state & PIN_MASK;
787 spin_unlock_irqrestore(&priv->lock, flags);
789 changed = prev ^ status;
790 /* FIXME: check if this is correct (active high/low) */
791 if ( ((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
792 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
793 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
794 ((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
804 static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
805 unsigned int cmd, unsigned long arg)
807 void __user *user_arg = (void __user *) arg;
810 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
811 __func__, port->number, cmd, arg);
815 if (copy_from_user(&x, user_arg, sizeof(x)))
817 return oti6858_tiocmset(port, NULL, x, 0);
820 if (copy_from_user(&x, user_arg, sizeof(x)))
822 return oti6858_tiocmset(port, NULL, 0, x);
825 dbg("%s(): TIOCMIWAIT", __func__);
826 return wait_modem_info(port, arg);
829 dbg("%s(): 0x%04x not supported", __func__, cmd);
836 static void oti6858_break_ctl(struct usb_serial_port *port, int break_state)
840 dbg("%s(port = %d)", __func__, port->number);
842 state = (break_state == 0) ? 0 : 1;
843 dbg("%s(): turning break %s", __func__, state ? "on" : "off");
847 result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
848 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
851 dbg("%s(): error sending break", __func__);
855 static void oti6858_shutdown(struct usb_serial *serial)
857 struct oti6858_private *priv;
860 dbg("%s()", __func__);
862 for (i = 0; i < serial->num_ports; ++i) {
863 priv = usb_get_serial_port_data(serial->port[i]);
865 oti6858_buf_free(priv->buf);
867 usb_set_serial_port_data(serial->port[i], NULL);
872 static void oti6858_read_int_callback(struct urb *urb)
874 struct usb_serial_port *port = urb->context;
875 struct oti6858_private *priv = usb_get_serial_port_data(port);
876 int transient = 0, can_recv = 0, resubmit = 1;
877 int status = urb->status;
879 dbg("%s(port = %d, status = %d)",
880 __func__, port->number, status);
889 /* this urb is terminated, clean up */
890 dbg("%s(): urb shutting down with status: %d",
894 dbg("%s(): nonzero urb status received: %d",
899 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
900 struct oti6858_control_pkt *xs = urb->transfer_buffer;
903 spin_lock_irqsave(&priv->lock, flags);
905 if (!priv->transient) {
906 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
907 if (xs->rx_bytes_avail == 0) {
909 priv->setup_done = 0;
911 dbg("%s(): scheduling setup_line()",
913 schedule_delayed_work(&priv->delayed_setup_work, 0);
917 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
919 } else if (!priv->setup_done) {
921 } else if (--priv->transient == 0) {
922 if (xs->rx_bytes_avail == 0) {
924 priv->setup_done = 0;
926 dbg("%s(): scheduling setup_line()",
928 schedule_delayed_work(&priv->delayed_setup_work, 0);
933 if (!priv->transient) {
934 if (xs->pin_state != priv->status.pin_state)
935 wake_up_interruptible(&priv->intr_wait);
936 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
939 if (!priv->transient && xs->rx_bytes_avail != 0) {
940 can_recv = xs->rx_bytes_avail;
941 priv->flags.read_urb_in_use = 1;
944 transient = priv->transient;
945 spin_unlock_irqrestore(&priv->lock, flags);
951 port->read_urb->dev = port->serial->dev;
952 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
954 priv->flags.read_urb_in_use = 0;
955 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
956 " error %d\n", __func__, result);
960 } else if (!transient) {
963 spin_lock_irqsave(&priv->lock, flags);
964 if (priv->flags.write_urb_in_use == 0
965 && oti6858_buf_data_avail(priv->buf) != 0) {
966 schedule_delayed_work(&priv->delayed_write_work,0);
969 spin_unlock_irqrestore(&priv->lock, flags);
975 // dbg("%s(): submitting interrupt urb", __func__);
976 urb->dev = port->serial->dev;
977 result = usb_submit_urb(urb, GFP_ATOMIC);
979 dev_err(&urb->dev->dev,
980 "%s(): usb_submit_urb() failed with"
981 " error %d\n", __func__, result);
986 static void oti6858_read_bulk_callback(struct urb *urb)
988 struct usb_serial_port *port = urb->context;
989 struct oti6858_private *priv = usb_get_serial_port_data(port);
990 struct tty_struct *tty;
991 unsigned char *data = urb->transfer_buffer;
993 int status = urb->status;
996 dbg("%s(port = %d, status = %d)",
997 __func__, port->number, status);
999 spin_lock_irqsave(&priv->lock, flags);
1000 priv->flags.read_urb_in_use = 0;
1001 spin_unlock_irqrestore(&priv->lock, flags);
1004 if (!port->open_count) {
1005 dbg("%s(): port is closed, exiting", __func__);
1009 if (status == -EPROTO) {
1010 // PL2303 mysteriously fails with -EPROTO reschedule the read
1011 dbg("%s - caught -EPROTO, resubmitting the urb", __func__);
1012 result = usb_submit_urb(urb, GFP_ATOMIC);
1014 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
1018 dbg("%s(): unable to handle the error, exiting", __func__);
1023 if (tty != NULL && urb->actual_length > 0) {
1024 tty_insert_flip_string(tty, data, urb->actual_length);
1025 tty_flip_buffer_push(tty);
1028 // schedule the interrupt urb if we are still open */
1029 if (port->open_count != 0) {
1030 port->interrupt_in_urb->dev = port->serial->dev;
1031 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1033 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1034 " error %d\n", __func__, result);
1039 static void oti6858_write_bulk_callback(struct urb *urb)
1041 struct usb_serial_port *port = urb->context;
1042 struct oti6858_private *priv = usb_get_serial_port_data(port);
1043 int status = urb->status;
1046 dbg("%s(port = %d, status = %d)",
1047 __func__, port->number, status);
1056 /* this urb is terminated, clean up */
1057 dbg("%s(): urb shutting down with status: %d",
1059 priv->flags.write_urb_in_use = 0;
1062 /* error in the urb, so we have to resubmit it */
1063 dbg("%s(): nonzero write bulk status received: %d",
1065 dbg("%s(): overflow in write", __func__);
1067 port->write_urb->transfer_buffer_length = 1;
1068 port->write_urb->dev = port->serial->dev;
1069 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1071 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1072 " error %d\n", __func__, result);
1078 priv->flags.write_urb_in_use = 0;
1080 // schedule the interrupt urb if we are still open */
1081 port->interrupt_in_urb->dev = port->serial->dev;
1082 dbg("%s(): submitting interrupt urb", __func__);
1083 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1085 dev_err(&port->dev, "%s(): failed submitting int urb,"
1086 " error %d\n", __func__, result);
1094 * Allocate a circular buffer and all associated memory.
1096 static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
1098 struct oti6858_buf *pb;
1103 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
1107 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1108 if (pb->buf_buf == NULL) {
1113 pb->buf_size = size;
1114 pb->buf_get = pb->buf_put = pb->buf_buf;
1122 * Free the buffer and all associated memory.
1124 static void oti6858_buf_free(struct oti6858_buf *pb)
1135 * Clear out all data in the circular buffer.
1137 static void oti6858_buf_clear(struct oti6858_buf *pb)
1140 /* equivalent to a get of all data available */
1141 pb->buf_get = pb->buf_put;
1146 * oti6858_buf_data_avail
1148 * Return the number of bytes of data available in the circular
1151 static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
1155 return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
1159 * oti6858_buf_space_avail
1161 * Return the number of bytes of space available in the circular
1164 static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
1168 return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
1174 * Copy data data from a user buffer and put it into the circular buffer.
1175 * Restrict to the amount of space available.
1177 * Return the number of bytes copied.
1179 static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
1187 len = oti6858_buf_space_avail(pb);
1194 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1196 memcpy(pb->buf_put, buf, len);
1197 memcpy(pb->buf_buf, buf+len, count - len);
1198 pb->buf_put = pb->buf_buf + count - len;
1200 memcpy(pb->buf_put, buf, count);
1202 pb->buf_put += count;
1203 else /* count == len */
1204 pb->buf_put = pb->buf_buf;
1213 * Get data from the circular buffer and copy to the given buffer.
1214 * Restrict to the amount of data available.
1216 * Return the number of bytes copied.
1218 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
1226 len = oti6858_buf_data_avail(pb);
1233 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1235 memcpy(buf, pb->buf_get, len);
1236 memcpy(buf+len, pb->buf_buf, count - len);
1237 pb->buf_get = pb->buf_buf + count - len;
1239 memcpy(buf, pb->buf_get, count);
1241 pb->buf_get += count;
1242 else /* count == len */
1243 pb->buf_get = pb->buf_buf;
1249 /* module description and (de)initialization */
1251 static int __init oti6858_init(void)
1255 if ((retval = usb_serial_register(&oti6858_device)) == 0) {
1256 if ((retval = usb_register(&oti6858_driver)) != 0)
1257 usb_serial_deregister(&oti6858_device);
1265 static void __exit oti6858_exit(void)
1267 usb_deregister(&oti6858_driver);
1268 usb_serial_deregister(&oti6858_device);
1271 module_init(oti6858_init);
1272 module_exit(oti6858_exit);
1274 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1275 MODULE_AUTHOR(OTI6858_AUTHOR);
1276 MODULE_VERSION(OTI6858_VERSION);
1277 MODULE_LICENSE("GPL");
1279 module_param(debug, bool, S_IRUGO | S_IWUSR);
1280 MODULE_PARM_DESC(debug, "enable debug output");