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
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>
56 #define OTI6858_DESCRIPTION \
57 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
58 #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
59 #define OTI6858_VERSION "0.1"
61 static struct usb_device_id id_table
[] = {
62 { USB_DEVICE(OTI6858_VENDOR_ID
, OTI6858_PRODUCT_ID
) },
66 MODULE_DEVICE_TABLE(usb
, id_table
);
68 static struct usb_driver oti6858_driver
= {
70 .probe
= usb_serial_probe
,
71 .disconnect
= usb_serial_disconnect
,
79 /* buffering code, copied from pl2303 driver */
80 #define PL2303_BUF_SIZE 1024
81 #define PL2303_TMP_BUF_SIZE 1024
84 unsigned int buf_size
;
91 #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
92 #define OTI6858_REQ_T_GET_STATUS 0x01
94 #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
95 #define OTI6858_REQ_T_SET_LINE 0x00
97 #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
98 #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
100 /* format of the control packet */
101 struct oti6858_control_pkt
{
102 __le16 divisor
; /* baud rate = 96000000 / (16 * divisor), LE */
103 #define OTI6858_MAX_BAUD_RATE 3000000
105 #define FMT_STOP_BITS_MASK 0xc0
106 #define FMT_STOP_BITS_1 0x00
107 #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
108 #define FMT_PARITY_MASK 0x38
109 #define FMT_PARITY_NONE 0x00
110 #define FMT_PARITY_ODD 0x08
111 #define FMT_PARITY_EVEN 0x18
112 #define FMT_PARITY_MARK 0x28
113 #define FMT_PARITY_SPACE 0x38
114 #define FMT_DATA_BITS_MASK 0x03
115 #define FMT_DATA_BITS_5 0x00
116 #define FMT_DATA_BITS_6 0x01
117 #define FMT_DATA_BITS_7 0x02
118 #define FMT_DATA_BITS_8 0x03
119 u8 something
; /* always equals 0x43 */
120 u8 control
; /* settings of flow control lines */
121 #define CONTROL_MASK 0x0c
122 #define CONTROL_DTR_HIGH 0x08
123 #define CONTROL_RTS_HIGH 0x04
125 #define TX_BUFFER_EMPTIED 0x09
127 #define PIN_MASK 0x3f
128 #define PIN_RTS 0x20 /* output pin */
129 #define PIN_CTS 0x10 /* input pin, active low */
130 #define PIN_DSR 0x08 /* input pin, active low */
131 #define PIN_DTR 0x04 /* output pin */
132 #define PIN_RI 0x02 /* input pin, active low */
133 #define PIN_DCD 0x01 /* input pin, active low */
134 u8 rx_bytes_avail
; /* number of bytes in rx buffer */;
137 #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
138 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
139 (((a)->divisor == (priv)->pending_setup.divisor) \
140 && ((a)->control == (priv)->pending_setup.control) \
141 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
143 /* function prototypes */
144 static int oti6858_open(struct tty_struct
*tty
,
145 struct usb_serial_port
*port
, struct file
*filp
);
146 static void oti6858_close(struct tty_struct
*tty
,
147 struct usb_serial_port
*port
, struct file
*filp
);
148 static void oti6858_set_termios(struct tty_struct
*tty
,
149 struct usb_serial_port
*port
, struct ktermios
*old
);
150 static int oti6858_ioctl(struct tty_struct
*tty
, struct file
*file
,
151 unsigned int cmd
, unsigned long arg
);
152 static void oti6858_read_int_callback(struct urb
*urb
);
153 static void oti6858_read_bulk_callback(struct urb
*urb
);
154 static void oti6858_write_bulk_callback(struct urb
*urb
);
155 static int oti6858_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
156 const unsigned char *buf
, int count
);
157 static int oti6858_write_room(struct tty_struct
*tty
);
158 static int oti6858_chars_in_buffer(struct tty_struct
*tty
);
159 static int oti6858_tiocmget(struct tty_struct
*tty
, struct file
*file
);
160 static int oti6858_tiocmset(struct tty_struct
*tty
, struct file
*file
,
161 unsigned int set
, unsigned int clear
);
162 static int oti6858_startup(struct usb_serial
*serial
);
163 static void oti6858_shutdown(struct usb_serial
*serial
);
165 /* functions operating on buffers */
166 static struct oti6858_buf
*oti6858_buf_alloc(unsigned int size
);
167 static void oti6858_buf_free(struct oti6858_buf
*pb
);
168 static void oti6858_buf_clear(struct oti6858_buf
*pb
);
169 static unsigned int oti6858_buf_data_avail(struct oti6858_buf
*pb
);
170 static unsigned int oti6858_buf_space_avail(struct oti6858_buf
*pb
);
171 static unsigned int oti6858_buf_put(struct oti6858_buf
*pb
, const char *buf
,
173 static unsigned int oti6858_buf_get(struct oti6858_buf
*pb
, char *buf
,
178 static struct usb_serial_driver oti6858_device
= {
180 .owner
= THIS_MODULE
,
183 .id_table
= id_table
,
185 .open
= oti6858_open
,
186 .close
= oti6858_close
,
187 .write
= oti6858_write
,
188 .ioctl
= oti6858_ioctl
,
189 .set_termios
= oti6858_set_termios
,
190 .tiocmget
= oti6858_tiocmget
,
191 .tiocmset
= oti6858_tiocmset
,
192 .read_bulk_callback
= oti6858_read_bulk_callback
,
193 .read_int_callback
= oti6858_read_int_callback
,
194 .write_bulk_callback
= oti6858_write_bulk_callback
,
195 .write_room
= oti6858_write_room
,
196 .chars_in_buffer
= oti6858_chars_in_buffer
,
197 .attach
= oti6858_startup
,
198 .shutdown
= oti6858_shutdown
,
201 struct oti6858_private
{
204 struct oti6858_buf
*buf
;
205 struct oti6858_control_pkt status
;
210 u8 termios_initialized
;
212 struct delayed_work delayed_write_work
;
221 struct delayed_work delayed_setup_work
;
223 wait_queue_head_t intr_wait
;
224 struct usb_serial_port
*port
; /* USB port with which associated */
228 /* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
229 #define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
231 static void setup_line(struct work_struct
*work
)
233 struct oti6858_private
*priv
= container_of(work
,
234 struct oti6858_private
, delayed_setup_work
.work
);
235 struct usb_serial_port
*port
= priv
->port
;
236 struct oti6858_control_pkt
*new_setup
;
240 dbg("%s(port = %d)", __func__
, port
->number
);
242 new_setup
= kmalloc(OTI6858_CTRL_PKT_SIZE
, GFP_KERNEL
);
243 if (new_setup
== NULL
) {
244 dev_err(&port
->dev
, "%s(): out of memory!\n", __func__
);
245 /* we will try again */
246 schedule_delayed_work(&priv
->delayed_setup_work
,
247 msecs_to_jiffies(2));
251 result
= usb_control_msg(port
->serial
->dev
,
252 usb_rcvctrlpipe(port
->serial
->dev
, 0),
253 OTI6858_REQ_T_GET_STATUS
,
254 OTI6858_REQ_GET_STATUS
,
256 new_setup
, OTI6858_CTRL_PKT_SIZE
,
259 if (result
!= OTI6858_CTRL_PKT_SIZE
) {
260 dev_err(&port
->dev
, "%s(): error reading status\n", __func__
);
262 /* we will try again */
263 schedule_delayed_work(&priv
->delayed_setup_work
,
264 msecs_to_jiffies(2));
268 spin_lock_irqsave(&priv
->lock
, flags
);
269 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup
, priv
)) {
270 new_setup
->divisor
= priv
->pending_setup
.divisor
;
271 new_setup
->control
= priv
->pending_setup
.control
;
272 new_setup
->frame_fmt
= priv
->pending_setup
.frame_fmt
;
274 spin_unlock_irqrestore(&priv
->lock
, flags
);
275 result
= usb_control_msg(port
->serial
->dev
,
276 usb_sndctrlpipe(port
->serial
->dev
, 0),
277 OTI6858_REQ_T_SET_LINE
,
278 OTI6858_REQ_SET_LINE
,
280 new_setup
, OTI6858_CTRL_PKT_SIZE
,
283 spin_unlock_irqrestore(&priv
->lock
, flags
);
288 spin_lock_irqsave(&priv
->lock
, flags
);
289 if (result
!= OTI6858_CTRL_PKT_SIZE
)
291 priv
->setup_done
= 1;
292 spin_unlock_irqrestore(&priv
->lock
, flags
);
294 dbg("%s(): submitting interrupt urb", __func__
);
295 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
296 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
298 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
299 " with error %d\n", __func__
, result
);
303 void send_data(struct work_struct
*work
)
305 struct oti6858_private
*priv
= container_of(work
,
306 struct oti6858_private
, delayed_write_work
.work
);
307 struct usb_serial_port
*port
= priv
->port
;
308 int count
= 0, result
;
312 dbg("%s(port = %d)", __func__
, port
->number
);
314 spin_lock_irqsave(&priv
->lock
, flags
);
315 if (priv
->flags
.write_urb_in_use
) {
316 spin_unlock_irqrestore(&priv
->lock
, flags
);
317 schedule_delayed_work(&priv
->delayed_write_work
,
318 msecs_to_jiffies(2));
321 priv
->flags
.write_urb_in_use
= 1;
323 count
= oti6858_buf_data_avail(priv
->buf
);
324 spin_unlock_irqrestore(&priv
->lock
, flags
);
325 if (count
> port
->bulk_out_size
)
326 count
= port
->bulk_out_size
;
329 result
= usb_control_msg(port
->serial
->dev
,
330 usb_rcvctrlpipe(port
->serial
->dev
, 0),
331 OTI6858_REQ_T_CHECK_TXBUFF
,
332 OTI6858_REQ_CHECK_TXBUFF
,
333 count
, 0, &allow
, 1, 100);
334 if (result
!= 1 || allow
!= 0)
339 priv
->flags
.write_urb_in_use
= 0;
341 dbg("%s(): submitting interrupt urb", __func__
);
342 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
343 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
345 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
346 " with error %d\n", __func__
, result
);
351 spin_lock_irqsave(&priv
->lock
, flags
);
352 oti6858_buf_get(priv
->buf
, port
->write_urb
->transfer_buffer
, count
);
353 spin_unlock_irqrestore(&priv
->lock
, flags
);
355 port
->write_urb
->transfer_buffer_length
= count
;
356 port
->write_urb
->dev
= port
->serial
->dev
;
357 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
359 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
360 " with error %d\n", __func__
, result
);
361 priv
->flags
.write_urb_in_use
= 0;
364 usb_serial_port_softint(port
);
367 static int oti6858_startup(struct usb_serial
*serial
)
369 struct usb_serial_port
*port
= serial
->port
[0];
370 struct oti6858_private
*priv
;
373 for (i
= 0; i
< serial
->num_ports
; ++i
) {
374 priv
= kzalloc(sizeof(struct oti6858_private
), GFP_KERNEL
);
377 priv
->buf
= oti6858_buf_alloc(PL2303_BUF_SIZE
);
378 if (priv
->buf
== NULL
) {
383 spin_lock_init(&priv
->lock
);
384 init_waitqueue_head(&priv
->intr_wait
);
385 /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
386 /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
388 INIT_DELAYED_WORK(&priv
->delayed_setup_work
, setup_line
);
389 INIT_DELAYED_WORK(&priv
->delayed_write_work
, send_data
);
391 usb_set_serial_port_data(serial
->port
[i
], priv
);
393 if (i
== serial
->num_ports
)
396 for (--i
; i
>= 0; --i
) {
397 priv
= usb_get_serial_port_data(serial
->port
[i
]);
398 oti6858_buf_free(priv
->buf
);
400 usb_set_serial_port_data(serial
->port
[i
], NULL
);
405 static int oti6858_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
406 const unsigned char *buf
, int count
)
408 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
411 dbg("%s(port = %d, count = %d)", __func__
, port
->number
, count
);
416 spin_lock_irqsave(&priv
->lock
, flags
);
417 count
= oti6858_buf_put(priv
->buf
, buf
, count
);
418 spin_unlock_irqrestore(&priv
->lock
, flags
);
423 static int oti6858_write_room(struct tty_struct
*tty
)
425 struct usb_serial_port
*port
= tty
->driver_data
;
426 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
430 dbg("%s(port = %d)", __func__
, port
->number
);
432 spin_lock_irqsave(&priv
->lock
, flags
);
433 room
= oti6858_buf_space_avail(priv
->buf
);
434 spin_unlock_irqrestore(&priv
->lock
, flags
);
439 static int oti6858_chars_in_buffer(struct tty_struct
*tty
)
441 struct usb_serial_port
*port
= tty
->driver_data
;
442 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
446 dbg("%s(port = %d)", __func__
, port
->number
);
448 spin_lock_irqsave(&priv
->lock
, flags
);
449 chars
= oti6858_buf_data_avail(priv
->buf
);
450 spin_unlock_irqrestore(&priv
->lock
, flags
);
455 static void oti6858_set_termios(struct tty_struct
*tty
,
456 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
458 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
461 u8 frame_fmt
, control
;
465 dbg("%s(port = %d)", __func__
, port
->number
);
468 dbg("%s(): no tty structures", __func__
);
472 spin_lock_irqsave(&priv
->lock
, flags
);
473 if (!priv
->flags
.termios_initialized
) {
474 *(tty
->termios
) = tty_std_termios
;
475 tty
->termios
->c_cflag
= B38400
| CS8
| CREAD
| HUPCL
| CLOCAL
;
476 tty
->termios
->c_ispeed
= 38400;
477 tty
->termios
->c_ospeed
= 38400;
478 priv
->flags
.termios_initialized
= 1;
480 spin_unlock_irqrestore(&priv
->lock
, flags
);
482 cflag
= tty
->termios
->c_cflag
;
484 spin_lock_irqsave(&priv
->lock
, flags
);
485 divisor
= priv
->pending_setup
.divisor
;
486 frame_fmt
= priv
->pending_setup
.frame_fmt
;
487 control
= priv
->pending_setup
.control
;
488 spin_unlock_irqrestore(&priv
->lock
, flags
);
490 frame_fmt
&= ~FMT_DATA_BITS_MASK
;
491 switch (cflag
& CSIZE
) {
493 frame_fmt
|= FMT_DATA_BITS_5
;
496 frame_fmt
|= FMT_DATA_BITS_6
;
499 frame_fmt
|= FMT_DATA_BITS_7
;
503 frame_fmt
|= FMT_DATA_BITS_8
;
507 /* manufacturer claims that this device can work with baud rates
508 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
509 * guarantee that any other baud rate will work (especially
512 br
= tty_get_baud_rate(tty
);
518 br
= min(br
, OTI6858_MAX_BAUD_RATE
);
520 new_divisor
= (96000000 + 8 * br
) / (16 * br
);
521 real_br
= 96000000 / (16 * new_divisor
);
522 divisor
= cpu_to_le16(new_divisor
);
523 tty_encode_baud_rate(tty
, real_br
, real_br
);
526 frame_fmt
&= ~FMT_STOP_BITS_MASK
;
527 if ((cflag
& CSTOPB
) != 0)
528 frame_fmt
|= FMT_STOP_BITS_2
;
530 frame_fmt
|= FMT_STOP_BITS_1
;
532 frame_fmt
&= ~FMT_PARITY_MASK
;
533 if ((cflag
& PARENB
) != 0) {
534 if ((cflag
& PARODD
) != 0)
535 frame_fmt
|= FMT_PARITY_ODD
;
537 frame_fmt
|= FMT_PARITY_EVEN
;
539 frame_fmt
|= FMT_PARITY_NONE
;
542 control
&= ~CONTROL_MASK
;
543 if ((cflag
& CRTSCTS
) != 0)
544 control
|= (CONTROL_DTR_HIGH
| CONTROL_RTS_HIGH
);
546 /* change control lines if we are switching to or from B0 */
548 spin_lock_irqsave(&priv->lock, flags);
549 control = priv->line_control;
550 if ((cflag & CBAUD) == B0)
551 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
553 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
554 if (control != priv->line_control) {
555 control = priv->line_control;
556 spin_unlock_irqrestore(&priv->lock, flags);
557 set_control_lines(serial->dev, control);
559 spin_unlock_irqrestore(&priv->lock, flags);
563 spin_lock_irqsave(&priv
->lock
, flags
);
564 if (divisor
!= priv
->pending_setup
.divisor
565 || control
!= priv
->pending_setup
.control
566 || frame_fmt
!= priv
->pending_setup
.frame_fmt
) {
567 priv
->pending_setup
.divisor
= divisor
;
568 priv
->pending_setup
.control
= control
;
569 priv
->pending_setup
.frame_fmt
= frame_fmt
;
571 spin_unlock_irqrestore(&priv
->lock
, flags
);
574 static int oti6858_open(struct tty_struct
*tty
,
575 struct usb_serial_port
*port
, struct file
*filp
)
577 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
578 struct ktermios tmp_termios
;
579 struct usb_serial
*serial
= port
->serial
;
580 struct oti6858_control_pkt
*buf
;
584 dbg("%s(port = %d)", __func__
, port
->number
);
586 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
587 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
589 if (port
->port
.count
!= 1)
592 buf
= kmalloc(OTI6858_CTRL_PKT_SIZE
, GFP_KERNEL
);
594 dev_err(&port
->dev
, "%s(): out of memory!\n", __func__
);
598 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
599 OTI6858_REQ_T_GET_STATUS
,
600 OTI6858_REQ_GET_STATUS
,
602 buf
, OTI6858_CTRL_PKT_SIZE
,
604 if (result
!= OTI6858_CTRL_PKT_SIZE
) {
605 /* assume default (after power-on reset) values */
606 buf
->divisor
= cpu_to_le16(0x009c); /* 38400 bps */
607 buf
->frame_fmt
= 0x03; /* 8N1 */
608 buf
->something
= 0x43;
609 buf
->control
= 0x4c; /* DTR, RTS */
610 buf
->tx_status
= 0x00;
611 buf
->pin_state
= 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
612 buf
->rx_bytes_avail
= 0x00;
615 spin_lock_irqsave(&priv
->lock
, flags
);
616 memcpy(&priv
->status
, buf
, OTI6858_CTRL_PKT_SIZE
);
617 priv
->pending_setup
.divisor
= buf
->divisor
;
618 priv
->pending_setup
.frame_fmt
= buf
->frame_fmt
;
619 priv
->pending_setup
.control
= buf
->control
;
620 spin_unlock_irqrestore(&priv
->lock
, flags
);
623 dbg("%s(): submitting interrupt urb", __func__
);
624 port
->interrupt_in_urb
->dev
= serial
->dev
;
625 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
627 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
628 " with error %d\n", __func__
, result
);
629 oti6858_close(tty
, port
, NULL
);
635 oti6858_set_termios(tty
, port
, &tmp_termios
);
640 static void oti6858_close(struct tty_struct
*tty
,
641 struct usb_serial_port
*port
, struct file
*filp
)
643 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
648 dbg("%s(port = %d)", __func__
, port
->number
);
650 /* wait for data to drain from the buffer */
651 spin_lock_irqsave(&priv
->lock
, flags
);
652 timeout
= 30 * HZ
; /* PL2303_CLOSING_WAIT */
653 init_waitqueue_entry(&wait
, current
);
654 add_wait_queue(&tty
->write_wait
, &wait
);
655 dbg("%s(): entering wait loop", __func__
);
657 set_current_state(TASK_INTERRUPTIBLE
);
658 if (oti6858_buf_data_avail(priv
->buf
) == 0
659 || timeout
== 0 || signal_pending(current
)
660 || port
->serial
->disconnected
)
662 spin_unlock_irqrestore(&priv
->lock
, flags
);
663 timeout
= schedule_timeout(timeout
);
664 spin_lock_irqsave(&priv
->lock
, flags
);
666 set_current_state(TASK_RUNNING
);
667 remove_wait_queue(&tty
->write_wait
, &wait
);
668 dbg("%s(): after wait loop", __func__
);
670 /* clear out any remaining data in the buffer */
671 oti6858_buf_clear(priv
->buf
);
672 spin_unlock_irqrestore(&priv
->lock
, flags
);
674 /* wait for characters to drain from the device */
675 /* (this is long enough for the entire 256 byte */
676 /* pl2303 hardware buffer to drain with no flow */
677 /* control for data rates of 1200 bps or more, */
678 /* for lower rates we should really know how much */
679 /* data is in the buffer to compute a delay */
680 /* that is not unnecessarily long) */
682 bps = tty_get_baud_rate(tty);
684 timeout = max((HZ*2560)/bps,HZ/10);
688 schedule_timeout_interruptible(timeout
);
689 dbg("%s(): after schedule_timeout_interruptible()", __func__
);
691 /* cancel scheduled setup */
692 cancel_delayed_work(&priv
->delayed_setup_work
);
693 cancel_delayed_work(&priv
->delayed_write_work
);
694 flush_scheduled_work();
696 /* shutdown our urbs */
697 dbg("%s(): shutting down urbs", __func__
);
698 usb_kill_urb(port
->write_urb
);
699 usb_kill_urb(port
->read_urb
);
700 usb_kill_urb(port
->interrupt_in_urb
);
703 if (tty && (tty->termios->c_cflag) & HUPCL) {
705 spin_lock_irqsave(&priv->lock, flags);
706 priv->pending_setup.control &= ~CONTROL_MASK;
707 spin_unlock_irqrestore(&priv->lock, flags);
712 static int oti6858_tiocmset(struct tty_struct
*tty
, struct file
*file
,
713 unsigned int set
, unsigned int clear
)
715 struct usb_serial_port
*port
= tty
->driver_data
;
716 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
720 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
721 __func__
, port
->number
, set
, clear
);
723 if (!usb_get_intfdata(port
->serial
->interface
))
726 /* FIXME: check if this is correct (active high/low) */
727 spin_lock_irqsave(&priv
->lock
, flags
);
728 control
= priv
->pending_setup
.control
;
729 if ((set
& TIOCM_RTS
) != 0)
730 control
|= CONTROL_RTS_HIGH
;
731 if ((set
& TIOCM_DTR
) != 0)
732 control
|= CONTROL_DTR_HIGH
;
733 if ((clear
& TIOCM_RTS
) != 0)
734 control
&= ~CONTROL_RTS_HIGH
;
735 if ((clear
& TIOCM_DTR
) != 0)
736 control
&= ~CONTROL_DTR_HIGH
;
738 if (control
!= priv
->pending_setup
.control
)
739 priv
->pending_setup
.control
= control
;
741 spin_unlock_irqrestore(&priv
->lock
, flags
);
745 static int oti6858_tiocmget(struct tty_struct
*tty
, struct file
*file
)
747 struct usb_serial_port
*port
= tty
->driver_data
;
748 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
753 dbg("%s(port = %d)", __func__
, port
->number
);
755 if (!usb_get_intfdata(port
->serial
->interface
))
758 spin_lock_irqsave(&priv
->lock
, flags
);
759 pin_state
= priv
->status
.pin_state
& PIN_MASK
;
760 spin_unlock_irqrestore(&priv
->lock
, flags
);
762 /* FIXME: check if this is correct (active high/low) */
763 if ((pin_state
& PIN_RTS
) != 0)
765 if ((pin_state
& PIN_CTS
) != 0)
767 if ((pin_state
& PIN_DSR
) != 0)
769 if ((pin_state
& PIN_DTR
) != 0)
771 if ((pin_state
& PIN_RI
) != 0)
773 if ((pin_state
& PIN_DCD
) != 0)
776 dbg("%s() = 0x%08x", __func__
, result
);
781 static int wait_modem_info(struct usb_serial_port
*port
, unsigned int arg
)
783 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
785 unsigned int prev
, status
;
786 unsigned int changed
;
788 spin_lock_irqsave(&priv
->lock
, flags
);
789 prev
= priv
->status
.pin_state
;
790 spin_unlock_irqrestore(&priv
->lock
, flags
);
793 wait_event_interruptible(priv
->intr_wait
,
794 priv
->status
.pin_state
!= prev
);
795 if (signal_pending(current
))
798 spin_lock_irqsave(&priv
->lock
, flags
);
799 status
= priv
->status
.pin_state
& PIN_MASK
;
800 spin_unlock_irqrestore(&priv
->lock
, flags
);
802 changed
= prev
^ status
;
803 /* FIXME: check if this is correct (active high/low) */
804 if (((arg
& TIOCM_RNG
) && (changed
& PIN_RI
)) ||
805 ((arg
& TIOCM_DSR
) && (changed
& PIN_DSR
)) ||
806 ((arg
& TIOCM_CD
) && (changed
& PIN_DCD
)) ||
807 ((arg
& TIOCM_CTS
) && (changed
& PIN_CTS
)))
816 static int oti6858_ioctl(struct tty_struct
*tty
, struct file
*file
,
817 unsigned int cmd
, unsigned long arg
)
819 struct usb_serial_port
*port
= tty
->driver_data
;
821 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
822 __func__
, port
->number
, cmd
, arg
);
826 dbg("%s(): TIOCMIWAIT", __func__
);
827 return wait_modem_info(port
, arg
);
829 dbg("%s(): 0x%04x not supported", __func__
, cmd
);
836 static void oti6858_shutdown(struct usb_serial
*serial
)
838 struct oti6858_private
*priv
;
841 dbg("%s()", __func__
);
843 for (i
= 0; i
< serial
->num_ports
; ++i
) {
844 priv
= usb_get_serial_port_data(serial
->port
[i
]);
846 oti6858_buf_free(priv
->buf
);
848 usb_set_serial_port_data(serial
->port
[i
], NULL
);
853 static void oti6858_read_int_callback(struct urb
*urb
)
855 struct usb_serial_port
*port
= urb
->context
;
856 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
857 int transient
= 0, can_recv
= 0, resubmit
= 1;
858 int status
= urb
->status
;
860 dbg("%s(port = %d, status = %d)",
861 __func__
, port
->number
, status
);
870 /* this urb is terminated, clean up */
871 dbg("%s(): urb shutting down with status: %d",
875 dbg("%s(): nonzero urb status received: %d",
880 if (status
== 0 && urb
->actual_length
== OTI6858_CTRL_PKT_SIZE
) {
881 struct oti6858_control_pkt
*xs
= urb
->transfer_buffer
;
884 spin_lock_irqsave(&priv
->lock
, flags
);
886 if (!priv
->transient
) {
887 if (!OTI6858_CTRL_EQUALS_PENDING(xs
, priv
)) {
888 if (xs
->rx_bytes_avail
== 0) {
890 priv
->setup_done
= 0;
892 dbg("%s(): scheduling setup_line()",
894 schedule_delayed_work(&priv
->delayed_setup_work
, 0);
898 if (OTI6858_CTRL_EQUALS_PENDING(xs
, priv
)) {
900 } else if (!priv
->setup_done
) {
902 } else if (--priv
->transient
== 0) {
903 if (xs
->rx_bytes_avail
== 0) {
905 priv
->setup_done
= 0;
907 dbg("%s(): scheduling setup_line()",
909 schedule_delayed_work(&priv
->delayed_setup_work
, 0);
914 if (!priv
->transient
) {
915 if (xs
->pin_state
!= priv
->status
.pin_state
)
916 wake_up_interruptible(&priv
->intr_wait
);
917 memcpy(&priv
->status
, xs
, OTI6858_CTRL_PKT_SIZE
);
920 if (!priv
->transient
&& xs
->rx_bytes_avail
!= 0) {
921 can_recv
= xs
->rx_bytes_avail
;
922 priv
->flags
.read_urb_in_use
= 1;
925 transient
= priv
->transient
;
926 spin_unlock_irqrestore(&priv
->lock
, flags
);
932 port
->read_urb
->dev
= port
->serial
->dev
;
933 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
935 priv
->flags
.read_urb_in_use
= 0;
936 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
937 " error %d\n", __func__
, result
);
941 } else if (!transient
) {
944 spin_lock_irqsave(&priv
->lock
, flags
);
945 if (priv
->flags
.write_urb_in_use
== 0
946 && oti6858_buf_data_avail(priv
->buf
) != 0) {
947 schedule_delayed_work(&priv
->delayed_write_work
, 0);
950 spin_unlock_irqrestore(&priv
->lock
, flags
);
956 /* dbg("%s(): submitting interrupt urb", __func__); */
957 urb
->dev
= port
->serial
->dev
;
958 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
960 dev_err(&urb
->dev
->dev
,
961 "%s(): usb_submit_urb() failed with"
962 " error %d\n", __func__
, result
);
967 static void oti6858_read_bulk_callback(struct urb
*urb
)
969 struct usb_serial_port
*port
= urb
->context
;
970 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
971 struct tty_struct
*tty
;
972 unsigned char *data
= urb
->transfer_buffer
;
974 int status
= urb
->status
;
977 dbg("%s(port = %d, status = %d)",
978 __func__
, port
->number
, status
);
980 spin_lock_irqsave(&priv
->lock
, flags
);
981 priv
->flags
.read_urb_in_use
= 0;
982 spin_unlock_irqrestore(&priv
->lock
, flags
);
985 if (!port
->port
.count
) {
986 dbg("%s(): port is closed, exiting", __func__
);
990 if (status == -EPROTO) {
991 * PL2303 mysteriously fails with -EPROTO reschedule
993 dbg("%s - caught -EPROTO, resubmitting the urb",
995 result = usb_submit_urb(urb, GFP_ATOMIC);
997 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
1001 dbg("%s(): unable to handle the error, exiting", __func__
);
1005 tty
= port
->port
.tty
;
1006 if (tty
!= NULL
&& urb
->actual_length
> 0) {
1007 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
1008 tty_flip_buffer_push(tty
);
1011 /* schedule the interrupt urb if we are still open */
1012 if (port
->port
.count
!= 0) {
1013 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
1014 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
1016 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
1017 " error %d\n", __func__
, result
);
1022 static void oti6858_write_bulk_callback(struct urb
*urb
)
1024 struct usb_serial_port
*port
= urb
->context
;
1025 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
1026 int status
= urb
->status
;
1029 dbg("%s(port = %d, status = %d)",
1030 __func__
, port
->number
, status
);
1039 /* this urb is terminated, clean up */
1040 dbg("%s(): urb shutting down with status: %d",
1042 priv
->flags
.write_urb_in_use
= 0;
1045 /* error in the urb, so we have to resubmit it */
1046 dbg("%s(): nonzero write bulk status received: %d",
1048 dbg("%s(): overflow in write", __func__
);
1050 port
->write_urb
->transfer_buffer_length
= 1;
1051 port
->write_urb
->dev
= port
->serial
->dev
;
1052 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
1054 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
1055 " error %d\n", __func__
, result
);
1061 priv
->flags
.write_urb_in_use
= 0;
1063 /* schedule the interrupt urb if we are still open */
1064 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
1065 dbg("%s(): submitting interrupt urb", __func__
);
1066 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
1068 dev_err(&port
->dev
, "%s(): failed submitting int urb,"
1069 " error %d\n", __func__
, result
);
1077 * Allocate a circular buffer and all associated memory.
1079 static struct oti6858_buf
*oti6858_buf_alloc(unsigned int size
)
1081 struct oti6858_buf
*pb
;
1086 pb
= kmalloc(sizeof(struct oti6858_buf
), GFP_KERNEL
);
1090 pb
->buf_buf
= kmalloc(size
, GFP_KERNEL
);
1091 if (pb
->buf_buf
== NULL
) {
1096 pb
->buf_size
= size
;
1097 pb
->buf_get
= pb
->buf_put
= pb
->buf_buf
;
1105 * Free the buffer and all associated memory.
1107 static void oti6858_buf_free(struct oti6858_buf
*pb
)
1118 * Clear out all data in the circular buffer.
1120 static void oti6858_buf_clear(struct oti6858_buf
*pb
)
1123 /* equivalent to a get of all data available */
1124 pb
->buf_get
= pb
->buf_put
;
1129 * oti6858_buf_data_avail
1131 * Return the number of bytes of data available in the circular
1134 static unsigned int oti6858_buf_data_avail(struct oti6858_buf
*pb
)
1138 return (pb
->buf_size
+ pb
->buf_put
- pb
->buf_get
) % pb
->buf_size
;
1142 * oti6858_buf_space_avail
1144 * Return the number of bytes of space available in the circular
1147 static unsigned int oti6858_buf_space_avail(struct oti6858_buf
*pb
)
1151 return (pb
->buf_size
+ pb
->buf_get
- pb
->buf_put
- 1) % pb
->buf_size
;
1157 * Copy data data from a user buffer and put it into the circular buffer.
1158 * Restrict to the amount of space available.
1160 * Return the number of bytes copied.
1162 static unsigned int oti6858_buf_put(struct oti6858_buf
*pb
, const char *buf
,
1170 len
= oti6858_buf_space_avail(pb
);
1177 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_put
;
1179 memcpy(pb
->buf_put
, buf
, len
);
1180 memcpy(pb
->buf_buf
, buf
+len
, count
- len
);
1181 pb
->buf_put
= pb
->buf_buf
+ count
- len
;
1183 memcpy(pb
->buf_put
, buf
, count
);
1185 pb
->buf_put
+= count
;
1186 else /* count == len */
1187 pb
->buf_put
= pb
->buf_buf
;
1196 * Get data from the circular buffer and copy to the given buffer.
1197 * Restrict to the amount of data available.
1199 * Return the number of bytes copied.
1201 static unsigned int oti6858_buf_get(struct oti6858_buf
*pb
, char *buf
,
1209 len
= oti6858_buf_data_avail(pb
);
1216 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_get
;
1218 memcpy(buf
, pb
->buf_get
, len
);
1219 memcpy(buf
+len
, pb
->buf_buf
, count
- len
);
1220 pb
->buf_get
= pb
->buf_buf
+ count
- len
;
1222 memcpy(buf
, pb
->buf_get
, count
);
1224 pb
->buf_get
+= count
;
1225 else /* count == len */
1226 pb
->buf_get
= pb
->buf_buf
;
1232 /* module description and (de)initialization */
1234 static int __init
oti6858_init(void)
1238 retval
= usb_serial_register(&oti6858_device
);
1240 retval
= usb_register(&oti6858_driver
);
1242 usb_serial_deregister(&oti6858_device
);
1247 static void __exit
oti6858_exit(void)
1249 usb_deregister(&oti6858_driver
);
1250 usb_serial_deregister(&oti6858_device
);
1253 module_init(oti6858_init
);
1254 module_exit(oti6858_exit
);
1256 MODULE_DESCRIPTION(OTI6858_DESCRIPTION
);
1257 MODULE_AUTHOR(OTI6858_AUTHOR
);
1258 MODULE_VERSION(OTI6858_VERSION
);
1259 MODULE_LICENSE("GPL");
1261 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1262 MODULE_PARM_DESC(debug
, "enable debug output");