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>
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
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/input.h>
37 #include <linux/smp_lock.h>
39 #include <linux/miscdevice.h>
40 #include <linux/uinput.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);
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. */
63 spin_lock(&udev->requests_lock);
65 for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
66 if (!udev->requests[id]) {
68 udev->requests[id] = request;
73 spin_unlock(&udev->requests_lock);
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)
82 return udev->requests[id];
85 static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
87 /* Allocate slot. If none are available right away, wait. */
88 return wait_event_interruptible(udev->requests_waitq,
89 !uinput_request_alloc_id(udev, request));
92 static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
94 /* Mark slot as available */
95 udev->requests[request->id] = NULL;
96 wake_up(&udev->requests_waitq);
98 complete(&request->done);
101 static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
103 /* Tell our userspace app about this new request by queueing an input event */
104 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
106 /* Wait for the request to complete */
107 wait_for_completion(&request->done);
108 return request->retval;
111 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
113 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
116 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
118 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
121 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
123 return uinput_dev_event(dev, EV_FF, effect_id, value);
126 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
128 struct uinput_request request;
132 init_completion(&request.done);
133 request.code = UI_FF_UPLOAD;
134 request.u.upload.effect = effect;
135 request.u.upload.old = old;
137 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
139 retval = uinput_request_submit(dev, &request);
144 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
146 struct uinput_request request;
149 if (!test_bit(EV_FF, dev->evbit))
153 init_completion(&request.done);
154 request.code = UI_FF_ERASE;
155 request.u.effect_id = effect_id;
157 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
159 retval = uinput_request_submit(dev, &request);
164 static void uinput_destroy_device(struct uinput_device *udev)
166 const char *name, *phys;
169 name = udev->dev->name;
170 phys = udev->dev->phys;
171 if (udev->state == UIST_CREATED)
172 input_unregister_device(udev->dev);
174 input_free_device(udev->dev);
180 udev->state = UIST_NEW_DEVICE;
183 static int uinput_create_device(struct uinput_device *udev)
185 struct input_dev *dev = udev->dev;
188 if (udev->state != UIST_SETUP_COMPLETE) {
189 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
193 if (udev->ff_effects_max) {
194 error = input_ff_create(dev, udev->ff_effects_max);
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;
205 error = input_register_device(udev->dev);
209 udev->state = UIST_CREATED;
213 fail2: input_ff_destroy(dev);
214 fail1: uinput_destroy_device(udev);
218 static int uinput_open(struct inode *inode, struct file *file)
220 struct uinput_device *newdev;
222 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
226 mutex_init(&newdev->mutex);
227 spin_lock_init(&newdev->requests_lock);
228 init_waitqueue_head(&newdev->requests_waitq);
229 init_waitqueue_head(&newdev->waitq);
230 newdev->state = UIST_NEW_DEVICE;
232 file->private_data = newdev;
237 static int uinput_validate_absbits(struct input_dev *dev)
242 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
243 if (!test_bit(cnt, dev->absbit))
246 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
248 "%s: invalid abs[%02x] min:%d max:%d\n",
250 dev->absmin[cnt], dev->absmax[cnt]);
255 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
257 "%s: absflat[%02x] out of range: %d "
259 UINPUT_NAME, cnt, dev->absflat[cnt],
260 dev->absmin[cnt], dev->absmax[cnt]);
268 static int uinput_allocate_device(struct uinput_device *udev)
270 udev->dev = input_allocate_device();
274 udev->dev->event = uinput_dev_event;
275 input_set_drvdata(udev->dev, udev);
280 static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
282 struct uinput_user_dev *user_dev;
283 struct input_dev *dev;
288 if (count != sizeof(struct uinput_user_dev))
292 retval = uinput_allocate_device(udev);
299 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
303 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
308 udev->ff_effects_max = user_dev->ff_effects_max;
310 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
317 dev->name = name = kmalloc(size, GFP_KERNEL);
322 strlcpy(name, user_dev->name, size);
324 dev->id.bustype = user_dev->id.bustype;
325 dev->id.vendor = user_dev->id.vendor;
326 dev->id.product = user_dev->id.product;
327 dev->id.version = user_dev->id.version;
329 size = sizeof(int) * (ABS_MAX + 1);
330 memcpy(dev->absmax, user_dev->absmax, size);
331 memcpy(dev->absmin, user_dev->absmin, size);
332 memcpy(dev->absfuzz, user_dev->absfuzz, size);
333 memcpy(dev->absflat, user_dev->absflat, size);
335 /* check if absmin/absmax/absfuzz/absflat are filled as
336 * told in Documentation/input/input-programming.txt */
337 if (test_bit(EV_ABS, dev->evbit)) {
338 retval = uinput_validate_absbits(dev);
343 udev->state = UIST_SETUP_COMPLETE;
351 static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
353 struct input_event ev;
355 if (count != sizeof(struct input_event))
358 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
361 input_event(udev->dev, ev.type, ev.code, ev.value);
363 return sizeof(struct input_event);
366 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
368 struct uinput_device *udev = file->private_data;
371 retval = mutex_lock_interruptible(&udev->mutex);
375 retval = udev->state == UIST_CREATED ?
376 uinput_inject_event(udev, buffer, count) :
377 uinput_setup_device(udev, buffer, count);
379 mutex_unlock(&udev->mutex);
384 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
386 struct uinput_device *udev = file->private_data;
389 if (udev->state != UIST_CREATED)
392 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
395 retval = wait_event_interruptible(udev->waitq,
396 udev->head != udev->tail || udev->state != UIST_CREATED);
400 retval = mutex_lock_interruptible(&udev->mutex);
404 if (udev->state != UIST_CREATED) {
409 while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
410 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
414 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
415 retval += sizeof(struct input_event);
419 mutex_unlock(&udev->mutex);
424 static unsigned int uinput_poll(struct file *file, poll_table *wait)
426 struct uinput_device *udev = file->private_data;
428 poll_wait(file, &udev->waitq, wait);
430 if (udev->head != udev->tail)
431 return POLLIN | POLLRDNORM;
436 static int uinput_release(struct inode *inode, struct file *file)
438 struct uinput_device *udev = file->private_data;
440 uinput_destroy_device(udev);
446 #define uinput_set_bit(_arg, _bit, _max) \
449 if (udev->state == UIST_CREATED) \
451 else if ((_arg) > (_max)) \
453 else set_bit((_arg), udev->dev->_bit); \
457 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
460 struct uinput_device *udev;
461 void __user *p = (void __user *)arg;
462 struct uinput_ff_upload ff_up;
463 struct uinput_ff_erase ff_erase;
464 struct uinput_request *req;
468 udev = file->private_data;
470 retval = mutex_lock_interruptible(&udev->mutex);
475 retval = uinput_allocate_device(udev);
482 retval = uinput_create_device(udev);
486 uinput_destroy_device(udev);
490 retval = uinput_set_bit(arg, evbit, EV_MAX);
494 retval = uinput_set_bit(arg, keybit, KEY_MAX);
498 retval = uinput_set_bit(arg, relbit, REL_MAX);
502 retval = uinput_set_bit(arg, absbit, ABS_MAX);
506 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
510 retval = uinput_set_bit(arg, ledbit, LED_MAX);
514 retval = uinput_set_bit(arg, sndbit, SND_MAX);
518 retval = uinput_set_bit(arg, ffbit, FF_MAX);
522 retval = uinput_set_bit(arg, swbit, SW_MAX);
526 if (udev->state == UIST_CREATED) {
530 length = strnlen_user(p, 1024);
535 kfree(udev->dev->phys);
536 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
541 if (copy_from_user(phys, p, length)) {
542 udev->dev->phys = NULL;
547 phys[length - 1] = '\0';
550 case UI_BEGIN_FF_UPLOAD:
551 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
555 req = uinput_request_find(udev, ff_up.request_id);
556 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
561 memcpy(&ff_up.effect, req->u.upload.effect, sizeof(struct ff_effect));
562 if (req->u.upload.old)
563 memcpy(&ff_up.old, req->u.upload.old, sizeof(struct ff_effect));
565 memset(&ff_up.old, 0, sizeof(struct ff_effect));
567 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
573 case UI_BEGIN_FF_ERASE:
574 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
578 req = uinput_request_find(udev, ff_erase.request_id);
579 if (!(req && req->code == UI_FF_ERASE)) {
584 ff_erase.effect_id = req->u.effect_id;
585 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
591 case UI_END_FF_UPLOAD:
592 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
596 req = uinput_request_find(udev, ff_up.request_id);
597 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
601 req->retval = ff_up.retval;
602 uinput_request_done(udev, req);
605 case UI_END_FF_ERASE:
606 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
610 req = uinput_request_find(udev, ff_erase.request_id);
611 if (!(req && req->code == UI_FF_ERASE)) {
615 req->retval = ff_erase.retval;
616 uinput_request_done(udev, req);
624 mutex_unlock(&udev->mutex);
628 static const struct file_operations uinput_fops = {
629 .owner = THIS_MODULE,
631 .release = uinput_release,
633 .write = uinput_write,
635 .unlocked_ioctl = uinput_ioctl,
638 static struct miscdevice uinput_misc = {
639 .fops = &uinput_fops,
640 .minor = UINPUT_MINOR,
644 static int __init uinput_init(void)
646 return misc_register(&uinput_misc);
649 static void __exit uinput_exit(void)
651 misc_deregister(&uinput_misc);
654 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
655 MODULE_DESCRIPTION("User level driver support for input subsystem");
656 MODULE_LICENSE("GPL");
657 MODULE_VERSION("0.3");
659 module_init(uinput_init);
660 module_exit(uinput_exit);