xref: /linux/drivers/input/evdev.c (revision 41e979f822b34e789560ae2752f26f4a018f5d7e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Event char devices, giving access to raw input device events.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 1999-2002 Vojtech Pavlik
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
71da177e4SLinus Torvalds  * under the terms of the GNU General Public License version 2 as published by
81da177e4SLinus Torvalds  * the Free Software Foundation.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #define EVDEV_MINOR_BASE	64
121da177e4SLinus Torvalds #define EVDEV_MINORS		32
131da177e4SLinus Torvalds #define EVDEV_BUFFER_SIZE	64
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/poll.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/input.h>
201da177e4SLinus Torvalds #include <linux/major.h>
211da177e4SLinus Torvalds #include <linux/smp_lock.h>
221da177e4SLinus Torvalds #include <linux/device.h>
231da177e4SLinus Torvalds #include <linux/devfs_fs_kernel.h>
2452658bb6SJuergen Kreileder #include <linux/compat.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds struct evdev {
271da177e4SLinus Torvalds 	int exist;
281da177e4SLinus Torvalds 	int open;
291da177e4SLinus Torvalds 	int minor;
301da177e4SLinus Torvalds 	char name[16];
311da177e4SLinus Torvalds 	struct input_handle handle;
321da177e4SLinus Torvalds 	wait_queue_head_t wait;
331da177e4SLinus Torvalds 	struct evdev_list *grab;
341da177e4SLinus Torvalds 	struct list_head list;
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct evdev_list {
381da177e4SLinus Torvalds 	struct input_event buffer[EVDEV_BUFFER_SIZE];
391da177e4SLinus Torvalds 	int head;
401da177e4SLinus Torvalds 	int tail;
411da177e4SLinus Torvalds 	struct fasync_struct *fasync;
421da177e4SLinus Torvalds 	struct evdev *evdev;
431da177e4SLinus Torvalds 	struct list_head node;
441da177e4SLinus Torvalds };
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static struct evdev *evdev_table[EVDEV_MINORS];
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
491da177e4SLinus Torvalds {
501da177e4SLinus Torvalds 	struct evdev *evdev = handle->private;
511da177e4SLinus Torvalds 	struct evdev_list *list;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	if (evdev->grab) {
541da177e4SLinus Torvalds 		list = evdev->grab;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 		do_gettimeofday(&list->buffer[list->head].time);
571da177e4SLinus Torvalds 		list->buffer[list->head].type = type;
581da177e4SLinus Torvalds 		list->buffer[list->head].code = code;
591da177e4SLinus Torvalds 		list->buffer[list->head].value = value;
601da177e4SLinus Torvalds 		list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 		kill_fasync(&list->fasync, SIGIO, POLL_IN);
631da177e4SLinus Torvalds 	} else
641da177e4SLinus Torvalds 		list_for_each_entry(list, &evdev->list, node) {
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 			do_gettimeofday(&list->buffer[list->head].time);
671da177e4SLinus Torvalds 			list->buffer[list->head].type = type;
681da177e4SLinus Torvalds 			list->buffer[list->head].code = code;
691da177e4SLinus Torvalds 			list->buffer[list->head].value = value;
701da177e4SLinus Torvalds 			list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 			kill_fasync(&list->fasync, SIGIO, POLL_IN);
731da177e4SLinus Torvalds 		}
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	wake_up_interruptible(&evdev->wait);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static int evdev_fasync(int fd, struct file *file, int on)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	int retval;
811da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
821da177e4SLinus Torvalds 	retval = fasync_helper(fd, file, on, &list->fasync);
831da177e4SLinus Torvalds 	return retval < 0 ? retval : 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static int evdev_flush(struct file * file)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
891da177e4SLinus Torvalds 	if (!list->evdev->exist) return -ENODEV;
901da177e4SLinus Torvalds 	return input_flush_device(&list->evdev->handle, file);
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static void evdev_free(struct evdev *evdev)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	evdev_table[evdev->minor] = NULL;
961da177e4SLinus Torvalds 	kfree(evdev);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static int evdev_release(struct inode * inode, struct file * file)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	if (list->evdev->grab == list) {
1041da177e4SLinus Torvalds 		input_release_device(&list->evdev->handle);
1051da177e4SLinus Torvalds 		list->evdev->grab = NULL;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	evdev_fasync(-1, file, 0);
1091da177e4SLinus Torvalds 	list_del(&list->node);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	if (!--list->evdev->open) {
1121da177e4SLinus Torvalds 		if (list->evdev->exist)
1131da177e4SLinus Torvalds 			input_close_device(&list->evdev->handle);
1141da177e4SLinus Torvalds 		else
1151da177e4SLinus Torvalds 			evdev_free(list->evdev);
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	kfree(list);
1191da177e4SLinus Torvalds 	return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static int evdev_open(struct inode * inode, struct file * file)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	struct evdev_list *list;
1251da177e4SLinus Torvalds 	int i = iminor(inode) - EVDEV_MINOR_BASE;
1261da177e4SLinus Torvalds 	int accept_err;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
1291da177e4SLinus Torvalds 		return -ENODEV;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
1321da177e4SLinus Torvalds 		return accept_err;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
1351da177e4SLinus Torvalds 		return -ENOMEM;
1361da177e4SLinus Torvalds 	memset(list, 0, sizeof(struct evdev_list));
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	list->evdev = evdev_table[i];
1391da177e4SLinus Torvalds 	list_add_tail(&list->node, &evdev_table[i]->list);
1401da177e4SLinus Torvalds 	file->private_data = list;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	if (!list->evdev->open++)
1431da177e4SLinus Torvalds 		if (list->evdev->exist)
1441da177e4SLinus Torvalds 			input_open_device(&list->evdev->handle);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
14952658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
15052658bb6SJuergen Kreileder struct input_event_compat {
15152658bb6SJuergen Kreileder 	struct compat_timeval time;
15252658bb6SJuergen Kreileder 	__u16 type;
15352658bb6SJuergen Kreileder 	__u16 code;
15452658bb6SJuergen Kreileder 	__s32 value;
15552658bb6SJuergen Kreileder };
15652658bb6SJuergen Kreileder 
15752658bb6SJuergen Kreileder #ifdef CONFIG_X86_64
15852658bb6SJuergen Kreileder #  define COMPAT_TEST test_thread_flag(TIF_IA32)
15952658bb6SJuergen Kreileder #elif defined(CONFIG_IA64)
16052658bb6SJuergen Kreileder #  define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
16152658bb6SJuergen Kreileder #elif defined(CONFIG_ARCH_S390)
16252658bb6SJuergen Kreileder #  define COMPAT_TEST test_thread_flag(TIF_31BIT)
16352658bb6SJuergen Kreileder #else
16452658bb6SJuergen Kreileder #  define COMPAT_TEST test_thread_flag(TIF_32BIT)
16552658bb6SJuergen Kreileder #endif
16652658bb6SJuergen Kreileder 
16752658bb6SJuergen Kreileder static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
16852658bb6SJuergen Kreileder {
16952658bb6SJuergen Kreileder 	struct evdev_list *list = file->private_data;
17052658bb6SJuergen Kreileder 	struct input_event_compat event;
17152658bb6SJuergen Kreileder 	int retval = 0;
17252658bb6SJuergen Kreileder 
17352658bb6SJuergen Kreileder 	while (retval < count) {
17452658bb6SJuergen Kreileder 		if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
17552658bb6SJuergen Kreileder 			return -EFAULT;
17652658bb6SJuergen Kreileder 		input_event(list->evdev->handle.dev, event.type, event.code, event.value);
17752658bb6SJuergen Kreileder 		retval += sizeof(struct input_event_compat);
17852658bb6SJuergen Kreileder 	}
17952658bb6SJuergen Kreileder 
18052658bb6SJuergen Kreileder 	return retval;
18152658bb6SJuergen Kreileder }
18252658bb6SJuergen Kreileder #endif
18352658bb6SJuergen Kreileder 
1841da177e4SLinus Torvalds static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
1871da177e4SLinus Torvalds 	struct input_event event;
1881da177e4SLinus Torvalds 	int retval = 0;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (!list->evdev->exist) return -ENODEV;
1911da177e4SLinus Torvalds 
19252658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
19352658bb6SJuergen Kreileder 	if (COMPAT_TEST)
19452658bb6SJuergen Kreileder 		return evdev_write_compat(file, buffer, count, ppos);
19552658bb6SJuergen Kreileder #endif
19652658bb6SJuergen Kreileder 
1971da177e4SLinus Torvalds 	while (retval < count) {
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 		if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
2001da177e4SLinus Torvalds 			return -EFAULT;
2011da177e4SLinus Torvalds 		input_event(list->evdev->handle.dev, event.type, event.code, event.value);
2021da177e4SLinus Torvalds 		retval += sizeof(struct input_event);
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	return retval;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
20852658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
20952658bb6SJuergen Kreileder static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
21052658bb6SJuergen Kreileder {
21152658bb6SJuergen Kreileder 	struct evdev_list *list = file->private_data;
21252658bb6SJuergen Kreileder 	int retval;
21352658bb6SJuergen Kreileder 
21452658bb6SJuergen Kreileder 	if (count < sizeof(struct input_event_compat))
21552658bb6SJuergen Kreileder 		return -EINVAL;
21652658bb6SJuergen Kreileder 
21752658bb6SJuergen Kreileder 	if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
21852658bb6SJuergen Kreileder 		return -EAGAIN;
21952658bb6SJuergen Kreileder 
22052658bb6SJuergen Kreileder 	retval = wait_event_interruptible(list->evdev->wait,
22152658bb6SJuergen Kreileder 		list->head != list->tail || (!list->evdev->exist));
22252658bb6SJuergen Kreileder 
22352658bb6SJuergen Kreileder 	if (retval)
22452658bb6SJuergen Kreileder 		return retval;
22552658bb6SJuergen Kreileder 
22652658bb6SJuergen Kreileder 	if (!list->evdev->exist)
22752658bb6SJuergen Kreileder 		return -ENODEV;
22852658bb6SJuergen Kreileder 
22952658bb6SJuergen Kreileder 	while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
23052658bb6SJuergen Kreileder 		struct input_event *event = (struct input_event *) list->buffer + list->tail;
23152658bb6SJuergen Kreileder 		struct input_event_compat event_compat;
23252658bb6SJuergen Kreileder 		event_compat.time.tv_sec = event->time.tv_sec;
23352658bb6SJuergen Kreileder 		event_compat.time.tv_usec = event->time.tv_usec;
23452658bb6SJuergen Kreileder 		event_compat.type = event->type;
23552658bb6SJuergen Kreileder 		event_compat.code = event->code;
23652658bb6SJuergen Kreileder 		event_compat.value = event->value;
23752658bb6SJuergen Kreileder 
23852658bb6SJuergen Kreileder 		if (copy_to_user(buffer + retval, &event_compat,
23952658bb6SJuergen Kreileder 			sizeof(struct input_event_compat))) return -EFAULT;
24052658bb6SJuergen Kreileder 		list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
24152658bb6SJuergen Kreileder 		retval += sizeof(struct input_event_compat);
24252658bb6SJuergen Kreileder 	}
24352658bb6SJuergen Kreileder 
24452658bb6SJuergen Kreileder 	return retval;
24552658bb6SJuergen Kreileder }
24652658bb6SJuergen Kreileder #endif
24752658bb6SJuergen Kreileder 
2481da177e4SLinus Torvalds static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
2511da177e4SLinus Torvalds 	int retval;
2521da177e4SLinus Torvalds 
25352658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
25452658bb6SJuergen Kreileder 	if (COMPAT_TEST)
25552658bb6SJuergen Kreileder 		return evdev_read_compat(file, buffer, count, ppos);
25652658bb6SJuergen Kreileder #endif
25752658bb6SJuergen Kreileder 
2581da177e4SLinus Torvalds 	if (count < sizeof(struct input_event))
2591da177e4SLinus Torvalds 		return -EINVAL;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
2621da177e4SLinus Torvalds 		return -EAGAIN;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	retval = wait_event_interruptible(list->evdev->wait,
2651da177e4SLinus Torvalds 		list->head != list->tail || (!list->evdev->exist));
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (retval)
2681da177e4SLinus Torvalds 		return retval;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (!list->evdev->exist)
2711da177e4SLinus Torvalds 		return -ENODEV;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
2741da177e4SLinus Torvalds 		if (copy_to_user(buffer + retval, list->buffer + list->tail,
2751da177e4SLinus Torvalds 			sizeof(struct input_event))) return -EFAULT;
2761da177e4SLinus Torvalds 		list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
2771da177e4SLinus Torvalds 		retval += sizeof(struct input_event);
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	return retval;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /* No kernel lock - fine */
2841da177e4SLinus Torvalds static unsigned int evdev_poll(struct file *file, poll_table *wait)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
2871da177e4SLinus Torvalds 	poll_wait(file, &list->evdev->wait, wait);
2881da177e4SLinus Torvalds 	return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
2891da177e4SLinus Torvalds 		(list->evdev->exist ? 0 : (POLLHUP | POLLERR));
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
29252658bb6SJuergen Kreileder static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct evdev_list *list = file->private_data;
2951da177e4SLinus Torvalds 	struct evdev *evdev = list->evdev;
2961da177e4SLinus Torvalds 	struct input_dev *dev = evdev->handle.dev;
2971da177e4SLinus Torvalds 	struct input_absinfo abs;
2981da177e4SLinus Torvalds 	void __user *p = (void __user *)arg;
2991da177e4SLinus Torvalds 	int __user *ip = (int __user *)arg;
3001da177e4SLinus Torvalds 	int i, t, u, v;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (!evdev->exist) return -ENODEV;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	switch (cmd) {
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		case EVIOCGVERSION:
3071da177e4SLinus Torvalds 			return put_user(EV_VERSION, ip);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 		case EVIOCGID:
3101da177e4SLinus Torvalds 			return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 		case EVIOCGKEYCODE:
3131da177e4SLinus Torvalds 			if (get_user(t, ip)) return -EFAULT;
3141da177e4SLinus Torvalds 			if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
3151da177e4SLinus Torvalds 			if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
3161da177e4SLinus Torvalds 			return 0;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 		case EVIOCSKEYCODE:
3191da177e4SLinus Torvalds 			if (get_user(t, ip)) return -EFAULT;
3201da177e4SLinus Torvalds 			if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
3211da177e4SLinus Torvalds 			if (get_user(v, ip + 1)) return -EFAULT;
3221da177e4SLinus Torvalds 			if (v < 0 || v > KEY_MAX) return -EINVAL;
3231da177e4SLinus Torvalds 			u = SET_INPUT_KEYCODE(dev, t, v);
3241da177e4SLinus Torvalds 			clear_bit(u, dev->keybit);
3251da177e4SLinus Torvalds 			set_bit(v, dev->keybit);
3261da177e4SLinus Torvalds 			for (i = 0; i < dev->keycodemax; i++)
3271da177e4SLinus Torvalds 				if (INPUT_KEYCODE(dev,i) == u)
3281da177e4SLinus Torvalds 					set_bit(u, dev->keybit);
3291da177e4SLinus Torvalds 			return 0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 		case EVIOCSFF:
3321da177e4SLinus Torvalds 			if (dev->upload_effect) {
3331da177e4SLinus Torvalds 				struct ff_effect effect;
3341da177e4SLinus Torvalds 				int err;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 				if (copy_from_user(&effect, p, sizeof(effect)))
3371da177e4SLinus Torvalds 					return -EFAULT;
3381da177e4SLinus Torvalds 				err = dev->upload_effect(dev, &effect);
3391da177e4SLinus Torvalds 				if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
3401da177e4SLinus Torvalds 					return -EFAULT;
3411da177e4SLinus Torvalds 				return err;
3421da177e4SLinus Torvalds 			}
3431da177e4SLinus Torvalds 			else return -ENOSYS;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		case EVIOCRMFF:
3461da177e4SLinus Torvalds 			if (dev->erase_effect) {
3471da177e4SLinus Torvalds 				return dev->erase_effect(dev, (int)arg);
3481da177e4SLinus Torvalds 			}
3491da177e4SLinus Torvalds 			else return -ENOSYS;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		case EVIOCGEFFECTS:
3521da177e4SLinus Torvalds 			if (put_user(dev->ff_effects_max, ip))
3531da177e4SLinus Torvalds 				return -EFAULT;
3541da177e4SLinus Torvalds 			return 0;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		case EVIOCGRAB:
3571da177e4SLinus Torvalds 			if (arg) {
3581da177e4SLinus Torvalds 				if (evdev->grab)
3591da177e4SLinus Torvalds 					return -EBUSY;
3601da177e4SLinus Torvalds 				if (input_grab_device(&evdev->handle))
3611da177e4SLinus Torvalds 					return -EBUSY;
3621da177e4SLinus Torvalds 				evdev->grab = list;
3631da177e4SLinus Torvalds 				return 0;
3641da177e4SLinus Torvalds 			} else {
3651da177e4SLinus Torvalds 				if (evdev->grab != list)
3661da177e4SLinus Torvalds 					return -EINVAL;
3671da177e4SLinus Torvalds 				input_release_device(&evdev->handle);
3681da177e4SLinus Torvalds 				evdev->grab = NULL;
3691da177e4SLinus Torvalds 				return 0;
3701da177e4SLinus Torvalds 			}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 		default:
3731da177e4SLinus Torvalds 
374*41e979f8SVojtech Pavlik 			if (_IOC_TYPE(cmd) != 'E')
3751da177e4SLinus Torvalds 				return -EINVAL;
3761da177e4SLinus Torvalds 
377*41e979f8SVojtech Pavlik 			if (_IOC_DIR(cmd) == _IOC_READ) {
378*41e979f8SVojtech Pavlik 
3791da177e4SLinus Torvalds 				if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 					long *bits;
3821da177e4SLinus Torvalds 					int len;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 					switch (_IOC_NR(cmd) & EV_MAX) {
3851da177e4SLinus Torvalds 						case      0: bits = dev->evbit;  len = EV_MAX;  break;
3861da177e4SLinus Torvalds 						case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
3871da177e4SLinus Torvalds 						case EV_REL: bits = dev->relbit; len = REL_MAX; break;
3881da177e4SLinus Torvalds 						case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
3891da177e4SLinus Torvalds 						case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
3901da177e4SLinus Torvalds 						case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
3911da177e4SLinus Torvalds 						case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
3921da177e4SLinus Torvalds 						case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
3931da177e4SLinus Torvalds 						default: return -EINVAL;
3941da177e4SLinus Torvalds 					}
3951da177e4SLinus Torvalds 					len = NBITS(len) * sizeof(long);
3961da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
3971da177e4SLinus Torvalds 					return copy_to_user(p, bits, len) ? -EFAULT : len;
3981da177e4SLinus Torvalds 				}
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
4011da177e4SLinus Torvalds 					int len;
4021da177e4SLinus Torvalds 					len = NBITS(KEY_MAX) * sizeof(long);
4031da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4041da177e4SLinus Torvalds 					return copy_to_user(p, dev->key, len) ? -EFAULT : len;
4051da177e4SLinus Torvalds 				}
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
4081da177e4SLinus Torvalds 					int len;
4091da177e4SLinus Torvalds 					len = NBITS(LED_MAX) * sizeof(long);
4101da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4111da177e4SLinus Torvalds 					return copy_to_user(p, dev->led, len) ? -EFAULT : len;
4121da177e4SLinus Torvalds 				}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
4151da177e4SLinus Torvalds 					int len;
4161da177e4SLinus Torvalds 					len = NBITS(SND_MAX) * sizeof(long);
4171da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4181da177e4SLinus Torvalds 					return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
4191da177e4SLinus Torvalds 				}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
4221da177e4SLinus Torvalds 					int len;
4231da177e4SLinus Torvalds 					if (!dev->name) return -ENOENT;
4241da177e4SLinus Torvalds 					len = strlen(dev->name) + 1;
4251da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4261da177e4SLinus Torvalds 					return copy_to_user(p, dev->name, len) ? -EFAULT : len;
4271da177e4SLinus Torvalds 				}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
4301da177e4SLinus Torvalds 					int len;
4311da177e4SLinus Torvalds 					if (!dev->phys) return -ENOENT;
4321da177e4SLinus Torvalds 					len = strlen(dev->phys) + 1;
4331da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4341da177e4SLinus Torvalds 					return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
4351da177e4SLinus Torvalds 				}
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
4381da177e4SLinus Torvalds 					int len;
4391da177e4SLinus Torvalds 					if (!dev->uniq) return -ENOENT;
4401da177e4SLinus Torvalds 					len = strlen(dev->uniq) + 1;
4411da177e4SLinus Torvalds 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
4421da177e4SLinus Torvalds 					return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
4431da177e4SLinus Torvalds 				}
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 				if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 					int t = _IOC_NR(cmd) & ABS_MAX;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 					abs.value = dev->abs[t];
4501da177e4SLinus Torvalds 					abs.minimum = dev->absmin[t];
4511da177e4SLinus Torvalds 					abs.maximum = dev->absmax[t];
4521da177e4SLinus Torvalds 					abs.fuzz = dev->absfuzz[t];
4531da177e4SLinus Torvalds 					abs.flat = dev->absflat[t];
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 					if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
4561da177e4SLinus Torvalds 						return -EFAULT;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 					return 0;
4591da177e4SLinus Torvalds 				}
4601da177e4SLinus Torvalds 
461*41e979f8SVojtech Pavlik 			}
462*41e979f8SVojtech Pavlik 
463*41e979f8SVojtech Pavlik 			if (_IOC_DIR(cmd) == _IOC_WRITE) {
464*41e979f8SVojtech Pavlik 
4651da177e4SLinus Torvalds 				if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 					int t = _IOC_NR(cmd) & ABS_MAX;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 					if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
4701da177e4SLinus Torvalds 						return -EFAULT;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 					dev->abs[t] = abs.value;
4731da177e4SLinus Torvalds 					dev->absmin[t] = abs.minimum;
4741da177e4SLinus Torvalds 					dev->absmax[t] = abs.maximum;
4751da177e4SLinus Torvalds 					dev->absfuzz[t] = abs.fuzz;
4761da177e4SLinus Torvalds 					dev->absflat[t] = abs.flat;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 					return 0;
4791da177e4SLinus Torvalds 				}
4801da177e4SLinus Torvalds 			}
481*41e979f8SVojtech Pavlik 	}
4821da177e4SLinus Torvalds 	return -EINVAL;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
48552658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
48652658bb6SJuergen Kreileder 
48752658bb6SJuergen Kreileder #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
48852658bb6SJuergen Kreileder #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
48952658bb6SJuergen Kreileder #define OFF_COMPAT(x)  ((x)%BITS_PER_LONG_COMPAT)
49052658bb6SJuergen Kreileder #define BIT_COMPAT(x)  (1UL<<OFF_COMPAT(x))
49152658bb6SJuergen Kreileder #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
49252658bb6SJuergen Kreileder #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
49352658bb6SJuergen Kreileder 
494*41e979f8SVojtech Pavlik #ifdef __BIG_ENDIAN
495*41e979f8SVojtech Pavlik #define bit_to_user(bit, max) \
496*41e979f8SVojtech Pavlik do { \
497*41e979f8SVojtech Pavlik 	int i; \
498*41e979f8SVojtech Pavlik 	int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
499*41e979f8SVojtech Pavlik 	if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
500*41e979f8SVojtech Pavlik 	for (i = 0; i < len / sizeof(compat_long_t); i++) \
501*41e979f8SVojtech Pavlik 		if (copy_to_user((compat_long_t*) p + i, \
502*41e979f8SVojtech Pavlik 				 (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
503*41e979f8SVojtech Pavlik 				 sizeof(compat_long_t))) \
504*41e979f8SVojtech Pavlik 			return -EFAULT; \
505*41e979f8SVojtech Pavlik 	return len; \
506*41e979f8SVojtech Pavlik } while (0)
507*41e979f8SVojtech Pavlik #else
508*41e979f8SVojtech Pavlik #define bit_to_user(bit, max) \
509*41e979f8SVojtech Pavlik do { \
510*41e979f8SVojtech Pavlik 	int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
511*41e979f8SVojtech Pavlik 	if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
512*41e979f8SVojtech Pavlik 	return copy_to_user(p, (bit), len) ? -EFAULT : len; \
513*41e979f8SVojtech Pavlik } while (0)
514*41e979f8SVojtech Pavlik #endif
515*41e979f8SVojtech Pavlik 
51652658bb6SJuergen Kreileder static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
51752658bb6SJuergen Kreileder {
51852658bb6SJuergen Kreileder 	struct evdev_list *list = file->private_data;
51952658bb6SJuergen Kreileder 	struct evdev *evdev = list->evdev;
52052658bb6SJuergen Kreileder 	struct input_dev *dev = evdev->handle.dev;
52152658bb6SJuergen Kreileder 	struct input_absinfo abs;
52252658bb6SJuergen Kreileder 	void __user *p = compat_ptr(arg);
52352658bb6SJuergen Kreileder 
52452658bb6SJuergen Kreileder 	if (!evdev->exist) return -ENODEV;
52552658bb6SJuergen Kreileder 
52652658bb6SJuergen Kreileder 	switch (cmd) {
52752658bb6SJuergen Kreileder 
52852658bb6SJuergen Kreileder 		case EVIOCGVERSION:
52952658bb6SJuergen Kreileder 		case EVIOCGID:
53052658bb6SJuergen Kreileder 		case EVIOCGKEYCODE:
53152658bb6SJuergen Kreileder 		case EVIOCSKEYCODE:
53252658bb6SJuergen Kreileder 		case EVIOCSFF:
53352658bb6SJuergen Kreileder 		case EVIOCRMFF:
53452658bb6SJuergen Kreileder 		case EVIOCGEFFECTS:
53552658bb6SJuergen Kreileder 		case EVIOCGRAB:
53652658bb6SJuergen Kreileder 			return evdev_ioctl(file, cmd, (unsigned long) p);
53752658bb6SJuergen Kreileder 
53852658bb6SJuergen Kreileder 		default:
53952658bb6SJuergen Kreileder 
540*41e979f8SVojtech Pavlik 			if (_IOC_TYPE(cmd) != 'E')
54152658bb6SJuergen Kreileder 				return -EINVAL;
54252658bb6SJuergen Kreileder 
543*41e979f8SVojtech Pavlik 			if (_IOC_DIR(cmd) == _IOC_READ) {
54452658bb6SJuergen Kreileder 
545*41e979f8SVojtech Pavlik 				if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
54652658bb6SJuergen Kreileder 					long *bits;
547*41e979f8SVojtech Pavlik 					int max;
54852658bb6SJuergen Kreileder 
54952658bb6SJuergen Kreileder 					switch (_IOC_NR(cmd) & EV_MAX) {
550*41e979f8SVojtech Pavlik 						case      0: bits = dev->evbit;  max = EV_MAX;  break;
551*41e979f8SVojtech Pavlik 						case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
552*41e979f8SVojtech Pavlik 						case EV_REL: bits = dev->relbit; max = REL_MAX; break;
553*41e979f8SVojtech Pavlik 						case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
554*41e979f8SVojtech Pavlik 						case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
555*41e979f8SVojtech Pavlik 						case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
556*41e979f8SVojtech Pavlik 						case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
557*41e979f8SVojtech Pavlik 						case EV_FF:  bits = dev->ffbit;  max = FF_MAX;  break;
55852658bb6SJuergen Kreileder 						default: return -EINVAL;
55952658bb6SJuergen Kreileder 					}
560*41e979f8SVojtech Pavlik 					bit_to_user(bits, max);
56152658bb6SJuergen Kreileder 				}
56252658bb6SJuergen Kreileder 
563*41e979f8SVojtech Pavlik 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
564*41e979f8SVojtech Pavlik 					bit_to_user(dev->key, KEY_MAX);
56552658bb6SJuergen Kreileder 
566*41e979f8SVojtech Pavlik 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
567*41e979f8SVojtech Pavlik 					bit_to_user(dev->led, LED_MAX);
56852658bb6SJuergen Kreileder 
569*41e979f8SVojtech Pavlik 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
570*41e979f8SVojtech Pavlik 					bit_to_user(dev->snd, SND_MAX);
57152658bb6SJuergen Kreileder 
57252658bb6SJuergen Kreileder 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
57352658bb6SJuergen Kreileder 					int len;
57452658bb6SJuergen Kreileder 					if (!dev->name) return -ENOENT;
57552658bb6SJuergen Kreileder 					len = strlen(dev->name) + 1;
57652658bb6SJuergen Kreileder 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
57752658bb6SJuergen Kreileder 					return copy_to_user(p, dev->name, len) ? -EFAULT : len;
57852658bb6SJuergen Kreileder 				}
57952658bb6SJuergen Kreileder 
58052658bb6SJuergen Kreileder 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
58152658bb6SJuergen Kreileder 					int len;
58252658bb6SJuergen Kreileder 					if (!dev->phys) return -ENOENT;
58352658bb6SJuergen Kreileder 					len = strlen(dev->phys) + 1;
58452658bb6SJuergen Kreileder 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
58552658bb6SJuergen Kreileder 					return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
58652658bb6SJuergen Kreileder 				}
58752658bb6SJuergen Kreileder 
58852658bb6SJuergen Kreileder 				if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
58952658bb6SJuergen Kreileder 					int len;
59052658bb6SJuergen Kreileder 					if (!dev->uniq) return -ENOENT;
59152658bb6SJuergen Kreileder 					len = strlen(dev->uniq) + 1;
59252658bb6SJuergen Kreileder 					if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
59352658bb6SJuergen Kreileder 					return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
59452658bb6SJuergen Kreileder 				}
59552658bb6SJuergen Kreileder 
59652658bb6SJuergen Kreileder 				if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
59752658bb6SJuergen Kreileder 
59852658bb6SJuergen Kreileder 					int t = _IOC_NR(cmd) & ABS_MAX;
59952658bb6SJuergen Kreileder 
60052658bb6SJuergen Kreileder 					abs.value = dev->abs[t];
60152658bb6SJuergen Kreileder 					abs.minimum = dev->absmin[t];
60252658bb6SJuergen Kreileder 					abs.maximum = dev->absmax[t];
60352658bb6SJuergen Kreileder 					abs.fuzz = dev->absfuzz[t];
60452658bb6SJuergen Kreileder 					abs.flat = dev->absflat[t];
60552658bb6SJuergen Kreileder 
60652658bb6SJuergen Kreileder 					if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
60752658bb6SJuergen Kreileder 						return -EFAULT;
60852658bb6SJuergen Kreileder 
60952658bb6SJuergen Kreileder 					return 0;
61052658bb6SJuergen Kreileder 				}
611*41e979f8SVojtech Pavlik 			}
612*41e979f8SVojtech Pavlik 
613*41e979f8SVojtech Pavlik 			if (_IOC_DIR(cmd) == _IOC_WRITE) {
61452658bb6SJuergen Kreileder 
61552658bb6SJuergen Kreileder 				if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
61652658bb6SJuergen Kreileder 
61752658bb6SJuergen Kreileder 					int t = _IOC_NR(cmd) & ABS_MAX;
61852658bb6SJuergen Kreileder 
61952658bb6SJuergen Kreileder 					if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
62052658bb6SJuergen Kreileder 						return -EFAULT;
62152658bb6SJuergen Kreileder 
62252658bb6SJuergen Kreileder 					dev->abs[t] = abs.value;
62352658bb6SJuergen Kreileder 					dev->absmin[t] = abs.minimum;
62452658bb6SJuergen Kreileder 					dev->absmax[t] = abs.maximum;
62552658bb6SJuergen Kreileder 					dev->absfuzz[t] = abs.fuzz;
62652658bb6SJuergen Kreileder 					dev->absflat[t] = abs.flat;
62752658bb6SJuergen Kreileder 
62852658bb6SJuergen Kreileder 					return 0;
62952658bb6SJuergen Kreileder 				}
63052658bb6SJuergen Kreileder 			}
631*41e979f8SVojtech Pavlik 	}
63252658bb6SJuergen Kreileder 	return -EINVAL;
63352658bb6SJuergen Kreileder }
63452658bb6SJuergen Kreileder #endif
63552658bb6SJuergen Kreileder 
6361da177e4SLinus Torvalds static struct file_operations evdev_fops = {
6371da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
6381da177e4SLinus Torvalds 	.read =		evdev_read,
6391da177e4SLinus Torvalds 	.write =	evdev_write,
6401da177e4SLinus Torvalds 	.poll =		evdev_poll,
6411da177e4SLinus Torvalds 	.open =		evdev_open,
6421da177e4SLinus Torvalds 	.release =	evdev_release,
64352658bb6SJuergen Kreileder 	.unlocked_ioctl = evdev_ioctl,
64452658bb6SJuergen Kreileder #ifdef CONFIG_COMPAT
64552658bb6SJuergen Kreileder 	.compat_ioctl =	evdev_ioctl_compat,
64652658bb6SJuergen Kreileder #endif
6471da177e4SLinus Torvalds 	.fasync =	evdev_fasync,
6481da177e4SLinus Torvalds 	.flush =	evdev_flush
6491da177e4SLinus Torvalds };
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct evdev *evdev;
6541da177e4SLinus Torvalds 	int minor;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
6571da177e4SLinus Torvalds 	if (minor == EVDEV_MINORS) {
6581da177e4SLinus Torvalds 		printk(KERN_ERR "evdev: no more free evdev devices\n");
6591da177e4SLinus Torvalds 		return NULL;
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
6631da177e4SLinus Torvalds 		return NULL;
6641da177e4SLinus Torvalds 	memset(evdev, 0, sizeof(struct evdev));
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	INIT_LIST_HEAD(&evdev->list);
6671da177e4SLinus Torvalds 	init_waitqueue_head(&evdev->wait);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	evdev->exist = 1;
6701da177e4SLinus Torvalds 	evdev->minor = minor;
6711da177e4SLinus Torvalds 	evdev->handle.dev = dev;
6721da177e4SLinus Torvalds 	evdev->handle.name = evdev->name;
6731da177e4SLinus Torvalds 	evdev->handle.handler = handler;
6741da177e4SLinus Torvalds 	evdev->handle.private = evdev;
6751da177e4SLinus Torvalds 	sprintf(evdev->name, "event%d", minor);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	evdev_table[minor] = evdev;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
6801da177e4SLinus Torvalds 			S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
6811da177e4SLinus Torvalds 	class_simple_device_add(input_class,
6821da177e4SLinus Torvalds 				MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
6831da177e4SLinus Torvalds 				dev->dev, "event%d", minor);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	return &evdev->handle;
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds static void evdev_disconnect(struct input_handle *handle)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 	struct evdev *evdev = handle->private;
6911da177e4SLinus Torvalds 	struct evdev_list *list;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	class_simple_device_remove(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
6941da177e4SLinus Torvalds 	devfs_remove("input/event%d", evdev->minor);
6951da177e4SLinus Torvalds 	evdev->exist = 0;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	if (evdev->open) {
6981da177e4SLinus Torvalds 		input_close_device(handle);
6991da177e4SLinus Torvalds 		wake_up_interruptible(&evdev->wait);
7001da177e4SLinus Torvalds 		list_for_each_entry(list, &evdev->list, node)
7011da177e4SLinus Torvalds 			kill_fasync(&list->fasync, SIGIO, POLL_HUP);
7021da177e4SLinus Torvalds 	} else
7031da177e4SLinus Torvalds 		evdev_free(evdev);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds static struct input_device_id evdev_ids[] = {
7071da177e4SLinus Torvalds 	{ .driver_info = 1 },	/* Matches all devices */
7081da177e4SLinus Torvalds 	{ },			/* Terminating zero entry */
7091da177e4SLinus Torvalds };
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds MODULE_DEVICE_TABLE(input, evdev_ids);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds static struct input_handler evdev_handler = {
7141da177e4SLinus Torvalds 	.event =	evdev_event,
7151da177e4SLinus Torvalds 	.connect =	evdev_connect,
7161da177e4SLinus Torvalds 	.disconnect =	evdev_disconnect,
7171da177e4SLinus Torvalds 	.fops =		&evdev_fops,
7181da177e4SLinus Torvalds 	.minor =	EVDEV_MINOR_BASE,
7191da177e4SLinus Torvalds 	.name =		"evdev",
7201da177e4SLinus Torvalds 	.id_table =	evdev_ids,
7211da177e4SLinus Torvalds };
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds static int __init evdev_init(void)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	input_register_handler(&evdev_handler);
7261da177e4SLinus Torvalds 	return 0;
7271da177e4SLinus Torvalds }
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds static void __exit evdev_exit(void)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	input_unregister_handler(&evdev_handler);
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds module_init(evdev_init);
7351da177e4SLinus Torvalds module_exit(evdev_exit);
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
7381da177e4SLinus Torvalds MODULE_DESCRIPTION("Input driver event char devices");
7391da177e4SLinus Torvalds MODULE_LICENSE("GPL");
740