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>
32d5e22360SYan Xinyu #include <linux/usb/cdc.h>
330d456194SMatthew Garrett #include <linux/usb/serial.h>
3402303f73SDan Williams #include <linux/serial.h>
350d456194SMatthew Garrett #include "usb-wwan.h"
360d456194SMatthew Garrett
37669e729fSDavid Ward /*
38669e729fSDavid Ward * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
39669e729fSDavid Ward * in CDC ACM.
40669e729fSDavid Ward */
usb_wwan_send_setup(struct usb_serial_port * port)41669e729fSDavid Ward static int usb_wwan_send_setup(struct usb_serial_port *port)
42669e729fSDavid Ward {
43669e729fSDavid Ward struct usb_serial *serial = port->serial;
44669e729fSDavid Ward struct usb_wwan_port_private *portdata;
45669e729fSDavid Ward int val = 0;
46669e729fSDavid Ward int ifnum;
47669e729fSDavid Ward int res;
48669e729fSDavid Ward
49669e729fSDavid Ward portdata = usb_get_serial_port_data(port);
50669e729fSDavid Ward
51669e729fSDavid Ward if (portdata->dtr_state)
52d5e22360SYan Xinyu val |= USB_CDC_CTRL_DTR;
53669e729fSDavid Ward if (portdata->rts_state)
54d5e22360SYan Xinyu val |= USB_CDC_CTRL_RTS;
55669e729fSDavid Ward
56669e729fSDavid Ward ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
57669e729fSDavid Ward
58669e729fSDavid Ward res = usb_autopm_get_interface(serial->interface);
59669e729fSDavid Ward if (res)
60669e729fSDavid Ward return res;
61669e729fSDavid Ward
62669e729fSDavid Ward res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
63d5e22360SYan Xinyu USB_CDC_REQ_SET_CONTROL_LINE_STATE,
64d5e22360SYan Xinyu USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
65d5e22360SYan Xinyu val, ifnum, NULL, 0, USB_CTRL_SET_TIMEOUT);
66669e729fSDavid Ward
67669e729fSDavid Ward usb_autopm_put_interface(port->serial->interface);
68669e729fSDavid Ward
69669e729fSDavid Ward return res;
70669e729fSDavid Ward }
71669e729fSDavid Ward
usb_wwan_dtr_rts(struct usb_serial_port * port,int on)720d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
730d456194SMatthew Garrett {
740d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
750d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata;
760d456194SMatthew Garrett
7737357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial);
780d456194SMatthew Garrett
79669e729fSDavid Ward if (!intfdata->use_send_setup)
800d456194SMatthew Garrett return;
810d456194SMatthew Garrett
820d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
83b2ca6990SJohan Hovold /* FIXME: locking */
840d456194SMatthew Garrett portdata->rts_state = on;
850d456194SMatthew Garrett portdata->dtr_state = on;
86b2ca6990SJohan Hovold
87669e729fSDavid Ward usb_wwan_send_setup(port);
880d456194SMatthew Garrett }
890d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts);
900d456194SMatthew Garrett
usb_wwan_tiocmget(struct tty_struct * tty)9160b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty)
920d456194SMatthew Garrett {
930d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data;
940d456194SMatthew Garrett unsigned int value;
950d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
960d456194SMatthew Garrett
970d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
980d456194SMatthew Garrett
990d456194SMatthew Garrett value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
1000d456194SMatthew Garrett ((portdata->dtr_state) ? TIOCM_DTR : 0) |
1010d456194SMatthew Garrett ((portdata->cts_state) ? TIOCM_CTS : 0) |
1020d456194SMatthew Garrett ((portdata->dsr_state) ? TIOCM_DSR : 0) |
1030d456194SMatthew Garrett ((portdata->dcd_state) ? TIOCM_CAR : 0) |
1040d456194SMatthew Garrett ((portdata->ri_state) ? TIOCM_RNG : 0);
1050d456194SMatthew Garrett
1060d456194SMatthew Garrett return value;
1070d456194SMatthew Garrett }
1080d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget);
1090d456194SMatthew Garrett
usb_wwan_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)11020b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty,
1110d456194SMatthew Garrett unsigned int set, unsigned int clear)
1120d456194SMatthew Garrett {
1130d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data;
1140d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
1150d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata;
1160d456194SMatthew Garrett
1170d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
11837357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial);
1190d456194SMatthew Garrett
120669e729fSDavid Ward if (!intfdata->use_send_setup)
1210d456194SMatthew Garrett return -EINVAL;
1220d456194SMatthew Garrett
1230d456194SMatthew Garrett /* FIXME: what locks portdata fields ? */
1240d456194SMatthew Garrett if (set & TIOCM_RTS)
1250d456194SMatthew Garrett portdata->rts_state = 1;
1260d456194SMatthew Garrett if (set & TIOCM_DTR)
1270d456194SMatthew Garrett portdata->dtr_state = 1;
1280d456194SMatthew Garrett
1290d456194SMatthew Garrett if (clear & TIOCM_RTS)
1300d456194SMatthew Garrett portdata->rts_state = 0;
1310d456194SMatthew Garrett if (clear & TIOCM_DTR)
1320d456194SMatthew Garrett portdata->dtr_state = 0;
133669e729fSDavid Ward return usb_wwan_send_setup(port);
1340d456194SMatthew Garrett }
1350d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset);
1360d456194SMatthew Garrett
usb_wwan_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)1370d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
1380d456194SMatthew Garrett const unsigned char *buf, int count)
1390d456194SMatthew Garrett {
1400d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
1410d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata;
1420d456194SMatthew Garrett int i;
1430d456194SMatthew Garrett int left, todo;
1440d456194SMatthew Garrett struct urb *this_urb = NULL; /* spurious */
1450d456194SMatthew Garrett int err;
1460d456194SMatthew Garrett unsigned long flags;
1470d456194SMatthew Garrett
1480d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
14937357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial);
1500d456194SMatthew Garrett
151a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
1520d456194SMatthew Garrett
1530d456194SMatthew Garrett left = count;
1540d456194SMatthew Garrett for (i = 0; left > 0 && i < N_OUT_URB; i++) {
1550d456194SMatthew Garrett todo = left;
1560d456194SMatthew Garrett if (todo > OUT_BUFLEN)
1570d456194SMatthew Garrett todo = OUT_BUFLEN;
1580d456194SMatthew Garrett
1590d456194SMatthew Garrett this_urb = portdata->out_urbs[i];
1600d456194SMatthew Garrett if (test_and_set_bit(i, &portdata->out_busy)) {
1610d456194SMatthew Garrett if (time_before(jiffies,
1620d456194SMatthew Garrett portdata->tx_start_time[i] + 10 * HZ))
1630d456194SMatthew Garrett continue;
1640d456194SMatthew Garrett usb_unlink_urb(this_urb);
1650d456194SMatthew Garrett continue;
1660d456194SMatthew Garrett }
167a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
1680d456194SMatthew Garrett usb_pipeendpoint(this_urb->pipe), i);
1690d456194SMatthew Garrett
1700d456194SMatthew Garrett err = usb_autopm_get_interface_async(port->serial->interface);
171db090473Sxiao jin if (err < 0) {
172db090473Sxiao jin clear_bit(i, &portdata->out_busy);
1730d456194SMatthew Garrett break;
174db090473Sxiao jin }
1750d456194SMatthew Garrett
1760d456194SMatthew Garrett /* send the data */
1770d456194SMatthew Garrett memcpy(this_urb->transfer_buffer, buf, todo);
1780d456194SMatthew Garrett this_urb->transfer_buffer_length = todo;
1790d456194SMatthew Garrett
1800d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags);
1810d456194SMatthew Garrett if (intfdata->suspended) {
1820d456194SMatthew Garrett usb_anchor_urb(this_urb, &portdata->delayed);
1830d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags);
1840d456194SMatthew Garrett } else {
1850d456194SMatthew Garrett intfdata->in_flight++;
1860d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags);
1870d456194SMatthew Garrett err = usb_submit_urb(this_urb, GFP_ATOMIC);
1880d456194SMatthew Garrett if (err) {
1898bb7ec65SJohan Hovold dev_err(&port->dev,
1908bb7ec65SJohan Hovold "%s: submit urb %d failed: %d\n",
1918bb7ec65SJohan Hovold __func__, i, err);
1920d456194SMatthew Garrett clear_bit(i, &portdata->out_busy);
1930d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags);
1940d456194SMatthew Garrett intfdata->in_flight--;
1950d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock,
1960d456194SMatthew Garrett flags);
1973d06bf15SOliver Neukum usb_autopm_put_interface_async(port->serial->interface);
198433508aeSOliver Neukum break;
1990d456194SMatthew Garrett }
2000d456194SMatthew Garrett }
2010d456194SMatthew Garrett
2020d456194SMatthew Garrett portdata->tx_start_time[i] = jiffies;
2030d456194SMatthew Garrett buf += todo;
2040d456194SMatthew Garrett left -= todo;
2050d456194SMatthew Garrett }
2060d456194SMatthew Garrett
2070d456194SMatthew Garrett count -= left;
208a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
2090d456194SMatthew Garrett return count;
2100d456194SMatthew Garrett }
2110d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write);
2120d456194SMatthew Garrett
usb_wwan_indat_callback(struct urb * urb)2130d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb)
2140d456194SMatthew Garrett {
2150d456194SMatthew Garrett int err;
2160d456194SMatthew Garrett int endpoint;
2170d456194SMatthew Garrett struct usb_serial_port *port;
218a80be97dSGreg Kroah-Hartman struct device *dev;
2190d456194SMatthew Garrett unsigned char *data = urb->transfer_buffer;
2200d456194SMatthew Garrett int status = urb->status;
2210d456194SMatthew Garrett
2220d456194SMatthew Garrett endpoint = usb_pipeendpoint(urb->pipe);
2230d456194SMatthew Garrett port = urb->context;
224a80be97dSGreg Kroah-Hartman dev = &port->dev;
2250d456194SMatthew Garrett
2260d456194SMatthew Garrett if (status) {
227a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
2280d456194SMatthew Garrett __func__, status, endpoint);
229986c1748SBin Liu
230986c1748SBin Liu /* don't resubmit on fatal errors */
231986c1748SBin Liu if (status == -ESHUTDOWN || status == -ENOENT)
232986c1748SBin Liu return;
2330d456194SMatthew Garrett } else {
2340d456194SMatthew Garrett if (urb->actual_length) {
23505c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data,
23638237fd2SJiri Slaby urb->actual_length);
2372e124b4aSJiri Slaby tty_flip_buffer_push(&port->port);
2380d456194SMatthew Garrett } else
239a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: empty read urb received\n", __func__);
2406c1ee66aSMatt Burtch }
2410d456194SMatthew Garrett /* Resubmit urb so we continue receiving */
2420d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC);
243c9c4558fSOliver Neukum if (err) {
244d2958d1bSJohan Hovold if (err != -EPERM && err != -ENODEV) {
2456c1ee66aSMatt Burtch dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
2466c1ee66aSMatt Burtch __func__, err);
247c9c4558fSOliver Neukum /* busy also in error unless we are killed */
2480d456194SMatthew Garrett usb_mark_last_busy(port->serial->dev);
2490d456194SMatthew Garrett }
250c9c4558fSOliver Neukum } else {
251c9c4558fSOliver Neukum usb_mark_last_busy(port->serial->dev);
252c9c4558fSOliver Neukum }
253c9c4558fSOliver Neukum }
2540d456194SMatthew Garrett
usb_wwan_outdat_callback(struct urb * urb)2550d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb)
2560d456194SMatthew Garrett {
2570d456194SMatthew Garrett struct usb_serial_port *port;
2580d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
2590d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata;
260a323f946SJohn Ogness unsigned long flags;
2610d456194SMatthew Garrett int i;
2620d456194SMatthew Garrett
2630d456194SMatthew Garrett port = urb->context;
26437357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial);
2650d456194SMatthew Garrett
2660d456194SMatthew Garrett usb_serial_port_softint(port);
2670d456194SMatthew Garrett usb_autopm_put_interface_async(port->serial->interface);
2680d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
269a323f946SJohn Ogness spin_lock_irqsave(&intfdata->susp_lock, flags);
2700d456194SMatthew Garrett intfdata->in_flight--;
271a323f946SJohn Ogness spin_unlock_irqrestore(&intfdata->susp_lock, flags);
2720d456194SMatthew Garrett
2730d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; ++i) {
2740d456194SMatthew Garrett if (portdata->out_urbs[i] == urb) {
2754e857c58SPeter Zijlstra smp_mb__before_atomic();
2760d456194SMatthew Garrett clear_bit(i, &portdata->out_busy);
2770d456194SMatthew Garrett break;
2780d456194SMatthew Garrett }
2790d456194SMatthew Garrett }
2800d456194SMatthew Garrett }
2810d456194SMatthew Garrett
usb_wwan_write_room(struct tty_struct * tty)28294cc7aeaSJiri Slaby unsigned int usb_wwan_write_room(struct tty_struct *tty)
2830d456194SMatthew Garrett {
2840d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data;
2850d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
2860d456194SMatthew Garrett int i;
28794cc7aeaSJiri Slaby unsigned int data_len = 0;
2880d456194SMatthew Garrett struct urb *this_urb;
2890d456194SMatthew Garrett
2900d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
2910d456194SMatthew Garrett
2920d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) {
2930d456194SMatthew Garrett this_urb = portdata->out_urbs[i];
2940d456194SMatthew Garrett if (this_urb && !test_bit(i, &portdata->out_busy))
2950d456194SMatthew Garrett data_len += OUT_BUFLEN;
2960d456194SMatthew Garrett }
2970d456194SMatthew Garrett
29894cc7aeaSJiri Slaby dev_dbg(&port->dev, "%s: %u\n", __func__, data_len);
2990d456194SMatthew Garrett return data_len;
3000d456194SMatthew Garrett }
3010d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room);
3020d456194SMatthew Garrett
usb_wwan_chars_in_buffer(struct tty_struct * tty)303155591d3SJiri Slaby unsigned int usb_wwan_chars_in_buffer(struct tty_struct *tty)
3040d456194SMatthew Garrett {
3050d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data;
3060d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
3070d456194SMatthew Garrett int i;
308155591d3SJiri Slaby unsigned int data_len = 0;
3090d456194SMatthew Garrett struct urb *this_urb;
3100d456194SMatthew Garrett
3110d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
3120d456194SMatthew Garrett
3130d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) {
3140d456194SMatthew Garrett this_urb = portdata->out_urbs[i];
3150d456194SMatthew Garrett /* FIXME: This locking is insufficient as this_urb may
3160d456194SMatthew Garrett go unused during the test */
3170d456194SMatthew Garrett if (this_urb && test_bit(i, &portdata->out_busy))
3180d456194SMatthew Garrett data_len += this_urb->transfer_buffer_length;
3190d456194SMatthew Garrett }
320155591d3SJiri Slaby dev_dbg(&port->dev, "%s: %u\n", __func__, data_len);
3210d456194SMatthew Garrett return data_len;
3220d456194SMatthew Garrett }
3230d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
3240d456194SMatthew Garrett
usb_wwan_open(struct tty_struct * tty,struct usb_serial_port * port)3250d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
3260d456194SMatthew Garrett {
3270d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
3280d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata;
3290d456194SMatthew Garrett struct usb_serial *serial = port->serial;
3300d456194SMatthew Garrett int i, err;
3310d456194SMatthew Garrett struct urb *urb;
3320d456194SMatthew Garrett
3330d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
33437357ca5SJohan Hovold intfdata = usb_get_serial_data(serial);
3350d456194SMatthew Garrett
3369096f1fbSJohan Hovold if (port->interrupt_in_urb) {
3379096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
3389096f1fbSJohan Hovold if (err) {
3398bb7ec65SJohan Hovold dev_err(&port->dev, "%s: submit int urb failed: %d\n",
3409096f1fbSJohan Hovold __func__, err);
3419096f1fbSJohan Hovold }
3429096f1fbSJohan Hovold }
3439096f1fbSJohan Hovold
3440d456194SMatthew Garrett /* Start reading from the IN endpoint */
3450d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) {
3460d456194SMatthew Garrett urb = portdata->in_urbs[i];
3470d456194SMatthew Garrett if (!urb)
3480d456194SMatthew Garrett continue;
3490d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_KERNEL);
3500d456194SMatthew Garrett if (err) {
3518bb7ec65SJohan Hovold dev_err(&port->dev,
3528bb7ec65SJohan Hovold "%s: submit read urb %d failed: %d\n",
3538bb7ec65SJohan Hovold __func__, i, err);
3540d456194SMatthew Garrett }
3550d456194SMatthew Garrett }
3560d456194SMatthew Garrett
3570d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock);
358c1c01803SJohan Hovold if (++intfdata->open_ports == 1)
359c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 1;
3600d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock);
3619a91aedcSOliver Neukum /* this balances a get in the generic USB serial code */
3620d456194SMatthew Garrett usb_autopm_put_interface(serial->interface);
3630d456194SMatthew Garrett
3640d456194SMatthew Garrett return 0;
3650d456194SMatthew Garrett }
3660d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open);
3670d456194SMatthew Garrett
unbusy_queued_urb(struct urb * urb,struct usb_wwan_port_private * portdata)36879eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb,
36979eed03eSJohan Hovold struct usb_wwan_port_private *portdata)
37079eed03eSJohan Hovold {
37179eed03eSJohan Hovold int i;
37279eed03eSJohan Hovold
37379eed03eSJohan Hovold for (i = 0; i < N_OUT_URB; i++) {
37479eed03eSJohan Hovold if (urb == portdata->out_urbs[i]) {
37579eed03eSJohan Hovold clear_bit(i, &portdata->out_busy);
37679eed03eSJohan Hovold break;
37779eed03eSJohan Hovold }
37879eed03eSJohan Hovold }
37979eed03eSJohan Hovold }
38079eed03eSJohan Hovold
usb_wwan_close(struct usb_serial_port * port)3810d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port)
3820d456194SMatthew Garrett {
3830d456194SMatthew Garrett int i;
3840d456194SMatthew Garrett struct usb_serial *serial = port->serial;
3850d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
38637357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
38779eed03eSJohan Hovold struct urb *urb;
3880d456194SMatthew Garrett
3890d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
3900d456194SMatthew Garrett
391b0a9aa6dSJohan Hovold /*
392b0a9aa6dSJohan Hovold * Need to take susp_lock to make sure port is not already being
393*688ee1d1SJohan Hovold * resumed, but no need to hold it due to the tty-port initialized
394*688ee1d1SJohan Hovold * flag.
395b0a9aa6dSJohan Hovold */
3960d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock);
397c1c01803SJohan Hovold if (--intfdata->open_ports == 0)
398c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 0;
3990d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock);
4000d456194SMatthew Garrett
40179eed03eSJohan Hovold for (;;) {
40279eed03eSJohan Hovold urb = usb_get_from_anchor(&portdata->delayed);
40379eed03eSJohan Hovold if (!urb)
40479eed03eSJohan Hovold break;
40579eed03eSJohan Hovold unbusy_queued_urb(urb, portdata);
40679eed03eSJohan Hovold usb_autopm_put_interface_async(serial->interface);
40779eed03eSJohan Hovold }
40879eed03eSJohan Hovold
4090d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++)
4100d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[i]);
4110d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++)
4120d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[i]);
4139096f1fbSJohan Hovold usb_kill_urb(port->interrupt_in_urb);
414e6d144bcSJohan Hovold
4159a91aedcSOliver Neukum usb_autopm_get_interface_no_resume(serial->interface);
4160d456194SMatthew Garrett }
4170d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close);
4180d456194SMatthew Garrett
usb_wwan_setup_urb(struct usb_serial_port * port,int endpoint,int dir,void * ctx,char * buf,int len,void (* callback)(struct urb *))419b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
420b8f0e820SJohan Hovold int endpoint,
4210d456194SMatthew Garrett int dir, void *ctx, char *buf, int len,
4220d456194SMatthew Garrett void (*callback) (struct urb *))
4230d456194SMatthew Garrett {
424b8f0e820SJohan Hovold struct usb_serial *serial = port->serial;
4252438c3a1SDaniele Palmas struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
4260d456194SMatthew Garrett struct urb *urb;
4270d456194SMatthew Garrett
4280d456194SMatthew Garrett urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
42910c642d0SJohan Hovold if (!urb)
4300d456194SMatthew Garrett return NULL;
4310d456194SMatthew Garrett
4320d456194SMatthew Garrett usb_fill_bulk_urb(urb, serial->dev,
4330d456194SMatthew Garrett usb_sndbulkpipe(serial->dev, endpoint) | dir,
4340d456194SMatthew Garrett buf, len, callback, ctx);
4350d456194SMatthew Garrett
4362438c3a1SDaniele Palmas if (intfdata->use_zlp && dir == USB_DIR_OUT)
4372438c3a1SDaniele Palmas urb->transfer_flags |= URB_ZERO_PACKET;
4382438c3a1SDaniele Palmas
4390d456194SMatthew Garrett return urb;
4400d456194SMatthew Garrett }
4410d456194SMatthew Garrett
usb_wwan_port_probe(struct usb_serial_port * port)442b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port)
4430d456194SMatthew Garrett {
4440d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
445b8f0e820SJohan Hovold struct urb *urb;
4460d456194SMatthew Garrett u8 *buffer;
447b8f0e820SJohan Hovold int i;
4480d456194SMatthew Garrett
449bd73bd88SJohan Hovold if (!port->bulk_in_size || !port->bulk_out_size)
450bd73bd88SJohan Hovold return -ENODEV;
451bd73bd88SJohan Hovold
4520d456194SMatthew Garrett portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
453b8f0e820SJohan Hovold if (!portdata)
454b8f0e820SJohan Hovold return -ENOMEM;
455b8f0e820SJohan Hovold
4560d456194SMatthew Garrett init_usb_anchor(&portdata->delayed);
4570d456194SMatthew Garrett
458b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) {
4590d456194SMatthew Garrett buffer = (u8 *)__get_free_page(GFP_KERNEL);
4600d456194SMatthew Garrett if (!buffer)
4610d456194SMatthew Garrett goto bail_out_error;
462b8f0e820SJohan Hovold portdata->in_buffer[i] = buffer;
463b8f0e820SJohan Hovold
464b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
465b8f0e820SJohan Hovold USB_DIR_IN, port,
466b8f0e820SJohan Hovold buffer, IN_BUFLEN,
467b8f0e820SJohan Hovold usb_wwan_indat_callback);
468b8f0e820SJohan Hovold portdata->in_urbs[i] = urb;
4690d456194SMatthew Garrett }
4700d456194SMatthew Garrett
471b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) {
4720d456194SMatthew Garrett buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
4730d456194SMatthew Garrett if (!buffer)
4740d456194SMatthew Garrett goto bail_out_error2;
475b8f0e820SJohan Hovold portdata->out_buffer[i] = buffer;
476b8f0e820SJohan Hovold
477b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
478b8f0e820SJohan Hovold USB_DIR_OUT, port,
479b8f0e820SJohan Hovold buffer, OUT_BUFLEN,
480b8f0e820SJohan Hovold usb_wwan_outdat_callback);
481b8f0e820SJohan Hovold portdata->out_urbs[i] = urb;
4820d456194SMatthew Garrett }
4830d456194SMatthew Garrett
4840d456194SMatthew Garrett usb_set_serial_port_data(port, portdata);
4850d456194SMatthew Garrett
4860d456194SMatthew Garrett return 0;
4870d456194SMatthew Garrett
4880d456194SMatthew Garrett bail_out_error2:
489b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) {
490b8f0e820SJohan Hovold usb_free_urb(portdata->out_urbs[i]);
491b8f0e820SJohan Hovold kfree(portdata->out_buffer[i]);
4920d456194SMatthew Garrett }
493b8f0e820SJohan Hovold bail_out_error:
494b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) {
495b8f0e820SJohan Hovold usb_free_urb(portdata->in_urbs[i]);
496b8f0e820SJohan Hovold free_page((unsigned long)portdata->in_buffer[i]);
497b8f0e820SJohan Hovold }
498b8f0e820SJohan Hovold kfree(portdata);
499b8f0e820SJohan Hovold
500b8f0e820SJohan Hovold return -ENOMEM;
501b8f0e820SJohan Hovold }
502b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
5030d456194SMatthew Garrett
usb_wwan_port_remove(struct usb_serial_port * port)504c5d1448fSUwe Kleine-König void usb_wwan_port_remove(struct usb_serial_port *port)
505a1028f0aSBjørn Mork {
506a1028f0aSBjørn Mork int i;
507a1028f0aSBjørn Mork struct usb_wwan_port_private *portdata;
508a1028f0aSBjørn Mork
509a1028f0aSBjørn Mork portdata = usb_get_serial_port_data(port);
510a1028f0aSBjørn Mork usb_set_serial_port_data(port, NULL);
511a1028f0aSBjørn Mork
512a1028f0aSBjørn Mork for (i = 0; i < N_IN_URB; i++) {
513a1028f0aSBjørn Mork usb_free_urb(portdata->in_urbs[i]);
514a1028f0aSBjørn Mork free_page((unsigned long)portdata->in_buffer[i]);
515a1028f0aSBjørn Mork }
516a1028f0aSBjørn Mork for (i = 0; i < N_OUT_URB; i++) {
517a1028f0aSBjørn Mork usb_free_urb(portdata->out_urbs[i]);
518a1028f0aSBjørn Mork kfree(portdata->out_buffer[i]);
519a1028f0aSBjørn Mork }
520a1028f0aSBjørn Mork
521a1028f0aSBjørn Mork kfree(portdata);
522a1028f0aSBjørn Mork }
523a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove);
524a1028f0aSBjørn Mork
525a1028f0aSBjørn Mork #ifdef CONFIG_PM
stop_urbs(struct usb_serial * serial)526ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial)
5270d456194SMatthew Garrett {
5280d456194SMatthew Garrett int i, j;
5290d456194SMatthew Garrett struct usb_serial_port *port;
5300d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
5310d456194SMatthew Garrett
5320d456194SMatthew Garrett for (i = 0; i < serial->num_ports; ++i) {
5330d456194SMatthew Garrett port = serial->port[i];
5340d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
535032129cbSBjørn Mork if (!portdata)
536032129cbSBjørn Mork continue;
5370d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++)
5380d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[j]);
5390d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++)
5400d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[j]);
541ae75c940SJohan Hovold usb_kill_urb(port->interrupt_in_urb);
5420d456194SMatthew Garrett }
5430d456194SMatthew Garrett }
5440d456194SMatthew Garrett
usb_wwan_suspend(struct usb_serial * serial,pm_message_t message)5450d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
5460d456194SMatthew Garrett {
54737357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
5480d456194SMatthew Garrett
5490d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock);
550170fad9eSJohan Hovold if (PMSG_IS_AUTO(message)) {
551170fad9eSJohan Hovold if (intfdata->in_flight) {
5520d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock);
5530d456194SMatthew Garrett return -EBUSY;
5540d456194SMatthew Garrett }
555170fad9eSJohan Hovold }
5560d456194SMatthew Garrett intfdata->suspended = 1;
5570d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock);
558170fad9eSJohan Hovold
559ae75c940SJohan Hovold stop_urbs(serial);
5600d456194SMatthew Garrett
5610d456194SMatthew Garrett return 0;
5620d456194SMatthew Garrett }
5630d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend);
5640d456194SMatthew Garrett
5653362c91cSJohan Hovold /* Caller must hold susp_lock. */
usb_wwan_submit_delayed_urbs(struct usb_serial_port * port)5663362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
5670d456194SMatthew Garrett {
5687436f412SJohan Hovold struct usb_serial *serial = port->serial;
56937357ca5SJohan Hovold struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
5700d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
5710d456194SMatthew Garrett struct urb *urb;
5727436f412SJohan Hovold int err_count = 0;
5737436f412SJohan Hovold int err;
5740d456194SMatthew Garrett
5750d456194SMatthew Garrett portdata = usb_get_serial_port_data(port);
57637357ca5SJohan Hovold
5773362c91cSJohan Hovold for (;;) {
5783362c91cSJohan Hovold urb = usb_get_from_anchor(&portdata->delayed);
5793362c91cSJohan Hovold if (!urb)
5803362c91cSJohan Hovold break;
5813362c91cSJohan Hovold
5820d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC);
5837436f412SJohan Hovold if (err) {
5843362c91cSJohan Hovold dev_err(&port->dev, "%s: submit urb failed: %d\n",
5857436f412SJohan Hovold __func__, err);
5867436f412SJohan Hovold err_count++;
58716871dcaSOliver Neukum unbusy_queued_urb(urb, portdata);
5887436f412SJohan Hovold usb_autopm_put_interface_async(serial->interface);
5897436f412SJohan Hovold continue;
59016871dcaSOliver Neukum }
5917436f412SJohan Hovold data->in_flight++;
5920d456194SMatthew Garrett }
593fb7ad4f9SJohan Hovold
5947436f412SJohan Hovold if (err_count)
5957436f412SJohan Hovold return -EIO;
5967436f412SJohan Hovold
5977436f412SJohan Hovold return 0;
5980d456194SMatthew Garrett }
5990d456194SMatthew Garrett
usb_wwan_resume(struct usb_serial * serial)6000d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial)
6010d456194SMatthew Garrett {
6020d456194SMatthew Garrett int i, j;
6030d456194SMatthew Garrett struct usb_serial_port *port;
60437357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
6050d456194SMatthew Garrett struct usb_wwan_port_private *portdata;
6060d456194SMatthew Garrett struct urb *urb;
607fb7ad4f9SJohan Hovold int err;
608fb7ad4f9SJohan Hovold int err_count = 0;
6090d456194SMatthew Garrett
610d9e93c08Sxiao jin spin_lock_irq(&intfdata->susp_lock);
6110d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) {
6120d456194SMatthew Garrett port = serial->port[i];
6130d456194SMatthew Garrett
614d41861caSPeter Hurley if (!tty_port_initialized(&port->port))
6150d456194SMatthew Garrett continue;
6160d456194SMatthew Garrett
617b0a9aa6dSJohan Hovold portdata = usb_get_serial_port_data(port);
618b0a9aa6dSJohan Hovold
6199096f1fbSJohan Hovold if (port->interrupt_in_urb) {
6209096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb,
6219096f1fbSJohan Hovold GFP_ATOMIC);
6229096f1fbSJohan Hovold if (err) {
6239096f1fbSJohan Hovold dev_err(&port->dev,
6249096f1fbSJohan Hovold "%s: submit int urb failed: %d\n",
6259096f1fbSJohan Hovold __func__, err);
626fb7ad4f9SJohan Hovold err_count++;
6279096f1fbSJohan Hovold }
6289096f1fbSJohan Hovold }
6299096f1fbSJohan Hovold
6303362c91cSJohan Hovold err = usb_wwan_submit_delayed_urbs(port);
631fb7ad4f9SJohan Hovold if (err)
632fb7ad4f9SJohan Hovold err_count++;
633fb7ad4f9SJohan Hovold
6340d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) {
6350d456194SMatthew Garrett urb = portdata->in_urbs[j];
6360d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC);
6370d456194SMatthew Garrett if (err < 0) {
638b0f9d003SJohan Hovold dev_err(&port->dev,
639b0f9d003SJohan Hovold "%s: submit read urb %d failed: %d\n",
640b0f9d003SJohan Hovold __func__, i, err);
641fb7ad4f9SJohan Hovold err_count++;
6420d456194SMatthew Garrett }
6430d456194SMatthew Garrett }
6440d456194SMatthew Garrett }
6450d456194SMatthew Garrett intfdata->suspended = 0;
6460d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock);
647fb7ad4f9SJohan Hovold
648fb7ad4f9SJohan Hovold if (err_count)
649fb7ad4f9SJohan Hovold return -EIO;
650fb7ad4f9SJohan Hovold
651fb7ad4f9SJohan Hovold return 0;
6520d456194SMatthew Garrett }
6530d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume);
6540d456194SMatthew Garrett #endif
6550d456194SMatthew Garrett
6560d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR);
6570d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC);
658627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
659