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/smp_lock.h>
38 #include <linux/miscdevice.h>
39 #include <linux/uinput.h>
40 #include "../input-compat.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)
83 return udev
->requests
[id
];
86 static inline int uinput_request_reserve_slot(struct uinput_device
*udev
, struct uinput_request
*request
)
88 /* Allocate slot. If none are available right away, wait. */
89 return wait_event_interruptible(udev
->requests_waitq
,
90 !uinput_request_alloc_id(udev
, request
));
93 static void uinput_request_done(struct uinput_device
*udev
, struct uinput_request
*request
)
95 /* Mark slot as available */
96 udev
->requests
[request
->id
] = NULL
;
97 wake_up(&udev
->requests_waitq
);
99 complete(&request
->done
);
102 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 wait_for_completion(&request
->done
);
109 return request
->retval
;
112 static void uinput_dev_set_gain(struct input_dev
*dev
, u16 gain
)
114 uinput_dev_event(dev
, EV_FF
, FF_GAIN
, gain
);
117 static void uinput_dev_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
119 uinput_dev_event(dev
, EV_FF
, FF_AUTOCENTER
, magnitude
);
122 static int uinput_dev_playback(struct input_dev
*dev
, int effect_id
, int value
)
124 return uinput_dev_event(dev
, EV_FF
, effect_id
, value
);
127 static int uinput_dev_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
129 struct uinput_request request
;
133 * uinput driver does not currently support periodic effects with
134 * custom waveform since it does not have a way to pass buffer of
135 * samples (custom_data) to userspace. If ever there is a device
136 * supporting custom waveforms we would need to define an additional
137 * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
139 if (effect
->type
== FF_PERIODIC
&&
140 effect
->u
.periodic
.waveform
== FF_CUSTOM
)
144 init_completion(&request
.done
);
145 request
.code
= UI_FF_UPLOAD
;
146 request
.u
.upload
.effect
= effect
;
147 request
.u
.upload
.old
= old
;
149 retval
= uinput_request_reserve_slot(input_get_drvdata(dev
), &request
);
151 retval
= uinput_request_submit(dev
, &request
);
156 static int uinput_dev_erase_effect(struct input_dev
*dev
, int effect_id
)
158 struct uinput_request request
;
161 if (!test_bit(EV_FF
, dev
->evbit
))
165 init_completion(&request
.done
);
166 request
.code
= UI_FF_ERASE
;
167 request
.u
.effect_id
= effect_id
;
169 retval
= uinput_request_reserve_slot(input_get_drvdata(dev
), &request
);
171 retval
= uinput_request_submit(dev
, &request
);
176 static void uinput_destroy_device(struct uinput_device
*udev
)
178 const char *name
, *phys
;
181 name
= udev
->dev
->name
;
182 phys
= udev
->dev
->phys
;
183 if (udev
->state
== UIST_CREATED
)
184 input_unregister_device(udev
->dev
);
186 input_free_device(udev
->dev
);
192 udev
->state
= UIST_NEW_DEVICE
;
195 static int uinput_create_device(struct uinput_device
*udev
)
197 struct input_dev
*dev
= udev
->dev
;
200 if (udev
->state
!= UIST_SETUP_COMPLETE
) {
201 printk(KERN_DEBUG
"%s: write device info first\n", UINPUT_NAME
);
205 if (udev
->ff_effects_max
) {
206 error
= input_ff_create(dev
, udev
->ff_effects_max
);
210 dev
->ff
->upload
= uinput_dev_upload_effect
;
211 dev
->ff
->erase
= uinput_dev_erase_effect
;
212 dev
->ff
->playback
= uinput_dev_playback
;
213 dev
->ff
->set_gain
= uinput_dev_set_gain
;
214 dev
->ff
->set_autocenter
= uinput_dev_set_autocenter
;
217 error
= input_register_device(udev
->dev
);
221 udev
->state
= UIST_CREATED
;
225 fail2
: input_ff_destroy(dev
);
226 fail1
: uinput_destroy_device(udev
);
230 static int uinput_open(struct inode
*inode
, struct file
*file
)
232 struct uinput_device
*newdev
;
234 newdev
= kzalloc(sizeof(struct uinput_device
), GFP_KERNEL
);
239 mutex_init(&newdev
->mutex
);
240 spin_lock_init(&newdev
->requests_lock
);
241 init_waitqueue_head(&newdev
->requests_waitq
);
242 init_waitqueue_head(&newdev
->waitq
);
243 newdev
->state
= UIST_NEW_DEVICE
;
245 file
->private_data
= newdev
;
251 static int uinput_validate_absbits(struct input_dev
*dev
)
256 for (cnt
= 0; cnt
< ABS_MAX
+ 1; cnt
++) {
257 if (!test_bit(cnt
, dev
->absbit
))
260 if ((dev
->absmax
[cnt
] <= dev
->absmin
[cnt
])) {
262 "%s: invalid abs[%02x] min:%d max:%d\n",
264 dev
->absmin
[cnt
], dev
->absmax
[cnt
]);
269 if (dev
->absflat
[cnt
] > (dev
->absmax
[cnt
] - dev
->absmin
[cnt
])) {
271 "%s: absflat[%02x] out of range: %d "
273 UINPUT_NAME
, cnt
, dev
->absflat
[cnt
],
274 dev
->absmin
[cnt
], dev
->absmax
[cnt
]);
282 static int uinput_allocate_device(struct uinput_device
*udev
)
284 udev
->dev
= input_allocate_device();
288 udev
->dev
->event
= uinput_dev_event
;
289 input_set_drvdata(udev
->dev
, udev
);
294 static int uinput_setup_device(struct uinput_device
*udev
, const char __user
*buffer
, size_t count
)
296 struct uinput_user_dev
*user_dev
;
297 struct input_dev
*dev
;
302 if (count
!= sizeof(struct uinput_user_dev
))
306 retval
= uinput_allocate_device(udev
);
313 user_dev
= kmalloc(sizeof(struct uinput_user_dev
), GFP_KERNEL
);
317 if (copy_from_user(user_dev
, buffer
, sizeof(struct uinput_user_dev
))) {
322 udev
->ff_effects_max
= user_dev
->ff_effects_max
;
324 size
= strnlen(user_dev
->name
, UINPUT_MAX_NAME_SIZE
) + 1;
331 dev
->name
= name
= kmalloc(size
, GFP_KERNEL
);
336 strlcpy(name
, user_dev
->name
, size
);
338 dev
->id
.bustype
= user_dev
->id
.bustype
;
339 dev
->id
.vendor
= user_dev
->id
.vendor
;
340 dev
->id
.product
= user_dev
->id
.product
;
341 dev
->id
.version
= user_dev
->id
.version
;
343 size
= sizeof(int) * (ABS_MAX
+ 1);
344 memcpy(dev
->absmax
, user_dev
->absmax
, size
);
345 memcpy(dev
->absmin
, user_dev
->absmin
, size
);
346 memcpy(dev
->absfuzz
, user_dev
->absfuzz
, size
);
347 memcpy(dev
->absflat
, user_dev
->absflat
, size
);
349 /* check if absmin/absmax/absfuzz/absflat are filled as
350 * told in Documentation/input/input-programming.txt */
351 if (test_bit(EV_ABS
, dev
->evbit
)) {
352 retval
= uinput_validate_absbits(dev
);
357 udev
->state
= UIST_SETUP_COMPLETE
;
365 static inline ssize_t
uinput_inject_event(struct uinput_device
*udev
, const char __user
*buffer
, size_t count
)
367 struct input_event ev
;
369 if (count
< input_event_size())
372 if (input_event_from_user(buffer
, &ev
))
375 input_event(udev
->dev
, ev
.type
, ev
.code
, ev
.value
);
377 return input_event_size();
380 static ssize_t
uinput_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
382 struct uinput_device
*udev
= file
->private_data
;
385 retval
= mutex_lock_interruptible(&udev
->mutex
);
389 retval
= udev
->state
== UIST_CREATED
?
390 uinput_inject_event(udev
, buffer
, count
) :
391 uinput_setup_device(udev
, buffer
, count
);
393 mutex_unlock(&udev
->mutex
);
398 static ssize_t
uinput_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
400 struct uinput_device
*udev
= file
->private_data
;
403 if (udev
->state
!= UIST_CREATED
)
406 if (udev
->head
== udev
->tail
&& (file
->f_flags
& O_NONBLOCK
))
409 retval
= wait_event_interruptible(udev
->waitq
,
410 udev
->head
!= udev
->tail
|| udev
->state
!= UIST_CREATED
);
414 retval
= mutex_lock_interruptible(&udev
->mutex
);
418 if (udev
->state
!= UIST_CREATED
) {
423 while (udev
->head
!= udev
->tail
&& retval
+ input_event_size() <= count
) {
424 if (input_event_to_user(buffer
+ retval
, &udev
->buff
[udev
->tail
])) {
428 udev
->tail
= (udev
->tail
+ 1) % UINPUT_BUFFER_SIZE
;
429 retval
+= input_event_size();
433 mutex_unlock(&udev
->mutex
);
438 static unsigned int uinput_poll(struct file
*file
, poll_table
*wait
)
440 struct uinput_device
*udev
= file
->private_data
;
442 poll_wait(file
, &udev
->waitq
, wait
);
444 if (udev
->head
!= udev
->tail
)
445 return POLLIN
| POLLRDNORM
;
450 static int uinput_release(struct inode
*inode
, struct file
*file
)
452 struct uinput_device
*udev
= file
->private_data
;
454 uinput_destroy_device(udev
);
461 struct uinput_ff_upload_compat
{
464 struct ff_effect_compat effect
;
465 struct ff_effect_compat old
;
468 static int uinput_ff_upload_to_user(char __user
*buffer
,
469 const struct uinput_ff_upload
*ff_up
)
471 if (INPUT_COMPAT_TEST
) {
472 struct uinput_ff_upload_compat ff_up_compat
;
474 ff_up_compat
.request_id
= ff_up
->request_id
;
475 ff_up_compat
.retval
= ff_up
->retval
;
477 * It so happens that the pointer that gives us the trouble
478 * is the last field in the structure. Since we don't support
479 * custom waveforms in uinput anyway we can just copy the whole
480 * thing (to the compat size) and ignore the pointer.
482 memcpy(&ff_up_compat
.effect
, &ff_up
->effect
,
483 sizeof(struct ff_effect_compat
));
484 memcpy(&ff_up_compat
.old
, &ff_up
->old
,
485 sizeof(struct ff_effect_compat
));
487 if (copy_to_user(buffer
, &ff_up_compat
,
488 sizeof(struct uinput_ff_upload_compat
)))
491 if (copy_to_user(buffer
, ff_up
,
492 sizeof(struct uinput_ff_upload
)))
499 static int uinput_ff_upload_from_user(const char __user
*buffer
,
500 struct uinput_ff_upload
*ff_up
)
502 if (INPUT_COMPAT_TEST
) {
503 struct uinput_ff_upload_compat ff_up_compat
;
505 if (copy_from_user(&ff_up_compat
, buffer
,
506 sizeof(struct uinput_ff_upload_compat
)))
509 ff_up
->request_id
= ff_up_compat
.request_id
;
510 ff_up
->retval
= ff_up_compat
.retval
;
511 memcpy(&ff_up
->effect
, &ff_up_compat
.effect
,
512 sizeof(struct ff_effect_compat
));
513 memcpy(&ff_up
->old
, &ff_up_compat
.old
,
514 sizeof(struct ff_effect_compat
));
517 if (copy_from_user(ff_up
, buffer
,
518 sizeof(struct uinput_ff_upload
)))
527 static int uinput_ff_upload_to_user(char __user
*buffer
,
528 const struct uinput_ff_upload
*ff_up
)
530 if (copy_to_user(buffer
, ff_up
, sizeof(struct uinput_ff_upload
)))
536 static int uinput_ff_upload_from_user(const char __user
*buffer
,
537 struct uinput_ff_upload
*ff_up
)
539 if (copy_from_user(ff_up
, buffer
, sizeof(struct uinput_ff_upload
)))
547 #define uinput_set_bit(_arg, _bit, _max) \
550 if (udev->state == UIST_CREATED) \
552 else if ((_arg) > (_max)) \
554 else set_bit((_arg), udev->dev->_bit); \
558 static long uinput_ioctl_handler(struct file
*file
, unsigned int cmd
,
559 unsigned long arg
, void __user
*p
)
562 struct uinput_device
*udev
= file
->private_data
;
563 struct uinput_ff_upload ff_up
;
564 struct uinput_ff_erase ff_erase
;
565 struct uinput_request
*req
;
569 retval
= mutex_lock_interruptible(&udev
->mutex
);
574 retval
= uinput_allocate_device(udev
);
581 retval
= uinput_create_device(udev
);
585 uinput_destroy_device(udev
);
589 retval
= uinput_set_bit(arg
, evbit
, EV_MAX
);
593 retval
= uinput_set_bit(arg
, keybit
, KEY_MAX
);
597 retval
= uinput_set_bit(arg
, relbit
, REL_MAX
);
601 retval
= uinput_set_bit(arg
, absbit
, ABS_MAX
);
605 retval
= uinput_set_bit(arg
, mscbit
, MSC_MAX
);
609 retval
= uinput_set_bit(arg
, ledbit
, LED_MAX
);
613 retval
= uinput_set_bit(arg
, sndbit
, SND_MAX
);
617 retval
= uinput_set_bit(arg
, ffbit
, FF_MAX
);
621 retval
= uinput_set_bit(arg
, swbit
, SW_MAX
);
625 if (udev
->state
== UIST_CREATED
) {
629 length
= strnlen_user(p
, 1024);
634 kfree(udev
->dev
->phys
);
635 udev
->dev
->phys
= phys
= kmalloc(length
, GFP_KERNEL
);
640 if (copy_from_user(phys
, p
, length
)) {
641 udev
->dev
->phys
= NULL
;
646 phys
[length
- 1] = '\0';
649 case UI_BEGIN_FF_UPLOAD
:
650 retval
= uinput_ff_upload_from_user(p
, &ff_up
);
654 req
= uinput_request_find(udev
, ff_up
.request_id
);
655 if (!req
|| req
->code
!= UI_FF_UPLOAD
|| !req
->u
.upload
.effect
) {
661 ff_up
.effect
= *req
->u
.upload
.effect
;
662 if (req
->u
.upload
.old
)
663 ff_up
.old
= *req
->u
.upload
.old
;
665 memset(&ff_up
.old
, 0, sizeof(struct ff_effect
));
667 retval
= uinput_ff_upload_to_user(p
, &ff_up
);
670 case UI_BEGIN_FF_ERASE
:
671 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
676 req
= uinput_request_find(udev
, ff_erase
.request_id
);
677 if (!req
|| req
->code
!= UI_FF_ERASE
) {
683 ff_erase
.effect_id
= req
->u
.effect_id
;
684 if (copy_to_user(p
, &ff_erase
, sizeof(ff_erase
))) {
691 case UI_END_FF_UPLOAD
:
692 retval
= uinput_ff_upload_from_user(p
, &ff_up
);
696 req
= uinput_request_find(udev
, ff_up
.request_id
);
697 if (!req
|| req
->code
!= UI_FF_UPLOAD
||
698 !req
->u
.upload
.effect
) {
703 req
->retval
= ff_up
.retval
;
704 uinput_request_done(udev
, req
);
707 case UI_END_FF_ERASE
:
708 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
713 req
= uinput_request_find(udev
, ff_erase
.request_id
);
714 if (!req
|| req
->code
!= UI_FF_ERASE
) {
719 req
->retval
= ff_erase
.retval
;
720 uinput_request_done(udev
, req
);
728 mutex_unlock(&udev
->mutex
);
732 static long uinput_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
734 return uinput_ioctl_handler(file
, cmd
, arg
, (void __user
*)arg
);
738 static long uinput_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
740 return uinput_ioctl_handler(file
, cmd
, arg
, compat_ptr(arg
));
744 static const struct file_operations uinput_fops
= {
745 .owner
= THIS_MODULE
,
747 .release
= uinput_release
,
749 .write
= uinput_write
,
751 .unlocked_ioctl
= uinput_ioctl
,
753 .compat_ioctl
= uinput_compat_ioctl
,
757 static struct miscdevice uinput_misc
= {
758 .fops
= &uinput_fops
,
759 .minor
= UINPUT_MINOR
,
763 static int __init
uinput_init(void)
765 return misc_register(&uinput_misc
);
768 static void __exit
uinput_exit(void)
770 misc_deregister(&uinput_misc
);
773 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
774 MODULE_DESCRIPTION("User level driver support for input subsystem");
775 MODULE_LICENSE("GPL");
776 MODULE_VERSION("0.3");
778 module_init(uinput_init
);
779 module_exit(uinput_exit
);