2 USB Driver for Sierra Wireless
4 Copyright (C) 2006, 2007 Kevin Lloyd <linux@sierrawireless.com>
6 IMPORTANT DISCLAIMER: This driver is not commercially supported by
7 Sierra Wireless. Use at your own risk.
9 This driver is free software; you can redistribute it and/or modify
10 it under the terms of Version 2 of the GNU General Public License as
11 published by the Free Software Foundation.
13 Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
14 Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
17 #define DRIVER_VERSION "v.1.2.5b"
18 #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
19 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
21 #include <linux/kernel.h>
22 #include <linux/jiffies.h>
23 #include <linux/errno.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/usb.h>
28 #include <linux/usb/serial.h>
30 #define SWIMS_USB_REQUEST_SetMode 0x0B
31 #define SWIMS_USB_REQUEST_TYPE_SetMode 0x40
32 #define SWIMS_USB_INDEX_SetMode 0x0000
33 #define SWIMS_SET_MODE_Modem 0x0001
35 /* per port private data */
38 #define IN_BUFLEN 4096
48 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
51 dev_dbg(&udev->dev, "%s", "SET POWER STATE\n");
52 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
53 0x00, /* __u8 request */
54 0x40, /* __u8 request type */
55 swiState, /* __u16 value */
57 NULL, /* void *data */
59 USB_CTRL_SET_TIMEOUT); /* int timeout */
63 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSocMode)
66 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
67 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
68 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
69 SWIMS_USB_REQUEST_TYPE_SetMode, /* __u8 request type */
70 eSocMode, /* __u16 value */
71 SWIMS_USB_INDEX_SetMode, /* __u16 index */
72 NULL, /* void *data */
74 USB_CTRL_SET_TIMEOUT); /* int timeout */
78 static int sierra_probe(struct usb_interface *iface,
79 const struct usb_device_id *id)
82 struct usb_device *udev;
84 udev = usb_get_dev(interface_to_usbdev(iface));
86 /* Check if in installer mode */
87 if (id->driver_info == DEVICE_INSTALLER) {
88 dev_dbg(&udev->dev, "%s", "FOUND DEVICE(SW)\n");
89 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
90 /*We do not want to bind to the device when in installer mode*/
94 return usb_serial_probe(iface, id);
97 static struct usb_device_id id_table [] = {
98 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
99 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
100 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
101 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
102 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
103 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
104 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
105 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
106 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
108 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
109 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
110 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
111 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
112 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
113 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
114 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
115 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
116 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
117 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
118 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
119 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
120 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
122 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
123 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
125 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
128 MODULE_DEVICE_TABLE(usb, id_table);
130 static struct usb_device_id id_table_1port [] = {
131 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
132 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
136 static struct usb_device_id id_table_3port [] = {
137 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
138 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
139 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
140 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
141 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
142 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
143 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
144 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
145 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
147 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
148 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
149 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
150 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
151 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
152 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
153 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
154 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
155 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
156 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
157 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
158 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
159 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
163 static struct usb_driver sierra_driver = {
165 .probe = sierra_probe,
166 .disconnect = usb_serial_disconnect,
167 .id_table = id_table,
172 struct sierra_port_private {
173 spinlock_t lock; /* lock the structure */
174 int outstanding_urbs; /* number of out urbs in flight */
176 /* Input endpoints and buffer for this port */
177 struct urb *in_urbs[N_IN_URB];
178 char in_buffer[N_IN_URB][IN_BUFLEN];
180 /* Settings for the port */
181 int rts_state; /* Handshaking pins (outputs) */
183 int cts_state; /* Handshaking pins (inputs) */
189 static int sierra_send_setup(struct usb_serial_port *port)
191 struct usb_serial *serial = port->serial;
192 struct sierra_port_private *portdata;
194 dbg("%s", __FUNCTION__);
196 portdata = usb_get_serial_port_data(port);
200 if (portdata->dtr_state)
202 if (portdata->rts_state)
205 return usb_control_msg(serial->dev,
206 usb_rcvctrlpipe(serial->dev, 0),
207 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
213 static void sierra_rx_throttle(struct usb_serial_port *port)
215 dbg("%s", __FUNCTION__);
218 static void sierra_rx_unthrottle(struct usb_serial_port *port)
220 dbg("%s", __FUNCTION__);
223 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
225 /* Unfortunately, I don't know how to send a break */
226 dbg("%s", __FUNCTION__);
229 static void sierra_set_termios(struct usb_serial_port *port,
230 struct ktermios *old_termios)
232 dbg("%s", __FUNCTION__);
233 tty_termios_copy_hw(port->tty->termios, old_termios);
234 sierra_send_setup(port);
237 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
240 struct sierra_port_private *portdata;
242 portdata = usb_get_serial_port_data(port);
244 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
245 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
246 ((portdata->cts_state) ? TIOCM_CTS : 0) |
247 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
248 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
249 ((portdata->ri_state) ? TIOCM_RNG : 0);
254 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
255 unsigned int set, unsigned int clear)
257 struct sierra_port_private *portdata;
259 portdata = usb_get_serial_port_data(port);
262 portdata->rts_state = 1;
264 portdata->dtr_state = 1;
266 if (clear & TIOCM_RTS)
267 portdata->rts_state = 0;
268 if (clear & TIOCM_DTR)
269 portdata->dtr_state = 0;
270 return sierra_send_setup(port);
273 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
274 unsigned int cmd, unsigned long arg)
279 static void sierra_outdat_callback(struct urb *urb)
281 struct usb_serial_port *port = urb->context;
282 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
283 int status = urb->status;
286 dbg("%s - port %d", __FUNCTION__, port->number);
288 /* free up the transfer buffer, as usb_free_urb() does not do this */
289 kfree(urb->transfer_buffer);
292 dbg("%s - nonzero write bulk status received: %d",
293 __FUNCTION__, status);
295 spin_lock_irqsave(&portdata->lock, flags);
296 --portdata->outstanding_urbs;
297 spin_unlock_irqrestore(&portdata->lock, flags);
299 usb_serial_port_softint(port);
303 static int sierra_write(struct usb_serial_port *port,
304 const unsigned char *buf, int count)
306 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
307 struct usb_serial *serial = port->serial;
309 unsigned char *buffer;
313 portdata = usb_get_serial_port_data(port);
315 dbg("%s: write (%d chars)", __FUNCTION__, count);
317 spin_lock_irqsave(&portdata->lock, flags);
318 if (portdata->outstanding_urbs > N_OUT_URB) {
319 spin_unlock_irqrestore(&portdata->lock, flags);
320 dbg("%s - write limit hit\n", __FUNCTION__);
323 portdata->outstanding_urbs++;
324 spin_unlock_irqrestore(&portdata->lock, flags);
326 buffer = kmalloc(count, GFP_ATOMIC);
328 dev_err(&port->dev, "out of memory\n");
330 goto error_no_buffer;
333 urb = usb_alloc_urb(0, GFP_ATOMIC);
335 dev_err(&port->dev, "no more free urbs\n");
340 memcpy(buffer, buf, count);
342 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
344 usb_fill_bulk_urb(urb, serial->dev,
345 usb_sndbulkpipe(serial->dev,
346 port->bulk_out_endpointAddress),
347 buffer, count, sierra_outdat_callback, port);
349 /* send it down the pipe */
350 status = usb_submit_urb(urb, GFP_ATOMIC);
352 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
353 "with status = %d\n", __FUNCTION__, status);
358 /* we are done with this urb, so let the host driver
359 * really free it when it is finished with it */
368 spin_lock_irqsave(&portdata->lock, flags);
369 --portdata->outstanding_urbs;
370 spin_unlock_irqrestore(&portdata->lock, flags);
374 static void sierra_indat_callback(struct urb *urb)
378 struct usb_serial_port *port;
379 struct tty_struct *tty;
380 unsigned char *data = urb->transfer_buffer;
381 int status = urb->status;
383 dbg("%s: %p", __FUNCTION__, urb);
385 endpoint = usb_pipeendpoint(urb->pipe);
386 port = (struct usb_serial_port *) urb->context;
389 dbg("%s: nonzero status: %d on endpoint %02x.",
390 __FUNCTION__, status, endpoint);
393 if (urb->actual_length) {
394 tty_buffer_request_room(tty, urb->actual_length);
395 tty_insert_flip_string(tty, data, urb->actual_length);
396 tty_flip_buffer_push(tty);
398 dbg("%s: empty read urb received", __FUNCTION__);
401 /* Resubmit urb so we continue receiving */
402 if (port->open_count && status != -ESHUTDOWN) {
403 err = usb_submit_urb(urb, GFP_ATOMIC);
405 dev_err(&port->dev, "resubmit read urb failed."
412 static void sierra_instat_callback(struct urb *urb)
415 int status = urb->status;
416 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
417 struct sierra_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);
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__, status);
456 /* Resubmit urb so we continue receiving IRQ data */
457 if (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 sierra_write_room(struct usb_serial_port *port)
468 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
471 dbg("%s - port %d", __FUNCTION__, port->number);
473 /* try to give a good number back based on if we have any free urbs at
474 * this point in time */
475 spin_lock_irqsave(&portdata->lock, flags);
476 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
477 spin_unlock_irqrestore(&portdata->lock, flags);
478 dbg("%s - write limit hit\n", __FUNCTION__);
481 spin_unlock_irqrestore(&portdata->lock, flags);
486 static int sierra_chars_in_buffer(struct usb_serial_port *port)
488 dbg("%s - port %d", __FUNCTION__, port->number);
491 * We can't really account for how much data we
492 * have sent out, but hasn't made it through to the
493 * device as we can't see the backend here, so just
494 * tell the tty layer that everything is flushed.
499 static int sierra_open(struct usb_serial_port *port, struct file *filp)
501 struct sierra_port_private *portdata;
502 struct usb_serial *serial = port->serial;
507 portdata = usb_get_serial_port_data(port);
509 dbg("%s", __FUNCTION__);
511 /* Set some sane defaults */
512 portdata->rts_state = 1;
513 portdata->dtr_state = 1;
515 /* Reset low level data toggle and start reading from endpoints */
516 for (i = 0; i < N_IN_URB; i++) {
517 urb = portdata->in_urbs[i];
520 if (urb->dev != serial->dev) {
521 dbg("%s: dev %p != %p", __FUNCTION__,
522 urb->dev, serial->dev);
527 * make sure endpoint data toggle is synchronized with the
530 usb_clear_halt(urb->dev, urb->pipe);
532 result = usb_submit_urb(urb, GFP_KERNEL);
534 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
535 i, result, urb->transfer_buffer_length);
539 port->tty->low_latency = 1;
541 sierra_send_setup(port);
543 /* start up the interrupt endpoint if we have one */
544 if (port->interrupt_in_urb) {
545 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
547 dev_err(&port->dev, "submit irq_in urb failed %d\n",
553 static void sierra_close(struct usb_serial_port *port, struct file *filp)
556 struct usb_serial *serial = port->serial;
557 struct sierra_port_private *portdata;
559 dbg("%s", __FUNCTION__);
560 portdata = usb_get_serial_port_data(port);
562 portdata->rts_state = 0;
563 portdata->dtr_state = 0;
566 sierra_send_setup(port);
568 /* Stop reading/writing urbs */
569 for (i = 0; i < N_IN_URB; i++)
570 usb_kill_urb(portdata->in_urbs[i]);
573 usb_kill_urb(port->interrupt_in_urb);
578 static int sierra_startup(struct usb_serial *serial)
580 struct usb_serial_port *port;
581 struct sierra_port_private *portdata;
586 dbg("%s", __FUNCTION__);
588 /*Set Device mode to D0 */
589 sierra_set_power_state(serial->dev, 0x0000);
591 /* Now setup per port private data */
592 for (i = 0; i < serial->num_ports; i++) {
593 port = serial->port[i];
594 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
596 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
600 spin_lock_init(&portdata->lock);
602 usb_set_serial_port_data(port, portdata);
604 /* initialize the in urbs */
605 for (j = 0; j < N_IN_URB; ++j) {
606 urb = usb_alloc_urb(0, GFP_KERNEL);
608 dbg("%s: alloc for in port failed.",
612 /* Fill URB using supplied data. */
613 usb_fill_bulk_urb(urb, serial->dev,
614 usb_rcvbulkpipe(serial->dev,
615 port->bulk_in_endpointAddress),
616 portdata->in_buffer[j], IN_BUFLEN,
617 sierra_indat_callback, port);
618 portdata->in_urbs[j] = urb;
625 static void sierra_shutdown(struct usb_serial *serial)
628 struct usb_serial_port *port;
629 struct sierra_port_private *portdata;
631 dbg("%s", __FUNCTION__);
633 for (i = 0; i < serial->num_ports; ++i) {
634 port = serial->port[i];
637 portdata = usb_get_serial_port_data(port);
641 for (j = 0; j < N_IN_URB; j++) {
642 usb_kill_urb(portdata->in_urbs[j]);
643 usb_free_urb(portdata->in_urbs[j]);
644 portdata->in_urbs[j] = NULL;
647 usb_set_serial_port_data(port, NULL);
651 static struct usb_serial_driver sierra_1port_device = {
653 .owner = THIS_MODULE,
656 .description = "Sierra USB modem (1 port)",
657 .id_table = id_table_1port,
658 .usb_driver = &sierra_driver,
659 .num_interrupt_in = NUM_DONT_CARE,
664 .close = sierra_close,
665 .write = sierra_write,
666 .write_room = sierra_write_room,
667 .chars_in_buffer = sierra_chars_in_buffer,
668 .throttle = sierra_rx_throttle,
669 .unthrottle = sierra_rx_unthrottle,
670 .ioctl = sierra_ioctl,
671 .set_termios = sierra_set_termios,
672 .break_ctl = sierra_break_ctl,
673 .tiocmget = sierra_tiocmget,
674 .tiocmset = sierra_tiocmset,
675 .attach = sierra_startup,
676 .shutdown = sierra_shutdown,
677 .read_int_callback = sierra_instat_callback,
680 static struct usb_serial_driver sierra_3port_device = {
682 .owner = THIS_MODULE,
685 .description = "Sierra USB modem (3 port)",
686 .id_table = id_table_3port,
687 .usb_driver = &sierra_driver,
688 .num_interrupt_in = NUM_DONT_CARE,
693 .close = sierra_close,
694 .write = sierra_write,
695 .write_room = sierra_write_room,
696 .chars_in_buffer = sierra_chars_in_buffer,
697 .throttle = sierra_rx_throttle,
698 .unthrottle = sierra_rx_unthrottle,
699 .ioctl = sierra_ioctl,
700 .set_termios = sierra_set_termios,
701 .break_ctl = sierra_break_ctl,
702 .tiocmget = sierra_tiocmget,
703 .tiocmset = sierra_tiocmset,
704 .attach = sierra_startup,
705 .shutdown = sierra_shutdown,
706 .read_int_callback = sierra_instat_callback,
709 /* Functions used by new usb-serial code. */
710 static int __init sierra_init(void)
713 retval = usb_serial_register(&sierra_1port_device);
715 goto failed_1port_device_register;
716 retval = usb_serial_register(&sierra_3port_device);
718 goto failed_3port_device_register;
721 retval = usb_register(&sierra_driver);
723 goto failed_driver_register;
725 info(DRIVER_DESC ": " DRIVER_VERSION);
729 failed_driver_register:
730 usb_serial_deregister(&sierra_3port_device);
731 failed_3port_device_register:
732 usb_serial_deregister(&sierra_1port_device);
733 failed_1port_device_register:
737 static void __exit sierra_exit(void)
739 usb_deregister (&sierra_driver);
740 usb_serial_deregister(&sierra_1port_device);
741 usb_serial_deregister(&sierra_3port_device);
744 module_init(sierra_init);
745 module_exit(sierra_exit);
747 MODULE_AUTHOR(DRIVER_AUTHOR);
748 MODULE_DESCRIPTION(DRIVER_DESC);
749 MODULE_VERSION(DRIVER_VERSION);
750 MODULE_LICENSE("GPL");
752 #ifdef CONFIG_USB_DEBUG
753 module_param(debug, bool, S_IRUGO | S_IWUSR);
754 MODULE_PARM_DESC(debug, "Debug messages");