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/kernel.h>
61 #include <linux/jiffies.h>
62 #include <linux/errno.h>
63 #include <linux/tty.h>
64 #include <linux/tty_flip.h>
65 #include <linux/module.h>
66 #include <linux/usb.h>
67 #include "usb-serial.h"
69 /* Function prototypes */
70 static int option_open(struct usb_serial_port *port, struct file *filp);
71 static void option_close(struct usb_serial_port *port, struct file *filp);
72 static int option_startup(struct usb_serial *serial);
73 static void option_shutdown(struct usb_serial *serial);
74 static void option_rx_throttle(struct usb_serial_port *port);
75 static void option_rx_unthrottle(struct usb_serial_port *port);
76 static int option_write_room(struct usb_serial_port *port);
78 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
80 static int option_write(struct usb_serial_port *port,
81 const unsigned char *buf, int count);
83 static int option_chars_in_buffer(struct usb_serial_port *port);
84 static int option_ioctl(struct usb_serial_port *port, struct file *file,
85 unsigned int cmd, unsigned long arg);
86 static void option_set_termios(struct usb_serial_port *port,
88 static void option_break_ctl(struct usb_serial_port *port, int break_state);
89 static int option_tiocmget(struct usb_serial_port *port, struct file *file);
90 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
91 unsigned int set, unsigned int clear);
92 static int option_send_setup(struct usb_serial_port *port);
94 /* Vendor and product IDs */
95 #define OPTION_VENDOR_ID 0x0AF0
96 #define HUAWEI_VENDOR_ID 0x12D1
97 #define AUDIOVOX_VENDOR_ID 0x0F3D
98 #define SIERRAWIRELESS_VENDOR_ID 0x1199
99 #define NOVATELWIRELESS_VENDOR_ID 0x1410
101 #define OPTION_PRODUCT_OLD 0x5000
102 #define OPTION_PRODUCT_FUSION 0x6000
103 #define OPTION_PRODUCT_FUSION2 0x6300
104 #define OPTION_PRODUCT_COBRA 0x6500
105 #define HUAWEI_PRODUCT_E600 0x1001
106 #define AUDIOVOX_PRODUCT_AIRCARD 0x0112
107 #define SIERRAWIRELESS_PRODUCT_MC8755 0x6802
108 #define NOVATELWIRELESS_PRODUCT_U740 0x1400
110 static struct usb_device_id option_ids[] = {
111 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
112 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
113 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
114 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
115 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
116 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
117 { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
118 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
119 { } /* Terminating entry */
122 static struct usb_device_id option_ids1[] = {
123 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
124 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
125 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
126 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
127 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
128 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
129 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
130 { } /* Terminating entry */
132 static struct usb_device_id option_ids3[] = {
133 { USB_DEVICE(SIERRAWIRELESS_VENDOR_ID, SIERRAWIRELESS_PRODUCT_MC8755) },
134 { } /* Terminating entry */
137 MODULE_DEVICE_TABLE(usb, option_ids);
139 static struct usb_driver option_driver = {
141 .probe = usb_serial_probe,
142 .disconnect = usb_serial_disconnect,
143 .id_table = option_ids,
147 /* The card has three separate interfaces, which the serial driver
148 * recognizes separately, thus num_port=1.
150 static struct usb_serial_driver option_3port_device = {
152 .owner = THIS_MODULE,
155 .description = "GSM modem (3-port)",
156 .id_table = option_ids3,
157 .num_interrupt_in = NUM_DONT_CARE,
158 .num_bulk_in = NUM_DONT_CARE,
159 .num_bulk_out = NUM_DONT_CARE,
162 .close = option_close,
163 .write = option_write,
164 .write_room = option_write_room,
165 .chars_in_buffer = option_chars_in_buffer,
166 .throttle = option_rx_throttle,
167 .unthrottle = option_rx_unthrottle,
168 .set_termios = option_set_termios,
169 .break_ctl = option_break_ctl,
170 .tiocmget = option_tiocmget,
171 .tiocmset = option_tiocmset,
172 .attach = option_startup,
173 .shutdown = option_shutdown,
174 .read_int_callback = option_instat_callback,
177 static struct usb_serial_driver option_1port_device = {
179 .owner = THIS_MODULE,
182 .description = "GSM modem (1-port)",
183 .id_table = option_ids1,
184 .num_interrupt_in = NUM_DONT_CARE,
185 .num_bulk_in = NUM_DONT_CARE,
186 .num_bulk_out = NUM_DONT_CARE,
189 .close = option_close,
190 .write = option_write,
191 .write_room = option_write_room,
192 .chars_in_buffer = option_chars_in_buffer,
193 .throttle = option_rx_throttle,
194 .unthrottle = option_rx_unthrottle,
195 .ioctl = option_ioctl,
196 .set_termios = option_set_termios,
197 .break_ctl = option_break_ctl,
198 .tiocmget = option_tiocmget,
199 .tiocmset = option_tiocmset,
200 .attach = option_startup,
201 .shutdown = option_shutdown,
202 .read_int_callback = option_instat_callback,
205 #ifdef CONFIG_USB_DEBUG
211 /* per port private data */
215 #define IN_BUFLEN 4096
216 #define OUT_BUFLEN 128
218 struct option_port_private {
219 /* Input endpoints and buffer for this port */
220 struct urb *in_urbs[N_IN_URB];
221 char in_buffer[N_IN_URB][IN_BUFLEN];
222 /* Output endpoints and buffer for this port */
223 struct urb *out_urbs[N_OUT_URB];
224 char out_buffer[N_OUT_URB][OUT_BUFLEN];
226 /* Settings for the port */
227 int rts_state; /* Handshaking pins (outputs) */
229 int cts_state; /* Handshaking pins (inputs) */
234 unsigned long tx_start_time[N_OUT_URB];
237 /* Functions used by new usb-serial code. */
238 static int __init option_init(void)
241 retval = usb_serial_register(&option_1port_device);
243 goto failed_1port_device_register;
244 retval = usb_serial_register(&option_3port_device);
246 goto failed_3port_device_register;
247 retval = usb_register(&option_driver);
249 goto failed_driver_register;
251 info(DRIVER_DESC ": " DRIVER_VERSION);
255 failed_driver_register:
256 usb_serial_deregister (&option_3port_device);
257 failed_3port_device_register:
258 usb_serial_deregister (&option_1port_device);
259 failed_1port_device_register:
263 static void __exit option_exit(void)
265 usb_deregister (&option_driver);
266 usb_serial_deregister (&option_3port_device);
267 usb_serial_deregister (&option_1port_device);
270 module_init(option_init);
271 module_exit(option_exit);
273 static void option_rx_throttle(struct usb_serial_port *port)
275 dbg("%s", __FUNCTION__);
278 static void option_rx_unthrottle(struct usb_serial_port *port)
280 dbg("%s", __FUNCTION__);
283 static void option_break_ctl(struct usb_serial_port *port, int break_state)
285 /* Unfortunately, I don't know how to send a break */
286 dbg("%s", __FUNCTION__);
289 static void option_set_termios(struct usb_serial_port *port,
290 struct termios *old_termios)
292 dbg("%s", __FUNCTION__);
294 option_send_setup(port);
297 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
300 struct option_port_private *portdata;
302 portdata = usb_get_serial_port_data(port);
304 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
305 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
306 ((portdata->cts_state) ? TIOCM_CTS : 0) |
307 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
308 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
309 ((portdata->ri_state) ? TIOCM_RNG : 0);
314 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
315 unsigned int set, unsigned int clear)
317 struct option_port_private *portdata;
319 portdata = usb_get_serial_port_data(port);
322 portdata->rts_state = 1;
324 portdata->dtr_state = 1;
326 if (clear & TIOCM_RTS)
327 portdata->rts_state = 0;
328 if (clear & TIOCM_DTR)
329 portdata->dtr_state = 0;
330 return option_send_setup(port);
333 static int option_ioctl(struct usb_serial_port *port, struct file *file,
334 unsigned int cmd, unsigned long arg)
340 static int option_write(struct usb_serial_port *port,
341 const unsigned char *buf, int count)
343 struct option_port_private *portdata;
346 struct urb *this_urb = NULL; /* spurious */
349 portdata = usb_get_serial_port_data(port);
351 dbg("%s: write (%d chars)", __FUNCTION__, count);
355 for (i=0; left > 0 && i < N_OUT_URB; i++) {
357 if (todo > OUT_BUFLEN)
360 this_urb = portdata->out_urbs[i];
361 if (this_urb->status == -EINPROGRESS) {
362 if (time_before(jiffies,
363 portdata->tx_start_time[i] + 10 * HZ))
365 usb_unlink_urb(this_urb);
368 if (this_urb->status != 0)
369 dbg("usb_write %p failed (err=%d)",
370 this_urb, this_urb->status);
372 dbg("%s: endpoint %d buf %d", __FUNCTION__,
373 usb_pipeendpoint(this_urb->pipe), i);
376 memcpy (this_urb->transfer_buffer, buf, todo);
377 this_urb->transfer_buffer_length = todo;
379 this_urb->dev = port->serial->dev;
380 err = usb_submit_urb(this_urb, GFP_ATOMIC);
382 dbg("usb_submit_urb %p (write bulk) failed "
383 "(%d, has %d)", this_urb,
384 err, this_urb->status);
387 portdata->tx_start_time[i] = jiffies;
393 dbg("%s: wrote (did %d)", __FUNCTION__, count);
397 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
401 struct usb_serial_port *port;
402 struct tty_struct *tty;
403 unsigned char *data = urb->transfer_buffer;
405 dbg("%s: %p", __FUNCTION__, urb);
407 endpoint = usb_pipeendpoint(urb->pipe);
408 port = (struct usb_serial_port *) urb->context;
411 dbg("%s: nonzero status: %d on endpoint %02x.",
412 __FUNCTION__, urb->status, endpoint);
415 if (urb->actual_length) {
416 tty_buffer_request_room(tty, urb->actual_length);
417 tty_insert_flip_string(tty, data, urb->actual_length);
418 tty_flip_buffer_push(tty);
420 dbg("%s: empty read urb received", __FUNCTION__);
423 /* Resubmit urb so we continue receiving */
424 if (port->open_count && urb->status != -ESHUTDOWN) {
425 err = usb_submit_urb(urb, GFP_ATOMIC);
427 printk(KERN_ERR "%s: resubmit read urb failed. "
428 "(%d)", __FUNCTION__, err);
434 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
436 struct usb_serial_port *port;
438 dbg("%s", __FUNCTION__);
440 port = (struct usb_serial_port *) urb->context;
442 usb_serial_port_softint(port);
445 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
448 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
449 struct option_port_private *portdata = usb_get_serial_port_data(port);
450 struct usb_serial *serial = port->serial;
452 dbg("%s", __FUNCTION__);
453 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
455 if (urb->status == 0) {
456 struct usb_ctrlrequest *req_pkt =
457 (struct usb_ctrlrequest *)urb->transfer_buffer;
460 dbg("%s: NULL req_pkt\n", __FUNCTION__);
463 if ((req_pkt->bRequestType == 0xA1) &&
464 (req_pkt->bRequest == 0x20)) {
466 unsigned char signals = *((unsigned char *)
467 urb->transfer_buffer +
468 sizeof(struct usb_ctrlrequest));
470 dbg("%s: signal x%x", __FUNCTION__, signals);
472 old_dcd_state = portdata->dcd_state;
473 portdata->cts_state = 1;
474 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
475 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
476 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
478 if (port->tty && !C_CLOCAL(port->tty) &&
479 old_dcd_state && !portdata->dcd_state)
480 tty_hangup(port->tty);
482 dbg("%s: type %x req %x", __FUNCTION__,
483 req_pkt->bRequestType,req_pkt->bRequest);
486 dbg("%s: error %d", __FUNCTION__, urb->status);
488 /* Resubmit urb so we continue receiving IRQ data */
489 if (urb->status != -ESHUTDOWN) {
490 urb->dev = serial->dev;
491 err = usb_submit_urb(urb, GFP_ATOMIC);
493 dbg("%s: resubmit intr urb failed. (%d)",
498 static int option_write_room(struct usb_serial_port *port)
500 struct option_port_private *portdata;
503 struct urb *this_urb;
505 portdata = usb_get_serial_port_data(port);
507 for (i=0; i < N_OUT_URB; i++) {
508 this_urb = portdata->out_urbs[i];
509 if (this_urb && this_urb->status != -EINPROGRESS)
510 data_len += OUT_BUFLEN;
513 dbg("%s: %d", __FUNCTION__, data_len);
517 static int option_chars_in_buffer(struct usb_serial_port *port)
519 struct option_port_private *portdata;
522 struct urb *this_urb;
524 portdata = usb_get_serial_port_data(port);
526 for (i=0; i < N_OUT_URB; i++) {
527 this_urb = portdata->out_urbs[i];
528 if (this_urb && this_urb->status == -EINPROGRESS)
529 data_len += this_urb->transfer_buffer_length;
531 dbg("%s: %d", __FUNCTION__, data_len);
535 static int option_open(struct usb_serial_port *port, struct file *filp)
537 struct option_port_private *portdata;
538 struct usb_serial *serial = port->serial;
542 portdata = usb_get_serial_port_data(port);
544 dbg("%s", __FUNCTION__);
546 /* Set some sane defaults */
547 portdata->rts_state = 1;
548 portdata->dtr_state = 1;
550 /* Reset low level data toggle and start reading from endpoints */
551 for (i = 0; i < N_IN_URB; i++) {
552 urb = portdata->in_urbs[i];
555 if (urb->dev != serial->dev) {
556 dbg("%s: dev %p != %p", __FUNCTION__,
557 urb->dev, serial->dev);
562 * make sure endpoint data toggle is synchronized with the
565 usb_clear_halt(urb->dev, urb->pipe);
567 err = usb_submit_urb(urb, GFP_KERNEL);
569 dbg("%s: submit urb %d failed (%d) %d",
570 __FUNCTION__, i, err,
571 urb->transfer_buffer_length);
575 /* Reset low level data toggle on out endpoints */
576 for (i = 0; i < N_OUT_URB; i++) {
577 urb = portdata->out_urbs[i];
580 urb->dev = serial->dev;
581 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
582 usb_pipeout(urb->pipe), 0); */
585 port->tty->low_latency = 1;
587 option_send_setup(port);
592 static inline void stop_urb(struct urb *urb)
594 if (urb && urb->status == -EINPROGRESS)
598 static void option_close(struct usb_serial_port *port, struct file *filp)
601 struct usb_serial *serial = port->serial;
602 struct option_port_private *portdata;
604 dbg("%s", __FUNCTION__);
605 portdata = usb_get_serial_port_data(port);
607 portdata->rts_state = 0;
608 portdata->dtr_state = 0;
611 option_send_setup(port);
613 /* Stop reading/writing urbs */
614 for (i = 0; i < N_IN_URB; i++)
615 stop_urb(portdata->in_urbs[i]);
616 for (i = 0; i < N_OUT_URB; i++)
617 stop_urb(portdata->out_urbs[i]);
622 /* Helper functions used by option_setup_urbs */
623 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
624 int dir, void *ctx, char *buf, int len,
625 void (*callback)(struct urb *, struct pt_regs *regs))
630 return NULL; /* endpoint not needed */
632 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
634 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
638 /* Fill URB using supplied data. */
639 usb_fill_bulk_urb(urb, serial->dev,
640 usb_sndbulkpipe(serial->dev, endpoint) | dir,
641 buf, len, callback, ctx);
647 static void option_setup_urbs(struct usb_serial *serial)
650 struct usb_serial_port *port;
651 struct option_port_private *portdata;
653 dbg("%s", __FUNCTION__);
656 for (i = 0; i < serial->num_ports; i++) {
657 port = serial->port[i];
658 portdata = usb_get_serial_port_data(port);
660 /* Do indat endpoints first */
661 for (j = 0; j < N_IN_URB; ++j) {
662 portdata->in_urbs[j] = option_setup_urb (serial,
663 port->bulk_in_endpointAddress, USB_DIR_IN, port,
664 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
667 /* outdat endpoints */
668 for (j = 0; j < N_OUT_URB; ++j) {
669 portdata->out_urbs[j] = option_setup_urb (serial,
670 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
671 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
676 static int option_send_setup(struct usb_serial_port *port)
678 struct usb_serial *serial = port->serial;
679 struct option_port_private *portdata;
681 dbg("%s", __FUNCTION__);
683 portdata = usb_get_serial_port_data(port);
687 if (portdata->dtr_state)
689 if (portdata->rts_state)
692 return usb_control_msg(serial->dev,
693 usb_rcvctrlpipe(serial->dev, 0),
694 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
700 static int option_startup(struct usb_serial *serial)
703 struct usb_serial_port *port;
704 struct option_port_private *portdata;
706 dbg("%s", __FUNCTION__);
708 /* Now setup per port private data */
709 for (i = 0; i < serial->num_ports; i++) {
710 port = serial->port[i];
711 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
713 dbg("%s: kmalloc for option_port_private (%d) failed!.",
718 usb_set_serial_port_data(port, portdata);
720 if (! port->interrupt_in_urb)
722 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
724 dbg("%s: submit irq_in urb failed %d",
728 option_setup_urbs(serial);
733 static void option_shutdown(struct usb_serial *serial)
736 struct usb_serial_port *port;
737 struct option_port_private *portdata;
739 dbg("%s", __FUNCTION__);
741 /* Stop reading/writing urbs */
742 for (i = 0; i < serial->num_ports; ++i) {
743 port = serial->port[i];
744 portdata = usb_get_serial_port_data(port);
745 for (j = 0; j < N_IN_URB; j++)
746 stop_urb(portdata->in_urbs[j]);
747 for (j = 0; j < N_OUT_URB; j++)
748 stop_urb(portdata->out_urbs[j]);
752 for (i = 0; i < serial->num_ports; ++i) {
753 port = serial->port[i];
754 portdata = usb_get_serial_port_data(port);
756 for (j = 0; j < N_IN_URB; j++) {
757 if (portdata->in_urbs[j]) {
758 usb_free_urb(portdata->in_urbs[j]);
759 portdata->in_urbs[j] = NULL;
762 for (j = 0; j < N_OUT_URB; j++) {
763 if (portdata->out_urbs[j]) {
764 usb_free_urb(portdata->out_urbs[j]);
765 portdata->out_urbs[j] = NULL;
770 /* Now free per port private data */
771 for (i = 0; i < serial->num_ports; i++) {
772 port = serial->port[i];
773 kfree(usb_get_serial_port_data(port));
777 MODULE_AUTHOR(DRIVER_AUTHOR);
778 MODULE_DESCRIPTION(DRIVER_DESC);
779 MODULE_VERSION(DRIVER_VERSION);
780 MODULE_LICENSE("GPL");
782 #ifdef CONFIG_USB_DEBUG
783 module_param(debug, bool, S_IRUGO | S_IWUSR);
784 MODULE_PARM_DESC(debug, "Debug messages");