blob: 820436ec60e99cf0eae9e9ea7ddeaa7fe3120436 [file] [log] [blame]
Matthew Garrett0d456192010-04-01 12:31:07 -04001/*
2 USB Driver layer for GSM modems
3
4 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
5
6 This driver is free software; you can redistribute it and/or modify
7 it under the terms of Version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
9
10 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
11
12 History: see the git log.
13
14 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
15
16 This driver exists because the "normal" serial driver doesn't work too well
17 with GSM modems. Issues:
18 - data loss -- one single Receive URB is not nearly enough
19 - controlling the baud rate doesn't make sense
20*/
21
22#define DRIVER_VERSION "v0.7.2"
23#define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
24#define DRIVER_DESC "USB Driver for GSM modems"
25
26#include <linux/kernel.h>
27#include <linux/jiffies.h>
28#include <linux/errno.h>
29#include <linux/slab.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/module.h>
33#include <linux/bitops.h>
Peter Huewe66921ed2010-12-09 23:27:35 +010034#include <linux/uaccess.h>
Matthew Garrett0d456192010-04-01 12:31:07 -040035#include <linux/usb.h>
36#include <linux/usb/serial.h>
Dan Williams02303f72010-11-19 16:04:00 -060037#include <linux/serial.h>
Matthew Garrett0d456192010-04-01 12:31:07 -040038#include "usb-wwan.h"
39
Rusty Russell90ab5ee2012-01-13 09:32:20 +103040static bool debug;
Matthew Garrett0d456192010-04-01 12:31:07 -040041
42void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
43{
Matthew Garrett0d456192010-04-01 12:31:07 -040044 struct usb_wwan_port_private *portdata;
45
46 struct usb_wwan_intf_private *intfdata;
47
48 dbg("%s", __func__);
49
50 intfdata = port->serial->private;
51
52 if (!intfdata->send_setup)
53 return;
54
55 portdata = usb_get_serial_port_data(port);
Johan Hovold906c9c42013-02-13 17:53:28 +010056 /* FIXME: locking */
Matthew Garrett0d456192010-04-01 12:31:07 -040057 portdata->rts_state = on;
58 portdata->dtr_state = on;
Johan Hovold906c9c42013-02-13 17:53:28 +010059
60 intfdata->send_setup(port);
Matthew Garrett0d456192010-04-01 12:31:07 -040061}
62EXPORT_SYMBOL(usb_wwan_dtr_rts);
63
64void usb_wwan_set_termios(struct tty_struct *tty,
65 struct usb_serial_port *port,
66 struct ktermios *old_termios)
67{
68 struct usb_wwan_intf_private *intfdata = port->serial->private;
69
70 dbg("%s", __func__);
71
72 /* Doesn't support option setting */
73 tty_termios_copy_hw(tty->termios, old_termios);
74
75 if (intfdata->send_setup)
76 intfdata->send_setup(port);
77}
78EXPORT_SYMBOL(usb_wwan_set_termios);
79
Alan Cox60b33c12011-02-14 16:26:14 +000080int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040081{
82 struct usb_serial_port *port = tty->driver_data;
83 unsigned int value;
84 struct usb_wwan_port_private *portdata;
85
86 portdata = usb_get_serial_port_data(port);
87
88 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
89 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
90 ((portdata->cts_state) ? TIOCM_CTS : 0) |
91 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
92 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
93 ((portdata->ri_state) ? TIOCM_RNG : 0);
94
95 return value;
96}
97EXPORT_SYMBOL(usb_wwan_tiocmget);
98
Alan Cox20b9d172011-02-14 16:26:50 +000099int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -0400100 unsigned int set, unsigned int clear)
101{
102 struct usb_serial_port *port = tty->driver_data;
103 struct usb_wwan_port_private *portdata;
104 struct usb_wwan_intf_private *intfdata;
105
106 portdata = usb_get_serial_port_data(port);
107 intfdata = port->serial->private;
108
109 if (!intfdata->send_setup)
110 return -EINVAL;
111
112 /* FIXME: what locks portdata fields ? */
113 if (set & TIOCM_RTS)
114 portdata->rts_state = 1;
115 if (set & TIOCM_DTR)
116 portdata->dtr_state = 1;
117
118 if (clear & TIOCM_RTS)
119 portdata->rts_state = 0;
120 if (clear & TIOCM_DTR)
121 portdata->dtr_state = 0;
122 return intfdata->send_setup(port);
123}
124EXPORT_SYMBOL(usb_wwan_tiocmset);
125
Dan Williams02303f72010-11-19 16:04:00 -0600126static int get_serial_info(struct usb_serial_port *port,
127 struct serial_struct __user *retinfo)
128{
129 struct serial_struct tmp;
130
131 if (!retinfo)
132 return -EFAULT;
133
134 memset(&tmp, 0, sizeof(tmp));
135 tmp.line = port->serial->minor;
136 tmp.port = port->number;
137 tmp.baud_base = tty_get_baud_rate(port->port.tty);
138 tmp.close_delay = port->port.close_delay / 10;
139 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
140 ASYNC_CLOSING_WAIT_NONE :
141 port->port.closing_wait / 10;
142
143 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
144 return -EFAULT;
145 return 0;
146}
147
148static int set_serial_info(struct usb_serial_port *port,
149 struct serial_struct __user *newinfo)
150{
151 struct serial_struct new_serial;
152 unsigned int closing_wait, close_delay;
153 int retval = 0;
154
155 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
156 return -EFAULT;
157
158 close_delay = new_serial.close_delay * 10;
159 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
160 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
161
162 mutex_lock(&port->port.mutex);
163
164 if (!capable(CAP_SYS_ADMIN)) {
165 if ((close_delay != port->port.close_delay) ||
166 (closing_wait != port->port.closing_wait))
167 retval = -EPERM;
168 else
169 retval = -EOPNOTSUPP;
170 } else {
171 port->port.close_delay = close_delay;
172 port->port.closing_wait = closing_wait;
173 }
174
175 mutex_unlock(&port->port.mutex);
176 return retval;
177}
178
Alan Cox00a0d0d2011-02-14 16:27:06 +0000179int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600180 unsigned int cmd, unsigned long arg)
181{
182 struct usb_serial_port *port = tty->driver_data;
183
184 dbg("%s cmd 0x%04x", __func__, cmd);
185
186 switch (cmd) {
187 case TIOCGSERIAL:
188 return get_serial_info(port,
189 (struct serial_struct __user *) arg);
190 case TIOCSSERIAL:
191 return set_serial_info(port,
192 (struct serial_struct __user *) arg);
193 default:
194 break;
195 }
196
197 dbg("%s arg not supported", __func__);
198
199 return -ENOIOCTLCMD;
200}
201EXPORT_SYMBOL(usb_wwan_ioctl);
202
Matthew Garrett0d456192010-04-01 12:31:07 -0400203/* Write */
204int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count)
206{
207 struct usb_wwan_port_private *portdata;
208 struct usb_wwan_intf_private *intfdata;
209 int i;
210 int left, todo;
211 struct urb *this_urb = NULL; /* spurious */
212 int err;
213 unsigned long flags;
214
215 portdata = usb_get_serial_port_data(port);
216 intfdata = port->serial->private;
217
218 dbg("%s: write (%d chars)", __func__, count);
219
220 i = 0;
221 left = count;
222 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
223 todo = left;
224 if (todo > OUT_BUFLEN)
225 todo = OUT_BUFLEN;
226
227 this_urb = portdata->out_urbs[i];
228 if (test_and_set_bit(i, &portdata->out_busy)) {
229 if (time_before(jiffies,
230 portdata->tx_start_time[i] + 10 * HZ))
231 continue;
232 usb_unlink_urb(this_urb);
233 continue;
234 }
235 dbg("%s: endpoint %d buf %d", __func__,
236 usb_pipeendpoint(this_urb->pipe), i);
237
238 err = usb_autopm_get_interface_async(port->serial->interface);
239 if (err < 0)
240 break;
241
242 /* send the data */
243 memcpy(this_urb->transfer_buffer, buf, todo);
244 this_urb->transfer_buffer_length = todo;
245
246 spin_lock_irqsave(&intfdata->susp_lock, flags);
247 if (intfdata->suspended) {
248 usb_anchor_urb(this_urb, &portdata->delayed);
249 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
250 } else {
251 intfdata->in_flight++;
252 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
253 err = usb_submit_urb(this_urb, GFP_ATOMIC);
254 if (err) {
255 dbg("usb_submit_urb %p (write bulk) failed "
256 "(%d)", this_urb, err);
257 clear_bit(i, &portdata->out_busy);
258 spin_lock_irqsave(&intfdata->susp_lock, flags);
259 intfdata->in_flight--;
260 spin_unlock_irqrestore(&intfdata->susp_lock,
261 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100262 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100263 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400264 }
265 }
266
267 portdata->tx_start_time[i] = jiffies;
268 buf += todo;
269 left -= todo;
270 }
271
272 count -= left;
273 dbg("%s: wrote (did %d)", __func__, count);
274 return count;
275}
276EXPORT_SYMBOL(usb_wwan_write);
277
278static void usb_wwan_indat_callback(struct urb *urb)
279{
280 int err;
281 int endpoint;
282 struct usb_serial_port *port;
283 struct tty_struct *tty;
284 unsigned char *data = urb->transfer_buffer;
285 int status = urb->status;
286
287 dbg("%s: %p", __func__, urb);
288
289 endpoint = usb_pipeendpoint(urb->pipe);
290 port = urb->context;
291
292 if (status) {
293 dbg("%s: nonzero status: %d on endpoint %02x.",
294 __func__, status, endpoint);
295 } else {
296 tty = tty_port_tty_get(&port->port);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100297 if (tty) {
298 if (urb->actual_length) {
299 tty_insert_flip_string(tty, data,
300 urb->actual_length);
301 tty_flip_buffer_push(tty);
302 } else
303 dbg("%s: empty read urb received", __func__);
304 tty_kref_put(tty);
305 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400306
307 /* Resubmit urb so we continue receiving */
308 if (status != -ESHUTDOWN) {
309 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100310 if (err) {
311 if (err != -EPERM) {
312 printk(KERN_ERR "%s: resubmit read urb failed. "
313 "(%d)", __func__, err);
314 /* busy also in error unless we are killed */
315 usb_mark_last_busy(port->serial->dev);
316 }
317 } else {
Matthew Garrett0d456192010-04-01 12:31:07 -0400318 usb_mark_last_busy(port->serial->dev);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100319 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400320 }
321
322 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400323}
324
325static void usb_wwan_outdat_callback(struct urb *urb)
326{
327 struct usb_serial_port *port;
328 struct usb_wwan_port_private *portdata;
329 struct usb_wwan_intf_private *intfdata;
330 int i;
331
332 dbg("%s", __func__);
333
334 port = urb->context;
335 intfdata = port->serial->private;
336
337 usb_serial_port_softint(port);
338 usb_autopm_put_interface_async(port->serial->interface);
339 portdata = usb_get_serial_port_data(port);
340 spin_lock(&intfdata->susp_lock);
341 intfdata->in_flight--;
342 spin_unlock(&intfdata->susp_lock);
343
344 for (i = 0; i < N_OUT_URB; ++i) {
345 if (portdata->out_urbs[i] == urb) {
346 smp_mb__before_clear_bit();
347 clear_bit(i, &portdata->out_busy);
348 break;
349 }
350 }
351}
352
353int usb_wwan_write_room(struct tty_struct *tty)
354{
355 struct usb_serial_port *port = tty->driver_data;
356 struct usb_wwan_port_private *portdata;
357 int i;
358 int data_len = 0;
359 struct urb *this_urb;
360
361 portdata = usb_get_serial_port_data(port);
362
363 for (i = 0; i < N_OUT_URB; i++) {
364 this_urb = portdata->out_urbs[i];
365 if (this_urb && !test_bit(i, &portdata->out_busy))
366 data_len += OUT_BUFLEN;
367 }
368
369 dbg("%s: %d", __func__, data_len);
370 return data_len;
371}
372EXPORT_SYMBOL(usb_wwan_write_room);
373
374int usb_wwan_chars_in_buffer(struct tty_struct *tty)
375{
376 struct usb_serial_port *port = tty->driver_data;
377 struct usb_wwan_port_private *portdata;
378 int i;
379 int data_len = 0;
380 struct urb *this_urb;
381
382 portdata = usb_get_serial_port_data(port);
383
384 for (i = 0; i < N_OUT_URB; i++) {
385 this_urb = portdata->out_urbs[i];
386 /* FIXME: This locking is insufficient as this_urb may
387 go unused during the test */
388 if (this_urb && test_bit(i, &portdata->out_busy))
389 data_len += this_urb->transfer_buffer_length;
390 }
391 dbg("%s: %d", __func__, data_len);
392 return data_len;
393}
394EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
395
396int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
397{
398 struct usb_wwan_port_private *portdata;
399 struct usb_wwan_intf_private *intfdata;
400 struct usb_serial *serial = port->serial;
401 int i, err;
402 struct urb *urb;
403
404 portdata = usb_get_serial_port_data(port);
405 intfdata = serial->private;
406
407 dbg("%s", __func__);
408
409 /* Start reading from the IN endpoint */
410 for (i = 0; i < N_IN_URB; i++) {
411 urb = portdata->in_urbs[i];
412 if (!urb)
413 continue;
414 err = usb_submit_urb(urb, GFP_KERNEL);
415 if (err) {
416 dbg("%s: submit urb %d failed (%d) %d",
417 __func__, i, err, urb->transfer_buffer_length);
418 }
419 }
420
421 if (intfdata->send_setup)
422 intfdata->send_setup(port);
423
424 serial->interface->needs_remote_wakeup = 1;
425 spin_lock_irq(&intfdata->susp_lock);
426 portdata->opened = 1;
427 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100428 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400429 usb_autopm_put_interface(serial->interface);
430
431 return 0;
432}
433EXPORT_SYMBOL(usb_wwan_open);
434
435void usb_wwan_close(struct usb_serial_port *port)
436{
437 int i;
438 struct usb_serial *serial = port->serial;
439 struct usb_wwan_port_private *portdata;
440 struct usb_wwan_intf_private *intfdata = port->serial->private;
441
442 dbg("%s", __func__);
443 portdata = usb_get_serial_port_data(port);
444
445 if (serial->dev) {
446 /* Stop reading/writing urbs */
447 spin_lock_irq(&intfdata->susp_lock);
448 portdata->opened = 0;
449 spin_unlock_irq(&intfdata->susp_lock);
450
451 for (i = 0; i < N_IN_URB; i++)
452 usb_kill_urb(portdata->in_urbs[i]);
453 for (i = 0; i < N_OUT_URB; i++)
454 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100455 /* balancing - important as an error cannot be handled*/
456 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400457 serial->interface->needs_remote_wakeup = 0;
458 }
459}
460EXPORT_SYMBOL(usb_wwan_close);
461
462/* Helper functions used by usb_wwan_setup_urbs */
463static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
464 int dir, void *ctx, char *buf, int len,
465 void (*callback) (struct urb *))
466{
467 struct urb *urb;
468
469 if (endpoint == -1)
470 return NULL; /* endpoint not needed */
471
472 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
473 if (urb == NULL) {
474 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
475 return NULL;
476 }
477
478 /* Fill URB using supplied data. */
479 usb_fill_bulk_urb(urb, serial->dev,
480 usb_sndbulkpipe(serial->dev, endpoint) | dir,
481 buf, len, callback, ctx);
482
483 return urb;
484}
485
486/* Setup urbs */
487static void usb_wwan_setup_urbs(struct usb_serial *serial)
488{
489 int i, j;
490 struct usb_serial_port *port;
491 struct usb_wwan_port_private *portdata;
492
493 dbg("%s", __func__);
494
495 for (i = 0; i < serial->num_ports; i++) {
496 port = serial->port[i];
497 portdata = usb_get_serial_port_data(port);
498
499 /* Do indat endpoints first */
500 for (j = 0; j < N_IN_URB; ++j) {
501 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
502 port->
503 bulk_in_endpointAddress,
504 USB_DIR_IN,
505 port,
506 portdata->
507 in_buffer[j],
508 IN_BUFLEN,
509 usb_wwan_indat_callback);
510 }
511
512 /* outdat endpoints */
513 for (j = 0; j < N_OUT_URB; ++j) {
514 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
515 port->
516 bulk_out_endpointAddress,
517 USB_DIR_OUT,
518 port,
519 portdata->
520 out_buffer
521 [j],
522 OUT_BUFLEN,
523 usb_wwan_outdat_callback);
524 }
525 }
526}
527
528int usb_wwan_startup(struct usb_serial *serial)
529{
530 int i, j, err;
531 struct usb_serial_port *port;
532 struct usb_wwan_port_private *portdata;
533 u8 *buffer;
534
535 dbg("%s", __func__);
536
537 /* Now setup per port private data */
538 for (i = 0; i < serial->num_ports; i++) {
539 port = serial->port[i];
540 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
541 if (!portdata) {
542 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
543 __func__, i);
544 return 1;
545 }
546 init_usb_anchor(&portdata->delayed);
547
548 for (j = 0; j < N_IN_URB; j++) {
549 buffer = (u8 *) __get_free_page(GFP_KERNEL);
550 if (!buffer)
551 goto bail_out_error;
552 portdata->in_buffer[j] = buffer;
553 }
554
555 for (j = 0; j < N_OUT_URB; j++) {
556 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
557 if (!buffer)
558 goto bail_out_error2;
559 portdata->out_buffer[j] = buffer;
560 }
561
562 usb_set_serial_port_data(port, portdata);
563
564 if (!port->interrupt_in_urb)
565 continue;
566 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
567 if (err)
568 dbg("%s: submit irq_in urb failed %d", __func__, err);
569 }
570 usb_wwan_setup_urbs(serial);
571 return 0;
572
573bail_out_error2:
574 for (j = 0; j < N_OUT_URB; j++)
575 kfree(portdata->out_buffer[j]);
576bail_out_error:
577 for (j = 0; j < N_IN_URB; j++)
578 if (portdata->in_buffer[j])
579 free_page((unsigned long)portdata->in_buffer[j]);
580 kfree(portdata);
581 return 1;
582}
583EXPORT_SYMBOL(usb_wwan_startup);
584
585static void stop_read_write_urbs(struct usb_serial *serial)
586{
587 int i, j;
588 struct usb_serial_port *port;
589 struct usb_wwan_port_private *portdata;
590
591 /* Stop reading/writing urbs */
592 for (i = 0; i < serial->num_ports; ++i) {
593 port = serial->port[i];
594 portdata = usb_get_serial_port_data(port);
595 for (j = 0; j < N_IN_URB; j++)
596 usb_kill_urb(portdata->in_urbs[j]);
597 for (j = 0; j < N_OUT_URB; j++)
598 usb_kill_urb(portdata->out_urbs[j]);
599 }
600}
601
602void usb_wwan_disconnect(struct usb_serial *serial)
603{
604 dbg("%s", __func__);
605
606 stop_read_write_urbs(serial);
607}
608EXPORT_SYMBOL(usb_wwan_disconnect);
609
610void usb_wwan_release(struct usb_serial *serial)
611{
612 int i, j;
613 struct usb_serial_port *port;
614 struct usb_wwan_port_private *portdata;
615
616 dbg("%s", __func__);
617
618 /* Now free them */
619 for (i = 0; i < serial->num_ports; ++i) {
620 port = serial->port[i];
621 portdata = usb_get_serial_port_data(port);
622
623 for (j = 0; j < N_IN_URB; j++) {
624 usb_free_urb(portdata->in_urbs[j]);
625 free_page((unsigned long)
626 portdata->in_buffer[j]);
627 portdata->in_urbs[j] = NULL;
628 }
629 for (j = 0; j < N_OUT_URB; j++) {
630 usb_free_urb(portdata->out_urbs[j]);
631 kfree(portdata->out_buffer[j]);
632 portdata->out_urbs[j] = NULL;
633 }
634 }
635
636 /* Now free per port private data */
637 for (i = 0; i < serial->num_ports; i++) {
638 port = serial->port[i];
639 kfree(usb_get_serial_port_data(port));
640 }
641}
642EXPORT_SYMBOL(usb_wwan_release);
643
644#ifdef CONFIG_PM
645int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
646{
647 struct usb_wwan_intf_private *intfdata = serial->private;
648 int b;
649
650 dbg("%s entered", __func__);
651
Alan Stern5b1b0b82011-08-19 23:49:48 +0200652 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400653 spin_lock_irq(&intfdata->susp_lock);
654 b = intfdata->in_flight;
655 spin_unlock_irq(&intfdata->susp_lock);
656
657 if (b)
658 return -EBUSY;
659 }
660
661 spin_lock_irq(&intfdata->susp_lock);
662 intfdata->suspended = 1;
663 spin_unlock_irq(&intfdata->susp_lock);
664 stop_read_write_urbs(serial);
665
666 return 0;
667}
668EXPORT_SYMBOL(usb_wwan_suspend);
669
Oliver Neukum16871dc2011-02-10 15:33:29 +0100670static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
671{
672 int i;
673
674 for (i = 0; i < N_OUT_URB; i++) {
675 if (urb == portdata->out_urbs[i]) {
676 clear_bit(i, &portdata->out_busy);
677 break;
678 }
679 }
680}
681
Matthew Garrett0d456192010-04-01 12:31:07 -0400682static void play_delayed(struct usb_serial_port *port)
683{
684 struct usb_wwan_intf_private *data;
685 struct usb_wwan_port_private *portdata;
686 struct urb *urb;
687 int err;
688
689 portdata = usb_get_serial_port_data(port);
690 data = port->serial->private;
691 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
692 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100693 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400694 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100695 } else {
696 /* we have to throw away the rest */
697 do {
698 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100699 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100700 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
701 break;
702 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400703 }
704}
705
706int usb_wwan_resume(struct usb_serial *serial)
707{
708 int i, j;
709 struct usb_serial_port *port;
710 struct usb_wwan_intf_private *intfdata = serial->private;
711 struct usb_wwan_port_private *portdata;
712 struct urb *urb;
713 int err = 0;
714
715 dbg("%s entered", __func__);
716 /* get the interrupt URBs resubmitted unconditionally */
717 for (i = 0; i < serial->num_ports; i++) {
718 port = serial->port[i];
719 if (!port->interrupt_in_urb) {
720 dbg("%s: No interrupt URB for port %d", __func__, i);
721 continue;
722 }
723 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
724 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
725 if (err < 0) {
726 err("%s: Error %d for interrupt URB of port%d",
727 __func__, err, i);
728 goto err_out;
729 }
730 }
731
732 for (i = 0; i < serial->num_ports; i++) {
733 /* walk all ports */
734 port = serial->port[i];
735 portdata = usb_get_serial_port_data(port);
736
737 /* skip closed ports */
738 spin_lock_irq(&intfdata->susp_lock);
739 if (!portdata->opened) {
740 spin_unlock_irq(&intfdata->susp_lock);
741 continue;
742 }
743
744 for (j = 0; j < N_IN_URB; j++) {
745 urb = portdata->in_urbs[j];
746 err = usb_submit_urb(urb, GFP_ATOMIC);
747 if (err < 0) {
748 err("%s: Error %d for bulk URB %d",
749 __func__, err, i);
750 spin_unlock_irq(&intfdata->susp_lock);
751 goto err_out;
752 }
753 }
754 play_delayed(port);
755 spin_unlock_irq(&intfdata->susp_lock);
756 }
757 spin_lock_irq(&intfdata->susp_lock);
758 intfdata->suspended = 0;
759 spin_unlock_irq(&intfdata->susp_lock);
760err_out:
761 return err;
762}
763EXPORT_SYMBOL(usb_wwan_resume);
764#endif
765
766MODULE_AUTHOR(DRIVER_AUTHOR);
767MODULE_DESCRIPTION(DRIVER_DESC);
768MODULE_VERSION(DRIVER_VERSION);
769MODULE_LICENSE("GPL");
770
771module_param(debug, bool, S_IRUGO | S_IWUSR);
772MODULE_PARM_DESC(debug, "Debug messages");