2 * Event char devices, giving access to raw input device events.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #define EVDEV_MINOR_BASE 64
12 #define EVDEV_MINORS 32
13 #define EVDEV_MIN_BUFFER_SIZE 64
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/input.h>
21 #include <linux/major.h>
22 #include <linux/device.h>
23 #include "input-compat.h"
29 struct input_handle handle
;
30 wait_queue_head_t wait
;
31 struct evdev_client
*grab
;
32 struct list_head client_list
;
33 spinlock_t client_lock
; /* protects client_list */
41 spinlock_t buffer_lock
; /* protects access to buffer, head and tail */
42 struct fasync_struct
*fasync
;
44 struct list_head node
;
46 struct input_event buffer
[];
49 static struct evdev
*evdev_table
[EVDEV_MINORS
];
50 static DEFINE_MUTEX(evdev_table_mutex
);
52 static void evdev_pass_event(struct evdev_client
*client
,
53 struct input_event
*event
)
56 * Interrupts are disabled, just acquire the lock
58 spin_lock(&client
->buffer_lock
);
59 client
->buffer
[client
->head
++] = *event
;
60 client
->head
&= client
->bufsize
- 1;
61 spin_unlock(&client
->buffer_lock
);
63 if (event
->type
== EV_SYN
)
64 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
68 * Pass incoming event to all connected clients.
70 static void evdev_event(struct input_handle
*handle
,
71 unsigned int type
, unsigned int code
, int value
)
73 struct evdev
*evdev
= handle
->private;
74 struct evdev_client
*client
;
75 struct input_event event
;
77 do_gettimeofday(&event
.time
);
84 client
= rcu_dereference(evdev
->grab
);
86 evdev_pass_event(client
, &event
);
88 list_for_each_entry_rcu(client
, &evdev
->client_list
, node
)
89 evdev_pass_event(client
, &event
);
93 wake_up_interruptible(&evdev
->wait
);
96 static int evdev_fasync(int fd
, struct file
*file
, int on
)
98 struct evdev_client
*client
= file
->private_data
;
100 return fasync_helper(fd
, file
, on
, &client
->fasync
);
103 static int evdev_flush(struct file
*file
, fl_owner_t id
)
105 struct evdev_client
*client
= file
->private_data
;
106 struct evdev
*evdev
= client
->evdev
;
109 retval
= mutex_lock_interruptible(&evdev
->mutex
);
116 retval
= input_flush_device(&evdev
->handle
, file
);
118 mutex_unlock(&evdev
->mutex
);
122 static void evdev_free(struct device
*dev
)
124 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
126 input_put_device(evdev
->handle
.dev
);
131 * Grabs an event device (along with underlying input device).
132 * This function is called with evdev->mutex taken.
134 static int evdev_grab(struct evdev
*evdev
, struct evdev_client
*client
)
141 error
= input_grab_device(&evdev
->handle
);
145 rcu_assign_pointer(evdev
->grab
, client
);
151 static int evdev_ungrab(struct evdev
*evdev
, struct evdev_client
*client
)
153 if (evdev
->grab
!= client
)
156 rcu_assign_pointer(evdev
->grab
, NULL
);
158 input_release_device(&evdev
->handle
);
163 static void evdev_attach_client(struct evdev
*evdev
,
164 struct evdev_client
*client
)
166 spin_lock(&evdev
->client_lock
);
167 list_add_tail_rcu(&client
->node
, &evdev
->client_list
);
168 spin_unlock(&evdev
->client_lock
);
172 static void evdev_detach_client(struct evdev
*evdev
,
173 struct evdev_client
*client
)
175 spin_lock(&evdev
->client_lock
);
176 list_del_rcu(&client
->node
);
177 spin_unlock(&evdev
->client_lock
);
181 static int evdev_open_device(struct evdev
*evdev
)
185 retval
= mutex_lock_interruptible(&evdev
->mutex
);
191 else if (!evdev
->open
++) {
192 retval
= input_open_device(&evdev
->handle
);
197 mutex_unlock(&evdev
->mutex
);
201 static void evdev_close_device(struct evdev
*evdev
)
203 mutex_lock(&evdev
->mutex
);
205 if (evdev
->exist
&& !--evdev
->open
)
206 input_close_device(&evdev
->handle
);
208 mutex_unlock(&evdev
->mutex
);
212 * Wake up users waiting for IO so they can disconnect from
215 static void evdev_hangup(struct evdev
*evdev
)
217 struct evdev_client
*client
;
219 spin_lock(&evdev
->client_lock
);
220 list_for_each_entry(client
, &evdev
->client_list
, node
)
221 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
222 spin_unlock(&evdev
->client_lock
);
224 wake_up_interruptible(&evdev
->wait
);
227 static int evdev_release(struct inode
*inode
, struct file
*file
)
229 struct evdev_client
*client
= file
->private_data
;
230 struct evdev
*evdev
= client
->evdev
;
232 mutex_lock(&evdev
->mutex
);
233 if (evdev
->grab
== client
)
234 evdev_ungrab(evdev
, client
);
235 mutex_unlock(&evdev
->mutex
);
237 evdev_detach_client(evdev
, client
);
240 evdev_close_device(evdev
);
241 put_device(&evdev
->dev
);
246 static unsigned int evdev_compute_buffer_size(struct input_dev
*dev
)
248 return EVDEV_MIN_BUFFER_SIZE
;
251 static int evdev_open(struct inode
*inode
, struct file
*file
)
254 struct evdev_client
*client
;
255 int i
= iminor(inode
) - EVDEV_MINOR_BASE
;
256 unsigned int bufsize
;
259 if (i
>= EVDEV_MINORS
)
262 error
= mutex_lock_interruptible(&evdev_table_mutex
);
265 evdev
= evdev_table
[i
];
267 get_device(&evdev
->dev
);
268 mutex_unlock(&evdev_table_mutex
);
273 bufsize
= evdev_compute_buffer_size(evdev
->handle
.dev
);
275 client
= kzalloc(sizeof(struct evdev_client
) +
276 bufsize
* sizeof(struct input_event
),
283 client
->bufsize
= bufsize
;
284 spin_lock_init(&client
->buffer_lock
);
285 client
->evdev
= evdev
;
286 evdev_attach_client(evdev
, client
);
288 error
= evdev_open_device(evdev
);
290 goto err_free_client
;
292 file
->private_data
= client
;
293 nonseekable_open(inode
, file
);
298 evdev_detach_client(evdev
, client
);
301 put_device(&evdev
->dev
);
305 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
,
306 size_t count
, loff_t
*ppos
)
308 struct evdev_client
*client
= file
->private_data
;
309 struct evdev
*evdev
= client
->evdev
;
310 struct input_event event
;
313 retval
= mutex_lock_interruptible(&evdev
->mutex
);
322 while (retval
< count
) {
324 if (input_event_from_user(buffer
+ retval
, &event
)) {
329 input_inject_event(&evdev
->handle
,
330 event
.type
, event
.code
, event
.value
);
331 retval
+= input_event_size();
335 mutex_unlock(&evdev
->mutex
);
339 static int evdev_fetch_next_event(struct evdev_client
*client
,
340 struct input_event
*event
)
344 spin_lock_irq(&client
->buffer_lock
);
346 have_event
= client
->head
!= client
->tail
;
348 *event
= client
->buffer
[client
->tail
++];
349 client
->tail
&= client
->bufsize
- 1;
352 spin_unlock_irq(&client
->buffer_lock
);
357 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
,
358 size_t count
, loff_t
*ppos
)
360 struct evdev_client
*client
= file
->private_data
;
361 struct evdev
*evdev
= client
->evdev
;
362 struct input_event event
;
365 if (count
< input_event_size())
368 if (client
->head
== client
->tail
&& evdev
->exist
&&
369 (file
->f_flags
& O_NONBLOCK
))
372 retval
= wait_event_interruptible(evdev
->wait
,
373 client
->head
!= client
->tail
|| !evdev
->exist
);
380 while (retval
+ input_event_size() <= count
&&
381 evdev_fetch_next_event(client
, &event
)) {
383 if (input_event_to_user(buffer
+ retval
, &event
))
386 retval
+= input_event_size();
392 /* No kernel lock - fine */
393 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
395 struct evdev_client
*client
= file
->private_data
;
396 struct evdev
*evdev
= client
->evdev
;
398 poll_wait(file
, &evdev
->wait
, wait
);
399 return ((client
->head
== client
->tail
) ? 0 : (POLLIN
| POLLRDNORM
)) |
400 (evdev
->exist
? 0 : (POLLHUP
| POLLERR
));
405 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
406 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
409 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
410 unsigned int maxlen
, void __user
*p
, int compat
)
415 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
419 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
420 if (copy_to_user((compat_long_t __user
*) p
+ i
,
421 (compat_long_t
*) bits
+
422 i
+ 1 - ((i
% 2) << 1),
423 sizeof(compat_long_t
)))
426 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
430 if (copy_to_user(p
, bits
, len
))
437 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
438 unsigned int maxlen
, void __user
*p
, int compat
)
441 BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
442 BITS_TO_LONGS(maxbit
) * sizeof(long);
447 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
449 #endif /* __BIG_ENDIAN */
453 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
454 unsigned int maxlen
, void __user
*p
, int compat
)
456 int len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
461 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
464 #endif /* CONFIG_COMPAT */
466 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
473 len
= strlen(str
) + 1;
477 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
480 #define OLD_KEY_MAX 0x1ff
481 static int handle_eviocgbit(struct input_dev
*dev
, unsigned int cmd
, void __user
*p
, int compat_mode
)
483 static unsigned long keymax_warn_time
;
487 switch (_IOC_NR(cmd
) & EV_MAX
) {
489 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
490 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
491 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
492 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
493 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
494 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
495 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
496 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
497 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
498 default: return -EINVAL
;
502 * Work around bugs in userspace programs that like to do
503 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
504 * should be in bytes, not in bits.
506 if ((_IOC_NR(cmd
) & EV_MAX
) == EV_KEY
&& _IOC_SIZE(cmd
) == OLD_KEY_MAX
) {
508 if (printk_timed_ratelimit(&keymax_warn_time
, 10 * 1000))
510 "evdev.c(EVIOCGBIT): Suspicious buffer size %u, "
511 "limiting output to %zu bytes. See "
512 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
514 BITS_TO_LONGS(OLD_KEY_MAX
) * sizeof(long));
517 return bits_to_user(bits
, len
, _IOC_SIZE(cmd
), p
, compat_mode
);
521 static long evdev_do_ioctl(struct file
*file
, unsigned int cmd
,
522 void __user
*p
, int compat_mode
)
524 struct evdev_client
*client
= file
->private_data
;
525 struct evdev
*evdev
= client
->evdev
;
526 struct input_dev
*dev
= evdev
->handle
.dev
;
527 struct input_absinfo abs
;
528 struct ff_effect effect
;
529 int __user
*ip
= (int __user
*)p
;
530 unsigned int i
, t
, u
, v
;
536 return put_user(EV_VERSION
, ip
);
539 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
544 if (!test_bit(EV_REP
, dev
->evbit
))
546 if (put_user(dev
->rep
[REP_DELAY
], ip
))
548 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
553 if (!test_bit(EV_REP
, dev
->evbit
))
557 if (get_user(v
, ip
+ 1))
560 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
561 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
569 error
= input_get_keycode(dev
, t
, &v
);
573 if (put_user(v
, ip
+ 1))
579 if (get_user(t
, ip
) || get_user(v
, ip
+ 1))
582 return input_set_keycode(dev
, t
, v
);
585 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
588 i
= test_bit(EV_FF
, dev
->evbit
) ?
589 dev
->ff
->max_effects
: 0;
596 return evdev_grab(evdev
, client
);
598 return evdev_ungrab(evdev
, client
);
602 if (_IOC_TYPE(cmd
) != 'E')
605 if (_IOC_DIR(cmd
) == _IOC_READ
) {
607 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0, 0)))
608 return handle_eviocgbit(dev
, cmd
, p
, compat_mode
);
610 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGKEY(0)))
611 return bits_to_user(dev
->key
, KEY_MAX
, _IOC_SIZE(cmd
),
614 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGLED(0)))
615 return bits_to_user(dev
->led
, LED_MAX
, _IOC_SIZE(cmd
),
618 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSND(0)))
619 return bits_to_user(dev
->snd
, SND_MAX
, _IOC_SIZE(cmd
),
622 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSW(0)))
623 return bits_to_user(dev
->sw
, SW_MAX
, _IOC_SIZE(cmd
),
626 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGNAME(0)))
627 return str_to_user(dev
->name
, _IOC_SIZE(cmd
), p
);
629 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGPHYS(0)))
630 return str_to_user(dev
->phys
, _IOC_SIZE(cmd
), p
);
632 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGUNIQ(0)))
633 return str_to_user(dev
->uniq
, _IOC_SIZE(cmd
), p
);
635 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
637 t
= _IOC_NR(cmd
) & ABS_MAX
;
639 abs
.value
= dev
->abs
[t
];
640 abs
.minimum
= dev
->absmin
[t
];
641 abs
.maximum
= dev
->absmax
[t
];
642 abs
.fuzz
= dev
->absfuzz
[t
];
643 abs
.flat
= dev
->absflat
[t
];
644 abs
.resolution
= dev
->absres
[t
];
646 if (copy_to_user(p
, &abs
, min_t(size_t,
648 sizeof(struct input_absinfo
))))
656 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
658 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCSFF
)) {
660 if (input_ff_effect_from_user(p
, _IOC_SIZE(cmd
), &effect
))
663 error
= input_ff_upload(dev
, &effect
, file
);
665 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
671 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
673 t
= _IOC_NR(cmd
) & ABS_MAX
;
675 if (copy_from_user(&abs
, p
, min_t(size_t,
677 sizeof(struct input_absinfo
))))
681 * Take event lock to ensure that we are not
682 * changing device parameters in the middle
685 spin_lock_irq(&dev
->event_lock
);
687 dev
->abs
[t
] = abs
.value
;
688 dev
->absmin
[t
] = abs
.minimum
;
689 dev
->absmax
[t
] = abs
.maximum
;
690 dev
->absfuzz
[t
] = abs
.fuzz
;
691 dev
->absflat
[t
] = abs
.flat
;
692 dev
->absres
[t
] = _IOC_SIZE(cmd
) < sizeof(struct input_absinfo
) ?
695 spin_unlock_irq(&dev
->event_lock
);
704 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
705 void __user
*p
, int compat_mode
)
707 struct evdev_client
*client
= file
->private_data
;
708 struct evdev
*evdev
= client
->evdev
;
711 retval
= mutex_lock_interruptible(&evdev
->mutex
);
720 retval
= evdev_do_ioctl(file
, cmd
, p
, compat_mode
);
723 mutex_unlock(&evdev
->mutex
);
727 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
729 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
733 static long evdev_ioctl_compat(struct file
*file
,
734 unsigned int cmd
, unsigned long arg
)
736 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
740 static const struct file_operations evdev_fops
= {
741 .owner
= THIS_MODULE
,
743 .write
= evdev_write
,
746 .release
= evdev_release
,
747 .unlocked_ioctl
= evdev_ioctl
,
749 .compat_ioctl
= evdev_ioctl_compat
,
751 .fasync
= evdev_fasync
,
755 static int evdev_install_chrdev(struct evdev
*evdev
)
758 * No need to do any locking here as calls to connect and
759 * disconnect are serialized by the input core
761 evdev_table
[evdev
->minor
] = evdev
;
765 static void evdev_remove_chrdev(struct evdev
*evdev
)
768 * Lock evdev table to prevent race with evdev_open()
770 mutex_lock(&evdev_table_mutex
);
771 evdev_table
[evdev
->minor
] = NULL
;
772 mutex_unlock(&evdev_table_mutex
);
776 * Mark device non-existent. This disables writes, ioctls and
777 * prevents new users from opening the device. Already posted
778 * blocking reads will stay, however new ones will fail.
780 static void evdev_mark_dead(struct evdev
*evdev
)
782 mutex_lock(&evdev
->mutex
);
784 mutex_unlock(&evdev
->mutex
);
787 static void evdev_cleanup(struct evdev
*evdev
)
789 struct input_handle
*handle
= &evdev
->handle
;
791 evdev_mark_dead(evdev
);
793 evdev_remove_chrdev(evdev
);
795 /* evdev is marked dead so no one else accesses evdev->open */
797 input_flush_device(handle
, NULL
);
798 input_close_device(handle
);
803 * Create new evdev device. Note that input core serializes calls
804 * to connect and disconnect so we don't need to lock evdev_table here.
806 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
807 const struct input_device_id
*id
)
813 for (minor
= 0; minor
< EVDEV_MINORS
; minor
++)
814 if (!evdev_table
[minor
])
817 if (minor
== EVDEV_MINORS
) {
818 printk(KERN_ERR
"evdev: no more free evdev devices\n");
822 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
826 INIT_LIST_HEAD(&evdev
->client_list
);
827 spin_lock_init(&evdev
->client_lock
);
828 mutex_init(&evdev
->mutex
);
829 init_waitqueue_head(&evdev
->wait
);
831 dev_set_name(&evdev
->dev
, "event%d", minor
);
833 evdev
->minor
= minor
;
835 evdev
->handle
.dev
= input_get_device(dev
);
836 evdev
->handle
.name
= dev_name(&evdev
->dev
);
837 evdev
->handle
.handler
= handler
;
838 evdev
->handle
.private = evdev
;
840 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
);
841 evdev
->dev
.class = &input_class
;
842 evdev
->dev
.parent
= &dev
->dev
;
843 evdev
->dev
.release
= evdev_free
;
844 device_initialize(&evdev
->dev
);
846 error
= input_register_handle(&evdev
->handle
);
850 error
= evdev_install_chrdev(evdev
);
852 goto err_unregister_handle
;
854 error
= device_add(&evdev
->dev
);
856 goto err_cleanup_evdev
;
861 evdev_cleanup(evdev
);
862 err_unregister_handle
:
863 input_unregister_handle(&evdev
->handle
);
865 put_device(&evdev
->dev
);
869 static void evdev_disconnect(struct input_handle
*handle
)
871 struct evdev
*evdev
= handle
->private;
873 device_del(&evdev
->dev
);
874 evdev_cleanup(evdev
);
875 input_unregister_handle(handle
);
876 put_device(&evdev
->dev
);
879 static const struct input_device_id evdev_ids
[] = {
880 { .driver_info
= 1 }, /* Matches all devices */
881 { }, /* Terminating zero entry */
884 MODULE_DEVICE_TABLE(input
, evdev_ids
);
886 static struct input_handler evdev_handler
= {
887 .event
= evdev_event
,
888 .connect
= evdev_connect
,
889 .disconnect
= evdev_disconnect
,
891 .minor
= EVDEV_MINOR_BASE
,
893 .id_table
= evdev_ids
,
896 static int __init
evdev_init(void)
898 return input_register_handler(&evdev_handler
);
901 static void __exit
evdev_exit(void)
903 input_unregister_handler(&evdev_handler
);
906 module_init(evdev_init
);
907 module_exit(evdev_exit
);
909 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
910 MODULE_DESCRIPTION("Input driver event char devices");
911 MODULE_LICENSE("GPL");