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>
12 History: see the git log.
14 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
16 This driver exists because the "normal" serial driver doesn't work too well
17 with GSM modems. Issues:
18 - data loss -- one single Receive URB is not nearly enough
19 - nonstandard flow (Option devices) control
20 - controlling the baud rate doesn't make sense
22 This driver is named "option" because the most common device it's
23 used for is a PC-Card (with an internal OHCI-USB interface, behind
24 which the GSM interface sits), made by Option Inc.
26 Some of the "one port" devices actually exhibit multiple USB instances
27 on the USB bus. This is not a bug, these ports are used for different
31 #define DRIVER_VERSION "v0.7.1"
32 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
33 #define DRIVER_DESC "USB Driver for GSM modems"
35 #include <linux/kernel.h>
36 #include <linux/jiffies.h>
37 #include <linux/errno.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/module.h>
41 #include <linux/usb.h>
42 //#include <linux/usb/serial.h>
43 #include "usb-serial.h"
45 /* Function prototypes */
46 static int option_open(struct usb_serial_port *port, struct file *filp);
47 static void option_close(struct usb_serial_port *port, struct file *filp);
48 static int option_startup(struct usb_serial *serial);
49 static void option_shutdown(struct usb_serial *serial);
50 static void option_rx_throttle(struct usb_serial_port *port);
51 static void option_rx_unthrottle(struct usb_serial_port *port);
52 static int option_write_room(struct usb_serial_port *port);
54 static void option_instat_callback(struct urb *urb, struct pt_regs *regs);
56 static int option_write(struct usb_serial_port *port,
57 const unsigned char *buf, int count);
59 static int option_chars_in_buffer(struct usb_serial_port *port);
60 static int option_ioctl(struct usb_serial_port *port, struct file *file,
61 unsigned int cmd, unsigned long arg);
62 static void option_set_termios(struct usb_serial_port *port,
64 static void option_break_ctl(struct usb_serial_port *port, int break_state);
65 static int option_tiocmget(struct usb_serial_port *port, struct file *file);
66 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
67 unsigned int set, unsigned int clear);
68 static int option_send_setup(struct usb_serial_port *port);
70 /* Vendor and product IDs */
71 #define OPTION_VENDOR_ID 0x0AF0
72 #define HUAWEI_VENDOR_ID 0x12D1
73 #define AUDIOVOX_VENDOR_ID 0x0F3D
74 #define NOVATELWIRELESS_VENDOR_ID 0x1410
75 #define ANYDATA_VENDOR_ID 0x16d5
77 #define OPTION_PRODUCT_OLD 0x5000
78 #define OPTION_PRODUCT_FUSION 0x6000
79 #define OPTION_PRODUCT_FUSION2 0x6300
80 #define OPTION_PRODUCT_COBRA 0x6500
81 #define OPTION_PRODUCT_COBRA2 0x6600
82 #define HUAWEI_PRODUCT_E600 0x1001
83 #define AUDIOVOX_PRODUCT_AIRCARD 0x0112
84 #define NOVATELWIRELESS_PRODUCT_U740 0x1400
85 #define ANYDATA_PRODUCT_ID 0x6501
87 static struct usb_device_id option_ids[] = {
88 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
89 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
90 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
91 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
92 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA2) },
93 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA2) },
94 { USB_DEVICE(OPTION_VENDOR_ID, 0x6701) },
95 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
96 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
97 { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ID) },
98 { } /* Terminating entry */
101 static struct usb_device_id option_ids1[] = {
102 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) },
103 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) },
104 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) },
105 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA) },
106 { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_COBRA2) },
107 { USB_DEVICE(OPTION_VENDOR_ID, 0x6701) },
108 { USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_E600) },
109 { USB_DEVICE(AUDIOVOX_VENDOR_ID, AUDIOVOX_PRODUCT_AIRCARD) },
110 { USB_DEVICE(NOVATELWIRELESS_VENDOR_ID,NOVATELWIRELESS_PRODUCT_U740) },
111 { USB_DEVICE(ANYDATA_VENDOR_ID, ANYDATA_PRODUCT_ID) },
112 { } /* Terminating entry */
115 MODULE_DEVICE_TABLE(usb, option_ids);
117 static struct usb_driver option_driver = {
119 .probe = usb_serial_probe,
120 .disconnect = usb_serial_disconnect,
121 .id_table = option_ids,
122 #if 1 // Add by Jared 03-13-2008
123 .owner = THIS_MODULE,
129 /* The card has three separate interfaces, which the serial driver
130 * recognizes separately, thus num_port=1.
133 static struct usb_serial_device_type option_1port_device = {
136 .owner = THIS_MODULE,
139 .description = "GSM modem (1-port)",
141 .owner = THIS_MODULE,
142 .name = "Option 3G data card",
143 .short_name = "option",
144 .id_table = option_ids1,
145 .num_interrupt_in = NUM_DONT_CARE,
146 .num_bulk_in = NUM_DONT_CARE,
147 .num_bulk_out = NUM_DONT_CARE,
150 .close = option_close,
151 .write = option_write,
152 .write_room = option_write_room,
153 .chars_in_buffer = option_chars_in_buffer,
154 .throttle = option_rx_throttle,
155 .unthrottle = option_rx_unthrottle,
156 .ioctl = option_ioctl,
157 .set_termios = option_set_termios,
158 .break_ctl = option_break_ctl,
159 .tiocmget = option_tiocmget,
160 .tiocmset = option_tiocmset,
161 .attach = option_startup,
162 .shutdown = option_shutdown,
163 .read_int_callback = option_instat_callback,
166 #ifdef CONFIG_USB_DEBUG
172 /* per port private data */
176 #define IN_BUFLEN 4096
177 #define OUT_BUFLEN 128
179 struct option_port_private {
180 /* Input endpoints and buffer for this port */
181 struct urb *in_urbs[N_IN_URB];
182 char in_buffer[N_IN_URB][IN_BUFLEN];
183 /* Output endpoints and buffer for this port */
184 struct urb *out_urbs[N_OUT_URB];
185 char out_buffer[N_OUT_URB][OUT_BUFLEN];
187 /* Settings for the port */
188 int rts_state; /* Handshaking pins (outputs) */
190 int cts_state; /* Handshaking pins (inputs) */
195 unsigned long tx_start_time[N_OUT_URB];
198 /* Functions used by new usb-serial code. */
199 static int __init option_init(void)
202 retval = usb_serial_register(&option_1port_device);
204 goto failed_1port_device_register;
205 retval = usb_register(&option_driver);
207 goto failed_driver_register;
209 info(DRIVER_DESC ": " DRIVER_VERSION);
213 failed_driver_register:
214 usb_serial_deregister (&option_1port_device);
215 failed_1port_device_register:
219 static void __exit option_exit(void)
221 usb_deregister (&option_driver);
222 usb_serial_deregister (&option_1port_device);
225 module_init(option_init);
226 module_exit(option_exit);
228 static void option_rx_throttle(struct usb_serial_port *port)
230 dbg("%s", __FUNCTION__);
233 static void option_rx_unthrottle(struct usb_serial_port *port)
235 dbg("%s", __FUNCTION__);
238 static void option_break_ctl(struct usb_serial_port *port, int break_state)
240 /* Unfortunately, I don't know how to send a break */
241 dbg("%s", __FUNCTION__);
244 static void option_set_termios(struct usb_serial_port *port,
245 struct termios *old_termios)
247 dbg("%s", __FUNCTION__);
249 option_send_setup(port);
252 static int option_tiocmget(struct usb_serial_port *port, struct file *file)
255 struct option_port_private *portdata;
257 portdata = usb_get_serial_port_data(port);
259 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
260 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
261 ((portdata->cts_state) ? TIOCM_CTS : 0) |
262 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
263 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
264 ((portdata->ri_state) ? TIOCM_RNG : 0);
269 static int option_tiocmset(struct usb_serial_port *port, struct file *file,
270 unsigned int set, unsigned int clear)
272 struct option_port_private *portdata;
274 portdata = usb_get_serial_port_data(port);
277 portdata->rts_state = 1;
279 portdata->dtr_state = 1;
281 if (clear & TIOCM_RTS)
282 portdata->rts_state = 0;
283 if (clear & TIOCM_DTR)
284 portdata->dtr_state = 0;
285 return option_send_setup(port);
288 static int option_ioctl(struct usb_serial_port *port, struct file *file,
289 unsigned int cmd, unsigned long arg)
295 static int option_write(struct usb_serial_port *port,
296 const unsigned char *buf, int count)
298 struct option_port_private *portdata;
301 struct urb *this_urb = NULL; /* spurious */
304 portdata = usb_get_serial_port_data(port);
306 dbg("%s: write (%d chars)", __FUNCTION__, count);
310 for (i=0; left > 0 && i < N_OUT_URB; i++) {
312 if (todo > OUT_BUFLEN)
315 this_urb = portdata->out_urbs[i];
316 if (this_urb->status == -EINPROGRESS) {
317 if (time_before(jiffies,
318 portdata->tx_start_time[i] + 10 * HZ))
320 usb_unlink_urb(this_urb);
323 if (this_urb->status != 0)
324 dbg("usb_write %p failed (err=%d)",
325 this_urb, this_urb->status);
327 dbg("%s: endpoint %d buf %d", __FUNCTION__,
328 usb_pipeendpoint(this_urb->pipe), i);
331 memcpy (this_urb->transfer_buffer, buf, todo);
332 this_urb->transfer_buffer_length = todo;
334 this_urb->dev = port->serial->dev;
335 err = usb_submit_urb(this_urb, GFP_ATOMIC);
337 dbg("usb_submit_urb %p (write bulk) failed "
338 "(%d, has %d)", this_urb,
339 err, this_urb->status);
342 portdata->tx_start_time[i] = jiffies;
348 dbg("%s: wrote (did %d)", __FUNCTION__, count);
352 static void option_indat_callback(struct urb *urb, struct pt_regs *regs)
356 struct usb_serial_port *port;
357 struct tty_struct *tty;
358 unsigned char *data = urb->transfer_buffer;
359 #if 1 // Masked by Jared 03-13-2008
363 dbg("%s: %p", __FUNCTION__, urb);
365 endpoint = usb_pipeendpoint(urb->pipe);
366 port = (struct usb_serial_port *) urb->context;
369 dbg("%s: nonzero status: %d on endpoint %02x.",
370 __FUNCTION__, urb->status, endpoint);
373 if (urb->actual_length) {
374 #if 0 // Masked by Jared 03-13-2008
375 tty_buffer_request_room(tty, urb->actual_length);
376 tty_insert_flip_string(tty, data, urb->actual_length);
378 for(i=0; i<urb->actual_length; i++) {
379 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
380 tty_flip_buffer_push(tty);
382 tty_insert_flip_char(tty, data[i], 0);
386 tty_flip_buffer_push(tty);
388 dbg("%s: empty read urb received", __FUNCTION__);
391 /* Resubmit urb so we continue receiving */
392 if (port->open_count && urb->status != -ESHUTDOWN) {
393 err = usb_submit_urb(urb, GFP_ATOMIC);
395 printk(KERN_ERR "%s: resubmit read urb failed. "
396 "(%d)", __FUNCTION__, err);
402 static void option_outdat_callback(struct urb *urb, struct pt_regs *regs)
404 struct usb_serial_port *port;
406 dbg("%s", __FUNCTION__);
408 port = (struct usb_serial_port *) urb->context;
410 usb_serial_port_softint(port);
413 static void option_instat_callback(struct urb *urb, struct pt_regs *regs)
416 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
417 struct option_port_private *portdata = usb_get_serial_port_data(port);
418 struct usb_serial *serial = port->serial;
420 dbg("%s", __FUNCTION__);
421 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
423 if (urb->status == 0) {
424 struct usb_ctrlrequest *req_pkt =
425 (struct usb_ctrlrequest *)urb->transfer_buffer;
428 dbg("%s: NULL req_pkt\n", __FUNCTION__);
431 if ((req_pkt->bRequestType == 0xA1) &&
432 (req_pkt->bRequest == 0x20)) {
434 unsigned char signals = *((unsigned char *)
435 urb->transfer_buffer +
436 sizeof(struct usb_ctrlrequest));
438 dbg("%s: signal x%x", __FUNCTION__, signals);
440 old_dcd_state = portdata->dcd_state;
441 portdata->cts_state = 1;
442 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
443 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
444 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
446 if (port->tty && !C_CLOCAL(port->tty) &&
447 old_dcd_state && !portdata->dcd_state)
448 tty_hangup(port->tty);
450 dbg("%s: type %x req %x", __FUNCTION__,
451 req_pkt->bRequestType,req_pkt->bRequest);
454 dbg("%s: error %d", __FUNCTION__, urb->status);
456 /* Resubmit urb so we continue receiving IRQ data */
457 if (urb->status != -ESHUTDOWN) {
458 urb->dev = serial->dev;
459 err = usb_submit_urb(urb, GFP_ATOMIC);
461 dbg("%s: resubmit intr urb failed. (%d)",
466 static int option_write_room(struct usb_serial_port *port)
468 struct option_port_private *portdata;
471 struct urb *this_urb;
473 portdata = usb_get_serial_port_data(port);
475 for (i=0; i < N_OUT_URB; i++) {
476 this_urb = portdata->out_urbs[i];
477 if (this_urb && this_urb->status != -EINPROGRESS)
478 data_len += OUT_BUFLEN;
481 dbg("%s: %d", __FUNCTION__, data_len);
485 static int option_chars_in_buffer(struct usb_serial_port *port)
487 struct option_port_private *portdata;
490 struct urb *this_urb;
492 portdata = usb_get_serial_port_data(port);
494 for (i=0; i < N_OUT_URB; i++) {
495 this_urb = portdata->out_urbs[i];
496 if (this_urb && this_urb->status == -EINPROGRESS)
497 data_len += this_urb->transfer_buffer_length;
499 dbg("%s: %d", __FUNCTION__, data_len);
503 static int option_open(struct usb_serial_port *port, struct file *filp)
505 struct option_port_private *portdata;
506 struct usb_serial *serial = port->serial;
510 portdata = usb_get_serial_port_data(port);
512 dbg("%s", __FUNCTION__);
514 /* Set some sane defaults */
515 portdata->rts_state = 1;
516 portdata->dtr_state = 1;
518 /* Reset low level data toggle and start reading from endpoints */
519 for (i = 0; i < N_IN_URB; i++) {
520 urb = portdata->in_urbs[i];
523 if (urb->dev != serial->dev) {
524 dbg("%s: dev %p != %p", __FUNCTION__,
525 urb->dev, serial->dev);
530 * make sure endpoint data toggle is synchronized with the
533 usb_clear_halt(urb->dev, urb->pipe);
535 err = usb_submit_urb(urb, GFP_KERNEL);
537 dbg("%s: submit urb %d failed (%d) %d",
538 __FUNCTION__, i, err,
539 urb->transfer_buffer_length);
543 /* Reset low level data toggle on out endpoints */
544 for (i = 0; i < N_OUT_URB; i++) {
545 urb = portdata->out_urbs[i];
548 urb->dev = serial->dev;
549 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
550 usb_pipeout(urb->pipe), 0); */
553 port->tty->low_latency = 1;
555 option_send_setup(port);
560 static inline void stop_urb(struct urb *urb)
562 if (urb && urb->status == -EINPROGRESS)
566 static void option_close(struct usb_serial_port *port, struct file *filp)
569 struct usb_serial *serial = port->serial;
570 struct option_port_private *portdata;
572 dbg("%s", __FUNCTION__);
573 portdata = usb_get_serial_port_data(port);
575 portdata->rts_state = 0;
576 portdata->dtr_state = 0;
579 option_send_setup(port);
581 /* Stop reading/writing urbs */
582 for (i = 0; i < N_IN_URB; i++)
583 stop_urb(portdata->in_urbs[i]);
584 for (i = 0; i < N_OUT_URB; i++)
585 stop_urb(portdata->out_urbs[i]);
590 /* Helper functions used by option_setup_urbs */
591 static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint,
592 int dir, void *ctx, char *buf, int len,
593 void (*callback)(struct urb *, struct pt_regs *regs))
598 return NULL; /* endpoint not needed */
600 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
602 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
606 /* Fill URB using supplied data. */
607 usb_fill_bulk_urb(urb, serial->dev,
608 usb_sndbulkpipe(serial->dev, endpoint) | dir,
609 buf, len, callback, ctx);
615 static void option_setup_urbs(struct usb_serial *serial)
618 struct usb_serial_port *port;
619 struct option_port_private *portdata;
621 dbg("%s", __FUNCTION__);
623 for (i = 0; i < serial->num_ports; i++) {
624 port = serial->port[i];
625 portdata = usb_get_serial_port_data(port);
627 /* Do indat endpoints first */
628 for (j = 0; j < N_IN_URB; ++j) {
629 portdata->in_urbs[j] = option_setup_urb (serial,
630 port->bulk_in_endpointAddress, USB_DIR_IN, port,
631 portdata->in_buffer[j], IN_BUFLEN, option_indat_callback);
634 /* outdat endpoints */
635 for (j = 0; j < N_OUT_URB; ++j) {
636 portdata->out_urbs[j] = option_setup_urb (serial,
637 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
638 portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback);
643 static int option_send_setup(struct usb_serial_port *port)
645 struct usb_serial *serial = port->serial;
646 struct option_port_private *portdata;
648 dbg("%s", __FUNCTION__);
650 portdata = usb_get_serial_port_data(port);
654 if (portdata->dtr_state)
656 if (portdata->rts_state)
659 return usb_control_msg(serial->dev,
660 usb_rcvctrlpipe(serial->dev, 0),
661 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
667 static int option_startup(struct usb_serial *serial)
670 struct usb_serial_port *port;
671 struct option_port_private *portdata;
673 dbg("%s", __FUNCTION__);
675 /* Now setup per port private data */
676 for (i = 0; i < serial->num_ports; i++) {
677 port = serial->port[i];
678 #if 0 // Masked by Jared 03-13-2008
679 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
681 portdata = kmalloc(sizeof(*portdata), GFP_KERNEL);
682 memset(portdata, 0, sizeof(*portdata));
686 dbg("%s: kmalloc for option_port_private (%d) failed!.",
691 usb_set_serial_port_data(port, portdata);
693 if (! port->interrupt_in_urb)
695 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
697 dbg("%s: submit irq_in urb failed %d",
701 option_setup_urbs(serial);
706 static void option_shutdown(struct usb_serial *serial)
709 struct usb_serial_port *port;
710 struct option_port_private *portdata;
712 dbg("%s", __FUNCTION__);
714 /* Stop reading/writing urbs */
715 for (i = 0; i < serial->num_ports; ++i) {
716 port = serial->port[i];
717 portdata = usb_get_serial_port_data(port);
718 for (j = 0; j < N_IN_URB; j++)
719 stop_urb(portdata->in_urbs[j]);
720 for (j = 0; j < N_OUT_URB; j++)
721 stop_urb(portdata->out_urbs[j]);
725 for (i = 0; i < serial->num_ports; ++i) {
726 port = serial->port[i];
727 portdata = usb_get_serial_port_data(port);
729 for (j = 0; j < N_IN_URB; j++) {
730 if (portdata->in_urbs[j]) {
731 usb_free_urb(portdata->in_urbs[j]);
732 portdata->in_urbs[j] = NULL;
735 for (j = 0; j < N_OUT_URB; j++) {
736 if (portdata->out_urbs[j]) {
737 usb_free_urb(portdata->out_urbs[j]);
738 portdata->out_urbs[j] = NULL;
743 /* Now free per port private data */
744 for (i = 0; i < serial->num_ports; i++) {
745 port = serial->port[i];
746 kfree(usb_get_serial_port_data(port));
750 MODULE_AUTHOR(DRIVER_AUTHOR);
751 MODULE_DESCRIPTION(DRIVER_DESC);
752 MODULE_VERSION(DRIVER_VERSION);
753 MODULE_LICENSE("GPL");
755 #ifdef CONFIG_USB_DEBUG
756 module_param(debug, bool, S_IRUGO | S_IWUSR);
757 MODULE_PARM_DESC(debug, "Debug messages");