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 440d456194SMatthew Garrett intfdata = port->serial->private; 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 580d456194SMatthew Garrett void usb_wwan_set_termios(struct tty_struct *tty, 590d456194SMatthew Garrett struct usb_serial_port *port, 600d456194SMatthew Garrett struct ktermios *old_termios) 610d456194SMatthew Garrett { 620d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = port->serial->private; 630d456194SMatthew Garrett 640d456194SMatthew Garrett /* Doesn't support option setting */ 65adc8d746SAlan Cox tty_termios_copy_hw(&tty->termios, old_termios); 660d456194SMatthew Garrett 670d456194SMatthew Garrett if (intfdata->send_setup) 680d456194SMatthew Garrett intfdata->send_setup(port); 690d456194SMatthew Garrett } 700d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_set_termios); 710d456194SMatthew Garrett 7260b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty) 730d456194SMatthew Garrett { 740d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 750d456194SMatthew Garrett unsigned int value; 760d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 770d456194SMatthew Garrett 780d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 790d456194SMatthew Garrett 800d456194SMatthew Garrett value = ((portdata->rts_state) ? TIOCM_RTS : 0) | 810d456194SMatthew Garrett ((portdata->dtr_state) ? TIOCM_DTR : 0) | 820d456194SMatthew Garrett ((portdata->cts_state) ? TIOCM_CTS : 0) | 830d456194SMatthew Garrett ((portdata->dsr_state) ? TIOCM_DSR : 0) | 840d456194SMatthew Garrett ((portdata->dcd_state) ? TIOCM_CAR : 0) | 850d456194SMatthew Garrett ((portdata->ri_state) ? TIOCM_RNG : 0); 860d456194SMatthew Garrett 870d456194SMatthew Garrett return value; 880d456194SMatthew Garrett } 890d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget); 900d456194SMatthew Garrett 9120b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty, 920d456194SMatthew Garrett unsigned int set, unsigned int clear) 930d456194SMatthew Garrett { 940d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 950d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 960d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 970d456194SMatthew Garrett 980d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 990d456194SMatthew Garrett intfdata = port->serial->private; 1000d456194SMatthew Garrett 1010d456194SMatthew Garrett if (!intfdata->send_setup) 1020d456194SMatthew Garrett return -EINVAL; 1030d456194SMatthew Garrett 1040d456194SMatthew Garrett /* FIXME: what locks portdata fields ? */ 1050d456194SMatthew Garrett if (set & TIOCM_RTS) 1060d456194SMatthew Garrett portdata->rts_state = 1; 1070d456194SMatthew Garrett if (set & TIOCM_DTR) 1080d456194SMatthew Garrett portdata->dtr_state = 1; 1090d456194SMatthew Garrett 1100d456194SMatthew Garrett if (clear & TIOCM_RTS) 1110d456194SMatthew Garrett portdata->rts_state = 0; 1120d456194SMatthew Garrett if (clear & TIOCM_DTR) 1130d456194SMatthew Garrett portdata->dtr_state = 0; 1140d456194SMatthew Garrett return intfdata->send_setup(port); 1150d456194SMatthew Garrett } 1160d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset); 1170d456194SMatthew Garrett 11802303f73SDan Williams static int get_serial_info(struct usb_serial_port *port, 11902303f73SDan Williams struct serial_struct __user *retinfo) 12002303f73SDan Williams { 12102303f73SDan Williams struct serial_struct tmp; 12202303f73SDan Williams 12302303f73SDan Williams if (!retinfo) 12402303f73SDan Williams return -EFAULT; 12502303f73SDan Williams 12602303f73SDan Williams memset(&tmp, 0, sizeof(tmp)); 127e5b1e206SGreg Kroah-Hartman tmp.line = port->minor; 1281143832eSGreg Kroah-Hartman tmp.port = port->port_number; 12902303f73SDan Williams tmp.baud_base = tty_get_baud_rate(port->port.tty); 13002303f73SDan Williams tmp.close_delay = port->port.close_delay / 10; 13102303f73SDan Williams tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 13202303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : 13302303f73SDan Williams port->port.closing_wait / 10; 13402303f73SDan Williams 13502303f73SDan Williams if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 13602303f73SDan Williams return -EFAULT; 13702303f73SDan Williams return 0; 13802303f73SDan Williams } 13902303f73SDan Williams 14002303f73SDan Williams static int set_serial_info(struct usb_serial_port *port, 14102303f73SDan Williams struct serial_struct __user *newinfo) 14202303f73SDan Williams { 14302303f73SDan Williams struct serial_struct new_serial; 14402303f73SDan Williams unsigned int closing_wait, close_delay; 14502303f73SDan Williams int retval = 0; 14602303f73SDan Williams 14702303f73SDan Williams if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 14802303f73SDan Williams return -EFAULT; 14902303f73SDan Williams 15002303f73SDan Williams close_delay = new_serial.close_delay * 10; 15102303f73SDan Williams closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 15202303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; 15302303f73SDan Williams 15402303f73SDan Williams mutex_lock(&port->port.mutex); 15502303f73SDan Williams 15602303f73SDan Williams if (!capable(CAP_SYS_ADMIN)) { 15702303f73SDan Williams if ((close_delay != port->port.close_delay) || 15802303f73SDan Williams (closing_wait != port->port.closing_wait)) 15902303f73SDan Williams retval = -EPERM; 16002303f73SDan Williams else 16102303f73SDan Williams retval = -EOPNOTSUPP; 16202303f73SDan Williams } else { 16302303f73SDan Williams port->port.close_delay = close_delay; 16402303f73SDan Williams port->port.closing_wait = closing_wait; 16502303f73SDan Williams } 16602303f73SDan Williams 16702303f73SDan Williams mutex_unlock(&port->port.mutex); 16802303f73SDan Williams return retval; 16902303f73SDan Williams } 17002303f73SDan Williams 17100a0d0d6SAlan Cox int usb_wwan_ioctl(struct tty_struct *tty, 17202303f73SDan Williams unsigned int cmd, unsigned long arg) 17302303f73SDan Williams { 17402303f73SDan Williams struct usb_serial_port *port = tty->driver_data; 17502303f73SDan Williams 176a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); 17702303f73SDan Williams 17802303f73SDan Williams switch (cmd) { 17902303f73SDan Williams case TIOCGSERIAL: 18002303f73SDan Williams return get_serial_info(port, 18102303f73SDan Williams (struct serial_struct __user *) arg); 18202303f73SDan Williams case TIOCSSERIAL: 18302303f73SDan Williams return set_serial_info(port, 18402303f73SDan Williams (struct serial_struct __user *) arg); 18502303f73SDan Williams default: 18602303f73SDan Williams break; 18702303f73SDan Williams } 18802303f73SDan Williams 189a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s arg not supported\n", __func__); 19002303f73SDan Williams 19102303f73SDan Williams return -ENOIOCTLCMD; 19202303f73SDan Williams } 19302303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl); 19402303f73SDan Williams 1950d456194SMatthew Garrett /* Write */ 1960d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port, 1970d456194SMatthew Garrett const unsigned char *buf, int count) 1980d456194SMatthew Garrett { 1990d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 2000d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 2010d456194SMatthew Garrett int i; 2020d456194SMatthew Garrett int left, todo; 2030d456194SMatthew Garrett struct urb *this_urb = NULL; /* spurious */ 2040d456194SMatthew Garrett int err; 2050d456194SMatthew Garrett unsigned long flags; 2060d456194SMatthew Garrett 2070d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 2080d456194SMatthew Garrett intfdata = port->serial->private; 2090d456194SMatthew Garrett 210a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count); 2110d456194SMatthew Garrett 2120d456194SMatthew Garrett i = 0; 2130d456194SMatthew Garrett left = count; 2140d456194SMatthew Garrett for (i = 0; left > 0 && i < N_OUT_URB; i++) { 2150d456194SMatthew Garrett todo = left; 2160d456194SMatthew Garrett if (todo > OUT_BUFLEN) 2170d456194SMatthew Garrett todo = OUT_BUFLEN; 2180d456194SMatthew Garrett 2190d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 2200d456194SMatthew Garrett if (test_and_set_bit(i, &portdata->out_busy)) { 2210d456194SMatthew Garrett if (time_before(jiffies, 2220d456194SMatthew Garrett portdata->tx_start_time[i] + 10 * HZ)) 2230d456194SMatthew Garrett continue; 2240d456194SMatthew Garrett usb_unlink_urb(this_urb); 2250d456194SMatthew Garrett continue; 2260d456194SMatthew Garrett } 227a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__, 2280d456194SMatthew Garrett usb_pipeendpoint(this_urb->pipe), i); 2290d456194SMatthew Garrett 2300d456194SMatthew Garrett err = usb_autopm_get_interface_async(port->serial->interface); 231db090473Sxiao jin if (err < 0) { 232db090473Sxiao jin clear_bit(i, &portdata->out_busy); 2330d456194SMatthew Garrett break; 234db090473Sxiao jin } 2350d456194SMatthew Garrett 2360d456194SMatthew Garrett /* send the data */ 2370d456194SMatthew Garrett memcpy(this_urb->transfer_buffer, buf, todo); 2380d456194SMatthew Garrett this_urb->transfer_buffer_length = todo; 2390d456194SMatthew Garrett 2400d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2410d456194SMatthew Garrett if (intfdata->suspended) { 2420d456194SMatthew Garrett usb_anchor_urb(this_urb, &portdata->delayed); 2430d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2440d456194SMatthew Garrett } else { 2450d456194SMatthew Garrett intfdata->in_flight++; 2460d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2470d456194SMatthew Garrett err = usb_submit_urb(this_urb, GFP_ATOMIC); 2480d456194SMatthew Garrett if (err) { 249a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, 250a80be97dSGreg Kroah-Hartman "usb_submit_urb %p (write bulk) failed (%d)\n", 251a80be97dSGreg Kroah-Hartman this_urb, err); 2520d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 2530d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2540d456194SMatthew Garrett intfdata->in_flight--; 2550d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, 2560d456194SMatthew Garrett flags); 2573d06bf15SOliver Neukum usb_autopm_put_interface_async(port->serial->interface); 258433508aeSOliver Neukum break; 2590d456194SMatthew Garrett } 2600d456194SMatthew Garrett } 2610d456194SMatthew Garrett 2620d456194SMatthew Garrett portdata->tx_start_time[i] = jiffies; 2630d456194SMatthew Garrett buf += todo; 2640d456194SMatthew Garrett left -= todo; 2650d456194SMatthew Garrett } 2660d456194SMatthew Garrett 2670d456194SMatthew Garrett count -= left; 268a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count); 2690d456194SMatthew Garrett return count; 2700d456194SMatthew Garrett } 2710d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write); 2720d456194SMatthew Garrett 2730d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb) 2740d456194SMatthew Garrett { 2750d456194SMatthew Garrett int err; 2760d456194SMatthew Garrett int endpoint; 2770d456194SMatthew Garrett struct usb_serial_port *port; 278a80be97dSGreg Kroah-Hartman struct device *dev; 2790d456194SMatthew Garrett unsigned char *data = urb->transfer_buffer; 2800d456194SMatthew Garrett int status = urb->status; 2810d456194SMatthew Garrett 2820d456194SMatthew Garrett endpoint = usb_pipeendpoint(urb->pipe); 2830d456194SMatthew Garrett port = urb->context; 284a80be97dSGreg Kroah-Hartman dev = &port->dev; 2850d456194SMatthew Garrett 2860d456194SMatthew Garrett if (status) { 287a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n", 2880d456194SMatthew Garrett __func__, status, endpoint); 2890d456194SMatthew Garrett } else { 2900d456194SMatthew Garrett if (urb->actual_length) { 29105c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data, 29238237fd2SJiri Slaby urb->actual_length); 2932e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 2940d456194SMatthew Garrett } else 295a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: empty read urb received\n", __func__); 2966c1ee66aSMatt Burtch } 2970d456194SMatthew Garrett /* Resubmit urb so we continue receiving */ 2980d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 299c9c4558fSOliver Neukum if (err) { 300c9c4558fSOliver Neukum if (err != -EPERM) { 3016c1ee66aSMatt Burtch dev_err(dev, "%s: resubmit read urb failed. (%d)\n", 3026c1ee66aSMatt Burtch __func__, err); 303c9c4558fSOliver Neukum /* busy also in error unless we are killed */ 3040d456194SMatthew Garrett usb_mark_last_busy(port->serial->dev); 3050d456194SMatthew Garrett } 306c9c4558fSOliver Neukum } else { 307c9c4558fSOliver Neukum usb_mark_last_busy(port->serial->dev); 308c9c4558fSOliver Neukum } 309c9c4558fSOliver Neukum } 3100d456194SMatthew Garrett 3110d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb) 3120d456194SMatthew Garrett { 3130d456194SMatthew Garrett struct usb_serial_port *port; 3140d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3150d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 3160d456194SMatthew Garrett int i; 3170d456194SMatthew Garrett 3180d456194SMatthew Garrett port = urb->context; 3190d456194SMatthew Garrett intfdata = port->serial->private; 3200d456194SMatthew Garrett 3210d456194SMatthew Garrett usb_serial_port_softint(port); 3220d456194SMatthew Garrett usb_autopm_put_interface_async(port->serial->interface); 3230d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3240d456194SMatthew Garrett spin_lock(&intfdata->susp_lock); 3250d456194SMatthew Garrett intfdata->in_flight--; 3260d456194SMatthew Garrett spin_unlock(&intfdata->susp_lock); 3270d456194SMatthew Garrett 3280d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; ++i) { 3290d456194SMatthew Garrett if (portdata->out_urbs[i] == urb) { 3300d456194SMatthew Garrett smp_mb__before_clear_bit(); 3310d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 3320d456194SMatthew Garrett break; 3330d456194SMatthew Garrett } 3340d456194SMatthew Garrett } 3350d456194SMatthew Garrett } 3360d456194SMatthew Garrett 3370d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty) 3380d456194SMatthew Garrett { 3390d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3400d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3410d456194SMatthew Garrett int i; 3420d456194SMatthew Garrett int data_len = 0; 3430d456194SMatthew Garrett struct urb *this_urb; 3440d456194SMatthew Garrett 3450d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3460d456194SMatthew Garrett 3470d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3480d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3490d456194SMatthew Garrett if (this_urb && !test_bit(i, &portdata->out_busy)) 3500d456194SMatthew Garrett data_len += OUT_BUFLEN; 3510d456194SMatthew Garrett } 3520d456194SMatthew Garrett 353a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); 3540d456194SMatthew Garrett return data_len; 3550d456194SMatthew Garrett } 3560d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room); 3570d456194SMatthew Garrett 3580d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty) 3590d456194SMatthew Garrett { 3600d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3610d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3620d456194SMatthew Garrett int i; 3630d456194SMatthew Garrett int data_len = 0; 3640d456194SMatthew Garrett struct urb *this_urb; 3650d456194SMatthew Garrett 3660d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3670d456194SMatthew Garrett 3680d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3690d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3700d456194SMatthew Garrett /* FIXME: This locking is insufficient as this_urb may 3710d456194SMatthew Garrett go unused during the test */ 3720d456194SMatthew Garrett if (this_urb && test_bit(i, &portdata->out_busy)) 3730d456194SMatthew Garrett data_len += this_urb->transfer_buffer_length; 3740d456194SMatthew Garrett } 375a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); 3760d456194SMatthew Garrett return data_len; 3770d456194SMatthew Garrett } 3780d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer); 3790d456194SMatthew Garrett 3800d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port) 3810d456194SMatthew Garrett { 3820d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3830d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 3840d456194SMatthew Garrett struct usb_serial *serial = port->serial; 3850d456194SMatthew Garrett int i, err; 3860d456194SMatthew Garrett struct urb *urb; 3870d456194SMatthew Garrett 3880d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3890d456194SMatthew Garrett intfdata = serial->private; 3900d456194SMatthew Garrett 3919096f1fbSJohan Hovold if (port->interrupt_in_urb) { 3929096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 3939096f1fbSJohan Hovold if (err) { 3949096f1fbSJohan Hovold dev_dbg(&port->dev, "%s: submit int urb failed: %d\n", 3959096f1fbSJohan Hovold __func__, err); 3969096f1fbSJohan Hovold } 3979096f1fbSJohan Hovold } 3989096f1fbSJohan Hovold 3990d456194SMatthew Garrett /* Start reading from the IN endpoint */ 4000d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) { 4010d456194SMatthew Garrett urb = portdata->in_urbs[i]; 4020d456194SMatthew Garrett if (!urb) 4030d456194SMatthew Garrett continue; 4040d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_KERNEL); 4050d456194SMatthew Garrett if (err) { 406a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n", 4070d456194SMatthew Garrett __func__, i, err, urb->transfer_buffer_length); 4080d456194SMatthew Garrett } 4090d456194SMatthew Garrett } 4100d456194SMatthew Garrett 4110d456194SMatthew Garrett if (intfdata->send_setup) 4120d456194SMatthew Garrett intfdata->send_setup(port); 4130d456194SMatthew Garrett 4140d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 4150d456194SMatthew Garrett portdata->opened = 1; 416*c1c01803SJohan Hovold if (++intfdata->open_ports == 1) 417*c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 1; 4180d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 4199a91aedcSOliver Neukum /* this balances a get in the generic USB serial code */ 4200d456194SMatthew Garrett usb_autopm_put_interface(serial->interface); 4210d456194SMatthew Garrett 4220d456194SMatthew Garrett return 0; 4230d456194SMatthew Garrett } 4240d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open); 4250d456194SMatthew Garrett 42679eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb, 42779eed03eSJohan Hovold struct usb_wwan_port_private *portdata) 42879eed03eSJohan Hovold { 42979eed03eSJohan Hovold int i; 43079eed03eSJohan Hovold 43179eed03eSJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 43279eed03eSJohan Hovold if (urb == portdata->out_urbs[i]) { 43379eed03eSJohan Hovold clear_bit(i, &portdata->out_busy); 43479eed03eSJohan Hovold break; 43579eed03eSJohan Hovold } 43679eed03eSJohan Hovold } 43779eed03eSJohan Hovold } 43879eed03eSJohan Hovold 4390d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port) 4400d456194SMatthew Garrett { 4410d456194SMatthew Garrett int i; 4420d456194SMatthew Garrett struct usb_serial *serial = port->serial; 4430d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 4440d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = port->serial->private; 44579eed03eSJohan Hovold struct urb *urb; 4460d456194SMatthew Garrett 4470d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 4480d456194SMatthew Garrett 4490d456194SMatthew Garrett /* Stop reading/writing urbs */ 4500d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 4510d456194SMatthew Garrett portdata->opened = 0; 452*c1c01803SJohan Hovold if (--intfdata->open_ports == 0) 453*c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 0; 4540d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 4550d456194SMatthew Garrett 45679eed03eSJohan Hovold for (;;) { 45779eed03eSJohan Hovold urb = usb_get_from_anchor(&portdata->delayed); 45879eed03eSJohan Hovold if (!urb) 45979eed03eSJohan Hovold break; 46079eed03eSJohan Hovold unbusy_queued_urb(urb, portdata); 46179eed03eSJohan Hovold usb_autopm_put_interface_async(serial->interface); 46279eed03eSJohan Hovold } 46379eed03eSJohan Hovold 4640d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) 4650d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[i]); 4660d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) 4670d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[i]); 4689096f1fbSJohan Hovold usb_kill_urb(port->interrupt_in_urb); 469e6d144bcSJohan Hovold 4709a91aedcSOliver Neukum /* balancing - important as an error cannot be handled*/ 4719a91aedcSOliver Neukum usb_autopm_get_interface_no_resume(serial->interface); 4720d456194SMatthew Garrett } 4730d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close); 4740d456194SMatthew Garrett 4750d456194SMatthew Garrett /* Helper functions used by usb_wwan_setup_urbs */ 476b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port, 477b8f0e820SJohan Hovold int endpoint, 4780d456194SMatthew Garrett int dir, void *ctx, char *buf, int len, 4790d456194SMatthew Garrett void (*callback) (struct urb *)) 4800d456194SMatthew Garrett { 481b8f0e820SJohan Hovold struct usb_serial *serial = port->serial; 4820d456194SMatthew Garrett struct urb *urb; 4830d456194SMatthew Garrett 4840d456194SMatthew Garrett urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ 48510c642d0SJohan Hovold if (!urb) 4860d456194SMatthew Garrett return NULL; 4870d456194SMatthew Garrett 4880d456194SMatthew Garrett /* Fill URB using supplied data. */ 4890d456194SMatthew Garrett usb_fill_bulk_urb(urb, serial->dev, 4900d456194SMatthew Garrett usb_sndbulkpipe(serial->dev, endpoint) | dir, 4910d456194SMatthew Garrett buf, len, callback, ctx); 4920d456194SMatthew Garrett 4930d456194SMatthew Garrett return urb; 4940d456194SMatthew Garrett } 4950d456194SMatthew Garrett 496b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port) 4970d456194SMatthew Garrett { 4980d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 499b8f0e820SJohan Hovold struct urb *urb; 5000d456194SMatthew Garrett u8 *buffer; 501b8f0e820SJohan Hovold int i; 5020d456194SMatthew Garrett 503bd73bd88SJohan Hovold if (!port->bulk_in_size || !port->bulk_out_size) 504bd73bd88SJohan Hovold return -ENODEV; 505bd73bd88SJohan Hovold 5060d456194SMatthew Garrett portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 507b8f0e820SJohan Hovold if (!portdata) 508b8f0e820SJohan Hovold return -ENOMEM; 509b8f0e820SJohan Hovold 5100d456194SMatthew Garrett init_usb_anchor(&portdata->delayed); 5110d456194SMatthew Garrett 512b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) { 5130d456194SMatthew Garrett buffer = (u8 *)__get_free_page(GFP_KERNEL); 5140d456194SMatthew Garrett if (!buffer) 5150d456194SMatthew Garrett goto bail_out_error; 516b8f0e820SJohan Hovold portdata->in_buffer[i] = buffer; 517b8f0e820SJohan Hovold 518b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress, 519b8f0e820SJohan Hovold USB_DIR_IN, port, 520b8f0e820SJohan Hovold buffer, IN_BUFLEN, 521b8f0e820SJohan Hovold usb_wwan_indat_callback); 522b8f0e820SJohan Hovold portdata->in_urbs[i] = urb; 5230d456194SMatthew Garrett } 5240d456194SMatthew Garrett 525b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 5260d456194SMatthew Garrett buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); 5270d456194SMatthew Garrett if (!buffer) 5280d456194SMatthew Garrett goto bail_out_error2; 529b8f0e820SJohan Hovold portdata->out_buffer[i] = buffer; 530b8f0e820SJohan Hovold 531b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress, 532b8f0e820SJohan Hovold USB_DIR_OUT, port, 533b8f0e820SJohan Hovold buffer, OUT_BUFLEN, 534b8f0e820SJohan Hovold usb_wwan_outdat_callback); 535b8f0e820SJohan Hovold portdata->out_urbs[i] = urb; 5360d456194SMatthew Garrett } 5370d456194SMatthew Garrett 5380d456194SMatthew Garrett usb_set_serial_port_data(port, portdata); 5390d456194SMatthew Garrett 5400d456194SMatthew Garrett return 0; 5410d456194SMatthew Garrett 5420d456194SMatthew Garrett bail_out_error2: 543b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 544b8f0e820SJohan Hovold usb_free_urb(portdata->out_urbs[i]); 545b8f0e820SJohan Hovold kfree(portdata->out_buffer[i]); 5460d456194SMatthew Garrett } 547b8f0e820SJohan Hovold bail_out_error: 548b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) { 549b8f0e820SJohan Hovold usb_free_urb(portdata->in_urbs[i]); 550b8f0e820SJohan Hovold free_page((unsigned long)portdata->in_buffer[i]); 551b8f0e820SJohan Hovold } 552b8f0e820SJohan Hovold kfree(portdata); 553b8f0e820SJohan Hovold 554b8f0e820SJohan Hovold return -ENOMEM; 555b8f0e820SJohan Hovold } 556b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe); 5570d456194SMatthew Garrett 558a1028f0aSBjørn Mork int usb_wwan_port_remove(struct usb_serial_port *port) 559a1028f0aSBjørn Mork { 560a1028f0aSBjørn Mork int i; 561a1028f0aSBjørn Mork struct usb_wwan_port_private *portdata; 562a1028f0aSBjørn Mork 563a1028f0aSBjørn Mork portdata = usb_get_serial_port_data(port); 564a1028f0aSBjørn Mork usb_set_serial_port_data(port, NULL); 565a1028f0aSBjørn Mork 566a1028f0aSBjørn Mork /* Stop reading/writing urbs and free them */ 567a1028f0aSBjørn Mork for (i = 0; i < N_IN_URB; i++) { 568a1028f0aSBjørn Mork usb_kill_urb(portdata->in_urbs[i]); 569a1028f0aSBjørn Mork usb_free_urb(portdata->in_urbs[i]); 570a1028f0aSBjørn Mork free_page((unsigned long)portdata->in_buffer[i]); 571a1028f0aSBjørn Mork } 572a1028f0aSBjørn Mork for (i = 0; i < N_OUT_URB; i++) { 573a1028f0aSBjørn Mork usb_kill_urb(portdata->out_urbs[i]); 574a1028f0aSBjørn Mork usb_free_urb(portdata->out_urbs[i]); 575a1028f0aSBjørn Mork kfree(portdata->out_buffer[i]); 576a1028f0aSBjørn Mork } 577a1028f0aSBjørn Mork 578a1028f0aSBjørn Mork /* Now free port private data */ 579a1028f0aSBjørn Mork kfree(portdata); 580a1028f0aSBjørn Mork return 0; 581a1028f0aSBjørn Mork } 582a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove); 583a1028f0aSBjørn Mork 584a1028f0aSBjørn Mork #ifdef CONFIG_PM 5850d456194SMatthew Garrett static void stop_read_write_urbs(struct usb_serial *serial) 5860d456194SMatthew Garrett { 5870d456194SMatthew Garrett int i, j; 5880d456194SMatthew Garrett struct usb_serial_port *port; 5890d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 5900d456194SMatthew Garrett 5910d456194SMatthew Garrett /* Stop reading/writing urbs */ 5920d456194SMatthew Garrett for (i = 0; i < serial->num_ports; ++i) { 5930d456194SMatthew Garrett port = serial->port[i]; 5940d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 595032129cbSBjørn Mork if (!portdata) 596032129cbSBjørn Mork continue; 5970d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) 5980d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[j]); 5990d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) 6000d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[j]); 6010d456194SMatthew Garrett } 6020d456194SMatthew Garrett } 6030d456194SMatthew Garrett 6040d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message) 6050d456194SMatthew Garrett { 6060d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = serial->private; 6070d456194SMatthew Garrett 6080d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 609170fad9eSJohan Hovold if (PMSG_IS_AUTO(message)) { 610170fad9eSJohan Hovold if (intfdata->in_flight) { 6110d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 6120d456194SMatthew Garrett return -EBUSY; 6130d456194SMatthew Garrett } 614170fad9eSJohan Hovold } 6150d456194SMatthew Garrett intfdata->suspended = 1; 6160d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 617170fad9eSJohan Hovold 6180d456194SMatthew Garrett stop_read_write_urbs(serial); 6190d456194SMatthew Garrett 6200d456194SMatthew Garrett return 0; 6210d456194SMatthew Garrett } 6220d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend); 6230d456194SMatthew Garrett 624fb7ad4f9SJohan Hovold static int play_delayed(struct usb_serial_port *port) 6250d456194SMatthew Garrett { 6267436f412SJohan Hovold struct usb_serial *serial = port->serial; 6270d456194SMatthew Garrett struct usb_wwan_intf_private *data; 6280d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6290d456194SMatthew Garrett struct urb *urb; 6307436f412SJohan Hovold int err_count = 0; 6317436f412SJohan Hovold int err; 6320d456194SMatthew Garrett 6330d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 6340d456194SMatthew Garrett data = port->serial->private; 6350d456194SMatthew Garrett while ((urb = usb_get_from_anchor(&portdata->delayed))) { 6360d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 6377436f412SJohan Hovold if (err) { 6387436f412SJohan Hovold dev_err(&port->dev, 6397436f412SJohan Hovold "%s: submit write urb failed: %d\n", 6407436f412SJohan Hovold __func__, err); 6417436f412SJohan Hovold err_count++; 64216871dcaSOliver Neukum unbusy_queued_urb(urb, portdata); 6437436f412SJohan Hovold usb_autopm_put_interface_async(serial->interface); 6447436f412SJohan Hovold continue; 64516871dcaSOliver Neukum } 6467436f412SJohan Hovold data->in_flight++; 6470d456194SMatthew Garrett } 648fb7ad4f9SJohan Hovold 6497436f412SJohan Hovold if (err_count) 6507436f412SJohan Hovold return -EIO; 6517436f412SJohan Hovold 6527436f412SJohan Hovold return 0; 6530d456194SMatthew Garrett } 6540d456194SMatthew Garrett 6550d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial) 6560d456194SMatthew Garrett { 6570d456194SMatthew Garrett int i, j; 6580d456194SMatthew Garrett struct usb_serial_port *port; 6590d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = serial->private; 6600d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6610d456194SMatthew Garrett struct urb *urb; 662fb7ad4f9SJohan Hovold int err; 663fb7ad4f9SJohan Hovold int err_count = 0; 6640d456194SMatthew Garrett 665d9e93c08Sxiao jin spin_lock_irq(&intfdata->susp_lock); 6660d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 6670d456194SMatthew Garrett /* walk all ports */ 6680d456194SMatthew Garrett port = serial->port[i]; 6690d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 6700d456194SMatthew Garrett 6710d456194SMatthew Garrett /* skip closed ports */ 672d9e93c08Sxiao jin if (!portdata || !portdata->opened) 6730d456194SMatthew Garrett continue; 6740d456194SMatthew Garrett 6759096f1fbSJohan Hovold if (port->interrupt_in_urb) { 6769096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb, 6779096f1fbSJohan Hovold GFP_ATOMIC); 6789096f1fbSJohan Hovold if (err) { 6799096f1fbSJohan Hovold dev_err(&port->dev, 6809096f1fbSJohan Hovold "%s: submit int urb failed: %d\n", 6819096f1fbSJohan Hovold __func__, err); 682fb7ad4f9SJohan Hovold err_count++; 6839096f1fbSJohan Hovold } 6849096f1fbSJohan Hovold } 6859096f1fbSJohan Hovold 686fb7ad4f9SJohan Hovold err = play_delayed(port); 687fb7ad4f9SJohan Hovold if (err) 688fb7ad4f9SJohan Hovold err_count++; 689fb7ad4f9SJohan Hovold 6900d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) { 6910d456194SMatthew Garrett urb = portdata->in_urbs[j]; 6920d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 6930d456194SMatthew Garrett if (err < 0) { 6943a1c2a82SGreg Kroah-Hartman dev_err(&port->dev, "%s: Error %d for bulk URB %d\n", 6950d456194SMatthew Garrett __func__, err, i); 696fb7ad4f9SJohan Hovold err_count++; 6970d456194SMatthew Garrett } 6980d456194SMatthew Garrett } 6990d456194SMatthew Garrett } 7000d456194SMatthew Garrett intfdata->suspended = 0; 7010d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 702fb7ad4f9SJohan Hovold 703fb7ad4f9SJohan Hovold if (err_count) 704fb7ad4f9SJohan Hovold return -EIO; 705fb7ad4f9SJohan Hovold 706fb7ad4f9SJohan Hovold return 0; 7070d456194SMatthew Garrett } 7080d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume); 7090d456194SMatthew Garrett #endif 7100d456194SMatthew Garrett 7110d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR); 7120d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC); 7130d456194SMatthew Garrett MODULE_LICENSE("GPL"); 714