1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  User level driver support for input subsystem
4  *
5  * Heavily based on evdev.c by Vojtech Pavlik
6  *
7  * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
8  *
9  * Changes/Revisions:
10  *	0.4	01/09/2014 (Benjamin Tissoires <benjamin.tissoires@redhat.com>)
11  *		- add UI_GET_SYSNAME ioctl
12  *	0.3	09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
13  *		- updated ff support for the changes in kernel interface
14  *		- added MODULE_VERSION
15  *	0.2	16/10/2004 (Micah Dowty <micah@navi.cx>)
16  *		- added force feedback support
17  *              - added UI_SET_PHYS
18  *	0.1	20/06/2002
19  *		- first public version
20  */
21 #include <uapi/linux/uinput.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/fs.h>
28 #include <linux/miscdevice.h>
29 #include <linux/overflow.h>
30 #include <linux/input/mt.h>
31 #include "../input-compat.h"
32 
33 #define UINPUT_NAME		"uinput"
34 #define UINPUT_BUFFER_SIZE	16
35 #define UINPUT_NUM_REQUESTS	16
36 
37 enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED };
38 
39 struct uinput_request {
40 	unsigned int		id;
41 	unsigned int		code;	/* UI_FF_UPLOAD, UI_FF_ERASE */
42 
43 	int			retval;
44 	struct completion	done;
45 
46 	union {
47 		unsigned int	effect_id;
48 		struct {
49 			struct ff_effect *effect;
50 			struct ff_effect *old;
51 		} upload;
52 	} u;
53 };
54 
55 struct uinput_device {
56 	struct input_dev	*dev;
57 	struct mutex		mutex;
58 	enum uinput_state	state;
59 	wait_queue_head_t	waitq;
60 	unsigned char		ready;
61 	unsigned char		head;
62 	unsigned char		tail;
63 	struct input_event	buff[UINPUT_BUFFER_SIZE];
64 	unsigned int		ff_effects_max;
65 
66 	struct uinput_request	*requests[UINPUT_NUM_REQUESTS];
67 	wait_queue_head_t	requests_waitq;
68 	spinlock_t		requests_lock;
69 };
70 
uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)71 static int uinput_dev_event(struct input_dev *dev,
72 			    unsigned int type, unsigned int code, int value)
73 {
74 	struct uinput_device	*udev = input_get_drvdata(dev);
75 	struct timespec64	ts;
76 
77 	ktime_get_ts64(&ts);
78 
79 	udev->buff[udev->head] = (struct input_event) {
80 		.input_event_sec = ts.tv_sec,
81 		.input_event_usec = ts.tv_nsec / NSEC_PER_USEC,
82 		.type = type,
83 		.code = code,
84 		.value = value,
85 	};
86 
87 	udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
88 
89 	wake_up_interruptible(&udev->waitq);
90 
91 	return 0;
92 }
93 
94 /* Atomically allocate an ID for the given request. Returns 0 on success. */
uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)95 static bool uinput_request_alloc_id(struct uinput_device *udev,
96 				    struct uinput_request *request)
97 {
98 	unsigned int id;
99 	bool reserved = false;
100 
101 	spin_lock(&udev->requests_lock);
102 
103 	for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
104 		if (!udev->requests[id]) {
105 			request->id = id;
106 			udev->requests[id] = request;
107 			reserved = true;
108 			break;
109 		}
110 	}
111 
112 	spin_unlock(&udev->requests_lock);
113 	return reserved;
114 }
115 
uinput_request_find(struct uinput_device *udev, unsigned int id)116 static struct uinput_request *uinput_request_find(struct uinput_device *udev,
117 						  unsigned int id)
118 {
119 	/* Find an input request, by ID. Returns NULL if the ID isn't valid. */
120 	if (id >= UINPUT_NUM_REQUESTS)
121 		return NULL;
122 
123 	return udev->requests[id];
124 }
125 
uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)126 static int uinput_request_reserve_slot(struct uinput_device *udev,
127 				       struct uinput_request *request)
128 {
129 	/* Allocate slot. If none are available right away, wait. */
130 	return wait_event_interruptible(udev->requests_waitq,
131 					uinput_request_alloc_id(udev, request));
132 }
133 
uinput_request_release_slot(struct uinput_device *udev, unsigned int id)134 static void uinput_request_release_slot(struct uinput_device *udev,
135 					unsigned int id)
136 {
137 	/* Mark slot as available */
138 	spin_lock(&udev->requests_lock);
139 	udev->requests[id] = NULL;
140 	spin_unlock(&udev->requests_lock);
141 
142 	wake_up(&udev->requests_waitq);
143 }
144 
uinput_request_send(struct uinput_device *udev, struct uinput_request *request)145 static int uinput_request_send(struct uinput_device *udev,
146 			       struct uinput_request *request)
147 {
148 	int retval;
149 
150 	retval = mutex_lock_interruptible(&udev->mutex);
151 	if (retval)
152 		return retval;
153 
154 	if (udev->state != UIST_CREATED) {
155 		retval = -ENODEV;
156 		goto out;
157 	}
158 
159 	init_completion(&request->done);
160 
161 	/*
162 	 * Tell our userspace application about this new request
163 	 * by queueing an input event.
164 	 */
165 	uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
166 
167  out:
168 	mutex_unlock(&udev->mutex);
169 	return retval;
170 }
171 
uinput_request_submit(struct uinput_device *udev, struct uinput_request *request)172 static int uinput_request_submit(struct uinput_device *udev,
173 				 struct uinput_request *request)
174 {
175 	int retval;
176 
177 	retval = uinput_request_reserve_slot(udev, request);
178 	if (retval)
179 		return retval;
180 
181 	retval = uinput_request_send(udev, request);
182 	if (retval)
183 		goto out;
184 
185 	if (!wait_for_completion_timeout(&request->done, 30 * HZ)) {
186 		retval = -ETIMEDOUT;
187 		goto out;
188 	}
189 
190 	retval = request->retval;
191 
192  out:
193 	uinput_request_release_slot(udev, request->id);
194 	return retval;
195 }
196 
197 /*
198  * Fail all outstanding requests so handlers don't wait for the userspace
199  * to finish processing them.
200  */
uinput_flush_requests(struct uinput_device *udev)201 static void uinput_flush_requests(struct uinput_device *udev)
202 {
203 	struct uinput_request *request;
204 	int i;
205 
206 	spin_lock(&udev->requests_lock);
207 
208 	for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
209 		request = udev->requests[i];
210 		if (request) {
211 			request->retval = -ENODEV;
212 			complete(&request->done);
213 		}
214 	}
215 
216 	spin_unlock(&udev->requests_lock);
217 }
218 
uinput_dev_set_gain(struct input_dev *dev, u16 gain)219 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
220 {
221 	uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
222 }
223 
uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)224 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
225 {
226 	uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
227 }
228 
uinput_dev_playback(struct input_dev *dev, int effect_id, int value)229 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
230 {
231 	return uinput_dev_event(dev, EV_FF, effect_id, value);
232 }
233 
uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)234 static int uinput_dev_upload_effect(struct input_dev *dev,
235 				    struct ff_effect *effect,
236 				    struct ff_effect *old)
237 {
238 	struct uinput_device *udev = input_get_drvdata(dev);
239 	struct uinput_request request;
240 
241 	/*
242 	 * uinput driver does not currently support periodic effects with
243 	 * custom waveform since it does not have a way to pass buffer of
244 	 * samples (custom_data) to userspace. If ever there is a device
245 	 * supporting custom waveforms we would need to define an additional
246 	 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
247 	 */
248 	if (effect->type == FF_PERIODIC &&
249 			effect->u.periodic.waveform == FF_CUSTOM)
250 		return -EINVAL;
251 
252 	request.code = UI_FF_UPLOAD;
253 	request.u.upload.effect = effect;
254 	request.u.upload.old = old;
255 
256 	return uinput_request_submit(udev, &request);
257 }
258 
uinput_dev_erase_effect(struct input_dev *dev, int effect_id)259 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
260 {
261 	struct uinput_device *udev = input_get_drvdata(dev);
262 	struct uinput_request request;
263 
264 	if (!test_bit(EV_FF, dev->evbit))
265 		return -ENOSYS;
266 
267 	request.code = UI_FF_ERASE;
268 	request.u.effect_id = effect_id;
269 
270 	return uinput_request_submit(udev, &request);
271 }
272 
uinput_dev_flush(struct input_dev *dev, struct file *file)273 static int uinput_dev_flush(struct input_dev *dev, struct file *file)
274 {
275 	/*
276 	 * If we are called with file == NULL that means we are tearing
277 	 * down the device, and therefore we can not handle FF erase
278 	 * requests: either we are handling UI_DEV_DESTROY (and holding
279 	 * the udev->mutex), or the file descriptor is closed and there is
280 	 * nobody on the other side anymore.
281 	 */
282 	return file ? input_ff_flush(dev, file) : 0;
283 }
284 
uinput_destroy_device(struct uinput_device *udev)285 static void uinput_destroy_device(struct uinput_device *udev)
286 {
287 	const char *name, *phys;
288 	struct input_dev *dev = udev->dev;
289 	enum uinput_state old_state = udev->state;
290 
291 	udev->state = UIST_NEW_DEVICE;
292 
293 	if (dev) {
294 		name = dev->name;
295 		phys = dev->phys;
296 		if (old_state == UIST_CREATED) {
297 			uinput_flush_requests(udev);
298 			input_unregister_device(dev);
299 		} else {
300 			input_free_device(dev);
301 		}
302 		kfree(name);
303 		kfree(phys);
304 		udev->dev = NULL;
305 	}
306 }
307 
uinput_create_device(struct uinput_device *udev)308 static int uinput_create_device(struct uinput_device *udev)
309 {
310 	struct input_dev *dev = udev->dev;
311 	int error, nslot;
312 
313 	if (udev->state != UIST_SETUP_COMPLETE) {
314 		printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
315 		return -EINVAL;
316 	}
317 
318 	if (test_bit(EV_ABS, dev->evbit)) {
319 		input_alloc_absinfo(dev);
320 		if (!dev->absinfo) {
321 			error = -EINVAL;
322 			goto fail1;
323 		}
324 
325 		if (test_bit(ABS_MT_SLOT, dev->absbit)) {
326 			nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
327 			error = input_mt_init_slots(dev, nslot, 0);
328 			if (error)
329 				goto fail1;
330 		} else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
331 			input_set_events_per_packet(dev, 60);
332 		}
333 	}
334 
335 	if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
336 		printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
337 			UINPUT_NAME);
338 		error = -EINVAL;
339 		goto fail1;
340 	}
341 
342 	if (udev->ff_effects_max) {
343 		error = input_ff_create(dev, udev->ff_effects_max);
344 		if (error)
345 			goto fail1;
346 
347 		dev->ff->upload = uinput_dev_upload_effect;
348 		dev->ff->erase = uinput_dev_erase_effect;
349 		dev->ff->playback = uinput_dev_playback;
350 		dev->ff->set_gain = uinput_dev_set_gain;
351 		dev->ff->set_autocenter = uinput_dev_set_autocenter;
352 		/*
353 		 * The standard input_ff_flush() implementation does
354 		 * not quite work for uinput as we can't reasonably
355 		 * handle FF requests during device teardown.
356 		 */
357 		dev->flush = uinput_dev_flush;
358 	}
359 
360 	dev->event = uinput_dev_event;
361 
362 	input_set_drvdata(udev->dev, udev);
363 
364 	error = input_register_device(udev->dev);
365 	if (error)
366 		goto fail2;
367 
368 	udev->state = UIST_CREATED;
369 
370 	return 0;
371 
372  fail2:	input_ff_destroy(dev);
373  fail1: uinput_destroy_device(udev);
374 	return error;
375 }
376 
uinput_open(struct inode *inode, struct file *file)377 static int uinput_open(struct inode *inode, struct file *file)
378 {
379 	struct uinput_device *newdev;
380 
381 	newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
382 	if (!newdev)
383 		return -ENOMEM;
384 
385 	mutex_init(&newdev->mutex);
386 	spin_lock_init(&newdev->requests_lock);
387 	init_waitqueue_head(&newdev->requests_waitq);
388 	init_waitqueue_head(&newdev->waitq);
389 	newdev->state = UIST_NEW_DEVICE;
390 
391 	file->private_data = newdev;
392 	stream_open(inode, file);
393 
394 	return 0;
395 }
396 
uinput_validate_absinfo(struct input_dev *dev, unsigned int code, const struct input_absinfo *abs)397 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
398 				   const struct input_absinfo *abs)
399 {
400 	int min, max, range;
401 
402 	min = abs->minimum;
403 	max = abs->maximum;
404 
405 	if ((min != 0 || max != 0) && max < min) {
406 		printk(KERN_DEBUG
407 		       "%s: invalid abs[%02x] min:%d max:%d\n",
408 		       UINPUT_NAME, code, min, max);
409 		return -EINVAL;
410 	}
411 
412 	if (!check_sub_overflow(max, min, &range) && abs->flat > range) {
413 		printk(KERN_DEBUG
414 		       "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
415 		       UINPUT_NAME, code, abs->flat, min, max);
416 		return -EINVAL;
417 	}
418 
419 	/*
420 	 * Limit number of contacts to a reasonable value (100). This
421 	 * ensures that we need less than 2 pages for struct input_mt
422 	 * (we are not using in-kernel slot assignment so not going to
423 	 * allocate memory for the "red" table), and we should have no
424 	 * trouble getting this much memory.
425 	 */
426 	if (code == ABS_MT_SLOT && max > 99) {
427 		printk(KERN_DEBUG
428 		       "%s: unreasonably large number of slots requested: %d\n",
429 		       UINPUT_NAME, max);
430 		return -EINVAL;
431 	}
432 
433 	return 0;
434 }
435 
uinput_validate_absbits(struct input_dev *dev)436 static int uinput_validate_absbits(struct input_dev *dev)
437 {
438 	unsigned int cnt;
439 	int error;
440 
441 	if (!test_bit(EV_ABS, dev->evbit))
442 		return 0;
443 
444 	/*
445 	 * Check if absmin/absmax/absfuzz/absflat are sane.
446 	 */
447 
448 	for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
449 		if (!dev->absinfo)
450 			return -EINVAL;
451 
452 		error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
453 		if (error)
454 			return error;
455 	}
456 
457 	return 0;
458 }
459 
uinput_dev_setup(struct uinput_device *udev, struct uinput_setup __user *arg)460 static int uinput_dev_setup(struct uinput_device *udev,
461 			    struct uinput_setup __user *arg)
462 {
463 	struct uinput_setup setup;
464 	struct input_dev *dev;
465 
466 	if (udev->state == UIST_CREATED)
467 		return -EINVAL;
468 
469 	if (copy_from_user(&setup, arg, sizeof(setup)))
470 		return -EFAULT;
471 
472 	if (!setup.name[0])
473 		return -EINVAL;
474 
475 	dev = udev->dev;
476 	dev->id = setup.id;
477 	udev->ff_effects_max = setup.ff_effects_max;
478 
479 	kfree(dev->name);
480 	dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
481 	if (!dev->name)
482 		return -ENOMEM;
483 
484 	udev->state = UIST_SETUP_COMPLETE;
485 	return 0;
486 }
487 
uinput_abs_setup(struct uinput_device *udev, struct uinput_setup __user *arg, size_t size)488 static int uinput_abs_setup(struct uinput_device *udev,
489 			    struct uinput_setup __user *arg, size_t size)
490 {
491 	struct uinput_abs_setup setup = {};
492 	struct input_dev *dev;
493 	int error;
494 
495 	if (size > sizeof(setup))
496 		return -E2BIG;
497 
498 	if (udev->state == UIST_CREATED)
499 		return -EINVAL;
500 
501 	if (copy_from_user(&setup, arg, size))
502 		return -EFAULT;
503 
504 	if (setup.code > ABS_MAX)
505 		return -ERANGE;
506 
507 	dev = udev->dev;
508 
509 	error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
510 	if (error)
511 		return error;
512 
513 	input_alloc_absinfo(dev);
514 	if (!dev->absinfo)
515 		return -ENOMEM;
516 
517 	set_bit(setup.code, dev->absbit);
518 	dev->absinfo[setup.code] = setup.absinfo;
519 	return 0;
520 }
521 
522 /* legacy setup via write() */
uinput_setup_device_legacy(struct uinput_device *udev, const char __user *buffer, size_t count)523 static int uinput_setup_device_legacy(struct uinput_device *udev,
524 				      const char __user *buffer, size_t count)
525 {
526 	struct uinput_user_dev	*user_dev;
527 	struct input_dev	*dev;
528 	int			i;
529 	int			retval;
530 
531 	if (count != sizeof(struct uinput_user_dev))
532 		return -EINVAL;
533 
534 	if (!udev->dev) {
535 		udev->dev = input_allocate_device();
536 		if (!udev->dev)
537 			return -ENOMEM;
538 	}
539 
540 	dev = udev->dev;
541 
542 	user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
543 	if (IS_ERR(user_dev))
544 		return PTR_ERR(user_dev);
545 
546 	udev->ff_effects_max = user_dev->ff_effects_max;
547 
548 	/* Ensure name is filled in */
549 	if (!user_dev->name[0]) {
550 		retval = -EINVAL;
551 		goto exit;
552 	}
553 
554 	kfree(dev->name);
555 	dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
556 			     GFP_KERNEL);
557 	if (!dev->name) {
558 		retval = -ENOMEM;
559 		goto exit;
560 	}
561 
562 	dev->id.bustype	= user_dev->id.bustype;
563 	dev->id.vendor	= user_dev->id.vendor;
564 	dev->id.product	= user_dev->id.product;
565 	dev->id.version	= user_dev->id.version;
566 
567 	for (i = 0; i < ABS_CNT; i++) {
568 		input_abs_set_max(dev, i, user_dev->absmax[i]);
569 		input_abs_set_min(dev, i, user_dev->absmin[i]);
570 		input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
571 		input_abs_set_flat(dev, i, user_dev->absflat[i]);
572 	}
573 
574 	retval = uinput_validate_absbits(dev);
575 	if (retval < 0)
576 		goto exit;
577 
578 	udev->state = UIST_SETUP_COMPLETE;
579 	retval = count;
580 
581  exit:
582 	kfree(user_dev);
583 	return retval;
584 }
585 
uinput_inject_events(struct uinput_device *udev, const char __user *buffer, size_t count)586 static ssize_t uinput_inject_events(struct uinput_device *udev,
587 				    const char __user *buffer, size_t count)
588 {
589 	struct input_event ev;
590 	size_t bytes = 0;
591 
592 	if (count != 0 && count < input_event_size())
593 		return -EINVAL;
594 
595 	while (bytes + input_event_size() <= count) {
596 		/*
597 		 * Note that even if some events were fetched successfully
598 		 * we are still going to return EFAULT instead of partial
599 		 * count to let userspace know that it got it's buffers
600 		 * all wrong.
601 		 */
602 		if (input_event_from_user(buffer + bytes, &ev))
603 			return -EFAULT;
604 
605 		input_event(udev->dev, ev.type, ev.code, ev.value);
606 		bytes += input_event_size();
607 		cond_resched();
608 	}
609 
610 	return bytes;
611 }
612 
uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)613 static ssize_t uinput_write(struct file *file, const char __user *buffer,
614 			    size_t count, loff_t *ppos)
615 {
616 	struct uinput_device *udev = file->private_data;
617 	int retval;
618 
619 	if (count == 0)
620 		return 0;
621 
622 	retval = mutex_lock_interruptible(&udev->mutex);
623 	if (retval)
624 		return retval;
625 
626 	retval = udev->state == UIST_CREATED ?
627 			uinput_inject_events(udev, buffer, count) :
628 			uinput_setup_device_legacy(udev, buffer, count);
629 
630 	mutex_unlock(&udev->mutex);
631 
632 	return retval;
633 }
634 
uinput_fetch_next_event(struct uinput_device *udev, struct input_event *event)635 static bool uinput_fetch_next_event(struct uinput_device *udev,
636 				    struct input_event *event)
637 {
638 	bool have_event;
639 
640 	spin_lock_irq(&udev->dev->event_lock);
641 
642 	have_event = udev->head != udev->tail;
643 	if (have_event) {
644 		*event = udev->buff[udev->tail];
645 		udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
646 	}
647 
648 	spin_unlock_irq(&udev->dev->event_lock);
649 
650 	return have_event;
651 }
652 
uinput_events_to_user(struct uinput_device *udev, char __user *buffer, size_t count)653 static ssize_t uinput_events_to_user(struct uinput_device *udev,
654 				     char __user *buffer, size_t count)
655 {
656 	struct input_event event;
657 	size_t read = 0;
658 
659 	while (read + input_event_size() <= count &&
660 	       uinput_fetch_next_event(udev, &event)) {
661 
662 		if (input_event_to_user(buffer + read, &event))
663 			return -EFAULT;
664 
665 		read += input_event_size();
666 	}
667 
668 	return read;
669 }
670 
uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)671 static ssize_t uinput_read(struct file *file, char __user *buffer,
672 			   size_t count, loff_t *ppos)
673 {
674 	struct uinput_device *udev = file->private_data;
675 	ssize_t retval;
676 
677 	if (count != 0 && count < input_event_size())
678 		return -EINVAL;
679 
680 	do {
681 		retval = mutex_lock_interruptible(&udev->mutex);
682 		if (retval)
683 			return retval;
684 
685 		if (udev->state != UIST_CREATED)
686 			retval = -ENODEV;
687 		else if (udev->head == udev->tail &&
688 			 (file->f_flags & O_NONBLOCK))
689 			retval = -EAGAIN;
690 		else
691 			retval = uinput_events_to_user(udev, buffer, count);
692 
693 		mutex_unlock(&udev->mutex);
694 
695 		if (retval || count == 0)
696 			break;
697 
698 		if (!(file->f_flags & O_NONBLOCK))
699 			retval = wait_event_interruptible(udev->waitq,
700 						  udev->head != udev->tail ||
701 						  udev->state != UIST_CREATED);
702 	} while (retval == 0);
703 
704 	return retval;
705 }
706 
uinput_poll(struct file *file, poll_table *wait)707 static __poll_t uinput_poll(struct file *file, poll_table *wait)
708 {
709 	struct uinput_device *udev = file->private_data;
710 	__poll_t mask = EPOLLOUT | EPOLLWRNORM; /* uinput is always writable */
711 
712 	poll_wait(file, &udev->waitq, wait);
713 
714 	if (udev->head != udev->tail)
715 		mask |= EPOLLIN | EPOLLRDNORM;
716 
717 	return mask;
718 }
719 
uinput_release(struct inode *inode, struct file *file)720 static int uinput_release(struct inode *inode, struct file *file)
721 {
722 	struct uinput_device *udev = file->private_data;
723 
724 	uinput_destroy_device(udev);
725 	kfree(udev);
726 
727 	return 0;
728 }
729 
730 #ifdef CONFIG_COMPAT
731 struct uinput_ff_upload_compat {
732 	__u32			request_id;
733 	__s32			retval;
734 	struct ff_effect_compat	effect;
735 	struct ff_effect_compat	old;
736 };
737 
uinput_ff_upload_to_user(char __user *buffer, const struct uinput_ff_upload *ff_up)738 static int uinput_ff_upload_to_user(char __user *buffer,
739 				    const struct uinput_ff_upload *ff_up)
740 {
741 	if (in_compat_syscall()) {
742 		struct uinput_ff_upload_compat ff_up_compat;
743 
744 		ff_up_compat.request_id = ff_up->request_id;
745 		ff_up_compat.retval = ff_up->retval;
746 		/*
747 		 * It so happens that the pointer that gives us the trouble
748 		 * is the last field in the structure. Since we don't support
749 		 * custom waveforms in uinput anyway we can just copy the whole
750 		 * thing (to the compat size) and ignore the pointer.
751 		 */
752 		memcpy(&ff_up_compat.effect, &ff_up->effect,
753 			sizeof(struct ff_effect_compat));
754 		memcpy(&ff_up_compat.old, &ff_up->old,
755 			sizeof(struct ff_effect_compat));
756 
757 		if (copy_to_user(buffer, &ff_up_compat,
758 				 sizeof(struct uinput_ff_upload_compat)))
759 			return -EFAULT;
760 	} else {
761 		if (copy_to_user(buffer, ff_up,
762 				 sizeof(struct uinput_ff_upload)))
763 			return -EFAULT;
764 	}
765 
766 	return 0;
767 }
768 
uinput_ff_upload_from_user(const char __user *buffer, struct uinput_ff_upload *ff_up)769 static int uinput_ff_upload_from_user(const char __user *buffer,
770 				      struct uinput_ff_upload *ff_up)
771 {
772 	if (in_compat_syscall()) {
773 		struct uinput_ff_upload_compat ff_up_compat;
774 
775 		if (copy_from_user(&ff_up_compat, buffer,
776 				   sizeof(struct uinput_ff_upload_compat)))
777 			return -EFAULT;
778 
779 		ff_up->request_id = ff_up_compat.request_id;
780 		ff_up->retval = ff_up_compat.retval;
781 		memcpy(&ff_up->effect, &ff_up_compat.effect,
782 			sizeof(struct ff_effect_compat));
783 		memcpy(&ff_up->old, &ff_up_compat.old,
784 			sizeof(struct ff_effect_compat));
785 
786 	} else {
787 		if (copy_from_user(ff_up, buffer,
788 				   sizeof(struct uinput_ff_upload)))
789 			return -EFAULT;
790 	}
791 
792 	return 0;
793 }
794 
795 #else
796 
uinput_ff_upload_to_user(char __user *buffer, const struct uinput_ff_upload *ff_up)797 static int uinput_ff_upload_to_user(char __user *buffer,
798 				    const struct uinput_ff_upload *ff_up)
799 {
800 	if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
801 		return -EFAULT;
802 
803 	return 0;
804 }
805 
uinput_ff_upload_from_user(const char __user *buffer, struct uinput_ff_upload *ff_up)806 static int uinput_ff_upload_from_user(const char __user *buffer,
807 				      struct uinput_ff_upload *ff_up)
808 {
809 	if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
810 		return -EFAULT;
811 
812 	return 0;
813 }
814 
815 #endif
816 
817 #define uinput_set_bit(_arg, _bit, _max)		\
818 ({							\
819 	int __ret = 0;					\
820 	if (udev->state == UIST_CREATED)		\
821 		__ret =  -EINVAL;			\
822 	else if ((_arg) > (_max))			\
823 		__ret = -EINVAL;			\
824 	else set_bit((_arg), udev->dev->_bit);		\
825 	__ret;						\
826 })
827 
uinput_str_to_user(void __user *dest, const char *str, unsigned int maxlen)828 static int uinput_str_to_user(void __user *dest, const char *str,
829 			      unsigned int maxlen)
830 {
831 	char __user *p = dest;
832 	int len, ret;
833 
834 	if (!str)
835 		return -ENOENT;
836 
837 	if (maxlen == 0)
838 		return -EINVAL;
839 
840 	len = strlen(str) + 1;
841 	if (len > maxlen)
842 		len = maxlen;
843 
844 	ret = copy_to_user(p, str, len);
845 	if (ret)
846 		return -EFAULT;
847 
848 	/* force terminating '\0' */
849 	ret = put_user(0, p + len - 1);
850 	return ret ? -EFAULT : len;
851 }
852 
uinput_ioctl_handler(struct file *file, unsigned int cmd, unsigned long arg, void __user *p)853 static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
854 				 unsigned long arg, void __user *p)
855 {
856 	int			retval;
857 	struct uinput_device	*udev = file->private_data;
858 	struct uinput_ff_upload ff_up;
859 	struct uinput_ff_erase  ff_erase;
860 	struct uinput_request   *req;
861 	char			*phys;
862 	const char		*name;
863 	unsigned int		size;
864 
865 	retval = mutex_lock_interruptible(&udev->mutex);
866 	if (retval)
867 		return retval;
868 
869 	if (!udev->dev) {
870 		udev->dev = input_allocate_device();
871 		if (!udev->dev) {
872 			retval = -ENOMEM;
873 			goto out;
874 		}
875 	}
876 
877 	switch (cmd) {
878 	case UI_GET_VERSION:
879 		if (put_user(UINPUT_VERSION, (unsigned int __user *)p))
880 			retval = -EFAULT;
881 		goto out;
882 
883 	case UI_DEV_CREATE:
884 		retval = uinput_create_device(udev);
885 		goto out;
886 
887 	case UI_DEV_DESTROY:
888 		uinput_destroy_device(udev);
889 		goto out;
890 
891 	case UI_DEV_SETUP:
892 		retval = uinput_dev_setup(udev, p);
893 		goto out;
894 
895 	/* UI_ABS_SETUP is handled in the variable size ioctls */
896 
897 	case UI_SET_EVBIT:
898 		retval = uinput_set_bit(arg, evbit, EV_MAX);
899 		goto out;
900 
901 	case UI_SET_KEYBIT:
902 		retval = uinput_set_bit(arg, keybit, KEY_MAX);
903 		goto out;
904 
905 	case UI_SET_RELBIT:
906 		retval = uinput_set_bit(arg, relbit, REL_MAX);
907 		goto out;
908 
909 	case UI_SET_ABSBIT:
910 		retval = uinput_set_bit(arg, absbit, ABS_MAX);
911 		goto out;
912 
913 	case UI_SET_MSCBIT:
914 		retval = uinput_set_bit(arg, mscbit, MSC_MAX);
915 		goto out;
916 
917 	case UI_SET_LEDBIT:
918 		retval = uinput_set_bit(arg, ledbit, LED_MAX);
919 		goto out;
920 
921 	case UI_SET_SNDBIT:
922 		retval = uinput_set_bit(arg, sndbit, SND_MAX);
923 		goto out;
924 
925 	case UI_SET_FFBIT:
926 		retval = uinput_set_bit(arg, ffbit, FF_MAX);
927 		goto out;
928 
929 	case UI_SET_SWBIT:
930 		retval = uinput_set_bit(arg, swbit, SW_MAX);
931 		goto out;
932 
933 	case UI_SET_PROPBIT:
934 		retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
935 		goto out;
936 
937 	case UI_SET_PHYS:
938 		if (udev->state == UIST_CREATED) {
939 			retval = -EINVAL;
940 			goto out;
941 		}
942 
943 		phys = strndup_user(p, 1024);
944 		if (IS_ERR(phys)) {
945 			retval = PTR_ERR(phys);
946 			goto out;
947 		}
948 
949 		kfree(udev->dev->phys);
950 		udev->dev->phys = phys;
951 		goto out;
952 
953 	case UI_BEGIN_FF_UPLOAD:
954 		retval = uinput_ff_upload_from_user(p, &ff_up);
955 		if (retval)
956 			goto out;
957 
958 		req = uinput_request_find(udev, ff_up.request_id);
959 		if (!req || req->code != UI_FF_UPLOAD ||
960 		    !req->u.upload.effect) {
961 			retval = -EINVAL;
962 			goto out;
963 		}
964 
965 		ff_up.retval = 0;
966 		ff_up.effect = *req->u.upload.effect;
967 		if (req->u.upload.old)
968 			ff_up.old = *req->u.upload.old;
969 		else
970 			memset(&ff_up.old, 0, sizeof(struct ff_effect));
971 
972 		retval = uinput_ff_upload_to_user(p, &ff_up);
973 		goto out;
974 
975 	case UI_BEGIN_FF_ERASE:
976 		if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
977 			retval = -EFAULT;
978 			goto out;
979 		}
980 
981 		req = uinput_request_find(udev, ff_erase.request_id);
982 		if (!req || req->code != UI_FF_ERASE) {
983 			retval = -EINVAL;
984 			goto out;
985 		}
986 
987 		ff_erase.retval = 0;
988 		ff_erase.effect_id = req->u.effect_id;
989 		if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
990 			retval = -EFAULT;
991 			goto out;
992 		}
993 
994 		goto out;
995 
996 	case UI_END_FF_UPLOAD:
997 		retval = uinput_ff_upload_from_user(p, &ff_up);
998 		if (retval)
999 			goto out;
1000 
1001 		req = uinput_request_find(udev, ff_up.request_id);
1002 		if (!req || req->code != UI_FF_UPLOAD ||
1003 		    !req->u.upload.effect) {
1004 			retval = -EINVAL;
1005 			goto out;
1006 		}
1007 
1008 		req->retval = ff_up.retval;
1009 		complete(&req->done);
1010 		goto out;
1011 
1012 	case UI_END_FF_ERASE:
1013 		if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
1014 			retval = -EFAULT;
1015 			goto out;
1016 		}
1017 
1018 		req = uinput_request_find(udev, ff_erase.request_id);
1019 		if (!req || req->code != UI_FF_ERASE) {
1020 			retval = -EINVAL;
1021 			goto out;
1022 		}
1023 
1024 		req->retval = ff_erase.retval;
1025 		complete(&req->done);
1026 		goto out;
1027 	}
1028 
1029 	size = _IOC_SIZE(cmd);
1030 
1031 	/* Now check variable-length commands */
1032 	switch (cmd & ~IOCSIZE_MASK) {
1033 	case UI_GET_SYSNAME(0):
1034 		if (udev->state != UIST_CREATED) {
1035 			retval = -ENOENT;
1036 			goto out;
1037 		}
1038 		name = dev_name(&udev->dev->dev);
1039 		retval = uinput_str_to_user(p, name, size);
1040 		goto out;
1041 
1042 	case UI_ABS_SETUP & ~IOCSIZE_MASK:
1043 		retval = uinput_abs_setup(udev, p, size);
1044 		goto out;
1045 	}
1046 
1047 	retval = -EINVAL;
1048  out:
1049 	mutex_unlock(&udev->mutex);
1050 	return retval;
1051 }
1052 
uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)1053 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1054 {
1055 	return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
1056 }
1057 
1058 #ifdef CONFIG_COMPAT
1059 
1060 /*
1061  * These IOCTLs change their size and thus their numbers between
1062  * 32 and 64 bits.
1063  */
1064 #define UI_SET_PHYS_COMPAT		\
1065 	_IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
1066 #define UI_BEGIN_FF_UPLOAD_COMPAT	\
1067 	_IOWR(UINPUT_IOCTL_BASE, 200, struct uinput_ff_upload_compat)
1068 #define UI_END_FF_UPLOAD_COMPAT		\
1069 	_IOW(UINPUT_IOCTL_BASE, 201, struct uinput_ff_upload_compat)
1070 
uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)1071 static long uinput_compat_ioctl(struct file *file,
1072 				unsigned int cmd, unsigned long arg)
1073 {
1074 	switch (cmd) {
1075 	case UI_SET_PHYS_COMPAT:
1076 		cmd = UI_SET_PHYS;
1077 		break;
1078 	case UI_BEGIN_FF_UPLOAD_COMPAT:
1079 		cmd = UI_BEGIN_FF_UPLOAD;
1080 		break;
1081 	case UI_END_FF_UPLOAD_COMPAT:
1082 		cmd = UI_END_FF_UPLOAD;
1083 		break;
1084 	}
1085 
1086 	return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
1087 }
1088 #endif
1089 
1090 static const struct file_operations uinput_fops = {
1091 	.owner		= THIS_MODULE,
1092 	.open		= uinput_open,
1093 	.release	= uinput_release,
1094 	.read		= uinput_read,
1095 	.write		= uinput_write,
1096 	.poll		= uinput_poll,
1097 	.unlocked_ioctl	= uinput_ioctl,
1098 #ifdef CONFIG_COMPAT
1099 	.compat_ioctl	= uinput_compat_ioctl,
1100 #endif
1101 	.llseek		= no_llseek,
1102 };
1103 
1104 static struct miscdevice uinput_misc = {
1105 	.fops		= &uinput_fops,
1106 	.minor		= UINPUT_MINOR,
1107 	.name		= UINPUT_NAME,
1108 };
1109 module_misc_device(uinput_misc);
1110 
1111 MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
1112 MODULE_ALIAS("devname:" UINPUT_NAME);
1113 
1114 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
1115 MODULE_DESCRIPTION("User level driver support for input subsystem");
1116 MODULE_LICENSE("GPL");
1117