drm/radeon: Poll for both connect/disconnect on analog connectors
[pandora-kernel.git] / drivers / hid / hidraw.c
index cf7d6d5..9e50f61 100644 (file)
@@ -87,13 +87,16 @@ static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count,
                len = list->buffer[list->tail].len > count ?
                        count : list->buffer[list->tail].len;
 
-               if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
-                       ret = -EFAULT;
-                       goto out;
+               if (list->buffer[list->tail].value) {
+                       if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
+                               ret = -EFAULT;
+                               goto out;
+                       }
+                       ret = len;
                }
-               ret = len;
 
                kfree(list->buffer[list->tail].value);
+               list->buffer[list->tail].value = NULL;
                list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
        }
 out:
@@ -110,7 +113,7 @@ static ssize_t hidraw_send_report(struct file *file, const char __user *buffer,
        __u8 *buf;
        int ret = 0;
 
-       if (!hidraw_table[minor]) {
+       if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
                ret = -ENODEV;
                goto out;
        }
@@ -258,7 +261,7 @@ static int hidraw_open(struct inode *inode, struct file *file)
        }
 
        mutex_lock(&minors_lock);
-       if (!hidraw_table[minor]) {
+       if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
                err = -ENODEV;
                goto out_unlock;
        }
@@ -292,35 +295,38 @@ out:
 
 }
 
+static void drop_ref(struct hidraw *hidraw, int exists_bit)
+{
+       if (exists_bit) {
+               hid_hw_close(hidraw->hid);
+               hidraw->exist = 0;
+               if (hidraw->open)
+                       wake_up_interruptible(&hidraw->wait);
+       } else {
+               --hidraw->open;
+       }
+
+       if (!hidraw->open && !hidraw->exist) {
+               device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
+               hidraw_table[hidraw->minor] = NULL;
+               kfree(hidraw);
+       }
+}
+
 static int hidraw_release(struct inode * inode, struct file * file)
 {
        unsigned int minor = iminor(inode);
-       struct hidraw *dev;
        struct hidraw_list *list = file->private_data;
-       int ret;
 
        mutex_lock(&minors_lock);
-       if (!hidraw_table[minor]) {
-               ret = -ENODEV;
-               goto unlock;
-       }
 
        list_del(&list->node);
-       dev = hidraw_table[minor];
-       if (!--dev->open) {
-               if (list->hidraw->exist) {
-                       hid_hw_power(dev->hid, PM_HINT_NORMAL);
-                       hid_hw_close(dev->hid);
-               } else {
-                       kfree(list->hidraw);
-               }
-       }
        kfree(list);
-       ret = 0;
-unlock:
-       mutex_unlock(&minors_lock);
 
-       return ret;
+       drop_ref(hidraw_table[minor], 0);
+
+       mutex_unlock(&minors_lock);
+       return 0;
 }
 
 static long hidraw_ioctl(struct file *file, unsigned int cmd,
@@ -437,19 +443,29 @@ static const struct file_operations hidraw_ops = {
        .llseek =       noop_llseek,
 };
 
-void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
+int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
 {
        struct hidraw *dev = hid->hidraw;
        struct hidraw_list *list;
+       int ret = 0;
 
        list_for_each_entry(list, &dev->list, node) {
-               list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
+               int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
+
+               if (new_head == list->tail)
+                       continue;
+
+               if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
+                       ret = -ENOMEM;
+                       break;
+               }
                list->buffer[list->head].len = len;
-               list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
+               list->head = new_head;
                kill_fasync(&list->fasync, SIGIO, POLL_IN);
        }
 
        wake_up_interruptible(&dev->wait);
+       return ret;
 }
 EXPORT_SYMBOL_GPL(hidraw_report_event);
 
@@ -514,18 +530,9 @@ void hidraw_disconnect(struct hid_device *hid)
        struct hidraw *hidraw = hid->hidraw;
 
        mutex_lock(&minors_lock);
-       hidraw->exist = 0;
 
-       device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
+       drop_ref(hidraw, 1);
 
-       hidraw_table[hidraw->minor] = NULL;
-
-       if (hidraw->open) {
-               hid_hw_close(hid);
-               wake_up_interruptible(&hidraw->wait);
-       } else {
-               kfree(hidraw);
-       }
        mutex_unlock(&minors_lock);
 }
 EXPORT_SYMBOL_GPL(hidraw_disconnect);
@@ -542,21 +549,28 @@ int __init hidraw_init(void)
 
        if (result < 0) {
                pr_warn("can't get major number\n");
-               result = 0;
                goto out;
        }
 
        hidraw_class = class_create(THIS_MODULE, "hidraw");
        if (IS_ERR(hidraw_class)) {
                result = PTR_ERR(hidraw_class);
-               unregister_chrdev(hidraw_major, "hidraw");
-               goto out;
+               goto error_cdev;
        }
 
         cdev_init(&hidraw_cdev, &hidraw_ops);
-        cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
+       result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
+       if (result < 0)
+               goto error_class;
+
 out:
        return result;
+
+error_class:
+       class_destroy(hidraw_class);
+error_cdev:
+       unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
+       goto out;
 }
 
 void hidraw_exit(void)