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>
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);
148 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
150 struct evdev_list *list = file->private_data;
151 struct input_event event;
154 if (!list->evdev->exist) return -ENODEV;
156 while (retval < count) {
158 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
160 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
161 retval += sizeof(struct input_event);
167 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
169 struct evdev_list *list = file->private_data;
172 if (count < sizeof(struct input_event))
175 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
178 retval = wait_event_interruptible(list->evdev->wait,
179 list->head != list->tail || (!list->evdev->exist));
184 if (!list->evdev->exist)
187 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
188 if (copy_to_user(buffer + retval, list->buffer + list->tail,
189 sizeof(struct input_event))) return -EFAULT;
190 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
191 retval += sizeof(struct input_event);
197 /* No kernel lock - fine */
198 static unsigned int evdev_poll(struct file *file, poll_table *wait)
200 struct evdev_list *list = file->private_data;
201 poll_wait(file, &list->evdev->wait, wait);
202 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
203 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
206 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
208 struct evdev_list *list = file->private_data;
209 struct evdev *evdev = list->evdev;
210 struct input_dev *dev = evdev->handle.dev;
211 struct input_absinfo abs;
212 void __user *p = (void __user *)arg;
213 int __user *ip = (int __user *)arg;
216 if (!evdev->exist) return -ENODEV;
221 return put_user(EV_VERSION, ip);
224 return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
227 if (get_user(t, ip)) return -EFAULT;
228 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
229 if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
233 if (get_user(t, ip)) return -EFAULT;
234 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
235 if (get_user(v, ip + 1)) return -EFAULT;
236 if (v < 0 || v > KEY_MAX) return -EINVAL;
237 u = SET_INPUT_KEYCODE(dev, t, v);
238 clear_bit(u, dev->keybit);
239 set_bit(v, dev->keybit);
240 for (i = 0; i < dev->keycodemax; i++)
241 if (INPUT_KEYCODE(dev,i) == u)
242 set_bit(u, dev->keybit);
246 if (dev->upload_effect) {
247 struct ff_effect effect;
250 if (copy_from_user(&effect, p, sizeof(effect)))
252 err = dev->upload_effect(dev, &effect);
253 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
260 if (dev->erase_effect) {
261 return dev->erase_effect(dev, (int)arg);
266 if (put_user(dev->ff_effects_max, ip))
274 if (input_grab_device(&evdev->handle))
279 if (evdev->grab != list)
281 input_release_device(&evdev->handle);
288 if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
291 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
296 switch (_IOC_NR(cmd) & EV_MAX) {
297 case 0: bits = dev->evbit; len = EV_MAX; break;
298 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
299 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
300 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
301 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
302 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
303 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
304 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
305 default: return -EINVAL;
307 len = NBITS(len) * sizeof(long);
308 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
309 return copy_to_user(p, bits, len) ? -EFAULT : len;
312 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
314 len = NBITS(KEY_MAX) * sizeof(long);
315 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
316 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
319 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
321 len = NBITS(LED_MAX) * sizeof(long);
322 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
323 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
326 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
328 len = NBITS(SND_MAX) * sizeof(long);
329 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
330 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
333 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
335 if (!dev->name) return -ENOENT;
336 len = strlen(dev->name) + 1;
337 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
338 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
341 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
343 if (!dev->phys) return -ENOENT;
344 len = strlen(dev->phys) + 1;
345 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
346 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
349 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
351 if (!dev->uniq) return -ENOENT;
352 len = strlen(dev->uniq) + 1;
353 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
354 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
357 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
359 int t = _IOC_NR(cmd) & ABS_MAX;
361 abs.value = dev->abs[t];
362 abs.minimum = dev->absmin[t];
363 abs.maximum = dev->absmax[t];
364 abs.fuzz = dev->absfuzz[t];
365 abs.flat = dev->absflat[t];
367 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
373 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
375 int t = _IOC_NR(cmd) & ABS_MAX;
377 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
380 dev->abs[t] = abs.value;
381 dev->absmin[t] = abs.minimum;
382 dev->absmax[t] = abs.maximum;
383 dev->absfuzz[t] = abs.fuzz;
384 dev->absflat[t] = abs.flat;
392 static struct file_operations evdev_fops = {
393 .owner = THIS_MODULE,
395 .write = evdev_write,
398 .release = evdev_release,
399 .ioctl = evdev_ioctl,
400 .fasync = evdev_fasync,
404 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
409 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
410 if (minor == EVDEV_MINORS) {
411 printk(KERN_ERR "evdev: no more free evdev devices\n");
415 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
417 memset(evdev, 0, sizeof(struct evdev));
419 INIT_LIST_HEAD(&evdev->list);
420 init_waitqueue_head(&evdev->wait);
423 evdev->minor = minor;
424 evdev->handle.dev = dev;
425 evdev->handle.name = evdev->name;
426 evdev->handle.handler = handler;
427 evdev->handle.private = evdev;
428 sprintf(evdev->name, "event%d", minor);
430 evdev_table[minor] = evdev;
432 devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
433 S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
434 class_device_create(input_class,
435 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
436 dev->dev, "event%d", minor);
438 return &evdev->handle;
441 static void evdev_disconnect(struct input_handle *handle)
443 struct evdev *evdev = handle->private;
444 struct evdev_list *list;
446 class_device_destroy(input_class,
447 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
448 devfs_remove("input/event%d", evdev->minor);
452 input_close_device(handle);
453 wake_up_interruptible(&evdev->wait);
454 list_for_each_entry(list, &evdev->list, node)
455 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
460 static struct input_device_id evdev_ids[] = {
461 { .driver_info = 1 }, /* Matches all devices */
462 { }, /* Terminating zero entry */
465 MODULE_DEVICE_TABLE(input, evdev_ids);
467 static struct input_handler evdev_handler = {
468 .event = evdev_event,
469 .connect = evdev_connect,
470 .disconnect = evdev_disconnect,
472 .minor = EVDEV_MINOR_BASE,
474 .id_table = evdev_ids,
477 static int __init evdev_init(void)
479 input_register_handler(&evdev_handler);
483 static void __exit evdev_exit(void)
485 input_unregister_handler(&evdev_handler);
488 module_init(evdev_init);
489 module_exit(evdev_exit);
491 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
492 MODULE_DESCRIPTION("Input driver event char devices");
493 MODULE_LICENSE("GPL");