blob: ebe8c6e24919f71da61b1362142b5edc6132c288 [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
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700282static void usb_wwan_in_work(struct work_struct *w)
283{
284 struct usb_wwan_port_private *portdata =
285 container_of(w, struct usb_wwan_port_private, in_work);
Jack Pham74715d92012-08-28 18:26:54 -0700286 struct usb_wwan_intf_private *intfdata;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700287 struct list_head *q = &portdata->in_urb_list;
288 struct urb *urb;
289 unsigned char *data;
290 struct tty_struct *tty;
291 struct usb_serial_port *port;
292 int err;
293 ssize_t len;
294 ssize_t count;
295 unsigned long flags;
296
297 spin_lock_irqsave(&portdata->in_lock, flags);
298 while (!list_empty(q)) {
299 urb = list_first_entry(q, struct urb, urb_list);
300 port = urb->context;
301 if (port->throttle_req || port->throttled)
302 break;
303
304 tty = tty_port_tty_get(&port->port);
305 if (!tty)
Hemant Kumar93867392012-07-09 12:23:06 -0700306 break;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700307
Jack Pham74715d92012-08-28 18:26:54 -0700308 /* list_empty() will still be false after this; it means
309 * URB is still being processed */
310 list_del(&urb->urb_list);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700311
312 spin_unlock_irqrestore(&portdata->in_lock, flags);
313
314 len = urb->actual_length - portdata->n_read;
315 data = urb->transfer_buffer + portdata->n_read;
316 count = tty_insert_flip_string(tty, data, len);
317 tty_flip_buffer_push(tty);
318 tty_kref_put(tty);
319
320 if (count < len) {
321 dbg("%s: len:%d count:%d n_read:%d\n", __func__,
322 len, count, portdata->n_read);
323 portdata->n_read += count;
324 port->throttled = true;
325
326 /* add request back to list */
327 spin_lock_irqsave(&portdata->in_lock, flags);
328 list_add(&urb->urb_list, q);
329 spin_unlock_irqrestore(&portdata->in_lock, flags);
330 return;
331 }
Jack Pham74715d92012-08-28 18:26:54 -0700332
333 /* re-init list pointer to indicate we are done with it */
334 INIT_LIST_HEAD(&urb->urb_list);
335
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700336 portdata->n_read = 0;
Jack Pham74715d92012-08-28 18:26:54 -0700337 intfdata = port->serial->private;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700338
Jack Pham74715d92012-08-28 18:26:54 -0700339 spin_lock_irqsave(&intfdata->susp_lock, flags);
340 if (!intfdata->suspended && !urb->anchor) {
341 usb_anchor_urb(urb, &portdata->submitted);
342 err = usb_submit_urb(urb, GFP_ATOMIC);
343 if (err) {
344 usb_unanchor_urb(urb);
345 if (err != -EPERM)
346 pr_err("%s: submit read urb failed:%d",
347 __func__, err);
348 }
349
350 usb_mark_last_busy(port->serial->dev);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700351 }
Jack Pham74715d92012-08-28 18:26:54 -0700352 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700353 spin_lock_irqsave(&portdata->in_lock, flags);
354 }
355 spin_unlock_irqrestore(&portdata->in_lock, flags);
356}
357
Matthew Garrett0d456192010-04-01 12:31:07 -0400358static void usb_wwan_indat_callback(struct urb *urb)
359{
360 int err;
361 int endpoint;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700362 struct usb_wwan_port_private *portdata;
Jack Pham74715d92012-08-28 18:26:54 -0700363 struct usb_wwan_intf_private *intfdata;
Matthew Garrett0d456192010-04-01 12:31:07 -0400364 struct usb_serial_port *port;
Matthew Garrett0d456192010-04-01 12:31:07 -0400365 int status = urb->status;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700366 unsigned long flags;
Matthew Garrett0d456192010-04-01 12:31:07 -0400367
368 dbg("%s: %p", __func__, urb);
369
370 endpoint = usb_pipeendpoint(urb->pipe);
371 port = urb->context;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700372 portdata = usb_get_serial_port_data(port);
Jack Pham74715d92012-08-28 18:26:54 -0700373 intfdata = port->serial->private;
Matthew Garrett0d456192010-04-01 12:31:07 -0400374
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700375 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400376
Hemant Kumare8e40df2012-07-25 15:24:45 -0700377 if ((status == -ENOENT || !status) && urb->actual_length) {
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700378 spin_lock_irqsave(&portdata->in_lock, flags);
379 list_add_tail(&urb->urb_list, &portdata->in_urb_list);
380 spin_unlock_irqrestore(&portdata->in_lock, flags);
Matthew Garrett0d456192010-04-01 12:31:07 -0400381
Pavankumar Kondeti52de63a2013-03-04 18:31:57 +0530382 queue_work(system_nrt_wq, &portdata->in_work);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700383
384 return;
385 }
386
387 dbg("%s: nonzero status: %d on endpoint %02x.",
388 __func__, status, endpoint);
389
Jack Pham74715d92012-08-28 18:26:54 -0700390 spin_lock(&intfdata->susp_lock);
391 if (intfdata->suspended || !portdata->opened) {
392 spin_unlock(&intfdata->susp_lock);
393 return;
394 }
395 spin_unlock(&intfdata->susp_lock);
396
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700397 if (status != -ESHUTDOWN) {
398 usb_anchor_urb(urb, &portdata->submitted);
399 err = usb_submit_urb(urb, GFP_ATOMIC);
400 if (err) {
401 usb_unanchor_urb(urb);
402 if (err != -EPERM)
403 pr_err("%s: submit read urb failed:%d",
404 __func__, err);
405 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400406 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400407}
408
409static void usb_wwan_outdat_callback(struct urb *urb)
410{
411 struct usb_serial_port *port;
412 struct usb_wwan_port_private *portdata;
413 struct usb_wwan_intf_private *intfdata;
414 int i;
415
416 dbg("%s", __func__);
417
418 port = urb->context;
419 intfdata = port->serial->private;
420
421 usb_serial_port_softint(port);
422 usb_autopm_put_interface_async(port->serial->interface);
423 portdata = usb_get_serial_port_data(port);
424 spin_lock(&intfdata->susp_lock);
425 intfdata->in_flight--;
426 spin_unlock(&intfdata->susp_lock);
427
428 for (i = 0; i < N_OUT_URB; ++i) {
429 if (portdata->out_urbs[i] == urb) {
430 smp_mb__before_clear_bit();
431 clear_bit(i, &portdata->out_busy);
432 break;
433 }
434 }
435}
436
437int usb_wwan_write_room(struct tty_struct *tty)
438{
439 struct usb_serial_port *port = tty->driver_data;
440 struct usb_wwan_port_private *portdata;
441 int i;
442 int data_len = 0;
443 struct urb *this_urb;
444
445 portdata = usb_get_serial_port_data(port);
446
447 for (i = 0; i < N_OUT_URB; i++) {
448 this_urb = portdata->out_urbs[i];
449 if (this_urb && !test_bit(i, &portdata->out_busy))
450 data_len += OUT_BUFLEN;
451 }
452
453 dbg("%s: %d", __func__, data_len);
454 return data_len;
455}
456EXPORT_SYMBOL(usb_wwan_write_room);
457
458int usb_wwan_chars_in_buffer(struct tty_struct *tty)
459{
460 struct usb_serial_port *port = tty->driver_data;
461 struct usb_wwan_port_private *portdata;
462 int i;
463 int data_len = 0;
464 struct urb *this_urb;
465
466 portdata = usb_get_serial_port_data(port);
467
468 for (i = 0; i < N_OUT_URB; i++) {
469 this_urb = portdata->out_urbs[i];
470 /* FIXME: This locking is insufficient as this_urb may
471 go unused during the test */
472 if (this_urb && test_bit(i, &portdata->out_busy))
473 data_len += this_urb->transfer_buffer_length;
474 }
475 dbg("%s: %d", __func__, data_len);
476 return data_len;
477}
478EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
479
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700480void usb_wwan_throttle(struct tty_struct *tty)
481{
482 struct usb_serial_port *port = tty->driver_data;
483
484 port->throttle_req = true;
485
486 dbg("%s:\n", __func__);
487}
488EXPORT_SYMBOL(usb_wwan_throttle);
489
490void usb_wwan_unthrottle(struct tty_struct *tty)
491{
492 struct usb_serial_port *port = tty->driver_data;
493 struct usb_wwan_port_private *portdata;
494
495 portdata = usb_get_serial_port_data(port);
496
497 dbg("%s:\n", __func__);
498 port->throttle_req = false;
499 port->throttled = false;
500
Pavankumar Kondeti52de63a2013-03-04 18:31:57 +0530501 queue_work(system_nrt_wq, &portdata->in_work);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700502}
503EXPORT_SYMBOL(usb_wwan_unthrottle);
504
Matthew Garrett0d456192010-04-01 12:31:07 -0400505int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
506{
507 struct usb_wwan_port_private *portdata;
508 struct usb_wwan_intf_private *intfdata;
509 struct usb_serial *serial = port->serial;
510 int i, err;
511 struct urb *urb;
512
513 portdata = usb_get_serial_port_data(port);
514 intfdata = serial->private;
515
Vamsi Krishna68194482011-12-12 18:28:48 -0800516 /* explicitly set the driver mode to raw */
Vamsi Krishnac502ecc2012-01-03 15:44:00 -0800517 tty->raw = 1;
518 tty->real_raw = 1;
Vamsi Krishna68194482011-12-12 18:28:48 -0800519
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800520 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
Matthew Garrett0d456192010-04-01 12:31:07 -0400521 dbg("%s", __func__);
522
523 /* Start reading from the IN endpoint */
524 for (i = 0; i < N_IN_URB; i++) {
525 urb = portdata->in_urbs[i];
526 if (!urb)
527 continue;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700528 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400529 err = usb_submit_urb(urb, GFP_KERNEL);
530 if (err) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700531 usb_unanchor_urb(urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400532 dbg("%s: submit urb %d failed (%d) %d",
533 __func__, i, err, urb->transfer_buffer_length);
534 }
535 }
536
537 if (intfdata->send_setup)
538 intfdata->send_setup(port);
539
540 serial->interface->needs_remote_wakeup = 1;
541 spin_lock_irq(&intfdata->susp_lock);
542 portdata->opened = 1;
543 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100544 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400545 usb_autopm_put_interface(serial->interface);
546
547 return 0;
548}
549EXPORT_SYMBOL(usb_wwan_open);
550
551void usb_wwan_close(struct usb_serial_port *port)
552{
553 int i;
554 struct usb_serial *serial = port->serial;
555 struct usb_wwan_port_private *portdata;
556 struct usb_wwan_intf_private *intfdata = port->serial->private;
557
558 dbg("%s", __func__);
559 portdata = usb_get_serial_port_data(port);
560
561 if (serial->dev) {
562 /* Stop reading/writing urbs */
563 spin_lock_irq(&intfdata->susp_lock);
564 portdata->opened = 0;
565 spin_unlock_irq(&intfdata->susp_lock);
566
567 for (i = 0; i < N_IN_URB; i++)
568 usb_kill_urb(portdata->in_urbs[i]);
569 for (i = 0; i < N_OUT_URB; i++)
570 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100571 /* balancing - important as an error cannot be handled*/
572 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400573 serial->interface->needs_remote_wakeup = 0;
574 }
575}
576EXPORT_SYMBOL(usb_wwan_close);
577
578/* Helper functions used by usb_wwan_setup_urbs */
579static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
580 int dir, void *ctx, char *buf, int len,
581 void (*callback) (struct urb *))
582{
583 struct urb *urb;
584
585 if (endpoint == -1)
586 return NULL; /* endpoint not needed */
587
588 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
589 if (urb == NULL) {
590 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
591 return NULL;
592 }
593
594 /* Fill URB using supplied data. */
595 usb_fill_bulk_urb(urb, serial->dev,
596 usb_sndbulkpipe(serial->dev, endpoint) | dir,
597 buf, len, callback, ctx);
598
599 return urb;
600}
601
602/* Setup urbs */
603static void usb_wwan_setup_urbs(struct usb_serial *serial)
604{
605 int i, j;
606 struct usb_serial_port *port;
607 struct usb_wwan_port_private *portdata;
608
609 dbg("%s", __func__);
610
611 for (i = 0; i < serial->num_ports; i++) {
612 port = serial->port[i];
613 portdata = usb_get_serial_port_data(port);
614
615 /* Do indat endpoints first */
616 for (j = 0; j < N_IN_URB; ++j) {
617 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
618 port->
619 bulk_in_endpointAddress,
620 USB_DIR_IN,
621 port,
622 portdata->
623 in_buffer[j],
624 IN_BUFLEN,
625 usb_wwan_indat_callback);
626 }
627
628 /* outdat endpoints */
629 for (j = 0; j < N_OUT_URB; ++j) {
630 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
631 port->
632 bulk_out_endpointAddress,
633 USB_DIR_OUT,
634 port,
635 portdata->
636 out_buffer
637 [j],
638 OUT_BUFLEN,
639 usb_wwan_outdat_callback);
640 }
641 }
642}
643
644int usb_wwan_startup(struct usb_serial *serial)
645{
646 int i, j, err;
647 struct usb_serial_port *port;
648 struct usb_wwan_port_private *portdata;
649 u8 *buffer;
650
651 dbg("%s", __func__);
652
653 /* Now setup per port private data */
654 for (i = 0; i < serial->num_ports; i++) {
655 port = serial->port[i];
656 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
657 if (!portdata) {
658 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
659 __func__, i);
660 return 1;
661 }
662 init_usb_anchor(&portdata->delayed);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700663 init_usb_anchor(&portdata->submitted);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700664 INIT_WORK(&portdata->in_work, usb_wwan_in_work);
665 INIT_LIST_HEAD(&portdata->in_urb_list);
666 spin_lock_init(&portdata->in_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400667
668 for (j = 0; j < N_IN_URB; j++) {
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800669 buffer = kmalloc(IN_BUFLEN, GFP_KERNEL);
Matthew Garrett0d456192010-04-01 12:31:07 -0400670 if (!buffer)
671 goto bail_out_error;
672 portdata->in_buffer[j] = buffer;
673 }
674
675 for (j = 0; j < N_OUT_URB; j++) {
676 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
677 if (!buffer)
678 goto bail_out_error2;
679 portdata->out_buffer[j] = buffer;
680 }
681
682 usb_set_serial_port_data(port, portdata);
683
684 if (!port->interrupt_in_urb)
685 continue;
686 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
687 if (err)
688 dbg("%s: submit irq_in urb failed %d", __func__, err);
689 }
690 usb_wwan_setup_urbs(serial);
691 return 0;
692
693bail_out_error2:
694 for (j = 0; j < N_OUT_URB; j++)
695 kfree(portdata->out_buffer[j]);
696bail_out_error:
697 for (j = 0; j < N_IN_URB; j++)
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800698 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400699 kfree(portdata);
700 return 1;
701}
702EXPORT_SYMBOL(usb_wwan_startup);
703
704static void stop_read_write_urbs(struct usb_serial *serial)
705{
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700706 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400707 struct usb_serial_port *port;
708 struct usb_wwan_port_private *portdata;
709
710 /* Stop reading/writing urbs */
711 for (i = 0; i < serial->num_ports; ++i) {
712 port = serial->port[i];
713 portdata = usb_get_serial_port_data(port);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700714 usb_kill_anchored_urbs(&portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400715 }
716}
717
718void usb_wwan_disconnect(struct usb_serial *serial)
719{
720 dbg("%s", __func__);
721
722 stop_read_write_urbs(serial);
723}
724EXPORT_SYMBOL(usb_wwan_disconnect);
725
726void usb_wwan_release(struct usb_serial *serial)
727{
728 int i, j;
729 struct usb_serial_port *port;
730 struct usb_wwan_port_private *portdata;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700731 struct urb *urb;
732 struct list_head *q;
733 unsigned long flags;
Matthew Garrett0d456192010-04-01 12:31:07 -0400734
735 /* Now free them */
736 for (i = 0; i < serial->num_ports; ++i) {
737 port = serial->port[i];
738 portdata = usb_get_serial_port_data(port);
739
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700740 cancel_work_sync(&portdata->in_work);
741 /* TBD: do we really need this */
742 spin_lock_irqsave(&portdata->in_lock, flags);
743 q = &portdata->in_urb_list;
744 while (!list_empty(q)) {
745 urb = list_first_entry(q, struct urb, urb_list);
746 list_del_init(&urb->urb_list);
747 }
748 spin_unlock_irqrestore(&portdata->in_lock, flags);
749
Matthew Garrett0d456192010-04-01 12:31:07 -0400750 for (j = 0; j < N_IN_URB; j++) {
751 usb_free_urb(portdata->in_urbs[j]);
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800752 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400753 portdata->in_urbs[j] = NULL;
754 }
755 for (j = 0; j < N_OUT_URB; j++) {
756 usb_free_urb(portdata->out_urbs[j]);
757 kfree(portdata->out_buffer[j]);
758 portdata->out_urbs[j] = NULL;
759 }
760 }
761
762 /* Now free per port private data */
763 for (i = 0; i < serial->num_ports; i++) {
764 port = serial->port[i];
765 kfree(usb_get_serial_port_data(port));
766 }
767}
768EXPORT_SYMBOL(usb_wwan_release);
769
770#ifdef CONFIG_PM
771int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
772{
773 struct usb_wwan_intf_private *intfdata = serial->private;
774 int b;
775
776 dbg("%s entered", __func__);
777
Alan Stern5b1b0b82011-08-19 23:49:48 +0200778 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400779 spin_lock_irq(&intfdata->susp_lock);
780 b = intfdata->in_flight;
781 spin_unlock_irq(&intfdata->susp_lock);
782
Hemant Kumarf2285db2012-07-23 20:02:51 -0700783 if (b || pm_runtime_autosuspend_expiration(&serial->dev->dev))
Matthew Garrett0d456192010-04-01 12:31:07 -0400784 return -EBUSY;
785 }
786
787 spin_lock_irq(&intfdata->susp_lock);
788 intfdata->suspended = 1;
789 spin_unlock_irq(&intfdata->susp_lock);
790 stop_read_write_urbs(serial);
791
792 return 0;
793}
794EXPORT_SYMBOL(usb_wwan_suspend);
795
Oliver Neukum16871dc2011-02-10 15:33:29 +0100796static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
797{
798 int i;
799
800 for (i = 0; i < N_OUT_URB; i++) {
801 if (urb == portdata->out_urbs[i]) {
802 clear_bit(i, &portdata->out_busy);
803 break;
804 }
805 }
806}
807
Matthew Garrett0d456192010-04-01 12:31:07 -0400808static void play_delayed(struct usb_serial_port *port)
809{
810 struct usb_wwan_intf_private *data;
811 struct usb_wwan_port_private *portdata;
812 struct urb *urb;
813 int err;
814
815 portdata = usb_get_serial_port_data(port);
816 data = port->serial->private;
817 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700818 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400819 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100820 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400821 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100822 } else {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700823 usb_unanchor_urb(urb);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100824 /* we have to throw away the rest */
825 do {
826 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100827 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100828 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
829 break;
830 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400831 }
832}
833
834int usb_wwan_resume(struct usb_serial *serial)
835{
836 int i, j;
837 struct usb_serial_port *port;
838 struct usb_wwan_intf_private *intfdata = serial->private;
839 struct usb_wwan_port_private *portdata;
840 struct urb *urb;
841 int err = 0;
842
843 dbg("%s entered", __func__);
844 /* get the interrupt URBs resubmitted unconditionally */
845 for (i = 0; i < serial->num_ports; i++) {
846 port = serial->port[i];
847 if (!port->interrupt_in_urb) {
848 dbg("%s: No interrupt URB for port %d", __func__, i);
849 continue;
850 }
851 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
852 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
853 if (err < 0) {
854 err("%s: Error %d for interrupt URB of port%d",
855 __func__, err, i);
856 goto err_out;
857 }
858 }
859
Hemant Kumar8e52b122012-05-15 12:18:12 -0700860 spin_lock_irq(&intfdata->susp_lock);
861 intfdata->suspended = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400862 for (i = 0; i < serial->num_ports; i++) {
863 /* walk all ports */
864 port = serial->port[i];
865 portdata = usb_get_serial_port_data(port);
866
867 /* skip closed ports */
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700868 if (!portdata->opened)
Matthew Garrett0d456192010-04-01 12:31:07 -0400869 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400870
871 for (j = 0; j < N_IN_URB; j++) {
872 urb = portdata->in_urbs[j];
Jack Pham74715d92012-08-28 18:26:54 -0700873
874 /* don't re-submit if it already was submitted or if
875 * it is being processed by in_work */
876 if (urb->anchor || !list_empty(&urb->urb_list))
877 continue;
878
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700879 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400880 err = usb_submit_urb(urb, GFP_ATOMIC);
881 if (err < 0) {
Jack Pham74715d92012-08-28 18:26:54 -0700882 err("%s: Error %d for bulk URB[%d]:%p %d",
883 __func__, err, j, urb, i);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700884 usb_unanchor_urb(urb);
885 intfdata->suspended = 1;
Matthew Garrett0d456192010-04-01 12:31:07 -0400886 spin_unlock_irq(&intfdata->susp_lock);
887 goto err_out;
888 }
889 }
890 play_delayed(port);
Matthew Garrett0d456192010-04-01 12:31:07 -0400891 }
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700892 spin_unlock_irq(&intfdata->susp_lock);
893
Matthew Garrett0d456192010-04-01 12:31:07 -0400894err_out:
895 return err;
896}
897EXPORT_SYMBOL(usb_wwan_resume);
898#endif
899
900MODULE_AUTHOR(DRIVER_AUTHOR);
901MODULE_DESCRIPTION(DRIVER_DESC);
902MODULE_VERSION(DRIVER_VERSION);
903MODULE_LICENSE("GPL");
904
905module_param(debug, bool, S_IRUGO | S_IWUSR);
906MODULE_PARM_DESC(debug, "Debug messages");