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_open(struct input_dev *dev)
44 static void uinput_dev_close(struct input_dev *dev)
49 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
51 struct uinput_device *udev;
55 udev->buff[udev->head].type = type;
56 udev->buff[udev->head].code = code;
57 udev->buff[udev->head].value = value;
58 do_gettimeofday(&udev->buff[udev->head].time);
59 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
61 wake_up_interruptible(&udev->waitq);
66 static int uinput_request_alloc_id(struct input_dev *dev, struct uinput_request *request)
68 /* Atomically allocate an ID for the given request. Returns 0 on success. */
69 struct uinput_device *udev = dev->private;
72 down(&udev->requests_sem);
73 for (id=0; id<UINPUT_NUM_REQUESTS; id++)
74 if (!udev->requests[id]) {
75 udev->requests[id] = request;
77 up(&udev->requests_sem);
80 up(&udev->requests_sem);
84 static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
86 /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
87 if (id >= UINPUT_NUM_REQUESTS || id < 0)
89 if (udev->requests[id]->completed)
91 return udev->requests[id];
94 static void uinput_request_init(struct input_dev *dev, struct uinput_request *request, int code)
96 struct uinput_device *udev = dev->private;
98 memset(request, 0, sizeof(struct uinput_request));
100 init_waitqueue_head(&request->waitq);
102 /* Allocate an ID. If none are available right away, wait. */
103 request->retval = wait_event_interruptible(udev->requests_waitq,
104 !uinput_request_alloc_id(dev, request));
107 static void uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
109 struct uinput_device *udev = dev->private;
112 /* Tell our userspace app about this new request by queueing an input event */
113 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
115 /* Wait for the request to complete */
116 retval = wait_event_interruptible(request->waitq, request->completed);
118 request->retval = retval;
120 /* Release this request's ID, let others know it's available */
121 udev->requests[request->id] = NULL;
122 wake_up_interruptible(&udev->requests_waitq);
125 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
127 struct uinput_request request;
129 if (!test_bit(EV_FF, dev->evbit))
132 uinput_request_init(dev, &request, UI_FF_UPLOAD);
134 return request.retval;
135 request.u.effect = effect;
136 uinput_request_submit(dev, &request);
137 return request.retval;
140 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
142 struct uinput_request request;
144 if (!test_bit(EV_FF, dev->evbit))
147 uinput_request_init(dev, &request, UI_FF_ERASE);
149 return request.retval;
150 request.u.effect_id = effect_id;
151 uinput_request_submit(dev, &request);
152 return request.retval;
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->open = uinput_dev_open;
163 udev->dev->close = uinput_dev_close;
164 udev->dev->event = uinput_dev_event;
165 udev->dev->upload_effect = uinput_dev_upload_effect;
166 udev->dev->erase_effect = uinput_dev_erase_effect;
167 udev->dev->private = udev;
169 init_waitqueue_head(&(udev->waitq));
171 input_register_device(udev->dev);
173 set_bit(UIST_CREATED, &(udev->state));
178 static int uinput_destroy_device(struct uinput_device *udev)
180 if (!test_bit(UIST_CREATED, &(udev->state))) {
181 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
185 input_unregister_device(udev->dev);
187 clear_bit(UIST_CREATED, &(udev->state));
192 static int uinput_open(struct inode *inode, struct file *file)
194 struct uinput_device *newdev;
195 struct input_dev *newinput;
197 newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
200 memset(newdev, 0, sizeof(struct uinput_device));
201 init_MUTEX(&newdev->requests_sem);
202 init_waitqueue_head(&newdev->requests_waitq);
204 newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
207 memset(newinput, 0, sizeof(struct input_dev));
209 newdev->dev = newinput;
211 file->private_data = newdev;
220 static int uinput_validate_absbits(struct input_dev *dev)
225 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
226 if (!test_bit(cnt, dev->absbit))
229 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
231 "%s: invalid abs[%02x] min:%d max:%d\n",
233 dev->absmin[cnt], dev->absmax[cnt]);
238 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
240 "%s: absflat[%02x] out of range: %d "
242 UINPUT_NAME, cnt, dev->absflat[cnt],
243 dev->absmin[cnt], dev->absmax[cnt]);
251 static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count)
253 struct uinput_user_dev *user_dev;
254 struct input_dev *dev;
255 struct uinput_device *udev;
261 udev = file->private_data;
264 user_dev = kmalloc(sizeof(*user_dev), GFP_KERNEL);
270 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
275 if (NULL != dev->name)
278 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
279 dev->name = kmalloc(size, GFP_KERNEL);
285 strlcpy(dev->name, user_dev->name, size);
286 dev->id.bustype = user_dev->id.bustype;
287 dev->id.vendor = user_dev->id.vendor;
288 dev->id.product = user_dev->id.product;
289 dev->id.version = user_dev->id.version;
290 dev->ff_effects_max = user_dev->ff_effects_max;
292 size = sizeof(int) * (ABS_MAX + 1);
293 memcpy(dev->absmax, user_dev->absmax, size);
294 memcpy(dev->absmin, user_dev->absmin, size);
295 memcpy(dev->absfuzz, user_dev->absfuzz, size);
296 memcpy(dev->absflat, user_dev->absflat, size);
298 /* check if absmin/absmax/absfuzz/absflat are filled as
299 * told in Documentation/input/input-programming.txt */
300 if (test_bit(EV_ABS, dev->evbit)) {
301 int err = uinput_validate_absbits(dev);
313 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
315 struct uinput_device *udev = file->private_data;
317 if (test_bit(UIST_CREATED, &(udev->state))) {
318 struct input_event ev;
320 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
322 input_event(udev->dev, ev.type, ev.code, ev.value);
325 count = uinput_alloc_device(file, buffer, count);
330 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
332 struct uinput_device *udev = file->private_data;
335 if (!test_bit(UIST_CREATED, &(udev->state)))
338 if ((udev->head == udev->tail) && (file->f_flags & O_NONBLOCK))
341 retval = wait_event_interruptible(udev->waitq,
342 (udev->head != udev->tail) ||
343 !test_bit(UIST_CREATED, &(udev->state)));
348 if (!test_bit(UIST_CREATED, &(udev->state)))
351 while ((udev->head != udev->tail) &&
352 (retval + sizeof(struct input_event) <= count)) {
353 if (copy_to_user(buffer + retval, &(udev->buff[udev->tail]),
354 sizeof(struct input_event))) return -EFAULT;
355 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
356 retval += sizeof(struct input_event);
362 static unsigned int uinput_poll(struct file *file, poll_table *wait)
364 struct uinput_device *udev = file->private_data;
366 poll_wait(file, &udev->waitq, wait);
368 if (udev->head != udev->tail)
369 return POLLIN | POLLRDNORM;
374 static int uinput_burn_device(struct uinput_device *udev)
376 if (test_bit(UIST_CREATED, &(udev->state)))
377 uinput_destroy_device(udev);
379 if (NULL != udev->dev->name)
380 kfree(udev->dev->name);
381 if (NULL != udev->dev->phys)
382 kfree(udev->dev->phys);
390 static int uinput_close(struct inode *inode, struct file *file)
392 return uinput_burn_device(file->private_data);
395 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
398 struct uinput_device *udev;
399 void __user *p = (void __user *)arg;
400 struct uinput_ff_upload ff_up;
401 struct uinput_ff_erase ff_erase;
402 struct uinput_request *req;
405 udev = file->private_data;
407 /* device attributes can not be changed after the device is created */
418 if (test_bit(UIST_CREATED, &(udev->state)))
424 retval = uinput_create_device(udev);
428 retval = uinput_destroy_device(udev);
436 set_bit(arg, udev->dev->evbit);
444 set_bit(arg, udev->dev->keybit);
452 set_bit(arg, udev->dev->relbit);
460 set_bit(arg, udev->dev->absbit);
468 set_bit(arg, udev->dev->mscbit);
476 set_bit(arg, udev->dev->ledbit);
484 set_bit(arg, udev->dev->sndbit);
492 set_bit(arg, udev->dev->ffbit);
496 length = strnlen_user(p, 1024);
501 if (NULL != udev->dev->phys)
502 kfree(udev->dev->phys);
503 udev->dev->phys = kmalloc(length, GFP_KERNEL);
504 if (!udev->dev->phys) {
508 if (copy_from_user(udev->dev->phys, p, length)) {
510 kfree(udev->dev->phys);
511 udev->dev->phys = NULL;
514 udev->dev->phys[length-1] = '\0';
517 case UI_BEGIN_FF_UPLOAD:
518 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
522 req = uinput_request_find(udev, ff_up.request_id);
523 if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
528 memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
529 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
535 case UI_BEGIN_FF_ERASE:
536 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
540 req = uinput_request_find(udev, ff_erase.request_id);
541 if (!(req && req->code==UI_FF_ERASE)) {
546 ff_erase.effect_id = req->u.effect_id;
547 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
553 case UI_END_FF_UPLOAD:
554 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
558 req = uinput_request_find(udev, ff_up.request_id);
559 if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
563 req->retval = ff_up.retval;
564 memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
566 wake_up_interruptible(&req->waitq);
569 case UI_END_FF_ERASE:
570 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
574 req = uinput_request_find(udev, ff_erase.request_id);
575 if (!(req && req->code==UI_FF_ERASE)) {
579 req->retval = ff_erase.retval;
581 wake_up_interruptible(&req->waitq);
590 static struct file_operations uinput_fops = {
591 .owner = THIS_MODULE,
593 .release = uinput_close,
595 .write = uinput_write,
597 .ioctl = uinput_ioctl,
600 static struct miscdevice uinput_misc = {
601 .fops = &uinput_fops,
602 .minor = UINPUT_MINOR,
606 static int __init uinput_init(void)
608 return misc_register(&uinput_misc);
611 static void __exit uinput_exit(void)
613 misc_deregister(&uinput_misc);
616 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
617 MODULE_DESCRIPTION("User level driver support for input subsystem");
618 MODULE_LICENSE("GPL");
620 module_init(uinput_init);
621 module_exit(uinput_exit);