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 int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
51 dev_dbg(&udev->dev, "%s", "SET POWER STATE");
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 int sierra_set_ms_mode(struct usb_device *udev, __u16 eSocMode)
66 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH");
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 int sierra_probe(struct usb_interface *iface, const struct usb_device_id *id)
81 struct usb_device *udev;
83 udev = usb_get_dev(interface_to_usbdev(iface));
85 /* Check if in installer mode */
86 if (id->driver_info == DEVICE_INSTALLER) {
87 dev_dbg(&udev->dev, "%s", "FOUND DEVICE(SW)\n");
88 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
89 /*We do not want to bind to the device when in installer mode*/
93 return usb_serial_probe(iface, id);
96 static struct usb_device_id id_table [] = {
97 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
98 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
99 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
100 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
101 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
102 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
103 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
104 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
106 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
107 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
108 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
109 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
110 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
111 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
112 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
113 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
114 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
115 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
116 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
118 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
119 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
121 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
124 MODULE_DEVICE_TABLE(usb, id_table);
126 static struct usb_device_id id_table_1port [] = {
127 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
128 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
132 static struct usb_device_id id_table_3port [] = {
133 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
134 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
135 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
136 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
137 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
138 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
139 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
140 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U*/
142 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
143 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
144 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
145 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
146 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
147 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
148 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
149 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
150 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
151 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
152 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
156 static struct usb_driver sierra_driver = {
158 .probe = sierra_probe,
159 .disconnect = usb_serial_disconnect,
160 .id_table = id_table,
165 struct sierra_port_private {
166 spinlock_t lock; /* lock the structure */
167 int outstanding_urbs; /* number of out urbs in flight */
169 /* Input endpoints and buffer for this port */
170 struct urb *in_urbs[N_IN_URB];
171 char in_buffer[N_IN_URB][IN_BUFLEN];
173 /* Settings for the port */
174 int rts_state; /* Handshaking pins (outputs) */
176 int cts_state; /* Handshaking pins (inputs) */
182 static int sierra_send_setup(struct usb_serial_port *port)
184 struct usb_serial *serial = port->serial;
185 struct sierra_port_private *portdata;
187 dbg("%s", __FUNCTION__);
189 portdata = usb_get_serial_port_data(port);
193 if (portdata->dtr_state)
195 if (portdata->rts_state)
198 return usb_control_msg(serial->dev,
199 usb_rcvctrlpipe(serial->dev, 0),
200 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
206 static void sierra_rx_throttle(struct usb_serial_port *port)
208 dbg("%s", __FUNCTION__);
211 static void sierra_rx_unthrottle(struct usb_serial_port *port)
213 dbg("%s", __FUNCTION__);
216 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
218 /* Unfortunately, I don't know how to send a break */
219 dbg("%s", __FUNCTION__);
222 static void sierra_set_termios(struct usb_serial_port *port,
223 struct ktermios *old_termios)
225 dbg("%s", __FUNCTION__);
227 sierra_send_setup(port);
230 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
233 struct sierra_port_private *portdata;
235 portdata = usb_get_serial_port_data(port);
237 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
238 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
239 ((portdata->cts_state) ? TIOCM_CTS : 0) |
240 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
241 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
242 ((portdata->ri_state) ? TIOCM_RNG : 0);
247 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
248 unsigned int set, unsigned int clear)
250 struct sierra_port_private *portdata;
252 portdata = usb_get_serial_port_data(port);
255 portdata->rts_state = 1;
257 portdata->dtr_state = 1;
259 if (clear & TIOCM_RTS)
260 portdata->rts_state = 0;
261 if (clear & TIOCM_DTR)
262 portdata->dtr_state = 0;
263 return sierra_send_setup(port);
266 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
267 unsigned int cmd, unsigned long arg)
272 static void sierra_outdat_callback(struct urb *urb)
274 struct usb_serial_port *port = urb->context;
275 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
276 int status = urb->status;
279 dbg("%s - port %d", __FUNCTION__, port->number);
281 /* free up the transfer buffer, as usb_free_urb() does not do this */
282 kfree(urb->transfer_buffer);
285 dbg("%s - nonzero write bulk status received: %d",
286 __FUNCTION__, status);
288 spin_lock_irqsave(&portdata->lock, flags);
289 --portdata->outstanding_urbs;
290 spin_unlock_irqrestore(&portdata->lock, flags);
292 usb_serial_port_softint(port);
296 static int sierra_write(struct usb_serial_port *port,
297 const unsigned char *buf, int count)
299 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
300 struct usb_serial *serial = port->serial;
302 unsigned char *buffer;
306 portdata = usb_get_serial_port_data(port);
308 dbg("%s: write (%d chars)", __FUNCTION__, count);
310 spin_lock_irqsave(&portdata->lock, flags);
311 if (portdata->outstanding_urbs > N_OUT_URB) {
312 spin_unlock_irqrestore(&portdata->lock, flags);
313 dbg("%s - write limit hit\n", __FUNCTION__);
316 portdata->outstanding_urbs++;
317 spin_unlock_irqrestore(&portdata->lock, flags);
319 buffer = kmalloc(count, GFP_ATOMIC);
321 dev_err(&port->dev, "out of memory\n");
323 goto error_no_buffer;
326 urb = usb_alloc_urb(0, GFP_ATOMIC);
328 dev_err(&port->dev, "no more free urbs\n");
333 memcpy(buffer, buf, count);
335 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
337 usb_fill_bulk_urb(urb, serial->dev,
338 usb_sndbulkpipe(serial->dev,
339 port->bulk_out_endpointAddress),
340 buffer, count, sierra_outdat_callback, port);
342 /* send it down the pipe */
343 status = usb_submit_urb(urb, GFP_ATOMIC);
345 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
346 "with status = %d\n", __FUNCTION__, status);
351 /* we are done with this urb, so let the host driver
352 * really free it when it is finished with it */
361 spin_lock_irqsave(&portdata->lock, flags);
362 --portdata->outstanding_urbs;
363 spin_unlock_irqrestore(&portdata->lock, flags);
367 static void sierra_indat_callback(struct urb *urb)
371 struct usb_serial_port *port;
372 struct tty_struct *tty;
373 unsigned char *data = urb->transfer_buffer;
374 int status = urb->status;
376 dbg("%s: %p", __FUNCTION__, urb);
378 endpoint = usb_pipeendpoint(urb->pipe);
379 port = (struct usb_serial_port *) urb->context;
382 dbg("%s: nonzero status: %d on endpoint %02x.",
383 __FUNCTION__, status, endpoint);
386 if (urb->actual_length) {
387 tty_buffer_request_room(tty, urb->actual_length);
388 tty_insert_flip_string(tty, data, urb->actual_length);
389 tty_flip_buffer_push(tty);
391 dbg("%s: empty read urb received", __FUNCTION__);
394 /* Resubmit urb so we continue receiving */
395 if (port->open_count && status != -ESHUTDOWN) {
396 err = usb_submit_urb(urb, GFP_ATOMIC);
398 dev_err(&port->dev, "resubmit read urb failed."
405 static void sierra_instat_callback(struct urb *urb)
408 int status = urb->status;
409 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
410 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
411 struct usb_serial *serial = port->serial;
413 dbg("%s", __FUNCTION__);
414 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
417 struct usb_ctrlrequest *req_pkt =
418 (struct usb_ctrlrequest *)urb->transfer_buffer;
421 dbg("%s: NULL req_pkt\n", __FUNCTION__);
424 if ((req_pkt->bRequestType == 0xA1) &&
425 (req_pkt->bRequest == 0x20)) {
427 unsigned char signals = *((unsigned char *)
428 urb->transfer_buffer +
429 sizeof(struct usb_ctrlrequest));
431 dbg("%s: signal x%x", __FUNCTION__, signals);
433 old_dcd_state = portdata->dcd_state;
434 portdata->cts_state = 1;
435 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
436 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
437 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
439 if (port->tty && !C_CLOCAL(port->tty) &&
440 old_dcd_state && !portdata->dcd_state)
441 tty_hangup(port->tty);
443 dbg("%s: type %x req %x", __FUNCTION__,
444 req_pkt->bRequestType,req_pkt->bRequest);
447 dbg("%s: error %d", __FUNCTION__, status);
449 /* Resubmit urb so we continue receiving IRQ data */
450 if (status != -ESHUTDOWN) {
451 urb->dev = serial->dev;
452 err = usb_submit_urb(urb, GFP_ATOMIC);
454 dbg("%s: resubmit intr urb failed. (%d)",
459 static int sierra_write_room(struct usb_serial_port *port)
461 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
464 dbg("%s - port %d", __FUNCTION__, port->number);
466 /* try to give a good number back based on if we have any free urbs at
467 * this point in time */
468 spin_lock_irqsave(&portdata->lock, flags);
469 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
470 spin_unlock_irqrestore(&portdata->lock, flags);
471 dbg("%s - write limit hit\n", __FUNCTION__);
474 spin_unlock_irqrestore(&portdata->lock, flags);
479 static int sierra_chars_in_buffer(struct usb_serial_port *port)
481 dbg("%s - port %d", __FUNCTION__, port->number);
484 * We can't really account for how much data we
485 * have sent out, but hasn't made it through to the
486 * device as we can't see the backend here, so just
487 * tell the tty layer that everything is flushed.
492 static int sierra_open(struct usb_serial_port *port, struct file *filp)
494 struct sierra_port_private *portdata;
495 struct usb_serial *serial = port->serial;
500 portdata = usb_get_serial_port_data(port);
502 dbg("%s", __FUNCTION__);
504 /* Set some sane defaults */
505 portdata->rts_state = 1;
506 portdata->dtr_state = 1;
508 /* Reset low level data toggle and start reading from endpoints */
509 for (i = 0; i < N_IN_URB; i++) {
510 urb = portdata->in_urbs[i];
513 if (urb->dev != serial->dev) {
514 dbg("%s: dev %p != %p", __FUNCTION__,
515 urb->dev, serial->dev);
520 * make sure endpoint data toggle is synchronized with the
523 usb_clear_halt(urb->dev, urb->pipe);
525 result = usb_submit_urb(urb, GFP_KERNEL);
527 dev_err(&port->dev, "submit urb %d failed (%d) %d",
528 i, result, urb->transfer_buffer_length);
532 port->tty->low_latency = 1;
534 sierra_send_setup(port);
536 /* start up the interrupt endpoint if we have one */
537 if (port->interrupt_in_urb) {
538 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
540 dev_err(&port->dev, "submit irq_in urb failed %d",
546 static void sierra_close(struct usb_serial_port *port, struct file *filp)
549 struct usb_serial *serial = port->serial;
550 struct sierra_port_private *portdata;
552 dbg("%s", __FUNCTION__);
553 portdata = usb_get_serial_port_data(port);
555 portdata->rts_state = 0;
556 portdata->dtr_state = 0;
559 sierra_send_setup(port);
561 /* Stop reading/writing urbs */
562 for (i = 0; i < N_IN_URB; i++)
563 usb_kill_urb(portdata->in_urbs[i]);
566 usb_kill_urb(port->interrupt_in_urb);
571 static int sierra_startup(struct usb_serial *serial)
573 struct usb_serial_port *port;
574 struct sierra_port_private *portdata;
579 dbg("%s", __FUNCTION__);
581 /*Set Device mode to D0 */
582 sierra_set_power_state(serial->dev, 0x0000);
584 /* Now setup per port private data */
585 for (i = 0; i < serial->num_ports; i++) {
586 port = serial->port[i];
587 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
589 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
593 spin_lock_init(&portdata->lock);
595 usb_set_serial_port_data(port, portdata);
597 /* initialize the in urbs */
598 for (j = 0; j < N_IN_URB; ++j) {
599 urb = usb_alloc_urb(0, GFP_KERNEL);
601 dbg("%s: alloc for in port failed.",
605 /* Fill URB using supplied data. */
606 usb_fill_bulk_urb(urb, serial->dev,
607 usb_rcvbulkpipe(serial->dev,
608 port->bulk_in_endpointAddress),
609 portdata->in_buffer[j], IN_BUFLEN,
610 sierra_indat_callback, port);
611 portdata->in_urbs[j] = urb;
618 static void sierra_shutdown(struct usb_serial *serial)
621 struct usb_serial_port *port;
622 struct sierra_port_private *portdata;
624 dbg("%s", __FUNCTION__);
626 for (i = 0; i < serial->num_ports; ++i) {
627 port = serial->port[i];
630 portdata = usb_get_serial_port_data(port);
634 for (j = 0; j < N_IN_URB; j++) {
635 usb_kill_urb(portdata->in_urbs[j]);
636 usb_free_urb(portdata->in_urbs[j]);
637 portdata->in_urbs[j] = NULL;
640 usb_set_serial_port_data(port, NULL);
644 static struct usb_serial_driver sierra_1port_device = {
646 .owner = THIS_MODULE,
649 .description = "Sierra USB modem (1 port)",
650 .id_table = id_table_1port,
651 .usb_driver = &sierra_driver,
652 .num_interrupt_in = NUM_DONT_CARE,
657 .close = sierra_close,
658 .write = sierra_write,
659 .write_room = sierra_write_room,
660 .chars_in_buffer = sierra_chars_in_buffer,
661 .throttle = sierra_rx_throttle,
662 .unthrottle = sierra_rx_unthrottle,
663 .ioctl = sierra_ioctl,
664 .set_termios = sierra_set_termios,
665 .break_ctl = sierra_break_ctl,
666 .tiocmget = sierra_tiocmget,
667 .tiocmset = sierra_tiocmset,
668 .attach = sierra_startup,
669 .shutdown = sierra_shutdown,
670 .read_int_callback = sierra_instat_callback,
673 static struct usb_serial_driver sierra_3port_device = {
675 .owner = THIS_MODULE,
678 .description = "Sierra USB modem (3 port)",
679 .id_table = id_table_3port,
680 .usb_driver = &sierra_driver,
681 .num_interrupt_in = NUM_DONT_CARE,
686 .close = sierra_close,
687 .write = sierra_write,
688 .write_room = sierra_write_room,
689 .chars_in_buffer = sierra_chars_in_buffer,
690 .throttle = sierra_rx_throttle,
691 .unthrottle = sierra_rx_unthrottle,
692 .ioctl = sierra_ioctl,
693 .set_termios = sierra_set_termios,
694 .break_ctl = sierra_break_ctl,
695 .tiocmget = sierra_tiocmget,
696 .tiocmset = sierra_tiocmset,
697 .attach = sierra_startup,
698 .shutdown = sierra_shutdown,
699 .read_int_callback = sierra_instat_callback,
702 /* Functions used by new usb-serial code. */
703 static int __init sierra_init(void)
706 retval = usb_serial_register(&sierra_1port_device);
708 goto failed_1port_device_register;
709 retval = usb_serial_register(&sierra_3port_device);
711 goto failed_3port_device_register;
714 retval = usb_register(&sierra_driver);
716 goto failed_driver_register;
718 info(DRIVER_DESC ": " DRIVER_VERSION);
722 failed_driver_register:
723 usb_serial_deregister(&sierra_3port_device);
724 failed_3port_device_register:
725 usb_serial_deregister(&sierra_1port_device);
726 failed_1port_device_register:
730 static void __exit sierra_exit(void)
732 usb_deregister (&sierra_driver);
733 usb_serial_deregister(&sierra_1port_device);
734 usb_serial_deregister(&sierra_3port_device);
737 module_init(sierra_init);
738 module_exit(sierra_exit);
740 MODULE_AUTHOR(DRIVER_AUTHOR);
741 MODULE_DESCRIPTION(DRIVER_DESC);
742 MODULE_VERSION(DRIVER_VERSION);
743 MODULE_LICENSE("GPL");
745 #ifdef CONFIG_USB_DEBUG
746 module_param(debug, bool, S_IRUGO | S_IWUSR);
747 MODULE_PARM_DESC(debug, "Debug messages");