xref: /linux/drivers/input/evdev.c (revision cf85f810f911234a06a4ef2439e8694b93b717fc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Event char devices, giving access to raw input device events.
4  *
5  * Copyright (c) 1999-2002 Vojtech Pavlik
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #define EVDEV_MINOR_BASE	64
11 #define EVDEV_MINORS		32
12 #define EVDEV_MIN_BUFFER_SIZE	64U
13 #define EVDEV_BUF_PACKETS	8
14 
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/input/mt.h>
23 #include <linux/major.h>
24 #include <linux/nospec.h>
25 #include <linux/device.h>
26 #include <linux/cdev.h>
27 #include "input-compat.h"
28 
29 struct evdev {
30 	int open;
31 	struct input_handle handle;
32 	struct evdev_client __rcu *grab;
33 	struct list_head client_list;
34 	spinlock_t client_lock; /* protects client_list */
35 	struct mutex mutex;
36 	struct device dev;
37 	struct cdev cdev;
38 	bool exist;
39 };
40 
41 struct evdev_client {
42 	unsigned int head;
43 	unsigned int tail;
44 	unsigned int packet_head; /* [future] position of the first element of next packet */
45 	spinlock_t buffer_lock; /* protects access to buffer, head and tail */
46 	wait_queue_head_t wait;
47 	struct fasync_struct *fasync;
48 	struct evdev *evdev;
49 	struct list_head node;
50 	enum input_clock_type clk_type;
51 	bool revoked;
52 	unsigned long *evmasks[EV_CNT];
53 	unsigned int bufsize;
54 	struct input_event buffer[] __counted_by(bufsize);
55 };
56 
57 static size_t evdev_get_mask_cnt(unsigned int type)
58 {
59 	static const size_t counts[EV_CNT] = {
60 		/* EV_SYN==0 is EV_CNT, _not_ SYN_CNT, see EVIOCGBIT */
61 		[EV_SYN]	= EV_CNT,
62 		[EV_KEY]	= KEY_CNT,
63 		[EV_REL]	= REL_CNT,
64 		[EV_ABS]	= ABS_CNT,
65 		[EV_MSC]	= MSC_CNT,
66 		[EV_SW]		= SW_CNT,
67 		[EV_LED]	= LED_CNT,
68 		[EV_SND]	= SND_CNT,
69 		[EV_FF]		= FF_CNT,
70 	};
71 	unsigned long mask = array_index_mask_nospec(type, EV_CNT);
72 
73 	/* Returns 0 for out-of-bounds types, including speculatively */
74 	return counts[type & mask] & mask;
75 }
76 
77 /* requires the buffer lock to be held */
78 static bool __evdev_is_filtered(struct evdev_client *client,
79 				unsigned int type,
80 				unsigned int code)
81 {
82 	unsigned long *mask;
83 	size_t cnt;
84 
85 	/* EV_SYN and unknown codes are never filtered */
86 	if (type == EV_SYN || type >= EV_CNT)
87 		return false;
88 
89 	/* first test whether the type is filtered */
90 	mask = client->evmasks[0];
91 	if (mask && !test_bit(type, mask))
92 		return true;
93 
94 	/* unknown values are never filtered */
95 	cnt = evdev_get_mask_cnt(type);
96 	if (!cnt || code >= cnt)
97 		return false;
98 
99 	mask = client->evmasks[type];
100 	return mask && !test_bit(code, mask);
101 }
102 
103 /* flush queued events of type @type, caller must hold client->buffer_lock */
104 static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
105 {
106 	unsigned int i, head, num;
107 	unsigned int mask = client->bufsize - 1;
108 	bool is_report;
109 	struct input_event *ev;
110 
111 	BUG_ON(type == EV_SYN);
112 
113 	head = client->tail;
114 	client->packet_head = client->tail;
115 
116 	/* init to 1 so a leading SYN_REPORT will not be dropped */
117 	num = 1;
118 
119 	for (i = client->tail; i != client->head; i = (i + 1) & mask) {
120 		ev = &client->buffer[i];
121 		is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
122 
123 		if (ev->type == type) {
124 			/* drop matched entry */
125 			continue;
126 		} else if (is_report && !num) {
127 			/* drop empty SYN_REPORT groups */
128 			continue;
129 		} else if (head != i) {
130 			/* move entry to fill the gap */
131 			client->buffer[head] = *ev;
132 		}
133 
134 		num++;
135 		head = (head + 1) & mask;
136 
137 		if (is_report) {
138 			num = 0;
139 			client->packet_head = head;
140 		}
141 	}
142 
143 	client->head = head;
144 }
145 
146 static void __evdev_queue_syn_dropped(struct evdev_client *client)
147 {
148 	ktime_t *ev_time = input_get_timestamp(client->evdev->handle.dev);
149 	struct timespec64 ts = ktime_to_timespec64(ev_time[client->clk_type]);
150 	struct input_event ev;
151 
152 	memset(&ev, 0, sizeof(ev));
153 	ev.input_event_sec = ts.tv_sec;
154 	ev.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
155 	ev.type = EV_SYN;
156 	ev.code = SYN_DROPPED;
157 
158 	client->buffer[client->head++] = ev;
159 	client->head &= client->bufsize - 1;
160 
161 	if (unlikely(client->head == client->tail)) {
162 		/* drop queue but keep our SYN_DROPPED event */
163 		client->tail = (client->head - 1) & (client->bufsize - 1);
164 		client->packet_head = client->tail;
165 	}
166 }
167 
168 static void evdev_queue_syn_dropped(struct evdev_client *client)
169 {
170 	unsigned long flags;
171 
172 	spin_lock_irqsave(&client->buffer_lock, flags);
173 	__evdev_queue_syn_dropped(client);
174 	spin_unlock_irqrestore(&client->buffer_lock, flags);
175 }
176 
177 static int evdev_set_clk_type(struct evdev_client *client, unsigned int clkid)
178 {
179 	unsigned long flags;
180 	enum input_clock_type clk_type;
181 
182 	switch (clkid) {
183 
184 	case CLOCK_REALTIME:
185 		clk_type = INPUT_CLK_REAL;
186 		break;
187 	case CLOCK_MONOTONIC:
188 		clk_type = INPUT_CLK_MONO;
189 		break;
190 	case CLOCK_BOOTTIME:
191 		clk_type = INPUT_CLK_BOOT;
192 		break;
193 	default:
194 		return -EINVAL;
195 	}
196 
197 	if (client->clk_type != clk_type) {
198 		client->clk_type = clk_type;
199 
200 		/*
201 		 * Flush pending events and queue SYN_DROPPED event,
202 		 * but only if the queue is not empty.
203 		 */
204 		spin_lock_irqsave(&client->buffer_lock, flags);
205 
206 		if (client->head != client->tail) {
207 			client->packet_head = client->head = client->tail;
208 			__evdev_queue_syn_dropped(client);
209 		}
210 
211 		spin_unlock_irqrestore(&client->buffer_lock, flags);
212 	}
213 
214 	return 0;
215 }
216 
217 static void __pass_event(struct evdev_client *client,
218 			 const struct input_event *event)
219 {
220 	client->buffer[client->head++] = *event;
221 	client->head &= client->bufsize - 1;
222 
223 	if (unlikely(client->head == client->tail)) {
224 		struct input_event ev;
225 
226 		memset(&ev, 0, sizeof(ev));
227 		ev.input_event_sec = event->input_event_sec;
228 		ev.input_event_usec = event->input_event_usec;
229 		ev.type = EV_SYN;
230 		ev.code = SYN_DROPPED;
231 
232 		/*
233 		 * This effectively "drops" all unconsumed events, leaving
234 		 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
235 		 */
236 		client->tail = (client->head - 2) & (client->bufsize - 1);
237 		client->buffer[client->tail] = ev;
238 		client->packet_head = client->tail;
239 	}
240 
241 	if (event->type == EV_SYN && event->code == SYN_REPORT) {
242 		client->packet_head = client->head;
243 		kill_fasync(&client->fasync, SIGIO, POLL_IN);
244 	}
245 }
246 
247 static void evdev_pass_values(struct evdev_client *client,
248 			const struct input_value *vals, unsigned int count,
249 			ktime_t *ev_time)
250 {
251 	const struct input_value *v;
252 	struct input_event event;
253 	struct timespec64 ts;
254 	bool wakeup = false;
255 
256 	if (client->revoked)
257 		return;
258 
259 	memset(&event, 0, sizeof(event));
260 
261 	ts = ktime_to_timespec64(ev_time[client->clk_type]);
262 	event.input_event_sec = ts.tv_sec;
263 	event.input_event_usec = ts.tv_nsec / NSEC_PER_USEC;
264 
265 	/* Interrupts are disabled, just acquire the lock. */
266 	spin_lock(&client->buffer_lock);
267 
268 	for (v = vals; v != vals + count; v++) {
269 		if (__evdev_is_filtered(client, v->type, v->code))
270 			continue;
271 
272 		if (v->type == EV_SYN && v->code == SYN_REPORT) {
273 			/* drop empty SYN_REPORT */
274 			if (client->packet_head == client->head)
275 				continue;
276 
277 			wakeup = true;
278 		}
279 
280 		event.type = v->type;
281 		event.code = v->code;
282 		event.value = v->value;
283 		__pass_event(client, &event);
284 	}
285 
286 	spin_unlock(&client->buffer_lock);
287 
288 	if (wakeup)
289 		wake_up_interruptible_poll(&client->wait,
290 			EPOLLIN | EPOLLOUT | EPOLLRDNORM | EPOLLWRNORM);
291 }
292 
293 /*
294  * Pass incoming events to all connected clients.
295  */
296 static unsigned int evdev_events(struct input_handle *handle,
297 				 struct input_value *vals, unsigned int count)
298 {
299 	struct evdev *evdev = handle->private;
300 	struct evdev_client *client;
301 	ktime_t *ev_time = input_get_timestamp(handle->dev);
302 
303 	rcu_read_lock();
304 
305 	client = rcu_dereference(evdev->grab);
306 
307 	if (client)
308 		evdev_pass_values(client, vals, count, ev_time);
309 	else
310 		list_for_each_entry_rcu(client, &evdev->client_list, node)
311 			evdev_pass_values(client, vals, count, ev_time);
312 
313 	rcu_read_unlock();
314 
315 	return count;
316 }
317 
318 static int evdev_fasync(int fd, struct file *file, int on)
319 {
320 	struct evdev_client *client = file->private_data;
321 
322 	return fasync_helper(fd, file, on, &client->fasync);
323 }
324 
325 static void evdev_free(struct device *dev)
326 {
327 	struct evdev *evdev = container_of(dev, struct evdev, dev);
328 
329 	input_put_device(evdev->handle.dev);
330 	kfree(evdev);
331 }
332 
333 /*
334  * Grabs an event device (along with underlying input device).
335  * This function is called with evdev->mutex taken.
336  */
337 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
338 {
339 	int error;
340 
341 	if (evdev->grab)
342 		return -EBUSY;
343 
344 	error = input_grab_device(&evdev->handle);
345 	if (error)
346 		return error;
347 
348 	rcu_assign_pointer(evdev->grab, client);
349 
350 	return 0;
351 }
352 
353 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
354 {
355 	struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
356 					lockdep_is_held(&evdev->mutex));
357 
358 	if (grab != client)
359 		return  -EINVAL;
360 
361 	rcu_assign_pointer(evdev->grab, NULL);
362 	synchronize_rcu();
363 	input_release_device(&evdev->handle);
364 
365 	return 0;
366 }
367 
368 static void evdev_attach_client(struct evdev *evdev,
369 				struct evdev_client *client)
370 {
371 	spin_lock(&evdev->client_lock);
372 	list_add_tail_rcu(&client->node, &evdev->client_list);
373 	spin_unlock(&evdev->client_lock);
374 }
375 
376 static void evdev_detach_client(struct evdev *evdev,
377 				struct evdev_client *client)
378 {
379 	spin_lock(&evdev->client_lock);
380 	list_del_rcu(&client->node);
381 	spin_unlock(&evdev->client_lock);
382 	synchronize_rcu();
383 }
384 
385 static int evdev_open_device(struct evdev *evdev)
386 {
387 	int retval;
388 
389 	retval = mutex_lock_interruptible(&evdev->mutex);
390 	if (retval)
391 		return retval;
392 
393 	if (!evdev->exist)
394 		retval = -ENODEV;
395 	else if (!evdev->open++) {
396 		retval = input_open_device(&evdev->handle);
397 		if (retval)
398 			evdev->open--;
399 	}
400 
401 	mutex_unlock(&evdev->mutex);
402 	return retval;
403 }
404 
405 static void evdev_close_device(struct evdev *evdev)
406 {
407 	mutex_lock(&evdev->mutex);
408 
409 	if (evdev->exist && !--evdev->open)
410 		input_close_device(&evdev->handle);
411 
412 	mutex_unlock(&evdev->mutex);
413 }
414 
415 /*
416  * Wake up users waiting for IO so they can disconnect from
417  * dead device.
418  */
419 static void evdev_hangup(struct evdev *evdev)
420 {
421 	struct evdev_client *client;
422 
423 	spin_lock(&evdev->client_lock);
424 	list_for_each_entry(client, &evdev->client_list, node) {
425 		kill_fasync(&client->fasync, SIGIO, POLL_HUP);
426 		wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
427 	}
428 	spin_unlock(&evdev->client_lock);
429 }
430 
431 static int evdev_release(struct inode *inode, struct file *file)
432 {
433 	struct evdev_client *client = file->private_data;
434 	struct evdev *evdev = client->evdev;
435 	unsigned int i;
436 
437 	mutex_lock(&evdev->mutex);
438 
439 	if (evdev->exist && !client->revoked)
440 		input_flush_device(&evdev->handle, file);
441 
442 	evdev_ungrab(evdev, client);
443 	mutex_unlock(&evdev->mutex);
444 
445 	evdev_detach_client(evdev, client);
446 
447 	for (i = 0; i < EV_CNT; ++i)
448 		bitmap_free(client->evmasks[i]);
449 
450 	kvfree(client);
451 
452 	evdev_close_device(evdev);
453 
454 	return 0;
455 }
456 
457 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
458 {
459 	unsigned int n_events =
460 		max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
461 		    EVDEV_MIN_BUFFER_SIZE);
462 
463 	return roundup_pow_of_two(n_events);
464 }
465 
466 static int evdev_open(struct inode *inode, struct file *file)
467 {
468 	struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
469 	unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
470 	struct evdev_client *client;
471 	int error;
472 
473 	client = kvzalloc_flex(*client, buffer, bufsize);
474 	if (!client)
475 		return -ENOMEM;
476 
477 	init_waitqueue_head(&client->wait);
478 	client->bufsize = bufsize;
479 	spin_lock_init(&client->buffer_lock);
480 	client->evdev = evdev;
481 	evdev_attach_client(evdev, client);
482 
483 	error = evdev_open_device(evdev);
484 	if (error)
485 		goto err_free_client;
486 
487 	file->private_data = client;
488 	stream_open(inode, file);
489 
490 	return 0;
491 
492  err_free_client:
493 	evdev_detach_client(evdev, client);
494 	kvfree(client);
495 	return error;
496 }
497 
498 static ssize_t evdev_write(struct file *file, const char __user *buffer,
499 			   size_t count, loff_t *ppos)
500 {
501 	struct evdev_client *client = file->private_data;
502 	struct evdev *evdev = client->evdev;
503 	struct input_event event;
504 	int retval = 0;
505 
506 	/*
507 	 * Limit amount of data we inject into the input subsystem so that
508 	 * we do not hold evdev->mutex for too long. 4096 bytes corresponds
509 	 * to 170 input events.
510 	 */
511 	count = min(count, 4096);
512 
513 	if (count != 0 && count < input_event_size())
514 		return -EINVAL;
515 
516 	retval = mutex_lock_interruptible(&evdev->mutex);
517 	if (retval)
518 		return retval;
519 
520 	if (!evdev->exist || client->revoked) {
521 		retval = -ENODEV;
522 		goto out;
523 	}
524 
525 	while (retval + input_event_size() <= count) {
526 
527 		if (input_event_from_user(buffer + retval, &event)) {
528 			retval = -EFAULT;
529 			goto out;
530 		}
531 		retval += input_event_size();
532 
533 		input_inject_event(&evdev->handle,
534 				   event.type, event.code, event.value);
535 		cond_resched();
536 	}
537 
538  out:
539 	mutex_unlock(&evdev->mutex);
540 	return retval;
541 }
542 
543 static int evdev_fetch_next_event(struct evdev_client *client,
544 				  struct input_event *event)
545 {
546 	int have_event;
547 
548 	spin_lock_irq(&client->buffer_lock);
549 
550 	have_event = client->packet_head != client->tail;
551 	if (have_event) {
552 		*event = client->buffer[client->tail++];
553 		client->tail &= client->bufsize - 1;
554 	}
555 
556 	spin_unlock_irq(&client->buffer_lock);
557 
558 	return have_event;
559 }
560 
561 static ssize_t evdev_read(struct file *file, char __user *buffer,
562 			  size_t count, loff_t *ppos)
563 {
564 	struct evdev_client *client = file->private_data;
565 	struct evdev *evdev = client->evdev;
566 	struct input_event event;
567 	size_t read = 0;
568 	int error;
569 
570 	if (count != 0 && count < input_event_size())
571 		return -EINVAL;
572 
573 	for (;;) {
574 		if (!evdev->exist || client->revoked)
575 			return -ENODEV;
576 
577 		if (client->packet_head == client->tail &&
578 		    (file->f_flags & O_NONBLOCK))
579 			return -EAGAIN;
580 
581 		/*
582 		 * count == 0 is special - no IO is done but we check
583 		 * for error conditions (see above).
584 		 */
585 		if (count == 0)
586 			break;
587 
588 		while (read + input_event_size() <= count &&
589 		       evdev_fetch_next_event(client, &event)) {
590 
591 			if (input_event_to_user(buffer + read, &event))
592 				return -EFAULT;
593 
594 			read += input_event_size();
595 		}
596 
597 		if (read)
598 			break;
599 
600 		if (!(file->f_flags & O_NONBLOCK)) {
601 			error = wait_event_interruptible(client->wait,
602 					client->packet_head != client->tail ||
603 					!evdev->exist || client->revoked);
604 			if (error)
605 				return error;
606 		}
607 	}
608 
609 	return read;
610 }
611 
612 /* No kernel lock - fine */
613 static __poll_t evdev_poll(struct file *file, poll_table *wait)
614 {
615 	struct evdev_client *client = file->private_data;
616 	struct evdev *evdev = client->evdev;
617 	__poll_t mask;
618 
619 	poll_wait(file, &client->wait, wait);
620 
621 	if (evdev->exist && !client->revoked)
622 		mask = EPOLLOUT | EPOLLWRNORM;
623 	else
624 		mask = EPOLLHUP | EPOLLERR;
625 
626 	if (client->packet_head != client->tail)
627 		mask |= EPOLLIN | EPOLLRDNORM;
628 
629 	return mask;
630 }
631 
632 #ifdef CONFIG_COMPAT
633 
634 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
635 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
636 
637 #ifdef __BIG_ENDIAN
638 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
639 			unsigned int maxlen, void __user *p, int compat)
640 {
641 	int len, i;
642 
643 	if (compat) {
644 		len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
645 		if (len > maxlen)
646 			len = maxlen;
647 
648 		for (i = 0; i < len / sizeof(compat_long_t); i++)
649 			if (copy_to_user((compat_long_t __user *) p + i,
650 					 (compat_long_t *) bits +
651 						i + 1 - ((i % 2) << 1),
652 					 sizeof(compat_long_t)))
653 				return -EFAULT;
654 	} else {
655 		len = BITS_TO_LONGS(maxbit) * sizeof(long);
656 		if (len > maxlen)
657 			len = maxlen;
658 
659 		if (copy_to_user(p, bits, len))
660 			return -EFAULT;
661 	}
662 
663 	return len;
664 }
665 
666 static int bits_from_user(unsigned long *bits, unsigned int maxbit,
667 			  unsigned int maxlen, const void __user *p, int compat)
668 {
669 	int len, i;
670 
671 	if (compat) {
672 		if (maxlen % sizeof(compat_long_t))
673 			return -EINVAL;
674 
675 		len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
676 		if (len > maxlen)
677 			len = maxlen;
678 
679 		for (i = 0; i < len / sizeof(compat_long_t); i++)
680 			if (copy_from_user((compat_long_t *) bits +
681 						i + 1 - ((i % 2) << 1),
682 					   (compat_long_t __user *) p + i,
683 					   sizeof(compat_long_t)))
684 				return -EFAULT;
685 		if (i % 2)
686 			*((compat_long_t *) bits + i - 1) = 0;
687 
688 	} else {
689 		if (maxlen % sizeof(long))
690 			return -EINVAL;
691 
692 		len = BITS_TO_LONGS(maxbit) * sizeof(long);
693 		if (len > maxlen)
694 			len = maxlen;
695 
696 		if (copy_from_user(bits, p, len))
697 			return -EFAULT;
698 	}
699 
700 	return len;
701 }
702 
703 #else
704 
705 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
706 			unsigned int maxlen, void __user *p, int compat)
707 {
708 	int len = compat ?
709 			BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
710 			BITS_TO_LONGS(maxbit) * sizeof(long);
711 
712 	if (len > maxlen)
713 		len = maxlen;
714 
715 	return copy_to_user(p, bits, len) ? -EFAULT : len;
716 }
717 
718 static int bits_from_user(unsigned long *bits, unsigned int maxbit,
719 			  unsigned int maxlen, const void __user *p, int compat)
720 {
721 	size_t chunk_size = compat ? sizeof(compat_long_t) : sizeof(long);
722 	int len;
723 
724 	if (maxlen % chunk_size)
725 		return -EINVAL;
726 
727 	len = compat ? BITS_TO_LONGS_COMPAT(maxbit) : BITS_TO_LONGS(maxbit);
728 	len *= chunk_size;
729 	if (len > maxlen)
730 		len = maxlen;
731 
732 	return copy_from_user(bits, p, len) ? -EFAULT : len;
733 }
734 
735 #endif /* __BIG_ENDIAN */
736 
737 #else
738 
739 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
740 			unsigned int maxlen, void __user *p, int compat)
741 {
742 	int len = BITS_TO_LONGS(maxbit) * sizeof(long);
743 
744 	if (len > maxlen)
745 		len = maxlen;
746 
747 	return copy_to_user(p, bits, len) ? -EFAULT : len;
748 }
749 
750 static int bits_from_user(unsigned long *bits, unsigned int maxbit,
751 			  unsigned int maxlen, const void __user *p, int compat)
752 {
753 	int len;
754 
755 	if (maxlen % sizeof(long))
756 		return -EINVAL;
757 
758 	len = BITS_TO_LONGS(maxbit) * sizeof(long);
759 	if (len > maxlen)
760 		len = maxlen;
761 
762 	return copy_from_user(bits, p, len) ? -EFAULT : len;
763 }
764 
765 #endif /* CONFIG_COMPAT */
766 
767 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
768 {
769 	int len;
770 
771 	if (!str)
772 		return -ENOENT;
773 
774 	len = strlen(str) + 1;
775 	if (len > maxlen)
776 		len = maxlen;
777 
778 	return copy_to_user(p, str, len) ? -EFAULT : len;
779 }
780 
781 static int handle_eviocgbit(struct input_dev *dev,
782 			    unsigned int type, unsigned int size,
783 			    void __user *p, int compat_mode)
784 {
785 	unsigned long *bits;
786 	int len;
787 
788 	switch (type) {
789 
790 	case      0: bits = dev->evbit;  len = EV_MAX;  break;
791 	case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
792 	case EV_REL: bits = dev->relbit; len = REL_MAX; break;
793 	case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
794 	case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
795 	case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
796 	case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
797 	case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
798 	case EV_SW:  bits = dev->swbit;  len = SW_MAX;  break;
799 	default: return -EINVAL;
800 	}
801 
802 	return bits_to_user(bits, len, size, p, compat_mode);
803 }
804 
805 static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
806 {
807 	struct input_keymap_entry ke = {
808 		.len	= sizeof(unsigned int),
809 		.flags	= 0,
810 	};
811 	int __user *ip = (int __user *)p;
812 	int error;
813 
814 	/* legacy case */
815 	if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
816 		return -EFAULT;
817 
818 	error = input_get_keycode(dev, &ke);
819 	if (error)
820 		return error;
821 
822 	if (put_user(ke.keycode, ip + 1))
823 		return -EFAULT;
824 
825 	return 0;
826 }
827 
828 static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
829 {
830 	struct input_keymap_entry ke;
831 	int error;
832 
833 	if (copy_from_user(&ke, p, sizeof(ke)))
834 		return -EFAULT;
835 
836 	error = input_get_keycode(dev, &ke);
837 	if (error)
838 		return error;
839 
840 	if (copy_to_user(p, &ke, sizeof(ke)))
841 		return -EFAULT;
842 
843 	return 0;
844 }
845 
846 static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
847 {
848 	struct input_keymap_entry ke = {
849 		.len	= sizeof(unsigned int),
850 		.flags	= 0,
851 	};
852 	int __user *ip = (int __user *)p;
853 
854 	if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
855 		return -EFAULT;
856 
857 	if (get_user(ke.keycode, ip + 1))
858 		return -EFAULT;
859 
860 	return input_set_keycode(dev, &ke);
861 }
862 
863 static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
864 {
865 	struct input_keymap_entry ke;
866 
867 	if (copy_from_user(&ke, p, sizeof(ke)))
868 		return -EFAULT;
869 
870 	if (ke.len > sizeof(ke.scancode))
871 		return -EINVAL;
872 
873 	return input_set_keycode(dev, &ke);
874 }
875 
876 /*
877  * If we transfer state to the user, we should flush all pending events
878  * of the same type from the client's queue. Otherwise, they might end up
879  * with duplicate events, which can screw up client's state tracking.
880  * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
881  * event so user-space will notice missing events.
882  *
883  * LOCKING:
884  * We need to take event_lock before buffer_lock to avoid dead-locks. But we
885  * need the even_lock only to guarantee consistent state. We can safely release
886  * it while flushing the queue. This allows input-core to handle filters while
887  * we flush the queue.
888  */
889 static int evdev_handle_get_val(struct evdev_client *client,
890 				struct input_dev *dev, unsigned int type,
891 				unsigned long *bits, unsigned int maxbit,
892 				unsigned int maxlen, void __user *p,
893 				int compat)
894 {
895 	int ret;
896 	unsigned long *mem;
897 
898 	mem = bitmap_alloc(maxbit, GFP_KERNEL);
899 	if (!mem)
900 		return -ENOMEM;
901 
902 	spin_lock_irq(&dev->event_lock);
903 	spin_lock(&client->buffer_lock);
904 
905 	bitmap_copy(mem, bits, maxbit);
906 
907 	spin_unlock(&dev->event_lock);
908 
909 	__evdev_flush_queue(client, type);
910 
911 	spin_unlock_irq(&client->buffer_lock);
912 
913 	ret = bits_to_user(mem, maxbit, maxlen, p, compat);
914 	if (ret < 0)
915 		evdev_queue_syn_dropped(client);
916 
917 	bitmap_free(mem);
918 
919 	return ret;
920 }
921 
922 static int evdev_handle_mt_request(struct input_dev *dev,
923 				   unsigned int size,
924 				   int __user *ip)
925 {
926 	const struct input_mt *mt = dev->mt;
927 	unsigned int code;
928 	int max_slots;
929 	int i;
930 
931 	if (get_user(code, &ip[0]))
932 		return -EFAULT;
933 	if (!mt || !input_is_mt_value(code))
934 		return -EINVAL;
935 
936 	max_slots = (size - sizeof(__u32)) / sizeof(__s32);
937 	for (i = 0; i < mt->num_slots && i < max_slots; i++) {
938 		int value = input_mt_get_value(&mt->slots[i], code);
939 		if (put_user(value, &ip[1 + i]))
940 			return -EFAULT;
941 	}
942 
943 	return 0;
944 }
945 
946 static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
947 			struct file *file)
948 {
949 	client->revoked = true;
950 	evdev_ungrab(evdev, client);
951 	input_flush_device(&evdev->handle, file);
952 	wake_up_interruptible_poll(&client->wait, EPOLLHUP | EPOLLERR);
953 
954 	return 0;
955 }
956 
957 /* must be called with evdev-mutex held */
958 static int evdev_set_mask(struct evdev_client *client,
959 			  unsigned int type,
960 			  const void __user *codes,
961 			  u32 codes_size,
962 			  int compat)
963 {
964 	unsigned long flags, *mask, *oldmask;
965 	size_t cnt;
966 	int error;
967 
968 	/* we allow unknown types and 'codes_size > size' for forward-compat */
969 	cnt = evdev_get_mask_cnt(type);
970 	if (!cnt)
971 		return 0;
972 
973 	mask = bitmap_zalloc(cnt, GFP_KERNEL);
974 	if (!mask)
975 		return -ENOMEM;
976 
977 	error = bits_from_user(mask, cnt - 1, codes_size, codes, compat);
978 	if (error < 0) {
979 		bitmap_free(mask);
980 		return error;
981 	}
982 
983 	spin_lock_irqsave(&client->buffer_lock, flags);
984 	oldmask = client->evmasks[type];
985 	client->evmasks[type] = mask;
986 	spin_unlock_irqrestore(&client->buffer_lock, flags);
987 
988 	bitmap_free(oldmask);
989 
990 	return 0;
991 }
992 
993 /* must be called with evdev-mutex held */
994 static int evdev_get_mask(struct evdev_client *client,
995 			  unsigned int type,
996 			  void __user *codes,
997 			  u32 codes_size,
998 			  int compat)
999 {
1000 	unsigned long *mask;
1001 	size_t cnt, size, xfer_size;
1002 	int i;
1003 	int error;
1004 
1005 	/* we allow unknown types and 'codes_size > size' for forward-compat */
1006 	cnt = evdev_get_mask_cnt(type);
1007 	size = sizeof(unsigned long) * BITS_TO_LONGS(cnt);
1008 	xfer_size = min_t(size_t, codes_size, size);
1009 
1010 	if (cnt > 0) {
1011 		mask = client->evmasks[type];
1012 		if (mask) {
1013 			error = bits_to_user(mask, cnt - 1,
1014 					     xfer_size, codes, compat);
1015 			if (error < 0)
1016 				return error;
1017 		} else {
1018 			/* fake mask with all bits set */
1019 			for (i = 0; i < xfer_size; i++)
1020 				if (put_user(0xffU, (u8 __user *)codes + i))
1021 					return -EFAULT;
1022 		}
1023 	}
1024 
1025 	if (xfer_size < codes_size)
1026 		if (clear_user(codes + xfer_size, codes_size - xfer_size))
1027 			return -EFAULT;
1028 
1029 	return 0;
1030 }
1031 
1032 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
1033 			   void __user *p, int compat_mode)
1034 {
1035 	struct evdev_client *client = file->private_data;
1036 	struct evdev *evdev = client->evdev;
1037 	struct input_dev *dev = evdev->handle.dev;
1038 	struct input_absinfo abs;
1039 	struct input_mask mask;
1040 	struct ff_effect effect;
1041 	int __user *ip = (int __user *)p;
1042 	unsigned int i, t, u, v;
1043 	unsigned int size;
1044 	int error;
1045 
1046 	/* First we check for fixed-length commands */
1047 	switch (cmd) {
1048 
1049 	case EVIOCGVERSION:
1050 		return put_user(EV_VERSION, ip);
1051 
1052 	case EVIOCGID:
1053 		if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
1054 			return -EFAULT;
1055 		return 0;
1056 
1057 	case EVIOCGREP:
1058 		if (!test_bit(EV_REP, dev->evbit))
1059 			return -ENOSYS;
1060 		if (put_user(dev->rep[REP_DELAY], ip))
1061 			return -EFAULT;
1062 		if (put_user(dev->rep[REP_PERIOD], ip + 1))
1063 			return -EFAULT;
1064 		return 0;
1065 
1066 	case EVIOCSREP:
1067 		if (!test_bit(EV_REP, dev->evbit))
1068 			return -ENOSYS;
1069 		if (get_user(u, ip))
1070 			return -EFAULT;
1071 		if (get_user(v, ip + 1))
1072 			return -EFAULT;
1073 
1074 		input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
1075 		input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
1076 
1077 		return 0;
1078 
1079 	case EVIOCRMFF:
1080 		return input_ff_erase(dev, (int)(unsigned long) p, file);
1081 
1082 	case EVIOCGEFFECTS:
1083 		i = test_bit(EV_FF, dev->evbit) ?
1084 				dev->ff->max_effects : 0;
1085 		if (put_user(i, ip))
1086 			return -EFAULT;
1087 		return 0;
1088 
1089 	case EVIOCGRAB:
1090 		if (p)
1091 			return evdev_grab(evdev, client);
1092 		else
1093 			return evdev_ungrab(evdev, client);
1094 
1095 	case EVIOCREVOKE:
1096 		if (p)
1097 			return -EINVAL;
1098 		else
1099 			return evdev_revoke(evdev, client, file);
1100 
1101 	case EVIOCGMASK: {
1102 		void __user *codes_ptr;
1103 
1104 		if (copy_from_user(&mask, p, sizeof(mask)))
1105 			return -EFAULT;
1106 
1107 		codes_ptr = (void __user *)(unsigned long)mask.codes_ptr;
1108 		return evdev_get_mask(client,
1109 				      mask.type, codes_ptr, mask.codes_size,
1110 				      compat_mode);
1111 	}
1112 
1113 	case EVIOCSMASK: {
1114 		const void __user *codes_ptr;
1115 
1116 		if (copy_from_user(&mask, p, sizeof(mask)))
1117 			return -EFAULT;
1118 
1119 		codes_ptr = (const void __user *)(unsigned long)mask.codes_ptr;
1120 		return evdev_set_mask(client,
1121 				      mask.type, codes_ptr, mask.codes_size,
1122 				      compat_mode);
1123 	}
1124 
1125 	case EVIOCSCLOCKID:
1126 		if (copy_from_user(&i, p, sizeof(unsigned int)))
1127 			return -EFAULT;
1128 
1129 		return evdev_set_clk_type(client, i);
1130 
1131 	case EVIOCGKEYCODE:
1132 		return evdev_handle_get_keycode(dev, p);
1133 
1134 	case EVIOCSKEYCODE:
1135 		return evdev_handle_set_keycode(dev, p);
1136 
1137 	case EVIOCGKEYCODE_V2:
1138 		return evdev_handle_get_keycode_v2(dev, p);
1139 
1140 	case EVIOCSKEYCODE_V2:
1141 		return evdev_handle_set_keycode_v2(dev, p);
1142 	}
1143 
1144 	size = _IOC_SIZE(cmd);
1145 
1146 	/* Now check variable-length commands */
1147 #define EVIOC_MASK_SIZE(nr)	((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
1148 	switch (EVIOC_MASK_SIZE(cmd)) {
1149 
1150 	case EVIOCGPROP(0):
1151 		return bits_to_user(dev->propbit, INPUT_PROP_MAX,
1152 				    size, p, compat_mode);
1153 
1154 	case EVIOCGMTSLOTS(0):
1155 		return evdev_handle_mt_request(dev, size, ip);
1156 
1157 	case EVIOCGKEY(0):
1158 		return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
1159 					    KEY_MAX, size, p, compat_mode);
1160 
1161 	case EVIOCGLED(0):
1162 		return evdev_handle_get_val(client, dev, EV_LED, dev->led,
1163 					    LED_MAX, size, p, compat_mode);
1164 
1165 	case EVIOCGSND(0):
1166 		return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
1167 					    SND_MAX, size, p, compat_mode);
1168 
1169 	case EVIOCGSW(0):
1170 		return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
1171 					    SW_MAX, size, p, compat_mode);
1172 
1173 	case EVIOCGNAME(0):
1174 		return str_to_user(dev->name, size, p);
1175 
1176 	case EVIOCGPHYS(0):
1177 		return str_to_user(dev->phys, size, p);
1178 
1179 	case EVIOCGUNIQ(0):
1180 		return str_to_user(dev->uniq, size, p);
1181 
1182 	case EVIOC_MASK_SIZE(EVIOCSFF):
1183 		if (input_ff_effect_from_user(p, size, &effect))
1184 			return -EFAULT;
1185 
1186 		error = input_ff_upload(dev, &effect, file);
1187 		if (error)
1188 			return error;
1189 
1190 		if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
1191 			return -EFAULT;
1192 
1193 		return 0;
1194 	}
1195 
1196 	/* Multi-number variable-length handlers */
1197 	if (_IOC_TYPE(cmd) != 'E')
1198 		return -EINVAL;
1199 
1200 	if (_IOC_DIR(cmd) == _IOC_READ) {
1201 
1202 		if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1203 			return handle_eviocgbit(dev,
1204 						_IOC_NR(cmd) & EV_MAX, size,
1205 						p, compat_mode);
1206 
1207 		if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
1208 
1209 			if (!dev->absinfo)
1210 				return -EINVAL;
1211 
1212 			t = _IOC_NR(cmd) & ABS_MAX;
1213 			abs = dev->absinfo[t];
1214 
1215 			if (copy_to_user(p, &abs, min_t(size_t,
1216 					size, sizeof(struct input_absinfo))))
1217 				return -EFAULT;
1218 
1219 			return 0;
1220 		}
1221 	}
1222 
1223 	if (_IOC_DIR(cmd) == _IOC_WRITE) {
1224 
1225 		if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1226 
1227 			if (!dev->absinfo)
1228 				return -EINVAL;
1229 
1230 			t = _IOC_NR(cmd) & ABS_MAX;
1231 
1232 			if (copy_from_user(&abs, p, min_t(size_t,
1233 					size, sizeof(struct input_absinfo))))
1234 				return -EFAULT;
1235 
1236 			if (size < sizeof(struct input_absinfo))
1237 				abs.resolution = 0;
1238 
1239 			/* We can't change number of reserved MT slots */
1240 			if (t == ABS_MT_SLOT)
1241 				return -EINVAL;
1242 
1243 			/*
1244 			 * Take event lock to ensure that we are not
1245 			 * changing device parameters in the middle
1246 			 * of event.
1247 			 */
1248 			spin_lock_irq(&dev->event_lock);
1249 			dev->absinfo[t] = abs;
1250 			spin_unlock_irq(&dev->event_lock);
1251 
1252 			return 0;
1253 		}
1254 	}
1255 
1256 	return -EINVAL;
1257 }
1258 
1259 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1260 				void __user *p, int compat_mode)
1261 {
1262 	struct evdev_client *client = file->private_data;
1263 	struct evdev *evdev = client->evdev;
1264 	int retval;
1265 
1266 	retval = mutex_lock_interruptible(&evdev->mutex);
1267 	if (retval)
1268 		return retval;
1269 
1270 	if (!evdev->exist || client->revoked) {
1271 		retval = -ENODEV;
1272 		goto out;
1273 	}
1274 
1275 	retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1276 
1277  out:
1278 	mutex_unlock(&evdev->mutex);
1279 	return retval;
1280 }
1281 
1282 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1283 {
1284 	return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1285 }
1286 
1287 #ifdef CONFIG_COMPAT
1288 static long evdev_ioctl_compat(struct file *file,
1289 				unsigned int cmd, unsigned long arg)
1290 {
1291 	return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
1292 }
1293 #endif
1294 
1295 static const struct file_operations evdev_fops = {
1296 	.owner		= THIS_MODULE,
1297 	.read		= evdev_read,
1298 	.write		= evdev_write,
1299 	.poll		= evdev_poll,
1300 	.open		= evdev_open,
1301 	.release	= evdev_release,
1302 	.unlocked_ioctl	= evdev_ioctl,
1303 #ifdef CONFIG_COMPAT
1304 	.compat_ioctl	= evdev_ioctl_compat,
1305 #endif
1306 	.fasync		= evdev_fasync,
1307 };
1308 
1309 /*
1310  * Mark device non-existent. This disables writes, ioctls and
1311  * prevents new users from opening the device. Already posted
1312  * blocking reads will stay, however new ones will fail.
1313  */
1314 static void evdev_mark_dead(struct evdev *evdev)
1315 {
1316 	mutex_lock(&evdev->mutex);
1317 	evdev->exist = false;
1318 	mutex_unlock(&evdev->mutex);
1319 }
1320 
1321 static void evdev_cleanup(struct evdev *evdev)
1322 {
1323 	struct input_handle *handle = &evdev->handle;
1324 
1325 	evdev_mark_dead(evdev);
1326 	evdev_hangup(evdev);
1327 
1328 	/* evdev is marked dead so no one else accesses evdev->open */
1329 	if (evdev->open) {
1330 		input_flush_device(handle, NULL);
1331 		input_close_device(handle);
1332 	}
1333 }
1334 
1335 /*
1336  * Create new evdev device. Note that input core serializes calls
1337  * to connect and disconnect.
1338  */
1339 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1340 			 const struct input_device_id *id)
1341 {
1342 	struct evdev *evdev;
1343 	int minor;
1344 	int dev_no;
1345 	int error;
1346 
1347 	minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1348 	if (minor < 0) {
1349 		error = minor;
1350 		pr_err("failed to reserve new minor: %d\n", error);
1351 		return error;
1352 	}
1353 
1354 	evdev = kzalloc_obj(struct evdev);
1355 	if (!evdev) {
1356 		error = -ENOMEM;
1357 		goto err_free_minor;
1358 	}
1359 
1360 	INIT_LIST_HEAD(&evdev->client_list);
1361 	spin_lock_init(&evdev->client_lock);
1362 	mutex_init(&evdev->mutex);
1363 	evdev->exist = true;
1364 
1365 	dev_no = minor;
1366 	/* Normalize device number if it falls into legacy range */
1367 	if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1368 		dev_no -= EVDEV_MINOR_BASE;
1369 	dev_set_name(&evdev->dev, "event%d", dev_no);
1370 
1371 	evdev->handle.dev = input_get_device(dev);
1372 	evdev->handle.name = dev_name(&evdev->dev);
1373 	evdev->handle.handler = handler;
1374 	evdev->handle.private = evdev;
1375 
1376 	evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
1377 	evdev->dev.class = &input_class;
1378 	evdev->dev.parent = &dev->dev;
1379 	evdev->dev.release = evdev_free;
1380 	device_initialize(&evdev->dev);
1381 
1382 	error = input_register_handle(&evdev->handle);
1383 	if (error)
1384 		goto err_free_evdev;
1385 
1386 	cdev_init(&evdev->cdev, &evdev_fops);
1387 
1388 	error = cdev_device_add(&evdev->cdev, &evdev->dev);
1389 	if (error)
1390 		goto err_cleanup_evdev;
1391 
1392 	return 0;
1393 
1394  err_cleanup_evdev:
1395 	evdev_cleanup(evdev);
1396 	input_unregister_handle(&evdev->handle);
1397  err_free_evdev:
1398 	put_device(&evdev->dev);
1399  err_free_minor:
1400 	input_free_minor(minor);
1401 	return error;
1402 }
1403 
1404 static void evdev_disconnect(struct input_handle *handle)
1405 {
1406 	struct evdev *evdev = handle->private;
1407 
1408 	cdev_device_del(&evdev->cdev, &evdev->dev);
1409 	evdev_cleanup(evdev);
1410 	input_free_minor(MINOR(evdev->dev.devt));
1411 	input_unregister_handle(handle);
1412 	put_device(&evdev->dev);
1413 }
1414 
1415 static const struct input_device_id evdev_ids[] = {
1416 	{
1417 		/* Matches all devices */
1418 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1419 		.evbit = { BIT_MASK(EV_SYN) },
1420 	},
1421 	{ }	/* Terminating zero entry */
1422 };
1423 
1424 MODULE_DEVICE_TABLE(input, evdev_ids);
1425 
1426 static struct input_handler evdev_handler = {
1427 	.events		= evdev_events,
1428 	.connect	= evdev_connect,
1429 	.disconnect	= evdev_disconnect,
1430 	.legacy_minors	= true,
1431 	.minor		= EVDEV_MINOR_BASE,
1432 	.name		= "evdev",
1433 	.id_table	= evdev_ids,
1434 };
1435 
1436 static int __init evdev_init(void)
1437 {
1438 	return input_register_handler(&evdev_handler);
1439 }
1440 
1441 static void __exit evdev_exit(void)
1442 {
1443 	input_unregister_handler(&evdev_handler);
1444 }
1445 
1446 module_init(evdev_init);
1447 module_exit(evdev_exit);
1448 
1449 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1450 MODULE_DESCRIPTION("Input driver event char devices");
1451 MODULE_LICENSE("GPL");
1452