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.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
24 * - added force feedback support
27 * - first public version
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>
36 #include <linux/miscdevice.h>
37 #include <linux/uinput.h>
39 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
41 struct uinput_device *udev;
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;
51 wake_up_interruptible(&udev->waitq);
56 static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
58 /* Atomically allocate an ID for the given request. Returns 0 on success. */
62 spin_lock(&udev->requests_lock);
64 for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
65 if (!udev->requests[id]) {
67 udev->requests[id] = request;
72 spin_unlock(&udev->requests_lock);
76 static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
78 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
79 if (id >= UINPUT_NUM_REQUESTS || id < 0)
81 return udev->requests[id];
84 static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
86 /* Allocate slot. If none are available right away, wait. */
87 return wait_event_interruptible(udev->requests_waitq,
88 !uinput_request_alloc_id(udev, request));
91 static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
93 /* Mark slot as available */
94 udev->requests[request->id] = NULL;
95 wake_up_interruptible(&udev->requests_waitq);
97 complete(&request->done);
100 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 retval = wait_for_completion_interruptible(&request->done);
110 retval = request->retval;
115 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
117 struct uinput_request request;
120 if (!test_bit(EV_FF, dev->evbit))
124 init_completion(&request.done);
125 request.code = UI_FF_UPLOAD;
126 request.u.effect = effect;
128 retval = uinput_request_reserve_slot(dev->private, &request);
130 retval = uinput_request_submit(dev, &request);
135 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
137 struct uinput_request request;
140 if (!test_bit(EV_FF, dev->evbit))
144 init_completion(&request.done);
145 request.code = UI_FF_ERASE;
146 request.u.effect_id = effect_id;
148 retval = uinput_request_reserve_slot(dev->private, &request);
150 retval = uinput_request_submit(dev, &request);
155 static void uinput_destroy_device(struct uinput_device *udev)
157 const char *name, *phys;
160 name = udev->dev->name;
161 phys = udev->dev->phys;
162 if (udev->state == UIST_CREATED)
163 input_unregister_device(udev->dev);
165 input_free_device(udev->dev);
171 udev->state = UIST_NEW_DEVICE;
174 static int uinput_create_device(struct uinput_device *udev)
178 if (udev->state != UIST_SETUP_COMPLETE) {
179 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
183 error = input_register_device(udev->dev);
185 uinput_destroy_device(udev);
189 udev->state = UIST_CREATED;
194 static int uinput_open(struct inode *inode, struct file *file)
196 struct uinput_device *newdev;
198 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
202 init_MUTEX(&newdev->sem);
203 spin_lock_init(&newdev->requests_lock);
204 init_waitqueue_head(&newdev->requests_waitq);
205 init_waitqueue_head(&newdev->waitq);
206 newdev->state = UIST_NEW_DEVICE;
208 file->private_data = newdev;
213 static int uinput_validate_absbits(struct input_dev *dev)
218 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
219 if (!test_bit(cnt, dev->absbit))
222 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
224 "%s: invalid abs[%02x] min:%d max:%d\n",
226 dev->absmin[cnt], dev->absmax[cnt]);
231 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
233 "%s: absflat[%02x] out of range: %d "
235 UINPUT_NAME, cnt, dev->absflat[cnt],
236 dev->absmin[cnt], dev->absmax[cnt]);
244 static int uinput_allocate_device(struct uinput_device *udev)
246 udev->dev = input_allocate_device();
250 udev->dev->event = uinput_dev_event;
251 udev->dev->upload_effect = uinput_dev_upload_effect;
252 udev->dev->erase_effect = uinput_dev_erase_effect;
253 udev->dev->private = udev;
258 static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
260 struct uinput_user_dev *user_dev;
261 struct input_dev *dev;
266 if (count != sizeof(struct uinput_user_dev))
270 retval = uinput_allocate_device(udev);
277 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
281 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
286 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
293 dev->name = name = kmalloc(size, GFP_KERNEL);
298 strlcpy(name, user_dev->name, size);
300 dev->id.bustype = user_dev->id.bustype;
301 dev->id.vendor = user_dev->id.vendor;
302 dev->id.product = user_dev->id.product;
303 dev->id.version = user_dev->id.version;
304 dev->ff_effects_max = user_dev->ff_effects_max;
306 size = sizeof(int) * (ABS_MAX + 1);
307 memcpy(dev->absmax, user_dev->absmax, size);
308 memcpy(dev->absmin, user_dev->absmin, size);
309 memcpy(dev->absfuzz, user_dev->absfuzz, size);
310 memcpy(dev->absflat, user_dev->absflat, size);
312 /* check if absmin/absmax/absfuzz/absflat are filled as
313 * told in Documentation/input/input-programming.txt */
314 if (test_bit(EV_ABS, dev->evbit)) {
315 retval = uinput_validate_absbits(dev);
320 udev->state = UIST_SETUP_COMPLETE;
328 static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
330 struct input_event ev;
332 if (count != sizeof(struct input_event))
335 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
338 input_event(udev->dev, ev.type, ev.code, ev.value);
340 return sizeof(struct input_event);
343 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
345 struct uinput_device *udev = file->private_data;
348 retval = down_interruptible(&udev->sem);
352 retval = udev->state == UIST_CREATED ?
353 uinput_inject_event(udev, buffer, count) :
354 uinput_setup_device(udev, buffer, count);
361 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
363 struct uinput_device *udev = file->private_data;
366 if (udev->state != UIST_CREATED)
369 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
372 retval = wait_event_interruptible(udev->waitq,
373 udev->head != udev->tail || udev->state != UIST_CREATED);
377 retval = down_interruptible(&udev->sem);
381 if (udev->state != UIST_CREATED) {
386 while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
387 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
391 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
392 retval += sizeof(struct input_event);
401 static unsigned int uinput_poll(struct file *file, poll_table *wait)
403 struct uinput_device *udev = file->private_data;
405 poll_wait(file, &udev->waitq, wait);
407 if (udev->head != udev->tail)
408 return POLLIN | POLLRDNORM;
413 static int uinput_release(struct inode *inode, struct file *file)
415 struct uinput_device *udev = file->private_data;
417 uinput_destroy_device(udev);
423 #define uinput_set_bit(_arg, _bit, _max) \
426 if (udev->state == UIST_CREATED) \
428 else if ((_arg) > (_max)) \
430 else set_bit((_arg), udev->dev->_bit); \
434 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
437 struct uinput_device *udev;
438 void __user *p = (void __user *)arg;
439 struct uinput_ff_upload ff_up;
440 struct uinput_ff_erase ff_erase;
441 struct uinput_request *req;
445 udev = file->private_data;
447 retval = down_interruptible(&udev->sem);
452 retval = uinput_allocate_device(udev);
459 retval = uinput_create_device(udev);
463 uinput_destroy_device(udev);
467 retval = uinput_set_bit(arg, evbit, EV_MAX);
471 retval = uinput_set_bit(arg, keybit, KEY_MAX);
475 retval = uinput_set_bit(arg, relbit, REL_MAX);
479 retval = uinput_set_bit(arg, absbit, ABS_MAX);
483 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
487 retval = uinput_set_bit(arg, ledbit, LED_MAX);
491 retval = uinput_set_bit(arg, sndbit, SND_MAX);
495 retval = uinput_set_bit(arg, ffbit, FF_MAX);
499 retval = uinput_set_bit(arg, swbit, SW_MAX);
503 if (udev->state == UIST_CREATED) {
507 length = strnlen_user(p, 1024);
512 kfree(udev->dev->phys);
513 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
518 if (copy_from_user(phys, p, length)) {
519 udev->dev->phys = NULL;
524 phys[length - 1] = '\0';
527 case UI_BEGIN_FF_UPLOAD:
528 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
532 req = uinput_request_find(udev, ff_up.request_id);
533 if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
538 memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
539 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
545 case UI_BEGIN_FF_ERASE:
546 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
550 req = uinput_request_find(udev, ff_erase.request_id);
551 if (!(req && req->code == UI_FF_ERASE)) {
556 ff_erase.effect_id = req->u.effect_id;
557 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
563 case UI_END_FF_UPLOAD:
564 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
568 req = uinput_request_find(udev, ff_up.request_id);
569 if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
573 req->retval = ff_up.retval;
574 memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
575 uinput_request_done(udev, req);
578 case UI_END_FF_ERASE:
579 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
583 req = uinput_request_find(udev, ff_erase.request_id);
584 if (!(req && req->code == UI_FF_ERASE)) {
588 req->retval = ff_erase.retval;
589 uinput_request_done(udev, req);
601 static struct file_operations uinput_fops = {
602 .owner = THIS_MODULE,
604 .release = uinput_release,
606 .write = uinput_write,
608 .unlocked_ioctl = uinput_ioctl,
611 static struct miscdevice uinput_misc = {
612 .fops = &uinput_fops,
613 .minor = UINPUT_MINOR,
617 static int __init uinput_init(void)
619 return misc_register(&uinput_misc);
622 static void __exit uinput_exit(void)
624 misc_deregister(&uinput_misc);
627 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
628 MODULE_DESCRIPTION("User level driver support for input subsystem");
629 MODULE_LICENSE("GPL");
631 module_init(uinput_init);
632 module_exit(uinput_exit);