xref: /openbmc/linux/drivers/usb/serial/usb_wwan.c (revision c5d1448fa353242635fa3e1fed6ab4558e0e7d9a)
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 
13515cc7badSAl Viro int usb_wwan_get_serial_info(struct tty_struct *tty,
13615cc7badSAl Viro 			   struct serial_struct *ss)
13702303f73SDan Williams {
13815cc7badSAl Viro 	struct usb_serial_port *port = tty->driver_data;
13902303f73SDan Williams 
14015cc7badSAl Viro 	ss->line            = port->minor;
14115cc7badSAl Viro 	ss->port            = port->port_number;
14215cc7badSAl Viro 	ss->baud_base       = tty_get_baud_rate(port->port.tty);
14315cc7badSAl Viro 	ss->close_delay	    = port->port.close_delay / 10;
14415cc7badSAl Viro 	ss->closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
14502303f73SDan Williams 				 ASYNC_CLOSING_WAIT_NONE :
14602303f73SDan Williams 				 port->port.closing_wait / 10;
14702303f73SDan Williams 	return 0;
14802303f73SDan Williams }
14915cc7badSAl Viro EXPORT_SYMBOL(usb_wwan_get_serial_info);
15002303f73SDan Williams 
15115cc7badSAl Viro int usb_wwan_set_serial_info(struct tty_struct *tty,
15215cc7badSAl Viro 			   struct serial_struct *ss)
15302303f73SDan Williams {
15415cc7badSAl Viro 	struct usb_serial_port *port = tty->driver_data;
15502303f73SDan Williams 	unsigned int closing_wait, close_delay;
15602303f73SDan Williams 	int retval = 0;
15702303f73SDan Williams 
15815cc7badSAl Viro 	close_delay = ss->close_delay * 10;
15915cc7badSAl Viro 	closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
16015cc7badSAl Viro 			ASYNC_CLOSING_WAIT_NONE : ss->closing_wait * 10;
16102303f73SDan Williams 
16202303f73SDan Williams 	mutex_lock(&port->port.mutex);
16302303f73SDan Williams 
16402303f73SDan Williams 	if (!capable(CAP_SYS_ADMIN)) {
16502303f73SDan Williams 		if ((close_delay != port->port.close_delay) ||
16602303f73SDan Williams 		    (closing_wait != port->port.closing_wait))
16702303f73SDan Williams 			retval = -EPERM;
16802303f73SDan Williams 		else
16902303f73SDan Williams 			retval = -EOPNOTSUPP;
17002303f73SDan Williams 	} else {
17102303f73SDan Williams 		port->port.close_delay  = close_delay;
17202303f73SDan Williams 		port->port.closing_wait = closing_wait;
17302303f73SDan Williams 	}
17402303f73SDan Williams 
17502303f73SDan Williams 	mutex_unlock(&port->port.mutex);
17602303f73SDan Williams 	return retval;
17702303f73SDan Williams }
17815cc7badSAl Viro EXPORT_SYMBOL(usb_wwan_set_serial_info);
17902303f73SDan Williams 
1800d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
1810d456194SMatthew Garrett 		   const unsigned char *buf, int count)
1820d456194SMatthew Garrett {
1830d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
1840d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
1850d456194SMatthew Garrett 	int i;
1860d456194SMatthew Garrett 	int left, todo;
1870d456194SMatthew Garrett 	struct urb *this_urb = NULL;	/* spurious */
1880d456194SMatthew Garrett 	int err;
1890d456194SMatthew Garrett 	unsigned long flags;
1900d456194SMatthew Garrett 
1910d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
19237357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
1930d456194SMatthew Garrett 
194a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
1950d456194SMatthew Garrett 
1960d456194SMatthew Garrett 	i = 0;
1970d456194SMatthew Garrett 	left = count;
1980d456194SMatthew Garrett 	for (i = 0; left > 0 && i < N_OUT_URB; i++) {
1990d456194SMatthew Garrett 		todo = left;
2000d456194SMatthew Garrett 		if (todo > OUT_BUFLEN)
2010d456194SMatthew Garrett 			todo = OUT_BUFLEN;
2020d456194SMatthew Garrett 
2030d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
2040d456194SMatthew Garrett 		if (test_and_set_bit(i, &portdata->out_busy)) {
2050d456194SMatthew Garrett 			if (time_before(jiffies,
2060d456194SMatthew Garrett 					portdata->tx_start_time[i] + 10 * HZ))
2070d456194SMatthew Garrett 				continue;
2080d456194SMatthew Garrett 			usb_unlink_urb(this_urb);
2090d456194SMatthew Garrett 			continue;
2100d456194SMatthew Garrett 		}
211a80be97dSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
2120d456194SMatthew Garrett 			usb_pipeendpoint(this_urb->pipe), i);
2130d456194SMatthew Garrett 
2140d456194SMatthew Garrett 		err = usb_autopm_get_interface_async(port->serial->interface);
215db090473Sxiao jin 		if (err < 0) {
216db090473Sxiao jin 			clear_bit(i, &portdata->out_busy);
2170d456194SMatthew Garrett 			break;
218db090473Sxiao jin 		}
2190d456194SMatthew Garrett 
2200d456194SMatthew Garrett 		/* send the data */
2210d456194SMatthew Garrett 		memcpy(this_urb->transfer_buffer, buf, todo);
2220d456194SMatthew Garrett 		this_urb->transfer_buffer_length = todo;
2230d456194SMatthew Garrett 
2240d456194SMatthew Garrett 		spin_lock_irqsave(&intfdata->susp_lock, flags);
2250d456194SMatthew Garrett 		if (intfdata->suspended) {
2260d456194SMatthew Garrett 			usb_anchor_urb(this_urb, &portdata->delayed);
2270d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2280d456194SMatthew Garrett 		} else {
2290d456194SMatthew Garrett 			intfdata->in_flight++;
2300d456194SMatthew Garrett 			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2310d456194SMatthew Garrett 			err = usb_submit_urb(this_urb, GFP_ATOMIC);
2320d456194SMatthew Garrett 			if (err) {
2338bb7ec65SJohan Hovold 				dev_err(&port->dev,
2348bb7ec65SJohan Hovold 					"%s: submit urb %d failed: %d\n",
2358bb7ec65SJohan Hovold 					__func__, i, err);
2360d456194SMatthew Garrett 				clear_bit(i, &portdata->out_busy);
2370d456194SMatthew Garrett 				spin_lock_irqsave(&intfdata->susp_lock, flags);
2380d456194SMatthew Garrett 				intfdata->in_flight--;
2390d456194SMatthew Garrett 				spin_unlock_irqrestore(&intfdata->susp_lock,
2400d456194SMatthew Garrett 						       flags);
2413d06bf15SOliver Neukum 				usb_autopm_put_interface_async(port->serial->interface);
242433508aeSOliver Neukum 				break;
2430d456194SMatthew Garrett 			}
2440d456194SMatthew Garrett 		}
2450d456194SMatthew Garrett 
2460d456194SMatthew Garrett 		portdata->tx_start_time[i] = jiffies;
2470d456194SMatthew Garrett 		buf += todo;
2480d456194SMatthew Garrett 		left -= todo;
2490d456194SMatthew Garrett 	}
2500d456194SMatthew Garrett 
2510d456194SMatthew Garrett 	count -= left;
252a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
2530d456194SMatthew Garrett 	return count;
2540d456194SMatthew Garrett }
2550d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write);
2560d456194SMatthew Garrett 
2570d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb)
2580d456194SMatthew Garrett {
2590d456194SMatthew Garrett 	int err;
2600d456194SMatthew Garrett 	int endpoint;
2610d456194SMatthew Garrett 	struct usb_serial_port *port;
262a80be97dSGreg Kroah-Hartman 	struct device *dev;
2630d456194SMatthew Garrett 	unsigned char *data = urb->transfer_buffer;
2640d456194SMatthew Garrett 	int status = urb->status;
2650d456194SMatthew Garrett 
2660d456194SMatthew Garrett 	endpoint = usb_pipeendpoint(urb->pipe);
2670d456194SMatthew Garrett 	port = urb->context;
268a80be97dSGreg Kroah-Hartman 	dev = &port->dev;
2690d456194SMatthew Garrett 
2700d456194SMatthew Garrett 	if (status) {
271a80be97dSGreg Kroah-Hartman 		dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
2720d456194SMatthew Garrett 			__func__, status, endpoint);
273986c1748SBin Liu 
274986c1748SBin Liu 		/* don't resubmit on fatal errors */
275986c1748SBin Liu 		if (status == -ESHUTDOWN || status == -ENOENT)
276986c1748SBin Liu 			return;
2770d456194SMatthew Garrett 	} else {
2780d456194SMatthew Garrett 		if (urb->actual_length) {
27905c7cd39SJiri Slaby 			tty_insert_flip_string(&port->port, data,
28038237fd2SJiri Slaby 					urb->actual_length);
2812e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
2820d456194SMatthew Garrett 		} else
283a80be97dSGreg Kroah-Hartman 			dev_dbg(dev, "%s: empty read urb received\n", __func__);
2846c1ee66aSMatt Burtch 	}
2850d456194SMatthew Garrett 	/* Resubmit urb so we continue receiving */
2860d456194SMatthew Garrett 	err = usb_submit_urb(urb, GFP_ATOMIC);
287c9c4558fSOliver Neukum 	if (err) {
288d2958d1bSJohan Hovold 		if (err != -EPERM && err != -ENODEV) {
2896c1ee66aSMatt Burtch 			dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
2906c1ee66aSMatt Burtch 				__func__, err);
291c9c4558fSOliver Neukum 			/* busy also in error unless we are killed */
2920d456194SMatthew Garrett 			usb_mark_last_busy(port->serial->dev);
2930d456194SMatthew Garrett 		}
294c9c4558fSOliver Neukum 	} else {
295c9c4558fSOliver Neukum 		usb_mark_last_busy(port->serial->dev);
296c9c4558fSOliver Neukum 	}
297c9c4558fSOliver Neukum }
2980d456194SMatthew Garrett 
2990d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb)
3000d456194SMatthew Garrett {
3010d456194SMatthew Garrett 	struct usb_serial_port *port;
3020d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3030d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
304a323f946SJohn Ogness 	unsigned long flags;
3050d456194SMatthew Garrett 	int i;
3060d456194SMatthew Garrett 
3070d456194SMatthew Garrett 	port = urb->context;
30837357ca5SJohan Hovold 	intfdata = usb_get_serial_data(port->serial);
3090d456194SMatthew Garrett 
3100d456194SMatthew Garrett 	usb_serial_port_softint(port);
3110d456194SMatthew Garrett 	usb_autopm_put_interface_async(port->serial->interface);
3120d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
313a323f946SJohn Ogness 	spin_lock_irqsave(&intfdata->susp_lock, flags);
3140d456194SMatthew Garrett 	intfdata->in_flight--;
315a323f946SJohn Ogness 	spin_unlock_irqrestore(&intfdata->susp_lock, flags);
3160d456194SMatthew Garrett 
3170d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; ++i) {
3180d456194SMatthew Garrett 		if (portdata->out_urbs[i] == urb) {
3194e857c58SPeter Zijlstra 			smp_mb__before_atomic();
3200d456194SMatthew Garrett 			clear_bit(i, &portdata->out_busy);
3210d456194SMatthew Garrett 			break;
3220d456194SMatthew Garrett 		}
3230d456194SMatthew Garrett 	}
3240d456194SMatthew Garrett }
3250d456194SMatthew Garrett 
3260d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty)
3270d456194SMatthew Garrett {
3280d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3290d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3300d456194SMatthew Garrett 	int i;
3310d456194SMatthew Garrett 	int data_len = 0;
3320d456194SMatthew Garrett 	struct urb *this_urb;
3330d456194SMatthew Garrett 
3340d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3350d456194SMatthew Garrett 
3360d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3370d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3380d456194SMatthew Garrett 		if (this_urb && !test_bit(i, &portdata->out_busy))
3390d456194SMatthew Garrett 			data_len += OUT_BUFLEN;
3400d456194SMatthew Garrett 	}
3410d456194SMatthew Garrett 
342a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3430d456194SMatthew Garrett 	return data_len;
3440d456194SMatthew Garrett }
3450d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room);
3460d456194SMatthew Garrett 
3470d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty)
3480d456194SMatthew Garrett {
3490d456194SMatthew Garrett 	struct usb_serial_port *port = tty->driver_data;
3500d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3510d456194SMatthew Garrett 	int i;
3520d456194SMatthew Garrett 	int data_len = 0;
3530d456194SMatthew Garrett 	struct urb *this_urb;
3540d456194SMatthew Garrett 
3550d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
3560d456194SMatthew Garrett 
3570d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++) {
3580d456194SMatthew Garrett 		this_urb = portdata->out_urbs[i];
3590d456194SMatthew Garrett 		/* FIXME: This locking is insufficient as this_urb may
3600d456194SMatthew Garrett 		   go unused during the test */
3610d456194SMatthew Garrett 		if (this_urb && test_bit(i, &portdata->out_busy))
3620d456194SMatthew Garrett 			data_len += this_urb->transfer_buffer_length;
3630d456194SMatthew Garrett 	}
364a80be97dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
3650d456194SMatthew Garrett 	return data_len;
3660d456194SMatthew Garrett }
3670d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
3680d456194SMatthew Garrett 
3690d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
3700d456194SMatthew Garrett {
3710d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
3720d456194SMatthew Garrett 	struct usb_wwan_intf_private *intfdata;
3730d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
3740d456194SMatthew Garrett 	int i, err;
3750d456194SMatthew Garrett 	struct urb *urb;
3760d456194SMatthew Garrett 
3770d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
37837357ca5SJohan Hovold 	intfdata = usb_get_serial_data(serial);
3790d456194SMatthew Garrett 
3809096f1fbSJohan Hovold 	if (port->interrupt_in_urb) {
3819096f1fbSJohan Hovold 		err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
3829096f1fbSJohan Hovold 		if (err) {
3838bb7ec65SJohan Hovold 			dev_err(&port->dev, "%s: submit int urb failed: %d\n",
3849096f1fbSJohan Hovold 				__func__, err);
3859096f1fbSJohan Hovold 		}
3869096f1fbSJohan Hovold 	}
3879096f1fbSJohan Hovold 
3880d456194SMatthew Garrett 	/* Start reading from the IN endpoint */
3890d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++) {
3900d456194SMatthew Garrett 		urb = portdata->in_urbs[i];
3910d456194SMatthew Garrett 		if (!urb)
3920d456194SMatthew Garrett 			continue;
3930d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_KERNEL);
3940d456194SMatthew Garrett 		if (err) {
3958bb7ec65SJohan Hovold 			dev_err(&port->dev,
3968bb7ec65SJohan Hovold 				"%s: submit read urb %d failed: %d\n",
3978bb7ec65SJohan Hovold 				__func__, i, err);
3980d456194SMatthew Garrett 		}
3990d456194SMatthew Garrett 	}
4000d456194SMatthew Garrett 
4010d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
402c1c01803SJohan Hovold 	if (++intfdata->open_ports == 1)
403c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 1;
4040d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4059a91aedcSOliver Neukum 	/* this balances a get in the generic USB serial code */
4060d456194SMatthew Garrett 	usb_autopm_put_interface(serial->interface);
4070d456194SMatthew Garrett 
4080d456194SMatthew Garrett 	return 0;
4090d456194SMatthew Garrett }
4100d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open);
4110d456194SMatthew Garrett 
41279eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb,
41379eed03eSJohan Hovold 					struct usb_wwan_port_private *portdata)
41479eed03eSJohan Hovold {
41579eed03eSJohan Hovold 	int i;
41679eed03eSJohan Hovold 
41779eed03eSJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
41879eed03eSJohan Hovold 		if (urb == portdata->out_urbs[i]) {
41979eed03eSJohan Hovold 			clear_bit(i, &portdata->out_busy);
42079eed03eSJohan Hovold 			break;
42179eed03eSJohan Hovold 		}
42279eed03eSJohan Hovold 	}
42379eed03eSJohan Hovold }
42479eed03eSJohan Hovold 
4250d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port)
4260d456194SMatthew Garrett {
4270d456194SMatthew Garrett 	int i;
4280d456194SMatthew Garrett 	struct usb_serial *serial = port->serial;
4290d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
43037357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
43179eed03eSJohan Hovold 	struct urb *urb;
4320d456194SMatthew Garrett 
4330d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
4340d456194SMatthew Garrett 
435b0a9aa6dSJohan Hovold 	/*
436b0a9aa6dSJohan Hovold 	 * Need to take susp_lock to make sure port is not already being
437d41861caSPeter Hurley 	 * resumed, but no need to hold it due to initialized
438b0a9aa6dSJohan Hovold 	 */
4390d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
440c1c01803SJohan Hovold 	if (--intfdata->open_ports == 0)
441c1c01803SJohan Hovold 		serial->interface->needs_remote_wakeup = 0;
4420d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
4430d456194SMatthew Garrett 
44479eed03eSJohan Hovold 	for (;;) {
44579eed03eSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
44679eed03eSJohan Hovold 		if (!urb)
44779eed03eSJohan Hovold 			break;
44879eed03eSJohan Hovold 		unbusy_queued_urb(urb, portdata);
44979eed03eSJohan Hovold 		usb_autopm_put_interface_async(serial->interface);
45079eed03eSJohan Hovold 	}
45179eed03eSJohan Hovold 
4520d456194SMatthew Garrett 	for (i = 0; i < N_IN_URB; i++)
4530d456194SMatthew Garrett 		usb_kill_urb(portdata->in_urbs[i]);
4540d456194SMatthew Garrett 	for (i = 0; i < N_OUT_URB; i++)
4550d456194SMatthew Garrett 		usb_kill_urb(portdata->out_urbs[i]);
4569096f1fbSJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
457e6d144bcSJohan Hovold 
4589a91aedcSOliver Neukum 	usb_autopm_get_interface_no_resume(serial->interface);
4590d456194SMatthew Garrett }
4600d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close);
4610d456194SMatthew Garrett 
462b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
463b8f0e820SJohan Hovold 				      int endpoint,
4640d456194SMatthew Garrett 				      int dir, void *ctx, char *buf, int len,
4650d456194SMatthew Garrett 				      void (*callback) (struct urb *))
4660d456194SMatthew Garrett {
467b8f0e820SJohan Hovold 	struct usb_serial *serial = port->serial;
4682438c3a1SDaniele Palmas 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
4690d456194SMatthew Garrett 	struct urb *urb;
4700d456194SMatthew Garrett 
4710d456194SMatthew Garrett 	urb = usb_alloc_urb(0, GFP_KERNEL);	/* No ISO */
47210c642d0SJohan Hovold 	if (!urb)
4730d456194SMatthew Garrett 		return NULL;
4740d456194SMatthew Garrett 
4750d456194SMatthew Garrett 	usb_fill_bulk_urb(urb, serial->dev,
4760d456194SMatthew Garrett 			  usb_sndbulkpipe(serial->dev, endpoint) | dir,
4770d456194SMatthew Garrett 			  buf, len, callback, ctx);
4780d456194SMatthew Garrett 
4792438c3a1SDaniele Palmas 	if (intfdata->use_zlp && dir == USB_DIR_OUT)
4802438c3a1SDaniele Palmas 		urb->transfer_flags |= URB_ZERO_PACKET;
4812438c3a1SDaniele Palmas 
4820d456194SMatthew Garrett 	return urb;
4830d456194SMatthew Garrett }
4840d456194SMatthew Garrett 
485b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port)
4860d456194SMatthew Garrett {
4870d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
488b8f0e820SJohan Hovold 	struct urb *urb;
4890d456194SMatthew Garrett 	u8 *buffer;
490b8f0e820SJohan Hovold 	int i;
4910d456194SMatthew Garrett 
492bd73bd88SJohan Hovold 	if (!port->bulk_in_size || !port->bulk_out_size)
493bd73bd88SJohan Hovold 		return -ENODEV;
494bd73bd88SJohan Hovold 
4950d456194SMatthew Garrett 	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
496b8f0e820SJohan Hovold 	if (!portdata)
497b8f0e820SJohan Hovold 		return -ENOMEM;
498b8f0e820SJohan Hovold 
4990d456194SMatthew Garrett 	init_usb_anchor(&portdata->delayed);
5000d456194SMatthew Garrett 
501b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
5020d456194SMatthew Garrett 		buffer = (u8 *)__get_free_page(GFP_KERNEL);
5030d456194SMatthew Garrett 		if (!buffer)
5040d456194SMatthew Garrett 			goto bail_out_error;
505b8f0e820SJohan Hovold 		portdata->in_buffer[i] = buffer;
506b8f0e820SJohan Hovold 
507b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
508b8f0e820SJohan Hovold 						USB_DIR_IN, port,
509b8f0e820SJohan Hovold 						buffer, IN_BUFLEN,
510b8f0e820SJohan Hovold 						usb_wwan_indat_callback);
511b8f0e820SJohan Hovold 		portdata->in_urbs[i] = urb;
5120d456194SMatthew Garrett 	}
5130d456194SMatthew Garrett 
514b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
5150d456194SMatthew Garrett 		buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
5160d456194SMatthew Garrett 		if (!buffer)
5170d456194SMatthew Garrett 			goto bail_out_error2;
518b8f0e820SJohan Hovold 		portdata->out_buffer[i] = buffer;
519b8f0e820SJohan Hovold 
520b8f0e820SJohan Hovold 		urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
521b8f0e820SJohan Hovold 						USB_DIR_OUT, port,
522b8f0e820SJohan Hovold 						buffer, OUT_BUFLEN,
523b8f0e820SJohan Hovold 						usb_wwan_outdat_callback);
524b8f0e820SJohan Hovold 		portdata->out_urbs[i] = urb;
5250d456194SMatthew Garrett 	}
5260d456194SMatthew Garrett 
5270d456194SMatthew Garrett 	usb_set_serial_port_data(port, portdata);
5280d456194SMatthew Garrett 
5290d456194SMatthew Garrett 	return 0;
5300d456194SMatthew Garrett 
5310d456194SMatthew Garrett bail_out_error2:
532b8f0e820SJohan Hovold 	for (i = 0; i < N_OUT_URB; i++) {
533b8f0e820SJohan Hovold 		usb_free_urb(portdata->out_urbs[i]);
534b8f0e820SJohan Hovold 		kfree(portdata->out_buffer[i]);
5350d456194SMatthew Garrett 	}
536b8f0e820SJohan Hovold bail_out_error:
537b8f0e820SJohan Hovold 	for (i = 0; i < N_IN_URB; i++) {
538b8f0e820SJohan Hovold 		usb_free_urb(portdata->in_urbs[i]);
539b8f0e820SJohan Hovold 		free_page((unsigned long)portdata->in_buffer[i]);
540b8f0e820SJohan Hovold 	}
541b8f0e820SJohan Hovold 	kfree(portdata);
542b8f0e820SJohan Hovold 
543b8f0e820SJohan Hovold 	return -ENOMEM;
544b8f0e820SJohan Hovold }
545b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
5460d456194SMatthew Garrett 
547*c5d1448fSUwe Kleine-König void usb_wwan_port_remove(struct usb_serial_port *port)
548a1028f0aSBjørn Mork {
549a1028f0aSBjørn Mork 	int i;
550a1028f0aSBjørn Mork 	struct usb_wwan_port_private *portdata;
551a1028f0aSBjørn Mork 
552a1028f0aSBjørn Mork 	portdata = usb_get_serial_port_data(port);
553a1028f0aSBjørn Mork 	usb_set_serial_port_data(port, NULL);
554a1028f0aSBjørn Mork 
555a1028f0aSBjørn Mork 	for (i = 0; i < N_IN_URB; i++) {
556a1028f0aSBjørn Mork 		usb_free_urb(portdata->in_urbs[i]);
557a1028f0aSBjørn Mork 		free_page((unsigned long)portdata->in_buffer[i]);
558a1028f0aSBjørn Mork 	}
559a1028f0aSBjørn Mork 	for (i = 0; i < N_OUT_URB; i++) {
560a1028f0aSBjørn Mork 		usb_free_urb(portdata->out_urbs[i]);
561a1028f0aSBjørn Mork 		kfree(portdata->out_buffer[i]);
562a1028f0aSBjørn Mork 	}
563a1028f0aSBjørn Mork 
564a1028f0aSBjørn Mork 	kfree(portdata);
565a1028f0aSBjørn Mork }
566a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove);
567a1028f0aSBjørn Mork 
568a1028f0aSBjørn Mork #ifdef CONFIG_PM
569ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial)
5700d456194SMatthew Garrett {
5710d456194SMatthew Garrett 	int i, j;
5720d456194SMatthew Garrett 	struct usb_serial_port *port;
5730d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
5740d456194SMatthew Garrett 
5750d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; ++i) {
5760d456194SMatthew Garrett 		port = serial->port[i];
5770d456194SMatthew Garrett 		portdata = usb_get_serial_port_data(port);
578032129cbSBjørn Mork 		if (!portdata)
579032129cbSBjørn Mork 			continue;
5800d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++)
5810d456194SMatthew Garrett 			usb_kill_urb(portdata->in_urbs[j]);
5820d456194SMatthew Garrett 		for (j = 0; j < N_OUT_URB; j++)
5830d456194SMatthew Garrett 			usb_kill_urb(portdata->out_urbs[j]);
584ae75c940SJohan Hovold 		usb_kill_urb(port->interrupt_in_urb);
5850d456194SMatthew Garrett 	}
5860d456194SMatthew Garrett }
5870d456194SMatthew Garrett 
5880d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
5890d456194SMatthew Garrett {
59037357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
5910d456194SMatthew Garrett 
5920d456194SMatthew Garrett 	spin_lock_irq(&intfdata->susp_lock);
593170fad9eSJohan Hovold 	if (PMSG_IS_AUTO(message)) {
594170fad9eSJohan Hovold 		if (intfdata->in_flight) {
5950d456194SMatthew Garrett 			spin_unlock_irq(&intfdata->susp_lock);
5960d456194SMatthew Garrett 			return -EBUSY;
5970d456194SMatthew Garrett 		}
598170fad9eSJohan Hovold 	}
5990d456194SMatthew Garrett 	intfdata->suspended = 1;
6000d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
601170fad9eSJohan Hovold 
602ae75c940SJohan Hovold 	stop_urbs(serial);
6030d456194SMatthew Garrett 
6040d456194SMatthew Garrett 	return 0;
6050d456194SMatthew Garrett }
6060d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend);
6070d456194SMatthew Garrett 
6083362c91cSJohan Hovold /* Caller must hold susp_lock. */
6093362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
6100d456194SMatthew Garrett {
6117436f412SJohan Hovold 	struct usb_serial *serial = port->serial;
61237357ca5SJohan Hovold 	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
6130d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6140d456194SMatthew Garrett 	struct urb *urb;
6157436f412SJohan Hovold 	int err_count = 0;
6167436f412SJohan Hovold 	int err;
6170d456194SMatthew Garrett 
6180d456194SMatthew Garrett 	portdata = usb_get_serial_port_data(port);
61937357ca5SJohan Hovold 
6203362c91cSJohan Hovold 	for (;;) {
6213362c91cSJohan Hovold 		urb = usb_get_from_anchor(&portdata->delayed);
6223362c91cSJohan Hovold 		if (!urb)
6233362c91cSJohan Hovold 			break;
6243362c91cSJohan Hovold 
6250d456194SMatthew Garrett 		err = usb_submit_urb(urb, GFP_ATOMIC);
6267436f412SJohan Hovold 		if (err) {
6273362c91cSJohan Hovold 			dev_err(&port->dev, "%s: submit urb failed: %d\n",
6287436f412SJohan Hovold 					__func__, err);
6297436f412SJohan Hovold 			err_count++;
63016871dcaSOliver Neukum 			unbusy_queued_urb(urb, portdata);
6317436f412SJohan Hovold 			usb_autopm_put_interface_async(serial->interface);
6327436f412SJohan Hovold 			continue;
63316871dcaSOliver Neukum 		}
6347436f412SJohan Hovold 		data->in_flight++;
6350d456194SMatthew Garrett 	}
636fb7ad4f9SJohan Hovold 
6377436f412SJohan Hovold 	if (err_count)
6387436f412SJohan Hovold 		return -EIO;
6397436f412SJohan Hovold 
6407436f412SJohan Hovold 	return 0;
6410d456194SMatthew Garrett }
6420d456194SMatthew Garrett 
6430d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial)
6440d456194SMatthew Garrett {
6450d456194SMatthew Garrett 	int i, j;
6460d456194SMatthew Garrett 	struct usb_serial_port *port;
64737357ca5SJohan Hovold 	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6480d456194SMatthew Garrett 	struct usb_wwan_port_private *portdata;
6490d456194SMatthew Garrett 	struct urb *urb;
650fb7ad4f9SJohan Hovold 	int err;
651fb7ad4f9SJohan Hovold 	int err_count = 0;
6520d456194SMatthew Garrett 
653d9e93c08Sxiao jin 	spin_lock_irq(&intfdata->susp_lock);
6540d456194SMatthew Garrett 	for (i = 0; i < serial->num_ports; i++) {
6550d456194SMatthew Garrett 		port = serial->port[i];
6560d456194SMatthew Garrett 
657d41861caSPeter Hurley 		if (!tty_port_initialized(&port->port))
6580d456194SMatthew Garrett 			continue;
6590d456194SMatthew Garrett 
660b0a9aa6dSJohan Hovold 		portdata = usb_get_serial_port_data(port);
661b0a9aa6dSJohan Hovold 
6629096f1fbSJohan Hovold 		if (port->interrupt_in_urb) {
6639096f1fbSJohan Hovold 			err = usb_submit_urb(port->interrupt_in_urb,
6649096f1fbSJohan Hovold 					GFP_ATOMIC);
6659096f1fbSJohan Hovold 			if (err) {
6669096f1fbSJohan Hovold 				dev_err(&port->dev,
6679096f1fbSJohan Hovold 					"%s: submit int urb failed: %d\n",
6689096f1fbSJohan Hovold 					__func__, err);
669fb7ad4f9SJohan Hovold 				err_count++;
6709096f1fbSJohan Hovold 			}
6719096f1fbSJohan Hovold 		}
6729096f1fbSJohan Hovold 
6733362c91cSJohan Hovold 		err = usb_wwan_submit_delayed_urbs(port);
674fb7ad4f9SJohan Hovold 		if (err)
675fb7ad4f9SJohan Hovold 			err_count++;
676fb7ad4f9SJohan Hovold 
6770d456194SMatthew Garrett 		for (j = 0; j < N_IN_URB; j++) {
6780d456194SMatthew Garrett 			urb = portdata->in_urbs[j];
6790d456194SMatthew Garrett 			err = usb_submit_urb(urb, GFP_ATOMIC);
6800d456194SMatthew Garrett 			if (err < 0) {
681b0f9d003SJohan Hovold 				dev_err(&port->dev,
682b0f9d003SJohan Hovold 					"%s: submit read urb %d failed: %d\n",
683b0f9d003SJohan Hovold 					__func__, i, err);
684fb7ad4f9SJohan Hovold 				err_count++;
6850d456194SMatthew Garrett 			}
6860d456194SMatthew Garrett 		}
6870d456194SMatthew Garrett 	}
6880d456194SMatthew Garrett 	intfdata->suspended = 0;
6890d456194SMatthew Garrett 	spin_unlock_irq(&intfdata->susp_lock);
690fb7ad4f9SJohan Hovold 
691fb7ad4f9SJohan Hovold 	if (err_count)
692fb7ad4f9SJohan Hovold 		return -EIO;
693fb7ad4f9SJohan Hovold 
694fb7ad4f9SJohan Hovold 	return 0;
6950d456194SMatthew Garrett }
6960d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume);
6970d456194SMatthew Garrett #endif
6980d456194SMatthew Garrett 
6990d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR);
7000d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC);
701627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
702