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 retval
= uinput_validate_absbits(dev
);
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
)))
320 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
) ||
341 !test_bit(UIST_CREATED
, &(udev
->state
)));
346 if (!test_bit(UIST_CREATED
, &(udev
->state
)))
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
);
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
;
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
);
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
)
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
;
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 if (NULL
!= udev
->dev
->phys
)
500 kfree(udev
->dev
->phys
);
501 udev
->dev
->phys
= kmalloc(length
, GFP_KERNEL
);
502 if (!udev
->dev
->phys
) {
506 if (copy_from_user(udev
->dev
->phys
, p
, length
)) {
508 kfree(udev
->dev
->phys
);
509 udev
->dev
->phys
= NULL
;
512 udev
->dev
->phys
[length
-1] = '\0';
515 case UI_BEGIN_FF_UPLOAD
:
516 if (copy_from_user(&ff_up
, p
, sizeof(ff_up
))) {
520 req
= uinput_request_find(udev
, ff_up
.request_id
);
521 if (!(req
&& req
->code
==UI_FF_UPLOAD
&& req
->u
.effect
)) {
526 memcpy(&ff_up
.effect
, req
->u
.effect
, sizeof(struct ff_effect
));
527 if (copy_to_user(p
, &ff_up
, sizeof(ff_up
))) {
533 case UI_BEGIN_FF_ERASE
:
534 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
538 req
= uinput_request_find(udev
, ff_erase
.request_id
);
539 if (!(req
&& req
->code
==UI_FF_ERASE
)) {
544 ff_erase
.effect_id
= req
->u
.effect_id
;
545 if (copy_to_user(p
, &ff_erase
, sizeof(ff_erase
))) {
551 case UI_END_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
.effect
)) {
561 req
->retval
= ff_up
.retval
;
562 memcpy(req
->u
.effect
, &ff_up
.effect
, sizeof(struct ff_effect
));
564 wake_up_interruptible(&req
->waitq
);
567 case UI_END_FF_ERASE
:
568 if (copy_from_user(&ff_erase
, p
, sizeof(ff_erase
))) {
572 req
= uinput_request_find(udev
, ff_erase
.request_id
);
573 if (!(req
&& req
->code
==UI_FF_ERASE
)) {
577 req
->retval
= ff_erase
.retval
;
579 wake_up_interruptible(&req
->waitq
);
588 static struct file_operations uinput_fops
= {
589 .owner
= THIS_MODULE
,
591 .release
= uinput_close
,
593 .write
= uinput_write
,
595 .ioctl
= uinput_ioctl
,
598 static struct miscdevice uinput_misc
= {
599 .fops
= &uinput_fops
,
600 .minor
= UINPUT_MINOR
,
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
);