[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / drivers / input / misc / uinput.c
blob158c8e845ff988a690c79f3f921e6ea39250df25
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.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
24 * - added force feedback support
25 * - added UI_SET_PHYS
26 * 0.1 20/06/2002
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>
35 #include <linux/fs.h>
36 #include <linux/miscdevice.h>
37 #include <linux/uinput.h>
39 static int uinput_dev_open(struct input_dev *dev)
41 return 0;
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;
53 udev = dev->private;
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);
63 return 0;
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;
70 int id;
72 down(&udev->requests_sem);
73 for (id=0; id<UINPUT_NUM_REQUESTS; id++)
74 if (!udev->requests[id]) {
75 udev->requests[id] = request;
76 request->id = id;
77 up(&udev->requests_sem);
78 return 0;
80 up(&udev->requests_sem);
81 return -1;
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)
88 return NULL;
89 if (udev->requests[id]->completed)
90 return NULL;
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));
99 request->code = code;
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;
110 int retval;
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);
117 if (retval)
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))
130 return -ENOSYS;
132 uinput_request_init(dev, &request, UI_FF_UPLOAD);
133 if (request.retval)
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))
145 return -ENOSYS;
147 uinput_request_init(dev, &request, UI_FF_ERASE);
148 if (request.retval)
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);
159 return -EINVAL;
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));
175 return 0;
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);
182 return -EINVAL;
185 input_unregister_device(udev->dev);
187 clear_bit(UIST_CREATED, &(udev->state));
189 return 0;
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);
198 if (!newdev)
199 goto error;
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);
205 if (!newinput)
206 goto cleanup;
207 memset(newinput, 0, sizeof(struct input_dev));
209 newdev->dev = newinput;
211 file->private_data = newdev;
213 return 0;
214 cleanup:
215 kfree(newdev);
216 error:
217 return -ENOMEM;
220 static int uinput_validate_absbits(struct input_dev *dev)
222 unsigned int cnt;
223 int retval = 0;
225 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
226 if (!test_bit(cnt, dev->absbit))
227 continue;
229 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
230 printk(KERN_DEBUG
231 "%s: invalid abs[%02x] min:%d max:%d\n",
232 UINPUT_NAME, cnt,
233 dev->absmin[cnt], dev->absmax[cnt]);
234 retval = -EINVAL;
235 break;
238 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
239 printk(KERN_DEBUG
240 "%s: absflat[%02x] out of range: %d "
241 "(min:%d/max:%d)\n",
242 UINPUT_NAME, cnt, dev->absflat[cnt],
243 dev->absmin[cnt], dev->absmax[cnt]);
244 retval = -EINVAL;
245 break;
248 return retval;
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;
256 int size,
257 retval;
259 retval = count;
261 udev = file->private_data;
262 dev = udev->dev;
264 user_dev = kmalloc(sizeof(*user_dev), GFP_KERNEL);
265 if (!user_dev) {
266 retval = -ENOMEM;
267 goto exit;
270 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
271 retval = -EFAULT;
272 goto exit;
275 if (NULL != dev->name)
276 kfree(dev->name);
278 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
279 dev->name = kmalloc(size, GFP_KERNEL);
280 if (!dev->name) {
281 retval = -ENOMEM;
282 goto exit;
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 retval = uinput_validate_absbits(dev);
302 if (retval < 0)
303 kfree(dev->name);
306 exit:
307 kfree(user_dev);
308 return retval;
311 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
313 struct uinput_device *udev = file->private_data;
315 if (test_bit(UIST_CREATED, &(udev->state))) {
316 struct input_event ev;
318 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
319 return -EFAULT;
320 input_event(udev->dev, ev.type, ev.code, ev.value);
322 else
323 count = uinput_alloc_device(file, buffer, count);
325 return 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;
331 int retval = 0;
333 if (!test_bit(UIST_CREATED, &(udev->state)))
334 return -ENODEV;
336 if ((udev->head == udev->tail) && (file->f_flags & O_NONBLOCK))
337 return -EAGAIN;
339 retval = wait_event_interruptible(udev->waitq,
340 (udev->head != udev->tail) ||
341 !test_bit(UIST_CREATED, &(udev->state)));
343 if (retval)
344 return retval;
346 if (!test_bit(UIST_CREATED, &(udev->state)))
347 return -ENODEV;
349 while ((udev->head != udev->tail) &&
350 (retval + sizeof(struct input_event) <= count)) {
351 if (copy_to_user(buffer + retval, &(udev->buff[udev->tail]),
352 sizeof(struct input_event))) return -EFAULT;
353 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
354 retval += sizeof(struct input_event);
357 return retval;
360 static unsigned int uinput_poll(struct file *file, poll_table *wait)
362 struct uinput_device *udev = file->private_data;
364 poll_wait(file, &udev->waitq, wait);
366 if (udev->head != udev->tail)
367 return POLLIN | POLLRDNORM;
369 return 0;
372 static int uinput_burn_device(struct uinput_device *udev)
374 if (test_bit(UIST_CREATED, &(udev->state)))
375 uinput_destroy_device(udev);
377 if (NULL != udev->dev->name)
378 kfree(udev->dev->name);
379 if (NULL != udev->dev->phys)
380 kfree(udev->dev->phys);
382 kfree(udev->dev);
383 kfree(udev);
385 return 0;
388 static int uinput_close(struct inode *inode, struct file *file)
390 return uinput_burn_device(file->private_data);
393 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
395 int retval = 0;
396 struct uinput_device *udev;
397 void __user *p = (void __user *)arg;
398 struct uinput_ff_upload ff_up;
399 struct uinput_ff_erase ff_erase;
400 struct uinput_request *req;
401 int length;
403 udev = file->private_data;
405 /* device attributes can not be changed after the device is created */
406 switch (cmd) {
407 case UI_SET_EVBIT:
408 case UI_SET_KEYBIT:
409 case UI_SET_RELBIT:
410 case UI_SET_ABSBIT:
411 case UI_SET_MSCBIT:
412 case UI_SET_LEDBIT:
413 case UI_SET_SNDBIT:
414 case UI_SET_FFBIT:
415 case UI_SET_PHYS:
416 if (test_bit(UIST_CREATED, &(udev->state)))
417 return -EINVAL;
420 switch (cmd) {
421 case UI_DEV_CREATE:
422 retval = uinput_create_device(udev);
423 break;
425 case UI_DEV_DESTROY:
426 retval = uinput_destroy_device(udev);
427 break;
429 case UI_SET_EVBIT:
430 if (arg > EV_MAX) {
431 retval = -EINVAL;
432 break;
434 set_bit(arg, udev->dev->evbit);
435 break;
437 case UI_SET_KEYBIT:
438 if (arg > KEY_MAX) {
439 retval = -EINVAL;
440 break;
442 set_bit(arg, udev->dev->keybit);
443 break;
445 case UI_SET_RELBIT:
446 if (arg > REL_MAX) {
447 retval = -EINVAL;
448 break;
450 set_bit(arg, udev->dev->relbit);
451 break;
453 case UI_SET_ABSBIT:
454 if (arg > ABS_MAX) {
455 retval = -EINVAL;
456 break;
458 set_bit(arg, udev->dev->absbit);
459 break;
461 case UI_SET_MSCBIT:
462 if (arg > MSC_MAX) {
463 retval = -EINVAL;
464 break;
466 set_bit(arg, udev->dev->mscbit);
467 break;
469 case UI_SET_LEDBIT:
470 if (arg > LED_MAX) {
471 retval = -EINVAL;
472 break;
474 set_bit(arg, udev->dev->ledbit);
475 break;
477 case UI_SET_SNDBIT:
478 if (arg > SND_MAX) {
479 retval = -EINVAL;
480 break;
482 set_bit(arg, udev->dev->sndbit);
483 break;
485 case UI_SET_FFBIT:
486 if (arg > FF_MAX) {
487 retval = -EINVAL;
488 break;
490 set_bit(arg, udev->dev->ffbit);
491 break;
493 case UI_SET_PHYS:
494 length = strnlen_user(p, 1024);
495 if (length <= 0) {
496 retval = -EFAULT;
497 break;
499 if (NULL != udev->dev->phys)
500 kfree(udev->dev->phys);
501 udev->dev->phys = kmalloc(length, GFP_KERNEL);
502 if (!udev->dev->phys) {
503 retval = -ENOMEM;
504 break;
506 if (copy_from_user(udev->dev->phys, p, length)) {
507 retval = -EFAULT;
508 kfree(udev->dev->phys);
509 udev->dev->phys = NULL;
510 break;
512 udev->dev->phys[length-1] = '\0';
513 break;
515 case UI_BEGIN_FF_UPLOAD:
516 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
517 retval = -EFAULT;
518 break;
520 req = uinput_request_find(udev, ff_up.request_id);
521 if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
522 retval = -EINVAL;
523 break;
525 ff_up.retval = 0;
526 memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
527 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
528 retval = -EFAULT;
529 break;
531 break;
533 case UI_BEGIN_FF_ERASE:
534 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
535 retval = -EFAULT;
536 break;
538 req = uinput_request_find(udev, ff_erase.request_id);
539 if (!(req && req->code==UI_FF_ERASE)) {
540 retval = -EINVAL;
541 break;
543 ff_erase.retval = 0;
544 ff_erase.effect_id = req->u.effect_id;
545 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
546 retval = -EFAULT;
547 break;
549 break;
551 case UI_END_FF_UPLOAD:
552 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
553 retval = -EFAULT;
554 break;
556 req = uinput_request_find(udev, ff_up.request_id);
557 if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
558 retval = -EINVAL;
559 break;
561 req->retval = ff_up.retval;
562 memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
563 req->completed = 1;
564 wake_up_interruptible(&req->waitq);
565 break;
567 case UI_END_FF_ERASE:
568 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
569 retval = -EFAULT;
570 break;
572 req = uinput_request_find(udev, ff_erase.request_id);
573 if (!(req && req->code==UI_FF_ERASE)) {
574 retval = -EINVAL;
575 break;
577 req->retval = ff_erase.retval;
578 req->completed = 1;
579 wake_up_interruptible(&req->waitq);
580 break;
582 default:
583 retval = -EINVAL;
585 return retval;
588 static struct file_operations uinput_fops = {
589 .owner = THIS_MODULE,
590 .open = uinput_open,
591 .release = uinput_close,
592 .read = uinput_read,
593 .write = uinput_write,
594 .poll = uinput_poll,
595 .ioctl = uinput_ioctl,
598 static struct miscdevice uinput_misc = {
599 .fops = &uinput_fops,
600 .minor = UINPUT_MINOR,
601 .name = UINPUT_NAME,
604 static int __init uinput_init(void)
606 return misc_register(&uinput_misc);
609 static void __exit uinput_exit(void)
611 misc_deregister(&uinput_misc);
614 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
615 MODULE_DESCRIPTION("User level driver support for input subsystem");
616 MODULE_LICENSE("GPL");
618 module_init(uinput_init);
619 module_exit(uinput_exit);