2 * Joystick device driver for the input driver suite.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 1999 Colin Van Dyke
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
14 #include <asm/system.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/joystick.h>
18 #include <linux/input.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/slab.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/poll.h>
26 #include <linux/init.h>
27 #include <linux/device.h>
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Joystick device interfaces");
31 MODULE_SUPPORTED_DEVICE("input/js");
32 MODULE_LICENSE("GPL");
34 #define JOYDEV_MINOR_BASE 0
35 #define JOYDEV_MINORS 16
36 #define JOYDEV_BUFFER_SIZE 64
42 struct input_handle handle;
43 wait_queue_head_t wait;
44 struct list_head client_list;
45 spinlock_t client_lock; /* protects client_list */
49 struct js_corr corr[ABS_MAX + 1];
50 struct JS_DATA_SAVE_TYPE glue;
53 __u16 keymap[KEY_MAX - BTN_MISC + 1];
54 __u16 keypam[KEY_MAX - BTN_MISC + 1];
55 __u8 absmap[ABS_MAX + 1];
56 __u8 abspam[ABS_MAX + 1];
57 __s16 abs[ABS_MAX + 1];
60 struct joydev_client {
61 struct js_event buffer[JOYDEV_BUFFER_SIZE];
65 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
66 struct fasync_struct *fasync;
67 struct joydev *joydev;
68 struct list_head node;
71 static struct joydev *joydev_table[JOYDEV_MINORS];
72 static DEFINE_MUTEX(joydev_table_mutex);
74 static int joydev_correct(int value, struct js_corr *corr)
82 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
83 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
84 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
91 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
94 static void joydev_pass_event(struct joydev_client *client,
95 struct js_event *event)
97 struct joydev *joydev = client->joydev;
100 * IRQs already disabled, just acquire the lock
102 spin_lock(&client->buffer_lock);
104 client->buffer[client->head] = *event;
106 if (client->startup == joydev->nabs + joydev->nkey) {
108 client->head &= JOYDEV_BUFFER_SIZE - 1;
109 if (client->tail == client->head)
113 spin_unlock(&client->buffer_lock);
115 kill_fasync(&client->fasync, SIGIO, POLL_IN);
118 static void joydev_event(struct input_handle *handle,
119 unsigned int type, unsigned int code, int value)
121 struct joydev *joydev = handle->private;
122 struct joydev_client *client;
123 struct js_event event;
128 if (code < BTN_MISC || value == 2)
130 event.type = JS_EVENT_BUTTON;
131 event.number = joydev->keymap[code - BTN_MISC];
136 event.type = JS_EVENT_AXIS;
137 event.number = joydev->absmap[code];
138 event.value = joydev_correct(value,
139 &joydev->corr[event.number]);
140 if (event.value == joydev->abs[event.number])
142 joydev->abs[event.number] = event.value;
149 event.time = jiffies_to_msecs(jiffies);
152 list_for_each_entry_rcu(client, &joydev->client_list, node)
153 joydev_pass_event(client, &event);
156 wake_up_interruptible(&joydev->wait);
159 static int joydev_fasync(int fd, struct file *file, int on)
161 struct joydev_client *client = file->private_data;
163 return fasync_helper(fd, file, on, &client->fasync);
166 static void joydev_free(struct device *dev)
168 struct joydev *joydev = container_of(dev, struct joydev, dev);
170 input_put_device(joydev->handle.dev);
174 static void joydev_attach_client(struct joydev *joydev,
175 struct joydev_client *client)
177 spin_lock(&joydev->client_lock);
178 list_add_tail_rcu(&client->node, &joydev->client_list);
179 spin_unlock(&joydev->client_lock);
183 static void joydev_detach_client(struct joydev *joydev,
184 struct joydev_client *client)
186 spin_lock(&joydev->client_lock);
187 list_del_rcu(&client->node);
188 spin_unlock(&joydev->client_lock);
192 static int joydev_open_device(struct joydev *joydev)
196 retval = mutex_lock_interruptible(&joydev->mutex);
202 else if (!joydev->open++) {
203 retval = input_open_device(&joydev->handle);
208 mutex_unlock(&joydev->mutex);
212 static void joydev_close_device(struct joydev *joydev)
214 mutex_lock(&joydev->mutex);
216 if (joydev->exist && !--joydev->open)
217 input_close_device(&joydev->handle);
219 mutex_unlock(&joydev->mutex);
223 * Wake up users waiting for IO so they can disconnect from
226 static void joydev_hangup(struct joydev *joydev)
228 struct joydev_client *client;
230 spin_lock(&joydev->client_lock);
231 list_for_each_entry(client, &joydev->client_list, node)
232 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
233 spin_unlock(&joydev->client_lock);
235 wake_up_interruptible(&joydev->wait);
238 static int joydev_release(struct inode *inode, struct file *file)
240 struct joydev_client *client = file->private_data;
241 struct joydev *joydev = client->joydev;
243 joydev_detach_client(joydev, client);
246 joydev_close_device(joydev);
247 put_device(&joydev->dev);
252 static int joydev_open(struct inode *inode, struct file *file)
254 struct joydev_client *client;
255 struct joydev *joydev;
256 int i = iminor(inode) - JOYDEV_MINOR_BASE;
259 if (i >= JOYDEV_MINORS)
262 error = mutex_lock_interruptible(&joydev_table_mutex);
265 joydev = joydev_table[i];
267 get_device(&joydev->dev);
268 mutex_unlock(&joydev_table_mutex);
273 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
279 spin_lock_init(&client->buffer_lock);
280 client->joydev = joydev;
281 joydev_attach_client(joydev, client);
283 error = joydev_open_device(joydev);
285 goto err_free_client;
287 file->private_data = client;
291 joydev_detach_client(joydev, client);
294 put_device(&joydev->dev);
298 static int joydev_generate_startup_event(struct joydev_client *client,
299 struct input_dev *input,
300 struct js_event *event)
302 struct joydev *joydev = client->joydev;
305 spin_lock_irq(&client->buffer_lock);
307 have_event = client->startup < joydev->nabs + joydev->nkey;
311 event->time = jiffies_to_msecs(jiffies);
312 if (client->startup < joydev->nkey) {
313 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
314 event->number = client->startup;
315 event->value = !!test_bit(joydev->keypam[event->number],
318 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
319 event->number = client->startup - joydev->nkey;
320 event->value = joydev->abs[event->number];
325 spin_unlock_irq(&client->buffer_lock);
330 static int joydev_fetch_next_event(struct joydev_client *client,
331 struct js_event *event)
335 spin_lock_irq(&client->buffer_lock);
337 have_event = client->head != client->tail;
339 *event = client->buffer[client->tail++];
340 client->tail &= JOYDEV_BUFFER_SIZE - 1;
343 spin_unlock_irq(&client->buffer_lock);
349 * Old joystick interface
351 static ssize_t joydev_0x_read(struct joydev_client *client,
352 struct input_dev *input,
355 struct joydev *joydev = client->joydev;
356 struct JS_DATA_TYPE data;
359 spin_lock_irq(&input->event_lock);
364 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
366 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
367 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
368 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
371 * Reset reader's event queue
373 spin_lock(&client->buffer_lock);
375 client->tail = client->head;
376 spin_unlock(&client->buffer_lock);
378 spin_unlock_irq(&input->event_lock);
380 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
383 return sizeof(struct JS_DATA_TYPE);
386 static inline int joydev_data_pending(struct joydev_client *client)
388 struct joydev *joydev = client->joydev;
390 return client->startup < joydev->nabs + joydev->nkey ||
391 client->head != client->tail;
394 static ssize_t joydev_read(struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
397 struct joydev_client *client = file->private_data;
398 struct joydev *joydev = client->joydev;
399 struct input_dev *input = joydev->handle.dev;
400 struct js_event event;
406 if (count < sizeof(struct js_event))
409 if (count == sizeof(struct JS_DATA_TYPE))
410 return joydev_0x_read(client, input, buf);
412 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
415 retval = wait_event_interruptible(joydev->wait,
416 !joydev->exist || joydev_data_pending(client));
423 while (retval + sizeof(struct js_event) <= count &&
424 joydev_generate_startup_event(client, input, &event)) {
426 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
429 retval += sizeof(struct js_event);
432 while (retval + sizeof(struct js_event) <= count &&
433 joydev_fetch_next_event(client, &event)) {
435 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
438 retval += sizeof(struct js_event);
444 /* No kernel lock - fine */
445 static unsigned int joydev_poll(struct file *file, poll_table *wait)
447 struct joydev_client *client = file->private_data;
448 struct joydev *joydev = client->joydev;
450 poll_wait(file, &joydev->wait, wait);
451 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
452 (joydev->exist ? 0 : (POLLHUP | POLLERR));
455 static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
456 void __user *argp, size_t len)
462 len = min(len, sizeof(joydev->abspam));
464 /* Validate the map. */
465 abspam = kmalloc(len, GFP_KERNEL);
469 if (copy_from_user(abspam, argp, len)) {
474 for (i = 0; i < joydev->nabs; i++) {
475 if (abspam[i] > ABS_MAX) {
481 memcpy(joydev->abspam, abspam, len);
488 static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
489 void __user *argp, size_t len)
495 len = min(len, sizeof(joydev->keypam));
497 /* Validate the map. */
498 keypam = kmalloc(len, GFP_KERNEL);
502 if (copy_from_user(keypam, argp, len)) {
507 for (i = 0; i < joydev->nkey; i++) {
508 if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
514 memcpy(joydev->keypam, keypam, len);
516 for (i = 0; i < joydev->nkey; i++)
517 joydev->keymap[keypam[i] - BTN_MISC] = i;
525 static int joydev_ioctl_common(struct joydev *joydev,
526 unsigned int cmd, void __user *argp)
528 struct input_dev *dev = joydev->handle.dev;
533 /* Process fixed-sized commands. */
537 return copy_from_user(&joydev->glue.JS_CORR, argp,
538 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
541 return copy_to_user(argp, &joydev->glue.JS_CORR,
542 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
545 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
548 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
551 return put_user(JS_VERSION, (__u32 __user *) argp);
554 return put_user(joydev->nabs, (__u8 __user *) argp);
557 return put_user(joydev->nkey, (__u8 __user *) argp);
560 if (copy_from_user(joydev->corr, argp,
561 sizeof(joydev->corr[0]) * joydev->nabs))
564 for (i = 0; i < joydev->nabs; i++) {
565 j = joydev->abspam[i];
566 joydev->abs[i] = joydev_correct(dev->abs[j],
572 return copy_to_user(argp, joydev->corr,
573 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
578 * Process variable-sized commands (the axis and button map commands
579 * are considered variable-sized to decouple them from the values of
580 * ABS_MAX and KEY_MAX).
582 switch (cmd & ~IOCSIZE_MASK) {
584 case (JSIOCSAXMAP & ~IOCSIZE_MASK):
585 return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
587 case (JSIOCGAXMAP & ~IOCSIZE_MASK):
588 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
589 return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
591 case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
592 return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
594 case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
595 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
596 return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
603 len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
604 return copy_to_user(argp, name, len) ? -EFAULT : len;
611 static long joydev_compat_ioctl(struct file *file,
612 unsigned int cmd, unsigned long arg)
614 struct joydev_client *client = file->private_data;
615 struct joydev *joydev = client->joydev;
616 void __user *argp = (void __user *)arg;
618 struct JS_DATA_SAVE_TYPE_32 ds32;
621 retval = mutex_lock_interruptible(&joydev->mutex);
625 if (!joydev->exist) {
632 case JS_SET_TIMELIMIT:
633 retval = get_user(tmp32, (s32 __user *) arg);
635 joydev->glue.JS_TIMELIMIT = tmp32;
638 case JS_GET_TIMELIMIT:
639 tmp32 = joydev->glue.JS_TIMELIMIT;
640 retval = put_user(tmp32, (s32 __user *) arg);
644 retval = copy_from_user(&ds32, argp,
645 sizeof(ds32)) ? -EFAULT : 0;
647 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
648 joydev->glue.BUSY = ds32.BUSY;
649 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
650 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
651 joydev->glue.JS_SAVE = ds32.JS_SAVE;
652 joydev->glue.JS_CORR = ds32.JS_CORR;
657 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
658 ds32.BUSY = joydev->glue.BUSY;
659 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
660 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
661 ds32.JS_SAVE = joydev->glue.JS_SAVE;
662 ds32.JS_CORR = joydev->glue.JS_CORR;
664 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
668 retval = joydev_ioctl_common(joydev, cmd, argp);
673 mutex_unlock(&joydev->mutex);
676 #endif /* CONFIG_COMPAT */
678 static long joydev_ioctl(struct file *file,
679 unsigned int cmd, unsigned long arg)
681 struct joydev_client *client = file->private_data;
682 struct joydev *joydev = client->joydev;
683 void __user *argp = (void __user *)arg;
686 retval = mutex_lock_interruptible(&joydev->mutex);
690 if (!joydev->exist) {
697 case JS_SET_TIMELIMIT:
698 retval = get_user(joydev->glue.JS_TIMELIMIT,
699 (long __user *) arg);
702 case JS_GET_TIMELIMIT:
703 retval = put_user(joydev->glue.JS_TIMELIMIT,
704 (long __user *) arg);
708 retval = copy_from_user(&joydev->glue, argp,
709 sizeof(joydev->glue)) ? -EFAULT: 0;
713 retval = copy_to_user(argp, &joydev->glue,
714 sizeof(joydev->glue)) ? -EFAULT : 0;
718 retval = joydev_ioctl_common(joydev, cmd, argp);
722 mutex_unlock(&joydev->mutex);
726 static const struct file_operations joydev_fops = {
727 .owner = THIS_MODULE,
731 .release = joydev_release,
732 .unlocked_ioctl = joydev_ioctl,
734 .compat_ioctl = joydev_compat_ioctl,
736 .fasync = joydev_fasync,
739 static int joydev_install_chrdev(struct joydev *joydev)
741 joydev_table[joydev->minor] = joydev;
745 static void joydev_remove_chrdev(struct joydev *joydev)
747 mutex_lock(&joydev_table_mutex);
748 joydev_table[joydev->minor] = NULL;
749 mutex_unlock(&joydev_table_mutex);
753 * Mark device non-existant. This disables writes, ioctls and
754 * prevents new users from opening the device. Already posted
755 * blocking reads will stay, however new ones will fail.
757 static void joydev_mark_dead(struct joydev *joydev)
759 mutex_lock(&joydev->mutex);
761 mutex_unlock(&joydev->mutex);
764 static void joydev_cleanup(struct joydev *joydev)
766 struct input_handle *handle = &joydev->handle;
768 joydev_mark_dead(joydev);
769 joydev_hangup(joydev);
770 joydev_remove_chrdev(joydev);
772 /* joydev is marked dead so noone else accesses joydev->open */
774 input_close_device(handle);
777 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
778 const struct input_device_id *id)
780 struct joydev *joydev;
784 for (minor = 0; minor < JOYDEV_MINORS; minor++)
785 if (!joydev_table[minor])
788 if (minor == JOYDEV_MINORS) {
789 printk(KERN_ERR "joydev: no more free joydev devices\n");
793 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
797 INIT_LIST_HEAD(&joydev->client_list);
798 spin_lock_init(&joydev->client_lock);
799 mutex_init(&joydev->mutex);
800 init_waitqueue_head(&joydev->wait);
802 dev_set_name(&joydev->dev, "js%d", minor);
804 joydev->minor = minor;
807 joydev->handle.dev = input_get_device(dev);
808 joydev->handle.name = dev_name(&joydev->dev);
809 joydev->handle.handler = handler;
810 joydev->handle.private = joydev;
812 for (i = 0; i < ABS_MAX + 1; i++)
813 if (test_bit(i, dev->absbit)) {
814 joydev->absmap[i] = joydev->nabs;
815 joydev->abspam[joydev->nabs] = i;
819 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
820 if (test_bit(i + BTN_MISC, dev->keybit)) {
821 joydev->keymap[i] = joydev->nkey;
822 joydev->keypam[joydev->nkey] = i + BTN_MISC;
826 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
827 if (test_bit(i + BTN_MISC, dev->keybit)) {
828 joydev->keymap[i] = joydev->nkey;
829 joydev->keypam[joydev->nkey] = i + BTN_MISC;
833 for (i = 0; i < joydev->nabs; i++) {
834 j = joydev->abspam[i];
835 if (dev->absmax[j] == dev->absmin[j]) {
836 joydev->corr[i].type = JS_CORR_NONE;
837 joydev->abs[i] = dev->abs[j];
840 joydev->corr[i].type = JS_CORR_BROKEN;
841 joydev->corr[i].prec = dev->absfuzz[j];
842 joydev->corr[i].coef[0] =
843 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
844 joydev->corr[i].coef[1] =
845 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
847 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
849 joydev->corr[i].coef[2] = (1 << 29) / t;
850 joydev->corr[i].coef[3] = (1 << 29) / t;
852 joydev->abs[i] = joydev_correct(dev->abs[j],
857 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
858 joydev->dev.class = &input_class;
859 joydev->dev.parent = &dev->dev;
860 joydev->dev.release = joydev_free;
861 device_initialize(&joydev->dev);
863 error = input_register_handle(&joydev->handle);
865 goto err_free_joydev;
867 error = joydev_install_chrdev(joydev);
869 goto err_unregister_handle;
871 error = device_add(&joydev->dev);
873 goto err_cleanup_joydev;
878 joydev_cleanup(joydev);
879 err_unregister_handle:
880 input_unregister_handle(&joydev->handle);
882 put_device(&joydev->dev);
886 static void joydev_disconnect(struct input_handle *handle)
888 struct joydev *joydev = handle->private;
890 device_del(&joydev->dev);
891 joydev_cleanup(joydev);
892 input_unregister_handle(handle);
893 put_device(&joydev->dev);
896 static const struct input_device_id joydev_blacklist[] = {
898 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
899 INPUT_DEVICE_ID_MATCH_KEYBIT,
900 .evbit = { BIT_MASK(EV_KEY) },
901 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
902 }, /* Avoid itouchpads and touchscreens */
904 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
905 INPUT_DEVICE_ID_MATCH_KEYBIT,
906 .evbit = { BIT_MASK(EV_KEY) },
907 .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) },
908 }, /* Avoid tablets, digitisers and similar devices */
909 { } /* Terminating entry */
912 static const struct input_device_id joydev_ids[] = {
914 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
915 INPUT_DEVICE_ID_MATCH_ABSBIT,
916 .evbit = { BIT_MASK(EV_ABS) },
917 .absbit = { BIT_MASK(ABS_X) },
920 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
921 INPUT_DEVICE_ID_MATCH_ABSBIT,
922 .evbit = { BIT_MASK(EV_ABS) },
923 .absbit = { BIT_MASK(ABS_WHEEL) },
926 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
927 INPUT_DEVICE_ID_MATCH_ABSBIT,
928 .evbit = { BIT_MASK(EV_ABS) },
929 .absbit = { BIT_MASK(ABS_THROTTLE) },
931 { } /* Terminating entry */
934 MODULE_DEVICE_TABLE(input, joydev_ids);
936 static struct input_handler joydev_handler = {
937 .event = joydev_event,
938 .connect = joydev_connect,
939 .disconnect = joydev_disconnect,
940 .fops = &joydev_fops,
941 .minor = JOYDEV_MINOR_BASE,
943 .id_table = joydev_ids,
944 .blacklist = joydev_blacklist,
947 static int __init joydev_init(void)
949 return input_register_handler(&joydev_handler);
952 static void __exit joydev_exit(void)
954 input_unregister_handler(&joydev_handler);
957 module_init(joydev_init);
958 module_exit(joydev_exit);