Merge branch 'master' into upstream
[pandora-kernel.git] / drivers / hid / usbhid / hiddev.c
index fedd88d..af0a7c1 100644 (file)
@@ -29,7 +29,6 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/smp_lock.h>
 #include <linux/input.h>
 #include <linux/usb.h>
 #include <linux/hid.h>
@@ -586,163 +585,168 @@ static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct hiddev_list *list = file->private_data;
        struct hiddev *hiddev = list->hiddev;
-       struct hid_device *hid = hiddev->hid;
-       struct usb_device *dev;
+       struct hid_device *hid;
        struct hiddev_collection_info cinfo;
        struct hiddev_report_info rinfo;
        struct hiddev_field_info finfo;
        struct hiddev_devinfo dinfo;
        struct hid_report *report;
        struct hid_field *field;
-       struct usbhid_device *usbhid = hid->driver_data;
        void __user *user_arg = (void __user *)arg;
-       int i, r;
-       
+       int i, r = -EINVAL;
+
        /* Called without BKL by compat methods so no BKL taken */
 
-       /* FIXME: Who or what stop this racing with a disconnect ?? */
-       if (!hiddev->exist || !hid)
-               return -EIO;
+       mutex_lock(&hiddev->existancelock);
+       if (!hiddev->exist) {
+               r = -ENODEV;
+               goto ret_unlock;
+       }
 
-       dev = hid_to_usb_dev(hid);
+       hid = hiddev->hid;
 
        switch (cmd) {
 
        case HIDIOCGVERSION:
-               return put_user(HID_VERSION, (int __user *)arg);
+               r = put_user(HID_VERSION, (int __user *)arg) ?
+                       -EFAULT : 0;
+               break;
 
        case HIDIOCAPPLICATION:
                if (arg < 0 || arg >= hid->maxapplication)
-                       return -EINVAL;
+                       break;
 
                for (i = 0; i < hid->maxcollection; i++)
                        if (hid->collection[i].type ==
                            HID_COLLECTION_APPLICATION && arg-- == 0)
                                break;
 
-               if (i == hid->maxcollection)
-                       return -EINVAL;
-
-               return hid->collection[i].usage;
+               if (i < hid->maxcollection)
+                       r = hid->collection[i].usage;
+               break;
 
        case HIDIOCGDEVINFO:
-               dinfo.bustype = BUS_USB;
-               dinfo.busnum = dev->bus->busnum;
-               dinfo.devnum = dev->devnum;
-               dinfo.ifnum = usbhid->ifnum;
-               dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
-               dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
-               dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
-               dinfo.num_applications = hid->maxapplication;
-               if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
-                       return -EFAULT;
-
-               return 0;
+               {
+                       struct usb_device *dev = hid_to_usb_dev(hid);
+                       struct usbhid_device *usbhid = hid->driver_data;
+
+                       dinfo.bustype = BUS_USB;
+                       dinfo.busnum = dev->bus->busnum;
+                       dinfo.devnum = dev->devnum;
+                       dinfo.ifnum = usbhid->ifnum;
+                       dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
+                       dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
+                       dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
+                       dinfo.num_applications = hid->maxapplication;
+
+                       r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
+                               -EFAULT : 0;
+                       break;
+               }
 
        case HIDIOCGFLAG:
-               if (put_user(list->flags, (int __user *)arg))
-                       return -EFAULT;
-
-               return 0;
+               r = put_user(list->flags, (int __user *)arg) ?
+                       -EFAULT : 0;
+               break;
 
        case HIDIOCSFLAG:
                {
                        int newflags;
-                       if (get_user(newflags, (int __user *)arg))
-                               return -EFAULT;
+
+                       if (get_user(newflags, (int __user *)arg)) {
+                               r = -EFAULT;
+                               break;
+                       }
 
                        if ((newflags & ~HIDDEV_FLAGS) != 0 ||
                            ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
                             (newflags & HIDDEV_FLAG_UREF) == 0))
-                               return -EINVAL;
+                               break;
 
                        list->flags = newflags;
 
-                       return 0;
+                       r = 0;
+                       break;
                }
 
        case HIDIOCGSTRING:
-               mutex_lock(&hiddev->existancelock);
-               if (hiddev->exist)
-                       r = hiddev_ioctl_string(hiddev, cmd, user_arg);
-               else
-                       r = -ENODEV;
-               mutex_unlock(&hiddev->existancelock);
-               return r;
+               r = hiddev_ioctl_string(hiddev, cmd, user_arg);
+               break;
 
        case HIDIOCINITREPORT:
-               mutex_lock(&hiddev->existancelock);
-               if (!hiddev->exist) {
-                       mutex_unlock(&hiddev->existancelock);
-                       return -ENODEV;
-               }
                usbhid_init_reports(hid);
-               mutex_unlock(&hiddev->existancelock);
-
-               return 0;
+               r = 0;
+               break;
 
        case HIDIOCGREPORT:
-               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
-                       return -EFAULT;
+               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
+                       r = -EFAULT;
+                       break;
+               }
 
                if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
-                       return -EINVAL;
+                       break;
 
-               if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
-                       return -EINVAL;
+               report = hiddev_lookup_report(hid, &rinfo);
+               if (report == NULL)
+                       break;
 
-               mutex_lock(&hiddev->existancelock);
-               if (hiddev->exist) {
-                       usbhid_submit_report(hid, report, USB_DIR_IN);
-                       usbhid_wait_io(hid);
-               }
-               mutex_unlock(&hiddev->existancelock);
+               usbhid_submit_report(hid, report, USB_DIR_IN);
+               usbhid_wait_io(hid);
 
-               return 0;
+               r = 0;
+               break;
 
        case HIDIOCSREPORT:
-               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
-                       return -EFAULT;
+               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
+                       r = -EFAULT;
+                       break;
+               }
 
                if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
-                       return -EINVAL;
+                       break;
 
-               if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
-                       return -EINVAL;
+               report = hiddev_lookup_report(hid, &rinfo);
+               if (report == NULL)
+                       break;
 
-               mutex_lock(&hiddev->existancelock);
-               if (hiddev->exist) {
-                       usbhid_submit_report(hid, report, USB_DIR_OUT);
-                       usbhid_wait_io(hid);
-               }
-               mutex_unlock(&hiddev->existancelock);
+               usbhid_submit_report(hid, report, USB_DIR_OUT);
+               usbhid_wait_io(hid);
 
-               return 0;
+               r = 0;
+               break;
 
        case HIDIOCGREPORTINFO:
-               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
-                       return -EFAULT;
+               if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
+                       r = -EFAULT;
+                       break;
+               }
 
-               if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
-                       return -EINVAL;
+               report = hiddev_lookup_report(hid, &rinfo);
+               if (report == NULL)
+                       break;
 
                rinfo.num_fields = report->maxfield;
 
-               if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
-                       return -EFAULT;
-
-               return 0;
+               r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
+                       -EFAULT : 0;
+               break;
 
        case HIDIOCGFIELDINFO:
-               if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
-                       return -EFAULT;
+               if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
+                       r = -EFAULT;
+                       break;
+               }
+
                rinfo.report_type = finfo.report_type;
                rinfo.report_id = finfo.report_id;
-               if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
-                       return -EINVAL;
+
+               report = hiddev_lookup_report(hid, &rinfo);
+               if (report == NULL)
+                       break;
 
                if (finfo.field_index >= report->maxfield)
-                       return -EINVAL;
+                       break;
 
                field = report->field[finfo.field_index];
                memset(&finfo, 0, sizeof(finfo));
@@ -761,10 +765,9 @@ static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                finfo.unit_exponent = field->unit_exponent;
                finfo.unit = field->unit;
 
-               if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
-                       return -EFAULT;
-
-               return 0;
+               r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
+                       -EFAULT : 0;
+               break;
 
        case HIDIOCGUCODE:
                /* fall through */
@@ -773,57 +776,66 @@ static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
        case HIDIOCGUSAGES:
        case HIDIOCSUSAGES:
        case HIDIOCGCOLLECTIONINDEX:
-               mutex_lock(&hiddev->existancelock);
-               if (hiddev->exist)
-                       r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
-               else
-                       r = -ENODEV;
-               mutex_unlock(&hiddev->existancelock);
-               return r;
+               r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
+               break;
 
        case HIDIOCGCOLLECTIONINFO:
-               if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
-                       return -EFAULT;
+               if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
+                       r = -EFAULT;
+                       break;
+               }
 
                if (cinfo.index >= hid->maxcollection)
-                       return -EINVAL;
+                       break;
 
                cinfo.type = hid->collection[cinfo.index].type;
                cinfo.usage = hid->collection[cinfo.index].usage;
                cinfo.level = hid->collection[cinfo.index].level;
 
-               if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
-                       return -EFAULT;
-               return 0;
+               r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
+                       -EFAULT : 0;
+               break;
 
        default:
-
                if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
-                       return -EINVAL;
+                       break;
 
                if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
                        int len;
-                       if (!hid->name)
-                               return 0;
+
+                       if (!hid->name) {
+                               r = 0;
+                               break;
+                       }
+
                        len = strlen(hid->name) + 1;
                        if (len > _IOC_SIZE(cmd))
                                 len = _IOC_SIZE(cmd);
-                       return copy_to_user(user_arg, hid->name, len) ?
+                       r = copy_to_user(user_arg, hid->name, len) ?
                                -EFAULT : len;
+                       break;
                }
 
                if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
                        int len;
-                       if (!hid->phys)
-                               return 0;
+
+                       if (!hid->phys) {
+                               r = 0;
+                               break;
+                       }
+
                        len = strlen(hid->phys) + 1;
                        if (len > _IOC_SIZE(cmd))
                                len = _IOC_SIZE(cmd);
-                       return copy_to_user(user_arg, hid->phys, len) ?
+                       r = copy_to_user(user_arg, hid->phys, len) ?
                                -EFAULT : len;
+                       break;
                }
        }
-       return -EINVAL;
+
+ret_unlock:
+       mutex_unlock(&hiddev->existancelock);
+       return r;
 }
 
 #ifdef CONFIG_COMPAT
@@ -893,7 +905,7 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)
        hiddev->exist = 1;
        retval = usb_register_dev(usbhid->intf, &hiddev_class);
        if (retval) {
-               err_hid("Not able to get a minor for this device.");
+               hid_err(hid, "Not able to get a minor for this device\n");
                hid->hiddev = NULL;
                kfree(hiddev);
                return -1;