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, 0x0021) }, /* Sierra Wireless AirCard 597E */
39 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
40 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
41 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
42 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
43 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
45 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
46 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
49 MODULE_DEVICE_TABLE(usb, id_table);
51 static struct usb_device_id id_table_1port [] = {
52 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
53 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */
57 static struct usb_device_id id_table_3port [] = {
58 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
59 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
60 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
61 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
62 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
63 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
64 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
65 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
66 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
67 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
68 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
72 static struct usb_driver sierra_driver = {
74 .probe = usb_serial_probe,
75 .disconnect = usb_serial_disconnect,
83 /* per port private data */
86 #define IN_BUFLEN 4096
87 #define OUT_BUFLEN 128
89 struct sierra_port_private {
90 /* Input endpoints and buffer for this port */
91 struct urb *in_urbs[N_IN_URB];
92 char in_buffer[N_IN_URB][IN_BUFLEN];
93 /* Output endpoints and buffer for this port */
94 struct urb *out_urbs[N_OUT_URB];
95 char out_buffer[N_OUT_URB][OUT_BUFLEN];
97 /* Settings for the port */
98 int rts_state; /* Handshaking pins (outputs) */
100 int cts_state; /* Handshaking pins (inputs) */
105 unsigned long tx_start_time[N_OUT_URB];
108 static int sierra_send_setup(struct usb_serial_port *port)
110 struct usb_serial *serial = port->serial;
111 struct sierra_port_private *portdata;
113 dbg("%s", __FUNCTION__);
115 portdata = usb_get_serial_port_data(port);
119 if (portdata->dtr_state)
121 if (portdata->rts_state)
124 return usb_control_msg(serial->dev,
125 usb_rcvctrlpipe(serial->dev, 0),
126 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
132 static void sierra_rx_throttle(struct usb_serial_port *port)
134 dbg("%s", __FUNCTION__);
137 static void sierra_rx_unthrottle(struct usb_serial_port *port)
139 dbg("%s", __FUNCTION__);
142 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
144 /* Unfortunately, I don't know how to send a break */
145 dbg("%s", __FUNCTION__);
148 static void sierra_set_termios(struct usb_serial_port *port,
149 struct ktermios *old_termios)
151 dbg("%s", __FUNCTION__);
153 sierra_send_setup(port);
156 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
159 struct sierra_port_private *portdata;
161 portdata = usb_get_serial_port_data(port);
163 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
164 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
165 ((portdata->cts_state) ? TIOCM_CTS : 0) |
166 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
167 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
168 ((portdata->ri_state) ? TIOCM_RNG : 0);
173 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
174 unsigned int set, unsigned int clear)
176 struct sierra_port_private *portdata;
178 portdata = usb_get_serial_port_data(port);
181 portdata->rts_state = 1;
183 portdata->dtr_state = 1;
185 if (clear & TIOCM_RTS)
186 portdata->rts_state = 0;
187 if (clear & TIOCM_DTR)
188 portdata->dtr_state = 0;
189 return sierra_send_setup(port);
192 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
193 unsigned int cmd, unsigned long arg)
199 static int sierra_write(struct usb_serial_port *port,
200 const unsigned char *buf, int count)
202 struct sierra_port_private *portdata;
205 struct urb *this_urb = NULL; /* spurious */
208 portdata = usb_get_serial_port_data(port);
210 dbg("%s: write (%d chars)", __FUNCTION__, count);
214 for (i=0; left > 0 && i < N_OUT_URB; i++) {
216 if (todo > OUT_BUFLEN)
219 this_urb = portdata->out_urbs[i];
220 if (this_urb->status == -EINPROGRESS) {
221 if (time_before(jiffies,
222 portdata->tx_start_time[i] + 10 * HZ))
224 usb_unlink_urb(this_urb);
227 if (this_urb->status != 0)
228 dbg("usb_write %p failed (err=%d)",
229 this_urb, this_urb->status);
231 dbg("%s: endpoint %d buf %d", __FUNCTION__,
232 usb_pipeendpoint(this_urb->pipe), i);
235 memcpy (this_urb->transfer_buffer, buf, todo);
236 this_urb->transfer_buffer_length = todo;
238 this_urb->dev = port->serial->dev;
239 err = usb_submit_urb(this_urb, GFP_ATOMIC);
241 dbg("usb_submit_urb %p (write bulk) failed "
242 "(%d, has %d)", this_urb,
243 err, this_urb->status);
246 portdata->tx_start_time[i] = jiffies;
252 dbg("%s: wrote (did %d)", __FUNCTION__, count);
256 static void sierra_indat_callback(struct urb *urb)
260 struct usb_serial_port *port;
261 struct tty_struct *tty;
262 unsigned char *data = urb->transfer_buffer;
264 dbg("%s: %p", __FUNCTION__, urb);
266 endpoint = usb_pipeendpoint(urb->pipe);
267 port = (struct usb_serial_port *) urb->context;
270 dbg("%s: nonzero status: %d on endpoint %02x.",
271 __FUNCTION__, urb->status, endpoint);
274 if (urb->actual_length) {
275 tty_buffer_request_room(tty, urb->actual_length);
276 tty_insert_flip_string(tty, data, urb->actual_length);
277 tty_flip_buffer_push(tty);
279 dbg("%s: empty read urb received", __FUNCTION__);
282 /* Resubmit urb so we continue receiving */
283 if (port->open_count && urb->status != -ESHUTDOWN) {
284 err = usb_submit_urb(urb, GFP_ATOMIC);
286 printk(KERN_ERR "%s: resubmit read urb failed. "
287 "(%d)", __FUNCTION__, err);
293 static void sierra_outdat_callback(struct urb *urb)
295 struct usb_serial_port *port;
297 dbg("%s", __FUNCTION__);
299 port = (struct usb_serial_port *) urb->context;
301 usb_serial_port_softint(port);
304 static void sierra_instat_callback(struct urb *urb)
307 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
308 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
309 struct usb_serial *serial = port->serial;
311 dbg("%s", __FUNCTION__);
312 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
314 if (urb->status == 0) {
315 struct usb_ctrlrequest *req_pkt =
316 (struct usb_ctrlrequest *)urb->transfer_buffer;
319 dbg("%s: NULL req_pkt\n", __FUNCTION__);
322 if ((req_pkt->bRequestType == 0xA1) &&
323 (req_pkt->bRequest == 0x20)) {
325 unsigned char signals = *((unsigned char *)
326 urb->transfer_buffer +
327 sizeof(struct usb_ctrlrequest));
329 dbg("%s: signal x%x", __FUNCTION__, signals);
331 old_dcd_state = portdata->dcd_state;
332 portdata->cts_state = 1;
333 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
334 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
335 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
337 if (port->tty && !C_CLOCAL(port->tty) &&
338 old_dcd_state && !portdata->dcd_state)
339 tty_hangup(port->tty);
341 dbg("%s: type %x req %x", __FUNCTION__,
342 req_pkt->bRequestType,req_pkt->bRequest);
345 dbg("%s: error %d", __FUNCTION__, urb->status);
347 /* Resubmit urb so we continue receiving IRQ data */
348 if (urb->status != -ESHUTDOWN) {
349 urb->dev = serial->dev;
350 err = usb_submit_urb(urb, GFP_ATOMIC);
352 dbg("%s: resubmit intr urb failed. (%d)",
357 static int sierra_write_room(struct usb_serial_port *port)
359 struct sierra_port_private *portdata;
362 struct urb *this_urb;
364 portdata = usb_get_serial_port_data(port);
366 for (i=0; i < N_OUT_URB; i++) {
367 this_urb = portdata->out_urbs[i];
368 if (this_urb && this_urb->status != -EINPROGRESS)
369 data_len += OUT_BUFLEN;
372 dbg("%s: %d", __FUNCTION__, data_len);
376 static int sierra_chars_in_buffer(struct usb_serial_port *port)
378 struct sierra_port_private *portdata;
381 struct urb *this_urb;
383 portdata = usb_get_serial_port_data(port);
385 for (i=0; i < N_OUT_URB; i++) {
386 this_urb = portdata->out_urbs[i];
387 if (this_urb && this_urb->status == -EINPROGRESS)
388 data_len += this_urb->transfer_buffer_length;
390 dbg("%s: %d", __FUNCTION__, data_len);
394 static int sierra_open(struct usb_serial_port *port, struct file *filp)
396 struct sierra_port_private *portdata;
397 struct usb_serial *serial = port->serial;
401 __u16 set_mode_dzero = 0x0000;
403 portdata = usb_get_serial_port_data(port);
405 dbg("%s", __FUNCTION__);
407 /* Set some sane defaults */
408 portdata->rts_state = 1;
409 portdata->dtr_state = 1;
411 /* Reset low level data toggle and start reading from endpoints */
412 for (i = 0; i < N_IN_URB; i++) {
413 urb = portdata->in_urbs[i];
416 if (urb->dev != serial->dev) {
417 dbg("%s: dev %p != %p", __FUNCTION__,
418 urb->dev, serial->dev);
423 * make sure endpoint data toggle is synchronized with the
426 usb_clear_halt(urb->dev, urb->pipe);
428 err = usb_submit_urb(urb, GFP_KERNEL);
430 dbg("%s: submit urb %d failed (%d) %d",
431 __FUNCTION__, i, err,
432 urb->transfer_buffer_length);
436 /* Reset low level data toggle on out endpoints */
437 for (i = 0; i < N_OUT_URB; i++) {
438 urb = portdata->out_urbs[i];
441 urb->dev = serial->dev;
442 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
443 usb_pipeout(urb->pipe), 0); */
446 port->tty->low_latency = 1;
449 result = usb_control_msg(serial->dev,
450 usb_rcvctrlpipe(serial->dev, 0),
451 0x00, 0x40, set_mode_dzero, 0, NULL,
452 0, USB_CTRL_SET_TIMEOUT);
454 sierra_send_setup(port);
459 static inline void stop_urb(struct urb *urb)
461 if (urb && urb->status == -EINPROGRESS)
465 static void sierra_close(struct usb_serial_port *port, struct file *filp)
468 struct usb_serial *serial = port->serial;
469 struct sierra_port_private *portdata;
471 dbg("%s", __FUNCTION__);
472 portdata = usb_get_serial_port_data(port);
474 portdata->rts_state = 0;
475 portdata->dtr_state = 0;
478 sierra_send_setup(port);
480 /* Stop reading/writing urbs */
481 for (i = 0; i < N_IN_URB; i++)
482 stop_urb(portdata->in_urbs[i]);
483 for (i = 0; i < N_OUT_URB; i++)
484 stop_urb(portdata->out_urbs[i]);
489 /* Helper functions used by sierra_setup_urbs */
490 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
491 int dir, void *ctx, char *buf, int len,
492 usb_complete_t callback)
497 return NULL; /* endpoint not needed */
499 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
501 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
505 /* Fill URB using supplied data. */
506 usb_fill_bulk_urb(urb, serial->dev,
507 usb_sndbulkpipe(serial->dev, endpoint) | dir,
508 buf, len, callback, ctx);
514 static void sierra_setup_urbs(struct usb_serial *serial)
517 struct usb_serial_port *port;
518 struct sierra_port_private *portdata;
520 dbg("%s", __FUNCTION__);
522 for (i = 0; i < serial->num_ports; i++) {
523 port = serial->port[i];
524 portdata = usb_get_serial_port_data(port);
526 /* Do indat endpoints first */
527 for (j = 0; j < N_IN_URB; ++j) {
528 portdata->in_urbs[j] = sierra_setup_urb (serial,
529 port->bulk_in_endpointAddress, USB_DIR_IN, port,
530 portdata->in_buffer[j], IN_BUFLEN, sierra_indat_callback);
533 /* outdat endpoints */
534 for (j = 0; j < N_OUT_URB; ++j) {
535 portdata->out_urbs[j] = sierra_setup_urb (serial,
536 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
537 portdata->out_buffer[j], OUT_BUFLEN, sierra_outdat_callback);
542 static int sierra_startup(struct usb_serial *serial)
545 struct usb_serial_port *port;
546 struct sierra_port_private *portdata;
548 dbg("%s", __FUNCTION__);
550 /* Now setup per port private data */
551 for (i = 0; i < serial->num_ports; i++) {
552 port = serial->port[i];
553 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
555 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
560 usb_set_serial_port_data(port, portdata);
562 if (! port->interrupt_in_urb)
564 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
566 dbg("%s: submit irq_in urb failed %d",
570 sierra_setup_urbs(serial);
575 static void sierra_shutdown(struct usb_serial *serial)
578 struct usb_serial_port *port;
579 struct sierra_port_private *portdata;
581 dbg("%s", __FUNCTION__);
583 /* Stop reading/writing urbs */
584 for (i = 0; i < serial->num_ports; ++i) {
585 port = serial->port[i];
586 portdata = usb_get_serial_port_data(port);
587 for (j = 0; j < N_IN_URB; j++)
588 stop_urb(portdata->in_urbs[j]);
589 for (j = 0; j < N_OUT_URB; j++)
590 stop_urb(portdata->out_urbs[j]);
594 for (i = 0; i < serial->num_ports; ++i) {
595 port = serial->port[i];
596 portdata = usb_get_serial_port_data(port);
598 for (j = 0; j < N_IN_URB; j++) {
599 if (portdata->in_urbs[j]) {
600 usb_free_urb(portdata->in_urbs[j]);
601 portdata->in_urbs[j] = NULL;
604 for (j = 0; j < N_OUT_URB; j++) {
605 if (portdata->out_urbs[j]) {
606 usb_free_urb(portdata->out_urbs[j]);
607 portdata->out_urbs[j] = NULL;
612 /* Now free per port private data */
613 for (i = 0; i < serial->num_ports; i++) {
614 port = serial->port[i];
615 kfree(usb_get_serial_port_data(port));
619 static struct usb_serial_driver sierra_1port_device = {
621 .owner = THIS_MODULE,
624 .description = "Sierra USB modem (1 port)",
625 .id_table = id_table_1port,
626 .usb_driver = &sierra_driver,
627 .num_interrupt_in = NUM_DONT_CARE,
632 .close = sierra_close,
633 .write = sierra_write,
634 .write_room = sierra_write_room,
635 .chars_in_buffer = sierra_chars_in_buffer,
636 .throttle = sierra_rx_throttle,
637 .unthrottle = sierra_rx_unthrottle,
638 .ioctl = sierra_ioctl,
639 .set_termios = sierra_set_termios,
640 .break_ctl = sierra_break_ctl,
641 .tiocmget = sierra_tiocmget,
642 .tiocmset = sierra_tiocmset,
643 .attach = sierra_startup,
644 .shutdown = sierra_shutdown,
645 .read_int_callback = sierra_instat_callback,
648 static struct usb_serial_driver sierra_3port_device = {
650 .owner = THIS_MODULE,
653 .description = "Sierra USB modem (3 port)",
654 .id_table = id_table_3port,
655 .usb_driver = &sierra_driver,
656 .num_interrupt_in = NUM_DONT_CARE,
661 .close = sierra_close,
662 .write = sierra_write,
663 .write_room = sierra_write_room,
664 .chars_in_buffer = sierra_chars_in_buffer,
665 .throttle = sierra_rx_throttle,
666 .unthrottle = sierra_rx_unthrottle,
667 .ioctl = sierra_ioctl,
668 .set_termios = sierra_set_termios,
669 .break_ctl = sierra_break_ctl,
670 .tiocmget = sierra_tiocmget,
671 .tiocmset = sierra_tiocmset,
672 .attach = sierra_startup,
673 .shutdown = sierra_shutdown,
674 .read_int_callback = sierra_instat_callback,
677 /* Functions used by new usb-serial code. */
678 static int __init sierra_init(void)
681 retval = usb_serial_register(&sierra_1port_device);
683 goto failed_1port_device_register;
684 retval = usb_serial_register(&sierra_3port_device);
686 goto failed_3port_device_register;
689 retval = usb_register(&sierra_driver);
691 goto failed_driver_register;
693 info(DRIVER_DESC ": " DRIVER_VERSION);
697 failed_driver_register:
698 usb_serial_deregister(&sierra_3port_device);
699 failed_3port_device_register:
700 usb_serial_deregister(&sierra_1port_device);
701 failed_1port_device_register:
705 static void __exit sierra_exit(void)
707 usb_deregister (&sierra_driver);
708 usb_serial_deregister(&sierra_1port_device);
709 usb_serial_deregister(&sierra_3port_device);
712 module_init(sierra_init);
713 module_exit(sierra_exit);
715 MODULE_AUTHOR(DRIVER_AUTHOR);
716 MODULE_DESCRIPTION(DRIVER_DESC);
717 MODULE_VERSION(DRIVER_VERSION);
718 MODULE_LICENSE("GPL");
720 #ifdef CONFIG_USB_DEBUG
721 module_param(debug, bool, S_IRUGO | S_IWUSR);
722 MODULE_PARM_DESC(debug, "Debug messages");