2 * Event char devices, giving access to raw input device events.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #define EVDEV_MINOR_BASE 64
12 #define EVDEV_MINORS 32
13 #define EVDEV_MIN_BUFFER_SIZE 64U
14 #define EVDEV_BUF_PACKETS 8
16 #include <linux/poll.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/major.h>
23 #include <linux/device.h>
24 #include "input-compat.h"
29 struct input_handle handle;
30 wait_queue_head_t wait;
31 struct evdev_client __rcu *grab;
32 struct list_head client_list;
33 spinlock_t client_lock; /* protects client_list */
42 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
43 struct fasync_struct *fasync;
45 struct list_head node;
47 struct input_event buffer[];
50 static struct evdev *evdev_table[EVDEV_MINORS];
51 static DEFINE_MUTEX(evdev_table_mutex);
53 static void evdev_pass_event(struct evdev_client *client,
54 struct input_event *event)
57 * Interrupts are disabled, just acquire the lock.
58 * Make sure we don't leave with the client buffer
59 * "empty" by having client->head == client->tail.
61 spin_lock(&client->buffer_lock);
63 client->buffer[client->head++] = *event;
64 client->head &= client->bufsize - 1;
65 } while (client->head == client->tail);
66 spin_unlock(&client->buffer_lock);
68 if (event->type == EV_SYN)
69 kill_fasync(&client->fasync, SIGIO, POLL_IN);
73 * Pass incoming event to all connected clients.
75 static void evdev_event(struct input_handle *handle,
76 unsigned int type, unsigned int code, int value)
78 struct evdev *evdev = handle->private;
79 struct evdev_client *client;
80 struct input_event event;
82 do_gettimeofday(&event.time);
89 client = rcu_dereference(evdev->grab);
91 evdev_pass_event(client, &event);
93 list_for_each_entry_rcu(client, &evdev->client_list, node)
94 evdev_pass_event(client, &event);
98 wake_up_interruptible(&evdev->wait);
101 static int evdev_fasync(int fd, struct file *file, int on)
103 struct evdev_client *client = file->private_data;
105 return fasync_helper(fd, file, on, &client->fasync);
108 static int evdev_flush(struct file *file, fl_owner_t id)
110 struct evdev_client *client = file->private_data;
111 struct evdev *evdev = client->evdev;
114 retval = mutex_lock_interruptible(&evdev->mutex);
121 retval = input_flush_device(&evdev->handle, file);
123 mutex_unlock(&evdev->mutex);
127 static void evdev_free(struct device *dev)
129 struct evdev *evdev = container_of(dev, struct evdev, dev);
131 input_put_device(evdev->handle.dev);
136 * Grabs an event device (along with underlying input device).
137 * This function is called with evdev->mutex taken.
139 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
146 error = input_grab_device(&evdev->handle);
150 rcu_assign_pointer(evdev->grab, client);
156 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
158 if (evdev->grab != client)
161 rcu_assign_pointer(evdev->grab, NULL);
163 input_release_device(&evdev->handle);
168 static void evdev_attach_client(struct evdev *evdev,
169 struct evdev_client *client)
171 spin_lock(&evdev->client_lock);
172 list_add_tail_rcu(&client->node, &evdev->client_list);
173 spin_unlock(&evdev->client_lock);
177 static void evdev_detach_client(struct evdev *evdev,
178 struct evdev_client *client)
180 spin_lock(&evdev->client_lock);
181 list_del_rcu(&client->node);
182 spin_unlock(&evdev->client_lock);
186 static int evdev_open_device(struct evdev *evdev)
190 retval = mutex_lock_interruptible(&evdev->mutex);
196 else if (!evdev->open++) {
197 retval = input_open_device(&evdev->handle);
202 mutex_unlock(&evdev->mutex);
206 static void evdev_close_device(struct evdev *evdev)
208 mutex_lock(&evdev->mutex);
210 if (evdev->exist && !--evdev->open)
211 input_close_device(&evdev->handle);
213 mutex_unlock(&evdev->mutex);
217 * Wake up users waiting for IO so they can disconnect from
220 static void evdev_hangup(struct evdev *evdev)
222 struct evdev_client *client;
224 spin_lock(&evdev->client_lock);
225 list_for_each_entry(client, &evdev->client_list, node)
226 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
227 spin_unlock(&evdev->client_lock);
229 wake_up_interruptible(&evdev->wait);
232 static int evdev_release(struct inode *inode, struct file *file)
234 struct evdev_client *client = file->private_data;
235 struct evdev *evdev = client->evdev;
237 mutex_lock(&evdev->mutex);
238 if (evdev->grab == client)
239 evdev_ungrab(evdev, client);
240 mutex_unlock(&evdev->mutex);
242 evdev_detach_client(evdev, client);
245 evdev_close_device(evdev);
246 put_device(&evdev->dev);
251 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
253 unsigned int n_events =
254 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
255 EVDEV_MIN_BUFFER_SIZE);
257 return roundup_pow_of_two(n_events);
260 static int evdev_open(struct inode *inode, struct file *file)
263 struct evdev_client *client;
264 int i = iminor(inode) - EVDEV_MINOR_BASE;
265 unsigned int bufsize;
268 if (i >= EVDEV_MINORS)
271 error = mutex_lock_interruptible(&evdev_table_mutex);
274 evdev = evdev_table[i];
276 get_device(&evdev->dev);
277 mutex_unlock(&evdev_table_mutex);
282 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
284 client = kzalloc(sizeof(struct evdev_client) +
285 bufsize * sizeof(struct input_event),
292 client->bufsize = bufsize;
293 spin_lock_init(&client->buffer_lock);
294 client->evdev = evdev;
295 evdev_attach_client(evdev, client);
297 error = evdev_open_device(evdev);
299 goto err_free_client;
301 file->private_data = client;
302 nonseekable_open(inode, file);
307 evdev_detach_client(evdev, client);
310 put_device(&evdev->dev);
314 static ssize_t evdev_write(struct file *file, const char __user *buffer,
315 size_t count, loff_t *ppos)
317 struct evdev_client *client = file->private_data;
318 struct evdev *evdev = client->evdev;
319 struct input_event event;
322 retval = mutex_lock_interruptible(&evdev->mutex);
331 while (retval < count) {
333 if (input_event_from_user(buffer + retval, &event)) {
338 input_inject_event(&evdev->handle,
339 event.type, event.code, event.value);
340 retval += input_event_size();
344 mutex_unlock(&evdev->mutex);
348 static int evdev_fetch_next_event(struct evdev_client *client,
349 struct input_event *event)
353 spin_lock_irq(&client->buffer_lock);
355 have_event = client->head != client->tail;
357 *event = client->buffer[client->tail++];
358 client->tail &= client->bufsize - 1;
361 spin_unlock_irq(&client->buffer_lock);
366 static ssize_t evdev_read(struct file *file, char __user *buffer,
367 size_t count, loff_t *ppos)
369 struct evdev_client *client = file->private_data;
370 struct evdev *evdev = client->evdev;
371 struct input_event event;
374 if (count < input_event_size())
377 if (client->head == client->tail && evdev->exist &&
378 (file->f_flags & O_NONBLOCK))
381 retval = wait_event_interruptible(evdev->wait,
382 client->head != client->tail || !evdev->exist);
389 while (retval + input_event_size() <= count &&
390 evdev_fetch_next_event(client, &event)) {
392 if (input_event_to_user(buffer + retval, &event))
395 retval += input_event_size();
401 /* No kernel lock - fine */
402 static unsigned int evdev_poll(struct file *file, poll_table *wait)
404 struct evdev_client *client = file->private_data;
405 struct evdev *evdev = client->evdev;
408 poll_wait(file, &evdev->wait, wait);
410 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
411 if (client->head != client->tail)
412 mask |= POLLIN | POLLRDNORM;
419 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
420 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
423 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
424 unsigned int maxlen, void __user *p, int compat)
429 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
433 for (i = 0; i < len / sizeof(compat_long_t); i++)
434 if (copy_to_user((compat_long_t __user *) p + i,
435 (compat_long_t *) bits +
436 i + 1 - ((i % 2) << 1),
437 sizeof(compat_long_t)))
440 len = BITS_TO_LONGS(maxbit) * sizeof(long);
444 if (copy_to_user(p, bits, len))
451 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
452 unsigned int maxlen, void __user *p, int compat)
455 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
456 BITS_TO_LONGS(maxbit) * sizeof(long);
461 return copy_to_user(p, bits, len) ? -EFAULT : len;
463 #endif /* __BIG_ENDIAN */
467 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
468 unsigned int maxlen, void __user *p, int compat)
470 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
475 return copy_to_user(p, bits, len) ? -EFAULT : len;
478 #endif /* CONFIG_COMPAT */
480 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
487 len = strlen(str) + 1;
491 return copy_to_user(p, str, len) ? -EFAULT : len;
494 #define OLD_KEY_MAX 0x1ff
495 static int handle_eviocgbit(struct input_dev *dev,
496 unsigned int type, unsigned int size,
497 void __user *p, int compat_mode)
499 static unsigned long keymax_warn_time;
505 case 0: bits = dev->evbit; len = EV_MAX; break;
506 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
507 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
508 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
509 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
510 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
511 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
512 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
513 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
514 default: return -EINVAL;
518 * Work around bugs in userspace programs that like to do
519 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
520 * should be in bytes, not in bits.
522 if (type == EV_KEY && size == OLD_KEY_MAX) {
524 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
526 "evdev.c(EVIOCGBIT): Suspicious buffer size %u, "
527 "limiting output to %zu bytes. See "
528 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
530 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
533 return bits_to_user(bits, len, size, p, compat_mode);
537 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
538 void __user *p, int compat_mode)
540 struct evdev_client *client = file->private_data;
541 struct evdev *evdev = client->evdev;
542 struct input_dev *dev = evdev->handle.dev;
543 struct input_absinfo abs;
544 struct ff_effect effect;
545 int __user *ip = (int __user *)p;
546 unsigned int i, t, u, v;
550 /* First we check for fixed-length commands */
554 return put_user(EV_VERSION, ip);
557 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
562 if (!test_bit(EV_REP, dev->evbit))
564 if (put_user(dev->rep[REP_DELAY], ip))
566 if (put_user(dev->rep[REP_PERIOD], ip + 1))
571 if (!test_bit(EV_REP, dev->evbit))
575 if (get_user(v, ip + 1))
578 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
579 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
587 error = input_get_keycode(dev, t, &v);
591 if (put_user(v, ip + 1))
597 if (get_user(t, ip) || get_user(v, ip + 1))
600 return input_set_keycode(dev, t, v);
603 return input_ff_erase(dev, (int)(unsigned long) p, file);
606 i = test_bit(EV_FF, dev->evbit) ?
607 dev->ff->max_effects : 0;
614 return evdev_grab(evdev, client);
616 return evdev_ungrab(evdev, client);
619 size = _IOC_SIZE(cmd);
621 /* Now check variable-length commands */
622 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
624 switch (EVIOC_MASK_SIZE(cmd)) {
627 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
630 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
633 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
636 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
639 return str_to_user(dev->name, size, p);
642 return str_to_user(dev->phys, size, p);
645 return str_to_user(dev->uniq, size, p);
647 case EVIOC_MASK_SIZE(EVIOCSFF):
648 if (input_ff_effect_from_user(p, size, &effect))
651 error = input_ff_upload(dev, &effect, file);
653 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
659 /* Multi-number variable-length handlers */
660 if (_IOC_TYPE(cmd) != 'E')
663 if (_IOC_DIR(cmd) == _IOC_READ) {
665 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
666 return handle_eviocgbit(dev,
667 _IOC_NR(cmd) & EV_MAX, size,
670 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
675 t = _IOC_NR(cmd) & ABS_MAX;
676 abs = dev->absinfo[t];
678 if (copy_to_user(p, &abs, min_t(size_t,
679 size, sizeof(struct input_absinfo))))
686 if (_IOC_DIR(cmd) == _IOC_WRITE) {
688 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
693 t = _IOC_NR(cmd) & ABS_MAX;
695 if (copy_from_user(&abs, p, min_t(size_t,
696 size, sizeof(struct input_absinfo))))
699 if (size < sizeof(struct input_absinfo))
702 /* We can't change number of reserved MT slots */
703 if (t == ABS_MT_SLOT)
707 * Take event lock to ensure that we are not
708 * changing device parameters in the middle
711 spin_lock_irq(&dev->event_lock);
712 dev->absinfo[t] = abs;
713 spin_unlock_irq(&dev->event_lock);
722 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
723 void __user *p, int compat_mode)
725 struct evdev_client *client = file->private_data;
726 struct evdev *evdev = client->evdev;
729 retval = mutex_lock_interruptible(&evdev->mutex);
738 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
741 mutex_unlock(&evdev->mutex);
745 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
747 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
751 static long evdev_ioctl_compat(struct file *file,
752 unsigned int cmd, unsigned long arg)
754 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
758 static const struct file_operations evdev_fops = {
759 .owner = THIS_MODULE,
761 .write = evdev_write,
764 .release = evdev_release,
765 .unlocked_ioctl = evdev_ioctl,
767 .compat_ioctl = evdev_ioctl_compat,
769 .fasync = evdev_fasync,
770 .flush = evdev_flush,
774 static int evdev_install_chrdev(struct evdev *evdev)
777 * No need to do any locking here as calls to connect and
778 * disconnect are serialized by the input core
780 evdev_table[evdev->minor] = evdev;
784 static void evdev_remove_chrdev(struct evdev *evdev)
787 * Lock evdev table to prevent race with evdev_open()
789 mutex_lock(&evdev_table_mutex);
790 evdev_table[evdev->minor] = NULL;
791 mutex_unlock(&evdev_table_mutex);
795 * Mark device non-existent. This disables writes, ioctls and
796 * prevents new users from opening the device. Already posted
797 * blocking reads will stay, however new ones will fail.
799 static void evdev_mark_dead(struct evdev *evdev)
801 mutex_lock(&evdev->mutex);
802 evdev->exist = false;
803 mutex_unlock(&evdev->mutex);
806 static void evdev_cleanup(struct evdev *evdev)
808 struct input_handle *handle = &evdev->handle;
810 evdev_mark_dead(evdev);
812 evdev_remove_chrdev(evdev);
814 /* evdev is marked dead so no one else accesses evdev->open */
816 input_flush_device(handle, NULL);
817 input_close_device(handle);
822 * Create new evdev device. Note that input core serializes calls
823 * to connect and disconnect so we don't need to lock evdev_table here.
825 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
826 const struct input_device_id *id)
832 for (minor = 0; minor < EVDEV_MINORS; minor++)
833 if (!evdev_table[minor])
836 if (minor == EVDEV_MINORS) {
837 printk(KERN_ERR "evdev: no more free evdev devices\n");
841 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
845 INIT_LIST_HEAD(&evdev->client_list);
846 spin_lock_init(&evdev->client_lock);
847 mutex_init(&evdev->mutex);
848 init_waitqueue_head(&evdev->wait);
850 dev_set_name(&evdev->dev, "event%d", minor);
852 evdev->minor = minor;
854 evdev->handle.dev = input_get_device(dev);
855 evdev->handle.name = dev_name(&evdev->dev);
856 evdev->handle.handler = handler;
857 evdev->handle.private = evdev;
859 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
860 evdev->dev.class = &input_class;
861 evdev->dev.parent = &dev->dev;
862 evdev->dev.release = evdev_free;
863 device_initialize(&evdev->dev);
865 error = input_register_handle(&evdev->handle);
869 error = evdev_install_chrdev(evdev);
871 goto err_unregister_handle;
873 error = device_add(&evdev->dev);
875 goto err_cleanup_evdev;
880 evdev_cleanup(evdev);
881 err_unregister_handle:
882 input_unregister_handle(&evdev->handle);
884 put_device(&evdev->dev);
888 static void evdev_disconnect(struct input_handle *handle)
890 struct evdev *evdev = handle->private;
892 device_del(&evdev->dev);
893 evdev_cleanup(evdev);
894 input_unregister_handle(handle);
895 put_device(&evdev->dev);
898 static const struct input_device_id evdev_ids[] = {
899 { .driver_info = 1 }, /* Matches all devices */
900 { }, /* Terminating zero entry */
903 MODULE_DEVICE_TABLE(input, evdev_ids);
905 static struct input_handler evdev_handler = {
906 .event = evdev_event,
907 .connect = evdev_connect,
908 .disconnect = evdev_disconnect,
910 .minor = EVDEV_MINOR_BASE,
912 .id_table = evdev_ids,
915 static int __init evdev_init(void)
917 return input_register_handler(&evdev_handler);
920 static void __exit evdev_exit(void)
922 input_unregister_handler(&evdev_handler);
925 module_init(evdev_init);
926 module_exit(evdev_exit);
928 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
929 MODULE_DESCRIPTION("Input driver event char devices");
930 MODULE_LICENSE("GPL");