blob: 395a55560007ee2f5256f175f7077f7169326a1f [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{
Matthew Garrett0d456192010-04-01 12:31:07 -040044 struct usb_wwan_port_private *portdata;
45
46 struct usb_wwan_intf_private *intfdata;
47
48 dbg("%s", __func__);
49
50 intfdata = port->serial->private;
51
52 if (!intfdata->send_setup)
53 return;
54
55 portdata = usb_get_serial_port_data(port);
Johan Hovold906c9c42013-02-13 17:53:28 +010056 /* FIXME: locking */
Matthew Garrett0d456192010-04-01 12:31:07 -040057 portdata->rts_state = on;
58 portdata->dtr_state = on;
Johan Hovold906c9c42013-02-13 17:53:28 +010059
60 intfdata->send_setup(port);
Matthew Garrett0d456192010-04-01 12:31:07 -040061}
62EXPORT_SYMBOL(usb_wwan_dtr_rts);
63
64void usb_wwan_set_termios(struct tty_struct *tty,
65 struct usb_serial_port *port,
66 struct ktermios *old_termios)
67{
68 struct usb_wwan_intf_private *intfdata = port->serial->private;
69
70 dbg("%s", __func__);
71
72 /* Doesn't support option setting */
73 tty_termios_copy_hw(tty->termios, old_termios);
74
75 if (intfdata->send_setup)
76 intfdata->send_setup(port);
77}
78EXPORT_SYMBOL(usb_wwan_set_termios);
79
Alan Cox60b33c12011-02-14 16:26:14 +000080int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040081{
82 struct usb_serial_port *port = tty->driver_data;
83 unsigned int value;
84 struct usb_wwan_port_private *portdata;
85
86 portdata = usb_get_serial_port_data(port);
87
88 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
89 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
90 ((portdata->cts_state) ? TIOCM_CTS : 0) |
91 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
92 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
93 ((portdata->ri_state) ? TIOCM_RNG : 0);
94
95 return value;
96}
97EXPORT_SYMBOL(usb_wwan_tiocmget);
98
Alan Cox20b9d172011-02-14 16:26:50 +000099int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -0400100 unsigned int set, unsigned int clear)
101{
102 struct usb_serial_port *port = tty->driver_data;
103 struct usb_wwan_port_private *portdata;
104 struct usb_wwan_intf_private *intfdata;
105
106 portdata = usb_get_serial_port_data(port);
107 intfdata = port->serial->private;
108
109 if (!intfdata->send_setup)
110 return -EINVAL;
111
112 /* FIXME: what locks portdata fields ? */
113 if (set & TIOCM_RTS)
114 portdata->rts_state = 1;
115 if (set & TIOCM_DTR)
116 portdata->dtr_state = 1;
117
118 if (clear & TIOCM_RTS)
119 portdata->rts_state = 0;
120 if (clear & TIOCM_DTR)
121 portdata->dtr_state = 0;
122 return intfdata->send_setup(port);
123}
124EXPORT_SYMBOL(usb_wwan_tiocmset);
125
Dan Williams02303f72010-11-19 16:04:00 -0600126static int get_serial_info(struct usb_serial_port *port,
127 struct serial_struct __user *retinfo)
128{
129 struct serial_struct tmp;
130
131 if (!retinfo)
132 return -EFAULT;
133
134 memset(&tmp, 0, sizeof(tmp));
135 tmp.line = port->serial->minor;
136 tmp.port = port->number;
137 tmp.baud_base = tty_get_baud_rate(port->port.tty);
138 tmp.close_delay = port->port.close_delay / 10;
139 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
140 ASYNC_CLOSING_WAIT_NONE :
141 port->port.closing_wait / 10;
142
143 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
144 return -EFAULT;
145 return 0;
146}
147
148static int set_serial_info(struct usb_serial_port *port,
149 struct serial_struct __user *newinfo)
150{
151 struct serial_struct new_serial;
152 unsigned int closing_wait, close_delay;
153 int retval = 0;
154
155 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
156 return -EFAULT;
157
158 close_delay = new_serial.close_delay * 10;
159 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
160 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
161
162 mutex_lock(&port->port.mutex);
163
164 if (!capable(CAP_SYS_ADMIN)) {
165 if ((close_delay != port->port.close_delay) ||
166 (closing_wait != port->port.closing_wait))
167 retval = -EPERM;
168 else
169 retval = -EOPNOTSUPP;
170 } else {
171 port->port.close_delay = close_delay;
172 port->port.closing_wait = closing_wait;
173 }
174
175 mutex_unlock(&port->port.mutex);
176 return retval;
177}
178
Alan Cox00a0d0d2011-02-14 16:27:06 +0000179int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600180 unsigned int cmd, unsigned long arg)
181{
182 struct usb_serial_port *port = tty->driver_data;
183
184 dbg("%s cmd 0x%04x", __func__, cmd);
185
186 switch (cmd) {
187 case TIOCGSERIAL:
188 return get_serial_info(port,
189 (struct serial_struct __user *) arg);
190 case TIOCSSERIAL:
191 return set_serial_info(port,
192 (struct serial_struct __user *) arg);
193 default:
194 break;
195 }
196
197 dbg("%s arg not supported", __func__);
198
199 return -ENOIOCTLCMD;
200}
201EXPORT_SYMBOL(usb_wwan_ioctl);
202
Matthew Garrett0d456192010-04-01 12:31:07 -0400203/* Write */
204int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count)
206{
207 struct usb_wwan_port_private *portdata;
208 struct usb_wwan_intf_private *intfdata;
209 int i;
210 int left, todo;
211 struct urb *this_urb = NULL; /* spurious */
212 int err;
213 unsigned long flags;
214
215 portdata = usb_get_serial_port_data(port);
216 intfdata = port->serial->private;
217
218 dbg("%s: write (%d chars)", __func__, count);
219
220 i = 0;
221 left = count;
222 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
223 todo = left;
224 if (todo > OUT_BUFLEN)
225 todo = OUT_BUFLEN;
226
227 this_urb = portdata->out_urbs[i];
228 if (test_and_set_bit(i, &portdata->out_busy)) {
229 if (time_before(jiffies,
230 portdata->tx_start_time[i] + 10 * HZ))
231 continue;
232 usb_unlink_urb(this_urb);
233 continue;
234 }
235 dbg("%s: endpoint %d buf %d", __func__,
236 usb_pipeendpoint(this_urb->pipe), i);
237
238 err = usb_autopm_get_interface_async(port->serial->interface);
xiao jin640768a2014-05-26 19:23:13 +0200239 if (err < 0) {
240 clear_bit(i, &portdata->out_busy);
Matthew Garrett0d456192010-04-01 12:31:07 -0400241 break;
xiao jin640768a2014-05-26 19:23:13 +0200242 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400243
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 Phamc7525b82012-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 Phamc7525b82012-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 Phamc7525b82012-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 Phamc7525b82012-08-28 18:26:54 -0700337 intfdata = port->serial->private;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700338
Jack Phamc7525b82012-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 Phamc7525b82012-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 Phamc7525b82012-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 Phamc7525b82012-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 Kondeti71d2f462013-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 Phamc7525b82012-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 Kondeti71d2f462013-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
Johan Hovold0886f4e2014-05-26 19:23:17 +0200523 if (port->interrupt_in_urb) {
524 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
525 if (err) {
526 dev_dbg(&port->dev, "%s: submit int urb failed: %d\n",
527 __func__, err);
528 }
529 }
530
Matthew Garrett0d456192010-04-01 12:31:07 -0400531 /* Start reading from the IN endpoint */
532 for (i = 0; i < N_IN_URB; i++) {
533 urb = portdata->in_urbs[i];
534 if (!urb)
535 continue;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700536 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400537 err = usb_submit_urb(urb, GFP_KERNEL);
538 if (err) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700539 usb_unanchor_urb(urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400540 dbg("%s: submit urb %d failed (%d) %d",
541 __func__, i, err, urb->transfer_buffer_length);
542 }
543 }
544
545 if (intfdata->send_setup)
546 intfdata->send_setup(port);
547
548 serial->interface->needs_remote_wakeup = 1;
549 spin_lock_irq(&intfdata->susp_lock);
550 portdata->opened = 1;
551 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100552 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400553 usb_autopm_put_interface(serial->interface);
554
555 return 0;
556}
557EXPORT_SYMBOL(usb_wwan_open);
558
Johan Hovoldca913952014-05-26 19:23:16 +0200559static void unbusy_queued_urb(struct urb *urb,
560 struct usb_wwan_port_private *portdata)
561{
562 int i;
563
564 for (i = 0; i < N_OUT_URB; i++) {
565 if (urb == portdata->out_urbs[i]) {
566 clear_bit(i, &portdata->out_busy);
567 break;
568 }
569 }
570}
571
Matthew Garrett0d456192010-04-01 12:31:07 -0400572void usb_wwan_close(struct usb_serial_port *port)
573{
574 int i;
575 struct usb_serial *serial = port->serial;
576 struct usb_wwan_port_private *portdata;
577 struct usb_wwan_intf_private *intfdata = port->serial->private;
Johan Hovoldca913952014-05-26 19:23:16 +0200578 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400579
580 dbg("%s", __func__);
581 portdata = usb_get_serial_port_data(port);
582
583 if (serial->dev) {
584 /* Stop reading/writing urbs */
585 spin_lock_irq(&intfdata->susp_lock);
586 portdata->opened = 0;
587 spin_unlock_irq(&intfdata->susp_lock);
588
Johan Hovoldca913952014-05-26 19:23:16 +0200589 for (;;) {
590 urb = usb_get_from_anchor(&portdata->delayed);
591 if (!urb)
592 break;
593 unbusy_queued_urb(urb, portdata);
594 usb_autopm_put_interface_async(serial->interface);
595 }
596
Matthew Garrett0d456192010-04-01 12:31:07 -0400597 for (i = 0; i < N_IN_URB; i++)
598 usb_kill_urb(portdata->in_urbs[i]);
599 for (i = 0; i < N_OUT_URB; i++)
600 usb_kill_urb(portdata->out_urbs[i]);
Johan Hovold0886f4e2014-05-26 19:23:17 +0200601 usb_kill_urb(port->interrupt_in_urb);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100602 /* balancing - important as an error cannot be handled*/
603 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400604 serial->interface->needs_remote_wakeup = 0;
605 }
606}
607EXPORT_SYMBOL(usb_wwan_close);
608
609/* Helper functions used by usb_wwan_setup_urbs */
610static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
611 int dir, void *ctx, char *buf, int len,
612 void (*callback) (struct urb *))
613{
614 struct urb *urb;
615
616 if (endpoint == -1)
617 return NULL; /* endpoint not needed */
618
619 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
620 if (urb == NULL) {
621 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
622 return NULL;
623 }
624
625 /* Fill URB using supplied data. */
626 usb_fill_bulk_urb(urb, serial->dev,
627 usb_sndbulkpipe(serial->dev, endpoint) | dir,
628 buf, len, callback, ctx);
629
630 return urb;
631}
632
633/* Setup urbs */
634static void usb_wwan_setup_urbs(struct usb_serial *serial)
635{
636 int i, j;
637 struct usb_serial_port *port;
638 struct usb_wwan_port_private *portdata;
639
640 dbg("%s", __func__);
641
642 for (i = 0; i < serial->num_ports; i++) {
643 port = serial->port[i];
644 portdata = usb_get_serial_port_data(port);
645
646 /* Do indat endpoints first */
647 for (j = 0; j < N_IN_URB; ++j) {
648 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
649 port->
650 bulk_in_endpointAddress,
651 USB_DIR_IN,
652 port,
653 portdata->
654 in_buffer[j],
655 IN_BUFLEN,
656 usb_wwan_indat_callback);
657 }
658
659 /* outdat endpoints */
660 for (j = 0; j < N_OUT_URB; ++j) {
661 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
662 port->
663 bulk_out_endpointAddress,
664 USB_DIR_OUT,
665 port,
666 portdata->
667 out_buffer
668 [j],
669 OUT_BUFLEN,
670 usb_wwan_outdat_callback);
671 }
672 }
673}
674
675int usb_wwan_startup(struct usb_serial *serial)
676{
Johan Hovold0886f4e2014-05-26 19:23:17 +0200677 int i, j;
Matthew Garrett0d456192010-04-01 12:31:07 -0400678 struct usb_serial_port *port;
679 struct usb_wwan_port_private *portdata;
680 u8 *buffer;
681
682 dbg("%s", __func__);
683
684 /* Now setup per port private data */
685 for (i = 0; i < serial->num_ports; i++) {
686 port = serial->port[i];
687 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
688 if (!portdata) {
689 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
690 __func__, i);
691 return 1;
692 }
693 init_usb_anchor(&portdata->delayed);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700694 init_usb_anchor(&portdata->submitted);
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700695 INIT_WORK(&portdata->in_work, usb_wwan_in_work);
696 INIT_LIST_HEAD(&portdata->in_urb_list);
697 spin_lock_init(&portdata->in_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400698
699 for (j = 0; j < N_IN_URB; j++) {
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800700 buffer = kmalloc(IN_BUFLEN, GFP_KERNEL);
Matthew Garrett0d456192010-04-01 12:31:07 -0400701 if (!buffer)
702 goto bail_out_error;
703 portdata->in_buffer[j] = buffer;
704 }
705
706 for (j = 0; j < N_OUT_URB; j++) {
707 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
708 if (!buffer)
709 goto bail_out_error2;
710 portdata->out_buffer[j] = buffer;
711 }
712
713 usb_set_serial_port_data(port, portdata);
Matthew Garrett0d456192010-04-01 12:31:07 -0400714 }
715 usb_wwan_setup_urbs(serial);
716 return 0;
717
718bail_out_error2:
719 for (j = 0; j < N_OUT_URB; j++)
720 kfree(portdata->out_buffer[j]);
721bail_out_error:
722 for (j = 0; j < N_IN_URB; j++)
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800723 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400724 kfree(portdata);
725 return 1;
726}
727EXPORT_SYMBOL(usb_wwan_startup);
728
729static void stop_read_write_urbs(struct usb_serial *serial)
730{
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700731 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400732 struct usb_serial_port *port;
733 struct usb_wwan_port_private *portdata;
734
735 /* Stop reading/writing urbs */
736 for (i = 0; i < serial->num_ports; ++i) {
737 port = serial->port[i];
738 portdata = usb_get_serial_port_data(port);
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700739 usb_kill_anchored_urbs(&portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400740 }
741}
742
743void usb_wwan_disconnect(struct usb_serial *serial)
744{
745 dbg("%s", __func__);
746
747 stop_read_write_urbs(serial);
748}
749EXPORT_SYMBOL(usb_wwan_disconnect);
750
751void usb_wwan_release(struct usb_serial *serial)
752{
753 int i, j;
754 struct usb_serial_port *port;
755 struct usb_wwan_port_private *portdata;
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700756 struct urb *urb;
757 struct list_head *q;
758 unsigned long flags;
Matthew Garrett0d456192010-04-01 12:31:07 -0400759
760 /* Now free them */
761 for (i = 0; i < serial->num_ports; ++i) {
762 port = serial->port[i];
763 portdata = usb_get_serial_port_data(port);
764
Vamsi Krishna6f806b82012-05-07 16:29:08 -0700765 cancel_work_sync(&portdata->in_work);
766 /* TBD: do we really need this */
767 spin_lock_irqsave(&portdata->in_lock, flags);
768 q = &portdata->in_urb_list;
769 while (!list_empty(q)) {
770 urb = list_first_entry(q, struct urb, urb_list);
771 list_del_init(&urb->urb_list);
772 }
773 spin_unlock_irqrestore(&portdata->in_lock, flags);
774
Matthew Garrett0d456192010-04-01 12:31:07 -0400775 for (j = 0; j < N_IN_URB; j++) {
776 usb_free_urb(portdata->in_urbs[j]);
Vamsi Krishna6ef832f2012-01-27 16:29:21 -0800777 kfree(portdata->in_buffer[j]);
Matthew Garrett0d456192010-04-01 12:31:07 -0400778 portdata->in_urbs[j] = NULL;
779 }
780 for (j = 0; j < N_OUT_URB; j++) {
781 usb_free_urb(portdata->out_urbs[j]);
782 kfree(portdata->out_buffer[j]);
783 portdata->out_urbs[j] = NULL;
784 }
785 }
786
787 /* Now free per port private data */
788 for (i = 0; i < serial->num_ports; i++) {
789 port = serial->port[i];
790 kfree(usb_get_serial_port_data(port));
791 }
792}
793EXPORT_SYMBOL(usb_wwan_release);
794
795#ifdef CONFIG_PM
796int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
797{
798 struct usb_wwan_intf_private *intfdata = serial->private;
Matthew Garrett0d456192010-04-01 12:31:07 -0400799
800 dbg("%s entered", __func__);
801
Johan Hovold887b3c32014-05-26 19:23:15 +0200802 spin_lock_irq(&intfdata->susp_lock);
Alan Stern5b1b0b82011-08-19 23:49:48 +0200803 if (PMSG_IS_AUTO(message)) {
Paul1097d782015-01-11 17:15:40 -0800804 if (intfdata->in_flight ||
805 pm_runtime_autosuspend_expiration(&serial->dev->dev)) {
Johan Hovold887b3c32014-05-26 19:23:15 +0200806 spin_unlock_irq(&intfdata->susp_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400807 return -EBUSY;
Johan Hovold887b3c32014-05-26 19:23:15 +0200808 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400809 }
810
Matthew Garrett0d456192010-04-01 12:31:07 -0400811 intfdata->suspended = 1;
812 spin_unlock_irq(&intfdata->susp_lock);
Johan Hovold887b3c32014-05-26 19:23:15 +0200813
Matthew Garrett0d456192010-04-01 12:31:07 -0400814 stop_read_write_urbs(serial);
815
816 return 0;
817}
818EXPORT_SYMBOL(usb_wwan_suspend);
819
Johan Hovold9e913512014-05-26 19:23:18 +0200820static int play_delayed(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400821{
822 struct usb_wwan_intf_private *data;
823 struct usb_wwan_port_private *portdata;
824 struct urb *urb;
Johan Hovold9e913512014-05-26 19:23:18 +0200825 int err = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400826
827 portdata = usb_get_serial_port_data(port);
828 data = port->serial->private;
829 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700830 usb_anchor_urb(urb, &portdata->submitted);
Matthew Garrett0d456192010-04-01 12:31:07 -0400831 err = usb_submit_urb(urb, GFP_ATOMIC);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100832 if (!err) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400833 data->in_flight++;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100834 } else {
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700835 usb_unanchor_urb(urb);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100836 /* we have to throw away the rest */
837 do {
838 unbusy_queued_urb(urb, portdata);
Oliver Neukum97ac01d2011-03-18 12:44:17 +0100839 usb_autopm_put_interface_no_suspend(port->serial->interface);
Oliver Neukum16871dc2011-02-10 15:33:29 +0100840 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
841 break;
842 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400843 }
Johan Hovold9e913512014-05-26 19:23:18 +0200844
845 return err;
Matthew Garrett0d456192010-04-01 12:31:07 -0400846}
847
848int usb_wwan_resume(struct usb_serial *serial)
849{
850 int i, j;
851 struct usb_serial_port *port;
852 struct usb_wwan_intf_private *intfdata = serial->private;
853 struct usb_wwan_port_private *portdata;
854 struct urb *urb;
Johan Hovold9e913512014-05-26 19:23:18 +0200855 int err;
856 int err_count = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400857
858 dbg("%s entered", __func__);
Matthew Garrett0d456192010-04-01 12:31:07 -0400859
Hemant Kumar8e52b122012-05-15 12:18:12 -0700860 spin_lock_irq(&intfdata->susp_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400861 for (i = 0; i < serial->num_ports; i++) {
862 /* walk all ports */
863 port = serial->port[i];
864 portdata = usb_get_serial_port_data(port);
865
866 /* skip closed ports */
xiao jin50476ee2014-05-26 19:23:14 +0200867 if (!portdata || !portdata->opened)
Matthew Garrett0d456192010-04-01 12:31:07 -0400868 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400869
Johan Hovold0886f4e2014-05-26 19:23:17 +0200870 if (port->interrupt_in_urb) {
871 err = usb_submit_urb(port->interrupt_in_urb,
872 GFP_ATOMIC);
873 if (err) {
874 dev_err(&port->dev,
875 "%s: submit int urb failed: %d\n",
876 __func__, err);
Johan Hovold9e913512014-05-26 19:23:18 +0200877 err_count++;
Johan Hovold0886f4e2014-05-26 19:23:17 +0200878 }
879 }
880
Johan Hovold9e913512014-05-26 19:23:18 +0200881 err = play_delayed(port);
882 if (err)
883 err_count++;
884
Matthew Garrett0d456192010-04-01 12:31:07 -0400885 for (j = 0; j < N_IN_URB; j++) {
886 urb = portdata->in_urbs[j];
887 err = usb_submit_urb(urb, GFP_ATOMIC);
888 if (err < 0) {
889 err("%s: Error %d for bulk URB %d",
890 __func__, err, i);
Johan Hovold9e913512014-05-26 19:23:18 +0200891 err_count++;
Matthew Garrett0d456192010-04-01 12:31:07 -0400892 }
893 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400894 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400895 intfdata->suspended = 0;
Hemant Kumar1ffb0392012-06-14 16:00:24 -0700896 spin_unlock_irq(&intfdata->susp_lock);
897
Johan Hovold9e913512014-05-26 19:23:18 +0200898 if (err_count)
899 return -EIO;
900
901 return 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400902}
903EXPORT_SYMBOL(usb_wwan_resume);
904#endif
905
906MODULE_AUTHOR(DRIVER_AUTHOR);
907MODULE_DESCRIPTION(DRIVER_DESC);
908MODULE_VERSION(DRIVER_VERSION);
909MODULE_LICENSE("GPL");
910
911module_param(debug, bool, S_IRUGO | S_IWUSR);
912MODULE_PARM_DESC(debug, "Debug messages");