USB: add another sierra wireless device id
[linux-2.6/linux-loongson.git] / drivers / usb / serial / sierra.c
blob69cc8fb4156de0305460de1a03e14da0a891c09e
1 /*
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>
16 History:
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 */
47 { }
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 */
54 { }
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, 0x6812) }, /* Sierra Wireless MC8775 */
66 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
67 { }
70 static struct usb_driver sierra_driver = {
71 .name = "sierra",
72 .probe = usb_serial_probe,
73 .disconnect = usb_serial_disconnect,
74 .id_table = id_table,
75 .no_dynamic_id = 1,
79 static int debug;
81 /* per port private data */
82 #define N_IN_URB 4
83 #define N_OUT_URB 1
84 #define IN_BUFLEN 4096
85 #define OUT_BUFLEN 128
87 struct sierra_port_private {
88 /* Input endpoints and buffer for this port */
89 struct urb *in_urbs[N_IN_URB];
90 char in_buffer[N_IN_URB][IN_BUFLEN];
91 /* Output endpoints and buffer for this port */
92 struct urb *out_urbs[N_OUT_URB];
93 char out_buffer[N_OUT_URB][OUT_BUFLEN];
95 /* Settings for the port */
96 int rts_state; /* Handshaking pins (outputs) */
97 int dtr_state;
98 int cts_state; /* Handshaking pins (inputs) */
99 int dsr_state;
100 int dcd_state;
101 int ri_state;
103 unsigned long tx_start_time[N_OUT_URB];
106 static int sierra_send_setup(struct usb_serial_port *port)
108 struct usb_serial *serial = port->serial;
109 struct sierra_port_private *portdata;
111 dbg("%s", __FUNCTION__);
113 portdata = usb_get_serial_port_data(port);
115 if (port->tty) {
116 int val = 0;
117 if (portdata->dtr_state)
118 val |= 0x01;
119 if (portdata->rts_state)
120 val |= 0x02;
122 return usb_control_msg(serial->dev,
123 usb_rcvctrlpipe(serial->dev, 0),
124 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
127 return 0;
130 static void sierra_rx_throttle(struct usb_serial_port *port)
132 dbg("%s", __FUNCTION__);
135 static void sierra_rx_unthrottle(struct usb_serial_port *port)
137 dbg("%s", __FUNCTION__);
140 static void sierra_break_ctl(struct usb_serial_port *port, int break_state)
142 /* Unfortunately, I don't know how to send a break */
143 dbg("%s", __FUNCTION__);
146 static void sierra_set_termios(struct usb_serial_port *port,
147 struct termios *old_termios)
149 dbg("%s", __FUNCTION__);
151 sierra_send_setup(port);
154 static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
156 unsigned int value;
157 struct sierra_port_private *portdata;
159 portdata = usb_get_serial_port_data(port);
161 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
162 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
163 ((portdata->cts_state) ? TIOCM_CTS : 0) |
164 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
165 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
166 ((portdata->ri_state) ? TIOCM_RNG : 0);
168 return value;
171 static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
172 unsigned int set, unsigned int clear)
174 struct sierra_port_private *portdata;
176 portdata = usb_get_serial_port_data(port);
178 if (set & TIOCM_RTS)
179 portdata->rts_state = 1;
180 if (set & TIOCM_DTR)
181 portdata->dtr_state = 1;
183 if (clear & TIOCM_RTS)
184 portdata->rts_state = 0;
185 if (clear & TIOCM_DTR)
186 portdata->dtr_state = 0;
187 return sierra_send_setup(port);
190 static int sierra_ioctl(struct usb_serial_port *port, struct file *file,
191 unsigned int cmd, unsigned long arg)
193 return -ENOIOCTLCMD;
196 /* Write */
197 static int sierra_write(struct usb_serial_port *port,
198 const unsigned char *buf, int count)
200 struct sierra_port_private *portdata;
201 int i;
202 int left, todo;
203 struct urb *this_urb = NULL; /* spurious */
204 int err;
206 portdata = usb_get_serial_port_data(port);
208 dbg("%s: write (%d chars)", __FUNCTION__, count);
210 i = 0;
211 left = count;
212 for (i=0; left > 0 && i < N_OUT_URB; i++) {
213 todo = left;
214 if (todo > OUT_BUFLEN)
215 todo = OUT_BUFLEN;
217 this_urb = portdata->out_urbs[i];
218 if (this_urb->status == -EINPROGRESS) {
219 if (time_before(jiffies,
220 portdata->tx_start_time[i] + 10 * HZ))
221 continue;
222 usb_unlink_urb(this_urb);
223 continue;
225 if (this_urb->status != 0)
226 dbg("usb_write %p failed (err=%d)",
227 this_urb, this_urb->status);
229 dbg("%s: endpoint %d buf %d", __FUNCTION__,
230 usb_pipeendpoint(this_urb->pipe), i);
232 /* send the data */
233 memcpy (this_urb->transfer_buffer, buf, todo);
234 this_urb->transfer_buffer_length = todo;
236 this_urb->dev = port->serial->dev;
237 err = usb_submit_urb(this_urb, GFP_ATOMIC);
238 if (err) {
239 dbg("usb_submit_urb %p (write bulk) failed "
240 "(%d, has %d)", this_urb,
241 err, this_urb->status);
242 continue;
244 portdata->tx_start_time[i] = jiffies;
245 buf += todo;
246 left -= todo;
249 count -= left;
250 dbg("%s: wrote (did %d)", __FUNCTION__, count);
251 return count;
254 static void sierra_indat_callback(struct urb *urb)
256 int err;
257 int endpoint;
258 struct usb_serial_port *port;
259 struct tty_struct *tty;
260 unsigned char *data = urb->transfer_buffer;
262 dbg("%s: %p", __FUNCTION__, urb);
264 endpoint = usb_pipeendpoint(urb->pipe);
265 port = (struct usb_serial_port *) urb->context;
267 if (urb->status) {
268 dbg("%s: nonzero status: %d on endpoint %02x.",
269 __FUNCTION__, urb->status, endpoint);
270 } else {
271 tty = port->tty;
272 if (urb->actual_length) {
273 tty_buffer_request_room(tty, urb->actual_length);
274 tty_insert_flip_string(tty, data, urb->actual_length);
275 tty_flip_buffer_push(tty);
276 } else {
277 dbg("%s: empty read urb received", __FUNCTION__);
280 /* Resubmit urb so we continue receiving */
281 if (port->open_count && urb->status != -ESHUTDOWN) {
282 err = usb_submit_urb(urb, GFP_ATOMIC);
283 if (err)
284 printk(KERN_ERR "%s: resubmit read urb failed. "
285 "(%d)", __FUNCTION__, err);
288 return;
291 static void sierra_outdat_callback(struct urb *urb)
293 struct usb_serial_port *port;
295 dbg("%s", __FUNCTION__);
297 port = (struct usb_serial_port *) urb->context;
299 usb_serial_port_softint(port);
302 static void sierra_instat_callback(struct urb *urb)
304 int err;
305 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
306 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
307 struct usb_serial *serial = port->serial;
309 dbg("%s", __FUNCTION__);
310 dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata);
312 if (urb->status == 0) {
313 struct usb_ctrlrequest *req_pkt =
314 (struct usb_ctrlrequest *)urb->transfer_buffer;
316 if (!req_pkt) {
317 dbg("%s: NULL req_pkt\n", __FUNCTION__);
318 return;
320 if ((req_pkt->bRequestType == 0xA1) &&
321 (req_pkt->bRequest == 0x20)) {
322 int old_dcd_state;
323 unsigned char signals = *((unsigned char *)
324 urb->transfer_buffer +
325 sizeof(struct usb_ctrlrequest));
327 dbg("%s: signal x%x", __FUNCTION__, signals);
329 old_dcd_state = portdata->dcd_state;
330 portdata->cts_state = 1;
331 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
332 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
333 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
335 if (port->tty && !C_CLOCAL(port->tty) &&
336 old_dcd_state && !portdata->dcd_state)
337 tty_hangup(port->tty);
338 } else {
339 dbg("%s: type %x req %x", __FUNCTION__,
340 req_pkt->bRequestType,req_pkt->bRequest);
342 } else
343 dbg("%s: error %d", __FUNCTION__, urb->status);
345 /* Resubmit urb so we continue receiving IRQ data */
346 if (urb->status != -ESHUTDOWN) {
347 urb->dev = serial->dev;
348 err = usb_submit_urb(urb, GFP_ATOMIC);
349 if (err)
350 dbg("%s: resubmit intr urb failed. (%d)",
351 __FUNCTION__, err);
355 static int sierra_write_room(struct usb_serial_port *port)
357 struct sierra_port_private *portdata;
358 int i;
359 int data_len = 0;
360 struct urb *this_urb;
362 portdata = usb_get_serial_port_data(port);
364 for (i=0; i < N_OUT_URB; i++) {
365 this_urb = portdata->out_urbs[i];
366 if (this_urb && this_urb->status != -EINPROGRESS)
367 data_len += OUT_BUFLEN;
370 dbg("%s: %d", __FUNCTION__, data_len);
371 return data_len;
374 static int sierra_chars_in_buffer(struct usb_serial_port *port)
376 struct sierra_port_private *portdata;
377 int i;
378 int data_len = 0;
379 struct urb *this_urb;
381 portdata = usb_get_serial_port_data(port);
383 for (i=0; i < N_OUT_URB; i++) {
384 this_urb = portdata->out_urbs[i];
385 if (this_urb && this_urb->status == -EINPROGRESS)
386 data_len += this_urb->transfer_buffer_length;
388 dbg("%s: %d", __FUNCTION__, data_len);
389 return data_len;
392 static int sierra_open(struct usb_serial_port *port, struct file *filp)
394 struct sierra_port_private *portdata;
395 struct usb_serial *serial = port->serial;
396 int i, err;
397 struct urb *urb;
399 portdata = usb_get_serial_port_data(port);
401 dbg("%s", __FUNCTION__);
403 /* Set some sane defaults */
404 portdata->rts_state = 1;
405 portdata->dtr_state = 1;
407 /* Reset low level data toggle and start reading from endpoints */
408 for (i = 0; i < N_IN_URB; i++) {
409 urb = portdata->in_urbs[i];
410 if (! urb)
411 continue;
412 if (urb->dev != serial->dev) {
413 dbg("%s: dev %p != %p", __FUNCTION__,
414 urb->dev, serial->dev);
415 continue;
419 * make sure endpoint data toggle is synchronized with the
420 * device
422 usb_clear_halt(urb->dev, urb->pipe);
424 err = usb_submit_urb(urb, GFP_KERNEL);
425 if (err) {
426 dbg("%s: submit urb %d failed (%d) %d",
427 __FUNCTION__, i, err,
428 urb->transfer_buffer_length);
432 /* Reset low level data toggle on out endpoints */
433 for (i = 0; i < N_OUT_URB; i++) {
434 urb = portdata->out_urbs[i];
435 if (! urb)
436 continue;
437 urb->dev = serial->dev;
438 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
439 usb_pipeout(urb->pipe), 0); */
442 port->tty->low_latency = 1;
444 sierra_send_setup(port);
446 return (0);
449 static inline void stop_urb(struct urb *urb)
451 if (urb && urb->status == -EINPROGRESS)
452 usb_kill_urb(urb);
455 static void sierra_close(struct usb_serial_port *port, struct file *filp)
457 int i;
458 struct usb_serial *serial = port->serial;
459 struct sierra_port_private *portdata;
461 dbg("%s", __FUNCTION__);
462 portdata = usb_get_serial_port_data(port);
464 portdata->rts_state = 0;
465 portdata->dtr_state = 0;
467 if (serial->dev) {
468 sierra_send_setup(port);
470 /* Stop reading/writing urbs */
471 for (i = 0; i < N_IN_URB; i++)
472 stop_urb(portdata->in_urbs[i]);
473 for (i = 0; i < N_OUT_URB; i++)
474 stop_urb(portdata->out_urbs[i]);
476 port->tty = NULL;
479 /* Helper functions used by sierra_setup_urbs */
480 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
481 int dir, void *ctx, char *buf, int len,
482 usb_complete_t callback)
484 struct urb *urb;
486 if (endpoint == -1)
487 return NULL; /* endpoint not needed */
489 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
490 if (urb == NULL) {
491 dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint);
492 return NULL;
495 /* Fill URB using supplied data. */
496 usb_fill_bulk_urb(urb, serial->dev,
497 usb_sndbulkpipe(serial->dev, endpoint) | dir,
498 buf, len, callback, ctx);
500 return urb;
503 /* Setup urbs */
504 static void sierra_setup_urbs(struct usb_serial *serial)
506 int i,j;
507 struct usb_serial_port *port;
508 struct sierra_port_private *portdata;
510 dbg("%s", __FUNCTION__);
512 for (i = 0; i < serial->num_ports; i++) {
513 port = serial->port[i];
514 portdata = usb_get_serial_port_data(port);
516 /* Do indat endpoints first */
517 for (j = 0; j < N_IN_URB; ++j) {
518 portdata->in_urbs[j] = sierra_setup_urb (serial,
519 port->bulk_in_endpointAddress, USB_DIR_IN, port,
520 portdata->in_buffer[j], IN_BUFLEN, sierra_indat_callback);
523 /* outdat endpoints */
524 for (j = 0; j < N_OUT_URB; ++j) {
525 portdata->out_urbs[j] = sierra_setup_urb (serial,
526 port->bulk_out_endpointAddress, USB_DIR_OUT, port,
527 portdata->out_buffer[j], OUT_BUFLEN, sierra_outdat_callback);
532 static int sierra_startup(struct usb_serial *serial)
534 int i, err;
535 struct usb_serial_port *port;
536 struct sierra_port_private *portdata;
538 dbg("%s", __FUNCTION__);
540 /* Now setup per port private data */
541 for (i = 0; i < serial->num_ports; i++) {
542 port = serial->port[i];
543 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
544 if (!portdata) {
545 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
546 __FUNCTION__, i);
547 return (1);
550 usb_set_serial_port_data(port, portdata);
552 if (! port->interrupt_in_urb)
553 continue;
554 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
555 if (err)
556 dbg("%s: submit irq_in urb failed %d",
557 __FUNCTION__, err);
560 sierra_setup_urbs(serial);
562 return (0);
565 static void sierra_shutdown(struct usb_serial *serial)
567 int i, j;
568 struct usb_serial_port *port;
569 struct sierra_port_private *portdata;
571 dbg("%s", __FUNCTION__);
573 /* Stop reading/writing urbs */
574 for (i = 0; i < serial->num_ports; ++i) {
575 port = serial->port[i];
576 portdata = usb_get_serial_port_data(port);
577 for (j = 0; j < N_IN_URB; j++)
578 stop_urb(portdata->in_urbs[j]);
579 for (j = 0; j < N_OUT_URB; j++)
580 stop_urb(portdata->out_urbs[j]);
583 /* Now free them */
584 for (i = 0; i < serial->num_ports; ++i) {
585 port = serial->port[i];
586 portdata = usb_get_serial_port_data(port);
588 for (j = 0; j < N_IN_URB; j++) {
589 if (portdata->in_urbs[j]) {
590 usb_free_urb(portdata->in_urbs[j]);
591 portdata->in_urbs[j] = NULL;
594 for (j = 0; j < N_OUT_URB; j++) {
595 if (portdata->out_urbs[j]) {
596 usb_free_urb(portdata->out_urbs[j]);
597 portdata->out_urbs[j] = NULL;
602 /* Now free per port private data */
603 for (i = 0; i < serial->num_ports; i++) {
604 port = serial->port[i];
605 kfree(usb_get_serial_port_data(port));
609 static struct usb_serial_driver sierra_1port_device = {
610 .driver = {
611 .owner = THIS_MODULE,
612 .name = "sierra1",
614 .description = "Sierra USB modem (1 port)",
615 .id_table = id_table_1port,
616 .num_interrupt_in = NUM_DONT_CARE,
617 .num_bulk_in = 1,
618 .num_bulk_out = 1,
619 .num_ports = 1,
620 .open = sierra_open,
621 .close = sierra_close,
622 .write = sierra_write,
623 .write_room = sierra_write_room,
624 .chars_in_buffer = sierra_chars_in_buffer,
625 .throttle = sierra_rx_throttle,
626 .unthrottle = sierra_rx_unthrottle,
627 .ioctl = sierra_ioctl,
628 .set_termios = sierra_set_termios,
629 .break_ctl = sierra_break_ctl,
630 .tiocmget = sierra_tiocmget,
631 .tiocmset = sierra_tiocmset,
632 .attach = sierra_startup,
633 .shutdown = sierra_shutdown,
634 .read_int_callback = sierra_instat_callback,
637 static struct usb_serial_driver sierra_3port_device = {
638 .driver = {
639 .owner = THIS_MODULE,
640 .name = "sierra3",
642 .description = "Sierra USB modem (3 port)",
643 .id_table = id_table_3port,
644 .num_interrupt_in = NUM_DONT_CARE,
645 .num_bulk_in = 3,
646 .num_bulk_out = 3,
647 .num_ports = 3,
648 .open = sierra_open,
649 .close = sierra_close,
650 .write = sierra_write,
651 .write_room = sierra_write_room,
652 .chars_in_buffer = sierra_chars_in_buffer,
653 .throttle = sierra_rx_throttle,
654 .unthrottle = sierra_rx_unthrottle,
655 .ioctl = sierra_ioctl,
656 .set_termios = sierra_set_termios,
657 .break_ctl = sierra_break_ctl,
658 .tiocmget = sierra_tiocmget,
659 .tiocmset = sierra_tiocmset,
660 .attach = sierra_startup,
661 .shutdown = sierra_shutdown,
662 .read_int_callback = sierra_instat_callback,
665 /* Functions used by new usb-serial code. */
666 static int __init sierra_init(void)
668 int retval;
669 retval = usb_serial_register(&sierra_1port_device);
670 if (retval)
671 goto failed_1port_device_register;
672 retval = usb_serial_register(&sierra_3port_device);
673 if (retval)
674 goto failed_3port_device_register;
677 retval = usb_register(&sierra_driver);
678 if (retval)
679 goto failed_driver_register;
681 info(DRIVER_DESC ": " DRIVER_VERSION);
683 return 0;
685 failed_driver_register:
686 usb_serial_deregister(&sierra_3port_device);
687 failed_3port_device_register:
688 usb_serial_deregister(&sierra_1port_device);
689 failed_1port_device_register:
690 return retval;
693 static void __exit sierra_exit(void)
695 usb_deregister (&sierra_driver);
696 usb_serial_deregister(&sierra_1port_device);
697 usb_serial_deregister(&sierra_3port_device);
700 module_init(sierra_init);
701 module_exit(sierra_exit);
703 MODULE_AUTHOR(DRIVER_AUTHOR);
704 MODULE_DESCRIPTION(DRIVER_DESC);
705 MODULE_VERSION(DRIVER_VERSION);
706 MODULE_LICENSE("GPL");
708 #ifdef CONFIG_USB_DEBUG
709 module_param(debug, bool, S_IRUGO | S_IWUSR);
710 MODULE_PARM_DESC(debug, "Debug messages");
711 #endif