sierra: driver urb handling improvements
[linux-2.6.git] / drivers / usb / serial / sierra.c
1 /*
2   USB Driver for Sierra Wireless
3
4   Copyright (C) 2006, 2007, 2008  Kevin Lloyd <klloyd@sierrawireless.com>
5
6   IMPORTANT DISCLAIMER: This driver is not commercially supported by
7   Sierra Wireless. Use at your own risk.
8
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.
12
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>
15 */
16
17 #define DRIVER_VERSION "v.1.3.3"
18 #define DRIVER_AUTHOR "Kevin Lloyd <klloyd@sierrawireless.com>"
19 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
20
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
30 #define SWIMS_USB_REQUEST_SetPower      0x00
31 #define SWIMS_USB_REQUEST_SetNmea       0x07
32
33 #define N_IN_URB        4
34 #define N_OUT_URB       4
35 #define IN_BUFLEN       4096
36
37 static int debug;
38 static int nmea;
39
40 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
41 {
42         int result;
43         dev_dbg(&udev->dev, "%s", __func__);
44         result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
45                         SWIMS_USB_REQUEST_SetPower,     /* __u8 request      */
46                         USB_TYPE_VENDOR,                /* __u8 request type */
47                         swiState,                       /* __u16 value       */
48                         0,                              /* __u16 index       */
49                         NULL,                           /* void *data        */
50                         0,                              /* __u16 size        */
51                         USB_CTRL_SET_TIMEOUT);          /* int timeout       */
52         return result;
53 }
54
55 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
56 {
57         int result;
58         dev_dbg(&udev->dev, "%s", __func__);
59         result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
60                         SWIMS_USB_REQUEST_SetNmea,      /* __u8 request      */
61                         USB_TYPE_VENDOR,                /* __u8 request type */
62                         enable,                         /* __u16 value       */
63                         0x0000,                         /* __u16 index       */
64                         NULL,                           /* void *data        */
65                         0,                              /* __u16 size        */
66                         USB_CTRL_SET_TIMEOUT);          /* int timeout       */
67         return result;
68 }
69
70 static int sierra_calc_num_ports(struct usb_serial *serial)
71 {
72         int result;
73         int *num_ports = usb_get_serial_data(serial);
74         dev_dbg(&serial->dev->dev, "%s", __func__);
75
76         result = *num_ports;
77
78         if (result) {
79                 kfree(num_ports);
80                 usb_set_serial_data(serial, NULL);
81         }
82
83         return result;
84 }
85
86 static int sierra_calc_interface(struct usb_serial *serial)
87 {
88         int interface;
89         struct usb_interface *p_interface;
90         struct usb_host_interface *p_host_interface;
91         dev_dbg(&serial->dev->dev, "%s", __func__);
92
93         /* Get the interface structure pointer from the serial struct */
94         p_interface = serial->interface;
95
96         /* Get a pointer to the host interface structure */
97         p_host_interface = p_interface->cur_altsetting;
98
99         /* read the interface descriptor for this active altsetting
100          * to find out the interface number we are on
101         */
102         interface = p_host_interface->desc.bInterfaceNumber;
103
104         return interface;
105 }
106
107 static int sierra_probe(struct usb_serial *serial,
108                         const struct usb_device_id *id)
109 {
110         int result = 0;
111         struct usb_device *udev;
112         int *num_ports;
113         u8 ifnum;
114         u8 numendpoints;
115
116         dev_dbg(&serial->dev->dev, "%s", __func__);
117
118         num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
119         if (!num_ports)
120                 return -ENOMEM;
121
122         ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
123         numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
124         udev = serial->dev;
125
126         /* Figure out the interface number from the serial structure */
127         ifnum = sierra_calc_interface(serial);
128
129         /*
130          * If this interface supports more than 1 alternate
131          * select the 2nd one
132          */
133         if (serial->interface->num_altsetting == 2) {
134                 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
135                         ifnum);
136                 /* We know the alternate setting is 1 for the MC8785 */
137                 usb_set_interface(udev, ifnum, 1);
138         }
139
140         /* Dummy interface present on some SKUs should be ignored */
141         if (ifnum == 0x99)
142                 *num_ports = 0;
143         else if (numendpoints <= 3)
144                 *num_ports = 1;
145         else
146                 *num_ports = (numendpoints-1)/2;
147
148         /*
149          * save off our num_ports info so that we can use it in the
150          * calc_num_ports callback
151          */
152         usb_set_serial_data(serial, (void *)num_ports);
153
154         return result;
155 }
156
157 static struct usb_device_id id_table [] = {
158         { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
159         { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
160         { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
161         { USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
162         { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
163         { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
164         { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
165         { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
166         { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
167         { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
168          /* Sierra Wireless C597 */
169         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
170          /* Sierra Wireless Device */
171         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
172         { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
173         { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */
174         { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */
175
176         { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
177         { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
178         { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
179         { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
180         { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */
181         { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
182         { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */
183         { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
184         { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
185         { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
186         { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
187         { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
188         { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
189         { USB_DEVICE(0x1199, 0x683C) }, /* Sierra Wireless MC8790 */
190         { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8790 */
191         { USB_DEVICE(0x1199, 0x683E) }, /* Sierra Wireless MC8790 */
192         { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
193         { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
194         { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
195         { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
196         { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
197         { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
198         { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
199         { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
200         /* Sierra Wireless C885 */
201         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
202         /* Sierra Wireless Device */
203         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
204         /* Sierra Wireless Device */
205         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
206         /* Sierra Wireless Device */
207         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
208
209         { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
210         { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
211
212         { }
213 };
214 MODULE_DEVICE_TABLE(usb, id_table);
215
216 static struct usb_driver sierra_driver = {
217         .name       = "sierra",
218         .probe      = usb_serial_probe,
219         .disconnect = usb_serial_disconnect,
220         .id_table   = id_table,
221         .no_dynamic_id =        1,
222 };
223
224 struct sierra_port_private {
225         spinlock_t lock;        /* lock the structure */
226         int outstanding_urbs;   /* number of out urbs in flight */
227
228         /* Input endpoints and buffers for this port */
229         struct urb *in_urbs[N_IN_URB];
230
231         /* Settings for the port */
232         int rts_state;  /* Handshaking pins (outputs) */
233         int dtr_state;
234         int cts_state;  /* Handshaking pins (inputs) */
235         int dsr_state;
236         int dcd_state;
237         int ri_state;
238 };
239
240 static int sierra_send_setup(struct usb_serial_port *port)
241 {
242         struct usb_serial *serial = port->serial;
243         struct sierra_port_private *portdata;
244         __u16 interface = 0;
245         int val = 0;
246
247         dev_dbg(&port->dev, "%s", __func__);
248
249         portdata = usb_get_serial_port_data(port);
250
251         if (portdata->dtr_state)
252                 val |= 0x01;
253         if (portdata->rts_state)
254                 val |= 0x02;
255
256         /* If composite device then properly report interface */
257         if (serial->num_ports == 1) {
258                 interface = sierra_calc_interface(serial);
259                 /* Control message is sent only to interfaces with
260                  * interrupt_in endpoints
261                  */
262                 if (port->interrupt_in_urb) {
263                         /* send control message */
264                         return usb_control_msg(serial->dev,
265                                 usb_rcvctrlpipe(serial->dev, 0),
266                                 0x22, 0x21, val, interface,
267                                 NULL, 0, USB_CTRL_SET_TIMEOUT);
268                 }
269         }
270
271         /* Otherwise the need to do non-composite mapping */
272         else {
273                 if (port->bulk_out_endpointAddress == 2)
274                         interface = 0;
275                 else if (port->bulk_out_endpointAddress == 4)
276                         interface = 1;
277                 else if (port->bulk_out_endpointAddress == 5)
278                         interface = 2;
279                 return usb_control_msg(serial->dev,
280                         usb_rcvctrlpipe(serial->dev, 0),
281                         0x22, 0x21, val, interface,
282                         NULL, 0, USB_CTRL_SET_TIMEOUT);
283         }
284         return 0;
285 }
286
287 static void sierra_set_termios(struct tty_struct *tty,
288                 struct usb_serial_port *port, struct ktermios *old_termios)
289 {
290         dev_dbg(&port->dev, "%s", __func__);
291         tty_termios_copy_hw(tty->termios, old_termios);
292         sierra_send_setup(port);
293 }
294
295 static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
296 {
297         struct usb_serial_port *port = tty->driver_data;
298         unsigned int value;
299         struct sierra_port_private *portdata;
300
301         dev_dbg(&port->dev, "%s", __func__);
302         portdata = usb_get_serial_port_data(port);
303
304         value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
305                 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
306                 ((portdata->cts_state) ? TIOCM_CTS : 0) |
307                 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
308                 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
309                 ((portdata->ri_state) ? TIOCM_RNG : 0);
310
311         return value;
312 }
313
314 static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
315                         unsigned int set, unsigned int clear)
316 {
317         struct usb_serial_port *port = tty->driver_data;
318         struct sierra_port_private *portdata;
319
320         portdata = usb_get_serial_port_data(port);
321
322         if (set & TIOCM_RTS)
323                 portdata->rts_state = 1;
324         if (set & TIOCM_DTR)
325                 portdata->dtr_state = 1;
326
327         if (clear & TIOCM_RTS)
328                 portdata->rts_state = 0;
329         if (clear & TIOCM_DTR)
330                 portdata->dtr_state = 0;
331         return sierra_send_setup(port);
332 }
333
334 static void sierra_release_urb(struct urb *urb)
335 {
336         struct usb_serial_port *port;
337         if (urb) {
338                 port =  urb->context;
339                 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
340                 kfree(urb->transfer_buffer);
341                 usb_free_urb(urb);
342         }
343 }
344
345 static void sierra_outdat_callback(struct urb *urb)
346 {
347         struct usb_serial_port *port = urb->context;
348         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
349         int status = urb->status;
350         unsigned long flags;
351
352         dev_dbg(&port->dev, "%s - port %d", __func__, port->number);
353
354         /* free up the transfer buffer, as usb_free_urb() does not do this */
355         kfree(urb->transfer_buffer);
356
357         if (status)
358                 dev_dbg(&port->dev, "%s - nonzero write bulk status "
359                     "received: %d", __func__, status);
360
361         spin_lock_irqsave(&portdata->lock, flags);
362         --portdata->outstanding_urbs;
363         spin_unlock_irqrestore(&portdata->lock, flags);
364
365         usb_serial_port_softint(port);
366 }
367
368 /* Write */
369 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
370                                         const unsigned char *buf, int count)
371 {
372         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
373         struct usb_serial *serial = port->serial;
374         unsigned long flags;
375         unsigned char *buffer;
376         struct urb *urb;
377         int status;
378
379         portdata = usb_get_serial_port_data(port);
380
381         dev_dbg(&port->dev, "%s: write (%d chars)", __func__, count);
382
383         spin_lock_irqsave(&portdata->lock, flags);
384         if (portdata->outstanding_urbs > N_OUT_URB) {
385                 spin_unlock_irqrestore(&portdata->lock, flags);
386                 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
387                 return 0;
388         }
389         portdata->outstanding_urbs++;
390         spin_unlock_irqrestore(&portdata->lock, flags);
391
392         buffer = kmalloc(count, GFP_ATOMIC);
393         if (!buffer) {
394                 dev_err(&port->dev, "out of memory\n");
395                 count = -ENOMEM;
396                 goto error_no_buffer;
397         }
398
399         urb = usb_alloc_urb(0, GFP_ATOMIC);
400         if (!urb) {
401                 dev_err(&port->dev, "no more free urbs\n");
402                 count = -ENOMEM;
403                 goto error_no_urb;
404         }
405
406         memcpy(buffer, buf, count);
407
408         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
409
410         usb_fill_bulk_urb(urb, serial->dev,
411                           usb_sndbulkpipe(serial->dev,
412                                           port->bulk_out_endpointAddress),
413                           buffer, count, sierra_outdat_callback, port);
414
415         /* send it down the pipe */
416         status = usb_submit_urb(urb, GFP_ATOMIC);
417         if (status) {
418                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
419                         "with status = %d\n", __func__, status);
420                 count = status;
421                 goto error;
422         }
423
424         /* we are done with this urb, so let the host driver
425          * really free it when it is finished with it */
426         usb_free_urb(urb);
427
428         return count;
429 error:
430         usb_free_urb(urb);
431 error_no_urb:
432         kfree(buffer);
433 error_no_buffer:
434         spin_lock_irqsave(&portdata->lock, flags);
435         --portdata->outstanding_urbs;
436         spin_unlock_irqrestore(&portdata->lock, flags);
437         return count;
438 }
439
440 static void sierra_indat_callback(struct urb *urb)
441 {
442         int err;
443         int endpoint;
444         struct usb_serial_port *port;
445         struct tty_struct *tty;
446         unsigned char *data = urb->transfer_buffer;
447         int status = urb->status;
448
449         dbg("%s: %p", __func__, urb);
450
451         endpoint = usb_pipeendpoint(urb->pipe);
452         port =  urb->context;
453
454         if (status) {
455                 dev_dbg(&port->dev, "%s: nonzero status: %d on"
456                     " endpoint %02x.", __func__, status, endpoint);
457         } else {
458                 if (urb->actual_length) {
459                         tty = tty_port_tty_get(&port->port);
460                         tty_buffer_request_room(tty, urb->actual_length);
461                         tty_insert_flip_string(tty, data, urb->actual_length);
462                         tty_flip_buffer_push(tty);
463                         tty_kref_put(tty);
464                 } else
465                         dev_dbg(&port->dev, "%s: empty read urb"
466                                 " received", __func__);
467
468                 /* Resubmit urb so we continue receiving */
469                 if (port->port.count && status != -ESHUTDOWN && status != -EPERM) {
470                         err = usb_submit_urb(urb, GFP_ATOMIC);
471                         if (err)
472                                 dev_err(&port->dev, "resubmit read urb failed."
473                                         "(%d)\n", err);
474                 }
475         }
476         return;
477 }
478
479 static void sierra_instat_callback(struct urb *urb)
480 {
481         int err;
482         int status = urb->status;
483         struct usb_serial_port *port =  urb->context;
484         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
485         struct usb_serial *serial = port->serial;
486
487         dev_dbg(&port->dev, "%s", __func__);
488         dev_dbg(&port->dev, "%s: urb %p port %p has data %p", __func__,
489                 urb, port, portdata);
490
491         if (status == 0) {
492                 struct usb_ctrlrequest *req_pkt =
493                                 (struct usb_ctrlrequest *)urb->transfer_buffer;
494
495                 if (!req_pkt) {
496                         dev_dbg(&port->dev, "%s: NULL req_pkt\n",
497                                 __func__);
498                         return;
499                 }
500                 if ((req_pkt->bRequestType == 0xA1) &&
501                                 (req_pkt->bRequest == 0x20)) {
502                         int old_dcd_state;
503                         unsigned char signals = *((unsigned char *)
504                                         urb->transfer_buffer +
505                                         sizeof(struct usb_ctrlrequest));
506                         struct tty_struct *tty;
507
508                         dev_dbg(&port->dev, "%s: signal x%x", __func__,
509                                 signals);
510
511                         old_dcd_state = portdata->dcd_state;
512                         portdata->cts_state = 1;
513                         portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
514                         portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
515                         portdata->ri_state = ((signals & 0x08) ? 1 : 0);
516
517                         tty = tty_port_tty_get(&port->port);
518                         if (tty && !C_CLOCAL(tty) &&
519                                         old_dcd_state && !portdata->dcd_state)
520                                 tty_hangup(tty);
521                         tty_kref_put(tty);
522                 } else {
523                         dev_dbg(&port->dev, "%s: type %x req %x",
524                                 __func__, req_pkt->bRequestType,
525                                 req_pkt->bRequest);
526                 }
527         } else
528                 dev_dbg(&port->dev, "%s: error %d", __func__, status);
529
530         /* Resubmit urb so we continue receiving IRQ data */
531         if (status != -ESHUTDOWN) {
532                 urb->dev = serial->dev;
533                 err = usb_submit_urb(urb, GFP_ATOMIC);
534                 if (err)
535                         dev_dbg(&port->dev, "%s: resubmit intr urb "
536                                 "failed. (%d)", __func__, err);
537         }
538 }
539
540 static int sierra_write_room(struct tty_struct *tty)
541 {
542         struct usb_serial_port *port = tty->driver_data;
543         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
544         unsigned long flags;
545
546         dev_dbg(&port->dev, "%s - port %d", __func__, port->number);
547
548         /* try to give a good number back based on if we have any free urbs at
549          * this point in time */
550         spin_lock_irqsave(&portdata->lock, flags);
551         if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
552                 spin_unlock_irqrestore(&portdata->lock, flags);
553                 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
554                 return 0;
555         }
556         spin_unlock_irqrestore(&portdata->lock, flags);
557
558         return 2048;
559 }
560
561 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
562 {
563         int i;
564         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
565
566         for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++)
567                 usb_kill_urb(portdata->in_urbs[i]);
568
569         usb_kill_urb(port->interrupt_in_urb);
570 }
571
572 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
573 {
574         int ok_cnt;
575         int err = -EINVAL;
576         int i;
577         struct urb *urb;
578         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
579
580         ok_cnt = 0;
581         for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
582                 urb = portdata->in_urbs[i];
583                 if (!urb)
584                         continue;
585                 err = usb_submit_urb(urb, mem_flags);
586                 if (err) {
587                         dev_err(&port->dev, "%s: submit urb failed: %d\n",
588                                 __func__, err);
589                 } else {
590                         ok_cnt++;
591                 }
592         }
593
594         if (ok_cnt && port->interrupt_in_urb) {
595                 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
596                 if (err) {
597                         dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
598                                 __func__, err);
599                 }
600         }
601
602         if (ok_cnt > 0) /* at least one rx urb submitted */
603                 return 0;
604         else
605                 return err;
606 }
607
608 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
609                                         int dir, void *ctx, int len,
610                                         gfp_t mem_flags,
611                                         usb_complete_t callback)
612 {
613         struct urb      *urb;
614         u8              *buf;
615
616         if (endpoint == -1)
617                 return NULL;
618
619         urb = usb_alloc_urb(0, mem_flags);
620         if (urb == NULL) {
621                 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
622                         __func__, endpoint);
623                 return NULL;
624         }
625
626         buf = kmalloc(len, mem_flags);
627         if (buf) {
628                 /* Fill URB using supplied data */
629                 usb_fill_bulk_urb(urb, serial->dev,
630                         usb_sndbulkpipe(serial->dev, endpoint) | dir,
631                         buf, len, callback, ctx);
632
633                 /* debug */
634                 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
635                                 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
636         } else {
637                 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
638                                 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
639
640                 sierra_release_urb(urb);
641                 urb = NULL;
642         }
643
644         return urb;
645 }
646
647 static void sierra_close(struct usb_serial_port *port)
648 {
649         int i;
650         struct usb_serial *serial = port->serial;
651         struct sierra_port_private *portdata;
652
653         dev_dbg(&port->dev, "%s\n", __func__);
654         portdata = usb_get_serial_port_data(port);
655
656         portdata->rts_state = 0;
657         portdata->dtr_state = 0;
658
659         if (serial->dev) {
660                 mutex_lock(&serial->disc_mutex);
661                 if (!serial->disconnected)
662                         sierra_send_setup(port);
663                 mutex_unlock(&serial->disc_mutex);
664
665                 /* Stop reading urbs */
666                 sierra_stop_rx_urbs(port);
667                 /* .. and release them */
668                 for (i = 0; i < N_IN_URB; i++) {
669                         sierra_release_urb(portdata->in_urbs[i]);
670                         portdata->in_urbs[i] = NULL;
671                 }
672         }
673 }
674
675 static int sierra_open(struct tty_struct *tty,
676                         struct usb_serial_port *port, struct file *filp)
677 {
678         struct sierra_port_private *portdata;
679         struct usb_serial *serial = port->serial;
680         int i;
681         int err;
682         int endpoint;
683         struct urb *urb;
684
685         portdata = usb_get_serial_port_data(port);
686
687         dev_dbg(&port->dev, "%s", __func__);
688
689         /* Set some sane defaults */
690         portdata->rts_state = 1;
691         portdata->dtr_state = 1;
692
693
694         endpoint = port->bulk_in_endpointAddress;
695         for (i = 0; i < ARRAY_SIZE(portdata->in_urbs); i++) {
696                 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
697                                         IN_BUFLEN, GFP_KERNEL,
698                                         sierra_indat_callback);
699                 portdata->in_urbs[i] = urb;
700         }
701         /* clear halt condition */
702         usb_clear_halt(serial->dev,
703                         usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
704
705         err = sierra_submit_rx_urbs(port, GFP_KERNEL);
706         if (err) {
707                 /* get rid of everything as in close */
708                 sierra_close(port);
709                 return err;
710         }
711         sierra_send_setup(port);
712
713         return 0;
714 }
715
716
717 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
718 {
719         struct usb_serial *serial = port->serial;
720         struct sierra_port_private *portdata;
721
722         portdata = usb_get_serial_port_data(port);
723         portdata->rts_state = on;
724         portdata->dtr_state = on;
725
726         if (serial->dev) {
727                 mutex_lock(&serial->disc_mutex);
728                 if (!serial->disconnected)
729                         sierra_send_setup(port);
730                 mutex_unlock(&serial->disc_mutex);
731         }
732 }
733
734 static int sierra_startup(struct usb_serial *serial)
735 {
736         struct usb_serial_port *port;
737         struct sierra_port_private *portdata;
738         int i;
739
740         dev_dbg(&serial->dev->dev, "%s", __func__);
741
742         /* Set Device mode to D0 */
743         sierra_set_power_state(serial->dev, 0x0000);
744
745         /* Check NMEA and set */
746         if (nmea)
747                 sierra_vsc_set_nmea(serial->dev, 1);
748
749         /* Now setup per port private data */
750         for (i = 0; i < serial->num_ports; i++) {
751                 port = serial->port[i];
752                 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
753                 if (!portdata) {
754                         dev_dbg(&port->dev, "%s: kmalloc for "
755                                 "sierra_port_private (%d) failed!.",
756                                 __func__, i);
757                         return -ENOMEM;
758                 }
759                 spin_lock_init(&portdata->lock);
760                 /* Set the port private data pointer */
761                 usb_set_serial_port_data(port, portdata);
762         }
763
764         return 0;
765 }
766
767 static void sierra_shutdown(struct usb_serial *serial)
768 {
769         int i;
770         struct usb_serial_port *port;
771         struct sierra_port_private *portdata;
772
773         dev_dbg(&serial->dev->dev, "%s", __func__);
774
775         for (i = 0; i < serial->num_ports; ++i) {
776                 port = serial->port[i];
777                 if (!port)
778                         continue;
779                 portdata = usb_get_serial_port_data(port);
780                 if (!portdata)
781                         continue;
782                 kfree(portdata);
783                 usb_set_serial_port_data(port, NULL);
784         }
785 }
786
787 static struct usb_serial_driver sierra_device = {
788         .driver = {
789                 .owner =        THIS_MODULE,
790                 .name =         "sierra",
791         },
792         .description       = "Sierra USB modem",
793         .id_table          = id_table,
794         .usb_driver        = &sierra_driver,
795         .calc_num_ports    = sierra_calc_num_ports,
796         .probe             = sierra_probe,
797         .open              = sierra_open,
798         .close             = sierra_close,
799         .dtr_rts           = sierra_dtr_rts,
800         .write             = sierra_write,
801         .write_room        = sierra_write_room,
802         .set_termios       = sierra_set_termios,
803         .tiocmget          = sierra_tiocmget,
804         .tiocmset          = sierra_tiocmset,
805         .attach            = sierra_startup,
806         .shutdown          = sierra_shutdown,
807         .read_int_callback = sierra_instat_callback,
808 };
809
810 /* Functions used by new usb-serial code. */
811 static int __init sierra_init(void)
812 {
813         int retval;
814         retval = usb_serial_register(&sierra_device);
815         if (retval)
816                 goto failed_device_register;
817
818
819         retval = usb_register(&sierra_driver);
820         if (retval)
821                 goto failed_driver_register;
822
823         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
824                DRIVER_DESC "\n");
825
826         return 0;
827
828 failed_driver_register:
829         usb_serial_deregister(&sierra_device);
830 failed_device_register:
831         return retval;
832 }
833
834 static void __exit sierra_exit(void)
835 {
836         usb_deregister(&sierra_driver);
837         usb_serial_deregister(&sierra_device);
838 }
839
840 module_init(sierra_init);
841 module_exit(sierra_exit);
842
843 MODULE_AUTHOR(DRIVER_AUTHOR);
844 MODULE_DESCRIPTION(DRIVER_DESC);
845 MODULE_VERSION(DRIVER_VERSION);
846 MODULE_LICENSE("GPL");
847
848 module_param(nmea, bool, S_IRUGO | S_IWUSR);
849 MODULE_PARM_DESC(nmea, "NMEA streaming");
850
851 module_param(debug, bool, S_IRUGO | S_IWUSR);
852 MODULE_PARM_DESC(debug, "Debug messages");