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
79 static struct usb_device_id option_ids[] = {
80 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
81 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
82 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
83 { } /* Terminating entry */
86 MODULE_DEVICE_TABLE(usb, option_ids);
88 static struct usb_driver option_driver = {
91 .probe = usb_serial_probe,
92 .disconnect = usb_serial_disconnect,
93 .id_table = option_ids,
96 /* The card has three separate interfaces, wich the serial driver
97 * recognizes separately, thus num_port=1.
99 static struct usb_serial_device_type option_3port_device = {
100 .owner = THIS_MODULE,
101 .name = "Option 3G data card",
102 .short_name = "option",
103 .id_table = option_ids,
104 .num_interrupt_in = NUM_DONT_CARE,
105 .num_bulk_in = NUM_DONT_CARE,
106 .num_bulk_out = NUM_DONT_CARE,
107 .num_ports = 1, /* 3, but the card reports its ports separately */
109 .close = option_close,
110 .write = option_write,
111 .write_room = option_write_room,
112 .chars_in_buffer = option_chars_in_buffer,
113 .throttle = option_rx_throttle,
114 .unthrottle = option_rx_unthrottle,
115 .ioctl = option_ioctl,
116 .set_termios = option_set_termios,
117 .break_ctl = option_break_ctl,
118 .tiocmget = option_tiocmget,
119 .tiocmset = option_tiocmset,
120 .attach = option_startup,
121 .shutdown = option_shutdown,
122 .read_int_callback = option_instat_callback,
125 #ifdef CONFIG_USB_DEBUG
131 /* per port private data */
135 #define IN_BUFLEN 1024
136 #define OUT_BUFLEN 128
138 struct option_port_private {
139 /* Input endpoints and buffer for this port */
140 struct urb *in_urbs[N_IN_URB];
141 char in_buffer[N_IN_URB][IN_BUFLEN];
142 /* Output endpoints and buffer for this port */
143 struct urb *out_urbs[N_OUT_URB];
144 char out_buffer[N_OUT_URB][OUT_BUFLEN];
146 /* Settings for the port */
147 int rts_state; /* Handshaking pins (outputs) */
149 int cts_state; /* Handshaking pins (inputs) */
154 unsigned long tx_start_time[N_OUT_URB];
157 /* Functions used by new usb-serial code. */
158 static int __init option_init(void)
161 retval = usb_serial_register(&option_3port_device);
163 goto failed_3port_device_register;
164 retval = usb_register(&option_driver);
166 goto failed_driver_register;
168 info(DRIVER_DESC ": " DRIVER_VERSION);
172 failed_driver_register:
173 usb_serial_deregister (&option_3port_device);
174 failed_3port_device_register:
178 static void __exit option_exit(void)
180 usb_deregister (&option_driver);
181 usb_serial_deregister (&option_3port_device);
184 module_init(option_init);
185 module_exit(option_exit);
187 static void option_rx_throttle(struct usb_serial_port *port)
189 dbg("%s", __FUNCTION__);
192 static void option_rx_unthrottle(struct usb_serial_port *port)
194 dbg("%s", __FUNCTION__);
197 static void option_break_ctl(struct usb_serial_port *port, int break_state)
199 /* Unfortunately, I don't know how to send a break */
200 dbg("%s", __FUNCTION__);
203 static void option_set_termios(struct usb_serial_port *port,
204 struct termios *old_termios)
206 dbg("%s", __FUNCTION__);
208 option_send_setup(port);
211 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
214 struct option_port_private *portdata;
216 portdata = usb_get_serial_port_data(port);
218 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
219 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
220 ((portdata->cts_state) ? TIOCM_CTS : 0) |
221 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
222 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
223 ((portdata->ri_state) ? TIOCM_RNG : 0);
228 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
229 unsigned int set, unsigned int clear)
231 struct option_port_private *portdata;
233 portdata = usb_get_serial_port_data(port);
236 portdata->rts_state = 1;
238 portdata->dtr_state = 1;
240 if (clear & TIOCM_RTS)
241 portdata->rts_state = 0;
242 if (clear & TIOCM_DTR)
243 portdata->dtr_state = 0;
244 return option_send_setup(port);
247 static int option_ioctl(struct usb_serial_port *port, struct file *file,
248 unsigned int cmd, unsigned long arg)
254 static int option_write(struct usb_serial_port *port,
255 const unsigned char *buf, int count)
257 struct option_port_private *portdata;
260 struct urb *this_urb = NULL; /* spurious */
263 portdata = usb_get_serial_port_data(port);
265 dbg("%s: write (%d chars)", __FUNCTION__, count);
269 for (i=0; left > 0 && i < N_OUT_URB; i++) {
271 if (todo > OUT_BUFLEN)
274 this_urb = portdata->out_urbs[i];
275 if (this_urb->status == -EINPROGRESS) {
276 if (time_before(jiffies,
277 portdata->tx_start_time[i] + 10 * HZ))
279 usb_unlink_urb(this_urb);
282 if (this_urb->status != 0)
283 dbg("usb_write %p failed (err=%d)",
284 this_urb, this_urb->status);
286 dbg("%s: endpoint %d buf %d", __FUNCTION__,
287 usb_pipeendpoint(this_urb->pipe), i);
290 memcpy (this_urb->transfer_buffer, buf, todo);
291 this_urb->transfer_buffer_length = todo;
293 this_urb->dev = port->serial->dev;
294 err = usb_submit_urb(this_urb, GFP_ATOMIC);
296 dbg("usb_submit_urb %p (write bulk) failed "
297 "(%d, has %d)", this_urb,
298 err, this_urb->status);
301 portdata->tx_start_time[i] = jiffies;
307 dbg("%s: wrote (did %d)", __FUNCTION__, count);
311 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
315 struct usb_serial_port *port;
316 struct tty_struct *tty;
317 unsigned char *data = urb->transfer_buffer;
319 dbg("%s: %p", __FUNCTION__, urb);
321 endpoint = usb_pipeendpoint(urb->pipe);
322 port = (struct usb_serial_port *) urb->context;
325 dbg("%s: nonzero status: %d on endpoint %02x.",
326 __FUNCTION__, urb->status, endpoint);
329 if (urb->actual_length) {
330 for (i = 0; i < urb->actual_length ; ++i) {
331 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
332 tty_flip_buffer_push(tty);
333 tty_insert_flip_char(tty, data[i], 0);
335 tty_flip_buffer_push(tty);
337 dbg("%s: empty read urb received", __FUNCTION__);
340 /* Resubmit urb so we continue receiving */
341 if (port->open_count && urb->status != -ESHUTDOWN) {
342 err = usb_submit_urb(urb, GFP_ATOMIC);
344 printk(KERN_ERR "%s: resubmit read urb failed. "
345 "(%d)", __FUNCTION__, err);
351 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
353 struct usb_serial_port *port;
355 dbg("%s", __FUNCTION__);
357 port = (struct usb_serial_port *) urb->context;
359 if (port->open_count)
360 schedule_work(&port->work);
363 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
366 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
367 struct option_port_private *portdata = usb_get_serial_port_data(port);
368 struct usb_serial *serial = port->serial;
370 dbg("%s", __FUNCTION__);
371 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
373 if (urb->status == 0) {
374 struct usb_ctrlrequest *req_pkt =
375 (struct usb_ctrlrequest *)urb->transfer_buffer;
378 dbg("%s: NULL req_pkt\n", __FUNCTION__);
381 if ((req_pkt->bRequestType == 0xA1) &&
382 (req_pkt->bRequest == 0x20)) {
384 unsigned char signals = *((unsigned char *)
385 urb->transfer_buffer +
386 sizeof(struct usb_ctrlrequest));
388 dbg("%s: signal x%x", __FUNCTION__, signals);
390 old_dcd_state = portdata->dcd_state;
391 portdata->cts_state = 1;
392 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
393 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
394 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
396 if (port->tty && !C_CLOCAL(port->tty) &&
397 old_dcd_state && !portdata->dcd_state)
398 tty_hangup(port->tty);
400 dbg("%s: type %x req %x", __FUNCTION__,
401 req_pkt->bRequestType,req_pkt->bRequest);
404 dbg("%s: error %d", __FUNCTION__, urb->status);
406 /* Resubmit urb so we continue receiving IRQ data */
407 if (urb->status != -ESHUTDOWN) {
408 urb->dev = serial->dev;
409 err = usb_submit_urb(urb, GFP_ATOMIC);
411 dbg("%s: resubmit intr urb failed. (%d)",
416 static int option_write_room(struct usb_serial_port *port)
418 struct option_port_private *portdata;
421 struct urb *this_urb;
423 portdata = usb_get_serial_port_data(port);
425 for (i=0; i < N_OUT_URB; i++) {
426 this_urb = portdata->out_urbs[i];
427 if (this_urb && this_urb->status != -EINPROGRESS)
428 data_len += OUT_BUFLEN;
431 dbg("%s: %d", __FUNCTION__, data_len);
435 static int option_chars_in_buffer(struct usb_serial_port *port)
437 struct option_port_private *portdata;
440 struct urb *this_urb;
442 portdata = usb_get_serial_port_data(port);
444 for (i=0; i < N_OUT_URB; i++) {
445 this_urb = portdata->out_urbs[i];
446 if (this_urb && this_urb->status == -EINPROGRESS)
447 data_len += this_urb->transfer_buffer_length;
449 dbg("%s: %d", __FUNCTION__, data_len);
453 static int option_open(struct usb_serial_port *port, struct file *filp)
455 struct option_port_private *portdata;
456 struct usb_serial *serial = port->serial;
460 portdata = usb_get_serial_port_data(port);
462 dbg("%s", __FUNCTION__);
464 /* Set some sane defaults */
465 portdata->rts_state = 1;
466 portdata->dtr_state = 1;
468 /* Reset low level data toggle and start reading from endpoints */
469 for (i = 0; i < N_IN_URB; i++) {
470 urb = portdata->in_urbs[i];
473 if (urb->dev != serial->dev) {
474 dbg("%s: dev %p != %p", __FUNCTION__,
475 urb->dev, serial->dev);
480 * make sure endpoint data toggle is synchronized with the
483 usb_clear_halt(urb->dev, urb->pipe);
485 err = usb_submit_urb(urb, GFP_KERNEL);
487 dbg("%s: submit urb %d failed (%d) %d",
488 __FUNCTION__, i, err,
489 urb->transfer_buffer_length);
493 /* Reset low level data toggle on out endpoints */
494 for (i = 0; i < N_OUT_URB; i++) {
495 urb = portdata->out_urbs[i];
498 urb->dev = serial->dev;
499 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
500 usb_pipeout(urb->pipe), 0); */
503 port->tty->low_latency = 1;
505 option_send_setup(port);
510 static inline void stop_urb(struct urb *urb)
512 if (urb && urb->status == -EINPROGRESS)
516 static void option_close(struct usb_serial_port *port, struct file *filp)
519 struct usb_serial *serial = port->serial;
520 struct option_port_private *portdata;
522 dbg("%s", __FUNCTION__);
523 portdata = usb_get_serial_port_data(port);
525 portdata->rts_state = 0;
526 portdata->dtr_state = 0;
529 option_send_setup(port);
531 /* Stop reading/writing urbs */
532 for (i = 0; i < N_IN_URB; i++)
533 stop_urb(portdata->in_urbs[i]);
534 for (i = 0; i < N_OUT_URB; i++)
535 stop_urb(portdata->out_urbs[i]);
540 /* Helper functions used by option_setup_urbs */
541 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
542 int dir, void *ctx, char *buf, int len,
543 void (*callback)(struct urb *, struct pt_regs *regs))
548 return NULL; /* endpoint not needed */
550 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
552 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
556 /* Fill URB using supplied data. */
557 usb_fill_bulk_urb(urb, serial->dev,
558 usb_sndbulkpipe(serial->dev, endpoint) | dir,
559 buf, len, callback, ctx);
565 static void option_setup_urbs(struct usb_serial *serial)
568 struct usb_serial_port *port;
569 struct option_port_private *portdata;
571 dbg("%s", __FUNCTION__);
573 port = serial->port[0];
574 portdata = usb_get_serial_port_data(port);
576 /* Do indat endpoints first */
577 for (j = 0; j <= N_IN_URB; ++j) {
578 portdata->in_urbs[j] = option_setup_urb (serial,
579 port->bulk_in_endpointAddress, USB_DIR_IN, port,
580 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
583 /* outdat endpoints */
584 for (j = 0; j <= N_OUT_URB; ++j) {
585 portdata->out_urbs[j] = option_setup_urb (serial,
586 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
587 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
591 static int option_send_setup(struct usb_serial_port *port)
593 struct usb_serial *serial = port->serial;
594 struct option_port_private *portdata;
596 dbg("%s", __FUNCTION__);
598 portdata = usb_get_serial_port_data(port);
602 if (portdata->dtr_state)
604 if (portdata->rts_state)
607 return usb_control_msg(serial->dev,
608 usb_rcvctrlpipe(serial->dev, 0),
609 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
615 static int option_startup(struct usb_serial *serial)
618 struct usb_serial_port *port;
619 struct option_port_private *portdata;
621 dbg("%s", __FUNCTION__);
623 /* Now setup per port private data */
624 for (i = 0; i < serial->num_ports; i++) {
625 port = serial->port[i];
626 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
628 dbg("%s: kmalloc for option_port_private (%d) failed!.",
632 memset(portdata, 0, sizeof(struct option_port_private));
634 usb_set_serial_port_data(port, portdata);
636 if (! port->interrupt_in_urb)
638 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
640 dbg("%s: submit irq_in urb failed %d",
644 option_setup_urbs(serial);
649 static void option_shutdown(struct usb_serial *serial)
652 struct usb_serial_port *port;
653 struct option_port_private *portdata;
655 dbg("%s", __FUNCTION__);
657 /* Stop reading/writing urbs */
658 for (i = 0; i < serial->num_ports; ++i) {
659 port = serial->port[i];
660 portdata = usb_get_serial_port_data(port);
661 for (j = 0; j < N_IN_URB; j++)
662 stop_urb(portdata->in_urbs[j]);
663 for (j = 0; j < N_OUT_URB; j++)
664 stop_urb(portdata->out_urbs[j]);
668 for (i = 0; i < serial->num_ports; ++i) {
669 port = serial->port[i];
670 portdata = usb_get_serial_port_data(port);
672 for (j = 0; j < N_IN_URB; j++) {
673 if (portdata->in_urbs[j]) {
674 usb_free_urb(portdata->in_urbs[j]);
675 portdata->in_urbs[j] = NULL;
678 for (j = 0; j < N_OUT_URB; j++) {
679 if (portdata->out_urbs[j]) {
680 usb_free_urb(portdata->out_urbs[j]);
681 portdata->out_urbs[j] = NULL;
686 /* Now free per port private data */
687 for (i = 0; i < serial->num_ports; i++) {
688 port = serial->port[i];
689 kfree(usb_get_serial_port_data(port));
693 MODULE_AUTHOR(DRIVER_AUTHOR);
694 MODULE_DESCRIPTION(DRIVER_DESC);
695 MODULE_VERSION(DRIVER_VERSION);
696 MODULE_LICENSE("GPL");
698 #ifdef CONFIG_USB_DEBUG
699 module_param(debug, bool, S_IRUGO | S_IWUSR);
700 MODULE_PARM_DESC(debug, "Debug messages");