xref: /openbmc/linux/drivers/usb/serial/usb_wwan.c (revision a323f94611aac0f68b710048ae53c73a53b5c7b5)
15fd54aceSGreg 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   Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
80d456194SMatthew Garrett 
90d456194SMatthew Garrett   History: see the git log.
100d456194SMatthew Garrett 
110d456194SMatthew Garrett   Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
120d456194SMatthew Garrett 
130d456194SMatthew Garrett   This driver exists because the "normal" serial driver doesn't work too well
140d456194SMatthew Garrett   with GSM modems. Issues:
150d456194SMatthew Garrett   - data loss -- one single Receive URB is not nearly enough
160d456194SMatthew Garrett   - controlling the baud rate doesn't make sense
170d456194SMatthew Garrett */
180d456194SMatthew Garrett 
190d456194SMatthew Garrett #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
200d456194SMatthew Garrett #define DRIVER_DESC "USB Driver for GSM modems"
210d456194SMatthew Garrett 
220d456194SMatthew Garrett #include <linux/kernel.h>
230d456194SMatthew Garrett #include <linux/jiffies.h>
240d456194SMatthew Garrett #include <linux/errno.h>
250d456194SMatthew Garrett #include <linux/slab.h>
260d456194SMatthew Garrett #include <linux/tty.h>
270d456194SMatthew Garrett #include <linux/tty_flip.h>
280d456194SMatthew Garrett #include <linux/module.h>
290d456194SMatthew Garrett #include <linux/bitops.h>
3066921eddSPeter Huewe #include <linux/uaccess.h>
310d456194SMatthew Garrett #include <linux/usb.h>
320d456194SMatthew Garrett #include <linux/usb/serial.h>
3302303f73SDan Williams #include <linux/serial.h>
340d456194SMatthew Garrett #include "usb-wwan.h"
350d456194SMatthew Garrett 
36669e729fSDavid Ward /*
37669e729fSDavid Ward  * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
38669e729fSDavid Ward  * in CDC ACM.
39669e729fSDavid Ward  */
40669e729fSDavid Ward static int usb_wwan_send_setup(struct usb_serial_port *port)
41669e729fSDavid Ward {
42669e729fSDavid Ward 	struct usb_serial *serial = port->serial;
43669e729fSDavid Ward 	struct usb_wwan_port_private *portdata;
44669e729fSDavid Ward 	int val = 0;
45669e729fSDavid Ward 	int ifnum;
46669e729fSDavid Ward 	int res;
47669e729fSDavid Ward 
48669e729fSDavid Ward 	portdata = usb_get_serial_port_data(port);
49669e729fSDavid Ward 
50669e729fSDavid Ward 	if (portdata->dtr_state)
51669e729fSDavid Ward 		val |= 0x01;
52669e729fSDavid Ward 	if (portdata->rts_state)
53669e729fSDavid Ward 		val |= 0x02;
54669e729fSDavid Ward 
55669e729fSDavid Ward 	ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
56669e729fSDavid Ward 
57669e729fSDavid Ward 	res = usb_autopm_get_interface(serial->interface);
58669e729fSDavid Ward 	if (res)
59669e729fSDavid Ward 		return res;
60669e729fSDavid Ward 
61669e729fSDavid Ward 	res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
62669e729fSDavid Ward 				0x22, 0x21, val, ifnum, NULL, 0,
63669e729fSDavid Ward 				USB_CTRL_SET_TIMEOUT);
64669e729fSDavid Ward 
65669e729fSDavid Ward 	usb_autopm_put_interface(port->serial->interface);
66669e729fSDavid Ward 
67669e729fSDavid Ward 	return res;
68669e729fSDavid Ward }
69669e729fSDavid Ward 
700d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
710d456194SMatthew Garrett {
720d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
730d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
740d456194SMatthew Garrett 
7537357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
760d456194SMatthew Garrett 
77669e729fSDavid Ward 	if (!intfdata->use_send_setup)
780d456194SMatthew Garrett 		return;
790d456194SMatthew Garrett 
800d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
81b2ca6990SJohan Hovold 	/* FIXME: locking */
820d456194SMatthew Garrett 	portdata->rts_state = on;
830d456194SMatthew Garrett 	portdata->dtr_state = on;
84b2ca6990SJohan Hovold 
85669e729fSDavid Ward 	usb_wwan_send_setup(port);
860d456194SMatthew Garrett }
870d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts);
880d456194SMatthew Garrett 
8960b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty)
900d456194SMatthew Garrett {
910d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
920d456194SMatthew Garrett 	unsigned int value;
930d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
940d456194SMatthew Garrett 
950d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
960d456194SMatthew Garrett 
970d456194SMatthew Garrett 	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
980d456194SMatthew Garrett 	    ((portdata->dtr_state) ? TIOCM_DTR : 0) |
990d456194SMatthew Garrett 	    ((portdata->cts_state) ? TIOCM_CTS : 0) |
1000d456194SMatthew Garrett 	    ((portdata->dsr_state) ? TIOCM_DSR : 0) |
1010d456194SMatthew Garrett 	    ((portdata->dcd_state) ? TIOCM_CAR : 0) |
1020d456194SMatthew Garrett 	    ((portdata->ri_state) ? TIOCM_RNG : 0);
1030d456194SMatthew Garrett 
1040d456194SMatthew Garrett 	return value;
1050d456194SMatthew Garrett }
1060d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget);
1070d456194SMatthew Garrett 
10820b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty,
1090d456194SMatthew Garrett 		      unsigned int set, unsigned int clear)
1100d456194SMatthew Garrett {
1110d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
1120d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
1130d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
1140d456194SMatthew Garrett 
1150d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
11637357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
1170d456194SMatthew Garrett 
118669e729fSDavid Ward 	if (!intfdata->use_send_setup)
1190d456194SMatthew Garrett 		return -EINVAL;
1200d456194SMatthew Garrett 
1210d456194SMatthew Garrett 	/* FIXME: what locks portdata fields ? */
1220d456194SMatthew Garrett 	if (set & TIOCM_RTS)
1230d456194SMatthew Garrett 		portdata->rts_state = 1;
1240d456194SMatthew Garrett 	if (set & TIOCM_DTR)
1250d456194SMatthew Garrett 		portdata->dtr_state = 1;
1260d456194SMatthew Garrett 
1270d456194SMatthew Garrett 	if (clear & TIOCM_RTS)
1280d456194SMatthew Garrett 		portdata->rts_state = 0;
1290d456194SMatthew Garrett 	if (clear & TIOCM_DTR)
1300d456194SMatthew Garrett 		portdata->dtr_state = 0;
131669e729fSDavid Ward 	return usb_wwan_send_setup(port);
1320d456194SMatthew Garrett }
1330d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset);
1340d456194SMatthew Garrett 
13502303f73SDan Williams static int get_serial_info(struct usb_serial_port *port,
13602303f73SDan Williams 			   struct serial_struct __user *retinfo)
13702303f73SDan Williams {
13802303f73SDan Williams 	struct serial_struct tmp;
13902303f73SDan Williams 
14002303f73SDan Williams 	memset(&tmp, 0, sizeof(tmp));
141e5b1e206SGreg Kroah-Hartman 	tmp.line            = port->minor;
1421143832eSGreg Kroah-Hartman 	tmp.port            = port->port_number;
14302303f73SDan Williams 	tmp.baud_base       = tty_get_baud_rate(port->port.tty);
14402303f73SDan Williams 	tmp.close_delay	    = port->port.close_delay / 10;
14502303f73SDan Williams 	tmp.closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
14602303f73SDan Williams 				 ASYNC_CLOSING_WAIT_NONE :
14702303f73SDan Williams 				 port->port.closing_wait / 10;
14802303f73SDan Williams 
14902303f73SDan Williams 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
15002303f73SDan Williams 		return -EFAULT;
15102303f73SDan Williams 	return 0;
15202303f73SDan Williams }
15302303f73SDan Williams 
15402303f73SDan Williams static int set_serial_info(struct usb_serial_port *port,
15502303f73SDan Williams 			   struct serial_struct __user *newinfo)
15602303f73SDan Williams {
15702303f73SDan Williams 	struct serial_struct new_serial;
15802303f73SDan Williams 	unsigned int closing_wait, close_delay;
15902303f73SDan Williams 	int retval = 0;
16002303f73SDan Williams 
16102303f73SDan Williams 	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
16202303f73SDan Williams 		return -EFAULT;
16302303f73SDan Williams 
16402303f73SDan Williams 	close_delay = new_serial.close_delay * 10;
16502303f73SDan Williams 	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
16602303f73SDan Williams 			ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
16702303f73SDan Williams 
16802303f73SDan Williams 	mutex_lock(&port->port.mutex);
16902303f73SDan Williams 
17002303f73SDan Williams 	if (!capable(CAP_SYS_ADMIN)) {
17102303f73SDan Williams 		if ((close_delay != port->port.close_delay) ||
17202303f73SDan Williams 		    (closing_wait != port->port.closing_wait))
17302303f73SDan Williams 			retval = -EPERM;
17402303f73SDan Williams 		else
17502303f73SDan Williams 			retval = -EOPNOTSUPP;
17602303f73SDan Williams 	} else {
17702303f73SDan Williams 		port->port.close_delay  = close_delay;
17802303f73SDan Williams 		port->port.closing_wait = closing_wait;
17902303f73SDan Williams 	}
18002303f73SDan Williams 
18102303f73SDan Williams 	mutex_unlock(&port->port.mutex);
18202303f73SDan Williams 	return retval;
18302303f73SDan Williams }
18402303f73SDan Williams 
18500a0d0d6SAlan Cox int usb_wwan_ioctl(struct tty_struct *tty,
18602303f73SDan Williams 		   unsigned int cmd, unsigned long arg)
18702303f73SDan Williams {
18802303f73SDan Williams 	struct usb_serial_port *port = tty->driver_data;
18902303f73SDan Williams 
190a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
19102303f73SDan Williams 
19202303f73SDan Williams 	switch (cmd) {
19302303f73SDan Williams 	case TIOCGSERIAL:
19402303f73SDan Williams 		return get_serial_info(port,
19502303f73SDan Williams 				       (struct serial_struct __user *) arg);
19602303f73SDan Williams 	case TIOCSSERIAL:
19702303f73SDan Williams 		return set_serial_info(port,
19802303f73SDan Williams 				       (struct serial_struct __user *) arg);
19902303f73SDan Williams 	default:
20002303f73SDan Williams 		break;
20102303f73SDan Williams 	}
20202303f73SDan Williams 
203a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s arg not supported\n", __func__);
20402303f73SDan Williams 
20502303f73SDan Williams 	return -ENOIOCTLCMD;
20602303f73SDan Williams }
20702303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl);
20802303f73SDan Williams 
2090d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
2100d456194SMatthew Garrett 		   const unsigned char *buf, int count)
2110d456194SMatthew Garrett {
2120d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
2130d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
2140d456194SMatthew Garrett 	int i;
2150d456194SMatthew Garrett 	int left, todo;
2160d456194SMatthew Garrett 	struct urb *this_urb = NULL;	/* spurious */
2170d456194SMatthew Garrett 	int err;
2180d456194SMatthew Garrett 	unsigned long flags;
2190d456194SMatthew Garrett 
2200d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
22137357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
2220d456194SMatthew Garrett 
223a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
2240d456194SMatthew Garrett 
2250d456194SMatthew Garrett 	i = 0;
2260d456194SMatthew Garrett 	left = count;
2270d456194SMatthew Garrett 	for (i = 0; left > 0 && i < N_OUT_URB; i++) {
2280d456194SMatthew Garrett 		todo = left;
2290d456194SMatthew Garrett 		if (todo > OUT_BUFLEN)
2300d456194SMatthew Garrett 			todo = OUT_BUFLEN;
2310d456194SMatthew Garrett 
2320d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
2330d456194SMatthew Garrett 		if (test_and_set_bit(i, &portdata->out_busy)) {
2340d456194SMatthew Garrett 			if (time_before(jiffies,
2350d456194SMatthew Garrett 					portdata->tx_start_time[i] + 10 * HZ))
2360d456194SMatthew Garrett 				continue;
2370d456194SMatthew Garrett 			usb_unlink_urb(this_urb);
2380d456194SMatthew Garrett 			continue;
2390d456194SMatthew Garrett 		}
240a80be97dSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
2410d456194SMatthew Garrett 			usb_pipeendpoint(this_urb->pipe), i);
2420d456194SMatthew Garrett 
2430d456194SMatthew Garrett 		err = usb_autopm_get_interface_async(port->serial->interface);
244db090473Sxiao jin 		if (err < 0) {
245db090473Sxiao jin 			clear_bit(i, &portdata->out_busy);
2460d456194SMatthew Garrett 			break;
247db090473Sxiao jin 		}
2480d456194SMatthew Garrett 
2490d456194SMatthew Garrett 		/* send the data */
2500d456194SMatthew Garrett 		memcpy(this_urb->transfer_buffer, buf, todo);
2510d456194SMatthew Garrett 		this_urb->transfer_buffer_length = todo;
2520d456194SMatthew Garrett 
2530d456194SMatthew Garrett 		spin_lock_irqsave(&intfdata->susp_lock, flags);
2540d456194SMatthew Garrett 		if (intfdata->suspended) {
2550d456194SMatthew Garrett 			usb_anchor_urb(this_urb, &portdata->delayed);
2560d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2570d456194SMatthew Garrett 		} else {
2580d456194SMatthew Garrett 			intfdata->in_flight++;
2590d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2600d456194SMatthew Garrett 			err = usb_submit_urb(this_urb, GFP_ATOMIC);
2610d456194SMatthew Garrett 			if (err) {
2628bb7ec65SJohan Hovold 				dev_err(&port->dev,
2638bb7ec65SJohan Hovold 					"%s: submit urb %d failed: %d\n",
2648bb7ec65SJohan Hovold 					__func__, i, err);
2650d456194SMatthew Garrett 				clear_bit(i, &portdata->out_busy);
2660d456194SMatthew Garrett 				spin_lock_irqsave(&intfdata->susp_lock, flags);
2670d456194SMatthew Garrett 				intfdata->in_flight--;
2680d456194SMatthew Garrett 				spin_unlock_irqrestore(&intfdata->susp_lock,
2690d456194SMatthew Garrett 						       flags);
2703d06bf15SOliver Neukum 				usb_autopm_put_interface_async(port->serial->interface);
271433508aeSOliver Neukum 				break;
2720d456194SMatthew Garrett 			}
2730d456194SMatthew Garrett 		}
2740d456194SMatthew Garrett 
2750d456194SMatthew Garrett 		portdata->tx_start_time[i] = jiffies;
2760d456194SMatthew Garrett 		buf += todo;
2770d456194SMatthew Garrett 		left -= todo;
2780d456194SMatthew Garrett 	}
2790d456194SMatthew Garrett 
2800d456194SMatthew Garrett 	count -= left;
281a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
2820d456194SMatthew Garrett 	return count;
2830d456194SMatthew Garrett }
2840d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write);
2850d456194SMatthew Garrett 
2860d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb)
2870d456194SMatthew Garrett {
2880d456194SMatthew Garrett 	int err;
2890d456194SMatthew Garrett 	int endpoint;
2900d456194SMatthew Garrett 	struct usb_serial_port *port;
291a80be97dSGreg Kroah-Hartman 	struct device *dev;
2920d456194SMatthew Garrett 	unsigned char *data = urb->transfer_buffer;
2930d456194SMatthew Garrett 	int status = urb->status;
2940d456194SMatthew Garrett 
2950d456194SMatthew Garrett 	endpoint = usb_pipeendpoint(urb->pipe);
2960d456194SMatthew Garrett 	port = urb->context;
297a80be97dSGreg Kroah-Hartman 	dev = &port->dev;
2980d456194SMatthew Garrett 
2990d456194SMatthew Garrett 	if (status) {
300a80be97dSGreg Kroah-Hartman 		dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
3010d456194SMatthew Garrett 			__func__, status, endpoint);
3020d456194SMatthew Garrett 	} else {
3030d456194SMatthew Garrett 		if (urb->actual_length) {
30405c7cd39SJiri Slaby 			tty_insert_flip_string(&port->port, data,
30538237fd2SJiri Slaby 					urb->actual_length);
3062e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
3070d456194SMatthew Garrett 		} else
308a80be97dSGreg Kroah-Hartman 			dev_dbg(dev, "%s: empty read urb received\n", __func__);
3096c1ee66aSMatt Burtch 	}
3100d456194SMatthew Garrett 	/* Resubmit urb so we continue receiving */
3110d456194SMatthew Garrett 	err = usb_submit_urb(urb, GFP_ATOMIC);
312c9c4558fSOliver Neukum 	if (err) {
313d2958d1bSJohan Hovold 		if (err != -EPERM && err != -ENODEV) {
3146c1ee66aSMatt Burtch 			dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
3156c1ee66aSMatt Burtch 				__func__, err);
316c9c4558fSOliver Neukum 			/* busy also in error unless we are killed */
3170d456194SMatthew Garrett 			usb_mark_last_busy(port->serial->dev);
3180d456194SMatthew Garrett 		}
319c9c4558fSOliver Neukum 	} else {
320c9c4558fSOliver Neukum 		usb_mark_last_busy(port->serial->dev);
321c9c4558fSOliver Neukum 	}
322c9c4558fSOliver Neukum }
3230d456194SMatthew Garrett 
3240d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb)
3250d456194SMatthew Garrett {
3260d456194SMatthew Garrett 	struct usb_serial_port *port;
3270d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3280d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
329*a323f946SJohn Ogness 	unsigned long flags;
3300d456194SMatthew Garrett 	int i;
3310d456194SMatthew Garrett 
3320d456194SMatthew Garrett 	port = urb->context;
33337357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
3340d456194SMatthew Garrett 
3350d456194SMatthew Garrett 	usb_serial_port_softint(port);
3360d456194SMatthew Garrett 	usb_autopm_put_interface_async(port->serial->interface);
3370d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
338*a323f946SJohn Ogness 	spin_lock_irqsave(&intfdata->susp_lock, flags);
3390d456194SMatthew Garrett 	intfdata->in_flight--;
340*a323f946SJohn Ogness 	spin_unlock_irqrestore(&intfdata->susp_lock, flags);
3410d456194SMatthew Garrett 
3420d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; ++i) {
3430d456194SMatthew Garrett 		if (portdata->out_urbs[i] == urb) {
3444e857c58SPeter Zijlstra 			smp_mb__before_atomic();
3450d456194SMatthew Garrett 			clear_bit(i, &portdata->out_busy);
3460d456194SMatthew Garrett 			break;
3470d456194SMatthew Garrett 		}
3480d456194SMatthew Garrett 	}
3490d456194SMatthew Garrett }
3500d456194SMatthew Garrett 
3510d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty)
3520d456194SMatthew Garrett {
3530d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3540d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3550d456194SMatthew Garrett 	int i;
3560d456194SMatthew Garrett 	int data_len = 0;
3570d456194SMatthew Garrett 	struct urb *this_urb;
3580d456194SMatthew Garrett 
3590d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3600d456194SMatthew Garrett 
3610d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3620d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3630d456194SMatthew Garrett 		if (this_urb && !test_bit(i, &portdata->out_busy))
3640d456194SMatthew Garrett 			data_len += OUT_BUFLEN;
3650d456194SMatthew Garrett 	}
3660d456194SMatthew Garrett 
367a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3680d456194SMatthew Garrett 	return data_len;
3690d456194SMatthew Garrett }
3700d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room);
3710d456194SMatthew Garrett 
3720d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty)
3730d456194SMatthew Garrett {
3740d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3750d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3760d456194SMatthew Garrett 	int i;
3770d456194SMatthew Garrett 	int data_len = 0;
3780d456194SMatthew Garrett 	struct urb *this_urb;
3790d456194SMatthew Garrett 
3800d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3810d456194SMatthew Garrett 
3820d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3830d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3840d456194SMatthew Garrett 		/* FIXME: This locking is insufficient as this_urb may
3850d456194SMatthew Garrett 		   go unused during the test */
3860d456194SMatthew Garrett 		if (this_urb && test_bit(i, &portdata->out_busy))
3870d456194SMatthew Garrett 			data_len += this_urb->transfer_buffer_length;
3880d456194SMatthew Garrett 	}
389a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3900d456194SMatthew Garrett 	return data_len;
3910d456194SMatthew Garrett }
3920d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
3930d456194SMatthew Garrett 
3940d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
3950d456194SMatthew Garrett {
3960d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3970d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
3980d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
3990d456194SMatthew Garrett 	int i, err;
4000d456194SMatthew Garrett 	struct urb *urb;
4010d456194SMatthew Garrett 
4020d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
40337357ca5SJohan Hovold 	intfdata = usb_get_serial_data(serial);
4040d456194SMatthew Garrett 
4059096f1fbSJohan Hovold 	if (port->interrupt_in_urb) {
4069096f1fbSJohan Hovold 		err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
4079096f1fbSJohan Hovold 		if (err) {
4088bb7ec65SJohan Hovold 			dev_err(&port->dev, "%s: submit int urb failed: %d\n",
4099096f1fbSJohan Hovold 				__func__, err);
4109096f1fbSJohan Hovold 		}
4119096f1fbSJohan Hovold 	}
4129096f1fbSJohan Hovold 
4130d456194SMatthew Garrett 	/* Start reading from the IN endpoint */
4140d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++) {
4150d456194SMatthew Garrett 		urb = portdata->in_urbs[i];
4160d456194SMatthew Garrett 		if (!urb)
4170d456194SMatthew Garrett 			continue;
4180d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_KERNEL);
4190d456194SMatthew Garrett 		if (err) {
4208bb7ec65SJohan Hovold 			dev_err(&port->dev,
4218bb7ec65SJohan Hovold 				"%s: submit read urb %d failed: %d\n",
4228bb7ec65SJohan Hovold 				__func__, i, err);
4230d456194SMatthew Garrett 		}
4240d456194SMatthew Garrett 	}
4250d456194SMatthew Garrett 
4260d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
427c1c01803SJohan Hovold 	if (++intfdata->open_ports == 1)
428c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 1;
4290d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4309a91aedcSOliver Neukum 	/* this balances a get in the generic USB serial code */
4310d456194SMatthew Garrett 	usb_autopm_put_interface(serial->interface);
4320d456194SMatthew Garrett 
4330d456194SMatthew Garrett 	return 0;
4340d456194SMatthew Garrett }
4350d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open);
4360d456194SMatthew Garrett 
43779eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb,
43879eed03eSJohan Hovold 					struct usb_wwan_port_private *portdata)
43979eed03eSJohan Hovold {
44079eed03eSJohan Hovold 	int i;
44179eed03eSJohan Hovold 
44279eed03eSJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
44379eed03eSJohan Hovold 		if (urb == portdata->out_urbs[i]) {
44479eed03eSJohan Hovold 			clear_bit(i, &portdata->out_busy);
44579eed03eSJohan Hovold 			break;
44679eed03eSJohan Hovold 		}
44779eed03eSJohan Hovold 	}
44879eed03eSJohan Hovold }
44979eed03eSJohan Hovold 
4500d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port)
4510d456194SMatthew Garrett {
4520d456194SMatthew Garrett 	int i;
4530d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
4540d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
45537357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
45679eed03eSJohan Hovold 	struct urb *urb;
4570d456194SMatthew Garrett 
4580d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
4590d456194SMatthew Garrett 
460b0a9aa6dSJohan Hovold 	/*
461b0a9aa6dSJohan Hovold 	 * Need to take susp_lock to make sure port is not already being
462d41861caSPeter Hurley 	 * resumed, but no need to hold it due to initialized
463b0a9aa6dSJohan Hovold 	 */
4640d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
465c1c01803SJohan Hovold 	if (--intfdata->open_ports == 0)
466c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 0;
4670d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4680d456194SMatthew Garrett 
46979eed03eSJohan Hovold 	for (;;) {
47079eed03eSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
47179eed03eSJohan Hovold 		if (!urb)
47279eed03eSJohan Hovold 			break;
47379eed03eSJohan Hovold 		unbusy_queued_urb(urb, portdata);
47479eed03eSJohan Hovold 		usb_autopm_put_interface_async(serial->interface);
47579eed03eSJohan Hovold 	}
47679eed03eSJohan Hovold 
4770d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++)
4780d456194SMatthew Garrett 		usb_kill_urb(portdata->in_urbs[i]);
4790d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++)
4800d456194SMatthew Garrett 		usb_kill_urb(portdata->out_urbs[i]);
4819096f1fbSJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
482e6d144bcSJohan Hovold 
4839a91aedcSOliver Neukum 	usb_autopm_get_interface_no_resume(serial->interface);
4840d456194SMatthew Garrett }
4850d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close);
4860d456194SMatthew Garrett 
487b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
488b8f0e820SJohan Hovold 				      int endpoint,
4890d456194SMatthew Garrett 				      int dir, void *ctx, char *buf, int len,
4900d456194SMatthew Garrett 				      void (*callback) (struct urb *))
4910d456194SMatthew Garrett {
492b8f0e820SJohan Hovold 	struct usb_serial *serial = port->serial;
4930d456194SMatthew Garrett 	struct urb *urb;
4940d456194SMatthew Garrett 
4950d456194SMatthew Garrett 	urb = usb_alloc_urb(0, GFP_KERNEL);	/* No ISO */
49610c642d0SJohan Hovold 	if (!urb)
4970d456194SMatthew Garrett 		return NULL;
4980d456194SMatthew Garrett 
4990d456194SMatthew Garrett 	usb_fill_bulk_urb(urb, serial->dev,
5000d456194SMatthew Garrett 			  usb_sndbulkpipe(serial->dev, endpoint) | dir,
5010d456194SMatthew Garrett 			  buf, len, callback, ctx);
5020d456194SMatthew Garrett 
5030d456194SMatthew Garrett 	return urb;
5040d456194SMatthew Garrett }
5050d456194SMatthew Garrett 
506b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port)
5070d456194SMatthew Garrett {
5080d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
509b8f0e820SJohan Hovold 	struct urb *urb;
5100d456194SMatthew Garrett 	u8 *buffer;
511b8f0e820SJohan Hovold 	int i;
5120d456194SMatthew Garrett 
513bd73bd88SJohan Hovold 	if (!port->bulk_in_size || !port->bulk_out_size)
514bd73bd88SJohan Hovold 		return -ENODEV;
515bd73bd88SJohan Hovold 
5160d456194SMatthew Garrett 	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
517b8f0e820SJohan Hovold 	if (!portdata)
518b8f0e820SJohan Hovold 		return -ENOMEM;
519b8f0e820SJohan Hovold 
5200d456194SMatthew Garrett 	init_usb_anchor(&portdata->delayed);
5210d456194SMatthew Garrett 
522b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
5230d456194SMatthew Garrett 		buffer = (u8 *)__get_free_page(GFP_KERNEL);
5240d456194SMatthew Garrett 		if (!buffer)
5250d456194SMatthew Garrett 			goto bail_out_error;
526b8f0e820SJohan Hovold 		portdata->in_buffer[i] = buffer;
527b8f0e820SJohan Hovold 
528b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
529b8f0e820SJohan Hovold 						USB_DIR_IN, port,
530b8f0e820SJohan Hovold 						buffer, IN_BUFLEN,
531b8f0e820SJohan Hovold 						usb_wwan_indat_callback);
532b8f0e820SJohan Hovold 		portdata->in_urbs[i] = urb;
5330d456194SMatthew Garrett 	}
5340d456194SMatthew Garrett 
535b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
5360d456194SMatthew Garrett 		buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
5370d456194SMatthew Garrett 		if (!buffer)
5380d456194SMatthew Garrett 			goto bail_out_error2;
539b8f0e820SJohan Hovold 		portdata->out_buffer[i] = buffer;
540b8f0e820SJohan Hovold 
541b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
542b8f0e820SJohan Hovold 						USB_DIR_OUT, port,
543b8f0e820SJohan Hovold 						buffer, OUT_BUFLEN,
544b8f0e820SJohan Hovold 						usb_wwan_outdat_callback);
545b8f0e820SJohan Hovold 		portdata->out_urbs[i] = urb;
5460d456194SMatthew Garrett 	}
5470d456194SMatthew Garrett 
5480d456194SMatthew Garrett 	usb_set_serial_port_data(port, portdata);
5490d456194SMatthew Garrett 
5500d456194SMatthew Garrett 	return 0;
5510d456194SMatthew Garrett 
5520d456194SMatthew Garrett bail_out_error2:
553b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
554b8f0e820SJohan Hovold 		usb_free_urb(portdata->out_urbs[i]);
555b8f0e820SJohan Hovold 		kfree(portdata->out_buffer[i]);
5560d456194SMatthew Garrett 	}
557b8f0e820SJohan Hovold bail_out_error:
558b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
559b8f0e820SJohan Hovold 		usb_free_urb(portdata->in_urbs[i]);
560b8f0e820SJohan Hovold 		free_page((unsigned long)portdata->in_buffer[i]);
561b8f0e820SJohan Hovold 	}
562b8f0e820SJohan Hovold 	kfree(portdata);
563b8f0e820SJohan Hovold 
564b8f0e820SJohan Hovold 	return -ENOMEM;
565b8f0e820SJohan Hovold }
566b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
5670d456194SMatthew Garrett 
568a1028f0aSBjørn Mork int usb_wwan_port_remove(struct usb_serial_port *port)
569a1028f0aSBjørn Mork {
570a1028f0aSBjørn Mork 	int i;
571a1028f0aSBjørn Mork 	struct usb_wwan_port_private *portdata;
572a1028f0aSBjørn Mork 
573a1028f0aSBjørn Mork 	portdata = usb_get_serial_port_data(port);
574a1028f0aSBjørn Mork 	usb_set_serial_port_data(port, NULL);
575a1028f0aSBjørn Mork 
576a1028f0aSBjørn Mork 	for (i = 0; i < N_IN_URB; i++) {
577a1028f0aSBjørn Mork 		usb_free_urb(portdata->in_urbs[i]);
578a1028f0aSBjørn Mork 		free_page((unsigned long)portdata->in_buffer[i]);
579a1028f0aSBjørn Mork 	}
580a1028f0aSBjørn Mork 	for (i = 0; i < N_OUT_URB; i++) {
581a1028f0aSBjørn Mork 		usb_free_urb(portdata->out_urbs[i]);
582a1028f0aSBjørn Mork 		kfree(portdata->out_buffer[i]);
583a1028f0aSBjørn Mork 	}
584a1028f0aSBjørn Mork 
585a1028f0aSBjørn Mork 	kfree(portdata);
5862b4aceabSJohan Hovold 
587a1028f0aSBjørn Mork 	return 0;
588a1028f0aSBjørn Mork }
589a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove);
590a1028f0aSBjørn Mork 
591a1028f0aSBjørn Mork #ifdef CONFIG_PM
592ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial)
5930d456194SMatthew Garrett {
5940d456194SMatthew Garrett 	int i, j;
5950d456194SMatthew Garrett 	struct usb_serial_port *port;
5960d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
5970d456194SMatthew Garrett 
5980d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; ++i) {
5990d456194SMatthew Garrett 		port = serial->port[i];
6000d456194SMatthew Garrett 		portdata = usb_get_serial_port_data(port);
601032129cbSBjørn Mork 		if (!portdata)
602032129cbSBjørn Mork 			continue;
6030d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++)
6040d456194SMatthew Garrett 			usb_kill_urb(portdata->in_urbs[j]);
6050d456194SMatthew Garrett 		for (j = 0; j < N_OUT_URB; j++)
6060d456194SMatthew Garrett 			usb_kill_urb(portdata->out_urbs[j]);
607ae75c940SJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
6080d456194SMatthew Garrett 	}
6090d456194SMatthew Garrett }
6100d456194SMatthew Garrett 
6110d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
6120d456194SMatthew Garrett {
61337357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6140d456194SMatthew Garrett 
6150d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
616170fad9eSJohan Hovold 	if (PMSG_IS_AUTO(message)) {
617170fad9eSJohan Hovold 		if (intfdata->in_flight) {
6180d456194SMatthew Garrett 			spin_unlock_irq(&intfdata->susp_lock);
6190d456194SMatthew Garrett 			return -EBUSY;
6200d456194SMatthew Garrett 		}
621170fad9eSJohan Hovold 	}
6220d456194SMatthew Garrett 	intfdata->suspended = 1;
6230d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
624170fad9eSJohan Hovold 
625ae75c940SJohan Hovold 	stop_urbs(serial);
6260d456194SMatthew Garrett 
6270d456194SMatthew Garrett 	return 0;
6280d456194SMatthew Garrett }
6290d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend);
6300d456194SMatthew Garrett 
6313362c91cSJohan Hovold /* Caller must hold susp_lock. */
6323362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
6330d456194SMatthew Garrett {
6347436f412SJohan Hovold 	struct usb_serial *serial = port->serial;
63537357ca5SJohan Hovold 	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
6360d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6370d456194SMatthew Garrett 	struct urb *urb;
6387436f412SJohan Hovold 	int err_count = 0;
6397436f412SJohan Hovold 	int err;
6400d456194SMatthew Garrett 
6410d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
64237357ca5SJohan Hovold 
6433362c91cSJohan Hovold 	for (;;) {
6443362c91cSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
6453362c91cSJohan Hovold 		if (!urb)
6463362c91cSJohan Hovold 			break;
6473362c91cSJohan Hovold 
6480d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_ATOMIC);
6497436f412SJohan Hovold 		if (err) {
6503362c91cSJohan Hovold 			dev_err(&port->dev, "%s: submit urb failed: %d\n",
6517436f412SJohan Hovold 					__func__, err);
6527436f412SJohan Hovold 			err_count++;
65316871dcaSOliver Neukum 			unbusy_queued_urb(urb, portdata);
6547436f412SJohan Hovold 			usb_autopm_put_interface_async(serial->interface);
6557436f412SJohan Hovold 			continue;
65616871dcaSOliver Neukum 		}
6577436f412SJohan Hovold 		data->in_flight++;
6580d456194SMatthew Garrett 	}
659fb7ad4f9SJohan Hovold 
6607436f412SJohan Hovold 	if (err_count)
6617436f412SJohan Hovold 		return -EIO;
6627436f412SJohan Hovold 
6637436f412SJohan Hovold 	return 0;
6640d456194SMatthew Garrett }
6650d456194SMatthew Garrett 
6660d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial)
6670d456194SMatthew Garrett {
6680d456194SMatthew Garrett 	int i, j;
6690d456194SMatthew Garrett 	struct usb_serial_port *port;
67037357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6710d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6720d456194SMatthew Garrett 	struct urb *urb;
673fb7ad4f9SJohan Hovold 	int err;
674fb7ad4f9SJohan Hovold 	int err_count = 0;
6750d456194SMatthew Garrett 
676d9e93c08Sxiao jin 	spin_lock_irq(&intfdata->susp_lock);
6770d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; i++) {
6780d456194SMatthew Garrett 		port = serial->port[i];
6790d456194SMatthew Garrett 
680d41861caSPeter Hurley 		if (!tty_port_initialized(&port->port))
6810d456194SMatthew Garrett 			continue;
6820d456194SMatthew Garrett 
683b0a9aa6dSJohan Hovold 		portdata = usb_get_serial_port_data(port);
684b0a9aa6dSJohan Hovold 
6859096f1fbSJohan Hovold 		if (port->interrupt_in_urb) {
6869096f1fbSJohan Hovold 			err = usb_submit_urb(port->interrupt_in_urb,
6879096f1fbSJohan Hovold 					GFP_ATOMIC);
6889096f1fbSJohan Hovold 			if (err) {
6899096f1fbSJohan Hovold 				dev_err(&port->dev,
6909096f1fbSJohan Hovold 					"%s: submit int urb failed: %d\n",
6919096f1fbSJohan Hovold 					__func__, err);
692fb7ad4f9SJohan Hovold 				err_count++;
6939096f1fbSJohan Hovold 			}
6949096f1fbSJohan Hovold 		}
6959096f1fbSJohan Hovold 
6963362c91cSJohan Hovold 		err = usb_wwan_submit_delayed_urbs(port);
697fb7ad4f9SJohan Hovold 		if (err)
698fb7ad4f9SJohan Hovold 			err_count++;
699fb7ad4f9SJohan Hovold 
7000d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++) {
7010d456194SMatthew Garrett 			urb = portdata->in_urbs[j];
7020d456194SMatthew Garrett 			err = usb_submit_urb(urb, GFP_ATOMIC);
7030d456194SMatthew Garrett 			if (err < 0) {
704b0f9d003SJohan Hovold 				dev_err(&port->dev,
705b0f9d003SJohan Hovold 					"%s: submit read urb %d failed: %d\n",
706b0f9d003SJohan Hovold 					__func__, i, err);
707fb7ad4f9SJohan Hovold 				err_count++;
7080d456194SMatthew Garrett 			}
7090d456194SMatthew Garrett 		}
7100d456194SMatthew Garrett 	}
7110d456194SMatthew Garrett 	intfdata->suspended = 0;
7120d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
713fb7ad4f9SJohan Hovold 
714fb7ad4f9SJohan Hovold 	if (err_count)
715fb7ad4f9SJohan Hovold 		return -EIO;
716fb7ad4f9SJohan Hovold 
717fb7ad4f9SJohan Hovold 	return 0;
7180d456194SMatthew Garrett }
7190d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume);
7200d456194SMatthew Garrett #endif
7210d456194SMatthew Garrett 
7220d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR);
7230d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC);
724627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
725