2 Option Card (PCMCIA to) USB to Serial Driver
4 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
6 This driver is free software; you can redistribute it and/or modify
7 it under the terms of Version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
10 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
14 2005-05-19 v0.1 Initial version, based on incomplete docs
15 and analysis of misbehavior of the standard driver
16 2005-05-20 v0.2 Extended the input buffer to avoid losing
17 random 64-byte chunks of data
18 2005-05-21 v0.3 implemented chars_in_buffer()
20 simplified the code somewhat
22 #define DRIVER_VERSION "v0.3"
23 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
24 #define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/jiffies.h>
29 #include <linux/errno.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/usb.h>
34 #include "usb-serial.h"
36 /* Function prototypes */
37 static int option_open (struct usb_serial_port
*port
, struct file
*filp
);
38 static void option_close (struct usb_serial_port
*port
, struct file
*filp
);
39 static int option_startup (struct usb_serial
*serial
);
40 static void option_shutdown (struct usb_serial
*serial
);
41 static void option_rx_throttle (struct usb_serial_port
*port
);
42 static void option_rx_unthrottle (struct usb_serial_port
*port
);
43 static int option_write_room (struct usb_serial_port
*port
);
45 static void option_instat_callback(struct urb
*urb
, struct pt_regs
*regs
);
48 static int option_write (struct usb_serial_port
*port
,
49 const unsigned char *buf
, int count
);
51 static int option_chars_in_buffer (struct usb_serial_port
*port
);
52 static int option_ioctl (struct usb_serial_port
*port
, struct file
*file
,
53 unsigned int cmd
, unsigned long arg
);
54 static void option_set_termios (struct usb_serial_port
*port
,
56 static void option_break_ctl (struct usb_serial_port
*port
, int break_state
);
57 static int option_tiocmget (struct usb_serial_port
*port
, struct file
*file
);
58 static int option_tiocmset (struct usb_serial_port
*port
, struct file
*file
,
59 unsigned int set
, unsigned int clear
);
60 static int option_send_setup (struct usb_serial_port
*port
);
62 /* Vendor and product IDs */
63 #define OPTION_VENDOR_ID 0x0AF0
65 #define OPTION_PRODUCT_OLD 0x5000
66 #define OPTION_PRODUCT_WLAN 0x6000
68 static struct usb_device_id option_ids
[] = {
69 { USB_DEVICE(OPTION_VENDOR_ID
, OPTION_PRODUCT_OLD
) },
70 { USB_DEVICE(OPTION_VENDOR_ID
, OPTION_PRODUCT_WLAN
) },
71 { } /* Terminating entry */
74 MODULE_DEVICE_TABLE(usb
, option_ids
);
76 static struct usb_driver option_driver
= {
79 .probe
= usb_serial_probe
,
80 .disconnect
= usb_serial_disconnect
,
81 .id_table
= option_ids
,
84 /* The card has three separate interfaces, wich the serial driver
85 * recognizes separately, thus num_port=1.
87 static struct usb_serial_device_type option_3port_device
= {
89 .name
= "Option 3-port card",
90 .short_name
= "option",
91 .id_table
= option_ids
,
92 .num_interrupt_in
= NUM_DONT_CARE
,
93 .num_bulk_in
= NUM_DONT_CARE
,
94 .num_bulk_out
= NUM_DONT_CARE
,
95 .num_ports
= 1, /* 3 */
97 .close
= option_close
,
98 .write
= option_write
,
99 .write_room
= option_write_room
,
100 .chars_in_buffer
= option_chars_in_buffer
,
101 .throttle
= option_rx_throttle
,
102 .unthrottle
= option_rx_unthrottle
,
103 .ioctl
= option_ioctl
,
104 .set_termios
= option_set_termios
,
105 .break_ctl
= option_break_ctl
,
106 .tiocmget
= option_tiocmget
,
107 .tiocmset
= option_tiocmset
,
108 .attach
= option_startup
,
109 .shutdown
= option_shutdown
,
110 .read_int_callback
= option_instat_callback
,
115 /* per port private data */
119 #define IN_BUFLEN 1024
120 #define OUT_BUFLEN 1024
122 struct option_port_private
{
123 /* Input endpoints and buffer for this port */
124 struct urb
*in_urbs
[N_IN_URB
];
125 char in_buffer
[N_IN_URB
][IN_BUFLEN
];
126 /* Output endpoints and buffer for this port */
127 struct urb
*out_urbs
[N_OUT_URB
];
128 char out_buffer
[N_OUT_URB
][OUT_BUFLEN
];
130 /* Settings for the port */
131 int rts_state
; /* Handshaking pins (outputs) */
133 int cts_state
; /* Handshaking pins (inputs) */
139 unsigned long tx_start_time
[N_OUT_URB
];
143 /* Functions used by new usb-serial code. */
148 retval
= usb_serial_register(&option_3port_device
);
150 goto failed_3port_device_register
;
151 retval
= usb_register(&option_driver
);
153 goto failed_driver_register
;
155 info(DRIVER_DESC
": " DRIVER_VERSION
);
159 failed_driver_register
:
160 usb_serial_deregister (&option_3port_device
);
161 failed_3port_device_register
:
168 usb_deregister (&option_driver
);
169 usb_serial_deregister (&option_3port_device
);
172 module_init(option_init
);
173 module_exit(option_exit
);
176 option_rx_throttle (struct usb_serial_port
*port
)
178 dbg("%s", __FUNCTION__
);
183 option_rx_unthrottle (struct usb_serial_port
*port
)
185 dbg("%s", __FUNCTION__
);
190 option_break_ctl (struct usb_serial_port
*port
, int break_state
)
192 /* Unfortunately, I don't know how to send a break */
193 dbg("%s", __FUNCTION__
);
198 option_set_termios (struct usb_serial_port
*port
,
199 struct termios
*old_termios
)
201 dbg("%s", __FUNCTION__
);
203 option_send_setup(port
);
207 option_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
210 struct option_port_private
*portdata
;
212 portdata
= usb_get_serial_port_data(port
);
214 value
= ((portdata
->rts_state
) ? TIOCM_RTS
: 0) |
215 ((portdata
->dtr_state
) ? TIOCM_DTR
: 0) |
216 ((portdata
->cts_state
) ? TIOCM_CTS
: 0) |
217 ((portdata
->dsr_state
) ? TIOCM_DSR
: 0) |
218 ((portdata
->dcd_state
) ? TIOCM_CAR
: 0) |
219 ((portdata
->ri_state
) ? TIOCM_RNG
: 0);
225 option_tiocmset (struct usb_serial_port
*port
, struct file
*file
,
226 unsigned int set
, unsigned int clear
)
228 struct option_port_private
*portdata
;
230 portdata
= usb_get_serial_port_data(port
);
233 portdata
->rts_state
= 1;
235 portdata
->dtr_state
= 1;
237 if (clear
& TIOCM_RTS
)
238 portdata
->rts_state
= 0;
239 if (clear
& TIOCM_DTR
)
240 portdata
->dtr_state
= 0;
241 return option_send_setup(port
);
245 option_ioctl (struct usb_serial_port
*port
, struct file
*file
,
246 unsigned int cmd
, unsigned long arg
)
253 option_write(struct usb_serial_port
*port
,
254 const unsigned char *buf
, int count
)
256 struct option_port_private
*portdata
;
259 struct urb
*this_urb
= NULL
; /* spurious */
262 portdata
= usb_get_serial_port_data(port
);
264 dbg("%s: write (%d chars)", __FUNCTION__
, count
);
267 spin_lock(&port
->lock
);
268 if (port
->write_urb_busy
) {
269 spin_unlock(&port
->lock
);
270 dbg("%s: already writing", __FUNCTION__
);
273 port
->write_urb_busy
= 1;
274 spin_unlock(&port
->lock
);
281 if (todo
> OUT_BUFLEN
)
284 for (;i
< N_OUT_URB
; i
++) {
285 /* Check we have a valid urb/endpoint before we use it... */
286 this_urb
= portdata
->out_urbs
[i
];
287 if (this_urb
->status
!= -EINPROGRESS
)
289 if (this_urb
->transfer_flags
& URB_ASYNC_UNLINK
)
291 if (time_before(jiffies
, portdata
->tx_start_time
[i
] + 10 * HZ
))
293 this_urb
->transfer_flags
|= URB_ASYNC_UNLINK
;
294 usb_unlink_urb(this_urb
);
297 if (i
== N_OUT_URB
) {
298 /* no bulk out free! */
299 dbg("%s: no output urb -- left %d", __FUNCTION__
,count
-left
);
301 port
->write_urb_busy
= 0;
306 dbg("%s: endpoint %d buf %d", __FUNCTION__
, usb_pipeendpoint(this_urb
->pipe
), i
);
308 memcpy (this_urb
->transfer_buffer
, buf
, todo
);
310 /* send the data out the bulk port */
311 this_urb
->transfer_buffer_length
= todo
;
313 this_urb
->transfer_flags
&= ~URB_ASYNC_UNLINK
;
314 this_urb
->dev
= port
->serial
->dev
;
315 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
317 dbg("usb_submit_urb %p (write bulk) failed (%d,, has %d)", this_urb
, err
, this_urb
->status
);
320 portdata
->tx_start_time
[i
] = jiffies
;
327 port
->write_urb_busy
= 0;
329 dbg("%s: wrote (did %d)", __FUNCTION__
, count
);
334 option_indat_callback (struct urb
*urb
, struct pt_regs
*regs
)
338 struct usb_serial_port
*port
;
339 struct tty_struct
*tty
;
340 unsigned char *data
= urb
->transfer_buffer
;
342 dbg("%s: %p", __FUNCTION__
, urb
);
344 endpoint
= usb_pipeendpoint(urb
->pipe
);
345 port
= (struct usb_serial_port
*) urb
->context
;
348 dbg("%s: nonzero status: %d on endpoint %02x.",
349 __FUNCTION__
, urb
->status
, endpoint
);
352 if (urb
->actual_length
) {
353 for (i
= 0; i
< urb
->actual_length
; ++i
) {
354 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
355 tty_flip_buffer_push(tty
);
356 tty_insert_flip_char(tty
, data
[i
], 0);
358 tty_flip_buffer_push(tty
);
360 dbg("%s: empty read urb received", __FUNCTION__
);
363 /* Resubmit urb so we continue receiving */
364 if (port
->open_count
&& urb
->status
!= -ESHUTDOWN
) {
365 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
367 printk(KERN_ERR
"%s: resubmit read urb failed. (%d)", __FUNCTION__
, err
);
374 option_outdat_callback (struct urb
*urb
, struct pt_regs
*regs
)
376 struct usb_serial_port
*port
;
378 dbg("%s", __FUNCTION__
);
380 port
= (struct usb_serial_port
*) urb
->context
;
382 if (port
->open_count
)
383 schedule_work(&port
->work
);
387 option_instat_callback (struct urb
*urb
, struct pt_regs
*regs
)
390 struct usb_serial_port
*port
= (struct usb_serial_port
*) urb
->context
;
391 struct option_port_private
*portdata
= usb_get_serial_port_data(port
);
392 struct usb_serial
*serial
= port
->serial
;
394 dbg("%s", __FUNCTION__
);
395 dbg("%s: urb %p port %p has data %p", __FUNCTION__
,urb
,port
,portdata
);
397 if (urb
->status
== 0) {
398 struct usb_ctrlrequest
*req_pkt
=
399 (struct usb_ctrlrequest
*)urb
->transfer_buffer
;
402 dbg("%s: NULL req_pkt\n", __FUNCTION__
);
405 if ((req_pkt
->bRequestType
== 0xA1) && (req_pkt
->bRequest
== 0x20)) {
407 unsigned char signals
= *((unsigned char *)
408 urb
->transfer_buffer
+ sizeof(struct usb_ctrlrequest
));
410 dbg("%s: signal x%x", __FUNCTION__
, signals
);
412 old_dcd_state
= portdata
->dcd_state
;
413 portdata
->cts_state
= 1;
414 portdata
->dcd_state
= ((signals
& 0x01) ? 1 : 0);
415 portdata
->dsr_state
= ((signals
& 0x02) ? 1 : 0);
416 portdata
->ri_state
= ((signals
& 0x08) ? 1 : 0);
418 if (port
->tty
&& !C_CLOCAL(port
->tty
)
419 && old_dcd_state
&& !portdata
->dcd_state
) {
420 tty_hangup(port
->tty
);
423 dbg("%s: type %x req %x", __FUNCTION__
, req_pkt
->bRequestType
,req_pkt
->bRequest
);
425 dbg("%s: error %d", __FUNCTION__
, urb
->status
);
427 /* Resubmit urb so we continue receiving IRQ data */
428 if (urb
->status
!= -ESHUTDOWN
) {
429 urb
->dev
= serial
->dev
;
430 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
432 dbg("%s: resubmit intr urb failed. (%d)", __FUNCTION__
, err
);
438 option_write_room (struct usb_serial_port
*port
)
440 struct option_port_private
*portdata
;
443 struct urb
*this_urb
;
445 portdata
= usb_get_serial_port_data(port
);
447 for (i
=0; i
< N_OUT_URB
; i
++)
448 this_urb
= portdata
->out_urbs
[i
];
449 if (this_urb
&& this_urb
->status
!= -EINPROGRESS
)
450 data_len
+= OUT_BUFLEN
;
452 dbg("%s: %d", __FUNCTION__
, data_len
);
458 option_chars_in_buffer (struct usb_serial_port
*port
)
460 struct option_port_private
*portdata
;
463 struct urb
*this_urb
;
465 portdata
= usb_get_serial_port_data(port
);
467 for (i
=0; i
< N_OUT_URB
; i
++)
468 this_urb
= portdata
->out_urbs
[i
];
469 if (this_urb
&& this_urb
->status
== -EINPROGRESS
)
470 data_len
+= this_urb
->transfer_buffer_length
;
472 dbg("%s: %d", __FUNCTION__
, data_len
);
478 option_open (struct usb_serial_port
*port
, struct file
*filp
)
480 struct option_port_private
*portdata
;
481 struct usb_serial
*serial
= port
->serial
;
485 portdata
= usb_get_serial_port_data(port
);
487 dbg("%s", __FUNCTION__
);
489 /* Set some sane defaults */
490 portdata
->rts_state
= 1;
491 portdata
->dtr_state
= 1;
493 /* Reset low level data toggle and start reading from endpoints */
494 for (i
= 0; i
< N_IN_URB
; i
++) {
495 urb
= portdata
->in_urbs
[i
];
498 if (urb
->dev
!= serial
->dev
) {
499 dbg("%s: dev %p != %p", __FUNCTION__
, urb
->dev
, serial
->dev
);
503 /* make sure endpoint data toggle is synchronized with the device */
505 usb_clear_halt(urb
->dev
, urb
->pipe
);
507 err
= usb_submit_urb(urb
, GFP_KERNEL
);
509 dbg("%s: submit urb %d failed (%d) %d", __FUNCTION__
, i
, err
,
510 urb
->transfer_buffer_length
);
514 /* Reset low level data toggle on out endpoints */
515 for (i
= 0; i
< N_OUT_URB
; i
++) {
516 urb
= portdata
->out_urbs
[i
];
519 urb
->dev
= serial
->dev
;
520 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
523 port
->tty
->low_latency
= 1;
525 option_send_setup(port
);
531 stop_urb(struct urb
*urb
)
533 if (urb
&& urb
->status
== -EINPROGRESS
) {
534 urb
->transfer_flags
&= ~URB_ASYNC_UNLINK
;
540 option_close(struct usb_serial_port
*port
, struct file
*filp
)
543 struct usb_serial
*serial
= port
->serial
;
544 struct option_port_private
*portdata
;
546 dbg("%s", __FUNCTION__
);
547 portdata
= usb_get_serial_port_data(port
);
549 portdata
->rts_state
= 0;
550 portdata
->dtr_state
= 0;
553 option_send_setup(port
);
555 /* Stop reading/writing urbs */
556 for (i
= 0; i
< N_IN_URB
; i
++)
557 stop_urb(portdata
->in_urbs
[i
]);
558 for (i
= 0; i
< N_OUT_URB
; i
++)
559 stop_urb(portdata
->out_urbs
[i
]);
565 /* Helper functions used by option_setup_urbs */
567 option_setup_urb (struct usb_serial
*serial
, int endpoint
,
568 int dir
, void *ctx
, char *buf
, int len
,
569 void (*callback
)(struct urb
*, struct pt_regs
*regs
))
574 return NULL
; /* endpoint not needed */
576 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
578 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__
, endpoint
);
582 /* Fill URB using supplied data. */
583 usb_fill_bulk_urb(urb
, serial
->dev
,
584 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
585 buf
, len
, callback
, ctx
);
592 option_setup_urbs(struct usb_serial
*serial
)
595 struct usb_serial_port
*port
;
596 struct option_port_private
*portdata
;
598 dbg("%s", __FUNCTION__
);
600 port
= serial
->port
[0];
601 portdata
= usb_get_serial_port_data(port
);
603 /* Do indat endpoints first */
604 for (j
= 0; j
<= N_IN_URB
; ++j
) {
605 portdata
->in_urbs
[j
] = option_setup_urb (serial
,
606 port
->bulk_in_endpointAddress
, USB_DIR_IN
, port
,
607 portdata
->in_buffer
[j
], IN_BUFLEN
, option_indat_callback
);
610 /* outdat endpoints */
611 for (j
= 0; j
<= N_OUT_URB
; ++j
) {
612 portdata
->out_urbs
[j
] = option_setup_urb (serial
,
613 port
->bulk_out_endpointAddress
, USB_DIR_OUT
, port
,
614 portdata
->out_buffer
[j
], OUT_BUFLEN
, option_outdat_callback
);
620 option_send_setup(struct usb_serial_port
*port
)
622 struct usb_serial
*serial
= port
->serial
;
623 struct option_port_private
*portdata
;
625 dbg("%s", __FUNCTION__
);
627 portdata
= usb_get_serial_port_data(port
);
631 if (portdata
->dtr_state
)
633 if (portdata
->rts_state
)
636 return usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
637 0x22,0x21,val
,0,NULL
,0,USB_CTRL_SET_TIMEOUT
);
645 option_startup (struct usb_serial
*serial
)
648 struct usb_serial_port
*port
;
649 struct option_port_private
*portdata
;
651 dbg("%s", __FUNCTION__
);
653 /* Now setup per port private data */
654 for (i
= 0; i
< serial
->num_ports
; i
++) {
655 port
= serial
->port
[i
];
656 portdata
= kmalloc(sizeof(struct option_port_private
), GFP_KERNEL
);
658 dbg("%s: kmalloc for option_port_private (%d) failed!.", __FUNCTION__
, i
);
661 memset(portdata
, 0, sizeof(struct option_port_private
));
663 usb_set_serial_port_data(port
, portdata
);
665 if (! port
->interrupt_in_urb
)
667 err
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
669 dbg("%s: submit irq_in urb failed %d", __FUNCTION__
, err
);
672 option_setup_urbs(serial
);
678 option_shutdown (struct usb_serial
*serial
)
681 struct usb_serial_port
*port
;
682 struct option_port_private
*portdata
;
684 dbg("%s", __FUNCTION__
);
686 /* Stop reading/writing urbs */
687 for (i
= 0; i
< serial
->num_ports
; ++i
) {
688 port
= serial
->port
[i
];
689 portdata
= usb_get_serial_port_data(port
);
690 for (j
= 0; j
< N_IN_URB
; j
++)
691 stop_urb(portdata
->in_urbs
[j
]);
692 for (j
= 0; j
< N_OUT_URB
; j
++)
693 stop_urb(portdata
->out_urbs
[j
]);
697 for (i
= 0; i
< serial
->num_ports
; ++i
) {
698 port
= serial
->port
[i
];
699 portdata
= usb_get_serial_port_data(port
);
701 for (j
= 0; j
< N_IN_URB
; j
++) {
702 if (portdata
->in_urbs
[j
]) {
703 usb_free_urb(portdata
->in_urbs
[j
]);
704 portdata
->in_urbs
[j
] = NULL
;
707 for (j
= 0; j
< N_OUT_URB
; j
++) {
708 if (portdata
->out_urbs
[j
]) {
709 usb_free_urb(portdata
->out_urbs
[j
]);
710 portdata
->out_urbs
[j
] = NULL
;
715 /* Now free per port private data */
716 for (i
= 0; i
< serial
->num_ports
; i
++) {
717 port
= serial
->port
[i
];
718 kfree(usb_get_serial_port_data(port
));
722 MODULE_AUTHOR(DRIVER_AUTHOR
);
723 MODULE_DESCRIPTION(DRIVER_DESC
);
724 MODULE_VERSION(DRIVER_VERSION
);
725 MODULE_LICENSE("GPL");
727 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
728 MODULE_PARM_DESC(debug
, "Debug messages");