blob: 9c3d20073ae3de6966260e0419600771da840f30 [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:
23 * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
24 * - added force feedback support
25 * - added UI_SET_PHYS
26 * 0.1 20/06/2002
27 * - first public version
28 */
29#include <linux/poll.h>
30#include <linux/slab.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/input.h>
34#include <linux/smp_lock.h>
35#include <linux/fs.h>
36#include <linux/miscdevice.h>
37#include <linux/uinput.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
40{
41 struct uinput_device *udev;
42
43 udev = dev->private;
44
45 udev->buff[udev->head].type = type;
46 udev->buff[udev->head].code = code;
47 udev->buff[udev->head].value = value;
48 do_gettimeofday(&udev->buff[udev->head].time);
49 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
50
51 wake_up_interruptible(&udev->waitq);
52
53 return 0;
54}
55
56static int uinput_request_alloc_id(struct input_dev *dev, struct uinput_request *request)
57{
58 /* Atomically allocate an ID for the given request. Returns 0 on success. */
59 struct uinput_device *udev = dev->private;
60 int id;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050061 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 down(&udev->requests_sem);
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]) {
67 udev->requests[id] = request;
68 request->id = id;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 up(&udev->requests_sem);
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;
82 if (udev->requests[id]->completed)
83 return NULL;
84 return udev->requests[id];
85}
86
87static void uinput_request_init(struct input_dev *dev, struct uinput_request *request, int code)
88{
89 struct uinput_device *udev = dev->private;
90
91 memset(request, 0, sizeof(struct uinput_request));
92 request->code = code;
93 init_waitqueue_head(&request->waitq);
94
95 /* Allocate an ID. If none are available right away, wait. */
96 request->retval = wait_event_interruptible(udev->requests_waitq,
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050097 !uinput_request_alloc_id(dev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
101{
102 struct uinput_device *udev = dev->private;
103 int retval;
104
105 /* Tell our userspace app about this new request by queueing an input event */
106 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
107
108 /* Wait for the request to complete */
109 retval = wait_event_interruptible(request->waitq, request->completed);
110 if (retval)
111 request->retval = retval;
112
113 /* Release this request's ID, let others know it's available */
114 udev->requests[request->id] = NULL;
115 wake_up_interruptible(&udev->requests_waitq);
116}
117
118static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
119{
120 struct uinput_request request;
121
122 if (!test_bit(EV_FF, dev->evbit))
123 return -ENOSYS;
124
125 uinput_request_init(dev, &request, UI_FF_UPLOAD);
126 if (request.retval)
127 return request.retval;
128 request.u.effect = effect;
129 uinput_request_submit(dev, &request);
130 return request.retval;
131}
132
133static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
134{
135 struct uinput_request request;
136
137 if (!test_bit(EV_FF, dev->evbit))
138 return -ENOSYS;
139
140 uinput_request_init(dev, &request, UI_FF_ERASE);
141 if (request.retval)
142 return request.retval;
143 request.u.effect_id = effect_id;
144 uinput_request_submit(dev, &request);
145 return request.retval;
146}
147
148static int uinput_create_device(struct uinput_device *udev)
149{
150 if (!udev->dev->name) {
151 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
152 return -EINVAL;
153 }
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 udev->dev->event = uinput_dev_event;
156 udev->dev->upload_effect = uinput_dev_upload_effect;
157 udev->dev->erase_effect = uinput_dev_erase_effect;
158 udev->dev->private = udev;
159
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500160 init_waitqueue_head(&udev->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 input_register_device(udev->dev);
163
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500164 set_bit(UIST_CREATED, &udev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 return 0;
167}
168
169static int uinput_destroy_device(struct uinput_device *udev)
170{
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500171 if (!test_bit(UIST_CREATED, &udev->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
173 return -EINVAL;
174 }
175
176 input_unregister_device(udev->dev);
177
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500178 clear_bit(UIST_CREATED, &udev->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 return 0;
181}
182
183static int uinput_open(struct inode *inode, struct file *file)
184{
185 struct uinput_device *newdev;
186 struct input_dev *newinput;
187
188 newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
189 if (!newdev)
190 goto error;
191 memset(newdev, 0, sizeof(struct uinput_device));
192 init_MUTEX(&newdev->requests_sem);
193 init_waitqueue_head(&newdev->requests_waitq);
194
195 newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
196 if (!newinput)
197 goto cleanup;
198 memset(newinput, 0, sizeof(struct input_dev));
199
200 newdev->dev = newinput;
201
202 file->private_data = newdev;
203
204 return 0;
205cleanup:
206 kfree(newdev);
207error:
208 return -ENOMEM;
209}
210
211static int uinput_validate_absbits(struct input_dev *dev)
212{
213 unsigned int cnt;
214 int retval = 0;
215
216 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
217 if (!test_bit(cnt, dev->absbit))
218 continue;
219
220 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
221 printk(KERN_DEBUG
222 "%s: invalid abs[%02x] min:%d max:%d\n",
223 UINPUT_NAME, cnt,
224 dev->absmin[cnt], dev->absmax[cnt]);
225 retval = -EINVAL;
226 break;
227 }
228
229 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
230 printk(KERN_DEBUG
231 "%s: absflat[%02x] out of range: %d "
232 "(min:%d/max:%d)\n",
233 UINPUT_NAME, cnt, dev->absflat[cnt],
234 dev->absmin[cnt], dev->absmax[cnt]);
235 retval = -EINVAL;
236 break;
237 }
238 }
239 return retval;
240}
241
242static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count)
243{
244 struct uinput_user_dev *user_dev;
245 struct input_dev *dev;
246 struct uinput_device *udev;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500247 int size;
248 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 retval = count;
251
252 udev = file->private_data;
253 dev = udev->dev;
254
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500255 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!user_dev) {
257 retval = -ENOMEM;
258 goto exit;
259 }
260
261 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
262 retval = -EFAULT;
263 goto exit;
264 }
265
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500266 if (dev->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 kfree(dev->name);
268
269 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
270 dev->name = kmalloc(size, GFP_KERNEL);
271 if (!dev->name) {
272 retval = -ENOMEM;
273 goto exit;
274 }
275
276 strlcpy(dev->name, user_dev->name, size);
277 dev->id.bustype = user_dev->id.bustype;
278 dev->id.vendor = user_dev->id.vendor;
279 dev->id.product = user_dev->id.product;
280 dev->id.version = user_dev->id.version;
281 dev->ff_effects_max = user_dev->ff_effects_max;
282
283 size = sizeof(int) * (ABS_MAX + 1);
284 memcpy(dev->absmax, user_dev->absmax, size);
285 memcpy(dev->absmin, user_dev->absmin, size);
286 memcpy(dev->absfuzz, user_dev->absfuzz, size);
287 memcpy(dev->absflat, user_dev->absflat, size);
288
289 /* check if absmin/absmax/absfuzz/absflat are filled as
290 * told in Documentation/input/input-programming.txt */
291 if (test_bit(EV_ABS, dev->evbit)) {
Ian Campbellb6cbf3e2005-06-01 02:39:25 -0500292 int err = uinput_validate_absbits(dev);
293 if (err < 0) {
294 retval = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kfree(dev->name);
Ian Campbellb6cbf3e2005-06-01 02:39:25 -0500296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299exit:
300 kfree(user_dev);
301 return retval;
302}
303
304static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
305{
306 struct uinput_device *udev = file->private_data;
307
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500308 if (test_bit(UIST_CREATED, &udev->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct input_event ev;
310
311 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
312 return -EFAULT;
313 input_event(udev->dev, ev.type, ev.code, ev.value);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500314 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 count = uinput_alloc_device(file, buffer, count);
316
317 return count;
318}
319
320static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
321{
322 struct uinput_device *udev = file->private_data;
323 int retval = 0;
324
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500325 if (!test_bit(UIST_CREATED, &udev->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -ENODEV;
327
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500328 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -EAGAIN;
330
331 retval = wait_event_interruptible(udev->waitq,
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500332 udev->head != udev->tail || !test_bit(UIST_CREATED, &udev->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (retval)
334 return retval;
335
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500336 if (!test_bit(UIST_CREATED, &udev->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -ENODEV;
338
339 while ((udev->head != udev->tail) &&
340 (retval + sizeof(struct input_event) <= count)) {
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500341 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event)))
342 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
344 retval += sizeof(struct input_event);
345 }
346
347 return retval;
348}
349
350static unsigned int uinput_poll(struct file *file, poll_table *wait)
351{
352 struct uinput_device *udev = file->private_data;
353
354 poll_wait(file, &udev->waitq, wait);
355
356 if (udev->head != udev->tail)
357 return POLLIN | POLLRDNORM;
358
359 return 0;
360}
361
362static int uinput_burn_device(struct uinput_device *udev)
363{
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500364 if (test_bit(UIST_CREATED, &udev->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 uinput_destroy_device(udev);
366
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500367 if (udev->dev->name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 kfree(udev->dev->name);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500369 if (udev->dev->phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 kfree(udev->dev->phys);
371
372 kfree(udev->dev);
373 kfree(udev);
374
375 return 0;
376}
377
378static int uinput_close(struct inode *inode, struct file *file)
379{
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500380 uinput_burn_device(file->private_data);
381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
385{
386 int retval = 0;
387 struct uinput_device *udev;
388 void __user *p = (void __user *)arg;
389 struct uinput_ff_upload ff_up;
390 struct uinput_ff_erase ff_erase;
391 struct uinput_request *req;
392 int length;
393
394 udev = file->private_data;
395
396 /* device attributes can not be changed after the device is created */
397 switch (cmd) {
398 case UI_SET_EVBIT:
399 case UI_SET_KEYBIT:
400 case UI_SET_RELBIT:
401 case UI_SET_ABSBIT:
402 case UI_SET_MSCBIT:
403 case UI_SET_LEDBIT:
404 case UI_SET_SNDBIT:
405 case UI_SET_FFBIT:
406 case UI_SET_PHYS:
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500407 if (test_bit(UIST_CREATED, &udev->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return -EINVAL;
409 }
410
411 switch (cmd) {
412 case UI_DEV_CREATE:
413 retval = uinput_create_device(udev);
414 break;
415
416 case UI_DEV_DESTROY:
417 retval = uinput_destroy_device(udev);
418 break;
419
420 case UI_SET_EVBIT:
421 if (arg > EV_MAX) {
422 retval = -EINVAL;
423 break;
424 }
425 set_bit(arg, udev->dev->evbit);
426 break;
427
428 case UI_SET_KEYBIT:
429 if (arg > KEY_MAX) {
430 retval = -EINVAL;
431 break;
432 }
433 set_bit(arg, udev->dev->keybit);
434 break;
435
436 case UI_SET_RELBIT:
437 if (arg > REL_MAX) {
438 retval = -EINVAL;
439 break;
440 }
441 set_bit(arg, udev->dev->relbit);
442 break;
443
444 case UI_SET_ABSBIT:
445 if (arg > ABS_MAX) {
446 retval = -EINVAL;
447 break;
448 }
449 set_bit(arg, udev->dev->absbit);
450 break;
451
452 case UI_SET_MSCBIT:
453 if (arg > MSC_MAX) {
454 retval = -EINVAL;
455 break;
456 }
457 set_bit(arg, udev->dev->mscbit);
458 break;
459
460 case UI_SET_LEDBIT:
461 if (arg > LED_MAX) {
462 retval = -EINVAL;
463 break;
464 }
465 set_bit(arg, udev->dev->ledbit);
466 break;
467
468 case UI_SET_SNDBIT:
469 if (arg > SND_MAX) {
470 retval = -EINVAL;
471 break;
472 }
473 set_bit(arg, udev->dev->sndbit);
474 break;
475
476 case UI_SET_FFBIT:
477 if (arg > FF_MAX) {
478 retval = -EINVAL;
479 break;
480 }
481 set_bit(arg, udev->dev->ffbit);
482 break;
483
484 case UI_SET_PHYS:
485 length = strnlen_user(p, 1024);
486 if (length <= 0) {
487 retval = -EFAULT;
488 break;
489 }
490 if (NULL != udev->dev->phys)
491 kfree(udev->dev->phys);
492 udev->dev->phys = kmalloc(length, GFP_KERNEL);
493 if (!udev->dev->phys) {
494 retval = -ENOMEM;
495 break;
496 }
497 if (copy_from_user(udev->dev->phys, p, length)) {
498 retval = -EFAULT;
499 kfree(udev->dev->phys);
500 udev->dev->phys = NULL;
501 break;
502 }
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500503 udev->dev->phys[length - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505
506 case UI_BEGIN_FF_UPLOAD:
507 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
508 retval = -EFAULT;
509 break;
510 }
511 req = uinput_request_find(udev, ff_up.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500512 if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 retval = -EINVAL;
514 break;
515 }
516 ff_up.retval = 0;
517 memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
518 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
519 retval = -EFAULT;
520 break;
521 }
522 break;
523
524 case UI_BEGIN_FF_ERASE:
525 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
526 retval = -EFAULT;
527 break;
528 }
529 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500530 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 retval = -EINVAL;
532 break;
533 }
534 ff_erase.retval = 0;
535 ff_erase.effect_id = req->u.effect_id;
536 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
537 retval = -EFAULT;
538 break;
539 }
540 break;
541
542 case UI_END_FF_UPLOAD:
543 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
544 retval = -EFAULT;
545 break;
546 }
547 req = uinput_request_find(udev, ff_up.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500548 if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 retval = -EINVAL;
550 break;
551 }
552 req->retval = ff_up.retval;
553 memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
554 req->completed = 1;
555 wake_up_interruptible(&req->waitq);
556 break;
557
558 case UI_END_FF_ERASE:
559 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
560 retval = -EFAULT;
561 break;
562 }
563 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500564 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 retval = -EINVAL;
566 break;
567 }
568 req->retval = ff_erase.retval;
569 req->completed = 1;
570 wake_up_interruptible(&req->waitq);
571 break;
572
573 default:
574 retval = -EINVAL;
575 }
576 return retval;
577}
578
579static struct file_operations uinput_fops = {
580 .owner = THIS_MODULE,
581 .open = uinput_open,
582 .release = uinput_close,
583 .read = uinput_read,
584 .write = uinput_write,
585 .poll = uinput_poll,
586 .ioctl = uinput_ioctl,
587};
588
589static struct miscdevice uinput_misc = {
590 .fops = &uinput_fops,
591 .minor = UINPUT_MINOR,
592 .name = UINPUT_NAME,
593};
594
595static int __init uinput_init(void)
596{
597 return misc_register(&uinput_misc);
598}
599
600static void __exit uinput_exit(void)
601{
602 misc_deregister(&uinput_misc);
603}
604
605MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
606MODULE_DESCRIPTION("User level driver support for input subsystem");
607MODULE_LICENSE("GPL");
608
609module_init(uinput_init);
610module_exit(uinput_exit);
611