fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / usb / core / devio.c
index 6bfab4b..4b3a6ab 100644 (file)
@@ -30,6 +30,8 @@
  *  Revision history
  *    22.12.1999   0.1   Initial release (split from proc_usb.c)
  *    04.01.2000   0.2   Turned into its own filesystem
+ *    30.09.2005   0.3   Fix user-triggerable oops in async URB delivery
+ *                      (CAN-2005-3055)
  */
 
 /*****************************************************************************/
@@ -43,6 +45,9 @@
 #include <linux/module.h>
 #include <linux/usb.h>
 #include <linux/usbdevice_fs.h>
+#include <linux/cdev.h>
+#include <linux/notifier.h>
+#include <linux/security.h>
 #include <asm/uaccess.h>
 #include <asm/byteorder.h>
 #include <linux/moduleparam.h>
 #include "hcd.h"       /* for usbcore internals */
 #include "usb.h"
 
+#define USB_MAXBUS                     64
+#define USB_DEVICE_MAX                 USB_MAXBUS * 128
+static struct class *usb_device_class;
+
+/* Mutual exclusion for removal, open, and release */
+DEFINE_MUTEX(usbfs_mutex);
+
 struct async {
        struct list_head asynclist;
        struct dev_state *ps;
-       struct task_struct *task;
+       struct pid *pid;
+       uid_t uid, euid;
        unsigned int signr;
        unsigned int ifnum;
        void __user *userbuffer;
        void __user *userurb;
        struct urb *urb;
+       u32 secid;
 };
 
 static int usbfs_snoop = 0;
@@ -71,12 +85,15 @@ MODULE_PARM_DESC (usbfs_snoop, "true to log all usbfs traffic");
                        dev_info( dev , format , ## arg);       \
        } while (0)
 
+#define USB_DEVICE_DEV         MKDEV(USB_DEVICE_MAJOR, 0)
+
 
 #define        MAX_USBFS_BUFFER_SIZE   16384
 
-static inline int connected (struct usb_device *dev)
+static inline int connected (struct dev_state *ps)
 {
-       return dev->state != USB_STATE_NOTATTACHED;
+       return (!list_empty(&ps->list) &&
+                       ps->dev->state != USB_STATE_NOTATTACHED);
 }
 
 static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
@@ -105,7 +122,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 = (struct dev_state *)file->private_data;
+       struct dev_state *ps = file->private_data;
        struct usb_device *dev = ps->dev;
        ssize_t ret = 0;
        unsigned len;
@@ -114,7 +131,7 @@ static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, l
 
        pos = *ppos;
        usb_lock_device(dev);
-       if (!connected(dev)) {
+       if (!connected(ps)) {
                ret = -ENODEV;
                goto err;
        } else if (pos < 0) {
@@ -123,26 +140,21 @@ static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes, l
        }
 
        if (pos < sizeof(struct usb_device_descriptor)) {
-               struct usb_device_descriptor *desc = kmalloc(sizeof(*desc), GFP_KERNEL);
-               if (!desc) {
-                       ret = -ENOMEM;
-                       goto err;
-               }
-               memcpy(desc, &dev->descriptor, sizeof(dev->descriptor));
-               le16_to_cpus(&desc->bcdUSB);
-               le16_to_cpus(&desc->idVendor);
-               le16_to_cpus(&desc->idProduct);
-               le16_to_cpus(&desc->bcdDevice);
+               struct usb_device_descriptor temp_desc ; /* 18 bytes - fits on the stack */
+
+               memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
+               le16_to_cpus(&temp_desc.bcdUSB);
+               le16_to_cpus(&temp_desc.idVendor);
+               le16_to_cpus(&temp_desc.idProduct);
+               le16_to_cpus(&temp_desc.bcdDevice);
 
                len = sizeof(struct usb_device_descriptor) - pos;
                if (len > nbytes)
                        len = nbytes;
-               if (copy_to_user(buf, ((char *)desc) + pos, len)) {
-                       kfree(desc);
+               if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
                        ret = -EFAULT;
                        goto err;
                }
-               kfree(desc);
 
                *ppos += len;
                buf += len;
@@ -199,10 +211,10 @@ err:
 static struct async *alloc_async(unsigned int numisoframes)
 {
         unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct usb_iso_packet_descriptor);
-        struct async *as = kmalloc(assize, GFP_KERNEL);
+        struct async *as = kzalloc(assize, GFP_KERNEL);
+
         if (!as)
                 return NULL;
-        memset(as, 0, assize);
        as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
        if (!as->urb) {
                kfree(as);
@@ -213,6 +225,7 @@ static struct async *alloc_async(unsigned int numisoframes)
 
 static void free_async(struct async *as)
 {
+       put_pid(as->pid);
        kfree(as->urb->transfer_buffer);
        kfree(as->urb->setup_packet);
        usb_free_urb(as->urb);
@@ -269,9 +282,31 @@ static inline struct async *async_getpending(struct dev_state *ps, void __user *
         return NULL;
 }
 
-static void async_completed(struct urb *urb, struct pt_regs *regs)
+static void snoop_urb(struct urb *urb, void __user *userurb)
+{
+       int j;
+       unsigned char *data = urb->transfer_buffer;
+
+       if (!usbfs_snoop)
+               return;
+
+       if (urb->pipe & USB_DIR_IN)
+               dev_info(&urb->dev->dev, "direction=IN\n");
+       else
+               dev_info(&urb->dev->dev, "direction=OUT\n");
+       dev_info(&urb->dev->dev, "userurb=%p\n", userurb);
+       dev_info(&urb->dev->dev, "transfer_buffer_length=%d\n",
+                urb->transfer_buffer_length);
+       dev_info(&urb->dev->dev, "actual_length=%d\n", urb->actual_length);
+       dev_info(&urb->dev->dev, "data: ");
+       for (j = 0; j < urb->transfer_buffer_length; ++j)
+               printk ("%02x ", data[j]);
+       printk("\n");
+}
+
+static void async_completed(struct urb *urb)
 {
-        struct async *as = (struct async *)urb->context;
+        struct async *as = urb->context;
         struct dev_state *ps = as->ps;
        struct siginfo sinfo;
 
@@ -283,9 +318,12 @@ static void async_completed(struct urb *urb, struct pt_regs *regs)
                sinfo.si_errno = as->urb->status;
                sinfo.si_code = SI_ASYNCIO;
                sinfo.si_addr = as->userurb;
-               send_sig_info(as->signr, &sinfo, as->task);
+               kill_pid_info_as_uid(as->signr, &sinfo, as->pid, as->uid,
+                                     as->euid, as->secid);
        }
-        wake_up(&ps->wait);
+       snoop(&urb->dev->dev, "urb complete\n");
+       snoop_urb(urb, as->userurb);
+       wake_up(&ps->wait);
 }
 
 static void destroy_async (struct dev_state *ps, struct list_head *list)
@@ -366,7 +404,6 @@ static void driver_disconnect(struct usb_interface *intf)
 }
 
 struct usb_driver usbfs_driver = {
-       .owner =        THIS_MODULE,
        .name =         "usbfs",
        .probe =        driver_probe,
        .disconnect =   driver_disconnect,
@@ -463,7 +500,8 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsig
 {
        int ret = 0;
 
-       if (ps->dev->state != USB_STATE_CONFIGURED)
+       if (ps->dev->state != USB_STATE_ADDRESS
+        && ps->dev->state != USB_STATE_CONFIGURED)
                return -EHOSTUNREACH;
        if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
                return 0;
@@ -482,30 +520,52 @@ static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsig
        return ret;
 }
 
+static struct usb_device *usbdev_lookup_minor(int minor)
+{
+       struct class_device *class_dev;
+       struct usb_device *dev = NULL;
+
+       down(&usb_device_class->sem);
+       list_for_each_entry(class_dev, &usb_device_class->children, node) {
+               if (class_dev->devt == MKDEV(USB_DEVICE_MAJOR, minor)) {
+                       dev = class_dev->class_data;
+                       break;
+               }
+       }
+       up(&usb_device_class->sem);
+
+       return dev;
+};
+
 /*
  * file operations
  */
 static int usbdev_open(struct inode *inode, struct file *file)
 {
-       struct usb_device *dev;
+       struct usb_device *dev = NULL;
        struct dev_state *ps;
        int ret;
 
-       /* 
-        * no locking necessary here, as chrdev_open has the kernel lock
-        * (still acquire the kernel lock for safety)
-        */
+       /* Protect against simultaneous removal or release */
+       mutex_lock(&usbfs_mutex);
+
        ret = -ENOMEM;
        if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
-               goto out_nolock;
+               goto out;
 
-       lock_kernel();
        ret = -ENOENT;
-       dev = usb_get_dev(inode->u.generic_ip);
-       if (!dev) {
-               kfree(ps);
+       /* check if we are called from a real node or usbfs */
+       if (imajor(inode) == USB_DEVICE_MAJOR)
+               dev = usbdev_lookup_minor(iminor(inode));
+       if (!dev)
+               dev = inode->i_private;
+       if (!dev)
                goto out;
-       }
+       ret = usb_autoresume_device(dev);
+       if (ret)
+               goto out;
+
+       usb_get_dev(dev);
        ret = 0;
        ps->dev = dev;
        ps->file = file;
@@ -514,37 +574,47 @@ static int usbdev_open(struct inode *inode, struct file *file)
        INIT_LIST_HEAD(&ps->async_completed);
        init_waitqueue_head(&ps->wait);
        ps->discsignr = 0;
-       ps->disctask = current;
+       ps->disc_pid = get_pid(task_pid(current));
+       ps->disc_uid = current->uid;
+       ps->disc_euid = current->euid;
        ps->disccontext = NULL;
        ps->ifclaimed = 0;
+       security_task_getsecid(current, &ps->secid);
        wmb();
        list_add_tail(&ps->list, &dev->filelist);
        file->private_data = ps;
  out:
-       unlock_kernel();
- out_nolock:
-        return ret;
+       if (ret)
+               kfree(ps);
+       mutex_unlock(&usbfs_mutex);
+       return ret;
 }
 
 static int usbdev_release(struct inode *inode, struct file *file)
 {
-       struct dev_state *ps = (struct dev_state *)file->private_data;
+       struct dev_state *ps = file->private_data;
        struct usb_device *dev = ps->dev;
        unsigned int ifnum;
 
        usb_lock_device(dev);
+
+       /* Protect against simultaneous open */
+       mutex_lock(&usbfs_mutex);
        list_del_init(&ps->list);
+       mutex_unlock(&usbfs_mutex);
+
        for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
                        ifnum++) {
                if (test_bit(ifnum, &ps->ifclaimed))
                        releaseintf(ps, ifnum);
        }
        destroy_all_async(ps);
+       usb_autosuspend_device(dev);
        usb_unlock_device(dev);
        usb_put_dev(dev);
-       ps->dev = NULL;
+       put_pid(ps->disc_pid);
        kfree(ps);
-        return 0;
+       return 0;
 }
 
 static int proc_control(struct dev_state *ps, void __user *arg)
@@ -569,8 +639,11 @@ static int proc_control(struct dev_state *ps, void __user *arg)
                        free_page((unsigned long)tbuf);
                        return -EINVAL;
                }
-               snoop(&dev->dev, "control read: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n", 
-                       ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
+               snoop(&dev->dev, "control read: bRequest=%02x "
+                               "bRrequestType=%02x wValue=%04x "
+                               "wIndex=%04x wLength=%04x\n",
+                       ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
+                               ctrl.wIndex, ctrl.wLength);
 
                usb_unlock_device(dev);
                i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.bRequest, ctrl.bRequestType,
@@ -579,11 +652,11 @@ static int proc_control(struct dev_state *ps, void __user *arg)
                if ((i > 0) && ctrl.wLength) {
                        if (usbfs_snoop) {
                                dev_info(&dev->dev, "control read: data ");
-                               for (j = 0; j < ctrl.wLength; ++j)
-                                       printk ("%02x ", (unsigned char)(tbuf)[j]);
+                               for (j = 0; j < i; ++j)
+                                       printk("%02x ", (unsigned char)(tbuf)[j]);
                                printk("\n");
                        }
-                       if (copy_to_user(ctrl.data, tbuf, ctrl.wLength)) {
+                       if (copy_to_user(ctrl.data, tbuf, i)) {
                                free_page((unsigned long)tbuf);
                                return -EFAULT;
                        }
@@ -595,12 +668,15 @@ static int proc_control(struct dev_state *ps, void __user *arg)
                                return -EFAULT;
                        }
                }
-               snoop(&dev->dev, "control write: bRequest=%02x bRrequestType=%02x wValue=%04x wIndex=%04x\n", 
-                       ctrl.bRequest, ctrl.bRequestType, ctrl.wValue, ctrl.wIndex);
+               snoop(&dev->dev, "control write: bRequest=%02x "
+                               "bRrequestType=%02x wValue=%04x "
+                               "wIndex=%04x wLength=%04x\n",
+                       ctrl.bRequest, ctrl.bRequestType, ctrl.wValue,
+                               ctrl.wIndex, ctrl.wLength);
                if (usbfs_snoop) {
                        dev_info(&dev->dev, "control write: data: ");
                        for (j = 0; j < ctrl.wLength; ++j)
-                               printk ("%02x ", (unsigned char)(tbuf)[j]);
+                               printk("%02x ", (unsigned char)(tbuf)[j]);
                        printk("\n");
                }
                usb_unlock_device(dev);
@@ -625,7 +701,7 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
        unsigned int tmo, len1, pipe;
        int len2;
        unsigned char *tbuf;
-       int i, ret;
+       int i, j, ret;
 
        if (copy_from_user(&bulk, arg, sizeof(bulk)))
                return -EFAULT;
@@ -650,10 +726,18 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
                        kfree(tbuf);
                        return -EINVAL;
                }
+               snoop(&dev->dev, "bulk read: len=0x%02x timeout=%04d\n",
+                       bulk.len, bulk.timeout);
                usb_unlock_device(dev);
                i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
                usb_lock_device(dev);
                if (!i && len2) {
+                       if (usbfs_snoop) {
+                               dev_info(&dev->dev, "bulk read: data ");
+                               for (j = 0; j < len2; ++j)
+                                       printk("%02x ", (unsigned char)(tbuf)[j]);
+                               printk("\n");
+                       }
                        if (copy_to_user(bulk.data, tbuf, len2)) {
                                kfree(tbuf);
                                return -EFAULT;
@@ -666,6 +750,14 @@ static int proc_bulk(struct dev_state *ps, void __user *arg)
                                return -EFAULT;
                        }
                }
+               snoop(&dev->dev, "bulk write: len=0x%02x timeout=%04d\n",
+                       bulk.len, bulk.timeout);
+               if (usbfs_snoop) {
+                       dev_info(&dev->dev, "bulk write: data: ");
+                       for (j = 0; j < len1; ++j)
+                               printk("%02x ", (unsigned char)(tbuf)[j]);
+                       printk("\n");
+               }
                usb_unlock_device(dev);
                i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
                usb_lock_device(dev);
@@ -746,8 +838,7 @@ static int proc_connectinfo(struct dev_state *ps, void __user *arg)
 
 static int proc_resetdevice(struct dev_state *ps)
 {
-       return usb_reset_device(ps->dev);
-
+       return usb_reset_composite_device(ps->dev, NULL);
 }
 
 static int proc_setintf(struct dev_state *ps, void __user *arg)
@@ -784,16 +875,16 @@ static int proc_setconfig(struct dev_state *ps, void __user *arg)
                for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
                        if (usb_interface_claimed(actconfig->interface[i])) {
                                dev_warn (&ps->dev->dev,
-                                       "usbfs: interface %d claimed "
+                                       "usbfs: interface %d claimed by %s "
                                        "while '%s' sets config #%d\n",
                                        actconfig->interface[i]
                                                ->cur_altsetting
                                                ->desc.bInterfaceNumber,
+                                       actconfig->interface[i]
+                                               ->dev.driver->name,
                                        current->comm, u);
-#if 0  /* FIXME:  enable in 2.6.10 or so */
                                status = -EBUSY;
                                break;
-#endif
                        }
                }
        }
@@ -811,7 +902,6 @@ 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,
                             struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
                             void __user *arg)
@@ -847,8 +937,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
                                != USB_ENDPOINT_XFER_CONTROL)
                        return -EINVAL;
-               /* min 8 byte setup packet, max arbitrary */
-               if (uurb->buffer_length < 8 || uurb->buffer_length > PAGE_SIZE)
+               /* min 8 byte setup packet, max 8 byte setup plus an arbitrary data stage */
+               if (uurb->buffer_length < 8 || uurb->buffer_length > (8 + MAX_USBFS_BUFFER_SIZE))
                        return -EINVAL;
                if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
                        return -ENOMEM;
@@ -872,6 +962,11 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        kfree(dr);
                        return -EFAULT;
                }
+               snoop(&ps->dev->dev, "control urb: bRequest=%02x "
+                       "bRrequestType=%02x wValue=%04x "
+                       "wIndex=%04x wLength=%04x\n",
+                       dr->bRequest, dr->bRequestType, dr->wValue,
+                       dr->wIndex, dr->wLength);
                break;
 
        case USBDEVFS_URB_TYPE_BULK:
@@ -886,6 +981,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        return -EINVAL;
                if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
                        return -EFAULT;
+               snoop(&ps->dev->dev, "bulk urb\n");
                break;
 
        case USBDEVFS_URB_TYPE_ISO:
@@ -904,7 +1000,8 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        return -EFAULT;
                }
                for (totlen = u = 0; u < uurb->number_of_packets; u++) {
-                       if (isopkt[u].length > 1023) {
+                       /* arbitrary limit, sufficient for USB 2.0 high-bandwidth iso */
+                       if (isopkt[u].length > 8192) {
                                kfree(isopkt);
                                return -EINVAL;
                        }
@@ -915,6 +1012,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        return -EINVAL;
                }
                uurb->buffer_length = totlen;
+               snoop(&ps->dev->dev, "iso urb\n");
                break;
 
        case USBDEVFS_URB_TYPE_INTERRUPT:
@@ -930,6 +1028,7 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                        return -EINVAL;
                if (!access_ok((uurb->endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb->buffer, uurb->buffer_length))
                        return -EFAULT;
+               snoop(&ps->dev->dev, "interrupt urb\n");
                break;
 
        default:
@@ -970,13 +1069,18 @@ static int proc_do_submiturb(struct dev_state *ps, struct usbdevfs_urb *uurb,
                as->userbuffer = NULL;
        as->signr = uurb->signr;
        as->ifnum = ifnum;
-       as->task = current;
+       as->pid = get_pid(task_pid(current));
+       as->uid = current->uid;
+       as->euid = current->euid;
+       security_task_getsecid(current, &as->secid);
        if (!(uurb->endpoint & USB_DIR_IN)) {
                if (copy_from_user(as->urb->transfer_buffer, uurb->buffer, as->urb->transfer_buffer_length)) {
                        free_async(as);
                        return -EFAULT;
                }
        }
+       snoop(&as->urb->dev->dev, "submit urb\n");
+       snoop_urb(as->urb, as->userurb);
         async_newpending(as);
         if ((ret = usb_submit_urb(as->urb, GFP_KERNEL))) {
                dev_printk(KERN_DEBUG, &ps->dev->dev, "usbfs: usb_submit_urb returned %d\n", ret);
@@ -1116,7 +1220,7 @@ static int proc_submiturb_compat(struct dev_state *ps, void __user *arg)
 {
        struct usbdevfs_urb uurb;
 
-       if (get_urb32(&uurb,(struct usbdevfs_urb32 *)arg))
+       if (get_urb32(&uurb,(struct usbdevfs_urb32 __user *)arg))
                return -EFAULT;
 
        return proc_do_submiturb(ps, &uurb, ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, arg);
@@ -1151,7 +1255,7 @@ static int processcompl_compat(struct async *as, void __user * __user *arg)
        }
 
        free_async(as);
-       if (put_user((u32)(u64)addr, (u32 __user *)arg))
+       if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
                return -EFAULT;
        return 0;
 }
@@ -1212,24 +1316,20 @@ static int proc_releaseinterface(struct dev_state *ps, void __user *arg)
        return 0;
 }
 
-static int proc_ioctl (struct dev_state *ps, void __user *arg)
+static int proc_ioctl(struct dev_state *ps, struct usbdevfs_ioctl *ctl)
 {
-       struct usbdevfs_ioctl   ctrl;
        int                     size;
        void                    *buf = NULL;
        int                     retval = 0;
        struct usb_interface    *intf = NULL;
        struct usb_driver       *driver = NULL;
-       int                     i;
 
-       /* get input parameters and alloc buffer */
-       if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
-               return -EFAULT;
-       if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
+       /* alloc buffer */
+       if ((size = _IOC_SIZE (ctl->ioctl_code)) > 0) {
                if ((buf = kmalloc (size, GFP_KERNEL)) == NULL)
                        return -ENOMEM;
-               if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
-                       if (copy_from_user (buf, ctrl.data, size)) {
+               if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
+                       if (copy_from_user (buf, ctl->data, size)) {
                                kfree(buf);
                                return -EFAULT;
                        }
@@ -1238,29 +1338,20 @@ static int proc_ioctl (struct dev_state *ps, void __user *arg)
                }
        }
 
-       if (!connected(ps->dev)) {
+       if (!connected(ps)) {
                kfree(buf);
                return -ENODEV;
        }
 
        if (ps->dev->state != USB_STATE_CONFIGURED)
                retval = -EHOSTUNREACH;
-       else if (!(intf = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
+       else if (!(intf = usb_ifnum_to_if (ps->dev, ctl->ifno)))
                retval = -EINVAL;
-       else switch (ctrl.ioctl_code) {
+       else switch (ctl->ioctl_code) {
 
        /* disconnect kernel driver from interface */
        case USBDEVFS_DISCONNECT:
 
-               /* don't allow the user to unbind the hub driver from
-                * a hub with children to manage */
-               for (i = 0; i < ps->dev->maxchild; ++i) {
-                       if (ps->dev->children[i])
-                               retval = -EBUSY;
-               }
-               if (retval)
-                       break;
-
                down_write(&usb_bus_type.subsys.rwsem);
                if (intf->dev.driver) {
                        driver = to_usb_driver(intf->dev.driver);
@@ -1274,9 +1365,7 @@ static int proc_ioctl (struct dev_state *ps, void __user *arg)
        /* let kernel drivers try to (re)bind to the interface */
        case USBDEVFS_CONNECT:
                usb_unlock_device(ps->dev);
-               usb_lock_all_devices();
-               bus_rescan_devices(intf->dev.bus);
-               usb_unlock_all_devices();
+               retval = bus_rescan_devices(intf->dev.bus);
                usb_lock_device(ps->dev);
                break;
 
@@ -1288,7 +1377,7 @@ static int proc_ioctl (struct dev_state *ps, void __user *arg)
                if (driver == NULL || driver->ioctl == NULL) {
                        retval = -ENOTTY;
                } else {
-                       retval = driver->ioctl (intf, ctrl.ioctl_code, buf);
+                       retval = driver->ioctl (intf, ctl->ioctl_code, buf);
                        if (retval == -ENOIOCTLCMD)
                                retval = -ENOTTY;
                }
@@ -1297,15 +1386,42 @@ static int proc_ioctl (struct dev_state *ps, void __user *arg)
 
        /* cleanup and return */
        if (retval >= 0
-                       && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
+                       && (_IOC_DIR (ctl->ioctl_code) & _IOC_READ) != 0
                        && size > 0
-                       && copy_to_user (ctrl.data, buf, size) != 0)
+                       && copy_to_user (ctl->data, buf, size) != 0)
                retval = -EFAULT;
 
        kfree(buf);
        return retval;
 }
 
+static int proc_ioctl_default(struct dev_state *ps, void __user *arg)
+{
+       struct usbdevfs_ioctl   ctrl;
+
+       if (copy_from_user(&ctrl, arg, sizeof (ctrl)))
+               return -EFAULT;
+       return proc_ioctl(ps, &ctrl);
+}
+
+#ifdef CONFIG_COMPAT
+static int proc_ioctl_compat(struct dev_state *ps, compat_uptr_t arg)
+{
+       struct usbdevfs_ioctl32 __user *uioc;
+       struct usbdevfs_ioctl ctrl;
+       u32 udata;
+
+       uioc = compat_ptr((long)arg);
+       if (get_user(ctrl.ifno, &uioc->ifno) ||
+           get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
+           __get_user(udata, &uioc->data))
+               return -EFAULT;
+       ctrl.data = compat_ptr(udata);
+
+       return proc_ioctl(ps, &ctrl);
+}
+#endif
+
 /*
  * NOTE:  All requests here that have interface numbers as parameters
  * are assuming that somehow the configuration has been prevented from
@@ -1313,7 +1429,7 @@ static int proc_ioctl (struct dev_state *ps, void __user *arg)
  */
 static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
 {
-       struct dev_state *ps = (struct dev_state *)file->private_data;
+       struct dev_state *ps = file->private_data;
        struct usb_device *dev = ps->dev;
        void __user *p = (void __user *)arg;
        int ret = -ENOTTY;
@@ -1321,7 +1437,7 @@ static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
        if (!(file->f_mode & FMODE_WRITE))
                return -EPERM;
        usb_lock_device(dev);
-       if (!connected(dev)) {
+       if (!connected(ps)) {
                usb_unlock_device(dev);
                return -ENODEV;
        }
@@ -1406,6 +1522,10 @@ static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
                ret = proc_reapurbnonblock_compat(ps, p);
                break;
 
+       case USBDEVFS_IOCTL32:
+               snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
+               ret = proc_ioctl_compat(ps, ptr_to_compat(p));
+               break;
 #endif
 
        case USBDEVFS_DISCARDURB:
@@ -1440,7 +1560,7 @@ static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
 
        case USBDEVFS_IOCTL:
                snoop(&dev->dev, "%s: IOCTL\n", __FUNCTION__);
-               ret = proc_ioctl(ps, p);
+               ret = proc_ioctl_default(ps, p);
                break;
        }
        usb_unlock_device(dev);
@@ -1452,18 +1572,18 @@ static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
 /* No kernel lock - fine */
 static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
 {
-       struct dev_state *ps = (struct dev_state *)file->private_data;
-        unsigned int mask = 0;
+       struct dev_state *ps = file->private_data;
+       unsigned int mask = 0;
 
        poll_wait(file, &ps->wait, wait);
        if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
                mask |= POLLOUT | POLLWRNORM;
-       if (!connected(ps->dev))
+       if (!connected(ps))
                mask |= POLLERR | POLLHUP;
        return mask;
 }
 
-struct file_operations usbfs_device_file_operations = {
+const struct file_operations usbfs_device_file_operations = {
        .llseek =       usbdev_lseek,
        .read =         usbdev_read,
        .poll =         usbdev_poll,
@@ -1471,3 +1591,92 @@ struct file_operations usbfs_device_file_operations = {
        .open =         usbdev_open,
        .release =      usbdev_release,
 };
+
+static int usbdev_add(struct usb_device *dev)
+{
+       int minor = ((dev->bus->busnum-1) * 128) + (dev->devnum-1);
+
+       dev->class_dev = class_device_create(usb_device_class, NULL,
+                               MKDEV(USB_DEVICE_MAJOR, minor), &dev->dev,
+                               "usbdev%d.%d", dev->bus->busnum, dev->devnum);
+       if (IS_ERR(dev->class_dev))
+               return PTR_ERR(dev->class_dev);
+
+       dev->class_dev->class_data = dev;
+       return 0;
+}
+
+static void usbdev_remove(struct usb_device *dev)
+{
+       class_device_unregister(dev->class_dev);
+}
+
+static int usbdev_notify(struct notifier_block *self, unsigned long action,
+                        void *dev)
+{
+       switch (action) {
+       case USB_DEVICE_ADD:
+               if (usbdev_add(dev))
+                       return NOTIFY_BAD;
+               break;
+       case USB_DEVICE_REMOVE:
+               usbdev_remove(dev);
+               break;
+       }
+       return NOTIFY_OK;
+}
+
+static struct notifier_block usbdev_nb = {
+       .notifier_call =        usbdev_notify,
+};
+
+static struct cdev usb_device_cdev = {
+       .kobj   = {.name = "usb_device", },
+       .owner  = THIS_MODULE,
+};
+
+int __init usbdev_init(void)
+{
+       int retval;
+
+       retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
+                       "usb_device");
+       if (retval) {
+               err("unable to register minors for usb_device");
+               goto out;
+       }
+       cdev_init(&usb_device_cdev, &usbfs_device_file_operations);
+       retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
+       if (retval) {
+               err("unable to get usb_device major %d", USB_DEVICE_MAJOR);
+               goto error_cdev;
+       }
+       usb_device_class = class_create(THIS_MODULE, "usb_device");
+       if (IS_ERR(usb_device_class)) {
+               err("unable to register usb_device class");
+               retval = PTR_ERR(usb_device_class);
+               goto error_class;
+       }
+
+       usb_register_notify(&usbdev_nb);
+
+out:
+       return retval;
+
+error_class:
+       usb_device_class = NULL;
+       cdev_del(&usb_device_cdev);
+
+error_cdev:
+       unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
+       goto out;
+}
+
+void usbdev_cleanup(void)
+{
+       usb_unregister_notify(&usbdev_nb);
+       class_destroy(usb_device_class);
+       cdev_del(&usb_device_cdev);
+       unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
+}
+