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;
82 retval = fasync_helper(fd, file, on, &list->fasync);
84 return retval < 0 ? retval : 0;
87 static int evdev_flush(struct file *file, fl_owner_t id)
89 struct evdev_list *list = file->private_data;
91 if (!list->evdev->exist)
94 return input_flush_device(&list->evdev->handle, file);
97 static void evdev_free(struct evdev *evdev)
99 evdev_table[evdev->minor] = NULL;
103 static int evdev_release(struct inode * inode, struct file * file)
105 struct evdev_list *list = file->private_data;
107 if (list->evdev->grab == list) {
108 input_release_device(&list->evdev->handle);
109 list->evdev->grab = NULL;
112 evdev_fasync(-1, file, 0);
113 list_del(&list->node);
115 if (!--list->evdev->open) {
116 if (list->evdev->exist)
117 input_close_device(&list->evdev->handle);
119 evdev_free(list->evdev);
126 static int evdev_open(struct inode * inode, struct file * file)
128 struct evdev_list *list;
129 int i = iminor(inode) - EVDEV_MINOR_BASE;
131 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
134 if (!(list = kzalloc(sizeof(struct evdev_list), GFP_KERNEL)))
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);
150 struct input_event_compat {
151 struct compat_timeval time;
157 /* Note to the author of this code: did it ever occur to
158 you why the ifdefs are needed? Think about it again. -AK */
160 # define COMPAT_TEST is_compat_task()
161 #elif defined(CONFIG_IA64)
162 # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
163 #elif defined(CONFIG_S390)
164 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
165 #elif defined(CONFIG_MIPS)
166 # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
168 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
171 static inline size_t evdev_event_size(void)
174 sizeof(struct input_event_compat) : sizeof(struct input_event);
177 static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
180 struct input_event_compat compat_event;
182 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
185 event->time.tv_sec = compat_event.time.tv_sec;
186 event->time.tv_usec = compat_event.time.tv_usec;
187 event->type = compat_event.type;
188 event->code = compat_event.code;
189 event->value = compat_event.value;
192 if (copy_from_user(event, buffer, sizeof(struct input_event)))
199 static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
202 struct input_event_compat compat_event;
204 compat_event.time.tv_sec = event->time.tv_sec;
205 compat_event.time.tv_usec = event->time.tv_usec;
206 compat_event.type = event->type;
207 compat_event.code = event->code;
208 compat_event.value = event->value;
210 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
214 if (copy_to_user(buffer, event, sizeof(struct input_event)))
223 static inline size_t evdev_event_size(void)
225 return sizeof(struct input_event);
228 static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
230 if (copy_from_user(event, buffer, sizeof(struct input_event)))
236 static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
238 if (copy_to_user(buffer, event, sizeof(struct input_event)))
244 #endif /* CONFIG_COMPAT */
246 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
248 struct evdev_list *list = file->private_data;
249 struct input_event event;
252 if (!list->evdev->exist)
255 while (retval < count) {
257 if (evdev_event_from_user(buffer + retval, &event))
259 input_inject_event(&list->evdev->handle, event.type, event.code, event.value);
260 retval += evdev_event_size();
266 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
268 struct evdev_list *list = file->private_data;
271 if (count < evdev_event_size())
274 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
277 retval = wait_event_interruptible(list->evdev->wait,
278 list->head != list->tail || (!list->evdev->exist));
283 if (!list->evdev->exist)
286 while (list->head != list->tail && retval + evdev_event_size() <= count) {
288 struct input_event *event = (struct input_event *) list->buffer + list->tail;
290 if (evdev_event_to_user(buffer + retval, event))
293 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
294 retval += evdev_event_size();
300 /* No kernel lock - fine */
301 static unsigned int evdev_poll(struct file *file, poll_table *wait)
303 struct evdev_list *list = file->private_data;
305 poll_wait(file, &list->evdev->wait, wait);
306 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
307 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
312 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
313 #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
316 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
317 unsigned int maxlen, void __user *p, int compat)
322 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
326 for (i = 0; i < len / sizeof(compat_long_t); i++)
327 if (copy_to_user((compat_long_t __user *) p + i,
328 (compat_long_t *) bits +
329 i + 1 - ((i % 2) << 1),
330 sizeof(compat_long_t)))
333 len = NBITS(maxbit) * sizeof(long);
337 if (copy_to_user(p, bits, len))
344 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
345 unsigned int maxlen, void __user *p, int compat)
348 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
349 NBITS(maxbit) * sizeof(long);
354 return copy_to_user(p, bits, len) ? -EFAULT : len;
356 #endif /* __BIG_ENDIAN */
360 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
361 unsigned int maxlen, void __user *p, int compat)
363 int len = NBITS(maxbit) * sizeof(long);
368 return copy_to_user(p, bits, len) ? -EFAULT : len;
371 #endif /* CONFIG_COMPAT */
373 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
380 len = strlen(str) + 1;
384 return copy_to_user(p, str, len) ? -EFAULT : len;
387 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
388 void __user *p, int compat_mode)
390 struct evdev_list *list = file->private_data;
391 struct evdev *evdev = list->evdev;
392 struct input_dev *dev = evdev->handle.dev;
393 struct input_absinfo abs;
394 struct ff_effect effect;
395 int __user *ip = (int __user *)p;
405 return put_user(EV_VERSION, ip);
408 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
413 if (!test_bit(EV_REP, dev->evbit))
415 if (put_user(dev->rep[REP_DELAY], ip))
417 if (put_user(dev->rep[REP_PERIOD], ip + 1))
422 if (!test_bit(EV_REP, dev->evbit))
426 if (get_user(v, ip + 1))
429 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
430 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
438 error = dev->getkeycode(dev, t, &v);
442 if (put_user(v, ip + 1))
448 if (get_user(t, ip) || get_user(v, ip + 1))
451 return dev->setkeycode(dev, t, v);
454 if (copy_from_user(&effect, p, sizeof(effect)))
457 error = input_ff_upload(dev, &effect, file);
459 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
465 return input_ff_erase(dev, (int)(unsigned long) p, file);
468 i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0;
477 if (input_grab_device(&evdev->handle))
482 if (evdev->grab != list)
484 input_release_device(&evdev->handle);
491 if (_IOC_TYPE(cmd) != 'E')
494 if (_IOC_DIR(cmd) == _IOC_READ) {
496 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
501 switch (_IOC_NR(cmd) & EV_MAX) {
502 case 0: bits = dev->evbit; len = EV_MAX; break;
503 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
504 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
505 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
506 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
507 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
508 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
509 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
510 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
511 default: return -EINVAL;
513 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
516 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
517 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
520 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
521 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
524 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
525 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
528 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
529 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
532 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
533 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
535 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
536 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
538 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
539 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
541 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
543 int t = _IOC_NR(cmd) & ABS_MAX;
545 abs.value = dev->abs[t];
546 abs.minimum = dev->absmin[t];
547 abs.maximum = dev->absmax[t];
548 abs.fuzz = dev->absfuzz[t];
549 abs.flat = dev->absflat[t];
551 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
559 if (_IOC_DIR(cmd) == _IOC_WRITE) {
561 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
563 int t = _IOC_NR(cmd) & ABS_MAX;
565 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
568 dev->abs[t] = abs.value;
569 dev->absmin[t] = abs.minimum;
570 dev->absmax[t] = abs.maximum;
571 dev->absfuzz[t] = abs.fuzz;
572 dev->absflat[t] = abs.flat;
581 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
583 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
587 static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
589 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
593 static const struct file_operations evdev_fops = {
594 .owner = THIS_MODULE,
596 .write = evdev_write,
599 .release = evdev_release,
600 .unlocked_ioctl = evdev_ioctl,
602 .compat_ioctl = evdev_ioctl_compat,
604 .fasync = evdev_fasync,
608 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev,
609 const struct input_device_id *id)
612 struct class_device *cdev;
615 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
616 if (minor == EVDEV_MINORS) {
617 printk(KERN_ERR "evdev: no more free evdev devices\n");
621 if (!(evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL)))
624 INIT_LIST_HEAD(&evdev->list);
625 init_waitqueue_head(&evdev->wait);
628 evdev->minor = minor;
629 evdev->handle.dev = dev;
630 evdev->handle.name = evdev->name;
631 evdev->handle.handler = handler;
632 evdev->handle.private = evdev;
633 sprintf(evdev->name, "event%d", minor);
635 evdev_table[minor] = evdev;
637 cdev = class_device_create(&input_class, &dev->cdev,
638 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
639 dev->cdev.dev, evdev->name);
641 /* temporary symlink to keep userspace happy */
642 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
645 return &evdev->handle;
648 static void evdev_disconnect(struct input_handle *handle)
650 struct evdev *evdev = handle->private;
651 struct evdev_list *list;
653 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
654 class_device_destroy(&input_class,
655 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
659 input_flush_device(handle, NULL);
660 input_close_device(handle);
661 wake_up_interruptible(&evdev->wait);
662 list_for_each_entry(list, &evdev->list, node)
663 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
668 static const struct input_device_id evdev_ids[] = {
669 { .driver_info = 1 }, /* Matches all devices */
670 { }, /* Terminating zero entry */
673 MODULE_DEVICE_TABLE(input, evdev_ids);
675 static struct input_handler evdev_handler = {
676 .event = evdev_event,
677 .connect = evdev_connect,
678 .disconnect = evdev_disconnect,
680 .minor = EVDEV_MINOR_BASE,
682 .id_table = evdev_ids,
685 static int __init evdev_init(void)
687 return input_register_handler(&evdev_handler);
690 static void __exit evdev_exit(void)
692 input_unregister_handler(&evdev_handler);
695 module_init(evdev_init);
696 module_exit(evdev_exit);
698 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
699 MODULE_DESCRIPTION("Input driver event char devices");
700 MODULE_LICENSE("GPL");