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 int uinput_create_device(struct uinput_device *udev)
157 if (!udev->dev->name) {
158 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
162 udev->dev->event = uinput_dev_event;
163 udev->dev->upload_effect = uinput_dev_upload_effect;
164 udev->dev->erase_effect = uinput_dev_erase_effect;
165 udev->dev->private = udev;
167 init_waitqueue_head(&udev->waitq);
169 input_register_device(udev->dev);
171 set_bit(UIST_CREATED, &udev->state);
176 static int uinput_destroy_device(struct uinput_device *udev)
178 if (!test_bit(UIST_CREATED, &udev->state)) {
179 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
183 input_unregister_device(udev->dev);
185 clear_bit(UIST_CREATED, &udev->state);
190 static int uinput_open(struct inode *inode, struct file *file)
192 struct uinput_device *newdev;
193 struct input_dev *newinput;
195 newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
198 memset(newdev, 0, sizeof(struct uinput_device));
199 spin_lock_init(&newdev->requests_lock);
200 init_waitqueue_head(&newdev->requests_waitq);
202 newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
205 memset(newinput, 0, sizeof(struct input_dev));
207 newdev->dev = newinput;
209 file->private_data = newdev;
218 static int uinput_validate_absbits(struct input_dev *dev)
223 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
224 if (!test_bit(cnt, dev->absbit))
227 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
229 "%s: invalid abs[%02x] min:%d max:%d\n",
231 dev->absmin[cnt], dev->absmax[cnt]);
236 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
238 "%s: absflat[%02x] out of range: %d "
240 UINPUT_NAME, cnt, dev->absflat[cnt],
241 dev->absmin[cnt], dev->absmax[cnt]);
249 static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count)
251 struct uinput_user_dev *user_dev;
252 struct input_dev *dev;
253 struct uinput_device *udev;
260 udev = file->private_data;
263 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
269 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
277 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
278 dev->name = name = kmalloc(size, GFP_KERNEL);
283 strlcpy(name, user_dev->name, size);
285 dev->id.bustype = user_dev->id.bustype;
286 dev->id.vendor = user_dev->id.vendor;
287 dev->id.product = user_dev->id.product;
288 dev->id.version = user_dev->id.version;
289 dev->ff_effects_max = user_dev->ff_effects_max;
291 size = sizeof(int) * (ABS_MAX + 1);
292 memcpy(dev->absmax, user_dev->absmax, size);
293 memcpy(dev->absmin, user_dev->absmin, size);
294 memcpy(dev->absfuzz, user_dev->absfuzz, size);
295 memcpy(dev->absflat, user_dev->absflat, size);
297 /* check if absmin/absmax/absfuzz/absflat are filled as
298 * told in Documentation/input/input-programming.txt */
299 if (test_bit(EV_ABS, dev->evbit)) {
300 int err = uinput_validate_absbits(dev);
312 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
314 struct uinput_device *udev = file->private_data;
316 if (test_bit(UIST_CREATED, &udev->state)) {
317 struct input_event ev;
319 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
321 input_event(udev->dev, ev.type, ev.code, ev.value);
323 count = uinput_alloc_device(file, buffer, count);
328 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
330 struct uinput_device *udev = file->private_data;
333 if (!test_bit(UIST_CREATED, &udev->state))
336 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
339 retval = wait_event_interruptible(udev->waitq,
340 udev->head != udev->tail || !test_bit(UIST_CREATED, &udev->state));
344 if (!test_bit(UIST_CREATED, &udev->state))
347 while ((udev->head != udev->tail) &&
348 (retval + sizeof(struct input_event) <= count)) {
349 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event)))
351 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
352 retval += sizeof(struct input_event);
358 static unsigned int uinput_poll(struct file *file, poll_table *wait)
360 struct uinput_device *udev = file->private_data;
362 poll_wait(file, &udev->waitq, wait);
364 if (udev->head != udev->tail)
365 return POLLIN | POLLRDNORM;
370 static int uinput_burn_device(struct uinput_device *udev)
372 if (test_bit(UIST_CREATED, &udev->state))
373 uinput_destroy_device(udev);
376 kfree(udev->dev->name);
378 kfree(udev->dev->phys);
386 static int uinput_close(struct inode *inode, struct file *file)
388 uinput_burn_device(file->private_data);
392 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
395 struct uinput_device *udev;
396 void __user *p = (void __user *)arg;
397 struct uinput_ff_upload ff_up;
398 struct uinput_ff_erase ff_erase;
399 struct uinput_request *req;
403 udev = file->private_data;
405 /* device attributes can not be changed after the device is created */
416 if (test_bit(UIST_CREATED, &udev->state))
422 retval = uinput_create_device(udev);
426 retval = uinput_destroy_device(udev);
434 set_bit(arg, udev->dev->evbit);
442 set_bit(arg, udev->dev->keybit);
450 set_bit(arg, udev->dev->relbit);
458 set_bit(arg, udev->dev->absbit);
466 set_bit(arg, udev->dev->mscbit);
474 set_bit(arg, udev->dev->ledbit);
482 set_bit(arg, udev->dev->sndbit);
490 set_bit(arg, udev->dev->ffbit);
494 length = strnlen_user(p, 1024);
499 kfree(udev->dev->phys);
500 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
505 if (copy_from_user(phys, p, length)) {
506 udev->dev->phys = NULL;
511 phys[length - 1] = '\0';
514 case UI_BEGIN_FF_UPLOAD:
515 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
519 req = uinput_request_find(udev, ff_up.request_id);
520 if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
525 memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
526 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
532 case UI_BEGIN_FF_ERASE:
533 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
537 req = uinput_request_find(udev, ff_erase.request_id);
538 if (!(req && req->code == UI_FF_ERASE)) {
543 ff_erase.effect_id = req->u.effect_id;
544 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
550 case UI_END_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.effect)) {
560 req->retval = ff_up.retval;
561 memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
562 uinput_request_done(udev, req);
565 case UI_END_FF_ERASE:
566 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
570 req = uinput_request_find(udev, ff_erase.request_id);
571 if (!(req && req->code == UI_FF_ERASE)) {
575 req->retval = ff_erase.retval;
576 uinput_request_done(udev, req);
585 static struct file_operations uinput_fops = {
586 .owner = THIS_MODULE,
588 .release = uinput_close,
590 .write = uinput_write,
592 .ioctl = uinput_ioctl,
595 static struct miscdevice uinput_misc = {
596 .fops = &uinput_fops,
597 .minor = UINPUT_MINOR,
601 static int __init uinput_init(void)
603 return misc_register(&uinput_misc);
606 static void __exit uinput_exit(void)
608 misc_deregister(&uinput_misc);
611 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
612 MODULE_DESCRIPTION("User level driver support for input subsystem");
613 MODULE_LICENSE("GPL");
615 module_init(uinput_init);
616 module_exit(uinput_exit);