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 <uapi/linux/uinput.h>
35 #include <linux/poll.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
41 #include <linux/miscdevice.h>
42 #include <linux/input/mt.h>
43 #include "../input-compat.h"
45 #define UINPUT_NAME "uinput"
46 #define UINPUT_BUFFER_SIZE 16
47 #define UINPUT_NUM_REQUESTS 16
49 enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED };
51 struct uinput_request {
53 unsigned int code; /* UI_FF_UPLOAD, UI_FF_ERASE */
56 struct completion done;
59 unsigned int effect_id;
61 struct ff_effect *effect;
62 struct ff_effect *old;
67 struct uinput_device {
68 struct input_dev *dev;
70 enum uinput_state state;
71 wait_queue_head_t waitq;
75 struct input_event buff[UINPUT_BUFFER_SIZE];
76 unsigned int ff_effects_max;
78 struct uinput_request *requests[UINPUT_NUM_REQUESTS];
79 wait_queue_head_t requests_waitq;
80 spinlock_t requests_lock;
83 static int uinput_dev_event(struct input_dev *dev,
84 unsigned int type, unsigned int code, int value)
86 struct uinput_device *udev = input_get_drvdata(dev);
88 udev->buff[udev->head].type = type;
89 udev->buff[udev->head].code = code;
90 udev->buff[udev->head].value = value;
91 do_gettimeofday(&udev->buff[udev->head].time);
92 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
94 wake_up_interruptible(&udev->waitq);
99 /* Atomically allocate an ID for the given request. Returns 0 on success. */
100 static bool uinput_request_alloc_id(struct uinput_device *udev,
101 struct uinput_request *request)
104 bool reserved = false;
106 spin_lock(&udev->requests_lock);
108 for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
109 if (!udev->requests[id]) {
111 udev->requests[id] = request;
117 spin_unlock(&udev->requests_lock);
121 static struct uinput_request *uinput_request_find(struct uinput_device *udev,
124 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
125 if (id >= UINPUT_NUM_REQUESTS)
128 return udev->requests[id];
131 static int uinput_request_reserve_slot(struct uinput_device *udev,
132 struct uinput_request *request)
134 /* Allocate slot. If none are available right away, wait. */
135 return wait_event_interruptible(udev->requests_waitq,
136 uinput_request_alloc_id(udev, request));
139 static void uinput_request_release_slot(struct uinput_device *udev,
142 /* Mark slot as available */
143 spin_lock(&udev->requests_lock);
144 udev->requests[id] = NULL;
145 spin_unlock(&udev->requests_lock);
147 wake_up(&udev->requests_waitq);
150 static int uinput_request_send(struct uinput_device *udev,
151 struct uinput_request *request)
155 retval = mutex_lock_interruptible(&udev->mutex);
159 if (udev->state != UIST_CREATED) {
164 init_completion(&request->done);
167 * Tell our userspace application about this new request
168 * by queueing an input event.
170 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
173 mutex_unlock(&udev->mutex);
177 static int uinput_request_submit(struct uinput_device *udev,
178 struct uinput_request *request)
182 retval = uinput_request_reserve_slot(udev, request);
186 retval = uinput_request_send(udev, request);
190 if (!wait_for_completion_timeout(&request->done, 30 * HZ)) {
195 retval = request->retval;
198 uinput_request_release_slot(udev, request->id);
203 * Fail all outstanding requests so handlers don't wait for the userspace
204 * to finish processing them.
206 static void uinput_flush_requests(struct uinput_device *udev)
208 struct uinput_request *request;
211 spin_lock(&udev->requests_lock);
213 for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
214 request = udev->requests[i];
216 request->retval = -ENODEV;
217 complete(&request->done);
221 spin_unlock(&udev->requests_lock);
224 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
226 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
229 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
231 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
234 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
236 return uinput_dev_event(dev, EV_FF, effect_id, value);
239 static int uinput_dev_upload_effect(struct input_dev *dev,
240 struct ff_effect *effect,
241 struct ff_effect *old)
243 struct uinput_device *udev = input_get_drvdata(dev);
244 struct uinput_request request;
247 * uinput driver does not currently support periodic effects with
248 * custom waveform since it does not have a way to pass buffer of
249 * samples (custom_data) to userspace. If ever there is a device
250 * supporting custom waveforms we would need to define an additional
251 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
253 if (effect->type == FF_PERIODIC &&
254 effect->u.periodic.waveform == FF_CUSTOM)
257 request.code = UI_FF_UPLOAD;
258 request.u.upload.effect = effect;
259 request.u.upload.old = old;
261 return uinput_request_submit(udev, &request);
264 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
266 struct uinput_device *udev = input_get_drvdata(dev);
267 struct uinput_request request;
269 if (!test_bit(EV_FF, dev->evbit))
272 request.code = UI_FF_ERASE;
273 request.u.effect_id = effect_id;
275 return uinput_request_submit(udev, &request);
278 static int uinput_dev_flush(struct input_dev *dev, struct file *file)
281 * If we are called with file == NULL that means we are tearing
282 * down the device, and therefore we can not handle FF erase
283 * requests: either we are handling UI_DEV_DESTROY (and holding
284 * the udev->mutex), or the file descriptor is closed and there is
285 * nobody on the other side anymore.
287 return file ? input_ff_flush(dev, file) : 0;
290 static void uinput_destroy_device(struct uinput_device *udev)
292 const char *name, *phys;
293 struct input_dev *dev = udev->dev;
294 enum uinput_state old_state = udev->state;
296 udev->state = UIST_NEW_DEVICE;
301 if (old_state == UIST_CREATED) {
302 uinput_flush_requests(udev);
303 input_unregister_device(dev);
305 input_free_device(dev);
313 static int uinput_create_device(struct uinput_device *udev)
315 struct input_dev *dev = udev->dev;
318 if (udev->state != UIST_SETUP_COMPLETE) {
319 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
323 if (test_bit(EV_ABS, dev->evbit)) {
324 input_alloc_absinfo(dev);
330 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
331 nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
332 error = input_mt_init_slots(dev, nslot, 0);
335 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
336 input_set_events_per_packet(dev, 60);
340 if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
341 printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
347 if (udev->ff_effects_max) {
348 error = input_ff_create(dev, udev->ff_effects_max);
352 dev->ff->upload = uinput_dev_upload_effect;
353 dev->ff->erase = uinput_dev_erase_effect;
354 dev->ff->playback = uinput_dev_playback;
355 dev->ff->set_gain = uinput_dev_set_gain;
356 dev->ff->set_autocenter = uinput_dev_set_autocenter;
358 * The standard input_ff_flush() implementation does
359 * not quite work for uinput as we can't reasonably
360 * handle FF requests during device teardown.
362 dev->flush = uinput_dev_flush;
365 dev->event = uinput_dev_event;
367 input_set_drvdata(udev->dev, udev);
369 error = input_register_device(udev->dev);
373 udev->state = UIST_CREATED;
377 fail2: input_ff_destroy(dev);
378 fail1: uinput_destroy_device(udev);
382 static int uinput_open(struct inode *inode, struct file *file)
384 struct uinput_device *newdev;
386 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
390 mutex_init(&newdev->mutex);
391 spin_lock_init(&newdev->requests_lock);
392 init_waitqueue_head(&newdev->requests_waitq);
393 init_waitqueue_head(&newdev->waitq);
394 newdev->state = UIST_NEW_DEVICE;
396 file->private_data = newdev;
397 nonseekable_open(inode, file);
402 static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
403 const struct input_absinfo *abs)
410 if ((min != 0 || max != 0) && max <= min) {
412 "%s: invalid abs[%02x] min:%d max:%d\n",
413 UINPUT_NAME, code, min, max);
417 if (abs->flat > max - min) {
419 "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
420 UINPUT_NAME, code, abs->flat, min, max);
427 static int uinput_validate_absbits(struct input_dev *dev)
432 if (!test_bit(EV_ABS, dev->evbit))
436 * Check if absmin/absmax/absfuzz/absflat are sane.
439 for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
443 error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
451 static int uinput_dev_setup(struct uinput_device *udev,
452 struct uinput_setup __user *arg)
454 struct uinput_setup setup;
455 struct input_dev *dev;
457 if (udev->state == UIST_CREATED)
460 if (copy_from_user(&setup, arg, sizeof(setup)))
468 udev->ff_effects_max = setup.ff_effects_max;
471 dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
475 udev->state = UIST_SETUP_COMPLETE;
479 static int uinput_abs_setup(struct uinput_device *udev,
480 struct uinput_setup __user *arg, size_t size)
482 struct uinput_abs_setup setup = {};
483 struct input_dev *dev;
486 if (size > sizeof(setup))
489 if (udev->state == UIST_CREATED)
492 if (copy_from_user(&setup, arg, size))
495 if (setup.code > ABS_MAX)
500 error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
504 input_alloc_absinfo(dev);
508 set_bit(setup.code, dev->absbit);
509 dev->absinfo[setup.code] = setup.absinfo;
513 /* legacy setup via write() */
514 static int uinput_setup_device_legacy(struct uinput_device *udev,
515 const char __user *buffer, size_t count)
517 struct uinput_user_dev *user_dev;
518 struct input_dev *dev;
522 if (count != sizeof(struct uinput_user_dev))
526 udev->dev = input_allocate_device();
533 user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
534 if (IS_ERR(user_dev))
535 return PTR_ERR(user_dev);
537 udev->ff_effects_max = user_dev->ff_effects_max;
539 /* Ensure name is filled in */
540 if (!user_dev->name[0]) {
546 dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
553 dev->id.bustype = user_dev->id.bustype;
554 dev->id.vendor = user_dev->id.vendor;
555 dev->id.product = user_dev->id.product;
556 dev->id.version = user_dev->id.version;
558 for (i = 0; i < ABS_CNT; i++) {
559 input_abs_set_max(dev, i, user_dev->absmax[i]);
560 input_abs_set_min(dev, i, user_dev->absmin[i]);
561 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
562 input_abs_set_flat(dev, i, user_dev->absflat[i]);
565 retval = uinput_validate_absbits(dev);
569 udev->state = UIST_SETUP_COMPLETE;
577 static ssize_t uinput_inject_events(struct uinput_device *udev,
578 const char __user *buffer, size_t count)
580 struct input_event ev;
583 if (count != 0 && count < input_event_size())
586 while (bytes + input_event_size() <= count) {
588 * Note that even if some events were fetched successfully
589 * we are still going to return EFAULT instead of partial
590 * count to let userspace know that it got it's buffers
593 if (input_event_from_user(buffer + bytes, &ev))
596 input_event(udev->dev, ev.type, ev.code, ev.value);
597 bytes += input_event_size();
603 static ssize_t uinput_write(struct file *file, const char __user *buffer,
604 size_t count, loff_t *ppos)
606 struct uinput_device *udev = file->private_data;
612 retval = mutex_lock_interruptible(&udev->mutex);
616 retval = udev->state == UIST_CREATED ?
617 uinput_inject_events(udev, buffer, count) :
618 uinput_setup_device_legacy(udev, buffer, count);
620 mutex_unlock(&udev->mutex);
625 static bool uinput_fetch_next_event(struct uinput_device *udev,
626 struct input_event *event)
630 spin_lock_irq(&udev->dev->event_lock);
632 have_event = udev->head != udev->tail;
634 *event = udev->buff[udev->tail];
635 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
638 spin_unlock_irq(&udev->dev->event_lock);
643 static ssize_t uinput_events_to_user(struct uinput_device *udev,
644 char __user *buffer, size_t count)
646 struct input_event event;
649 while (read + input_event_size() <= count &&
650 uinput_fetch_next_event(udev, &event)) {
652 if (input_event_to_user(buffer + read, &event))
655 read += input_event_size();
661 static ssize_t uinput_read(struct file *file, char __user *buffer,
662 size_t count, loff_t *ppos)
664 struct uinput_device *udev = file->private_data;
667 if (count != 0 && count < input_event_size())
671 retval = mutex_lock_interruptible(&udev->mutex);
675 if (udev->state != UIST_CREATED)
677 else if (udev->head == udev->tail &&
678 (file->f_flags & O_NONBLOCK))
681 retval = uinput_events_to_user(udev, buffer, count);
683 mutex_unlock(&udev->mutex);
685 if (retval || count == 0)
688 if (!(file->f_flags & O_NONBLOCK))
689 retval = wait_event_interruptible(udev->waitq,
690 udev->head != udev->tail ||
691 udev->state != UIST_CREATED);
692 } while (retval == 0);
697 static __poll_t uinput_poll(struct file *file, poll_table *wait)
699 struct uinput_device *udev = file->private_data;
701 poll_wait(file, &udev->waitq, wait);
703 if (udev->head != udev->tail)
704 return POLLIN | POLLRDNORM;
709 static int uinput_release(struct inode *inode, struct file *file)
711 struct uinput_device *udev = file->private_data;
713 uinput_destroy_device(udev);
720 struct uinput_ff_upload_compat {
723 struct ff_effect_compat effect;
724 struct ff_effect_compat old;
727 static int uinput_ff_upload_to_user(char __user *buffer,
728 const struct uinput_ff_upload *ff_up)
730 if (in_compat_syscall()) {
731 struct uinput_ff_upload_compat ff_up_compat;
733 ff_up_compat.request_id = ff_up->request_id;
734 ff_up_compat.retval = ff_up->retval;
736 * It so happens that the pointer that gives us the trouble
737 * is the last field in the structure. Since we don't support
738 * custom waveforms in uinput anyway we can just copy the whole
739 * thing (to the compat size) and ignore the pointer.
741 memcpy(&ff_up_compat.effect, &ff_up->effect,
742 sizeof(struct ff_effect_compat));
743 memcpy(&ff_up_compat.old, &ff_up->old,
744 sizeof(struct ff_effect_compat));
746 if (copy_to_user(buffer, &ff_up_compat,
747 sizeof(struct uinput_ff_upload_compat)))
750 if (copy_to_user(buffer, ff_up,
751 sizeof(struct uinput_ff_upload)))
758 static int uinput_ff_upload_from_user(const char __user *buffer,
759 struct uinput_ff_upload *ff_up)
761 if (in_compat_syscall()) {
762 struct uinput_ff_upload_compat ff_up_compat;
764 if (copy_from_user(&ff_up_compat, buffer,
765 sizeof(struct uinput_ff_upload_compat)))
768 ff_up->request_id = ff_up_compat.request_id;
769 ff_up->retval = ff_up_compat.retval;
770 memcpy(&ff_up->effect, &ff_up_compat.effect,
771 sizeof(struct ff_effect_compat));
772 memcpy(&ff_up->old, &ff_up_compat.old,
773 sizeof(struct ff_effect_compat));
776 if (copy_from_user(ff_up, buffer,
777 sizeof(struct uinput_ff_upload)))
786 static int uinput_ff_upload_to_user(char __user *buffer,
787 const struct uinput_ff_upload *ff_up)
789 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
795 static int uinput_ff_upload_from_user(const char __user *buffer,
796 struct uinput_ff_upload *ff_up)
798 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
806 #define uinput_set_bit(_arg, _bit, _max) \
809 if (udev->state == UIST_CREATED) \
811 else if ((_arg) > (_max)) \
813 else set_bit((_arg), udev->dev->_bit); \
817 static int uinput_str_to_user(void __user *dest, const char *str,
820 char __user *p = dest;
829 len = strlen(str) + 1;
833 ret = copy_to_user(p, str, len);
837 /* force terminating '\0' */
838 ret = put_user(0, p + len - 1);
839 return ret ? -EFAULT : len;
842 static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
843 unsigned long arg, void __user *p)
846 struct uinput_device *udev = file->private_data;
847 struct uinput_ff_upload ff_up;
848 struct uinput_ff_erase ff_erase;
849 struct uinput_request *req;
854 retval = mutex_lock_interruptible(&udev->mutex);
859 udev->dev = input_allocate_device();
868 if (put_user(UINPUT_VERSION, (unsigned int __user *)p))
873 retval = uinput_create_device(udev);
877 uinput_destroy_device(udev);
881 retval = uinput_dev_setup(udev, p);
884 /* UI_ABS_SETUP is handled in the variable size ioctls */
887 retval = uinput_set_bit(arg, evbit, EV_MAX);
891 retval = uinput_set_bit(arg, keybit, KEY_MAX);
895 retval = uinput_set_bit(arg, relbit, REL_MAX);
899 retval = uinput_set_bit(arg, absbit, ABS_MAX);
903 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
907 retval = uinput_set_bit(arg, ledbit, LED_MAX);
911 retval = uinput_set_bit(arg, sndbit, SND_MAX);
915 retval = uinput_set_bit(arg, ffbit, FF_MAX);
919 retval = uinput_set_bit(arg, swbit, SW_MAX);
923 retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
927 if (udev->state == UIST_CREATED) {
932 phys = strndup_user(p, 1024);
934 retval = PTR_ERR(phys);
938 kfree(udev->dev->phys);
939 udev->dev->phys = phys;
942 case UI_BEGIN_FF_UPLOAD:
943 retval = uinput_ff_upload_from_user(p, &ff_up);
947 req = uinput_request_find(udev, ff_up.request_id);
948 if (!req || req->code != UI_FF_UPLOAD ||
949 !req->u.upload.effect) {
955 ff_up.effect = *req->u.upload.effect;
956 if (req->u.upload.old)
957 ff_up.old = *req->u.upload.old;
959 memset(&ff_up.old, 0, sizeof(struct ff_effect));
961 retval = uinput_ff_upload_to_user(p, &ff_up);
964 case UI_BEGIN_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) {
977 ff_erase.effect_id = req->u.effect_id;
978 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
985 case UI_END_FF_UPLOAD:
986 retval = uinput_ff_upload_from_user(p, &ff_up);
990 req = uinput_request_find(udev, ff_up.request_id);
991 if (!req || req->code != UI_FF_UPLOAD ||
992 !req->u.upload.effect) {
997 req->retval = ff_up.retval;
998 complete(&req->done);
1001 case UI_END_FF_ERASE:
1002 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
1007 req = uinput_request_find(udev, ff_erase.request_id);
1008 if (!req || req->code != UI_FF_ERASE) {
1013 req->retval = ff_erase.retval;
1014 complete(&req->done);
1018 size = _IOC_SIZE(cmd);
1020 /* Now check variable-length commands */
1021 switch (cmd & ~IOCSIZE_MASK) {
1022 case UI_GET_SYSNAME(0):
1023 if (udev->state != UIST_CREATED) {
1027 name = dev_name(&udev->dev->dev);
1028 retval = uinput_str_to_user(p, name, size);
1031 case UI_ABS_SETUP & ~IOCSIZE_MASK:
1032 retval = uinput_abs_setup(udev, p, size);
1038 mutex_unlock(&udev->mutex);
1042 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1044 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
1047 #ifdef CONFIG_COMPAT
1049 #define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
1051 static long uinput_compat_ioctl(struct file *file,
1052 unsigned int cmd, unsigned long arg)
1054 if (cmd == UI_SET_PHYS_COMPAT)
1057 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
1061 static const struct file_operations uinput_fops = {
1062 .owner = THIS_MODULE,
1063 .open = uinput_open,
1064 .release = uinput_release,
1065 .read = uinput_read,
1066 .write = uinput_write,
1067 .poll = uinput_poll,
1068 .unlocked_ioctl = uinput_ioctl,
1069 #ifdef CONFIG_COMPAT
1070 .compat_ioctl = uinput_compat_ioctl,
1072 .llseek = no_llseek,
1075 static struct miscdevice uinput_misc = {
1076 .fops = &uinput_fops,
1077 .minor = UINPUT_MINOR,
1078 .name = UINPUT_NAME,
1080 module_misc_device(uinput_misc);
1082 MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
1083 MODULE_ALIAS("devname:" UINPUT_NAME);
1085 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
1086 MODULE_DESCRIPTION("User level driver support for input subsystem");
1087 MODULE_LICENSE("GPL");
1088 MODULE_VERSION("0.3");