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