xref: /openbmc/linux/drivers/usb/serial/usb_wwan.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20d456194SMatthew Garrett /*
30d456194SMatthew Garrett   USB Driver layer for GSM modems
40d456194SMatthew Garrett 
50d456194SMatthew Garrett   Copyright (C) 2005  Matthias Urlichs <smurf@smurf.noris.de>
60d456194SMatthew Garrett 
70d456194SMatthew Garrett   This driver is free software; you can redistribute it and/or modify
80d456194SMatthew Garrett   it under the terms of Version 2 of the GNU General Public License as
90d456194SMatthew Garrett   published by the Free Software Foundation.
100d456194SMatthew Garrett 
110d456194SMatthew Garrett   Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
120d456194SMatthew Garrett 
130d456194SMatthew Garrett   History: see the git log.
140d456194SMatthew Garrett 
150d456194SMatthew Garrett   Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
160d456194SMatthew Garrett 
170d456194SMatthew Garrett   This driver exists because the "normal" serial driver doesn't work too well
180d456194SMatthew Garrett   with GSM modems. Issues:
190d456194SMatthew Garrett   - data loss -- one single Receive URB is not nearly enough
200d456194SMatthew Garrett   - controlling the baud rate doesn't make sense
210d456194SMatthew Garrett */
220d456194SMatthew Garrett 
230d456194SMatthew Garrett #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
240d456194SMatthew Garrett #define DRIVER_DESC "USB Driver for GSM modems"
250d456194SMatthew Garrett 
260d456194SMatthew Garrett #include <linux/kernel.h>
270d456194SMatthew Garrett #include <linux/jiffies.h>
280d456194SMatthew Garrett #include <linux/errno.h>
290d456194SMatthew Garrett #include <linux/slab.h>
300d456194SMatthew Garrett #include <linux/tty.h>
310d456194SMatthew Garrett #include <linux/tty_flip.h>
320d456194SMatthew Garrett #include <linux/module.h>
330d456194SMatthew Garrett #include <linux/bitops.h>
3466921eddSPeter Huewe #include <linux/uaccess.h>
350d456194SMatthew Garrett #include <linux/usb.h>
360d456194SMatthew Garrett #include <linux/usb/serial.h>
3702303f73SDan Williams #include <linux/serial.h>
380d456194SMatthew Garrett #include "usb-wwan.h"
390d456194SMatthew Garrett 
40669e729fSDavid Ward /*
41669e729fSDavid Ward  * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
42669e729fSDavid Ward  * in CDC ACM.
43669e729fSDavid Ward  */
44669e729fSDavid Ward static int usb_wwan_send_setup(struct usb_serial_port *port)
45669e729fSDavid Ward {
46669e729fSDavid Ward 	struct usb_serial *serial = port->serial;
47669e729fSDavid Ward 	struct usb_wwan_port_private *portdata;
48669e729fSDavid Ward 	int val = 0;
49669e729fSDavid Ward 	int ifnum;
50669e729fSDavid Ward 	int res;
51669e729fSDavid Ward 
52669e729fSDavid Ward 	portdata = usb_get_serial_port_data(port);
53669e729fSDavid Ward 
54669e729fSDavid Ward 	if (portdata->dtr_state)
55669e729fSDavid Ward 		val |= 0x01;
56669e729fSDavid Ward 	if (portdata->rts_state)
57669e729fSDavid Ward 		val |= 0x02;
58669e729fSDavid Ward 
59669e729fSDavid Ward 	ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
60669e729fSDavid Ward 
61669e729fSDavid Ward 	res = usb_autopm_get_interface(serial->interface);
62669e729fSDavid Ward 	if (res)
63669e729fSDavid Ward 		return res;
64669e729fSDavid Ward 
65669e729fSDavid Ward 	res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
66669e729fSDavid Ward 				0x22, 0x21, val, ifnum, NULL, 0,
67669e729fSDavid Ward 				USB_CTRL_SET_TIMEOUT);
68669e729fSDavid Ward 
69669e729fSDavid Ward 	usb_autopm_put_interface(port->serial->interface);
70669e729fSDavid Ward 
71669e729fSDavid Ward 	return res;
72669e729fSDavid Ward }
73669e729fSDavid Ward 
740d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
750d456194SMatthew Garrett {
760d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
770d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
780d456194SMatthew Garrett 
7937357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
800d456194SMatthew Garrett 
81669e729fSDavid Ward 	if (!intfdata->use_send_setup)
820d456194SMatthew Garrett 		return;
830d456194SMatthew Garrett 
840d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
85b2ca6990SJohan Hovold 	/* FIXME: locking */
860d456194SMatthew Garrett 	portdata->rts_state = on;
870d456194SMatthew Garrett 	portdata->dtr_state = on;
88b2ca6990SJohan Hovold 
89669e729fSDavid Ward 	usb_wwan_send_setup(port);
900d456194SMatthew Garrett }
910d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts);
920d456194SMatthew Garrett 
9360b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty)
940d456194SMatthew Garrett {
950d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
960d456194SMatthew Garrett 	unsigned int value;
970d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
980d456194SMatthew Garrett 
990d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
1000d456194SMatthew Garrett 
1010d456194SMatthew Garrett 	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
1020d456194SMatthew Garrett 	    ((portdata->dtr_state) ? TIOCM_DTR : 0) |
1030d456194SMatthew Garrett 	    ((portdata->cts_state) ? TIOCM_CTS : 0) |
1040d456194SMatthew Garrett 	    ((portdata->dsr_state) ? TIOCM_DSR : 0) |
1050d456194SMatthew Garrett 	    ((portdata->dcd_state) ? TIOCM_CAR : 0) |
1060d456194SMatthew Garrett 	    ((portdata->ri_state) ? TIOCM_RNG : 0);
1070d456194SMatthew Garrett 
1080d456194SMatthew Garrett 	return value;
1090d456194SMatthew Garrett }
1100d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget);
1110d456194SMatthew Garrett 
11220b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty,
1130d456194SMatthew Garrett 		      unsigned int set, unsigned int clear)
1140d456194SMatthew Garrett {
1150d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
1160d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
1170d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
1180d456194SMatthew Garrett 
1190d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
12037357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
1210d456194SMatthew Garrett 
122669e729fSDavid Ward 	if (!intfdata->use_send_setup)
1230d456194SMatthew Garrett 		return -EINVAL;
1240d456194SMatthew Garrett 
1250d456194SMatthew Garrett 	/* FIXME: what locks portdata fields ? */
1260d456194SMatthew Garrett 	if (set & TIOCM_RTS)
1270d456194SMatthew Garrett 		portdata->rts_state = 1;
1280d456194SMatthew Garrett 	if (set & TIOCM_DTR)
1290d456194SMatthew Garrett 		portdata->dtr_state = 1;
1300d456194SMatthew Garrett 
1310d456194SMatthew Garrett 	if (clear & TIOCM_RTS)
1320d456194SMatthew Garrett 		portdata->rts_state = 0;
1330d456194SMatthew Garrett 	if (clear & TIOCM_DTR)
1340d456194SMatthew Garrett 		portdata->dtr_state = 0;
135669e729fSDavid Ward 	return usb_wwan_send_setup(port);
1360d456194SMatthew Garrett }
1370d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset);
1380d456194SMatthew Garrett 
13902303f73SDan Williams static int get_serial_info(struct usb_serial_port *port,
14002303f73SDan Williams 			   struct serial_struct __user *retinfo)
14102303f73SDan Williams {
14202303f73SDan Williams 	struct serial_struct tmp;
14302303f73SDan Williams 
14402303f73SDan Williams 	memset(&tmp, 0, sizeof(tmp));
145e5b1e206SGreg Kroah-Hartman 	tmp.line            = port->minor;
1461143832eSGreg Kroah-Hartman 	tmp.port            = port->port_number;
14702303f73SDan Williams 	tmp.baud_base       = tty_get_baud_rate(port->port.tty);
14802303f73SDan Williams 	tmp.close_delay	    = port->port.close_delay / 10;
14902303f73SDan Williams 	tmp.closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
15002303f73SDan Williams 				 ASYNC_CLOSING_WAIT_NONE :
15102303f73SDan Williams 				 port->port.closing_wait / 10;
15202303f73SDan Williams 
15302303f73SDan Williams 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
15402303f73SDan Williams 		return -EFAULT;
15502303f73SDan Williams 	return 0;
15602303f73SDan Williams }
15702303f73SDan Williams 
15802303f73SDan Williams static int set_serial_info(struct usb_serial_port *port,
15902303f73SDan Williams 			   struct serial_struct __user *newinfo)
16002303f73SDan Williams {
16102303f73SDan Williams 	struct serial_struct new_serial;
16202303f73SDan Williams 	unsigned int closing_wait, close_delay;
16302303f73SDan Williams 	int retval = 0;
16402303f73SDan Williams 
16502303f73SDan Williams 	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
16602303f73SDan Williams 		return -EFAULT;
16702303f73SDan Williams 
16802303f73SDan Williams 	close_delay = new_serial.close_delay * 10;
16902303f73SDan Williams 	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
17002303f73SDan Williams 			ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
17102303f73SDan Williams 
17202303f73SDan Williams 	mutex_lock(&port->port.mutex);
17302303f73SDan Williams 
17402303f73SDan Williams 	if (!capable(CAP_SYS_ADMIN)) {
17502303f73SDan Williams 		if ((close_delay != port->port.close_delay) ||
17602303f73SDan Williams 		    (closing_wait != port->port.closing_wait))
17702303f73SDan Williams 			retval = -EPERM;
17802303f73SDan Williams 		else
17902303f73SDan Williams 			retval = -EOPNOTSUPP;
18002303f73SDan Williams 	} else {
18102303f73SDan Williams 		port->port.close_delay  = close_delay;
18202303f73SDan Williams 		port->port.closing_wait = closing_wait;
18302303f73SDan Williams 	}
18402303f73SDan Williams 
18502303f73SDan Williams 	mutex_unlock(&port->port.mutex);
18602303f73SDan Williams 	return retval;
18702303f73SDan Williams }
18802303f73SDan Williams 
18900a0d0d6SAlan Cox int usb_wwan_ioctl(struct tty_struct *tty,
19002303f73SDan Williams 		   unsigned int cmd, unsigned long arg)
19102303f73SDan Williams {
19202303f73SDan Williams 	struct usb_serial_port *port = tty->driver_data;
19302303f73SDan Williams 
194a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
19502303f73SDan Williams 
19602303f73SDan Williams 	switch (cmd) {
19702303f73SDan Williams 	case TIOCGSERIAL:
19802303f73SDan Williams 		return get_serial_info(port,
19902303f73SDan Williams 				       (struct serial_struct __user *) arg);
20002303f73SDan Williams 	case TIOCSSERIAL:
20102303f73SDan Williams 		return set_serial_info(port,
20202303f73SDan Williams 				       (struct serial_struct __user *) arg);
20302303f73SDan Williams 	default:
20402303f73SDan Williams 		break;
20502303f73SDan Williams 	}
20602303f73SDan Williams 
207a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s arg not supported\n", __func__);
20802303f73SDan Williams 
20902303f73SDan Williams 	return -ENOIOCTLCMD;
21002303f73SDan Williams }
21102303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl);
21202303f73SDan Williams 
2130d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
2140d456194SMatthew Garrett 		   const unsigned char *buf, int count)
2150d456194SMatthew Garrett {
2160d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
2170d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
2180d456194SMatthew Garrett 	int i;
2190d456194SMatthew Garrett 	int left, todo;
2200d456194SMatthew Garrett 	struct urb *this_urb = NULL;	/* spurious */
2210d456194SMatthew Garrett 	int err;
2220d456194SMatthew Garrett 	unsigned long flags;
2230d456194SMatthew Garrett 
2240d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
22537357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
2260d456194SMatthew Garrett 
227a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
2280d456194SMatthew Garrett 
2290d456194SMatthew Garrett 	i = 0;
2300d456194SMatthew Garrett 	left = count;
2310d456194SMatthew Garrett 	for (i = 0; left > 0 && i < N_OUT_URB; i++) {
2320d456194SMatthew Garrett 		todo = left;
2330d456194SMatthew Garrett 		if (todo > OUT_BUFLEN)
2340d456194SMatthew Garrett 			todo = OUT_BUFLEN;
2350d456194SMatthew Garrett 
2360d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
2370d456194SMatthew Garrett 		if (test_and_set_bit(i, &portdata->out_busy)) {
2380d456194SMatthew Garrett 			if (time_before(jiffies,
2390d456194SMatthew Garrett 					portdata->tx_start_time[i] + 10 * HZ))
2400d456194SMatthew Garrett 				continue;
2410d456194SMatthew Garrett 			usb_unlink_urb(this_urb);
2420d456194SMatthew Garrett 			continue;
2430d456194SMatthew Garrett 		}
244a80be97dSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
2450d456194SMatthew Garrett 			usb_pipeendpoint(this_urb->pipe), i);
2460d456194SMatthew Garrett 
2470d456194SMatthew Garrett 		err = usb_autopm_get_interface_async(port->serial->interface);
248db090473Sxiao jin 		if (err < 0) {
249db090473Sxiao jin 			clear_bit(i, &portdata->out_busy);
2500d456194SMatthew Garrett 			break;
251db090473Sxiao jin 		}
2520d456194SMatthew Garrett 
2530d456194SMatthew Garrett 		/* send the data */
2540d456194SMatthew Garrett 		memcpy(this_urb->transfer_buffer, buf, todo);
2550d456194SMatthew Garrett 		this_urb->transfer_buffer_length = todo;
2560d456194SMatthew Garrett 
2570d456194SMatthew Garrett 		spin_lock_irqsave(&intfdata->susp_lock, flags);
2580d456194SMatthew Garrett 		if (intfdata->suspended) {
2590d456194SMatthew Garrett 			usb_anchor_urb(this_urb, &portdata->delayed);
2600d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2610d456194SMatthew Garrett 		} else {
2620d456194SMatthew Garrett 			intfdata->in_flight++;
2630d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2640d456194SMatthew Garrett 			err = usb_submit_urb(this_urb, GFP_ATOMIC);
2650d456194SMatthew Garrett 			if (err) {
2668bb7ec65SJohan Hovold 				dev_err(&port->dev,
2678bb7ec65SJohan Hovold 					"%s: submit urb %d failed: %d\n",
2688bb7ec65SJohan Hovold 					__func__, i, err);
2690d456194SMatthew Garrett 				clear_bit(i, &portdata->out_busy);
2700d456194SMatthew Garrett 				spin_lock_irqsave(&intfdata->susp_lock, flags);
2710d456194SMatthew Garrett 				intfdata->in_flight--;
2720d456194SMatthew Garrett 				spin_unlock_irqrestore(&intfdata->susp_lock,
2730d456194SMatthew Garrett 						       flags);
2743d06bf15SOliver Neukum 				usb_autopm_put_interface_async(port->serial->interface);
275433508aeSOliver Neukum 				break;
2760d456194SMatthew Garrett 			}
2770d456194SMatthew Garrett 		}
2780d456194SMatthew Garrett 
2790d456194SMatthew Garrett 		portdata->tx_start_time[i] = jiffies;
2800d456194SMatthew Garrett 		buf += todo;
2810d456194SMatthew Garrett 		left -= todo;
2820d456194SMatthew Garrett 	}
2830d456194SMatthew Garrett 
2840d456194SMatthew Garrett 	count -= left;
285a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
2860d456194SMatthew Garrett 	return count;
2870d456194SMatthew Garrett }
2880d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write);
2890d456194SMatthew Garrett 
2900d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb)
2910d456194SMatthew Garrett {
2920d456194SMatthew Garrett 	int err;
2930d456194SMatthew Garrett 	int endpoint;
2940d456194SMatthew Garrett 	struct usb_serial_port *port;
295a80be97dSGreg Kroah-Hartman 	struct device *dev;
2960d456194SMatthew Garrett 	unsigned char *data = urb->transfer_buffer;
2970d456194SMatthew Garrett 	int status = urb->status;
2980d456194SMatthew Garrett 
2990d456194SMatthew Garrett 	endpoint = usb_pipeendpoint(urb->pipe);
3000d456194SMatthew Garrett 	port = urb->context;
301a80be97dSGreg Kroah-Hartman 	dev = &port->dev;
3020d456194SMatthew Garrett 
3030d456194SMatthew Garrett 	if (status) {
304a80be97dSGreg Kroah-Hartman 		dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
3050d456194SMatthew Garrett 			__func__, status, endpoint);
3060d456194SMatthew Garrett 	} else {
3070d456194SMatthew Garrett 		if (urb->actual_length) {
30805c7cd39SJiri Slaby 			tty_insert_flip_string(&port->port, data,
30938237fd2SJiri Slaby 					urb->actual_length);
3102e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
3110d456194SMatthew Garrett 		} else
312a80be97dSGreg Kroah-Hartman 			dev_dbg(dev, "%s: empty read urb received\n", __func__);
3136c1ee66aSMatt Burtch 	}
3140d456194SMatthew Garrett 	/* Resubmit urb so we continue receiving */
3150d456194SMatthew Garrett 	err = usb_submit_urb(urb, GFP_ATOMIC);
316c9c4558fSOliver Neukum 	if (err) {
317d2958d1bSJohan Hovold 		if (err != -EPERM && err != -ENODEV) {
3186c1ee66aSMatt Burtch 			dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
3196c1ee66aSMatt Burtch 				__func__, err);
320c9c4558fSOliver Neukum 			/* busy also in error unless we are killed */
3210d456194SMatthew Garrett 			usb_mark_last_busy(port->serial->dev);
3220d456194SMatthew Garrett 		}
323c9c4558fSOliver Neukum 	} else {
324c9c4558fSOliver Neukum 		usb_mark_last_busy(port->serial->dev);
325c9c4558fSOliver Neukum 	}
326c9c4558fSOliver Neukum }
3270d456194SMatthew Garrett 
3280d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb)
3290d456194SMatthew Garrett {
3300d456194SMatthew Garrett 	struct usb_serial_port *port;
3310d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3320d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
3330d456194SMatthew Garrett 	int i;
3340d456194SMatthew Garrett 
3350d456194SMatthew Garrett 	port = urb->context;
33637357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
3370d456194SMatthew Garrett 
3380d456194SMatthew Garrett 	usb_serial_port_softint(port);
3390d456194SMatthew Garrett 	usb_autopm_put_interface_async(port->serial->interface);
3400d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3410d456194SMatthew Garrett 	spin_lock(&intfdata->susp_lock);
3420d456194SMatthew Garrett 	intfdata->in_flight--;
3430d456194SMatthew Garrett 	spin_unlock(&intfdata->susp_lock);
3440d456194SMatthew Garrett 
3450d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; ++i) {
3460d456194SMatthew Garrett 		if (portdata->out_urbs[i] == urb) {
3474e857c58SPeter Zijlstra 			smp_mb__before_atomic();
3480d456194SMatthew Garrett 			clear_bit(i, &portdata->out_busy);
3490d456194SMatthew Garrett 			break;
3500d456194SMatthew Garrett 		}
3510d456194SMatthew Garrett 	}
3520d456194SMatthew Garrett }
3530d456194SMatthew Garrett 
3540d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty)
3550d456194SMatthew Garrett {
3560d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3570d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3580d456194SMatthew Garrett 	int i;
3590d456194SMatthew Garrett 	int data_len = 0;
3600d456194SMatthew Garrett 	struct urb *this_urb;
3610d456194SMatthew Garrett 
3620d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3630d456194SMatthew Garrett 
3640d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3650d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3660d456194SMatthew Garrett 		if (this_urb && !test_bit(i, &portdata->out_busy))
3670d456194SMatthew Garrett 			data_len += OUT_BUFLEN;
3680d456194SMatthew Garrett 	}
3690d456194SMatthew Garrett 
370a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3710d456194SMatthew Garrett 	return data_len;
3720d456194SMatthew Garrett }
3730d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room);
3740d456194SMatthew Garrett 
3750d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty)
3760d456194SMatthew Garrett {
3770d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3780d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3790d456194SMatthew Garrett 	int i;
3800d456194SMatthew Garrett 	int data_len = 0;
3810d456194SMatthew Garrett 	struct urb *this_urb;
3820d456194SMatthew Garrett 
3830d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3840d456194SMatthew Garrett 
3850d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3860d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3870d456194SMatthew Garrett 		/* FIXME: This locking is insufficient as this_urb may
3880d456194SMatthew Garrett 		   go unused during the test */
3890d456194SMatthew Garrett 		if (this_urb && test_bit(i, &portdata->out_busy))
3900d456194SMatthew Garrett 			data_len += this_urb->transfer_buffer_length;
3910d456194SMatthew Garrett 	}
392a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3930d456194SMatthew Garrett 	return data_len;
3940d456194SMatthew Garrett }
3950d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
3960d456194SMatthew Garrett 
3970d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
3980d456194SMatthew Garrett {
3990d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
4000d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
4010d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
4020d456194SMatthew Garrett 	int i, err;
4030d456194SMatthew Garrett 	struct urb *urb;
4040d456194SMatthew Garrett 
4050d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
40637357ca5SJohan Hovold 	intfdata = usb_get_serial_data(serial);
4070d456194SMatthew Garrett 
4089096f1fbSJohan Hovold 	if (port->interrupt_in_urb) {
4099096f1fbSJohan Hovold 		err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
4109096f1fbSJohan Hovold 		if (err) {
4118bb7ec65SJohan Hovold 			dev_err(&port->dev, "%s: submit int urb failed: %d\n",
4129096f1fbSJohan Hovold 				__func__, err);
4139096f1fbSJohan Hovold 		}
4149096f1fbSJohan Hovold 	}
4159096f1fbSJohan Hovold 
4160d456194SMatthew Garrett 	/* Start reading from the IN endpoint */
4170d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++) {
4180d456194SMatthew Garrett 		urb = portdata->in_urbs[i];
4190d456194SMatthew Garrett 		if (!urb)
4200d456194SMatthew Garrett 			continue;
4210d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_KERNEL);
4220d456194SMatthew Garrett 		if (err) {
4238bb7ec65SJohan Hovold 			dev_err(&port->dev,
4248bb7ec65SJohan Hovold 				"%s: submit read urb %d failed: %d\n",
4258bb7ec65SJohan Hovold 				__func__, i, err);
4260d456194SMatthew Garrett 		}
4270d456194SMatthew Garrett 	}
4280d456194SMatthew Garrett 
4290d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
430c1c01803SJohan Hovold 	if (++intfdata->open_ports == 1)
431c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 1;
4320d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4339a91aedcSOliver Neukum 	/* this balances a get in the generic USB serial code */
4340d456194SMatthew Garrett 	usb_autopm_put_interface(serial->interface);
4350d456194SMatthew Garrett 
4360d456194SMatthew Garrett 	return 0;
4370d456194SMatthew Garrett }
4380d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open);
4390d456194SMatthew Garrett 
44079eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb,
44179eed03eSJohan Hovold 					struct usb_wwan_port_private *portdata)
44279eed03eSJohan Hovold {
44379eed03eSJohan Hovold 	int i;
44479eed03eSJohan Hovold 
44579eed03eSJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
44679eed03eSJohan Hovold 		if (urb == portdata->out_urbs[i]) {
44779eed03eSJohan Hovold 			clear_bit(i, &portdata->out_busy);
44879eed03eSJohan Hovold 			break;
44979eed03eSJohan Hovold 		}
45079eed03eSJohan Hovold 	}
45179eed03eSJohan Hovold }
45279eed03eSJohan Hovold 
4530d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port)
4540d456194SMatthew Garrett {
4550d456194SMatthew Garrett 	int i;
4560d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
4570d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
45837357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
45979eed03eSJohan Hovold 	struct urb *urb;
4600d456194SMatthew Garrett 
4610d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
4620d456194SMatthew Garrett 
463b0a9aa6dSJohan Hovold 	/*
464b0a9aa6dSJohan Hovold 	 * Need to take susp_lock to make sure port is not already being
465d41861caSPeter Hurley 	 * resumed, but no need to hold it due to initialized
466b0a9aa6dSJohan Hovold 	 */
4670d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
468c1c01803SJohan Hovold 	if (--intfdata->open_ports == 0)
469c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 0;
4700d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4710d456194SMatthew Garrett 
47279eed03eSJohan Hovold 	for (;;) {
47379eed03eSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
47479eed03eSJohan Hovold 		if (!urb)
47579eed03eSJohan Hovold 			break;
47679eed03eSJohan Hovold 		unbusy_queued_urb(urb, portdata);
47779eed03eSJohan Hovold 		usb_autopm_put_interface_async(serial->interface);
47879eed03eSJohan Hovold 	}
47979eed03eSJohan Hovold 
4800d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++)
4810d456194SMatthew Garrett 		usb_kill_urb(portdata->in_urbs[i]);
4820d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++)
4830d456194SMatthew Garrett 		usb_kill_urb(portdata->out_urbs[i]);
4849096f1fbSJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
485e6d144bcSJohan Hovold 
4869a91aedcSOliver Neukum 	usb_autopm_get_interface_no_resume(serial->interface);
4870d456194SMatthew Garrett }
4880d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close);
4890d456194SMatthew Garrett 
490b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
491b8f0e820SJohan Hovold 				      int endpoint,
4920d456194SMatthew Garrett 				      int dir, void *ctx, char *buf, int len,
4930d456194SMatthew Garrett 				      void (*callback) (struct urb *))
4940d456194SMatthew Garrett {
495b8f0e820SJohan Hovold 	struct usb_serial *serial = port->serial;
4960d456194SMatthew Garrett 	struct urb *urb;
4970d456194SMatthew Garrett 
4980d456194SMatthew Garrett 	urb = usb_alloc_urb(0, GFP_KERNEL);	/* No ISO */
49910c642d0SJohan Hovold 	if (!urb)
5000d456194SMatthew Garrett 		return NULL;
5010d456194SMatthew Garrett 
5020d456194SMatthew Garrett 	usb_fill_bulk_urb(urb, serial->dev,
5030d456194SMatthew Garrett 			  usb_sndbulkpipe(serial->dev, endpoint) | dir,
5040d456194SMatthew Garrett 			  buf, len, callback, ctx);
5050d456194SMatthew Garrett 
5060d456194SMatthew Garrett 	return urb;
5070d456194SMatthew Garrett }
5080d456194SMatthew Garrett 
509b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port)
5100d456194SMatthew Garrett {
5110d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
512b8f0e820SJohan Hovold 	struct urb *urb;
5130d456194SMatthew Garrett 	u8 *buffer;
514b8f0e820SJohan Hovold 	int i;
5150d456194SMatthew Garrett 
516bd73bd88SJohan Hovold 	if (!port->bulk_in_size || !port->bulk_out_size)
517bd73bd88SJohan Hovold 		return -ENODEV;
518bd73bd88SJohan Hovold 
5190d456194SMatthew Garrett 	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
520b8f0e820SJohan Hovold 	if (!portdata)
521b8f0e820SJohan Hovold 		return -ENOMEM;
522b8f0e820SJohan Hovold 
5230d456194SMatthew Garrett 	init_usb_anchor(&portdata->delayed);
5240d456194SMatthew Garrett 
525b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
5260d456194SMatthew Garrett 		buffer = (u8 *)__get_free_page(GFP_KERNEL);
5270d456194SMatthew Garrett 		if (!buffer)
5280d456194SMatthew Garrett 			goto bail_out_error;
529b8f0e820SJohan Hovold 		portdata->in_buffer[i] = buffer;
530b8f0e820SJohan Hovold 
531b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
532b8f0e820SJohan Hovold 						USB_DIR_IN, port,
533b8f0e820SJohan Hovold 						buffer, IN_BUFLEN,
534b8f0e820SJohan Hovold 						usb_wwan_indat_callback);
535b8f0e820SJohan Hovold 		portdata->in_urbs[i] = urb;
5360d456194SMatthew Garrett 	}
5370d456194SMatthew Garrett 
538b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
5390d456194SMatthew Garrett 		buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
5400d456194SMatthew Garrett 		if (!buffer)
5410d456194SMatthew Garrett 			goto bail_out_error2;
542b8f0e820SJohan Hovold 		portdata->out_buffer[i] = buffer;
543b8f0e820SJohan Hovold 
544b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
545b8f0e820SJohan Hovold 						USB_DIR_OUT, port,
546b8f0e820SJohan Hovold 						buffer, OUT_BUFLEN,
547b8f0e820SJohan Hovold 						usb_wwan_outdat_callback);
548b8f0e820SJohan Hovold 		portdata->out_urbs[i] = urb;
5490d456194SMatthew Garrett 	}
5500d456194SMatthew Garrett 
5510d456194SMatthew Garrett 	usb_set_serial_port_data(port, portdata);
5520d456194SMatthew Garrett 
5530d456194SMatthew Garrett 	return 0;
5540d456194SMatthew Garrett 
5550d456194SMatthew Garrett bail_out_error2:
556b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
557b8f0e820SJohan Hovold 		usb_free_urb(portdata->out_urbs[i]);
558b8f0e820SJohan Hovold 		kfree(portdata->out_buffer[i]);
5590d456194SMatthew Garrett 	}
560b8f0e820SJohan Hovold bail_out_error:
561b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
562b8f0e820SJohan Hovold 		usb_free_urb(portdata->in_urbs[i]);
563b8f0e820SJohan Hovold 		free_page((unsigned long)portdata->in_buffer[i]);
564b8f0e820SJohan Hovold 	}
565b8f0e820SJohan Hovold 	kfree(portdata);
566b8f0e820SJohan Hovold 
567b8f0e820SJohan Hovold 	return -ENOMEM;
568b8f0e820SJohan Hovold }
569b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
5700d456194SMatthew Garrett 
571a1028f0aSBjørn Mork int usb_wwan_port_remove(struct usb_serial_port *port)
572a1028f0aSBjørn Mork {
573a1028f0aSBjørn Mork 	int i;
574a1028f0aSBjørn Mork 	struct usb_wwan_port_private *portdata;
575a1028f0aSBjørn Mork 
576a1028f0aSBjørn Mork 	portdata = usb_get_serial_port_data(port);
577a1028f0aSBjørn Mork 	usb_set_serial_port_data(port, NULL);
578a1028f0aSBjørn Mork 
579a1028f0aSBjørn Mork 	for (i = 0; i < N_IN_URB; i++) {
580a1028f0aSBjørn Mork 		usb_free_urb(portdata->in_urbs[i]);
581a1028f0aSBjørn Mork 		free_page((unsigned long)portdata->in_buffer[i]);
582a1028f0aSBjørn Mork 	}
583a1028f0aSBjørn Mork 	for (i = 0; i < N_OUT_URB; i++) {
584a1028f0aSBjørn Mork 		usb_free_urb(portdata->out_urbs[i]);
585a1028f0aSBjørn Mork 		kfree(portdata->out_buffer[i]);
586a1028f0aSBjørn Mork 	}
587a1028f0aSBjørn Mork 
588a1028f0aSBjørn Mork 	kfree(portdata);
5892b4aceabSJohan Hovold 
590a1028f0aSBjørn Mork 	return 0;
591a1028f0aSBjørn Mork }
592a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove);
593a1028f0aSBjørn Mork 
594a1028f0aSBjørn Mork #ifdef CONFIG_PM
595ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial)
5960d456194SMatthew Garrett {
5970d456194SMatthew Garrett 	int i, j;
5980d456194SMatthew Garrett 	struct usb_serial_port *port;
5990d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6000d456194SMatthew Garrett 
6010d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; ++i) {
6020d456194SMatthew Garrett 		port = serial->port[i];
6030d456194SMatthew Garrett 		portdata = usb_get_serial_port_data(port);
604032129cbSBjørn Mork 		if (!portdata)
605032129cbSBjørn Mork 			continue;
6060d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++)
6070d456194SMatthew Garrett 			usb_kill_urb(portdata->in_urbs[j]);
6080d456194SMatthew Garrett 		for (j = 0; j < N_OUT_URB; j++)
6090d456194SMatthew Garrett 			usb_kill_urb(portdata->out_urbs[j]);
610ae75c940SJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
6110d456194SMatthew Garrett 	}
6120d456194SMatthew Garrett }
6130d456194SMatthew Garrett 
6140d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
6150d456194SMatthew Garrett {
61637357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6170d456194SMatthew Garrett 
6180d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
619170fad9eSJohan Hovold 	if (PMSG_IS_AUTO(message)) {
620170fad9eSJohan Hovold 		if (intfdata->in_flight) {
6210d456194SMatthew Garrett 			spin_unlock_irq(&intfdata->susp_lock);
6220d456194SMatthew Garrett 			return -EBUSY;
6230d456194SMatthew Garrett 		}
624170fad9eSJohan Hovold 	}
6250d456194SMatthew Garrett 	intfdata->suspended = 1;
6260d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
627170fad9eSJohan Hovold 
628ae75c940SJohan Hovold 	stop_urbs(serial);
6290d456194SMatthew Garrett 
6300d456194SMatthew Garrett 	return 0;
6310d456194SMatthew Garrett }
6320d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend);
6330d456194SMatthew Garrett 
6343362c91cSJohan Hovold /* Caller must hold susp_lock. */
6353362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
6360d456194SMatthew Garrett {
6377436f412SJohan Hovold 	struct usb_serial *serial = port->serial;
63837357ca5SJohan Hovold 	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
6390d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6400d456194SMatthew Garrett 	struct urb *urb;
6417436f412SJohan Hovold 	int err_count = 0;
6427436f412SJohan Hovold 	int err;
6430d456194SMatthew Garrett 
6440d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
64537357ca5SJohan Hovold 
6463362c91cSJohan Hovold 	for (;;) {
6473362c91cSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
6483362c91cSJohan Hovold 		if (!urb)
6493362c91cSJohan Hovold 			break;
6503362c91cSJohan Hovold 
6510d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_ATOMIC);
6527436f412SJohan Hovold 		if (err) {
6533362c91cSJohan Hovold 			dev_err(&port->dev, "%s: submit urb failed: %d\n",
6547436f412SJohan Hovold 					__func__, err);
6557436f412SJohan Hovold 			err_count++;
65616871dcaSOliver Neukum 			unbusy_queued_urb(urb, portdata);
6577436f412SJohan Hovold 			usb_autopm_put_interface_async(serial->interface);
6587436f412SJohan Hovold 			continue;
65916871dcaSOliver Neukum 		}
6607436f412SJohan Hovold 		data->in_flight++;
6610d456194SMatthew Garrett 	}
662fb7ad4f9SJohan Hovold 
6637436f412SJohan Hovold 	if (err_count)
6647436f412SJohan Hovold 		return -EIO;
6657436f412SJohan Hovold 
6667436f412SJohan Hovold 	return 0;
6670d456194SMatthew Garrett }
6680d456194SMatthew Garrett 
6690d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial)
6700d456194SMatthew Garrett {
6710d456194SMatthew Garrett 	int i, j;
6720d456194SMatthew Garrett 	struct usb_serial_port *port;
67337357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6740d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6750d456194SMatthew Garrett 	struct urb *urb;
676fb7ad4f9SJohan Hovold 	int err;
677fb7ad4f9SJohan Hovold 	int err_count = 0;
6780d456194SMatthew Garrett 
679d9e93c08Sxiao jin 	spin_lock_irq(&intfdata->susp_lock);
6800d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; i++) {
6810d456194SMatthew Garrett 		port = serial->port[i];
6820d456194SMatthew Garrett 
683d41861caSPeter Hurley 		if (!tty_port_initialized(&port->port))
6840d456194SMatthew Garrett 			continue;
6850d456194SMatthew Garrett 
686b0a9aa6dSJohan Hovold 		portdata = usb_get_serial_port_data(port);
687b0a9aa6dSJohan Hovold 
6889096f1fbSJohan Hovold 		if (port->interrupt_in_urb) {
6899096f1fbSJohan Hovold 			err = usb_submit_urb(port->interrupt_in_urb,
6909096f1fbSJohan Hovold 					GFP_ATOMIC);
6919096f1fbSJohan Hovold 			if (err) {
6929096f1fbSJohan Hovold 				dev_err(&port->dev,
6939096f1fbSJohan Hovold 					"%s: submit int urb failed: %d\n",
6949096f1fbSJohan Hovold 					__func__, err);
695fb7ad4f9SJohan Hovold 				err_count++;
6969096f1fbSJohan Hovold 			}
6979096f1fbSJohan Hovold 		}
6989096f1fbSJohan Hovold 
6993362c91cSJohan Hovold 		err = usb_wwan_submit_delayed_urbs(port);
700fb7ad4f9SJohan Hovold 		if (err)
701fb7ad4f9SJohan Hovold 			err_count++;
702fb7ad4f9SJohan Hovold 
7030d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++) {
7040d456194SMatthew Garrett 			urb = portdata->in_urbs[j];
7050d456194SMatthew Garrett 			err = usb_submit_urb(urb, GFP_ATOMIC);
7060d456194SMatthew Garrett 			if (err < 0) {
707b0f9d003SJohan Hovold 				dev_err(&port->dev,
708b0f9d003SJohan Hovold 					"%s: submit read urb %d failed: %d\n",
709b0f9d003SJohan Hovold 					__func__, i, err);
710fb7ad4f9SJohan Hovold 				err_count++;
7110d456194SMatthew Garrett 			}
7120d456194SMatthew Garrett 		}
7130d456194SMatthew Garrett 	}
7140d456194SMatthew Garrett 	intfdata->suspended = 0;
7150d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
716fb7ad4f9SJohan Hovold 
717fb7ad4f9SJohan Hovold 	if (err_count)
718fb7ad4f9SJohan Hovold 		return -EIO;
719fb7ad4f9SJohan Hovold 
720fb7ad4f9SJohan Hovold 	return 0;
7210d456194SMatthew Garrett }
7220d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume);
7230d456194SMatthew Garrett #endif
7240d456194SMatthew Garrett 
7250d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR);
7260d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC);
7270d456194SMatthew Garrett MODULE_LICENSE("GPL");
728