blob: a3e9c095f0d83a39738ef74b9e3a9ef7b41a2d6e [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
Matthew Garrett0d456192010-04-01 12:31:07 -040040void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
41{
42 struct usb_serial *serial = port->serial;
43 struct usb_wwan_port_private *portdata;
Matthew Garrett0d456192010-04-01 12:31:07 -040044 struct usb_wwan_intf_private *intfdata;
45
Matthew Garrett0d456192010-04-01 12:31:07 -040046 intfdata = port->serial->private;
47
48 if (!intfdata->send_setup)
49 return;
50
51 portdata = usb_get_serial_port_data(port);
52 mutex_lock(&serial->disc_mutex);
53 portdata->rts_state = on;
54 portdata->dtr_state = on;
55 if (serial->dev)
56 intfdata->send_setup(port);
57 mutex_unlock(&serial->disc_mutex);
58}
59EXPORT_SYMBOL(usb_wwan_dtr_rts);
60
61void usb_wwan_set_termios(struct tty_struct *tty,
62 struct usb_serial_port *port,
63 struct ktermios *old_termios)
64{
65 struct usb_wwan_intf_private *intfdata = port->serial->private;
66
Matthew Garrett0d456192010-04-01 12:31:07 -040067 /* Doesn't support option setting */
Alan Coxadc8d742012-07-14 15:31:47 +010068 tty_termios_copy_hw(&tty->termios, old_termios);
Matthew Garrett0d456192010-04-01 12:31:07 -040069
70 if (intfdata->send_setup)
71 intfdata->send_setup(port);
72}
73EXPORT_SYMBOL(usb_wwan_set_termios);
74
Alan Cox60b33c12011-02-14 16:26:14 +000075int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040076{
77 struct usb_serial_port *port = tty->driver_data;
78 unsigned int value;
79 struct usb_wwan_port_private *portdata;
80
81 portdata = usb_get_serial_port_data(port);
82
83 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
84 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
85 ((portdata->cts_state) ? TIOCM_CTS : 0) |
86 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
87 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
88 ((portdata->ri_state) ? TIOCM_RNG : 0);
89
90 return value;
91}
92EXPORT_SYMBOL(usb_wwan_tiocmget);
93
Alan Cox20b9d172011-02-14 16:26:50 +000094int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -040095 unsigned int set, unsigned int clear)
96{
97 struct usb_serial_port *port = tty->driver_data;
98 struct usb_wwan_port_private *portdata;
99 struct usb_wwan_intf_private *intfdata;
100
101 portdata = usb_get_serial_port_data(port);
102 intfdata = port->serial->private;
103
104 if (!intfdata->send_setup)
105 return -EINVAL;
106
107 /* FIXME: what locks portdata fields ? */
108 if (set & TIOCM_RTS)
109 portdata->rts_state = 1;
110 if (set & TIOCM_DTR)
111 portdata->dtr_state = 1;
112
113 if (clear & TIOCM_RTS)
114 portdata->rts_state = 0;
115 if (clear & TIOCM_DTR)
116 portdata->dtr_state = 0;
117 return intfdata->send_setup(port);
118}
119EXPORT_SYMBOL(usb_wwan_tiocmset);
120
Dan Williams02303f72010-11-19 16:04:00 -0600121static int get_serial_info(struct usb_serial_port *port,
122 struct serial_struct __user *retinfo)
123{
124 struct serial_struct tmp;
125
126 if (!retinfo)
127 return -EFAULT;
128
129 memset(&tmp, 0, sizeof(tmp));
130 tmp.line = port->serial->minor;
131 tmp.port = port->number;
132 tmp.baud_base = tty_get_baud_rate(port->port.tty);
133 tmp.close_delay = port->port.close_delay / 10;
134 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
135 ASYNC_CLOSING_WAIT_NONE :
136 port->port.closing_wait / 10;
137
138 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
139 return -EFAULT;
140 return 0;
141}
142
143static int set_serial_info(struct usb_serial_port *port,
144 struct serial_struct __user *newinfo)
145{
146 struct serial_struct new_serial;
147 unsigned int closing_wait, close_delay;
148 int retval = 0;
149
150 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
151 return -EFAULT;
152
153 close_delay = new_serial.close_delay * 10;
154 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
155 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
156
157 mutex_lock(&port->port.mutex);
158
159 if (!capable(CAP_SYS_ADMIN)) {
160 if ((close_delay != port->port.close_delay) ||
161 (closing_wait != port->port.closing_wait))
162 retval = -EPERM;
163 else
164 retval = -EOPNOTSUPP;
165 } else {
166 port->port.close_delay = close_delay;
167 port->port.closing_wait = closing_wait;
168 }
169
170 mutex_unlock(&port->port.mutex);
171 return retval;
172}
173
Alan Cox00a0d0d2011-02-14 16:27:06 +0000174int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600175 unsigned int cmd, unsigned long arg)
176{
177 struct usb_serial_port *port = tty->driver_data;
178
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700179 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Dan Williams02303f72010-11-19 16:04:00 -0600180
181 switch (cmd) {
182 case TIOCGSERIAL:
183 return get_serial_info(port,
184 (struct serial_struct __user *) arg);
185 case TIOCSSERIAL:
186 return set_serial_info(port,
187 (struct serial_struct __user *) arg);
188 default:
189 break;
190 }
191
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700192 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Dan Williams02303f72010-11-19 16:04:00 -0600193
194 return -ENOIOCTLCMD;
195}
196EXPORT_SYMBOL(usb_wwan_ioctl);
197
Matthew Garrett0d456192010-04-01 12:31:07 -0400198/* Write */
199int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
200 const unsigned char *buf, int count)
201{
202 struct usb_wwan_port_private *portdata;
203 struct usb_wwan_intf_private *intfdata;
204 int i;
205 int left, todo;
206 struct urb *this_urb = NULL; /* spurious */
207 int err;
208 unsigned long flags;
209
210 portdata = usb_get_serial_port_data(port);
211 intfdata = port->serial->private;
212
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700213 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400214
215 i = 0;
216 left = count;
217 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
218 todo = left;
219 if (todo > OUT_BUFLEN)
220 todo = OUT_BUFLEN;
221
222 this_urb = portdata->out_urbs[i];
223 if (test_and_set_bit(i, &portdata->out_busy)) {
224 if (time_before(jiffies,
225 portdata->tx_start_time[i] + 10 * HZ))
226 continue;
227 usb_unlink_urb(this_urb);
228 continue;
229 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700230 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
231 usb_pipeendpoint(this_urb->pipe), i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400232
233 err = usb_autopm_get_interface_async(port->serial->interface);
234 if (err < 0)
235 break;
236
237 /* send the data */
238 memcpy(this_urb->transfer_buffer, buf, todo);
239 this_urb->transfer_buffer_length = todo;
240
241 spin_lock_irqsave(&intfdata->susp_lock, flags);
242 if (intfdata->suspended) {
243 usb_anchor_urb(this_urb, &portdata->delayed);
244 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
245 } else {
246 intfdata->in_flight++;
247 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
248 err = usb_submit_urb(this_urb, GFP_ATOMIC);
249 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700250 dev_dbg(&port->dev,
251 "usb_submit_urb %p (write bulk) failed (%d)\n",
252 this_urb, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400253 clear_bit(i, &portdata->out_busy);
254 spin_lock_irqsave(&intfdata->susp_lock, flags);
255 intfdata->in_flight--;
256 spin_unlock_irqrestore(&intfdata->susp_lock,
257 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100258 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100259 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400260 }
261 }
262
263 portdata->tx_start_time[i] = jiffies;
264 buf += todo;
265 left -= todo;
266 }
267
268 count -= left;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700269 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400270 return count;
271}
272EXPORT_SYMBOL(usb_wwan_write);
273
274static void usb_wwan_indat_callback(struct urb *urb)
275{
276 int err;
277 int endpoint;
278 struct usb_serial_port *port;
279 struct tty_struct *tty;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700280 struct device *dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400281 unsigned char *data = urb->transfer_buffer;
282 int status = urb->status;
283
Matthew Garrett0d456192010-04-01 12:31:07 -0400284 endpoint = usb_pipeendpoint(urb->pipe);
285 port = urb->context;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700286 dev = &port->dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400287
288 if (status) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700289 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
290 __func__, status, endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400291 } else {
292 tty = tty_port_tty_get(&port->port);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100293 if (tty) {
294 if (urb->actual_length) {
295 tty_insert_flip_string(tty, data,
296 urb->actual_length);
297 tty_flip_buffer_push(tty);
298 } else
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700299 dev_dbg(dev, "%s: empty read urb received\n", __func__);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100300 tty_kref_put(tty);
301 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400302
303 /* Resubmit urb so we continue receiving */
Dan Carpenterec428992012-04-20 09:33:31 +0300304 err = usb_submit_urb(urb, GFP_ATOMIC);
305 if (err) {
306 if (err != -EPERM) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700307 dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err);
Dan Carpenterec428992012-04-20 09:33:31 +0300308 /* busy also in error unless we are killed */
Matthew Garrett0d456192010-04-01 12:31:07 -0400309 usb_mark_last_busy(port->serial->dev);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100310 }
Dan Carpenterec428992012-04-20 09:33:31 +0300311 } else {
312 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400313 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400314 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400315}
316
317static void usb_wwan_outdat_callback(struct urb *urb)
318{
319 struct usb_serial_port *port;
320 struct usb_wwan_port_private *portdata;
321 struct usb_wwan_intf_private *intfdata;
322 int i;
323
Matthew Garrett0d456192010-04-01 12:31:07 -0400324 port = urb->context;
325 intfdata = port->serial->private;
326
327 usb_serial_port_softint(port);
328 usb_autopm_put_interface_async(port->serial->interface);
329 portdata = usb_get_serial_port_data(port);
330 spin_lock(&intfdata->susp_lock);
331 intfdata->in_flight--;
332 spin_unlock(&intfdata->susp_lock);
333
334 for (i = 0; i < N_OUT_URB; ++i) {
335 if (portdata->out_urbs[i] == urb) {
336 smp_mb__before_clear_bit();
337 clear_bit(i, &portdata->out_busy);
338 break;
339 }
340 }
341}
342
343int usb_wwan_write_room(struct tty_struct *tty)
344{
345 struct usb_serial_port *port = tty->driver_data;
346 struct usb_wwan_port_private *portdata;
347 int i;
348 int data_len = 0;
349 struct urb *this_urb;
350
351 portdata = usb_get_serial_port_data(port);
352
353 for (i = 0; i < N_OUT_URB; i++) {
354 this_urb = portdata->out_urbs[i];
355 if (this_urb && !test_bit(i, &portdata->out_busy))
356 data_len += OUT_BUFLEN;
357 }
358
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700359 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400360 return data_len;
361}
362EXPORT_SYMBOL(usb_wwan_write_room);
363
364int usb_wwan_chars_in_buffer(struct tty_struct *tty)
365{
366 struct usb_serial_port *port = tty->driver_data;
367 struct usb_wwan_port_private *portdata;
368 int i;
369 int data_len = 0;
370 struct urb *this_urb;
371
372 portdata = usb_get_serial_port_data(port);
373
374 for (i = 0; i < N_OUT_URB; i++) {
375 this_urb = portdata->out_urbs[i];
376 /* FIXME: This locking is insufficient as this_urb may
377 go unused during the test */
378 if (this_urb && test_bit(i, &portdata->out_busy))
379 data_len += this_urb->transfer_buffer_length;
380 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700381 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400382 return data_len;
383}
384EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
385
386int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
387{
388 struct usb_wwan_port_private *portdata;
389 struct usb_wwan_intf_private *intfdata;
390 struct usb_serial *serial = port->serial;
391 int i, err;
392 struct urb *urb;
393
394 portdata = usb_get_serial_port_data(port);
395 intfdata = serial->private;
396
Matthew Garrett0d456192010-04-01 12:31:07 -0400397 /* Start reading from the IN endpoint */
398 for (i = 0; i < N_IN_URB; i++) {
399 urb = portdata->in_urbs[i];
400 if (!urb)
401 continue;
402 err = usb_submit_urb(urb, GFP_KERNEL);
403 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700404 dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
405 __func__, i, err, urb->transfer_buffer_length);
Matthew Garrett0d456192010-04-01 12:31:07 -0400406 }
407 }
408
409 if (intfdata->send_setup)
410 intfdata->send_setup(port);
411
412 serial->interface->needs_remote_wakeup = 1;
413 spin_lock_irq(&intfdata->susp_lock);
414 portdata->opened = 1;
415 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100416 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400417 usb_autopm_put_interface(serial->interface);
418
419 return 0;
420}
421EXPORT_SYMBOL(usb_wwan_open);
422
423void usb_wwan_close(struct usb_serial_port *port)
424{
425 int i;
426 struct usb_serial *serial = port->serial;
427 struct usb_wwan_port_private *portdata;
428 struct usb_wwan_intf_private *intfdata = port->serial->private;
429
Matthew Garrett0d456192010-04-01 12:31:07 -0400430 portdata = usb_get_serial_port_data(port);
431
432 if (serial->dev) {
433 /* Stop reading/writing urbs */
434 spin_lock_irq(&intfdata->susp_lock);
435 portdata->opened = 0;
436 spin_unlock_irq(&intfdata->susp_lock);
437
438 for (i = 0; i < N_IN_URB; i++)
439 usb_kill_urb(portdata->in_urbs[i]);
440 for (i = 0; i < N_OUT_URB; i++)
441 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100442 /* balancing - important as an error cannot be handled*/
443 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400444 serial->interface->needs_remote_wakeup = 0;
445 }
446}
447EXPORT_SYMBOL(usb_wwan_close);
448
449/* Helper functions used by usb_wwan_setup_urbs */
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200450static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
451 int endpoint,
Matthew Garrett0d456192010-04-01 12:31:07 -0400452 int dir, void *ctx, char *buf, int len,
453 void (*callback) (struct urb *))
454{
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200455 struct usb_serial *serial = port->serial;
Matthew Garrett0d456192010-04-01 12:31:07 -0400456 struct urb *urb;
457
Matthew Garrett0d456192010-04-01 12:31:07 -0400458 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
459 if (urb == NULL) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700460 dev_dbg(&serial->interface->dev,
461 "%s: alloc for endpoint %d failed.\n", __func__,
462 endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400463 return NULL;
464 }
465
466 /* Fill URB using supplied data. */
467 usb_fill_bulk_urb(urb, serial->dev,
468 usb_sndbulkpipe(serial->dev, endpoint) | dir,
469 buf, len, callback, ctx);
470
471 return urb;
472}
473
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200474int usb_wwan_port_probe(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400475{
Matthew Garrett0d456192010-04-01 12:31:07 -0400476 struct usb_wwan_port_private *portdata;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200477 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400478 u8 *buffer;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200479 int err;
480 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400481
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200482 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
483 if (!portdata)
484 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400485
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200486 init_usb_anchor(&portdata->delayed);
Matthew Garrett0d456192010-04-01 12:31:07 -0400487
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200488 for (i = 0; i < N_IN_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200489 if (!port->bulk_in_size)
490 break;
491
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200492 buffer = (u8 *)__get_free_page(GFP_KERNEL);
493 if (!buffer)
494 goto bail_out_error;
495 portdata->in_buffer[i] = buffer;
Matthew Garrett0d456192010-04-01 12:31:07 -0400496
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200497 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
498 USB_DIR_IN, port,
499 buffer, IN_BUFLEN,
500 usb_wwan_indat_callback);
501 portdata->in_urbs[i] = urb;
502 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400503
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200504 for (i = 0; i < N_OUT_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200505 if (!port->bulk_out_size)
506 break;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200507
508 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
509 if (!buffer)
510 goto bail_out_error2;
511 portdata->out_buffer[i] = buffer;
512
513 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
514 USB_DIR_OUT, port,
515 buffer, OUT_BUFLEN,
516 usb_wwan_outdat_callback);
517 portdata->out_urbs[i] = urb;
518 }
519
520 usb_set_serial_port_data(port, portdata);
521
522 if (port->interrupt_in_urb) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400523 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
524 if (err)
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700525 dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
526 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400527 }
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200528
Matthew Garrett0d456192010-04-01 12:31:07 -0400529 return 0;
530
531bail_out_error2:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200532 for (i = 0; i < N_OUT_URB; i++) {
533 usb_free_urb(portdata->out_urbs[i]);
534 kfree(portdata->out_buffer[i]);
535 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400536bail_out_error:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200537 for (i = 0; i < N_IN_URB; i++) {
538 usb_free_urb(portdata->in_urbs[i]);
539 free_page((unsigned long)portdata->in_buffer[i]);
540 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400541 kfree(portdata);
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200542
543 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400544}
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200545EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
Matthew Garrett0d456192010-04-01 12:31:07 -0400546
Bjørn Morka1028f02012-07-27 01:11:41 +0200547int usb_wwan_port_remove(struct usb_serial_port *port)
548{
549 int i;
550 struct usb_wwan_port_private *portdata;
551
552 portdata = usb_get_serial_port_data(port);
553 usb_set_serial_port_data(port, NULL);
554
555 /* Stop reading/writing urbs and free them */
556 for (i = 0; i < N_IN_URB; i++) {
557 usb_kill_urb(portdata->in_urbs[i]);
558 usb_free_urb(portdata->in_urbs[i]);
559 free_page((unsigned long)portdata->in_buffer[i]);
560 }
561 for (i = 0; i < N_OUT_URB; i++) {
562 usb_kill_urb(portdata->out_urbs[i]);
563 usb_free_urb(portdata->out_urbs[i]);
564 kfree(portdata->out_buffer[i]);
565 }
566
567 /* Now free port private data */
568 kfree(portdata);
569 return 0;
570}
571EXPORT_SYMBOL(usb_wwan_port_remove);
572
573#ifdef CONFIG_PM
Matthew Garrett0d456192010-04-01 12:31:07 -0400574static void stop_read_write_urbs(struct usb_serial *serial)
575{
576 int i, j;
577 struct usb_serial_port *port;
578 struct usb_wwan_port_private *portdata;
579
580 /* Stop reading/writing urbs */
581 for (i = 0; i < serial->num_ports; ++i) {
582 port = serial->port[i];
583 portdata = usb_get_serial_port_data(port);
Bjørn Mork032129c2012-07-27 01:11:43 +0200584 if (!portdata)
585 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400586 for (j = 0; j < N_IN_URB; j++)
587 usb_kill_urb(portdata->in_urbs[j]);
588 for (j = 0; j < N_OUT_URB; j++)
589 usb_kill_urb(portdata->out_urbs[j]);
590 }
591}
592
Matthew Garrett0d456192010-04-01 12:31:07 -0400593int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
594{
595 struct usb_wwan_intf_private *intfdata = serial->private;
596 int b;
597
Alan Stern5b1b0b82011-08-19 23:49:48 +0200598 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400599 spin_lock_irq(&intfdata->susp_lock);
600 b = intfdata->in_flight;
601 spin_unlock_irq(&intfdata->susp_lock);
602
603 if (b)
604 return -EBUSY;
605 }
606
607 spin_lock_irq(&intfdata->susp_lock);
608 intfdata->suspended = 1;
609 spin_unlock_irq(&intfdata->susp_lock);
610 stop_read_write_urbs(serial);
611
612 return 0;
613}
614EXPORT_SYMBOL(usb_wwan_suspend);
615
Oliver Neukum16871dc2011-02-10 15:33:29 +0100616static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
617{
618 int i;
619
620 for (i = 0; i < N_OUT_URB; i++) {
621 if (urb == portdata->out_urbs[i]) {
622 clear_bit(i, &portdata->out_busy);
623 break;
624 }
625 }
626}
627
Matthew Garrett0d456192010-04-01 12:31:07 -0400628static void play_delayed(struct usb_serial_port *port)
629{
630 struct usb_wwan_intf_private *data;
631 struct usb_wwan_port_private *portdata;
632 struct urb *urb;
633 int err;
634
635 portdata = usb_get_serial_port_data(port);
636 data = port->serial->private;
637 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
638 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100639 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400640 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100641 } else {
642 /* we have to throw away the rest */
643 do {
644 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100645 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100646 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
647 break;
648 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400649 }
650}
651
652int usb_wwan_resume(struct usb_serial *serial)
653{
654 int i, j;
655 struct usb_serial_port *port;
656 struct usb_wwan_intf_private *intfdata = serial->private;
657 struct usb_wwan_port_private *portdata;
658 struct urb *urb;
659 int err = 0;
660
Matthew Garrett0d456192010-04-01 12:31:07 -0400661 /* get the interrupt URBs resubmitted unconditionally */
662 for (i = 0; i < serial->num_ports; i++) {
663 port = serial->port[i];
664 if (!port->interrupt_in_urb) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700665 dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
Matthew Garrett0d456192010-04-01 12:31:07 -0400666 continue;
667 }
668 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700669 dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400670 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700671 dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
672 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400673 goto err_out;
674 }
675 }
676
677 for (i = 0; i < serial->num_ports; i++) {
678 /* walk all ports */
679 port = serial->port[i];
680 portdata = usb_get_serial_port_data(port);
681
682 /* skip closed ports */
683 spin_lock_irq(&intfdata->susp_lock);
Bjørn Mork032129c2012-07-27 01:11:43 +0200684 if (!portdata || !portdata->opened) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400685 spin_unlock_irq(&intfdata->susp_lock);
686 continue;
687 }
688
689 for (j = 0; j < N_IN_URB; j++) {
690 urb = portdata->in_urbs[j];
691 err = usb_submit_urb(urb, GFP_ATOMIC);
692 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700693 dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
694 __func__, err, i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400695 spin_unlock_irq(&intfdata->susp_lock);
696 goto err_out;
697 }
698 }
699 play_delayed(port);
700 spin_unlock_irq(&intfdata->susp_lock);
701 }
702 spin_lock_irq(&intfdata->susp_lock);
703 intfdata->suspended = 0;
704 spin_unlock_irq(&intfdata->susp_lock);
705err_out:
706 return err;
707}
708EXPORT_SYMBOL(usb_wwan_resume);
709#endif
710
711MODULE_AUTHOR(DRIVER_AUTHOR);
712MODULE_DESCRIPTION(DRIVER_DESC);
713MODULE_VERSION(DRIVER_VERSION);
714MODULE_LICENSE("GPL");