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/devfs_fs_kernel.h>
24 #include <linux/compat.h>
31 struct input_handle handle;
32 wait_queue_head_t wait;
33 struct evdev_list *grab;
34 struct list_head list;
38 struct input_event buffer[EVDEV_BUFFER_SIZE];
41 struct fasync_struct *fasync;
43 struct list_head node;
46 static struct evdev *evdev_table[EVDEV_MINORS];
48 static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
50 struct evdev *evdev = handle->private;
51 struct evdev_list *list;
56 do_gettimeofday(&list->buffer[list->head].time);
57 list->buffer[list->head].type = type;
58 list->buffer[list->head].code = code;
59 list->buffer[list->head].value = value;
60 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
62 kill_fasync(&list->fasync, SIGIO, POLL_IN);
64 list_for_each_entry(list, &evdev->list, node) {
66 do_gettimeofday(&list->buffer[list->head].time);
67 list->buffer[list->head].type = type;
68 list->buffer[list->head].code = code;
69 list->buffer[list->head].value = value;
70 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
72 kill_fasync(&list->fasync, SIGIO, POLL_IN);
75 wake_up_interruptible(&evdev->wait);
78 static int evdev_fasync(int fd, struct file *file, int on)
81 struct evdev_list *list = file->private_data;
82 retval = fasync_helper(fd, file, on, &list->fasync);
83 return retval < 0 ? retval : 0;
86 static int evdev_flush(struct file * file)
88 struct evdev_list *list = file->private_data;
89 if (!list->evdev->exist) return -ENODEV;
90 return input_flush_device(&list->evdev->handle, file);
93 static void evdev_free(struct evdev *evdev)
95 evdev_table[evdev->minor] = NULL;
99 static int evdev_release(struct inode * inode, struct file * file)
101 struct evdev_list *list = file->private_data;
103 if (list->evdev->grab == list) {
104 input_release_device(&list->evdev->handle);
105 list->evdev->grab = NULL;
108 evdev_fasync(-1, file, 0);
109 list_del(&list->node);
111 if (!--list->evdev->open) {
112 if (list->evdev->exist)
113 input_close_device(&list->evdev->handle);
115 evdev_free(list->evdev);
122 static int evdev_open(struct inode * inode, struct file * file)
124 struct evdev_list *list;
125 int i = iminor(inode) - EVDEV_MINOR_BASE;
128 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
131 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
134 if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
136 memset(list, 0, sizeof(struct evdev_list));
138 list->evdev = evdev_table[i];
139 list_add_tail(&list->node, &evdev_table[i]->list);
140 file->private_data = list;
142 if (!list->evdev->open++)
143 if (list->evdev->exist)
144 input_open_device(&list->evdev->handle);
150 struct input_event_compat {
151 struct compat_timeval time;
158 # define COMPAT_TEST test_thread_flag(TIF_IA32)
159 #elif defined(CONFIG_IA64)
160 # define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
161 #elif defined(CONFIG_ARCH_S390)
162 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
163 #elif defined(CONFIG_MIPS)
164 # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
166 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
169 static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
171 struct evdev_list *list = file->private_data;
172 struct input_event_compat event;
175 while (retval < count) {
176 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
178 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
179 retval += sizeof(struct input_event_compat);
186 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
188 struct evdev_list *list = file->private_data;
189 struct input_event event;
192 if (!list->evdev->exist) return -ENODEV;
196 return evdev_write_compat(file, buffer, count, ppos);
199 while (retval < count) {
201 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
203 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
204 retval += sizeof(struct input_event);
211 static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
213 struct evdev_list *list = file->private_data;
216 if (count < sizeof(struct input_event_compat))
219 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
222 retval = wait_event_interruptible(list->evdev->wait,
223 list->head != list->tail || (!list->evdev->exist));
228 if (!list->evdev->exist)
231 while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
232 struct input_event *event = (struct input_event *) list->buffer + list->tail;
233 struct input_event_compat event_compat;
234 event_compat.time.tv_sec = event->time.tv_sec;
235 event_compat.time.tv_usec = event->time.tv_usec;
236 event_compat.type = event->type;
237 event_compat.code = event->code;
238 event_compat.value = event->value;
240 if (copy_to_user(buffer + retval, &event_compat,
241 sizeof(struct input_event_compat))) return -EFAULT;
242 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
243 retval += sizeof(struct input_event_compat);
250 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
252 struct evdev_list *list = file->private_data;
257 return evdev_read_compat(file, buffer, count, ppos);
260 if (count < sizeof(struct input_event))
263 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
266 retval = wait_event_interruptible(list->evdev->wait,
267 list->head != list->tail || (!list->evdev->exist));
272 if (!list->evdev->exist)
275 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
276 if (copy_to_user(buffer + retval, list->buffer + list->tail,
277 sizeof(struct input_event))) return -EFAULT;
278 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
279 retval += sizeof(struct input_event);
285 /* No kernel lock - fine */
286 static unsigned int evdev_poll(struct file *file, poll_table *wait)
288 struct evdev_list *list = file->private_data;
289 poll_wait(file, &list->evdev->wait, wait);
290 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
291 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
294 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
296 struct evdev_list *list = file->private_data;
297 struct evdev *evdev = list->evdev;
298 struct input_dev *dev = evdev->handle.dev;
299 struct input_absinfo abs;
300 void __user *p = (void __user *)arg;
301 int __user *ip = (int __user *)arg;
304 if (!evdev->exist) return -ENODEV;
309 return put_user(EV_VERSION, ip);
312 return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
315 if (get_user(t, ip)) return -EFAULT;
316 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
317 if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
321 if (get_user(t, ip)) return -EFAULT;
322 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
323 if (get_user(v, ip + 1)) return -EFAULT;
324 if (v < 0 || v > KEY_MAX) return -EINVAL;
325 if (v >> (dev->keycodesize * 8)) return -EINVAL;
326 u = SET_INPUT_KEYCODE(dev, t, v);
327 clear_bit(u, dev->keybit);
328 set_bit(v, dev->keybit);
329 for (i = 0; i < dev->keycodemax; i++)
330 if (INPUT_KEYCODE(dev,i) == u)
331 set_bit(u, dev->keybit);
335 if (dev->upload_effect) {
336 struct ff_effect effect;
339 if (copy_from_user(&effect, p, sizeof(effect)))
341 err = dev->upload_effect(dev, &effect);
342 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
349 if (dev->erase_effect) {
350 return dev->erase_effect(dev, (int)arg);
355 if (put_user(dev->ff_effects_max, ip))
363 if (input_grab_device(&evdev->handle))
368 if (evdev->grab != list)
370 input_release_device(&evdev->handle);
377 if (_IOC_TYPE(cmd) != 'E')
380 if (_IOC_DIR(cmd) == _IOC_READ) {
382 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
387 switch (_IOC_NR(cmd) & EV_MAX) {
388 case 0: bits = dev->evbit; len = EV_MAX; break;
389 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
390 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
391 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
392 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
393 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
394 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
395 case EV_FF: bits = dev->ffbit; len = FF_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(EVIOCGNAME(0))) {
426 if (!dev->name) return -ENOENT;
427 len = strlen(dev->name) + 1;
428 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
429 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
432 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
434 if (!dev->phys) return -ENOENT;
435 len = strlen(dev->phys) + 1;
436 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
437 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
440 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
442 if (!dev->uniq) return -ENOENT;
443 len = strlen(dev->uniq) + 1;
444 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
445 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
448 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
450 int t = _IOC_NR(cmd) & ABS_MAX;
452 abs.value = dev->abs[t];
453 abs.minimum = dev->absmin[t];
454 abs.maximum = dev->absmax[t];
455 abs.fuzz = dev->absfuzz[t];
456 abs.flat = dev->absflat[t];
458 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
466 if (_IOC_DIR(cmd) == _IOC_WRITE) {
468 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
470 int t = _IOC_NR(cmd) & ABS_MAX;
472 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
475 dev->abs[t] = abs.value;
476 dev->absmin[t] = abs.minimum;
477 dev->absmax[t] = abs.maximum;
478 dev->absfuzz[t] = abs.fuzz;
479 dev->absflat[t] = abs.flat;
490 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
491 #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
492 #define OFF_COMPAT(x) ((x)%BITS_PER_LONG_COMPAT)
493 #define BIT_COMPAT(x) (1UL<<OFF_COMPAT(x))
494 #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
495 #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
498 #define bit_to_user(bit, max) \
501 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
502 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
503 for (i = 0; i < len / sizeof(compat_long_t); i++) \
504 if (copy_to_user((compat_long_t*) p + i, \
505 (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
506 sizeof(compat_long_t))) \
511 #define bit_to_user(bit, max) \
513 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
514 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
515 return copy_to_user(p, (bit), len) ? -EFAULT : len; \
519 static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
521 struct evdev_list *list = file->private_data;
522 struct evdev *evdev = list->evdev;
523 struct input_dev *dev = evdev->handle.dev;
524 struct input_absinfo abs;
525 void __user *p = compat_ptr(arg);
527 if (!evdev->exist) return -ENODEV;
539 return evdev_ioctl(file, cmd, (unsigned long) p);
543 if (_IOC_TYPE(cmd) != 'E')
546 if (_IOC_DIR(cmd) == _IOC_READ) {
548 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
552 switch (_IOC_NR(cmd) & EV_MAX) {
553 case 0: bits = dev->evbit; max = EV_MAX; break;
554 case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
555 case EV_REL: bits = dev->relbit; max = REL_MAX; break;
556 case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
557 case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
558 case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
559 case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
560 case EV_FF: bits = dev->ffbit; max = FF_MAX; break;
561 default: return -EINVAL;
563 bit_to_user(bits, max);
566 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
567 bit_to_user(dev->key, KEY_MAX);
569 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
570 bit_to_user(dev->led, LED_MAX);
572 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
573 bit_to_user(dev->snd, SND_MAX);
575 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
577 if (!dev->name) return -ENOENT;
578 len = strlen(dev->name) + 1;
579 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
580 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
583 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
585 if (!dev->phys) return -ENOENT;
586 len = strlen(dev->phys) + 1;
587 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
588 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
591 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
593 if (!dev->uniq) return -ENOENT;
594 len = strlen(dev->uniq) + 1;
595 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
596 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
599 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
601 int t = _IOC_NR(cmd) & ABS_MAX;
603 abs.value = dev->abs[t];
604 abs.minimum = dev->absmin[t];
605 abs.maximum = dev->absmax[t];
606 abs.fuzz = dev->absfuzz[t];
607 abs.flat = dev->absflat[t];
609 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
616 if (_IOC_DIR(cmd) == _IOC_WRITE) {
618 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
620 int t = _IOC_NR(cmd) & ABS_MAX;
622 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
625 dev->abs[t] = abs.value;
626 dev->absmin[t] = abs.minimum;
627 dev->absmax[t] = abs.maximum;
628 dev->absfuzz[t] = abs.fuzz;
629 dev->absflat[t] = abs.flat;
639 static struct file_operations evdev_fops = {
640 .owner = THIS_MODULE,
642 .write = evdev_write,
645 .release = evdev_release,
646 .unlocked_ioctl = evdev_ioctl,
648 .compat_ioctl = evdev_ioctl_compat,
650 .fasync = evdev_fasync,
654 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
659 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
660 if (minor == EVDEV_MINORS) {
661 printk(KERN_ERR "evdev: no more free evdev devices\n");
665 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
667 memset(evdev, 0, sizeof(struct evdev));
669 INIT_LIST_HEAD(&evdev->list);
670 init_waitqueue_head(&evdev->wait);
673 evdev->minor = minor;
674 evdev->handle.dev = dev;
675 evdev->handle.name = evdev->name;
676 evdev->handle.handler = handler;
677 evdev->handle.private = evdev;
678 sprintf(evdev->name, "event%d", minor);
680 evdev_table[minor] = evdev;
682 devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
683 S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
684 class_device_create(input_class,
685 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
686 dev->dev, "event%d", minor);
688 return &evdev->handle;
691 static void evdev_disconnect(struct input_handle *handle)
693 struct evdev *evdev = handle->private;
694 struct evdev_list *list;
696 class_device_destroy(input_class,
697 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
698 devfs_remove("input/event%d", evdev->minor);
702 input_close_device(handle);
703 wake_up_interruptible(&evdev->wait);
704 list_for_each_entry(list, &evdev->list, node)
705 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
710 static struct input_device_id evdev_ids[] = {
711 { .driver_info = 1 }, /* Matches all devices */
712 { }, /* Terminating zero entry */
715 MODULE_DEVICE_TABLE(input, evdev_ids);
717 static struct input_handler evdev_handler = {
718 .event = evdev_event,
719 .connect = evdev_connect,
720 .disconnect = evdev_disconnect,
722 .minor = EVDEV_MINOR_BASE,
724 .id_table = evdev_ids,
727 static int __init evdev_init(void)
729 input_register_handler(&evdev_handler);
733 static void __exit evdev_exit(void)
735 input_unregister_handler(&evdev_handler);
738 module_init(evdev_init);
739 module_exit(evdev_exit);
741 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
742 MODULE_DESCRIPTION("Input driver event char devices");
743 MODULE_LICENSE("GPL");