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>
19 #define DRIVER_VERSION "v.1.0.5"
20 #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>"
21 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
23 #include <linux/kernel.h>
24 #include <linux/jiffies.h>
25 #include <linux/errno.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/module.h>
29 #include <linux/usb.h>
30 #include <linux/usb/serial.h>
33 static struct usb_device_id id_table
[] = {
34 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
35 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
36 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
37 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
38 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
39 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
40 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
41 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 for Europe */
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, 0x0018) }, /* Sierra Wireless MC5720 */
59 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
60 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
61 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
62 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
63 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
64 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
65 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 for Europe */
66 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
67 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
71 static struct usb_driver sierra_driver
= {
73 .probe
= usb_serial_probe
,
74 .disconnect
= usb_serial_disconnect
,
82 /* per port private data */
85 #define IN_BUFLEN 4096
86 #define OUT_BUFLEN 128
88 struct sierra_port_private
{
89 /* Input endpoints and buffer for this port */
90 struct urb
*in_urbs
[N_IN_URB
];
91 char in_buffer
[N_IN_URB
][IN_BUFLEN
];
92 /* Output endpoints and buffer for this port */
93 struct urb
*out_urbs
[N_OUT_URB
];
94 char out_buffer
[N_OUT_URB
][OUT_BUFLEN
];
96 /* Settings for the port */
97 int rts_state
; /* Handshaking pins (outputs) */
99 int cts_state
; /* Handshaking pins (inputs) */
104 unsigned long tx_start_time
[N_OUT_URB
];
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
)
198 static int sierra_write(struct usb_serial_port
*port
,
199 const unsigned char *buf
, int count
)
201 struct sierra_port_private
*portdata
;
204 struct urb
*this_urb
= NULL
; /* spurious */
207 portdata
= usb_get_serial_port_data(port
);
209 dbg("%s: write (%d chars)", __FUNCTION__
, count
);
213 for (i
=0; left
> 0 && i
< N_OUT_URB
; i
++) {
215 if (todo
> OUT_BUFLEN
)
218 this_urb
= portdata
->out_urbs
[i
];
219 if (this_urb
->status
== -EINPROGRESS
) {
220 if (time_before(jiffies
,
221 portdata
->tx_start_time
[i
] + 10 * HZ
))
223 usb_unlink_urb(this_urb
);
226 if (this_urb
->status
!= 0)
227 dbg("usb_write %p failed (err=%d)",
228 this_urb
, this_urb
->status
);
230 dbg("%s: endpoint %d buf %d", __FUNCTION__
,
231 usb_pipeendpoint(this_urb
->pipe
), i
);
234 memcpy (this_urb
->transfer_buffer
, buf
, todo
);
235 this_urb
->transfer_buffer_length
= todo
;
237 this_urb
->dev
= port
->serial
->dev
;
238 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
240 dbg("usb_submit_urb %p (write bulk) failed "
241 "(%d, has %d)", this_urb
,
242 err
, this_urb
->status
);
245 portdata
->tx_start_time
[i
] = jiffies
;
251 dbg("%s: wrote (did %d)", __FUNCTION__
, count
);
255 static void sierra_indat_callback(struct urb
*urb
)
259 struct usb_serial_port
*port
;
260 struct tty_struct
*tty
;
261 unsigned char *data
= urb
->transfer_buffer
;
263 dbg("%s: %p", __FUNCTION__
, urb
);
265 endpoint
= usb_pipeendpoint(urb
->pipe
);
266 port
= (struct usb_serial_port
*) urb
->context
;
269 dbg("%s: nonzero status: %d on endpoint %02x.",
270 __FUNCTION__
, urb
->status
, endpoint
);
273 if (urb
->actual_length
) {
274 tty_buffer_request_room(tty
, urb
->actual_length
);
275 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
276 tty_flip_buffer_push(tty
);
278 dbg("%s: empty read urb received", __FUNCTION__
);
281 /* Resubmit urb so we continue receiving */
282 if (port
->open_count
&& urb
->status
!= -ESHUTDOWN
) {
283 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
285 printk(KERN_ERR
"%s: resubmit read urb failed. "
286 "(%d)", __FUNCTION__
, err
);
292 static void sierra_outdat_callback(struct urb
*urb
)
294 struct usb_serial_port
*port
;
296 dbg("%s", __FUNCTION__
);
298 port
= (struct usb_serial_port
*) urb
->context
;
300 usb_serial_port_softint(port
);
303 static void sierra_instat_callback(struct urb
*urb
)
306 struct usb_serial_port
*port
= (struct usb_serial_port
*) urb
->context
;
307 struct sierra_port_private
*portdata
= usb_get_serial_port_data(port
);
308 struct usb_serial
*serial
= port
->serial
;
310 dbg("%s", __FUNCTION__
);
311 dbg("%s: urb %p port %p has data %p", __FUNCTION__
,urb
,port
,portdata
);
313 if (urb
->status
== 0) {
314 struct usb_ctrlrequest
*req_pkt
=
315 (struct usb_ctrlrequest
*)urb
->transfer_buffer
;
318 dbg("%s: NULL req_pkt\n", __FUNCTION__
);
321 if ((req_pkt
->bRequestType
== 0xA1) &&
322 (req_pkt
->bRequest
== 0x20)) {
324 unsigned char signals
= *((unsigned char *)
325 urb
->transfer_buffer
+
326 sizeof(struct usb_ctrlrequest
));
328 dbg("%s: signal x%x", __FUNCTION__
, signals
);
330 old_dcd_state
= portdata
->dcd_state
;
331 portdata
->cts_state
= 1;
332 portdata
->dcd_state
= ((signals
& 0x01) ? 1 : 0);
333 portdata
->dsr_state
= ((signals
& 0x02) ? 1 : 0);
334 portdata
->ri_state
= ((signals
& 0x08) ? 1 : 0);
336 if (port
->tty
&& !C_CLOCAL(port
->tty
) &&
337 old_dcd_state
&& !portdata
->dcd_state
)
338 tty_hangup(port
->tty
);
340 dbg("%s: type %x req %x", __FUNCTION__
,
341 req_pkt
->bRequestType
,req_pkt
->bRequest
);
344 dbg("%s: error %d", __FUNCTION__
, urb
->status
);
346 /* Resubmit urb so we continue receiving IRQ data */
347 if (urb
->status
!= -ESHUTDOWN
) {
348 urb
->dev
= serial
->dev
;
349 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
351 dbg("%s: resubmit intr urb failed. (%d)",
356 static int sierra_write_room(struct usb_serial_port
*port
)
358 struct sierra_port_private
*portdata
;
361 struct urb
*this_urb
;
363 portdata
= usb_get_serial_port_data(port
);
365 for (i
=0; i
< N_OUT_URB
; i
++) {
366 this_urb
= portdata
->out_urbs
[i
];
367 if (this_urb
&& this_urb
->status
!= -EINPROGRESS
)
368 data_len
+= OUT_BUFLEN
;
371 dbg("%s: %d", __FUNCTION__
, data_len
);
375 static int sierra_chars_in_buffer(struct usb_serial_port
*port
)
377 struct sierra_port_private
*portdata
;
380 struct urb
*this_urb
;
382 portdata
= usb_get_serial_port_data(port
);
384 for (i
=0; i
< N_OUT_URB
; i
++) {
385 this_urb
= portdata
->out_urbs
[i
];
386 if (this_urb
&& this_urb
->status
== -EINPROGRESS
)
387 data_len
+= this_urb
->transfer_buffer_length
;
389 dbg("%s: %d", __FUNCTION__
, data_len
);
393 static int sierra_open(struct usb_serial_port
*port
, struct file
*filp
)
395 struct sierra_port_private
*portdata
;
396 struct usb_serial
*serial
= port
->serial
;
400 portdata
= usb_get_serial_port_data(port
);
402 dbg("%s", __FUNCTION__
);
404 /* Set some sane defaults */
405 portdata
->rts_state
= 1;
406 portdata
->dtr_state
= 1;
408 /* Reset low level data toggle and start reading from endpoints */
409 for (i
= 0; i
< N_IN_URB
; i
++) {
410 urb
= portdata
->in_urbs
[i
];
413 if (urb
->dev
!= serial
->dev
) {
414 dbg("%s: dev %p != %p", __FUNCTION__
,
415 urb
->dev
, serial
->dev
);
420 * make sure endpoint data toggle is synchronized with the
423 usb_clear_halt(urb
->dev
, urb
->pipe
);
425 err
= usb_submit_urb(urb
, GFP_KERNEL
);
427 dbg("%s: submit urb %d failed (%d) %d",
428 __FUNCTION__
, i
, err
,
429 urb
->transfer_buffer_length
);
433 /* Reset low level data toggle on out endpoints */
434 for (i
= 0; i
< N_OUT_URB
; i
++) {
435 urb
= portdata
->out_urbs
[i
];
438 urb
->dev
= serial
->dev
;
439 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
440 usb_pipeout(urb->pipe), 0); */
443 port
->tty
->low_latency
= 1;
445 sierra_send_setup(port
);
450 static inline void stop_urb(struct urb
*urb
)
452 if (urb
&& urb
->status
== -EINPROGRESS
)
456 static void sierra_close(struct usb_serial_port
*port
, struct file
*filp
)
459 struct usb_serial
*serial
= port
->serial
;
460 struct sierra_port_private
*portdata
;
462 dbg("%s", __FUNCTION__
);
463 portdata
= usb_get_serial_port_data(port
);
465 portdata
->rts_state
= 0;
466 portdata
->dtr_state
= 0;
469 sierra_send_setup(port
);
471 /* Stop reading/writing urbs */
472 for (i
= 0; i
< N_IN_URB
; i
++)
473 stop_urb(portdata
->in_urbs
[i
]);
474 for (i
= 0; i
< N_OUT_URB
; i
++)
475 stop_urb(portdata
->out_urbs
[i
]);
480 /* Helper functions used by sierra_setup_urbs */
481 static struct urb
*sierra_setup_urb(struct usb_serial
*serial
, int endpoint
,
482 int dir
, void *ctx
, char *buf
, int len
,
483 usb_complete_t callback
)
488 return NULL
; /* endpoint not needed */
490 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
492 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__
, endpoint
);
496 /* Fill URB using supplied data. */
497 usb_fill_bulk_urb(urb
, serial
->dev
,
498 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
499 buf
, len
, callback
, ctx
);
505 static void sierra_setup_urbs(struct usb_serial
*serial
)
508 struct usb_serial_port
*port
;
509 struct sierra_port_private
*portdata
;
511 dbg("%s", __FUNCTION__
);
513 for (i
= 0; i
< serial
->num_ports
; i
++) {
514 port
= serial
->port
[i
];
515 portdata
= usb_get_serial_port_data(port
);
517 /* Do indat endpoints first */
518 for (j
= 0; j
< N_IN_URB
; ++j
) {
519 portdata
->in_urbs
[j
] = sierra_setup_urb (serial
,
520 port
->bulk_in_endpointAddress
, USB_DIR_IN
, port
,
521 portdata
->in_buffer
[j
], IN_BUFLEN
, sierra_indat_callback
);
524 /* outdat endpoints */
525 for (j
= 0; j
< N_OUT_URB
; ++j
) {
526 portdata
->out_urbs
[j
] = sierra_setup_urb (serial
,
527 port
->bulk_out_endpointAddress
, USB_DIR_OUT
, port
,
528 portdata
->out_buffer
[j
], OUT_BUFLEN
, sierra_outdat_callback
);
533 static int sierra_startup(struct usb_serial
*serial
)
536 struct usb_serial_port
*port
;
537 struct sierra_port_private
*portdata
;
539 dbg("%s", __FUNCTION__
);
541 /* Now setup per port private data */
542 for (i
= 0; i
< serial
->num_ports
; i
++) {
543 port
= serial
->port
[i
];
544 portdata
= kzalloc(sizeof(*portdata
), GFP_KERNEL
);
546 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
551 usb_set_serial_port_data(port
, portdata
);
553 if (! port
->interrupt_in_urb
)
555 err
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
557 dbg("%s: submit irq_in urb failed %d",
561 sierra_setup_urbs(serial
);
566 static void sierra_shutdown(struct usb_serial
*serial
)
569 struct usb_serial_port
*port
;
570 struct sierra_port_private
*portdata
;
572 dbg("%s", __FUNCTION__
);
574 /* Stop reading/writing urbs */
575 for (i
= 0; i
< serial
->num_ports
; ++i
) {
576 port
= serial
->port
[i
];
577 portdata
= usb_get_serial_port_data(port
);
578 for (j
= 0; j
< N_IN_URB
; j
++)
579 stop_urb(portdata
->in_urbs
[j
]);
580 for (j
= 0; j
< N_OUT_URB
; j
++)
581 stop_urb(portdata
->out_urbs
[j
]);
585 for (i
= 0; i
< serial
->num_ports
; ++i
) {
586 port
= serial
->port
[i
];
587 portdata
= usb_get_serial_port_data(port
);
589 for (j
= 0; j
< N_IN_URB
; j
++) {
590 if (portdata
->in_urbs
[j
]) {
591 usb_free_urb(portdata
->in_urbs
[j
]);
592 portdata
->in_urbs
[j
] = NULL
;
595 for (j
= 0; j
< N_OUT_URB
; j
++) {
596 if (portdata
->out_urbs
[j
]) {
597 usb_free_urb(portdata
->out_urbs
[j
]);
598 portdata
->out_urbs
[j
] = NULL
;
603 /* Now free per port private data */
604 for (i
= 0; i
< serial
->num_ports
; i
++) {
605 port
= serial
->port
[i
];
606 kfree(usb_get_serial_port_data(port
));
610 static struct usb_serial_driver sierra_1port_device
= {
612 .owner
= THIS_MODULE
,
615 .description
= "Sierra USB modem (1 port)",
616 .id_table
= id_table_1port
,
617 .num_interrupt_in
= NUM_DONT_CARE
,
622 .close
= sierra_close
,
623 .write
= sierra_write
,
624 .write_room
= sierra_write_room
,
625 .chars_in_buffer
= sierra_chars_in_buffer
,
626 .throttle
= sierra_rx_throttle
,
627 .unthrottle
= sierra_rx_unthrottle
,
628 .ioctl
= sierra_ioctl
,
629 .set_termios
= sierra_set_termios
,
630 .break_ctl
= sierra_break_ctl
,
631 .tiocmget
= sierra_tiocmget
,
632 .tiocmset
= sierra_tiocmset
,
633 .attach
= sierra_startup
,
634 .shutdown
= sierra_shutdown
,
635 .read_int_callback
= sierra_instat_callback
,
638 static struct usb_serial_driver sierra_3port_device
= {
640 .owner
= THIS_MODULE
,
643 .description
= "Sierra USB modem (3 port)",
644 .id_table
= id_table_3port
,
645 .num_interrupt_in
= NUM_DONT_CARE
,
650 .close
= sierra_close
,
651 .write
= sierra_write
,
652 .write_room
= sierra_write_room
,
653 .chars_in_buffer
= sierra_chars_in_buffer
,
654 .throttle
= sierra_rx_throttle
,
655 .unthrottle
= sierra_rx_unthrottle
,
656 .ioctl
= sierra_ioctl
,
657 .set_termios
= sierra_set_termios
,
658 .break_ctl
= sierra_break_ctl
,
659 .tiocmget
= sierra_tiocmget
,
660 .tiocmset
= sierra_tiocmset
,
661 .attach
= sierra_startup
,
662 .shutdown
= sierra_shutdown
,
663 .read_int_callback
= sierra_instat_callback
,
666 /* Functions used by new usb-serial code. */
667 static int __init
sierra_init(void)
670 retval
= usb_serial_register(&sierra_1port_device
);
672 goto failed_1port_device_register
;
673 retval
= usb_serial_register(&sierra_3port_device
);
675 goto failed_3port_device_register
;
678 retval
= usb_register(&sierra_driver
);
680 goto failed_driver_register
;
682 info(DRIVER_DESC
": " DRIVER_VERSION
);
686 failed_driver_register
:
687 usb_serial_deregister(&sierra_3port_device
);
688 failed_3port_device_register
:
689 usb_serial_deregister(&sierra_1port_device
);
690 failed_1port_device_register
:
694 static void __exit
sierra_exit(void)
696 usb_deregister (&sierra_driver
);
697 usb_serial_deregister(&sierra_1port_device
);
698 usb_serial_deregister(&sierra_3port_device
);
701 module_init(sierra_init
);
702 module_exit(sierra_exit
);
704 MODULE_AUTHOR(DRIVER_AUTHOR
);
705 MODULE_DESCRIPTION(DRIVER_DESC
);
706 MODULE_VERSION(DRIVER_VERSION
);
707 MODULE_LICENSE("GPL");
709 #ifdef CONFIG_USB_DEBUG
710 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
711 MODULE_PARM_DESC(debug
, "Debug messages");