2 * User level driver support for input subsystem
4 * Heavily based on evdev.c by Vojtech Pavlik
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.
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.
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
20 * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
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
32 * - first public version
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>
40 #include <linux/miscdevice.h>
41 #include <linux/uinput.h>
42 #include <linux/overflow.h>
43 #include <linux/input/mt.h>
44 #include "../input-compat.h"
46 static int uinput_dev_event(struct input_dev *dev,
47 unsigned int type, unsigned int code, int value)
49 struct uinput_device *udev = input_get_drvdata(dev);
51 udev->buff[udev->head].type = type;
52 udev->buff[udev->head].code = code;
53 udev->buff[udev->head].value = value;
54 do_gettimeofday(&udev->buff[udev->head].time);
55 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
57 wake_up_interruptible(&udev->waitq);
62 /* Atomically allocate an ID for the given request. Returns 0 on success. */
63 static bool uinput_request_alloc_id(struct uinput_device *udev,
64 struct uinput_request *request)
67 bool reserved = false;
69 spin_lock(&udev->requests_lock);
71 for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
72 if (!udev->requests[id]) {
74 udev->requests[id] = request;
80 spin_unlock(&udev->requests_lock);
84 static struct uinput_request *uinput_request_find(struct uinput_device *udev,
87 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
88 if (id >= UINPUT_NUM_REQUESTS)
91 return udev->requests[id];
94 static int uinput_request_reserve_slot(struct uinput_device *udev,
95 struct uinput_request *request)
97 /* Allocate slot. If none are available right away, wait. */
98 return wait_event_interruptible(udev->requests_waitq,
99 uinput_request_alloc_id(udev, request));
102 static void uinput_request_done(struct uinput_device *udev,
103 struct uinput_request *request)
105 /* Mark slot as available */
106 udev->requests[request->id] = NULL;
107 wake_up(&udev->requests_waitq);
109 complete(&request->done);
112 static int uinput_request_send(struct uinput_device *udev,
113 struct uinput_request *request)
117 retval = mutex_lock_interruptible(&udev->mutex);
121 if (udev->state != UIST_CREATED) {
126 init_completion(&request->done);
129 * Tell our userspace application about this new request
130 * by queueing an input event.
132 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
135 mutex_unlock(&udev->mutex);
139 static int uinput_request_submit(struct uinput_device *udev,
140 struct uinput_request *request)
144 error = uinput_request_reserve_slot(udev, request);
148 error = uinput_request_send(udev, request);
150 uinput_request_done(udev, request);
154 wait_for_completion(&request->done);
155 return request->retval;
159 * Fail all outstanding requests so handlers don't wait for the userspace
160 * to finish processing them.
162 static void uinput_flush_requests(struct uinput_device *udev)
164 struct uinput_request *request;
167 spin_lock(&udev->requests_lock);
169 for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
170 request = udev->requests[i];
172 request->retval = -ENODEV;
173 uinput_request_done(udev, request);
177 spin_unlock(&udev->requests_lock);
180 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
182 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
185 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
187 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
190 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
192 return uinput_dev_event(dev, EV_FF, effect_id, value);
195 static int uinput_dev_upload_effect(struct input_dev *dev,
196 struct ff_effect *effect,
197 struct ff_effect *old)
199 struct uinput_device *udev = input_get_drvdata(dev);
200 struct uinput_request request;
203 * uinput driver does not currently support periodic effects with
204 * custom waveform since it does not have a way to pass buffer of
205 * samples (custom_data) to userspace. If ever there is a device
206 * supporting custom waveforms we would need to define an additional
207 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
209 if (effect->type == FF_PERIODIC &&
210 effect->u.periodic.waveform == FF_CUSTOM)
213 request.code = UI_FF_UPLOAD;
214 request.u.upload.effect = effect;
215 request.u.upload.old = old;
217 return uinput_request_submit(udev, &request);
220 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
222 struct uinput_device *udev = input_get_drvdata(dev);
223 struct uinput_request request;
225 if (!test_bit(EV_FF, dev->evbit))
228 request.code = UI_FF_ERASE;
229 request.u.effect_id = effect_id;
231 return uinput_request_submit(udev, &request);
234 static int uinput_dev_flush(struct input_dev *dev, struct file *file)
237 * If we are called with file == NULL that means we are tearing
238 * down the device, and therefore we can not handle FF erase
239 * requests: either we are handling UI_DEV_DESTROY (and holding
240 * the udev->mutex), or the file descriptor is closed and there is
241 * nobody on the other side anymore.
243 return file ? input_ff_flush(dev, file) : 0;
246 static void uinput_destroy_device(struct uinput_device *udev)
248 const char *name, *phys;
249 struct input_dev *dev = udev->dev;
250 enum uinput_state old_state = udev->state;
252 udev->state = UIST_NEW_DEVICE;
257 if (old_state == UIST_CREATED) {
258 uinput_flush_requests(udev);
259 input_unregister_device(dev);
261 input_free_device(dev);
269 static int uinput_create_device(struct uinput_device *udev)
271 struct input_dev *dev = udev->dev;
274 if (udev->state != UIST_SETUP_COMPLETE) {
275 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
279 if (test_bit(EV_ABS, dev->evbit)) {
280 input_alloc_absinfo(dev);
286 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
287 nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
288 error = input_mt_init_slots(dev, nslot, 0);
291 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
292 input_set_events_per_packet(dev, 60);
296 if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
297 printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
303 if (udev->ff_effects_max) {
304 error = input_ff_create(dev, udev->ff_effects_max);
308 dev->ff->upload = uinput_dev_upload_effect;
309 dev->ff->erase = uinput_dev_erase_effect;
310 dev->ff->playback = uinput_dev_playback;
311 dev->ff->set_gain = uinput_dev_set_gain;
312 dev->ff->set_autocenter = uinput_dev_set_autocenter;
314 * The standard input_ff_flush() implementation does
315 * not quite work for uinput as we can't reasonably
316 * handle FF requests during device teardown.
318 dev->flush = uinput_dev_flush;
321 error = input_register_device(udev->dev);
325 udev->state = UIST_CREATED;
329 fail2: input_ff_destroy(dev);
330 fail1: uinput_destroy_device(udev);
334 static int uinput_open(struct inode *inode, struct file *file)
336 struct uinput_device *newdev;
338 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
342 mutex_init(&newdev->mutex);
343 spin_lock_init(&newdev->requests_lock);
344 init_waitqueue_head(&newdev->requests_waitq);
345 init_waitqueue_head(&newdev->waitq);
346 newdev->state = UIST_NEW_DEVICE;
348 file->private_data = newdev;
349 nonseekable_open(inode, file);
354 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
355 const struct input_absinfo *abs)
362 if ((min != 0 || max != 0) && max <= min) {
364 "%s: invalid abs[%02x] min:%d max:%d\n",
365 UINPUT_NAME, code, min, max);
369 if (!check_sub_overflow(max, min, &range) && abs->flat > range) {
371 "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
372 UINPUT_NAME, code, abs->flat, min, max);
379 static int uinput_validate_absbits(struct input_dev *dev)
384 if (!test_bit(EV_ABS, dev->evbit))
388 * Check if absmin/absmax/absfuzz/absflat are sane.
391 for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
395 error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
403 static int uinput_allocate_device(struct uinput_device *udev)
405 udev->dev = input_allocate_device();
409 udev->dev->event = uinput_dev_event;
410 input_set_drvdata(udev->dev, udev);
415 static int uinput_dev_setup(struct uinput_device *udev,
416 struct uinput_setup __user *arg)
418 struct uinput_setup setup;
419 struct input_dev *dev;
421 if (udev->state == UIST_CREATED)
424 if (copy_from_user(&setup, arg, sizeof(setup)))
432 udev->ff_effects_max = setup.ff_effects_max;
435 dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
439 udev->state = UIST_SETUP_COMPLETE;
443 static int uinput_abs_setup(struct uinput_device *udev,
444 struct uinput_setup __user *arg, size_t size)
446 struct uinput_abs_setup setup = {};
447 struct input_dev *dev;
450 if (size > sizeof(setup))
453 if (udev->state == UIST_CREATED)
456 if (copy_from_user(&setup, arg, size))
459 if (setup.code > ABS_MAX)
464 error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
468 input_alloc_absinfo(dev);
472 set_bit(setup.code, dev->absbit);
473 dev->absinfo[setup.code] = setup.absinfo;
477 /* legacy setup via write() */
478 static int uinput_setup_device_legacy(struct uinput_device *udev,
479 const char __user *buffer, size_t count)
481 struct uinput_user_dev *user_dev;
482 struct input_dev *dev;
486 if (count != sizeof(struct uinput_user_dev))
490 retval = uinput_allocate_device(udev);
497 user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
498 if (IS_ERR(user_dev))
499 return PTR_ERR(user_dev);
501 udev->ff_effects_max = user_dev->ff_effects_max;
503 /* Ensure name is filled in */
504 if (!user_dev->name[0]) {
510 dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
517 dev->id.bustype = user_dev->id.bustype;
518 dev->id.vendor = user_dev->id.vendor;
519 dev->id.product = user_dev->id.product;
520 dev->id.version = user_dev->id.version;
522 for (i = 0; i < ABS_CNT; i++) {
523 input_abs_set_max(dev, i, user_dev->absmax[i]);
524 input_abs_set_min(dev, i, user_dev->absmin[i]);
525 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
526 input_abs_set_flat(dev, i, user_dev->absflat[i]);
529 retval = uinput_validate_absbits(dev);
533 udev->state = UIST_SETUP_COMPLETE;
541 static ssize_t uinput_inject_events(struct uinput_device *udev,
542 const char __user *buffer, size_t count)
544 struct input_event ev;
547 if (count != 0 && count < input_event_size())
550 while (bytes + input_event_size() <= count) {
552 * Note that even if some events were fetched successfully
553 * we are still going to return EFAULT instead of partial
554 * count to let userspace know that it got it's buffers
557 if (input_event_from_user(buffer + bytes, &ev))
560 input_event(udev->dev, ev.type, ev.code, ev.value);
561 bytes += input_event_size();
567 static ssize_t uinput_write(struct file *file, const char __user *buffer,
568 size_t count, loff_t *ppos)
570 struct uinput_device *udev = file->private_data;
576 retval = mutex_lock_interruptible(&udev->mutex);
580 retval = udev->state == UIST_CREATED ?
581 uinput_inject_events(udev, buffer, count) :
582 uinput_setup_device_legacy(udev, buffer, count);
584 mutex_unlock(&udev->mutex);
589 static bool uinput_fetch_next_event(struct uinput_device *udev,
590 struct input_event *event)
594 spin_lock_irq(&udev->dev->event_lock);
596 have_event = udev->head != udev->tail;
598 *event = udev->buff[udev->tail];
599 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
602 spin_unlock_irq(&udev->dev->event_lock);
607 static ssize_t uinput_events_to_user(struct uinput_device *udev,
608 char __user *buffer, size_t count)
610 struct input_event event;
613 while (read + input_event_size() <= count &&
614 uinput_fetch_next_event(udev, &event)) {
616 if (input_event_to_user(buffer + read, &event))
619 read += input_event_size();
625 static ssize_t uinput_read(struct file *file, char __user *buffer,
626 size_t count, loff_t *ppos)
628 struct uinput_device *udev = file->private_data;
631 if (count != 0 && count < input_event_size())
635 retval = mutex_lock_interruptible(&udev->mutex);
639 if (udev->state != UIST_CREATED)
641 else if (udev->head == udev->tail &&
642 (file->f_flags & O_NONBLOCK))
645 retval = uinput_events_to_user(udev, buffer, count);
647 mutex_unlock(&udev->mutex);
649 if (retval || count == 0)
652 if (!(file->f_flags & O_NONBLOCK))
653 retval = wait_event_interruptible(udev->waitq,
654 udev->head != udev->tail ||
655 udev->state != UIST_CREATED);
656 } while (retval == 0);
661 static unsigned int uinput_poll(struct file *file, poll_table *wait)
663 struct uinput_device *udev = file->private_data;
665 poll_wait(file, &udev->waitq, wait);
667 if (udev->head != udev->tail)
668 return POLLIN | POLLRDNORM;
673 static int uinput_release(struct inode *inode, struct file *file)
675 struct uinput_device *udev = file->private_data;
677 uinput_destroy_device(udev);
684 struct uinput_ff_upload_compat {
687 struct ff_effect_compat effect;
688 struct ff_effect_compat old;
691 static int uinput_ff_upload_to_user(char __user *buffer,
692 const struct uinput_ff_upload *ff_up)
694 if (in_compat_syscall()) {
695 struct uinput_ff_upload_compat ff_up_compat;
697 ff_up_compat.request_id = ff_up->request_id;
698 ff_up_compat.retval = ff_up->retval;
700 * It so happens that the pointer that gives us the trouble
701 * is the last field in the structure. Since we don't support
702 * custom waveforms in uinput anyway we can just copy the whole
703 * thing (to the compat size) and ignore the pointer.
705 memcpy(&ff_up_compat.effect, &ff_up->effect,
706 sizeof(struct ff_effect_compat));
707 memcpy(&ff_up_compat.old, &ff_up->old,
708 sizeof(struct ff_effect_compat));
710 if (copy_to_user(buffer, &ff_up_compat,
711 sizeof(struct uinput_ff_upload_compat)))
714 if (copy_to_user(buffer, ff_up,
715 sizeof(struct uinput_ff_upload)))
722 static int uinput_ff_upload_from_user(const char __user *buffer,
723 struct uinput_ff_upload *ff_up)
725 if (in_compat_syscall()) {
726 struct uinput_ff_upload_compat ff_up_compat;
728 if (copy_from_user(&ff_up_compat, buffer,
729 sizeof(struct uinput_ff_upload_compat)))
732 ff_up->request_id = ff_up_compat.request_id;
733 ff_up->retval = ff_up_compat.retval;
734 memcpy(&ff_up->effect, &ff_up_compat.effect,
735 sizeof(struct ff_effect_compat));
736 memcpy(&ff_up->old, &ff_up_compat.old,
737 sizeof(struct ff_effect_compat));
740 if (copy_from_user(ff_up, buffer,
741 sizeof(struct uinput_ff_upload)))
750 static int uinput_ff_upload_to_user(char __user *buffer,
751 const struct uinput_ff_upload *ff_up)
753 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
759 static int uinput_ff_upload_from_user(const char __user *buffer,
760 struct uinput_ff_upload *ff_up)
762 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
770 #define uinput_set_bit(_arg, _bit, _max) \
773 if (udev->state == UIST_CREATED) \
775 else if ((_arg) > (_max)) \
777 else set_bit((_arg), udev->dev->_bit); \
781 static int uinput_str_to_user(void __user *dest, const char *str,
784 char __user *p = dest;
793 len = strlen(str) + 1;
797 ret = copy_to_user(p, str, len);
801 /* force terminating '\0' */
802 ret = put_user(0, p + len - 1);
803 return ret ? -EFAULT : len;
806 static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
807 unsigned long arg, void __user *p)
810 struct uinput_device *udev = file->private_data;
811 struct uinput_ff_upload ff_up;
812 struct uinput_ff_erase ff_erase;
813 struct uinput_request *req;
818 retval = mutex_lock_interruptible(&udev->mutex);
823 retval = uinput_allocate_device(udev);
830 if (put_user(UINPUT_VERSION,
831 (unsigned int __user *)p))
836 retval = uinput_create_device(udev);
840 uinput_destroy_device(udev);
844 retval = uinput_dev_setup(udev, p);
847 /* UI_ABS_SETUP is handled in the variable size ioctls */
850 retval = uinput_set_bit(arg, evbit, EV_MAX);
854 retval = uinput_set_bit(arg, keybit, KEY_MAX);
858 retval = uinput_set_bit(arg, relbit, REL_MAX);
862 retval = uinput_set_bit(arg, absbit, ABS_MAX);
866 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
870 retval = uinput_set_bit(arg, ledbit, LED_MAX);
874 retval = uinput_set_bit(arg, sndbit, SND_MAX);
878 retval = uinput_set_bit(arg, ffbit, FF_MAX);
882 retval = uinput_set_bit(arg, swbit, SW_MAX);
886 retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
890 if (udev->state == UIST_CREATED) {
895 phys = strndup_user(p, 1024);
897 retval = PTR_ERR(phys);
901 kfree(udev->dev->phys);
902 udev->dev->phys = phys;
905 case UI_BEGIN_FF_UPLOAD:
906 retval = uinput_ff_upload_from_user(p, &ff_up);
910 req = uinput_request_find(udev, ff_up.request_id);
911 if (!req || req->code != UI_FF_UPLOAD ||
912 !req->u.upload.effect) {
918 ff_up.effect = *req->u.upload.effect;
919 if (req->u.upload.old)
920 ff_up.old = *req->u.upload.old;
922 memset(&ff_up.old, 0, sizeof(struct ff_effect));
924 retval = uinput_ff_upload_to_user(p, &ff_up);
927 case UI_BEGIN_FF_ERASE:
928 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
933 req = uinput_request_find(udev, ff_erase.request_id);
934 if (!req || req->code != UI_FF_ERASE) {
940 ff_erase.effect_id = req->u.effect_id;
941 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
948 case UI_END_FF_UPLOAD:
949 retval = uinput_ff_upload_from_user(p, &ff_up);
953 req = uinput_request_find(udev, ff_up.request_id);
954 if (!req || req->code != UI_FF_UPLOAD ||
955 !req->u.upload.effect) {
960 req->retval = ff_up.retval;
961 uinput_request_done(udev, req);
964 case UI_END_FF_ERASE:
965 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
970 req = uinput_request_find(udev, ff_erase.request_id);
971 if (!req || req->code != UI_FF_ERASE) {
976 req->retval = ff_erase.retval;
977 uinput_request_done(udev, req);
981 size = _IOC_SIZE(cmd);
983 /* Now check variable-length commands */
984 switch (cmd & ~IOCSIZE_MASK) {
985 case UI_GET_SYSNAME(0):
986 if (udev->state != UIST_CREATED) {
990 name = dev_name(&udev->dev->dev);
991 retval = uinput_str_to_user(p, name, size);
994 case UI_ABS_SETUP & ~IOCSIZE_MASK:
995 retval = uinput_abs_setup(udev, p, size);
1001 mutex_unlock(&udev->mutex);
1005 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1007 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
1010 #ifdef CONFIG_COMPAT
1013 * These IOCTLs change their size and thus their numbers between
1016 #define UI_SET_PHYS_COMPAT \
1017 _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
1018 #define UI_BEGIN_FF_UPLOAD_COMPAT \
1019 _IOWR(UINPUT_IOCTL_BASE, 200, struct uinput_ff_upload_compat)
1020 #define UI_END_FF_UPLOAD_COMPAT \
1021 _IOW(UINPUT_IOCTL_BASE, 201, struct uinput_ff_upload_compat)
1023 static long uinput_compat_ioctl(struct file *file,
1024 unsigned int cmd, unsigned long arg)
1027 case UI_SET_PHYS_COMPAT:
1030 case UI_BEGIN_FF_UPLOAD_COMPAT:
1031 cmd = UI_BEGIN_FF_UPLOAD;
1033 case UI_END_FF_UPLOAD_COMPAT:
1034 cmd = UI_END_FF_UPLOAD;
1038 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
1042 static const struct file_operations uinput_fops = {
1043 .owner = THIS_MODULE,
1044 .open = uinput_open,
1045 .release = uinput_release,
1046 .read = uinput_read,
1047 .write = uinput_write,
1048 .poll = uinput_poll,
1049 .unlocked_ioctl = uinput_ioctl,
1050 #ifdef CONFIG_COMPAT
1051 .compat_ioctl = uinput_compat_ioctl,
1053 .llseek = no_llseek,
1056 static struct miscdevice uinput_misc = {
1057 .fops = &uinput_fops,
1058 .minor = UINPUT_MINOR,
1059 .name = UINPUT_NAME,
1061 module_misc_device(uinput_misc);
1063 MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
1064 MODULE_ALIAS("devname:" UINPUT_NAME);
1066 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
1067 MODULE_DESCRIPTION("User level driver support for input subsystem");
1068 MODULE_LICENSE("GPL");
1069 MODULE_VERSION("0.3");