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