blob: 2bcfa0b3506145951f9fb53d85cf0bcaea739e94 [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>
33#include <linux/slab.h>
34#include <linux/module.h>
35#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/smp_lock.h>
37#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <linux/uinput.h>
Arnd Bergmann87029652008-05-20 19:16:53 +020040#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
43{
Dmitry Torokhov373f9712007-04-12 01:34:33 -040044 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 udev->buff[udev->head].type = type;
47 udev->buff[udev->head].code = code;
48 udev->buff[udev->head].value = value;
49 do_gettimeofday(&udev->buff[udev->head].time);
50 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
51
52 wake_up_interruptible(&udev->waitq);
53
54 return 0;
55}
56
Dmitry Torokhov0048e602005-06-30 00:48:14 -050057static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 /* Atomically allocate an ID for the given request. Returns 0 on success. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int id;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050061 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dmitry Torokhov0048e602005-06-30 00:48:14 -050063 spin_lock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050064
65 for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (!udev->requests[id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 request->id = id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -050068 udev->requests[id] = request;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050069 err = 0;
70 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050072
Dmitry Torokhov0048e602005-06-30 00:48:14 -050073 spin_unlock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050074 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
77static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
78{
79 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
80 if (id >= UINPUT_NUM_REQUESTS || id < 0)
81 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return udev->requests[id];
83}
84
Dmitry Torokhov0048e602005-06-30 00:48:14 -050085static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050087 /* Allocate slot. If none are available right away, wait. */
88 return wait_event_interruptible(udev->requests_waitq,
89 !uinput_request_alloc_id(udev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Dmitry Torokhov0048e602005-06-30 00:48:14 -050092static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050094 /* Mark slot as available */
95 udev->requests[request->id] = NULL;
Dmitry Torokhove597f0c82005-11-20 00:51:43 -050096 wake_up(&udev->requests_waitq);
Dmitry Torokhove7507ed2005-10-17 16:43:32 -070097
98 complete(&request->done);
Dmitry Torokhov0048e602005-06-30 00:48:14 -050099}
100
101static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* Tell our userspace app about this new request by queueing an input event */
104 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
105
106 /* Wait for the request to complete */
Dmitry Torokhove597f0c82005-11-20 00:51:43 -0500107 wait_for_completion(&request->done);
108 return request->retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Anssi Hannulaff462552006-07-19 01:41:09 -0400111static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
112{
113 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
114}
115
116static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
117{
118 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
119}
120
121static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
122{
123 return uinput_dev_event(dev, EV_FF, effect_id, value);
124}
125
126static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct uinput_request request;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500129 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500131 request.id = -1;
132 init_completion(&request.done);
133 request.code = UI_FF_UPLOAD;
Anssi Hannulaff462552006-07-19 01:41:09 -0400134 request.u.upload.effect = effect;
135 request.u.upload.old = old;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500136
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400137 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500138 if (!retval)
139 retval = uinput_request_submit(dev, &request);
140
141 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
145{
146 struct uinput_request request;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500147 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!test_bit(EV_FF, dev->evbit))
150 return -ENOSYS;
151
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500152 request.id = -1;
153 init_completion(&request.done);
154 request.code = UI_FF_ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 request.u.effect_id = effect_id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500156
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400157 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500158 if (!retval)
159 retval = uinput_request_submit(dev, &request);
160
161 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Dmitry Torokhov29506412005-11-20 00:51:22 -0500164static void uinput_destroy_device(struct uinput_device *udev)
165{
166 const char *name, *phys;
167
168 if (udev->dev) {
169 name = udev->dev->name;
170 phys = udev->dev->phys;
171 if (udev->state == UIST_CREATED)
172 input_unregister_device(udev->dev);
173 else
174 input_free_device(udev->dev);
175 kfree(name);
176 kfree(phys);
177 udev->dev = NULL;
178 }
179
180 udev->state = UIST_NEW_DEVICE;
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int uinput_create_device(struct uinput_device *udev)
184{
Anssi Hannulaff462552006-07-19 01:41:09 -0400185 struct input_dev *dev = udev->dev;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500186 int error;
187
188 if (udev->state != UIST_SETUP_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
190 return -EINVAL;
191 }
192
Anssi Hannulaff462552006-07-19 01:41:09 -0400193 if (udev->ff_effects_max) {
194 error = input_ff_create(dev, udev->ff_effects_max);
195 if (error)
196 goto fail1;
197
198 dev->ff->upload = uinput_dev_upload_effect;
199 dev->ff->erase = uinput_dev_erase_effect;
200 dev->ff->playback = uinput_dev_playback;
201 dev->ff->set_gain = uinput_dev_set_gain;
202 dev->ff->set_autocenter = uinput_dev_set_autocenter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
Anssi Hannulaff462552006-07-19 01:41:09 -0400205 error = input_register_device(udev->dev);
206 if (error)
207 goto fail2;
208
Dmitry Torokhov29506412005-11-20 00:51:22 -0500209 udev->state = UIST_CREATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400212
213 fail2: input_ff_destroy(dev);
214 fail1: uinput_destroy_device(udev);
215 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static int uinput_open(struct inode *inode, struct file *file)
219{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500220 struct uinput_device *newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dmitry Torokhov29506412005-11-20 00:51:22 -0500222 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!newdev)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500224 return -ENOMEM;
225
Arnd Bergmann87029652008-05-20 19:16:53 +0200226 lock_kernel();
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500227 mutex_init(&newdev->mutex);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500228 spin_lock_init(&newdev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 init_waitqueue_head(&newdev->requests_waitq);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500230 init_waitqueue_head(&newdev->waitq);
231 newdev->state = UIST_NEW_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 file->private_data = newdev;
Arnd Bergmann87029652008-05-20 19:16:53 +0200234 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static int uinput_validate_absbits(struct input_dev *dev)
240{
241 unsigned int cnt;
242 int retval = 0;
243
244 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
245 if (!test_bit(cnt, dev->absbit))
246 continue;
247
248 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
249 printk(KERN_DEBUG
250 "%s: invalid abs[%02x] min:%d max:%d\n",
251 UINPUT_NAME, cnt,
252 dev->absmin[cnt], dev->absmax[cnt]);
253 retval = -EINVAL;
254 break;
255 }
256
257 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
258 printk(KERN_DEBUG
259 "%s: absflat[%02x] out of range: %d "
260 "(min:%d/max:%d)\n",
261 UINPUT_NAME, cnt, dev->absflat[cnt],
262 dev->absmin[cnt], dev->absmax[cnt]);
263 retval = -EINVAL;
264 break;
265 }
266 }
267 return retval;
268}
269
Dmitry Torokhov29506412005-11-20 00:51:22 -0500270static int uinput_allocate_device(struct uinput_device *udev)
271{
272 udev->dev = input_allocate_device();
273 if (!udev->dev)
274 return -ENOMEM;
275
276 udev->dev->event = uinput_dev_event;
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400277 input_set_drvdata(udev->dev, udev);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500278
279 return 0;
280}
281
282static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 struct uinput_user_dev *user_dev;
285 struct input_dev *dev;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500286 char *name;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500287 int size;
288 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dmitry Torokhov29506412005-11-20 00:51:22 -0500290 if (count != sizeof(struct uinput_user_dev))
291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Dmitry Torokhov29506412005-11-20 00:51:22 -0500293 if (!udev->dev) {
294 retval = uinput_allocate_device(udev);
295 if (retval)
296 return retval;
297 }
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 dev = udev->dev;
300
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500301 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500302 if (!user_dev)
303 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
306 retval = -EFAULT;
307 goto exit;
308 }
309
Anssi Hannulaff462552006-07-19 01:41:09 -0400310 udev->ff_effects_max = user_dev->ff_effects_max;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500313 if (!size) {
314 retval = -EINVAL;
315 goto exit;
316 }
317
318 kfree(dev->name);
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500319 dev->name = name = kmalloc(size, GFP_KERNEL);
320 if (!name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 retval = -ENOMEM;
322 goto exit;
323 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500324 strlcpy(name, user_dev->name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 dev->id.bustype = user_dev->id.bustype;
327 dev->id.vendor = user_dev->id.vendor;
328 dev->id.product = user_dev->id.product;
329 dev->id.version = user_dev->id.version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 size = sizeof(int) * (ABS_MAX + 1);
332 memcpy(dev->absmax, user_dev->absmax, size);
333 memcpy(dev->absmin, user_dev->absmin, size);
334 memcpy(dev->absfuzz, user_dev->absfuzz, size);
335 memcpy(dev->absflat, user_dev->absflat, size);
336
337 /* check if absmin/absmax/absfuzz/absflat are filled as
338 * told in Documentation/input/input-programming.txt */
339 if (test_bit(EV_ABS, dev->evbit)) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500340 retval = uinput_validate_absbits(dev);
341 if (retval < 0)
342 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Dmitry Torokhov29506412005-11-20 00:51:22 -0500345 udev->state = UIST_SETUP_COMPLETE;
346 retval = count;
347
348 exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(user_dev);
350 return retval;
351}
352
Dmitry Torokhov29506412005-11-20 00:51:22 -0500353static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
354{
355 struct input_event ev;
356
357 if (count != sizeof(struct input_event))
358 return -EINVAL;
359
360 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
361 return -EFAULT;
362
363 input_event(udev->dev, ev.type, ev.code, ev.value);
364
365 return sizeof(struct input_event);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
369{
370 struct uinput_device *udev = file->private_data;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500371 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500373 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500374 if (retval)
375 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dmitry Torokhov29506412005-11-20 00:51:22 -0500377 retval = udev->state == UIST_CREATED ?
378 uinput_inject_event(udev, buffer, count) :
379 uinput_setup_device(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500381 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500382
383 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
387{
388 struct uinput_device *udev = file->private_data;
389 int retval = 0;
390
Dmitry Torokhov29506412005-11-20 00:51:22 -0500391 if (udev->state != UIST_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -ENODEV;
393
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500394 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EAGAIN;
396
397 retval = wait_event_interruptible(udev->waitq,
Dmitry Torokhov29506412005-11-20 00:51:22 -0500398 udev->head != udev->tail || udev->state != UIST_CREATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (retval)
400 return retval;
401
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500402 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500403 if (retval)
404 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dmitry Torokhov29506412005-11-20 00:51:22 -0500406 if (udev->state != UIST_CREATED) {
407 retval = -ENODEV;
408 goto out;
409 }
410
411 while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
412 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
413 retval = -EFAULT;
414 goto out;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
417 retval += sizeof(struct input_event);
418 }
419
Dmitry Torokhov29506412005-11-20 00:51:22 -0500420 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500421 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return retval;
424}
425
426static unsigned int uinput_poll(struct file *file, poll_table *wait)
427{
428 struct uinput_device *udev = file->private_data;
429
430 poll_wait(file, &udev->waitq, wait);
431
432 if (udev->head != udev->tail)
433 return POLLIN | POLLRDNORM;
434
435 return 0;
436}
437
Dmitry Torokhov29506412005-11-20 00:51:22 -0500438static int uinput_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500440 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dmitry Torokhov29506412005-11-20 00:51:22 -0500442 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 kfree(udev);
444
445 return 0;
446}
447
Dmitry Torokhov29506412005-11-20 00:51:22 -0500448#define uinput_set_bit(_arg, _bit, _max) \
449({ \
450 int __ret = 0; \
451 if (udev->state == UIST_CREATED) \
452 __ret = -EINVAL; \
453 else if ((_arg) > (_max)) \
454 __ret = -EINVAL; \
455 else set_bit((_arg), udev->dev->_bit); \
456 __ret; \
457})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dmitry Torokhov29506412005-11-20 00:51:22 -0500459static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500461 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct uinput_device *udev;
463 void __user *p = (void __user *)arg;
464 struct uinput_ff_upload ff_up;
465 struct uinput_ff_erase ff_erase;
466 struct uinput_request *req;
467 int length;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500468 char *phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 udev = file->private_data;
471
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500472 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500473 if (retval)
474 return retval;
475
476 if (!udev->dev) {
477 retval = uinput_allocate_device(udev);
478 if (retval)
479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 switch (cmd) {
483 case UI_DEV_CREATE:
484 retval = uinput_create_device(udev);
485 break;
486
487 case UI_DEV_DESTROY:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500488 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490
491 case UI_SET_EVBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500492 retval = uinput_set_bit(arg, evbit, EV_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494
495 case UI_SET_KEYBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500496 retval = uinput_set_bit(arg, keybit, KEY_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498
499 case UI_SET_RELBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500500 retval = uinput_set_bit(arg, relbit, REL_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502
503 case UI_SET_ABSBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500504 retval = uinput_set_bit(arg, absbit, ABS_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506
507 case UI_SET_MSCBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500508 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510
511 case UI_SET_LEDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500512 retval = uinput_set_bit(arg, ledbit, LED_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
514
515 case UI_SET_SNDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500516 retval = uinput_set_bit(arg, sndbit, SND_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
518
519 case UI_SET_FFBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500520 retval = uinput_set_bit(arg, ffbit, FF_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500523 case UI_SET_SWBIT:
524 retval = uinput_set_bit(arg, swbit, SW_MAX);
525 break;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 case UI_SET_PHYS:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500528 if (udev->state == UIST_CREATED) {
529 retval = -EINVAL;
530 goto out;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 length = strnlen_user(p, 1024);
533 if (length <= 0) {
534 retval = -EFAULT;
535 break;
536 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500537 kfree(udev->dev->phys);
538 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
539 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 retval = -ENOMEM;
541 break;
542 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500543 if (copy_from_user(phys, p, length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 udev->dev->phys = NULL;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500545 kfree(phys);
546 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500549 phys[length - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551
552 case UI_BEGIN_FF_UPLOAD:
553 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
554 retval = -EFAULT;
555 break;
556 }
557 req = uinput_request_find(udev, ff_up.request_id);
Anssi Hannulaff462552006-07-19 01:41:09 -0400558 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 retval = -EINVAL;
560 break;
561 }
562 ff_up.retval = 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400563 memcpy(&ff_up.effect, req->u.upload.effect, sizeof(struct ff_effect));
564 if (req->u.upload.old)
565 memcpy(&ff_up.old, req->u.upload.old, sizeof(struct ff_effect));
566 else
567 memset(&ff_up.old, 0, sizeof(struct ff_effect));
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
570 retval = -EFAULT;
571 break;
572 }
573 break;
574
575 case UI_BEGIN_FF_ERASE:
576 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
577 retval = -EFAULT;
578 break;
579 }
580 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500581 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 retval = -EINVAL;
583 break;
584 }
585 ff_erase.retval = 0;
586 ff_erase.effect_id = req->u.effect_id;
587 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
588 retval = -EFAULT;
589 break;
590 }
591 break;
592
593 case UI_END_FF_UPLOAD:
594 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
595 retval = -EFAULT;
596 break;
597 }
598 req = uinput_request_find(udev, ff_up.request_id);
Anssi Hannulaff462552006-07-19 01:41:09 -0400599 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 retval = -EINVAL;
601 break;
602 }
603 req->retval = ff_up.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500604 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606
607 case UI_END_FF_ERASE:
608 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
609 retval = -EFAULT;
610 break;
611 }
612 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500613 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 retval = -EINVAL;
615 break;
616 }
617 req->retval = ff_erase.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500618 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
620
621 default:
622 retval = -EINVAL;
623 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500624
625 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500626 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return retval;
628}
629
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800630static const struct file_operations uinput_fops = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500631 .owner = THIS_MODULE,
632 .open = uinput_open,
633 .release = uinput_release,
634 .read = uinput_read,
635 .write = uinput_write,
636 .poll = uinput_poll,
637 .unlocked_ioctl = uinput_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638};
639
640static struct miscdevice uinput_misc = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500641 .fops = &uinput_fops,
642 .minor = UINPUT_MINOR,
643 .name = UINPUT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644};
645
646static int __init uinput_init(void)
647{
648 return misc_register(&uinput_misc);
649}
650
651static void __exit uinput_exit(void)
652{
653 misc_deregister(&uinput_misc);
654}
655
656MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
657MODULE_DESCRIPTION("User level driver support for input subsystem");
658MODULE_LICENSE("GPL");
Anssi Hannulaff462552006-07-19 01:41:09 -0400659MODULE_VERSION("0.3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661module_init(uinput_init);
662module_exit(uinput_exit);
663