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 39*669e729fSDavid Ward /* 40*669e729fSDavid Ward * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request 41*669e729fSDavid Ward * in CDC ACM. 42*669e729fSDavid Ward */ 43*669e729fSDavid Ward static int usb_wwan_send_setup(struct usb_serial_port *port) 44*669e729fSDavid Ward { 45*669e729fSDavid Ward struct usb_serial *serial = port->serial; 46*669e729fSDavid Ward struct usb_wwan_port_private *portdata; 47*669e729fSDavid Ward int val = 0; 48*669e729fSDavid Ward int ifnum; 49*669e729fSDavid Ward int res; 50*669e729fSDavid Ward 51*669e729fSDavid Ward portdata = usb_get_serial_port_data(port); 52*669e729fSDavid Ward 53*669e729fSDavid Ward if (portdata->dtr_state) 54*669e729fSDavid Ward val |= 0x01; 55*669e729fSDavid Ward if (portdata->rts_state) 56*669e729fSDavid Ward val |= 0x02; 57*669e729fSDavid Ward 58*669e729fSDavid Ward ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber; 59*669e729fSDavid Ward 60*669e729fSDavid Ward res = usb_autopm_get_interface(serial->interface); 61*669e729fSDavid Ward if (res) 62*669e729fSDavid Ward return res; 63*669e729fSDavid Ward 64*669e729fSDavid Ward res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 65*669e729fSDavid Ward 0x22, 0x21, val, ifnum, NULL, 0, 66*669e729fSDavid Ward USB_CTRL_SET_TIMEOUT); 67*669e729fSDavid Ward 68*669e729fSDavid Ward usb_autopm_put_interface(port->serial->interface); 69*669e729fSDavid Ward 70*669e729fSDavid Ward return res; 71*669e729fSDavid Ward } 72*669e729fSDavid Ward 730d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on) 740d456194SMatthew Garrett { 750d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 760d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 770d456194SMatthew Garrett 7837357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial); 790d456194SMatthew Garrett 80*669e729fSDavid Ward if (!intfdata->use_send_setup) 810d456194SMatthew Garrett return; 820d456194SMatthew Garrett 830d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 84b2ca6990SJohan Hovold /* FIXME: locking */ 850d456194SMatthew Garrett portdata->rts_state = on; 860d456194SMatthew Garrett portdata->dtr_state = on; 87b2ca6990SJohan Hovold 88*669e729fSDavid Ward usb_wwan_send_setup(port); 890d456194SMatthew Garrett } 900d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts); 910d456194SMatthew Garrett 9260b33c13SAlan Cox int usb_wwan_tiocmget(struct tty_struct *tty) 930d456194SMatthew Garrett { 940d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 950d456194SMatthew Garrett unsigned int value; 960d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 970d456194SMatthew Garrett 980d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 990d456194SMatthew Garrett 1000d456194SMatthew Garrett value = ((portdata->rts_state) ? TIOCM_RTS : 0) | 1010d456194SMatthew Garrett ((portdata->dtr_state) ? TIOCM_DTR : 0) | 1020d456194SMatthew Garrett ((portdata->cts_state) ? TIOCM_CTS : 0) | 1030d456194SMatthew Garrett ((portdata->dsr_state) ? TIOCM_DSR : 0) | 1040d456194SMatthew Garrett ((portdata->dcd_state) ? TIOCM_CAR : 0) | 1050d456194SMatthew Garrett ((portdata->ri_state) ? TIOCM_RNG : 0); 1060d456194SMatthew Garrett 1070d456194SMatthew Garrett return value; 1080d456194SMatthew Garrett } 1090d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget); 1100d456194SMatthew Garrett 11120b9d177SAlan Cox int usb_wwan_tiocmset(struct tty_struct *tty, 1120d456194SMatthew Garrett unsigned int set, unsigned int clear) 1130d456194SMatthew Garrett { 1140d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 1150d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 1160d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 1170d456194SMatthew Garrett 1180d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 11937357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial); 1200d456194SMatthew Garrett 121*669e729fSDavid Ward if (!intfdata->use_send_setup) 1220d456194SMatthew Garrett return -EINVAL; 1230d456194SMatthew Garrett 1240d456194SMatthew Garrett /* FIXME: what locks portdata fields ? */ 1250d456194SMatthew Garrett if (set & TIOCM_RTS) 1260d456194SMatthew Garrett portdata->rts_state = 1; 1270d456194SMatthew Garrett if (set & TIOCM_DTR) 1280d456194SMatthew Garrett portdata->dtr_state = 1; 1290d456194SMatthew Garrett 1300d456194SMatthew Garrett if (clear & TIOCM_RTS) 1310d456194SMatthew Garrett portdata->rts_state = 0; 1320d456194SMatthew Garrett if (clear & TIOCM_DTR) 1330d456194SMatthew Garrett portdata->dtr_state = 0; 134*669e729fSDavid Ward return usb_wwan_send_setup(port); 1350d456194SMatthew Garrett } 1360d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset); 1370d456194SMatthew Garrett 13802303f73SDan Williams static int get_serial_info(struct usb_serial_port *port, 13902303f73SDan Williams struct serial_struct __user *retinfo) 14002303f73SDan Williams { 14102303f73SDan Williams struct serial_struct tmp; 14202303f73SDan Williams 14302303f73SDan Williams if (!retinfo) 14402303f73SDan Williams return -EFAULT; 14502303f73SDan Williams 14602303f73SDan Williams memset(&tmp, 0, sizeof(tmp)); 147e5b1e206SGreg Kroah-Hartman tmp.line = port->minor; 1481143832eSGreg Kroah-Hartman tmp.port = port->port_number; 14902303f73SDan Williams tmp.baud_base = tty_get_baud_rate(port->port.tty); 15002303f73SDan Williams tmp.close_delay = port->port.close_delay / 10; 15102303f73SDan Williams tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 15202303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : 15302303f73SDan Williams port->port.closing_wait / 10; 15402303f73SDan Williams 15502303f73SDan Williams if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 15602303f73SDan Williams return -EFAULT; 15702303f73SDan Williams return 0; 15802303f73SDan Williams } 15902303f73SDan Williams 16002303f73SDan Williams static int set_serial_info(struct usb_serial_port *port, 16102303f73SDan Williams struct serial_struct __user *newinfo) 16202303f73SDan Williams { 16302303f73SDan Williams struct serial_struct new_serial; 16402303f73SDan Williams unsigned int closing_wait, close_delay; 16502303f73SDan Williams int retval = 0; 16602303f73SDan Williams 16702303f73SDan Williams if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 16802303f73SDan Williams return -EFAULT; 16902303f73SDan Williams 17002303f73SDan Williams close_delay = new_serial.close_delay * 10; 17102303f73SDan Williams closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 17202303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; 17302303f73SDan Williams 17402303f73SDan Williams mutex_lock(&port->port.mutex); 17502303f73SDan Williams 17602303f73SDan Williams if (!capable(CAP_SYS_ADMIN)) { 17702303f73SDan Williams if ((close_delay != port->port.close_delay) || 17802303f73SDan Williams (closing_wait != port->port.closing_wait)) 17902303f73SDan Williams retval = -EPERM; 18002303f73SDan Williams else 18102303f73SDan Williams retval = -EOPNOTSUPP; 18202303f73SDan Williams } else { 18302303f73SDan Williams port->port.close_delay = close_delay; 18402303f73SDan Williams port->port.closing_wait = closing_wait; 18502303f73SDan Williams } 18602303f73SDan Williams 18702303f73SDan Williams mutex_unlock(&port->port.mutex); 18802303f73SDan Williams return retval; 18902303f73SDan Williams } 19002303f73SDan Williams 19100a0d0d6SAlan Cox int usb_wwan_ioctl(struct tty_struct *tty, 19202303f73SDan Williams unsigned int cmd, unsigned long arg) 19302303f73SDan Williams { 19402303f73SDan Williams struct usb_serial_port *port = tty->driver_data; 19502303f73SDan Williams 196a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); 19702303f73SDan Williams 19802303f73SDan Williams switch (cmd) { 19902303f73SDan Williams case TIOCGSERIAL: 20002303f73SDan Williams return get_serial_info(port, 20102303f73SDan Williams (struct serial_struct __user *) arg); 20202303f73SDan Williams case TIOCSSERIAL: 20302303f73SDan Williams return set_serial_info(port, 20402303f73SDan Williams (struct serial_struct __user *) arg); 20502303f73SDan Williams default: 20602303f73SDan Williams break; 20702303f73SDan Williams } 20802303f73SDan Williams 209a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s arg not supported\n", __func__); 21002303f73SDan Williams 21102303f73SDan Williams return -ENOIOCTLCMD; 21202303f73SDan Williams } 21302303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl); 21402303f73SDan Williams 2150d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port, 2160d456194SMatthew Garrett const unsigned char *buf, int count) 2170d456194SMatthew Garrett { 2180d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 2190d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 2200d456194SMatthew Garrett int i; 2210d456194SMatthew Garrett int left, todo; 2220d456194SMatthew Garrett struct urb *this_urb = NULL; /* spurious */ 2230d456194SMatthew Garrett int err; 2240d456194SMatthew Garrett unsigned long flags; 2250d456194SMatthew Garrett 2260d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 22737357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial); 2280d456194SMatthew Garrett 229a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count); 2300d456194SMatthew Garrett 2310d456194SMatthew Garrett i = 0; 2320d456194SMatthew Garrett left = count; 2330d456194SMatthew Garrett for (i = 0; left > 0 && i < N_OUT_URB; i++) { 2340d456194SMatthew Garrett todo = left; 2350d456194SMatthew Garrett if (todo > OUT_BUFLEN) 2360d456194SMatthew Garrett todo = OUT_BUFLEN; 2370d456194SMatthew Garrett 2380d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 2390d456194SMatthew Garrett if (test_and_set_bit(i, &portdata->out_busy)) { 2400d456194SMatthew Garrett if (time_before(jiffies, 2410d456194SMatthew Garrett portdata->tx_start_time[i] + 10 * HZ)) 2420d456194SMatthew Garrett continue; 2430d456194SMatthew Garrett usb_unlink_urb(this_urb); 2440d456194SMatthew Garrett continue; 2450d456194SMatthew Garrett } 246a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__, 2470d456194SMatthew Garrett usb_pipeendpoint(this_urb->pipe), i); 2480d456194SMatthew Garrett 2490d456194SMatthew Garrett err = usb_autopm_get_interface_async(port->serial->interface); 250db090473Sxiao jin if (err < 0) { 251db090473Sxiao jin clear_bit(i, &portdata->out_busy); 2520d456194SMatthew Garrett break; 253db090473Sxiao jin } 2540d456194SMatthew Garrett 2550d456194SMatthew Garrett /* send the data */ 2560d456194SMatthew Garrett memcpy(this_urb->transfer_buffer, buf, todo); 2570d456194SMatthew Garrett this_urb->transfer_buffer_length = todo; 2580d456194SMatthew Garrett 2590d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2600d456194SMatthew Garrett if (intfdata->suspended) { 2610d456194SMatthew Garrett usb_anchor_urb(this_urb, &portdata->delayed); 2620d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2630d456194SMatthew Garrett } else { 2640d456194SMatthew Garrett intfdata->in_flight++; 2650d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2660d456194SMatthew Garrett err = usb_submit_urb(this_urb, GFP_ATOMIC); 2670d456194SMatthew Garrett if (err) { 2688bb7ec65SJohan Hovold dev_err(&port->dev, 2698bb7ec65SJohan Hovold "%s: submit urb %d failed: %d\n", 2708bb7ec65SJohan Hovold __func__, i, err); 2710d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 2720d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2730d456194SMatthew Garrett intfdata->in_flight--; 2740d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, 2750d456194SMatthew Garrett flags); 2763d06bf15SOliver Neukum usb_autopm_put_interface_async(port->serial->interface); 277433508aeSOliver Neukum break; 2780d456194SMatthew Garrett } 2790d456194SMatthew Garrett } 2800d456194SMatthew Garrett 2810d456194SMatthew Garrett portdata->tx_start_time[i] = jiffies; 2820d456194SMatthew Garrett buf += todo; 2830d456194SMatthew Garrett left -= todo; 2840d456194SMatthew Garrett } 2850d456194SMatthew Garrett 2860d456194SMatthew Garrett count -= left; 287a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count); 2880d456194SMatthew Garrett return count; 2890d456194SMatthew Garrett } 2900d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write); 2910d456194SMatthew Garrett 2920d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb) 2930d456194SMatthew Garrett { 2940d456194SMatthew Garrett int err; 2950d456194SMatthew Garrett int endpoint; 2960d456194SMatthew Garrett struct usb_serial_port *port; 297a80be97dSGreg Kroah-Hartman struct device *dev; 2980d456194SMatthew Garrett unsigned char *data = urb->transfer_buffer; 2990d456194SMatthew Garrett int status = urb->status; 3000d456194SMatthew Garrett 3010d456194SMatthew Garrett endpoint = usb_pipeendpoint(urb->pipe); 3020d456194SMatthew Garrett port = urb->context; 303a80be97dSGreg Kroah-Hartman dev = &port->dev; 3040d456194SMatthew Garrett 3050d456194SMatthew Garrett if (status) { 306a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n", 3070d456194SMatthew Garrett __func__, status, endpoint); 3080d456194SMatthew Garrett } else { 3090d456194SMatthew Garrett if (urb->actual_length) { 31005c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data, 31138237fd2SJiri Slaby urb->actual_length); 3122e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 3130d456194SMatthew Garrett } else 314a80be97dSGreg Kroah-Hartman dev_dbg(dev, "%s: empty read urb received\n", __func__); 3156c1ee66aSMatt Burtch } 3160d456194SMatthew Garrett /* Resubmit urb so we continue receiving */ 3170d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 318c9c4558fSOliver Neukum if (err) { 319d2958d1bSJohan Hovold if (err != -EPERM && err != -ENODEV) { 3206c1ee66aSMatt Burtch dev_err(dev, "%s: resubmit read urb failed. (%d)\n", 3216c1ee66aSMatt Burtch __func__, err); 322c9c4558fSOliver Neukum /* busy also in error unless we are killed */ 3230d456194SMatthew Garrett usb_mark_last_busy(port->serial->dev); 3240d456194SMatthew Garrett } 325c9c4558fSOliver Neukum } else { 326c9c4558fSOliver Neukum usb_mark_last_busy(port->serial->dev); 327c9c4558fSOliver Neukum } 328c9c4558fSOliver Neukum } 3290d456194SMatthew Garrett 3300d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb) 3310d456194SMatthew Garrett { 3320d456194SMatthew Garrett struct usb_serial_port *port; 3330d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3340d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 3350d456194SMatthew Garrett int i; 3360d456194SMatthew Garrett 3370d456194SMatthew Garrett port = urb->context; 33837357ca5SJohan Hovold intfdata = usb_get_serial_data(port->serial); 3390d456194SMatthew Garrett 3400d456194SMatthew Garrett usb_serial_port_softint(port); 3410d456194SMatthew Garrett usb_autopm_put_interface_async(port->serial->interface); 3420d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3430d456194SMatthew Garrett spin_lock(&intfdata->susp_lock); 3440d456194SMatthew Garrett intfdata->in_flight--; 3450d456194SMatthew Garrett spin_unlock(&intfdata->susp_lock); 3460d456194SMatthew Garrett 3470d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; ++i) { 3480d456194SMatthew Garrett if (portdata->out_urbs[i] == urb) { 3494e857c58SPeter Zijlstra smp_mb__before_atomic(); 3500d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 3510d456194SMatthew Garrett break; 3520d456194SMatthew Garrett } 3530d456194SMatthew Garrett } 3540d456194SMatthew Garrett } 3550d456194SMatthew Garrett 3560d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty) 3570d456194SMatthew Garrett { 3580d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3590d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3600d456194SMatthew Garrett int i; 3610d456194SMatthew Garrett int data_len = 0; 3620d456194SMatthew Garrett struct urb *this_urb; 3630d456194SMatthew Garrett 3640d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3650d456194SMatthew Garrett 3660d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3670d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3680d456194SMatthew Garrett if (this_urb && !test_bit(i, &portdata->out_busy)) 3690d456194SMatthew Garrett data_len += OUT_BUFLEN; 3700d456194SMatthew Garrett } 3710d456194SMatthew Garrett 372a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); 3730d456194SMatthew Garrett return data_len; 3740d456194SMatthew Garrett } 3750d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room); 3760d456194SMatthew Garrett 3770d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty) 3780d456194SMatthew Garrett { 3790d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3800d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3810d456194SMatthew Garrett int i; 3820d456194SMatthew Garrett int data_len = 0; 3830d456194SMatthew Garrett struct urb *this_urb; 3840d456194SMatthew Garrett 3850d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3860d456194SMatthew Garrett 3870d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3880d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3890d456194SMatthew Garrett /* FIXME: This locking is insufficient as this_urb may 3900d456194SMatthew Garrett go unused during the test */ 3910d456194SMatthew Garrett if (this_urb && test_bit(i, &portdata->out_busy)) 3920d456194SMatthew Garrett data_len += this_urb->transfer_buffer_length; 3930d456194SMatthew Garrett } 394a80be97dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s: %d\n", __func__, data_len); 3950d456194SMatthew Garrett return data_len; 3960d456194SMatthew Garrett } 3970d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer); 3980d456194SMatthew Garrett 3990d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port) 4000d456194SMatthew Garrett { 4010d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 4020d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 4030d456194SMatthew Garrett struct usb_serial *serial = port->serial; 4040d456194SMatthew Garrett int i, err; 4050d456194SMatthew Garrett struct urb *urb; 4060d456194SMatthew Garrett 4070d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 40837357ca5SJohan Hovold intfdata = usb_get_serial_data(serial); 4090d456194SMatthew Garrett 4109096f1fbSJohan Hovold if (port->interrupt_in_urb) { 4119096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 4129096f1fbSJohan Hovold if (err) { 4138bb7ec65SJohan Hovold dev_err(&port->dev, "%s: submit int urb failed: %d\n", 4149096f1fbSJohan Hovold __func__, err); 4159096f1fbSJohan Hovold } 4169096f1fbSJohan Hovold } 4179096f1fbSJohan Hovold 4180d456194SMatthew Garrett /* Start reading from the IN endpoint */ 4190d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) { 4200d456194SMatthew Garrett urb = portdata->in_urbs[i]; 4210d456194SMatthew Garrett if (!urb) 4220d456194SMatthew Garrett continue; 4230d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_KERNEL); 4240d456194SMatthew Garrett if (err) { 4258bb7ec65SJohan Hovold dev_err(&port->dev, 4268bb7ec65SJohan Hovold "%s: submit read urb %d failed: %d\n", 4278bb7ec65SJohan Hovold __func__, i, err); 4280d456194SMatthew Garrett } 4290d456194SMatthew Garrett } 4300d456194SMatthew Garrett 4310d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 432c1c01803SJohan Hovold if (++intfdata->open_ports == 1) 433c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 1; 4340d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 4359a91aedcSOliver Neukum /* this balances a get in the generic USB serial code */ 4360d456194SMatthew Garrett usb_autopm_put_interface(serial->interface); 4370d456194SMatthew Garrett 4380d456194SMatthew Garrett return 0; 4390d456194SMatthew Garrett } 4400d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_open); 4410d456194SMatthew Garrett 44279eed03eSJohan Hovold static void unbusy_queued_urb(struct urb *urb, 44379eed03eSJohan Hovold struct usb_wwan_port_private *portdata) 44479eed03eSJohan Hovold { 44579eed03eSJohan Hovold int i; 44679eed03eSJohan Hovold 44779eed03eSJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 44879eed03eSJohan Hovold if (urb == portdata->out_urbs[i]) { 44979eed03eSJohan Hovold clear_bit(i, &portdata->out_busy); 45079eed03eSJohan Hovold break; 45179eed03eSJohan Hovold } 45279eed03eSJohan Hovold } 45379eed03eSJohan Hovold } 45479eed03eSJohan Hovold 4550d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port) 4560d456194SMatthew Garrett { 4570d456194SMatthew Garrett int i; 4580d456194SMatthew Garrett struct usb_serial *serial = port->serial; 4590d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 46037357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial); 46179eed03eSJohan Hovold struct urb *urb; 4620d456194SMatthew Garrett 4630d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 4640d456194SMatthew Garrett 465b0a9aa6dSJohan Hovold /* 466b0a9aa6dSJohan Hovold * Need to take susp_lock to make sure port is not already being 467b0a9aa6dSJohan Hovold * resumed, but no need to hold it due to ASYNC_INITIALIZED. 468b0a9aa6dSJohan Hovold */ 4690d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 470c1c01803SJohan Hovold if (--intfdata->open_ports == 0) 471c1c01803SJohan Hovold serial->interface->needs_remote_wakeup = 0; 4720d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 4730d456194SMatthew Garrett 47479eed03eSJohan Hovold for (;;) { 47579eed03eSJohan Hovold urb = usb_get_from_anchor(&portdata->delayed); 47679eed03eSJohan Hovold if (!urb) 47779eed03eSJohan Hovold break; 47879eed03eSJohan Hovold unbusy_queued_urb(urb, portdata); 47979eed03eSJohan Hovold usb_autopm_put_interface_async(serial->interface); 48079eed03eSJohan Hovold } 48179eed03eSJohan Hovold 4820d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) 4830d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[i]); 4840d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) 4850d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[i]); 4869096f1fbSJohan Hovold usb_kill_urb(port->interrupt_in_urb); 487e6d144bcSJohan Hovold 4889a91aedcSOliver Neukum usb_autopm_get_interface_no_resume(serial->interface); 4890d456194SMatthew Garrett } 4900d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close); 4910d456194SMatthew Garrett 492b8f0e820SJohan Hovold static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port, 493b8f0e820SJohan Hovold int endpoint, 4940d456194SMatthew Garrett int dir, void *ctx, char *buf, int len, 4950d456194SMatthew Garrett void (*callback) (struct urb *)) 4960d456194SMatthew Garrett { 497b8f0e820SJohan Hovold struct usb_serial *serial = port->serial; 4980d456194SMatthew Garrett struct urb *urb; 4990d456194SMatthew Garrett 5000d456194SMatthew Garrett urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ 50110c642d0SJohan Hovold if (!urb) 5020d456194SMatthew Garrett return NULL; 5030d456194SMatthew Garrett 5040d456194SMatthew Garrett usb_fill_bulk_urb(urb, serial->dev, 5050d456194SMatthew Garrett usb_sndbulkpipe(serial->dev, endpoint) | dir, 5060d456194SMatthew Garrett buf, len, callback, ctx); 5070d456194SMatthew Garrett 5080d456194SMatthew Garrett return urb; 5090d456194SMatthew Garrett } 5100d456194SMatthew Garrett 511b8f0e820SJohan Hovold int usb_wwan_port_probe(struct usb_serial_port *port) 5120d456194SMatthew Garrett { 5130d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 514b8f0e820SJohan Hovold struct urb *urb; 5150d456194SMatthew Garrett u8 *buffer; 516b8f0e820SJohan Hovold int i; 5170d456194SMatthew Garrett 518bd73bd88SJohan Hovold if (!port->bulk_in_size || !port->bulk_out_size) 519bd73bd88SJohan Hovold return -ENODEV; 520bd73bd88SJohan Hovold 5210d456194SMatthew Garrett portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 522b8f0e820SJohan Hovold if (!portdata) 523b8f0e820SJohan Hovold return -ENOMEM; 524b8f0e820SJohan Hovold 5250d456194SMatthew Garrett init_usb_anchor(&portdata->delayed); 5260d456194SMatthew Garrett 527b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) { 5280d456194SMatthew Garrett buffer = (u8 *)__get_free_page(GFP_KERNEL); 5290d456194SMatthew Garrett if (!buffer) 5300d456194SMatthew Garrett goto bail_out_error; 531b8f0e820SJohan Hovold portdata->in_buffer[i] = buffer; 532b8f0e820SJohan Hovold 533b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress, 534b8f0e820SJohan Hovold USB_DIR_IN, port, 535b8f0e820SJohan Hovold buffer, IN_BUFLEN, 536b8f0e820SJohan Hovold usb_wwan_indat_callback); 537b8f0e820SJohan Hovold portdata->in_urbs[i] = urb; 5380d456194SMatthew Garrett } 5390d456194SMatthew Garrett 540b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 5410d456194SMatthew Garrett buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); 5420d456194SMatthew Garrett if (!buffer) 5430d456194SMatthew Garrett goto bail_out_error2; 544b8f0e820SJohan Hovold portdata->out_buffer[i] = buffer; 545b8f0e820SJohan Hovold 546b8f0e820SJohan Hovold urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress, 547b8f0e820SJohan Hovold USB_DIR_OUT, port, 548b8f0e820SJohan Hovold buffer, OUT_BUFLEN, 549b8f0e820SJohan Hovold usb_wwan_outdat_callback); 550b8f0e820SJohan Hovold portdata->out_urbs[i] = urb; 5510d456194SMatthew Garrett } 5520d456194SMatthew Garrett 5530d456194SMatthew Garrett usb_set_serial_port_data(port, portdata); 5540d456194SMatthew Garrett 5550d456194SMatthew Garrett return 0; 5560d456194SMatthew Garrett 5570d456194SMatthew Garrett bail_out_error2: 558b8f0e820SJohan Hovold for (i = 0; i < N_OUT_URB; i++) { 559b8f0e820SJohan Hovold usb_free_urb(portdata->out_urbs[i]); 560b8f0e820SJohan Hovold kfree(portdata->out_buffer[i]); 5610d456194SMatthew Garrett } 562b8f0e820SJohan Hovold bail_out_error: 563b8f0e820SJohan Hovold for (i = 0; i < N_IN_URB; i++) { 564b8f0e820SJohan Hovold usb_free_urb(portdata->in_urbs[i]); 565b8f0e820SJohan Hovold free_page((unsigned long)portdata->in_buffer[i]); 566b8f0e820SJohan Hovold } 567b8f0e820SJohan Hovold kfree(portdata); 568b8f0e820SJohan Hovold 569b8f0e820SJohan Hovold return -ENOMEM; 570b8f0e820SJohan Hovold } 571b8f0e820SJohan Hovold EXPORT_SYMBOL_GPL(usb_wwan_port_probe); 5720d456194SMatthew Garrett 573a1028f0aSBjørn Mork int usb_wwan_port_remove(struct usb_serial_port *port) 574a1028f0aSBjørn Mork { 575a1028f0aSBjørn Mork int i; 576a1028f0aSBjørn Mork struct usb_wwan_port_private *portdata; 577a1028f0aSBjørn Mork 578a1028f0aSBjørn Mork portdata = usb_get_serial_port_data(port); 579a1028f0aSBjørn Mork usb_set_serial_port_data(port, NULL); 580a1028f0aSBjørn Mork 581a1028f0aSBjørn Mork for (i = 0; i < N_IN_URB; i++) { 582a1028f0aSBjørn Mork usb_free_urb(portdata->in_urbs[i]); 583a1028f0aSBjørn Mork free_page((unsigned long)portdata->in_buffer[i]); 584a1028f0aSBjørn Mork } 585a1028f0aSBjørn Mork for (i = 0; i < N_OUT_URB; i++) { 586a1028f0aSBjørn Mork usb_free_urb(portdata->out_urbs[i]); 587a1028f0aSBjørn Mork kfree(portdata->out_buffer[i]); 588a1028f0aSBjørn Mork } 589a1028f0aSBjørn Mork 590a1028f0aSBjørn Mork kfree(portdata); 5912b4aceabSJohan Hovold 592a1028f0aSBjørn Mork return 0; 593a1028f0aSBjørn Mork } 594a1028f0aSBjørn Mork EXPORT_SYMBOL(usb_wwan_port_remove); 595a1028f0aSBjørn Mork 596a1028f0aSBjørn Mork #ifdef CONFIG_PM 597ae75c940SJohan Hovold static void stop_urbs(struct usb_serial *serial) 5980d456194SMatthew Garrett { 5990d456194SMatthew Garrett int i, j; 6000d456194SMatthew Garrett struct usb_serial_port *port; 6010d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6020d456194SMatthew Garrett 6030d456194SMatthew Garrett for (i = 0; i < serial->num_ports; ++i) { 6040d456194SMatthew Garrett port = serial->port[i]; 6050d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 606032129cbSBjørn Mork if (!portdata) 607032129cbSBjørn Mork continue; 6080d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) 6090d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[j]); 6100d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) 6110d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[j]); 612ae75c940SJohan Hovold usb_kill_urb(port->interrupt_in_urb); 6130d456194SMatthew Garrett } 6140d456194SMatthew Garrett } 6150d456194SMatthew Garrett 6160d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message) 6170d456194SMatthew Garrett { 61837357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial); 6190d456194SMatthew Garrett 6200d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 621170fad9eSJohan Hovold if (PMSG_IS_AUTO(message)) { 622170fad9eSJohan Hovold if (intfdata->in_flight) { 6230d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 6240d456194SMatthew Garrett return -EBUSY; 6250d456194SMatthew Garrett } 626170fad9eSJohan Hovold } 6270d456194SMatthew Garrett intfdata->suspended = 1; 6280d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 629170fad9eSJohan Hovold 630ae75c940SJohan Hovold stop_urbs(serial); 6310d456194SMatthew Garrett 6320d456194SMatthew Garrett return 0; 6330d456194SMatthew Garrett } 6340d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend); 6350d456194SMatthew Garrett 6363362c91cSJohan Hovold /* Caller must hold susp_lock. */ 6373362c91cSJohan Hovold static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port) 6380d456194SMatthew Garrett { 6397436f412SJohan Hovold struct usb_serial *serial = port->serial; 64037357ca5SJohan Hovold struct usb_wwan_intf_private *data = usb_get_serial_data(serial); 6410d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6420d456194SMatthew Garrett struct urb *urb; 6437436f412SJohan Hovold int err_count = 0; 6447436f412SJohan Hovold int err; 6450d456194SMatthew Garrett 6460d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 64737357ca5SJohan Hovold 6483362c91cSJohan Hovold for (;;) { 6493362c91cSJohan Hovold urb = usb_get_from_anchor(&portdata->delayed); 6503362c91cSJohan Hovold if (!urb) 6513362c91cSJohan Hovold break; 6523362c91cSJohan Hovold 6530d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 6547436f412SJohan Hovold if (err) { 6553362c91cSJohan Hovold dev_err(&port->dev, "%s: submit urb failed: %d\n", 6567436f412SJohan Hovold __func__, err); 6577436f412SJohan Hovold err_count++; 65816871dcaSOliver Neukum unbusy_queued_urb(urb, portdata); 6597436f412SJohan Hovold usb_autopm_put_interface_async(serial->interface); 6607436f412SJohan Hovold continue; 66116871dcaSOliver Neukum } 6627436f412SJohan Hovold data->in_flight++; 6630d456194SMatthew Garrett } 664fb7ad4f9SJohan Hovold 6657436f412SJohan Hovold if (err_count) 6667436f412SJohan Hovold return -EIO; 6677436f412SJohan Hovold 6687436f412SJohan Hovold return 0; 6690d456194SMatthew Garrett } 6700d456194SMatthew Garrett 6710d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial) 6720d456194SMatthew Garrett { 6730d456194SMatthew Garrett int i, j; 6740d456194SMatthew Garrett struct usb_serial_port *port; 67537357ca5SJohan Hovold struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial); 6760d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6770d456194SMatthew Garrett struct urb *urb; 678fb7ad4f9SJohan Hovold int err; 679fb7ad4f9SJohan Hovold int err_count = 0; 6800d456194SMatthew Garrett 681d9e93c08Sxiao jin spin_lock_irq(&intfdata->susp_lock); 6820d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 6830d456194SMatthew Garrett port = serial->port[i]; 6840d456194SMatthew Garrett 685b0a9aa6dSJohan Hovold if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) 6860d456194SMatthew Garrett continue; 6870d456194SMatthew Garrett 688b0a9aa6dSJohan Hovold portdata = usb_get_serial_port_data(port); 689b0a9aa6dSJohan Hovold 6909096f1fbSJohan Hovold if (port->interrupt_in_urb) { 6919096f1fbSJohan Hovold err = usb_submit_urb(port->interrupt_in_urb, 6929096f1fbSJohan Hovold GFP_ATOMIC); 6939096f1fbSJohan Hovold if (err) { 6949096f1fbSJohan Hovold dev_err(&port->dev, 6959096f1fbSJohan Hovold "%s: submit int urb failed: %d\n", 6969096f1fbSJohan Hovold __func__, err); 697fb7ad4f9SJohan Hovold err_count++; 6989096f1fbSJohan Hovold } 6999096f1fbSJohan Hovold } 7009096f1fbSJohan Hovold 7013362c91cSJohan Hovold err = usb_wwan_submit_delayed_urbs(port); 702fb7ad4f9SJohan Hovold if (err) 703fb7ad4f9SJohan Hovold err_count++; 704fb7ad4f9SJohan Hovold 7050d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) { 7060d456194SMatthew Garrett urb = portdata->in_urbs[j]; 7070d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 7080d456194SMatthew Garrett if (err < 0) { 709b0f9d003SJohan Hovold dev_err(&port->dev, 710b0f9d003SJohan Hovold "%s: submit read urb %d failed: %d\n", 711b0f9d003SJohan Hovold __func__, i, err); 712fb7ad4f9SJohan Hovold err_count++; 7130d456194SMatthew Garrett } 7140d456194SMatthew Garrett } 7150d456194SMatthew Garrett } 7160d456194SMatthew Garrett intfdata->suspended = 0; 7170d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 718fb7ad4f9SJohan Hovold 719fb7ad4f9SJohan Hovold if (err_count) 720fb7ad4f9SJohan Hovold return -EIO; 721fb7ad4f9SJohan Hovold 722fb7ad4f9SJohan Hovold return 0; 7230d456194SMatthew Garrett } 7240d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume); 7250d456194SMatthew Garrett #endif 7260d456194SMatthew Garrett 7270d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR); 7280d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC); 7290d456194SMatthew Garrett MODULE_LICENSE("GPL"); 730