blob: e6847050d421b7eee2ab3f61d8db56eae14339ad [file] [log] [blame]
flintman140747e2014-06-04 21:20:26 -04001/*
2 USB Driver for Sierra Wireless
3
4 Copyright (C) 2006, 2007, 2008 Kevin Lloyd <klloyd@sierrawireless.com>,
5
6 Copyright (C) 2008 - 2011 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/* Uncomment to force power level set to auto when attaching a device */
22/*#define POWER_LEVEL_AUTO*/
23
24/* Sierra driver for kernel 2.6.35 to kernel-3.0 */
25/* Note: This is 'combined' usb serial driver developed specifically for Android
26 * platform. This driver handles the usb serial interfaces of QMI/Gobi3K devices
27 * in addition to the devices handled by the 'traditional' sierra driver.
28 * When this driver is used GobiSerial driver must be removed to avoid
29 * duplication!
30 */
31#define DRIVER_VERSION "v.1.7.42_android_generic_2"
32#define DRIVER_AUTHOR "Kevin Lloyd, Elina Pasheva, Matthew Safar, Rory Filer"
33#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
34
35#include <linux/version.h>
36#include <linux/kernel.h>
37#include <linux/jiffies.h>
38#include <linux/errno.h>
39#include <linux/tty.h>
40#include <linux/slab.h>
41#include <linux/tty_flip.h>
42#include <linux/module.h>
43#include <linux/usb.h>
44#include <linux/usb/serial.h>
45#include <asm/unaligned.h>
46
47#define SWIMS_USB_REQUEST_SetPower 0x00
48#define SWIMS_USB_REQUEST_GetFwAttr 0x06
49#define SWIMS_USB_REQUEST_SetNmea 0x07
50#define USB_REQUEST_TYPE_CLASS 0xA1
51#define USB_REQUEST_IFACE 0x20
52
53#define N_IN_URB_HM 8
54#define N_OUT_URB_HM 64
55#define N_IN_URB 4
56#define N_OUT_URB 4
57#define IN_BUFLEN 4096
58
59#define MAX_TRANSFER (PAGE_SIZE - 512)
60/* MAX_TRANSFER is chosen so that the VM is not stressed by
61 allocations > PAGE_SIZE and the number of packets in a page
62 is an integer 512 is the largest possible packet on EHCI */
63
64#define SWI_FW_ATTR_PM_MASK 0x02
65/* PORTION_LEN defines the length of device attribute buffer */
66#define PORTION_LEN 4096
67
68static bool debug;
69static bool nmea;
70
71/* sysfs attributes */
72static int sierra_create_sysfs_attrs(struct usb_serial_port *port);
73static int sierra_remove_sysfs_attrs(struct usb_serial_port *port);
74
75/* Used in interface blacklisting */
76struct sierra_iface_info {
77 const u32 infolen; /* number of interface numbers on blacklist */
78 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
79};
80
81/* per interface statistics */
82struct sierra_intf_stats {
83 atomic_t rx_bytes; /* received bytes */
84 atomic_t indat_cb_cnt; /* indat callback count */
85 atomic_t indat_cb_fail; /* indat cb with error */
86
87 atomic_t tx_bytes; /* transmitted bytes */
88 atomic_t write_cnt; /* no. of writes */
89 atomic_t write_err; /* no. of failed writes */
90
91 atomic_t delayed_writes; /* no. of delayed writes */
92 atomic_t delayed_write_err; /* no. of delayed write errs */
93
94 atomic_t outdat_cb_cnt; /* outdat callback count */
95 atomic_t outdat_cb_fail; /* outdat cb with error */
96
97};
98
99struct sierra_intf_private {
100 spinlock_t susp_lock;
101 unsigned int suspended:1;
102 int in_flight;
103
104 struct sierra_intf_stats stats;
105};
106
107static int is_qmi_gobi_device(struct usb_device *udev)
108{
109 switch (udev->descriptor.idProduct)
110 {
111 case 0x920c: /* Gobi 3000 QDL */
112 case 0x920d: /* Gobi 3000 Composite */
113 case 0x68A2:
114 case 0x9010:
115 case 0x9011:
116 case 0x9012:
117 case 0x9013:
118 case 0x9014:
119 case 0x9015:
120 case 0x9018:
121 case 0x9019:
122 case 0x361D:
123 case 0x371D:
124 return 1;
125 break;
126
127 default:
128 return 0;
129 break;
130 }
131 return 0;
132}
133
134static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
135{
136 int result;
137 dev_dbg(&udev->dev, "%s\n", __func__);
138 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
139 SWIMS_USB_REQUEST_SetPower, /* __u8 request */
140 USB_TYPE_VENDOR, /* __u8 request type */
141 swiState, /* __u16 value */
142 0, /* __u16 index */
143 NULL, /* void *data */
144 0, /* __u16 size */
145 USB_CTRL_SET_TIMEOUT); /* int timeout */
146 return result;
147}
148
149static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
150{
151 int result = 0; /* assume successful return */
152 dev_dbg(&udev->dev, "%s\n", __func__);
153
154 /* exclude QMI and Gobi devices */
155 switch (udev->descriptor.idProduct)
156 {
157 case 0x920c: /* Gobi 3000 QDL */
158 case 0x920d: /* Gobi 3000 Composite */
159 case 0x68A2:
160 case 0x9010:
161 case 0x9011:
162 case 0x9012:
163 case 0x9013:
164 case 0x9014:
165 case 0x9015:
166 case 0x9018:
167 case 0x9019:
168 case 0x361D:
169 case 0x371D:
170 /* For these GPS will be enabled with different command
171 * during port open
172 */
173 break;
174
175 default:
176 /* Send vendor specific command to enable NMEA */
177 result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
178 SWIMS_USB_REQUEST_SetNmea, /* __u8 request */
179 USB_TYPE_VENDOR, /* __u8 request type */
180 enable, /* __u16 value */
181 0x0000, /* __u16 index */
182 NULL, /* void *data */
183 0, /* __u16 size */
184 USB_CTRL_SET_TIMEOUT); /* int timeout */
185 break;
186 }
187 return result;
188}
189
190static int sierra_get_fw_attr(struct usb_device *udev, u16 *data)
191{
192 int result;
193 u16 *attrdata;
194
195 dev_dbg(&udev->dev, "%s\n", __func__);
196
197 attrdata = kmalloc(sizeof(*attrdata), GFP_KERNEL);
198 if (!attrdata)
199 return -ENOMEM;
200
201 result = usb_control_msg(udev,
202 usb_rcvctrlpipe(udev, 0),
203 SWIMS_USB_REQUEST_GetFwAttr, /* __u8 request*/
204 USB_TYPE_VENDOR | USB_DIR_IN, /* request type*/
205 0x0000, /* __u16 value */
206 0x0000, /* __u16 index */
207 attrdata, /* void *data */
208 sizeof(*attrdata), /* _u16 size */
209 USB_CTRL_SET_TIMEOUT); /* in timeout */
210
211 if (result < 0) {
212 kfree(attrdata);
213 return -EIO;
214 }
215
216 *data = *attrdata;
217
218 kfree(attrdata);
219 return result;
220}
221
222static int sierra_calc_num_ports(struct usb_serial *serial)
223{
224 int num_ports = 0;
225 u8 ifnum, numendpoints;
226
227 dev_dbg(&serial->dev->dev, "%s\n", __func__);
228
229 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
230 numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
231
232 /* Dummy interface present on some SKUs should be ignored */
233 if (ifnum == 0x99)
234 num_ports = 0;
235 else if (numendpoints <= 3)
236 num_ports = 1;
237 else
238 num_ports = (numendpoints-1)/2;
239 return num_ports;
240}
241
242static int is_blacklisted(const u8 ifnum,
243 const struct sierra_iface_info *blacklist)
244{
245 const u8 *info;
246 int i;
247
248 if (blacklist) {
249 info = blacklist->ifaceinfo;
250
251 for (i = 0; i < blacklist->infolen; i++) {
252 if (info[i] == ifnum)
253 return 1;
254 }
255 }
256 return 0;
257}
258
259static int is_himemory(const u8 ifnum,
260 const struct sierra_iface_info *himemorylist)
261{
262 const u8 *info;
263 int i;
264
265 if (himemorylist) {
266 info = himemorylist->ifaceinfo;
267
268 for (i=0; i < himemorylist->infolen; i++) {
269 if (info[i] == ifnum)
270 return 1;
271 }
272 }
273 return 0;
274}
275
276static int sierra_calc_interface(struct usb_serial *serial)
277{
278 int interface;
279 struct usb_interface *p_interface;
280 struct usb_host_interface *p_host_interface;
281
282 /* Get the interface structure pointer from the serial struct */
283 p_interface = serial->interface;
284
285 /* Get a pointer to the host interface structure */
286 p_host_interface = p_interface->cur_altsetting;
287
288 /* read the interface descriptor for this active altsetting
289 * to find out the interface number we are on */
290 interface = p_host_interface->desc.bInterfaceNumber;
291
292 return interface;
293}
294
295static int is_gps_port_qmi_gobi(struct usb_serial_port * pPort )
296{
297 switch (pPort->serial->dev->descriptor.idProduct)
298 {
299 case 0x68A2: /* Sierra Wireless QMI */
300 case 0x68A3: /* Sierra Wireless DIP */
301 if (pPort->serial->interface->cur_altsetting->desc.bInterfaceNumber == 2)
302 return 1;
303 break;
304 /* Check PIDs indicating non-boot mode */
305 case 0x9011: /* Sierra Wireless G3K */
306 case 0x9013: /* Sierra Wireless G3K */
307 case 0x9015: /* Sierra Wireless G3K */
308 case 0x9019: /* Sierra Wireless G3K */
309 case 0x371D: /* G3K */
310 if (pPort->serial->interface->cur_altsetting->desc.bInterfaceNumber == 3)
311 return 1;
312 break;
313
314 default:
315 return 0;
316 break;
317 }
318 return 0;
319}
320
321static int sierra_probe(struct usb_serial *serial,
322 const struct usb_device_id *id)
323{
324 int result = 0;
325 struct usb_device *udev;
326 struct sierra_intf_private *intfdata;
327 u8 ifnum;
328
329 udev = serial->dev;
330 dev_dbg(&udev->dev, "%s\n", __func__);
331
332 ifnum = sierra_calc_interface(serial);
333 /*
334 * If this interface supports more than 1 alternate
335 * select the 2nd one
336 */
337 if (serial->interface->num_altsetting == 2) {
338 dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
339 ifnum);
340 /* We know the alternate setting is for composite USB interface
341 * modems
342 */
343 usb_set_interface(udev, ifnum, 1);
344 }
345
346 /* ifnum could have changed - by calling usb_set_interface */
347 ifnum = sierra_calc_interface(serial);
348
349 if (is_blacklisted(ifnum,
350 (struct sierra_iface_info *)id->driver_info)) {
351 dev_dbg(&serial->dev->dev,
352 "Ignoring blacklisted interface #%d\n", ifnum);
353 return -ENODEV;
354 }
355
356 intfdata = serial->private = kzalloc(sizeof(struct sierra_intf_private),
357 GFP_KERNEL);
358 if (!intfdata)
359 return -ENOMEM;
360 spin_lock_init(&intfdata->susp_lock);
361
362 return result;
363}
364
365/* interfaces with higher memory requirements */
366static const u8 hi_memory_typeA_ifaces[] = { 0 };
367static const struct sierra_iface_info typeA_interface_list = {
368 .infolen = ARRAY_SIZE(hi_memory_typeA_ifaces),
369 .ifaceinfo = hi_memory_typeA_ifaces,
370};
371
372static const u8 hi_memory_typeB_ifaces[] = { 2, 3, 4, 5, 6 };
373static const struct sierra_iface_info typeB_interface_list = {
374 .infolen = ARRAY_SIZE(hi_memory_typeB_ifaces),
375 .ifaceinfo = hi_memory_typeB_ifaces,
376};
377
378/* 'blacklist' of interfaces not served by this driver */
379static const u8 direct_ip_non_serial_ifaces[] = { 7, 8, 9, 10, 11 };
380static const struct sierra_iface_info direct_ip_interface_blacklist = {
381 .infolen = ARRAY_SIZE( direct_ip_non_serial_ifaces ),
382 .ifaceinfo = direct_ip_non_serial_ifaces,
383};
384
385static const signed char qmi_non_serial_ifaces[] = { 8 };
386static const struct sierra_iface_info qmi_interface_blacklist = {
387 .infolen = ARRAY_SIZE( qmi_non_serial_ifaces ),
388 .ifaceinfo = qmi_non_serial_ifaces,
389};
390
391static const signed char gobi_non_serial_ifaces[] = { 0 };
392static const struct sierra_iface_info gobi_interface_blacklist = {
393 .infolen = ARRAY_SIZE( gobi_non_serial_ifaces ),
394 .ifaceinfo = gobi_non_serial_ifaces,
395};
396
397
398static const struct usb_device_id id_table [] = {
399 { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
400 { USB_DEVICE(0x03F0, 0x1B1D) }, /* HP ev2200 a.k.a MC5720 */
401 { USB_DEVICE(0x03F0, 0x1E1D) }, /* HP hs2300 a.k.a MC8775 */
402 { USB_DEVICE(0x03F0, 0x211D) }, /* HP ev2210 a.k.a MC5725 */
403
404 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
405 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
406 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
407 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
408 { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
409 { USB_DEVICE(0x1199, 0x0022) }, /* Sierra Wireless EM5725 */
410 { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
411 { USB_DEVICE(0x1199, 0x0224) }, /* Sierra Wireless MC5727 */
412 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
413 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
414 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
415 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
416 { USB_DEVICE(0x1199, 0x0301) }, /* Sierra Wireless USB Dongle 250U/3G */
417 /* Sierra Wireless MC5728 */
418 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0400, 0xFF, 0xFF, 0xFF) },
419
420 /* Sierra Wireless C597 */
421 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
422 /* Sierra Wireless T598 */
423 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
424 { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless T11 */
425 { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless AC402 */
426 { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless MC5728 */
427 { USB_DEVICE(0x114F, 0x6000) }, /* Sierra Wireless Q26 Elite */
428
429 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
430 { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
431 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
432 { USB_DEVICE(0x1199, 0x6805) }, /* Sierra Wireless MC8765 */
433 { USB_DEVICE(0x1199, 0x6808) }, /* Sierra Wireless MC8755 */
434 { USB_DEVICE(0x1199, 0x6809) }, /* Sierra Wireless MC8765 */
435 { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
436 { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 */
437 { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
438 { USB_DEVICE(0x1199, 0x6816) }, /* Sierra Wireless MC8775 */
439 { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
440 { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
441 { USB_DEVICE(0x1199, 0x6822) }, /* Sierra Wireless AirCard 875E */
442 { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
443 { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
444 { USB_DEVICE(0x1199, 0x6834) }, /* Sierra Wireless MC8780 */
445 { USB_DEVICE(0x1199, 0x6835) }, /* Sierra Wireless MC8781 */
446 { USB_DEVICE(0x1199, 0x6838) }, /* Sierra Wireless MC8780 */
447 { USB_DEVICE(0x1199, 0x6839) }, /* Sierra Wireless MC8781 */
448 { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
449 { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
450 /* Sierra Wireless MC8790, MC8791, MC8792 Composite */
451 { USB_DEVICE(0x1199, 0x683C) },
452 { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8791 Composite */
453 /* Sierra Wireless MC8790, MC8791, MC8792 */
454 { USB_DEVICE(0x1199, 0x683E) },
455 { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
456 { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
457 { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
458 { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
459 { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
460 { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
461 { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
462 { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
463 /* Sierra Wireless C885 */
464 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
465 /* Sierra Wireless C888, Air Card 501, USB 303, USB 304 */
466 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
467 /* Sierra Wireless C22/C33 */
468 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
469 /* Sierra Wireless HSPA Non-Composite Device */
470 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
471 { USB_DEVICE(0x1199, 0x6893) }, /* Sierra Wireless Device */
472 /* Sierra Wireless Direct IP modems */
473 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68A3, 0xFF, 0xFF, 0xFF),
474 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
475 },
476 /* AT&T Direct IP modems */
477 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68A3, 0xFF, 0xFF, 0xFF),
478 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
479 },
480 /* Sierra Wireless Direct IP LTE modems */
481 { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x68AA, 0xFF, 0xFF, 0xFF),
482 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
483 },
484 /* AT&T Direct IP LTE modems */
485 { USB_DEVICE_AND_INTERFACE_INFO(0x0F3D, 0x68AA, 0xFF, 0xFF, 0xFF),
486 .driver_info = (kernel_ulong_t)&direct_ip_interface_blacklist
487 },
488 /* Wireless 5720 VZW Mobile Broadband (EVDO Rev-A) Minicard GPS Port */
489 { USB_DEVICE(0x413C, 0x8133) },
490 /* Combined with QMI/Gobi devices */
491 { USB_DEVICE(0x05c6, 0x920c) }, /* Gobi 3000 QDL */
492 { USB_DEVICE(0x05c6, 0x920d) }, /* Gobi 3000 Composite */
493 /* Sierra Wireless QMI VID/PID */
494 { USB_DEVICE(0x1199, 0x68A2),
495 .driver_info = (kernel_ulong_t)&qmi_interface_blacklist
496 },
497 /* Sierra Wireless G3K Boot VID/PID */
498 { USB_DEVICE(0x1199, 0x9010) },
499 /* Sierra Wireless G3K Device Application VID/PID */
500 { USB_DEVICE(0x1199, 0x9011),
501 .driver_info = (kernel_ulong_t)&gobi_interface_blacklist
502 },
503 /* Sierra Wireless G3K Boot VID/PID */
504 { USB_DEVICE(0x1199, 0x9012) },
505 /* Sierra Wireless G3K Application VID/PID */
506 { USB_DEVICE(0x1199, 0x9013),
507 .driver_info = (kernel_ulong_t)&gobi_interface_blacklist
508 },
509 /* Sierra Wireless G3K Boot VID/PID */
510 { USB_DEVICE(0x1199, 0x9014) },
511 /* Sierra Wireless G3K Application VID/PID */
512 { USB_DEVICE(0x1199, 0x9015),
513 .driver_info = (kernel_ulong_t)&gobi_interface_blacklist
514 },
515 /* Sierra Wireless G3K Boot VID/PID */
516 { USB_DEVICE(0x1199, 0x9018) },
517 /* Sierra Wireless G3K Application VID/PID */
518 { USB_DEVICE(0x1199, 0x9019),
519 .driver_info = (kernel_ulong_t)&gobi_interface_blacklist
520 },
521 /* G3K Boot VID/PID */
522 { USB_DEVICE(0x03F0, 0x361D) },
523 /* G3K Application VID/PID */
524 { USB_DEVICE(0x03F0, 0x371D),
525 .driver_info = (kernel_ulong_t)&gobi_interface_blacklist
526 },
527
528 { }
529};
530MODULE_DEVICE_TABLE(usb, id_table);
531
532/* per port private data */
533struct sierra_port_private {
534 spinlock_t lock; /* lock the structure */
535 int outstanding_urbs; /* number of out urbs in flight */
536
537 struct usb_anchor active;
538 struct usb_anchor delayed;
539
540 int num_out_urbs;
541 int num_in_urbs;
542 /* Input endpoints and buffers for this port */
543 struct urb *in_urbs[N_IN_URB_HM];
544
545 /* Settings for the port */
546 int rts_state; /* Handshaking pins (outputs) */
547 int dtr_state;
548 int cts_state; /* Handshaking pins (inputs) */
549 int dsr_state;
550 int dcd_state;
551 int ri_state;
552 unsigned int opened:1;
553};
554
555static int sierra_send_setup(struct usb_serial_port *port)
556{
557 struct usb_serial *serial = port->serial;
558 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
559 __u16 interface = 0;
560 int val = 0;
561 int do_send = 0;
562 int retval;
563
564 dev_dbg(&port->dev, "%s\n", __func__);
565
566 if (portdata->dtr_state)
567 val |= 0x01;
568 if (portdata->rts_state)
569 val |= 0x02;
570
571 /* If composite device then properly report interface */
572 if (serial->num_ports == 1) {
573 interface = sierra_calc_interface(serial);
574 /* Control message is sent only to interfaces with
575 * interrupt_in endpoints
576 */
577 if (port->interrupt_in_urb) {
578 /* send control message */
579 do_send = 1;
580 }
581 }
582
583 /* Otherwise the need to do non-composite mapping */
584 else {
585 if (port->bulk_out_endpointAddress == 2)
586 interface = 0;
587 else if (port->bulk_out_endpointAddress == 4)
588 interface = 1;
589 else if (port->bulk_out_endpointAddress == 5)
590 interface = 2;
591
592 do_send = 1;
593 }
594 if (!do_send)
595 return 0;
596
597 usb_autopm_get_interface(serial->interface);
598 retval = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
599 0x22, 0x21, val, interface, NULL, 0, USB_CTRL_SET_TIMEOUT);
600 usb_autopm_put_interface(serial->interface);
601
602 return retval;
603}
604
605static void sierra_set_termios(struct tty_struct *tty,
606 struct usb_serial_port *port, struct ktermios *old_termios)
607{
608 dev_dbg(&port->dev, "%s\n", __func__);
609 tty_termios_copy_hw(tty->termios, old_termios);
610 sierra_send_setup(port);
611}
612
613#if (LINUX_VERSION_CODE >= KERNEL_VERSION( 2,6,39 ))
614static int sierra_tiocmget(struct tty_struct *tty)
615#else
616static int sierra_tiocmget(struct tty_struct *tty, struct file *file)
617#endif
618{
619 struct usb_serial_port *port = tty->driver_data;
620 unsigned int value;
621 struct sierra_port_private *portdata;
622
623 dev_dbg(&port->dev, "%s\n", __func__);
624 portdata = usb_get_serial_port_data(port);
625
626 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
627 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
628 ((portdata->cts_state) ? TIOCM_CTS : 0) |
629 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
630 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
631 ((portdata->ri_state) ? TIOCM_RNG : 0);
632
633 return value;
634}
635
636#if (LINUX_VERSION_CODE >= KERNEL_VERSION( 2,6,39 ))
637static int sierra_tiocmset(struct tty_struct *tty,
638 unsigned int set, unsigned int clear)
639#else
640static int sierra_tiocmset(struct tty_struct *tty, struct file *file,
641 unsigned int set, unsigned int clear)
642#endif
643{
644 struct usb_serial_port *port = tty->driver_data;
645 struct sierra_port_private *portdata;
646
647 portdata = usb_get_serial_port_data(port);
648
649 if (set & TIOCM_RTS)
650 portdata->rts_state = 1;
651 if (set & TIOCM_DTR)
652 portdata->dtr_state = 1;
653
654 if (clear & TIOCM_RTS)
655 portdata->rts_state = 0;
656 if (clear & TIOCM_DTR)
657 portdata->dtr_state = 0;
658 return sierra_send_setup(port);
659}
660
661static void sierra_release_urb(struct urb *urb)
662{
663 struct usb_serial_port *port;
664 if (urb) {
665 port = urb->context;
666 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
667 usb_free_urb(urb);
668 }
669}
670
671/* Sysfs Attributes */
672
673static ssize_t show_suspend_status(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
676 struct usb_serial_port *port;
677 struct sierra_port_private *portdata;
678 struct sierra_intf_private *intfdata;
679 unsigned long flags;
680 unsigned int flag_suspended = 0;
681
682 port = to_usb_serial_port(dev);
683 portdata = usb_get_serial_port_data(port);
684 intfdata = port->serial->private;
685
686 spin_lock_irqsave(&intfdata->susp_lock, flags);
687 flag_suspended = intfdata->suspended;
688 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
689
690 return snprintf(buf, PORTION_LEN, "%i\n", flag_suspended);
691}
692
693static ssize_t show_stats(struct device *dev,
694 struct device_attribute *attr, char *buf)
695{
696 struct usb_serial_port *port;
697 struct sierra_intf_private *intfdata;
698
699 port = to_usb_serial_port(dev);
700 intfdata = port->serial->private;
701
702 return snprintf(buf, PORTION_LEN,
703 "rx: %i B\tindat: %i\tindat err: %i\n"
704 "tx: %i B\toutdat: %i\toutdat err: %i\n"
705 "writes: %i\t\twrite err: %i\n"
706 "delayed writes: %i\tdelayed write err: %i\n",
707 atomic_read(&intfdata->stats.rx_bytes), atomic_read(&intfdata->stats.indat_cb_cnt), atomic_read(&intfdata->stats.indat_cb_fail),
708 atomic_read(&intfdata->stats.tx_bytes), atomic_read(&intfdata->stats.outdat_cb_cnt), atomic_read(&intfdata->stats.outdat_cb_fail),
709 atomic_read(&intfdata->stats.write_cnt), atomic_read(&intfdata->stats.write_err),
710 atomic_read(&intfdata->stats.delayed_writes), atomic_read(&intfdata->stats.delayed_write_err)
711 );
712}
713
714/* Read only suspend status */
715static DEVICE_ATTR(suspend_status, S_IWUSR | S_IRUGO, show_suspend_status,
716 NULL);
717
718/* Read only statistics */
719static DEVICE_ATTR(stats, S_IWUSR | S_IRUGO, show_stats, NULL);
720
721static int sierra_create_sysfs_attrs(struct usb_serial_port *port)
722{
723 int result = 0;
724
725 result = device_create_file(&port->dev, &dev_attr_stats);
726 if (unlikely (result < 0))
727 return result;
728 return device_create_file(&port->dev, &dev_attr_suspend_status);
729}
730
731static int sierra_remove_sysfs_attrs(struct usb_serial_port *port)
732{
733 device_remove_file(&port->dev, &dev_attr_stats);
734 device_remove_file(&port->dev, &dev_attr_suspend_status);
735 return 0;
736}
737
738static void sierra_outdat_callback(struct urb *urb)
739{
740 struct usb_serial_port *port = urb->context;
741 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
742 struct sierra_intf_private *intfdata;
743 int status = urb->status;
744
745 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
746 intfdata = port->serial->private;
747
748 usb_autopm_put_interface_async(port->serial->interface);
749
750 atomic_inc(&intfdata->stats.outdat_cb_cnt);
751
752 if (status) {
753 dev_dbg(&port->dev, "%s - nonzero write bulk status "
754 "received: %d\n", __func__, status);
755 atomic_inc(&intfdata->stats.outdat_cb_fail);
756 }
757
758 spin_lock(&portdata->lock);
759 --portdata->outstanding_urbs;
760 spin_unlock(&portdata->lock);
761
762 spin_lock(&intfdata->susp_lock);
763 --intfdata->in_flight;
764 spin_unlock(&intfdata->susp_lock);
765
766 usb_serial_port_softint(port);
767}
768
769/* Write */
770static int sierra_write(struct tty_struct *tty,
771 struct usb_serial_port *port,
772 const unsigned char *buf, int count)
773{
774 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
775 struct sierra_intf_private *intfdata;
776 struct usb_serial *serial = port->serial;
777 unsigned long flags;
778 unsigned char *buffer;
779 struct urb *urb;
780 size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
781 int retval = 0;
782
783 /* verify that we actually have some data to write */
784 if (count == 0)
785 return 0;
786
787 dev_dbg(&port->dev, "%s: write (%zu bytes)\n", __func__, writesize);
788
789 intfdata = serial->private;
790
791 spin_lock_irqsave(&portdata->lock, flags);
792 if (portdata->outstanding_urbs > portdata->num_out_urbs) {
793 spin_unlock_irqrestore(&portdata->lock, flags);
794 dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
795 return 0;
796 }
797 portdata->outstanding_urbs++;
798 spin_unlock_irqrestore(&portdata->lock, flags);
799
800 retval = usb_autopm_get_interface_async(serial->interface);
801 if (unlikely(retval < 0)) {
802 spin_lock_irqsave(&portdata->lock, flags);
803 portdata->outstanding_urbs--;
804 spin_unlock_irqrestore(&portdata->lock, flags);
805 return retval;
806 }
807
808 buffer = kmalloc(writesize, GFP_ATOMIC);
809 if (!buffer) {
810 dev_err(&port->dev, "out of memory\n");
811 spin_lock_irqsave(&portdata->lock, flags);
812 --portdata->outstanding_urbs;
813 spin_unlock_irqrestore(&portdata->lock, flags);
814 usb_autopm_put_interface_async(serial->interface);
815 return -ENOMEM;
816 }
817
818 urb = usb_alloc_urb(0, GFP_ATOMIC);
819 if (!urb) {
820 dev_err(&port->dev, "no more free urbs\n");
821 kfree(buffer);
822 spin_lock_irqsave(&portdata->lock, flags);
823 --portdata->outstanding_urbs;
824 spin_unlock_irqrestore(&portdata->lock, flags);
825 usb_autopm_put_interface_async(serial->interface);
826 return -ENOMEM;
827 }
828
829 memcpy(buffer, buf, writesize);
830
831 usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
832
833 usb_fill_bulk_urb(urb, serial->dev,
834 usb_sndbulkpipe(serial->dev,
835 port->bulk_out_endpointAddress),
836 buffer, writesize, sierra_outdat_callback, port);
837
838 /* Handle the need to send a zero length packet and release the
839 * transfer buffer
840 */
841 urb->transfer_flags |= (URB_ZERO_PACKET | URB_FREE_BUFFER);
842
843 spin_lock_irqsave(&intfdata->susp_lock, flags);
844
845 if (intfdata->suspended) {
846 usb_anchor_urb(urb, &portdata->delayed);
847 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
848 /* release our reference to this urb, the USB core will
849 * eventually free it entirely */
850 usb_free_urb(urb);
851 return writesize;
852 }
853 usb_anchor_urb(urb, &portdata->active);
854
855 /* send it down the pipe */
856 retval = usb_submit_urb(urb, GFP_ATOMIC);
857 if (retval) {
858 usb_unanchor_urb(urb);
859 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
860
861 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
862 "with status = %d\n", __func__, retval);
863 usb_free_urb(urb);
864 spin_lock_irqsave(&portdata->lock, flags);
865 --portdata->outstanding_urbs;
866 spin_unlock_irqrestore(&portdata->lock, flags);
867 usb_autopm_put_interface_async(serial->interface);
868 atomic_inc(&intfdata->stats.write_err);
869 return retval;
870 } else {
871 intfdata->in_flight++;
872 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
873 atomic_inc(&intfdata->stats.write_cnt);
874 atomic_add(writesize, &intfdata->stats.tx_bytes);
875 }
876 /* release our reference to this urb, the USB core will eventually
877 * free it entirely */
878 usb_free_urb(urb);
879
880 return writesize;
881}
882
883static void sierra_indat_callback(struct urb *urb)
884{
885 int err;
886 int endpoint;
887 struct usb_serial_port *port = urb->context;
888 struct tty_struct *tty;
889 struct sierra_intf_private *intfdata;
890 unsigned char *data = urb->transfer_buffer;
891 int status = urb->status;
892
893 endpoint = usb_pipeendpoint(urb->pipe);
894
895 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
896
897 intfdata = port->serial->private;
898
899 atomic_inc(&intfdata->stats.indat_cb_cnt); /* indat calls */
900
901 if (status) {
902 dev_dbg(&port->dev, "%s: nonzero status: %d on"
903 " endpoint %02x\n", __func__, status, endpoint);
904 atomic_inc(&intfdata->stats.indat_cb_fail); /* indat fails */
905 } else {
906 if (urb->actual_length) {
907 tty = tty_port_tty_get(&port->port);
908 if (tty) {
909 tty_insert_flip_string(tty, data,
910 urb->actual_length);
911 tty_flip_buffer_push(tty);
912
913 tty_kref_put(tty);
914 /* tty invalid after this point */
915 /* rx'd bytes */
916 atomic_add(urb->actual_length,
917 &intfdata->stats.rx_bytes);
918 usb_serial_debug_data(debug, &port->dev,
919 __func__, urb->actual_length, data);
920 }
921 } else {
922 dev_dbg(&port->dev, "%s: empty read urb"
923 " received\n", __func__);
924 }
925 }
926
927 /* Resubmit urb so we continue receiving */
928 if (status != -ESHUTDOWN && status != -ENOENT && status != -ENODEV) {
929 usb_mark_last_busy(port->serial->dev);
930 err = usb_submit_urb(urb, GFP_ATOMIC);
931 if (err && err != -ENODEV)
932 dev_err(&port->dev, "resubmit read urb failed."
933 "(%d)\n", err);
934 }
935
936 return;
937}
938
939static void sierra_instat_callback(struct urb *urb)
940{
941 int err;
942 int status = urb->status;
943 struct usb_serial_port *port = urb->context;
944 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
945 struct usb_serial *serial = port->serial;
946
947 dev_dbg(&port->dev, "%s: %p\n", __func__, urb);
948
949 if (status == 0) {
950 struct usb_ctrlrequest *req_pkt =
951 (struct usb_ctrlrequest *)urb->transfer_buffer;
952
953 const u16 *sigp = (u16 *)(req_pkt + 1);
954 /* usb_ctrlrequest we parsed is followed by two bytes of data
955 * make sure we received that many bytes
956 */
957 if (urb->actual_length >= sizeof(*req_pkt) + sizeof(*sigp) &&
958 req_pkt->bRequestType == USB_REQUEST_TYPE_CLASS &&
959 req_pkt->bRequest == USB_REQUEST_IFACE) {
960 int old_dcd_state;
961 const u16 signals = get_unaligned_le16(sigp);
962 struct tty_struct *tty;
963
964 dev_dbg(&port->dev, "%s: signal 0x%x\n", __func__,
965 signals);
966
967 old_dcd_state = portdata->dcd_state;
968 /* Note: CTS from modem is in reverse logic! */
969 portdata->cts_state = ((signals & 0x100) ? 0 : 1);
970 portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
971 portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
972 portdata->ri_state = ((signals & 0x08) ? 1 : 0);
973
974 tty = tty_port_tty_get(&port->port);
975 if (tty && !C_CLOCAL(tty) &&
976 old_dcd_state && !portdata->dcd_state)
977 tty_hangup(tty);
978 tty_kref_put(tty);
979 } else {
980 /* dump the data we don't understand to log */
981 usb_serial_debug_data(1, &port->dev, __func__,
982 urb->actual_length, urb->transfer_buffer);
983 }
984 } else
985 dev_dbg(&port->dev, "%s: error %d\n", __func__, status);
986
987 /* Resubmit urb so we continue receiving IRQ data */
988 if (status != -ESHUTDOWN && status != -ENOENT && status != -ENODEV) {
989 usb_mark_last_busy(serial->dev);
990 urb->dev = serial->dev;
991 err = usb_submit_urb(urb, GFP_ATOMIC);
992 if (err && err != -ENODEV)
993 dev_err(&port->dev, "%s: resubmit intr urb "
994 "failed. (%d)\n", __func__, err);
995 }
996}
997
998static int sierra_write_room(struct tty_struct *tty)
999{
1000 struct usb_serial_port *port = tty->driver_data;
1001 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
1002 unsigned long flags;
1003 int retval;
1004
1005 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
1006
1007 /* try to give a good number back based on if we have any free urbs at
1008 * this point in time */
1009 retval = MAX_TRANSFER;
1010
1011 spin_lock_irqsave(&portdata->lock, flags);
1012 if (portdata->outstanding_urbs >= portdata->num_out_urbs) {
1013 retval = 0;
1014 }
1015 spin_unlock_irqrestore(&portdata->lock, flags);
1016
1017 return retval;
1018}
1019
1020static void sierra_stop_rx_urbs(struct usb_serial_port *port)
1021{
1022 int i;
1023 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
1024
1025 for (i = 0; i < portdata->num_in_urbs; i++)
1026 usb_kill_urb(portdata->in_urbs[i]);
1027
1028 usb_kill_urb(port->interrupt_in_urb);
1029}
1030
1031static int sierra_submit_rx_urbs(struct usb_serial_port *port, gfp_t mem_flags)
1032{
1033 int ok_cnt;
1034 int err = -EINVAL;
1035 int i;
1036 struct urb *urb;
1037 struct sierra_port_private *portdata = usb_get_serial_port_data(port);
1038
1039 ok_cnt = 0;
1040 for (i = 0; i < portdata->num_in_urbs; i++) {
1041 urb = portdata->in_urbs[i];
1042 if (!urb)
1043 continue;
1044 urb->transfer_flags |= URB_FREE_BUFFER;
1045 err = usb_submit_urb(urb, mem_flags);
1046 if (err) {
1047 dev_err(&port->dev, "%s: submit urb failed: %d\n",
1048 __func__, err);
1049 } else {
1050 ok_cnt++;
1051 }
1052 }
1053
1054 if (ok_cnt && port->interrupt_in_urb) {
1055 err = usb_submit_urb(port->interrupt_in_urb, mem_flags);
1056 if (err) {
1057 dev_err(&port->dev, "%s: submit intr urb failed: %d\n",
1058 __func__, err);
1059 }
1060 }
1061
1062 if (ok_cnt > 0) /* at least one rx urb submitted */
1063 return 0;
1064 else
1065 return err;
1066}
1067
1068static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint,
1069 int dir, void *ctx, int len,
1070 gfp_t mem_flags,
1071 usb_complete_t callback)
1072{
1073 struct urb *urb;
1074 u8 *buf;
1075
1076 if (endpoint == -1)
1077 return NULL;
1078
1079 urb = usb_alloc_urb(0, mem_flags);
1080 if (urb == NULL) {
1081 dev_dbg(&serial->dev->dev, "%s: alloc for endpoint %d failed\n",
1082 __func__, endpoint);
1083 return NULL;
1084 }
1085
1086 buf = kmalloc(len, mem_flags);
1087 if (buf) {
1088 /* Fill URB using supplied data */
1089 usb_fill_bulk_urb(urb, serial->dev,
1090 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1091 buf, len, callback, ctx);
1092
1093 /* debug */
1094 dev_dbg(&serial->dev->dev, "%s %c u : %p d:%p\n", __func__,
1095 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
1096 } else {
1097 dev_dbg(&serial->dev->dev, "%s %c u:%p d:%p\n", __func__,
1098 dir == USB_DIR_IN ? 'i' : 'o', urb, buf);
1099
1100 sierra_release_urb(urb);
1101 urb = NULL;
1102 }
1103
1104 return urb;
1105}
1106static void sierra_close(struct usb_serial_port *port)
1107{
1108 int i;
1109 struct urb *urb;
1110 struct usb_serial *serial = port->serial;
1111 struct sierra_port_private *portdata;
1112 struct sierra_intf_private *intfdata = port->serial->private;
1113 const char stopMessage[] = "$GPS_STOP";
1114 int nResult;
1115 int bytesWrote;
1116
1117 dev_dbg(&port->dev, "%s\n", __func__);
1118 portdata = usb_get_serial_port_data(port);
1119
1120 portdata->rts_state = 0;
1121 portdata->dtr_state = 0;
1122
1123 usb_autopm_get_interface(serial->interface);
1124
1125 if (serial->dev) {
1126 if ((is_gps_port_qmi_gobi(port) == 1))
1127 {
1128 usb_autopm_get_interface(serial->interface);
1129 /* Send stopMessage , 1s timeout */
1130 nResult = usb_bulk_msg( serial->dev,
1131 usb_sndbulkpipe( serial->dev,
1132 port->bulk_out_endpointAddress),
1133 (void *)&stopMessage[0],
1134 sizeof( stopMessage ),
1135 &bytesWrote,
1136 1000 );
1137 usb_autopm_put_interface(serial->interface);
1138 /* TBD analyze return value */
1139 }
1140
1141 mutex_lock(&serial->disc_mutex);
1142 if (!serial->disconnected)
1143 sierra_send_setup(port);
1144 mutex_unlock(&serial->disc_mutex);
1145 spin_lock_irq(&intfdata->susp_lock);
1146 portdata->opened = 0;
1147 spin_unlock_irq(&intfdata->susp_lock);
1148
1149 /* Stop reading urbs */
1150 sierra_stop_rx_urbs(port);
1151 /* .. and release them */
1152 for (i = 0; i < portdata->num_in_urbs; i++) {
1153 sierra_release_urb(portdata->in_urbs[i]);
1154 portdata->in_urbs[i] = NULL;
1155 }
1156 while((urb = usb_get_from_anchor(&portdata->delayed))) {
1157 sierra_release_urb(urb);
1158 usb_autopm_put_interface(serial->interface);
1159 }
1160 /* wait for active to finish */
1161 usb_wait_anchor_empty_timeout(&portdata->active, 500);
1162 usb_kill_anchored_urbs(&portdata->active);
1163
1164 }
1165}
1166
1167static int sierra_open(struct tty_struct *tty, struct usb_serial_port *port)
1168{
1169 struct sierra_port_private *portdata;
1170 struct usb_serial *serial = port->serial;
1171 struct sierra_intf_private *intfdata = serial->private;
1172 int i;
1173 int err;
1174 int endpoint;
1175 struct urb *urb;
1176 const char startMessage[] = "$GPS_START";
1177 int nResult;
1178 int bytesWrote;
1179
1180 portdata = usb_get_serial_port_data(port);
1181
1182 dev_dbg(&port->dev, "%s\n", __func__);
1183
1184 /* Set some sane defaults */
1185 portdata->rts_state = 1;
1186 portdata->dtr_state = 1;
1187
1188 if (is_gps_port_qmi_gobi(port))
1189 {
1190 usb_autopm_get_interface(serial->interface);
1191 /* Send startMessage, 1s timeout */
1192 nResult = usb_bulk_msg( serial->dev,
1193 usb_sndbulkpipe( serial->dev,
1194 port->bulk_out_endpointAddress),
1195 (void *)&startMessage[0],
1196 sizeof( startMessage ),
1197 &bytesWrote,
1198 1000 );
1199 usb_autopm_put_interface(serial->interface);
1200 /* TBD analyze return value */
1201 }
1202
1203 endpoint = port->bulk_in_endpointAddress;
1204 for (i = 0; i < portdata->num_in_urbs; i++) {
1205 urb = sierra_setup_urb(serial, endpoint, USB_DIR_IN, port,
1206 IN_BUFLEN, GFP_KERNEL,
1207 sierra_indat_callback);
1208 portdata->in_urbs[i] = urb;
1209 }
1210 /* clear halt condition */
1211 usb_clear_halt(serial->dev,
1212 usb_sndbulkpipe(serial->dev, endpoint) | USB_DIR_IN);
1213
1214 /* reset outstanding out urbs counter */
1215 spin_lock_irq(&portdata->lock);
1216 portdata->outstanding_urbs = 0;
1217 spin_unlock_irq(&portdata->lock);
1218
1219 err = sierra_submit_rx_urbs(port, GFP_KERNEL);
1220 if (err) {
1221 /* do everything as in close() but do not call close() because
1222 * usbserial calls sierra_open() with mutex taken;
1223 * then if we call sierra_close() inside sierra_open() we
1224 * violate 'no nested mutexes' kernel condition
1225 */
1226 portdata->rts_state = 0;
1227 portdata->dtr_state = 0;
1228 usb_autopm_get_interface(serial->interface);
1229 /* Stop reading urbs */
1230 sierra_stop_rx_urbs(port);
1231 /* .. and release them */
1232 for (i = 0; i < portdata->num_in_urbs; i++) {
1233 sierra_release_urb(portdata->in_urbs[i]);
1234 portdata->in_urbs[i] = NULL;
1235 }
1236 while((urb = usb_get_from_anchor(&portdata->delayed))) {
1237 sierra_release_urb(urb);
1238 usb_autopm_put_interface(serial->interface);
1239 }
1240 /* wait for active to finish */
1241 usb_wait_anchor_empty_timeout(&portdata->active, 500);
1242 usb_kill_anchored_urbs(&portdata->active);
1243 /* restore balance for autopm */
1244 usb_autopm_put_interface(serial->interface);
1245 return err;
1246 }
1247 sierra_send_setup(port);
1248
1249 spin_lock_irq(&intfdata->susp_lock);
1250 portdata->opened = 1;
1251 spin_unlock_irq(&intfdata->susp_lock);
1252 usb_autopm_put_interface(serial->interface);
1253
1254 return 0;
1255}
1256
1257static void sierra_dtr_rts(struct usb_serial_port *port, int on)
1258{
1259 struct usb_serial *serial = port->serial;
1260 struct sierra_port_private *portdata;
1261
1262 portdata = usb_get_serial_port_data(port);
1263 portdata->rts_state = on;
1264 portdata->dtr_state = on;
1265
1266 if (serial->dev) {
1267 mutex_lock(&serial->disc_mutex);
1268 if (!serial->disconnected)
1269 sierra_send_setup(port);
1270 mutex_unlock(&serial->disc_mutex);
1271 }
1272}
1273
1274static int sierra_startup(struct usb_serial *serial)
1275{
1276 struct usb_serial_port *port = NULL;
1277 struct sierra_port_private *portdata = NULL;
1278 struct sierra_iface_info *himemoryp = NULL;
1279 int i;
1280 u8 ifnum;
1281 u16 fw_attr;
1282 int result;
1283 int autosuspend;
1284
1285 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1286
1287 /* Exclude QMI and Gobi devices */
1288 if ( !is_qmi_gobi_device(serial->dev) )
1289 /* Set Device mode to D0 */
1290 sierra_set_power_state(serial->dev, 0x0000);
1291
1292 /* Check NMEA and set */
1293 if (nmea)
1294 sierra_vsc_set_nmea(serial->dev, 1);
1295
1296 if (serial->num_ports) {
1297 /* Note: One big piece of memory is allocated for all ports
1298 * private data in one shot. This memory is split into equal
1299 * pieces for each port.
1300 */
1301 portdata = (struct sierra_port_private *)kzalloc
1302 (sizeof(*portdata) * serial->num_ports, GFP_KERNEL);
1303 if (!portdata) {
1304 dev_dbg(&serial->dev->dev, "%s: No memory!\n", __func__);
1305 return -ENOMEM;
1306 }
1307 }
1308
1309 /* Now setup per port private data */
1310 for (i = 0; i < serial->num_ports; i++, portdata++) {
1311 port = serial->port[i];
1312 /* Initialize selected members of private data because these
1313 * may be referred to right away */
1314 spin_lock_init(&portdata->lock);
1315 init_usb_anchor(&portdata->active);
1316 init_usb_anchor(&portdata->delayed);
1317
1318 portdata->cts_state = 1;
1319
1320 ifnum = i;
1321 /* Assume low memory requirements */
1322 portdata->num_out_urbs = N_OUT_URB;
1323 portdata->num_in_urbs = N_IN_URB;
1324
1325 /* Determine actual memory requirements */
1326 if (serial->num_ports == 1) {
1327 /* Get interface number for composite device */
1328 ifnum = sierra_calc_interface(serial);
1329 himemoryp =
1330 (struct sierra_iface_info *)&typeB_interface_list;
1331 if (is_himemory(ifnum, himemoryp)) {
1332 portdata->num_out_urbs = N_OUT_URB_HM;
1333 portdata->num_in_urbs = N_IN_URB_HM;
1334 }
1335 }
1336 else {
1337 himemoryp =
1338 (struct sierra_iface_info *)&typeA_interface_list;
1339 if (is_himemory(i, himemoryp)) {
1340 portdata->num_out_urbs = N_OUT_URB_HM;
1341 portdata->num_in_urbs = N_IN_URB_HM;
1342 }
1343 }
1344 dev_dbg(&serial->dev->dev,
1345 "Memory usage (urbs) interface #%d, in=%d, out=%d\n",
1346 ifnum,portdata->num_in_urbs, portdata->num_out_urbs );
1347 /* Set the port private data pointer */
1348 usb_set_serial_port_data(port, portdata);
1349 }
1350 serial->interface->needs_remote_wakeup = 1;
1351
1352 if (is_qmi_gobi_device(serial->dev)) {
1353 /* QMI and Gobi devices always support auto-suspend*/
1354 autosuspend = 1;
1355 } else {
1356 result = sierra_get_fw_attr(serial->dev, &fw_attr);
1357 autosuspend = (result == sizeof(fw_attr) &&
1358 (fw_attr & SWI_FW_ATTR_PM_MASK) );
1359 }
1360
1361 if (autosuspend) {
1362 dev_info(&serial->dev->dev,
1363 "APM supported, enabling autosuspend.\n");
1364/*******************************************************************************
1365 * If you want the default /sys/bus/usb/devices/.../.../power/level to be forced
1366 * to auto, the following needs to be compiled in.
1367 */
1368#ifdef POWER_LEVEL_AUTO
1369 /* make power level default be 'auto' */
1370 usb_enable_autosuspend(serial->dev);
1371#endif
1372 } else {
1373 usb_disable_autosuspend(serial->dev);
1374 }
1375
1376 return 0;
1377}
1378
1379static void sierra_release(struct usb_serial *serial)
1380{
1381 int i;
1382 struct usb_serial_port *port;
1383 struct sierra_intf_private *intfdata = serial->private;
1384
1385 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1386
1387 if (serial->num_ports > 0) {
1388 port = serial->port[0];
1389 if (port)
1390 /* Note: The entire piece of memory that was allocated
1391 * in the startup routine can be released by passing
1392 * a pointer to the beginning of the piece.
1393 * This address corresponds to the address of the chunk
1394 * that was given to port 0.
1395 */
1396 kfree(usb_get_serial_port_data(port));
1397 }
1398
1399 for (i = 0; i < serial->num_ports; ++i) {
1400 port = serial->port[i];
1401 if (!port)
1402 continue;
1403 usb_set_serial_port_data(port, NULL);
1404 }
1405 kfree(intfdata);
1406}
1407
1408#ifdef CONFIG_PM
1409static void stop_read_write_urbs(struct usb_serial *serial)
1410{
1411 int i;
1412 struct usb_serial_port *port;
1413 struct sierra_port_private *portdata;
1414
1415 /* Stop reading/writing urbs */
1416 for (i = 0; i < serial->num_ports; ++i) {
1417 port = serial->port[i];
1418 portdata = usb_get_serial_port_data(port);
1419 sierra_stop_rx_urbs(port);
1420 usb_kill_anchored_urbs(&portdata->active);
1421 }
1422}
1423
1424static int sierra_suspend(struct usb_serial *serial, pm_message_t message)
1425{
1426 struct sierra_intf_private *intfdata;
1427
1428 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1429
1430 intfdata = serial->private;
1431 spin_lock_irq(&intfdata->susp_lock);
1432
1433 if (message.event & PM_EVENT_AUTO) {
1434 if (intfdata->in_flight) {
1435 spin_unlock_irq(&intfdata->susp_lock);
1436 return -EBUSY;
1437 }
1438 }
1439 intfdata->suspended = 1;
1440 spin_unlock_irq(&intfdata->susp_lock);
1441
1442 stop_read_write_urbs(serial);
1443
1444 return 0;
1445}
1446
1447static int sierra_resume(struct usb_serial *serial)
1448{
1449 struct usb_serial_port *port;
1450 struct sierra_intf_private *intfdata = serial->private;
1451 struct sierra_port_private *portdata;
1452 struct urb *urb;
1453 int ec = 0;
1454 int i, err;
1455 int len;
1456 int failed_submits;
1457
1458 dev_dbg(&serial->dev->dev, "%s\n", __func__);
1459
1460 spin_lock_irq(&intfdata->susp_lock);
1461 for (i = 0; i < serial->num_ports; i++) {
1462 port = serial->port[i];
1463 portdata = usb_get_serial_port_data(port);
1464 failed_submits = 0;
1465 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
1466 usb_anchor_urb(urb, &portdata->active);
1467 intfdata->in_flight++;
1468 len = urb->transfer_buffer_length;
1469 err = usb_submit_urb(urb, GFP_ATOMIC);
1470 if (err < 0) {
1471 intfdata->in_flight--;
1472 usb_unanchor_urb(urb);
1473 failed_submits++;
1474 atomic_inc(&intfdata->stats.delayed_write_err);
1475 /* fix pm_usage_cnt */
1476 usb_autopm_put_interface_async(
1477 port->serial->interface);
1478 } else {
1479 atomic_inc(&intfdata->stats.delayed_writes);
1480 atomic_add(len, &intfdata->stats.tx_bytes);
1481 }
1482 /* release urb - usb_get_from_anchor increased kref */
1483 usb_free_urb(urb);
1484 }
1485
1486 if (portdata->opened) {
1487 err = sierra_submit_rx_urbs(port, GFP_ATOMIC);
1488 if (err)
1489 ec++;
1490 }
1491 if (failed_submits) {
1492 /* fix outstanding_urbs counter */
1493 spin_lock(&portdata->lock); /* assuming irq disabled */
1494 portdata->outstanding_urbs -= failed_submits;
1495 spin_unlock(&portdata->lock);
1496 /* unblock a writer */
1497 usb_serial_port_softint(port);
1498 }
1499 }
1500 intfdata->suspended = 0;
1501 spin_unlock_irq(&intfdata->susp_lock);
1502
1503 return ec ? -EIO : 0;
1504}
1505#else
1506#define sierra_suspend NULL
1507#define sierra_resume NULL
1508#endif
1509
1510static int sierra_reset_resume(struct usb_interface *intf)
1511{
1512 struct usb_serial *serial = usb_get_intfdata(intf);
1513 dev_err(&serial->dev->dev, "%s\n", __func__);
1514 return usb_serial_resume(intf);
1515}
1516
1517static struct usb_driver sierra_driver = {
1518 .name = "sierra",
1519 .probe = usb_serial_probe,
1520 .disconnect = usb_serial_disconnect,
1521 .suspend = usb_serial_suspend,
1522 .resume = usb_serial_resume,
1523 .reset_resume = sierra_reset_resume,
1524 .id_table = id_table,
1525
1526 .no_dynamic_id = 1,
1527 .supports_autosuspend = 1,
1528};
1529
1530
1531static struct usb_serial_driver sierra_device = {
1532 .driver = {
1533 .owner = THIS_MODULE,
1534 .name = "sierra",
1535 },
1536 .description = "Sierra USB modem",
1537 .id_table = id_table,
1538 .usb_driver = &sierra_driver,
1539 .calc_num_ports = sierra_calc_num_ports,
1540 .probe = sierra_probe,
1541 .open = sierra_open,
1542 .close = sierra_close,
1543 .dtr_rts = sierra_dtr_rts,
1544 .write = sierra_write,
1545 .write_room = sierra_write_room,
1546 .set_termios = sierra_set_termios,
1547 .tiocmget = sierra_tiocmget,
1548 .tiocmset = sierra_tiocmset,
1549 .attach = sierra_startup,
1550 .release = sierra_release,
1551 .port_probe = sierra_create_sysfs_attrs,
1552 .port_remove = sierra_remove_sysfs_attrs,
1553 .suspend = sierra_suspend,
1554 .resume = sierra_resume,
1555 .read_int_callback = sierra_instat_callback,
1556
1557};
1558
1559static struct usb_serial_driver * const serial_drivers[] = {
1560 &sierra_device, NULL
1561};
1562
1563module_usb_serial_driver(sierra_driver, serial_drivers);
1564
1565MODULE_AUTHOR(DRIVER_AUTHOR);
1566MODULE_DESCRIPTION(DRIVER_DESC);
1567MODULE_VERSION(DRIVER_VERSION);
1568MODULE_LICENSE("GPL");
1569
1570module_param(nmea, bool, S_IRUGO | S_IWUSR);
1571MODULE_PARM_DESC(nmea, "NMEA streaming");
1572
1573module_param(debug, bool, S_IRUGO | S_IWUSR);
1574MODULE_PARM_DESC(debug, "Debug messages");