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