blob: 1c8664cea13267bcb844e55511a59516ee4f14a5 [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;
Joel King913565a2012-07-11 16:18:54 -0700519 tty->update_room_in_ldisc = 1;
Vamsi Krishna68194482011-12-12 18:28:48 -0800520
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800521 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
Matthew Garrett0d456192010-04-01 12:31:07 -0400522 dbg("%s", __func__);
523
524 /* Start reading from the IN endpoint */
525 for (i = 0; i < N_IN_URB; i++) {
526 urb = portdata->in_urbs[i];
527 if (!urb)
528 continue;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700529 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400530 err = usb_submit_urb(urb, GFP_KERNEL);
531 if (err) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700532 usb_unanchor_urb(urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400533 dbg("%s: submit urb %d failed (%d) %d",
534 __func__, i, err, urb->transfer_buffer_length);
535 }
536 }
537
538 if (intfdata->send_setup)
539 intfdata->send_setup(port);
540
541 serial->interface->needs_remote_wakeup = 1;
542 spin_lock_irq(&intfdata->susp_lock);
543 portdata->opened = 1;
544 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100545 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400546 usb_autopm_put_interface(serial->interface);
547
548 return 0;
549}
550EXPORT_SYMBOL(usb_wwan_open);
551
552void usb_wwan_close(struct usb_serial_port *port)
553{
554 int i;
555 struct usb_serial *serial = port->serial;
556 struct usb_wwan_port_private *portdata;
557 struct usb_wwan_intf_private *intfdata = port->serial->private;
558
559 dbg("%s", __func__);
560 portdata = usb_get_serial_port_data(port);
561
562 if (serial->dev) {
563 /* Stop reading/writing urbs */
564 spin_lock_irq(&intfdata->susp_lock);
565 portdata->opened = 0;
566 spin_unlock_irq(&intfdata->susp_lock);
567
568 for (i = 0; i < N_IN_URB; i++)
569 usb_kill_urb(portdata->in_urbs[i]);
570 for (i = 0; i < N_OUT_URB; i++)
571 usb_kill_urb(portdata->out_urbs[i]);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100572 /* balancing - important as an error cannot be handled*/
573 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400574 serial->interface->needs_remote_wakeup = 0;
575 }
576}
577EXPORT_SYMBOL(usb_wwan_close);
578
579/* Helper functions used by usb_wwan_setup_urbs */
580static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
581 int dir, void *ctx, char *buf, int len,
582 void (*callback) (struct urb *))
583{
584 struct urb *urb;
585
586 if (endpoint == -1)
587 return NULL; /* endpoint not needed */
588
589 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
590 if (urb == NULL) {
591 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
592 return NULL;
593 }
594
595 /* Fill URB using supplied data. */
596 usb_fill_bulk_urb(urb, serial->dev,
597 usb_sndbulkpipe(serial->dev, endpoint) | dir,
598 buf, len, callback, ctx);
599
600 return urb;
601}
602
603/* Setup urbs */
604static void usb_wwan_setup_urbs(struct usb_serial *serial)
605{
606 int i, j;
607 struct usb_serial_port *port;
608 struct usb_wwan_port_private *portdata;
609
610 dbg("%s", __func__);
611
612 for (i = 0; i < serial->num_ports; i++) {
613 port = serial->port[i];
614 portdata = usb_get_serial_port_data(port);
615
616 /* Do indat endpoints first */
617 for (j = 0; j < N_IN_URB; ++j) {
618 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
619 port->
620 bulk_in_endpointAddress,
621 USB_DIR_IN,
622 port,
623 portdata->
624 in_buffer[j],
625 IN_BUFLEN,
626 usb_wwan_indat_callback);
627 }
628
629 /* outdat endpoints */
630 for (j = 0; j < N_OUT_URB; ++j) {
631 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
632 port->
633 bulk_out_endpointAddress,
634 USB_DIR_OUT,
635 port,
636 portdata->
637 out_buffer
638 [j],
639 OUT_BUFLEN,
640 usb_wwan_outdat_callback);
641 }
642 }
643}
644
645int usb_wwan_startup(struct usb_serial *serial)
646{
647 int i, j, err;
648 struct usb_serial_port *port;
649 struct usb_wwan_port_private *portdata;
650 u8 *buffer;
651
652 dbg("%s", __func__);
653
654 /* Now setup per port private data */
655 for (i = 0; i < serial->num_ports; i++) {
656 port = serial->port[i];
657 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
658 if (!portdata) {
659 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
660 __func__, i);
661 return 1;
662 }
663 init_usb_anchor(&portdata->delayed);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700664 init_usb_anchor(&portdata->submitted);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700665 INIT_WORK(&portdata->in_work, usb_wwan_in_work);
666 INIT_LIST_HEAD(&portdata->in_urb_list);
667 spin_lock_init(&portdata->in_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400668
669 for (j = 0; j < N_IN_URB; j++) {
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800670 buffer = kmalloc(IN_BUFLEN, GFP_KERNEL);
Matthew Garrett0d456192010-04-01 12:31:07 -0400671 if (!buffer)
672 goto bail_out_error;
673 portdata->in_buffer[j] = buffer;
674 }
675
676 for (j = 0; j < N_OUT_URB; j++) {
677 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
678 if (!buffer)
679 goto bail_out_error2;
680 portdata->out_buffer[j] = buffer;
681 }
682
683 usb_set_serial_port_data(port, portdata);
684
685 if (!port->interrupt_in_urb)
686 continue;
687 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
688 if (err)
689 dbg("%s: submit irq_in urb failed %d", __func__, err);
690 }
691 usb_wwan_setup_urbs(serial);
692 return 0;
693
694bail_out_error2:
695 for (j = 0; j < N_OUT_URB; j++)
696 kfree(portdata->out_buffer[j]);
697bail_out_error:
698 for (j = 0; j < N_IN_URB; j++)
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800699 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400700 kfree(portdata);
701 return 1;
702}
703EXPORT_SYMBOL(usb_wwan_startup);
704
705static void stop_read_write_urbs(struct usb_serial *serial)
706{
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700707 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400708 struct usb_serial_port *port;
709 struct usb_wwan_port_private *portdata;
710
711 /* Stop reading/writing urbs */
712 for (i = 0; i < serial->num_ports; ++i) {
713 port = serial->port[i];
714 portdata = usb_get_serial_port_data(port);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700715 usb_kill_anchored_urbs(&portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400716 }
717}
718
719void usb_wwan_disconnect(struct usb_serial *serial)
720{
721 dbg("%s", __func__);
722
723 stop_read_write_urbs(serial);
724}
725EXPORT_SYMBOL(usb_wwan_disconnect);
726
727void usb_wwan_release(struct usb_serial *serial)
728{
729 int i, j;
730 struct usb_serial_port *port;
731 struct usb_wwan_port_private *portdata;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700732 struct urb *urb;
733 struct list_head *q;
734 unsigned long flags;
Matthew Garrett0d456192010-04-01 12:31:07 -0400735
736 /* Now free them */
737 for (i = 0; i < serial->num_ports; ++i) {
738 port = serial->port[i];
739 portdata = usb_get_serial_port_data(port);
740
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700741 cancel_work_sync(&portdata->in_work);
742 /* TBD: do we really need this */
743 spin_lock_irqsave(&portdata->in_lock, flags);
744 q = &portdata->in_urb_list;
745 while (!list_empty(q)) {
746 urb = list_first_entry(q, struct urb, urb_list);
747 list_del_init(&urb->urb_list);
748 }
749 spin_unlock_irqrestore(&portdata->in_lock, flags);
750
Matthew Garrett0d456192010-04-01 12:31:07 -0400751 for (j = 0; j < N_IN_URB; j++) {
752 usb_free_urb(portdata->in_urbs[j]);
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800753 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400754 portdata->in_urbs[j] = NULL;
755 }
756 for (j = 0; j < N_OUT_URB; j++) {
757 usb_free_urb(portdata->out_urbs[j]);
758 kfree(portdata->out_buffer[j]);
759 portdata->out_urbs[j] = NULL;
760 }
761 }
762
763 /* Now free per port private data */
764 for (i = 0; i < serial->num_ports; i++) {
765 port = serial->port[i];
766 kfree(usb_get_serial_port_data(port));
767 }
768}
769EXPORT_SYMBOL(usb_wwan_release);
770
771#ifdef CONFIG_PM
772int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
773{
774 struct usb_wwan_intf_private *intfdata = serial->private;
775 int b;
776
777 dbg("%s entered", __func__);
778
Alan Stern5b1b0b82011-08-19 23:49:48 +0200779 if (PMSG_IS_AUTO(message)) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400780 spin_lock_irq(&intfdata->susp_lock);
781 b = intfdata->in_flight;
782 spin_unlock_irq(&intfdata->susp_lock);
783
Hemant Kumarf2285db2012-07-23 20:02:51 -0700784 if (b || pm_runtime_autosuspend_expiration(&serial->dev->dev))
Matthew Garrett0d456192010-04-01 12:31:07 -0400785 return -EBUSY;
786 }
787
788 spin_lock_irq(&intfdata->susp_lock);
789 intfdata->suspended = 1;
790 spin_unlock_irq(&intfdata->susp_lock);
791 stop_read_write_urbs(serial);
792
793 return 0;
794}
795EXPORT_SYMBOL(usb_wwan_suspend);
796
Oliver Neukum16871dc2011-02-10 15:33:29 +0100797static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
798{
799 int i;
800
801 for (i = 0; i < N_OUT_URB; i++) {
802 if (urb == portdata->out_urbs[i]) {
803 clear_bit(i, &portdata->out_busy);
804 break;
805 }
806 }
807}
808
Matthew Garrett0d456192010-04-01 12:31:07 -0400809static void play_delayed(struct usb_serial_port *port)
810{
811 struct usb_wwan_intf_private *data;
812 struct usb_wwan_port_private *portdata;
813 struct urb *urb;
814 int err;
815
816 portdata = usb_get_serial_port_data(port);
817 data = port->serial->private;
818 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700819 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400820 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100821 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400822 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100823 } else {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700824 usb_unanchor_urb(urb);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100825 /* we have to throw away the rest */
826 do {
827 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100828 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100829 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
830 break;
831 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400832 }
833}
834
835int usb_wwan_resume(struct usb_serial *serial)
836{
837 int i, j;
838 struct usb_serial_port *port;
839 struct usb_wwan_intf_private *intfdata = serial->private;
840 struct usb_wwan_port_private *portdata;
841 struct urb *urb;
842 int err = 0;
843
844 dbg("%s entered", __func__);
845 /* get the interrupt URBs resubmitted unconditionally */
846 for (i = 0; i < serial->num_ports; i++) {
847 port = serial->port[i];
848 if (!port->interrupt_in_urb) {
849 dbg("%s: No interrupt URB for port %d", __func__, i);
850 continue;
851 }
852 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
853 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
854 if (err < 0) {
855 err("%s: Error %d for interrupt URB of port%d",
856 __func__, err, i);
857 goto err_out;
858 }
859 }
860
Hemant Kumar8e52b122012-05-15 12:18:12 -0700861 spin_lock_irq(&intfdata->susp_lock);
862 intfdata->suspended = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400863 for (i = 0; i < serial->num_ports; i++) {
864 /* walk all ports */
865 port = serial->port[i];
866 portdata = usb_get_serial_port_data(port);
867
868 /* skip closed ports */
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700869 if (!portdata->opened)
Matthew Garrett0d456192010-04-01 12:31:07 -0400870 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400871
872 for (j = 0; j < N_IN_URB; j++) {
873 urb = portdata->in_urbs[j];
Jack Pham74715d92012-08-28 18:26:54 -0700874
875 /* don't re-submit if it already was submitted or if
876 * it is being processed by in_work */
877 if (urb->anchor || !list_empty(&urb->urb_list))
878 continue;
879
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700880 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400881 err = usb_submit_urb(urb, GFP_ATOMIC);
882 if (err < 0) {
Jack Pham74715d92012-08-28 18:26:54 -0700883 err("%s: Error %d for bulk URB[%d]:%p %d",
884 __func__, err, j, urb, i);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700885 usb_unanchor_urb(urb);
886 intfdata->suspended = 1;
Matthew Garrett0d456192010-04-01 12:31:07 -0400887 spin_unlock_irq(&intfdata->susp_lock);
888 goto err_out;
889 }
890 }
891 play_delayed(port);
Matthew Garrett0d456192010-04-01 12:31:07 -0400892 }
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700893 spin_unlock_irq(&intfdata->susp_lock);
894
Matthew Garrett0d456192010-04-01 12:31:07 -0400895err_out:
896 return err;
897}
898EXPORT_SYMBOL(usb_wwan_resume);
899#endif
900
901MODULE_AUTHOR(DRIVER_AUTHOR);
902MODULE_DESCRIPTION(DRIVER_DESC);
903MODULE_VERSION(DRIVER_VERSION);
904MODULE_LICENSE("GPL");
905
906module_param(debug, bool, S_IRUGO | S_IWUSR);
907MODULE_PARM_DESC(debug, "Debug messages");