Merge tag 'v4.4-rc5'
[firefly-linux-kernel-4.4.55.git] / drivers / usb / core / devio.c
index c88c4fb9459dfbc94119e8a0590a924fa5868f0c..38ae877c46e3124eb07c2a46ddd33bc66ac54d93 100644 (file)
 #include <linux/security.h>
 #include <linux/user_namespace.h>
 #include <linux/scatterlist.h>
-#include <asm/uaccess.h>
+#include <linux/uaccess.h>
 #include <asm/byteorder.h>
 #include <linux/moduleparam.h>
 
 #include "usb.h"
 
 #define USB_MAXBUS                     64
-#define USB_DEVICE_MAX                 USB_MAXBUS * 128
+#define USB_DEVICE_MAX                 (USB_MAXBUS * 128)
 #define USB_SG_SIZE                    16384 /* split-size for large txs */
 
 /* Mutual exclusion for removal, open, and release */
 DEFINE_MUTEX(usbfs_mutex);
 
-struct dev_state {
+struct usb_dev_state {
        struct list_head list;      /* state list */
        struct usb_device *dev;
        struct file *file;
@@ -81,7 +81,7 @@ struct dev_state {
 
 struct async {
        struct list_head asynclist;
-       struct dev_state *ps;
+       struct usb_dev_state *ps;
        struct pid *pid;
        const struct cred *cred;
        unsigned int signr;
@@ -103,7 +103,7 @@ MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
 #define snoop(dev, format, arg...)                             \
        do {                                                    \
                if (usbfs_snoop)                                \
-                       dev_info(dev , format , ## arg);        \
+                       dev_info(dev, format, ## arg);          \
        } while (0)
 
 enum snoop_when {
@@ -118,7 +118,7 @@ module_param(usbfs_memory_mb, uint, 0644);
 MODULE_PARM_DESC(usbfs_memory_mb,
                "maximum MB allowed for usbfs buffers (0 = no limit)");
 
-/* Hard limit, necessary to avoid aithmetic overflow */
+/* Hard limit, necessary to avoid arithmetic overflow */
 #define USBFS_XFER_MAX         (UINT_MAX / 2 - 1000000)
 
 static atomic_t usbfs_memory_usage;    /* Total memory currently allocated */
@@ -151,7 +151,7 @@ static void usbfs_decrease_memory_usage(unsigned amount)
        atomic_sub(amount, &usbfs_memory_usage);
 }
 
-static int connected(struct dev_state *ps)
+static int connected(struct usb_dev_state *ps)
 {
        return (!list_empty(&ps->list) &&
                        ps->dev->state != USB_STATE_NOTATTACHED);
@@ -184,7 +184,7 @@ static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
                           loff_t *ppos)
 {
-       struct dev_state *ps = file->private_data;
+       struct usb_dev_state *ps = file->private_data;
        struct usb_device *dev = ps->dev;
        ssize_t ret = 0;
        unsigned len;
@@ -307,7 +307,7 @@ static void free_async(struct async *as)
 
 static void async_newpending(struct async *as)
 {
-       struct dev_state *ps = as->ps;
+       struct usb_dev_state *ps = as->ps;
        unsigned long flags;
 
        spin_lock_irqsave(&ps->lock, flags);
@@ -317,7 +317,7 @@ static void async_newpending(struct async *as)
 
 static void async_removepending(struct async *as)
 {
-       struct dev_state *ps = as->ps;
+       struct usb_dev_state *ps = as->ps;
        unsigned long flags;
 
        spin_lock_irqsave(&ps->lock, flags);
@@ -325,7 +325,7 @@ static void async_removepending(struct async *as)
        spin_unlock_irqrestore(&ps->lock, flags);
 }
 
-static struct async *async_getcompleted(struct dev_state *ps)
+static struct async *async_getcompleted(struct usb_dev_state *ps)
 {
        unsigned long flags;
        struct async *as = NULL;
@@ -340,7 +340,7 @@ static struct async *async_getcompleted(struct dev_state *ps)
        return as;
 }
 
-static struct async *async_getpending(struct dev_state *ps,
+static struct async *async_getpending(struct usb_dev_state *ps,
                                             void __user *userurb)
 {
        struct async *as;
@@ -448,7 +448,7 @@ static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
 #define AS_CONTINUATION        1
 #define AS_UNLINK      2
 
-static void cancel_bulk_urbs(struct dev_state *ps, unsigned bulk_addr)
+static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
 __releases(ps->lock)
 __acquires(ps->lock)
 {
@@ -489,7 +489,7 @@ __acquires(ps->lock)
 static void async_completed(struct urb *urb)
 {
        struct async *as = urb->context;
-       struct dev_state *ps = as->ps;
+       struct usb_dev_state *ps = as->ps;
        struct siginfo sinfo;
        struct pid *pid = NULL;
        u32 secid = 0;
@@ -501,6 +501,7 @@ static void async_completed(struct urb *urb)
        as->status = urb->status;
        signr = as->signr;
        if (signr) {
+               memset(&sinfo, 0, sizeof(sinfo));
                sinfo.si_signo = as->signr;
                sinfo.si_errno = as->status;
                sinfo.si_code = SI_ASYNCIO;
@@ -512,7 +513,7 @@ static void async_completed(struct urb *urb)
        snoop(&urb->dev->dev, "urb complete\n");
        snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
                        as->status, COMPLETE, NULL, 0);
-       if ((urb->transfer_flags & URB_DIR_MASK) == USB_DIR_IN)
+       if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
                snoop_urb_data(urb, urb->actual_length);
 
        if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
@@ -529,7 +530,7 @@ static void async_completed(struct urb *urb)
        wake_up(&ps->wait);
 }
 
-static void destroy_async(struct dev_state *ps, struct list_head *list)
+static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
 {
        struct urb *urb;
        struct async *as;
@@ -551,7 +552,7 @@ static void destroy_async(struct dev_state *ps, struct list_head *list)
        spin_unlock_irqrestore(&ps->lock, flags);
 }
 
-static void destroy_async_on_interface(struct dev_state *ps,
+static void destroy_async_on_interface(struct usb_dev_state *ps,
                                       unsigned int ifnum)
 {
        struct list_head *p, *q, hitlist;
@@ -566,7 +567,7 @@ static void destroy_async_on_interface(struct dev_state *ps,
        destroy_async(ps, &hitlist);
 }
 
-static void destroy_all_async(struct dev_state *ps)
+static void destroy_all_async(struct usb_dev_state *ps)
 {
        destroy_async(ps, &ps->async_pending);
 }
@@ -585,7 +586,7 @@ static int driver_probe(struct usb_interface *intf,
 
 static void driver_disconnect(struct usb_interface *intf)
 {
-       struct dev_state *ps = usb_get_intfdata(intf);
+       struct usb_dev_state *ps = usb_get_intfdata(intf);
        unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
 
        if (!ps)
@@ -628,7 +629,7 @@ struct usb_driver usbfs_driver = {
        .resume =       driver_resume,
 };
 
-static int claimintf(struct dev_state *ps, unsigned int ifnum)
+static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
 {
        struct usb_device *dev = ps->dev;
        struct usb_interface *intf;
@@ -650,7 +651,7 @@ static int claimintf(struct dev_state *ps, unsigned int ifnum)
        return err;
 }
 
-static int releaseintf(struct dev_state *ps, unsigned int ifnum)
+static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
 {
        struct usb_device *dev;
        struct usb_interface *intf;
@@ -670,7 +671,7 @@ static int releaseintf(struct dev_state *ps, unsigned int ifnum)
        return err;
 }
 
-static int checkintf(struct dev_state *ps, unsigned int ifnum)
+static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
 {
        if (ps->dev->state != USB_STATE_CONFIGURED)
                return -EHOSTUNREACH;
@@ -710,7 +711,7 @@ static int findintfep(struct usb_device *dev, unsigned int ep)
        return -ENOENT;
 }
 
-static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
+static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
                           unsigned int request, unsigned int index)
 {
        int ret = 0;
@@ -725,15 +726,15 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
 
        /*
         * check for the special corner case 'get_device_id' in the printer
-        * class specification, where wIndex is (interface << 8 | altsetting)
-        * instead of just interface
+        * class specification, which we always want to allow as it is used
+        * to query things like ink level, etc.
         */
        if (requesttype == 0xa1 && request == 0) {
                alt_setting = usb_find_alt_setting(ps->dev->actconfig,
                                                   index >> 8, index & 0xff);
                if (alt_setting
                 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
-                       index >>= 8;
+                       return 0;
        }
 
        index &= 0xff;
@@ -742,6 +743,22 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
                if ((index & ~USB_DIR_IN) == 0)
                        return 0;
                ret = findintfep(ps->dev, index);
+               if (ret < 0) {
+                       /*
+                        * Some not fully compliant Win apps seem to get
+                        * index wrong and have the endpoint number here
+                        * rather than the endpoint address (with the
+                        * correct direction). Win does let this through,
+                        * so we'll not reject it here but leave it to
+                        * the device to not break KVM. But we warn.
+                        */
+                       ret = findintfep(ps->dev, index ^ 0x80);
+                       if (ret >= 0)
+                               dev_info(&ps->dev->dev,
+                                       "%s: process %i (%s) requesting ep %02x but needs %02x\n",
+                                       __func__, task_pid_nr(current),
+                                       current->comm, index, index ^ 0x80);
+               }
                if (ret >= 0)
                        ret = checkintf(ps, ret);
                break;
@@ -753,6 +770,88 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype,
        return ret;
 }
 
+static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
+                                                    unsigned char ep)
+{
+       if (ep & USB_ENDPOINT_DIR_MASK)
+               return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
+       else
+               return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
+}
+
+static int parse_usbdevfs_streams(struct usb_dev_state *ps,
+                                 struct usbdevfs_streams __user *streams,
+                                 unsigned int *num_streams_ret,
+                                 unsigned int *num_eps_ret,
+                                 struct usb_host_endpoint ***eps_ret,
+                                 struct usb_interface **intf_ret)
+{
+       unsigned int i, num_streams, num_eps;
+       struct usb_host_endpoint **eps;
+       struct usb_interface *intf = NULL;
+       unsigned char ep;
+       int ifnum, ret;
+
+       if (get_user(num_streams, &streams->num_streams) ||
+           get_user(num_eps, &streams->num_eps))
+               return -EFAULT;
+
+       if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
+               return -EINVAL;
+
+       /* The XHCI controller allows max 2 ^ 16 streams */
+       if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
+               return -EINVAL;
+
+       eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
+       if (!eps)
+               return -ENOMEM;
+
+       for (i = 0; i < num_eps; i++) {
+               if (get_user(ep, &streams->eps[i])) {
+                       ret = -EFAULT;
+                       goto error;
+               }
+               eps[i] = ep_to_host_endpoint(ps->dev, ep);
+               if (!eps[i]) {
+                       ret = -EINVAL;
+                       goto error;
+               }
+
+               /* usb_alloc/free_streams operate on an usb_interface */
+               ifnum = findintfep(ps->dev, ep);
+               if (ifnum < 0) {
+                       ret = ifnum;
+                       goto error;
+               }
+
+               if (i == 0) {
+                       ret = checkintf(ps, ifnum);
+                       if (ret < 0)
+                               goto error;
+                       intf = usb_ifnum_to_if(ps->dev, ifnum);
+               } else {
+                       /* Verify all eps belong to the same interface */
+                       if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
+                               ret = -EINVAL;
+                               goto error;
+                       }
+               }
+       }
+
+       if (num_streams_ret)
+               *num_streams_ret = num_streams;
+       *num_eps_ret = num_eps;
+       *eps_ret = eps;
+       *intf_ret = intf;
+
+       return 0;
+
+error:
+       kfree(eps);
+       return ret;
+}
+
 static int match_devt(struct device *dev, void *data)
 {
        return dev->devt == (dev_t) (unsigned long) data;
@@ -775,11 +874,11 @@ static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
 static int usbdev_open(struct inode *inode, struct file *file)
 {
        struct usb_device *dev = NULL;
-       struct dev_state *ps;
+       struct usb_dev_state *ps;
        int ret;
 
        ret = -ENOMEM;
-       ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL);
+       ps = kmalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
        if (!ps)
                goto out_free_ps;
 
@@ -836,7 +935,7 @@ static int usbdev_open(struct inode *inode, struct file *file)
 
 static int usbdev_release(struct inode *inode, struct file *file)
 {
-       struct dev_state *ps = file->private_data;
+       struct usb_dev_state *ps = file->private_data;
        struct usb_device *dev = ps->dev;
        unsigned int ifnum;
        struct async *as;
@@ -867,7 +966,7 @@ static int usbdev_release(struct inode *inode, struct file *file)
        return 0;
 }
 
-static int proc_control(struct dev_state *ps, void __user *arg)
+static int proc_control(struct usb_dev_state *ps, void __user *arg)
 {
        struct usb_device *dev = ps->dev;
        struct usbdevfs_ctrltransfer ctrl;
@@ -898,10 +997,8 @@ static int proc_control(struct dev_state *ps, void __user *arg)
        snoop(&dev->dev, "control urb: bRequestType=%02x "
                "bRequest=%02x wValue=%04x "
                "wIndex=%04x wLength=%04x\n",
-               ctrl.bRequestType, ctrl.bRequest,
-               __le16_to_cpup(&ctrl.wValue),
-               __le16_to_cpup(&ctrl.wIndex),
-               __le16_to_cpup(&ctrl.wLength));
+               ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
+               ctrl.wIndex, ctrl.wLength);
        if (ctrl.bRequestType & 0x80) {
                if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
                                               ctrl.wLength)) {
@@ -956,7 +1053,7 @@ static int proc_control(struct dev_state *ps, void __user *arg)
        return ret;
 }
 
-static int proc_bulk(struct dev_state *ps, void __user *arg)
+static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
 {
        struct usb_device *dev = ps->dev;
        struct usbdevfs_bulktransfer bulk;
@@ -985,7 +1082,8 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
        ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
        if (ret)
                return ret;
-       if (!(tbuf = kmalloc(len1, GFP_KERNEL))) {
+       tbuf = kmalloc(len1, GFP_KERNEL);
+       if (!tbuf) {
                ret = -ENOMEM;
                goto done;
        }
@@ -1029,7 +1127,21 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
        return ret;
 }
 
-static int proc_resetep(struct dev_state *ps, void __user *arg)
+static void check_reset_of_active_ep(struct usb_device *udev,
+               unsigned int epnum, char *ioctl_name)
+{
+       struct usb_host_endpoint **eps;
+       struct usb_host_endpoint *ep;
+
+       eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
+       ep = eps[epnum & 0x0f];
+       if (ep && !list_empty(&ep->urb_list))
+               dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
+                               task_pid_nr(current), current->comm,
+                               ioctl_name, epnum);
+}
+
+static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned int ep;
        int ret;
@@ -1042,11 +1154,12 @@ static int proc_resetep(struct dev_state *ps, void __user *arg)
        ret = checkintf(ps, ret);
        if (ret)
                return ret;
+       check_reset_of_active_ep(ps->dev, ep, "RESETEP");
        usb_reset_endpoint(ps->dev, ep);
        return 0;
 }
 
-static int proc_clearhalt(struct dev_state *ps, void __user *arg)
+static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned int ep;
        int pipe;
@@ -1060,6 +1173,7 @@ static int proc_clearhalt(struct dev_state *ps, void __user *arg)
        ret = checkintf(ps, ret);
        if (ret)
                return ret;
+       check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
        if (ep & USB_DIR_IN)
                pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
        else
@@ -1068,7 +1182,7 @@ static int proc_clearhalt(struct dev_state *ps, void __user *arg)
        return usb_clear_halt(ps->dev, pipe);
 }
 
-static int proc_getdriver(struct dev_state *ps, void __user *arg)
+static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_getdriver gd;
        struct usb_interface *intf;
@@ -1087,7 +1201,7 @@ static int proc_getdriver(struct dev_state *ps, void __user *arg)
        return ret;
 }
 
-static int proc_connectinfo(struct dev_state *ps, void __user *arg)
+static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_connectinfo ci = {
                .devnum = ps->dev->devnum,
@@ -1099,25 +1213,29 @@ static int proc_connectinfo(struct dev_state *ps, void __user *arg)
        return 0;
 }
 
-static int proc_resetdevice(struct dev_state *ps)
+static int proc_resetdevice(struct usb_dev_state *ps)
 {
        return usb_reset_device(ps->dev);
 }
 
-static int proc_setintf(struct dev_state *ps, void __user *arg)
+static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_setinterface setintf;
        int ret;
 
        if (copy_from_user(&setintf, arg, sizeof(setintf)))
                return -EFAULT;
-       if ((ret = checkintf(ps, setintf.interface)))
+       ret = checkintf(ps, setintf.interface);
+       if (ret)
                return ret;
+
+       destroy_async_on_interface(ps, setintf.interface);
+
        return usb_set_interface(ps->dev, setintf.interface,
                        setintf.altsetting);
 }
 
-static int proc_setconfig(struct dev_state *ps, void __user *arg)
+static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
 {
        int u;
        int status = 0;
@@ -1165,7 +1283,7 @@ static int proc_setconfig(struct dev_state *ps, void __user *arg)
        return status;
 }
 
-static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
+static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
                        struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
                        void __user *arg)
 {
@@ -1175,6 +1293,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
        struct usb_ctrlrequest *dr = NULL;
        unsigned int u, totlen, isofrmlen;
        int i, ret, is_in, num_sgs = 0, ifnum = -1;
+       int number_of_packets = 0;
+       unsigned int stream_id = 0;
        void *buf;
 
        if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
@@ -1195,18 +1315,13 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                if (ret)
                        return ret;
        }
-       if ((uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0) {
-               is_in = 1;
-               ep = ps->dev->ep_in[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
-       } else {
-               is_in = 0;
-               ep = ps->dev->ep_out[uurb->endpoint & USB_ENDPOINT_NUMBER_MASK];
-       }
+       ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
        if (!ep)
                return -ENOENT;
+       is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
 
        u = 0;
-       switch(uurb->type) {
+       switch (uurb->type) {
        case USBDEVFS_URB_TYPE_CONTROL:
                if (!usb_endpoint_xfer_control(&ep->desc))
                        return -EINVAL;
@@ -1228,7 +1343,6 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                                      le16_to_cpup(&dr->wIndex));
                if (ret)
                        goto error;
-               uurb->number_of_packets = 0;
                uurb->buffer_length = le16_to_cpup(&dr->wLength);
                uurb->buffer += 8;
                if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
@@ -1258,17 +1372,17 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
                        goto interrupt_urb;
                }
-               uurb->number_of_packets = 0;
                num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
                if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
                        num_sgs = 0;
+               if (ep->streams)
+                       stream_id = uurb->stream_id;
                break;
 
        case USBDEVFS_URB_TYPE_INTERRUPT:
                if (!usb_endpoint_xfer_int(&ep->desc))
                        return -EINVAL;
  interrupt_urb:
-               uurb->number_of_packets = 0;
                break;
 
        case USBDEVFS_URB_TYPE_ISO:
@@ -1278,15 +1392,17 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        return -EINVAL;
                if (!usb_endpoint_xfer_isoc(&ep->desc))
                        return -EINVAL;
+               number_of_packets = uurb->number_of_packets;
                isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
-                                  uurb->number_of_packets;
-               if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
+                                  number_of_packets;
+               isopkt = kmalloc(isofrmlen, GFP_KERNEL);
+               if (!isopkt)
                        return -ENOMEM;
                if (copy_from_user(isopkt, iso_frame_desc, isofrmlen)) {
                        ret = -EFAULT;
                        goto error;
                }
-               for (totlen = u = 0; u < uurb->number_of_packets; u++) {
+               for (totlen = u = 0; u < number_of_packets; u++) {
                        /*
                         * arbitrary limit need for USB 3.0
                         * bMaxBurst (0~15 allowed, 1~16 packets)
@@ -1317,7 +1433,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                ret = -EFAULT;
                goto error;
        }
-       as = alloc_async(uurb->number_of_packets);
+       as = alloc_async(number_of_packets);
        if (!as) {
                ret = -ENOMEM;
                goto error;
@@ -1397,7 +1513,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
        u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
        if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
                u |= URB_ISO_ASAP;
-       if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK)
+       if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
                u |= URB_SHORT_NOT_OK;
        if (uurb->flags & USBDEVFS_URB_NO_FSBR)
                u |= URB_NO_FSBR;
@@ -1411,7 +1527,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
        as->urb->setup_packet = (unsigned char *)dr;
        dr = NULL;
        as->urb->start_frame = uurb->start_frame;
-       as->urb->number_of_packets = uurb->number_of_packets;
+       as->urb->number_of_packets = number_of_packets;
+       as->urb->stream_id = stream_id;
        if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
                        ps->dev->speed == USB_SPEED_HIGH)
                as->urb->interval = 1 << min(15, ep->desc.bInterval - 1);
@@ -1419,7 +1536,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                as->urb->interval = ep->desc.bInterval;
        as->urb->context = as;
        as->urb->complete = async_completed;
-       for (totlen = u = 0; u < uurb->number_of_packets; u++) {
+       for (totlen = u = 0; u < number_of_packets; u++) {
                as->urb->iso_frame_desc[u].offset = totlen;
                as->urb->iso_frame_desc[u].length = isopkt[u].length;
                totlen += isopkt[u].length;
@@ -1494,7 +1611,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
        return ret;
 }
 
-static int proc_submiturb(struct dev_state *ps, void __user *arg)
+static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_urb uurb;
 
@@ -1506,7 +1623,7 @@ static int proc_submiturb(struct dev_state *ps, void __user *arg)
                        arg);
 }
 
-static int proc_unlinkurb(struct dev_state *ps, void __user *arg)
+static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
 {
        struct urb *urb;
        struct async *as;
@@ -1566,7 +1683,7 @@ err_out:
        return -EFAULT;
 }
 
-static struct async *reap_as(struct dev_state *ps)
+static struct async *reap_as(struct usb_dev_state *ps)
 {
        DECLARE_WAITQUEUE(wait, current);
        struct async *as = NULL;
@@ -1576,7 +1693,7 @@ static struct async *reap_as(struct dev_state *ps)
        for (;;) {
                __set_current_state(TASK_INTERRUPTIBLE);
                as = async_getcompleted(ps);
-               if (as)
+               if (as || !connected(ps))
                        break;
                if (signal_pending(current))
                        break;
@@ -1589,7 +1706,7 @@ static struct async *reap_as(struct dev_state *ps)
        return as;
 }
 
-static int proc_reapurb(struct dev_state *ps, void __user *arg)
+static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
 {
        struct async *as = reap_as(ps);
        if (as) {
@@ -1599,55 +1716,56 @@ static int proc_reapurb(struct dev_state *ps, void __user *arg)
        }
        if (signal_pending(current))
                return -EINTR;
-       return -EIO;
+       return -ENODEV;
 }
 
-static int proc_reapurbnonblock(struct dev_state *ps, void __user *arg)
+static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
 {
        int retval;
        struct async *as;
 
        as = async_getcompleted(ps);
-       retval = -EAGAIN;
        if (as) {
                retval = processcompl(as, (void __user * __user *)arg);
                free_async(as);
+       } else {
+               retval = (connected(ps) ? -EAGAIN : -ENODEV);
        }
        return retval;
 }
 
 #ifdef CONFIG_COMPAT
-static int proc_control_compat(struct dev_state *ps,
+static int proc_control_compat(struct usb_dev_state *ps,
                                struct usbdevfs_ctrltransfer32 __user *p32)
 {
-        struct usbdevfs_ctrltransfer __user *p;
-        __u32 udata;
-        p = compat_alloc_user_space(sizeof(*p));
-        if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
-            get_user(udata, &p32->data) ||
+       struct usbdevfs_ctrltransfer __user *p;
+       __u32 udata;
+       p = compat_alloc_user_space(sizeof(*p));
+       if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
+           get_user(udata, &p32->data) ||
            put_user(compat_ptr(udata), &p->data))
                return -EFAULT;
-        return proc_control(ps, p);
+       return proc_control(ps, p);
 }
 
-static int proc_bulk_compat(struct dev_state *ps,
+static int proc_bulk_compat(struct usb_dev_state *ps,
                        struct usbdevfs_bulktransfer32 __user *p32)
 {
-        struct usbdevfs_bulktransfer __user *p;
-        compat_uint_t n;
-        compat_caddr_t addr;
+       struct usbdevfs_bulktransfer __user *p;
+       compat_uint_t n;
+       compat_caddr_t addr;
 
-        p = compat_alloc_user_space(sizeof(*p));
+       p = compat_alloc_user_space(sizeof(*p));
 
-        if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
-            get_user(n, &p32->len) || put_user(n, &p->len) ||
-            get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
-            get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
-                return -EFAULT;
+       if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
+           get_user(n, &p32->len) || put_user(n, &p->len) ||
+           get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
+           get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
+               return -EFAULT;
 
-        return proc_bulk(ps, p);
+       return proc_bulk(ps, p);
 }
-static int proc_disconnectsignal_compat(struct dev_state *ps, void __user *arg)
+static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_disconnectsignal32 ds;
 
@@ -1685,7 +1803,7 @@ static int get_urb32(struct usbdevfs_urb *kurb,
        return 0;
 }
 
-static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
+static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_urb uurb;
 
@@ -1731,7 +1849,7 @@ static int processcompl_compat(struct async *as, void __user * __user *arg)
        return 0;
 }
 
-static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
+static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
 {
        struct async *as = reap_as(ps);
        if (as) {
@@ -1741,19 +1859,20 @@ static int proc_reapurb_compat(struct dev_state *ps, void __user *arg)
        }
        if (signal_pending(current))
                return -EINTR;
-       return -EIO;
+       return -ENODEV;
 }
 
-static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
+static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
 {
        int retval;
        struct async *as;
 
-       retval = -EAGAIN;
        as = async_getcompleted(ps);
        if (as) {
                retval = processcompl_compat(as, (void __user * __user *)arg);
                free_async(as);
+       } else {
+               retval = (connected(ps) ? -EAGAIN : -ENODEV);
        }
        return retval;
 }
@@ -1761,7 +1880,7 @@ static int proc_reapurbnonblock_compat(struct dev_state *ps, void __user *arg)
 
 #endif
 
-static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
+static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_disconnectsignal ds;
 
@@ -1772,7 +1891,7 @@ static int proc_disconnectsignal(struct dev_state *ps, void __user *arg)
        return 0;
 }
 
-static int proc_claiminterface(struct dev_state *ps, void __user *arg)
+static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned int ifnum;
 
@@ -1781,20 +1900,21 @@ static int proc_claiminterface(struct dev_state *ps, void __user *arg)
        return claimintf(ps, ifnum);
 }
 
-static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
+static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned int ifnum;
        int ret;
 
        if (get_user(ifnum, (unsigned int __user *)arg))
                return -EFAULT;
-       if ((ret = releaseintf(ps, ifnum)) < 0)
+       ret = releaseintf(ps, ifnum);
+       if (ret < 0)
                return ret;
        destroy_async_on_interface (ps, ifnum);
        return 0;
 }
 
-static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
+static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
 {
        int                     size;
        void                    *buf = NULL;
@@ -1803,8 +1923,10 @@ static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
        struct usb_driver       *driver = NULL;
 
        /* alloc buffer */
-       if ((size = _IOC_SIZE(ctl->ioctl_code)) > 0) {
-               if ((buf = kmalloc(size, GFP_KERNEL)) == NULL)
+       size = _IOC_SIZE(ctl->ioctl_code);
+       if (size > 0) {
+               buf = kmalloc(size, GFP_KERNEL);
+               if (buf == NULL)
                        return -ENOMEM;
                if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
                        if (copy_from_user(buf, ctl->data, size)) {
@@ -1869,7 +1991,7 @@ static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
        return retval;
 }
 
-static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
+static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_ioctl   ctrl;
 
@@ -1879,7 +2001,7 @@ static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
 }
 
 #ifdef CONFIG_COMPAT
-static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
+static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
 {
        struct usbdevfs_ioctl32 __user *uioc;
        struct usbdevfs_ioctl ctrl;
@@ -1897,7 +2019,7 @@ static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
 }
 #endif
 
-static int proc_claim_port(struct dev_state *ps, void __user *arg)
+static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned portnum;
        int rc;
@@ -1911,7 +2033,7 @@ static int proc_claim_port(struct dev_state *ps, void __user *arg)
        return rc;
 }
 
-static int proc_release_port(struct dev_state *ps, void __user *arg)
+static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
 {
        unsigned portnum;
 
@@ -1920,11 +2042,12 @@ static int proc_release_port(struct dev_state *ps, void __user *arg)
        return usb_hub_release_port(ps->dev, portnum, ps);
 }
 
-static int proc_get_capabilities(struct dev_state *ps, void __user *arg)
+static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
 {
        __u32 caps;
 
-       caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM;
+       caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
+                       USBDEVFS_CAP_REAP_AFTER_DISCONNECT;
        if (!ps->dev->bus->no_stop_on_short)
                caps |= USBDEVFS_CAP_BULK_CONTINUATION;
        if (ps->dev->bus->sg_tablesize)
@@ -1936,7 +2059,7 @@ static int proc_get_capabilities(struct dev_state *ps, void __user *arg)
        return 0;
 }
 
-static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
+static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
 {
        struct usbdevfs_disconnect_claim dc;
        struct usb_interface *intf;
@@ -1968,6 +2091,45 @@ static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
        return claimintf(ps, dc.interface);
 }
 
+static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
+{
+       unsigned num_streams, num_eps;
+       struct usb_host_endpoint **eps;
+       struct usb_interface *intf;
+       int r;
+
+       r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
+                                  &eps, &intf);
+       if (r)
+               return r;
+
+       destroy_async_on_interface(ps,
+                                  intf->altsetting[0].desc.bInterfaceNumber);
+
+       r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
+       kfree(eps);
+       return r;
+}
+
+static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
+{
+       unsigned num_eps;
+       struct usb_host_endpoint **eps;
+       struct usb_interface *intf;
+       int r;
+
+       r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
+       if (r)
+               return r;
+
+       destroy_async_on_interface(ps,
+                                  intf->altsetting[0].desc.bInterfaceNumber);
+
+       r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
+       kfree(eps);
+       return r;
+}
+
 /*
  * NOTE:  All requests here that have interface numbers as parameters
  * are assuming that somehow the configuration has been prevented from
@@ -1976,7 +2138,7 @@ static int proc_disconnect_claim(struct dev_state *ps, void __user *arg)
 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
                                void __user *p)
 {
-       struct dev_state *ps = file->private_data;
+       struct usb_dev_state *ps = file->private_data;
        struct inode *inode = file_inode(file);
        struct usb_device *dev = ps->dev;
        int ret = -ENOTTY;
@@ -1985,6 +2147,32 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
                return -EPERM;
 
        usb_lock_device(dev);
+
+       /* Reap operations are allowed even after disconnection */
+       switch (cmd) {
+       case USBDEVFS_REAPURB:
+               snoop(&dev->dev, "%s: REAPURB\n", __func__);
+               ret = proc_reapurb(ps, p);
+               goto done;
+
+       case USBDEVFS_REAPURBNDELAY:
+               snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
+               ret = proc_reapurbnonblock(ps, p);
+               goto done;
+
+#ifdef CONFIG_COMPAT
+       case USBDEVFS_REAPURB32:
+               snoop(&dev->dev, "%s: REAPURB32\n", __func__);
+               ret = proc_reapurb_compat(ps, p);
+               goto done;
+
+       case USBDEVFS_REAPURBNDELAY32:
+               snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
+               ret = proc_reapurbnonblock_compat(ps, p);
+               goto done;
+#endif
+       }
+
        if (!connected(ps)) {
                usb_unlock_device(dev);
                return -ENODEV;
@@ -2078,16 +2266,6 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
                        inode->i_mtime = CURRENT_TIME;
                break;
 
-       case USBDEVFS_REAPURB32:
-               snoop(&dev->dev, "%s: REAPURB32\n", __func__);
-               ret = proc_reapurb_compat(ps, p);
-               break;
-
-       case USBDEVFS_REAPURBNDELAY32:
-               snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
-               ret = proc_reapurbnonblock_compat(ps, p);
-               break;
-
        case USBDEVFS_IOCTL32:
                snoop(&dev->dev, "%s: IOCTL32\n", __func__);
                ret = proc_ioctl_compat(ps, ptr_to_compat(p));
@@ -2099,16 +2277,6 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
                ret = proc_unlinkurb(ps, p);
                break;
 
-       case USBDEVFS_REAPURB:
-               snoop(&dev->dev, "%s: REAPURB\n", __func__);
-               ret = proc_reapurb(ps, p);
-               break;
-
-       case USBDEVFS_REAPURBNDELAY:
-               snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
-               ret = proc_reapurbnonblock(ps, p);
-               break;
-
        case USBDEVFS_DISCSIGNAL:
                snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
                ret = proc_disconnectsignal(ps, p);
@@ -2144,7 +2312,15 @@ static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
        case USBDEVFS_DISCONNECT_CLAIM:
                ret = proc_disconnect_claim(ps, p);
                break;
+       case USBDEVFS_ALLOC_STREAMS:
+               ret = proc_alloc_streams(ps, p);
+               break;
+       case USBDEVFS_FREE_STREAMS:
+               ret = proc_free_streams(ps, p);
+               break;
        }
+
+ done:
        usb_unlock_device(dev);
        if (ret >= 0)
                inode->i_atime = CURRENT_TIME;
@@ -2177,7 +2353,7 @@ static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
 static unsigned int usbdev_poll(struct file *file,
                                struct poll_table_struct *wait)
 {
-       struct dev_state *ps = file->private_data;
+       struct usb_dev_state *ps = file->private_data;
        unsigned int mask = 0;
 
        poll_wait(file, &ps->wait, wait);
@@ -2203,15 +2379,16 @@ const struct file_operations usbdev_file_operations = {
 
 static void usbdev_remove(struct usb_device *udev)
 {
-       struct dev_state *ps;
+       struct usb_dev_state *ps;
        struct siginfo sinfo;
 
        while (!list_empty(&udev->filelist)) {
-               ps = list_entry(udev->filelist.next, struct dev_state, list);
+               ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
                destroy_all_async(ps);
                wake_up_all(&ps->wait);
                list_del_init(&ps->list);
                if (ps->discsignr) {
+                       memset(&sinfo, 0, sizeof(sinfo));
                        sinfo.si_signo = ps->discsignr;
                        sinfo.si_errno = EPIPE;
                        sinfo.si_code = SI_ASYNCIO;
@@ -2236,7 +2413,7 @@ static int usbdev_notify(struct notifier_block *self,
 }
 
 static struct notifier_block usbdev_nb = {
-       .notifier_call =        usbdev_notify,
+       .notifier_call =        usbdev_notify,
 };
 
 static struct cdev usb_device_cdev;