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