xref: /openbmc/linux/drivers/usb/serial/usb_wwan.c (revision d2958d1b8fb0cffa362b187c0375a5aff4fe3825)
10d456194SMatthew Garrett /*
20d456194SMatthew Garrett   USB Driver layer for GSM modems
30d456194SMatthew Garrett 
40d456194SMatthew Garrett   Copyright (C) 2005  Matthias Urlichs <smurf@smurf.noris.de>
50d456194SMatthew Garrett 
60d456194SMatthew Garrett   This driver is free software; you can redistribute it and/or modify
70d456194SMatthew Garrett   it under the terms of Version 2 of the GNU General Public License as
80d456194SMatthew Garrett   published by the Free Software Foundation.
90d456194SMatthew Garrett 
100d456194SMatthew Garrett   Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
110d456194SMatthew Garrett 
120d456194SMatthew Garrett   History: see the git log.
130d456194SMatthew Garrett 
140d456194SMatthew Garrett   Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
150d456194SMatthew Garrett 
160d456194SMatthew Garrett   This driver exists because the "normal" serial driver doesn't work too well
170d456194SMatthew Garrett   with GSM modems. Issues:
180d456194SMatthew Garrett   - data loss -- one single Receive URB is not nearly enough
190d456194SMatthew Garrett   - controlling the baud rate doesn't make sense
200d456194SMatthew Garrett */
210d456194SMatthew Garrett 
220d456194SMatthew Garrett #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
230d456194SMatthew Garrett #define DRIVER_DESC "USB Driver for GSM modems"
240d456194SMatthew Garrett 
250d456194SMatthew Garrett #include <linux/kernel.h>
260d456194SMatthew Garrett #include <linux/jiffies.h>
270d456194SMatthew Garrett #include <linux/errno.h>
280d456194SMatthew Garrett #include <linux/slab.h>
290d456194SMatthew Garrett #include <linux/tty.h>
300d456194SMatthew Garrett #include <linux/tty_flip.h>
310d456194SMatthew Garrett #include <linux/module.h>
320d456194SMatthew Garrett #include <linux/bitops.h>
3366921eddSPeter Huewe #include <linux/uaccess.h>
340d456194SMatthew Garrett #include <linux/usb.h>
350d456194SMatthew Garrett #include <linux/usb/serial.h>
3602303f73SDan Williams #include <linux/serial.h>
370d456194SMatthew Garrett #include "usb-wwan.h"
380d456194SMatthew Garrett 
390d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
400d456194SMatthew Garrett {
410d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
420d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
430d456194SMatthew Garrett 
4437357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
450d456194SMatthew Garrett 
460d456194SMatthew Garrett 	if (!intfdata->send_setup)
470d456194SMatthew Garrett 		return;
480d456194SMatthew Garrett 
490d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
50b2ca6990SJohan Hovold 	/* FIXME: locking */
510d456194SMatthew Garrett 	portdata->rts_state = on;
520d456194SMatthew Garrett 	portdata->dtr_state = on;
53b2ca6990SJohan Hovold 
540d456194SMatthew Garrett 	intfdata->send_setup(port);
550d456194SMatthew Garrett }
560d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts);
570d456194SMatthew Garrett 
5860b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty)
590d456194SMatthew Garrett {
600d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
610d456194SMatthew Garrett 	unsigned int value;
620d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
630d456194SMatthew Garrett 
640d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
650d456194SMatthew Garrett 
660d456194SMatthew Garrett 	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
670d456194SMatthew Garrett 	    ((portdata->dtr_state) ? TIOCM_DTR : 0) |
680d456194SMatthew Garrett 	    ((portdata->cts_state) ? TIOCM_CTS : 0) |
690d456194SMatthew Garrett 	    ((portdata->dsr_state) ? TIOCM_DSR : 0) |
700d456194SMatthew Garrett 	    ((portdata->dcd_state) ? TIOCM_CAR : 0) |
710d456194SMatthew Garrett 	    ((portdata->ri_state) ? TIOCM_RNG : 0);
720d456194SMatthew Garrett 
730d456194SMatthew Garrett 	return value;
740d456194SMatthew Garrett }
750d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget);
760d456194SMatthew Garrett 
7720b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty,
780d456194SMatthew Garrett 		      unsigned int set, unsigned int clear)
790d456194SMatthew Garrett {
800d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
810d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
820d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
830d456194SMatthew Garrett 
840d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
8537357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
860d456194SMatthew Garrett 
870d456194SMatthew Garrett 	if (!intfdata->send_setup)
880d456194SMatthew Garrett 		return -EINVAL;
890d456194SMatthew Garrett 
900d456194SMatthew Garrett 	/* FIXME: what locks portdata fields ? */
910d456194SMatthew Garrett 	if (set & TIOCM_RTS)
920d456194SMatthew Garrett 		portdata->rts_state = 1;
930d456194SMatthew Garrett 	if (set & TIOCM_DTR)
940d456194SMatthew Garrett 		portdata->dtr_state = 1;
950d456194SMatthew Garrett 
960d456194SMatthew Garrett 	if (clear & TIOCM_RTS)
970d456194SMatthew Garrett 		portdata->rts_state = 0;
980d456194SMatthew Garrett 	if (clear & TIOCM_DTR)
990d456194SMatthew Garrett 		portdata->dtr_state = 0;
1000d456194SMatthew Garrett 	return intfdata->send_setup(port);
1010d456194SMatthew Garrett }
1020d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset);
1030d456194SMatthew Garrett 
10402303f73SDan Williams static int get_serial_info(struct usb_serial_port *port,
10502303f73SDan Williams 			   struct serial_struct __user *retinfo)
10602303f73SDan Williams {
10702303f73SDan Williams 	struct serial_struct tmp;
10802303f73SDan Williams 
10902303f73SDan Williams 	if (!retinfo)
11002303f73SDan Williams 		return -EFAULT;
11102303f73SDan Williams 
11202303f73SDan Williams 	memset(&tmp, 0, sizeof(tmp));
113e5b1e206SGreg Kroah-Hartman 	tmp.line            = port->minor;
1141143832eSGreg Kroah-Hartman 	tmp.port            = port->port_number;
11502303f73SDan Williams 	tmp.baud_base       = tty_get_baud_rate(port->port.tty);
11602303f73SDan Williams 	tmp.close_delay	    = port->port.close_delay / 10;
11702303f73SDan Williams 	tmp.closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
11802303f73SDan Williams 				 ASYNC_CLOSING_WAIT_NONE :
11902303f73SDan Williams 				 port->port.closing_wait / 10;
12002303f73SDan Williams 
12102303f73SDan Williams 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
12202303f73SDan Williams 		return -EFAULT;
12302303f73SDan Williams 	return 0;
12402303f73SDan Williams }
12502303f73SDan Williams 
12602303f73SDan Williams static int set_serial_info(struct usb_serial_port *port,
12702303f73SDan Williams 			   struct serial_struct __user *newinfo)
12802303f73SDan Williams {
12902303f73SDan Williams 	struct serial_struct new_serial;
13002303f73SDan Williams 	unsigned int closing_wait, close_delay;
13102303f73SDan Williams 	int retval = 0;
13202303f73SDan Williams 
13302303f73SDan Williams 	if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
13402303f73SDan Williams 		return -EFAULT;
13502303f73SDan Williams 
13602303f73SDan Williams 	close_delay = new_serial.close_delay * 10;
13702303f73SDan Williams 	closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
13802303f73SDan Williams 			ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
13902303f73SDan Williams 
14002303f73SDan Williams 	mutex_lock(&port->port.mutex);
14102303f73SDan Williams 
14202303f73SDan Williams 	if (!capable(CAP_SYS_ADMIN)) {
14302303f73SDan Williams 		if ((close_delay != port->port.close_delay) ||
14402303f73SDan Williams 		    (closing_wait != port->port.closing_wait))
14502303f73SDan Williams 			retval = -EPERM;
14602303f73SDan Williams 		else
14702303f73SDan Williams 			retval = -EOPNOTSUPP;
14802303f73SDan Williams 	} else {
14902303f73SDan Williams 		port->port.close_delay  = close_delay;
15002303f73SDan Williams 		port->port.closing_wait = closing_wait;
15102303f73SDan Williams 	}
15202303f73SDan Williams 
15302303f73SDan Williams 	mutex_unlock(&port->port.mutex);
15402303f73SDan Williams 	return retval;
15502303f73SDan Williams }
15602303f73SDan Williams 
15700a0d0d6SAlan Cox int usb_wwan_ioctl(struct tty_struct *tty,
15802303f73SDan Williams 		   unsigned int cmd, unsigned long arg)
15902303f73SDan Williams {
16002303f73SDan Williams 	struct usb_serial_port *port = tty->driver_data;
16102303f73SDan Williams 
162a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
16302303f73SDan Williams 
16402303f73SDan Williams 	switch (cmd) {
16502303f73SDan Williams 	case TIOCGSERIAL:
16602303f73SDan Williams 		return get_serial_info(port,
16702303f73SDan Williams 				       (struct serial_struct __user *) arg);
16802303f73SDan Williams 	case TIOCSSERIAL:
16902303f73SDan Williams 		return set_serial_info(port,
17002303f73SDan Williams 				       (struct serial_struct __user *) arg);
17102303f73SDan Williams 	default:
17202303f73SDan Williams 		break;
17302303f73SDan Williams 	}
17402303f73SDan Williams 
175a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s arg not supported\n", __func__);
17602303f73SDan Williams 
17702303f73SDan Williams 	return -ENOIOCTLCMD;
17802303f73SDan Williams }
17902303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl);
18002303f73SDan Williams 
1810d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
1820d456194SMatthew Garrett 		   const unsigned char *buf, int count)
1830d456194SMatthew Garrett {
1840d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
1850d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
1860d456194SMatthew Garrett 	int i;
1870d456194SMatthew Garrett 	int left, todo;
1880d456194SMatthew Garrett 	struct urb *this_urb = NULL;	/* spurious */
1890d456194SMatthew Garrett 	int err;
1900d456194SMatthew Garrett 	unsigned long flags;
1910d456194SMatthew Garrett 
1920d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
19337357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
1940d456194SMatthew Garrett 
195a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
1960d456194SMatthew Garrett 
1970d456194SMatthew Garrett 	i = 0;
1980d456194SMatthew Garrett 	left = count;
1990d456194SMatthew Garrett 	for (i = 0; left > 0 && i < N_OUT_URB; i++) {
2000d456194SMatthew Garrett 		todo = left;
2010d456194SMatthew Garrett 		if (todo > OUT_BUFLEN)
2020d456194SMatthew Garrett 			todo = OUT_BUFLEN;
2030d456194SMatthew Garrett 
2040d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
2050d456194SMatthew Garrett 		if (test_and_set_bit(i, &portdata->out_busy)) {
2060d456194SMatthew Garrett 			if (time_before(jiffies,
2070d456194SMatthew Garrett 					portdata->tx_start_time[i] + 10 * HZ))
2080d456194SMatthew Garrett 				continue;
2090d456194SMatthew Garrett 			usb_unlink_urb(this_urb);
2100d456194SMatthew Garrett 			continue;
2110d456194SMatthew Garrett 		}
212a80be97dSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
2130d456194SMatthew Garrett 			usb_pipeendpoint(this_urb->pipe), i);
2140d456194SMatthew Garrett 
2150d456194SMatthew Garrett 		err = usb_autopm_get_interface_async(port->serial->interface);
216db090473Sxiao jin 		if (err < 0) {
217db090473Sxiao jin 			clear_bit(i, &portdata->out_busy);
2180d456194SMatthew Garrett 			break;
219db090473Sxiao jin 		}
2200d456194SMatthew Garrett 
2210d456194SMatthew Garrett 		/* send the data */
2220d456194SMatthew Garrett 		memcpy(this_urb->transfer_buffer, buf, todo);
2230d456194SMatthew Garrett 		this_urb->transfer_buffer_length = todo;
2240d456194SMatthew Garrett 
2250d456194SMatthew Garrett 		spin_lock_irqsave(&intfdata->susp_lock, flags);
2260d456194SMatthew Garrett 		if (intfdata->suspended) {
2270d456194SMatthew Garrett 			usb_anchor_urb(this_urb, &portdata->delayed);
2280d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2290d456194SMatthew Garrett 		} else {
2300d456194SMatthew Garrett 			intfdata->in_flight++;
2310d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2320d456194SMatthew Garrett 			err = usb_submit_urb(this_urb, GFP_ATOMIC);
2330d456194SMatthew Garrett 			if (err) {
2348bb7ec65SJohan Hovold 				dev_err(&port->dev,
2358bb7ec65SJohan Hovold 					"%s: submit urb %d failed: %d\n",
2368bb7ec65SJohan Hovold 					__func__, i, err);
2370d456194SMatthew Garrett 				clear_bit(i, &portdata->out_busy);
2380d456194SMatthew Garrett 				spin_lock_irqsave(&intfdata->susp_lock, flags);
2390d456194SMatthew Garrett 				intfdata->in_flight--;
2400d456194SMatthew Garrett 				spin_unlock_irqrestore(&intfdata->susp_lock,
2410d456194SMatthew Garrett 						       flags);
2423d06bf15SOliver Neukum 				usb_autopm_put_interface_async(port->serial->interface);
243433508aeSOliver Neukum 				break;
2440d456194SMatthew Garrett 			}
2450d456194SMatthew Garrett 		}
2460d456194SMatthew Garrett 
2470d456194SMatthew Garrett 		portdata->tx_start_time[i] = jiffies;
2480d456194SMatthew Garrett 		buf += todo;
2490d456194SMatthew Garrett 		left -= todo;
2500d456194SMatthew Garrett 	}
2510d456194SMatthew Garrett 
2520d456194SMatthew Garrett 	count -= left;
253a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
2540d456194SMatthew Garrett 	return count;
2550d456194SMatthew Garrett }
2560d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write);
2570d456194SMatthew Garrett 
2580d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb)
2590d456194SMatthew Garrett {
2600d456194SMatthew Garrett 	int err;
2610d456194SMatthew Garrett 	int endpoint;
2620d456194SMatthew Garrett 	struct usb_serial_port *port;
263a80be97dSGreg Kroah-Hartman 	struct device *dev;
2640d456194SMatthew Garrett 	unsigned char *data = urb->transfer_buffer;
2650d456194SMatthew Garrett 	int status = urb->status;
2660d456194SMatthew Garrett 
2670d456194SMatthew Garrett 	endpoint = usb_pipeendpoint(urb->pipe);
2680d456194SMatthew Garrett 	port = urb->context;
269a80be97dSGreg Kroah-Hartman 	dev = &port->dev;
2700d456194SMatthew Garrett 
2710d456194SMatthew Garrett 	if (status) {
272a80be97dSGreg Kroah-Hartman 		dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
2730d456194SMatthew Garrett 			__func__, status, endpoint);
2740d456194SMatthew Garrett 	} else {
2750d456194SMatthew Garrett 		if (urb->actual_length) {
27605c7cd39SJiri Slaby 			tty_insert_flip_string(&port->port, data,
27738237fd2SJiri Slaby 					urb->actual_length);
2782e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
2790d456194SMatthew Garrett 		} else
280a80be97dSGreg Kroah-Hartman 			dev_dbg(dev, "%s: empty read urb received\n", __func__);
2816c1ee66aSMatt Burtch 	}
2820d456194SMatthew Garrett 	/* Resubmit urb so we continue receiving */
2830d456194SMatthew Garrett 	err = usb_submit_urb(urb, GFP_ATOMIC);
284c9c4558fSOliver Neukum 	if (err) {
285*d2958d1bSJohan Hovold 		if (err != -EPERM && err != -ENODEV) {
2866c1ee66aSMatt Burtch 			dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
2876c1ee66aSMatt Burtch 				__func__, err);
288c9c4558fSOliver Neukum 			/* busy also in error unless we are killed */
2890d456194SMatthew Garrett 			usb_mark_last_busy(port->serial->dev);
2900d456194SMatthew Garrett 		}
291c9c4558fSOliver Neukum 	} else {
292c9c4558fSOliver Neukum 		usb_mark_last_busy(port->serial->dev);
293c9c4558fSOliver Neukum 	}
294c9c4558fSOliver Neukum }
2950d456194SMatthew Garrett 
2960d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb)
2970d456194SMatthew Garrett {
2980d456194SMatthew Garrett 	struct usb_serial_port *port;
2990d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3000d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
3010d456194SMatthew Garrett 	int i;
3020d456194SMatthew Garrett 
3030d456194SMatthew Garrett 	port = urb->context;
30437357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
3050d456194SMatthew Garrett 
3060d456194SMatthew Garrett 	usb_serial_port_softint(port);
3070d456194SMatthew Garrett 	usb_autopm_put_interface_async(port->serial->interface);
3080d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3090d456194SMatthew Garrett 	spin_lock(&intfdata->susp_lock);
3100d456194SMatthew Garrett 	intfdata->in_flight--;
3110d456194SMatthew Garrett 	spin_unlock(&intfdata->susp_lock);
3120d456194SMatthew Garrett 
3130d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; ++i) {
3140d456194SMatthew Garrett 		if (portdata->out_urbs[i] == urb) {
3154e857c58SPeter Zijlstra 			smp_mb__before_atomic();
3160d456194SMatthew Garrett 			clear_bit(i, &portdata->out_busy);
3170d456194SMatthew Garrett 			break;
3180d456194SMatthew Garrett 		}
3190d456194SMatthew Garrett 	}
3200d456194SMatthew Garrett }
3210d456194SMatthew Garrett 
3220d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty)
3230d456194SMatthew Garrett {
3240d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3250d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3260d456194SMatthew Garrett 	int i;
3270d456194SMatthew Garrett 	int data_len = 0;
3280d456194SMatthew Garrett 	struct urb *this_urb;
3290d456194SMatthew Garrett 
3300d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3310d456194SMatthew Garrett 
3320d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3330d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3340d456194SMatthew Garrett 		if (this_urb && !test_bit(i, &portdata->out_busy))
3350d456194SMatthew Garrett 			data_len += OUT_BUFLEN;
3360d456194SMatthew Garrett 	}
3370d456194SMatthew Garrett 
338a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3390d456194SMatthew Garrett 	return data_len;
3400d456194SMatthew Garrett }
3410d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room);
3420d456194SMatthew Garrett 
3430d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty)
3440d456194SMatthew Garrett {
3450d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3460d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3470d456194SMatthew Garrett 	int i;
3480d456194SMatthew Garrett 	int data_len = 0;
3490d456194SMatthew Garrett 	struct urb *this_urb;
3500d456194SMatthew Garrett 
3510d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3520d456194SMatthew Garrett 
3530d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3540d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3550d456194SMatthew Garrett 		/* FIXME: This locking is insufficient as this_urb may
3560d456194SMatthew Garrett 		   go unused during the test */
3570d456194SMatthew Garrett 		if (this_urb && test_bit(i, &portdata->out_busy))
3580d456194SMatthew Garrett 			data_len += this_urb->transfer_buffer_length;
3590d456194SMatthew Garrett 	}
360a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3610d456194SMatthew Garrett 	return data_len;
3620d456194SMatthew Garrett }
3630d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
3640d456194SMatthew Garrett 
3650d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
3660d456194SMatthew Garrett {
3670d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3680d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
3690d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
3700d456194SMatthew Garrett 	int i, err;
3710d456194SMatthew Garrett 	struct urb *urb;
3720d456194SMatthew Garrett 
3730d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
37437357ca5SJohan Hovold 	intfdata = usb_get_serial_data(serial);
3750d456194SMatthew Garrett 
3769096f1fbSJohan Hovold 	if (port->interrupt_in_urb) {
3779096f1fbSJohan Hovold 		err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
3789096f1fbSJohan Hovold 		if (err) {
3798bb7ec65SJohan Hovold 			dev_err(&port->dev, "%s: submit int urb failed: %d\n",
3809096f1fbSJohan Hovold 				__func__, err);
3819096f1fbSJohan Hovold 		}
3829096f1fbSJohan Hovold 	}
3839096f1fbSJohan Hovold 
3840d456194SMatthew Garrett 	/* Start reading from the IN endpoint */
3850d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++) {
3860d456194SMatthew Garrett 		urb = portdata->in_urbs[i];
3870d456194SMatthew Garrett 		if (!urb)
3880d456194SMatthew Garrett 			continue;
3890d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_KERNEL);
3900d456194SMatthew Garrett 		if (err) {
3918bb7ec65SJohan Hovold 			dev_err(&port->dev,
3928bb7ec65SJohan Hovold 				"%s: submit read urb %d failed: %d\n",
3938bb7ec65SJohan Hovold 				__func__, i, err);
3940d456194SMatthew Garrett 		}
3950d456194SMatthew Garrett 	}
3960d456194SMatthew Garrett 
3970d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
398c1c01803SJohan Hovold 	if (++intfdata->open_ports == 1)
399c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 1;
4000d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4019a91aedcSOliver Neukum 	/* this balances a get in the generic USB serial code */
4020d456194SMatthew Garrett 	usb_autopm_put_interface(serial->interface);
4030d456194SMatthew Garrett 
4040d456194SMatthew Garrett 	return 0;
4050d456194SMatthew Garrett }
4060d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open);
4070d456194SMatthew Garrett 
40879eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb,
40979eed03eSJohan Hovold 					struct usb_wwan_port_private *portdata)
41079eed03eSJohan Hovold {
41179eed03eSJohan Hovold 	int i;
41279eed03eSJohan Hovold 
41379eed03eSJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
41479eed03eSJohan Hovold 		if (urb == portdata->out_urbs[i]) {
41579eed03eSJohan Hovold 			clear_bit(i, &portdata->out_busy);
41679eed03eSJohan Hovold 			break;
41779eed03eSJohan Hovold 		}
41879eed03eSJohan Hovold 	}
41979eed03eSJohan Hovold }
42079eed03eSJohan Hovold 
4210d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port)
4220d456194SMatthew Garrett {
4230d456194SMatthew Garrett 	int i;
4240d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
4250d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
42637357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
42779eed03eSJohan Hovold 	struct urb *urb;
4280d456194SMatthew Garrett 
4290d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
4300d456194SMatthew Garrett 
431b0a9aa6dSJohan Hovold 	/*
432b0a9aa6dSJohan Hovold 	 * Need to take susp_lock to make sure port is not already being
433b0a9aa6dSJohan Hovold 	 * resumed, but no need to hold it due to ASYNC_INITIALIZED.
434b0a9aa6dSJohan Hovold 	 */
4350d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
436c1c01803SJohan Hovold 	if (--intfdata->open_ports == 0)
437c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 0;
4380d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4390d456194SMatthew Garrett 
44079eed03eSJohan Hovold 	for (;;) {
44179eed03eSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
44279eed03eSJohan Hovold 		if (!urb)
44379eed03eSJohan Hovold 			break;
44479eed03eSJohan Hovold 		unbusy_queued_urb(urb, portdata);
44579eed03eSJohan Hovold 		usb_autopm_put_interface_async(serial->interface);
44679eed03eSJohan Hovold 	}
44779eed03eSJohan Hovold 
4480d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++)
4490d456194SMatthew Garrett 		usb_kill_urb(portdata->in_urbs[i]);
4500d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++)
4510d456194SMatthew Garrett 		usb_kill_urb(portdata->out_urbs[i]);
4529096f1fbSJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
453e6d144bcSJohan Hovold 
4549a91aedcSOliver Neukum 	usb_autopm_get_interface_no_resume(serial->interface);
4550d456194SMatthew Garrett }
4560d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close);
4570d456194SMatthew Garrett 
458b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
459b8f0e820SJohan Hovold 				      int endpoint,
4600d456194SMatthew Garrett 				      int dir, void *ctx, char *buf, int len,
4610d456194SMatthew Garrett 				      void (*callback) (struct urb *))
4620d456194SMatthew Garrett {
463b8f0e820SJohan Hovold 	struct usb_serial *serial = port->serial;
4640d456194SMatthew Garrett 	struct urb *urb;
4650d456194SMatthew Garrett 
4660d456194SMatthew Garrett 	urb = usb_alloc_urb(0, GFP_KERNEL);	/* No ISO */
46710c642d0SJohan Hovold 	if (!urb)
4680d456194SMatthew Garrett 		return NULL;
4690d456194SMatthew Garrett 
4700d456194SMatthew Garrett 	usb_fill_bulk_urb(urb, serial->dev,
4710d456194SMatthew Garrett 			  usb_sndbulkpipe(serial->dev, endpoint) | dir,
4720d456194SMatthew Garrett 			  buf, len, callback, ctx);
4730d456194SMatthew Garrett 
4740d456194SMatthew Garrett 	return urb;
4750d456194SMatthew Garrett }
4760d456194SMatthew Garrett 
477b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port)
4780d456194SMatthew Garrett {
4790d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
480b8f0e820SJohan Hovold 	struct urb *urb;
4810d456194SMatthew Garrett 	u8 *buffer;
482b8f0e820SJohan Hovold 	int i;
4830d456194SMatthew Garrett 
484bd73bd88SJohan Hovold 	if (!port->bulk_in_size || !port->bulk_out_size)
485bd73bd88SJohan Hovold 		return -ENODEV;
486bd73bd88SJohan Hovold 
4870d456194SMatthew Garrett 	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
488b8f0e820SJohan Hovold 	if (!portdata)
489b8f0e820SJohan Hovold 		return -ENOMEM;
490b8f0e820SJohan Hovold 
4910d456194SMatthew Garrett 	init_usb_anchor(&portdata->delayed);
4920d456194SMatthew Garrett 
493b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
4940d456194SMatthew Garrett 		buffer = (u8 *)__get_free_page(GFP_KERNEL);
4950d456194SMatthew Garrett 		if (!buffer)
4960d456194SMatthew Garrett 			goto bail_out_error;
497b8f0e820SJohan Hovold 		portdata->in_buffer[i] = buffer;
498b8f0e820SJohan Hovold 
499b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
500b8f0e820SJohan Hovold 						USB_DIR_IN, port,
501b8f0e820SJohan Hovold 						buffer, IN_BUFLEN,
502b8f0e820SJohan Hovold 						usb_wwan_indat_callback);
503b8f0e820SJohan Hovold 		portdata->in_urbs[i] = urb;
5040d456194SMatthew Garrett 	}
5050d456194SMatthew Garrett 
506b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
5070d456194SMatthew Garrett 		buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
5080d456194SMatthew Garrett 		if (!buffer)
5090d456194SMatthew Garrett 			goto bail_out_error2;
510b8f0e820SJohan Hovold 		portdata->out_buffer[i] = buffer;
511b8f0e820SJohan Hovold 
512b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
513b8f0e820SJohan Hovold 						USB_DIR_OUT, port,
514b8f0e820SJohan Hovold 						buffer, OUT_BUFLEN,
515b8f0e820SJohan Hovold 						usb_wwan_outdat_callback);
516b8f0e820SJohan Hovold 		portdata->out_urbs[i] = urb;
5170d456194SMatthew Garrett 	}
5180d456194SMatthew Garrett 
5190d456194SMatthew Garrett 	usb_set_serial_port_data(port, portdata);
5200d456194SMatthew Garrett 
5210d456194SMatthew Garrett 	return 0;
5220d456194SMatthew Garrett 
5230d456194SMatthew Garrett bail_out_error2:
524b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
525b8f0e820SJohan Hovold 		usb_free_urb(portdata->out_urbs[i]);
526b8f0e820SJohan Hovold 		kfree(portdata->out_buffer[i]);
5270d456194SMatthew Garrett 	}
528b8f0e820SJohan Hovold bail_out_error:
529b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
530b8f0e820SJohan Hovold 		usb_free_urb(portdata->in_urbs[i]);
531b8f0e820SJohan Hovold 		free_page((unsigned long)portdata->in_buffer[i]);
532b8f0e820SJohan Hovold 	}
533b8f0e820SJohan Hovold 	kfree(portdata);
534b8f0e820SJohan Hovold 
535b8f0e820SJohan Hovold 	return -ENOMEM;
536b8f0e820SJohan Hovold }
537b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
5380d456194SMatthew Garrett 
539a1028f0aSBjørn Mork int usb_wwan_port_remove(struct usb_serial_port *port)
540a1028f0aSBjørn Mork {
541a1028f0aSBjørn Mork 	int i;
542a1028f0aSBjørn Mork 	struct usb_wwan_port_private *portdata;
543a1028f0aSBjørn Mork 
544a1028f0aSBjørn Mork 	portdata = usb_get_serial_port_data(port);
545a1028f0aSBjørn Mork 	usb_set_serial_port_data(port, NULL);
546a1028f0aSBjørn Mork 
547a1028f0aSBjørn Mork 	for (i = 0; i < N_IN_URB; i++) {
548a1028f0aSBjørn Mork 		usb_free_urb(portdata->in_urbs[i]);
549a1028f0aSBjørn Mork 		free_page((unsigned long)portdata->in_buffer[i]);
550a1028f0aSBjørn Mork 	}
551a1028f0aSBjørn Mork 	for (i = 0; i < N_OUT_URB; i++) {
552a1028f0aSBjørn Mork 		usb_free_urb(portdata->out_urbs[i]);
553a1028f0aSBjørn Mork 		kfree(portdata->out_buffer[i]);
554a1028f0aSBjørn Mork 	}
555a1028f0aSBjørn Mork 
556a1028f0aSBjørn Mork 	kfree(portdata);
5572b4aceabSJohan Hovold 
558a1028f0aSBjørn Mork 	return 0;
559a1028f0aSBjørn Mork }
560a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove);
561a1028f0aSBjørn Mork 
562a1028f0aSBjørn Mork #ifdef CONFIG_PM
563ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial)
5640d456194SMatthew Garrett {
5650d456194SMatthew Garrett 	int i, j;
5660d456194SMatthew Garrett 	struct usb_serial_port *port;
5670d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
5680d456194SMatthew Garrett 
5690d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; ++i) {
5700d456194SMatthew Garrett 		port = serial->port[i];
5710d456194SMatthew Garrett 		portdata = usb_get_serial_port_data(port);
572032129cbSBjørn Mork 		if (!portdata)
573032129cbSBjørn Mork 			continue;
5740d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++)
5750d456194SMatthew Garrett 			usb_kill_urb(portdata->in_urbs[j]);
5760d456194SMatthew Garrett 		for (j = 0; j < N_OUT_URB; j++)
5770d456194SMatthew Garrett 			usb_kill_urb(portdata->out_urbs[j]);
578ae75c940SJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
5790d456194SMatthew Garrett 	}
5800d456194SMatthew Garrett }
5810d456194SMatthew Garrett 
5820d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
5830d456194SMatthew Garrett {
58437357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
5850d456194SMatthew Garrett 
5860d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
587170fad9eSJohan Hovold 	if (PMSG_IS_AUTO(message)) {
588170fad9eSJohan Hovold 		if (intfdata->in_flight) {
5890d456194SMatthew Garrett 			spin_unlock_irq(&intfdata->susp_lock);
5900d456194SMatthew Garrett 			return -EBUSY;
5910d456194SMatthew Garrett 		}
592170fad9eSJohan Hovold 	}
5930d456194SMatthew Garrett 	intfdata->suspended = 1;
5940d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
595170fad9eSJohan Hovold 
596ae75c940SJohan Hovold 	stop_urbs(serial);
5970d456194SMatthew Garrett 
5980d456194SMatthew Garrett 	return 0;
5990d456194SMatthew Garrett }
6000d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend);
6010d456194SMatthew Garrett 
6023362c91cSJohan Hovold /* Caller must hold susp_lock. */
6033362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
6040d456194SMatthew Garrett {
6057436f412SJohan Hovold 	struct usb_serial *serial = port->serial;
60637357ca5SJohan Hovold 	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
6070d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6080d456194SMatthew Garrett 	struct urb *urb;
6097436f412SJohan Hovold 	int err_count = 0;
6107436f412SJohan Hovold 	int err;
6110d456194SMatthew Garrett 
6120d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
61337357ca5SJohan Hovold 
6143362c91cSJohan Hovold 	for (;;) {
6153362c91cSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
6163362c91cSJohan Hovold 		if (!urb)
6173362c91cSJohan Hovold 			break;
6183362c91cSJohan Hovold 
6190d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_ATOMIC);
6207436f412SJohan Hovold 		if (err) {
6213362c91cSJohan Hovold 			dev_err(&port->dev, "%s: submit urb failed: %d\n",
6227436f412SJohan Hovold 					__func__, err);
6237436f412SJohan Hovold 			err_count++;
62416871dcaSOliver Neukum 			unbusy_queued_urb(urb, portdata);
6257436f412SJohan Hovold 			usb_autopm_put_interface_async(serial->interface);
6267436f412SJohan Hovold 			continue;
62716871dcaSOliver Neukum 		}
6287436f412SJohan Hovold 		data->in_flight++;
6290d456194SMatthew Garrett 	}
630fb7ad4f9SJohan Hovold 
6317436f412SJohan Hovold 	if (err_count)
6327436f412SJohan Hovold 		return -EIO;
6337436f412SJohan Hovold 
6347436f412SJohan Hovold 	return 0;
6350d456194SMatthew Garrett }
6360d456194SMatthew Garrett 
6370d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial)
6380d456194SMatthew Garrett {
6390d456194SMatthew Garrett 	int i, j;
6400d456194SMatthew Garrett 	struct usb_serial_port *port;
64137357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6420d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6430d456194SMatthew Garrett 	struct urb *urb;
644fb7ad4f9SJohan Hovold 	int err;
645fb7ad4f9SJohan Hovold 	int err_count = 0;
6460d456194SMatthew Garrett 
647d9e93c08Sxiao jin 	spin_lock_irq(&intfdata->susp_lock);
6480d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; i++) {
6490d456194SMatthew Garrett 		port = serial->port[i];
6500d456194SMatthew Garrett 
651b0a9aa6dSJohan Hovold 		if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
6520d456194SMatthew Garrett 			continue;
6530d456194SMatthew Garrett 
654b0a9aa6dSJohan Hovold 		portdata = usb_get_serial_port_data(port);
655b0a9aa6dSJohan Hovold 
6569096f1fbSJohan Hovold 		if (port->interrupt_in_urb) {
6579096f1fbSJohan Hovold 			err = usb_submit_urb(port->interrupt_in_urb,
6589096f1fbSJohan Hovold 					GFP_ATOMIC);
6599096f1fbSJohan Hovold 			if (err) {
6609096f1fbSJohan Hovold 				dev_err(&port->dev,
6619096f1fbSJohan Hovold 					"%s: submit int urb failed: %d\n",
6629096f1fbSJohan Hovold 					__func__, err);
663fb7ad4f9SJohan Hovold 				err_count++;
6649096f1fbSJohan Hovold 			}
6659096f1fbSJohan Hovold 		}
6669096f1fbSJohan Hovold 
6673362c91cSJohan Hovold 		err = usb_wwan_submit_delayed_urbs(port);
668fb7ad4f9SJohan Hovold 		if (err)
669fb7ad4f9SJohan Hovold 			err_count++;
670fb7ad4f9SJohan Hovold 
6710d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++) {
6720d456194SMatthew Garrett 			urb = portdata->in_urbs[j];
6730d456194SMatthew Garrett 			err = usb_submit_urb(urb, GFP_ATOMIC);
6740d456194SMatthew Garrett 			if (err < 0) {
675b0f9d003SJohan Hovold 				dev_err(&port->dev,
676b0f9d003SJohan Hovold 					"%s: submit read urb %d failed: %d\n",
677b0f9d003SJohan Hovold 					__func__, i, err);
678fb7ad4f9SJohan Hovold 				err_count++;
6790d456194SMatthew Garrett 			}
6800d456194SMatthew Garrett 		}
6810d456194SMatthew Garrett 	}
6820d456194SMatthew Garrett 	intfdata->suspended = 0;
6830d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
684fb7ad4f9SJohan Hovold 
685fb7ad4f9SJohan Hovold 	if (err_count)
686fb7ad4f9SJohan Hovold 		return -EIO;
687fb7ad4f9SJohan Hovold 
688fb7ad4f9SJohan Hovold 	return 0;
6890d456194SMatthew Garrett }
6900d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume);
6910d456194SMatthew Garrett #endif
6920d456194SMatthew Garrett 
6930d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR);
6940d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC);
6950d456194SMatthew Garrett MODULE_LICENSE("GPL");
696