11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #define EVDEV_MINOR_BASE 64
14 #define EVDEV_MINORS 32
15 #define EVDEV_MIN_BUFFER_SIZE 64U
16 #define EVDEV_BUF_PACKETS 8
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
25 #include <linux/device.h>
68 client->
buffer[client->
tail].time =
event->time;
82 static void evdev_pass_values(
struct evdev_client *client,
83 const struct input_value *vals,
unsigned int count,
87 const struct input_value *
v;
97 for (v = vals; v != vals +
count; v++) {
100 event.value = v->value;
101 __pass_event(client, &event);
115 static void evdev_events(
struct input_handle *
handle,
116 const struct input_value *vals,
unsigned int count)
118 struct evdev *evdev = handle->private;
130 evdev_pass_values(client, vals, count, time_mono, time_real);
133 evdev_pass_values(client, vals, count,
134 time_mono, time_real);
142 static
void evdev_event(
struct input_handle *handle,
145 struct input_value vals[] = { {
type,
code, value } };
147 evdev_events(handle, vals, 1);
150 static int evdev_fasync(
int fd,
struct file *
file,
int on)
160 struct evdev *evdev = client->
evdev;
170 retval = input_flush_device(&evdev->
handle, file);
176 static void evdev_free(
struct device *
dev)
178 struct evdev *evdev =
container_of(dev,
struct evdev, dev);
180 input_put_device(evdev->
handle.dev);
188 static int evdev_grab(
struct evdev *evdev,
struct evdev_client *client)
195 error = input_grab_device(&evdev->
handle);
204 static int evdev_ungrab(
struct evdev *evdev,
struct evdev_client *client)
207 lockdep_is_held(&evdev->
mutex));
214 input_release_device(&evdev->
handle);
219 static void evdev_attach_client(
struct evdev *evdev,
227 static void evdev_detach_client(
struct evdev *evdev,
231 list_del_rcu(&client->
node);
236 static int evdev_open_device(
struct evdev *evdev)
246 else if (!evdev->
open++) {
247 retval = input_open_device(&evdev->
handle);
256 static void evdev_close_device(
struct evdev *evdev)
261 input_close_device(&evdev->
handle);
270 static void evdev_hangup(
struct evdev *evdev)
277 spin_unlock(&evdev->client_lock);
285 struct evdev *evdev = client->
evdev;
288 evdev_ungrab(evdev, client);
291 evdev_detach_client(evdev, client);
294 evdev_close_device(evdev);
299 static unsigned int evdev_compute_buffer_size(
struct input_dev *
dev)
301 unsigned int n_events =
308 static int evdev_open(
struct inode *
inode,
struct file *file)
311 unsigned int bufsize = evdev_compute_buffer_size(evdev->
handle.dev);
324 evdev_attach_client(evdev, client);
326 error = evdev_open_device(evdev);
328 goto err_free_client;
336 evdev_detach_client(evdev, client);
341 static ssize_t evdev_write(
struct file *file,
const char __user *
buffer,
342 size_t count, loff_t *ppos)
345 struct evdev *evdev = client->
evdev;
349 if (count != 0 && count < input_event_size())
361 while (retval + input_event_size() <= count) {
367 retval += input_event_size();
369 input_inject_event(&evdev->
handle,
378 static int evdev_fetch_next_event(
struct evdev_client *client,
396 static ssize_t evdev_read(
struct file *file,
char __user *buffer,
397 size_t count, loff_t *ppos)
400 struct evdev *evdev = client->
evdev;
405 if (count != 0 && count < input_event_size())
423 while (read + input_event_size() <= count &&
424 evdev_fetch_next_event(client, &event)) {
429 read += input_event_size();
448 static unsigned int evdev_poll(
struct file *file,
poll_table *
wait)
451 struct evdev *evdev = client->
evdev;
454 poll_wait(file, &evdev->
wait, wait);
465 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
466 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
469 static int bits_to_user(
unsigned long *
bits,
unsigned int maxbit,
470 unsigned int maxlen,
void __user *
p,
int compat)
482 i + 1 - ((i % 2) << 1),
497 static int bits_to_user(
unsigned long *bits,
unsigned int maxbit,
498 unsigned int maxlen,
void __user *p,
int compat)
513 static int bits_to_user(
unsigned long *bits,
unsigned int maxbit,
514 unsigned int maxlen,
void __user *p,
int compat)
526 static int str_to_user(
const char *
str,
unsigned int maxlen,
void __user *p)
540 #define OLD_KEY_MAX 0x1ff
541 static int handle_eviocgbit(
struct input_dev *
dev,
542 unsigned int type,
unsigned int size,
543 void __user *p,
int compat_mode)
545 static unsigned long keymax_warn_time;
551 case 0: bits = dev->evbit; len =
EV_MAX;
break;
558 case EV_FF: bits = dev->ffbit; len =
FF_MAX;
break;
559 case EV_SW: bits = dev->swbit; len =
SW_MAX;
break;
570 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
571 pr_warning(
"(EVIOCGBIT): Suspicious buffer size %u, "
572 "limiting output to %zu bytes. See "
573 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
578 return bits_to_user(bits, len, size, p, compat_mode);
582 static int evdev_handle_get_keycode(
struct input_dev *
dev,
void __user *p)
585 .
len =
sizeof(
unsigned int),
595 error = input_get_keycode(dev, &ke);
605 static int evdev_handle_get_keycode_v2(
struct input_dev *dev,
void __user *p)
613 error = input_get_keycode(dev, &ke);
623 static int evdev_handle_set_keycode(
struct input_dev *dev,
void __user *p)
626 .
len =
sizeof(
unsigned int),
637 return input_set_keycode(dev, &ke);
640 static int evdev_handle_set_keycode_v2(
struct input_dev *dev,
void __user *p)
650 return input_set_keycode(dev, &ke);
653 static int evdev_handle_mt_request(
struct input_dev *dev,
657 const struct input_mt *mt = dev->mt;
664 if (!mt || !input_is_mt_value(code))
667 max_slots = (size -
sizeof(
__u32)) /
sizeof(
__s32);
668 for (i = 0; i < mt->
num_slots && i < max_slots; i++) {
669 int value = input_mt_get_value(&mt->
slots[i], code);
677 static long evdev_do_ioctl(
struct file *file,
unsigned int cmd,
678 void __user *p,
int compat_mode)
681 struct evdev *evdev = client->
evdev;
682 struct input_dev *dev = evdev->
handle.dev;
686 unsigned int i,
t,
u,
v;
728 dev->ff->max_effects : 0;
735 return evdev_grab(evdev, client);
737 return evdev_ungrab(evdev, client);
748 return evdev_handle_get_keycode(dev, p);
751 return evdev_handle_set_keycode(dev, p);
754 return evdev_handle_get_keycode_v2(dev, p);
757 return evdev_handle_set_keycode_v2(dev, p);
763 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
768 size, p, compat_mode);
771 return evdev_handle_mt_request(dev, size, ip);
780 return bits_to_user(dev->snd,
SND_MAX, size, p, compat_mode);
813 return handle_eviocgbit(dev,
823 abs = dev->absinfo[
t];
858 spin_lock_irq(&dev->event_lock);
859 dev->absinfo[
t] =
abs;
860 spin_unlock_irq(&dev->event_lock);
869 static long evdev_ioctl_handler(
struct file *file,
unsigned int cmd,
870 void __user *p,
int compat_mode)
873 struct evdev *evdev = client->
evdev;
885 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
892 static long evdev_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
894 return evdev_ioctl_handler(file, cmd, (
void __user *)arg, 0);
898 static long evdev_ioctl_compat(
struct file *file,
899 unsigned int cmd,
unsigned long arg)
901 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
908 .write = evdev_write,
911 .release = evdev_release,
912 .unlocked_ioctl = evdev_ioctl,
914 .compat_ioctl = evdev_ioctl_compat,
916 .fasync = evdev_fasync,
917 .flush = evdev_flush,
926 static void evdev_mark_dead(
struct evdev *evdev)
929 evdev->
exist =
false;
933 static void evdev_cleanup(
struct evdev *evdev)
935 struct input_handle *handle = &evdev->
handle;
937 evdev_mark_dead(evdev);
944 input_flush_device(handle,
NULL);
945 input_close_device(handle);
953 static int evdev_connect(
struct input_handler *handler,
struct input_dev *dev,
964 pr_err(
"failed to reserve new minor: %d\n", error);
968 evdev = kzalloc(
sizeof(
struct evdev),
GFP_KERNEL);
986 evdev->
handle.dev = input_get_device(dev);
987 evdev->
handle.name = dev_name(&evdev->
dev);
988 evdev->
handle.handler = handler;
989 evdev->
handle.private = evdev;
992 evdev->
dev.class = &input_class;
993 evdev->
dev.parent = &dev->dev;
994 evdev->
dev.release = evdev_free;
997 error = input_register_handle(&evdev->
handle);
1002 evdev->
cdev.kobj.parent = &evdev->
dev.kobj;
1005 goto err_unregister_handle;
1009 goto err_cleanup_evdev;
1014 evdev_cleanup(evdev);
1015 err_unregister_handle:
1016 input_unregister_handle(&evdev->
handle);
1020 input_free_minor(minor);
1024 static void evdev_disconnect(
struct input_handle *handle)
1026 struct evdev *evdev = handle->private;
1029 evdev_cleanup(evdev);
1030 input_free_minor(
MINOR(evdev->
dev.devt));
1031 input_unregister_handle(handle);
1036 { .driver_info = 1 },
1042 static struct input_handler evdev_handler = {
1043 .event = evdev_event,
1044 .events = evdev_events,
1045 .connect = evdev_connect,
1046 .disconnect = evdev_disconnect,
1047 .legacy_minors =
true,
1050 .id_table = evdev_ids,
1053 static int __init evdev_init(
void)
1055 return input_register_handler(&evdev_handler);
1058 static void __exit evdev_exit(
void)
1060 input_unregister_handler(&evdev_handler);