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 with 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
21 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load
22 removed some dead code
25 2005-06-20 v0.4.1 add missing braces :-/
26 killed end-of-line whitespace
27 2005-07-15 v0.4.2 rename WLAN product to FUSION, add FUSION2
29 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
33 #define DRIVER_VERSION "v0.4"
34 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
35 #define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
37 #include <linux/config.h>
38 #include <linux/kernel.h>
39 #include <linux/jiffies.h>
40 #include <linux/errno.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/module.h>
44 #include <linux/usb.h>
45 #include "usb-serial.h"
47 /* Function prototypes */
48 static int option_open (struct usb_serial_port *port, struct file *filp);
49 static void option_close (struct usb_serial_port *port, struct file *filp);
50 static int option_startup (struct usb_serial *serial);
51 static void option_shutdown (struct usb_serial *serial);
52 static void option_rx_throttle (struct usb_serial_port *port);
53 static void option_rx_unthrottle (struct usb_serial_port *port);
54 static int option_write_room (struct usb_serial_port *port);
56 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
58 static int option_write (struct usb_serial_port *port,
59 const unsigned char *buf, int count);
61 static int option_chars_in_buffer (struct usb_serial_port *port);
62 static int option_ioctl (struct usb_serial_port *port, struct file *file,
63 unsigned int cmd, unsigned long arg);
64 static void option_set_termios (struct usb_serial_port *port,
66 static void option_break_ctl (struct usb_serial_port *port, int break_state);
67 static int option_tiocmget (struct usb_serial_port *port, struct file *file);
68 static int option_tiocmset (struct usb_serial_port *port, struct file *file,
69 unsigned int set, unsigned int clear);
70 static int option_send_setup (struct usb_serial_port *port);
72 /* Vendor and product IDs */
73 #define OPTION_VENDOR_ID 0x0AF0
75 #define OPTION_PRODUCT_OLD 0x5000
76 #define OPTION_PRODUCT_FUSION 0x6000
77 #define OPTION_PRODUCT_FUSION2 0x6300
80 static struct usb_device_id option_ids[] = {
81 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
82 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
83 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
84 { } /* Terminating entry */
87 MODULE_DEVICE_TABLE(usb, option_ids);
89 static struct usb_driver option_driver = {
92 .probe = usb_serial_probe,
93 .disconnect = usb_serial_disconnect,
94 .id_table = option_ids,
97 /* The card has three separate interfaces, wich the serial driver
98 * recognizes separately, thus num_port=1.
100 static struct usb_serial_device_type option_3port_device = {
101 .owner = THIS_MODULE,
102 .name = "Option 3G data card",
103 .short_name = "option",
104 .id_table = option_ids,
105 .num_interrupt_in = NUM_DONT_CARE,
106 .num_bulk_in = NUM_DONT_CARE,
107 .num_bulk_out = NUM_DONT_CARE,
108 .num_ports = 1, /* 3, but the card reports its ports separately */
110 .close = option_close,
111 .write = option_write,
112 .write_room = option_write_room,
113 .chars_in_buffer = option_chars_in_buffer,
114 .throttle = option_rx_throttle,
115 .unthrottle = option_rx_unthrottle,
116 .ioctl = option_ioctl,
117 .set_termios = option_set_termios,
118 .break_ctl = option_break_ctl,
119 .tiocmget = option_tiocmget,
120 .tiocmset = option_tiocmset,
121 .attach = option_startup,
122 .shutdown = option_shutdown,
123 .read_int_callback = option_instat_callback,
126 #ifdef CONFIG_USB_DEBUG
133 /* per port private data */
137 #define IN_BUFLEN 1024
138 #define OUT_BUFLEN 128
140 struct option_port_private {
141 /* Input endpoints and buffer for this port */
142 struct urb *in_urbs[N_IN_URB];
143 char in_buffer[N_IN_URB][IN_BUFLEN];
144 /* Output endpoints and buffer for this port */
145 struct urb *out_urbs[N_OUT_URB];
146 char out_buffer[N_OUT_URB][OUT_BUFLEN];
148 /* Settings for the port */
149 int rts_state; /* Handshaking pins (outputs) */
151 int cts_state; /* Handshaking pins (inputs) */
156 unsigned long tx_start_time[N_OUT_URB];
160 /* Functions used by new usb-serial code. */
165 retval = usb_serial_register(&option_3port_device);
167 goto failed_3port_device_register;
168 retval = usb_register(&option_driver);
170 goto failed_driver_register;
172 info(DRIVER_DESC ": " DRIVER_VERSION);
176 failed_driver_register:
177 usb_serial_deregister (&option_3port_device);
178 failed_3port_device_register:
185 usb_deregister (&option_driver);
186 usb_serial_deregister (&option_3port_device);
189 module_init(option_init);
190 module_exit(option_exit);
193 option_rx_throttle (struct usb_serial_port *port)
195 dbg("%s", __FUNCTION__);
200 option_rx_unthrottle (struct usb_serial_port *port)
202 dbg("%s", __FUNCTION__);
207 option_break_ctl (struct usb_serial_port *port, int break_state)
209 /* Unfortunately, I don't know how to send a break */
210 dbg("%s", __FUNCTION__);
215 option_set_termios (struct usb_serial_port *port,
216 struct termios *old_termios)
218 dbg("%s", __FUNCTION__);
220 option_send_setup(port);
224 option_tiocmget (struct usb_serial_port *port, struct file *file)
227 struct option_port_private *portdata;
229 portdata = usb_get_serial_port_data(port);
231 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
232 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
233 ((portdata->cts_state) ? TIOCM_CTS : 0) |
234 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
235 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
236 ((portdata->ri_state) ? TIOCM_RNG : 0);
242 option_tiocmset (struct usb_serial_port *port, struct file *file,
243 unsigned int set, unsigned int clear)
245 struct option_port_private *portdata;
247 portdata = usb_get_serial_port_data(port);
250 portdata->rts_state = 1;
252 portdata->dtr_state = 1;
254 if (clear & TIOCM_RTS)
255 portdata->rts_state = 0;
256 if (clear & TIOCM_DTR)
257 portdata->dtr_state = 0;
258 return option_send_setup(port);
262 option_ioctl (struct usb_serial_port *port, struct file *file,
263 unsigned int cmd, unsigned long arg)
270 option_write (struct usb_serial_port *port,
271 const unsigned char *buf, int count)
273 struct option_port_private *portdata;
276 struct urb *this_urb = NULL; /* spurious */
279 portdata = usb_get_serial_port_data(port);
281 dbg("%s: write (%d chars)", __FUNCTION__, count);
285 for (i=0; left > 0 && i < N_OUT_URB; i++) {
287 if (todo > OUT_BUFLEN)
290 this_urb = portdata->out_urbs[i];
291 if (this_urb->status == -EINPROGRESS) {
292 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
294 if (time_before(jiffies, portdata->tx_start_time[i] + 10 * HZ))
296 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
297 usb_unlink_urb(this_urb);
300 if (this_urb->status != 0)
301 dbg("usb_write %p failed (err=%d)", this_urb, this_urb->status);
303 dbg("%s: endpoint %d buf %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), i);
306 memcpy (this_urb->transfer_buffer, buf, todo);
307 this_urb->transfer_buffer_length = todo;
309 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
310 this_urb->dev = port->serial->dev;
311 err = usb_submit_urb(this_urb, GFP_ATOMIC);
313 dbg("usb_submit_urb %p (write bulk) failed (%d, has %d)", this_urb, err, this_urb->status);
316 portdata->tx_start_time[i] = jiffies;
322 dbg("%s: wrote (did %d)", __FUNCTION__, count);
327 option_indat_callback (struct urb *urb, struct pt_regs *regs)
331 struct usb_serial_port *port;
332 struct tty_struct *tty;
333 unsigned char *data = urb->transfer_buffer;
335 dbg("%s: %p", __FUNCTION__, urb);
337 endpoint = usb_pipeendpoint(urb->pipe);
338 port = (struct usb_serial_port *) urb->context;
341 dbg("%s: nonzero status: %d on endpoint %02x.",
342 __FUNCTION__, urb->status, endpoint);
345 if (urb->actual_length) {
346 for (i = 0; i < urb->actual_length ; ++i) {
347 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
348 tty_flip_buffer_push(tty);
349 tty_insert_flip_char(tty, data[i], 0);
351 tty_flip_buffer_push(tty);
353 dbg("%s: empty read urb received", __FUNCTION__);
356 /* Resubmit urb so we continue receiving */
357 if (port->open_count && urb->status != -ESHUTDOWN) {
358 err = usb_submit_urb(urb, GFP_ATOMIC);
360 printk(KERN_ERR "%s: resubmit read urb failed. (%d)", __FUNCTION__, err);
367 option_outdat_callback (struct urb *urb, struct pt_regs *regs)
369 struct usb_serial_port *port;
371 dbg("%s", __FUNCTION__);
373 port = (struct usb_serial_port *) urb->context;
375 if (port->open_count)
376 schedule_work(&port->work);
380 option_instat_callback (struct urb *urb, struct pt_regs *regs)
383 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
384 struct option_port_private *portdata = usb_get_serial_port_data(port);
385 struct usb_serial *serial = port->serial;
387 dbg("%s", __FUNCTION__);
388 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
390 if (urb->status == 0) {
391 struct usb_ctrlrequest *req_pkt =
392 (struct usb_ctrlrequest *)urb->transfer_buffer;
395 dbg("%s: NULL req_pkt\n", __FUNCTION__);
398 if ((req_pkt->bRequestType == 0xA1) && (req_pkt->bRequest == 0x20)) {
400 unsigned char signals = *((unsigned char *)
401 urb->transfer_buffer + sizeof(struct usb_ctrlrequest));
403 dbg("%s: signal x%x", __FUNCTION__, signals);
405 old_dcd_state = portdata->dcd_state;
406 portdata->cts_state = 1;
407 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
408 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
409 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
411 if (port->tty && !C_CLOCAL(port->tty)
412 && old_dcd_state && !portdata->dcd_state) {
413 tty_hangup(port->tty);
416 dbg("%s: type %x req %x", __FUNCTION__, req_pkt->bRequestType,req_pkt->bRequest);
418 dbg("%s: error %d", __FUNCTION__, urb->status);
420 /* Resubmit urb so we continue receiving IRQ data */
421 if (urb->status != -ESHUTDOWN) {
422 urb->dev = serial->dev;
423 err = usb_submit_urb(urb, GFP_ATOMIC);
425 dbg("%s: resubmit intr urb failed. (%d)", __FUNCTION__, err);
431 option_write_room (struct usb_serial_port *port)
433 struct option_port_private *portdata;
436 struct urb *this_urb;
438 portdata = usb_get_serial_port_data(port);
440 for (i=0; i < N_OUT_URB; i++) {
441 this_urb = portdata->out_urbs[i];
442 if (this_urb && this_urb->status != -EINPROGRESS)
443 data_len += OUT_BUFLEN;
446 dbg("%s: %d", __FUNCTION__, data_len);
452 option_chars_in_buffer (struct usb_serial_port *port)
454 struct option_port_private *portdata;
457 struct urb *this_urb;
459 portdata = usb_get_serial_port_data(port);
461 for (i=0; i < N_OUT_URB; i++) {
462 this_urb = portdata->out_urbs[i];
463 if (this_urb && this_urb->status == -EINPROGRESS)
464 data_len += this_urb->transfer_buffer_length;
466 dbg("%s: %d", __FUNCTION__, data_len);
472 option_open (struct usb_serial_port *port, struct file *filp)
474 struct option_port_private *portdata;
475 struct usb_serial *serial = port->serial;
479 portdata = usb_get_serial_port_data(port);
481 dbg("%s", __FUNCTION__);
483 /* Set some sane defaults */
484 portdata->rts_state = 1;
485 portdata->dtr_state = 1;
487 /* Reset low level data toggle and start reading from endpoints */
488 for (i = 0; i < N_IN_URB; i++) {
489 urb = portdata->in_urbs[i];
492 if (urb->dev != serial->dev) {
493 dbg("%s: dev %p != %p", __FUNCTION__, urb->dev, serial->dev);
497 /* make sure endpoint data toggle is synchronized with the device */
499 usb_clear_halt(urb->dev, urb->pipe);
501 err = usb_submit_urb(urb, GFP_KERNEL);
503 dbg("%s: submit urb %d failed (%d) %d", __FUNCTION__, i, err,
504 urb->transfer_buffer_length);
508 /* Reset low level data toggle on out endpoints */
509 for (i = 0; i < N_OUT_URB; i++) {
510 urb = portdata->out_urbs[i];
513 urb->dev = serial->dev;
514 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
517 port->tty->low_latency = 1;
519 option_send_setup(port);
525 stop_urb (struct urb *urb)
527 if (urb && urb->status == -EINPROGRESS) {
528 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
534 option_close (struct usb_serial_port *port, struct file *filp)
537 struct usb_serial *serial = port->serial;
538 struct option_port_private *portdata;
540 dbg("%s", __FUNCTION__);
541 portdata = usb_get_serial_port_data(port);
543 portdata->rts_state = 0;
544 portdata->dtr_state = 0;
547 option_send_setup(port);
549 /* Stop reading/writing urbs */
550 for (i = 0; i < N_IN_URB; i++)
551 stop_urb(portdata->in_urbs[i]);
552 for (i = 0; i < N_OUT_URB; i++)
553 stop_urb(portdata->out_urbs[i]);
559 /* Helper functions used by option_setup_urbs */
561 option_setup_urb (struct usb_serial *serial, int endpoint,
562 int dir, void *ctx, char *buf, int len,
563 void (*callback)(struct urb *, struct pt_regs *regs))
568 return NULL; /* endpoint not needed */
570 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
572 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
576 /* Fill URB using supplied data. */
577 usb_fill_bulk_urb(urb, serial->dev,
578 usb_sndbulkpipe(serial->dev, endpoint) | dir,
579 buf, len, callback, ctx);
586 option_setup_urbs (struct usb_serial *serial)
589 struct usb_serial_port *port;
590 struct option_port_private *portdata;
592 dbg("%s", __FUNCTION__);
594 port = serial->port[0];
595 portdata = usb_get_serial_port_data(port);
597 /* Do indat endpoints first */
598 for (j = 0; j <= N_IN_URB; ++j) {
599 portdata->in_urbs[j] = option_setup_urb (serial,
600 port->bulk_in_endpointAddress, USB_DIR_IN, port,
601 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
604 /* outdat endpoints */
605 for (j = 0; j <= N_OUT_URB; ++j) {
606 portdata->out_urbs[j] = option_setup_urb (serial,
607 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
608 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
614 option_send_setup (struct usb_serial_port *port)
616 struct usb_serial *serial = port->serial;
617 struct option_port_private *portdata;
619 dbg("%s", __FUNCTION__);
621 portdata = usb_get_serial_port_data(port);
625 if (portdata->dtr_state)
627 if (portdata->rts_state)
630 return usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
631 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
639 option_startup (struct usb_serial *serial)
642 struct usb_serial_port *port;
643 struct option_port_private *portdata;
645 dbg("%s", __FUNCTION__);
647 /* Now setup per port private data */
648 for (i = 0; i < serial->num_ports; i++) {
649 port = serial->port[i];
650 portdata = kmalloc(sizeof(struct option_port_private), GFP_KERNEL);
652 dbg("%s: kmalloc for option_port_private (%d) failed!.", __FUNCTION__, i);
655 memset(portdata, 0, sizeof(struct option_port_private));
657 usb_set_serial_port_data(port, portdata);
659 if (! port->interrupt_in_urb)
661 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
663 dbg("%s: submit irq_in urb failed %d", __FUNCTION__, err);
666 option_setup_urbs(serial);
672 option_shutdown (struct usb_serial *serial)
675 struct usb_serial_port *port;
676 struct option_port_private *portdata;
678 dbg("%s", __FUNCTION__);
680 /* Stop reading/writing urbs */
681 for (i = 0; i < serial->num_ports; ++i) {
682 port = serial->port[i];
683 portdata = usb_get_serial_port_data(port);
684 for (j = 0; j < N_IN_URB; j++)
685 stop_urb(portdata->in_urbs[j]);
686 for (j = 0; j < N_OUT_URB; j++)
687 stop_urb(portdata->out_urbs[j]);
691 for (i = 0; i < serial->num_ports; ++i) {
692 port = serial->port[i];
693 portdata = usb_get_serial_port_data(port);
695 for (j = 0; j < N_IN_URB; j++) {
696 if (portdata->in_urbs[j]) {
697 usb_free_urb(portdata->in_urbs[j]);
698 portdata->in_urbs[j] = NULL;
701 for (j = 0; j < N_OUT_URB; j++) {
702 if (portdata->out_urbs[j]) {
703 usb_free_urb(portdata->out_urbs[j]);
704 portdata->out_urbs[j] = NULL;
709 /* Now free per port private data */
710 for (i = 0; i < serial->num_ports; i++) {
711 port = serial->port[i];
712 kfree(usb_get_serial_port_data(port));
716 MODULE_AUTHOR(DRIVER_AUTHOR);
717 MODULE_DESCRIPTION(DRIVER_DESC);
718 MODULE_VERSION(DRIVER_VERSION);
719 MODULE_LICENSE("GPL");
721 #ifdef CONFIG_USB_DEBUG
722 module_param(debug, bool, S_IRUGO | S_IWUSR);
723 MODULE_PARM_DESC(debug, "Debug messages");