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);
153 list_for_each_entry_rcu(client, &joydev->client_list, node)
154 joydev_pass_event(client, &event);
157 wake_up_interruptible(&joydev->wait);
160 static int joydev_fasync(int fd, struct file *file, int on)
163 struct joydev_client *client = file->private_data;
165 retval = fasync_helper(fd, file, on, &client->fasync);
167 return retval < 0 ? retval : 0;
170 static void joydev_free(struct device *dev)
172 struct joydev *joydev = container_of(dev, struct joydev, dev);
174 input_put_device(joydev->handle.dev);
178 static void joydev_attach_client(struct joydev *joydev,
179 struct joydev_client *client)
181 spin_lock(&joydev->client_lock);
182 list_add_tail_rcu(&client->node, &joydev->client_list);
183 spin_unlock(&joydev->client_lock);
187 static void joydev_detach_client(struct joydev *joydev,
188 struct joydev_client *client)
190 spin_lock(&joydev->client_lock);
191 list_del_rcu(&client->node);
192 spin_unlock(&joydev->client_lock);
196 static int joydev_open_device(struct joydev *joydev)
200 retval = mutex_lock_interruptible(&joydev->mutex);
206 else if (!joydev->open++) {
207 retval = input_open_device(&joydev->handle);
212 mutex_unlock(&joydev->mutex);
216 static void joydev_close_device(struct joydev *joydev)
218 mutex_lock(&joydev->mutex);
220 if (joydev->exist && !--joydev->open)
221 input_close_device(&joydev->handle);
223 mutex_unlock(&joydev->mutex);
227 * Wake up users waiting for IO so they can disconnect from
230 static void joydev_hangup(struct joydev *joydev)
232 struct joydev_client *client;
234 spin_lock(&joydev->client_lock);
235 list_for_each_entry(client, &joydev->client_list, node)
236 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
237 spin_unlock(&joydev->client_lock);
239 wake_up_interruptible(&joydev->wait);
242 static int joydev_release(struct inode *inode, struct file *file)
244 struct joydev_client *client = file->private_data;
245 struct joydev *joydev = client->joydev;
247 joydev_detach_client(joydev, client);
250 joydev_close_device(joydev);
251 put_device(&joydev->dev);
256 static int joydev_open(struct inode *inode, struct file *file)
258 struct joydev_client *client;
259 struct joydev *joydev;
260 int i = iminor(inode) - JOYDEV_MINOR_BASE;
263 if (i >= JOYDEV_MINORS)
266 error = mutex_lock_interruptible(&joydev_table_mutex);
269 joydev = joydev_table[i];
271 get_device(&joydev->dev);
272 mutex_unlock(&joydev_table_mutex);
277 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
283 spin_lock_init(&client->buffer_lock);
284 client->joydev = joydev;
285 joydev_attach_client(joydev, client);
287 error = joydev_open_device(joydev);
289 goto err_free_client;
291 file->private_data = client;
295 joydev_detach_client(joydev, client);
298 put_device(&joydev->dev);
302 static int joydev_generate_startup_event(struct joydev_client *client,
303 struct input_dev *input,
304 struct js_event *event)
306 struct joydev *joydev = client->joydev;
309 spin_lock_irq(&client->buffer_lock);
311 have_event = client->startup < joydev->nabs + joydev->nkey;
315 event->time = jiffies_to_msecs(jiffies);
316 if (client->startup < joydev->nkey) {
317 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
318 event->number = client->startup;
319 event->value = !!test_bit(joydev->keypam[event->number],
322 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
323 event->number = client->startup - joydev->nkey;
324 event->value = joydev->abs[event->number];
329 spin_unlock_irq(&client->buffer_lock);
334 static int joydev_fetch_next_event(struct joydev_client *client,
335 struct js_event *event)
339 spin_lock_irq(&client->buffer_lock);
341 have_event = client->head != client->tail;
343 *event = client->buffer[client->tail++];
344 client->tail &= JOYDEV_BUFFER_SIZE - 1;
347 spin_unlock_irq(&client->buffer_lock);
353 * Old joystick interface
355 static ssize_t joydev_0x_read(struct joydev_client *client,
356 struct input_dev *input,
359 struct joydev *joydev = client->joydev;
360 struct JS_DATA_TYPE data;
363 spin_lock_irq(&input->event_lock);
368 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
370 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
371 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
372 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
375 * Reset reader's event queue
377 spin_lock(&client->buffer_lock);
379 client->tail = client->head;
380 spin_unlock(&client->buffer_lock);
382 spin_unlock_irq(&input->event_lock);
384 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
387 return sizeof(struct JS_DATA_TYPE);
390 static inline int joydev_data_pending(struct joydev_client *client)
392 struct joydev *joydev = client->joydev;
394 return client->startup < joydev->nabs + joydev->nkey ||
395 client->head != client->tail;
398 static ssize_t joydev_read(struct file *file, char __user *buf,
399 size_t count, loff_t *ppos)
401 struct joydev_client *client = file->private_data;
402 struct joydev *joydev = client->joydev;
403 struct input_dev *input = joydev->handle.dev;
404 struct js_event event;
410 if (count < sizeof(struct js_event))
413 if (count == sizeof(struct JS_DATA_TYPE))
414 return joydev_0x_read(client, input, buf);
416 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
419 retval = wait_event_interruptible(joydev->wait,
420 !joydev->exist || joydev_data_pending(client));
427 while (retval + sizeof(struct js_event) <= count &&
428 joydev_generate_startup_event(client, input, &event)) {
430 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
433 retval += sizeof(struct js_event);
436 while (retval + sizeof(struct js_event) <= count &&
437 joydev_fetch_next_event(client, &event)) {
439 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
442 retval += sizeof(struct js_event);
448 /* No kernel lock - fine */
449 static unsigned int joydev_poll(struct file *file, poll_table *wait)
451 struct joydev_client *client = file->private_data;
452 struct joydev *joydev = client->joydev;
454 poll_wait(file, &joydev->wait, wait);
455 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
456 (joydev->exist ? 0 : (POLLHUP | POLLERR));
459 static int joydev_ioctl_common(struct joydev *joydev,
460 unsigned int cmd, void __user *argp)
462 struct input_dev *dev = joydev->handle.dev;
468 return copy_from_user(&joydev->glue.JS_CORR, argp,
469 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
472 return copy_to_user(argp, &joydev->glue.JS_CORR,
473 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
476 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
479 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
482 return put_user(JS_VERSION, (__u32 __user *) argp);
485 return put_user(joydev->nabs, (__u8 __user *) argp);
488 return put_user(joydev->nkey, (__u8 __user *) argp);
491 if (copy_from_user(joydev->corr, argp,
492 sizeof(joydev->corr[0]) * joydev->nabs))
495 for (i = 0; i < joydev->nabs; i++) {
496 j = joydev->abspam[i];
497 joydev->abs[i] = joydev_correct(dev->abs[j],
503 return copy_to_user(argp, joydev->corr,
504 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
507 if (copy_from_user(joydev->abspam, argp,
508 sizeof(__u8) * (ABS_MAX + 1)))
511 for (i = 0; i < joydev->nabs; i++) {
512 if (joydev->abspam[i] > ABS_MAX)
514 joydev->absmap[joydev->abspam[i]] = i;
519 return copy_to_user(argp, joydev->abspam,
520 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
523 if (copy_from_user(joydev->keypam, argp,
524 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
527 for (i = 0; i < joydev->nkey; i++) {
528 if (joydev->keypam[i] > KEY_MAX ||
529 joydev->keypam[i] < BTN_MISC)
531 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
537 return copy_to_user(argp, joydev->keypam,
538 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
541 if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
545 len = strlen(dev->name) + 1;
546 if (len > _IOC_SIZE(cmd))
547 len = _IOC_SIZE(cmd);
548 if (copy_to_user(argp, dev->name, len))
557 static long joydev_compat_ioctl(struct file *file,
558 unsigned int cmd, unsigned long arg)
560 struct joydev_client *client = file->private_data;
561 struct joydev *joydev = client->joydev;
562 void __user *argp = (void __user *)arg;
564 struct JS_DATA_SAVE_TYPE_32 ds32;
567 retval = mutex_lock_interruptible(&joydev->mutex);
571 if (!joydev->exist) {
578 case JS_SET_TIMELIMIT:
579 retval = get_user(tmp32, (s32 __user *) arg);
581 joydev->glue.JS_TIMELIMIT = tmp32;
584 case JS_GET_TIMELIMIT:
585 tmp32 = joydev->glue.JS_TIMELIMIT;
586 retval = put_user(tmp32, (s32 __user *) arg);
590 retval = copy_from_user(&ds32, argp,
591 sizeof(ds32)) ? -EFAULT : 0;
593 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
594 joydev->glue.BUSY = ds32.BUSY;
595 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
596 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
597 joydev->glue.JS_SAVE = ds32.JS_SAVE;
598 joydev->glue.JS_CORR = ds32.JS_CORR;
603 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
604 ds32.BUSY = joydev->glue.BUSY;
605 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
606 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
607 ds32.JS_SAVE = joydev->glue.JS_SAVE;
608 ds32.JS_CORR = joydev->glue.JS_CORR;
610 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
614 retval = joydev_ioctl_common(joydev, cmd, argp);
619 mutex_unlock(&joydev->mutex);
622 #endif /* CONFIG_COMPAT */
624 static long joydev_ioctl(struct file *file,
625 unsigned int cmd, unsigned long arg)
627 struct joydev_client *client = file->private_data;
628 struct joydev *joydev = client->joydev;
629 void __user *argp = (void __user *)arg;
632 retval = mutex_lock_interruptible(&joydev->mutex);
636 if (!joydev->exist) {
643 case JS_SET_TIMELIMIT:
644 retval = get_user(joydev->glue.JS_TIMELIMIT,
645 (long __user *) arg);
648 case JS_GET_TIMELIMIT:
649 retval = put_user(joydev->glue.JS_TIMELIMIT,
650 (long __user *) arg);
654 retval = copy_from_user(&joydev->glue, argp,
655 sizeof(joydev->glue)) ? -EFAULT: 0;
659 retval = copy_to_user(argp, &joydev->glue,
660 sizeof(joydev->glue)) ? -EFAULT : 0;
664 retval = joydev_ioctl_common(joydev, cmd, argp);
668 mutex_unlock(&joydev->mutex);
672 static const struct file_operations joydev_fops = {
673 .owner = THIS_MODULE,
677 .release = joydev_release,
678 .unlocked_ioctl = joydev_ioctl,
680 .compat_ioctl = joydev_compat_ioctl,
682 .fasync = joydev_fasync,
685 static int joydev_install_chrdev(struct joydev *joydev)
687 joydev_table[joydev->minor] = joydev;
691 static void joydev_remove_chrdev(struct joydev *joydev)
693 mutex_lock(&joydev_table_mutex);
694 joydev_table[joydev->minor] = NULL;
695 mutex_unlock(&joydev_table_mutex);
699 * Mark device non-existant. This disables writes, ioctls and
700 * prevents new users from opening the device. Already posted
701 * blocking reads will stay, however new ones will fail.
703 static void joydev_mark_dead(struct joydev *joydev)
705 mutex_lock(&joydev->mutex);
707 mutex_unlock(&joydev->mutex);
710 static void joydev_cleanup(struct joydev *joydev)
712 struct input_handle *handle = &joydev->handle;
714 joydev_mark_dead(joydev);
715 joydev_hangup(joydev);
716 joydev_remove_chrdev(joydev);
718 /* joydev is marked dead so noone else accesses joydev->open */
720 input_close_device(handle);
723 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
724 const struct input_device_id *id)
726 struct joydev *joydev;
730 for (minor = 0; minor < JOYDEV_MINORS; minor++)
731 if (!joydev_table[minor])
734 if (minor == JOYDEV_MINORS) {
735 printk(KERN_ERR "joydev: no more free joydev devices\n");
739 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
743 INIT_LIST_HEAD(&joydev->client_list);
744 spin_lock_init(&joydev->client_lock);
745 mutex_init(&joydev->mutex);
746 init_waitqueue_head(&joydev->wait);
748 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
750 joydev->minor = minor;
753 joydev->handle.dev = input_get_device(dev);
754 joydev->handle.name = joydev->name;
755 joydev->handle.handler = handler;
756 joydev->handle.private = joydev;
758 for (i = 0; i < ABS_MAX + 1; i++)
759 if (test_bit(i, dev->absbit)) {
760 joydev->absmap[i] = joydev->nabs;
761 joydev->abspam[joydev->nabs] = i;
765 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
766 if (test_bit(i + BTN_MISC, dev->keybit)) {
767 joydev->keymap[i] = joydev->nkey;
768 joydev->keypam[joydev->nkey] = i + BTN_MISC;
772 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
773 if (test_bit(i + BTN_MISC, dev->keybit)) {
774 joydev->keymap[i] = joydev->nkey;
775 joydev->keypam[joydev->nkey] = i + BTN_MISC;
779 for (i = 0; i < joydev->nabs; i++) {
780 j = joydev->abspam[i];
781 if (dev->absmax[j] == dev->absmin[j]) {
782 joydev->corr[i].type = JS_CORR_NONE;
783 joydev->abs[i] = dev->abs[j];
786 joydev->corr[i].type = JS_CORR_BROKEN;
787 joydev->corr[i].prec = dev->absfuzz[j];
788 joydev->corr[i].coef[0] =
789 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
790 joydev->corr[i].coef[1] =
791 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
793 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
795 joydev->corr[i].coef[2] = (1 << 29) / t;
796 joydev->corr[i].coef[3] = (1 << 29) / t;
798 joydev->abs[i] = joydev_correct(dev->abs[j],
803 dev_set_name(&joydev->dev, joydev->name);
804 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
805 joydev->dev.class = &input_class;
806 joydev->dev.parent = &dev->dev;
807 joydev->dev.release = joydev_free;
808 device_initialize(&joydev->dev);
810 error = input_register_handle(&joydev->handle);
812 goto err_free_joydev;
814 error = joydev_install_chrdev(joydev);
816 goto err_unregister_handle;
818 error = device_add(&joydev->dev);
820 goto err_cleanup_joydev;
825 joydev_cleanup(joydev);
826 err_unregister_handle:
827 input_unregister_handle(&joydev->handle);
829 put_device(&joydev->dev);
833 static void joydev_disconnect(struct input_handle *handle)
835 struct joydev *joydev = handle->private;
837 device_del(&joydev->dev);
838 joydev_cleanup(joydev);
839 input_unregister_handle(handle);
840 put_device(&joydev->dev);
843 static const struct input_device_id joydev_blacklist[] = {
845 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
846 INPUT_DEVICE_ID_MATCH_KEYBIT,
847 .evbit = { BIT_MASK(EV_KEY) },
848 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
849 }, /* Avoid itouchpads, touchscreens and tablets */
850 { } /* Terminating entry */
853 static const struct input_device_id joydev_ids[] = {
855 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
856 INPUT_DEVICE_ID_MATCH_ABSBIT,
857 .evbit = { BIT_MASK(EV_ABS) },
858 .absbit = { BIT_MASK(ABS_X) },
861 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
862 INPUT_DEVICE_ID_MATCH_ABSBIT,
863 .evbit = { BIT_MASK(EV_ABS) },
864 .absbit = { BIT_MASK(ABS_WHEEL) },
867 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
868 INPUT_DEVICE_ID_MATCH_ABSBIT,
869 .evbit = { BIT_MASK(EV_ABS) },
870 .absbit = { BIT_MASK(ABS_THROTTLE) },
872 { } /* Terminating entry */
875 MODULE_DEVICE_TABLE(input, joydev_ids);
877 static struct input_handler joydev_handler = {
878 .event = joydev_event,
879 .connect = joydev_connect,
880 .disconnect = joydev_disconnect,
881 .fops = &joydev_fops,
882 .minor = JOYDEV_MINOR_BASE,
884 .id_table = joydev_ids,
885 .blacklist = joydev_blacklist,
888 static int __init joydev_init(void)
890 return input_register_handler(&joydev_handler);
893 static void __exit joydev_exit(void)
895 input_unregister_handler(&joydev_handler);
898 module_init(joydev_init);
899 module_exit(joydev_exit);