added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / input / misc / uinput.c
blob46b7caeb2817f3363eda4570d52e8bc1bdeedc00
1 /*
2 * User level driver support for input subsystem
4 * Heavily based on evdev.c by Vojtech Pavlik
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.
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.
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
20 * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
22 * Changes/Revisions:
23 * 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
26 * 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
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/smp_lock.h>
37 #include <linux/fs.h>
38 #include <linux/miscdevice.h>
39 #include <linux/uinput.h>
40 #include "../input-compat.h"
42 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
44 struct uinput_device *udev = input_get_drvdata(dev);
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;
52 wake_up_interruptible(&udev->waitq);
54 return 0;
57 static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
59 /* Atomically allocate an ID for the given request. Returns 0 on success. */
60 int id;
61 int err = -1;
63 spin_lock(&udev->requests_lock);
65 for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
66 if (!udev->requests[id]) {
67 request->id = id;
68 udev->requests[id] = request;
69 err = 0;
70 break;
73 spin_unlock(&udev->requests_lock);
74 return err;
77 static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
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;
83 return udev->requests[id];
86 static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
88 /* Allocate slot. If none are available right away, wait. */
89 return wait_event_interruptible(udev->requests_waitq,
90 !uinput_request_alloc_id(udev, request));
93 static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
95 /* Mark slot as available */
96 udev->requests[request->id] = NULL;
97 wake_up(&udev->requests_waitq);
99 complete(&request->done);
102 static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
104 /* Tell our userspace app about this new request by queueing an input event */
105 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
107 /* Wait for the request to complete */
108 wait_for_completion(&request->done);
109 return request->retval;
112 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
114 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
117 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
119 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
122 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
124 return uinput_dev_event(dev, EV_FF, effect_id, value);
127 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
129 struct uinput_request request;
130 int retval;
133 * uinput driver does not currently support periodic effects with
134 * custom waveform since it does not have a way to pass buffer of
135 * samples (custom_data) to userspace. If ever there is a device
136 * supporting custom waveforms we would need to define an additional
137 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
139 if (effect->type == FF_PERIODIC &&
140 effect->u.periodic.waveform == FF_CUSTOM)
141 return -EINVAL;
143 request.id = -1;
144 init_completion(&request.done);
145 request.code = UI_FF_UPLOAD;
146 request.u.upload.effect = effect;
147 request.u.upload.old = old;
149 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
150 if (!retval)
151 retval = uinput_request_submit(dev, &request);
153 return retval;
156 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
158 struct uinput_request request;
159 int retval;
161 if (!test_bit(EV_FF, dev->evbit))
162 return -ENOSYS;
164 request.id = -1;
165 init_completion(&request.done);
166 request.code = UI_FF_ERASE;
167 request.u.effect_id = effect_id;
169 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
170 if (!retval)
171 retval = uinput_request_submit(dev, &request);
173 return retval;
176 static void uinput_destroy_device(struct uinput_device *udev)
178 const char *name, *phys;
180 if (udev->dev) {
181 name = udev->dev->name;
182 phys = udev->dev->phys;
183 if (udev->state == UIST_CREATED)
184 input_unregister_device(udev->dev);
185 else
186 input_free_device(udev->dev);
187 kfree(name);
188 kfree(phys);
189 udev->dev = NULL;
192 udev->state = UIST_NEW_DEVICE;
195 static int uinput_create_device(struct uinput_device *udev)
197 struct input_dev *dev = udev->dev;
198 int error;
200 if (udev->state != UIST_SETUP_COMPLETE) {
201 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
202 return -EINVAL;
205 if (udev->ff_effects_max) {
206 error = input_ff_create(dev, udev->ff_effects_max);
207 if (error)
208 goto fail1;
210 dev->ff->upload = uinput_dev_upload_effect;
211 dev->ff->erase = uinput_dev_erase_effect;
212 dev->ff->playback = uinput_dev_playback;
213 dev->ff->set_gain = uinput_dev_set_gain;
214 dev->ff->set_autocenter = uinput_dev_set_autocenter;
217 error = input_register_device(udev->dev);
218 if (error)
219 goto fail2;
221 udev->state = UIST_CREATED;
223 return 0;
225 fail2: input_ff_destroy(dev);
226 fail1: uinput_destroy_device(udev);
227 return error;
230 static int uinput_open(struct inode *inode, struct file *file)
232 struct uinput_device *newdev;
234 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
235 if (!newdev)
236 return -ENOMEM;
238 lock_kernel();
239 mutex_init(&newdev->mutex);
240 spin_lock_init(&newdev->requests_lock);
241 init_waitqueue_head(&newdev->requests_waitq);
242 init_waitqueue_head(&newdev->waitq);
243 newdev->state = UIST_NEW_DEVICE;
245 file->private_data = newdev;
246 unlock_kernel();
248 return 0;
251 static int uinput_validate_absbits(struct input_dev *dev)
253 unsigned int cnt;
254 int retval = 0;
256 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
257 if (!test_bit(cnt, dev->absbit))
258 continue;
260 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
261 printk(KERN_DEBUG
262 "%s: invalid abs[%02x] min:%d max:%d\n",
263 UINPUT_NAME, cnt,
264 dev->absmin[cnt], dev->absmax[cnt]);
265 retval = -EINVAL;
266 break;
269 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
270 printk(KERN_DEBUG
271 "%s: absflat[%02x] out of range: %d "
272 "(min:%d/max:%d)\n",
273 UINPUT_NAME, cnt, dev->absflat[cnt],
274 dev->absmin[cnt], dev->absmax[cnt]);
275 retval = -EINVAL;
276 break;
279 return retval;
282 static int uinput_allocate_device(struct uinput_device *udev)
284 udev->dev = input_allocate_device();
285 if (!udev->dev)
286 return -ENOMEM;
288 udev->dev->event = uinput_dev_event;
289 input_set_drvdata(udev->dev, udev);
291 return 0;
294 static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
296 struct uinput_user_dev *user_dev;
297 struct input_dev *dev;
298 char *name;
299 int size;
300 int retval;
302 if (count != sizeof(struct uinput_user_dev))
303 return -EINVAL;
305 if (!udev->dev) {
306 retval = uinput_allocate_device(udev);
307 if (retval)
308 return retval;
311 dev = udev->dev;
313 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
314 if (!user_dev)
315 return -ENOMEM;
317 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
318 retval = -EFAULT;
319 goto exit;
322 udev->ff_effects_max = user_dev->ff_effects_max;
324 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
325 if (!size) {
326 retval = -EINVAL;
327 goto exit;
330 kfree(dev->name);
331 dev->name = name = kmalloc(size, GFP_KERNEL);
332 if (!name) {
333 retval = -ENOMEM;
334 goto exit;
336 strlcpy(name, user_dev->name, size);
338 dev->id.bustype = user_dev->id.bustype;
339 dev->id.vendor = user_dev->id.vendor;
340 dev->id.product = user_dev->id.product;
341 dev->id.version = user_dev->id.version;
343 size = sizeof(int) * (ABS_MAX + 1);
344 memcpy(dev->absmax, user_dev->absmax, size);
345 memcpy(dev->absmin, user_dev->absmin, size);
346 memcpy(dev->absfuzz, user_dev->absfuzz, size);
347 memcpy(dev->absflat, user_dev->absflat, size);
349 /* check if absmin/absmax/absfuzz/absflat are filled as
350 * told in Documentation/input/input-programming.txt */
351 if (test_bit(EV_ABS, dev->evbit)) {
352 retval = uinput_validate_absbits(dev);
353 if (retval < 0)
354 goto exit;
357 udev->state = UIST_SETUP_COMPLETE;
358 retval = count;
360 exit:
361 kfree(user_dev);
362 return retval;
365 static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
367 struct input_event ev;
369 if (count < input_event_size())
370 return -EINVAL;
372 if (input_event_from_user(buffer, &ev))
373 return -EFAULT;
375 input_event(udev->dev, ev.type, ev.code, ev.value);
377 return input_event_size();
380 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
382 struct uinput_device *udev = file->private_data;
383 int retval;
385 retval = mutex_lock_interruptible(&udev->mutex);
386 if (retval)
387 return retval;
389 retval = udev->state == UIST_CREATED ?
390 uinput_inject_event(udev, buffer, count) :
391 uinput_setup_device(udev, buffer, count);
393 mutex_unlock(&udev->mutex);
395 return retval;
398 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
400 struct uinput_device *udev = file->private_data;
401 int retval = 0;
403 if (udev->state != UIST_CREATED)
404 return -ENODEV;
406 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
407 return -EAGAIN;
409 retval = wait_event_interruptible(udev->waitq,
410 udev->head != udev->tail || udev->state != UIST_CREATED);
411 if (retval)
412 return retval;
414 retval = mutex_lock_interruptible(&udev->mutex);
415 if (retval)
416 return retval;
418 if (udev->state != UIST_CREATED) {
419 retval = -ENODEV;
420 goto out;
423 while (udev->head != udev->tail && retval + input_event_size() <= count) {
424 if (input_event_to_user(buffer + retval, &udev->buff[udev->tail])) {
425 retval = -EFAULT;
426 goto out;
428 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
429 retval += input_event_size();
432 out:
433 mutex_unlock(&udev->mutex);
435 return retval;
438 static unsigned int uinput_poll(struct file *file, poll_table *wait)
440 struct uinput_device *udev = file->private_data;
442 poll_wait(file, &udev->waitq, wait);
444 if (udev->head != udev->tail)
445 return POLLIN | POLLRDNORM;
447 return 0;
450 static int uinput_release(struct inode *inode, struct file *file)
452 struct uinput_device *udev = file->private_data;
454 uinput_destroy_device(udev);
455 kfree(udev);
457 return 0;
460 #ifdef CONFIG_COMPAT
461 struct uinput_ff_upload_compat {
462 int request_id;
463 int retval;
464 struct ff_effect_compat effect;
465 struct ff_effect_compat old;
468 static int uinput_ff_upload_to_user(char __user *buffer,
469 const struct uinput_ff_upload *ff_up)
471 if (INPUT_COMPAT_TEST) {
472 struct uinput_ff_upload_compat ff_up_compat;
474 ff_up_compat.request_id = ff_up->request_id;
475 ff_up_compat.retval = ff_up->retval;
477 * It so happens that the pointer that gives us the trouble
478 * is the last field in the structure. Since we don't support
479 * custom waveforms in uinput anyway we can just copy the whole
480 * thing (to the compat size) and ignore the pointer.
482 memcpy(&ff_up_compat.effect, &ff_up->effect,
483 sizeof(struct ff_effect_compat));
484 memcpy(&ff_up_compat.old, &ff_up->old,
485 sizeof(struct ff_effect_compat));
487 if (copy_to_user(buffer, &ff_up_compat,
488 sizeof(struct uinput_ff_upload_compat)))
489 return -EFAULT;
490 } else {
491 if (copy_to_user(buffer, ff_up,
492 sizeof(struct uinput_ff_upload)))
493 return -EFAULT;
496 return 0;
499 static int uinput_ff_upload_from_user(const char __user *buffer,
500 struct uinput_ff_upload *ff_up)
502 if (INPUT_COMPAT_TEST) {
503 struct uinput_ff_upload_compat ff_up_compat;
505 if (copy_from_user(&ff_up_compat, buffer,
506 sizeof(struct uinput_ff_upload_compat)))
507 return -EFAULT;
509 ff_up->request_id = ff_up_compat.request_id;
510 ff_up->retval = ff_up_compat.retval;
511 memcpy(&ff_up->effect, &ff_up_compat.effect,
512 sizeof(struct ff_effect_compat));
513 memcpy(&ff_up->old, &ff_up_compat.old,
514 sizeof(struct ff_effect_compat));
516 } else {
517 if (copy_from_user(ff_up, buffer,
518 sizeof(struct uinput_ff_upload)))
519 return -EFAULT;
522 return 0;
525 #else
527 static int uinput_ff_upload_to_user(char __user *buffer,
528 const struct uinput_ff_upload *ff_up)
530 if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
531 return -EFAULT;
533 return 0;
536 static int uinput_ff_upload_from_user(const char __user *buffer,
537 struct uinput_ff_upload *ff_up)
539 if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
540 return -EFAULT;
542 return 0;
545 #endif
547 #define uinput_set_bit(_arg, _bit, _max) \
548 ({ \
549 int __ret = 0; \
550 if (udev->state == UIST_CREATED) \
551 __ret = -EINVAL; \
552 else if ((_arg) > (_max)) \
553 __ret = -EINVAL; \
554 else set_bit((_arg), udev->dev->_bit); \
555 __ret; \
558 static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
559 unsigned long arg, void __user *p)
561 int retval;
562 struct uinput_device *udev = file->private_data;
563 struct uinput_ff_upload ff_up;
564 struct uinput_ff_erase ff_erase;
565 struct uinput_request *req;
566 int length;
567 char *phys;
569 retval = mutex_lock_interruptible(&udev->mutex);
570 if (retval)
571 return retval;
573 if (!udev->dev) {
574 retval = uinput_allocate_device(udev);
575 if (retval)
576 goto out;
579 switch (cmd) {
580 case UI_DEV_CREATE:
581 retval = uinput_create_device(udev);
582 break;
584 case UI_DEV_DESTROY:
585 uinput_destroy_device(udev);
586 break;
588 case UI_SET_EVBIT:
589 retval = uinput_set_bit(arg, evbit, EV_MAX);
590 break;
592 case UI_SET_KEYBIT:
593 retval = uinput_set_bit(arg, keybit, KEY_MAX);
594 break;
596 case UI_SET_RELBIT:
597 retval = uinput_set_bit(arg, relbit, REL_MAX);
598 break;
600 case UI_SET_ABSBIT:
601 retval = uinput_set_bit(arg, absbit, ABS_MAX);
602 break;
604 case UI_SET_MSCBIT:
605 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
606 break;
608 case UI_SET_LEDBIT:
609 retval = uinput_set_bit(arg, ledbit, LED_MAX);
610 break;
612 case UI_SET_SNDBIT:
613 retval = uinput_set_bit(arg, sndbit, SND_MAX);
614 break;
616 case UI_SET_FFBIT:
617 retval = uinput_set_bit(arg, ffbit, FF_MAX);
618 break;
620 case UI_SET_SWBIT:
621 retval = uinput_set_bit(arg, swbit, SW_MAX);
622 break;
624 case UI_SET_PHYS:
625 if (udev->state == UIST_CREATED) {
626 retval = -EINVAL;
627 goto out;
629 length = strnlen_user(p, 1024);
630 if (length <= 0) {
631 retval = -EFAULT;
632 break;
634 kfree(udev->dev->phys);
635 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
636 if (!phys) {
637 retval = -ENOMEM;
638 break;
640 if (copy_from_user(phys, p, length)) {
641 udev->dev->phys = NULL;
642 kfree(phys);
643 retval = -EFAULT;
644 break;
646 phys[length - 1] = '\0';
647 break;
649 case UI_BEGIN_FF_UPLOAD:
650 retval = uinput_ff_upload_from_user(p, &ff_up);
651 if (retval)
652 break;
654 req = uinput_request_find(udev, ff_up.request_id);
655 if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
656 retval = -EINVAL;
657 break;
660 ff_up.retval = 0;
661 ff_up.effect = *req->u.upload.effect;
662 if (req->u.upload.old)
663 ff_up.old = *req->u.upload.old;
664 else
665 memset(&ff_up.old, 0, sizeof(struct ff_effect));
667 retval = uinput_ff_upload_to_user(p, &ff_up);
668 break;
670 case UI_BEGIN_FF_ERASE:
671 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
672 retval = -EFAULT;
673 break;
676 req = uinput_request_find(udev, ff_erase.request_id);
677 if (!req || req->code != UI_FF_ERASE) {
678 retval = -EINVAL;
679 break;
682 ff_erase.retval = 0;
683 ff_erase.effect_id = req->u.effect_id;
684 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
685 retval = -EFAULT;
686 break;
689 break;
691 case UI_END_FF_UPLOAD:
692 retval = uinput_ff_upload_from_user(p, &ff_up);
693 if (retval)
694 break;
696 req = uinput_request_find(udev, ff_up.request_id);
697 if (!req || req->code != UI_FF_UPLOAD ||
698 !req->u.upload.effect) {
699 retval = -EINVAL;
700 break;
703 req->retval = ff_up.retval;
704 uinput_request_done(udev, req);
705 break;
707 case UI_END_FF_ERASE:
708 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
709 retval = -EFAULT;
710 break;
713 req = uinput_request_find(udev, ff_erase.request_id);
714 if (!req || req->code != UI_FF_ERASE) {
715 retval = -EINVAL;
716 break;
719 req->retval = ff_erase.retval;
720 uinput_request_done(udev, req);
721 break;
723 default:
724 retval = -EINVAL;
727 out:
728 mutex_unlock(&udev->mutex);
729 return retval;
732 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
734 return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
737 #ifdef CONFIG_COMPAT
738 static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
740 return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
742 #endif
744 static const struct file_operations uinput_fops = {
745 .owner = THIS_MODULE,
746 .open = uinput_open,
747 .release = uinput_release,
748 .read = uinput_read,
749 .write = uinput_write,
750 .poll = uinput_poll,
751 .unlocked_ioctl = uinput_ioctl,
752 #ifdef CONFIG_COMPAT
753 .compat_ioctl = uinput_compat_ioctl,
754 #endif
757 static struct miscdevice uinput_misc = {
758 .fops = &uinput_fops,
759 .minor = UINPUT_MINOR,
760 .name = UINPUT_NAME,
763 static int __init uinput_init(void)
765 return misc_register(&uinput_misc);
768 static void __exit uinput_exit(void)
770 misc_deregister(&uinput_misc);
773 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
774 MODULE_DESCRIPTION("User level driver support for input subsystem");
775 MODULE_LICENSE("GPL");
776 MODULE_VERSION("0.3");
778 module_init(uinput_init);
779 module_exit(uinput_exit);