blob: b247e1c8e8f655bc6fef59e70542f7ea27534b8b [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:
Anssi Hannulaff462552006-07-19 01:41:09 -040023 * 0.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
24 * - updated ff support for the changes in kernel interface
25 * - added MODULE_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
27 * - added force feedback support
28 * - added UI_SET_PHYS
29 * 0.1 20/06/2002
30 * - first public version
31 */
32#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040033#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/module.h>
36#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <linux/uinput.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010040#include <linux/input/mt.h>
Philip Langdale2d56f3a2008-10-16 22:31:42 -040041#include "../input-compat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
44{
Dmitry Torokhov373f9712007-04-12 01:34:33 -040045 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 udev->buff[udev->head].type = type;
48 udev->buff[udev->head].code = code;
49 udev->buff[udev->head].value = value;
50 do_gettimeofday(&udev->buff[udev->head].time);
51 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
52
53 wake_up_interruptible(&udev->waitq);
54
55 return 0;
56}
57
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070058/* Atomically allocate an ID for the given request. Returns 0 on success. */
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070059static bool uinput_request_alloc_id(struct uinput_device *udev,
60 struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070062 unsigned int id;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070063 bool reserved = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Dmitry Torokhov0048e602005-06-30 00:48:14 -050065 spin_lock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050066
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070067 for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!udev->requests[id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 request->id = id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -050070 udev->requests[id] = request;
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070071 reserved = true;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070074 }
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050075
Dmitry Torokhov0048e602005-06-30 00:48:14 -050076 spin_unlock(&udev->requests_lock);
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070077 return reserved;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070080static struct uinput_request *uinput_request_find(struct uinput_device *udev,
81 unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
Dmitry Torokhovc5b35332012-07-29 22:48:32 -070084 if (id >= UINPUT_NUM_REQUESTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return NULL;
Philip Langdale2d56f3a2008-10-16 22:31:42 -040086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return udev->requests[id];
88}
89
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070090static int uinput_request_reserve_slot(struct uinput_device *udev,
91 struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050093 /* Allocate slot. If none are available right away, wait. */
94 return wait_event_interruptible(udev->requests_waitq,
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070095 uinput_request_alloc_id(udev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Dmitry Torokhov0048e602005-06-30 00:48:14 -050098static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500100 /* Mark slot as available */
101 udev->requests[request->id] = NULL;
Dmitry Torokhove597f0c82005-11-20 00:51:43 -0500102 wake_up(&udev->requests_waitq);
Dmitry Torokhove7507ed2005-10-17 16:43:32 -0700103
104 complete(&request->done);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500105}
106
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700107static int uinput_request_send(struct uinput_device *udev,
108 struct uinput_request *request)
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500109{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700110 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700112 retval = mutex_lock_interruptible(&udev->mutex);
113 if (retval)
114 return retval;
115
116 if (udev->state != UIST_CREATED) {
117 retval = -ENODEV;
118 goto out;
119 }
120
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700121 init_completion(&request->done);
122
123 /*
124 * Tell our userspace application about this new request
125 * by queueing an input event.
126 */
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700127 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
128
129 out:
130 mutex_unlock(&udev->mutex);
131 return retval;
132}
133
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700134static int uinput_request_submit(struct uinput_device *udev,
135 struct uinput_request *request)
136{
137 int error;
138
139 error = uinput_request_reserve_slot(udev, request);
140 if (error)
141 return error;
142
143 error = uinput_request_send(udev, request);
144 if (error) {
145 uinput_request_done(udev, request);
146 return error;
147 }
148
149 wait_for_completion(&request->done);
150 return request->retval;
151}
152
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700153/*
154 * Fail all ouitstanding requests so handlers don't wait for the userspace
155 * to finish processing them.
156 */
157static void uinput_flush_requests(struct uinput_device *udev)
158{
159 struct uinput_request *request;
160 int i;
161
162 spin_lock(&udev->requests_lock);
163
164 for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
165 request = udev->requests[i];
166 if (request) {
167 request->retval = -ENODEV;
168 uinput_request_done(udev, request);
169 }
170 }
171
172 spin_unlock(&udev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Anssi Hannulaff462552006-07-19 01:41:09 -0400175static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
176{
177 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
178}
179
180static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
181{
182 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
183}
184
185static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
186{
187 return uinput_dev_event(dev, EV_FF, effect_id, value);
188}
189
190static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700192 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct uinput_request request;
194
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400195 /*
196 * uinput driver does not currently support periodic effects with
197 * custom waveform since it does not have a way to pass buffer of
198 * samples (custom_data) to userspace. If ever there is a device
199 * supporting custom waveforms we would need to define an additional
200 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
201 */
202 if (effect->type == FF_PERIODIC &&
203 effect->u.periodic.waveform == FF_CUSTOM)
204 return -EINVAL;
205
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500206 request.code = UI_FF_UPLOAD;
Anssi Hannulaff462552006-07-19 01:41:09 -0400207 request.u.upload.effect = effect;
208 request.u.upload.old = old;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500209
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700210 return uinput_request_submit(udev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
214{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700215 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct uinput_request request;
217
218 if (!test_bit(EV_FF, dev->evbit))
219 return -ENOSYS;
220
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500221 request.code = UI_FF_ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 request.u.effect_id = effect_id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500223
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700224 return uinput_request_submit(udev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Dmitry Torokhov29506412005-11-20 00:51:22 -0500227static void uinput_destroy_device(struct uinput_device *udev)
228{
229 const char *name, *phys;
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700230 struct input_dev *dev = udev->dev;
231 enum uinput_state old_state = udev->state;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500232
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700233 udev->state = UIST_NEW_DEVICE;
234
235 if (dev) {
236 name = dev->name;
237 phys = dev->phys;
238 if (old_state == UIST_CREATED) {
239 uinput_flush_requests(udev);
240 input_unregister_device(dev);
241 } else {
242 input_free_device(dev);
243 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500244 kfree(name);
245 kfree(phys);
246 udev->dev = NULL;
247 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static int uinput_create_device(struct uinput_device *udev)
251{
Anssi Hannulaff462552006-07-19 01:41:09 -0400252 struct input_dev *dev = udev->dev;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500253 int error;
254
255 if (udev->state != UIST_SETUP_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
257 return -EINVAL;
258 }
259
Anssi Hannulaff462552006-07-19 01:41:09 -0400260 if (udev->ff_effects_max) {
261 error = input_ff_create(dev, udev->ff_effects_max);
262 if (error)
263 goto fail1;
264
265 dev->ff->upload = uinput_dev_upload_effect;
266 dev->ff->erase = uinput_dev_erase_effect;
267 dev->ff->playback = uinput_dev_playback;
268 dev->ff->set_gain = uinput_dev_set_gain;
269 dev->ff->set_autocenter = uinput_dev_set_autocenter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Anssi Hannulaff462552006-07-19 01:41:09 -0400272 error = input_register_device(udev->dev);
273 if (error)
274 goto fail2;
275
Dmitry Torokhov29506412005-11-20 00:51:22 -0500276 udev->state = UIST_CREATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400279
280 fail2: input_ff_destroy(dev);
281 fail1: uinput_destroy_device(udev);
282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static int uinput_open(struct inode *inode, struct file *file)
286{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500287 struct uinput_device *newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dmitry Torokhov29506412005-11-20 00:51:22 -0500289 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!newdev)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500291 return -ENOMEM;
292
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500293 mutex_init(&newdev->mutex);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500294 spin_lock_init(&newdev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 init_waitqueue_head(&newdev->requests_waitq);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500296 init_waitqueue_head(&newdev->waitq);
297 newdev->state = UIST_NEW_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 file->private_data = newdev;
Dmitry Torokhovdaf8a962010-02-04 00:30:39 -0800300 nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static int uinput_validate_absbits(struct input_dev *dev)
306{
307 unsigned int cnt;
308 int retval = 0;
309
Daniel Mack81c2a3b2010-05-20 22:52:58 -0700310 for (cnt = 0; cnt < ABS_CNT; cnt++) {
Peter Hutterera718d792011-03-30 22:25:34 -0700311 int min, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!test_bit(cnt, dev->absbit))
313 continue;
314
Peter Hutterera718d792011-03-30 22:25:34 -0700315 min = input_abs_get_min(dev, cnt);
316 max = input_abs_get_max(dev, cnt);
317
318 if ((min != 0 || max != 0) && max <= min) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 printk(KERN_DEBUG
320 "%s: invalid abs[%02x] min:%d max:%d\n",
321 UINPUT_NAME, cnt,
Daniel Mack987a6c02010-08-02 20:15:17 -0700322 input_abs_get_min(dev, cnt),
323 input_abs_get_max(dev, cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = -EINVAL;
325 break;
326 }
327
Daniel Mack987a6c02010-08-02 20:15:17 -0700328 if (input_abs_get_flat(dev, cnt) >
329 input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 printk(KERN_DEBUG
Daniel Mack987a6c02010-08-02 20:15:17 -0700331 "%s: abs_flat #%02x out of range: %d "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 "(min:%d/max:%d)\n",
Daniel Mack987a6c02010-08-02 20:15:17 -0700333 UINPUT_NAME, cnt,
334 input_abs_get_flat(dev, cnt),
335 input_abs_get_min(dev, cnt),
336 input_abs_get_max(dev, cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 retval = -EINVAL;
338 break;
339 }
340 }
341 return retval;
342}
343
Dmitry Torokhov29506412005-11-20 00:51:22 -0500344static int uinput_allocate_device(struct uinput_device *udev)
345{
346 udev->dev = input_allocate_device();
347 if (!udev->dev)
348 return -ENOMEM;
349
350 udev->dev->event = uinput_dev_event;
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400351 input_set_drvdata(udev->dev, udev);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500352
353 return 0;
354}
355
356static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct uinput_user_dev *user_dev;
359 struct input_dev *dev;
David Herrmann5d9d6e92011-02-11 01:10:44 -0800360 int i;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500361 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dmitry Torokhov29506412005-11-20 00:51:22 -0500363 if (count != sizeof(struct uinput_user_dev))
364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dmitry Torokhov29506412005-11-20 00:51:22 -0500366 if (!udev->dev) {
367 retval = uinput_allocate_device(udev);
368 if (retval)
369 return retval;
370 }
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dev = udev->dev;
373
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800374 user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
Dan Carpenter163d2772011-02-18 08:30:52 -0800375 if (IS_ERR(user_dev))
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800376 return PTR_ERR(user_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Anssi Hannulaff462552006-07-19 01:41:09 -0400378 udev->ff_effects_max = user_dev->ff_effects_max;
379
David Herrmann5d9d6e92011-02-11 01:10:44 -0800380 /* Ensure name is filled in */
381 if (!user_dev->name[0]) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500382 retval = -EINVAL;
383 goto exit;
384 }
385
386 kfree(dev->name);
David Herrmann5d9d6e92011-02-11 01:10:44 -0800387 dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
388 GFP_KERNEL);
389 if (!dev->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 retval = -ENOMEM;
391 goto exit;
392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dev->id.bustype = user_dev->id.bustype;
395 dev->id.vendor = user_dev->id.vendor;
396 dev->id.product = user_dev->id.product;
397 dev->id.version = user_dev->id.version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Daniel Mack987a6c02010-08-02 20:15:17 -0700399 for (i = 0; i < ABS_CNT; i++) {
400 input_abs_set_max(dev, i, user_dev->absmax[i]);
401 input_abs_set_min(dev, i, user_dev->absmin[i]);
402 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
403 input_abs_set_flat(dev, i, user_dev->absflat[i]);
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* check if absmin/absmax/absfuzz/absflat are filled as
407 * told in Documentation/input/input-programming.txt */
408 if (test_bit(EV_ABS, dev->evbit)) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500409 retval = uinput_validate_absbits(dev);
410 if (retval < 0)
411 goto exit;
Henrik Rydberg38e7afe2010-09-19 16:25:36 -0700412 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
413 int nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
Henrik Rydberg8cde8102010-11-27 10:50:54 +0100414 input_mt_init_slots(dev, nslot);
Henrik Rydberg38e7afe2010-09-19 16:25:36 -0700415 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
416 input_set_events_per_packet(dev, 60);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Dmitry Torokhov29506412005-11-20 00:51:22 -0500420 udev->state = UIST_SETUP_COMPLETE;
421 retval = count;
422
423 exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 kfree(user_dev);
425 return retval;
426}
427
Dmitry Torokhov29506412005-11-20 00:51:22 -0500428static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
429{
430 struct input_event ev;
431
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400432 if (count < input_event_size())
Dmitry Torokhov29506412005-11-20 00:51:22 -0500433 return -EINVAL;
434
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400435 if (input_event_from_user(buffer, &ev))
Dmitry Torokhov29506412005-11-20 00:51:22 -0500436 return -EFAULT;
437
438 input_event(udev->dev, ev.type, ev.code, ev.value);
439
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400440 return input_event_size();
Dmitry Torokhov29506412005-11-20 00:51:22 -0500441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
444{
445 struct uinput_device *udev = file->private_data;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500446 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700448 if (count == 0)
449 return 0;
450
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500451 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500452 if (retval)
453 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dmitry Torokhov29506412005-11-20 00:51:22 -0500455 retval = udev->state == UIST_CREATED ?
456 uinput_inject_event(udev, buffer, count) :
457 uinput_setup_device(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500459 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500460
461 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Dmitry Torokhov929d1af2012-07-29 22:48:31 -0700464static bool uinput_fetch_next_event(struct uinput_device *udev,
465 struct input_event *event)
466{
467 bool have_event;
468
469 spin_lock_irq(&udev->dev->event_lock);
470
471 have_event = udev->head != udev->tail;
472 if (have_event) {
473 *event = udev->buff[udev->tail];
474 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
475 }
476
477 spin_unlock_irq(&udev->dev->event_lock);
478
479 return have_event;
480}
481
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700482static ssize_t uinput_events_to_user(struct uinput_device *udev,
483 char __user *buffer, size_t count)
484{
485 struct input_event event;
486 size_t read = 0;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700487
488 while (read + input_event_size() <= count &&
489 uinput_fetch_next_event(udev, &event)) {
490
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700491 if (input_event_to_user(buffer + read, &event))
492 return -EFAULT;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700493
494 read += input_event_size();
495 }
496
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700497 return read;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700498}
499
500static ssize_t uinput_read(struct file *file, char __user *buffer,
501 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct uinput_device *udev = file->private_data;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700504 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Herrmannf40033a2012-07-29 22:48:31 -0700506 if (count != 0 && count < input_event_size())
507 return -EINVAL;
508
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700509 do {
510 retval = mutex_lock_interruptible(&udev->mutex);
511 if (retval)
512 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700514 if (udev->state != UIST_CREATED)
515 retval = -ENODEV;
516 else if (udev->head == udev->tail &&
517 (file->f_flags & O_NONBLOCK))
518 retval = -EAGAIN;
519 else
520 retval = uinput_events_to_user(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700522 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700524 if (retval || count == 0)
525 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700527 if (!(file->f_flags & O_NONBLOCK))
528 retval = wait_event_interruptible(udev->waitq,
529 udev->head != udev->tail ||
530 udev->state != UIST_CREATED);
531 } while (retval == 0);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return retval;
534}
535
536static unsigned int uinput_poll(struct file *file, poll_table *wait)
537{
538 struct uinput_device *udev = file->private_data;
539
540 poll_wait(file, &udev->waitq, wait);
541
542 if (udev->head != udev->tail)
543 return POLLIN | POLLRDNORM;
544
545 return 0;
546}
547
Dmitry Torokhov29506412005-11-20 00:51:22 -0500548static int uinput_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500550 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dmitry Torokhov29506412005-11-20 00:51:22 -0500552 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 kfree(udev);
554
555 return 0;
556}
557
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400558#ifdef CONFIG_COMPAT
559struct uinput_ff_upload_compat {
Dmitry Torokhovc5b35332012-07-29 22:48:32 -0700560 __u32 request_id;
561 __s32 retval;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400562 struct ff_effect_compat effect;
563 struct ff_effect_compat old;
564};
565
566static int uinput_ff_upload_to_user(char __user *buffer,
567 const struct uinput_ff_upload *ff_up)
568{
569 if (INPUT_COMPAT_TEST) {
570 struct uinput_ff_upload_compat ff_up_compat;
571
572 ff_up_compat.request_id = ff_up->request_id;
573 ff_up_compat.retval = ff_up->retval;
574 /*
575 * It so happens that the pointer that gives us the trouble
576 * is the last field in the structure. Since we don't support
577 * custom waveforms in uinput anyway we can just copy the whole
578 * thing (to the compat size) and ignore the pointer.
579 */
580 memcpy(&ff_up_compat.effect, &ff_up->effect,
581 sizeof(struct ff_effect_compat));
582 memcpy(&ff_up_compat.old, &ff_up->old,
583 sizeof(struct ff_effect_compat));
584
585 if (copy_to_user(buffer, &ff_up_compat,
586 sizeof(struct uinput_ff_upload_compat)))
587 return -EFAULT;
588 } else {
589 if (copy_to_user(buffer, ff_up,
590 sizeof(struct uinput_ff_upload)))
591 return -EFAULT;
592 }
593
594 return 0;
595}
596
597static int uinput_ff_upload_from_user(const char __user *buffer,
598 struct uinput_ff_upload *ff_up)
599{
600 if (INPUT_COMPAT_TEST) {
601 struct uinput_ff_upload_compat ff_up_compat;
602
603 if (copy_from_user(&ff_up_compat, buffer,
604 sizeof(struct uinput_ff_upload_compat)))
605 return -EFAULT;
606
607 ff_up->request_id = ff_up_compat.request_id;
608 ff_up->retval = ff_up_compat.retval;
609 memcpy(&ff_up->effect, &ff_up_compat.effect,
610 sizeof(struct ff_effect_compat));
611 memcpy(&ff_up->old, &ff_up_compat.old,
612 sizeof(struct ff_effect_compat));
613
614 } else {
615 if (copy_from_user(ff_up, buffer,
616 sizeof(struct uinput_ff_upload)))
617 return -EFAULT;
618 }
619
620 return 0;
621}
622
623#else
624
625static int uinput_ff_upload_to_user(char __user *buffer,
626 const struct uinput_ff_upload *ff_up)
627{
628 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
629 return -EFAULT;
630
631 return 0;
632}
633
634static int uinput_ff_upload_from_user(const char __user *buffer,
635 struct uinput_ff_upload *ff_up)
636{
637 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
638 return -EFAULT;
639
640 return 0;
641}
642
643#endif
644
Dmitry Torokhov29506412005-11-20 00:51:22 -0500645#define uinput_set_bit(_arg, _bit, _max) \
646({ \
647 int __ret = 0; \
648 if (udev->state == UIST_CREATED) \
649 __ret = -EINVAL; \
650 else if ((_arg) > (_max)) \
651 __ret = -EINVAL; \
652 else set_bit((_arg), udev->dev->_bit); \
653 __ret; \
654})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400656static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
657 unsigned long arg, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500659 int retval;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400660 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct uinput_ff_upload ff_up;
662 struct uinput_ff_erase ff_erase;
663 struct uinput_request *req;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500664 char *phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500666 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500667 if (retval)
668 return retval;
669
670 if (!udev->dev) {
671 retval = uinput_allocate_device(udev);
672 if (retval)
673 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 switch (cmd) {
677 case UI_DEV_CREATE:
678 retval = uinput_create_device(udev);
679 break;
680
681 case UI_DEV_DESTROY:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500682 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684
685 case UI_SET_EVBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500686 retval = uinput_set_bit(arg, evbit, EV_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
688
689 case UI_SET_KEYBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500690 retval = uinput_set_bit(arg, keybit, KEY_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
692
693 case UI_SET_RELBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500694 retval = uinput_set_bit(arg, relbit, REL_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696
697 case UI_SET_ABSBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500698 retval = uinput_set_bit(arg, absbit, ABS_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 break;
700
701 case UI_SET_MSCBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500702 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704
705 case UI_SET_LEDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500706 retval = uinput_set_bit(arg, ledbit, LED_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708
709 case UI_SET_SNDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500710 retval = uinput_set_bit(arg, sndbit, SND_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712
713 case UI_SET_FFBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500714 retval = uinput_set_bit(arg, ffbit, FF_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 break;
716
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500717 case UI_SET_SWBIT:
718 retval = uinput_set_bit(arg, swbit, SW_MAX);
719 break;
720
Henrik Rydberg85b77202010-12-18 20:51:13 +0100721 case UI_SET_PROPBIT:
722 retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
723 break;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case UI_SET_PHYS:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500726 if (udev->state == UIST_CREATED) {
727 retval = -EINVAL;
728 goto out;
729 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800730
731 phys = strndup_user(p, 1024);
732 if (IS_ERR(phys)) {
733 retval = PTR_ERR(phys);
734 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800736
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500737 kfree(udev->dev->phys);
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800738 udev->dev->phys = phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740
741 case UI_BEGIN_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400742 retval = uinput_ff_upload_from_user(p, &ff_up);
743 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 req = uinput_request_find(udev, ff_up.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400747 if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 retval = -EINVAL;
749 break;
750 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ff_up.retval = 0;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400753 ff_up.effect = *req->u.upload.effect;
Anssi Hannulaff462552006-07-19 01:41:09 -0400754 if (req->u.upload.old)
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400755 ff_up.old = *req->u.upload.old;
Anssi Hannulaff462552006-07-19 01:41:09 -0400756 else
757 memset(&ff_up.old, 0, sizeof(struct ff_effect));
758
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400759 retval = uinput_ff_upload_to_user(p, &ff_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761
762 case UI_BEGIN_FF_ERASE:
763 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
764 retval = -EFAULT;
765 break;
766 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400769 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 retval = -EINVAL;
771 break;
772 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 ff_erase.retval = 0;
775 ff_erase.effect_id = req->u.effect_id;
776 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
777 retval = -EFAULT;
778 break;
779 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 break;
782
783 case UI_END_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400784 retval = uinput_ff_upload_from_user(p, &ff_up);
785 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 break;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 req = uinput_request_find(udev, ff_up.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400789 if (!req || req->code != UI_FF_UPLOAD ||
790 !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 retval = -EINVAL;
792 break;
793 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 req->retval = ff_up.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500796 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
798
799 case UI_END_FF_ERASE:
800 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
801 retval = -EFAULT;
802 break;
803 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400806 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 retval = -EINVAL;
808 break;
809 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 req->retval = ff_erase.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500812 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
814
815 default:
816 retval = -EINVAL;
817 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500818
819 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500820 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return retval;
822}
823
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400824static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
825{
826 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
827}
828
829#ifdef CONFIG_COMPAT
830static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
831{
832 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
833}
834#endif
835
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800836static const struct file_operations uinput_fops = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500837 .owner = THIS_MODULE,
838 .open = uinput_open,
839 .release = uinput_release,
840 .read = uinput_read,
841 .write = uinput_write,
842 .poll = uinput_poll,
843 .unlocked_ioctl = uinput_ioctl,
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400844#ifdef CONFIG_COMPAT
845 .compat_ioctl = uinput_compat_ioctl,
846#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200847 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
850static struct miscdevice uinput_misc = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500851 .fops = &uinput_fops,
852 .minor = UINPUT_MINOR,
853 .name = UINPUT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854};
Kay Sievers8905aaa2010-08-19 09:52:28 -0700855MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
856MODULE_ALIAS("devname:" UINPUT_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858static int __init uinput_init(void)
859{
860 return misc_register(&uinput_misc);
861}
862
863static void __exit uinput_exit(void)
864{
865 misc_deregister(&uinput_misc);
866}
867
868MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
869MODULE_DESCRIPTION("User level driver support for input subsystem");
870MODULE_LICENSE("GPL");
Anssi Hannulaff462552006-07-19 01:41:09 -0400871MODULE_VERSION("0.3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873module_init(uinput_init);
874module_exit(uinput_exit);
875