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