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