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 /* pandora hack: convert some normal keys to joystick keys */
79 } converted_keys[] = {
80 { KEY_LEFTCTRL, BTN_SELECT },
81 { KEY_LEFTALT, BTN_START },
82 { KEY_MENU, BTN_MODE },
85 static int joydev_correct(int value, struct js_corr *corr)
93 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
94 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
95 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
102 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
105 static void joydev_pass_event(struct joydev_client *client,
106 struct js_event *event)
108 struct joydev *joydev = client->joydev;
111 * IRQs already disabled, just acquire the lock
113 spin_lock(&client->buffer_lock);
115 client->buffer[client->head] = *event;
117 if (client->startup == joydev->nabs + joydev->nkey) {
119 client->head &= JOYDEV_BUFFER_SIZE - 1;
120 if (client->tail == client->head)
124 spin_unlock(&client->buffer_lock);
126 kill_fasync(&client->fasync, SIGIO, POLL_IN);
129 static void joydev_event(struct input_handle *handle,
130 unsigned int type, unsigned int code, int value)
132 struct joydev *joydev = handle->private;
133 struct joydev_client *client;
134 struct js_event event;
140 for (i = 0; i < ARRAY_SIZE(converted_keys); i++)
141 if (code == converted_keys[i].from) {
142 code = converted_keys[i].to;
146 if (code < BTN_MISC || value == 2)
148 event.type = JS_EVENT_BUTTON;
149 event.number = joydev->keymap[code - BTN_MISC];
154 event.type = JS_EVENT_AXIS;
155 event.number = joydev->absmap[code];
156 event.value = joydev_correct(value,
157 &joydev->corr[event.number]);
158 if (event.value == joydev->abs[event.number])
160 joydev->abs[event.number] = event.value;
167 event.time = jiffies_to_msecs(jiffies);
170 list_for_each_entry_rcu(client, &joydev->client_list, node)
171 joydev_pass_event(client, &event);
174 wake_up_interruptible(&joydev->wait);
177 static int joydev_fasync(int fd, struct file *file, int on)
180 struct joydev_client *client = file->private_data;
182 retval = fasync_helper(fd, file, on, &client->fasync);
184 return retval < 0 ? retval : 0;
187 static void joydev_free(struct device *dev)
189 struct joydev *joydev = container_of(dev, struct joydev, dev);
191 input_put_device(joydev->handle.dev);
195 static void joydev_attach_client(struct joydev *joydev,
196 struct joydev_client *client)
198 spin_lock(&joydev->client_lock);
199 list_add_tail_rcu(&client->node, &joydev->client_list);
200 spin_unlock(&joydev->client_lock);
204 static void joydev_detach_client(struct joydev *joydev,
205 struct joydev_client *client)
207 spin_lock(&joydev->client_lock);
208 list_del_rcu(&client->node);
209 spin_unlock(&joydev->client_lock);
213 static int joydev_open_device(struct joydev *joydev)
217 retval = mutex_lock_interruptible(&joydev->mutex);
223 else if (!joydev->open++) {
224 retval = input_open_device(&joydev->handle);
229 mutex_unlock(&joydev->mutex);
233 static void joydev_close_device(struct joydev *joydev)
235 mutex_lock(&joydev->mutex);
237 if (joydev->exist && !--joydev->open)
238 input_close_device(&joydev->handle);
240 mutex_unlock(&joydev->mutex);
244 * Wake up users waiting for IO so they can disconnect from
247 static void joydev_hangup(struct joydev *joydev)
249 struct joydev_client *client;
251 spin_lock(&joydev->client_lock);
252 list_for_each_entry(client, &joydev->client_list, node)
253 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
254 spin_unlock(&joydev->client_lock);
256 wake_up_interruptible(&joydev->wait);
259 static int joydev_release(struct inode *inode, struct file *file)
261 struct joydev_client *client = file->private_data;
262 struct joydev *joydev = client->joydev;
264 joydev_fasync(-1, file, 0);
265 joydev_detach_client(joydev, client);
268 joydev_close_device(joydev);
269 put_device(&joydev->dev);
274 static int joydev_open(struct inode *inode, struct file *file)
276 struct joydev_client *client;
277 struct joydev *joydev;
278 int i = iminor(inode) - JOYDEV_MINOR_BASE;
281 if (i >= JOYDEV_MINORS)
284 error = mutex_lock_interruptible(&joydev_table_mutex);
287 joydev = joydev_table[i];
289 get_device(&joydev->dev);
290 mutex_unlock(&joydev_table_mutex);
295 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
301 spin_lock_init(&client->buffer_lock);
302 client->joydev = joydev;
303 joydev_attach_client(joydev, client);
305 error = joydev_open_device(joydev);
307 goto err_free_client;
309 file->private_data = client;
313 joydev_detach_client(joydev, client);
316 put_device(&joydev->dev);
320 static int joydev_generate_startup_event(struct joydev_client *client,
321 struct input_dev *input,
322 struct js_event *event)
324 struct joydev *joydev = client->joydev;
327 spin_lock_irq(&client->buffer_lock);
329 have_event = client->startup < joydev->nabs + joydev->nkey;
333 event->time = jiffies_to_msecs(jiffies);
334 if (client->startup < joydev->nkey) {
335 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
336 event->number = client->startup;
337 event->value = !!test_bit(joydev->keypam[event->number],
340 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
341 event->number = client->startup - joydev->nkey;
342 event->value = joydev->abs[event->number];
347 spin_unlock_irq(&client->buffer_lock);
352 static int joydev_fetch_next_event(struct joydev_client *client,
353 struct js_event *event)
357 spin_lock_irq(&client->buffer_lock);
359 have_event = client->head != client->tail;
361 *event = client->buffer[client->tail++];
362 client->tail &= JOYDEV_BUFFER_SIZE - 1;
365 spin_unlock_irq(&client->buffer_lock);
371 * Old joystick interface
373 static ssize_t joydev_0x_read(struct joydev_client *client,
374 struct input_dev *input,
377 struct joydev *joydev = client->joydev;
378 struct JS_DATA_TYPE data;
381 spin_lock_irq(&input->event_lock);
386 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
388 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
389 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
390 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
393 * Reset reader's event queue
395 spin_lock(&client->buffer_lock);
397 client->tail = client->head;
398 spin_unlock(&client->buffer_lock);
400 spin_unlock_irq(&input->event_lock);
402 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
405 return sizeof(struct JS_DATA_TYPE);
408 static inline int joydev_data_pending(struct joydev_client *client)
410 struct joydev *joydev = client->joydev;
412 return client->startup < joydev->nabs + joydev->nkey ||
413 client->head != client->tail;
416 static ssize_t joydev_read(struct file *file, char __user *buf,
417 size_t count, loff_t *ppos)
419 struct joydev_client *client = file->private_data;
420 struct joydev *joydev = client->joydev;
421 struct input_dev *input = joydev->handle.dev;
422 struct js_event event;
428 if (count < sizeof(struct js_event))
431 if (count == sizeof(struct JS_DATA_TYPE))
432 return joydev_0x_read(client, input, buf);
434 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
437 retval = wait_event_interruptible(joydev->wait,
438 !joydev->exist || joydev_data_pending(client));
445 while (retval + sizeof(struct js_event) <= count &&
446 joydev_generate_startup_event(client, input, &event)) {
448 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
451 retval += sizeof(struct js_event);
454 while (retval + sizeof(struct js_event) <= count &&
455 joydev_fetch_next_event(client, &event)) {
457 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
460 retval += sizeof(struct js_event);
466 /* No kernel lock - fine */
467 static unsigned int joydev_poll(struct file *file, poll_table *wait)
469 struct joydev_client *client = file->private_data;
470 struct joydev *joydev = client->joydev;
472 poll_wait(file, &joydev->wait, wait);
473 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
474 (joydev->exist ? 0 : (POLLHUP | POLLERR));
477 static int joydev_ioctl_common(struct joydev *joydev,
478 unsigned int cmd, void __user *argp)
480 struct input_dev *dev = joydev->handle.dev;
486 return copy_from_user(&joydev->glue.JS_CORR, argp,
487 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
490 return copy_to_user(argp, &joydev->glue.JS_CORR,
491 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
494 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
497 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
500 return put_user(JS_VERSION, (__u32 __user *) argp);
503 return put_user(joydev->nabs, (__u8 __user *) argp);
506 return put_user(joydev->nkey, (__u8 __user *) argp);
509 if (copy_from_user(joydev->corr, argp,
510 sizeof(joydev->corr[0]) * joydev->nabs))
513 for (i = 0; i < joydev->nabs; i++) {
514 j = joydev->abspam[i];
515 joydev->abs[i] = joydev_correct(dev->abs[j],
521 return copy_to_user(argp, joydev->corr,
522 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
525 if (copy_from_user(joydev->abspam, argp,
526 sizeof(__u8) * (ABS_MAX + 1)))
529 for (i = 0; i < joydev->nabs; i++) {
530 if (joydev->abspam[i] > ABS_MAX)
532 joydev->absmap[joydev->abspam[i]] = i;
537 return copy_to_user(argp, joydev->abspam,
538 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
541 if (copy_from_user(joydev->keypam, argp,
542 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
545 for (i = 0; i < joydev->nkey; i++) {
546 if (joydev->keypam[i] > KEY_MAX ||
547 joydev->keypam[i] < BTN_MISC)
549 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
555 return copy_to_user(argp, joydev->keypam,
556 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
559 if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
563 len = strlen(dev->name) + 1;
564 if (len > _IOC_SIZE(cmd))
565 len = _IOC_SIZE(cmd);
566 if (copy_to_user(argp, dev->name, len))
575 static long joydev_compat_ioctl(struct file *file,
576 unsigned int cmd, unsigned long arg)
578 struct joydev_client *client = file->private_data;
579 struct joydev *joydev = client->joydev;
580 void __user *argp = (void __user *)arg;
582 struct JS_DATA_SAVE_TYPE_32 ds32;
585 retval = mutex_lock_interruptible(&joydev->mutex);
589 if (!joydev->exist) {
596 case JS_SET_TIMELIMIT:
597 retval = get_user(tmp32, (s32 __user *) arg);
599 joydev->glue.JS_TIMELIMIT = tmp32;
602 case JS_GET_TIMELIMIT:
603 tmp32 = joydev->glue.JS_TIMELIMIT;
604 retval = put_user(tmp32, (s32 __user *) arg);
608 retval = copy_from_user(&ds32, argp,
609 sizeof(ds32)) ? -EFAULT : 0;
611 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
612 joydev->glue.BUSY = ds32.BUSY;
613 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
614 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
615 joydev->glue.JS_SAVE = ds32.JS_SAVE;
616 joydev->glue.JS_CORR = ds32.JS_CORR;
621 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
622 ds32.BUSY = joydev->glue.BUSY;
623 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
624 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
625 ds32.JS_SAVE = joydev->glue.JS_SAVE;
626 ds32.JS_CORR = joydev->glue.JS_CORR;
628 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
632 retval = joydev_ioctl_common(joydev, cmd, argp);
637 mutex_unlock(&joydev->mutex);
640 #endif /* CONFIG_COMPAT */
642 static long joydev_ioctl(struct file *file,
643 unsigned int cmd, unsigned long arg)
645 struct joydev_client *client = file->private_data;
646 struct joydev *joydev = client->joydev;
647 void __user *argp = (void __user *)arg;
650 retval = mutex_lock_interruptible(&joydev->mutex);
654 if (!joydev->exist) {
661 case JS_SET_TIMELIMIT:
662 retval = get_user(joydev->glue.JS_TIMELIMIT,
663 (long __user *) arg);
666 case JS_GET_TIMELIMIT:
667 retval = put_user(joydev->glue.JS_TIMELIMIT,
668 (long __user *) arg);
672 retval = copy_from_user(&joydev->glue, argp,
673 sizeof(joydev->glue)) ? -EFAULT: 0;
677 retval = copy_to_user(argp, &joydev->glue,
678 sizeof(joydev->glue)) ? -EFAULT : 0;
682 retval = joydev_ioctl_common(joydev, cmd, argp);
686 mutex_unlock(&joydev->mutex);
690 static const struct file_operations joydev_fops = {
691 .owner = THIS_MODULE,
695 .release = joydev_release,
696 .unlocked_ioctl = joydev_ioctl,
698 .compat_ioctl = joydev_compat_ioctl,
700 .fasync = joydev_fasync,
703 static int joydev_install_chrdev(struct joydev *joydev)
705 joydev_table[joydev->minor] = joydev;
709 static void joydev_remove_chrdev(struct joydev *joydev)
711 mutex_lock(&joydev_table_mutex);
712 joydev_table[joydev->minor] = NULL;
713 mutex_unlock(&joydev_table_mutex);
717 * Mark device non-existant. This disables writes, ioctls and
718 * prevents new users from opening the device. Already posted
719 * blocking reads will stay, however new ones will fail.
721 static void joydev_mark_dead(struct joydev *joydev)
723 mutex_lock(&joydev->mutex);
725 mutex_unlock(&joydev->mutex);
728 static void joydev_cleanup(struct joydev *joydev)
730 struct input_handle *handle = &joydev->handle;
732 joydev_mark_dead(joydev);
733 joydev_hangup(joydev);
734 joydev_remove_chrdev(joydev);
736 /* joydev is marked dead so noone else accesses joydev->open */
738 input_close_device(handle);
741 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
742 const struct input_device_id *id)
744 struct joydev *joydev;
748 for (minor = 0; minor < JOYDEV_MINORS; minor++)
749 if (!joydev_table[minor])
752 if (minor == JOYDEV_MINORS) {
753 printk(KERN_ERR "joydev: no more free joydev devices\n");
757 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
761 INIT_LIST_HEAD(&joydev->client_list);
762 spin_lock_init(&joydev->client_lock);
763 mutex_init(&joydev->mutex);
764 init_waitqueue_head(&joydev->wait);
766 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
768 joydev->minor = minor;
771 joydev->handle.dev = input_get_device(dev);
772 joydev->handle.name = joydev->name;
773 joydev->handle.handler = handler;
774 joydev->handle.private = joydev;
776 for (i = 0; i < ABS_MAX + 1; i++)
777 if (test_bit(i, dev->absbit)) {
778 joydev->absmap[i] = joydev->nabs;
779 joydev->abspam[joydev->nabs] = i;
783 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
784 if (test_bit(i + BTN_MISC, dev->keybit)) {
785 joydev->keymap[i] = joydev->nkey;
786 joydev->keypam[joydev->nkey] = i + BTN_MISC;
790 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
791 if (test_bit(i + BTN_MISC, dev->keybit)) {
792 joydev->keymap[i] = joydev->nkey;
793 joydev->keypam[joydev->nkey] = i + BTN_MISC;
797 for (i = 0; i < ARRAY_SIZE(converted_keys); i++)
798 if (test_bit(converted_keys[i].from, dev->keybit)) {
799 joydev->keymap[converted_keys[i].to - BTN_MISC] = joydev->nkey;
800 joydev->keypam[joydev->nkey] = converted_keys[i].to;
804 for (i = 0; i < joydev->nabs; i++) {
805 j = joydev->abspam[i];
806 if (dev->absmax[j] == dev->absmin[j]) {
807 joydev->corr[i].type = JS_CORR_NONE;
808 joydev->abs[i] = dev->abs[j];
811 joydev->corr[i].type = JS_CORR_BROKEN;
812 joydev->corr[i].prec = dev->absfuzz[j];
813 joydev->corr[i].coef[0] =
814 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
815 joydev->corr[i].coef[1] =
816 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
818 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
820 joydev->corr[i].coef[2] = (1 << 29) / t;
821 joydev->corr[i].coef[3] = (1 << 29) / t;
823 joydev->abs[i] = joydev_correct(dev->abs[j],
828 strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
829 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
830 joydev->dev.class = &input_class;
831 joydev->dev.parent = &dev->dev;
832 joydev->dev.release = joydev_free;
833 device_initialize(&joydev->dev);
835 error = input_register_handle(&joydev->handle);
837 goto err_free_joydev;
839 error = joydev_install_chrdev(joydev);
841 goto err_unregister_handle;
843 error = device_add(&joydev->dev);
845 goto err_cleanup_joydev;
850 joydev_cleanup(joydev);
851 err_unregister_handle:
852 input_unregister_handle(&joydev->handle);
854 put_device(&joydev->dev);
858 static void joydev_disconnect(struct input_handle *handle)
860 struct joydev *joydev = handle->private;
862 device_del(&joydev->dev);
863 joydev_cleanup(joydev);
864 input_unregister_handle(handle);
865 put_device(&joydev->dev);
868 static const struct input_device_id joydev_blacklist[] = {
870 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871 INPUT_DEVICE_ID_MATCH_KEYBIT,
872 .evbit = { BIT_MASK(EV_KEY) },
873 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
874 }, /* Avoid itouchpads, touchscreens and tablets */
875 { } /* Terminating entry */
878 static const struct input_device_id joydev_ids[] = {
880 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
881 INPUT_DEVICE_ID_MATCH_ABSBIT,
882 .evbit = { BIT_MASK(EV_ABS) },
883 .absbit = { BIT_MASK(ABS_X) },
886 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
887 INPUT_DEVICE_ID_MATCH_ABSBIT,
888 .evbit = { BIT_MASK(EV_ABS) },
889 .absbit = { BIT_MASK(ABS_RX) },
892 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
893 INPUT_DEVICE_ID_MATCH_ABSBIT,
894 .evbit = { BIT_MASK(EV_ABS) },
895 .absbit = { BIT_MASK(ABS_WHEEL) },
898 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
899 INPUT_DEVICE_ID_MATCH_ABSBIT,
900 .evbit = { BIT_MASK(EV_ABS) },
901 .absbit = { BIT_MASK(ABS_THROTTLE) },
903 { } /* Terminating entry */
906 MODULE_DEVICE_TABLE(input, joydev_ids);
908 static struct input_handler joydev_handler = {
909 .event = joydev_event,
910 .connect = joydev_connect,
911 .disconnect = joydev_disconnect,
912 .fops = &joydev_fops,
913 .minor = JOYDEV_MINOR_BASE,
915 .id_table = joydev_ids,
916 .blacklist = joydev_blacklist,
919 static int __init joydev_init(void)
921 return input_register_handler(&joydev_handler);
924 static void __exit joydev_exit(void)
926 input_unregister_handler(&joydev_handler);
929 module_init(joydev_init);
930 module_exit(joydev_exit);