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_BUFFER_SIZE 64
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/major.h>
21 #include <linux/smp_lock.h>
22 #include <linux/device.h>
23 #include <linux/compat.h>
30 struct input_handle handle;
31 wait_queue_head_t wait;
32 struct evdev_list *grab;
33 struct list_head list;
37 struct input_event buffer[EVDEV_BUFFER_SIZE];
40 struct fasync_struct *fasync;
42 struct list_head node;
45 static struct evdev *evdev_table[EVDEV_MINORS];
47 static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
49 struct evdev *evdev = handle->private;
50 struct evdev_list *list;
55 do_gettimeofday(&list->buffer[list->head].time);
56 list->buffer[list->head].type = type;
57 list->buffer[list->head].code = code;
58 list->buffer[list->head].value = value;
59 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
61 kill_fasync(&list->fasync, SIGIO, POLL_IN);
63 list_for_each_entry(list, &evdev->list, node) {
65 do_gettimeofday(&list->buffer[list->head].time);
66 list->buffer[list->head].type = type;
67 list->buffer[list->head].code = code;
68 list->buffer[list->head].value = value;
69 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
71 kill_fasync(&list->fasync, SIGIO, POLL_IN);
74 wake_up_interruptible(&evdev->wait);
77 static int evdev_fasync(int fd, struct file *file, int on)
80 struct evdev_list *list = file->private_data;
81 retval = fasync_helper(fd, file, on, &list->fasync);
82 return retval < 0 ? retval : 0;
85 static int evdev_flush(struct file * file)
87 struct evdev_list *list = file->private_data;
88 if (!list->evdev->exist) return -ENODEV;
89 return input_flush_device(&list->evdev->handle, file);
92 static void evdev_free(struct evdev *evdev)
94 evdev_table[evdev->minor] = NULL;
98 static int evdev_release(struct inode * inode, struct file * file)
100 struct evdev_list *list = file->private_data;
102 if (list->evdev->grab == list) {
103 input_release_device(&list->evdev->handle);
104 list->evdev->grab = NULL;
107 evdev_fasync(-1, file, 0);
108 list_del(&list->node);
110 if (!--list->evdev->open) {
111 if (list->evdev->exist)
112 input_close_device(&list->evdev->handle);
114 evdev_free(list->evdev);
121 static int evdev_open(struct inode * inode, struct file * file)
123 struct evdev_list *list;
124 int i = iminor(inode) - EVDEV_MINOR_BASE;
127 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
130 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
133 if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
135 memset(list, 0, sizeof(struct evdev_list));
137 list->evdev = evdev_table[i];
138 list_add_tail(&list->node, &evdev_table[i]->list);
139 file->private_data = list;
141 if (!list->evdev->open++)
142 if (list->evdev->exist)
143 input_open_device(&list->evdev->handle);
149 struct input_event_compat {
150 struct compat_timeval time;
157 # define COMPAT_TEST test_thread_flag(TIF_IA32)
158 #elif defined(CONFIG_IA64)
159 # define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
160 #elif defined(CONFIG_ARCH_S390)
161 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
162 #elif defined(CONFIG_MIPS)
163 # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
165 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
168 static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
170 struct evdev_list *list = file->private_data;
171 struct input_event_compat event;
174 while (retval < count) {
175 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
177 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
178 retval += sizeof(struct input_event_compat);
185 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
187 struct evdev_list *list = file->private_data;
188 struct input_event event;
191 if (!list->evdev->exist) return -ENODEV;
195 return evdev_write_compat(file, buffer, count, ppos);
198 while (retval < count) {
200 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
202 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
203 retval += sizeof(struct input_event);
210 static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
212 struct evdev_list *list = file->private_data;
215 if (count < sizeof(struct input_event_compat))
218 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
221 retval = wait_event_interruptible(list->evdev->wait,
222 list->head != list->tail || (!list->evdev->exist));
227 if (!list->evdev->exist)
230 while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
231 struct input_event *event = (struct input_event *) list->buffer + list->tail;
232 struct input_event_compat event_compat;
233 event_compat.time.tv_sec = event->time.tv_sec;
234 event_compat.time.tv_usec = event->time.tv_usec;
235 event_compat.type = event->type;
236 event_compat.code = event->code;
237 event_compat.value = event->value;
239 if (copy_to_user(buffer + retval, &event_compat,
240 sizeof(struct input_event_compat))) return -EFAULT;
241 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
242 retval += sizeof(struct input_event_compat);
249 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
251 struct evdev_list *list = file->private_data;
256 return evdev_read_compat(file, buffer, count, ppos);
259 if (count < sizeof(struct input_event))
262 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
265 retval = wait_event_interruptible(list->evdev->wait,
266 list->head != list->tail || (!list->evdev->exist));
271 if (!list->evdev->exist)
274 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
275 if (copy_to_user(buffer + retval, list->buffer + list->tail,
276 sizeof(struct input_event))) return -EFAULT;
277 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
278 retval += sizeof(struct input_event);
284 /* No kernel lock - fine */
285 static unsigned int evdev_poll(struct file *file, poll_table *wait)
287 struct evdev_list *list = file->private_data;
288 poll_wait(file, &list->evdev->wait, wait);
289 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
290 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
293 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
295 struct evdev_list *list = file->private_data;
296 struct evdev *evdev = list->evdev;
297 struct input_dev *dev = evdev->handle.dev;
298 struct input_absinfo abs;
299 void __user *p = (void __user *)arg;
300 int __user *ip = (int __user *)arg;
303 if (!evdev->exist) return -ENODEV;
308 return put_user(EV_VERSION, ip);
311 return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
314 if (get_user(t, ip)) return -EFAULT;
315 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
316 if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
320 if (get_user(t, ip)) return -EFAULT;
321 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
322 if (get_user(v, ip + 1)) return -EFAULT;
323 if (v < 0 || v > KEY_MAX) return -EINVAL;
324 if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8))) return -EINVAL;
325 u = SET_INPUT_KEYCODE(dev, t, v);
326 clear_bit(u, dev->keybit);
327 set_bit(v, dev->keybit);
328 for (i = 0; i < dev->keycodemax; i++)
329 if (INPUT_KEYCODE(dev,i) == u)
330 set_bit(u, dev->keybit);
334 if (dev->upload_effect) {
335 struct ff_effect effect;
338 if (copy_from_user(&effect, p, sizeof(effect)))
340 err = dev->upload_effect(dev, &effect);
341 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
348 if (dev->erase_effect) {
349 return dev->erase_effect(dev, (int)arg);
354 if (put_user(dev->ff_effects_max, ip))
362 if (input_grab_device(&evdev->handle))
367 if (evdev->grab != list)
369 input_release_device(&evdev->handle);
376 if (_IOC_TYPE(cmd) != 'E')
379 if (_IOC_DIR(cmd) == _IOC_READ) {
381 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
386 switch (_IOC_NR(cmd) & EV_MAX) {
387 case 0: bits = dev->evbit; len = EV_MAX; break;
388 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
389 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
390 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
391 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
392 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
393 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
394 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
395 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
396 default: return -EINVAL;
398 len = NBITS(len) * sizeof(long);
399 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
400 return copy_to_user(p, bits, len) ? -EFAULT : len;
403 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
405 len = NBITS(KEY_MAX) * sizeof(long);
406 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
407 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
410 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
412 len = NBITS(LED_MAX) * sizeof(long);
413 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
414 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
417 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
419 len = NBITS(SND_MAX) * sizeof(long);
420 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
421 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
424 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) {
426 len = NBITS(SW_MAX) * sizeof(long);
427 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
428 return copy_to_user(p, dev->sw, len) ? -EFAULT : len;
431 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
433 if (!dev->name) return -ENOENT;
434 len = strlen(dev->name) + 1;
435 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
436 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
439 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
441 if (!dev->phys) return -ENOENT;
442 len = strlen(dev->phys) + 1;
443 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
444 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
447 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
449 if (!dev->uniq) return -ENOENT;
450 len = strlen(dev->uniq) + 1;
451 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
452 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
455 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
457 int t = _IOC_NR(cmd) & ABS_MAX;
459 abs.value = dev->abs[t];
460 abs.minimum = dev->absmin[t];
461 abs.maximum = dev->absmax[t];
462 abs.fuzz = dev->absfuzz[t];
463 abs.flat = dev->absflat[t];
465 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
473 if (_IOC_DIR(cmd) == _IOC_WRITE) {
475 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
477 int t = _IOC_NR(cmd) & ABS_MAX;
479 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
482 dev->abs[t] = abs.value;
483 dev->absmin[t] = abs.minimum;
484 dev->absmax[t] = abs.maximum;
485 dev->absfuzz[t] = abs.fuzz;
486 dev->absflat[t] = abs.flat;
497 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
498 #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
499 #define OFF_COMPAT(x) ((x)%BITS_PER_LONG_COMPAT)
500 #define BIT_COMPAT(x) (1UL<<OFF_COMPAT(x))
501 #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
502 #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
505 #define bit_to_user(bit, max) \
508 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
509 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
510 for (i = 0; i < len / sizeof(compat_long_t); i++) \
511 if (copy_to_user((compat_long_t __user *) p + i, \
512 (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
513 sizeof(compat_long_t))) \
518 #define bit_to_user(bit, max) \
520 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
521 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
522 return copy_to_user(p, (bit), len) ? -EFAULT : len; \
526 static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
528 struct evdev_list *list = file->private_data;
529 struct evdev *evdev = list->evdev;
530 struct input_dev *dev = evdev->handle.dev;
531 struct input_absinfo abs;
532 void __user *p = compat_ptr(arg);
534 if (!evdev->exist) return -ENODEV;
546 return evdev_ioctl(file, cmd, (unsigned long) p);
550 if (_IOC_TYPE(cmd) != 'E')
553 if (_IOC_DIR(cmd) == _IOC_READ) {
555 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
559 switch (_IOC_NR(cmd) & EV_MAX) {
560 case 0: bits = dev->evbit; max = EV_MAX; break;
561 case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
562 case EV_REL: bits = dev->relbit; max = REL_MAX; break;
563 case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
564 case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
565 case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
566 case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
567 case EV_FF: bits = dev->ffbit; max = FF_MAX; break;
568 case EV_SW: bits = dev->swbit; max = SW_MAX; break;
569 default: return -EINVAL;
571 bit_to_user(bits, max);
574 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
575 bit_to_user(dev->key, KEY_MAX);
577 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
578 bit_to_user(dev->led, LED_MAX);
580 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
581 bit_to_user(dev->snd, SND_MAX);
583 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
584 bit_to_user(dev->sw, SW_MAX);
586 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
588 if (!dev->name) return -ENOENT;
589 len = strlen(dev->name) + 1;
590 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
591 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
594 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
596 if (!dev->phys) return -ENOENT;
597 len = strlen(dev->phys) + 1;
598 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
599 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
602 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
604 if (!dev->uniq) return -ENOENT;
605 len = strlen(dev->uniq) + 1;
606 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
607 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
610 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
612 int t = _IOC_NR(cmd) & ABS_MAX;
614 abs.value = dev->abs[t];
615 abs.minimum = dev->absmin[t];
616 abs.maximum = dev->absmax[t];
617 abs.fuzz = dev->absfuzz[t];
618 abs.flat = dev->absflat[t];
620 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
627 if (_IOC_DIR(cmd) == _IOC_WRITE) {
629 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
631 int t = _IOC_NR(cmd) & ABS_MAX;
633 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
636 dev->abs[t] = abs.value;
637 dev->absmin[t] = abs.minimum;
638 dev->absmax[t] = abs.maximum;
639 dev->absfuzz[t] = abs.fuzz;
640 dev->absflat[t] = abs.flat;
650 static struct file_operations evdev_fops = {
651 .owner = THIS_MODULE,
653 .write = evdev_write,
656 .release = evdev_release,
657 .unlocked_ioctl = evdev_ioctl,
659 .compat_ioctl = evdev_ioctl_compat,
661 .fasync = evdev_fasync,
665 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
668 struct class_device *cdev;
671 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
672 if (minor == EVDEV_MINORS) {
673 printk(KERN_ERR "evdev: no more free evdev devices\n");
677 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
679 memset(evdev, 0, sizeof(struct evdev));
681 INIT_LIST_HEAD(&evdev->list);
682 init_waitqueue_head(&evdev->wait);
685 evdev->minor = minor;
686 evdev->handle.dev = dev;
687 evdev->handle.name = evdev->name;
688 evdev->handle.handler = handler;
689 evdev->handle.private = evdev;
690 sprintf(evdev->name, "event%d", minor);
692 evdev_table[minor] = evdev;
694 cdev = class_device_create(&input_class, &dev->cdev,
695 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
696 dev->cdev.dev, evdev->name);
698 /* temporary symlink to keep userspace happy */
699 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
702 return &evdev->handle;
705 static void evdev_disconnect(struct input_handle *handle)
707 struct evdev *evdev = handle->private;
708 struct evdev_list *list;
710 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
711 class_device_destroy(&input_class,
712 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
716 input_close_device(handle);
717 wake_up_interruptible(&evdev->wait);
718 list_for_each_entry(list, &evdev->list, node)
719 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
724 static struct input_device_id evdev_ids[] = {
725 { .driver_info = 1 }, /* Matches all devices */
726 { }, /* Terminating zero entry */
729 MODULE_DEVICE_TABLE(input, evdev_ids);
731 static struct input_handler evdev_handler = {
732 .event = evdev_event,
733 .connect = evdev_connect,
734 .disconnect = evdev_disconnect,
736 .minor = EVDEV_MINOR_BASE,
738 .id_table = evdev_ids,
741 static int __init evdev_init(void)
743 input_register_handler(&evdev_handler);
747 static void __exit evdev_exit(void)
749 input_unregister_handler(&evdev_handler);
752 module_init(evdev_init);
753 module_exit(evdev_exit);
755 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
756 MODULE_DESCRIPTION("Input driver event char devices");
757 MODULE_LICENSE("GPL");