2 USB Driver for Sierra Wireless
4 Copyright (C) 2006, 2007, 2008 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.8"
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>
29 #include <linux/usb/ch9.h>
31 #define SWIMS_USB_REQUEST_SetPower 0x00
32 #define SWIMS_USB_REQUEST_SetNmea 0x07
33 #define SWIMS_USB_REQUEST_SetMode 0x0B
34 #define SWIMS_USB_REQUEST_TYPE_VSC_SET 0x40
35 #define SWIMS_SET_MODE_Modem 0x0001
37 /* per port private data */
40 #define IN_BUFLEN 4096
44 static int truinstall = 1;
52 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
55 dev_dbg(&udev->dev, "%s", "SET POWER STATE\n");
56 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
57 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
58 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
59 swiState, /* __u16 value */
61 NULL, /* void *data */
63 USB_CTRL_SET_TIMEOUT); /* int timeout */
67 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
70 dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
71 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
72 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
73 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
74 eSWocMode, /* __u16 value */
75 0x0000, /* __u16 index */
76 NULL, /* void *data */
78 USB_CTRL_SET_TIMEOUT); /* int timeout */
82 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
85 dev_dbg(&udev->dev, "%s", "NMEA Enable sent\n");
86 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
87 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
88 SWIMS_USB_REQUEST_TYPE_VSC_SET, /* __u8 request type */
89 enable, /* __u16 value */
90 0x0000, /* __u16 index */
91 NULL, /* void *data */
93 USB_CTRL_SET_TIMEOUT); /* int timeout */
97 static int sierra_calc_num_ports(struct usb_serial *serial)
100 int *num_ports = usb_get_serial_data(serial);
106 usb_set_serial_data(serial, NULL);
112 static int sierra_probe(struct usb_serial *serial,
113 const struct usb_device_id *id)
116 struct usb_device *udev;
120 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
124 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
127 /* Check if in installer mode */
128 if (truinstall && id->driver_info == DEVICE_INSTALLER) {
129 dev_dbg(&udev->dev, "%s", "FOUND TRU-INSTALL DEVICE(SW)\n");
130 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
131 /* Don't bind to the device when in installer mode */
134 } else if (id->driver_info == DEVICE_1_PORT)
136 else if (ifnum == 0x99)
141 * save off our num_ports info so that we can use it in the
142 * calc_num_ports callback
144 usb_set_serial_data(serial, (void *)num_ports);
149 static struct usb_device_id id_table [] = {
150 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
151 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
152 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
153 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
154 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
155 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
156 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
157 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
158 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
159 { USB_DEVICE(0x1199, 0x0023) }, /* Sierra Wireless AirCard */
161 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
162 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
163 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
164 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
165 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Thinkpad internal) */
166 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
167 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
168 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780*/
169 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781*/
170 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
171 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
172 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
173 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
174 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
175 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
177 { USB_DEVICE(0x1199, 0x6468) }, /* Sierra Wireless MP3G - EVDO */
178 { USB_DEVICE(0x1199, 0x6469) }, /* Sierra Wireless MP3G - UMTS/HSPA */
180 { USB_DEVICE(0x1199, 0x0112), .driver_info = DEVICE_1_PORT }, /* Sierra Wireless AirCard 580 */
181 { USB_DEVICE(0x0F3D, 0x0112), .driver_info = DEVICE_1_PORT }, /* Airprime/Sierra PC 5220 */
183 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
186 MODULE_DEVICE_TABLE(usb, id_table);
188 static struct usb_driver sierra_driver = {
190 .probe = usb_serial_probe,
191 .disconnect = usb_serial_disconnect,
192 .id_table = id_table,
196 struct sierra_port_private {
197 spinlock_t lock; /* lock the structure */
198 int outstanding_urbs; /* number of out urbs in flight */
200 /* Input endpoints and buffers for this port */
201 struct urb *in_urbs[N_IN_URB];
202 char *in_buffer[N_IN_URB];
204 /* Settings for the port */
205 int rts_state; /* Handshaking pins (outputs) */
207 int cts_state; /* Handshaking pins (inputs) */
213 static int sierra_send_setup(struct usb_serial_port *port)
215 struct usb_serial *serial = port->serial;
216 struct sierra_port_private *portdata;
219 dbg("%s", __FUNCTION__);
221 portdata = usb_get_serial_port_data(port);
225 if (portdata->dtr_state)
227 if (portdata->rts_state)
230 /* Determine which port is targeted */
231 if (port->bulk_out_endpointAddress == 2)
233 else if (port->bulk_out_endpointAddress == 4)
235 else if (port->bulk_out_endpointAddress == 5)
238 return usb_control_msg(serial->dev,
239 usb_rcvctrlpipe(serial->dev, 0),
240 0x22, 0x21, val, interface,
241 NULL, 0, USB_CTRL_SET_TIMEOUT);
247 static void sierra_rx_throttle(struct usb_serial_port *port)
249 dbg("%s", __FUNCTION__);
252 static void sierra_rx_unthrottle(struct usb_serial_port *port)
254 dbg("%s", __FUNCTION__);
257 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
259 /* Unfortunately, I don't know how to send a break */
260 dbg("%s", __FUNCTION__);
263 static void sierra_set_termios(struct usb_serial_port *port,
264 struct ktermios *old_termios)
266 dbg("%s", __FUNCTION__);
267 tty_termios_copy_hw(port->tty->termios, old_termios);
268 sierra_send_setup(port);
271 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
274 struct sierra_port_private *portdata;
276 portdata = usb_get_serial_port_data(port);
278 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
279 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
280 ((portdata->cts_state) ? TIOCM_CTS : 0) |
281 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
282 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
283 ((portdata->ri_state) ? TIOCM_RNG : 0);
288 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
289 unsigned int set, unsigned int clear)
291 struct sierra_port_private *portdata;
293 portdata = usb_get_serial_port_data(port);
296 portdata->rts_state = 1;
298 portdata->dtr_state = 1;
300 if (clear & TIOCM_RTS)
301 portdata->rts_state = 0;
302 if (clear & TIOCM_DTR)
303 portdata->dtr_state = 0;
304 return sierra_send_setup(port);
307 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
308 unsigned int cmd, unsigned long arg)
313 static void sierra_outdat_callback(struct urb *urb)
315 struct usb_serial_port *port = urb->context;
316 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
317 int status = urb->status;
320 dbg("%s - port %d", __FUNCTION__, port->number);
322 /* free up the transfer buffer, as usb_free_urb() does not do this */
323 kfree(urb->transfer_buffer);
326 dbg("%s - nonzero write bulk status received: %d",
327 __FUNCTION__, status);
329 spin_lock_irqsave(&portdata->lock, flags);
330 --portdata->outstanding_urbs;
331 spin_unlock_irqrestore(&portdata->lock, flags);
333 usb_serial_port_softint(port);
337 static int sierra_write(struct usb_serial_port *port,
338 const unsigned char *buf, int count)
340 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
341 struct usb_serial *serial = port->serial;
343 unsigned char *buffer;
347 portdata = usb_get_serial_port_data(port);
349 dbg("%s: write (%d chars)", __FUNCTION__, count);
351 spin_lock_irqsave(&portdata->lock, flags);
352 if (portdata->outstanding_urbs > N_OUT_URB) {
353 spin_unlock_irqrestore(&portdata->lock, flags);
354 dbg("%s - write limit hit\n", __FUNCTION__);
357 portdata->outstanding_urbs++;
358 spin_unlock_irqrestore(&portdata->lock, flags);
360 buffer = kmalloc(count, GFP_ATOMIC);
362 dev_err(&port->dev, "out of memory\n");
364 goto error_no_buffer;
367 urb = usb_alloc_urb(0, GFP_ATOMIC);
369 dev_err(&port->dev, "no more free urbs\n");
374 memcpy(buffer, buf, count);
376 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
378 usb_fill_bulk_urb(urb, serial->dev,
379 usb_sndbulkpipe(serial->dev,
380 port->bulk_out_endpointAddress),
381 buffer, count, sierra_outdat_callback, port);
383 /* send it down the pipe */
384 status = usb_submit_urb(urb, GFP_ATOMIC);
386 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
387 "with status = %d\n", __FUNCTION__, status);
392 /* we are done with this urb, so let the host driver
393 * really free it when it is finished with it */
402 spin_lock_irqsave(&portdata->lock, flags);
403 --portdata->outstanding_urbs;
404 spin_unlock_irqrestore(&portdata->lock, flags);
408 static void sierra_indat_callback(struct urb *urb)
412 struct usb_serial_port *port;
413 struct tty_struct *tty;
414 unsigned char *data = urb->transfer_buffer;
415 int status = urb->status;
417 dbg("%s: %p", __FUNCTION__, urb);
419 endpoint = usb_pipeendpoint(urb->pipe);
420 port = (struct usb_serial_port *) urb->context;
423 dbg("%s: nonzero status: %d on endpoint %02x.",
424 __FUNCTION__, status, endpoint);
427 if (urb->actual_length) {
428 tty_buffer_request_room(tty, urb->actual_length);
429 tty_insert_flip_string(tty, data, urb->actual_length);
430 tty_flip_buffer_push(tty);
432 dbg("%s: empty read urb received", __FUNCTION__);
435 /* Resubmit urb so we continue receiving */
436 if (port->open_count && status != -ESHUTDOWN) {
437 err = usb_submit_urb(urb, GFP_ATOMIC);
439 dev_err(&port->dev, "resubmit read urb failed."
446 static void sierra_instat_callback(struct urb *urb)
449 int status = urb->status;
450 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
451 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
452 struct usb_serial *serial = port->serial;
454 dbg("%s", __FUNCTION__);
455 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
458 struct usb_ctrlrequest *req_pkt =
459 (struct usb_ctrlrequest *)urb->transfer_buffer;
462 dbg("%s: NULL req_pkt\n", __FUNCTION__);
465 if ((req_pkt->bRequestType == 0xA1) &&
466 (req_pkt->bRequest == 0x20)) {
468 unsigned char signals = *((unsigned char *)
469 urb->transfer_buffer +
470 sizeof(struct usb_ctrlrequest));
472 dbg("%s: signal x%x", __FUNCTION__, signals);
474 old_dcd_state = portdata->dcd_state;
475 portdata->cts_state = 1;
476 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
477 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
478 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
480 if (port->tty && !C_CLOCAL(port->tty) &&
481 old_dcd_state && !portdata->dcd_state)
482 tty_hangup(port->tty);
484 dbg("%s: type %x req %x", __FUNCTION__,
485 req_pkt->bRequestType,req_pkt->bRequest);
488 dbg("%s: error %d", __FUNCTION__, status);
490 /* Resubmit urb so we continue receiving IRQ data */
491 if (status != -ESHUTDOWN) {
492 urb->dev = serial->dev;
493 err = usb_submit_urb(urb, GFP_ATOMIC);
495 dbg("%s: resubmit intr urb failed. (%d)",
500 static int sierra_write_room(struct usb_serial_port *port)
502 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
505 dbg("%s - port %d", __FUNCTION__, port->number);
507 /* try to give a good number back based on if we have any free urbs at
508 * this point in time */
509 spin_lock_irqsave(&portdata->lock, flags);
510 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
511 spin_unlock_irqrestore(&portdata->lock, flags);
512 dbg("%s - write limit hit\n", __FUNCTION__);
515 spin_unlock_irqrestore(&portdata->lock, flags);
520 static int sierra_chars_in_buffer(struct usb_serial_port *port)
522 dbg("%s - port %d", __FUNCTION__, port->number);
525 * We can't really account for how much data we
526 * have sent out, but hasn't made it through to the
527 * device as we can't see the backend here, so just
528 * tell the tty layer that everything is flushed.
533 static int sierra_open(struct usb_serial_port *port, struct file *filp)
535 struct sierra_port_private *portdata;
536 struct usb_serial *serial = port->serial;
541 portdata = usb_get_serial_port_data(port);
543 dbg("%s", __FUNCTION__);
545 /* Set some sane defaults */
546 portdata->rts_state = 1;
547 portdata->dtr_state = 1;
549 /* Reset low level data toggle and start reading from endpoints */
550 for (i = 0; i < N_IN_URB; i++) {
551 urb = portdata->in_urbs[i];
554 if (urb->dev != serial->dev) {
555 dbg("%s: dev %p != %p", __FUNCTION__,
556 urb->dev, serial->dev);
561 * make sure endpoint data toggle is synchronized with the
564 usb_clear_halt(urb->dev, urb->pipe);
566 result = usb_submit_urb(urb, GFP_KERNEL);
568 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
569 i, result, urb->transfer_buffer_length);
573 port->tty->low_latency = 1;
575 sierra_send_setup(port);
577 /* start up the interrupt endpoint if we have one */
578 if (port->interrupt_in_urb) {
579 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
581 dev_err(&port->dev, "submit irq_in urb failed %d\n",
587 static void sierra_close(struct usb_serial_port *port, struct file *filp)
590 struct usb_serial *serial = port->serial;
591 struct sierra_port_private *portdata;
593 dbg("%s", __FUNCTION__);
594 portdata = usb_get_serial_port_data(port);
596 portdata->rts_state = 0;
597 portdata->dtr_state = 0;
600 mutex_lock(&serial->disc_mutex);
601 if (!serial->disconnected)
602 sierra_send_setup(port);
603 mutex_unlock(&serial->disc_mutex);
605 /* Stop reading/writing urbs */
606 for (i = 0; i < N_IN_URB; i++)
607 usb_kill_urb(portdata->in_urbs[i]);
610 usb_kill_urb(port->interrupt_in_urb);
615 static int sierra_startup(struct usb_serial *serial)
617 struct usb_serial_port *port;
618 struct sierra_port_private *portdata;
623 dbg("%s", __FUNCTION__);
625 /* Set Device mode to D0 */
626 sierra_set_power_state(serial->dev, 0x0000);
628 /* Check NMEA and set */
630 sierra_vsc_set_nmea(serial->dev, 1);
632 /* Now setup per port private data */
633 for (i = 0; i < serial->num_ports; i++) {
634 port = serial->port[i];
635 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
637 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
641 spin_lock_init(&portdata->lock);
642 for (j = 0; j < N_IN_URB; j++) {
643 portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
644 if (!portdata->in_buffer[j]) {
645 for (--j; j >= 0; j--)
646 kfree(portdata->in_buffer[j]);
652 usb_set_serial_port_data(port, portdata);
654 /* initialize the in urbs */
655 for (j = 0; j < N_IN_URB; ++j) {
656 urb = usb_alloc_urb(0, GFP_KERNEL);
658 dbg("%s: alloc for in port failed.",
662 /* Fill URB using supplied data. */
663 usb_fill_bulk_urb(urb, serial->dev,
664 usb_rcvbulkpipe(serial->dev,
665 port->bulk_in_endpointAddress),
666 portdata->in_buffer[j], IN_BUFLEN,
667 sierra_indat_callback, port);
668 portdata->in_urbs[j] = urb;
675 static void sierra_shutdown(struct usb_serial *serial)
678 struct usb_serial_port *port;
679 struct sierra_port_private *portdata;
681 dbg("%s", __FUNCTION__);
683 for (i = 0; i < serial->num_ports; ++i) {
684 port = serial->port[i];
687 portdata = usb_get_serial_port_data(port);
691 for (j = 0; j < N_IN_URB; j++) {
692 usb_kill_urb(portdata->in_urbs[j]);
693 usb_free_urb(portdata->in_urbs[j]);
694 kfree(portdata->in_buffer[j]);
697 usb_set_serial_port_data(port, NULL);
701 static struct usb_serial_driver sierra_device = {
703 .owner = THIS_MODULE,
706 .description = "Sierra USB modem",
707 .id_table = id_table,
708 .usb_driver = &sierra_driver,
709 .num_interrupt_in = NUM_DONT_CARE,
710 .num_bulk_in = NUM_DONT_CARE,
711 .num_bulk_out = NUM_DONT_CARE,
712 .calc_num_ports = sierra_calc_num_ports,
713 .probe = sierra_probe,
715 .close = sierra_close,
716 .write = sierra_write,
717 .write_room = sierra_write_room,
718 .chars_in_buffer = sierra_chars_in_buffer,
719 .throttle = sierra_rx_throttle,
720 .unthrottle = sierra_rx_unthrottle,
721 .ioctl = sierra_ioctl,
722 .set_termios = sierra_set_termios,
723 .break_ctl = sierra_break_ctl,
724 .tiocmget = sierra_tiocmget,
725 .tiocmset = sierra_tiocmset,
726 .attach = sierra_startup,
727 .shutdown = sierra_shutdown,
728 .read_int_callback = sierra_instat_callback,
731 /* Functions used by new usb-serial code. */
732 static int __init sierra_init(void)
735 retval = usb_serial_register(&sierra_device);
737 goto failed_device_register;
740 retval = usb_register(&sierra_driver);
742 goto failed_driver_register;
744 info(DRIVER_DESC ": " DRIVER_VERSION);
748 failed_driver_register:
749 usb_serial_deregister(&sierra_device);
750 failed_device_register:
754 static void __exit sierra_exit(void)
756 usb_deregister (&sierra_driver);
757 usb_serial_deregister(&sierra_device);
760 module_init(sierra_init);
761 module_exit(sierra_exit);
763 MODULE_AUTHOR(DRIVER_AUTHOR);
764 MODULE_DESCRIPTION(DRIVER_DESC);
765 MODULE_VERSION(DRIVER_VERSION);
766 MODULE_LICENSE("GPL");
768 module_param(truinstall, bool, 0);
769 MODULE_PARM_DESC(truinstall, "TRU-Install support");
771 module_param(nmea, bool, 0);
772 MODULE_PARM_DESC(nmea, "NMEA streaming");
774 #ifdef CONFIG_USB_DEBUG
775 module_param(debug, bool, S_IRUGO | S_IWUSR);
776 MODULE_PARM_DESC(debug, "Debug messages");