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_VERSION "v0.7.2" 230d456194SMatthew Garrett #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>" 240d456194SMatthew Garrett #define DRIVER_DESC "USB Driver for GSM modems" 250d456194SMatthew Garrett 260d456194SMatthew Garrett #include <linux/kernel.h> 270d456194SMatthew Garrett #include <linux/jiffies.h> 280d456194SMatthew Garrett #include <linux/errno.h> 290d456194SMatthew Garrett #include <linux/slab.h> 300d456194SMatthew Garrett #include <linux/tty.h> 310d456194SMatthew Garrett #include <linux/tty_flip.h> 320d456194SMatthew Garrett #include <linux/module.h> 330d456194SMatthew Garrett #include <linux/bitops.h> 340d456194SMatthew Garrett #include <linux/usb.h> 350d456194SMatthew Garrett #include <linux/usb/serial.h> 36*02303f73SDan Williams #include <linux/serial.h> 370d456194SMatthew Garrett #include "usb-wwan.h" 380d456194SMatthew Garrett 390d456194SMatthew Garrett static int debug; 400d456194SMatthew Garrett 410d456194SMatthew Garrett void usb_wwan_dtr_rts(struct usb_serial_port *port, int on) 420d456194SMatthew Garrett { 430d456194SMatthew Garrett struct usb_serial *serial = port->serial; 440d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 450d456194SMatthew Garrett 460d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 470d456194SMatthew Garrett 480d456194SMatthew Garrett dbg("%s", __func__); 490d456194SMatthew Garrett 500d456194SMatthew Garrett intfdata = port->serial->private; 510d456194SMatthew Garrett 520d456194SMatthew Garrett if (!intfdata->send_setup) 530d456194SMatthew Garrett return; 540d456194SMatthew Garrett 550d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 560d456194SMatthew Garrett mutex_lock(&serial->disc_mutex); 570d456194SMatthew Garrett portdata->rts_state = on; 580d456194SMatthew Garrett portdata->dtr_state = on; 590d456194SMatthew Garrett if (serial->dev) 600d456194SMatthew Garrett intfdata->send_setup(port); 610d456194SMatthew Garrett mutex_unlock(&serial->disc_mutex); 620d456194SMatthew Garrett } 630d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_dtr_rts); 640d456194SMatthew Garrett 650d456194SMatthew Garrett void usb_wwan_set_termios(struct tty_struct *tty, 660d456194SMatthew Garrett struct usb_serial_port *port, 670d456194SMatthew Garrett struct ktermios *old_termios) 680d456194SMatthew Garrett { 690d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = port->serial->private; 700d456194SMatthew Garrett 710d456194SMatthew Garrett dbg("%s", __func__); 720d456194SMatthew Garrett 730d456194SMatthew Garrett /* Doesn't support option setting */ 740d456194SMatthew Garrett tty_termios_copy_hw(tty->termios, old_termios); 750d456194SMatthew Garrett 760d456194SMatthew Garrett if (intfdata->send_setup) 770d456194SMatthew Garrett intfdata->send_setup(port); 780d456194SMatthew Garrett } 790d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_set_termios); 800d456194SMatthew Garrett 810d456194SMatthew Garrett int usb_wwan_tiocmget(struct tty_struct *tty, struct file *file) 820d456194SMatthew Garrett { 830d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 840d456194SMatthew Garrett unsigned int value; 850d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 860d456194SMatthew Garrett 870d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 880d456194SMatthew Garrett 890d456194SMatthew Garrett value = ((portdata->rts_state) ? TIOCM_RTS : 0) | 900d456194SMatthew Garrett ((portdata->dtr_state) ? TIOCM_DTR : 0) | 910d456194SMatthew Garrett ((portdata->cts_state) ? TIOCM_CTS : 0) | 920d456194SMatthew Garrett ((portdata->dsr_state) ? TIOCM_DSR : 0) | 930d456194SMatthew Garrett ((portdata->dcd_state) ? TIOCM_CAR : 0) | 940d456194SMatthew Garrett ((portdata->ri_state) ? TIOCM_RNG : 0); 950d456194SMatthew Garrett 960d456194SMatthew Garrett return value; 970d456194SMatthew Garrett } 980d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmget); 990d456194SMatthew Garrett 1000d456194SMatthew Garrett int usb_wwan_tiocmset(struct tty_struct *tty, struct file *file, 1010d456194SMatthew Garrett unsigned int set, unsigned int clear) 1020d456194SMatthew Garrett { 1030d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 1040d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 1050d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 1060d456194SMatthew Garrett 1070d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 1080d456194SMatthew Garrett intfdata = port->serial->private; 1090d456194SMatthew Garrett 1100d456194SMatthew Garrett if (!intfdata->send_setup) 1110d456194SMatthew Garrett return -EINVAL; 1120d456194SMatthew Garrett 1130d456194SMatthew Garrett /* FIXME: what locks portdata fields ? */ 1140d456194SMatthew Garrett if (set & TIOCM_RTS) 1150d456194SMatthew Garrett portdata->rts_state = 1; 1160d456194SMatthew Garrett if (set & TIOCM_DTR) 1170d456194SMatthew Garrett portdata->dtr_state = 1; 1180d456194SMatthew Garrett 1190d456194SMatthew Garrett if (clear & TIOCM_RTS) 1200d456194SMatthew Garrett portdata->rts_state = 0; 1210d456194SMatthew Garrett if (clear & TIOCM_DTR) 1220d456194SMatthew Garrett portdata->dtr_state = 0; 1230d456194SMatthew Garrett return intfdata->send_setup(port); 1240d456194SMatthew Garrett } 1250d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_tiocmset); 1260d456194SMatthew Garrett 127*02303f73SDan Williams static int get_serial_info(struct usb_serial_port *port, 128*02303f73SDan Williams struct serial_struct __user *retinfo) 129*02303f73SDan Williams { 130*02303f73SDan Williams struct serial_struct tmp; 131*02303f73SDan Williams 132*02303f73SDan Williams if (!retinfo) 133*02303f73SDan Williams return -EFAULT; 134*02303f73SDan Williams 135*02303f73SDan Williams memset(&tmp, 0, sizeof(tmp)); 136*02303f73SDan Williams tmp.line = port->serial->minor; 137*02303f73SDan Williams tmp.port = port->number; 138*02303f73SDan Williams tmp.baud_base = tty_get_baud_rate(port->port.tty); 139*02303f73SDan Williams tmp.close_delay = port->port.close_delay / 10; 140*02303f73SDan Williams tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 141*02303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : 142*02303f73SDan Williams port->port.closing_wait / 10; 143*02303f73SDan Williams 144*02303f73SDan Williams if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 145*02303f73SDan Williams return -EFAULT; 146*02303f73SDan Williams return 0; 147*02303f73SDan Williams } 148*02303f73SDan Williams 149*02303f73SDan Williams static int set_serial_info(struct usb_serial_port *port, 150*02303f73SDan Williams struct serial_struct __user *newinfo) 151*02303f73SDan Williams { 152*02303f73SDan Williams struct serial_struct new_serial; 153*02303f73SDan Williams unsigned int closing_wait, close_delay; 154*02303f73SDan Williams int retval = 0; 155*02303f73SDan Williams 156*02303f73SDan Williams if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 157*02303f73SDan Williams return -EFAULT; 158*02303f73SDan Williams 159*02303f73SDan Williams close_delay = new_serial.close_delay * 10; 160*02303f73SDan Williams closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 161*02303f73SDan Williams ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; 162*02303f73SDan Williams 163*02303f73SDan Williams mutex_lock(&port->port.mutex); 164*02303f73SDan Williams 165*02303f73SDan Williams if (!capable(CAP_SYS_ADMIN)) { 166*02303f73SDan Williams if ((close_delay != port->port.close_delay) || 167*02303f73SDan Williams (closing_wait != port->port.closing_wait)) 168*02303f73SDan Williams retval = -EPERM; 169*02303f73SDan Williams else 170*02303f73SDan Williams retval = -EOPNOTSUPP; 171*02303f73SDan Williams } else { 172*02303f73SDan Williams port->port.close_delay = close_delay; 173*02303f73SDan Williams port->port.closing_wait = closing_wait; 174*02303f73SDan Williams } 175*02303f73SDan Williams 176*02303f73SDan Williams mutex_unlock(&port->port.mutex); 177*02303f73SDan Williams return retval; 178*02303f73SDan Williams } 179*02303f73SDan Williams 180*02303f73SDan Williams int usb_wwan_ioctl(struct tty_struct *tty, struct file *file, 181*02303f73SDan Williams unsigned int cmd, unsigned long arg) 182*02303f73SDan Williams { 183*02303f73SDan Williams struct usb_serial_port *port = tty->driver_data; 184*02303f73SDan Williams 185*02303f73SDan Williams dbg("%s cmd 0x%04x", __func__, cmd); 186*02303f73SDan Williams 187*02303f73SDan Williams switch (cmd) { 188*02303f73SDan Williams case TIOCGSERIAL: 189*02303f73SDan Williams return get_serial_info(port, 190*02303f73SDan Williams (struct serial_struct __user *) arg); 191*02303f73SDan Williams case TIOCSSERIAL: 192*02303f73SDan Williams return set_serial_info(port, 193*02303f73SDan Williams (struct serial_struct __user *) arg); 194*02303f73SDan Williams default: 195*02303f73SDan Williams break; 196*02303f73SDan Williams } 197*02303f73SDan Williams 198*02303f73SDan Williams dbg("%s arg not supported", __func__); 199*02303f73SDan Williams 200*02303f73SDan Williams return -ENOIOCTLCMD; 201*02303f73SDan Williams } 202*02303f73SDan Williams EXPORT_SYMBOL(usb_wwan_ioctl); 203*02303f73SDan Williams 2040d456194SMatthew Garrett /* Write */ 2050d456194SMatthew Garrett int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port, 2060d456194SMatthew Garrett const unsigned char *buf, int count) 2070d456194SMatthew Garrett { 2080d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 2090d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 2100d456194SMatthew Garrett int i; 2110d456194SMatthew Garrett int left, todo; 2120d456194SMatthew Garrett struct urb *this_urb = NULL; /* spurious */ 2130d456194SMatthew Garrett int err; 2140d456194SMatthew Garrett unsigned long flags; 2150d456194SMatthew Garrett 2160d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 2170d456194SMatthew Garrett intfdata = port->serial->private; 2180d456194SMatthew Garrett 2190d456194SMatthew Garrett dbg("%s: write (%d chars)", __func__, count); 2200d456194SMatthew Garrett 2210d456194SMatthew Garrett i = 0; 2220d456194SMatthew Garrett left = count; 2230d456194SMatthew Garrett for (i = 0; left > 0 && i < N_OUT_URB; i++) { 2240d456194SMatthew Garrett todo = left; 2250d456194SMatthew Garrett if (todo > OUT_BUFLEN) 2260d456194SMatthew Garrett todo = OUT_BUFLEN; 2270d456194SMatthew Garrett 2280d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 2290d456194SMatthew Garrett if (test_and_set_bit(i, &portdata->out_busy)) { 2300d456194SMatthew Garrett if (time_before(jiffies, 2310d456194SMatthew Garrett portdata->tx_start_time[i] + 10 * HZ)) 2320d456194SMatthew Garrett continue; 2330d456194SMatthew Garrett usb_unlink_urb(this_urb); 2340d456194SMatthew Garrett continue; 2350d456194SMatthew Garrett } 2360d456194SMatthew Garrett dbg("%s: endpoint %d buf %d", __func__, 2370d456194SMatthew Garrett usb_pipeendpoint(this_urb->pipe), i); 2380d456194SMatthew Garrett 2390d456194SMatthew Garrett err = usb_autopm_get_interface_async(port->serial->interface); 2400d456194SMatthew Garrett if (err < 0) 2410d456194SMatthew Garrett break; 2420d456194SMatthew Garrett 2430d456194SMatthew Garrett /* send the data */ 2440d456194SMatthew Garrett memcpy(this_urb->transfer_buffer, buf, todo); 2450d456194SMatthew Garrett this_urb->transfer_buffer_length = todo; 2460d456194SMatthew Garrett 2470d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2480d456194SMatthew Garrett if (intfdata->suspended) { 2490d456194SMatthew Garrett usb_anchor_urb(this_urb, &portdata->delayed); 2500d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2510d456194SMatthew Garrett } else { 2520d456194SMatthew Garrett intfdata->in_flight++; 2530d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, flags); 2540d456194SMatthew Garrett err = usb_submit_urb(this_urb, GFP_ATOMIC); 2550d456194SMatthew Garrett if (err) { 2560d456194SMatthew Garrett dbg("usb_submit_urb %p (write bulk) failed " 2570d456194SMatthew Garrett "(%d)", this_urb, err); 2580d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 2590d456194SMatthew Garrett spin_lock_irqsave(&intfdata->susp_lock, flags); 2600d456194SMatthew Garrett intfdata->in_flight--; 2610d456194SMatthew Garrett spin_unlock_irqrestore(&intfdata->susp_lock, 2620d456194SMatthew Garrett flags); 2630d456194SMatthew Garrett continue; 2640d456194SMatthew Garrett } 2650d456194SMatthew Garrett } 2660d456194SMatthew Garrett 2670d456194SMatthew Garrett portdata->tx_start_time[i] = jiffies; 2680d456194SMatthew Garrett buf += todo; 2690d456194SMatthew Garrett left -= todo; 2700d456194SMatthew Garrett } 2710d456194SMatthew Garrett 2720d456194SMatthew Garrett count -= left; 2730d456194SMatthew Garrett dbg("%s: wrote (did %d)", __func__, count); 2740d456194SMatthew Garrett return count; 2750d456194SMatthew Garrett } 2760d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write); 2770d456194SMatthew Garrett 2780d456194SMatthew Garrett static void usb_wwan_indat_callback(struct urb *urb) 2790d456194SMatthew Garrett { 2800d456194SMatthew Garrett int err; 2810d456194SMatthew Garrett int endpoint; 2820d456194SMatthew Garrett struct usb_serial_port *port; 2830d456194SMatthew Garrett struct tty_struct *tty; 2840d456194SMatthew Garrett unsigned char *data = urb->transfer_buffer; 2850d456194SMatthew Garrett int status = urb->status; 2860d456194SMatthew Garrett 2870d456194SMatthew Garrett dbg("%s: %p", __func__, urb); 2880d456194SMatthew Garrett 2890d456194SMatthew Garrett endpoint = usb_pipeendpoint(urb->pipe); 2900d456194SMatthew Garrett port = urb->context; 2910d456194SMatthew Garrett 2920d456194SMatthew Garrett if (status) { 2930d456194SMatthew Garrett dbg("%s: nonzero status: %d on endpoint %02x.", 2940d456194SMatthew Garrett __func__, status, endpoint); 2950d456194SMatthew Garrett } else { 2960d456194SMatthew Garrett tty = tty_port_tty_get(&port->port); 2970d456194SMatthew Garrett if (urb->actual_length) { 2980d456194SMatthew Garrett tty_insert_flip_string(tty, data, urb->actual_length); 2990d456194SMatthew Garrett tty_flip_buffer_push(tty); 3000d456194SMatthew Garrett } else 3010d456194SMatthew Garrett dbg("%s: empty read urb received", __func__); 3020d456194SMatthew Garrett tty_kref_put(tty); 3030d456194SMatthew Garrett 3040d456194SMatthew Garrett /* Resubmit urb so we continue receiving */ 3050d456194SMatthew Garrett if (status != -ESHUTDOWN) { 3060d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 3070d456194SMatthew Garrett if (err && err != -EPERM) 3080d456194SMatthew Garrett printk(KERN_ERR "%s: resubmit read urb failed. " 3090d456194SMatthew Garrett "(%d)", __func__, err); 3100d456194SMatthew Garrett else 3110d456194SMatthew Garrett usb_mark_last_busy(port->serial->dev); 3120d456194SMatthew Garrett } 3130d456194SMatthew Garrett 3140d456194SMatthew Garrett } 3150d456194SMatthew Garrett } 3160d456194SMatthew Garrett 3170d456194SMatthew Garrett static void usb_wwan_outdat_callback(struct urb *urb) 3180d456194SMatthew Garrett { 3190d456194SMatthew Garrett struct usb_serial_port *port; 3200d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3210d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 3220d456194SMatthew Garrett int i; 3230d456194SMatthew Garrett 3240d456194SMatthew Garrett dbg("%s", __func__); 3250d456194SMatthew Garrett 3260d456194SMatthew Garrett port = urb->context; 3270d456194SMatthew Garrett intfdata = port->serial->private; 3280d456194SMatthew Garrett 3290d456194SMatthew Garrett usb_serial_port_softint(port); 3300d456194SMatthew Garrett usb_autopm_put_interface_async(port->serial->interface); 3310d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3320d456194SMatthew Garrett spin_lock(&intfdata->susp_lock); 3330d456194SMatthew Garrett intfdata->in_flight--; 3340d456194SMatthew Garrett spin_unlock(&intfdata->susp_lock); 3350d456194SMatthew Garrett 3360d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; ++i) { 3370d456194SMatthew Garrett if (portdata->out_urbs[i] == urb) { 3380d456194SMatthew Garrett smp_mb__before_clear_bit(); 3390d456194SMatthew Garrett clear_bit(i, &portdata->out_busy); 3400d456194SMatthew Garrett break; 3410d456194SMatthew Garrett } 3420d456194SMatthew Garrett } 3430d456194SMatthew Garrett } 3440d456194SMatthew Garrett 3450d456194SMatthew Garrett int usb_wwan_write_room(struct tty_struct *tty) 3460d456194SMatthew Garrett { 3470d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3480d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3490d456194SMatthew Garrett int i; 3500d456194SMatthew Garrett int data_len = 0; 3510d456194SMatthew Garrett struct urb *this_urb; 3520d456194SMatthew Garrett 3530d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3540d456194SMatthew Garrett 3550d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3560d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3570d456194SMatthew Garrett if (this_urb && !test_bit(i, &portdata->out_busy)) 3580d456194SMatthew Garrett data_len += OUT_BUFLEN; 3590d456194SMatthew Garrett } 3600d456194SMatthew Garrett 3610d456194SMatthew Garrett dbg("%s: %d", __func__, data_len); 3620d456194SMatthew Garrett return data_len; 3630d456194SMatthew Garrett } 3640d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_write_room); 3650d456194SMatthew Garrett 3660d456194SMatthew Garrett int usb_wwan_chars_in_buffer(struct tty_struct *tty) 3670d456194SMatthew Garrett { 3680d456194SMatthew Garrett struct usb_serial_port *port = tty->driver_data; 3690d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3700d456194SMatthew Garrett int i; 3710d456194SMatthew Garrett int data_len = 0; 3720d456194SMatthew Garrett struct urb *this_urb; 3730d456194SMatthew Garrett 3740d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3750d456194SMatthew Garrett 3760d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) { 3770d456194SMatthew Garrett this_urb = portdata->out_urbs[i]; 3780d456194SMatthew Garrett /* FIXME: This locking is insufficient as this_urb may 3790d456194SMatthew Garrett go unused during the test */ 3800d456194SMatthew Garrett if (this_urb && test_bit(i, &portdata->out_busy)) 3810d456194SMatthew Garrett data_len += this_urb->transfer_buffer_length; 3820d456194SMatthew Garrett } 3830d456194SMatthew Garrett dbg("%s: %d", __func__, data_len); 3840d456194SMatthew Garrett return data_len; 3850d456194SMatthew Garrett } 3860d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_chars_in_buffer); 3870d456194SMatthew Garrett 3880d456194SMatthew Garrett int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port) 3890d456194SMatthew Garrett { 3900d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 3910d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata; 3920d456194SMatthew Garrett struct usb_serial *serial = port->serial; 3930d456194SMatthew Garrett int i, err; 3940d456194SMatthew Garrett struct urb *urb; 3950d456194SMatthew Garrett 3960d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 3970d456194SMatthew Garrett intfdata = serial->private; 3980d456194SMatthew Garrett 3990d456194SMatthew Garrett dbg("%s", __func__); 4000d456194SMatthew Garrett 4010d456194SMatthew Garrett /* Start reading from the IN endpoint */ 4020d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) { 4030d456194SMatthew Garrett urb = portdata->in_urbs[i]; 4040d456194SMatthew Garrett if (!urb) 4050d456194SMatthew Garrett continue; 4060d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_KERNEL); 4070d456194SMatthew Garrett if (err) { 4080d456194SMatthew Garrett dbg("%s: submit urb %d failed (%d) %d", 4090d456194SMatthew Garrett __func__, i, err, urb->transfer_buffer_length); 4100d456194SMatthew Garrett } 4110d456194SMatthew Garrett } 4120d456194SMatthew Garrett 4130d456194SMatthew Garrett if (intfdata->send_setup) 4140d456194SMatthew Garrett intfdata->send_setup(port); 4150d456194SMatthew Garrett 4160d456194SMatthew Garrett serial->interface->needs_remote_wakeup = 1; 4170d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 4180d456194SMatthew Garrett portdata->opened = 1; 4190d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 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 4260d456194SMatthew Garrett void usb_wwan_close(struct usb_serial_port *port) 4270d456194SMatthew Garrett { 4280d456194SMatthew Garrett int i; 4290d456194SMatthew Garrett struct usb_serial *serial = port->serial; 4300d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 4310d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = port->serial->private; 4320d456194SMatthew Garrett 4330d456194SMatthew Garrett dbg("%s", __func__); 4340d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 4350d456194SMatthew Garrett 4360d456194SMatthew Garrett if (serial->dev) { 4370d456194SMatthew Garrett /* Stop reading/writing urbs */ 4380d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 4390d456194SMatthew Garrett portdata->opened = 0; 4400d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 4410d456194SMatthew Garrett 4420d456194SMatthew Garrett for (i = 0; i < N_IN_URB; i++) 4430d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[i]); 4440d456194SMatthew Garrett for (i = 0; i < N_OUT_URB; i++) 4450d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[i]); 4460d456194SMatthew Garrett usb_autopm_get_interface(serial->interface); 4470d456194SMatthew Garrett serial->interface->needs_remote_wakeup = 0; 4480d456194SMatthew Garrett } 4490d456194SMatthew Garrett } 4500d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_close); 4510d456194SMatthew Garrett 4520d456194SMatthew Garrett /* Helper functions used by usb_wwan_setup_urbs */ 4530d456194SMatthew Garrett static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint, 4540d456194SMatthew Garrett int dir, void *ctx, char *buf, int len, 4550d456194SMatthew Garrett void (*callback) (struct urb *)) 4560d456194SMatthew Garrett { 4570d456194SMatthew Garrett struct urb *urb; 4580d456194SMatthew Garrett 4590d456194SMatthew Garrett if (endpoint == -1) 4600d456194SMatthew Garrett return NULL; /* endpoint not needed */ 4610d456194SMatthew Garrett 4620d456194SMatthew Garrett urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ 4630d456194SMatthew Garrett if (urb == NULL) { 4640d456194SMatthew Garrett dbg("%s: alloc for endpoint %d failed.", __func__, endpoint); 4650d456194SMatthew Garrett return NULL; 4660d456194SMatthew Garrett } 4670d456194SMatthew Garrett 4680d456194SMatthew Garrett /* Fill URB using supplied data. */ 4690d456194SMatthew Garrett usb_fill_bulk_urb(urb, serial->dev, 4700d456194SMatthew Garrett usb_sndbulkpipe(serial->dev, endpoint) | dir, 4710d456194SMatthew Garrett buf, len, callback, ctx); 4720d456194SMatthew Garrett 4730d456194SMatthew Garrett return urb; 4740d456194SMatthew Garrett } 4750d456194SMatthew Garrett 4760d456194SMatthew Garrett /* Setup urbs */ 4770d456194SMatthew Garrett static void usb_wwan_setup_urbs(struct usb_serial *serial) 4780d456194SMatthew Garrett { 4790d456194SMatthew Garrett int i, j; 4800d456194SMatthew Garrett struct usb_serial_port *port; 4810d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 4820d456194SMatthew Garrett 4830d456194SMatthew Garrett dbg("%s", __func__); 4840d456194SMatthew Garrett 4850d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 4860d456194SMatthew Garrett port = serial->port[i]; 4870d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 4880d456194SMatthew Garrett 4890d456194SMatthew Garrett /* Do indat endpoints first */ 4900d456194SMatthew Garrett for (j = 0; j < N_IN_URB; ++j) { 4910d456194SMatthew Garrett portdata->in_urbs[j] = usb_wwan_setup_urb(serial, 4920d456194SMatthew Garrett port-> 4930d456194SMatthew Garrett bulk_in_endpointAddress, 4940d456194SMatthew Garrett USB_DIR_IN, 4950d456194SMatthew Garrett port, 4960d456194SMatthew Garrett portdata-> 4970d456194SMatthew Garrett in_buffer[j], 4980d456194SMatthew Garrett IN_BUFLEN, 4990d456194SMatthew Garrett usb_wwan_indat_callback); 5000d456194SMatthew Garrett } 5010d456194SMatthew Garrett 5020d456194SMatthew Garrett /* outdat endpoints */ 5030d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; ++j) { 5040d456194SMatthew Garrett portdata->out_urbs[j] = usb_wwan_setup_urb(serial, 5050d456194SMatthew Garrett port-> 5060d456194SMatthew Garrett bulk_out_endpointAddress, 5070d456194SMatthew Garrett USB_DIR_OUT, 5080d456194SMatthew Garrett port, 5090d456194SMatthew Garrett portdata-> 5100d456194SMatthew Garrett out_buffer 5110d456194SMatthew Garrett [j], 5120d456194SMatthew Garrett OUT_BUFLEN, 5130d456194SMatthew Garrett usb_wwan_outdat_callback); 5140d456194SMatthew Garrett } 5150d456194SMatthew Garrett } 5160d456194SMatthew Garrett } 5170d456194SMatthew Garrett 5180d456194SMatthew Garrett int usb_wwan_startup(struct usb_serial *serial) 5190d456194SMatthew Garrett { 5200d456194SMatthew Garrett int i, j, err; 5210d456194SMatthew Garrett struct usb_serial_port *port; 5220d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 5230d456194SMatthew Garrett u8 *buffer; 5240d456194SMatthew Garrett 5250d456194SMatthew Garrett dbg("%s", __func__); 5260d456194SMatthew Garrett 5270d456194SMatthew Garrett /* Now setup per port private data */ 5280d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 5290d456194SMatthew Garrett port = serial->port[i]; 5300d456194SMatthew Garrett portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 5310d456194SMatthew Garrett if (!portdata) { 5320d456194SMatthew Garrett dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.", 5330d456194SMatthew Garrett __func__, i); 5340d456194SMatthew Garrett return 1; 5350d456194SMatthew Garrett } 5360d456194SMatthew Garrett init_usb_anchor(&portdata->delayed); 5370d456194SMatthew Garrett 5380d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) { 5390d456194SMatthew Garrett buffer = (u8 *) __get_free_page(GFP_KERNEL); 5400d456194SMatthew Garrett if (!buffer) 5410d456194SMatthew Garrett goto bail_out_error; 5420d456194SMatthew Garrett portdata->in_buffer[j] = buffer; 5430d456194SMatthew Garrett } 5440d456194SMatthew Garrett 5450d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) { 5460d456194SMatthew Garrett buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); 5470d456194SMatthew Garrett if (!buffer) 5480d456194SMatthew Garrett goto bail_out_error2; 5490d456194SMatthew Garrett portdata->out_buffer[j] = buffer; 5500d456194SMatthew Garrett } 5510d456194SMatthew Garrett 5520d456194SMatthew Garrett usb_set_serial_port_data(port, portdata); 5530d456194SMatthew Garrett 5540d456194SMatthew Garrett if (!port->interrupt_in_urb) 5550d456194SMatthew Garrett continue; 5560d456194SMatthew Garrett err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 5570d456194SMatthew Garrett if (err) 5580d456194SMatthew Garrett dbg("%s: submit irq_in urb failed %d", __func__, err); 5590d456194SMatthew Garrett } 5600d456194SMatthew Garrett usb_wwan_setup_urbs(serial); 5610d456194SMatthew Garrett return 0; 5620d456194SMatthew Garrett 5630d456194SMatthew Garrett bail_out_error2: 5640d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) 5650d456194SMatthew Garrett kfree(portdata->out_buffer[j]); 5660d456194SMatthew Garrett bail_out_error: 5670d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) 5680d456194SMatthew Garrett if (portdata->in_buffer[j]) 5690d456194SMatthew Garrett free_page((unsigned long)portdata->in_buffer[j]); 5700d456194SMatthew Garrett kfree(portdata); 5710d456194SMatthew Garrett return 1; 5720d456194SMatthew Garrett } 5730d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_startup); 5740d456194SMatthew Garrett 5750d456194SMatthew Garrett static void stop_read_write_urbs(struct usb_serial *serial) 5760d456194SMatthew Garrett { 5770d456194SMatthew Garrett int i, j; 5780d456194SMatthew Garrett struct usb_serial_port *port; 5790d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 5800d456194SMatthew Garrett 5810d456194SMatthew Garrett /* Stop reading/writing urbs */ 5820d456194SMatthew Garrett for (i = 0; i < serial->num_ports; ++i) { 5830d456194SMatthew Garrett port = serial->port[i]; 5840d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 5850d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) 5860d456194SMatthew Garrett usb_kill_urb(portdata->in_urbs[j]); 5870d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) 5880d456194SMatthew Garrett usb_kill_urb(portdata->out_urbs[j]); 5890d456194SMatthew Garrett } 5900d456194SMatthew Garrett } 5910d456194SMatthew Garrett 5920d456194SMatthew Garrett void usb_wwan_disconnect(struct usb_serial *serial) 5930d456194SMatthew Garrett { 5940d456194SMatthew Garrett dbg("%s", __func__); 5950d456194SMatthew Garrett 5960d456194SMatthew Garrett stop_read_write_urbs(serial); 5970d456194SMatthew Garrett } 5980d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_disconnect); 5990d456194SMatthew Garrett 6000d456194SMatthew Garrett void usb_wwan_release(struct usb_serial *serial) 6010d456194SMatthew Garrett { 6020d456194SMatthew Garrett int i, j; 6030d456194SMatthew Garrett struct usb_serial_port *port; 6040d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6050d456194SMatthew Garrett 6060d456194SMatthew Garrett dbg("%s", __func__); 6070d456194SMatthew Garrett 6080d456194SMatthew Garrett /* Now free them */ 6090d456194SMatthew Garrett for (i = 0; i < serial->num_ports; ++i) { 6100d456194SMatthew Garrett port = serial->port[i]; 6110d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 6120d456194SMatthew Garrett 6130d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) { 6140d456194SMatthew Garrett usb_free_urb(portdata->in_urbs[j]); 6150d456194SMatthew Garrett free_page((unsigned long) 6160d456194SMatthew Garrett portdata->in_buffer[j]); 6170d456194SMatthew Garrett portdata->in_urbs[j] = NULL; 6180d456194SMatthew Garrett } 6190d456194SMatthew Garrett for (j = 0; j < N_OUT_URB; j++) { 6200d456194SMatthew Garrett usb_free_urb(portdata->out_urbs[j]); 6210d456194SMatthew Garrett kfree(portdata->out_buffer[j]); 6220d456194SMatthew Garrett portdata->out_urbs[j] = NULL; 6230d456194SMatthew Garrett } 6240d456194SMatthew Garrett } 6250d456194SMatthew Garrett 6260d456194SMatthew Garrett /* Now free per port private data */ 6270d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 6280d456194SMatthew Garrett port = serial->port[i]; 6290d456194SMatthew Garrett kfree(usb_get_serial_port_data(port)); 6300d456194SMatthew Garrett } 6310d456194SMatthew Garrett } 6320d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_release); 6330d456194SMatthew Garrett 6340d456194SMatthew Garrett #ifdef CONFIG_PM 6350d456194SMatthew Garrett int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message) 6360d456194SMatthew Garrett { 6370d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = serial->private; 6380d456194SMatthew Garrett int b; 6390d456194SMatthew Garrett 6400d456194SMatthew Garrett dbg("%s entered", __func__); 6410d456194SMatthew Garrett 6420d456194SMatthew Garrett if (message.event & PM_EVENT_AUTO) { 6430d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 6440d456194SMatthew Garrett b = intfdata->in_flight; 6450d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 6460d456194SMatthew Garrett 6470d456194SMatthew Garrett if (b) 6480d456194SMatthew Garrett return -EBUSY; 6490d456194SMatthew Garrett } 6500d456194SMatthew Garrett 6510d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 6520d456194SMatthew Garrett intfdata->suspended = 1; 6530d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 6540d456194SMatthew Garrett stop_read_write_urbs(serial); 6550d456194SMatthew Garrett 6560d456194SMatthew Garrett return 0; 6570d456194SMatthew Garrett } 6580d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_suspend); 6590d456194SMatthew Garrett 6600d456194SMatthew Garrett static void play_delayed(struct usb_serial_port *port) 6610d456194SMatthew Garrett { 6620d456194SMatthew Garrett struct usb_wwan_intf_private *data; 6630d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6640d456194SMatthew Garrett struct urb *urb; 6650d456194SMatthew Garrett int err; 6660d456194SMatthew Garrett 6670d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 6680d456194SMatthew Garrett data = port->serial->private; 6690d456194SMatthew Garrett while ((urb = usb_get_from_anchor(&portdata->delayed))) { 6700d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 6710d456194SMatthew Garrett if (!err) 6720d456194SMatthew Garrett data->in_flight++; 6730d456194SMatthew Garrett } 6740d456194SMatthew Garrett } 6750d456194SMatthew Garrett 6760d456194SMatthew Garrett int usb_wwan_resume(struct usb_serial *serial) 6770d456194SMatthew Garrett { 6780d456194SMatthew Garrett int i, j; 6790d456194SMatthew Garrett struct usb_serial_port *port; 6800d456194SMatthew Garrett struct usb_wwan_intf_private *intfdata = serial->private; 6810d456194SMatthew Garrett struct usb_wwan_port_private *portdata; 6820d456194SMatthew Garrett struct urb *urb; 6830d456194SMatthew Garrett int err = 0; 6840d456194SMatthew Garrett 6850d456194SMatthew Garrett dbg("%s entered", __func__); 6860d456194SMatthew Garrett /* get the interrupt URBs resubmitted unconditionally */ 6870d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 6880d456194SMatthew Garrett port = serial->port[i]; 6890d456194SMatthew Garrett if (!port->interrupt_in_urb) { 6900d456194SMatthew Garrett dbg("%s: No interrupt URB for port %d", __func__, i); 6910d456194SMatthew Garrett continue; 6920d456194SMatthew Garrett } 6930d456194SMatthew Garrett err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 6940d456194SMatthew Garrett dbg("Submitted interrupt URB for port %d (result %d)", i, err); 6950d456194SMatthew Garrett if (err < 0) { 6960d456194SMatthew Garrett err("%s: Error %d for interrupt URB of port%d", 6970d456194SMatthew Garrett __func__, err, i); 6980d456194SMatthew Garrett goto err_out; 6990d456194SMatthew Garrett } 7000d456194SMatthew Garrett } 7010d456194SMatthew Garrett 7020d456194SMatthew Garrett for (i = 0; i < serial->num_ports; i++) { 7030d456194SMatthew Garrett /* walk all ports */ 7040d456194SMatthew Garrett port = serial->port[i]; 7050d456194SMatthew Garrett portdata = usb_get_serial_port_data(port); 7060d456194SMatthew Garrett 7070d456194SMatthew Garrett /* skip closed ports */ 7080d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 7090d456194SMatthew Garrett if (!portdata->opened) { 7100d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 7110d456194SMatthew Garrett continue; 7120d456194SMatthew Garrett } 7130d456194SMatthew Garrett 7140d456194SMatthew Garrett for (j = 0; j < N_IN_URB; j++) { 7150d456194SMatthew Garrett urb = portdata->in_urbs[j]; 7160d456194SMatthew Garrett err = usb_submit_urb(urb, GFP_ATOMIC); 7170d456194SMatthew Garrett if (err < 0) { 7180d456194SMatthew Garrett err("%s: Error %d for bulk URB %d", 7190d456194SMatthew Garrett __func__, err, i); 7200d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 7210d456194SMatthew Garrett goto err_out; 7220d456194SMatthew Garrett } 7230d456194SMatthew Garrett } 7240d456194SMatthew Garrett play_delayed(port); 7250d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 7260d456194SMatthew Garrett } 7270d456194SMatthew Garrett spin_lock_irq(&intfdata->susp_lock); 7280d456194SMatthew Garrett intfdata->suspended = 0; 7290d456194SMatthew Garrett spin_unlock_irq(&intfdata->susp_lock); 7300d456194SMatthew Garrett err_out: 7310d456194SMatthew Garrett return err; 7320d456194SMatthew Garrett } 7330d456194SMatthew Garrett EXPORT_SYMBOL(usb_wwan_resume); 7340d456194SMatthew Garrett #endif 7350d456194SMatthew Garrett 7360d456194SMatthew Garrett MODULE_AUTHOR(DRIVER_AUTHOR); 7370d456194SMatthew Garrett MODULE_DESCRIPTION(DRIVER_DESC); 7380d456194SMatthew Garrett MODULE_VERSION(DRIVER_VERSION); 7390d456194SMatthew Garrett MODULE_LICENSE("GPL"); 7400d456194SMatthew Garrett 7410d456194SMatthew Garrett module_param(debug, bool, S_IRUGO | S_IWUSR); 7420d456194SMatthew Garrett MODULE_PARM_DESC(debug, "Debug messages"); 743