blob: c30704fa1a064fecfcedbf647354e1ecf617cb0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * User level driver support for input subsystem
3 *
4 * Heavily based on evdev.c by Vojtech Pavlik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
21 *
22 * Changes/Revisions:
Benjamin Tissoirese3480a62014-01-30 17:20:24 -080023 * 0.4 01/09/2014 (Benjamin Tissoires <benjamin.tissoires@redhat.com>)
24 * - add UI_GET_SYSNAME ioctl
Anssi Hannulaff462552006-07-19 01:41:09 -040025 * 0.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
26 * - updated ff support for the changes in kernel interface
27 * - added MODULE_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
29 * - added force feedback support
30 * - added UI_SET_PHYS
31 * 0.1 20/06/2002
32 * - first public version
33 */
34#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040035#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
37#include <linux/module.h>
38#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/fs.h>
40#include <linux/miscdevice.h>
41#include <linux/uinput.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010042#include <linux/input/mt.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040043#include "../input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Dmitry Torokhov54ce1652012-07-29 22:48:32 -070045static int uinput_dev_event(struct input_dev *dev,
46 unsigned int type, unsigned int code, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Dmitry Torokhov373f9712007-04-12 01:34:33 -040048 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 udev->buff[udev->head].type = type;
51 udev->buff[udev->head].code = code;
52 udev->buff[udev->head].value = value;
53 do_gettimeofday(&udev->buff[udev->head].time);
54 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
55
56 wake_up_interruptible(&udev->waitq);
57
58 return 0;
59}
60
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070061/* Atomically allocate an ID for the given request. Returns 0 on success. */
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070062static bool uinput_request_alloc_id(struct uinput_device *udev,
63 struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070065 unsigned int id;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070066 bool reserved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dmitry Torokhov0048e602005-06-30 00:48:14 -050068 spin_lock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050069
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070070 for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (!udev->requests[id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 request->id = id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -050073 udev->requests[id] = request;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070074 reserved = true;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070077 }
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050078
Dmitry Torokhov0048e602005-06-30 00:48:14 -050079 spin_unlock(&udev->requests_lock);
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070080 return reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070083static struct uinput_request *uinput_request_find(struct uinput_device *udev,
84 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070087 if (id >= UINPUT_NUM_REQUESTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return NULL;
Philip Langdale2d56f3a2008-10-16 22:31:42 -040089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return udev->requests[id];
91}
92
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070093static int uinput_request_reserve_slot(struct uinput_device *udev,
94 struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050096 /* Allocate slot. If none are available right away, wait. */
97 return wait_event_interruptible(udev->requests_waitq,
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070098 uinput_request_alloc_id(udev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700101static void uinput_request_release_slot(struct uinput_device *udev,
102 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500104 /* Mark slot as available */
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700105 spin_lock(&udev->requests_lock);
106 udev->requests[id] = NULL;
107 spin_unlock(&udev->requests_lock);
Dmitry Torokhove7507ed2005-10-17 16:43:32 -0700108
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700109 wake_up(&udev->requests_waitq);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500110}
111
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700112static int uinput_request_send(struct uinput_device *udev,
113 struct uinput_request *request)
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500114{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700115 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700117 retval = mutex_lock_interruptible(&udev->mutex);
118 if (retval)
119 return retval;
120
121 if (udev->state != UIST_CREATED) {
122 retval = -ENODEV;
123 goto out;
124 }
125
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700126 init_completion(&request->done);
127
128 /*
129 * Tell our userspace application about this new request
130 * by queueing an input event.
131 */
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700132 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
133
134 out:
135 mutex_unlock(&udev->mutex);
136 return retval;
137}
138
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700139static int uinput_request_submit(struct uinput_device *udev,
140 struct uinput_request *request)
141{
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700142 int retval;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700143
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700144 retval = uinput_request_reserve_slot(udev, request);
145 if (retval)
146 return retval;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700147
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700148 retval = uinput_request_send(udev, request);
149 if (retval)
150 goto out;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700151
Dmitry Torokhov8e009112017-09-06 16:31:29 -0700152 if (!wait_for_completion_timeout(&request->done, 30 * HZ)) {
153 retval = -ETIMEDOUT;
154 goto out;
155 }
156
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700157 retval = request->retval;
158
159 out:
160 uinput_request_release_slot(udev, request->id);
161 return retval;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700162}
163
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700164/*
Dmitry Torokhov54ce1652012-07-29 22:48:32 -0700165 * Fail all outstanding requests so handlers don't wait for the userspace
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700166 * to finish processing them.
167 */
168static void uinput_flush_requests(struct uinput_device *udev)
169{
170 struct uinput_request *request;
171 int i;
172
173 spin_lock(&udev->requests_lock);
174
175 for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
176 request = udev->requests[i];
177 if (request) {
178 request->retval = -ENODEV;
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700179 complete(&request->done);
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700180 }
181 }
182
183 spin_unlock(&udev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Anssi Hannulaff462552006-07-19 01:41:09 -0400186static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
187{
188 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
189}
190
191static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
192{
193 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
194}
195
196static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
197{
198 return uinput_dev_event(dev, EV_FF, effect_id, value);
199}
200
Dmitry Torokhov54ce1652012-07-29 22:48:32 -0700201static int uinput_dev_upload_effect(struct input_dev *dev,
202 struct ff_effect *effect,
203 struct ff_effect *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700205 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct uinput_request request;
207
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400208 /*
209 * uinput driver does not currently support periodic effects with
210 * custom waveform since it does not have a way to pass buffer of
211 * samples (custom_data) to userspace. If ever there is a device
212 * supporting custom waveforms we would need to define an additional
213 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
214 */
215 if (effect->type == FF_PERIODIC &&
216 effect->u.periodic.waveform == FF_CUSTOM)
217 return -EINVAL;
218
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500219 request.code = UI_FF_UPLOAD;
Anssi Hannulaff462552006-07-19 01:41:09 -0400220 request.u.upload.effect = effect;
221 request.u.upload.old = old;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500222
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700223 return uinput_request_submit(udev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
227{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700228 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct uinput_request request;
230
231 if (!test_bit(EV_FF, dev->evbit))
232 return -ENOSYS;
233
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500234 request.code = UI_FF_ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 request.u.effect_id = effect_id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500236
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700237 return uinput_request_submit(udev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Dmitry Torokhove8b95722017-09-01 17:13:43 -0700240static int uinput_dev_flush(struct input_dev *dev, struct file *file)
241{
242 /*
243 * If we are called with file == NULL that means we are tearing
244 * down the device, and therefore we can not handle FF erase
245 * requests: either we are handling UI_DEV_DESTROY (and holding
246 * the udev->mutex), or the file descriptor is closed and there is
247 * nobody on the other side anymore.
248 */
249 return file ? input_ff_flush(dev, file) : 0;
250}
251
Dmitry Torokhov29506412005-11-20 00:51:22 -0500252static void uinput_destroy_device(struct uinput_device *udev)
253{
254 const char *name, *phys;
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700255 struct input_dev *dev = udev->dev;
256 enum uinput_state old_state = udev->state;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500257
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700258 udev->state = UIST_NEW_DEVICE;
259
260 if (dev) {
261 name = dev->name;
262 phys = dev->phys;
263 if (old_state == UIST_CREATED) {
264 uinput_flush_requests(udev);
265 input_unregister_device(dev);
266 } else {
267 input_free_device(dev);
268 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500269 kfree(name);
270 kfree(phys);
271 udev->dev = NULL;
272 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int uinput_create_device(struct uinput_device *udev)
276{
Anssi Hannulaff462552006-07-19 01:41:09 -0400277 struct input_dev *dev = udev->dev;
David Herrmannfbae10d2015-10-25 10:34:13 +0100278 int error, nslot;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500279
280 if (udev->state != UIST_SETUP_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
282 return -EINVAL;
283 }
284
Dmitry Torokhov601bbbe2017-01-31 14:56:43 -0800285 if (test_bit(EV_ABS, dev->evbit)) {
286 input_alloc_absinfo(dev);
287 if (!dev->absinfo) {
288 error = -EINVAL;
David Herrmannfbae10d2015-10-25 10:34:13 +0100289 goto fail1;
Dmitry Torokhov601bbbe2017-01-31 14:56:43 -0800290 }
291
292 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
293 nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
294 error = input_mt_init_slots(dev, nslot, 0);
295 if (error)
296 goto fail1;
297 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
298 input_set_events_per_packet(dev, 60);
299 }
David Herrmannfbae10d2015-10-25 10:34:13 +0100300 }
301
Elias Vanderstuyftdaf6cd02015-12-18 17:32:19 -0800302 if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) {
303 printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n",
304 UINPUT_NAME);
305 error = -EINVAL;
306 goto fail1;
307 }
308
Anssi Hannulaff462552006-07-19 01:41:09 -0400309 if (udev->ff_effects_max) {
310 error = input_ff_create(dev, udev->ff_effects_max);
311 if (error)
312 goto fail1;
313
314 dev->ff->upload = uinput_dev_upload_effect;
315 dev->ff->erase = uinput_dev_erase_effect;
316 dev->ff->playback = uinput_dev_playback;
317 dev->ff->set_gain = uinput_dev_set_gain;
318 dev->ff->set_autocenter = uinput_dev_set_autocenter;
Dmitry Torokhove8b95722017-09-01 17:13:43 -0700319 /*
320 * The standard input_ff_flush() implementation does
321 * not quite work for uinput as we can't reasonably
322 * handle FF requests during device teardown.
323 */
324 dev->flush = uinput_dev_flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Anssi Hannulaff462552006-07-19 01:41:09 -0400327 error = input_register_device(udev->dev);
328 if (error)
329 goto fail2;
330
Dmitry Torokhov29506412005-11-20 00:51:22 -0500331 udev->state = UIST_CREATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 return 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400334
335 fail2: input_ff_destroy(dev);
336 fail1: uinput_destroy_device(udev);
337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340static int uinput_open(struct inode *inode, struct file *file)
341{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500342 struct uinput_device *newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dmitry Torokhov29506412005-11-20 00:51:22 -0500344 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!newdev)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500346 return -ENOMEM;
347
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500348 mutex_init(&newdev->mutex);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500349 spin_lock_init(&newdev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 init_waitqueue_head(&newdev->requests_waitq);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500351 init_waitqueue_head(&newdev->waitq);
352 newdev->state = UIST_NEW_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 file->private_data = newdev;
Dmitry Torokhovdaf8a962010-02-04 00:30:39 -0800355 nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
David Herrmannfbae10d2015-10-25 10:34:13 +0100360static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code,
361 const struct input_absinfo *abs)
362{
363 int min, max;
364
365 min = abs->minimum;
366 max = abs->maximum;
367
368 if ((min != 0 || max != 0) && max <= min) {
369 printk(KERN_DEBUG
370 "%s: invalid abs[%02x] min:%d max:%d\n",
371 UINPUT_NAME, code, min, max);
372 return -EINVAL;
373 }
374
375 if (abs->flat > max - min) {
376 printk(KERN_DEBUG
377 "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n",
378 UINPUT_NAME, code, abs->flat, min, max);
379 return -EINVAL;
380 }
381
382 return 0;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static int uinput_validate_absbits(struct input_dev *dev)
386{
387 unsigned int cnt;
David Herrmannfbae10d2015-10-25 10:34:13 +0100388 int error;
David Herrmannbcb898e2014-07-20 17:16:23 -0700389
390 if (!test_bit(EV_ABS, dev->evbit))
391 return 0;
392
393 /*
394 * Check if absmin/absmax/absfuzz/absflat are sane.
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Anshul Gargb6d30962015-07-09 10:18:22 -0700397 for_each_set_bit(cnt, dev->absbit, ABS_CNT) {
David Herrmannfbae10d2015-10-25 10:34:13 +0100398 if (!dev->absinfo)
David Herrmannbcb898e2014-07-20 17:16:23 -0700399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Herrmannfbae10d2015-10-25 10:34:13 +0100401 error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]);
402 if (error)
403 return error;
David Herrmannbcb898e2014-07-20 17:16:23 -0700404 }
405
406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Dmitry Torokhov29506412005-11-20 00:51:22 -0500409static int uinput_allocate_device(struct uinput_device *udev)
410{
411 udev->dev = input_allocate_device();
412 if (!udev->dev)
413 return -ENOMEM;
414
415 udev->dev->event = uinput_dev_event;
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400416 input_set_drvdata(udev->dev, udev);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500417
418 return 0;
419}
420
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800421static int uinput_dev_setup(struct uinput_device *udev,
422 struct uinput_setup __user *arg)
423{
424 struct uinput_setup setup;
425 struct input_dev *dev;
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800426
427 if (udev->state == UIST_CREATED)
428 return -EINVAL;
429
430 if (copy_from_user(&setup, arg, sizeof(setup)))
431 return -EFAULT;
432
433 if (!setup.name[0])
434 return -EINVAL;
435
436 dev = udev->dev;
437 dev->id = setup.id;
438 udev->ff_effects_max = setup.ff_effects_max;
439
440 kfree(dev->name);
441 dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL);
442 if (!dev->name)
443 return -ENOMEM;
444
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800445 udev->state = UIST_SETUP_COMPLETE;
446 return 0;
447}
448
449static int uinput_abs_setup(struct uinput_device *udev,
450 struct uinput_setup __user *arg, size_t size)
451{
452 struct uinput_abs_setup setup = {};
453 struct input_dev *dev;
David Herrmannfbae10d2015-10-25 10:34:13 +0100454 int error;
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800455
456 if (size > sizeof(setup))
457 return -E2BIG;
458
459 if (udev->state == UIST_CREATED)
460 return -EINVAL;
461
462 if (copy_from_user(&setup, arg, size))
463 return -EFAULT;
464
465 if (setup.code > ABS_MAX)
466 return -ERANGE;
467
468 dev = udev->dev;
469
David Herrmannfbae10d2015-10-25 10:34:13 +0100470 error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo);
471 if (error)
472 return error;
473
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800474 input_alloc_absinfo(dev);
475 if (!dev->absinfo)
476 return -ENOMEM;
477
478 set_bit(setup.code, dev->absbit);
479 dev->absinfo[setup.code] = setup.absinfo;
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800480 return 0;
481}
482
483/* legacy setup via write() */
484static int uinput_setup_device_legacy(struct uinput_device *udev,
485 const char __user *buffer, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct uinput_user_dev *user_dev;
488 struct input_dev *dev;
David Herrmann5d9d6e92011-02-11 01:10:44 -0800489 int i;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500490 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Torokhov29506412005-11-20 00:51:22 -0500492 if (count != sizeof(struct uinput_user_dev))
493 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dmitry Torokhov29506412005-11-20 00:51:22 -0500495 if (!udev->dev) {
496 retval = uinput_allocate_device(udev);
497 if (retval)
498 return retval;
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dev = udev->dev;
502
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800503 user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
Dan Carpenter163d2772011-02-18 08:30:52 -0800504 if (IS_ERR(user_dev))
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800505 return PTR_ERR(user_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Anssi Hannulaff462552006-07-19 01:41:09 -0400507 udev->ff_effects_max = user_dev->ff_effects_max;
508
David Herrmann5d9d6e92011-02-11 01:10:44 -0800509 /* Ensure name is filled in */
510 if (!user_dev->name[0]) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500511 retval = -EINVAL;
512 goto exit;
513 }
514
515 kfree(dev->name);
David Herrmann5d9d6e92011-02-11 01:10:44 -0800516 dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
517 GFP_KERNEL);
518 if (!dev->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 retval = -ENOMEM;
520 goto exit;
521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dev->id.bustype = user_dev->id.bustype;
524 dev->id.vendor = user_dev->id.vendor;
525 dev->id.product = user_dev->id.product;
526 dev->id.version = user_dev->id.version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Dmitry Torokhov72d47362015-09-19 11:22:57 -0700528 for (i = 0; i < ABS_CNT; i++) {
Daniel Mack987a6c02010-08-02 20:15:17 -0700529 input_abs_set_max(dev, i, user_dev->absmax[i]);
530 input_abs_set_min(dev, i, user_dev->absmin[i]);
531 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
532 input_abs_set_flat(dev, i, user_dev->absflat[i]);
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
David Herrmannbcb898e2014-07-20 17:16:23 -0700535 retval = uinput_validate_absbits(dev);
536 if (retval < 0)
537 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dmitry Torokhov29506412005-11-20 00:51:22 -0500539 udev->state = UIST_SETUP_COMPLETE;
540 retval = count;
541
542 exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kfree(user_dev);
544 return retval;
545}
546
Ryan Malloncbf05412013-09-18 12:40:47 -0700547static ssize_t uinput_inject_events(struct uinput_device *udev,
548 const char __user *buffer, size_t count)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500549{
550 struct input_event ev;
Ryan Malloncbf05412013-09-18 12:40:47 -0700551 size_t bytes = 0;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500552
Ryan Malloncbf05412013-09-18 12:40:47 -0700553 if (count != 0 && count < input_event_size())
Dmitry Torokhov29506412005-11-20 00:51:22 -0500554 return -EINVAL;
555
Ryan Malloncbf05412013-09-18 12:40:47 -0700556 while (bytes + input_event_size() <= count) {
557 /*
558 * Note that even if some events were fetched successfully
559 * we are still going to return EFAULT instead of partial
560 * count to let userspace know that it got it's buffers
561 * all wrong.
562 */
563 if (input_event_from_user(buffer + bytes, &ev))
564 return -EFAULT;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500565
Ryan Malloncbf05412013-09-18 12:40:47 -0700566 input_event(udev->dev, ev.type, ev.code, ev.value);
567 bytes += input_event_size();
568 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500569
Ryan Malloncbf05412013-09-18 12:40:47 -0700570 return bytes;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500571}
572
Dmitry Torokhov54ce1652012-07-29 22:48:32 -0700573static ssize_t uinput_write(struct file *file, const char __user *buffer,
574 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct uinput_device *udev = file->private_data;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500577 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700579 if (count == 0)
580 return 0;
581
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500582 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500583 if (retval)
584 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dmitry Torokhov29506412005-11-20 00:51:22 -0500586 retval = udev->state == UIST_CREATED ?
Ryan Malloncbf05412013-09-18 12:40:47 -0700587 uinput_inject_events(udev, buffer, count) :
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800588 uinput_setup_device_legacy(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500590 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500591
592 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Dmitry Torokhov929d1af2012-07-29 22:48:31 -0700595static bool uinput_fetch_next_event(struct uinput_device *udev,
596 struct input_event *event)
597{
598 bool have_event;
599
600 spin_lock_irq(&udev->dev->event_lock);
601
602 have_event = udev->head != udev->tail;
603 if (have_event) {
604 *event = udev->buff[udev->tail];
605 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
606 }
607
608 spin_unlock_irq(&udev->dev->event_lock);
609
610 return have_event;
611}
612
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700613static ssize_t uinput_events_to_user(struct uinput_device *udev,
614 char __user *buffer, size_t count)
615{
616 struct input_event event;
617 size_t read = 0;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700618
619 while (read + input_event_size() <= count &&
620 uinput_fetch_next_event(udev, &event)) {
621
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700622 if (input_event_to_user(buffer + read, &event))
623 return -EFAULT;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700624
625 read += input_event_size();
626 }
627
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700628 return read;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700629}
630
631static ssize_t uinput_read(struct file *file, char __user *buffer,
632 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct uinput_device *udev = file->private_data;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700635 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Herrmannf40033a2012-07-29 22:48:31 -0700637 if (count != 0 && count < input_event_size())
638 return -EINVAL;
639
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700640 do {
641 retval = mutex_lock_interruptible(&udev->mutex);
642 if (retval)
643 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700645 if (udev->state != UIST_CREATED)
646 retval = -ENODEV;
647 else if (udev->head == udev->tail &&
648 (file->f_flags & O_NONBLOCK))
649 retval = -EAGAIN;
650 else
651 retval = uinput_events_to_user(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700653 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700655 if (retval || count == 0)
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700658 if (!(file->f_flags & O_NONBLOCK))
659 retval = wait_event_interruptible(udev->waitq,
660 udev->head != udev->tail ||
661 udev->state != UIST_CREATED);
662 } while (retval == 0);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return retval;
665}
666
667static unsigned int uinput_poll(struct file *file, poll_table *wait)
668{
669 struct uinput_device *udev = file->private_data;
670
671 poll_wait(file, &udev->waitq, wait);
672
673 if (udev->head != udev->tail)
674 return POLLIN | POLLRDNORM;
675
676 return 0;
677}
678
Dmitry Torokhov29506412005-11-20 00:51:22 -0500679static int uinput_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500681 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dmitry Torokhov29506412005-11-20 00:51:22 -0500683 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 kfree(udev);
685
686 return 0;
687}
688
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400689#ifdef CONFIG_COMPAT
690struct uinput_ff_upload_compat {
Dmitry Torokhovc5b35332012-07-29 22:48:32 -0700691 __u32 request_id;
692 __s32 retval;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400693 struct ff_effect_compat effect;
694 struct ff_effect_compat old;
695};
696
697static int uinput_ff_upload_to_user(char __user *buffer,
698 const struct uinput_ff_upload *ff_up)
699{
Andrew Mortonb8b4ead2016-03-25 14:20:47 -0700700 if (in_compat_syscall()) {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400701 struct uinput_ff_upload_compat ff_up_compat;
702
703 ff_up_compat.request_id = ff_up->request_id;
704 ff_up_compat.retval = ff_up->retval;
705 /*
706 * It so happens that the pointer that gives us the trouble
707 * is the last field in the structure. Since we don't support
708 * custom waveforms in uinput anyway we can just copy the whole
709 * thing (to the compat size) and ignore the pointer.
710 */
711 memcpy(&ff_up_compat.effect, &ff_up->effect,
712 sizeof(struct ff_effect_compat));
713 memcpy(&ff_up_compat.old, &ff_up->old,
714 sizeof(struct ff_effect_compat));
715
716 if (copy_to_user(buffer, &ff_up_compat,
717 sizeof(struct uinput_ff_upload_compat)))
718 return -EFAULT;
719 } else {
720 if (copy_to_user(buffer, ff_up,
721 sizeof(struct uinput_ff_upload)))
722 return -EFAULT;
723 }
724
725 return 0;
726}
727
728static int uinput_ff_upload_from_user(const char __user *buffer,
729 struct uinput_ff_upload *ff_up)
730{
Andrew Mortonb8b4ead2016-03-25 14:20:47 -0700731 if (in_compat_syscall()) {
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400732 struct uinput_ff_upload_compat ff_up_compat;
733
734 if (copy_from_user(&ff_up_compat, buffer,
735 sizeof(struct uinput_ff_upload_compat)))
736 return -EFAULT;
737
738 ff_up->request_id = ff_up_compat.request_id;
739 ff_up->retval = ff_up_compat.retval;
740 memcpy(&ff_up->effect, &ff_up_compat.effect,
741 sizeof(struct ff_effect_compat));
742 memcpy(&ff_up->old, &ff_up_compat.old,
743 sizeof(struct ff_effect_compat));
744
745 } else {
746 if (copy_from_user(ff_up, buffer,
747 sizeof(struct uinput_ff_upload)))
748 return -EFAULT;
749 }
750
751 return 0;
752}
753
754#else
755
756static int uinput_ff_upload_to_user(char __user *buffer,
757 const struct uinput_ff_upload *ff_up)
758{
759 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
760 return -EFAULT;
761
762 return 0;
763}
764
765static int uinput_ff_upload_from_user(const char __user *buffer,
766 struct uinput_ff_upload *ff_up)
767{
768 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
769 return -EFAULT;
770
771 return 0;
772}
773
774#endif
775
Dmitry Torokhov29506412005-11-20 00:51:22 -0500776#define uinput_set_bit(_arg, _bit, _max) \
777({ \
778 int __ret = 0; \
779 if (udev->state == UIST_CREATED) \
780 __ret = -EINVAL; \
781 else if ((_arg) > (_max)) \
782 __ret = -EINVAL; \
783 else set_bit((_arg), udev->dev->_bit); \
784 __ret; \
785})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Benjamin Tissoirese3480a62014-01-30 17:20:24 -0800787static int uinput_str_to_user(void __user *dest, const char *str,
788 unsigned int maxlen)
789{
790 char __user *p = dest;
791 int len, ret;
792
793 if (!str)
794 return -ENOENT;
795
796 if (maxlen == 0)
797 return -EINVAL;
798
799 len = strlen(str) + 1;
800 if (len > maxlen)
801 len = maxlen;
802
803 ret = copy_to_user(p, str, len);
804 if (ret)
805 return -EFAULT;
806
807 /* force terminating '\0' */
808 ret = put_user(0, p + len - 1);
809 return ret ? -EFAULT : len;
810}
811
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400812static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
813 unsigned long arg, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500815 int retval;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400816 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct uinput_ff_upload ff_up;
818 struct uinput_ff_erase ff_erase;
819 struct uinput_request *req;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500820 char *phys;
Benjamin Tissoirese3480a62014-01-30 17:20:24 -0800821 const char *name;
822 unsigned int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500824 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500825 if (retval)
826 return retval;
827
828 if (!udev->dev) {
829 retval = uinput_allocate_device(udev);
830 if (retval)
831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
834 switch (cmd) {
David Herrmannba4e9a62014-07-20 17:27:09 -0700835 case UI_GET_VERSION:
836 if (put_user(UINPUT_VERSION,
837 (unsigned int __user *)p))
838 retval = -EFAULT;
839 goto out;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 case UI_DEV_CREATE:
842 retval = uinput_create_device(udev);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 case UI_DEV_DESTROY:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500846 uinput_destroy_device(udev);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800849 case UI_DEV_SETUP:
850 retval = uinput_dev_setup(udev, p);
851 goto out;
852
853 /* UI_ABS_SETUP is handled in the variable size ioctls */
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 case UI_SET_EVBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500856 retval = uinput_set_bit(arg, evbit, EV_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 case UI_SET_KEYBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500860 retval = uinput_set_bit(arg, keybit, KEY_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 case UI_SET_RELBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500864 retval = uinput_set_bit(arg, relbit, REL_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 case UI_SET_ABSBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500868 retval = uinput_set_bit(arg, absbit, ABS_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 case UI_SET_MSCBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500872 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 case UI_SET_LEDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500876 retval = uinput_set_bit(arg, ledbit, LED_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 case UI_SET_SNDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500880 retval = uinput_set_bit(arg, sndbit, SND_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 case UI_SET_FFBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500884 retval = uinput_set_bit(arg, ffbit, FF_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500887 case UI_SET_SWBIT:
888 retval = uinput_set_bit(arg, swbit, SW_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800889 goto out;
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500890
Henrik Rydberg85b77202010-12-18 20:51:13 +0100891 case UI_SET_PROPBIT:
892 retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800893 goto out;
Henrik Rydberg85b77202010-12-18 20:51:13 +0100894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 case UI_SET_PHYS:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500896 if (udev->state == UIST_CREATED) {
897 retval = -EINVAL;
898 goto out;
899 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800900
901 phys = strndup_user(p, 1024);
902 if (IS_ERR(phys)) {
903 retval = PTR_ERR(phys);
904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800906
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500907 kfree(udev->dev->phys);
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800908 udev->dev->phys = phys;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800909 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 case UI_BEGIN_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400912 retval = uinput_ff_upload_from_user(p, &ff_up);
913 if (retval)
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800914 goto out;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 req = uinput_request_find(udev, ff_up.request_id);
Dmitry Torokhov54ce1652012-07-29 22:48:32 -0700917 if (!req || req->code != UI_FF_UPLOAD ||
918 !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 retval = -EINVAL;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ff_up.retval = 0;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400924 ff_up.effect = *req->u.upload.effect;
Anssi Hannulaff462552006-07-19 01:41:09 -0400925 if (req->u.upload.old)
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400926 ff_up.old = *req->u.upload.old;
Anssi Hannulaff462552006-07-19 01:41:09 -0400927 else
928 memset(&ff_up.old, 0, sizeof(struct ff_effect));
929
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400930 retval = uinput_ff_upload_to_user(p, &ff_up);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800931 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 case UI_BEGIN_FF_ERASE:
934 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
935 retval = -EFAULT;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800936 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400940 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 retval = -EINVAL;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800942 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ff_erase.retval = 0;
946 ff_erase.effect_id = req->u.effect_id;
947 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
948 retval = -EFAULT;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800949 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400951
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 case UI_END_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400955 retval = uinput_ff_upload_from_user(p, &ff_up);
956 if (retval)
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800957 goto out;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 req = uinput_request_find(udev, ff_up.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400960 if (!req || req->code != UI_FF_UPLOAD ||
961 !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 retval = -EINVAL;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800963 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 req->retval = ff_up.retval;
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700967 complete(&req->done);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800968 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 case UI_END_FF_ERASE:
971 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
972 retval = -EFAULT;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400977 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 retval = -EINVAL;
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800979 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 req->retval = ff_erase.retval;
Dmitry Torokhov6b4877c2017-09-06 16:22:59 -0700983 complete(&req->done);
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -0800984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500986
Benjamin Tissoirese3480a62014-01-30 17:20:24 -0800987 size = _IOC_SIZE(cmd);
988
989 /* Now check variable-length commands */
990 switch (cmd & ~IOCSIZE_MASK) {
991 case UI_GET_SYSNAME(0):
992 if (udev->state != UIST_CREATED) {
993 retval = -ENOENT;
994 goto out;
995 }
996 name = dev_name(&udev->dev->dev);
997 retval = uinput_str_to_user(p, name, size);
998 goto out;
Benjamin Tissoires052876f8e2015-12-18 17:20:09 -0800999
1000 case UI_ABS_SETUP & ~IOCSIZE_MASK:
1001 retval = uinput_abs_setup(udev, p, size);
1002 goto out;
Benjamin Tissoirese3480a62014-01-30 17:20:24 -08001003 }
1004
Benjamin Tisssoires9d51e802014-01-30 17:16:36 -08001005 retval = -EINVAL;
Dmitry Torokhov29506412005-11-20 00:51:22 -05001006 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -05001007 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return retval;
1009}
1010
Philip Langdale2d56f3a2008-10-16 22:31:42 -04001011static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1012{
1013 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
1014}
1015
1016#ifdef CONFIG_COMPAT
Ricky Liangaffa80b2016-05-20 10:58:59 -07001017
1018#define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t)
1019
Dmitry Torokhov54ce1652012-07-29 22:48:32 -07001020static long uinput_compat_ioctl(struct file *file,
1021 unsigned int cmd, unsigned long arg)
Philip Langdale2d56f3a2008-10-16 22:31:42 -04001022{
Ricky Liangaffa80b2016-05-20 10:58:59 -07001023 if (cmd == UI_SET_PHYS_COMPAT)
1024 cmd = UI_SET_PHYS;
1025
Philip Langdale2d56f3a2008-10-16 22:31:42 -04001026 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
1027}
1028#endif
1029
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001030static const struct file_operations uinput_fops = {
Dmitry Torokhov29506412005-11-20 00:51:22 -05001031 .owner = THIS_MODULE,
1032 .open = uinput_open,
1033 .release = uinput_release,
1034 .read = uinput_read,
1035 .write = uinput_write,
1036 .poll = uinput_poll,
1037 .unlocked_ioctl = uinput_ioctl,
Philip Langdale2d56f3a2008-10-16 22:31:42 -04001038#ifdef CONFIG_COMPAT
1039 .compat_ioctl = uinput_compat_ioctl,
1040#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02001041 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042};
1043
1044static struct miscdevice uinput_misc = {
Dmitry Torokhov29506412005-11-20 00:51:22 -05001045 .fops = &uinput_fops,
1046 .minor = UINPUT_MINOR,
1047 .name = UINPUT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048};
PrasannaKumar Muralidharanca75d602016-08-25 22:30:49 +05301049module_misc_device(uinput_misc);
1050
Kay Sievers8905aaa2010-08-19 09:52:28 -07001051MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
1052MODULE_ALIAS("devname:" UINPUT_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
1055MODULE_DESCRIPTION("User level driver support for input subsystem");
1056MODULE_LICENSE("GPL");
Anssi Hannulaff462552006-07-19 01:41:09 -04001057MODULE_VERSION("0.3");