Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/dtor/input
[safe/jmp/linux-2.6] / drivers / input / joydev.c
index 40d2b46..9bcc542 100644 (file)
@@ -43,7 +43,7 @@ struct joydev {
        char name[16];
        struct input_handle handle;
        wait_queue_head_t wait;
-       struct list_head list;
+       struct list_head client_list;
        struct js_corr corr[ABS_MAX + 1];
        struct JS_DATA_SAVE_TYPE glue;
        int nabs;
@@ -55,7 +55,7 @@ struct joydev {
        __s16 abs[ABS_MAX + 1];
 };
 
-struct joydev_list {
+struct joydev_client {
        struct js_event buffer[JOYDEV_BUFFER_SIZE];
        int head;
        int tail;
@@ -81,22 +81,20 @@ static int joydev_correct(int value, struct js_corr *corr)
                        return 0;
        }
 
-       if (value < -32767) return -32767;
-       if (value >  32767) return  32767;
-
-       return value;
+       return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
 }
 
 static void joydev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
 {
        struct joydev *joydev = handle->private;
-       struct joydev_list *list;
+       struct joydev_client *client;
        struct js_event event;
 
        switch (type) {
 
                case EV_KEY:
-                       if (code < BTN_MISC || value == 2) return;
+                       if (code < BTN_MISC || value == 2)
+                               return;
                        event.type = JS_EVENT_BUTTON;
                        event.number = joydev->keymap[code - BTN_MISC];
                        event.value = value;
@@ -106,7 +104,8 @@ static void joydev_event(struct input_handle *handle, unsigned int type, unsigne
                        event.type = JS_EVENT_AXIS;
                        event.number = joydev->absmap[code];
                        event.value = joydev_correct(value, joydev->corr + event.number);
-                       if (event.value == joydev->abs[event.number]) return;
+                       if (event.value == joydev->abs[event.number])
+                               return;
                        joydev->abs[event.number] = event.value;
                        break;
 
@@ -116,15 +115,15 @@ static void joydev_event(struct input_handle *handle, unsigned int type, unsigne
 
        event.time = jiffies_to_msecs(jiffies);
 
-       list_for_each_entry(list, &joydev->list, node) {
+       list_for_each_entry(client, &joydev->client_list, node) {
 
-               memcpy(list->buffer + list->head, &event, sizeof(struct js_event));
+               memcpy(client->buffer + client->head, &event, sizeof(struct js_event));
 
-               if (list->startup == joydev->nabs + joydev->nkey)
-                       if (list->tail == (list->head = (list->head + 1) & (JOYDEV_BUFFER_SIZE - 1)))
-                               list->startup = 0;
+               if (client->startup == joydev->nabs + joydev->nkey)
+                       if (client->tail == (client->head = (client->head + 1) & (JOYDEV_BUFFER_SIZE - 1)))
+                               client->startup = 0;
 
-               kill_fasync(&list->fasync, SIGIO, POLL_IN);
+               kill_fasync(&client->fasync, SIGIO, POLL_IN);
        }
 
        wake_up_interruptible(&joydev->wait);
@@ -133,8 +132,10 @@ static void joydev_event(struct input_handle *handle, unsigned int type, unsigne
 static int joydev_fasync(int fd, struct file *file, int on)
 {
        int retval;
-       struct joydev_list *list = file->private_data;
-       retval = fasync_helper(fd, file, on, &list->fasync);
+       struct joydev_client *client = file->private_data;
+
+       retval = fasync_helper(fd, file, on, &client->fasync);
+
        return retval < 0 ? retval : 0;
 }
 
@@ -144,61 +145,73 @@ static void joydev_free(struct joydev *joydev)
        kfree(joydev);
 }
 
-static int joydev_release(struct inode * inode, struct file * file)
+static int joydev_release(struct inode *inode, struct file *file)
 {
-       struct joydev_list *list = file->private_data;
+       struct joydev_client *client = file->private_data;
+       struct joydev *joydev = client->joydev;
 
        joydev_fasync(-1, file, 0);
 
-       list_del(&list->node);
+       list_del(&client->node);
+       kfree(client);
 
-       if (!--list->joydev->open) {
-               if (list->joydev->exist)
-                       input_close_device(&list->joydev->handle);
+       if (!--joydev->open) {
+               if (joydev->exist)
+                       input_close_device(&joydev->handle);
                else
-                       joydev_free(list->joydev);
+                       joydev_free(joydev);
        }
 
-       kfree(list);
        return 0;
 }
 
 static int joydev_open(struct inode *inode, struct file *file)
 {
-       struct joydev_list *list;
+       struct joydev_client *client;
+       struct joydev *joydev;
        int i = iminor(inode) - JOYDEV_MINOR_BASE;
+       int error;
 
-       if (i >= JOYDEV_MINORS || !joydev_table[i])
+       if (i >= JOYDEV_MINORS)
                return -ENODEV;
 
-       if (!(list = kmalloc(sizeof(struct joydev_list), GFP_KERNEL)))
+       joydev = joydev_table[i];
+       if (!joydev || !joydev->exist)
+               return -ENODEV;
+
+       client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
+       if (!client)
                return -ENOMEM;
-       memset(list, 0, sizeof(struct joydev_list));
 
-       list->joydev = joydev_table[i];
-       list_add_tail(&list->node, &joydev_table[i]->list);
-       file->private_data = list;
+       client->joydev = joydev;
+       list_add_tail(&client->node, &joydev->client_list);
 
-       if (!list->joydev->open++)
-               if (list->joydev->exist)
-                       input_open_device(&list->joydev->handle);
+       if (!joydev->open++ && joydev->exist) {
+               error = input_open_device(&joydev->handle);
+               if (error) {
+                       list_del(&client->node);
+                       kfree(client);
+                       return error;
+               }
+       }
 
+       file->private_data = client;
        return 0;
 }
 
-static ssize_t joydev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
+static ssize_t joydev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
 {
        return -EINVAL;
 }
 
 static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 {
-       struct joydev_list *list = file->private_data;
-       struct joydev *joydev = list->joydev;
+       struct joydev_client *client = file->private_data;
+       struct joydev *joydev = client->joydev;
        struct input_dev *input = joydev->handle.dev;
        int retval = 0;
 
-       if (!list->joydev->exist)
+       if (!joydev->exist)
                return -ENODEV;
 
        if (count < sizeof(struct js_event))
@@ -217,56 +230,55 @@ static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, lo
                if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
                        return -EFAULT;
 
-               list->startup = 0;
-               list->tail = list->head;
+               client->startup = 0;
+               client->tail = client->head;
 
                return sizeof(struct JS_DATA_TYPE);
        }
 
-       if (list->startup == joydev->nabs + joydev->nkey
-               && list->head == list->tail && (file->f_flags & O_NONBLOCK))
-                       return -EAGAIN;
-
-       retval = wait_event_interruptible(list->joydev->wait,
-                                         !list->joydev->exist ||
-                                         list->startup < joydev->nabs + joydev->nkey ||
-                                         list->head != list->tail);
+       if (client->startup == joydev->nabs + joydev->nkey &&
+           client->head == client->tail && (file->f_flags & O_NONBLOCK))
+               return -EAGAIN;
 
+       retval = wait_event_interruptible(joydev->wait,
+                                         !joydev->exist ||
+                                         client->startup < joydev->nabs + joydev->nkey ||
+                                         client->head != client->tail);
        if (retval)
                return retval;
 
-       if (!list->joydev->exist)
+       if (!joydev->exist)
                return -ENODEV;
 
-       while (list->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) {
+       while (client->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) {
 
                struct js_event event;
 
                event.time = jiffies_to_msecs(jiffies);
 
-               if (list->startup < joydev->nkey) {
+               if (client->startup < joydev->nkey) {
                        event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
-                       event.number = list->startup;
+                       event.number = client->startup;
                        event.value = !!test_bit(joydev->keypam[event.number], input->key);
                } else {
                        event.type = JS_EVENT_AXIS | JS_EVENT_INIT;
-                       event.number = list->startup - joydev->nkey;
+                       event.number = client->startup - joydev->nkey;
                        event.value = joydev->abs[event.number];
                }
 
                if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
                        return -EFAULT;
 
-               list->startup++;
+               client->startup++;
                retval += sizeof(struct js_event);
        }
 
-       while (list->head != list->tail && retval + sizeof(struct js_event) <= count) {
+       while (client->head != client->tail && retval + sizeof(struct js_event) <= count) {
 
-               if (copy_to_user(buf + retval, list->buffer + list->tail, sizeof(struct js_event)))
+               if (copy_to_user(buf + retval, client->buffer + client->tail, sizeof(struct js_event)))
                        return -EFAULT;
 
-               list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
+               client->tail = (client->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
                retval += sizeof(struct js_event);
        }
 
@@ -276,10 +288,12 @@ static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, lo
 /* No kernel lock - fine */
 static unsigned int joydev_poll(struct file *file, poll_table *wait)
 {
-       struct joydev_list *list = file->private_data;
-       poll_wait(file, &list->joydev->wait, wait);
-       return ((list->head != list->tail || list->startup < list->joydev->nabs + list->joydev->nkey) ? 
-               (POLLIN | POLLRDNORM) : 0) | (list->joydev->exist ? 0 : (POLLHUP | POLLERR));
+       struct joydev_client *client = file->private_data;
+       struct joydev *joydev = client->joydev;
+
+       poll_wait(file, &joydev->wait, wait);
+       return ((client->head != client->tail || client->startup < joydev->nabs + joydev->nkey) ?
+               (POLLIN | POLLRDNORM) : 0) | (joydev->exist ? 0 : (POLLHUP | POLLERR));
 }
 
 static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __user *argp)
@@ -292,20 +306,26 @@ static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __u
                case JS_SET_CAL:
                        return copy_from_user(&joydev->glue.JS_CORR, argp,
                                sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
+
                case JS_GET_CAL:
                        return copy_to_user(argp, &joydev->glue.JS_CORR,
                                sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
+
                case JS_SET_TIMEOUT:
                        return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
+
                case JS_GET_TIMEOUT:
                        return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
 
                case JSIOCGVERSION:
                        return put_user(JS_VERSION, (__u32 __user *) argp);
+
                case JSIOCGAXES:
                        return put_user(joydev->nabs, (__u8 __user *) argp);
+
                case JSIOCGBUTTONS:
                        return put_user(joydev->nkey, (__u8 __user *) argp);
+
                case JSIOCSCORR:
                        if (copy_from_user(joydev->corr, argp,
                                      sizeof(joydev->corr[0]) * joydev->nabs))
@@ -315,38 +335,49 @@ static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __u
                                joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i);
                        }
                        return 0;
+
                case JSIOCGCORR:
                        return copy_to_user(argp, joydev->corr,
                                                sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
+
                case JSIOCSAXMAP:
                        if (copy_from_user(joydev->abspam, argp, sizeof(__u8) * (ABS_MAX + 1)))
                                return -EFAULT;
                        for (i = 0; i < joydev->nabs; i++) {
-                               if (joydev->abspam[i] > ABS_MAX) return -EINVAL;
+                               if (joydev->abspam[i] > ABS_MAX)
+                                       return -EINVAL;
                                joydev->absmap[joydev->abspam[i]] = i;
                        }
                        return 0;
+
                case JSIOCGAXMAP:
                        return copy_to_user(argp, joydev->abspam,
                                                sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
+
                case JSIOCSBTNMAP:
                        if (copy_from_user(joydev->keypam, argp, sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
                                return -EFAULT;
                        for (i = 0; i < joydev->nkey; i++) {
-                               if (joydev->keypam[i] > KEY_MAX || joydev->keypam[i] < BTN_MISC) return -EINVAL;
+                               if (joydev->keypam[i] > KEY_MAX || joydev->keypam[i] < BTN_MISC)
+                                       return -EINVAL;
                                joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
                        }
                        return 0;
+
                case JSIOCGBTNMAP:
                        return copy_to_user(argp, joydev->keypam,
                                                sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
+
                default:
                        if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
                                int len;
-                               if (!dev->name) return 0;
+                               if (!dev->name)
+                                       return 0;
                                len = strlen(dev->name) + 1;
-                               if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
-                               if (copy_to_user(argp, dev->name, len)) return -EFAULT;
+                               if (len > _IOC_SIZE(cmd))
+                                       len = _IOC_SIZE(cmd);
+                               if (copy_to_user(argp, dev->name, len))
+                                       return -EFAULT;
                                return len;
                        }
        }
@@ -356,14 +387,16 @@ static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __u
 #ifdef CONFIG_COMPAT
 static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       struct joydev_list *list = file->private_data;
-       struct joydev *joydev = list->joydev;
+       struct joydev_client *client = file->private_data;
+       struct joydev *joydev = client->joydev;
        void __user *argp = (void __user *)arg;
        s32 tmp32;
        struct JS_DATA_SAVE_TYPE_32 ds32;
        int err;
 
-       if (!joydev->exist) return -ENODEV;
+       if (!joydev->exist)
+               return -ENODEV;
+
        switch(cmd) {
        case JS_SET_TIMELIMIT:
                err = get_user(tmp32, (s32 __user *) arg);
@@ -396,8 +429,7 @@ static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
                ds32.JS_SAVE       = joydev->glue.JS_SAVE;
                ds32.JS_CORR       = joydev->glue.JS_CORR;
 
-               err = copy_to_user(argp, &ds32,
-                                         sizeof(ds32)) ? -EFAULT : 0;
+               err = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
                break;
 
        default:
@@ -409,11 +441,12 @@ static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
 
 static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
 {
-       struct joydev_list *list = file->private_data;
-       struct joydev *joydev = list->joydev;
+       struct joydev_client *client = file->private_data;
+       struct joydev *joydev = client->joydev;
        void __user *argp = (void __user *)arg;
 
-       if (!joydev->exist) return -ENODEV;
+       if (!joydev->exist)
+               return -ENODEV;
 
        switch(cmd) {
                case JS_SET_TIMELIMIT:
@@ -431,7 +464,7 @@ static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd
        }
 }
 
-static struct file_operations joydev_fops = {
+static const struct file_operations joydev_fops = {
        .owner =        THIS_MODULE,
        .read =         joydev_read,
        .write =        joydev_write,
@@ -445,22 +478,26 @@ static struct file_operations joydev_fops = {
        .fasync =       joydev_fasync,
 };
 
-static struct input_handle *joydev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
+static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
+                         const struct input_device_id *id)
 {
        struct joydev *joydev;
+       struct class_device *cdev;
+       dev_t devt;
        int i, j, t, minor;
+       int error;
 
        for (minor = 0; minor < JOYDEV_MINORS && joydev_table[minor]; minor++);
        if (minor == JOYDEV_MINORS) {
                printk(KERN_ERR "joydev: no more free joydev devices\n");
-               return NULL;
+               return -ENFILE;
        }
 
-       if (!(joydev = kmalloc(sizeof(struct joydev), GFP_KERNEL)))
-               return NULL;
-       memset(joydev, 0, sizeof(struct joydev));
+       joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
+       if (!joydev)
+               return -ENOMEM;
 
-       INIT_LIST_HEAD(&joydev->list);
+       INIT_LIST_HEAD(&joydev->client_list);
        init_waitqueue_head(&joydev->wait);
 
        joydev->minor = minor;
@@ -513,40 +550,68 @@ static struct input_handle *joydev_connect(struct input_handler *handler, struct
 
        joydev_table[minor] = joydev;
 
-       class_device_create(input_class, NULL,
-                       MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor),
-                       dev->dev, "js%d", minor);
+       devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor),
+
+       cdev = class_device_create(&input_class, &dev->cdev, devt,
+                                  dev->cdev.dev, joydev->name);
+       if (IS_ERR(cdev)) {
+               error = PTR_ERR(cdev);
+               goto err_free_joydev;
+       }
+
+       /* temporary symlink to keep userspace happy */
+       error = sysfs_create_link(&input_class.subsys.kobj,
+                                 &cdev->kobj, joydev->name);
+       if (error)
+               goto err_cdev_destroy;
+
+       error = input_register_handle(&joydev->handle);
+       if (error)
+               goto err_remove_link;
+
+       return 0;
 
-       return &joydev->handle;
+ err_remove_link:
+       sysfs_remove_link(&input_class.subsys.kobj, joydev->name);
+ err_cdev_destroy:
+       class_device_destroy(&input_class, devt);
+ err_free_joydev:
+       joydev_table[minor] = NULL;
+       kfree(joydev);
+       return error;
 }
 
+
 static void joydev_disconnect(struct input_handle *handle)
 {
        struct joydev *joydev = handle->private;
-       struct joydev_list *list;
+       struct joydev_client *client;
 
-       class_device_destroy(input_class, MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + joydev->minor));
+       input_unregister_handle(handle);
+
+       sysfs_remove_link(&input_class.subsys.kobj, joydev->name);
+       class_device_destroy(&input_class, MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + joydev->minor));
        joydev->exist = 0;
 
        if (joydev->open) {
                input_close_device(handle);
                wake_up_interruptible(&joydev->wait);
-               list_for_each_entry(list, &joydev->list, node)
-                       kill_fasync(&list->fasync, SIGIO, POLL_HUP);
+               list_for_each_entry(client, &joydev->client_list, node)
+                       kill_fasync(&client->fasync, SIGIO, POLL_HUP);
        } else
                joydev_free(joydev);
 }
 
-static struct input_device_id joydev_blacklist[] = {
+static const struct input_device_id joydev_blacklist[] = {
        {
                .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
                .evbit = { BIT(EV_KEY) },
                .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
-       },      /* Avoid itouchpads, touchscreens and tablets */
-       { },    /* Terminating entry */
+       },      /* Avoid itouchpads, touchscreens and tablets */
+       { }     /* Terminating entry */
 };
 
-static struct input_device_id joydev_ids[] = {
+static const struct input_device_id joydev_ids[] = {
        {
                .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
                .evbit = { BIT(EV_ABS) },
@@ -562,7 +627,7 @@ static struct input_device_id joydev_ids[] = {
                .evbit = { BIT(EV_ABS) },
                .absbit = { BIT(ABS_THROTTLE) },
        },
-       { },    /* Terminating entry */
+       { }     /* Terminating entry */
 };
 
 MODULE_DEVICE_TABLE(input, joydev_ids);
@@ -575,13 +640,12 @@ static struct input_handler joydev_handler = {
        .minor =        JOYDEV_MINOR_BASE,
        .name =         "joydev",
        .id_table =     joydev_ids,
-       .blacklist =    joydev_blacklist,
+       .blacklist =    joydev_blacklist,
 };
 
 static int __init joydev_init(void)
 {
-       input_register_handler(&joydev_handler);
-       return 0;
+       return input_register_handler(&joydev_handler);
 }
 
 static void __exit joydev_exit(void)