blob: 9e48be615b2f374ece41bbd372acb8eda552004c [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Matthew Garrett0d456192010-04-01 12:31:07 -04002/*
3 USB Driver layer for GSM modems
4
5 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
6
7 This driver is free software; you can redistribute it and/or modify
8 it under the terms of Version 2 of the GNU General Public License as
9 published by the Free Software Foundation.
10
11 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
12
13 History: see the git log.
14
15 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
16
17 This driver exists because the "normal" serial driver doesn't work too well
18 with GSM modems. Issues:
19 - data loss -- one single Receive URB is not nearly enough
20 - controlling the baud rate doesn't make sense
21*/
22
Matthew Garrett0d456192010-04-01 12:31:07 -040023#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
David Ward669e7292015-09-16 12:27:57 -040040/*
41 * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
42 * in CDC ACM.
43 */
44static int usb_wwan_send_setup(struct usb_serial_port *port)
45{
46 struct usb_serial *serial = port->serial;
47 struct usb_wwan_port_private *portdata;
48 int val = 0;
49 int ifnum;
50 int res;
51
52 portdata = usb_get_serial_port_data(port);
53
54 if (portdata->dtr_state)
55 val |= 0x01;
56 if (portdata->rts_state)
57 val |= 0x02;
58
59 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
60
61 res = usb_autopm_get_interface(serial->interface);
62 if (res)
63 return res;
64
65 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
66 0x22, 0x21, val, ifnum, NULL, 0,
67 USB_CTRL_SET_TIMEOUT);
68
69 usb_autopm_put_interface(port->serial->interface);
70
71 return res;
72}
73
Matthew Garrett0d456192010-04-01 12:31:07 -040074void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
75{
Matthew Garrett0d456192010-04-01 12:31:07 -040076 struct usb_wwan_port_private *portdata;
Matthew Garrett0d456192010-04-01 12:31:07 -040077 struct usb_wwan_intf_private *intfdata;
78
Johan Hovold37357ca2014-05-26 19:23:26 +020079 intfdata = usb_get_serial_data(port->serial);
Matthew Garrett0d456192010-04-01 12:31:07 -040080
David Ward669e7292015-09-16 12:27:57 -040081 if (!intfdata->use_send_setup)
Matthew Garrett0d456192010-04-01 12:31:07 -040082 return;
83
84 portdata = usb_get_serial_port_data(port);
Johan Hovoldb2ca6992013-02-13 17:53:28 +010085 /* FIXME: locking */
Matthew Garrett0d456192010-04-01 12:31:07 -040086 portdata->rts_state = on;
87 portdata->dtr_state = on;
Johan Hovoldb2ca6992013-02-13 17:53:28 +010088
David Ward669e7292015-09-16 12:27:57 -040089 usb_wwan_send_setup(port);
Matthew Garrett0d456192010-04-01 12:31:07 -040090}
91EXPORT_SYMBOL(usb_wwan_dtr_rts);
92
Alan Cox60b33c12011-02-14 16:26:14 +000093int usb_wwan_tiocmget(struct tty_struct *tty)
Matthew Garrett0d456192010-04-01 12:31:07 -040094{
95 struct usb_serial_port *port = tty->driver_data;
96 unsigned int value;
97 struct usb_wwan_port_private *portdata;
98
99 portdata = usb_get_serial_port_data(port);
100
101 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
102 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
103 ((portdata->cts_state) ? TIOCM_CTS : 0) |
104 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
105 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
106 ((portdata->ri_state) ? TIOCM_RNG : 0);
107
108 return value;
109}
110EXPORT_SYMBOL(usb_wwan_tiocmget);
111
Alan Cox20b9d172011-02-14 16:26:50 +0000112int usb_wwan_tiocmset(struct tty_struct *tty,
Matthew Garrett0d456192010-04-01 12:31:07 -0400113 unsigned int set, unsigned int clear)
114{
115 struct usb_serial_port *port = tty->driver_data;
116 struct usb_wwan_port_private *portdata;
117 struct usb_wwan_intf_private *intfdata;
118
119 portdata = usb_get_serial_port_data(port);
Johan Hovold37357ca2014-05-26 19:23:26 +0200120 intfdata = usb_get_serial_data(port->serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400121
David Ward669e7292015-09-16 12:27:57 -0400122 if (!intfdata->use_send_setup)
Matthew Garrett0d456192010-04-01 12:31:07 -0400123 return -EINVAL;
124
125 /* FIXME: what locks portdata fields ? */
126 if (set & TIOCM_RTS)
127 portdata->rts_state = 1;
128 if (set & TIOCM_DTR)
129 portdata->dtr_state = 1;
130
131 if (clear & TIOCM_RTS)
132 portdata->rts_state = 0;
133 if (clear & TIOCM_DTR)
134 portdata->dtr_state = 0;
David Ward669e7292015-09-16 12:27:57 -0400135 return usb_wwan_send_setup(port);
Matthew Garrett0d456192010-04-01 12:31:07 -0400136}
137EXPORT_SYMBOL(usb_wwan_tiocmset);
138
Dan Williams02303f72010-11-19 16:04:00 -0600139static int get_serial_info(struct usb_serial_port *port,
140 struct serial_struct __user *retinfo)
141{
142 struct serial_struct tmp;
143
Dan Williams02303f72010-11-19 16:04:00 -0600144 memset(&tmp, 0, sizeof(tmp));
Greg Kroah-Hartmane5b1e202013-06-07 11:04:28 -0700145 tmp.line = port->minor;
Greg Kroah-Hartman11438322013-06-06 10:32:00 -0700146 tmp.port = port->port_number;
Dan Williams02303f72010-11-19 16:04:00 -0600147 tmp.baud_base = tty_get_baud_rate(port->port.tty);
148 tmp.close_delay = port->port.close_delay / 10;
149 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
150 ASYNC_CLOSING_WAIT_NONE :
151 port->port.closing_wait / 10;
152
153 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
154 return -EFAULT;
155 return 0;
156}
157
158static int set_serial_info(struct usb_serial_port *port,
159 struct serial_struct __user *newinfo)
160{
161 struct serial_struct new_serial;
162 unsigned int closing_wait, close_delay;
163 int retval = 0;
164
165 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
166 return -EFAULT;
167
168 close_delay = new_serial.close_delay * 10;
169 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
170 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
171
172 mutex_lock(&port->port.mutex);
173
174 if (!capable(CAP_SYS_ADMIN)) {
175 if ((close_delay != port->port.close_delay) ||
176 (closing_wait != port->port.closing_wait))
177 retval = -EPERM;
178 else
179 retval = -EOPNOTSUPP;
180 } else {
181 port->port.close_delay = close_delay;
182 port->port.closing_wait = closing_wait;
183 }
184
185 mutex_unlock(&port->port.mutex);
186 return retval;
187}
188
Alan Cox00a0d0d2011-02-14 16:27:06 +0000189int usb_wwan_ioctl(struct tty_struct *tty,
Dan Williams02303f72010-11-19 16:04:00 -0600190 unsigned int cmd, unsigned long arg)
191{
192 struct usb_serial_port *port = tty->driver_data;
193
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700194 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
Dan Williams02303f72010-11-19 16:04:00 -0600195
196 switch (cmd) {
197 case TIOCGSERIAL:
198 return get_serial_info(port,
199 (struct serial_struct __user *) arg);
200 case TIOCSSERIAL:
201 return set_serial_info(port,
202 (struct serial_struct __user *) arg);
203 default:
204 break;
205 }
206
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700207 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
Dan Williams02303f72010-11-19 16:04:00 -0600208
209 return -ENOIOCTLCMD;
210}
211EXPORT_SYMBOL(usb_wwan_ioctl);
212
Matthew Garrett0d456192010-04-01 12:31:07 -0400213int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
214 const unsigned char *buf, int count)
215{
216 struct usb_wwan_port_private *portdata;
217 struct usb_wwan_intf_private *intfdata;
218 int i;
219 int left, todo;
220 struct urb *this_urb = NULL; /* spurious */
221 int err;
222 unsigned long flags;
223
224 portdata = usb_get_serial_port_data(port);
Johan Hovold37357ca2014-05-26 19:23:26 +0200225 intfdata = usb_get_serial_data(port->serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400226
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700227 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400228
229 i = 0;
230 left = count;
231 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
232 todo = left;
233 if (todo > OUT_BUFLEN)
234 todo = OUT_BUFLEN;
235
236 this_urb = portdata->out_urbs[i];
237 if (test_and_set_bit(i, &portdata->out_busy)) {
238 if (time_before(jiffies,
239 portdata->tx_start_time[i] + 10 * HZ))
240 continue;
241 usb_unlink_urb(this_urb);
242 continue;
243 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700244 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
245 usb_pipeendpoint(this_urb->pipe), i);
Matthew Garrett0d456192010-04-01 12:31:07 -0400246
247 err = usb_autopm_get_interface_async(port->serial->interface);
xiao jindb090472014-05-26 19:23:13 +0200248 if (err < 0) {
249 clear_bit(i, &portdata->out_busy);
Matthew Garrett0d456192010-04-01 12:31:07 -0400250 break;
xiao jindb090472014-05-26 19:23:13 +0200251 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400252
253 /* send the data */
254 memcpy(this_urb->transfer_buffer, buf, todo);
255 this_urb->transfer_buffer_length = todo;
256
257 spin_lock_irqsave(&intfdata->susp_lock, flags);
258 if (intfdata->suspended) {
259 usb_anchor_urb(this_urb, &portdata->delayed);
260 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
261 } else {
262 intfdata->in_flight++;
263 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
264 err = usb_submit_urb(this_urb, GFP_ATOMIC);
265 if (err) {
Johan Hovold8bb7ec62014-05-26 19:23:31 +0200266 dev_err(&port->dev,
267 "%s: submit urb %d failed: %d\n",
268 __func__, i, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400269 clear_bit(i, &portdata->out_busy);
270 spin_lock_irqsave(&intfdata->susp_lock, flags);
271 intfdata->in_flight--;
272 spin_unlock_irqrestore(&intfdata->susp_lock,
273 flags);
Oliver Neukum3d06bf12011-02-10 15:33:17 +0100274 usb_autopm_put_interface_async(port->serial->interface);
Oliver Neukum433508a2011-02-10 15:33:23 +0100275 break;
Matthew Garrett0d456192010-04-01 12:31:07 -0400276 }
277 }
278
279 portdata->tx_start_time[i] = jiffies;
280 buf += todo;
281 left -= todo;
282 }
283
284 count -= left;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700285 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
Matthew Garrett0d456192010-04-01 12:31:07 -0400286 return count;
287}
288EXPORT_SYMBOL(usb_wwan_write);
289
290static void usb_wwan_indat_callback(struct urb *urb)
291{
292 int err;
293 int endpoint;
294 struct usb_serial_port *port;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700295 struct device *dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400296 unsigned char *data = urb->transfer_buffer;
297 int status = urb->status;
298
Matthew Garrett0d456192010-04-01 12:31:07 -0400299 endpoint = usb_pipeendpoint(urb->pipe);
300 port = urb->context;
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700301 dev = &port->dev;
Matthew Garrett0d456192010-04-01 12:31:07 -0400302
303 if (status) {
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700304 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
305 __func__, status, endpoint);
Matthew Garrett0d456192010-04-01 12:31:07 -0400306 } else {
Jiri Slaby2e124b42013-01-03 15:53:06 +0100307 if (urb->actual_length) {
308 tty_insert_flip_string(&port->port, data,
309 urb->actual_length);
310 tty_flip_buffer_push(&port->port);
311 } else
312 dev_dbg(dev, "%s: empty read urb received\n", __func__);
Matt Burtch6c1ee662013-08-12 10:11:39 -0700313 }
314 /* Resubmit urb so we continue receiving */
315 err = usb_submit_urb(urb, GFP_ATOMIC);
316 if (err) {
Johan Hovoldd2958d12015-08-17 17:35:24 +0200317 if (err != -EPERM && err != -ENODEV) {
Matt Burtch6c1ee662013-08-12 10:11:39 -0700318 dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
319 __func__, err);
320 /* busy also in error unless we are killed */
Dan Carpenterec428992012-04-20 09:33:31 +0300321 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400322 }
Matt Burtch6c1ee662013-08-12 10:11:39 -0700323 } else {
324 usb_mark_last_busy(port->serial->dev);
Matthew Garrett0d456192010-04-01 12:31:07 -0400325 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400326}
327
328static void usb_wwan_outdat_callback(struct urb *urb)
329{
330 struct usb_serial_port *port;
331 struct usb_wwan_port_private *portdata;
332 struct usb_wwan_intf_private *intfdata;
333 int i;
334
Matthew Garrett0d456192010-04-01 12:31:07 -0400335 port = urb->context;
Johan Hovold37357ca2014-05-26 19:23:26 +0200336 intfdata = usb_get_serial_data(port->serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400337
338 usb_serial_port_softint(port);
339 usb_autopm_put_interface_async(port->serial->interface);
340 portdata = usb_get_serial_port_data(port);
341 spin_lock(&intfdata->susp_lock);
342 intfdata->in_flight--;
343 spin_unlock(&intfdata->susp_lock);
344
345 for (i = 0; i < N_OUT_URB; ++i) {
346 if (portdata->out_urbs[i] == urb) {
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100347 smp_mb__before_atomic();
Matthew Garrett0d456192010-04-01 12:31:07 -0400348 clear_bit(i, &portdata->out_busy);
349 break;
350 }
351 }
352}
353
354int usb_wwan_write_room(struct tty_struct *tty)
355{
356 struct usb_serial_port *port = tty->driver_data;
357 struct usb_wwan_port_private *portdata;
358 int i;
359 int data_len = 0;
360 struct urb *this_urb;
361
362 portdata = usb_get_serial_port_data(port);
363
364 for (i = 0; i < N_OUT_URB; i++) {
365 this_urb = portdata->out_urbs[i];
366 if (this_urb && !test_bit(i, &portdata->out_busy))
367 data_len += OUT_BUFLEN;
368 }
369
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700370 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400371 return data_len;
372}
373EXPORT_SYMBOL(usb_wwan_write_room);
374
375int usb_wwan_chars_in_buffer(struct tty_struct *tty)
376{
377 struct usb_serial_port *port = tty->driver_data;
378 struct usb_wwan_port_private *portdata;
379 int i;
380 int data_len = 0;
381 struct urb *this_urb;
382
383 portdata = usb_get_serial_port_data(port);
384
385 for (i = 0; i < N_OUT_URB; i++) {
386 this_urb = portdata->out_urbs[i];
387 /* FIXME: This locking is insufficient as this_urb may
388 go unused during the test */
389 if (this_urb && test_bit(i, &portdata->out_busy))
390 data_len += this_urb->transfer_buffer_length;
391 }
Greg Kroah-Hartmana80be972012-09-13 17:41:50 -0700392 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
Matthew Garrett0d456192010-04-01 12:31:07 -0400393 return data_len;
394}
395EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
396
397int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
398{
399 struct usb_wwan_port_private *portdata;
400 struct usb_wwan_intf_private *intfdata;
401 struct usb_serial *serial = port->serial;
402 int i, err;
403 struct urb *urb;
404
405 portdata = usb_get_serial_port_data(port);
Johan Hovold37357ca2014-05-26 19:23:26 +0200406 intfdata = usb_get_serial_data(serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400407
Johan Hovold9096f1f2014-05-26 19:23:17 +0200408 if (port->interrupt_in_urb) {
409 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
410 if (err) {
Johan Hovold8bb7ec62014-05-26 19:23:31 +0200411 dev_err(&port->dev, "%s: submit int urb failed: %d\n",
Johan Hovold9096f1f2014-05-26 19:23:17 +0200412 __func__, err);
413 }
414 }
415
Matthew Garrett0d456192010-04-01 12:31:07 -0400416 /* Start reading from the IN endpoint */
417 for (i = 0; i < N_IN_URB; i++) {
418 urb = portdata->in_urbs[i];
419 if (!urb)
420 continue;
421 err = usb_submit_urb(urb, GFP_KERNEL);
422 if (err) {
Johan Hovold8bb7ec62014-05-26 19:23:31 +0200423 dev_err(&port->dev,
424 "%s: submit read urb %d failed: %d\n",
425 __func__, i, err);
Matthew Garrett0d456192010-04-01 12:31:07 -0400426 }
427 }
428
Matthew Garrett0d456192010-04-01 12:31:07 -0400429 spin_lock_irq(&intfdata->susp_lock);
Johan Hovoldc1c01802014-05-26 19:23:20 +0200430 if (++intfdata->open_ports == 1)
431 serial->interface->needs_remote_wakeup = 1;
Matthew Garrett0d456192010-04-01 12:31:07 -0400432 spin_unlock_irq(&intfdata->susp_lock);
Oliver Neukum9a91aed2011-02-10 15:33:37 +0100433 /* this balances a get in the generic USB serial code */
Matthew Garrett0d456192010-04-01 12:31:07 -0400434 usb_autopm_put_interface(serial->interface);
435
436 return 0;
437}
438EXPORT_SYMBOL(usb_wwan_open);
439
Johan Hovold79eed032014-05-26 19:23:16 +0200440static void unbusy_queued_urb(struct urb *urb,
441 struct usb_wwan_port_private *portdata)
442{
443 int i;
444
445 for (i = 0; i < N_OUT_URB; i++) {
446 if (urb == portdata->out_urbs[i]) {
447 clear_bit(i, &portdata->out_busy);
448 break;
449 }
450 }
451}
452
Matthew Garrett0d456192010-04-01 12:31:07 -0400453void usb_wwan_close(struct usb_serial_port *port)
454{
455 int i;
456 struct usb_serial *serial = port->serial;
457 struct usb_wwan_port_private *portdata;
Johan Hovold37357ca2014-05-26 19:23:26 +0200458 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
Johan Hovold79eed032014-05-26 19:23:16 +0200459 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400460
Matthew Garrett0d456192010-04-01 12:31:07 -0400461 portdata = usb_get_serial_port_data(port);
462
Johan Hovoldb0a9aa62014-05-26 19:23:32 +0200463 /*
464 * Need to take susp_lock to make sure port is not already being
Peter Hurleyd41861c2016-04-09 17:53:25 -0700465 * resumed, but no need to hold it due to initialized
Johan Hovoldb0a9aa62014-05-26 19:23:32 +0200466 */
Johan Hovolde6d144b2013-03-21 12:36:37 +0100467 spin_lock_irq(&intfdata->susp_lock);
Johan Hovoldc1c01802014-05-26 19:23:20 +0200468 if (--intfdata->open_ports == 0)
469 serial->interface->needs_remote_wakeup = 0;
Johan Hovolde6d144b2013-03-21 12:36:37 +0100470 spin_unlock_irq(&intfdata->susp_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400471
Johan Hovold79eed032014-05-26 19:23:16 +0200472 for (;;) {
473 urb = usb_get_from_anchor(&portdata->delayed);
474 if (!urb)
475 break;
476 unbusy_queued_urb(urb, portdata);
477 usb_autopm_put_interface_async(serial->interface);
478 }
479
Johan Hovolde6d144b2013-03-21 12:36:37 +0100480 for (i = 0; i < N_IN_URB; i++)
481 usb_kill_urb(portdata->in_urbs[i]);
482 for (i = 0; i < N_OUT_URB; i++)
483 usb_kill_urb(portdata->out_urbs[i]);
Johan Hovold9096f1f2014-05-26 19:23:17 +0200484 usb_kill_urb(port->interrupt_in_urb);
Johan Hovolde6d144b2013-03-21 12:36:37 +0100485
Johan Hovolde6d144b2013-03-21 12:36:37 +0100486 usb_autopm_get_interface_no_resume(serial->interface);
Matthew Garrett0d456192010-04-01 12:31:07 -0400487}
488EXPORT_SYMBOL(usb_wwan_close);
489
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200490static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
491 int endpoint,
Matthew Garrett0d456192010-04-01 12:31:07 -0400492 int dir, void *ctx, char *buf, int len,
493 void (*callback) (struct urb *))
494{
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200495 struct usb_serial *serial = port->serial;
Matthew Garrett0d456192010-04-01 12:31:07 -0400496 struct urb *urb;
497
Matthew Garrett0d456192010-04-01 12:31:07 -0400498 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
Johan Hovold10c642d2013-12-29 19:22:56 +0100499 if (!urb)
Matthew Garrett0d456192010-04-01 12:31:07 -0400500 return NULL;
Matthew Garrett0d456192010-04-01 12:31:07 -0400501
Matthew Garrett0d456192010-04-01 12:31:07 -0400502 usb_fill_bulk_urb(urb, serial->dev,
503 usb_sndbulkpipe(serial->dev, endpoint) | dir,
504 buf, len, callback, ctx);
505
506 return urb;
507}
508
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200509int usb_wwan_port_probe(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400510{
Matthew Garrett0d456192010-04-01 12:31:07 -0400511 struct usb_wwan_port_private *portdata;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200512 struct urb *urb;
Matthew Garrett0d456192010-04-01 12:31:07 -0400513 u8 *buffer;
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200514 int i;
Matthew Garrett0d456192010-04-01 12:31:07 -0400515
Johan Hovoldbd73bd82014-04-03 13:06:46 +0200516 if (!port->bulk_in_size || !port->bulk_out_size)
517 return -ENODEV;
518
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200519 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
520 if (!portdata)
521 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400522
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200523 init_usb_anchor(&portdata->delayed);
Matthew Garrett0d456192010-04-01 12:31:07 -0400524
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200525 for (i = 0; i < N_IN_URB; i++) {
526 buffer = (u8 *)__get_free_page(GFP_KERNEL);
527 if (!buffer)
528 goto bail_out_error;
529 portdata->in_buffer[i] = buffer;
Matthew Garrett0d456192010-04-01 12:31:07 -0400530
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200531 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
532 USB_DIR_IN, port,
533 buffer, IN_BUFLEN,
534 usb_wwan_indat_callback);
535 portdata->in_urbs[i] = urb;
536 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400537
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200538 for (i = 0; i < N_OUT_URB; i++) {
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200539 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
540 if (!buffer)
541 goto bail_out_error2;
542 portdata->out_buffer[i] = buffer;
543
544 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
545 USB_DIR_OUT, port,
546 buffer, OUT_BUFLEN,
547 usb_wwan_outdat_callback);
548 portdata->out_urbs[i] = urb;
549 }
550
551 usb_set_serial_port_data(port, portdata);
552
Matthew Garrett0d456192010-04-01 12:31:07 -0400553 return 0;
554
555bail_out_error2:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200556 for (i = 0; i < N_OUT_URB; i++) {
557 usb_free_urb(portdata->out_urbs[i]);
558 kfree(portdata->out_buffer[i]);
559 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400560bail_out_error:
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200561 for (i = 0; i < N_IN_URB; i++) {
562 usb_free_urb(portdata->in_urbs[i]);
563 free_page((unsigned long)portdata->in_buffer[i]);
564 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400565 kfree(portdata);
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200566
567 return -ENOMEM;
Matthew Garrett0d456192010-04-01 12:31:07 -0400568}
Johan Hovoldb8f0e822012-10-25 10:29:16 +0200569EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
Matthew Garrett0d456192010-04-01 12:31:07 -0400570
Bjørn Morka1028f02012-07-27 01:11:41 +0200571int usb_wwan_port_remove(struct usb_serial_port *port)
572{
573 int i;
574 struct usb_wwan_port_private *portdata;
575
576 portdata = usb_get_serial_port_data(port);
577 usb_set_serial_port_data(port, NULL);
578
Bjørn Morka1028f02012-07-27 01:11:41 +0200579 for (i = 0; i < N_IN_URB; i++) {
Bjørn Morka1028f02012-07-27 01:11:41 +0200580 usb_free_urb(portdata->in_urbs[i]);
581 free_page((unsigned long)portdata->in_buffer[i]);
582 }
583 for (i = 0; i < N_OUT_URB; i++) {
Bjørn Morka1028f02012-07-27 01:11:41 +0200584 usb_free_urb(portdata->out_urbs[i]);
585 kfree(portdata->out_buffer[i]);
586 }
587
Bjørn Morka1028f02012-07-27 01:11:41 +0200588 kfree(portdata);
Johan Hovold2b4acea2014-05-26 19:23:23 +0200589
Bjørn Morka1028f02012-07-27 01:11:41 +0200590 return 0;
591}
592EXPORT_SYMBOL(usb_wwan_port_remove);
593
594#ifdef CONFIG_PM
Johan Hovoldae75c942014-05-26 19:23:24 +0200595static void stop_urbs(struct usb_serial *serial)
Matthew Garrett0d456192010-04-01 12:31:07 -0400596{
597 int i, j;
598 struct usb_serial_port *port;
599 struct usb_wwan_port_private *portdata;
600
Matthew Garrett0d456192010-04-01 12:31:07 -0400601 for (i = 0; i < serial->num_ports; ++i) {
602 port = serial->port[i];
603 portdata = usb_get_serial_port_data(port);
Bjørn Mork032129c2012-07-27 01:11:43 +0200604 if (!portdata)
605 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400606 for (j = 0; j < N_IN_URB; j++)
607 usb_kill_urb(portdata->in_urbs[j]);
608 for (j = 0; j < N_OUT_URB; j++)
609 usb_kill_urb(portdata->out_urbs[j]);
Johan Hovoldae75c942014-05-26 19:23:24 +0200610 usb_kill_urb(port->interrupt_in_urb);
Matthew Garrett0d456192010-04-01 12:31:07 -0400611 }
612}
613
Matthew Garrett0d456192010-04-01 12:31:07 -0400614int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
615{
Johan Hovold37357ca2014-05-26 19:23:26 +0200616 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400617
618 spin_lock_irq(&intfdata->susp_lock);
Johan Hovold170fad92014-05-26 19:23:15 +0200619 if (PMSG_IS_AUTO(message)) {
620 if (intfdata->in_flight) {
621 spin_unlock_irq(&intfdata->susp_lock);
622 return -EBUSY;
623 }
624 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400625 intfdata->suspended = 1;
626 spin_unlock_irq(&intfdata->susp_lock);
Johan Hovold170fad92014-05-26 19:23:15 +0200627
Johan Hovoldae75c942014-05-26 19:23:24 +0200628 stop_urbs(serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400629
630 return 0;
631}
632EXPORT_SYMBOL(usb_wwan_suspend);
633
Johan Hovold3362c912014-05-26 19:23:27 +0200634/* Caller must hold susp_lock. */
635static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
Matthew Garrett0d456192010-04-01 12:31:07 -0400636{
Johan Hovold7436f412014-05-26 19:23:19 +0200637 struct usb_serial *serial = port->serial;
Johan Hovold37357ca2014-05-26 19:23:26 +0200638 struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400639 struct usb_wwan_port_private *portdata;
640 struct urb *urb;
Johan Hovold7436f412014-05-26 19:23:19 +0200641 int err_count = 0;
642 int err;
Matthew Garrett0d456192010-04-01 12:31:07 -0400643
644 portdata = usb_get_serial_port_data(port);
Johan Hovold37357ca2014-05-26 19:23:26 +0200645
Johan Hovold3362c912014-05-26 19:23:27 +0200646 for (;;) {
647 urb = usb_get_from_anchor(&portdata->delayed);
648 if (!urb)
649 break;
650
Matthew Garrett0d456192010-04-01 12:31:07 -0400651 err = usb_submit_urb(urb, GFP_ATOMIC);
Johan Hovold7436f412014-05-26 19:23:19 +0200652 if (err) {
Johan Hovold3362c912014-05-26 19:23:27 +0200653 dev_err(&port->dev, "%s: submit urb failed: %d\n",
Johan Hovold7436f412014-05-26 19:23:19 +0200654 __func__, err);
655 err_count++;
656 unbusy_queued_urb(urb, portdata);
657 usb_autopm_put_interface_async(serial->interface);
658 continue;
Oliver Neukum16871dc2011-02-10 15:33:29 +0100659 }
Johan Hovold7436f412014-05-26 19:23:19 +0200660 data->in_flight++;
Matthew Garrett0d456192010-04-01 12:31:07 -0400661 }
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200662
Johan Hovold7436f412014-05-26 19:23:19 +0200663 if (err_count)
664 return -EIO;
665
666 return 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400667}
668
669int usb_wwan_resume(struct usb_serial *serial)
670{
671 int i, j;
672 struct usb_serial_port *port;
Johan Hovold37357ca2014-05-26 19:23:26 +0200673 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
Matthew Garrett0d456192010-04-01 12:31:07 -0400674 struct usb_wwan_port_private *portdata;
675 struct urb *urb;
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200676 int err;
677 int err_count = 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400678
xiao jind9e93c02014-05-26 19:23:14 +0200679 spin_lock_irq(&intfdata->susp_lock);
Matthew Garrett0d456192010-04-01 12:31:07 -0400680 for (i = 0; i < serial->num_ports; i++) {
Matthew Garrett0d456192010-04-01 12:31:07 -0400681 port = serial->port[i];
Matthew Garrett0d456192010-04-01 12:31:07 -0400682
Peter Hurleyd41861c2016-04-09 17:53:25 -0700683 if (!tty_port_initialized(&port->port))
Matthew Garrett0d456192010-04-01 12:31:07 -0400684 continue;
Matthew Garrett0d456192010-04-01 12:31:07 -0400685
Johan Hovoldb0a9aa62014-05-26 19:23:32 +0200686 portdata = usb_get_serial_port_data(port);
687
Johan Hovold9096f1f2014-05-26 19:23:17 +0200688 if (port->interrupt_in_urb) {
689 err = usb_submit_urb(port->interrupt_in_urb,
690 GFP_ATOMIC);
691 if (err) {
692 dev_err(&port->dev,
693 "%s: submit int urb failed: %d\n",
694 __func__, err);
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200695 err_count++;
Johan Hovold9096f1f2014-05-26 19:23:17 +0200696 }
697 }
698
Johan Hovold3362c912014-05-26 19:23:27 +0200699 err = usb_wwan_submit_delayed_urbs(port);
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200700 if (err)
701 err_count++;
702
Matthew Garrett0d456192010-04-01 12:31:07 -0400703 for (j = 0; j < N_IN_URB; j++) {
704 urb = portdata->in_urbs[j];
705 err = usb_submit_urb(urb, GFP_ATOMIC);
706 if (err < 0) {
Johan Hovoldb0f9d002014-05-26 19:23:25 +0200707 dev_err(&port->dev,
708 "%s: submit read urb %d failed: %d\n",
709 __func__, i, err);
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200710 err_count++;
Matthew Garrett0d456192010-04-01 12:31:07 -0400711 }
712 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400713 }
Matthew Garrett0d456192010-04-01 12:31:07 -0400714 intfdata->suspended = 0;
715 spin_unlock_irq(&intfdata->susp_lock);
Johan Hovoldfb7ad4f2014-05-26 19:23:18 +0200716
717 if (err_count)
718 return -EIO;
719
720 return 0;
Matthew Garrett0d456192010-04-01 12:31:07 -0400721}
722EXPORT_SYMBOL(usb_wwan_resume);
723#endif
724
725MODULE_AUTHOR(DRIVER_AUTHOR);
726MODULE_DESCRIPTION(DRIVER_DESC);
Matthew Garrett0d456192010-04-01 12:31:07 -0400727MODULE_LICENSE("GPL");