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