blob: 293b460030cb2c5a67addcf2d721fac43e2e257f [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{
41 struct usb_serial *serial = port->serial;
42 struct usb_wwan_port_private *portdata;
Matthew Garrett0d456192010-04-01 12:31:07 -040043 struct usb_wwan_intf_private *intfdata;
44
Matthew Garrett0d456192010-04-01 12:31:07 -040045 intfdata = port->serial->private;
46
47 if (!intfdata->send_setup)
48 return;
49
50 portdata = usb_get_serial_port_data(port);
51 mutex_lock(&serial->disc_mutex);
52 portdata->rts_state = on;
53 portdata->dtr_state = on;
54 if (serial->dev)
55 intfdata->send_setup(port);
56 mutex_unlock(&serial->disc_mutex);
57}
58EXPORT_SYMBOL(usb_wwan_dtr_rts);
59
60void usb_wwan_set_termios(struct tty_struct *tty,
61 struct usb_serial_port *port,
62 struct ktermios *old_termios)
63{
64 struct usb_wwan_intf_private *intfdata = port->serial->private;
65
Matthew Garrett0d456192010-04-01 12:31:07 -040066 /* Doesn't support option setting */
Alan Coxadc8d742012-07-14 15:31:47 +010067 tty_termios_copy_hw(&tty->termios, old_termios);
Matthew Garrett0d456192010-04-01 12:31:07 -040068
69 if (intfdata->send_setup)
70 intfdata->send_setup(port);
71}
72EXPORT_SYMBOL(usb_wwan_set_termios);
73
Alan Cox60b33c12011-02-14 16:26:14 +000074int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040075{
76 struct usb_serial_port *port = tty->driver_data;
77 unsigned int value;
78 struct usb_wwan_port_private *portdata;
79
80 portdata = usb_get_serial_port_data(port);
81
82 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
83 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
84 ((portdata->cts_state) ? TIOCM_CTS : 0) |
85 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
86 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
87 ((portdata->ri_state) ? TIOCM_RNG : 0);
88
89 return value;
90}
91EXPORT_SYMBOL(usb_wwan_tiocmget);
92
Alan Cox20b9d172011-02-14 16:26:50 +000093int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -040094 unsigned int set, unsigned int clear)
95{
96 struct usb_serial_port *port = tty->driver_data;
97 struct usb_wwan_port_private *portdata;
98 struct usb_wwan_intf_private *intfdata;
99
100 portdata = usb_get_serial_port_data(port);
101 intfdata = port->serial->private;
102
103 if (!intfdata->send_setup)
104 return -EINVAL;
105
106 /* FIXME: what locks portdata fields ? */
107 if (set & TIOCM_RTS)
108 portdata->rts_state = 1;
109 if (set & TIOCM_DTR)
110 portdata->dtr_state = 1;
111
112 if (clear & TIOCM_RTS)
113 portdata->rts_state = 0;
114 if (clear & TIOCM_DTR)
115 portdata->dtr_state = 0;
116 return intfdata->send_setup(port);
117}
118EXPORT_SYMBOL(usb_wwan_tiocmset);
119
Dan Williams02303f72010-11-19 16:04:00 -0600120static int get_serial_info(struct usb_serial_port *port,
121 struct serial_struct __user *retinfo)
122{
123 struct serial_struct tmp;
124
125 if (!retinfo)
126 return -EFAULT;
127
128 memset(&tmp, 0, sizeof(tmp));
129 tmp.line = port->serial->minor;
130 tmp.port = port->number;
131 tmp.baud_base = tty_get_baud_rate(port->port.tty);
132 tmp.close_delay = port->port.close_delay / 10;
133 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
134 ASYNC_CLOSING_WAIT_NONE :
135 port->port.closing_wait / 10;
136
137 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
138 return -EFAULT;
139 return 0;
140}
141
142static int set_serial_info(struct usb_serial_port *port,
143 struct serial_struct __user *newinfo)
144{
145 struct serial_struct new_serial;
146 unsigned int closing_wait, close_delay;
147 int retval = 0;
148
149 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
150 return -EFAULT;
151
152 close_delay = new_serial.close_delay * 10;
153 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
154 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
155
156 mutex_lock(&port->port.mutex);
157
158 if (!capable(CAP_SYS_ADMIN)) {
159 if ((close_delay != port->port.close_delay) ||
160 (closing_wait != port->port.closing_wait))
161 retval = -EPERM;
162 else
163 retval = -EOPNOTSUPP;
164 } else {
165 port->port.close_delay = close_delay;
166 port->port.closing_wait = closing_wait;
167 }
168
169 mutex_unlock(&port->port.mutex);
170 return retval;
171}
172
Alan Cox00a0d0d2011-02-14 16:27:06 +0000173int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600174 unsigned int cmd, unsigned long arg)
175{
176 struct usb_serial_port *port = tty->driver_data;
177
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700178 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Dan Williams02303f72010-11-19 16:04:00 -0600179
180 switch (cmd) {
181 case TIOCGSERIAL:
182 return get_serial_info(port,
183 (struct serial_struct __user *) arg);
184 case TIOCSSERIAL:
185 return set_serial_info(port,
186 (struct serial_struct __user *) arg);
187 default:
188 break;
189 }
190
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700191 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Dan Williams02303f72010-11-19 16:04:00 -0600192
193 return -ENOIOCTLCMD;
194}
195EXPORT_SYMBOL(usb_wwan_ioctl);
196
Matthew Garrett0d456192010-04-01 12:31:07 -0400197/* Write */
198int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
199 const unsigned char *buf, int count)
200{
201 struct usb_wwan_port_private *portdata;
202 struct usb_wwan_intf_private *intfdata;
203 int i;
204 int left, todo;
205 struct urb *this_urb = NULL; /* spurious */
206 int err;
207 unsigned long flags;
208
209 portdata = usb_get_serial_port_data(port);
210 intfdata = port->serial->private;
211
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700212 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400213
214 i = 0;
215 left = count;
216 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
217 todo = left;
218 if (todo > OUT_BUFLEN)
219 todo = OUT_BUFLEN;
220
221 this_urb = portdata->out_urbs[i];
222 if (test_and_set_bit(i, &portdata->out_busy)) {
223 if (time_before(jiffies,
224 portdata->tx_start_time[i] + 10 * HZ))
225 continue;
226 usb_unlink_urb(this_urb);
227 continue;
228 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700229 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
230 usb_pipeendpoint(this_urb->pipe), i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400231
232 err = usb_autopm_get_interface_async(port->serial->interface);
233 if (err < 0)
234 break;
235
236 /* send the data */
237 memcpy(this_urb->transfer_buffer, buf, todo);
238 this_urb->transfer_buffer_length = todo;
239
240 spin_lock_irqsave(&intfdata->susp_lock, flags);
241 if (intfdata->suspended) {
242 usb_anchor_urb(this_urb, &portdata->delayed);
243 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
244 } else {
245 intfdata->in_flight++;
246 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
247 err = usb_submit_urb(this_urb, GFP_ATOMIC);
248 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700249 dev_dbg(&port->dev,
250 "usb_submit_urb %p (write bulk) failed (%d)\n",
251 this_urb, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400252 clear_bit(i, &portdata->out_busy);
253 spin_lock_irqsave(&intfdata->susp_lock, flags);
254 intfdata->in_flight--;
255 spin_unlock_irqrestore(&intfdata->susp_lock,
256 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100257 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100258 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400259 }
260 }
261
262 portdata->tx_start_time[i] = jiffies;
263 buf += todo;
264 left -= todo;
265 }
266
267 count -= left;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700268 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400269 return count;
270}
271EXPORT_SYMBOL(usb_wwan_write);
272
273static void usb_wwan_indat_callback(struct urb *urb)
274{
275 int err;
276 int endpoint;
277 struct usb_serial_port *port;
278 struct tty_struct *tty;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700279 struct device *dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400280 unsigned char *data = urb->transfer_buffer;
281 int status = urb->status;
282
Matthew Garrett0d456192010-04-01 12:31:07 -0400283 endpoint = usb_pipeendpoint(urb->pipe);
284 port = urb->context;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700285 dev = &port->dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400286
287 if (status) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700288 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
289 __func__, status, endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400290 } else {
291 tty = tty_port_tty_get(&port->port);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100292 if (tty) {
293 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100294 tty_insert_flip_string(&port->port, data,
Jiri Slaby38237fd2011-02-15 15:55:07 +0100295 urb->actual_length);
296 tty_flip_buffer_push(tty);
297 } else
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700298 dev_dbg(dev, "%s: empty read urb received\n", __func__);
Jiri Slaby38237fd2011-02-15 15:55:07 +0100299 tty_kref_put(tty);
300 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400301
302 /* Resubmit urb so we continue receiving */
Dan Carpenterec428992012-04-20 09:33:31 +0300303 err = usb_submit_urb(urb, GFP_ATOMIC);
304 if (err) {
305 if (err != -EPERM) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700306 dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err);
Dan Carpenterec428992012-04-20 09:33:31 +0300307 /* busy also in error unless we are killed */
Matthew Garrett0d456192010-04-01 12:31:07 -0400308 usb_mark_last_busy(port->serial->dev);
Oliver Neukumc9c45582011-02-10 15:33:10 +0100309 }
Dan Carpenterec428992012-04-20 09:33:31 +0300310 } else {
311 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400312 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400313 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400314}
315
316static void usb_wwan_outdat_callback(struct urb *urb)
317{
318 struct usb_serial_port *port;
319 struct usb_wwan_port_private *portdata;
320 struct usb_wwan_intf_private *intfdata;
321 int i;
322
Matthew Garrett0d456192010-04-01 12:31:07 -0400323 port = urb->context;
324 intfdata = port->serial->private;
325
326 usb_serial_port_softint(port);
327 usb_autopm_put_interface_async(port->serial->interface);
328 portdata = usb_get_serial_port_data(port);
329 spin_lock(&intfdata->susp_lock);
330 intfdata->in_flight--;
331 spin_unlock(&intfdata->susp_lock);
332
333 for (i = 0; i < N_OUT_URB; ++i) {
334 if (portdata->out_urbs[i] == urb) {
335 smp_mb__before_clear_bit();
336 clear_bit(i, &portdata->out_busy);
337 break;
338 }
339 }
340}
341
342int usb_wwan_write_room(struct tty_struct *tty)
343{
344 struct usb_serial_port *port = tty->driver_data;
345 struct usb_wwan_port_private *portdata;
346 int i;
347 int data_len = 0;
348 struct urb *this_urb;
349
350 portdata = usb_get_serial_port_data(port);
351
352 for (i = 0; i < N_OUT_URB; i++) {
353 this_urb = portdata->out_urbs[i];
354 if (this_urb && !test_bit(i, &portdata->out_busy))
355 data_len += OUT_BUFLEN;
356 }
357
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700358 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400359 return data_len;
360}
361EXPORT_SYMBOL(usb_wwan_write_room);
362
363int usb_wwan_chars_in_buffer(struct tty_struct *tty)
364{
365 struct usb_serial_port *port = tty->driver_data;
366 struct usb_wwan_port_private *portdata;
367 int i;
368 int data_len = 0;
369 struct urb *this_urb;
370
371 portdata = usb_get_serial_port_data(port);
372
373 for (i = 0; i < N_OUT_URB; i++) {
374 this_urb = portdata->out_urbs[i];
375 /* FIXME: This locking is insufficient as this_urb may
376 go unused during the test */
377 if (this_urb && test_bit(i, &portdata->out_busy))
378 data_len += this_urb->transfer_buffer_length;
379 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700380 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400381 return data_len;
382}
383EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
384
385int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
386{
387 struct usb_wwan_port_private *portdata;
388 struct usb_wwan_intf_private *intfdata;
389 struct usb_serial *serial = port->serial;
390 int i, err;
391 struct urb *urb;
392
393 portdata = usb_get_serial_port_data(port);
394 intfdata = serial->private;
395
Matthew Garrett0d456192010-04-01 12:31:07 -0400396 /* Start reading from the IN endpoint */
397 for (i = 0; i < N_IN_URB; i++) {
398 urb = portdata->in_urbs[i];
399 if (!urb)
400 continue;
401 err = usb_submit_urb(urb, GFP_KERNEL);
402 if (err) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700403 dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
404 __func__, i, err, urb->transfer_buffer_length);
Matthew Garrett0d456192010-04-01 12:31:07 -0400405 }
406 }
407
408 if (intfdata->send_setup)
409 intfdata->send_setup(port);
410
411 serial->interface->needs_remote_wakeup = 1;
412 spin_lock_irq(&intfdata->susp_lock);
413 portdata->opened = 1;
414 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100415 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400416 usb_autopm_put_interface(serial->interface);
417
418 return 0;
419}
420EXPORT_SYMBOL(usb_wwan_open);
421
422void usb_wwan_close(struct usb_serial_port *port)
423{
424 int i;
425 struct usb_serial *serial = port->serial;
426 struct usb_wwan_port_private *portdata;
427 struct usb_wwan_intf_private *intfdata = port->serial->private;
428
Matthew Garrett0d456192010-04-01 12:31:07 -0400429 portdata = usb_get_serial_port_data(port);
430
431 if (serial->dev) {
432 /* Stop reading/writing urbs */
433 spin_lock_irq(&intfdata->susp_lock);
434 portdata->opened = 0;
435 spin_unlock_irq(&intfdata->susp_lock);
436
437 for (i = 0; i < N_IN_URB; i++)
438 usb_kill_urb(portdata->in_urbs[i]);
439 for (i = 0; i < N_OUT_URB; i++)
440 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100441 /* balancing - important as an error cannot be handled*/
442 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400443 serial->interface->needs_remote_wakeup = 0;
444 }
445}
446EXPORT_SYMBOL(usb_wwan_close);
447
448/* Helper functions used by usb_wwan_setup_urbs */
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200449static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
450 int endpoint,
Matthew Garrett0d456192010-04-01 12:31:07 -0400451 int dir, void *ctx, char *buf, int len,
452 void (*callback) (struct urb *))
453{
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200454 struct usb_serial *serial = port->serial;
Matthew Garrett0d456192010-04-01 12:31:07 -0400455 struct urb *urb;
456
Matthew Garrett0d456192010-04-01 12:31:07 -0400457 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
458 if (urb == NULL) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700459 dev_dbg(&serial->interface->dev,
460 "%s: alloc for endpoint %d failed.\n", __func__,
461 endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400462 return NULL;
463 }
464
465 /* Fill URB using supplied data. */
466 usb_fill_bulk_urb(urb, serial->dev,
467 usb_sndbulkpipe(serial->dev, endpoint) | dir,
468 buf, len, callback, ctx);
469
470 return urb;
471}
472
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200473int usb_wwan_port_probe(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400474{
Matthew Garrett0d456192010-04-01 12:31:07 -0400475 struct usb_wwan_port_private *portdata;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200476 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400477 u8 *buffer;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200478 int err;
479 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400480
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200481 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
482 if (!portdata)
483 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400484
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200485 init_usb_anchor(&portdata->delayed);
Matthew Garrett0d456192010-04-01 12:31:07 -0400486
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200487 for (i = 0; i < N_IN_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200488 if (!port->bulk_in_size)
489 break;
490
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200491 buffer = (u8 *)__get_free_page(GFP_KERNEL);
492 if (!buffer)
493 goto bail_out_error;
494 portdata->in_buffer[i] = buffer;
Matthew Garrett0d456192010-04-01 12:31:07 -0400495
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200496 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
497 USB_DIR_IN, port,
498 buffer, IN_BUFLEN,
499 usb_wwan_indat_callback);
500 portdata->in_urbs[i] = urb;
501 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400502
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200503 for (i = 0; i < N_OUT_URB; i++) {
Johan Hovold8e493ca12012-10-26 18:44:20 +0200504 if (!port->bulk_out_size)
505 break;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200506
507 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
508 if (!buffer)
509 goto bail_out_error2;
510 portdata->out_buffer[i] = buffer;
511
512 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
513 USB_DIR_OUT, port,
514 buffer, OUT_BUFLEN,
515 usb_wwan_outdat_callback);
516 portdata->out_urbs[i] = urb;
517 }
518
519 usb_set_serial_port_data(port, portdata);
520
521 if (port->interrupt_in_urb) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400522 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
523 if (err)
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700524 dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
525 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400526 }
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200527
Matthew Garrett0d456192010-04-01 12:31:07 -0400528 return 0;
529
530bail_out_error2:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200531 for (i = 0; i < N_OUT_URB; i++) {
532 usb_free_urb(portdata->out_urbs[i]);
533 kfree(portdata->out_buffer[i]);
534 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400535bail_out_error:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200536 for (i = 0; i < N_IN_URB; i++) {
537 usb_free_urb(portdata->in_urbs[i]);
538 free_page((unsigned long)portdata->in_buffer[i]);
539 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400540 kfree(portdata);
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200541
542 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400543}
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200544EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
Matthew Garrett0d456192010-04-01 12:31:07 -0400545
Bjørn Morka1028f02012-07-27 01:11:41 +0200546int usb_wwan_port_remove(struct usb_serial_port *port)
547{
548 int i;
549 struct usb_wwan_port_private *portdata;
550
551 portdata = usb_get_serial_port_data(port);
552 usb_set_serial_port_data(port, NULL);
553
554 /* Stop reading/writing urbs and free them */
555 for (i = 0; i < N_IN_URB; i++) {
556 usb_kill_urb(portdata->in_urbs[i]);
557 usb_free_urb(portdata->in_urbs[i]);
558 free_page((unsigned long)portdata->in_buffer[i]);
559 }
560 for (i = 0; i < N_OUT_URB; i++) {
561 usb_kill_urb(portdata->out_urbs[i]);
562 usb_free_urb(portdata->out_urbs[i]);
563 kfree(portdata->out_buffer[i]);
564 }
565
566 /* Now free port private data */
567 kfree(portdata);
568 return 0;
569}
570EXPORT_SYMBOL(usb_wwan_port_remove);
571
572#ifdef CONFIG_PM
Matthew Garrett0d456192010-04-01 12:31:07 -0400573static void stop_read_write_urbs(struct usb_serial *serial)
574{
575 int i, j;
576 struct usb_serial_port *port;
577 struct usb_wwan_port_private *portdata;
578
579 /* Stop reading/writing urbs */
580 for (i = 0; i < serial->num_ports; ++i) {
581 port = serial->port[i];
582 portdata = usb_get_serial_port_data(port);
Bjørn Mork032129c2012-07-27 01:11:43 +0200583 if (!portdata)
584 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400585 for (j = 0; j < N_IN_URB; j++)
586 usb_kill_urb(portdata->in_urbs[j]);
587 for (j = 0; j < N_OUT_URB; j++)
588 usb_kill_urb(portdata->out_urbs[j]);
589 }
590}
591
Matthew Garrett0d456192010-04-01 12:31:07 -0400592int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
593{
594 struct usb_wwan_intf_private *intfdata = serial->private;
595 int b;
596
Alan Stern5b1b0b82011-08-19 23:49:48 +0200597 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400598 spin_lock_irq(&intfdata->susp_lock);
599 b = intfdata->in_flight;
600 spin_unlock_irq(&intfdata->susp_lock);
601
602 if (b)
603 return -EBUSY;
604 }
605
606 spin_lock_irq(&intfdata->susp_lock);
607 intfdata->suspended = 1;
608 spin_unlock_irq(&intfdata->susp_lock);
609 stop_read_write_urbs(serial);
610
611 return 0;
612}
613EXPORT_SYMBOL(usb_wwan_suspend);
614
Oliver Neukum16871dc2011-02-10 15:33:29 +0100615static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
616{
617 int i;
618
619 for (i = 0; i < N_OUT_URB; i++) {
620 if (urb == portdata->out_urbs[i]) {
621 clear_bit(i, &portdata->out_busy);
622 break;
623 }
624 }
625}
626
Matthew Garrett0d456192010-04-01 12:31:07 -0400627static void play_delayed(struct usb_serial_port *port)
628{
629 struct usb_wwan_intf_private *data;
630 struct usb_wwan_port_private *portdata;
631 struct urb *urb;
632 int err;
633
634 portdata = usb_get_serial_port_data(port);
635 data = port->serial->private;
636 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
637 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100638 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400639 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100640 } else {
641 /* we have to throw away the rest */
642 do {
643 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100644 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100645 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
646 break;
647 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400648 }
649}
650
651int usb_wwan_resume(struct usb_serial *serial)
652{
653 int i, j;
654 struct usb_serial_port *port;
655 struct usb_wwan_intf_private *intfdata = serial->private;
656 struct usb_wwan_port_private *portdata;
657 struct urb *urb;
658 int err = 0;
659
Matthew Garrett0d456192010-04-01 12:31:07 -0400660 /* get the interrupt URBs resubmitted unconditionally */
661 for (i = 0; i < serial->num_ports; i++) {
662 port = serial->port[i];
663 if (!port->interrupt_in_urb) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700664 dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
Matthew Garrett0d456192010-04-01 12:31:07 -0400665 continue;
666 }
667 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700668 dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400669 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700670 dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
671 __func__, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400672 goto err_out;
673 }
674 }
675
676 for (i = 0; i < serial->num_ports; i++) {
677 /* walk all ports */
678 port = serial->port[i];
679 portdata = usb_get_serial_port_data(port);
680
681 /* skip closed ports */
682 spin_lock_irq(&intfdata->susp_lock);
Bjørn Mork032129c2012-07-27 01:11:43 +0200683 if (!portdata || !portdata->opened) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400684 spin_unlock_irq(&intfdata->susp_lock);
685 continue;
686 }
687
688 for (j = 0; j < N_IN_URB; j++) {
689 urb = portdata->in_urbs[j];
690 err = usb_submit_urb(urb, GFP_ATOMIC);
691 if (err < 0) {
Greg Kroah-Hartman3a1c2a82012-04-20 16:54:01 -0700692 dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
693 __func__, err, i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400694 spin_unlock_irq(&intfdata->susp_lock);
695 goto err_out;
696 }
697 }
698 play_delayed(port);
699 spin_unlock_irq(&intfdata->susp_lock);
700 }
701 spin_lock_irq(&intfdata->susp_lock);
702 intfdata->suspended = 0;
703 spin_unlock_irq(&intfdata->susp_lock);
704err_out:
705 return err;
706}
707EXPORT_SYMBOL(usb_wwan_resume);
708#endif
709
710MODULE_AUTHOR(DRIVER_AUTHOR);
711MODULE_DESCRIPTION(DRIVER_DESC);
Matthew Garrett0d456192010-04-01 12:31:07 -0400712MODULE_LICENSE("GPL");