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>
41 static int uinput_dev_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
43 struct uinput_device
*udev
= input_get_drvdata(dev
);
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(&udev
->requests_waitq
);
97 complete(&request
->done
);
100 static int uinput_request_submit(struct input_dev
*dev
, struct uinput_request
*request
)
102 /* Tell our userspace app about this new request by queueing an input event */
103 uinput_dev_event(dev
, EV_UINPUT
, request
->code
, request
->id
);
105 /* Wait for the request to complete */
106 wait_for_completion(&request
->done
);
107 return request
->retval
;
110 static void uinput_dev_set_gain(struct input_dev
*dev
, u16 gain
)
112 uinput_dev_event(dev
, EV_FF
, FF_GAIN
, gain
);
115 static void uinput_dev_set_autocenter(struct input_dev
*dev
, u16 magnitude
)
117 uinput_dev_event(dev
, EV_FF
, FF_AUTOCENTER
, magnitude
);
120 static int uinput_dev_playback(struct input_dev
*dev
, int effect_id
, int value
)
122 return uinput_dev_event(dev
, EV_FF
, effect_id
, value
);
125 static int uinput_dev_upload_effect(struct input_dev
*dev
, struct ff_effect
*effect
, struct ff_effect
*old
)
127 struct uinput_request request
;
131 init_completion(&request
.done
);
132 request
.code
= UI_FF_UPLOAD
;
133 request
.u
.upload
.effect
= effect
;
134 request
.u
.upload
.old
= old
;
136 retval
= uinput_request_reserve_slot(input_get_drvdata(dev
), &request
);
138 retval
= uinput_request_submit(dev
, &request
);
143 static int uinput_dev_erase_effect(struct input_dev
*dev
, int effect_id
)
145 struct uinput_request request
;
148 if (!test_bit(EV_FF
, dev
->evbit
))
152 init_completion(&request
.done
);
153 request
.code
= UI_FF_ERASE
;
154 request
.u
.effect_id
= effect_id
;
156 retval
= uinput_request_reserve_slot(input_get_drvdata(dev
), &request
);
158 retval
= uinput_request_submit(dev
, &request
);
163 static void uinput_destroy_device(struct uinput_device
*udev
)
165 const char *name
, *phys
;
168 name
= udev
->dev
->name
;
169 phys
= udev
->dev
->phys
;
170 if (udev
->state
== UIST_CREATED
)
171 input_unregister_device(udev
->dev
);
173 input_free_device(udev
->dev
);
179 udev
->state
= UIST_NEW_DEVICE
;
182 static int uinput_create_device(struct uinput_device
*udev
)
184 struct input_dev
*dev
= udev
->dev
;
187 if (udev
->state
!= UIST_SETUP_COMPLETE
) {
188 printk(KERN_DEBUG
"%s: write device info first\n", UINPUT_NAME
);
192 if (udev
->ff_effects_max
) {
193 error
= input_ff_create(dev
, udev
->ff_effects_max
);
197 dev
->ff
->upload
= uinput_dev_upload_effect
;
198 dev
->ff
->erase
= uinput_dev_erase_effect
;
199 dev
->ff
->playback
= uinput_dev_playback
;
200 dev
->ff
->set_gain
= uinput_dev_set_gain
;
201 dev
->ff
->set_autocenter
= uinput_dev_set_autocenter
;
204 error
= input_register_device(udev
->dev
);
208 udev
->state
= UIST_CREATED
;
212 fail2
: input_ff_destroy(dev
);
213 fail1
: uinput_destroy_device(udev
);
217 static int uinput_open(struct inode
*inode
, struct file
*file
)
219 struct uinput_device
*newdev
;
221 newdev
= kzalloc(sizeof(struct uinput_device
), GFP_KERNEL
);
226 mutex_init(&newdev
->mutex
);
227 spin_lock_init(&newdev
->requests_lock
);
228 init_waitqueue_head(&newdev
->requests_waitq
);
229 init_waitqueue_head(&newdev
->waitq
);
230 newdev
->state
= UIST_NEW_DEVICE
;
232 file
->private_data
= newdev
;
238 static int uinput_validate_absbits(struct input_dev
*dev
)
243 for (cnt
= 0; cnt
< ABS_MAX
+ 1; cnt
++) {
244 if (!test_bit(cnt
, dev
->absbit
))
247 if ((dev
->absmax
[cnt
] <= dev
->absmin
[cnt
])) {
249 "%s: invalid abs[%02x] min:%d max:%d\n",
251 dev
->absmin
[cnt
], dev
->absmax
[cnt
]);
256 if (dev
->absflat
[cnt
] > (dev
->absmax
[cnt
] - dev
->absmin
[cnt
])) {
258 "%s: absflat[%02x] out of range: %d "
260 UINPUT_NAME
, cnt
, dev
->absflat
[cnt
],
261 dev
->absmin
[cnt
], dev
->absmax
[cnt
]);
269 static int uinput_allocate_device(struct uinput_device
*udev
)
271 udev
->dev
= input_allocate_device();
275 udev
->dev
->event
= uinput_dev_event
;
276 input_set_drvdata(udev
->dev
, udev
);
281 static int uinput_setup_device(struct uinput_device
*udev
, const char __user
*buffer
, size_t count
)
283 struct uinput_user_dev
*user_dev
;
284 struct input_dev
*dev
;
289 if (count
!= sizeof(struct uinput_user_dev
))
293 retval
= uinput_allocate_device(udev
);
300 user_dev
= kmalloc(sizeof(struct uinput_user_dev
), GFP_KERNEL
);
304 if (copy_from_user(user_dev
, buffer
, sizeof(struct uinput_user_dev
))) {
309 udev
->ff_effects_max
= user_dev
->ff_effects_max
;
311 size
= strnlen(user_dev
->name
, UINPUT_MAX_NAME_SIZE
) + 1;
318 dev
->name
= name
= kmalloc(size
, GFP_KERNEL
);
323 strlcpy(name
, user_dev
->name
, size
);
325 dev
->id
.bustype
= user_dev
->id
.bustype
;
326 dev
->id
.vendor
= user_dev
->id
.vendor
;
327 dev
->id
.product
= user_dev
->id
.product
;
328 dev
->id
.version
= user_dev
->id
.version
;
330 size
= sizeof(int) * (ABS_MAX
+ 1);
331 memcpy(dev
->absmax
, user_dev
->absmax
, size
);
332 memcpy(dev
->absmin
, user_dev
->absmin
, size
);
333 memcpy(dev
->absfuzz
, user_dev
->absfuzz
, size
);
334 memcpy(dev
->absflat
, user_dev
->absflat
, size
);
336 /* check if absmin/absmax/absfuzz/absflat are filled as
337 * told in Documentation/input/input-programming.txt */
338 if (test_bit(EV_ABS
, dev
->evbit
)) {
339 retval
= uinput_validate_absbits(dev
);
344 udev
->state
= UIST_SETUP_COMPLETE
;
352 static inline ssize_t
uinput_inject_event(struct uinput_device
*udev
, const char __user
*buffer
, size_t count
)
354 struct input_event ev
;
356 if (count
!= sizeof(struct input_event
))
359 if (copy_from_user(&ev
, buffer
, sizeof(struct input_event
)))
362 input_event(udev
->dev
, ev
.type
, ev
.code
, ev
.value
);
364 return sizeof(struct input_event
);
367 static ssize_t
uinput_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
369 struct uinput_device
*udev
= file
->private_data
;
372 retval
= mutex_lock_interruptible(&udev
->mutex
);
376 retval
= udev
->state
== UIST_CREATED
?
377 uinput_inject_event(udev
, buffer
, count
) :
378 uinput_setup_device(udev
, buffer
, count
);
380 mutex_unlock(&udev
->mutex
);
385 static ssize_t
uinput_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
387 struct uinput_device
*udev
= file
->private_data
;
390 if (udev
->state
!= UIST_CREATED
)
393 if (udev
->head
== udev
->tail
&& (file
->f_flags
& O_NONBLOCK
))
396 retval
= wait_event_interruptible(udev
->waitq
,
397 udev
->head
!= udev
->tail
|| udev
->state
!= UIST_CREATED
);
401 retval
= mutex_lock_interruptible(&udev
->mutex
);
405 if (udev
->state
!= UIST_CREATED
) {
410 while (udev
->head
!= udev
->tail
&& retval
+ sizeof(struct input_event
) <= count
) {
411 if (copy_to_user(buffer
+ retval
, &udev
->buff
[udev
->tail
], sizeof(struct input_event
))) {
415 udev
->tail
= (udev
->tail
+ 1) % UINPUT_BUFFER_SIZE
;
416 retval
+= sizeof(struct input_event
);
420 mutex_unlock(&udev
->mutex
);
425 static unsigned int uinput_poll(struct file
*file
, poll_table
*wait
)
427 struct uinput_device
*udev
= file
->private_data
;
429 poll_wait(file
, &udev
->waitq
, wait
);
431 if (udev
->head
!= udev
->tail
)
432 return POLLIN
| POLLRDNORM
;
437 static int uinput_release(struct inode
*inode
, struct file
*file
)
439 struct uinput_device
*udev
= file
->private_data
;
441 uinput_destroy_device(udev
);
447 #define uinput_set_bit(_arg, _bit, _max) \
450 if (udev->state == UIST_CREATED) \
452 else if ((_arg) > (_max)) \
454 else set_bit((_arg), udev->dev->_bit); \
458 static long uinput_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
461 struct uinput_device
*udev
;
462 void __user
*p
= (void __user
*)arg
;
463 struct uinput_ff_upload ff_up
;
464 struct uinput_ff_erase ff_erase
;
465 struct uinput_request
*req
;
469 udev
= file
->private_data
;
471 retval
= mutex_lock_interruptible(&udev
->mutex
);
476 retval
= uinput_allocate_device(udev
);
483 retval
= uinput_create_device(udev
);
487 uinput_destroy_device(udev
);
491 retval
= uinput_set_bit(arg
, evbit
, EV_MAX
);
495 retval
= uinput_set_bit(arg
, keybit
, KEY_MAX
);
499 retval
= uinput_set_bit(arg
, relbit
, REL_MAX
);
503 retval
= uinput_set_bit(arg
, absbit
, ABS_MAX
);
507 retval
= uinput_set_bit(arg
, mscbit
, MSC_MAX
);
511 retval
= uinput_set_bit(arg
, ledbit
, LED_MAX
);
515 retval
= uinput_set_bit(arg
, sndbit
, SND_MAX
);
519 retval
= uinput_set_bit(arg
, ffbit
, FF_MAX
);
523 retval
= uinput_set_bit(arg
, swbit
, SW_MAX
);
527 if (udev
->state
== UIST_CREATED
) {
531 length
= strnlen_user(p
, 1024);
536 kfree(udev
->dev
->phys
);
537 udev
->dev
->phys
= phys
= kmalloc(length
, GFP_KERNEL
);
542 if (copy_from_user(phys
, p
, length
)) {
543 udev
->dev
->phys
= NULL
;
548 phys
[length
- 1] = '\0';
551 case UI_BEGIN_FF_UPLOAD
:
552 if (copy_from_user(&ff_up
, p
, sizeof(ff_up
))) {
556 req
= uinput_request_find(udev
, ff_up
.request_id
);
557 if (!(req
&& req
->code
== UI_FF_UPLOAD
&& req
->u
.upload
.effect
)) {
562 memcpy(&ff_up
.effect
, req
->u
.upload
.effect
, sizeof(struct ff_effect
));
563 if (req
->u
.upload
.old
)
564 memcpy(&ff_up
.old
, req
->u
.upload
.old
, sizeof(struct ff_effect
));
566 memset(&ff_up
.old
, 0, sizeof(struct ff_effect
));
568 if (copy_to_user(p
, &ff_up
, sizeof(ff_up
))) {
574 case UI_BEGIN_FF_ERASE
:
575 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
579 req
= uinput_request_find(udev
, ff_erase
.request_id
);
580 if (!(req
&& req
->code
== UI_FF_ERASE
)) {
585 ff_erase
.effect_id
= req
->u
.effect_id
;
586 if (copy_to_user(p
, &ff_erase
, sizeof(ff_erase
))) {
592 case UI_END_FF_UPLOAD
:
593 if (copy_from_user(&ff_up
, p
, sizeof(ff_up
))) {
597 req
= uinput_request_find(udev
, ff_up
.request_id
);
598 if (!(req
&& req
->code
== UI_FF_UPLOAD
&& req
->u
.upload
.effect
)) {
602 req
->retval
= ff_up
.retval
;
603 uinput_request_done(udev
, req
);
606 case UI_END_FF_ERASE
:
607 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
611 req
= uinput_request_find(udev
, ff_erase
.request_id
);
612 if (!(req
&& req
->code
== UI_FF_ERASE
)) {
616 req
->retval
= ff_erase
.retval
;
617 uinput_request_done(udev
, req
);
625 mutex_unlock(&udev
->mutex
);
629 static const struct file_operations uinput_fops
= {
630 .owner
= THIS_MODULE
,
632 .release
= uinput_release
,
634 .write
= uinput_write
,
636 .unlocked_ioctl
= uinput_ioctl
,
639 static struct miscdevice uinput_misc
= {
640 .fops
= &uinput_fops
,
641 .minor
= UINPUT_MINOR
,
645 static int __init
uinput_init(void)
647 return misc_register(&uinput_misc
);
650 static void __exit
uinput_exit(void)
652 misc_deregister(&uinput_misc
);
655 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
656 MODULE_DESCRIPTION("User level driver support for input subsystem");
657 MODULE_LICENSE("GPL");
658 MODULE_VERSION("0.3");
660 module_init(uinput_init
);
661 module_exit(uinput_exit
);