GNU Linux-libre 4.14.302-gnu1
[releases.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 /* Uncomment to log function calls */
20 /* #define DEBUG */
21
22 #define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
23 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
24
25 #include <linux/kernel.h>
26 #include <linux/jiffies.h>
27 #include <linux/errno.h>
28 #include <linux/tty.h>
29 #include <linux/slab.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
34
35 #define SWIMS_USB_REQUEST_SetPower      0x00
36 #define SWIMS_USB_REQUEST_SetNmea       0x07
37
38 #define N_IN_URB_HM     8
39 #define N_OUT_URB_HM    64
40 #define N_IN_URB        4
41 #define N_OUT_URB       4
42 #define IN_BUFLEN       4096
43
44 #define MAX_TRANSFER            (PAGE_SIZE - 512)
45 /* MAX_TRANSFER is chosen so that the VM is not stressed by
46    allocations > PAGE_SIZE and the number of packets in a page
47    is an integer 512 is the largest possible packet on EHCI */
48
49 static bool nmea;
50
51 /* Used in interface blacklisting */
52 struct sierra_iface_info {
53         const u32 infolen;      /* number of interface numbers on blacklist */
54         const u8  *ifaceinfo;   /* pointer to the array holding the numbers */
55 };
56
57 struct sierra_intf_private {
58         spinlock_t susp_lock;
59         unsigned int suspended:1;
60         int in_flight;
61         unsigned int open_ports;
62 };
63
64 static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
65 {
66         return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
67                         SWIMS_USB_REQUEST_SetPower,     /* __u8 request      */
68                         USB_TYPE_VENDOR,                /* __u8 request type */
69                         swiState,                       /* __u16 value       */
70                         0,                              /* __u16 index       */
71                         NULL,                           /* void *data        */
72                         0,                              /* __u16 size        */
73                         USB_CTRL_SET_TIMEOUT);          /* int timeout       */
74 }
75
76 static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
77 {
78         return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
79                         SWIMS_USB_REQUEST_SetNmea,      /* __u8 request      */
80                         USB_TYPE_VENDOR,                /* __u8 request type */
81                         enable,                         /* __u16 value       */
82                         0x0000,                         /* __u16 index       */
83                         NULL,                           /* void *data        */
84                         0,                              /* __u16 size        */
85                         USB_CTRL_SET_TIMEOUT);          /* int timeout       */
86 }
87
88 static int sierra_calc_num_ports(struct usb_serial *serial,
89                                         struct usb_serial_endpoints *epds)
90 {
91         int num_ports = 0;
92         u8 ifnum, numendpoints;
93
94         ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
95         numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
96
97         /* Dummy interface present on some SKUs should be ignored */
98         if (ifnum == 0x99)
99                 num_ports = 0;
100         else if (numendpoints <= 3)
101                 num_ports = 1;
102         else
103                 num_ports = (numendpoints-1)/2;
104         return num_ports;
105 }
106
107 static int is_blacklisted(const u8 ifnum,
108                                 const struct sierra_iface_info *blacklist)
109 {
110         const u8  *info;
111         int i;
112
113         if (blacklist) {
114                 info = blacklist->ifaceinfo;
115
116                 for (i = 0; i < blacklist->infolen; i++) {
117                         if (info[i] == ifnum)
118                                 return 1;
119                 }
120         }
121         return 0;
122 }
123
124 static int is_himemory(const u8 ifnum,
125                                 const struct sierra_iface_info *himemorylist)
126 {
127         const u8  *info;
128         int i;
129
130         if (himemorylist) {
131                 info = himemorylist->ifaceinfo;
132
133                 for (i=0; i < himemorylist->infolen; i++) {
134                         if (info[i] == ifnum)
135                                 return 1;
136                 }
137         }
138         return 0;
139 }
140
141 static u8 sierra_interface_num(struct usb_serial *serial)
142 {
143         return serial->interface->cur_altsetting->desc.bInterfaceNumber;
144 }
145
146 static int sierra_probe(struct usb_serial *serial,
147                         const struct usb_device_id *id)
148 {
149         int result = 0;
150         struct usb_device *udev;
151         u8 ifnum;
152
153         udev = serial->dev;
154         ifnum = sierra_interface_num(serial);
155
156         /*
157          * If this interface supports more than 1 alternate
158          * select the 2nd one
159          */
160         if (serial->interface->num_altsetting == 2) {
161                 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
162                         ifnum);
163                 /* We know the alternate setting is 1 for the MC8785 */
164                 usb_set_interface(udev, ifnum, 1);
165         }
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 /* interfaces with higher memory requirements */
178 static const u8 hi_memory_typeA_ifaces[] = { 0, 2 };
179 static const struct sierra_iface_info typeA_interface_list = {
180         .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
181         .ifaceinfo = hi_memory_typeA_ifaces,
182 };
183
184 static const u8 hi_memory_typeB_ifaces[] = { 3, 4, 5, 6 };
185 static const struct sierra_iface_info typeB_interface_list = {
186         .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
187         .ifaceinfo = hi_memory_typeB_ifaces,
188 };
189
190 /* 'blacklist' of interfaces not served by this driver */
191 static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11, 19, 20 };
192 static const struct sierra_iface_info direct_ip_interface_blacklist = {
193         .infolen = ARRAY_SIZE(direct_ip_non_serial_ifaces),
194         .ifaceinfo = direct_ip_non_serial_ifaces,
195 };
196
197 static const struct usb_device_id id_table[] = {
198         { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
199         { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
200         { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
201         { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
202
203         { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
204         { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
205         { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
206         { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
207         { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
208         { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
209         { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
210         { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
211         { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
212         { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
213         { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
214         { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
215         { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U */
216         /* Sierra Wireless C597 */
217         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
218         /* Sierra Wireless T598 */
219         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
220         { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
221         { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
222         { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
223         { USB_DEVICE(0x1199, 0x0029) }, /* Sierra Wireless Device */
224
225         { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
226         { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
227         { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
228         { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
229         { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
230         { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
231         { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
232         { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
233         { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
234         { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
235         { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
236         { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
237         { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
238         { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
239         { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
240         { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
241         { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
242         { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
243         { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
244         { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
245         { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
246         /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
247         { USB_DEVICE(0x1199, 0x683C) },
248         { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
249         /* Sierra Wireless MC8790, MC8791, MC8792 */
250         { USB_DEVICE(0x1199, 0x683E) },
251         { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
252         { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
253         { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
254         { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
255         { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
256         { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
257         { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
258         { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
259         /* Sierra Wireless C885 */
260         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
261         /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
262         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
263         /* Sierra Wireless C22/C33 */
264         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
265         /* Sierra Wireless HSPA Non-Composite Device */
266         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
267         { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
268         /* Sierra Wireless Direct IP modems */
269         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
270           .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
271         },
272         { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
273           .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
274         },
275         { USB_DEVICE(0x1199, 0x68AB) }, /* Sierra Wireless AR8550 */
276         /* AT&T Direct IP LTE modems */
277         { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
278           .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
279         },
280         /* Airprime/Sierra Wireless Direct IP modems */
281         { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
282           .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
283         },
284
285         { }
286 };
287 MODULE_DEVICE_TABLE(usb, id_table);
288
289
290 struct sierra_port_private {
291         spinlock_t lock;        /* lock the structure */
292         int outstanding_urbs;   /* number of out urbs in flight */
293         struct usb_anchor active;
294         struct usb_anchor delayed;
295
296         int num_out_urbs;
297         int num_in_urbs;
298         /* Input endpoints and buffers for this port */
299         struct urb *in_urbs[N_IN_URB_HM];
300
301         /* Settings for the port */
302         int rts_state;  /* Handshaking pins (outputs) */
303         int dtr_state;
304         int cts_state;  /* Handshaking pins (inputs) */
305         int dsr_state;
306         int dcd_state;
307         int ri_state;
308 };
309
310 static int sierra_send_setup(struct usb_serial_port *port)
311 {
312         struct usb_serial *serial = port->serial;
313         struct sierra_port_private *portdata;
314         __u16 interface = 0;
315         int val = 0;
316         int do_send = 0;
317         int retval;
318
319         portdata = usb_get_serial_port_data(port);
320
321         if (portdata->dtr_state)
322                 val |= 0x01;
323         if (portdata->rts_state)
324                 val |= 0x02;
325
326         /* If composite device then properly report interface */
327         if (serial->num_ports == 1) {
328                 interface = sierra_interface_num(serial);
329                 /* Control message is sent only to interfaces with
330                  * interrupt_in endpoints
331                  */
332                 if (port->interrupt_in_urb) {
333                         /* send control message */
334                         do_send = 1;
335                 }
336         }
337
338         /* Otherwise the need to do non-composite mapping */
339         else {
340                 if (port->bulk_out_endpointAddress == 2)
341                         interface = 0;
342                 else if (port->bulk_out_endpointAddress == 4)
343                         interface = 1;
344                 else if (port->bulk_out_endpointAddress == 5)
345                         interface = 2;
346
347                 do_send = 1;
348         }
349         if (!do_send)
350                 return 0;
351
352         retval = usb_autopm_get_interface(serial->interface);
353         if (retval < 0)
354                 return retval;
355
356         retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
357                 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
358         usb_autopm_put_interface(serial->interface);
359
360         return retval;
361 }
362
363 static int sierra_tiocmget(struct tty_struct *tty)
364 {
365         struct usb_serial_port *port = tty->driver_data;
366         unsigned int value;
367         struct sierra_port_private *portdata;
368
369         portdata = usb_get_serial_port_data(port);
370
371         value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
372                 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
373                 ((portdata->cts_state) ? TIOCM_CTS : 0) |
374                 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
375                 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
376                 ((portdata->ri_state) ? TIOCM_RNG : 0);
377
378         return value;
379 }
380
381 static int sierra_tiocmset(struct tty_struct *tty,
382                         unsigned int set, unsigned int clear)
383 {
384         struct usb_serial_port *port = tty->driver_data;
385         struct sierra_port_private *portdata;
386
387         portdata = usb_get_serial_port_data(port);
388
389         if (set & TIOCM_RTS)
390                 portdata->rts_state = 1;
391         if (set & TIOCM_DTR)
392                 portdata->dtr_state = 1;
393
394         if (clear & TIOCM_RTS)
395                 portdata->rts_state = 0;
396         if (clear & TIOCM_DTR)
397                 portdata->dtr_state = 0;
398         return sierra_send_setup(port);
399 }
400
401 static void sierra_release_urb(struct urb *urb)
402 {
403         if (urb) {
404                 kfree(urb->transfer_buffer);
405                 usb_free_urb(urb);
406         }
407 }
408
409 static void sierra_outdat_callback(struct urb *urb)
410 {
411         struct usb_serial_port *port = urb->context;
412         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
413         struct sierra_intf_private *intfdata;
414         int status = urb->status;
415
416         intfdata = usb_get_serial_data(port->serial);
417
418         /* free up the transfer buffer, as usb_free_urb() does not do this */
419         kfree(urb->transfer_buffer);
420         usb_autopm_put_interface_async(port->serial->interface);
421         if (status)
422                 dev_dbg(&port->dev, "%s - nonzero write bulk status "
423                     "received: %d\n", __func__, status);
424
425         spin_lock(&portdata->lock);
426         --portdata->outstanding_urbs;
427         spin_unlock(&portdata->lock);
428         spin_lock(&intfdata->susp_lock);
429         --intfdata->in_flight;
430         spin_unlock(&intfdata->susp_lock);
431
432         usb_serial_port_softint(port);
433 }
434
435 /* Write */
436 static int sierra_write(struct tty_struct *tty, struct usb_serial_port *port,
437                                         const unsigned char *buf, int count)
438 {
439         struct sierra_port_private *portdata;
440         struct sierra_intf_private *intfdata;
441         struct usb_serial *serial = port->serial;
442         unsigned long flags;
443         unsigned char *buffer;
444         struct urb *urb;
445         size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
446         int retval = 0;
447
448         /* verify that we actually have some data to write */
449         if (count == 0)
450                 return 0;
451
452         portdata = usb_get_serial_port_data(port);
453         intfdata = usb_get_serial_data(serial);
454
455         dev_dbg(&port->dev, "%s: write (%zd bytes)\n", __func__, writesize);
456         spin_lock_irqsave(&portdata->lock, flags);
457         dev_dbg(&port->dev, "%s - outstanding_urbs: %d\n", __func__,
458                 portdata->outstanding_urbs);
459         if (portdata->outstanding_urbs > portdata->num_out_urbs) {
460                 spin_unlock_irqrestore(&portdata->lock, flags);
461                 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
462                 return 0;
463         }
464         portdata->outstanding_urbs++;
465         dev_dbg(&port->dev, "%s - 1, outstanding_urbs: %d\n", __func__,
466                 portdata->outstanding_urbs);
467         spin_unlock_irqrestore(&portdata->lock, flags);
468
469         retval = usb_autopm_get_interface_async(serial->interface);
470         if (retval < 0) {
471                 spin_lock_irqsave(&portdata->lock, flags);
472                 portdata->outstanding_urbs--;
473                 spin_unlock_irqrestore(&portdata->lock, flags);
474                 goto error_simple;
475         }
476
477         buffer = kmalloc(writesize, GFP_ATOMIC);
478         if (!buffer) {
479                 retval = -ENOMEM;
480                 goto error_no_buffer;
481         }
482
483         urb = usb_alloc_urb(0, GFP_ATOMIC);
484         if (!urb) {
485                 retval = -ENOMEM;
486                 goto error_no_urb;
487         }
488
489         memcpy(buffer, buf, writesize);
490
491         usb_serial_debug_data(&port->dev, __func__, writesize, buffer);
492
493         usb_fill_bulk_urb(urb, serial->dev,
494                           usb_sndbulkpipe(serial->dev,
495                                           port->bulk_out_endpointAddress),
496                           buffer, writesize, sierra_outdat_callback, port);
497
498         /* Handle the need to send a zero length packet */
499         urb->transfer_flags |= URB_ZERO_PACKET;
500
501         spin_lock_irqsave(&intfdata->susp_lock, flags);
502
503         if (intfdata->suspended) {
504                 usb_anchor_urb(urb, &portdata->delayed);
505                 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
506                 goto skip_power;
507         } else {
508                 usb_anchor_urb(urb, &portdata->active);
509         }
510         /* send it down the pipe */
511         retval = usb_submit_urb(urb, GFP_ATOMIC);
512         if (retval) {
513                 usb_unanchor_urb(urb);
514                 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
515                 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
516                         "with status = %d\n", __func__, retval);
517                 goto error;
518         } else {
519                 intfdata->in_flight++;
520                 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
521         }
522
523 skip_power:
524         /* we are done with this urb, so let the host driver
525          * really free it when it is finished with it */
526         usb_free_urb(urb);
527
528         return writesize;
529 error:
530         usb_free_urb(urb);
531 error_no_urb:
532         kfree(buffer);
533 error_no_buffer:
534         spin_lock_irqsave(&portdata->lock, flags);
535         --portdata->outstanding_urbs;
536         dev_dbg(&port->dev, "%s - 2. outstanding_urbs: %d\n", __func__,
537                 portdata->outstanding_urbs);
538         spin_unlock_irqrestore(&portdata->lock, flags);
539         usb_autopm_put_interface_async(serial->interface);
540 error_simple:
541         return retval;
542 }
543
544 static void sierra_indat_callback(struct urb *urb)
545 {
546         int err;
547         int endpoint;
548         struct usb_serial_port *port;
549         unsigned char *data = urb->transfer_buffer;
550         int status = urb->status;
551
552         endpoint = usb_pipeendpoint(urb->pipe);
553         port = urb->context;
554
555         if (status) {
556                 dev_dbg(&port->dev, "%s: nonzero status: %d on"
557                         " endpoint %02x\n", __func__, status, endpoint);
558         } else {
559                 if (urb->actual_length) {
560                         tty_insert_flip_string(&port->port, data,
561                                 urb->actual_length);
562                         tty_flip_buffer_push(&port->port);
563
564                         usb_serial_debug_data(&port->dev, __func__,
565                                               urb->actual_length, data);
566                 } else {
567                         dev_dbg(&port->dev, "%s: empty read urb"
568                                 " received\n", __func__);
569                 }
570         }
571
572         /* Resubmit urb so we continue receiving */
573         if (status != -ESHUTDOWN && status != -EPERM) {
574                 usb_mark_last_busy(port->serial->dev);
575                 err = usb_submit_urb(urb, GFP_ATOMIC);
576                 if (err && err != -EPERM)
577                         dev_err(&port->dev, "resubmit read urb failed."
578                                 "(%d)\n", err);
579         }
580 }
581
582 static void sierra_instat_callback(struct urb *urb)
583 {
584         int err;
585         int status = urb->status;
586         struct usb_serial_port *port =  urb->context;
587         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
588         struct usb_serial *serial = port->serial;
589
590         dev_dbg(&port->dev, "%s: urb %p port %p has data %p\n", __func__,
591                 urb, port, portdata);
592
593         if (status == 0) {
594                 struct usb_ctrlrequest *req_pkt =
595                                 (struct usb_ctrlrequest *)urb->transfer_buffer;
596
597                 if (!req_pkt) {
598                         dev_dbg(&port->dev, "%s: NULL req_pkt\n",
599                                 __func__);
600                         return;
601                 }
602                 if ((req_pkt->bRequestType == 0xA1) &&
603                                 (req_pkt->bRequest == 0x20)) {
604                         int old_dcd_state;
605                         unsigned char signals = *((unsigned char *)
606                                         urb->transfer_buffer +
607                                         sizeof(struct usb_ctrlrequest));
608
609                         dev_dbg(&port->dev, "%s: signal x%x\n", __func__,
610                                 signals);
611
612                         old_dcd_state = portdata->dcd_state;
613                         portdata->cts_state = 1;
614                         portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
615                         portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
616                         portdata->ri_state = ((signals & 0x08) ? 1 : 0);
617
618                         if (old_dcd_state && !portdata->dcd_state)
619                                 tty_port_tty_hangup(&port->port, true);
620                 } else {
621                         dev_dbg(&port->dev, "%s: type %x req %x\n",
622                                 __func__, req_pkt->bRequestType,
623                                 req_pkt->bRequest);
624                 }
625         } else
626                 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
627
628         /* Resubmit urb so we continue receiving IRQ data */
629         if (status != -ESHUTDOWN && status != -ENOENT) {
630                 usb_mark_last_busy(serial->dev);
631                 err = usb_submit_urb(urb, GFP_ATOMIC);
632                 if (err && err != -EPERM)
633                         dev_err(&port->dev, "%s: resubmit intr urb "
634                                 "failed. (%d)\n", __func__, err);
635         }
636 }
637
638 static int sierra_write_room(struct tty_struct *tty)
639 {
640         struct usb_serial_port *port = tty->driver_data;
641         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
642         unsigned long flags;
643
644         /* try to give a good number back based on if we have any free urbs at
645          * this point in time */
646         spin_lock_irqsave(&portdata->lock, flags);
647         if (portdata->outstanding_urbs > (portdata->num_out_urbs * 2) / 3) {
648                 spin_unlock_irqrestore(&portdata->lock, flags);
649                 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
650                 return 0;
651         }
652         spin_unlock_irqrestore(&portdata->lock, flags);
653
654         return 2048;
655 }
656
657 static int sierra_chars_in_buffer(struct tty_struct *tty)
658 {
659         struct usb_serial_port *port = tty->driver_data;
660         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
661         unsigned long flags;
662         int chars;
663
664         /* NOTE: This overcounts somewhat. */
665         spin_lock_irqsave(&portdata->lock, flags);
666         chars = portdata->outstanding_urbs * MAX_TRANSFER;
667         spin_unlock_irqrestore(&portdata->lock, flags);
668
669         dev_dbg(&port->dev, "%s - %d\n", __func__, chars);
670
671         return chars;
672 }
673
674 static void sierra_stop_rx_urbs(struct usb_serial_port *port)
675 {
676         int i;
677         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
678
679         for (i = 0; i < portdata->num_in_urbs; i++)
680                 usb_kill_urb(portdata->in_urbs[i]);
681
682         usb_kill_urb(port->interrupt_in_urb);
683 }
684
685 static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
686 {
687         int ok_cnt;
688         int err = -EINVAL;
689         int i;
690         struct urb *urb;
691         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
692
693         ok_cnt = 0;
694         for (i = 0; i < portdata->num_in_urbs; i++) {
695                 urb = portdata->in_urbs[i];
696                 if (!urb)
697                         continue;
698                 err = usb_submit_urb(urb, mem_flags);
699                 if (err) {
700                         dev_err(&port->dev, "%s: submit urb failed: %d\n",
701                                 __func__, err);
702                 } else {
703                         ok_cnt++;
704                 }
705         }
706
707         if (ok_cnt && port->interrupt_in_urb) {
708                 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
709                 if (err) {
710                         dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
711                                 __func__, err);
712                 }
713         }
714
715         if (ok_cnt > 0) /* at least one rx urb submitted */
716                 return 0;
717         else
718                 return err;
719 }
720
721 static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
722                                         int dir, void *ctx, int len,
723                                         gfp_t mem_flags,
724                                         usb_complete_t callback)
725 {
726         struct urb      *urb;
727         u8              *buf;
728
729         urb = usb_alloc_urb(0, mem_flags);
730         if (!urb)
731                 return NULL;
732
733         buf = kmalloc(len, mem_flags);
734         if (buf) {
735                 /* Fill URB using supplied data */
736                 usb_fill_bulk_urb(urb, serial->dev,
737                         usb_sndbulkpipe(serial->dev, endpoint) | dir,
738                         buf, len, callback, ctx);
739
740                 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
741                                 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
742         } else {
743                 sierra_release_urb(urb);
744                 urb = NULL;
745         }
746
747         return urb;
748 }
749
750 static void sierra_close(struct usb_serial_port *port)
751 {
752         int i;
753         struct usb_serial *serial = port->serial;
754         struct sierra_port_private *portdata;
755         struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
756         struct urb *urb;
757
758         portdata = usb_get_serial_port_data(port);
759
760         /*
761          * Need to take susp_lock to make sure port is not already being
762          * resumed, but no need to hold it due to the tty-port initialized
763          * flag.
764          */
765         spin_lock_irq(&intfdata->susp_lock);
766         if (--intfdata->open_ports == 0)
767                 serial->interface->needs_remote_wakeup = 0;
768         spin_unlock_irq(&intfdata->susp_lock);
769
770         for (;;) {
771                 urb = usb_get_from_anchor(&portdata->delayed);
772                 if (!urb)
773                         break;
774                 kfree(urb->transfer_buffer);
775                 usb_free_urb(urb);
776                 usb_autopm_put_interface_async(serial->interface);
777                 spin_lock_irq(&portdata->lock);
778                 portdata->outstanding_urbs--;
779                 spin_unlock_irq(&portdata->lock);
780         }
781
782         sierra_stop_rx_urbs(port);
783         usb_kill_anchored_urbs(&portdata->active);
784
785         for (i = 0; i < portdata->num_in_urbs; i++) {
786                 sierra_release_urb(portdata->in_urbs[i]);
787                 portdata->in_urbs[i] = NULL;
788         }
789
790         usb_autopm_get_interface_no_resume(serial->interface);
791 }
792
793 static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
794 {
795         struct sierra_port_private *portdata;
796         struct usb_serial *serial = port->serial;
797         struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
798         int i;
799         int err;
800         int endpoint;
801         struct urb *urb;
802
803         portdata = usb_get_serial_port_data(port);
804
805         endpoint = port->bulk_in_endpointAddress;
806         for (i = 0; i < portdata->num_in_urbs; i++) {
807                 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
808                                         IN_BUFLEN, GFP_KERNEL,
809                                         sierra_indat_callback);
810                 portdata->in_urbs[i] = urb;
811         }
812         /* clear halt condition */
813         usb_clear_halt(serial->dev,
814                         usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
815
816         err = sierra_submit_rx_urbs(port, GFP_KERNEL);
817         if (err)
818                 goto err_submit;
819
820         spin_lock_irq(&intfdata->susp_lock);
821         if (++intfdata->open_ports == 1)
822                 serial->interface->needs_remote_wakeup = 1;
823         spin_unlock_irq(&intfdata->susp_lock);
824         usb_autopm_put_interface(serial->interface);
825
826         return 0;
827
828 err_submit:
829         sierra_stop_rx_urbs(port);
830
831         for (i = 0; i < portdata->num_in_urbs; i++) {
832                 sierra_release_urb(portdata->in_urbs[i]);
833                 portdata->in_urbs[i] = NULL;
834         }
835
836         return err;
837 }
838
839
840 static void sierra_dtr_rts(struct usb_serial_port *port, int on)
841 {
842         struct sierra_port_private *portdata;
843
844         portdata = usb_get_serial_port_data(port);
845         portdata->rts_state = on;
846         portdata->dtr_state = on;
847
848         sierra_send_setup(port);
849 }
850
851 static int sierra_startup(struct usb_serial *serial)
852 {
853         struct sierra_intf_private *intfdata;
854
855         intfdata = kzalloc(sizeof(*intfdata), GFP_KERNEL);
856         if (!intfdata)
857                 return -ENOMEM;
858
859         spin_lock_init(&intfdata->susp_lock);
860
861         usb_set_serial_data(serial, intfdata);
862
863         /* Set Device mode to D0 */
864         sierra_set_power_state(serial->dev, 0x0000);
865
866         /* Check NMEA and set */
867         if (nmea)
868                 sierra_vsc_set_nmea(serial->dev, 1);
869
870         return 0;
871 }
872
873 static void sierra_release(struct usb_serial *serial)
874 {
875         struct sierra_intf_private *intfdata;
876
877         intfdata = usb_get_serial_data(serial);
878         kfree(intfdata);
879 }
880
881 static int sierra_port_probe(struct usb_serial_port *port)
882 {
883         struct usb_serial *serial = port->serial;
884         struct sierra_port_private *portdata;
885         const struct sierra_iface_info *himemoryp;
886         u8 ifnum;
887
888         portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
889         if (!portdata)
890                 return -ENOMEM;
891
892         spin_lock_init(&portdata->lock);
893         init_usb_anchor(&portdata->active);
894         init_usb_anchor(&portdata->delayed);
895
896         /* Assume low memory requirements */
897         portdata->num_out_urbs = N_OUT_URB;
898         portdata->num_in_urbs  = N_IN_URB;
899
900         /* Determine actual memory requirements */
901         if (serial->num_ports == 1) {
902                 /* Get interface number for composite device */
903                 ifnum = sierra_interface_num(serial);
904                 himemoryp = &typeB_interface_list;
905         } else {
906                 /* This is really the usb-serial port number of the interface
907                  * rather than the interface number.
908                  */
909                 ifnum = port->port_number;
910                 himemoryp = &typeA_interface_list;
911         }
912
913         if (is_himemory(ifnum, himemoryp)) {
914                 portdata->num_out_urbs = N_OUT_URB_HM;
915                 portdata->num_in_urbs  = N_IN_URB_HM;
916         }
917
918         dev_dbg(&port->dev,
919                         "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
920                         ifnum, portdata->num_in_urbs, portdata->num_out_urbs);
921
922         usb_set_serial_port_data(port, portdata);
923
924         return 0;
925 }
926
927 static int sierra_port_remove(struct usb_serial_port *port)
928 {
929         struct sierra_port_private *portdata;
930
931         portdata = usb_get_serial_port_data(port);
932         usb_set_serial_port_data(port, NULL);
933         kfree(portdata);
934
935         return 0;
936 }
937
938 #ifdef CONFIG_PM
939 static void stop_read_write_urbs(struct usb_serial *serial)
940 {
941         int i;
942         struct usb_serial_port *port;
943         struct sierra_port_private *portdata;
944
945         /* Stop reading/writing urbs */
946         for (i = 0; i < serial->num_ports; ++i) {
947                 port = serial->port[i];
948                 portdata = usb_get_serial_port_data(port);
949                 if (!portdata)
950                         continue;
951                 sierra_stop_rx_urbs(port);
952                 usb_kill_anchored_urbs(&portdata->active);
953         }
954 }
955
956 static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
957 {
958         struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
959
960         spin_lock_irq(&intfdata->susp_lock);
961         if (PMSG_IS_AUTO(message)) {
962                 if (intfdata->in_flight) {
963                         spin_unlock_irq(&intfdata->susp_lock);
964                         return -EBUSY;
965                 }
966         }
967         intfdata->suspended = 1;
968         spin_unlock_irq(&intfdata->susp_lock);
969
970         stop_read_write_urbs(serial);
971
972         return 0;
973 }
974
975 /* Caller must hold susp_lock. */
976 static int sierra_submit_delayed_urbs(struct usb_serial_port *port)
977 {
978         struct sierra_port_private *portdata = usb_get_serial_port_data(port);
979         struct sierra_intf_private *intfdata;
980         struct urb *urb;
981         int ec = 0;
982         int err;
983
984         intfdata = usb_get_serial_data(port->serial);
985
986         for (;;) {
987                 urb = usb_get_from_anchor(&portdata->delayed);
988                 if (!urb)
989                         break;
990
991                 usb_anchor_urb(urb, &portdata->active);
992                 intfdata->in_flight++;
993                 err = usb_submit_urb(urb, GFP_ATOMIC);
994                 if (err) {
995                         dev_err(&port->dev, "%s - submit urb failed: %d",
996                                         __func__, err);
997                         ec++;
998                         intfdata->in_flight--;
999                         usb_unanchor_urb(urb);
1000                         kfree(urb->transfer_buffer);
1001                         usb_free_urb(urb);
1002
1003                         spin_lock(&portdata->lock);
1004                         portdata->outstanding_urbs--;
1005                         spin_unlock(&portdata->lock);
1006                 }
1007         }
1008
1009         if (ec)
1010                 return -EIO;
1011
1012         return 0;
1013 }
1014
1015 static int sierra_resume(struct usb_serial *serial)
1016 {
1017         struct usb_serial_port *port;
1018         struct sierra_intf_private *intfdata = usb_get_serial_data(serial);
1019         int ec = 0;
1020         int i, err;
1021
1022         spin_lock_irq(&intfdata->susp_lock);
1023         for (i = 0; i < serial->num_ports; i++) {
1024                 port = serial->port[i];
1025
1026                 if (!tty_port_initialized(&port->port))
1027                         continue;
1028
1029                 err = sierra_submit_delayed_urbs(port);
1030                 if (err)
1031                         ec++;
1032
1033                 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
1034                 if (err)
1035                         ec++;
1036         }
1037         intfdata->suspended = 0;
1038         spin_unlock_irq(&intfdata->susp_lock);
1039
1040         return ec ? -EIO : 0;
1041 }
1042
1043 #else
1044 #define sierra_suspend NULL
1045 #define sierra_resume NULL
1046 #endif
1047
1048 static struct usb_serial_driver sierra_device = {
1049         .driver = {
1050                 .owner =        THIS_MODULE,
1051                 .name =         "sierra",
1052         },
1053         .description       = "Sierra USB modem",
1054         .id_table          = id_table,
1055         .calc_num_ports    = sierra_calc_num_ports,
1056         .probe             = sierra_probe,
1057         .open              = sierra_open,
1058         .close             = sierra_close,
1059         .dtr_rts           = sierra_dtr_rts,
1060         .write             = sierra_write,
1061         .write_room        = sierra_write_room,
1062         .chars_in_buffer   = sierra_chars_in_buffer,
1063         .tiocmget          = sierra_tiocmget,
1064         .tiocmset          = sierra_tiocmset,
1065         .attach            = sierra_startup,
1066         .release           = sierra_release,
1067         .port_probe        = sierra_port_probe,
1068         .port_remove       = sierra_port_remove,
1069         .suspend           = sierra_suspend,
1070         .resume            = sierra_resume,
1071         .read_int_callback = sierra_instat_callback,
1072 };
1073
1074 static struct usb_serial_driver * const serial_drivers[] = {
1075         &sierra_device, NULL
1076 };
1077
1078 module_usb_serial_driver(serial_drivers, id_table);
1079
1080 MODULE_AUTHOR(DRIVER_AUTHOR);
1081 MODULE_DESCRIPTION(DRIVER_DESC);
1082 MODULE_LICENSE("GPL");
1083
1084 module_param(nmea, bool, S_IRUGO | S_IWUSR);
1085 MODULE_PARM_DESC(nmea, "NMEA streaming");