MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / serial / option.c
blob35a0f6b0924aa5dfb07e19293e320490be79e375
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
28 2005-09-10 v0.4.3 added HUAWEI E600 card and Audiovox AirCard
29 2005-09-20 v0.4.4 increased recv buffer size: the card sometimes
30 wants to send >2000 bytes.
32 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
36 #define DRIVER_VERSION "v0.4"
37 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
38 #define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver"
40 #include <linux/config.h>
41 #include <linux/kernel.h>
42 #include <linux/jiffies.h>
43 #include <linux/errno.h>
44 #include <linux/tty.h>
45 #include <linux/tty_flip.h>
46 #include <linux/module.h>
47 #include <linux/usb.h>
48 #include "usb-serial.h"
50 /* Function prototypes */
51 static int option_open(struct usb_serial_port *port, struct file *filp);
52 static void option_close(struct usb_serial_port *port, struct file *filp);
53 static int option_startup(struct usb_serial *serial);
54 static void option_shutdown(struct usb_serial *serial);
55 static void option_rx_throttle(struct usb_serial_port *port);
56 static void option_rx_unthrottle(struct usb_serial_port *port);
57 static int option_write_room(struct usb_serial_port *port);
59 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
61 static int option_write(struct usb_serial_port *port,int from_user,
62 const unsigned char *buf, int count);
64 static int option_chars_in_buffer(struct usb_serial_port *port);
65 static int option_ioctl(struct usb_serial_port *port, struct file *file,
66 unsigned int cmd, unsigned long arg);
67 static void option_set_termios(struct usb_serial_port *port,
68 struct termios *old);
69 static void option_break_ctl(struct usb_serial_port *port, int break_state);
70 static int option_tiocmget(struct usb_serial_port *port, struct file *file);
71 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
72 unsigned int set, unsigned int clear);
73 static int option_send_setup(struct usb_serial_port *port);
75 /* Vendor and product IDs */
76 #define OPTION_VENDOR_ID 0x0AF0
77 #define HUAWEI_VENDOR_ID 0x12D1
78 #define AUDIOVOX_VENDOR_ID 0x0F3D
80 #define OPTION_PRODUCT_OLD 0x5000
81 #define OPTION_PRODUCT_FUSION 0x6000
82 #define OPTION_PRODUCT_FUSION2 0x6300
83 #define HUAWEI_PRODUCT_E600 0x1001
84 #define AUDIOVOX_PRODUCT_AIRCARD 0x0112
86 static struct usb_device_id option_ids[] = {
87 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
88 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
89 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
90 { USB_DEVICE(OPTION_VENDOR_ID, 0x6701) },
91 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
92 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
93 { } /* Terminating entry */
96 MODULE_DEVICE_TABLE(usb, option_ids);
98 static struct usb_driver option_driver = {
99 .owner = THIS_MODULE,
100 .name = "option",
101 .probe = usb_serial_probe,
102 .disconnect = usb_serial_disconnect,
103 .id_table = option_ids,
106 /* The card has three separate interfaces, wich the serial driver
107 * recognizes separately, thus num_port=1.
109 static struct usb_serial_device_type option_3port_device = {
110 .owner = THIS_MODULE,
111 .name = "Option 3G data card",
112 .short_name = "option",
113 .id_table = option_ids,
114 .num_interrupt_in = NUM_DONT_CARE,
115 .num_bulk_in = NUM_DONT_CARE,
116 .num_bulk_out = NUM_DONT_CARE,
117 .num_ports = 1, /* 3, but the card reports its ports separately */
118 .open = option_open,
119 .close = option_close,
120 .write = option_write,
121 .write_room = option_write_room,
122 .chars_in_buffer = option_chars_in_buffer,
123 .throttle = option_rx_throttle,
124 .unthrottle = option_rx_unthrottle,
125 .ioctl = option_ioctl,
126 .set_termios = option_set_termios,
127 .break_ctl = option_break_ctl,
128 .tiocmget = option_tiocmget,
129 .tiocmset = option_tiocmset,
130 .attach = option_startup,
131 .shutdown = option_shutdown,
132 .read_int_callback = option_instat_callback,
135 #ifdef CONFIG_USB_DEBUG
136 static int debug;
137 #else
138 #define debug 0
139 #endif
141 /* per port private data */
143 #define N_IN_URB 4
144 #define N_OUT_URB 1
145 #define IN_BUFLEN 4096
146 #define OUT_BUFLEN 128
148 struct option_port_private {
149 /* Input endpoints and buffer for this port */
150 struct urb *in_urbs[N_IN_URB];
151 char in_buffer[N_IN_URB][IN_BUFLEN];
152 /* Output endpoints and buffer for this port */
153 struct urb *out_urbs[N_OUT_URB];
154 char out_buffer[N_OUT_URB][OUT_BUFLEN];
156 /* Settings for the port */
157 int rts_state; /* Handshaking pins (outputs) */
158 int dtr_state;
159 int cts_state; /* Handshaking pins (inputs) */
160 int dsr_state;
161 int dcd_state;
162 int ri_state;
164 unsigned long tx_start_time[N_OUT_URB];
167 /* Functions used by new usb-serial code. */
168 static int __init option_init(void)
170 int retval;
171 retval = usb_serial_register(&option_3port_device);
172 if (retval)
173 goto failed_3port_device_register;
174 retval = usb_register(&option_driver);
175 if (retval)
176 goto failed_driver_register;
178 info(DRIVER_DESC ": " DRIVER_VERSION);
180 return 0;
182 failed_driver_register:
183 usb_serial_deregister (&option_3port_device);
184 failed_3port_device_register:
185 return retval;
188 static void __exit option_exit(void)
190 usb_deregister (&option_driver);
191 usb_serial_deregister (&option_3port_device);
194 module_init(option_init);
195 module_exit(option_exit);
197 static void option_rx_throttle(struct usb_serial_port *port)
199 dbg("%s", __FUNCTION__);
202 static void option_rx_unthrottle(struct usb_serial_port *port)
204 dbg("%s", __FUNCTION__);
207 static void 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__);
213 static void option_set_termios(struct usb_serial_port *port,
214 struct termios *old_termios)
216 dbg("%s", __FUNCTION__);
218 option_send_setup(port);
221 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
223 unsigned int value;
224 struct option_port_private *portdata;
226 portdata = usb_get_serial_port_data(port);
228 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
229 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
230 ((portdata->cts_state) ? TIOCM_CTS : 0) |
231 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
232 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
233 ((portdata->ri_state) ? TIOCM_RNG : 0);
235 return value;
238 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
239 unsigned int set, unsigned int clear)
241 struct option_port_private *portdata;
243 portdata = usb_get_serial_port_data(port);
245 if (set & TIOCM_RTS)
246 portdata->rts_state = 1;
247 if (set & TIOCM_DTR)
248 portdata->dtr_state = 1;
250 if (clear & TIOCM_RTS)
251 portdata->rts_state = 0;
252 if (clear & TIOCM_DTR)
253 portdata->dtr_state = 0;
254 return option_send_setup(port);
257 static int option_ioctl(struct usb_serial_port *port, struct file *file,
258 unsigned int cmd, unsigned long arg)
260 return -ENOIOCTLCMD;
263 /* Write */
264 static int option_write(struct usb_serial_port *port,int from_user,
265 const unsigned char *buf, int count)
267 struct option_port_private *portdata;
268 int i;
269 unsigned int left, todo;
270 struct urb *this_urb = NULL; /* spurious */
271 int err;
272 portdata = usb_get_serial_port_data(port);
274 dbg("%s: write (%d chars)", __FUNCTION__, count);
276 left = count;
277 for (i=0; left > 0 && i < N_OUT_URB; i++) {
278 todo = left;
279 if (todo > OUT_BUFLEN)
280 todo = OUT_BUFLEN;
282 this_urb = portdata->out_urbs[i];
283 if (this_urb->status == -EINPROGRESS) {
284 if (time_before(jiffies,
285 portdata->tx_start_time[i] + 10 * HZ))
286 continue;
287 usb_unlink_urb(this_urb);
288 continue;
290 if (this_urb->status != 0)
291 dbg("usb_write %p failed (err=%d)",
292 this_urb, this_urb->status);
294 dbg("%s: endpoint %d buf %d", __FUNCTION__,
295 usb_pipeendpoint(this_urb->pipe), i);
297 /* send the data */
298 memcpy (this_urb->transfer_buffer, buf, todo);
299 this_urb->transfer_buffer_length = todo;
301 this_urb->dev = port->serial->dev;
302 err = usb_submit_urb(this_urb, GFP_ATOMIC);
303 if (err) {
304 dbg("usb_submit_urb %p (write bulk) failed "
305 "(%d, has %d)", this_urb,
306 err, this_urb->status);
307 continue;
309 portdata->tx_start_time[i] = jiffies;
310 buf += todo;
311 left -= todo;
314 count -= left;
315 dbg("%s: wrote (did %d)", __FUNCTION__, count);
316 return count;
319 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
321 int i, err;
322 int endpoint;
323 struct usb_serial_port *port;
324 struct tty_struct *tty;
325 unsigned char *data = urb->transfer_buffer;
327 dbg("%s: %p", __FUNCTION__, urb);
329 endpoint = usb_pipeendpoint(urb->pipe);
330 port = (struct usb_serial_port *) urb->context;
332 if (urb->status) {
333 dbg("%s: nonzero status: %d on endpoint %02x.",
334 __FUNCTION__, urb->status, endpoint);
335 } else {
336 tty = port->tty;
337 if (urb->actual_length) {
338 for (i = 0; i < urb->actual_length ; ++i) {
339 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
340 tty_flip_buffer_push(tty);
341 tty_insert_flip_char(tty, data[i], 0);
343 tty_flip_buffer_push(tty);
344 } else {
345 dbg("%s: empty read urb received", __FUNCTION__);
348 /* Resubmit urb so we continue receiving */
349 if (port->open_count && urb->status != -ESHUTDOWN) {
350 err = usb_submit_urb(urb, GFP_ATOMIC);
351 if (err)
352 printk(KERN_ERR "%s: resubmit read urb failed. "
353 "(%d)", __FUNCTION__, err);
356 return;
359 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
361 struct usb_serial_port *port;
363 dbg("%s", __FUNCTION__);
365 port = (struct usb_serial_port *) urb->context;
367 if (port->open_count)
368 schedule_work(&port->work);
371 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
373 int err;
374 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
375 struct option_port_private *portdata = usb_get_serial_port_data(port);
376 struct usb_serial *serial = port->serial;
378 dbg("%s", __FUNCTION__);
379 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
381 if (urb->status == 0) {
382 struct usb_ctrlrequest *req_pkt =
383 (struct usb_ctrlrequest *)urb->transfer_buffer;
385 if (!req_pkt) {
386 dbg("%s: NULL req_pkt\n", __FUNCTION__);
387 return;
389 if ((req_pkt->bRequestType == 0xA1) &&
390 (req_pkt->bRequest == 0x20)) {
391 int old_dcd_state;
392 unsigned char signals = *((unsigned char *)
393 urb->transfer_buffer +
394 sizeof(struct usb_ctrlrequest));
396 dbg("%s: signal x%x", __FUNCTION__, signals);
398 old_dcd_state = portdata->dcd_state;
399 portdata->cts_state = 1;
400 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
401 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
402 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
404 if (port->tty && !C_CLOCAL(port->tty) &&
405 old_dcd_state && !portdata->dcd_state)
406 tty_hangup(port->tty);
407 } else {
408 dbg("%s: type %x req %x", __FUNCTION__,
409 req_pkt->bRequestType,req_pkt->bRequest);
411 } else
412 dbg("%s: error %d", __FUNCTION__, urb->status);
414 /* Resubmit urb so we continue receiving IRQ data */
415 if (urb->status != -ESHUTDOWN) {
416 urb->dev = serial->dev;
417 err = usb_submit_urb(urb, GFP_ATOMIC);
418 if (err)
419 dbg("%s: resubmit intr urb failed. (%d)",
420 __FUNCTION__, err);
424 static int option_write_room(struct usb_serial_port *port)
426 struct option_port_private *portdata;
427 int i;
428 int data_len = 0;
429 struct urb *this_urb;
431 portdata = usb_get_serial_port_data(port);
433 for (i=0; i < N_OUT_URB; i++) {
434 this_urb = portdata->out_urbs[i];
435 if (this_urb && this_urb->status != -EINPROGRESS)
436 data_len += OUT_BUFLEN;
439 dbg("%s: %d", __FUNCTION__, data_len);
440 return data_len;
443 static int option_chars_in_buffer(struct usb_serial_port *port)
445 struct option_port_private *portdata;
446 int i;
447 int data_len = 0;
448 struct urb *this_urb;
450 portdata = usb_get_serial_port_data(port);
452 for (i=0; i < N_OUT_URB; i++) {
453 this_urb = portdata->out_urbs[i];
454 if (this_urb && this_urb->status == -EINPROGRESS)
455 data_len += this_urb->transfer_buffer_length;
457 dbg("%s: %d", __FUNCTION__, data_len);
458 return data_len;
461 static int option_open(struct usb_serial_port *port, struct file *filp)
463 struct option_port_private *portdata;
464 struct usb_serial *serial = port->serial;
465 int i, err;
466 struct urb *urb;
468 portdata = usb_get_serial_port_data(port);
470 dbg("%s", __FUNCTION__);
472 /* Set some sane defaults */
473 portdata->rts_state = 1;
474 portdata->dtr_state = 1;
476 /* Reset low level data toggle and start reading from endpoints */
477 for (i = 0; i < N_IN_URB; i++) {
478 urb = portdata->in_urbs[i];
479 if (! urb)
480 continue;
481 if (urb->dev != serial->dev) {
482 dbg("%s: dev %p != %p", __FUNCTION__,
483 urb->dev, serial->dev);
484 continue;
488 * make sure endpoint data toggle is synchronized with the
489 * device
491 usb_clear_halt(urb->dev, urb->pipe);
493 err = usb_submit_urb(urb, GFP_KERNEL);
494 if (err) {
495 dbg("%s: submit urb %d failed (%d) %d",
496 __FUNCTION__, i, err,
497 urb->transfer_buffer_length);
501 /* Reset low level data toggle on out endpoints */
502 for (i = 0; i < N_OUT_URB; i++) {
503 urb = portdata->out_urbs[i];
504 if (! urb)
505 continue;
506 urb->dev = serial->dev;
507 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
508 usb_pipeout(urb->pipe), 0); */
511 port->tty->low_latency = 1;
513 option_send_setup(port);
515 return (0);
518 static inline void stop_urb(struct urb *urb)
520 if (urb && urb->status == -EINPROGRESS)
521 usb_kill_urb(urb);
524 static void option_close(struct usb_serial_port *port, struct file *filp)
526 int i;
527 struct usb_serial *serial = port->serial;
528 struct option_port_private *portdata;
530 dbg("%s", __FUNCTION__);
531 portdata = usb_get_serial_port_data(port);
533 portdata->rts_state = 0;
534 portdata->dtr_state = 0;
536 if (serial->dev) {
537 option_send_setup(port);
539 /* Stop reading/writing urbs */
540 for (i = 0; i < N_IN_URB; i++)
541 stop_urb(portdata->in_urbs[i]);
542 for (i = 0; i < N_OUT_URB; i++)
543 stop_urb(portdata->out_urbs[i]);
545 port->tty = NULL;
548 /* Helper functions used by option_setup_urbs */
549 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
550 int dir, void *ctx, char *buf, int len,
551 void (*callback)(struct urb *, struct pt_regs *regs))
553 struct urb *urb;
555 if (endpoint == -1)
556 return NULL; /* endpoint not needed */
558 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
559 if (urb == NULL) {
560 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
561 return NULL;
564 /* Fill URB using supplied data. */
565 usb_fill_bulk_urb(urb, serial->dev,
566 usb_sndbulkpipe(serial->dev, endpoint) | dir,
567 buf, len, callback, ctx);
569 return urb;
572 /* Setup urbs */
573 static void option_setup_urbs(struct usb_serial *serial)
575 int j;
576 struct usb_serial_port *port;
577 struct option_port_private *portdata;
579 dbg("%s", __FUNCTION__);
581 port = serial->port[0];
582 portdata = usb_get_serial_port_data(port);
584 /* Do indat endpoints first */
585 for (j = 0; j <= N_IN_URB; ++j) {
586 portdata->in_urbs[j] = option_setup_urb (serial,
587 port->bulk_in_endpointAddress, USB_DIR_IN, port,
588 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
591 /* outdat endpoints */
592 for (j = 0; j <= N_OUT_URB; ++j) {
593 portdata->out_urbs[j] = option_setup_urb (serial,
594 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
595 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
599 static int option_send_setup(struct usb_serial_port *port)
601 struct usb_serial *serial = port->serial;
602 struct option_port_private *portdata;
604 dbg("%s", __FUNCTION__);
606 portdata = usb_get_serial_port_data(port);
608 if (port->tty) {
609 int val = 0;
610 if (portdata->dtr_state)
611 val |= 0x01;
612 if (portdata->rts_state)
613 val |= 0x02;
615 return usb_control_msg(serial->dev,
616 usb_rcvctrlpipe(serial->dev, 0),
617 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
620 return 0;
623 static int option_startup(struct usb_serial *serial)
625 int i, err;
626 struct usb_serial_port *port;
627 struct option_port_private *portdata;
629 dbg("%s", __FUNCTION__);
631 /* Now setup per port private data */
632 for (i = 0; i < serial->num_ports; i++) {
633 port = serial->port[i];
634 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
635 if (!portdata) {
636 dbg("%s: kmalloc for option_port_private (%d) failed!.",
637 __FUNCTION__, i);
638 return (1);
640 memset(portdata, 0, sizeof(struct option_port_private));
642 usb_set_serial_port_data(port, portdata);
644 if (! port->interrupt_in_urb)
645 continue;
646 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
647 if (err)
648 dbg("%s: submit irq_in urb failed %d",
649 __FUNCTION__, err);
652 option_setup_urbs(serial);
654 return (0);
657 static void option_shutdown(struct usb_serial *serial)
659 int i, j;
660 struct usb_serial_port *port;
661 struct option_port_private *portdata;
663 dbg("%s", __FUNCTION__);
665 /* Stop reading/writing urbs */
666 for (i = 0; i < serial->num_ports; ++i) {
667 port = serial->port[i];
668 portdata = usb_get_serial_port_data(port);
669 for (j = 0; j < N_IN_URB; j++)
670 stop_urb(portdata->in_urbs[j]);
671 for (j = 0; j < N_OUT_URB; j++)
672 stop_urb(portdata->out_urbs[j]);
675 /* Now free them */
676 for (i = 0; i < serial->num_ports; ++i) {
677 port = serial->port[i];
678 portdata = usb_get_serial_port_data(port);
680 for (j = 0; j < N_IN_URB; j++) {
681 if (portdata->in_urbs[j]) {
682 usb_free_urb(portdata->in_urbs[j]);
683 portdata->in_urbs[j] = NULL;
686 for (j = 0; j < N_OUT_URB; j++) {
687 if (portdata->out_urbs[j]) {
688 usb_free_urb(portdata->out_urbs[j]);
689 portdata->out_urbs[j] = NULL;
694 /* Now free per port private data */
695 for (i = 0; i < serial->num_ports; i++) {
696 port = serial->port[i];
697 kfree(usb_get_serial_port_data(port));
701 MODULE_AUTHOR(DRIVER_AUTHOR);
702 MODULE_DESCRIPTION(DRIVER_DESC);
703 MODULE_VERSION(DRIVER_VERSION);
704 MODULE_LICENSE("GPL");
706 #ifdef CONFIG_USB_DEBUG
707 module_param(debug, bool, S_IRUGO | S_IWUSR);
708 MODULE_PARM_DESC(debug, "Debug messages");
709 #endif