initial commit with v3.6.7
[linux-3.6.7-moxart.git] / drivers / usb / serial / oti6858.c
blob3aa582e4a594ffb0bcf5557c8268455c54696877
1 /*
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
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!
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
29 * driver
31 * TODO:
32 * - implement correct flushing for ioctls and oti6858_close()
33 * - check how errors (rx overflow, parity error, framing error) are reported
34 * - implement oti6858_break_ctl()
35 * - implement more ioctls
36 * - test/implement flow control
37 * - allow setting custom baud rates
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/tty.h>
45 #include <linux/tty_driver.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/spinlock.h>
51 #include <linux/usb.h>
52 #include <linux/usb/serial.h>
53 #include <linux/uaccess.h>
54 #include <linux/kfifo.h>
55 #include "oti6858.h"
57 #define OTI6858_DESCRIPTION \
58 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
59 #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
60 #define OTI6858_VERSION "0.2"
62 static const struct usb_device_id id_table[] = {
63 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
64 { }
67 MODULE_DEVICE_TABLE(usb, id_table);
69 static bool debug;
71 /* requests */
72 #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
73 #define OTI6858_REQ_T_GET_STATUS 0x01
75 #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
76 #define OTI6858_REQ_T_SET_LINE 0x00
78 #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
79 #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
81 /* format of the control packet */
82 struct oti6858_control_pkt {
83 __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
84 #define OTI6858_MAX_BAUD_RATE 3000000
85 u8 frame_fmt;
86 #define FMT_STOP_BITS_MASK 0xc0
87 #define FMT_STOP_BITS_1 0x00
88 #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
89 #define FMT_PARITY_MASK 0x38
90 #define FMT_PARITY_NONE 0x00
91 #define FMT_PARITY_ODD 0x08
92 #define FMT_PARITY_EVEN 0x18
93 #define FMT_PARITY_MARK 0x28
94 #define FMT_PARITY_SPACE 0x38
95 #define FMT_DATA_BITS_MASK 0x03
96 #define FMT_DATA_BITS_5 0x00
97 #define FMT_DATA_BITS_6 0x01
98 #define FMT_DATA_BITS_7 0x02
99 #define FMT_DATA_BITS_8 0x03
100 u8 something; /* always equals 0x43 */
101 u8 control; /* settings of flow control lines */
102 #define CONTROL_MASK 0x0c
103 #define CONTROL_DTR_HIGH 0x08
104 #define CONTROL_RTS_HIGH 0x04
105 u8 tx_status;
106 #define TX_BUFFER_EMPTIED 0x09
107 u8 pin_state;
108 #define PIN_MASK 0x3f
109 #define PIN_RTS 0x20 /* output pin */
110 #define PIN_CTS 0x10 /* input pin, active low */
111 #define PIN_DSR 0x08 /* input pin, active low */
112 #define PIN_DTR 0x04 /* output pin */
113 #define PIN_RI 0x02 /* input pin, active low */
114 #define PIN_DCD 0x01 /* input pin, active low */
115 u8 rx_bytes_avail; /* number of bytes in rx buffer */;
118 #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
119 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
120 (((a)->divisor == (priv)->pending_setup.divisor) \
121 && ((a)->control == (priv)->pending_setup.control) \
122 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
124 /* function prototypes */
125 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
126 static void oti6858_close(struct usb_serial_port *port);
127 static void oti6858_set_termios(struct tty_struct *tty,
128 struct usb_serial_port *port, struct ktermios *old);
129 static void oti6858_init_termios(struct tty_struct *tty);
130 static int oti6858_ioctl(struct tty_struct *tty,
131 unsigned int cmd, unsigned long arg);
132 static void oti6858_read_int_callback(struct urb *urb);
133 static void oti6858_read_bulk_callback(struct urb *urb);
134 static void oti6858_write_bulk_callback(struct urb *urb);
135 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
136 const unsigned char *buf, int count);
137 static int oti6858_write_room(struct tty_struct *tty);
138 static int oti6858_chars_in_buffer(struct tty_struct *tty);
139 static int oti6858_tiocmget(struct tty_struct *tty);
140 static int oti6858_tiocmset(struct tty_struct *tty,
141 unsigned int set, unsigned int clear);
142 static int oti6858_port_probe(struct usb_serial_port *port);
143 static int oti6858_port_remove(struct usb_serial_port *port);
145 /* device info */
146 static struct usb_serial_driver oti6858_device = {
147 .driver = {
148 .owner = THIS_MODULE,
149 .name = "oti6858",
151 .id_table = id_table,
152 .num_ports = 1,
153 .open = oti6858_open,
154 .close = oti6858_close,
155 .write = oti6858_write,
156 .ioctl = oti6858_ioctl,
157 .set_termios = oti6858_set_termios,
158 .init_termios = oti6858_init_termios,
159 .tiocmget = oti6858_tiocmget,
160 .tiocmset = oti6858_tiocmset,
161 .read_bulk_callback = oti6858_read_bulk_callback,
162 .read_int_callback = oti6858_read_int_callback,
163 .write_bulk_callback = oti6858_write_bulk_callback,
164 .write_room = oti6858_write_room,
165 .chars_in_buffer = oti6858_chars_in_buffer,
166 .port_probe = oti6858_port_probe,
167 .port_remove = oti6858_port_remove,
170 static struct usb_serial_driver * const serial_drivers[] = {
171 &oti6858_device, NULL
174 struct oti6858_private {
175 spinlock_t lock;
177 struct oti6858_control_pkt status;
179 struct {
180 u8 read_urb_in_use;
181 u8 write_urb_in_use;
182 } flags;
183 struct delayed_work delayed_write_work;
185 struct {
186 __le16 divisor;
187 u8 frame_fmt;
188 u8 control;
189 } pending_setup;
190 u8 transient;
191 u8 setup_done;
192 struct delayed_work delayed_setup_work;
194 wait_queue_head_t intr_wait;
195 struct usb_serial_port *port; /* USB port with which associated */
198 static void setup_line(struct work_struct *work)
200 struct oti6858_private *priv = container_of(work,
201 struct oti6858_private, delayed_setup_work.work);
202 struct usb_serial_port *port = priv->port;
203 struct oti6858_control_pkt *new_setup;
204 unsigned long flags;
205 int result;
207 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
208 if (new_setup == NULL) {
209 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
210 /* we will try again */
211 schedule_delayed_work(&priv->delayed_setup_work,
212 msecs_to_jiffies(2));
213 return;
216 result = usb_control_msg(port->serial->dev,
217 usb_rcvctrlpipe(port->serial->dev, 0),
218 OTI6858_REQ_T_GET_STATUS,
219 OTI6858_REQ_GET_STATUS,
220 0, 0,
221 new_setup, OTI6858_CTRL_PKT_SIZE,
222 100);
224 if (result != OTI6858_CTRL_PKT_SIZE) {
225 dev_err(&port->dev, "%s(): error reading status\n", __func__);
226 kfree(new_setup);
227 /* we will try again */
228 schedule_delayed_work(&priv->delayed_setup_work,
229 msecs_to_jiffies(2));
230 return;
233 spin_lock_irqsave(&priv->lock, flags);
234 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
235 new_setup->divisor = priv->pending_setup.divisor;
236 new_setup->control = priv->pending_setup.control;
237 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
239 spin_unlock_irqrestore(&priv->lock, flags);
240 result = usb_control_msg(port->serial->dev,
241 usb_sndctrlpipe(port->serial->dev, 0),
242 OTI6858_REQ_T_SET_LINE,
243 OTI6858_REQ_SET_LINE,
244 0, 0,
245 new_setup, OTI6858_CTRL_PKT_SIZE,
246 100);
247 } else {
248 spin_unlock_irqrestore(&priv->lock, flags);
249 result = 0;
251 kfree(new_setup);
253 spin_lock_irqsave(&priv->lock, flags);
254 if (result != OTI6858_CTRL_PKT_SIZE)
255 priv->transient = 0;
256 priv->setup_done = 1;
257 spin_unlock_irqrestore(&priv->lock, flags);
259 dbg("%s(): submitting interrupt urb", __func__);
260 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
261 if (result != 0) {
262 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
263 " with error %d\n", __func__, result);
267 static void send_data(struct work_struct *work)
269 struct oti6858_private *priv = container_of(work,
270 struct oti6858_private, delayed_write_work.work);
271 struct usb_serial_port *port = priv->port;
272 int count = 0, result;
273 unsigned long flags;
274 u8 *allow;
276 spin_lock_irqsave(&priv->lock, flags);
277 if (priv->flags.write_urb_in_use) {
278 spin_unlock_irqrestore(&priv->lock, flags);
279 schedule_delayed_work(&priv->delayed_write_work,
280 msecs_to_jiffies(2));
281 return;
283 priv->flags.write_urb_in_use = 1;
284 spin_unlock_irqrestore(&priv->lock, flags);
286 spin_lock_irqsave(&port->lock, flags);
287 count = kfifo_len(&port->write_fifo);
288 spin_unlock_irqrestore(&port->lock, flags);
290 if (count > port->bulk_out_size)
291 count = port->bulk_out_size;
293 if (count != 0) {
294 allow = kmalloc(1, GFP_KERNEL);
295 if (!allow) {
296 dev_err_console(port, "%s(): kmalloc failed\n",
297 __func__);
298 return;
300 result = usb_control_msg(port->serial->dev,
301 usb_rcvctrlpipe(port->serial->dev, 0),
302 OTI6858_REQ_T_CHECK_TXBUFF,
303 OTI6858_REQ_CHECK_TXBUFF,
304 count, 0, allow, 1, 100);
305 if (result != 1 || *allow != 0)
306 count = 0;
307 kfree(allow);
310 if (count == 0) {
311 priv->flags.write_urb_in_use = 0;
313 dbg("%s(): submitting interrupt urb", __func__);
314 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
315 if (result != 0) {
316 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
317 " with error %d\n", __func__, result);
319 return;
322 count = kfifo_out_locked(&port->write_fifo,
323 port->write_urb->transfer_buffer,
324 count, &port->lock);
325 port->write_urb->transfer_buffer_length = count;
326 result = usb_submit_urb(port->write_urb, GFP_NOIO);
327 if (result != 0) {
328 dev_err_console(port, "%s(): usb_submit_urb() failed"
329 " with error %d\n", __func__, result);
330 priv->flags.write_urb_in_use = 0;
333 usb_serial_port_softint(port);
336 static int oti6858_port_probe(struct usb_serial_port *port)
338 struct oti6858_private *priv;
340 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
341 if (!priv)
342 return -ENOMEM;
344 spin_lock_init(&priv->lock);
345 init_waitqueue_head(&priv->intr_wait);
346 priv->port = port;
347 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
348 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
350 usb_set_serial_port_data(port, priv);
352 return 0;
355 static int oti6858_port_remove(struct usb_serial_port *port)
357 struct oti6858_private *priv;
359 priv = usb_get_serial_port_data(port);
360 kfree(priv);
362 return 0;
365 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
366 const unsigned char *buf, int count)
368 if (!count)
369 return count;
371 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
373 return count;
376 static int oti6858_write_room(struct tty_struct *tty)
378 struct usb_serial_port *port = tty->driver_data;
379 int room = 0;
380 unsigned long flags;
382 spin_lock_irqsave(&port->lock, flags);
383 room = kfifo_avail(&port->write_fifo);
384 spin_unlock_irqrestore(&port->lock, flags);
386 return room;
389 static int oti6858_chars_in_buffer(struct tty_struct *tty)
391 struct usb_serial_port *port = tty->driver_data;
392 int chars = 0;
393 unsigned long flags;
395 spin_lock_irqsave(&port->lock, flags);
396 chars = kfifo_len(&port->write_fifo);
397 spin_unlock_irqrestore(&port->lock, flags);
399 return chars;
402 static void oti6858_init_termios(struct tty_struct *tty)
404 *(tty->termios) = tty_std_termios;
405 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
406 tty->termios->c_ispeed = 38400;
407 tty->termios->c_ospeed = 38400;
410 static void oti6858_set_termios(struct tty_struct *tty,
411 struct usb_serial_port *port, struct ktermios *old_termios)
413 struct oti6858_private *priv = usb_get_serial_port_data(port);
414 unsigned long flags;
415 unsigned int cflag;
416 u8 frame_fmt, control;
417 __le16 divisor;
418 int br;
420 if (!tty) {
421 dbg("%s(): no tty structures", __func__);
422 return;
425 cflag = tty->termios->c_cflag;
427 spin_lock_irqsave(&priv->lock, flags);
428 divisor = priv->pending_setup.divisor;
429 frame_fmt = priv->pending_setup.frame_fmt;
430 control = priv->pending_setup.control;
431 spin_unlock_irqrestore(&priv->lock, flags);
433 frame_fmt &= ~FMT_DATA_BITS_MASK;
434 switch (cflag & CSIZE) {
435 case CS5:
436 frame_fmt |= FMT_DATA_BITS_5;
437 break;
438 case CS6:
439 frame_fmt |= FMT_DATA_BITS_6;
440 break;
441 case CS7:
442 frame_fmt |= FMT_DATA_BITS_7;
443 break;
444 default:
445 case CS8:
446 frame_fmt |= FMT_DATA_BITS_8;
447 break;
450 /* manufacturer claims that this device can work with baud rates
451 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
452 * guarantee that any other baud rate will work (especially
453 * the higher ones)
455 br = tty_get_baud_rate(tty);
456 if (br == 0) {
457 divisor = 0;
458 } else {
459 int real_br;
460 int new_divisor;
461 br = min(br, OTI6858_MAX_BAUD_RATE);
463 new_divisor = (96000000 + 8 * br) / (16 * br);
464 real_br = 96000000 / (16 * new_divisor);
465 divisor = cpu_to_le16(new_divisor);
466 tty_encode_baud_rate(tty, real_br, real_br);
469 frame_fmt &= ~FMT_STOP_BITS_MASK;
470 if ((cflag & CSTOPB) != 0)
471 frame_fmt |= FMT_STOP_BITS_2;
472 else
473 frame_fmt |= FMT_STOP_BITS_1;
475 frame_fmt &= ~FMT_PARITY_MASK;
476 if ((cflag & PARENB) != 0) {
477 if ((cflag & PARODD) != 0)
478 frame_fmt |= FMT_PARITY_ODD;
479 else
480 frame_fmt |= FMT_PARITY_EVEN;
481 } else {
482 frame_fmt |= FMT_PARITY_NONE;
485 control &= ~CONTROL_MASK;
486 if ((cflag & CRTSCTS) != 0)
487 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
489 /* change control lines if we are switching to or from B0 */
490 /* FIXME:
491 spin_lock_irqsave(&priv->lock, flags);
492 control = priv->line_control;
493 if ((cflag & CBAUD) == B0)
494 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
495 else
496 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
497 if (control != priv->line_control) {
498 control = priv->line_control;
499 spin_unlock_irqrestore(&priv->lock, flags);
500 set_control_lines(serial->dev, control);
501 } else {
502 spin_unlock_irqrestore(&priv->lock, flags);
506 spin_lock_irqsave(&priv->lock, flags);
507 if (divisor != priv->pending_setup.divisor
508 || control != priv->pending_setup.control
509 || frame_fmt != priv->pending_setup.frame_fmt) {
510 priv->pending_setup.divisor = divisor;
511 priv->pending_setup.control = control;
512 priv->pending_setup.frame_fmt = frame_fmt;
514 spin_unlock_irqrestore(&priv->lock, flags);
517 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
519 struct oti6858_private *priv = usb_get_serial_port_data(port);
520 struct ktermios tmp_termios;
521 struct usb_serial *serial = port->serial;
522 struct oti6858_control_pkt *buf;
523 unsigned long flags;
524 int result;
526 usb_clear_halt(serial->dev, port->write_urb->pipe);
527 usb_clear_halt(serial->dev, port->read_urb->pipe);
529 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
530 if (buf == NULL) {
531 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
532 return -ENOMEM;
535 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
536 OTI6858_REQ_T_GET_STATUS,
537 OTI6858_REQ_GET_STATUS,
538 0, 0,
539 buf, OTI6858_CTRL_PKT_SIZE,
540 100);
541 if (result != OTI6858_CTRL_PKT_SIZE) {
542 /* assume default (after power-on reset) values */
543 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
544 buf->frame_fmt = 0x03; /* 8N1 */
545 buf->something = 0x43;
546 buf->control = 0x4c; /* DTR, RTS */
547 buf->tx_status = 0x00;
548 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
549 buf->rx_bytes_avail = 0x00;
552 spin_lock_irqsave(&priv->lock, flags);
553 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
554 priv->pending_setup.divisor = buf->divisor;
555 priv->pending_setup.frame_fmt = buf->frame_fmt;
556 priv->pending_setup.control = buf->control;
557 spin_unlock_irqrestore(&priv->lock, flags);
558 kfree(buf);
560 dbg("%s(): submitting interrupt urb", __func__);
561 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
562 if (result != 0) {
563 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
564 " with error %d\n", __func__, result);
565 oti6858_close(port);
566 return result;
569 /* setup termios */
570 if (tty)
571 oti6858_set_termios(tty, port, &tmp_termios);
572 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
573 return 0;
576 static void oti6858_close(struct usb_serial_port *port)
578 struct oti6858_private *priv = usb_get_serial_port_data(port);
579 unsigned long flags;
581 spin_lock_irqsave(&port->lock, flags);
582 /* clear out any remaining data in the buffer */
583 kfifo_reset_out(&port->write_fifo);
584 spin_unlock_irqrestore(&port->lock, flags);
586 dbg("%s(): after buf_clear()", __func__);
588 /* cancel scheduled setup */
589 cancel_delayed_work_sync(&priv->delayed_setup_work);
590 cancel_delayed_work_sync(&priv->delayed_write_work);
592 /* shutdown our urbs */
593 dbg("%s(): shutting down urbs", __func__);
594 usb_kill_urb(port->write_urb);
595 usb_kill_urb(port->read_urb);
596 usb_kill_urb(port->interrupt_in_urb);
599 static int oti6858_tiocmset(struct tty_struct *tty,
600 unsigned int set, unsigned int clear)
602 struct usb_serial_port *port = tty->driver_data;
603 struct oti6858_private *priv = usb_get_serial_port_data(port);
604 unsigned long flags;
605 u8 control;
607 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
608 __func__, port->number, set, clear);
610 /* FIXME: check if this is correct (active high/low) */
611 spin_lock_irqsave(&priv->lock, flags);
612 control = priv->pending_setup.control;
613 if ((set & TIOCM_RTS) != 0)
614 control |= CONTROL_RTS_HIGH;
615 if ((set & TIOCM_DTR) != 0)
616 control |= CONTROL_DTR_HIGH;
617 if ((clear & TIOCM_RTS) != 0)
618 control &= ~CONTROL_RTS_HIGH;
619 if ((clear & TIOCM_DTR) != 0)
620 control &= ~CONTROL_DTR_HIGH;
622 if (control != priv->pending_setup.control)
623 priv->pending_setup.control = control;
625 spin_unlock_irqrestore(&priv->lock, flags);
626 return 0;
629 static int oti6858_tiocmget(struct tty_struct *tty)
631 struct usb_serial_port *port = tty->driver_data;
632 struct oti6858_private *priv = usb_get_serial_port_data(port);
633 unsigned long flags;
634 unsigned pin_state;
635 unsigned result = 0;
637 spin_lock_irqsave(&priv->lock, flags);
638 pin_state = priv->status.pin_state & PIN_MASK;
639 spin_unlock_irqrestore(&priv->lock, flags);
641 /* FIXME: check if this is correct (active high/low) */
642 if ((pin_state & PIN_RTS) != 0)
643 result |= TIOCM_RTS;
644 if ((pin_state & PIN_CTS) != 0)
645 result |= TIOCM_CTS;
646 if ((pin_state & PIN_DSR) != 0)
647 result |= TIOCM_DSR;
648 if ((pin_state & PIN_DTR) != 0)
649 result |= TIOCM_DTR;
650 if ((pin_state & PIN_RI) != 0)
651 result |= TIOCM_RI;
652 if ((pin_state & PIN_DCD) != 0)
653 result |= TIOCM_CD;
655 dbg("%s() = 0x%08x", __func__, result);
657 return result;
660 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
662 struct oti6858_private *priv = usb_get_serial_port_data(port);
663 unsigned long flags;
664 unsigned int prev, status;
665 unsigned int changed;
667 spin_lock_irqsave(&priv->lock, flags);
668 prev = priv->status.pin_state;
669 spin_unlock_irqrestore(&priv->lock, flags);
671 while (1) {
672 wait_event_interruptible(priv->intr_wait,
673 priv->status.pin_state != prev);
674 if (signal_pending(current))
675 return -ERESTARTSYS;
677 spin_lock_irqsave(&priv->lock, flags);
678 status = priv->status.pin_state & PIN_MASK;
679 spin_unlock_irqrestore(&priv->lock, flags);
681 changed = prev ^ status;
682 /* FIXME: check if this is correct (active high/low) */
683 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
684 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
685 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
686 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
687 return 0;
688 prev = status;
691 /* NOTREACHED */
692 return 0;
695 static int oti6858_ioctl(struct tty_struct *tty,
696 unsigned int cmd, unsigned long arg)
698 struct usb_serial_port *port = tty->driver_data;
700 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
701 __func__, port->number, cmd, arg);
703 switch (cmd) {
704 case TIOCMIWAIT:
705 dbg("%s(): TIOCMIWAIT", __func__);
706 return wait_modem_info(port, arg);
707 default:
708 dbg("%s(): 0x%04x not supported", __func__, cmd);
709 break;
711 return -ENOIOCTLCMD;
714 static void oti6858_read_int_callback(struct urb *urb)
716 struct usb_serial_port *port = urb->context;
717 struct oti6858_private *priv = usb_get_serial_port_data(port);
718 int transient = 0, can_recv = 0, resubmit = 1;
719 int status = urb->status;
721 switch (status) {
722 case 0:
723 /* success */
724 break;
725 case -ECONNRESET:
726 case -ENOENT:
727 case -ESHUTDOWN:
728 /* this urb is terminated, clean up */
729 dbg("%s(): urb shutting down with status: %d",
730 __func__, status);
731 return;
732 default:
733 dbg("%s(): nonzero urb status received: %d",
734 __func__, status);
735 break;
738 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
739 struct oti6858_control_pkt *xs = urb->transfer_buffer;
740 unsigned long flags;
742 spin_lock_irqsave(&priv->lock, flags);
744 if (!priv->transient) {
745 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
746 if (xs->rx_bytes_avail == 0) {
747 priv->transient = 4;
748 priv->setup_done = 0;
749 resubmit = 0;
750 dbg("%s(): scheduling setup_line()",
751 __func__);
752 schedule_delayed_work(&priv->delayed_setup_work, 0);
755 } else {
756 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
757 priv->transient = 0;
758 } else if (!priv->setup_done) {
759 resubmit = 0;
760 } else if (--priv->transient == 0) {
761 if (xs->rx_bytes_avail == 0) {
762 priv->transient = 4;
763 priv->setup_done = 0;
764 resubmit = 0;
765 dbg("%s(): scheduling setup_line()",
766 __func__);
767 schedule_delayed_work(&priv->delayed_setup_work, 0);
772 if (!priv->transient) {
773 if (xs->pin_state != priv->status.pin_state)
774 wake_up_interruptible(&priv->intr_wait);
775 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
778 if (!priv->transient && xs->rx_bytes_avail != 0) {
779 can_recv = xs->rx_bytes_avail;
780 priv->flags.read_urb_in_use = 1;
783 transient = priv->transient;
784 spin_unlock_irqrestore(&priv->lock, flags);
787 if (can_recv) {
788 int result;
790 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
791 if (result != 0) {
792 priv->flags.read_urb_in_use = 0;
793 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
794 " error %d\n", __func__, result);
795 } else {
796 resubmit = 0;
798 } else if (!transient) {
799 unsigned long flags;
800 int count;
802 spin_lock_irqsave(&port->lock, flags);
803 count = kfifo_len(&port->write_fifo);
804 spin_unlock_irqrestore(&port->lock, flags);
806 spin_lock_irqsave(&priv->lock, flags);
807 if (priv->flags.write_urb_in_use == 0 && count != 0) {
808 schedule_delayed_work(&priv->delayed_write_work, 0);
809 resubmit = 0;
811 spin_unlock_irqrestore(&priv->lock, flags);
814 if (resubmit) {
815 int result;
817 /* dbg("%s(): submitting interrupt urb", __func__); */
818 result = usb_submit_urb(urb, GFP_ATOMIC);
819 if (result != 0) {
820 dev_err(&urb->dev->dev,
821 "%s(): usb_submit_urb() failed with"
822 " error %d\n", __func__, result);
827 static void oti6858_read_bulk_callback(struct urb *urb)
829 struct usb_serial_port *port = urb->context;
830 struct oti6858_private *priv = usb_get_serial_port_data(port);
831 struct tty_struct *tty;
832 unsigned char *data = urb->transfer_buffer;
833 unsigned long flags;
834 int status = urb->status;
835 int result;
837 spin_lock_irqsave(&priv->lock, flags);
838 priv->flags.read_urb_in_use = 0;
839 spin_unlock_irqrestore(&priv->lock, flags);
841 if (status != 0) {
842 dbg("%s(): unable to handle the error, exiting", __func__);
843 return;
846 tty = tty_port_tty_get(&port->port);
847 if (tty != NULL && urb->actual_length > 0) {
848 tty_insert_flip_string(tty, data, urb->actual_length);
849 tty_flip_buffer_push(tty);
851 tty_kref_put(tty);
853 /* schedule the interrupt urb */
854 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
855 if (result != 0 && result != -EPERM) {
856 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
857 " error %d\n", __func__, result);
861 static void oti6858_write_bulk_callback(struct urb *urb)
863 struct usb_serial_port *port = urb->context;
864 struct oti6858_private *priv = usb_get_serial_port_data(port);
865 int status = urb->status;
866 int result;
868 switch (status) {
869 case 0:
870 /* success */
871 break;
872 case -ECONNRESET:
873 case -ENOENT:
874 case -ESHUTDOWN:
875 /* this urb is terminated, clean up */
876 dbg("%s(): urb shutting down with status: %d",
877 __func__, status);
878 priv->flags.write_urb_in_use = 0;
879 return;
880 default:
881 /* error in the urb, so we have to resubmit it */
882 dbg("%s(): nonzero write bulk status received: %d",
883 __func__, status);
884 dbg("%s(): overflow in write", __func__);
886 port->write_urb->transfer_buffer_length = 1;
887 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
888 if (result) {
889 dev_err_console(port, "%s(): usb_submit_urb() failed,"
890 " error %d\n", __func__, result);
891 } else {
892 return;
896 priv->flags.write_urb_in_use = 0;
898 /* schedule the interrupt urb if we are still open */
899 dbg("%s(): submitting interrupt urb", __func__);
900 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
901 if (result != 0) {
902 dev_err(&port->dev, "%s(): failed submitting int urb,"
903 " error %d\n", __func__, result);
907 module_usb_serial_driver(serial_drivers, id_table);
909 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
910 MODULE_AUTHOR(OTI6858_AUTHOR);
911 MODULE_VERSION(OTI6858_VERSION);
912 MODULE_LICENSE("GPL");
914 module_param(debug, bool, S_IRUGO | S_IWUSR);
915 MODULE_PARM_DESC(debug, "enable debug output");