USB Serial Sierra: Dynamic interface detection
[linux-2.6/mini2440.git] / drivers / usb / serial / sierra.c
blobe2a7463346fc8ad3313ae2ba477ecead06941cbe
1 /*
2 USB Driver for Sierra Wireless
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@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.9c"
18 #define DRIVER_AUTHOR "Kevin Lloyd <klloyd@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_SET_MODE_Modem 0x0001
36 /* per port private data */
37 #define N_IN_URB 4
38 #define N_OUT_URB 4
39 #define IN_BUFLEN 4096
41 static int debug;
42 static int nmea;
43 static int truinstall = 1;
45 enum devicetype {
46 DEVICE_3_PORT = 0,
47 DEVICE_1_PORT = 1,
48 DEVICE_INSTALLER = 2,
51 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
53 int result;
54 dev_dbg(&udev->dev, "%s", __func__);
55 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
56 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
57 USB_TYPE_VENDOR, /* __u8 request type */
58 swiState, /* __u16 value */
59 0, /* __u16 index */
60 NULL, /* void *data */
61 0, /* __u16 size */
62 USB_CTRL_SET_TIMEOUT); /* int timeout */
63 return result;
66 static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
68 int result;
69 dev_dbg(&udev->dev, "%s", __func__);
70 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
71 SWIMS_USB_REQUEST_SetMode, /* __u8 request */
72 USB_TYPE_VENDOR, /* __u8 request type */
73 eSWocMode, /* __u16 value */
74 0x0000, /* __u16 index */
75 NULL, /* void *data */
76 0, /* __u16 size */
77 USB_CTRL_SET_TIMEOUT); /* int timeout */
78 return result;
81 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
83 int result;
84 dev_dbg(&udev->dev, "%s", __func__);
85 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
86 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
87 USB_TYPE_VENDOR, /* __u8 request type */
88 enable, /* __u16 value */
89 0x0000, /* __u16 index */
90 NULL, /* void *data */
91 0, /* __u16 size */
92 USB_CTRL_SET_TIMEOUT); /* int timeout */
93 return result;
96 static int sierra_calc_num_ports(struct usb_serial *serial)
98 int result;
99 int *num_ports = usb_get_serial_data(serial);
100 dev_dbg(&serial->dev->dev, "%s", __func__);
102 result = *num_ports;
104 if (result) {
105 kfree(num_ports);
106 usb_set_serial_data(serial, NULL);
109 return result;
112 static int sierra_calc_interface(struct usb_serial *serial)
114 int interface;
115 struct usb_interface *p_interface;
116 struct usb_host_interface *p_host_interface;
117 dev_dbg(&serial->dev->dev, "%s", __func__);
119 /* Get the interface structure pointer from the serial struct */
120 p_interface = serial->interface;
122 /* Get a pointer to the host interface structure */
123 p_host_interface = p_interface->cur_altsetting;
125 /* read the interface descriptor for this active altsetting
126 * to find out the interface number we are on
128 interface = p_host_interface->desc.bInterfaceNumber;
130 return interface;
133 static int sierra_probe(struct usb_serial *serial,
134 const struct usb_device_id *id)
136 int result = 0;
137 struct usb_device *udev;
138 int *num_ports;
139 u8 ifnum;
140 u8 numendpoints;
142 dev_dbg(&serial->dev->dev, "%s", __func__);
144 num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
145 if (!num_ports)
146 return -ENOMEM;
148 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
149 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
150 udev = serial->dev;
152 /* Figure out the interface number from the serial structure */
153 ifnum = sierra_calc_interface(serial);
156 * If this interface supports more than 1 alternate
157 * select the 2nd one
159 if (serial->interface->num_altsetting == 2) {
160 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
161 ifnum);
162 /* We know the alternate setting is 1 for the MC8785 */
163 usb_set_interface(udev, ifnum, 1);
166 /* Check if in installer mode */
167 if (truinstall && id->driver_info == DEVICE_INSTALLER) {
168 dev_dbg(&udev->dev, "%s", "FOUND TRU-INSTALL DEVICE(SW)\n");
169 result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
170 /* Don't bind to the device when in installer mode */
171 kfree(num_ports);
172 return -EIO;
173 /* Dummy interface present on some SKUs should be ignored */
174 } else if (ifnum == 0x99)
175 *num_ports = 0;
176 else if (numendpoints <= 3)
177 *num_ports = 1;
178 else
179 *num_ports = (numendpoints-1)/2;
182 * save off our num_ports info so that we can use it in the
183 * calc_num_ports callback
185 usb_set_serial_data(serial, (void *)num_ports);
187 return result;
190 static struct usb_device_id id_table [] = {
191 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
192 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
193 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
194 { USB_DEVICE(0x0f30, 0x1b1d) }, /* Sierra Wireless MC5720 */
195 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
196 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
197 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
198 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
199 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
200 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
201 /* Sierra Wireless C597 */
202 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
203 /* Sierra Wireless Device */
204 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
205 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
207 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
208 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
209 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
210 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
211 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */
212 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
213 { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */
214 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
215 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
216 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
217 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
218 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
219 { USB_DEVICE(0x1199, 0x683C) }, /* Sierra Wireless MC8790 */
220 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8790 */
221 { USB_DEVICE(0x1199, 0x683E) }, /* Sierra Wireless MC8790 */
222 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
223 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
224 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
225 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
226 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
227 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
228 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
229 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
230 /* Sierra Wireless C885 */
231 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
232 /* Sierra Wireless Device */
233 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
234 /* Sierra Wireless Device */
235 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
237 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
238 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
240 { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
243 MODULE_DEVICE_TABLE(usb, id_table);
245 static struct usb_driver sierra_driver = {
246 .name = "sierra",
247 .probe = usb_serial_probe,
248 .disconnect = usb_serial_disconnect,
249 .id_table = id_table,
250 .no_dynamic_id = 1,
253 struct sierra_port_private {
254 spinlock_t lock; /* lock the structure */
255 int outstanding_urbs; /* number of out urbs in flight */
257 /* Input endpoints and buffers for this port */
258 struct urb *in_urbs[N_IN_URB];
259 char *in_buffer[N_IN_URB];
261 /* Settings for the port */
262 int rts_state; /* Handshaking pins (outputs) */
263 int dtr_state;
264 int cts_state; /* Handshaking pins (inputs) */
265 int dsr_state;
266 int dcd_state;
267 int ri_state;
270 static int sierra_send_setup(struct tty_struct *tty,
271 struct usb_serial_port *port)
273 struct usb_serial *serial = port->serial;
274 struct sierra_port_private *portdata;
275 __u16 interface = 0;
277 dbg("%s", __func__);
279 portdata = usb_get_serial_port_data(port);
281 if (tty) {
282 int val = 0;
283 if (portdata->dtr_state)
284 val |= 0x01;
285 if (portdata->rts_state)
286 val |= 0x02;
288 /* If composite device then properly report interface */
289 if (serial->num_ports == 1)
290 interface = sierra_calc_interface(serial);
292 /* Otherwise the need to do non-composite mapping */
293 else {
294 if (port->bulk_out_endpointAddress == 2)
295 interface = 0;
296 else if (port->bulk_out_endpointAddress == 4)
297 interface = 1;
298 else if (port->bulk_out_endpointAddress == 5)
299 interface = 2;
302 return usb_control_msg(serial->dev,
303 usb_rcvctrlpipe(serial->dev, 0),
304 0x22, 0x21, val, interface,
305 NULL, 0, USB_CTRL_SET_TIMEOUT);
308 return 0;
311 static void sierra_set_termios(struct tty_struct *tty,
312 struct usb_serial_port *port, struct ktermios *old_termios)
314 dbg("%s", __func__);
315 tty_termios_copy_hw(tty->termios, old_termios);
316 sierra_send_setup(tty, port);
319 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
321 struct usb_serial_port *port = tty->driver_data;
322 unsigned int value;
323 struct sierra_port_private *portdata;
325 portdata = usb_get_serial_port_data(port);
327 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
328 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
329 ((portdata->cts_state) ? TIOCM_CTS : 0) |
330 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
331 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
332 ((portdata->ri_state) ? TIOCM_RNG : 0);
334 return value;
337 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
338 unsigned int set, unsigned int clear)
340 struct usb_serial_port *port = tty->driver_data;
341 struct sierra_port_private *portdata;
343 portdata = usb_get_serial_port_data(port);
345 if (set & TIOCM_RTS)
346 portdata->rts_state = 1;
347 if (set & TIOCM_DTR)
348 portdata->dtr_state = 1;
350 if (clear & TIOCM_RTS)
351 portdata->rts_state = 0;
352 if (clear & TIOCM_DTR)
353 portdata->dtr_state = 0;
354 return sierra_send_setup(tty, port);
357 static void sierra_outdat_callback(struct urb *urb)
359 struct usb_serial_port *port = urb->context;
360 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
361 int status = urb->status;
362 unsigned long flags;
364 dbg("%s - port %d", __func__, port->number);
366 /* free up the transfer buffer, as usb_free_urb() does not do this */
367 kfree(urb->transfer_buffer);
369 if (status)
370 dbg("%s - nonzero write bulk status received: %d",
371 __func__, status);
373 spin_lock_irqsave(&portdata->lock, flags);
374 --portdata->outstanding_urbs;
375 spin_unlock_irqrestore(&portdata->lock, flags);
377 usb_serial_port_softint(port);
380 /* Write */
381 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
382 const unsigned char *buf, int count)
384 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
385 struct usb_serial *serial = port->serial;
386 unsigned long flags;
387 unsigned char *buffer;
388 struct urb *urb;
389 int status;
391 portdata = usb_get_serial_port_data(port);
393 dbg("%s: write (%d chars)", __func__, count);
395 spin_lock_irqsave(&portdata->lock, flags);
396 if (portdata->outstanding_urbs > N_OUT_URB) {
397 spin_unlock_irqrestore(&portdata->lock, flags);
398 dbg("%s - write limit hit\n", __func__);
399 return 0;
401 portdata->outstanding_urbs++;
402 spin_unlock_irqrestore(&portdata->lock, flags);
404 buffer = kmalloc(count, GFP_ATOMIC);
405 if (!buffer) {
406 dev_err(&port->dev, "out of memory\n");
407 count = -ENOMEM;
408 goto error_no_buffer;
411 urb = usb_alloc_urb(0, GFP_ATOMIC);
412 if (!urb) {
413 dev_err(&port->dev, "no more free urbs\n");
414 count = -ENOMEM;
415 goto error_no_urb;
418 memcpy(buffer, buf, count);
420 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
422 usb_fill_bulk_urb(urb, serial->dev,
423 usb_sndbulkpipe(serial->dev,
424 port->bulk_out_endpointAddress),
425 buffer, count, sierra_outdat_callback, port);
427 /* send it down the pipe */
428 status = usb_submit_urb(urb, GFP_ATOMIC);
429 if (status) {
430 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
431 "with status = %d\n", __func__, status);
432 count = status;
433 goto error;
436 /* we are done with this urb, so let the host driver
437 * really free it when it is finished with it */
438 usb_free_urb(urb);
440 return count;
441 error:
442 usb_free_urb(urb);
443 error_no_urb:
444 kfree(buffer);
445 error_no_buffer:
446 spin_lock_irqsave(&portdata->lock, flags);
447 --portdata->outstanding_urbs;
448 spin_unlock_irqrestore(&portdata->lock, flags);
449 return count;
452 static void sierra_indat_callback(struct urb *urb)
454 int err;
455 int endpoint;
456 struct usb_serial_port *port;
457 struct tty_struct *tty;
458 unsigned char *data = urb->transfer_buffer;
459 int status = urb->status;
461 dbg("%s: %p", __func__, urb);
463 endpoint = usb_pipeendpoint(urb->pipe);
464 port = urb->context;
466 if (status) {
467 dbg("%s: nonzero status: %d on endpoint %02x.",
468 __func__, status, endpoint);
469 } else {
470 tty = port->port.tty;
471 if (urb->actual_length) {
472 tty_buffer_request_room(tty, urb->actual_length);
473 tty_insert_flip_string(tty, data, urb->actual_length);
474 tty_flip_buffer_push(tty);
475 } else {
476 dbg("%s: empty read urb received", __func__);
479 /* Resubmit urb so we continue receiving */
480 if (port->port.count && status != -ESHUTDOWN) {
481 err = usb_submit_urb(urb, GFP_ATOMIC);
482 if (err)
483 dev_err(&port->dev, "resubmit read urb failed."
484 "(%d)\n", err);
487 return;
490 static void sierra_instat_callback(struct urb *urb)
492 int err;
493 int status = urb->status;
494 struct usb_serial_port *port = urb->context;
495 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
496 struct usb_serial *serial = port->serial;
498 dbg("%s", __func__);
499 dbg("%s: urb %p port %p has data %p", __func__, urb, port, portdata);
501 if (status == 0) {
502 struct usb_ctrlrequest *req_pkt =
503 (struct usb_ctrlrequest *)urb->transfer_buffer;
505 if (!req_pkt) {
506 dbg("%s: NULL req_pkt\n", __func__);
507 return;
509 if ((req_pkt->bRequestType == 0xA1) &&
510 (req_pkt->bRequest == 0x20)) {
511 int old_dcd_state;
512 unsigned char signals = *((unsigned char *)
513 urb->transfer_buffer +
514 sizeof(struct usb_ctrlrequest));
516 dbg("%s: signal x%x", __func__, signals);
518 old_dcd_state = portdata->dcd_state;
519 portdata->cts_state = 1;
520 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
521 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
522 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
524 if (port->port.tty && !C_CLOCAL(port->port.tty) &&
525 old_dcd_state && !portdata->dcd_state)
526 tty_hangup(port->port.tty);
527 } else {
528 dbg("%s: type %x req %x", __func__,
529 req_pkt->bRequestType, req_pkt->bRequest);
531 } else
532 dbg("%s: error %d", __func__, status);
534 /* Resubmit urb so we continue receiving IRQ data */
535 if (status != -ESHUTDOWN) {
536 urb->dev = serial->dev;
537 err = usb_submit_urb(urb, GFP_ATOMIC);
538 if (err)
539 dbg("%s: resubmit intr urb failed. (%d)",
540 __func__, err);
544 static int sierra_write_room(struct tty_struct *tty)
546 struct usb_serial_port *port = tty->driver_data;
547 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
548 unsigned long flags;
550 dbg("%s - port %d", __func__, port->number);
552 /* try to give a good number back based on if we have any free urbs at
553 * this point in time */
554 spin_lock_irqsave(&portdata->lock, flags);
555 if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
556 spin_unlock_irqrestore(&portdata->lock, flags);
557 dbg("%s - write limit hit\n", __func__);
558 return 0;
560 spin_unlock_irqrestore(&portdata->lock, flags);
562 return 2048;
565 static int sierra_open(struct tty_struct *tty,
566 struct usb_serial_port *port, struct file *filp)
568 struct sierra_port_private *portdata;
569 struct usb_serial *serial = port->serial;
570 int i;
571 struct urb *urb;
572 int result;
574 portdata = usb_get_serial_port_data(port);
576 dbg("%s", __func__);
578 /* Set some sane defaults */
579 portdata->rts_state = 1;
580 portdata->dtr_state = 1;
582 /* Reset low level data toggle and start reading from endpoints */
583 for (i = 0; i < N_IN_URB; i++) {
584 urb = portdata->in_urbs[i];
585 if (!urb)
586 continue;
587 if (urb->dev != serial->dev) {
588 dbg("%s: dev %p != %p", __func__,
589 urb->dev, serial->dev);
590 continue;
594 * make sure endpoint data toggle is synchronized with the
595 * device
597 usb_clear_halt(urb->dev, urb->pipe);
599 result = usb_submit_urb(urb, GFP_KERNEL);
600 if (result) {
601 dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
602 i, result, urb->transfer_buffer_length);
606 if (tty)
607 tty->low_latency = 1;
609 sierra_send_setup(tty, port);
611 /* start up the interrupt endpoint if we have one */
612 if (port->interrupt_in_urb) {
613 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
614 if (result)
615 dev_err(&port->dev, "submit irq_in urb failed %d\n",
616 result);
618 return 0;
621 static void sierra_close(struct tty_struct *tty,
622 struct usb_serial_port *port, struct file *filp)
624 int i;
625 struct usb_serial *serial = port->serial;
626 struct sierra_port_private *portdata;
628 dbg("%s", __func__);
629 portdata = usb_get_serial_port_data(port);
631 portdata->rts_state = 0;
632 portdata->dtr_state = 0;
634 if (serial->dev) {
635 mutex_lock(&serial->disc_mutex);
636 if (!serial->disconnected)
637 sierra_send_setup(tty, port);
638 mutex_unlock(&serial->disc_mutex);
640 /* Stop reading/writing urbs */
641 for (i = 0; i < N_IN_URB; i++)
642 usb_kill_urb(portdata->in_urbs[i]);
645 usb_kill_urb(port->interrupt_in_urb);
647 port->port.tty = NULL; /* FIXME */
650 static int sierra_startup(struct usb_serial *serial)
652 struct usb_serial_port *port;
653 struct sierra_port_private *portdata;
654 struct urb *urb;
655 int i;
656 int j;
658 dbg("%s", __func__);
660 /* Set Device mode to D0 */
661 sierra_set_power_state(serial->dev, 0x0000);
663 /* Check NMEA and set */
664 if (nmea)
665 sierra_vsc_set_nmea(serial->dev, 1);
667 /* Now setup per port private data */
668 for (i = 0; i < serial->num_ports; i++) {
669 port = serial->port[i];
670 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
671 if (!portdata) {
672 dbg("%s: kmalloc for sierra_port_private (%d) failed!.",
673 __func__, i);
674 return -ENOMEM;
676 spin_lock_init(&portdata->lock);
677 for (j = 0; j < N_IN_URB; j++) {
678 portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
679 if (!portdata->in_buffer[j]) {
680 for (--j; j >= 0; j--)
681 kfree(portdata->in_buffer[j]);
682 kfree(portdata);
683 return -ENOMEM;
687 usb_set_serial_port_data(port, portdata);
689 /* initialize the in urbs */
690 for (j = 0; j < N_IN_URB; ++j) {
691 urb = usb_alloc_urb(0, GFP_KERNEL);
692 if (urb == NULL) {
693 dbg("%s: alloc for in port failed.",
694 __func__);
695 continue;
697 /* Fill URB using supplied data. */
698 usb_fill_bulk_urb(urb, serial->dev,
699 usb_rcvbulkpipe(serial->dev,
700 port->bulk_in_endpointAddress),
701 portdata->in_buffer[j], IN_BUFLEN,
702 sierra_indat_callback, port);
703 portdata->in_urbs[j] = urb;
707 return 0;
710 static void sierra_shutdown(struct usb_serial *serial)
712 int i, j;
713 struct usb_serial_port *port;
714 struct sierra_port_private *portdata;
716 dbg("%s", __func__);
718 for (i = 0; i < serial->num_ports; ++i) {
719 port = serial->port[i];
720 if (!port)
721 continue;
722 portdata = usb_get_serial_port_data(port);
723 if (!portdata)
724 continue;
726 for (j = 0; j < N_IN_URB; j++) {
727 usb_kill_urb(portdata->in_urbs[j]);
728 usb_free_urb(portdata->in_urbs[j]);
729 kfree(portdata->in_buffer[j]);
731 kfree(portdata);
732 usb_set_serial_port_data(port, NULL);
736 static struct usb_serial_driver sierra_device = {
737 .driver = {
738 .owner = THIS_MODULE,
739 .name = "sierra",
741 .description = "Sierra USB modem",
742 .id_table = id_table,
743 .usb_driver = &sierra_driver,
744 .calc_num_ports = sierra_calc_num_ports,
745 .probe = sierra_probe,
746 .open = sierra_open,
747 .close = sierra_close,
748 .write = sierra_write,
749 .write_room = sierra_write_room,
750 .set_termios = sierra_set_termios,
751 .tiocmget = sierra_tiocmget,
752 .tiocmset = sierra_tiocmset,
753 .attach = sierra_startup,
754 .shutdown = sierra_shutdown,
755 .read_int_callback = sierra_instat_callback,
758 /* Functions used by new usb-serial code. */
759 static int __init sierra_init(void)
761 int retval;
762 retval = usb_serial_register(&sierra_device);
763 if (retval)
764 goto failed_device_register;
767 retval = usb_register(&sierra_driver);
768 if (retval)
769 goto failed_driver_register;
771 info(DRIVER_DESC ": " DRIVER_VERSION);
773 return 0;
775 failed_driver_register:
776 usb_serial_deregister(&sierra_device);
777 failed_device_register:
778 return retval;
781 static void __exit sierra_exit(void)
783 usb_deregister(&sierra_driver);
784 usb_serial_deregister(&sierra_device);
787 module_init(sierra_init);
788 module_exit(sierra_exit);
790 MODULE_AUTHOR(DRIVER_AUTHOR);
791 MODULE_DESCRIPTION(DRIVER_DESC);
792 MODULE_VERSION(DRIVER_VERSION);
793 MODULE_LICENSE("GPL");
795 module_param(nmea, bool, S_IRUGO | S_IWUSR);
796 MODULE_PARM_DESC(nmea, "NMEA streaming");
798 #ifdef CONFIG_USB_DEBUG
799 module_param(debug, bool, S_IRUGO | S_IWUSR);
800 MODULE_PARM_DESC(debug, "Debug messages");
801 #endif