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 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/vmalloc.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/input/mt.h>
26 #include <linux/major.h>
27 #include <linux/device.h>
28 #include <linux/cdev.h>
29 #include "input-compat.h"
31 enum evdev_clock_type {
40 struct input_handle handle;
41 wait_queue_head_t wait;
42 struct evdev_client __rcu *grab;
43 struct list_head client_list;
44 spinlock_t client_lock; /* protects client_list */
54 unsigned int packet_head; /* [future] position of the first element of next packet */
55 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
56 struct fasync_struct *fasync;
58 struct list_head node;
62 struct input_event buffer[];
65 /* flush queued events of type @type, caller must hold client->buffer_lock */
66 static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
68 unsigned int i, head, num;
69 unsigned int mask = client->bufsize - 1;
71 struct input_event *ev;
73 BUG_ON(type == EV_SYN);
76 client->packet_head = client->tail;
78 /* init to 1 so a leading SYN_REPORT will not be dropped */
81 for (i = client->tail; i != client->head; i = (i + 1) & mask) {
82 ev = &client->buffer[i];
83 is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
85 if (ev->type == type) {
86 /* drop matched entry */
88 } else if (is_report && !num) {
89 /* drop empty SYN_REPORT groups */
91 } else if (head != i) {
92 /* move entry to fill the gap */
93 client->buffer[head].time = ev->time;
94 client->buffer[head].type = ev->type;
95 client->buffer[head].code = ev->code;
96 client->buffer[head].value = ev->value;
100 head = (head + 1) & mask;
104 client->packet_head = head;
111 static void __evdev_queue_syn_dropped(struct evdev_client *client)
113 struct input_event ev;
116 time = client->clk_type == EV_CLK_REAL ?
118 client->clk_type == EV_CLK_MONO ?
120 ktime_get_boottime();
122 ev.time = ktime_to_timeval(time);
124 ev.code = SYN_DROPPED;
127 client->buffer[client->head++] = ev;
128 client->head &= client->bufsize - 1;
130 if (unlikely(client->head == client->tail)) {
131 /* drop queue but keep our SYN_DROPPED event */
132 client->tail = (client->head - 1) & (client->bufsize - 1);
133 client->packet_head = client->tail;
137 static void evdev_queue_syn_dropped(struct evdev_client *client)
141 spin_lock_irqsave(&client->buffer_lock, flags);
142 __evdev_queue_syn_dropped(client);
143 spin_unlock_irqrestore(&client->buffer_lock, flags);
146 static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
150 if (client->clk_type == clkid)
156 client->clk_type = EV_CLK_REAL;
158 case CLOCK_MONOTONIC:
159 client->clk_type = EV_CLK_MONO;
162 client->clk_type = EV_CLK_BOOT;
169 * Flush pending events and queue SYN_DROPPED event,
170 * but only if the queue is not empty.
172 spin_lock_irqsave(&client->buffer_lock, flags);
174 if (client->head != client->tail) {
175 client->packet_head = client->head = client->tail;
176 __evdev_queue_syn_dropped(client);
179 spin_unlock_irqrestore(&client->buffer_lock, flags);
184 static void __pass_event(struct evdev_client *client,
185 const struct input_event *event)
187 client->buffer[client->head++] = *event;
188 client->head &= client->bufsize - 1;
190 if (unlikely(client->head == client->tail)) {
192 * This effectively "drops" all unconsumed events, leaving
193 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
195 client->tail = (client->head - 2) & (client->bufsize - 1);
197 client->buffer[client->tail].time = event->time;
198 client->buffer[client->tail].type = EV_SYN;
199 client->buffer[client->tail].code = SYN_DROPPED;
200 client->buffer[client->tail].value = 0;
202 client->packet_head = client->tail;
205 if (event->type == EV_SYN && event->code == SYN_REPORT) {
206 client->packet_head = client->head;
207 kill_fasync(&client->fasync, SIGIO, POLL_IN);
211 static void evdev_pass_values(struct evdev_client *client,
212 const struct input_value *vals, unsigned int count,
215 struct evdev *evdev = client->evdev;
216 const struct input_value *v;
217 struct input_event event;
223 event.time = ktime_to_timeval(ev_time[client->clk_type]);
225 /* Interrupts are disabled, just acquire the lock. */
226 spin_lock(&client->buffer_lock);
228 for (v = vals; v != vals + count; v++) {
229 event.type = v->type;
230 event.code = v->code;
231 event.value = v->value;
232 __pass_event(client, &event);
233 if (v->type == EV_SYN && v->code == SYN_REPORT)
237 spin_unlock(&client->buffer_lock);
240 wake_up_interruptible(&evdev->wait);
244 * Pass incoming events to all connected clients.
246 static void evdev_events(struct input_handle *handle,
247 const struct input_value *vals, unsigned int count)
249 struct evdev *evdev = handle->private;
250 struct evdev_client *client;
251 ktime_t ev_time[EV_CLK_MAX];
253 ev_time[EV_CLK_MONO] = ktime_get();
254 ev_time[EV_CLK_REAL] = ktime_mono_to_real(ev_time[EV_CLK_MONO]);
255 ev_time[EV_CLK_BOOT] = ktime_mono_to_any(ev_time[EV_CLK_MONO],
260 client = rcu_dereference(evdev->grab);
263 evdev_pass_values(client, vals, count, ev_time);
265 list_for_each_entry_rcu(client, &evdev->client_list, node)
266 evdev_pass_values(client, vals, count, ev_time);
272 * Pass incoming event to all connected clients.
274 static void evdev_event(struct input_handle *handle,
275 unsigned int type, unsigned int code, int value)
277 struct input_value vals[] = { { type, code, value } };
279 evdev_events(handle, vals, 1);
282 static int evdev_fasync(int fd, struct file *file, int on)
284 struct evdev_client *client = file->private_data;
286 return fasync_helper(fd, file, on, &client->fasync);
289 static int evdev_flush(struct file *file, fl_owner_t id)
291 struct evdev_client *client = file->private_data;
292 struct evdev *evdev = client->evdev;
294 mutex_lock(&evdev->mutex);
296 if (evdev->exist && !client->revoked)
297 input_flush_device(&evdev->handle, file);
299 mutex_unlock(&evdev->mutex);
303 static void evdev_free(struct device *dev)
305 struct evdev *evdev = container_of(dev, struct evdev, dev);
307 input_put_device(evdev->handle.dev);
312 * Grabs an event device (along with underlying input device).
313 * This function is called with evdev->mutex taken.
315 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
322 error = input_grab_device(&evdev->handle);
326 rcu_assign_pointer(evdev->grab, client);
331 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
333 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
334 lockdep_is_held(&evdev->mutex));
339 rcu_assign_pointer(evdev->grab, NULL);
341 input_release_device(&evdev->handle);
346 static void evdev_attach_client(struct evdev *evdev,
347 struct evdev_client *client)
349 spin_lock(&evdev->client_lock);
350 list_add_tail_rcu(&client->node, &evdev->client_list);
351 spin_unlock(&evdev->client_lock);
354 static void evdev_detach_client(struct evdev *evdev,
355 struct evdev_client *client)
357 spin_lock(&evdev->client_lock);
358 list_del_rcu(&client->node);
359 spin_unlock(&evdev->client_lock);
363 static int evdev_open_device(struct evdev *evdev)
367 retval = mutex_lock_interruptible(&evdev->mutex);
373 else if (!evdev->open++) {
374 retval = input_open_device(&evdev->handle);
379 mutex_unlock(&evdev->mutex);
383 static void evdev_close_device(struct evdev *evdev)
385 mutex_lock(&evdev->mutex);
387 if (evdev->exist && !--evdev->open)
388 input_close_device(&evdev->handle);
390 mutex_unlock(&evdev->mutex);
394 * Wake up users waiting for IO so they can disconnect from
397 static void evdev_hangup(struct evdev *evdev)
399 struct evdev_client *client;
401 spin_lock(&evdev->client_lock);
402 list_for_each_entry(client, &evdev->client_list, node)
403 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
404 spin_unlock(&evdev->client_lock);
406 wake_up_interruptible(&evdev->wait);
409 static int evdev_release(struct inode *inode, struct file *file)
411 struct evdev_client *client = file->private_data;
412 struct evdev *evdev = client->evdev;
414 mutex_lock(&evdev->mutex);
415 evdev_ungrab(evdev, client);
416 mutex_unlock(&evdev->mutex);
418 evdev_detach_client(evdev, client);
420 if (is_vmalloc_addr(client))
425 evdev_close_device(evdev);
430 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
432 unsigned int n_events =
433 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
434 EVDEV_MIN_BUFFER_SIZE);
436 return roundup_pow_of_two(n_events);
439 static int evdev_open(struct inode *inode, struct file *file)
441 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
442 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
443 unsigned int size = sizeof(struct evdev_client) +
444 bufsize * sizeof(struct input_event);
445 struct evdev_client *client;
448 client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
450 client = vzalloc(size);
454 client->bufsize = bufsize;
455 spin_lock_init(&client->buffer_lock);
456 client->evdev = evdev;
457 evdev_attach_client(evdev, client);
459 error = evdev_open_device(evdev);
461 goto err_free_client;
463 file->private_data = client;
464 nonseekable_open(inode, file);
469 evdev_detach_client(evdev, client);
474 static ssize_t evdev_write(struct file *file, const char __user *buffer,
475 size_t count, loff_t *ppos)
477 struct evdev_client *client = file->private_data;
478 struct evdev *evdev = client->evdev;
479 struct input_event event;
482 if (count != 0 && count < input_event_size())
485 retval = mutex_lock_interruptible(&evdev->mutex);
489 if (!evdev->exist || client->revoked) {
494 while (retval + input_event_size() <= count) {
496 if (input_event_from_user(buffer + retval, &event)) {
500 retval += input_event_size();
502 input_inject_event(&evdev->handle,
503 event.type, event.code, event.value);
507 mutex_unlock(&evdev->mutex);
511 static int evdev_fetch_next_event(struct evdev_client *client,
512 struct input_event *event)
516 spin_lock_irq(&client->buffer_lock);
518 have_event = client->packet_head != client->tail;
520 *event = client->buffer[client->tail++];
521 client->tail &= client->bufsize - 1;
524 spin_unlock_irq(&client->buffer_lock);
529 static ssize_t evdev_read(struct file *file, char __user *buffer,
530 size_t count, loff_t *ppos)
532 struct evdev_client *client = file->private_data;
533 struct evdev *evdev = client->evdev;
534 struct input_event event;
538 if (count != 0 && count < input_event_size())
542 if (!evdev->exist || client->revoked)
545 if (client->packet_head == client->tail &&
546 (file->f_flags & O_NONBLOCK))
550 * count == 0 is special - no IO is done but we check
551 * for error conditions (see above).
556 while (read + input_event_size() <= count &&
557 evdev_fetch_next_event(client, &event)) {
559 if (input_event_to_user(buffer + read, &event))
562 read += input_event_size();
568 if (!(file->f_flags & O_NONBLOCK)) {
569 error = wait_event_interruptible(evdev->wait,
570 client->packet_head != client->tail ||
571 !evdev->exist || client->revoked);
580 /* No kernel lock - fine */
581 static unsigned int evdev_poll(struct file *file, poll_table *wait)
583 struct evdev_client *client = file->private_data;
584 struct evdev *evdev = client->evdev;
587 poll_wait(file, &evdev->wait, wait);
589 if (evdev->exist && !client->revoked)
590 mask = POLLOUT | POLLWRNORM;
592 mask = POLLHUP | POLLERR;
594 if (client->packet_head != client->tail)
595 mask |= POLLIN | POLLRDNORM;
602 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
603 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
606 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
607 unsigned int maxlen, void __user *p, int compat)
612 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
616 for (i = 0; i < len / sizeof(compat_long_t); i++)
617 if (copy_to_user((compat_long_t __user *) p + i,
618 (compat_long_t *) bits +
619 i + 1 - ((i % 2) << 1),
620 sizeof(compat_long_t)))
623 len = BITS_TO_LONGS(maxbit) * sizeof(long);
627 if (copy_to_user(p, bits, len))
634 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
635 unsigned int maxlen, void __user *p, int compat)
638 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
639 BITS_TO_LONGS(maxbit) * sizeof(long);
644 return copy_to_user(p, bits, len) ? -EFAULT : len;
646 #endif /* __BIG_ENDIAN */
650 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
651 unsigned int maxlen, void __user *p, int compat)
653 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
658 return copy_to_user(p, bits, len) ? -EFAULT : len;
661 #endif /* CONFIG_COMPAT */
663 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
670 len = strlen(str) + 1;
674 return copy_to_user(p, str, len) ? -EFAULT : len;
677 static int handle_eviocgbit(struct input_dev *dev,
678 unsigned int type, unsigned int size,
679 void __user *p, int compat_mode)
686 case 0: bits = dev->evbit; len = EV_MAX; break;
687 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
688 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
689 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
690 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
691 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
692 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
693 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
694 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
695 default: return -EINVAL;
698 return bits_to_user(bits, len, size, p, compat_mode);
701 static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
703 struct input_keymap_entry ke = {
704 .len = sizeof(unsigned int),
707 int __user *ip = (int __user *)p;
711 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
714 error = input_get_keycode(dev, &ke);
718 if (put_user(ke.keycode, ip + 1))
724 static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
726 struct input_keymap_entry ke;
729 if (copy_from_user(&ke, p, sizeof(ke)))
732 error = input_get_keycode(dev, &ke);
736 if (copy_to_user(p, &ke, sizeof(ke)))
742 static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
744 struct input_keymap_entry ke = {
745 .len = sizeof(unsigned int),
748 int __user *ip = (int __user *)p;
750 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
753 if (get_user(ke.keycode, ip + 1))
756 return input_set_keycode(dev, &ke);
759 static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
761 struct input_keymap_entry ke;
763 if (copy_from_user(&ke, p, sizeof(ke)))
766 if (ke.len > sizeof(ke.scancode))
769 return input_set_keycode(dev, &ke);
773 * If we transfer state to the user, we should flush all pending events
774 * of the same type from the client's queue. Otherwise, they might end up
775 * with duplicate events, which can screw up client's state tracking.
776 * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
777 * event so user-space will notice missing events.
780 * We need to take event_lock before buffer_lock to avoid dead-locks. But we
781 * need the even_lock only to guarantee consistent state. We can safely release
782 * it while flushing the queue. This allows input-core to handle filters while
783 * we flush the queue.
785 static int evdev_handle_get_val(struct evdev_client *client,
786 struct input_dev *dev, unsigned int type,
787 unsigned long *bits, unsigned int maxbit,
788 unsigned int maxlen, void __user *p,
795 len = BITS_TO_LONGS(maxbit) * sizeof(unsigned long);
796 mem = kmalloc(len, GFP_KERNEL);
800 spin_lock_irq(&dev->event_lock);
801 spin_lock(&client->buffer_lock);
803 memcpy(mem, bits, len);
805 spin_unlock(&dev->event_lock);
807 __evdev_flush_queue(client, type);
809 spin_unlock_irq(&client->buffer_lock);
811 ret = bits_to_user(mem, maxbit, maxlen, p, compat);
813 evdev_queue_syn_dropped(client);
820 static int evdev_handle_mt_request(struct input_dev *dev,
824 const struct input_mt *mt = dev->mt;
829 if (get_user(code, &ip[0]))
831 if (!mt || !input_is_mt_value(code))
834 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
835 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
836 int value = input_mt_get_value(&mt->slots[i], code);
837 if (put_user(value, &ip[1 + i]))
844 static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
847 client->revoked = true;
848 evdev_ungrab(evdev, client);
849 input_flush_device(&evdev->handle, file);
850 wake_up_interruptible(&evdev->wait);
855 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
856 void __user *p, int compat_mode)
858 struct evdev_client *client = file->private_data;
859 struct evdev *evdev = client->evdev;
860 struct input_dev *dev = evdev->handle.dev;
861 struct input_absinfo abs;
862 struct ff_effect effect;
863 int __user *ip = (int __user *)p;
864 unsigned int i, t, u, v;
868 /* First we check for fixed-length commands */
872 return put_user(EV_VERSION, ip);
875 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
880 if (!test_bit(EV_REP, dev->evbit))
882 if (put_user(dev->rep[REP_DELAY], ip))
884 if (put_user(dev->rep[REP_PERIOD], ip + 1))
889 if (!test_bit(EV_REP, dev->evbit))
893 if (get_user(v, ip + 1))
896 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
897 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
902 return input_ff_erase(dev, (int)(unsigned long) p, file);
905 i = test_bit(EV_FF, dev->evbit) ?
906 dev->ff->max_effects : 0;
913 return evdev_grab(evdev, client);
915 return evdev_ungrab(evdev, client);
921 return evdev_revoke(evdev, client, file);
924 if (copy_from_user(&i, p, sizeof(unsigned int)))
927 return evdev_set_clk_type(client, i);
930 return evdev_handle_get_keycode(dev, p);
933 return evdev_handle_set_keycode(dev, p);
935 case EVIOCGKEYCODE_V2:
936 return evdev_handle_get_keycode_v2(dev, p);
938 case EVIOCSKEYCODE_V2:
939 return evdev_handle_set_keycode_v2(dev, p);
942 size = _IOC_SIZE(cmd);
944 /* Now check variable-length commands */
945 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
946 switch (EVIOC_MASK_SIZE(cmd)) {
949 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
950 size, p, compat_mode);
952 case EVIOCGMTSLOTS(0):
953 return evdev_handle_mt_request(dev, size, ip);
956 return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
957 KEY_MAX, size, p, compat_mode);
960 return evdev_handle_get_val(client, dev, EV_LED, dev->led,
961 LED_MAX, size, p, compat_mode);
964 return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
965 SND_MAX, size, p, compat_mode);
968 return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
969 SW_MAX, size, p, compat_mode);
972 return str_to_user(dev->name, size, p);
975 return str_to_user(dev->phys, size, p);
978 return str_to_user(dev->uniq, size, p);
980 case EVIOC_MASK_SIZE(EVIOCSFF):
981 if (input_ff_effect_from_user(p, size, &effect))
984 error = input_ff_upload(dev, &effect, file);
988 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
994 /* Multi-number variable-length handlers */
995 if (_IOC_TYPE(cmd) != 'E')
998 if (_IOC_DIR(cmd) == _IOC_READ) {
1000 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1001 return handle_eviocgbit(dev,
1002 _IOC_NR(cmd) & EV_MAX, size,
1005 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
1010 t = _IOC_NR(cmd) & ABS_MAX;
1011 abs = dev->absinfo[t];
1013 if (copy_to_user(p, &abs, min_t(size_t,
1014 size, sizeof(struct input_absinfo))))
1021 if (_IOC_DIR(cmd) == _IOC_WRITE) {
1023 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1028 t = _IOC_NR(cmd) & ABS_MAX;
1030 if (copy_from_user(&abs, p, min_t(size_t,
1031 size, sizeof(struct input_absinfo))))
1034 if (size < sizeof(struct input_absinfo))
1037 /* We can't change number of reserved MT slots */
1038 if (t == ABS_MT_SLOT)
1042 * Take event lock to ensure that we are not
1043 * changing device parameters in the middle
1046 spin_lock_irq(&dev->event_lock);
1047 dev->absinfo[t] = abs;
1048 spin_unlock_irq(&dev->event_lock);
1057 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1058 void __user *p, int compat_mode)
1060 struct evdev_client *client = file->private_data;
1061 struct evdev *evdev = client->evdev;
1064 retval = mutex_lock_interruptible(&evdev->mutex);
1068 if (!evdev->exist || client->revoked) {
1073 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1076 mutex_unlock(&evdev->mutex);
1080 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1082 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1085 #ifdef CONFIG_COMPAT
1086 static long evdev_ioctl_compat(struct file *file,
1087 unsigned int cmd, unsigned long arg)
1089 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
1093 static const struct file_operations evdev_fops = {
1094 .owner = THIS_MODULE,
1096 .write = evdev_write,
1099 .release = evdev_release,
1100 .unlocked_ioctl = evdev_ioctl,
1101 #ifdef CONFIG_COMPAT
1102 .compat_ioctl = evdev_ioctl_compat,
1104 .fasync = evdev_fasync,
1105 .flush = evdev_flush,
1106 .llseek = no_llseek,
1110 * Mark device non-existent. This disables writes, ioctls and
1111 * prevents new users from opening the device. Already posted
1112 * blocking reads will stay, however new ones will fail.
1114 static void evdev_mark_dead(struct evdev *evdev)
1116 mutex_lock(&evdev->mutex);
1117 evdev->exist = false;
1118 mutex_unlock(&evdev->mutex);
1121 static void evdev_cleanup(struct evdev *evdev)
1123 struct input_handle *handle = &evdev->handle;
1125 evdev_mark_dead(evdev);
1126 evdev_hangup(evdev);
1128 cdev_del(&evdev->cdev);
1130 /* evdev is marked dead so no one else accesses evdev->open */
1132 input_flush_device(handle, NULL);
1133 input_close_device(handle);
1138 * Create new evdev device. Note that input core serializes calls
1139 * to connect and disconnect.
1141 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1142 const struct input_device_id *id)
1144 struct evdev *evdev;
1149 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1152 pr_err("failed to reserve new minor: %d\n", error);
1156 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1159 goto err_free_minor;
1162 INIT_LIST_HEAD(&evdev->client_list);
1163 spin_lock_init(&evdev->client_lock);
1164 mutex_init(&evdev->mutex);
1165 init_waitqueue_head(&evdev->wait);
1166 evdev->exist = true;
1169 /* Normalize device number if it falls into legacy range */
1170 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1171 dev_no -= EVDEV_MINOR_BASE;
1172 dev_set_name(&evdev->dev, "event%d", dev_no);
1174 evdev->handle.dev = input_get_device(dev);
1175 evdev->handle.name = dev_name(&evdev->dev);
1176 evdev->handle.handler = handler;
1177 evdev->handle.private = evdev;
1179 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
1180 evdev->dev.class = &input_class;
1181 evdev->dev.parent = &dev->dev;
1182 evdev->dev.release = evdev_free;
1183 device_initialize(&evdev->dev);
1185 error = input_register_handle(&evdev->handle);
1187 goto err_free_evdev;
1189 cdev_init(&evdev->cdev, &evdev_fops);
1190 evdev->cdev.kobj.parent = &evdev->dev.kobj;
1191 error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
1193 goto err_unregister_handle;
1195 error = device_add(&evdev->dev);
1197 goto err_cleanup_evdev;
1202 evdev_cleanup(evdev);
1203 err_unregister_handle:
1204 input_unregister_handle(&evdev->handle);
1206 put_device(&evdev->dev);
1208 input_free_minor(minor);
1212 static void evdev_disconnect(struct input_handle *handle)
1214 struct evdev *evdev = handle->private;
1216 device_del(&evdev->dev);
1217 evdev_cleanup(evdev);
1218 input_free_minor(MINOR(evdev->dev.devt));
1219 input_unregister_handle(handle);
1220 put_device(&evdev->dev);
1223 static const struct input_device_id evdev_ids[] = {
1224 { .driver_info = 1 }, /* Matches all devices */
1225 { }, /* Terminating zero entry */
1228 MODULE_DEVICE_TABLE(input, evdev_ids);
1230 static struct input_handler evdev_handler = {
1231 .event = evdev_event,
1232 .events = evdev_events,
1233 .connect = evdev_connect,
1234 .disconnect = evdev_disconnect,
1235 .legacy_minors = true,
1236 .minor = EVDEV_MINOR_BASE,
1238 .id_table = evdev_ids,
1241 static int __init evdev_init(void)
1243 return input_register_handler(&evdev_handler);
1246 static void __exit evdev_exit(void)
1248 input_unregister_handler(&evdev_handler);
1251 module_init(evdev_init);
1252 module_exit(evdev_exit);
1254 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1255 MODULE_DESCRIPTION("Input driver event char devices");
1256 MODULE_LICENSE("GPL");