blob: 6099365102db55f32a1fbb5fbecea5fc1d702d8e [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -070080static struct uinput_request *uinput_request_find(struct uinput_device *udev, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
83 if (id >= UINPUT_NUM_REQUESTS || id < 0)
84 return NULL;
Philip Langdale2d56f3a2008-10-16 22:31:42 -040085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return udev->requests[id];
87}
88
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070089static int uinput_request_reserve_slot(struct uinput_device *udev,
90 struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050092 /* Allocate slot. If none are available right away, wait. */
93 return wait_event_interruptible(udev->requests_waitq,
Dmitry Torokhov00ce7562012-07-29 22:48:32 -070094 uinput_request_alloc_id(udev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Dmitry Torokhov0048e602005-06-30 00:48:14 -050097static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050099 /* Mark slot as available */
100 udev->requests[request->id] = NULL;
Dmitry Torokhove597f0c82005-11-20 00:51:43 -0500101 wake_up(&udev->requests_waitq);
Dmitry Torokhove7507ed2005-10-17 16:43:32 -0700102
103 complete(&request->done);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500104}
105
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700106static int uinput_request_send(struct uinput_device *udev,
107 struct uinput_request *request)
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500108{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700109 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700111 retval = mutex_lock_interruptible(&udev->mutex);
112 if (retval)
113 return retval;
114
115 if (udev->state != UIST_CREATED) {
116 retval = -ENODEV;
117 goto out;
118 }
119
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700120 init_completion(&request->done);
121
122 /*
123 * Tell our userspace application about this new request
124 * by queueing an input event.
125 */
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700126 uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
127
128 out:
129 mutex_unlock(&udev->mutex);
130 return retval;
131}
132
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700133static int uinput_request_submit(struct uinput_device *udev,
134 struct uinput_request *request)
135{
136 int error;
137
138 error = uinput_request_reserve_slot(udev, request);
139 if (error)
140 return error;
141
142 error = uinput_request_send(udev, request);
143 if (error) {
144 uinput_request_done(udev, request);
145 return error;
146 }
147
148 wait_for_completion(&request->done);
149 return request->retval;
150}
151
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700152/*
153 * Fail all ouitstanding requests so handlers don't wait for the userspace
154 * to finish processing them.
155 */
156static void uinput_flush_requests(struct uinput_device *udev)
157{
158 struct uinput_request *request;
159 int i;
160
161 spin_lock(&udev->requests_lock);
162
163 for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
164 request = udev->requests[i];
165 if (request) {
166 request->retval = -ENODEV;
167 uinput_request_done(udev, request);
168 }
169 }
170
171 spin_unlock(&udev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Anssi Hannulaff462552006-07-19 01:41:09 -0400174static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
175{
176 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
177}
178
179static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
180{
181 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
182}
183
184static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
185{
186 return uinput_dev_event(dev, EV_FF, effect_id, value);
187}
188
189static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700191 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct uinput_request request;
193
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400194 /*
195 * uinput driver does not currently support periodic effects with
196 * custom waveform since it does not have a way to pass buffer of
197 * samples (custom_data) to userspace. If ever there is a device
198 * supporting custom waveforms we would need to define an additional
199 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
200 */
201 if (effect->type == FF_PERIODIC &&
202 effect->u.periodic.waveform == FF_CUSTOM)
203 return -EINVAL;
204
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500205 request.code = UI_FF_UPLOAD;
Anssi Hannulaff462552006-07-19 01:41:09 -0400206 request.u.upload.effect = effect;
207 request.u.upload.old = old;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500208
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700209 return uinput_request_submit(udev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
213{
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700214 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct uinput_request request;
216
217 if (!test_bit(EV_FF, dev->evbit))
218 return -ENOSYS;
219
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500220 request.code = UI_FF_ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 request.u.effect_id = effect_id;
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
Dmitry Torokhov29506412005-11-20 00:51:22 -0500226static void uinput_destroy_device(struct uinput_device *udev)
227{
228 const char *name, *phys;
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700229 struct input_dev *dev = udev->dev;
230 enum uinput_state old_state = udev->state;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500231
Aristeu Sergio Rozanski Filho05cebd32009-05-14 22:01:57 -0700232 udev->state = UIST_NEW_DEVICE;
233
234 if (dev) {
235 name = dev->name;
236 phys = dev->phys;
237 if (old_state == UIST_CREATED) {
238 uinput_flush_requests(udev);
239 input_unregister_device(dev);
240 } else {
241 input_free_device(dev);
242 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500243 kfree(name);
244 kfree(phys);
245 udev->dev = NULL;
246 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int uinput_create_device(struct uinput_device *udev)
250{
Anssi Hannulaff462552006-07-19 01:41:09 -0400251 struct input_dev *dev = udev->dev;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500252 int error;
253
254 if (udev->state != UIST_SETUP_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
256 return -EINVAL;
257 }
258
Anssi Hannulaff462552006-07-19 01:41:09 -0400259 if (udev->ff_effects_max) {
260 error = input_ff_create(dev, udev->ff_effects_max);
261 if (error)
262 goto fail1;
263
264 dev->ff->upload = uinput_dev_upload_effect;
265 dev->ff->erase = uinput_dev_erase_effect;
266 dev->ff->playback = uinput_dev_playback;
267 dev->ff->set_gain = uinput_dev_set_gain;
268 dev->ff->set_autocenter = uinput_dev_set_autocenter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Anssi Hannulaff462552006-07-19 01:41:09 -0400271 error = input_register_device(udev->dev);
272 if (error)
273 goto fail2;
274
Dmitry Torokhov29506412005-11-20 00:51:22 -0500275 udev->state = UIST_CREATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400278
279 fail2: input_ff_destroy(dev);
280 fail1: uinput_destroy_device(udev);
281 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static int uinput_open(struct inode *inode, struct file *file)
285{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500286 struct uinput_device *newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dmitry Torokhov29506412005-11-20 00:51:22 -0500288 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!newdev)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500290 return -ENOMEM;
291
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500292 mutex_init(&newdev->mutex);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500293 spin_lock_init(&newdev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 init_waitqueue_head(&newdev->requests_waitq);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500295 init_waitqueue_head(&newdev->waitq);
296 newdev->state = UIST_NEW_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 file->private_data = newdev;
Dmitry Torokhovdaf8a962010-02-04 00:30:39 -0800299 nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static int uinput_validate_absbits(struct input_dev *dev)
305{
306 unsigned int cnt;
307 int retval = 0;
308
Daniel Mack81c2a3b2010-05-20 22:52:58 -0700309 for (cnt = 0; cnt < ABS_CNT; cnt++) {
Peter Hutterera718d792011-03-30 22:25:34 -0700310 int min, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!test_bit(cnt, dev->absbit))
312 continue;
313
Peter Hutterera718d792011-03-30 22:25:34 -0700314 min = input_abs_get_min(dev, cnt);
315 max = input_abs_get_max(dev, cnt);
316
317 if ((min != 0 || max != 0) && max <= min) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 printk(KERN_DEBUG
319 "%s: invalid abs[%02x] min:%d max:%d\n",
320 UINPUT_NAME, cnt,
Daniel Mack987a6c02010-08-02 20:15:17 -0700321 input_abs_get_min(dev, cnt),
322 input_abs_get_max(dev, cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 retval = -EINVAL;
324 break;
325 }
326
Daniel Mack987a6c02010-08-02 20:15:17 -0700327 if (input_abs_get_flat(dev, cnt) >
328 input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 printk(KERN_DEBUG
Daniel Mack987a6c02010-08-02 20:15:17 -0700330 "%s: abs_flat #%02x out of range: %d "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 "(min:%d/max:%d)\n",
Daniel Mack987a6c02010-08-02 20:15:17 -0700332 UINPUT_NAME, cnt,
333 input_abs_get_flat(dev, cnt),
334 input_abs_get_min(dev, cnt),
335 input_abs_get_max(dev, cnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 retval = -EINVAL;
337 break;
338 }
339 }
340 return retval;
341}
342
Dmitry Torokhov29506412005-11-20 00:51:22 -0500343static int uinput_allocate_device(struct uinput_device *udev)
344{
345 udev->dev = input_allocate_device();
346 if (!udev->dev)
347 return -ENOMEM;
348
349 udev->dev->event = uinput_dev_event;
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400350 input_set_drvdata(udev->dev, udev);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500351
352 return 0;
353}
354
355static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct uinput_user_dev *user_dev;
358 struct input_dev *dev;
David Herrmann5d9d6e92011-02-11 01:10:44 -0800359 int i;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500360 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dmitry Torokhov29506412005-11-20 00:51:22 -0500362 if (count != sizeof(struct uinput_user_dev))
363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Dmitry Torokhov29506412005-11-20 00:51:22 -0500365 if (!udev->dev) {
366 retval = uinput_allocate_device(udev);
367 if (retval)
368 return retval;
369 }
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dev = udev->dev;
372
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800373 user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
Dan Carpenter163d2772011-02-18 08:30:52 -0800374 if (IS_ERR(user_dev))
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800375 return PTR_ERR(user_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Anssi Hannulaff462552006-07-19 01:41:09 -0400377 udev->ff_effects_max = user_dev->ff_effects_max;
378
David Herrmann5d9d6e92011-02-11 01:10:44 -0800379 /* Ensure name is filled in */
380 if (!user_dev->name[0]) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500381 retval = -EINVAL;
382 goto exit;
383 }
384
385 kfree(dev->name);
David Herrmann5d9d6e92011-02-11 01:10:44 -0800386 dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
387 GFP_KERNEL);
388 if (!dev->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 retval = -ENOMEM;
390 goto exit;
391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dev->id.bustype = user_dev->id.bustype;
394 dev->id.vendor = user_dev->id.vendor;
395 dev->id.product = user_dev->id.product;
396 dev->id.version = user_dev->id.version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Daniel Mack987a6c02010-08-02 20:15:17 -0700398 for (i = 0; i < ABS_CNT; i++) {
399 input_abs_set_max(dev, i, user_dev->absmax[i]);
400 input_abs_set_min(dev, i, user_dev->absmin[i]);
401 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
402 input_abs_set_flat(dev, i, user_dev->absflat[i]);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* check if absmin/absmax/absfuzz/absflat are filled as
406 * told in Documentation/input/input-programming.txt */
407 if (test_bit(EV_ABS, dev->evbit)) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500408 retval = uinput_validate_absbits(dev);
409 if (retval < 0)
410 goto exit;
Henrik Rydberg38e7afe2010-09-19 16:25:36 -0700411 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
412 int nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
Henrik Rydberg8cde8102010-11-27 10:50:54 +0100413 input_mt_init_slots(dev, nslot);
Henrik Rydberg38e7afe2010-09-19 16:25:36 -0700414 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
415 input_set_events_per_packet(dev, 60);
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Dmitry Torokhov29506412005-11-20 00:51:22 -0500419 udev->state = UIST_SETUP_COMPLETE;
420 retval = count;
421
422 exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 kfree(user_dev);
424 return retval;
425}
426
Dmitry Torokhov29506412005-11-20 00:51:22 -0500427static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
428{
429 struct input_event ev;
430
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400431 if (count < input_event_size())
Dmitry Torokhov29506412005-11-20 00:51:22 -0500432 return -EINVAL;
433
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400434 if (input_event_from_user(buffer, &ev))
Dmitry Torokhov29506412005-11-20 00:51:22 -0500435 return -EFAULT;
436
437 input_event(udev->dev, ev.type, ev.code, ev.value);
438
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400439 return input_event_size();
Dmitry Torokhov29506412005-11-20 00:51:22 -0500440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
443{
444 struct uinput_device *udev = file->private_data;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500445 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700447 if (count == 0)
448 return 0;
449
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500450 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500451 if (retval)
452 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dmitry Torokhov29506412005-11-20 00:51:22 -0500454 retval = udev->state == UIST_CREATED ?
455 uinput_inject_event(udev, buffer, count) :
456 uinput_setup_device(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500458 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500459
460 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Dmitry Torokhov929d1af2012-07-29 22:48:31 -0700463static bool uinput_fetch_next_event(struct uinput_device *udev,
464 struct input_event *event)
465{
466 bool have_event;
467
468 spin_lock_irq(&udev->dev->event_lock);
469
470 have_event = udev->head != udev->tail;
471 if (have_event) {
472 *event = udev->buff[udev->tail];
473 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
474 }
475
476 spin_unlock_irq(&udev->dev->event_lock);
477
478 return have_event;
479}
480
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700481static ssize_t uinput_events_to_user(struct uinput_device *udev,
482 char __user *buffer, size_t count)
483{
484 struct input_event event;
485 size_t read = 0;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700486
487 while (read + input_event_size() <= count &&
488 uinput_fetch_next_event(udev, &event)) {
489
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700490 if (input_event_to_user(buffer + read, &event))
491 return -EFAULT;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700492
493 read += input_event_size();
494 }
495
Dmitry Torokhov00ce7562012-07-29 22:48:32 -0700496 return read;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700497}
498
499static ssize_t uinput_read(struct file *file, char __user *buffer,
500 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct uinput_device *udev = file->private_data;
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700503 ssize_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
David Herrmannf40033a2012-07-29 22:48:31 -0700505 if (count != 0 && count < input_event_size())
506 return -EINVAL;
507
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700508 do {
509 retval = mutex_lock_interruptible(&udev->mutex);
510 if (retval)
511 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700513 if (udev->state != UIST_CREATED)
514 retval = -ENODEV;
515 else if (udev->head == udev->tail &&
516 (file->f_flags & O_NONBLOCK))
517 retval = -EAGAIN;
518 else
519 retval = uinput_events_to_user(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700521 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700523 if (retval || count == 0)
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Dmitry Torokhov22ae19c2012-07-29 22:48:31 -0700526 if (!(file->f_flags & O_NONBLOCK))
527 retval = wait_event_interruptible(udev->waitq,
528 udev->head != udev->tail ||
529 udev->state != UIST_CREATED);
530 } while (retval == 0);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return retval;
533}
534
535static unsigned int uinput_poll(struct file *file, poll_table *wait)
536{
537 struct uinput_device *udev = file->private_data;
538
539 poll_wait(file, &udev->waitq, wait);
540
541 if (udev->head != udev->tail)
542 return POLLIN | POLLRDNORM;
543
544 return 0;
545}
546
Dmitry Torokhov29506412005-11-20 00:51:22 -0500547static int uinput_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500549 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Dmitry Torokhov29506412005-11-20 00:51:22 -0500551 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 kfree(udev);
553
554 return 0;
555}
556
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400557#ifdef CONFIG_COMPAT
558struct uinput_ff_upload_compat {
559 int request_id;
560 int retval;
561 struct ff_effect_compat effect;
562 struct ff_effect_compat old;
563};
564
565static int uinput_ff_upload_to_user(char __user *buffer,
566 const struct uinput_ff_upload *ff_up)
567{
568 if (INPUT_COMPAT_TEST) {
569 struct uinput_ff_upload_compat ff_up_compat;
570
571 ff_up_compat.request_id = ff_up->request_id;
572 ff_up_compat.retval = ff_up->retval;
573 /*
574 * It so happens that the pointer that gives us the trouble
575 * is the last field in the structure. Since we don't support
576 * custom waveforms in uinput anyway we can just copy the whole
577 * thing (to the compat size) and ignore the pointer.
578 */
579 memcpy(&ff_up_compat.effect, &ff_up->effect,
580 sizeof(struct ff_effect_compat));
581 memcpy(&ff_up_compat.old, &ff_up->old,
582 sizeof(struct ff_effect_compat));
583
584 if (copy_to_user(buffer, &ff_up_compat,
585 sizeof(struct uinput_ff_upload_compat)))
586 return -EFAULT;
587 } else {
588 if (copy_to_user(buffer, ff_up,
589 sizeof(struct uinput_ff_upload)))
590 return -EFAULT;
591 }
592
593 return 0;
594}
595
596static int uinput_ff_upload_from_user(const char __user *buffer,
597 struct uinput_ff_upload *ff_up)
598{
599 if (INPUT_COMPAT_TEST) {
600 struct uinput_ff_upload_compat ff_up_compat;
601
602 if (copy_from_user(&ff_up_compat, buffer,
603 sizeof(struct uinput_ff_upload_compat)))
604 return -EFAULT;
605
606 ff_up->request_id = ff_up_compat.request_id;
607 ff_up->retval = ff_up_compat.retval;
608 memcpy(&ff_up->effect, &ff_up_compat.effect,
609 sizeof(struct ff_effect_compat));
610 memcpy(&ff_up->old, &ff_up_compat.old,
611 sizeof(struct ff_effect_compat));
612
613 } else {
614 if (copy_from_user(ff_up, buffer,
615 sizeof(struct uinput_ff_upload)))
616 return -EFAULT;
617 }
618
619 return 0;
620}
621
622#else
623
624static int uinput_ff_upload_to_user(char __user *buffer,
625 const struct uinput_ff_upload *ff_up)
626{
627 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
628 return -EFAULT;
629
630 return 0;
631}
632
633static int uinput_ff_upload_from_user(const char __user *buffer,
634 struct uinput_ff_upload *ff_up)
635{
636 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
637 return -EFAULT;
638
639 return 0;
640}
641
642#endif
643
Dmitry Torokhov29506412005-11-20 00:51:22 -0500644#define uinput_set_bit(_arg, _bit, _max) \
645({ \
646 int __ret = 0; \
647 if (udev->state == UIST_CREATED) \
648 __ret = -EINVAL; \
649 else if ((_arg) > (_max)) \
650 __ret = -EINVAL; \
651 else set_bit((_arg), udev->dev->_bit); \
652 __ret; \
653})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400655static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
656 unsigned long arg, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500658 int retval;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400659 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct uinput_ff_upload ff_up;
661 struct uinput_ff_erase ff_erase;
662 struct uinput_request *req;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500663 char *phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500665 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500666 if (retval)
667 return retval;
668
669 if (!udev->dev) {
670 retval = uinput_allocate_device(udev);
671 if (retval)
672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 switch (cmd) {
676 case UI_DEV_CREATE:
677 retval = uinput_create_device(udev);
678 break;
679
680 case UI_DEV_DESTROY:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500681 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
683
684 case UI_SET_EVBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500685 retval = uinput_set_bit(arg, evbit, EV_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687
688 case UI_SET_KEYBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500689 retval = uinput_set_bit(arg, keybit, KEY_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
691
692 case UI_SET_RELBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500693 retval = uinput_set_bit(arg, relbit, REL_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
695
696 case UI_SET_ABSBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500697 retval = uinput_set_bit(arg, absbit, ABS_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
699
700 case UI_SET_MSCBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500701 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703
704 case UI_SET_LEDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500705 retval = uinput_set_bit(arg, ledbit, LED_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
707
708 case UI_SET_SNDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500709 retval = uinput_set_bit(arg, sndbit, SND_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
711
712 case UI_SET_FFBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500713 retval = uinput_set_bit(arg, ffbit, FF_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500716 case UI_SET_SWBIT:
717 retval = uinput_set_bit(arg, swbit, SW_MAX);
718 break;
719
Henrik Rydberg85b77202010-12-18 20:51:13 +0100720 case UI_SET_PROPBIT:
721 retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
722 break;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 case UI_SET_PHYS:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500725 if (udev->state == UIST_CREATED) {
726 retval = -EINVAL;
727 goto out;
728 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800729
730 phys = strndup_user(p, 1024);
731 if (IS_ERR(phys)) {
732 retval = PTR_ERR(phys);
733 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800735
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500736 kfree(udev->dev->phys);
Dmitry Torokhov4dfcc272011-02-11 01:10:45 -0800737 udev->dev->phys = phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739
740 case UI_BEGIN_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400741 retval = uinput_ff_upload_from_user(p, &ff_up);
742 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 req = uinput_request_find(udev, ff_up.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400746 if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 retval = -EINVAL;
748 break;
749 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ff_up.retval = 0;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400752 ff_up.effect = *req->u.upload.effect;
Anssi Hannulaff462552006-07-19 01:41:09 -0400753 if (req->u.upload.old)
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400754 ff_up.old = *req->u.upload.old;
Anssi Hannulaff462552006-07-19 01:41:09 -0400755 else
756 memset(&ff_up.old, 0, sizeof(struct ff_effect));
757
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400758 retval = uinput_ff_upload_to_user(p, &ff_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760
761 case UI_BEGIN_FF_ERASE:
762 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
763 retval = -EFAULT;
764 break;
765 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400768 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 retval = -EINVAL;
770 break;
771 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ff_erase.retval = 0;
774 ff_erase.effect_id = req->u.effect_id;
775 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
776 retval = -EFAULT;
777 break;
778 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781
782 case UI_END_FF_UPLOAD:
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400783 retval = uinput_ff_upload_from_user(p, &ff_up);
784 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 req = uinput_request_find(udev, ff_up.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400788 if (!req || req->code != UI_FF_UPLOAD ||
789 !req->u.upload.effect) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 retval = -EINVAL;
791 break;
792 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 req->retval = ff_up.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500795 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797
798 case UI_END_FF_ERASE:
799 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
800 retval = -EFAULT;
801 break;
802 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 req = uinput_request_find(udev, ff_erase.request_id);
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400805 if (!req || req->code != UI_FF_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 retval = -EINVAL;
807 break;
808 }
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 req->retval = ff_erase.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500811 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813
814 default:
815 retval = -EINVAL;
816 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500817
818 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500819 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return retval;
821}
822
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400823static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
824{
825 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
826}
827
828#ifdef CONFIG_COMPAT
829static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
830{
831 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
832}
833#endif
834
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800835static const struct file_operations uinput_fops = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500836 .owner = THIS_MODULE,
837 .open = uinput_open,
838 .release = uinput_release,
839 .read = uinput_read,
840 .write = uinput_write,
841 .poll = uinput_poll,
842 .unlocked_ioctl = uinput_ioctl,
Philip Langdale2d56f3a2008-10-16 22:31:42 -0400843#ifdef CONFIG_COMPAT
844 .compat_ioctl = uinput_compat_ioctl,
845#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200846 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847};
848
849static struct miscdevice uinput_misc = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500850 .fops = &uinput_fops,
851 .minor = UINPUT_MINOR,
852 .name = UINPUT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853};
Kay Sievers8905aaa2010-08-19 09:52:28 -0700854MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
855MODULE_ALIAS("devname:" UINPUT_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857static int __init uinput_init(void)
858{
859 return misc_register(&uinput_misc);
860}
861
862static void __exit uinput_exit(void)
863{
864 misc_deregister(&uinput_misc);
865}
866
867MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
868MODULE_DESCRIPTION("User level driver support for input subsystem");
869MODULE_LICENSE("GPL");
Anssi Hannulaff462552006-07-19 01:41:09 -0400870MODULE_VERSION("0.3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872module_init(uinput_init);
873module_exit(uinput_exit);
874