Linux v2.6.13
[linux-2.6/next.git] / drivers / usb / serial / option.c
blobe9256408757ffdf3a7f5cc3a832bf8b503c808c9
1 /*
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>
12 History:
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()
19 turned on low_latency
20 simplified the code somewhat
21 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load
22 removed some dead code
23 added sponsor notice
24 coding style clean-up
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,
65 struct termios *old);
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 = {
90 .owner = THIS_MODULE,
91 .name = "option",
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 */
109 .open = option_open,
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
127 static int debug;
128 #else
129 #define debug 0
130 #endif
133 /* per port private data */
135 #define N_IN_URB 4
136 #define N_OUT_URB 1
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) */
150 int dtr_state;
151 int cts_state; /* Handshaking pins (inputs) */
152 int dsr_state;
153 int dcd_state;
154 int ri_state;
156 unsigned long tx_start_time[N_OUT_URB];
160 /* Functions used by new usb-serial code. */
161 static int __init
162 option_init (void)
164 int retval;
165 retval = usb_serial_register(&option_3port_device);
166 if (retval)
167 goto failed_3port_device_register;
168 retval = usb_register(&option_driver);
169 if (retval)
170 goto failed_driver_register;
172 info(DRIVER_DESC ": " DRIVER_VERSION);
174 return 0;
176 failed_driver_register:
177 usb_serial_deregister (&option_3port_device);
178 failed_3port_device_register:
179 return retval;
182 static void __exit
183 option_exit (void)
185 usb_deregister (&option_driver);
186 usb_serial_deregister (&option_3port_device);
189 module_init(option_init);
190 module_exit(option_exit);
192 static void
193 option_rx_throttle (struct usb_serial_port *port)
195 dbg("%s", __FUNCTION__);
199 static void
200 option_rx_unthrottle (struct usb_serial_port *port)
202 dbg("%s", __FUNCTION__);
206 static void
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__);
214 static void
215 option_set_termios (struct usb_serial_port *port,
216 struct termios *old_termios)
218 dbg("%s", __FUNCTION__);
220 option_send_setup(port);
223 static int
224 option_tiocmget (struct usb_serial_port *port, struct file *file)
226 unsigned int value;
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);
238 return value;
241 static int
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);
249 if (set & TIOCM_RTS)
250 portdata->rts_state = 1;
251 if (set & TIOCM_DTR)
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);
261 static int
262 option_ioctl (struct usb_serial_port *port, struct file *file,
263 unsigned int cmd, unsigned long arg)
265 return -ENOIOCTLCMD;
268 /* Write */
269 static int
270 option_write (struct usb_serial_port *port,
271 const unsigned char *buf, int count)
273 struct option_port_private *portdata;
274 int i;
275 int left, todo;
276 struct urb *this_urb = NULL; /* spurious */
277 int err;
279 portdata = usb_get_serial_port_data(port);
281 dbg("%s: write (%d chars)", __FUNCTION__, count);
283 i = 0;
284 left = count;
285 for (i=0; left > 0 && i < N_OUT_URB; i++) {
286 todo = left;
287 if (todo > OUT_BUFLEN)
288 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)
293 continue;
294 if (time_before(jiffies, portdata->tx_start_time[i] + 10 * HZ))
295 continue;
296 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
297 usb_unlink_urb(this_urb);
298 continue;
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);
305 /* send the data */
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);
312 if (err) {
313 dbg("usb_submit_urb %p (write bulk) failed (%d, has %d)", this_urb, err, this_urb->status);
314 continue;
316 portdata->tx_start_time[i] = jiffies;
317 buf += todo;
318 left -= todo;
321 count -= left;
322 dbg("%s: wrote (did %d)", __FUNCTION__, count);
323 return count;
326 static void
327 option_indat_callback (struct urb *urb, struct pt_regs *regs)
329 int i, err;
330 int endpoint;
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;
340 if (urb->status) {
341 dbg("%s: nonzero status: %d on endpoint %02x.",
342 __FUNCTION__, urb->status, endpoint);
343 } else {
344 tty = port->tty;
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);
352 } else {
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);
359 if (err)
360 printk(KERN_ERR "%s: resubmit read urb failed. (%d)", __FUNCTION__, err);
363 return;
366 static void
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);
379 static void
380 option_instat_callback (struct urb *urb, struct pt_regs *regs)
382 int err;
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;
394 if (!req_pkt) {
395 dbg("%s: NULL req_pkt\n", __FUNCTION__);
396 return;
398 if ((req_pkt->bRequestType == 0xA1) && (req_pkt->bRequest == 0x20)) {
399 int old_dcd_state;
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);
415 } else
416 dbg("%s: type %x req %x", __FUNCTION__, req_pkt->bRequestType,req_pkt->bRequest);
417 } else
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);
424 if (err)
425 dbg("%s: resubmit intr urb failed. (%d)", __FUNCTION__, err);
430 static int
431 option_write_room (struct usb_serial_port *port)
433 struct option_port_private *portdata;
434 int i;
435 int data_len = 0;
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);
447 return data_len;
451 static int
452 option_chars_in_buffer (struct usb_serial_port *port)
454 struct option_port_private *portdata;
455 int i;
456 int data_len = 0;
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);
467 return data_len;
471 static int
472 option_open (struct usb_serial_port *port, struct file *filp)
474 struct option_port_private *portdata;
475 struct usb_serial *serial = port->serial;
476 int i, err;
477 struct urb *urb;
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];
490 if (! urb)
491 continue;
492 if (urb->dev != serial->dev) {
493 dbg("%s: dev %p != %p", __FUNCTION__, urb->dev, serial->dev);
494 continue;
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);
502 if (err) {
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];
511 if (! urb)
512 continue;
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);
521 return (0);
524 static inline void
525 stop_urb (struct urb *urb)
527 if (urb && urb->status == -EINPROGRESS) {
528 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
529 usb_kill_urb(urb);
533 static void
534 option_close (struct usb_serial_port *port, struct file *filp)
536 int i;
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;
546 if (serial->dev) {
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]);
555 port->tty = NULL;
559 /* Helper functions used by option_setup_urbs */
560 static struct urb *
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))
565 struct urb *urb;
567 if (endpoint == -1)
568 return NULL; /* endpoint not needed */
570 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
571 if (urb == NULL) {
572 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
573 return NULL;
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);
581 return urb;
584 /* Setup urbs */
585 static void
586 option_setup_urbs (struct usb_serial *serial)
588 int j;
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);
613 static int
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);
623 if (port->tty) {
624 int val = 0;
625 if (portdata->dtr_state)
626 val |= 0x01;
627 if (portdata->rts_state)
628 val |= 0x02;
630 return usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
631 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
634 return 0;
638 static int
639 option_startup (struct usb_serial *serial)
641 int i, err;
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);
651 if (!portdata) {
652 dbg("%s: kmalloc for option_port_private (%d) failed!.", __FUNCTION__, i);
653 return (1);
655 memset(portdata, 0, sizeof(struct option_port_private));
657 usb_set_serial_port_data(port, portdata);
659 if (! port->interrupt_in_urb)
660 continue;
661 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
662 if (err)
663 dbg("%s: submit irq_in urb failed %d", __FUNCTION__, err);
666 option_setup_urbs(serial);
668 return (0);
671 static void
672 option_shutdown (struct usb_serial *serial)
674 int i, j;
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]);
690 /* Now free them */
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");
724 #endif