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.2.6.15
blobb73f87478656ea43604832df644ebb092f184f93
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,
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(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
91         { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
92         { } /* Terminating entry */
95 MODULE_DEVICE_TABLE(usb, option_ids);
97 static struct usb_driver option_driver = {
98         .owner      = THIS_MODULE,
99         .name       = "option",
100         .probe      = usb_serial_probe,
101         .disconnect = usb_serial_disconnect,
102         .id_table   = option_ids,
105 /* The card has three separate interfaces, wich the serial driver
106  * recognizes separately, thus num_port=1.
107  */
108 static struct usb_serial_device_type option_3port_device = {
109         //.driver = {
110                 .owner =        THIS_MODULE,
111         //      .name =         "option",
112         //},
113         .name              = "Option 3G data card",
114         .short_name        = "option",
115         //.description       = "Option 3G data card",
116         .id_table          = option_ids,
117         .num_interrupt_in  = NUM_DONT_CARE,
118         .num_bulk_in       = NUM_DONT_CARE,
119         .num_bulk_out      = NUM_DONT_CARE,
120         .num_ports         = 1, /* 3, but the card reports its ports separately */
121         .open              = option_open,
122         .close             = option_close,
123         .write             = option_write,
124         .write_room        = option_write_room,
125         .chars_in_buffer   = option_chars_in_buffer,
126         .throttle          = option_rx_throttle,
127         .unthrottle        = option_rx_unthrottle,
128         .ioctl             = option_ioctl,
129         .set_termios       = option_set_termios,
130         .break_ctl         = option_break_ctl,
131         .tiocmget          = option_tiocmget,
132         .tiocmset          = option_tiocmset,
133         .attach            = option_startup,
134         .shutdown          = option_shutdown,
135         .read_int_callback = option_instat_callback,
138 #ifdef CONFIG_USB_DEBUG
139 static int debug;
140 #else
141 #define debug 0
142 #endif
144 /* per port private data */
146 #define N_IN_URB 4
147 #define N_OUT_URB 1
148 #define IN_BUFLEN 4096
149 #define OUT_BUFLEN 128
151 struct option_port_private {
152         /* Input endpoints and buffer for this port */
153         struct urb *in_urbs[N_IN_URB];
154         char in_buffer[N_IN_URB][IN_BUFLEN];
155         /* Output endpoints and buffer for this port */
156         struct urb *out_urbs[N_OUT_URB];
157         char out_buffer[N_OUT_URB][OUT_BUFLEN];
159         /* Settings for the port */
160         int rts_state;  /* Handshaking pins (outputs) */
161         int dtr_state;
162         int cts_state;  /* Handshaking pins (inputs) */
163         int dsr_state;
164         int dcd_state;
165         int ri_state;
167         unsigned long tx_start_time[N_OUT_URB];
170 /* Functions used by new usb-serial code. */
171 static int __init option_init(void)
173         int retval;
174         retval = usb_serial_register(&option_3port_device);
175         if (retval)
176                 goto failed_3port_device_register;
177         retval = usb_register(&option_driver);
178         if (retval)
179                 goto failed_driver_register;
181         info(DRIVER_DESC ": " DRIVER_VERSION);
183         return 0;
185 failed_driver_register:
186         usb_serial_deregister (&option_3port_device);
187 failed_3port_device_register:
188         return retval;
191 static void __exit option_exit(void)
193         usb_deregister (&option_driver);
194         usb_serial_deregister (&option_3port_device);
197 module_init(option_init);
198 module_exit(option_exit);
200 static void option_rx_throttle(struct usb_serial_port *port)
202         dbg("%s", __FUNCTION__);
205 static void option_rx_unthrottle(struct usb_serial_port *port)
207         dbg("%s", __FUNCTION__);
210 static void option_break_ctl(struct usb_serial_port *port, int break_state)
212         /* Unfortunately, I don't know how to send a break */
213         dbg("%s", __FUNCTION__);
216 static void option_set_termios(struct usb_serial_port *port,
217                         struct termios *old_termios)
219         dbg("%s", __FUNCTION__);
221         option_send_setup(port);
224 static int 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 option_tiocmset(struct usb_serial_port *port, struct file *file,
242                         unsigned int set, unsigned int clear)
244         struct option_port_private *portdata;
246         portdata = usb_get_serial_port_data(port);
248         if (set & TIOCM_RTS)
249                 portdata->rts_state = 1;
250         if (set & TIOCM_DTR)
251                 portdata->dtr_state = 1;
253         if (clear & TIOCM_RTS)
254                 portdata->rts_state = 0;
255         if (clear & TIOCM_DTR)
256                 portdata->dtr_state = 0;
257         return option_send_setup(port);
260 static int option_ioctl(struct usb_serial_port *port, struct file *file,
261                         unsigned int cmd, unsigned long arg)
263         return -ENOIOCTLCMD;
266 /* Write */
267 static int option_write(struct usb_serial_port *port,
268                         const unsigned char *buf, int count)
270         struct option_port_private *portdata;
271         int i;
272         int left, todo;
273         struct urb *this_urb = NULL; /* spurious */
274         int err;
276         portdata = usb_get_serial_port_data(port);
278         dbg("%s: write (%d chars)", __FUNCTION__, count);
280         i = 0;
281         left = count;
282         for (i=0; left > 0 && i < N_OUT_URB; i++) {
283                 todo = left;
284                 if (todo > OUT_BUFLEN)
285                         todo = OUT_BUFLEN;
287                 this_urb = portdata->out_urbs[i];
288                 if (this_urb->status == -EINPROGRESS) {
289                         if (time_before(jiffies,
290                                         portdata->tx_start_time[i] + 10 * HZ))
291                                 continue;
292                         usb_unlink_urb(this_urb);
293                         continue;
294                 }
295                 if (this_urb->status != 0)
296                         dbg("usb_write %p failed (err=%d)",
297                                 this_urb, this_urb->status);
299                 dbg("%s: endpoint %d buf %d", __FUNCTION__,
300                         usb_pipeendpoint(this_urb->pipe), i);
302                 /* send the data */
303                 memcpy (this_urb->transfer_buffer, buf, todo);
304                 this_urb->transfer_buffer_length = todo;
306                 this_urb->dev = port->serial->dev;
307                 err = usb_submit_urb(this_urb, GFP_ATOMIC);
308                 if (err) {
309                         dbg("usb_submit_urb %p (write bulk) failed "
310                                 "(%d, has %d)", this_urb,
311                                 err, this_urb->status);
312                         continue;
313                 }
314                 portdata->tx_start_time[i] = jiffies;
315                 buf += todo;
316                 left -= todo;
317         }
319         count -= left;
320         dbg("%s: wrote (did %d)", __FUNCTION__, count);
321         return count;
324 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
326         int i, err;
327         int endpoint;
328         struct usb_serial_port *port;
329         struct tty_struct *tty;
330         unsigned char *data = urb->transfer_buffer;
332         dbg("%s: %p", __FUNCTION__, urb);
334         endpoint = usb_pipeendpoint(urb->pipe);
335         port = (struct usb_serial_port *) urb->context;
337         if (urb->status) {
338                 dbg("%s: nonzero status: %d on endpoint %02x.",
339                     __FUNCTION__, urb->status, endpoint);
340         } else {
341                 tty = port->tty;
342                 if (urb->actual_length) {
343                         for (i = 0; i < urb->actual_length ; ++i) {
344                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
345                                         tty_flip_buffer_push(tty);
346                                 tty_insert_flip_char(tty, data[i], 0);
347                         }
348                         tty_flip_buffer_push(tty);
349                 } else {
350                         dbg("%s: empty read urb received", __FUNCTION__);
351                 }
353                 /* Resubmit urb so we continue receiving */
354                 if (port->open_count && urb->status != -ESHUTDOWN) {
355                         err = usb_submit_urb(urb, GFP_ATOMIC);
356                         if (err)
357                                 printk(KERN_ERR "%s: resubmit read urb failed. "
358                                         "(%d)", __FUNCTION__, err);
359                 }
360         }
361         return;
364 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
366         struct usb_serial_port *port;
368         dbg("%s", __FUNCTION__);
370         port = (struct usb_serial_port *) urb->context;
372         if (port->open_count)
373                 schedule_work(&port->work);
376 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
378         int err;
379         struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
380         struct option_port_private *portdata = usb_get_serial_port_data(port);
381         struct usb_serial *serial = port->serial;
383         dbg("%s", __FUNCTION__);
384         dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
386         if (urb->status == 0) {
387                 struct usb_ctrlrequest *req_pkt =
388                                 (struct usb_ctrlrequest *)urb->transfer_buffer;
390                 if (!req_pkt) {
391                         dbg("%s: NULL req_pkt\n", __FUNCTION__);
392                         return;
393                 }
394                 if ((req_pkt->bRequestType == 0xA1) &&
395                                 (req_pkt->bRequest == 0x20)) {
396                         int old_dcd_state;
397                         unsigned char signals = *((unsigned char *)
398                                         urb->transfer_buffer +
399                                         sizeof(struct usb_ctrlrequest));
401                         dbg("%s: signal x%x", __FUNCTION__, signals);
403                         old_dcd_state = portdata->dcd_state;
404                         portdata->cts_state = 1;
405                         portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
406                         portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
407                         portdata->ri_state = ((signals & 0x08) ? 1 : 0);
409                         if (port->tty && !C_CLOCAL(port->tty) &&
410                                         old_dcd_state && !portdata->dcd_state)
411                                 tty_hangup(port->tty);
412                 } else {
413                         dbg("%s: type %x req %x", __FUNCTION__,
414                                 req_pkt->bRequestType,req_pkt->bRequest);
415                 }
416         } else
417                 dbg("%s: error %d", __FUNCTION__, urb->status);
419         /* Resubmit urb so we continue receiving IRQ data */
420         if (urb->status != -ESHUTDOWN) {
421                 urb->dev = serial->dev;
422                 err = usb_submit_urb(urb, GFP_ATOMIC);
423                 if (err)
424                         dbg("%s: resubmit intr urb failed. (%d)",
425                                 __FUNCTION__, err);
426         }
429 static int option_write_room(struct usb_serial_port *port)
431         struct option_port_private *portdata;
432         int i;
433         int data_len = 0;
434         struct urb *this_urb;
436         portdata = usb_get_serial_port_data(port);
438         for (i=0; i < N_OUT_URB; i++) {
439                 this_urb = portdata->out_urbs[i];
440                 if (this_urb && this_urb->status != -EINPROGRESS)
441                         data_len += OUT_BUFLEN;
442         }
444         dbg("%s: %d", __FUNCTION__, data_len);
445         return data_len;
448 static int option_chars_in_buffer(struct usb_serial_port *port)
450         struct option_port_private *portdata;
451         int i;
452         int data_len = 0;
453         struct urb *this_urb;
455         portdata = usb_get_serial_port_data(port);
457         for (i=0; i < N_OUT_URB; i++) {
458                 this_urb = portdata->out_urbs[i];
459                 if (this_urb && this_urb->status == -EINPROGRESS)
460                         data_len += this_urb->transfer_buffer_length;
461         }
462         dbg("%s: %d", __FUNCTION__, data_len);
463         return data_len;
466 static int option_open(struct usb_serial_port *port, struct file *filp)
468         struct option_port_private *portdata;
469         struct usb_serial *serial = port->serial;
470         int i, err;
471         struct urb *urb;
473         portdata = usb_get_serial_port_data(port);
475         dbg("%s", __FUNCTION__);
477         /* Set some sane defaults */
478         portdata->rts_state = 1;
479         portdata->dtr_state = 1;
481         /* Reset low level data toggle and start reading from endpoints */
482         for (i = 0; i < N_IN_URB; i++) {
483                 urb = portdata->in_urbs[i];
484                 if (! urb)
485                         continue;
486                 if (urb->dev != serial->dev) {
487                         dbg("%s: dev %p != %p", __FUNCTION__,
488                                 urb->dev, serial->dev);
489                         continue;
490                 }
492                 /*
493                  * make sure endpoint data toggle is synchronized with the
494                  * device
495                  */
496                 usb_clear_halt(urb->dev, urb->pipe);
498                 err = usb_submit_urb(urb, GFP_KERNEL);
499                 if (err) {
500                         dbg("%s: submit urb %d failed (%d) %d",
501                                 __FUNCTION__, i, err,
502                                 urb->transfer_buffer_length);
503                 }
504         }
506         /* Reset low level data toggle on out endpoints */
507         for (i = 0; i < N_OUT_URB; i++) {
508                 urb = portdata->out_urbs[i];
509                 if (! urb)
510                         continue;
511                 urb->dev = serial->dev;
512                 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
513                                 usb_pipeout(urb->pipe), 0); */
514         }
516         port->tty->low_latency = 1;
518         option_send_setup(port);
520         return (0);
523 static inline void stop_urb(struct urb *urb)
525         if (urb && urb->status == -EINPROGRESS)
526                 usb_kill_urb(urb);
529 static void option_close(struct usb_serial_port *port, struct file *filp)
531         int i;
532         struct usb_serial *serial = port->serial;
533         struct option_port_private *portdata;
535         dbg("%s", __FUNCTION__);
536         portdata = usb_get_serial_port_data(port);
538         portdata->rts_state = 0;
539         portdata->dtr_state = 0;
541         if (serial->dev) {
542                 option_send_setup(port);
544                 /* Stop reading/writing urbs */
545                 for (i = 0; i < N_IN_URB; i++)
546                         stop_urb(portdata->in_urbs[i]);
547                 for (i = 0; i < N_OUT_URB; i++)
548                         stop_urb(portdata->out_urbs[i]);
549         }
550         port->tty = NULL;
553 /* Helper functions used by option_setup_urbs */
554 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
555                 int dir, void *ctx, char *buf, int len,
556                 void (*callback)(struct urb *, struct pt_regs *regs))
558         struct urb *urb;
560         if (endpoint == -1)
561                 return NULL;            /* endpoint not needed */
563         urb = usb_alloc_urb(0, GFP_KERNEL);             /* No ISO */
564         if (urb == NULL) {
565                 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
566                 return NULL;
567         }
569                 /* Fill URB using supplied data. */
570         usb_fill_bulk_urb(urb, serial->dev,
571                       usb_sndbulkpipe(serial->dev, endpoint) | dir,
572                       buf, len, callback, ctx);
574         return urb;
577 /* Setup urbs */
578 static void option_setup_urbs(struct usb_serial *serial)
580         int j;
581         struct usb_serial_port *port;
582         struct option_port_private *portdata;
584         dbg("%s", __FUNCTION__);
586         port = serial->port[0];
587         portdata = usb_get_serial_port_data(port);
589         /* Do indat endpoints first */
590         for (j = 0; j <= N_IN_URB; ++j) {
591                 portdata->in_urbs[j] = option_setup_urb (serial,
592                   port->bulk_in_endpointAddress, USB_DIR_IN, port,
593                   portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
594         }
596         /* outdat endpoints */
597         for (j = 0; j <= N_OUT_URB; ++j) {
598                 portdata->out_urbs[j] = option_setup_urb (serial,
599                   port->bulk_out_endpointAddress, USB_DIR_OUT, port,
600                   portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
601         }
604 static int option_send_setup(struct usb_serial_port *port)
606         struct usb_serial *serial = port->serial;
607         struct option_port_private *portdata;
609         dbg("%s", __FUNCTION__);
611         portdata = usb_get_serial_port_data(port);
613         if (port->tty) {
614                 int val = 0;
615                 if (portdata->dtr_state)
616                         val |= 0x01;
617                 if (portdata->rts_state)
618                         val |= 0x02;
620                 return usb_control_msg(serial->dev,
621                                 usb_rcvctrlpipe(serial->dev, 0),
622                                 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
623         }
625         return 0;
628 static int option_startup(struct usb_serial *serial)
630         int i, err;
631         struct usb_serial_port *port;
632         struct option_port_private *portdata;
634         dbg("%s", __FUNCTION__);
636         /* Now setup per port private data */
637         for (i = 0; i < serial->num_ports; i++) {
638                 port = serial->port[i];
639                 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
640                 if (!portdata) {
641                         dbg("%s: kmalloc for option_port_private (%d) failed!.",
642                                         __FUNCTION__, i);
643                         return (1);
644                 }
645                 memset(portdata, 0, sizeof(struct option_port_private));
647                 usb_set_serial_port_data(port, portdata);
649                 if (! port->interrupt_in_urb)
650                         continue;
651                 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
652                 if (err)
653                         dbg("%s: submit irq_in urb failed %d",
654                                 __FUNCTION__, err);
655         }
657         option_setup_urbs(serial);
659         return (0);
662 static void option_shutdown(struct usb_serial *serial)
664         int i, j;
665         struct usb_serial_port *port;
666         struct option_port_private *portdata;
668         dbg("%s", __FUNCTION__);
670         /* Stop reading/writing urbs */
671         for (i = 0; i < serial->num_ports; ++i) {
672                 port = serial->port[i];
673                 portdata = usb_get_serial_port_data(port);
674                 for (j = 0; j < N_IN_URB; j++)
675                         stop_urb(portdata->in_urbs[j]);
676                 for (j = 0; j < N_OUT_URB; j++)
677                         stop_urb(portdata->out_urbs[j]);
678         }
680         /* Now free them */
681         for (i = 0; i < serial->num_ports; ++i) {
682                 port = serial->port[i];
683                 portdata = usb_get_serial_port_data(port);
685                 for (j = 0; j < N_IN_URB; j++) {
686                         if (portdata->in_urbs[j]) {
687                                 usb_free_urb(portdata->in_urbs[j]);
688                                 portdata->in_urbs[j] = NULL;
689                         }
690                 }
691                 for (j = 0; j < N_OUT_URB; j++) {
692                         if (portdata->out_urbs[j]) {
693                                 usb_free_urb(portdata->out_urbs[j]);
694                                 portdata->out_urbs[j] = NULL;
695                         }
696                 }
697         }
699         /* Now free per port private data */
700         for (i = 0; i < serial->num_ports; i++) {
701                 port = serial->port[i];
702                 kfree(usb_get_serial_port_data(port));
703         }
706 MODULE_AUTHOR(DRIVER_AUTHOR);
707 MODULE_DESCRIPTION(DRIVER_DESC);
708 MODULE_VERSION(DRIVER_VERSION);
709 MODULE_LICENSE("GPL");
711 #ifdef CONFIG_USB_DEBUG
712 module_param(debug, bool, S_IRUGO | S_IWUSR);
713 MODULE_PARM_DESC(debug, "Debug messages");
714 #endif