2 USB Driver for GSM modems
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
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.
31 2006-04-10 v0.5 fixed two array overrun errors :-/
32 2006-04-21 v0.5.1 added support for Sierra Wireless MC8755
33 2006-05-15 v0.6 re-enable multi-port support
34 2006-06-01 v0.6.1 add COBRA
35 2006-06-01 v0.6.2 add backwards-compatibility stuff
36 2006-06-01 v0.6.3 add Novatel Wireless
37 2006-06-01 v0.7 Option => GSM
39 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
41 This driver exists because the "normal" serial driver doesn't work too well
42 with GSM modems. Issues:
43 - data loss -- one single Receive URB is not nearly enough
44 - nonstandard flow (Option devices) and multiplex (Sierra) control
45 - controlling the baud rate doesn't make sense
47 This driver is named "option" because the most common device it's
48 used for is a PC-Card (with an internal OHCI-USB interface, behind
49 which the GSM interface sits), made by Option Inc.
51 Some of the "one port" devices actually exhibit multiple USB instances
52 on the USB bus. This is not a bug, these ports are used for different
56 #define DRIVER_VERSION "v0.7.0"
57 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
58 #define DRIVER_DESC "USB Driver for GSM modems"
60 #include <linux/config.h>
61 #include <linux/kernel.h>
62 #include <linux/jiffies.h>
63 #include <linux/errno.h>
64 #include <linux/tty.h>
65 #include <linux/tty_flip.h>
66 #include <linux/module.h>
67 #include <linux/usb.h>
68 #include "usb-serial.h"
70 /* Function prototypes */
71 static int option_open(struct usb_serial_port *port, struct file *filp);
72 static void option_close(struct usb_serial_port *port, struct file *filp);
73 static int option_startup(struct usb_serial *serial);
74 static void option_shutdown(struct usb_serial *serial);
75 static void option_rx_throttle(struct usb_serial_port *port);
76 static void option_rx_unthrottle(struct usb_serial_port *port);
77 static int option_write_room(struct usb_serial_port *port);
79 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
81 static int option_write(struct usb_serial_port *port,
82 const unsigned char *buf, int count);
84 static int option_chars_in_buffer(struct usb_serial_port *port);
85 static int option_ioctl(struct usb_serial_port *port, struct file *file,
86 unsigned int cmd, unsigned long arg);
87 static void option_set_termios(struct usb_serial_port *port,
89 static void option_break_ctl(struct usb_serial_port *port, int break_state);
90 static int option_tiocmget(struct usb_serial_port *port, struct file *file);
91 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
92 unsigned int set, unsigned int clear);
93 static int option_send_setup(struct usb_serial_port *port);
95 /* Vendor and product IDs */
96 #define OPTION_VENDOR_ID 0x0AF0
97 #define HUAWEI_VENDOR_ID 0x12D1
98 #define AUDIOVOX_VENDOR_ID 0x0F3D
99 #define SIERRAWIRELESS_VENDOR_ID 0x1199
100 #define NOVATELWIRELESS_VENDOR_ID 0x1410
102 #define OPTION_PRODUCT_OLD 0x5000
103 #define OPTION_PRODUCT_FUSION 0x6000
104 #define OPTION_PRODUCT_FUSION2 0x6300
105 #define OPTION_PRODUCT_COBRA 0x6500
106 #define HUAWEI_PRODUCT_E600 0x1001
107 #define AUDIOVOX_PRODUCT_AIRCARD 0x0112
108 #define SIERRAWIRELESS_PRODUCT_MC8755 0x6802
109 #define NOVATELWIRELESS_PRODUCT_U740 0x1400
111 static struct usb_device_id option_ids[] = {
112 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
113 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
114 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
115 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
116 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
117 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
118 { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
119 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
120 { } /* Terminating entry */
123 static struct usb_device_id option_ids1[] = {
124 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
125 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
126 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
127 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
128 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
129 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
130 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
131 { } /* Terminating entry */
133 static struct usb_device_id option_ids3[] = {
134 { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
135 { } /* Terminating entry */
138 MODULE_DEVICE_TABLE(usb, option_ids);
140 static struct usb_driver option_driver = {
142 .probe = usb_serial_probe,
143 .disconnect = usb_serial_disconnect,
144 .id_table = option_ids,
148 /* The card has three separate interfaces, which the serial driver
149 * recognizes separately, thus num_port=1.
151 static struct usb_serial_driver option_3port_device = {
153 .owner = THIS_MODULE,
156 .description = "GSM modem (3-port)",
157 .id_table = option_ids3,
158 .num_interrupt_in = NUM_DONT_CARE,
159 .num_bulk_in = NUM_DONT_CARE,
160 .num_bulk_out = NUM_DONT_CARE,
163 .close = option_close,
164 .write = option_write,
165 .write_room = option_write_room,
166 .chars_in_buffer = option_chars_in_buffer,
167 .throttle = option_rx_throttle,
168 .unthrottle = option_rx_unthrottle,
169 .set_termios = option_set_termios,
170 .break_ctl = option_break_ctl,
171 .tiocmget = option_tiocmget,
172 .tiocmset = option_tiocmset,
173 .attach = option_startup,
174 .shutdown = option_shutdown,
175 .read_int_callback = option_instat_callback,
178 static struct usb_serial_driver option_1port_device = {
180 .owner = THIS_MODULE,
183 .description = "GSM modem (1-port)",
184 .id_table = option_ids1,
185 .num_interrupt_in = NUM_DONT_CARE,
186 .num_bulk_in = NUM_DONT_CARE,
187 .num_bulk_out = NUM_DONT_CARE,
190 .close = option_close,
191 .write = option_write,
192 .write_room = option_write_room,
193 .chars_in_buffer = option_chars_in_buffer,
194 .throttle = option_rx_throttle,
195 .unthrottle = option_rx_unthrottle,
196 .ioctl = option_ioctl,
197 .set_termios = option_set_termios,
198 .break_ctl = option_break_ctl,
199 .tiocmget = option_tiocmget,
200 .tiocmset = option_tiocmset,
201 .attach = option_startup,
202 .shutdown = option_shutdown,
203 .read_int_callback = option_instat_callback,
206 #ifdef CONFIG_USB_DEBUG
212 /* per port private data */
216 #define IN_BUFLEN 4096
217 #define OUT_BUFLEN 128
219 struct option_port_private {
220 /* Input endpoints and buffer for this port */
221 struct urb *in_urbs[N_IN_URB];
222 char in_buffer[N_IN_URB][IN_BUFLEN];
223 /* Output endpoints and buffer for this port */
224 struct urb *out_urbs[N_OUT_URB];
225 char out_buffer[N_OUT_URB][OUT_BUFLEN];
227 /* Settings for the port */
228 int rts_state; /* Handshaking pins (outputs) */
230 int cts_state; /* Handshaking pins (inputs) */
235 unsigned long tx_start_time[N_OUT_URB];
238 /* Functions used by new usb-serial code. */
239 static int __init option_init(void)
242 retval = usb_serial_register(&option_1port_device);
244 goto failed_1port_device_register;
245 retval = usb_serial_register(&option_3port_device);
247 goto failed_3port_device_register;
248 retval = usb_register(&option_driver);
250 goto failed_driver_register;
252 info(DRIVER_DESC ": " DRIVER_VERSION);
256 failed_driver_register:
257 usb_serial_deregister (&option_3port_device);
258 failed_3port_device_register:
259 usb_serial_deregister (&option_1port_device);
260 failed_1port_device_register:
264 static void __exit option_exit(void)
266 usb_deregister (&option_driver);
267 usb_serial_deregister (&option_3port_device);
268 usb_serial_deregister (&option_1port_device);
271 module_init(option_init);
272 module_exit(option_exit);
274 static void option_rx_throttle(struct usb_serial_port *port)
276 dbg("%s", __FUNCTION__);
279 static void option_rx_unthrottle(struct usb_serial_port *port)
281 dbg("%s", __FUNCTION__);
284 static void option_break_ctl(struct usb_serial_port *port, int break_state)
286 /* Unfortunately, I don't know how to send a break */
287 dbg("%s", __FUNCTION__);
290 static void option_set_termios(struct usb_serial_port *port,
291 struct termios *old_termios)
293 dbg("%s", __FUNCTION__);
295 option_send_setup(port);
298 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
301 struct option_port_private *portdata;
303 portdata = usb_get_serial_port_data(port);
305 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
306 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
307 ((portdata->cts_state) ? TIOCM_CTS : 0) |
308 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
309 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
310 ((portdata->ri_state) ? TIOCM_RNG : 0);
315 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
316 unsigned int set, unsigned int clear)
318 struct option_port_private *portdata;
320 portdata = usb_get_serial_port_data(port);
323 portdata->rts_state = 1;
325 portdata->dtr_state = 1;
327 if (clear & TIOCM_RTS)
328 portdata->rts_state = 0;
329 if (clear & TIOCM_DTR)
330 portdata->dtr_state = 0;
331 return option_send_setup(port);
334 static int option_ioctl(struct usb_serial_port *port, struct file *file,
335 unsigned int cmd, unsigned long arg)
341 static int option_write(struct usb_serial_port *port,
342 const unsigned char *buf, int count)
344 struct option_port_private *portdata;
347 struct urb *this_urb = NULL; /* spurious */
350 portdata = usb_get_serial_port_data(port);
352 dbg("%s: write (%d chars)", __FUNCTION__, count);
356 for (i=0; left > 0 && i < N_OUT_URB; i++) {
358 if (todo > OUT_BUFLEN)
361 this_urb = portdata->out_urbs[i];
362 if (this_urb->status == -EINPROGRESS) {
363 if (time_before(jiffies,
364 portdata->tx_start_time[i] + 10 * HZ))
366 usb_unlink_urb(this_urb);
369 if (this_urb->status != 0)
370 dbg("usb_write %p failed (err=%d)",
371 this_urb, this_urb->status);
373 dbg("%s: endpoint %d buf %d", __FUNCTION__,
374 usb_pipeendpoint(this_urb->pipe), i);
377 memcpy (this_urb->transfer_buffer, buf, todo);
378 this_urb->transfer_buffer_length = todo;
380 this_urb->dev = port->serial->dev;
381 err = usb_submit_urb(this_urb, GFP_ATOMIC);
383 dbg("usb_submit_urb %p (write bulk) failed "
384 "(%d, has %d)", this_urb,
385 err, this_urb->status);
388 portdata->tx_start_time[i] = jiffies;
394 dbg("%s: wrote (did %d)", __FUNCTION__, count);
398 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
402 struct usb_serial_port *port;
403 struct tty_struct *tty;
404 unsigned char *data = urb->transfer_buffer;
406 dbg("%s: %p", __FUNCTION__, urb);
408 endpoint = usb_pipeendpoint(urb->pipe);
409 port = (struct usb_serial_port *) urb->context;
412 dbg("%s: nonzero status: %d on endpoint %02x.",
413 __FUNCTION__, urb->status, endpoint);
416 if (urb->actual_length) {
417 tty_buffer_request_room(tty, urb->actual_length);
418 tty_insert_flip_string(tty, data, urb->actual_length);
419 tty_flip_buffer_push(tty);
421 dbg("%s: empty read urb received", __FUNCTION__);
424 /* Resubmit urb so we continue receiving */
425 if (port->open_count && urb->status != -ESHUTDOWN) {
426 err = usb_submit_urb(urb, GFP_ATOMIC);
428 printk(KERN_ERR "%s: resubmit read urb failed. "
429 "(%d)", __FUNCTION__, err);
435 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
437 struct usb_serial_port *port;
439 dbg("%s", __FUNCTION__);
441 port = (struct usb_serial_port *) urb->context;
443 usb_serial_port_softint(port);
446 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
449 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
450 struct option_port_private *portdata = usb_get_serial_port_data(port);
451 struct usb_serial *serial = port->serial;
453 dbg("%s", __FUNCTION__);
454 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
456 if (urb->status == 0) {
457 struct usb_ctrlrequest *req_pkt =
458 (struct usb_ctrlrequest *)urb->transfer_buffer;
461 dbg("%s: NULL req_pkt\n", __FUNCTION__);
464 if ((req_pkt->bRequestType == 0xA1) &&
465 (req_pkt->bRequest == 0x20)) {
467 unsigned char signals = *((unsigned char *)
468 urb->transfer_buffer +
469 sizeof(struct usb_ctrlrequest));
471 dbg("%s: signal x%x", __FUNCTION__, signals);
473 old_dcd_state = portdata->dcd_state;
474 portdata->cts_state = 1;
475 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
476 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
477 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
479 if (port->tty && !C_CLOCAL(port->tty) &&
480 old_dcd_state && !portdata->dcd_state)
481 tty_hangup(port->tty);
483 dbg("%s: type %x req %x", __FUNCTION__,
484 req_pkt->bRequestType,req_pkt->bRequest);
487 dbg("%s: error %d", __FUNCTION__, urb->status);
489 /* Resubmit urb so we continue receiving IRQ data */
490 if (urb->status != -ESHUTDOWN) {
491 urb->dev = serial->dev;
492 err = usb_submit_urb(urb, GFP_ATOMIC);
494 dbg("%s: resubmit intr urb failed. (%d)",
499 static int option_write_room(struct usb_serial_port *port)
501 struct option_port_private *portdata;
504 struct urb *this_urb;
506 portdata = usb_get_serial_port_data(port);
508 for (i=0; i < N_OUT_URB; i++) {
509 this_urb = portdata->out_urbs[i];
510 if (this_urb && this_urb->status != -EINPROGRESS)
511 data_len += OUT_BUFLEN;
514 dbg("%s: %d", __FUNCTION__, data_len);
518 static int option_chars_in_buffer(struct usb_serial_port *port)
520 struct option_port_private *portdata;
523 struct urb *this_urb;
525 portdata = usb_get_serial_port_data(port);
527 for (i=0; i < N_OUT_URB; i++) {
528 this_urb = portdata->out_urbs[i];
529 if (this_urb && this_urb->status == -EINPROGRESS)
530 data_len += this_urb->transfer_buffer_length;
532 dbg("%s: %d", __FUNCTION__, data_len);
536 static int option_open(struct usb_serial_port *port, struct file *filp)
538 struct option_port_private *portdata;
539 struct usb_serial *serial = port->serial;
543 portdata = usb_get_serial_port_data(port);
545 dbg("%s", __FUNCTION__);
547 /* Set some sane defaults */
548 portdata->rts_state = 1;
549 portdata->dtr_state = 1;
551 /* Reset low level data toggle and start reading from endpoints */
552 for (i = 0; i < N_IN_URB; i++) {
553 urb = portdata->in_urbs[i];
556 if (urb->dev != serial->dev) {
557 dbg("%s: dev %p != %p", __FUNCTION__,
558 urb->dev, serial->dev);
563 * make sure endpoint data toggle is synchronized with the
566 usb_clear_halt(urb->dev, urb->pipe);
568 err = usb_submit_urb(urb, GFP_KERNEL);
570 dbg("%s: submit urb %d failed (%d) %d",
571 __FUNCTION__, i, err,
572 urb->transfer_buffer_length);
576 /* Reset low level data toggle on out endpoints */
577 for (i = 0; i < N_OUT_URB; i++) {
578 urb = portdata->out_urbs[i];
581 urb->dev = serial->dev;
582 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
583 usb_pipeout(urb->pipe), 0); */
586 port->tty->low_latency = 1;
588 option_send_setup(port);
593 static inline void stop_urb(struct urb *urb)
595 if (urb && urb->status == -EINPROGRESS)
599 static void option_close(struct usb_serial_port *port, struct file *filp)
602 struct usb_serial *serial = port->serial;
603 struct option_port_private *portdata;
605 dbg("%s", __FUNCTION__);
606 portdata = usb_get_serial_port_data(port);
608 portdata->rts_state = 0;
609 portdata->dtr_state = 0;
612 option_send_setup(port);
614 /* Stop reading/writing urbs */
615 for (i = 0; i < N_IN_URB; i++)
616 stop_urb(portdata->in_urbs[i]);
617 for (i = 0; i < N_OUT_URB; i++)
618 stop_urb(portdata->out_urbs[i]);
623 /* Helper functions used by option_setup_urbs */
624 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
625 int dir, void *ctx, char *buf, int len,
626 void (*callback)(struct urb *, struct pt_regs *regs))
631 return NULL; /* endpoint not needed */
633 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
635 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
639 /* Fill URB using supplied data. */
640 usb_fill_bulk_urb(urb, serial->dev,
641 usb_sndbulkpipe(serial->dev, endpoint) | dir,
642 buf, len, callback, ctx);
648 static void option_setup_urbs(struct usb_serial *serial)
651 struct usb_serial_port *port;
652 struct option_port_private *portdata;
654 dbg("%s", __FUNCTION__);
657 for (i = 0; i < serial->num_ports; i++) {
658 port = serial->port[i];
659 portdata = usb_get_serial_port_data(port);
661 /* Do indat endpoints first */
662 for (j = 0; j < N_IN_URB; ++j) {
663 portdata->in_urbs[j] = option_setup_urb (serial,
664 port->bulk_in_endpointAddress, USB_DIR_IN, port,
665 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
668 /* outdat endpoints */
669 for (j = 0; j < N_OUT_URB; ++j) {
670 portdata->out_urbs[j] = option_setup_urb (serial,
671 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
672 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
677 static int option_send_setup(struct usb_serial_port *port)
679 struct usb_serial *serial = port->serial;
680 struct option_port_private *portdata;
682 dbg("%s", __FUNCTION__);
684 portdata = usb_get_serial_port_data(port);
688 if (portdata->dtr_state)
690 if (portdata->rts_state)
693 return usb_control_msg(serial->dev,
694 usb_rcvctrlpipe(serial->dev, 0),
695 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
701 static int option_startup(struct usb_serial *serial)
704 struct usb_serial_port *port;
705 struct option_port_private *portdata;
707 dbg("%s", __FUNCTION__);
709 /* Now setup per port private data */
710 for (i = 0; i < serial->num_ports; i++) {
711 port = serial->port[i];
712 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
714 dbg("%s: kmalloc for option_port_private (%d) failed!.",
719 usb_set_serial_port_data(port, portdata);
721 if (! port->interrupt_in_urb)
723 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
725 dbg("%s: submit irq_in urb failed %d",
729 option_setup_urbs(serial);
734 static void option_shutdown(struct usb_serial *serial)
737 struct usb_serial_port *port;
738 struct option_port_private *portdata;
740 dbg("%s", __FUNCTION__);
742 /* Stop reading/writing urbs */
743 for (i = 0; i < serial->num_ports; ++i) {
744 port = serial->port[i];
745 portdata = usb_get_serial_port_data(port);
746 for (j = 0; j < N_IN_URB; j++)
747 stop_urb(portdata->in_urbs[j]);
748 for (j = 0; j < N_OUT_URB; j++)
749 stop_urb(portdata->out_urbs[j]);
753 for (i = 0; i < serial->num_ports; ++i) {
754 port = serial->port[i];
755 portdata = usb_get_serial_port_data(port);
757 for (j = 0; j < N_IN_URB; j++) {
758 if (portdata->in_urbs[j]) {
759 usb_free_urb(portdata->in_urbs[j]);
760 portdata->in_urbs[j] = NULL;
763 for (j = 0; j < N_OUT_URB; j++) {
764 if (portdata->out_urbs[j]) {
765 usb_free_urb(portdata->out_urbs[j]);
766 portdata->out_urbs[j] = NULL;
771 /* Now free per port private data */
772 for (i = 0; i < serial->num_ports; i++) {
773 port = serial->port[i];
774 kfree(usb_get_serial_port_data(port));
778 MODULE_AUTHOR(DRIVER_AUTHOR);
779 MODULE_DESCRIPTION(DRIVER_DESC);
780 MODULE_VERSION(DRIVER_VERSION);
781 MODULE_LICENSE("GPL");
783 #ifdef CONFIG_USB_DEBUG
784 module_param(debug, bool, S_IRUGO | S_IWUSR);
785 MODULE_PARM_DESC(debug, "Debug messages");