2 USB Driver for Sierra Wireless
4 Copyright (C) 2006 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>
18 #define DRIVER_VERSION "v.1.0.6"
19 #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
20 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
22 #include <linux/kernel.h>
23 #include <linux/jiffies.h>
24 #include <linux/errno.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/usb.h>
29 #include <linux/usb/serial.h>
32 static struct usb_device_id id_table [] = {
33 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
34 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
35 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
36 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
37 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
38 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
39 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
40 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
41 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
42 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
43 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
44 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
46 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
47 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
50 MODULE_DEVICE_TABLE(usb, id_table);
52 static struct usb_device_id id_table_1port [] = {
53 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
54 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
58 static struct usb_device_id id_table_3port [] = {
59 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
60 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
61 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
62 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
63 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
64 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
65 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
66 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
67 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
68 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
69 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
70 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
74 static struct usb_driver sierra_driver = {
76 .probe = usb_serial_probe,
77 .disconnect = usb_serial_disconnect,
85 /* per port private data */
88 #define IN_BUFLEN 4096
90 struct sierra_port_private {
91 spinlock_t lock; /* lock the structure */
92 int outstanding_urbs; /* number of out urbs in flight */
94 /* Input endpoints and buffer for this port */
95 struct urb *in_urbs[N_IN_URB];
96 char in_buffer[N_IN_URB][IN_BUFLEN];
98 /* Settings for the port */
99 int rts_state; /* Handshaking pins (outputs) */
101 int cts_state; /* Handshaking pins (inputs) */
107 static int sierra_send_setup(struct usb_serial_port *port)
109 struct usb_serial *serial = port->serial;
110 struct sierra_port_private *portdata;
112 dbg("%s", __FUNCTION__);
114 portdata = usb_get_serial_port_data(port);
118 if (portdata->dtr_state)
120 if (portdata->rts_state)
123 return usb_control_msg(serial->dev,
124 usb_rcvctrlpipe(serial->dev, 0),
125 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
131 static void sierra_rx_throttle(struct usb_serial_port *port)
133 dbg("%s", __FUNCTION__);
136 static void sierra_rx_unthrottle(struct usb_serial_port *port)
138 dbg("%s", __FUNCTION__);
141 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
143 /* Unfortunately, I don't know how to send a break */
144 dbg("%s", __FUNCTION__);
147 static void sierra_set_termios(struct usb_serial_port *port,
148 struct ktermios *old_termios)
150 dbg("%s", __FUNCTION__);
152 sierra_send_setup(port);
155 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
158 struct sierra_port_private *portdata;
160 portdata = usb_get_serial_port_data(port);
162 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
163 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
164 ((portdata->cts_state) ? TIOCM_CTS : 0) |
165 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
166 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
167 ((portdata->ri_state) ? TIOCM_RNG : 0);
172 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
173 unsigned int set, unsigned int clear)
175 struct sierra_port_private *portdata;
177 portdata = usb_get_serial_port_data(port);
180 portdata->rts_state = 1;
182 portdata->dtr_state = 1;
184 if (clear & TIOCM_RTS)
185 portdata->rts_state = 0;
186 if (clear & TIOCM_DTR)
187 portdata->dtr_state = 0;
188 return sierra_send_setup(port);
191 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
192 unsigned int cmd, unsigned long arg)
197 static void sierra_outdat_callback(struct urb *urb)
199 struct usb_serial_port *port = urb->context;
200 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
201 int status = urb->status;
204 dbg("%s - port %d", __FUNCTION__, port->number);
206 /* free up the transfer buffer, as usb_free_urb() does not do this */
207 kfree(urb->transfer_buffer);
210 dbg("%s - nonzero write bulk status received: %d",
211 __FUNCTION__, status);
213 spin_lock_irqsave(&portdata->lock, flags);
214 --portdata->outstanding_urbs;
215 spin_unlock_irqrestore(&portdata->lock, flags);
217 usb_serial_port_softint(port);
221 static int sierra_write(struct usb_serial_port *port,
222 const unsigned char *buf, int count)
224 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
225 struct usb_serial *serial = port->serial;
227 unsigned char *buffer;
231 portdata = usb_get_serial_port_data(port);
233 dbg("%s: write (%d chars)", __FUNCTION__, count);
235 spin_lock_irqsave(&portdata->lock, flags);
236 if (portdata->outstanding_urbs > N_OUT_URB) {
237 spin_unlock_irqrestore(&portdata->lock, flags);
238 dbg("%s - write limit hit\n", __FUNCTION__);
241 portdata->outstanding_urbs++;
242 spin_unlock_irqrestore(&portdata->lock, flags);
244 buffer = kmalloc(count, GFP_ATOMIC);
246 dev_err(&port->dev, "out of memory\n");
248 goto error_no_buffer;
251 urb = usb_alloc_urb(0, GFP_ATOMIC);
253 dev_err(&port->dev, "no more free urbs\n");
258 memcpy(buffer, buf, count);
260 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
262 usb_fill_bulk_urb(urb, serial->dev,
263 usb_sndbulkpipe(serial->dev,
264 port->bulk_out_endpointAddress),
265 buffer, count, sierra_outdat_callback, port);
267 /* send it down the pipe */
268 status = usb_submit_urb(urb, GFP_ATOMIC);
270 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
271 "with status = %d\n", __FUNCTION__, status);
276 /* we are done with this urb, so let the host driver
277 * really free it when it is finished with it */
286 spin_lock_irqsave(&portdata->lock, flags);
287 --portdata->outstanding_urbs;
288 spin_unlock_irqrestore(&portdata->lock, flags);
292 static void sierra_indat_callback(struct urb *urb)
296 struct usb_serial_port *port;
297 struct tty_struct *tty;
298 unsigned char *data = urb->transfer_buffer;
299 int status = urb->status;
301 dbg("%s: %p", __FUNCTION__, urb);
303 endpoint = usb_pipeendpoint(urb->pipe);
304 port = (struct usb_serial_port *) urb->context;
307 dbg("%s: nonzero status: %d on endpoint %02x.",
308 __FUNCTION__, status, endpoint);
311 if (urb->actual_length) {
312 tty_buffer_request_room(tty, urb->actual_length);
313 tty_insert_flip_string(tty, data, urb->actual_length);
314 tty_flip_buffer_push(tty);
316 dbg("%s: empty read urb received", __FUNCTION__);
319 /* Resubmit urb so we continue receiving */
320 if (port->open_count && status != -ESHUTDOWN) {
321 err = usb_submit_urb(urb, GFP_ATOMIC);
323 dev_err(&port->dev, "resubmit read urb failed."
330 static void sierra_instat_callback(struct urb *urb)
333 int status = urb->status;
334 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
335 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
336 struct usb_serial *serial = port->serial;
338 dbg("%s", __FUNCTION__);
339 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
342 struct usb_ctrlrequest *req_pkt =
343 (struct usb_ctrlrequest *)urb->transfer_buffer;
346 dbg("%s: NULL req_pkt\n", __FUNCTION__);
349 if ((req_pkt->bRequestType == 0xA1) &&
350 (req_pkt->bRequest == 0x20)) {
352 unsigned char signals = *((unsigned char *)
353 urb->transfer_buffer +
354 sizeof(struct usb_ctrlrequest));
356 dbg("%s: signal x%x", __FUNCTION__, signals);
358 old_dcd_state = portdata->dcd_state;
359 portdata->cts_state = 1;
360 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
361 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
362 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
364 if (port->tty && !C_CLOCAL(port->tty) &&
365 old_dcd_state && !portdata->dcd_state)
366 tty_hangup(port->tty);
368 dbg("%s: type %x req %x", __FUNCTION__,
369 req_pkt->bRequestType,req_pkt->bRequest);
372 dbg("%s: error %d", __FUNCTION__, status);
374 /* Resubmit urb so we continue receiving IRQ data */
375 if (status != -ESHUTDOWN) {
376 urb->dev = serial->dev;
377 err = usb_submit_urb(urb, GFP_ATOMIC);
379 dbg("%s: resubmit intr urb failed. (%d)",
384 static int sierra_write_room(struct usb_serial_port *port)
386 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
389 dbg("%s - port %d", __FUNCTION__, port->number);
391 /* try to give a good number back based on if we have any free urbs at
392 * this point in time */
393 spin_lock_irqsave(&portdata->lock, flags);
394 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
395 spin_unlock_irqrestore(&portdata->lock, flags);
396 dbg("%s - write limit hit\n", __FUNCTION__);
399 spin_unlock_irqrestore(&portdata->lock, flags);
404 static int sierra_chars_in_buffer(struct usb_serial_port *port)
406 dbg("%s - port %d", __FUNCTION__, port->number);
409 * We can't really account for how much data we
410 * have sent out, but hasn't made it through to the
411 * device as we can't see the backend here, so just
412 * tell the tty layer that everything is flushed.
417 static int sierra_open(struct usb_serial_port *port, struct file *filp)
419 struct sierra_port_private *portdata;
420 struct usb_serial *serial = port->serial;
424 __u16 set_mode_dzero = 0x0000;
426 portdata = usb_get_serial_port_data(port);
428 dbg("%s", __FUNCTION__);
430 /* Set some sane defaults */
431 portdata->rts_state = 1;
432 portdata->dtr_state = 1;
434 /* Reset low level data toggle and start reading from endpoints */
435 for (i = 0; i < N_IN_URB; i++) {
436 urb = portdata->in_urbs[i];
439 if (urb->dev != serial->dev) {
440 dbg("%s: dev %p != %p", __FUNCTION__,
441 urb->dev, serial->dev);
446 * make sure endpoint data toggle is synchronized with the
449 usb_clear_halt(urb->dev, urb->pipe);
451 result = usb_submit_urb(urb, GFP_KERNEL);
453 dev_err(&port->dev, "submit urb %d failed (%d) %d",
454 i, result, urb->transfer_buffer_length);
458 port->tty->low_latency = 1;
461 result = usb_control_msg(serial->dev,
462 usb_rcvctrlpipe(serial->dev, 0),
463 0x00, 0x40, set_mode_dzero, 0, NULL,
464 0, USB_CTRL_SET_TIMEOUT);
466 sierra_send_setup(port);
468 /* start up the interrupt endpoint if we have one */
469 if (port->interrupt_in_urb) {
470 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
472 dev_err(&port->dev, "submit irq_in urb failed %d",
478 static void sierra_close(struct usb_serial_port *port, struct file *filp)
481 struct usb_serial *serial = port->serial;
482 struct sierra_port_private *portdata;
484 dbg("%s", __FUNCTION__);
485 portdata = usb_get_serial_port_data(port);
487 portdata->rts_state = 0;
488 portdata->dtr_state = 0;
491 sierra_send_setup(port);
493 /* Stop reading/writing urbs */
494 for (i = 0; i < N_IN_URB; i++)
495 usb_kill_urb(portdata->in_urbs[i]);
498 usb_kill_urb(port->interrupt_in_urb);
503 static int sierra_startup(struct usb_serial *serial)
505 struct usb_serial_port *port;
506 struct sierra_port_private *portdata;
511 dbg("%s", __FUNCTION__);
513 /* Now setup per port private data */
514 for (i = 0; i < serial->num_ports; i++) {
515 port = serial->port[i];
516 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
518 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
522 spin_lock_init(&portdata->lock);
524 usb_set_serial_port_data(port, portdata);
526 /* initialize the in urbs */
527 for (j = 0; j < N_IN_URB; ++j) {
528 urb = usb_alloc_urb(0, GFP_KERNEL);
530 dbg("%s: alloc for in port failed.",
534 /* Fill URB using supplied data. */
535 usb_fill_bulk_urb(urb, serial->dev,
536 usb_rcvbulkpipe(serial->dev,
537 port->bulk_in_endpointAddress),
538 portdata->in_buffer[j], IN_BUFLEN,
539 sierra_indat_callback, port);
540 portdata->in_urbs[j] = urb;
547 static void sierra_shutdown(struct usb_serial *serial)
550 struct usb_serial_port *port;
551 struct sierra_port_private *portdata;
553 dbg("%s", __FUNCTION__);
555 for (i = 0; i < serial->num_ports; ++i) {
556 port = serial->port[i];
559 portdata = usb_get_serial_port_data(port);
563 for (j = 0; j < N_IN_URB; j++) {
564 usb_kill_urb(portdata->in_urbs[j]);
565 usb_free_urb(portdata->in_urbs[j]);
566 portdata->in_urbs[j] = NULL;
569 usb_set_serial_port_data(port, NULL);
573 static struct usb_serial_driver sierra_1port_device = {
575 .owner = THIS_MODULE,
578 .description = "Sierra USB modem (1 port)",
579 .id_table = id_table_1port,
580 .usb_driver = &sierra_driver,
581 .num_interrupt_in = NUM_DONT_CARE,
586 .close = sierra_close,
587 .write = sierra_write,
588 .write_room = sierra_write_room,
589 .chars_in_buffer = sierra_chars_in_buffer,
590 .throttle = sierra_rx_throttle,
591 .unthrottle = sierra_rx_unthrottle,
592 .ioctl = sierra_ioctl,
593 .set_termios = sierra_set_termios,
594 .break_ctl = sierra_break_ctl,
595 .tiocmget = sierra_tiocmget,
596 .tiocmset = sierra_tiocmset,
597 .attach = sierra_startup,
598 .shutdown = sierra_shutdown,
599 .read_int_callback = sierra_instat_callback,
602 static struct usb_serial_driver sierra_3port_device = {
604 .owner = THIS_MODULE,
607 .description = "Sierra USB modem (3 port)",
608 .id_table = id_table_3port,
609 .usb_driver = &sierra_driver,
610 .num_interrupt_in = NUM_DONT_CARE,
615 .close = sierra_close,
616 .write = sierra_write,
617 .write_room = sierra_write_room,
618 .chars_in_buffer = sierra_chars_in_buffer,
619 .throttle = sierra_rx_throttle,
620 .unthrottle = sierra_rx_unthrottle,
621 .ioctl = sierra_ioctl,
622 .set_termios = sierra_set_termios,
623 .break_ctl = sierra_break_ctl,
624 .tiocmget = sierra_tiocmget,
625 .tiocmset = sierra_tiocmset,
626 .attach = sierra_startup,
627 .shutdown = sierra_shutdown,
628 .read_int_callback = sierra_instat_callback,
631 /* Functions used by new usb-serial code. */
632 static int __init sierra_init(void)
635 retval = usb_serial_register(&sierra_1port_device);
637 goto failed_1port_device_register;
638 retval = usb_serial_register(&sierra_3port_device);
640 goto failed_3port_device_register;
643 retval = usb_register(&sierra_driver);
645 goto failed_driver_register;
647 info(DRIVER_DESC ": " DRIVER_VERSION);
651 failed_driver_register:
652 usb_serial_deregister(&sierra_3port_device);
653 failed_3port_device_register:
654 usb_serial_deregister(&sierra_1port_device);
655 failed_1port_device_register:
659 static void __exit sierra_exit(void)
661 usb_deregister (&sierra_driver);
662 usb_serial_deregister(&sierra_1port_device);
663 usb_serial_deregister(&sierra_3port_device);
666 module_init(sierra_init);
667 module_exit(sierra_exit);
669 MODULE_AUTHOR(DRIVER_AUTHOR);
670 MODULE_DESCRIPTION(DRIVER_DESC);
671 MODULE_VERSION(DRIVER_VERSION);
672 MODULE_LICENSE("GPL");
674 #ifdef CONFIG_USB_DEBUG
675 module_param(debug, bool, S_IRUGO | S_IWUSR);
676 MODULE_PARM_DESC(debug, "Debug messages");