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