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 const 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
, struct usb_serial_port
*port
);
145 static void oti6858_close(struct usb_serial_port
*port
);
146 static void oti6858_set_termios(struct tty_struct
*tty
,
147 struct usb_serial_port
*port
, struct ktermios
*old
);
148 static void oti6858_init_termios(struct tty_struct
*tty
);
149 static int oti6858_ioctl(struct tty_struct
*tty
, struct file
*file
,
150 unsigned int cmd
, unsigned long arg
);
151 static void oti6858_read_int_callback(struct urb
*urb
);
152 static void oti6858_read_bulk_callback(struct urb
*urb
);
153 static void oti6858_write_bulk_callback(struct urb
*urb
);
154 static int oti6858_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
155 const unsigned char *buf
, int count
);
156 static int oti6858_write_room(struct tty_struct
*tty
);
157 static int oti6858_chars_in_buffer(struct tty_struct
*tty
);
158 static int oti6858_tiocmget(struct tty_struct
*tty
, struct file
*file
);
159 static int oti6858_tiocmset(struct tty_struct
*tty
, struct file
*file
,
160 unsigned int set
, unsigned int clear
);
161 static int oti6858_startup(struct usb_serial
*serial
);
162 static void oti6858_release(struct usb_serial
*serial
);
164 /* functions operating on buffers */
165 static struct oti6858_buf
*oti6858_buf_alloc(unsigned int size
);
166 static void oti6858_buf_free(struct oti6858_buf
*pb
);
167 static void oti6858_buf_clear(struct oti6858_buf
*pb
);
168 static unsigned int oti6858_buf_data_avail(struct oti6858_buf
*pb
);
169 static unsigned int oti6858_buf_space_avail(struct oti6858_buf
*pb
);
170 static unsigned int oti6858_buf_put(struct oti6858_buf
*pb
, const char *buf
,
172 static unsigned int oti6858_buf_get(struct oti6858_buf
*pb
, char *buf
,
177 static struct usb_serial_driver oti6858_device
= {
179 .owner
= THIS_MODULE
,
182 .id_table
= id_table
,
184 .open
= oti6858_open
,
185 .close
= oti6858_close
,
186 .write
= oti6858_write
,
187 .ioctl
= oti6858_ioctl
,
188 .set_termios
= oti6858_set_termios
,
189 .init_termios
= oti6858_init_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 .release
= oti6858_release
,
201 struct oti6858_private
{
204 struct oti6858_buf
*buf
;
205 struct oti6858_control_pkt status
;
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 */
226 static void setup_line(struct work_struct
*work
)
228 struct oti6858_private
*priv
= container_of(work
,
229 struct oti6858_private
, delayed_setup_work
.work
);
230 struct usb_serial_port
*port
= priv
->port
;
231 struct oti6858_control_pkt
*new_setup
;
235 dbg("%s(port = %d)", __func__
, port
->number
);
237 new_setup
= kmalloc(OTI6858_CTRL_PKT_SIZE
, GFP_KERNEL
);
238 if (new_setup
== NULL
) {
239 dev_err(&port
->dev
, "%s(): out of memory!\n", __func__
);
240 /* we will try again */
241 schedule_delayed_work(&priv
->delayed_setup_work
,
242 msecs_to_jiffies(2));
246 result
= usb_control_msg(port
->serial
->dev
,
247 usb_rcvctrlpipe(port
->serial
->dev
, 0),
248 OTI6858_REQ_T_GET_STATUS
,
249 OTI6858_REQ_GET_STATUS
,
251 new_setup
, OTI6858_CTRL_PKT_SIZE
,
254 if (result
!= OTI6858_CTRL_PKT_SIZE
) {
255 dev_err(&port
->dev
, "%s(): error reading status\n", __func__
);
257 /* we will try again */
258 schedule_delayed_work(&priv
->delayed_setup_work
,
259 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_KERNEL
);
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
,
301 struct oti6858_private
, delayed_write_work
.work
);
302 struct usb_serial_port
*port
= priv
->port
;
303 int count
= 0, result
;
307 dbg("%s(port = %d)", __func__
, port
->number
);
309 spin_lock_irqsave(&priv
->lock
, flags
);
310 if (priv
->flags
.write_urb_in_use
) {
311 spin_unlock_irqrestore(&priv
->lock
, flags
);
312 schedule_delayed_work(&priv
->delayed_write_work
,
313 msecs_to_jiffies(2));
316 priv
->flags
.write_urb_in_use
= 1;
318 count
= oti6858_buf_data_avail(priv
->buf
);
319 spin_unlock_irqrestore(&priv
->lock
, flags
);
320 if (count
> port
->bulk_out_size
)
321 count
= port
->bulk_out_size
;
324 allow
= kmalloc(1, GFP_KERNEL
);
326 dev_err(&port
->dev
, "%s(): kmalloc failed\n",
330 result
= usb_control_msg(port
->serial
->dev
,
331 usb_rcvctrlpipe(port
->serial
->dev
, 0),
332 OTI6858_REQ_T_CHECK_TXBUFF
,
333 OTI6858_REQ_CHECK_TXBUFF
,
334 count
, 0, allow
, 1, 100);
335 if (result
!= 1 || *allow
!= 0)
341 priv
->flags
.write_urb_in_use
= 0;
343 dbg("%s(): submitting interrupt urb", __func__
);
344 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
345 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_NOIO
);
347 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
348 " with error %d\n", __func__
, result
);
353 spin_lock_irqsave(&priv
->lock
, flags
);
354 oti6858_buf_get(priv
->buf
, port
->write_urb
->transfer_buffer
, count
);
355 spin_unlock_irqrestore(&priv
->lock
, flags
);
357 port
->write_urb
->transfer_buffer_length
= count
;
358 port
->write_urb
->dev
= port
->serial
->dev
;
359 result
= usb_submit_urb(port
->write_urb
, GFP_NOIO
);
361 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
362 " with error %d\n", __func__
, result
);
363 priv
->flags
.write_urb_in_use
= 0;
366 usb_serial_port_softint(port
);
369 static int oti6858_startup(struct usb_serial
*serial
)
371 struct usb_serial_port
*port
= serial
->port
[0];
372 struct oti6858_private
*priv
;
375 for (i
= 0; i
< serial
->num_ports
; ++i
) {
376 priv
= kzalloc(sizeof(struct oti6858_private
), GFP_KERNEL
);
379 priv
->buf
= oti6858_buf_alloc(PL2303_BUF_SIZE
);
380 if (priv
->buf
== NULL
) {
385 spin_lock_init(&priv
->lock
);
386 init_waitqueue_head(&priv
->intr_wait
);
387 /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
388 /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
390 INIT_DELAYED_WORK(&priv
->delayed_setup_work
, setup_line
);
391 INIT_DELAYED_WORK(&priv
->delayed_write_work
, send_data
);
393 usb_set_serial_port_data(serial
->port
[i
], priv
);
395 if (i
== serial
->num_ports
)
398 for (--i
; i
>= 0; --i
) {
399 priv
= usb_get_serial_port_data(serial
->port
[i
]);
400 oti6858_buf_free(priv
->buf
);
402 usb_set_serial_port_data(serial
->port
[i
], NULL
);
407 static int oti6858_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
408 const unsigned char *buf
, int count
)
410 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
413 dbg("%s(port = %d, count = %d)", __func__
, port
->number
, count
);
418 spin_lock_irqsave(&priv
->lock
, flags
);
419 count
= oti6858_buf_put(priv
->buf
, buf
, count
);
420 spin_unlock_irqrestore(&priv
->lock
, flags
);
425 static int oti6858_write_room(struct tty_struct
*tty
)
427 struct usb_serial_port
*port
= tty
->driver_data
;
428 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
432 dbg("%s(port = %d)", __func__
, port
->number
);
434 spin_lock_irqsave(&priv
->lock
, flags
);
435 room
= oti6858_buf_space_avail(priv
->buf
);
436 spin_unlock_irqrestore(&priv
->lock
, flags
);
441 static int oti6858_chars_in_buffer(struct tty_struct
*tty
)
443 struct usb_serial_port
*port
= tty
->driver_data
;
444 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
448 dbg("%s(port = %d)", __func__
, port
->number
);
450 spin_lock_irqsave(&priv
->lock
, flags
);
451 chars
= oti6858_buf_data_avail(priv
->buf
);
452 spin_unlock_irqrestore(&priv
->lock
, flags
);
457 static void oti6858_init_termios(struct tty_struct
*tty
)
459 *(tty
->termios
) = tty_std_termios
;
460 tty
->termios
->c_cflag
= B38400
| CS8
| CREAD
| HUPCL
| CLOCAL
;
461 tty
->termios
->c_ispeed
= 38400;
462 tty
->termios
->c_ospeed
= 38400;
465 static void oti6858_set_termios(struct tty_struct
*tty
,
466 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
468 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
471 u8 frame_fmt
, control
;
475 dbg("%s(port = %d)", __func__
, port
->number
);
478 dbg("%s(): no tty structures", __func__
);
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
, struct usb_serial_port
*port
)
576 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
577 struct ktermios tmp_termios
;
578 struct usb_serial
*serial
= port
->serial
;
579 struct oti6858_control_pkt
*buf
;
583 dbg("%s(port = %d)", __func__
, port
->number
);
585 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
586 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
588 buf
= kmalloc(OTI6858_CTRL_PKT_SIZE
, GFP_KERNEL
);
590 dev_err(&port
->dev
, "%s(): out of memory!\n", __func__
);
594 result
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
595 OTI6858_REQ_T_GET_STATUS
,
596 OTI6858_REQ_GET_STATUS
,
598 buf
, OTI6858_CTRL_PKT_SIZE
,
600 if (result
!= OTI6858_CTRL_PKT_SIZE
) {
601 /* assume default (after power-on reset) values */
602 buf
->divisor
= cpu_to_le16(0x009c); /* 38400 bps */
603 buf
->frame_fmt
= 0x03; /* 8N1 */
604 buf
->something
= 0x43;
605 buf
->control
= 0x4c; /* DTR, RTS */
606 buf
->tx_status
= 0x00;
607 buf
->pin_state
= 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
608 buf
->rx_bytes_avail
= 0x00;
611 spin_lock_irqsave(&priv
->lock
, flags
);
612 memcpy(&priv
->status
, buf
, OTI6858_CTRL_PKT_SIZE
);
613 priv
->pending_setup
.divisor
= buf
->divisor
;
614 priv
->pending_setup
.frame_fmt
= buf
->frame_fmt
;
615 priv
->pending_setup
.control
= buf
->control
;
616 spin_unlock_irqrestore(&priv
->lock
, flags
);
619 dbg("%s(): submitting interrupt urb", __func__
);
620 port
->interrupt_in_urb
->dev
= serial
->dev
;
621 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
623 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed"
624 " with error %d\n", __func__
, result
);
631 oti6858_set_termios(tty
, port
, &tmp_termios
);
632 port
->port
.drain_delay
= 256; /* FIXME: check the FIFO length */
636 static void oti6858_close(struct usb_serial_port
*port
)
638 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
641 dbg("%s(port = %d)", __func__
, port
->number
);
643 spin_lock_irqsave(&priv
->lock
, flags
);
644 /* clear out any remaining data in the buffer */
645 oti6858_buf_clear(priv
->buf
);
646 spin_unlock_irqrestore(&priv
->lock
, flags
);
648 dbg("%s(): after buf_clear()", __func__
);
650 /* cancel scheduled setup */
651 cancel_delayed_work(&priv
->delayed_setup_work
);
652 cancel_delayed_work(&priv
->delayed_write_work
);
653 flush_scheduled_work();
655 /* shutdown our urbs */
656 dbg("%s(): shutting down urbs", __func__
);
657 usb_kill_urb(port
->write_urb
);
658 usb_kill_urb(port
->read_urb
);
659 usb_kill_urb(port
->interrupt_in_urb
);
662 static int oti6858_tiocmset(struct tty_struct
*tty
, struct file
*file
,
663 unsigned int set
, unsigned int clear
)
665 struct usb_serial_port
*port
= tty
->driver_data
;
666 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
670 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
671 __func__
, port
->number
, set
, clear
);
673 if (!usb_get_intfdata(port
->serial
->interface
))
676 /* FIXME: check if this is correct (active high/low) */
677 spin_lock_irqsave(&priv
->lock
, flags
);
678 control
= priv
->pending_setup
.control
;
679 if ((set
& TIOCM_RTS
) != 0)
680 control
|= CONTROL_RTS_HIGH
;
681 if ((set
& TIOCM_DTR
) != 0)
682 control
|= CONTROL_DTR_HIGH
;
683 if ((clear
& TIOCM_RTS
) != 0)
684 control
&= ~CONTROL_RTS_HIGH
;
685 if ((clear
& TIOCM_DTR
) != 0)
686 control
&= ~CONTROL_DTR_HIGH
;
688 if (control
!= priv
->pending_setup
.control
)
689 priv
->pending_setup
.control
= control
;
691 spin_unlock_irqrestore(&priv
->lock
, flags
);
695 static int oti6858_tiocmget(struct tty_struct
*tty
, struct file
*file
)
697 struct usb_serial_port
*port
= tty
->driver_data
;
698 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
703 dbg("%s(port = %d)", __func__
, port
->number
);
705 if (!usb_get_intfdata(port
->serial
->interface
))
708 spin_lock_irqsave(&priv
->lock
, flags
);
709 pin_state
= priv
->status
.pin_state
& PIN_MASK
;
710 spin_unlock_irqrestore(&priv
->lock
, flags
);
712 /* FIXME: check if this is correct (active high/low) */
713 if ((pin_state
& PIN_RTS
) != 0)
715 if ((pin_state
& PIN_CTS
) != 0)
717 if ((pin_state
& PIN_DSR
) != 0)
719 if ((pin_state
& PIN_DTR
) != 0)
721 if ((pin_state
& PIN_RI
) != 0)
723 if ((pin_state
& PIN_DCD
) != 0)
726 dbg("%s() = 0x%08x", __func__
, result
);
731 static int wait_modem_info(struct usb_serial_port
*port
, unsigned int arg
)
733 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
735 unsigned int prev
, status
;
736 unsigned int changed
;
738 spin_lock_irqsave(&priv
->lock
, flags
);
739 prev
= priv
->status
.pin_state
;
740 spin_unlock_irqrestore(&priv
->lock
, flags
);
743 wait_event_interruptible(priv
->intr_wait
,
744 priv
->status
.pin_state
!= prev
);
745 if (signal_pending(current
))
748 spin_lock_irqsave(&priv
->lock
, flags
);
749 status
= priv
->status
.pin_state
& PIN_MASK
;
750 spin_unlock_irqrestore(&priv
->lock
, flags
);
752 changed
= prev
^ status
;
753 /* FIXME: check if this is correct (active high/low) */
754 if (((arg
& TIOCM_RNG
) && (changed
& PIN_RI
)) ||
755 ((arg
& TIOCM_DSR
) && (changed
& PIN_DSR
)) ||
756 ((arg
& TIOCM_CD
) && (changed
& PIN_DCD
)) ||
757 ((arg
& TIOCM_CTS
) && (changed
& PIN_CTS
)))
766 static int oti6858_ioctl(struct tty_struct
*tty
, struct file
*file
,
767 unsigned int cmd
, unsigned long arg
)
769 struct usb_serial_port
*port
= tty
->driver_data
;
771 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
772 __func__
, port
->number
, cmd
, arg
);
776 dbg("%s(): TIOCMIWAIT", __func__
);
777 return wait_modem_info(port
, arg
);
779 dbg("%s(): 0x%04x not supported", __func__
, cmd
);
786 static void oti6858_release(struct usb_serial
*serial
)
788 struct oti6858_private
*priv
;
791 dbg("%s()", __func__
);
793 for (i
= 0; i
< serial
->num_ports
; ++i
) {
794 priv
= usb_get_serial_port_data(serial
->port
[i
]);
796 oti6858_buf_free(priv
->buf
);
802 static void oti6858_read_int_callback(struct urb
*urb
)
804 struct usb_serial_port
*port
= urb
->context
;
805 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
806 int transient
= 0, can_recv
= 0, resubmit
= 1;
807 int status
= urb
->status
;
809 dbg("%s(port = %d, status = %d)",
810 __func__
, port
->number
, status
);
819 /* this urb is terminated, clean up */
820 dbg("%s(): urb shutting down with status: %d",
824 dbg("%s(): nonzero urb status received: %d",
829 if (status
== 0 && urb
->actual_length
== OTI6858_CTRL_PKT_SIZE
) {
830 struct oti6858_control_pkt
*xs
= urb
->transfer_buffer
;
833 spin_lock_irqsave(&priv
->lock
, flags
);
835 if (!priv
->transient
) {
836 if (!OTI6858_CTRL_EQUALS_PENDING(xs
, priv
)) {
837 if (xs
->rx_bytes_avail
== 0) {
839 priv
->setup_done
= 0;
841 dbg("%s(): scheduling setup_line()",
843 schedule_delayed_work(&priv
->delayed_setup_work
, 0);
847 if (OTI6858_CTRL_EQUALS_PENDING(xs
, priv
)) {
849 } else if (!priv
->setup_done
) {
851 } else if (--priv
->transient
== 0) {
852 if (xs
->rx_bytes_avail
== 0) {
854 priv
->setup_done
= 0;
856 dbg("%s(): scheduling setup_line()",
858 schedule_delayed_work(&priv
->delayed_setup_work
, 0);
863 if (!priv
->transient
) {
864 if (xs
->pin_state
!= priv
->status
.pin_state
)
865 wake_up_interruptible(&priv
->intr_wait
);
866 memcpy(&priv
->status
, xs
, OTI6858_CTRL_PKT_SIZE
);
869 if (!priv
->transient
&& xs
->rx_bytes_avail
!= 0) {
870 can_recv
= xs
->rx_bytes_avail
;
871 priv
->flags
.read_urb_in_use
= 1;
874 transient
= priv
->transient
;
875 spin_unlock_irqrestore(&priv
->lock
, flags
);
881 port
->read_urb
->dev
= port
->serial
->dev
;
882 result
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
884 priv
->flags
.read_urb_in_use
= 0;
885 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
886 " error %d\n", __func__
, result
);
890 } else if (!transient
) {
893 spin_lock_irqsave(&priv
->lock
, flags
);
894 if (priv
->flags
.write_urb_in_use
== 0
895 && oti6858_buf_data_avail(priv
->buf
) != 0) {
896 schedule_delayed_work(&priv
->delayed_write_work
, 0);
899 spin_unlock_irqrestore(&priv
->lock
, flags
);
905 /* dbg("%s(): submitting interrupt urb", __func__); */
906 urb
->dev
= port
->serial
->dev
;
907 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
909 dev_err(&urb
->dev
->dev
,
910 "%s(): usb_submit_urb() failed with"
911 " error %d\n", __func__
, result
);
916 static void oti6858_read_bulk_callback(struct urb
*urb
)
918 struct usb_serial_port
*port
= urb
->context
;
919 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
920 struct tty_struct
*tty
;
921 unsigned char *data
= urb
->transfer_buffer
;
923 int status
= urb
->status
;
926 dbg("%s(port = %d, status = %d)",
927 __func__
, port
->number
, status
);
929 spin_lock_irqsave(&priv
->lock
, flags
);
930 priv
->flags
.read_urb_in_use
= 0;
931 spin_unlock_irqrestore(&priv
->lock
, flags
);
935 if (status == -EPROTO) {
936 * PL2303 mysteriously fails with -EPROTO reschedule
938 dbg("%s - caught -EPROTO, resubmitting the urb",
940 result = usb_submit_urb(urb, GFP_ATOMIC);
942 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
946 dbg("%s(): unable to handle the error, exiting", __func__
);
950 tty
= tty_port_tty_get(&port
->port
);
951 if (tty
!= NULL
&& urb
->actual_length
> 0) {
952 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
953 tty_flip_buffer_push(tty
);
957 /* schedule the interrupt urb */
958 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
959 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
960 if (result
!= 0 && result
!= -EPERM
) {
961 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
962 " error %d\n", __func__
, result
);
966 static void oti6858_write_bulk_callback(struct urb
*urb
)
968 struct usb_serial_port
*port
= urb
->context
;
969 struct oti6858_private
*priv
= usb_get_serial_port_data(port
);
970 int status
= urb
->status
;
973 dbg("%s(port = %d, status = %d)",
974 __func__
, port
->number
, status
);
983 /* this urb is terminated, clean up */
984 dbg("%s(): urb shutting down with status: %d",
986 priv
->flags
.write_urb_in_use
= 0;
989 /* error in the urb, so we have to resubmit it */
990 dbg("%s(): nonzero write bulk status received: %d",
992 dbg("%s(): overflow in write", __func__
);
994 port
->write_urb
->transfer_buffer_length
= 1;
995 port
->write_urb
->dev
= port
->serial
->dev
;
996 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
998 dev_err(&port
->dev
, "%s(): usb_submit_urb() failed,"
999 " error %d\n", __func__
, result
);
1005 priv
->flags
.write_urb_in_use
= 0;
1007 /* schedule the interrupt urb if we are still open */
1008 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
1009 dbg("%s(): submitting interrupt urb", __func__
);
1010 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_ATOMIC
);
1012 dev_err(&port
->dev
, "%s(): failed submitting int urb,"
1013 " error %d\n", __func__
, result
);
1021 * Allocate a circular buffer and all associated memory.
1023 static struct oti6858_buf
*oti6858_buf_alloc(unsigned int size
)
1025 struct oti6858_buf
*pb
;
1030 pb
= kmalloc(sizeof(struct oti6858_buf
), GFP_KERNEL
);
1034 pb
->buf_buf
= kmalloc(size
, GFP_KERNEL
);
1035 if (pb
->buf_buf
== NULL
) {
1040 pb
->buf_size
= size
;
1041 pb
->buf_get
= pb
->buf_put
= pb
->buf_buf
;
1049 * Free the buffer and all associated memory.
1051 static void oti6858_buf_free(struct oti6858_buf
*pb
)
1062 * Clear out all data in the circular buffer.
1064 static void oti6858_buf_clear(struct oti6858_buf
*pb
)
1067 /* equivalent to a get of all data available */
1068 pb
->buf_get
= pb
->buf_put
;
1073 * oti6858_buf_data_avail
1075 * Return the number of bytes of data available in the circular
1078 static unsigned int oti6858_buf_data_avail(struct oti6858_buf
*pb
)
1082 return (pb
->buf_size
+ pb
->buf_put
- pb
->buf_get
) % pb
->buf_size
;
1086 * oti6858_buf_space_avail
1088 * Return the number of bytes of space available in the circular
1091 static unsigned int oti6858_buf_space_avail(struct oti6858_buf
*pb
)
1095 return (pb
->buf_size
+ pb
->buf_get
- pb
->buf_put
- 1) % pb
->buf_size
;
1101 * Copy data data from a user buffer and put it into the circular buffer.
1102 * Restrict to the amount of space available.
1104 * Return the number of bytes copied.
1106 static unsigned int oti6858_buf_put(struct oti6858_buf
*pb
, const char *buf
,
1114 len
= oti6858_buf_space_avail(pb
);
1121 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_put
;
1123 memcpy(pb
->buf_put
, buf
, len
);
1124 memcpy(pb
->buf_buf
, buf
+len
, count
- len
);
1125 pb
->buf_put
= pb
->buf_buf
+ count
- len
;
1127 memcpy(pb
->buf_put
, buf
, count
);
1129 pb
->buf_put
+= count
;
1130 else /* count == len */
1131 pb
->buf_put
= pb
->buf_buf
;
1140 * Get data from the circular buffer and copy to the given buffer.
1141 * Restrict to the amount of data available.
1143 * Return the number of bytes copied.
1145 static unsigned int oti6858_buf_get(struct oti6858_buf
*pb
, char *buf
,
1153 len
= oti6858_buf_data_avail(pb
);
1160 len
= pb
->buf_buf
+ pb
->buf_size
- pb
->buf_get
;
1162 memcpy(buf
, pb
->buf_get
, len
);
1163 memcpy(buf
+len
, pb
->buf_buf
, count
- len
);
1164 pb
->buf_get
= pb
->buf_buf
+ count
- len
;
1166 memcpy(buf
, pb
->buf_get
, count
);
1168 pb
->buf_get
+= count
;
1169 else /* count == len */
1170 pb
->buf_get
= pb
->buf_buf
;
1176 /* module description and (de)initialization */
1178 static int __init
oti6858_init(void)
1182 retval
= usb_serial_register(&oti6858_device
);
1184 retval
= usb_register(&oti6858_driver
);
1186 usb_serial_deregister(&oti6858_device
);
1191 static void __exit
oti6858_exit(void)
1193 usb_deregister(&oti6858_driver
);
1194 usb_serial_deregister(&oti6858_device
);
1197 module_init(oti6858_init
);
1198 module_exit(oti6858_exit
);
1200 MODULE_DESCRIPTION(OTI6858_DESCRIPTION
);
1201 MODULE_AUTHOR(OTI6858_AUTHOR
);
1202 MODULE_VERSION(OTI6858_VERSION
);
1203 MODULE_LICENSE("GPL");
1205 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1206 MODULE_PARM_DESC(debug
, "enable debug output");