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
43 struct input_handle handle;
44 wait_queue_head_t wait;
45 struct list_head client_list;
46 spinlock_t client_lock; /* protects client_list */
50 struct js_corr corr[ABS_MAX + 1];
51 struct JS_DATA_SAVE_TYPE glue;
54 __u16 keymap[KEY_MAX - BTN_MISC + 1];
55 __u16 keypam[KEY_MAX - BTN_MISC + 1];
56 __u8 absmap[ABS_MAX + 1];
57 __u8 abspam[ABS_MAX + 1];
58 __s16 abs[ABS_MAX + 1];
61 struct joydev_client {
62 struct js_event buffer[JOYDEV_BUFFER_SIZE];
66 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
67 struct fasync_struct *fasync;
68 struct joydev *joydev;
69 struct list_head node;
72 static struct joydev *joydev_table[JOYDEV_MINORS];
73 static DEFINE_MUTEX(joydev_table_mutex);
75 static int joydev_correct(int value, struct js_corr *corr)
83 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
84 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
85 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
92 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
95 static void joydev_pass_event(struct joydev_client *client,
96 struct js_event *event)
98 struct joydev *joydev = client->joydev;
101 * IRQs already disabled, just acquire the lock
103 spin_lock(&client->buffer_lock);
105 client->buffer[client->head] = *event;
107 if (client->startup == joydev->nabs + joydev->nkey) {
109 client->head &= JOYDEV_BUFFER_SIZE - 1;
110 if (client->tail == client->head)
114 spin_unlock(&client->buffer_lock);
116 kill_fasync(&client->fasync, SIGIO, POLL_IN);
119 static void joydev_event(struct input_handle *handle,
120 unsigned int type, unsigned int code, int value)
122 struct joydev *joydev = handle->private;
123 struct joydev_client *client;
124 struct js_event event;
129 if (code < BTN_MISC || value == 2)
131 event.type = JS_EVENT_BUTTON;
132 event.number = joydev->keymap[code - BTN_MISC];
137 event.type = JS_EVENT_AXIS;
138 event.number = joydev->absmap[code];
139 event.value = joydev_correct(value,
140 &joydev->corr[event.number]);
141 if (event.value == joydev->abs[event.number])
143 joydev->abs[event.number] = event.value;
150 event.time = jiffies_to_msecs(jiffies);
152 list_for_each_entry_rcu(client, &joydev->client_list, node)
153 joydev_pass_event(client, &event);
155 wake_up_interruptible(&joydev->wait);
158 static int joydev_fasync(int fd, struct file *file, int on)
161 struct joydev_client *client = file->private_data;
163 retval = fasync_helper(fd, file, on, &client->fasync);
165 return retval < 0 ? retval : 0;
168 static void joydev_free(struct device *dev)
170 struct joydev *joydev = container_of(dev, struct joydev, dev);
175 static void joydev_attach_client(struct joydev *joydev,
176 struct joydev_client *client)
178 spin_lock(&joydev->client_lock);
179 list_add_tail_rcu(&client->node, &joydev->client_list);
180 spin_unlock(&joydev->client_lock);
182 * We don't use synchronize_rcu() here because read-side
183 * critical section is protected by a spinlock (dev->event_lock)
184 * instead of rcu_read_lock().
189 static void joydev_detach_client(struct joydev *joydev,
190 struct joydev_client *client)
192 spin_lock(&joydev->client_lock);
193 list_del_rcu(&client->node);
194 spin_unlock(&joydev->client_lock);
198 static int joydev_open_device(struct joydev *joydev)
202 retval = mutex_lock_interruptible(&joydev->mutex);
208 else if (!joydev->open++) {
209 retval = input_open_device(&joydev->handle);
214 mutex_unlock(&joydev->mutex);
218 static void joydev_close_device(struct joydev *joydev)
220 mutex_lock(&joydev->mutex);
222 if (joydev->exist && !--joydev->open)
223 input_close_device(&joydev->handle);
225 mutex_unlock(&joydev->mutex);
229 * Wake up users waiting for IO so they can disconnect from
232 static void joydev_hangup(struct joydev *joydev)
234 struct joydev_client *client;
236 spin_lock(&joydev->client_lock);
237 list_for_each_entry(client, &joydev->client_list, node)
238 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
239 spin_unlock(&joydev->client_lock);
241 wake_up_interruptible(&joydev->wait);
244 static int joydev_release(struct inode *inode, struct file *file)
246 struct joydev_client *client = file->private_data;
247 struct joydev *joydev = client->joydev;
249 joydev_fasync(-1, file, 0);
250 joydev_detach_client(joydev, client);
253 joydev_close_device(joydev);
254 put_device(&joydev->dev);
259 static int joydev_open(struct inode *inode, struct file *file)
261 struct joydev_client *client;
262 struct joydev *joydev;
263 int i = iminor(inode) - JOYDEV_MINOR_BASE;
266 if (i >= JOYDEV_MINORS)
269 error = mutex_lock_interruptible(&joydev_table_mutex);
272 joydev = joydev_table[i];
274 get_device(&joydev->dev);
275 mutex_unlock(&joydev_table_mutex);
280 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
286 spin_lock_init(&client->buffer_lock);
287 client->joydev = joydev;
288 joydev_attach_client(joydev, client);
290 error = joydev_open_device(joydev);
292 goto err_free_client;
294 file->private_data = client;
298 joydev_detach_client(joydev, client);
301 put_device(&joydev->dev);
305 static int joydev_generate_startup_event(struct joydev_client *client,
306 struct input_dev *input,
307 struct js_event *event)
309 struct joydev *joydev = client->joydev;
312 spin_lock_irq(&client->buffer_lock);
314 have_event = client->startup < joydev->nabs + joydev->nkey;
318 event->time = jiffies_to_msecs(jiffies);
319 if (client->startup < joydev->nkey) {
320 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
321 event->number = client->startup;
322 event->value = !!test_bit(joydev->keypam[event->number],
325 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
326 event->number = client->startup - joydev->nkey;
327 event->value = joydev->abs[event->number];
332 spin_unlock_irq(&client->buffer_lock);
337 static int joydev_fetch_next_event(struct joydev_client *client,
338 struct js_event *event)
342 spin_lock_irq(&client->buffer_lock);
344 have_event = client->head != client->tail;
346 *event = client->buffer[client->tail++];
347 client->tail &= JOYDEV_BUFFER_SIZE - 1;
350 spin_unlock_irq(&client->buffer_lock);
356 * Old joystick interface
358 static ssize_t joydev_0x_read(struct joydev_client *client,
359 struct input_dev *input,
362 struct joydev *joydev = client->joydev;
363 struct JS_DATA_TYPE data;
366 spin_lock_irq(&input->event_lock);
371 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
373 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
374 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
375 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
378 * Reset reader's event queue
380 spin_lock(&client->buffer_lock);
382 client->tail = client->head;
383 spin_unlock(&client->buffer_lock);
385 spin_unlock_irq(&input->event_lock);
387 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
390 return sizeof(struct JS_DATA_TYPE);
393 static inline int joydev_data_pending(struct joydev_client *client)
395 struct joydev *joydev = client->joydev;
397 return client->startup < joydev->nabs + joydev->nkey ||
398 client->head != client->tail;
401 static ssize_t joydev_read(struct file *file, char __user *buf,
402 size_t count, loff_t *ppos)
404 struct joydev_client *client = file->private_data;
405 struct joydev *joydev = client->joydev;
406 struct input_dev *input = joydev->handle.dev;
407 struct js_event event;
413 if (count < sizeof(struct js_event))
416 if (count == sizeof(struct JS_DATA_TYPE))
417 return joydev_0x_read(client, input, buf);
419 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
422 retval = wait_event_interruptible(joydev->wait,
423 !joydev->exist || joydev_data_pending(client));
430 while (retval + sizeof(struct js_event) <= count &&
431 joydev_generate_startup_event(client, input, &event)) {
433 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
436 retval += sizeof(struct js_event);
439 while (retval + sizeof(struct js_event) <= count &&
440 joydev_fetch_next_event(client, &event)) {
442 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
445 retval += sizeof(struct js_event);
451 /* No kernel lock - fine */
452 static unsigned int joydev_poll(struct file *file, poll_table *wait)
454 struct joydev_client *client = file->private_data;
455 struct joydev *joydev = client->joydev;
457 poll_wait(file, &joydev->wait, wait);
458 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
459 (joydev->exist ? 0 : (POLLHUP | POLLERR));
462 static int joydev_ioctl_common(struct joydev *joydev,
463 unsigned int cmd, void __user *argp)
465 struct input_dev *dev = joydev->handle.dev;
471 return copy_from_user(&joydev->glue.JS_CORR, argp,
472 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
475 return copy_to_user(argp, &joydev->glue.JS_CORR,
476 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
479 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
482 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
485 return put_user(JS_VERSION, (__u32 __user *) argp);
488 return put_user(joydev->nabs, (__u8 __user *) argp);
491 return put_user(joydev->nkey, (__u8 __user *) argp);
494 if (copy_from_user(joydev->corr, argp,
495 sizeof(joydev->corr[0]) * joydev->nabs))
498 for (i = 0; i < joydev->nabs; i++) {
499 j = joydev->abspam[i];
500 joydev->abs[i] = joydev_correct(dev->abs[j],
506 return copy_to_user(argp, joydev->corr,
507 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
510 if (copy_from_user(joydev->abspam, argp,
511 sizeof(__u8) * (ABS_MAX + 1)))
514 for (i = 0; i < joydev->nabs; i++) {
515 if (joydev->abspam[i] > ABS_MAX)
517 joydev->absmap[joydev->abspam[i]] = i;
522 return copy_to_user(argp, joydev->abspam,
523 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
526 if (copy_from_user(joydev->keypam, argp,
527 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
530 for (i = 0; i < joydev->nkey; i++) {
531 if (joydev->keypam[i] > KEY_MAX ||
532 joydev->keypam[i] < BTN_MISC)
534 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
540 return copy_to_user(argp, joydev->keypam,
541 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
544 if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
548 len = strlen(dev->name) + 1;
549 if (len > _IOC_SIZE(cmd))
550 len = _IOC_SIZE(cmd);
551 if (copy_to_user(argp, dev->name, len))
560 static long joydev_compat_ioctl(struct file *file,
561 unsigned int cmd, unsigned long arg)
563 struct joydev_client *client = file->private_data;
564 struct joydev *joydev = client->joydev;
565 void __user *argp = (void __user *)arg;
567 struct JS_DATA_SAVE_TYPE_32 ds32;
570 retval = mutex_lock_interruptible(&joydev->mutex);
574 if (!joydev->exist) {
581 case JS_SET_TIMELIMIT:
582 retval = get_user(tmp32, (s32 __user *) arg);
584 joydev->glue.JS_TIMELIMIT = tmp32;
587 case JS_GET_TIMELIMIT:
588 tmp32 = joydev->glue.JS_TIMELIMIT;
589 retval = put_user(tmp32, (s32 __user *) arg);
593 retval = copy_from_user(&ds32, argp,
594 sizeof(ds32)) ? -EFAULT : 0;
596 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
597 joydev->glue.BUSY = ds32.BUSY;
598 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
599 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
600 joydev->glue.JS_SAVE = ds32.JS_SAVE;
601 joydev->glue.JS_CORR = ds32.JS_CORR;
606 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
607 ds32.BUSY = joydev->glue.BUSY;
608 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
609 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
610 ds32.JS_SAVE = joydev->glue.JS_SAVE;
611 ds32.JS_CORR = joydev->glue.JS_CORR;
613 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
617 retval = joydev_ioctl_common(joydev, cmd, argp);
622 mutex_unlock(&joydev->mutex);
625 #endif /* CONFIG_COMPAT */
627 static long joydev_ioctl(struct file *file,
628 unsigned int cmd, unsigned long arg)
630 struct joydev_client *client = file->private_data;
631 struct joydev *joydev = client->joydev;
632 void __user *argp = (void __user *)arg;
635 retval = mutex_lock_interruptible(&joydev->mutex);
639 if (!joydev->exist) {
646 case JS_SET_TIMELIMIT:
647 retval = get_user(joydev->glue.JS_TIMELIMIT,
648 (long __user *) arg);
651 case JS_GET_TIMELIMIT:
652 retval = put_user(joydev->glue.JS_TIMELIMIT,
653 (long __user *) arg);
657 retval = copy_from_user(&joydev->glue, argp,
658 sizeof(joydev->glue)) ? -EFAULT: 0;
662 retval = copy_to_user(argp, &joydev->glue,
663 sizeof(joydev->glue)) ? -EFAULT : 0;
667 retval = joydev_ioctl_common(joydev, cmd, argp);
671 mutex_unlock(&joydev->mutex);
675 static const struct file_operations joydev_fops = {
676 .owner = THIS_MODULE,
680 .release = joydev_release,
681 .unlocked_ioctl = joydev_ioctl,
683 .compat_ioctl = joydev_compat_ioctl,
685 .fasync = joydev_fasync,
688 static int joydev_install_chrdev(struct joydev *joydev)
690 joydev_table[joydev->minor] = joydev;
694 static void joydev_remove_chrdev(struct joydev *joydev)
696 mutex_lock(&joydev_table_mutex);
697 joydev_table[joydev->minor] = NULL;
698 mutex_unlock(&joydev_table_mutex);
702 * Mark device non-existant. This disables writes, ioctls and
703 * prevents new users from opening the device. Already posted
704 * blocking reads will stay, however new ones will fail.
706 static void joydev_mark_dead(struct joydev *joydev)
708 mutex_lock(&joydev->mutex);
710 mutex_unlock(&joydev->mutex);
713 static void joydev_cleanup(struct joydev *joydev)
715 struct input_handle *handle = &joydev->handle;
717 joydev_mark_dead(joydev);
718 joydev_hangup(joydev);
719 joydev_remove_chrdev(joydev);
721 /* joydev is marked dead so noone else accesses joydev->open */
723 input_close_device(handle);
726 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
727 const struct input_device_id *id)
729 struct joydev *joydev;
733 for (minor = 0; minor < JOYDEV_MINORS; minor++)
734 if (!joydev_table[minor])
737 if (minor == JOYDEV_MINORS) {
738 printk(KERN_ERR "joydev: no more free joydev devices\n");
742 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
746 INIT_LIST_HEAD(&joydev->client_list);
747 spin_lock_init(&joydev->client_lock);
748 mutex_init(&joydev->mutex);
749 init_waitqueue_head(&joydev->wait);
751 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
753 joydev->minor = minor;
756 joydev->handle.dev = dev;
757 joydev->handle.name = joydev->name;
758 joydev->handle.handler = handler;
759 joydev->handle.private = joydev;
761 for (i = 0; i < ABS_MAX + 1; i++)
762 if (test_bit(i, dev->absbit)) {
763 joydev->absmap[i] = joydev->nabs;
764 joydev->abspam[joydev->nabs] = i;
768 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
769 if (test_bit(i + BTN_MISC, dev->keybit)) {
770 joydev->keymap[i] = joydev->nkey;
771 joydev->keypam[joydev->nkey] = i + BTN_MISC;
775 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
776 if (test_bit(i + BTN_MISC, dev->keybit)) {
777 joydev->keymap[i] = joydev->nkey;
778 joydev->keypam[joydev->nkey] = i + BTN_MISC;
782 for (i = 0; i < joydev->nabs; i++) {
783 j = joydev->abspam[i];
784 if (dev->absmax[j] == dev->absmin[j]) {
785 joydev->corr[i].type = JS_CORR_NONE;
786 joydev->abs[i] = dev->abs[j];
789 joydev->corr[i].type = JS_CORR_BROKEN;
790 joydev->corr[i].prec = dev->absfuzz[j];
791 joydev->corr[i].coef[0] =
792 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
793 joydev->corr[i].coef[1] =
794 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
796 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
798 joydev->corr[i].coef[2] = (1 << 29) / t;
799 joydev->corr[i].coef[3] = (1 << 29) / t;
801 joydev->abs[i] = joydev_correct(dev->abs[j],
806 strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
807 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
808 joydev->dev.class = &input_class;
809 joydev->dev.parent = &dev->dev;
810 joydev->dev.release = joydev_free;
811 device_initialize(&joydev->dev);
813 error = input_register_handle(&joydev->handle);
815 goto err_free_joydev;
817 error = joydev_install_chrdev(joydev);
819 goto err_unregister_handle;
821 error = device_add(&joydev->dev);
823 goto err_cleanup_joydev;
828 joydev_cleanup(joydev);
829 err_unregister_handle:
830 input_unregister_handle(&joydev->handle);
832 put_device(&joydev->dev);
836 static void joydev_disconnect(struct input_handle *handle)
838 struct joydev *joydev = handle->private;
840 device_del(&joydev->dev);
841 joydev_cleanup(joydev);
842 input_unregister_handle(handle);
843 put_device(&joydev->dev);
846 static const struct input_device_id joydev_blacklist[] = {
848 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
849 INPUT_DEVICE_ID_MATCH_KEYBIT,
850 .evbit = { BIT(EV_KEY) },
851 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
852 }, /* Avoid itouchpads, touchscreens and tablets */
853 { } /* Terminating entry */
856 static const struct input_device_id joydev_ids[] = {
858 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
859 INPUT_DEVICE_ID_MATCH_ABSBIT,
860 .evbit = { BIT(EV_ABS) },
861 .absbit = { BIT(ABS_X) },
864 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
865 INPUT_DEVICE_ID_MATCH_ABSBIT,
866 .evbit = { BIT(EV_ABS) },
867 .absbit = { BIT(ABS_WHEEL) },
870 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871 INPUT_DEVICE_ID_MATCH_ABSBIT,
872 .evbit = { BIT(EV_ABS) },
873 .absbit = { BIT(ABS_THROTTLE) },
875 { } /* Terminating entry */
878 MODULE_DEVICE_TABLE(input, joydev_ids);
880 static struct input_handler joydev_handler = {
881 .event = joydev_event,
882 .connect = joydev_connect,
883 .disconnect = joydev_disconnect,
884 .fops = &joydev_fops,
885 .minor = JOYDEV_MINOR_BASE,
887 .id_table = joydev_ids,
888 .blacklist = joydev_blacklist,
891 static int __init joydev_init(void)
893 return input_register_handler(&joydev_handler);
896 static void __exit joydev_exit(void)
898 input_unregister_handler(&joydev_handler);
901 module_init(joydev_init);
902 module_exit(joydev_exit);