blob: 1355a6cd45089832ee73520e9e9b1c6343bf7a23 [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
Matthew Garrett0d456192010-04-01 12:31:07 -040022#define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
23#define DRIVER_DESC "USB Driver for GSM modems"
24
25#include <linux/kernel.h>
26#include <linux/jiffies.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/module.h>
32#include <linux/bitops.h>
Peter Huewe66921ed2010-12-09 23:27:35 +010033#include <linux/uaccess.h>
Matthew Garrett0d456192010-04-01 12:31:07 -040034#include <linux/usb.h>
35#include <linux/usb/serial.h>
Dan Williams02303f72010-11-19 16:04:00 -060036#include <linux/serial.h>
Matthew Garrett0d456192010-04-01 12:31:07 -040037#include "usb-wwan.h"
38
Matthew Garrett0d456192010-04-01 12:31:07 -040039void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
40{
Matthew Garrett0d456192010-04-01 12:31:07 -040041 struct usb_wwan_port_private *portdata;
Matthew Garrett0d456192010-04-01 12:31:07 -040042 struct usb_wwan_intf_private *intfdata;
43
Matthew Garrett0d456192010-04-01 12:31:07 -040044 intfdata = port->serial->private;
45
46 if (!intfdata->send_setup)
47 return;
48
49 portdata = usb_get_serial_port_data(port);
Johan Hovoldb2ca6992013-02-13 17:53:28 +010050 /* FIXME: locking */
Matthew Garrett0d456192010-04-01 12:31:07 -040051 portdata->rts_state = on;
52 portdata->dtr_state = on;
Johan Hovoldb2ca6992013-02-13 17:53:28 +010053
54 intfdata->send_setup(port);
Matthew Garrett0d456192010-04-01 12:31:07 -040055}
56EXPORT_SYMBOL(usb_wwan_dtr_rts);
57
58void usb_wwan_set_termios(struct tty_struct *tty,
59 struct usb_serial_port *port,
60 struct ktermios *old_termios)
61{
62 struct usb_wwan_intf_private *intfdata = port->serial->private;
63
Matthew Garrett0d456192010-04-01 12:31:07 -040064 /* Doesn't support option setting */
Alan Coxadc8d742012-07-14 15:31:47 +010065 tty_termios_copy_hw(&tty->termios, old_termios);
Matthew Garrett0d456192010-04-01 12:31:07 -040066
67 if (intfdata->send_setup)
68 intfdata->send_setup(port);
69}
70EXPORT_SYMBOL(usb_wwan_set_termios);
71
Alan Cox60b33c12011-02-14 16:26:14 +000072int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040073{
74 struct usb_serial_port *port = tty->driver_data;
75 unsigned int value;
76 struct usb_wwan_port_private *portdata;
77
78 portdata = usb_get_serial_port_data(port);
79
80 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
81 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
82 ((portdata->cts_state) ? TIOCM_CTS : 0) |
83 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
84 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
85 ((portdata->ri_state) ? TIOCM_RNG : 0);
86
87 return value;
88}
89EXPORT_SYMBOL(usb_wwan_tiocmget);
90
Alan Cox20b9d172011-02-14 16:26:50 +000091int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -040092 unsigned int set, unsigned int clear)
93{
94 struct usb_serial_port *port = tty->driver_data;
95 struct usb_wwan_port_private *portdata;
96 struct usb_wwan_intf_private *intfdata;
97
98 portdata = usb_get_serial_port_data(port);
99 intfdata = port->serial->private;
100
101 if (!intfdata->send_setup)
102 return -EINVAL;
103
104 /* FIXME: what locks portdata fields ? */
105 if (set & TIOCM_RTS)
106 portdata->rts_state = 1;
107 if (set & TIOCM_DTR)
108 portdata->dtr_state = 1;
109
110 if (clear & TIOCM_RTS)
111 portdata->rts_state = 0;
112 if (clear & TIOCM_DTR)
113 portdata->dtr_state = 0;
114 return intfdata->send_setup(port);
115}
116EXPORT_SYMBOL(usb_wwan_tiocmset);
117
Dan Williams02303f72010-11-19 16:04:00 -0600118static int get_serial_info(struct usb_serial_port *port,
119 struct serial_struct __user *retinfo)
120{
121 struct serial_struct tmp;
122
123 if (!retinfo)
124 return -EFAULT;
125
126 memset(&tmp, 0, sizeof(tmp));
127 tmp.line = port->serial->minor;
128 tmp.port = port->number;
129 tmp.baud_base = tty_get_baud_rate(port->port.tty);
130 tmp.close_delay = port->port.close_delay / 10;
131 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
132 ASYNC_CLOSING_WAIT_NONE :
133 port->port.closing_wait / 10;
134
135 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
136 return -EFAULT;
137 return 0;
138}
139
140static int set_serial_info(struct usb_serial_port *port,
141 struct serial_struct __user *newinfo)
142{
143 struct serial_struct new_serial;
144 unsigned int closing_wait, close_delay;
145 int retval = 0;
146
147 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
148 return -EFAULT;
149
150 close_delay = new_serial.close_delay * 10;
151 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
152 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
153
154 mutex_lock(&port->port.mutex);
155
156 if (!capable(CAP_SYS_ADMIN)) {
157 if ((close_delay != port->port.close_delay) ||
158 (closing_wait != port->port.closing_wait))
159 retval = -EPERM;
160 else
161 retval = -EOPNOTSUPP;
162 } else {
163 port->port.close_delay = close_delay;
164 port->port.closing_wait = closing_wait;
165 }
166
167 mutex_unlock(&port->port.mutex);
168 return retval;
169}
170
Alan Cox00a0d0d2011-02-14 16:27:06 +0000171int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600172 unsigned int cmd, unsigned long arg)
173{
174 struct usb_serial_port *port = tty->driver_data;
175
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700176 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Dan Williams02303f72010-11-19 16:04:00 -0600177
178 switch (cmd) {
179 case TIOCGSERIAL:
180 return get_serial_info(port,
181 (struct serial_struct __user *) arg);
182 case TIOCSSERIAL:
183 return set_serial_info(port,
184 (struct serial_struct __user *) arg);
185 default:
186 break;
187 }
188
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700189 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Dan Williams02303f72010-11-19 16:04:00 -0600190
191 return -ENOIOCTLCMD;
192}
193EXPORT_SYMBOL(usb_wwan_ioctl);
194
Matthew Garrett0d456192010-04-01 12:31:07 -0400195/* Write */
196int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
197 const unsigned char *buf, int count)
198{
199 struct usb_wwan_port_private *portdata;
200 struct usb_wwan_intf_private *intfdata;
201 int i;
202 int left, todo;
203 struct urb *this_urb = NULL; /* spurious */
204 int err;
205 unsigned long flags;
206
207 portdata = usb_get_serial_port_data(port);
208 intfdata = port->serial->private;
209
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700210 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400211
212 i = 0;
213 left = count;
214 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
215 todo = left;
216 if (todo > OUT_BUFLEN)
217 todo = OUT_BUFLEN;
218
219 this_urb = portdata->out_urbs[i];
220 if (test_and_set_bit(i, &portdata->out_busy)) {
221 if (time_before(jiffies,
222 portdata->tx_start_time[i] + 10 * HZ))
223 continue;
224 usb_unlink_urb(this_urb);
225 continue;
226 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700227 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
228 usb_pipeendpoint(this_urb->pipe), i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400229
230 err = usb_autopm_get_interface_async(port->serial->interface);
231 if (err < 0)
232 break;
233
234 /* send the data */
235 memcpy(this_urb->transfer_buffer, buf, todo);
236 this_urb->transfer_buffer_length = todo;
237
238 spin_lock_irqsave(&intfdata->susp_lock, flags);
239 if (intfdata->suspended) {
240 usb_anchor_urb(this_urb, &portdata->delayed);
241 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
242 } else {
243 intfdata->in_flight++;
244 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
245 err = usb_submit_urb(this_urb, GFP_ATOMIC);
246 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700247 dev_dbg(&port->dev,
248 "usb_submit_urb %p (write bulk) failed (%d)\n",
249 this_urb, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400250 clear_bit(i, &portdata->out_busy);
251 spin_lock_irqsave(&intfdata->susp_lock, flags);
252 intfdata->in_flight--;
253 spin_unlock_irqrestore(&intfdata->susp_lock,
254 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100255 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100256 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400257 }
258 }
259
260 portdata->tx_start_time[i] = jiffies;
261 buf += todo;
262 left -= todo;
263 }
264
265 count -= left;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700266 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400267 return count;
268}
269EXPORT_SYMBOL(usb_wwan_write);
270
271static void usb_wwan_indat_callback(struct urb *urb)
272{
273 int err;
274 int endpoint;
275 struct usb_serial_port *port;
276 struct tty_struct *tty;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700277 struct device *dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400278 unsigned char *data = urb->transfer_buffer;
279 int status = urb->status;
280
Matthew Garrett0d456192010-04-01 12:31:07 -0400281 endpoint = usb_pipeendpoint(urb->pipe);
282 port = urb->context;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700283 dev = &port->dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400284
285 if (status) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700286 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
287 __func__, status, endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400288 } else {
289 tty = tty_port_tty_get(&port->port);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100290 if (tty) {
291 if (urb->actual_length) {
292 tty_insert_flip_string(tty, data,
293 urb->actual_length);
294 tty_flip_buffer_push(tty);
295 } else
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700296 dev_dbg(dev, "%s: empty read urb received\n", __func__);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100297 tty_kref_put(tty);
298 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400299
300 /* Resubmit urb so we continue receiving */
Dan Carpenterec428992012-04-20 09:33:31 +0300301 err = usb_submit_urb(urb, GFP_ATOMIC);
302 if (err) {
303 if (err != -EPERM) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700304 dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err);
Dan Carpenterec428992012-04-20 09:33:31 +0300305 /* busy also in error unless we are killed */
Matthew Garrett0d456192010-04-01 12:31:07 -0400306 usb_mark_last_busy(port->serial->dev);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100307 }
Dan Carpenterec428992012-04-20 09:33:31 +0300308 } else {
309 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400310 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400311 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400312}
313
314static void usb_wwan_outdat_callback(struct urb *urb)
315{
316 struct usb_serial_port *port;
317 struct usb_wwan_port_private *portdata;
318 struct usb_wwan_intf_private *intfdata;
319 int i;
320
Matthew Garrett0d456192010-04-01 12:31:07 -0400321 port = urb->context;
322 intfdata = port->serial->private;
323
324 usb_serial_port_softint(port);
325 usb_autopm_put_interface_async(port->serial->interface);
326 portdata = usb_get_serial_port_data(port);
327 spin_lock(&intfdata->susp_lock);
328 intfdata->in_flight--;
329 spin_unlock(&intfdata->susp_lock);
330
331 for (i = 0; i < N_OUT_URB; ++i) {
332 if (portdata->out_urbs[i] == urb) {
333 smp_mb__before_clear_bit();
334 clear_bit(i, &portdata->out_busy);
335 break;
336 }
337 }
338}
339
340int usb_wwan_write_room(struct tty_struct *tty)
341{
342 struct usb_serial_port *port = tty->driver_data;
343 struct usb_wwan_port_private *portdata;
344 int i;
345 int data_len = 0;
346 struct urb *this_urb;
347
348 portdata = usb_get_serial_port_data(port);
349
350 for (i = 0; i < N_OUT_URB; i++) {
351 this_urb = portdata->out_urbs[i];
352 if (this_urb && !test_bit(i, &portdata->out_busy))
353 data_len += OUT_BUFLEN;
354 }
355
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700356 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400357 return data_len;
358}
359EXPORT_SYMBOL(usb_wwan_write_room);
360
361int usb_wwan_chars_in_buffer(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 /* FIXME: This locking is insufficient as this_urb may
374 go unused during the test */
375 if (this_urb && test_bit(i, &portdata->out_busy))
376 data_len += this_urb->transfer_buffer_length;
377 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700378 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400379 return data_len;
380}
381EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
382
383int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
384{
385 struct usb_wwan_port_private *portdata;
386 struct usb_wwan_intf_private *intfdata;
387 struct usb_serial *serial = port->serial;
388 int i, err;
389 struct urb *urb;
390
391 portdata = usb_get_serial_port_data(port);
392 intfdata = serial->private;
393
Matthew Garrett0d456192010-04-01 12:31:07 -0400394 /* Start reading from the IN endpoint */
395 for (i = 0; i < N_IN_URB; i++) {
396 urb = portdata->in_urbs[i];
397 if (!urb)
398 continue;
399 err = usb_submit_urb(urb, GFP_KERNEL);
400 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700401 dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
402 __func__, i, err, urb->transfer_buffer_length);
Matthew Garrett0d456192010-04-01 12:31:07 -0400403 }
404 }
405
406 if (intfdata->send_setup)
407 intfdata->send_setup(port);
408
409 serial->interface->needs_remote_wakeup = 1;
410 spin_lock_irq(&intfdata->susp_lock);
411 portdata->opened = 1;
412 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100413 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400414 usb_autopm_put_interface(serial->interface);
415
416 return 0;
417}
418EXPORT_SYMBOL(usb_wwan_open);
419
420void usb_wwan_close(struct usb_serial_port *port)
421{
422 int i;
423 struct usb_serial *serial = port->serial;
424 struct usb_wwan_port_private *portdata;
425 struct usb_wwan_intf_private *intfdata = port->serial->private;
426
Matthew Garrett0d456192010-04-01 12:31:07 -0400427 portdata = usb_get_serial_port_data(port);
428
429 if (serial->dev) {
430 /* Stop reading/writing urbs */
431 spin_lock_irq(&intfdata->susp_lock);
432 portdata->opened = 0;
433 spin_unlock_irq(&intfdata->susp_lock);
434
435 for (i = 0; i < N_IN_URB; i++)
436 usb_kill_urb(portdata->in_urbs[i]);
437 for (i = 0; i < N_OUT_URB; i++)
438 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100439 /* balancing - important as an error cannot be handled*/
440 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400441 serial->interface->needs_remote_wakeup = 0;
442 }
443}
444EXPORT_SYMBOL(usb_wwan_close);
445
446/* Helper functions used by usb_wwan_setup_urbs */
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200447static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
448 int endpoint,
Matthew Garrett0d456192010-04-01 12:31:07 -0400449 int dir, void *ctx, char *buf, int len,
450 void (*callback) (struct urb *))
451{
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200452 struct usb_serial *serial = port->serial;
Matthew Garrett0d456192010-04-01 12:31:07 -0400453 struct urb *urb;
454
Matthew Garrett0d456192010-04-01 12:31:07 -0400455 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
456 if (urb == NULL) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700457 dev_dbg(&serial->interface->dev,
458 "%s: alloc for endpoint %d failed.\n", __func__,
459 endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400460 return NULL;
461 }
462
463 /* Fill URB using supplied data. */
464 usb_fill_bulk_urb(urb, serial->dev,
465 usb_sndbulkpipe(serial->dev, endpoint) | dir,
466 buf, len, callback, ctx);
467
468 return urb;
469}
470
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200471int usb_wwan_port_probe(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400472{
Matthew Garrett0d456192010-04-01 12:31:07 -0400473 struct usb_wwan_port_private *portdata;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200474 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400475 u8 *buffer;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200476 int err;
477 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400478
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200479 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
480 if (!portdata)
481 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400482
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200483 init_usb_anchor(&portdata->delayed);
Matthew Garrett0d456192010-04-01 12:31:07 -0400484
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200485 for (i = 0; i < N_IN_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200486 if (!port->bulk_in_size)
487 break;
488
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200489 buffer = (u8 *)__get_free_page(GFP_KERNEL);
490 if (!buffer)
491 goto bail_out_error;
492 portdata->in_buffer[i] = buffer;
Matthew Garrett0d456192010-04-01 12:31:07 -0400493
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200494 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
495 USB_DIR_IN, port,
496 buffer, IN_BUFLEN,
497 usb_wwan_indat_callback);
498 portdata->in_urbs[i] = urb;
499 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400500
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200501 for (i = 0; i < N_OUT_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200502 if (!port->bulk_out_size)
503 break;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200504
505 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
506 if (!buffer)
507 goto bail_out_error2;
508 portdata->out_buffer[i] = buffer;
509
510 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
511 USB_DIR_OUT, port,
512 buffer, OUT_BUFLEN,
513 usb_wwan_outdat_callback);
514 portdata->out_urbs[i] = urb;
515 }
516
517 usb_set_serial_port_data(port, portdata);
518
519 if (port->interrupt_in_urb) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400520 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
521 if (err)
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700522 dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
523 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400524 }
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200525
Matthew Garrett0d456192010-04-01 12:31:07 -0400526 return 0;
527
528bail_out_error2:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200529 for (i = 0; i < N_OUT_URB; i++) {
530 usb_free_urb(portdata->out_urbs[i]);
531 kfree(portdata->out_buffer[i]);
532 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400533bail_out_error:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200534 for (i = 0; i < N_IN_URB; i++) {
535 usb_free_urb(portdata->in_urbs[i]);
536 free_page((unsigned long)portdata->in_buffer[i]);
537 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400538 kfree(portdata);
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200539
540 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400541}
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200542EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
Matthew Garrett0d456192010-04-01 12:31:07 -0400543
Bjørn Morka1028f02012-07-27 01:11:41 +0200544int usb_wwan_port_remove(struct usb_serial_port *port)
545{
546 int i;
547 struct usb_wwan_port_private *portdata;
548
549 portdata = usb_get_serial_port_data(port);
550 usb_set_serial_port_data(port, NULL);
551
552 /* Stop reading/writing urbs and free them */
553 for (i = 0; i < N_IN_URB; i++) {
554 usb_kill_urb(portdata->in_urbs[i]);
555 usb_free_urb(portdata->in_urbs[i]);
556 free_page((unsigned long)portdata->in_buffer[i]);
557 }
558 for (i = 0; i < N_OUT_URB; i++) {
559 usb_kill_urb(portdata->out_urbs[i]);
560 usb_free_urb(portdata->out_urbs[i]);
561 kfree(portdata->out_buffer[i]);
562 }
563
564 /* Now free port private data */
565 kfree(portdata);
566 return 0;
567}
568EXPORT_SYMBOL(usb_wwan_port_remove);
569
570#ifdef CONFIG_PM
Matthew Garrett0d456192010-04-01 12:31:07 -0400571static void stop_read_write_urbs(struct usb_serial *serial)
572{
573 int i, j;
574 struct usb_serial_port *port;
575 struct usb_wwan_port_private *portdata;
576
577 /* Stop reading/writing urbs */
578 for (i = 0; i < serial->num_ports; ++i) {
579 port = serial->port[i];
580 portdata = usb_get_serial_port_data(port);
Bjørn Mork032129c2012-07-27 01:11:43 +0200581 if (!portdata)
582 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400583 for (j = 0; j < N_IN_URB; j++)
584 usb_kill_urb(portdata->in_urbs[j]);
585 for (j = 0; j < N_OUT_URB; j++)
586 usb_kill_urb(portdata->out_urbs[j]);
587 }
588}
589
Matthew Garrett0d456192010-04-01 12:31:07 -0400590int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
591{
592 struct usb_wwan_intf_private *intfdata = serial->private;
593 int b;
594
Alan Stern5b1b0b82011-08-19 23:49:48 +0200595 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400596 spin_lock_irq(&intfdata->susp_lock);
597 b = intfdata->in_flight;
598 spin_unlock_irq(&intfdata->susp_lock);
599
600 if (b)
601 return -EBUSY;
602 }
603
604 spin_lock_irq(&intfdata->susp_lock);
605 intfdata->suspended = 1;
606 spin_unlock_irq(&intfdata->susp_lock);
607 stop_read_write_urbs(serial);
608
609 return 0;
610}
611EXPORT_SYMBOL(usb_wwan_suspend);
612
Oliver Neukum16871dc2011-02-10 15:33:29 +0100613static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
614{
615 int i;
616
617 for (i = 0; i < N_OUT_URB; i++) {
618 if (urb == portdata->out_urbs[i]) {
619 clear_bit(i, &portdata->out_busy);
620 break;
621 }
622 }
623}
624
Matthew Garrett0d456192010-04-01 12:31:07 -0400625static void play_delayed(struct usb_serial_port *port)
626{
627 struct usb_wwan_intf_private *data;
628 struct usb_wwan_port_private *portdata;
629 struct urb *urb;
630 int err;
631
632 portdata = usb_get_serial_port_data(port);
633 data = port->serial->private;
634 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
635 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100636 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400637 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100638 } else {
639 /* we have to throw away the rest */
640 do {
641 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100642 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100643 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
644 break;
645 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400646 }
647}
648
649int usb_wwan_resume(struct usb_serial *serial)
650{
651 int i, j;
652 struct usb_serial_port *port;
653 struct usb_wwan_intf_private *intfdata = serial->private;
654 struct usb_wwan_port_private *portdata;
655 struct urb *urb;
656 int err = 0;
657
Matthew Garrett0d456192010-04-01 12:31:07 -0400658 /* get the interrupt URBs resubmitted unconditionally */
659 for (i = 0; i < serial->num_ports; i++) {
660 port = serial->port[i];
661 if (!port->interrupt_in_urb) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700662 dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
Matthew Garrett0d456192010-04-01 12:31:07 -0400663 continue;
664 }
665 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700666 dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400667 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700668 dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
669 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400670 goto err_out;
671 }
672 }
673
674 for (i = 0; i < serial->num_ports; i++) {
675 /* walk all ports */
676 port = serial->port[i];
677 portdata = usb_get_serial_port_data(port);
678
679 /* skip closed ports */
680 spin_lock_irq(&intfdata->susp_lock);
Bjørn Mork032129c2012-07-27 01:11:43 +0200681 if (!portdata || !portdata->opened) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400682 spin_unlock_irq(&intfdata->susp_lock);
683 continue;
684 }
685
686 for (j = 0; j < N_IN_URB; j++) {
687 urb = portdata->in_urbs[j];
688 err = usb_submit_urb(urb, GFP_ATOMIC);
689 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700690 dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
691 __func__, err, i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400692 spin_unlock_irq(&intfdata->susp_lock);
693 goto err_out;
694 }
695 }
696 play_delayed(port);
697 spin_unlock_irq(&intfdata->susp_lock);
698 }
699 spin_lock_irq(&intfdata->susp_lock);
700 intfdata->suspended = 0;
701 spin_unlock_irq(&intfdata->susp_lock);
702err_out:
703 return err;
704}
705EXPORT_SYMBOL(usb_wwan_resume);
706#endif
707
708MODULE_AUTHOR(DRIVER_AUTHOR);
709MODULE_DESCRIPTION(DRIVER_DESC);
Matthew Garrett0d456192010-04-01 12:31:07 -0400710MODULE_LICENSE("GPL");