blob: 0c58554432bd4fe269cea1705648b9bf9e6b0d06 [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{
44 struct usb_serial *serial = port->serial;
45 struct usb_wwan_port_private *portdata;
46
47 struct usb_wwan_intf_private *intfdata;
48
49 dbg("%s", __func__);
50
51 intfdata = port->serial->private;
52
53 if (!intfdata->send_setup)
54 return;
55
56 portdata = usb_get_serial_port_data(port);
57 mutex_lock(&serial->disc_mutex);
58 portdata->rts_state = on;
59 portdata->dtr_state = on;
60 if (serial->dev)
61 intfdata->send_setup(port);
62 mutex_unlock(&serial->disc_mutex);
63}
64EXPORT_SYMBOL(usb_wwan_dtr_rts);
65
66void usb_wwan_set_termios(struct tty_struct *tty,
67 struct usb_serial_port *port,
68 struct ktermios *old_termios)
69{
70 struct usb_wwan_intf_private *intfdata = port->serial->private;
71
72 dbg("%s", __func__);
73
74 /* Doesn't support option setting */
75 tty_termios_copy_hw(tty->termios, old_termios);
76
77 if (intfdata->send_setup)
78 intfdata->send_setup(port);
79}
80EXPORT_SYMBOL(usb_wwan_set_termios);
81
Alan Cox60b33c12011-02-14 16:26:14 +000082int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040083{
84 struct usb_serial_port *port = tty->driver_data;
85 unsigned int value;
86 struct usb_wwan_port_private *portdata;
87
88 portdata = usb_get_serial_port_data(port);
89
90 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
91 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
92 ((portdata->cts_state) ? TIOCM_CTS : 0) |
93 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
94 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
95 ((portdata->ri_state) ? TIOCM_RNG : 0);
96
97 return value;
98}
99EXPORT_SYMBOL(usb_wwan_tiocmget);
100
Alan Cox20b9d172011-02-14 16:26:50 +0000101int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -0400102 unsigned int set, unsigned int clear)
103{
104 struct usb_serial_port *port = tty->driver_data;
105 struct usb_wwan_port_private *portdata;
106 struct usb_wwan_intf_private *intfdata;
107
108 portdata = usb_get_serial_port_data(port);
109 intfdata = port->serial->private;
110
111 if (!intfdata->send_setup)
112 return -EINVAL;
113
114 /* FIXME: what locks portdata fields ? */
115 if (set & TIOCM_RTS)
116 portdata->rts_state = 1;
117 if (set & TIOCM_DTR)
118 portdata->dtr_state = 1;
119
120 if (clear & TIOCM_RTS)
121 portdata->rts_state = 0;
122 if (clear & TIOCM_DTR)
123 portdata->dtr_state = 0;
124 return intfdata->send_setup(port);
125}
126EXPORT_SYMBOL(usb_wwan_tiocmset);
127
Dan Williams02303f72010-11-19 16:04:00 -0600128static int get_serial_info(struct usb_serial_port *port,
129 struct serial_struct __user *retinfo)
130{
131 struct serial_struct tmp;
132
133 if (!retinfo)
134 return -EFAULT;
135
136 memset(&tmp, 0, sizeof(tmp));
137 tmp.line = port->serial->minor;
138 tmp.port = port->number;
139 tmp.baud_base = tty_get_baud_rate(port->port.tty);
140 tmp.close_delay = port->port.close_delay / 10;
141 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
142 ASYNC_CLOSING_WAIT_NONE :
143 port->port.closing_wait / 10;
144
145 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
146 return -EFAULT;
147 return 0;
148}
149
150static int set_serial_info(struct usb_serial_port *port,
151 struct serial_struct __user *newinfo)
152{
153 struct serial_struct new_serial;
154 unsigned int closing_wait, close_delay;
155 int retval = 0;
156
157 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
158 return -EFAULT;
159
160 close_delay = new_serial.close_delay * 10;
161 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
162 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
163
164 mutex_lock(&port->port.mutex);
165
166 if (!capable(CAP_SYS_ADMIN)) {
167 if ((close_delay != port->port.close_delay) ||
168 (closing_wait != port->port.closing_wait))
169 retval = -EPERM;
170 else
171 retval = -EOPNOTSUPP;
172 } else {
173 port->port.close_delay = close_delay;
174 port->port.closing_wait = closing_wait;
175 }
176
177 mutex_unlock(&port->port.mutex);
178 return retval;
179}
180
Alan Cox00a0d0d2011-02-14 16:27:06 +0000181int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600182 unsigned int cmd, unsigned long arg)
183{
184 struct usb_serial_port *port = tty->driver_data;
185
186 dbg("%s cmd 0x%04x", __func__, cmd);
187
188 switch (cmd) {
189 case TIOCGSERIAL:
190 return get_serial_info(port,
191 (struct serial_struct __user *) arg);
192 case TIOCSSERIAL:
193 return set_serial_info(port,
194 (struct serial_struct __user *) arg);
195 default:
196 break;
197 }
198
199 dbg("%s arg not supported", __func__);
200
201 return -ENOIOCTLCMD;
202}
203EXPORT_SYMBOL(usb_wwan_ioctl);
204
Matthew Garrett0d456192010-04-01 12:31:07 -0400205/* Write */
206int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
207 const unsigned char *buf, int count)
208{
209 struct usb_wwan_port_private *portdata;
210 struct usb_wwan_intf_private *intfdata;
211 int i;
212 int left, todo;
213 struct urb *this_urb = NULL; /* spurious */
214 int err;
215 unsigned long flags;
216
217 portdata = usb_get_serial_port_data(port);
218 intfdata = port->serial->private;
219
220 dbg("%s: write (%d chars)", __func__, count);
221
222 i = 0;
223 left = count;
224 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
225 todo = left;
226 if (todo > OUT_BUFLEN)
227 todo = OUT_BUFLEN;
228
229 this_urb = portdata->out_urbs[i];
230 if (test_and_set_bit(i, &portdata->out_busy)) {
231 if (time_before(jiffies,
232 portdata->tx_start_time[i] + 10 * HZ))
233 continue;
234 usb_unlink_urb(this_urb);
235 continue;
236 }
237 dbg("%s: endpoint %d buf %d", __func__,
238 usb_pipeendpoint(this_urb->pipe), i);
239
240 err = usb_autopm_get_interface_async(port->serial->interface);
241 if (err < 0)
242 break;
243
244 /* send the data */
245 memcpy(this_urb->transfer_buffer, buf, todo);
246 this_urb->transfer_buffer_length = todo;
247
248 spin_lock_irqsave(&intfdata->susp_lock, flags);
249 if (intfdata->suspended) {
250 usb_anchor_urb(this_urb, &portdata->delayed);
251 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
252 } else {
253 intfdata->in_flight++;
254 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700255 usb_anchor_urb(this_urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400256 err = usb_submit_urb(this_urb, GFP_ATOMIC);
257 if (err) {
258 dbg("usb_submit_urb %p (write bulk) failed "
259 "(%d)", this_urb, err);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700260 usb_unanchor_urb(this_urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400261 clear_bit(i, &portdata->out_busy);
262 spin_lock_irqsave(&intfdata->susp_lock, flags);
263 intfdata->in_flight--;
264 spin_unlock_irqrestore(&intfdata->susp_lock,
265 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100266 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100267 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400268 }
269 }
270
271 portdata->tx_start_time[i] = jiffies;
272 buf += todo;
273 left -= todo;
274 }
275
276 count -= left;
277 dbg("%s: wrote (did %d)", __func__, count);
278 return count;
279}
280EXPORT_SYMBOL(usb_wwan_write);
281
282static void usb_wwan_indat_callback(struct urb *urb)
283{
284 int err;
285 int endpoint;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700286 struct usb_wwan_port_private *portdata;
Matthew Garrett0d456192010-04-01 12:31:07 -0400287 struct usb_serial_port *port;
288 struct tty_struct *tty;
289 unsigned char *data = urb->transfer_buffer;
290 int status = urb->status;
291
292 dbg("%s: %p", __func__, urb);
293
294 endpoint = usb_pipeendpoint(urb->pipe);
295 port = urb->context;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700296 portdata = usb_get_serial_port_data(port);
Matthew Garrett0d456192010-04-01 12:31:07 -0400297
298 if (status) {
299 dbg("%s: nonzero status: %d on endpoint %02x.",
300 __func__, status, endpoint);
301 } else {
302 tty = tty_port_tty_get(&port->port);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100303 if (tty) {
304 if (urb->actual_length) {
305 tty_insert_flip_string(tty, data,
306 urb->actual_length);
307 tty_flip_buffer_push(tty);
308 } else
309 dbg("%s: empty read urb received", __func__);
310 tty_kref_put(tty);
311 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400312
313 /* Resubmit urb so we continue receiving */
314 if (status != -ESHUTDOWN) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700315 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400316 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100317 if (err) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700318 usb_unanchor_urb(urb);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100319 if (err != -EPERM) {
320 printk(KERN_ERR "%s: resubmit read urb failed. "
321 "(%d)", __func__, err);
322 /* busy also in error unless we are killed */
323 usb_mark_last_busy(port->serial->dev);
324 }
325 } else {
Matthew Garrett0d456192010-04-01 12:31:07 -0400326 usb_mark_last_busy(port->serial->dev);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100327 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400328 }
329
330 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400331}
332
333static void usb_wwan_outdat_callback(struct urb *urb)
334{
335 struct usb_serial_port *port;
336 struct usb_wwan_port_private *portdata;
337 struct usb_wwan_intf_private *intfdata;
338 int i;
339
340 dbg("%s", __func__);
341
342 port = urb->context;
343 intfdata = port->serial->private;
344
345 usb_serial_port_softint(port);
346 usb_autopm_put_interface_async(port->serial->interface);
347 portdata = usb_get_serial_port_data(port);
348 spin_lock(&intfdata->susp_lock);
349 intfdata->in_flight--;
350 spin_unlock(&intfdata->susp_lock);
351
352 for (i = 0; i < N_OUT_URB; ++i) {
353 if (portdata->out_urbs[i] == urb) {
354 smp_mb__before_clear_bit();
355 clear_bit(i, &portdata->out_busy);
356 break;
357 }
358 }
359}
360
361int usb_wwan_write_room(struct tty_struct *tty)
362{
363 struct usb_serial_port *port = tty->driver_data;
364 struct usb_wwan_port_private *portdata;
365 int i;
366 int data_len = 0;
367 struct urb *this_urb;
368
369 portdata = usb_get_serial_port_data(port);
370
371 for (i = 0; i < N_OUT_URB; i++) {
372 this_urb = portdata->out_urbs[i];
373 if (this_urb && !test_bit(i, &portdata->out_busy))
374 data_len += OUT_BUFLEN;
375 }
376
377 dbg("%s: %d", __func__, data_len);
378 return data_len;
379}
380EXPORT_SYMBOL(usb_wwan_write_room);
381
382int usb_wwan_chars_in_buffer(struct tty_struct *tty)
383{
384 struct usb_serial_port *port = tty->driver_data;
385 struct usb_wwan_port_private *portdata;
386 int i;
387 int data_len = 0;
388 struct urb *this_urb;
389
390 portdata = usb_get_serial_port_data(port);
391
392 for (i = 0; i < N_OUT_URB; i++) {
393 this_urb = portdata->out_urbs[i];
394 /* FIXME: This locking is insufficient as this_urb may
395 go unused during the test */
396 if (this_urb && test_bit(i, &portdata->out_busy))
397 data_len += this_urb->transfer_buffer_length;
398 }
399 dbg("%s: %d", __func__, data_len);
400 return data_len;
401}
402EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
403
404int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
405{
406 struct usb_wwan_port_private *portdata;
407 struct usb_wwan_intf_private *intfdata;
408 struct usb_serial *serial = port->serial;
409 int i, err;
410 struct urb *urb;
411
412 portdata = usb_get_serial_port_data(port);
413 intfdata = serial->private;
414
Vamsi Krishna68194482011-12-12 18:28:48 -0800415 /* explicitly set the driver mode to raw */
Vamsi Krishnac502ecc2012-01-03 15:44:00 -0800416 tty->raw = 1;
417 tty->real_raw = 1;
Vamsi Krishna68194482011-12-12 18:28:48 -0800418
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800419 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
Matthew Garrett0d456192010-04-01 12:31:07 -0400420 dbg("%s", __func__);
421
422 /* Start reading from the IN endpoint */
423 for (i = 0; i < N_IN_URB; i++) {
424 urb = portdata->in_urbs[i];
425 if (!urb)
426 continue;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700427 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400428 err = usb_submit_urb(urb, GFP_KERNEL);
429 if (err) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700430 usb_unanchor_urb(urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400431 dbg("%s: submit urb %d failed (%d) %d",
432 __func__, i, err, urb->transfer_buffer_length);
433 }
434 }
435
436 if (intfdata->send_setup)
437 intfdata->send_setup(port);
438
439 serial->interface->needs_remote_wakeup = 1;
440 spin_lock_irq(&intfdata->susp_lock);
441 portdata->opened = 1;
442 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100443 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400444 usb_autopm_put_interface(serial->interface);
445
446 return 0;
447}
448EXPORT_SYMBOL(usb_wwan_open);
449
450void usb_wwan_close(struct usb_serial_port *port)
451{
452 int i;
453 struct usb_serial *serial = port->serial;
454 struct usb_wwan_port_private *portdata;
455 struct usb_wwan_intf_private *intfdata = port->serial->private;
456
457 dbg("%s", __func__);
458 portdata = usb_get_serial_port_data(port);
459
460 if (serial->dev) {
461 /* Stop reading/writing urbs */
462 spin_lock_irq(&intfdata->susp_lock);
463 portdata->opened = 0;
464 spin_unlock_irq(&intfdata->susp_lock);
465
466 for (i = 0; i < N_IN_URB; i++)
467 usb_kill_urb(portdata->in_urbs[i]);
468 for (i = 0; i < N_OUT_URB; i++)
469 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100470 /* balancing - important as an error cannot be handled*/
471 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400472 serial->interface->needs_remote_wakeup = 0;
473 }
474}
475EXPORT_SYMBOL(usb_wwan_close);
476
477/* Helper functions used by usb_wwan_setup_urbs */
478static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
479 int dir, void *ctx, char *buf, int len,
480 void (*callback) (struct urb *))
481{
482 struct urb *urb;
483
484 if (endpoint == -1)
485 return NULL; /* endpoint not needed */
486
487 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
488 if (urb == NULL) {
489 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
490 return NULL;
491 }
492
493 /* Fill URB using supplied data. */
494 usb_fill_bulk_urb(urb, serial->dev,
495 usb_sndbulkpipe(serial->dev, endpoint) | dir,
496 buf, len, callback, ctx);
497
498 return urb;
499}
500
501/* Setup urbs */
502static void usb_wwan_setup_urbs(struct usb_serial *serial)
503{
504 int i, j;
505 struct usb_serial_port *port;
506 struct usb_wwan_port_private *portdata;
507
508 dbg("%s", __func__);
509
510 for (i = 0; i < serial->num_ports; i++) {
511 port = serial->port[i];
512 portdata = usb_get_serial_port_data(port);
513
514 /* Do indat endpoints first */
515 for (j = 0; j < N_IN_URB; ++j) {
516 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
517 port->
518 bulk_in_endpointAddress,
519 USB_DIR_IN,
520 port,
521 portdata->
522 in_buffer[j],
523 IN_BUFLEN,
524 usb_wwan_indat_callback);
525 }
526
527 /* outdat endpoints */
528 for (j = 0; j < N_OUT_URB; ++j) {
529 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
530 port->
531 bulk_out_endpointAddress,
532 USB_DIR_OUT,
533 port,
534 portdata->
535 out_buffer
536 [j],
537 OUT_BUFLEN,
538 usb_wwan_outdat_callback);
539 }
540 }
541}
542
543int usb_wwan_startup(struct usb_serial *serial)
544{
545 int i, j, err;
546 struct usb_serial_port *port;
547 struct usb_wwan_port_private *portdata;
548 u8 *buffer;
549
550 dbg("%s", __func__);
551
552 /* Now setup per port private data */
553 for (i = 0; i < serial->num_ports; i++) {
554 port = serial->port[i];
555 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
556 if (!portdata) {
557 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
558 __func__, i);
559 return 1;
560 }
561 init_usb_anchor(&portdata->delayed);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700562 init_usb_anchor(&portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400563
564 for (j = 0; j < N_IN_URB; j++) {
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800565 buffer = kmalloc(IN_BUFLEN, GFP_KERNEL);
Matthew Garrett0d456192010-04-01 12:31:07 -0400566 if (!buffer)
567 goto bail_out_error;
568 portdata->in_buffer[j] = buffer;
569 }
570
571 for (j = 0; j < N_OUT_URB; j++) {
572 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
573 if (!buffer)
574 goto bail_out_error2;
575 portdata->out_buffer[j] = buffer;
576 }
577
578 usb_set_serial_port_data(port, portdata);
579
580 if (!port->interrupt_in_urb)
581 continue;
582 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
583 if (err)
584 dbg("%s: submit irq_in urb failed %d", __func__, err);
585 }
586 usb_wwan_setup_urbs(serial);
587 return 0;
588
589bail_out_error2:
590 for (j = 0; j < N_OUT_URB; j++)
591 kfree(portdata->out_buffer[j]);
592bail_out_error:
593 for (j = 0; j < N_IN_URB; j++)
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800594 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400595 kfree(portdata);
596 return 1;
597}
598EXPORT_SYMBOL(usb_wwan_startup);
599
600static void stop_read_write_urbs(struct usb_serial *serial)
601{
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700602 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400603 struct usb_serial_port *port;
604 struct usb_wwan_port_private *portdata;
605
606 /* Stop reading/writing urbs */
607 for (i = 0; i < serial->num_ports; ++i) {
608 port = serial->port[i];
609 portdata = usb_get_serial_port_data(port);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700610 usb_kill_anchored_urbs(&portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400611 }
612}
613
614void usb_wwan_disconnect(struct usb_serial *serial)
615{
616 dbg("%s", __func__);
617
618 stop_read_write_urbs(serial);
619}
620EXPORT_SYMBOL(usb_wwan_disconnect);
621
622void usb_wwan_release(struct usb_serial *serial)
623{
624 int i, j;
625 struct usb_serial_port *port;
626 struct usb_wwan_port_private *portdata;
627
628 dbg("%s", __func__);
629
630 /* Now free them */
631 for (i = 0; i < serial->num_ports; ++i) {
632 port = serial->port[i];
633 portdata = usb_get_serial_port_data(port);
634
635 for (j = 0; j < N_IN_URB; j++) {
636 usb_free_urb(portdata->in_urbs[j]);
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800637 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400638 portdata->in_urbs[j] = NULL;
639 }
640 for (j = 0; j < N_OUT_URB; j++) {
641 usb_free_urb(portdata->out_urbs[j]);
642 kfree(portdata->out_buffer[j]);
643 portdata->out_urbs[j] = NULL;
644 }
645 }
646
647 /* Now free per port private data */
648 for (i = 0; i < serial->num_ports; i++) {
649 port = serial->port[i];
650 kfree(usb_get_serial_port_data(port));
651 }
652}
653EXPORT_SYMBOL(usb_wwan_release);
654
655#ifdef CONFIG_PM
656int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
657{
658 struct usb_wwan_intf_private *intfdata = serial->private;
659 int b;
660
661 dbg("%s entered", __func__);
662
Alan Stern5b1b0b82011-08-19 23:49:48 +0200663 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400664 spin_lock_irq(&intfdata->susp_lock);
665 b = intfdata->in_flight;
666 spin_unlock_irq(&intfdata->susp_lock);
667
668 if (b)
669 return -EBUSY;
670 }
671
672 spin_lock_irq(&intfdata->susp_lock);
673 intfdata->suspended = 1;
674 spin_unlock_irq(&intfdata->susp_lock);
675 stop_read_write_urbs(serial);
676
677 return 0;
678}
679EXPORT_SYMBOL(usb_wwan_suspend);
680
Oliver Neukum16871dc2011-02-10 15:33:29 +0100681static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
682{
683 int i;
684
685 for (i = 0; i < N_OUT_URB; i++) {
686 if (urb == portdata->out_urbs[i]) {
687 clear_bit(i, &portdata->out_busy);
688 break;
689 }
690 }
691}
692
Matthew Garrett0d456192010-04-01 12:31:07 -0400693static void play_delayed(struct usb_serial_port *port)
694{
695 struct usb_wwan_intf_private *data;
696 struct usb_wwan_port_private *portdata;
697 struct urb *urb;
698 int err;
699
700 portdata = usb_get_serial_port_data(port);
701 data = port->serial->private;
702 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700703 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400704 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100705 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400706 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100707 } else {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700708 usb_unanchor_urb(urb);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100709 /* we have to throw away the rest */
710 do {
711 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100712 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100713 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
714 break;
715 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400716 }
717}
718
719int usb_wwan_resume(struct usb_serial *serial)
720{
721 int i, j;
722 struct usb_serial_port *port;
723 struct usb_wwan_intf_private *intfdata = serial->private;
724 struct usb_wwan_port_private *portdata;
725 struct urb *urb;
726 int err = 0;
727
728 dbg("%s entered", __func__);
729 /* get the interrupt URBs resubmitted unconditionally */
730 for (i = 0; i < serial->num_ports; i++) {
731 port = serial->port[i];
732 if (!port->interrupt_in_urb) {
733 dbg("%s: No interrupt URB for port %d", __func__, i);
734 continue;
735 }
736 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
737 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
738 if (err < 0) {
739 err("%s: Error %d for interrupt URB of port%d",
740 __func__, err, i);
741 goto err_out;
742 }
743 }
744
Hemant Kumar8e52b122012-05-15 12:18:12 -0700745 spin_lock_irq(&intfdata->susp_lock);
746 intfdata->suspended = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400747 for (i = 0; i < serial->num_ports; i++) {
748 /* walk all ports */
749 port = serial->port[i];
750 portdata = usb_get_serial_port_data(port);
751
752 /* skip closed ports */
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700753 if (!portdata->opened)
Matthew Garrett0d456192010-04-01 12:31:07 -0400754 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400755
756 for (j = 0; j < N_IN_URB; j++) {
757 urb = portdata->in_urbs[j];
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700758 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400759 err = usb_submit_urb(urb, GFP_ATOMIC);
760 if (err < 0) {
761 err("%s: Error %d for bulk URB %d",
762 __func__, err, i);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700763 usb_unanchor_urb(urb);
764 intfdata->suspended = 1;
Matthew Garrett0d456192010-04-01 12:31:07 -0400765 spin_unlock_irq(&intfdata->susp_lock);
766 goto err_out;
767 }
768 }
769 play_delayed(port);
Matthew Garrett0d456192010-04-01 12:31:07 -0400770 }
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700771 spin_unlock_irq(&intfdata->susp_lock);
772
Matthew Garrett0d456192010-04-01 12:31:07 -0400773err_out:
774 return err;
775}
776EXPORT_SYMBOL(usb_wwan_resume);
777#endif
778
779MODULE_AUTHOR(DRIVER_AUTHOR);
780MODULE_DESCRIPTION(DRIVER_DESC);
781MODULE_VERSION(DRIVER_VERSION);
782MODULE_LICENSE("GPL");
783
784module_param(debug, bool, S_IRUGO | S_IWUSR);
785MODULE_PARM_DESC(debug, "Debug messages");