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_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 <linux/wakelock.h>
24 #include "input-compat.h"
30 struct input_handle handle
;
31 wait_queue_head_t wait
;
32 struct evdev_client
*grab
;
33 struct list_head client_list
;
34 spinlock_t client_lock
; /* protects client_list */
40 struct input_event buffer
[EVDEV_BUFFER_SIZE
];
43 spinlock_t buffer_lock
; /* protects access to buffer, head and tail */
44 struct fasync_struct
*fasync
;
46 struct list_head node
;
47 struct wake_lock wake_lock
;
51 static struct evdev
*evdev_table
[EVDEV_MINORS
];
52 static DEFINE_MUTEX(evdev_table_mutex
);
54 static void evdev_pass_event(struct evdev_client
*client
,
55 struct input_event
*event
)
58 * Interrupts are disabled, just acquire the lock
60 spin_lock(&client
->buffer_lock
);
61 wake_lock_timeout(&client
->wake_lock
, 5 * HZ
);
62 client
->buffer
[client
->head
++] = *event
;
63 client
->head
&= EVDEV_BUFFER_SIZE
- 1;
64 spin_unlock(&client
->buffer_lock
);
66 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
70 * Pass incoming event to all connected clients.
72 static void evdev_event(struct input_handle
*handle
,
73 unsigned int type
, unsigned int code
, int value
)
75 struct evdev
*evdev
= handle
->private;
76 struct evdev_client
*client
;
77 struct input_event event
;
81 event
.time
.tv_sec
= ts
.tv_sec
;
82 event
.time
.tv_usec
= ts
.tv_nsec
/ NSEC_PER_USEC
;
89 client
= rcu_dereference(evdev
->grab
);
91 evdev_pass_event(client
, &event
);
93 list_for_each_entry_rcu(client
, &evdev
->client_list
, node
)
94 evdev_pass_event(client
, &event
);
98 wake_up_interruptible(&evdev
->wait
);
101 static int evdev_fasync(int fd
, struct file
*file
, int on
)
103 struct evdev_client
*client
= file
->private_data
;
105 return fasync_helper(fd
, file
, on
, &client
->fasync
);
108 static int evdev_flush(struct file
*file
, fl_owner_t id
)
110 struct evdev_client
*client
= file
->private_data
;
111 struct evdev
*evdev
= client
->evdev
;
114 retval
= mutex_lock_interruptible(&evdev
->mutex
);
121 retval
= input_flush_device(&evdev
->handle
, file
);
123 mutex_unlock(&evdev
->mutex
);
127 static void evdev_free(struct device
*dev
)
129 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
131 input_put_device(evdev
->handle
.dev
);
136 * Grabs an event device (along with underlying input device).
137 * This function is called with evdev->mutex taken.
139 static int evdev_grab(struct evdev
*evdev
, struct evdev_client
*client
)
146 error
= input_grab_device(&evdev
->handle
);
150 rcu_assign_pointer(evdev
->grab
, client
);
156 static int evdev_ungrab(struct evdev
*evdev
, struct evdev_client
*client
)
158 if (evdev
->grab
!= client
)
161 rcu_assign_pointer(evdev
->grab
, NULL
);
163 input_release_device(&evdev
->handle
);
168 static void evdev_attach_client(struct evdev
*evdev
,
169 struct evdev_client
*client
)
171 spin_lock(&evdev
->client_lock
);
172 list_add_tail_rcu(&client
->node
, &evdev
->client_list
);
173 spin_unlock(&evdev
->client_lock
);
177 static void evdev_detach_client(struct evdev
*evdev
,
178 struct evdev_client
*client
)
180 spin_lock(&evdev
->client_lock
);
181 list_del_rcu(&client
->node
);
182 spin_unlock(&evdev
->client_lock
);
186 static int evdev_open_device(struct evdev
*evdev
)
190 retval
= mutex_lock_interruptible(&evdev
->mutex
);
196 else if (!evdev
->open
++) {
197 retval
= input_open_device(&evdev
->handle
);
202 mutex_unlock(&evdev
->mutex
);
206 static void evdev_close_device(struct evdev
*evdev
)
208 mutex_lock(&evdev
->mutex
);
210 if (evdev
->exist
&& !--evdev
->open
)
211 input_close_device(&evdev
->handle
);
213 mutex_unlock(&evdev
->mutex
);
217 * Wake up users waiting for IO so they can disconnect from
220 static void evdev_hangup(struct evdev
*evdev
)
222 struct evdev_client
*client
;
224 spin_lock(&evdev
->client_lock
);
225 list_for_each_entry(client
, &evdev
->client_list
, node
)
226 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
227 spin_unlock(&evdev
->client_lock
);
229 wake_up_interruptible(&evdev
->wait
);
232 static int evdev_release(struct inode
*inode
, struct file
*file
)
234 struct evdev_client
*client
= file
->private_data
;
235 struct evdev
*evdev
= client
->evdev
;
237 mutex_lock(&evdev
->mutex
);
238 if (evdev
->grab
== client
)
239 evdev_ungrab(evdev
, client
);
240 mutex_unlock(&evdev
->mutex
);
242 evdev_detach_client(evdev
, client
);
243 wake_lock_destroy(&client
->wake_lock
);
246 evdev_close_device(evdev
);
247 put_device(&evdev
->dev
);
252 static int evdev_open(struct inode
*inode
, struct file
*file
)
255 struct evdev_client
*client
;
256 int i
= iminor(inode
) - EVDEV_MINOR_BASE
;
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 client
= kzalloc(sizeof(struct evdev_client
), GFP_KERNEL
);
279 spin_lock_init(&client
->buffer_lock
);
280 snprintf(client
->name
, sizeof(client
->name
), "%s-%d",
281 dev_name(&evdev
->dev
), task_tgid_vnr(current
));
282 wake_lock_init(&client
->wake_lock
, WAKE_LOCK_SUSPEND
, client
->name
);
283 client
->evdev
= evdev
;
284 evdev_attach_client(evdev
, client
);
286 error
= evdev_open_device(evdev
);
288 goto err_free_client
;
290 file
->private_data
= client
;
294 evdev_detach_client(evdev
, client
);
297 put_device(&evdev
->dev
);
301 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
,
302 size_t count
, loff_t
*ppos
)
304 struct evdev_client
*client
= file
->private_data
;
305 struct evdev
*evdev
= client
->evdev
;
306 struct input_event event
;
309 retval
= mutex_lock_interruptible(&evdev
->mutex
);
318 while (retval
< count
) {
320 if (input_event_from_user(buffer
+ retval
, &event
)) {
325 input_inject_event(&evdev
->handle
,
326 event
.type
, event
.code
, event
.value
);
327 retval
+= input_event_size();
331 mutex_unlock(&evdev
->mutex
);
335 static int evdev_fetch_next_event(struct evdev_client
*client
,
336 struct input_event
*event
)
340 spin_lock_irq(&client
->buffer_lock
);
342 have_event
= client
->head
!= client
->tail
;
344 *event
= client
->buffer
[client
->tail
++];
345 client
->tail
&= EVDEV_BUFFER_SIZE
- 1;
346 if (client
->head
== client
->tail
)
347 wake_unlock(&client
->wake_lock
);
350 spin_unlock_irq(&client
->buffer_lock
);
355 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
,
356 size_t count
, loff_t
*ppos
)
358 struct evdev_client
*client
= file
->private_data
;
359 struct evdev
*evdev
= client
->evdev
;
360 struct input_event event
;
363 if (count
< input_event_size())
366 if (client
->head
== client
->tail
&& evdev
->exist
&&
367 (file
->f_flags
& O_NONBLOCK
))
370 retval
= wait_event_interruptible(evdev
->wait
,
371 client
->head
!= client
->tail
|| !evdev
->exist
);
378 while (retval
+ input_event_size() <= count
&&
379 evdev_fetch_next_event(client
, &event
)) {
381 if (input_event_to_user(buffer
+ retval
, &event
))
384 retval
+= input_event_size();
390 /* No kernel lock - fine */
391 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
393 struct evdev_client
*client
= file
->private_data
;
394 struct evdev
*evdev
= client
->evdev
;
396 poll_wait(file
, &evdev
->wait
, wait
);
397 return ((client
->head
== client
->tail
) ? 0 : (POLLIN
| POLLRDNORM
)) |
398 (evdev
->exist
? 0 : (POLLHUP
| POLLERR
));
403 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
404 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
407 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
408 unsigned int maxlen
, void __user
*p
, int compat
)
413 len
= BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
);
417 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
418 if (copy_to_user((compat_long_t __user
*) p
+ i
,
419 (compat_long_t
*) bits
+
420 i
+ 1 - ((i
% 2) << 1),
421 sizeof(compat_long_t
)))
424 len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
428 if (copy_to_user(p
, bits
, len
))
435 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
436 unsigned int maxlen
, void __user
*p
, int compat
)
439 BITS_TO_LONGS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
440 BITS_TO_LONGS(maxbit
) * sizeof(long);
445 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
447 #endif /* __BIG_ENDIAN */
451 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
452 unsigned int maxlen
, void __user
*p
, int compat
)
454 int len
= BITS_TO_LONGS(maxbit
) * sizeof(long);
459 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
462 #endif /* CONFIG_COMPAT */
464 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
471 len
= strlen(str
) + 1;
475 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
478 #define OLD_KEY_MAX 0x1ff
479 static int handle_eviocgbit(struct input_dev
*dev
, unsigned int cmd
, void __user
*p
, int compat_mode
)
481 static unsigned long keymax_warn_time
;
485 switch (_IOC_NR(cmd
) & EV_MAX
) {
487 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
488 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
489 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
490 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
491 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
492 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
493 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
494 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
495 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
496 default: return -EINVAL
;
500 * Work around bugs in userspace programs that like to do
501 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
502 * should be in bytes, not in bits.
504 if ((_IOC_NR(cmd
) & EV_MAX
) == EV_KEY
&& _IOC_SIZE(cmd
) == OLD_KEY_MAX
) {
506 if (printk_timed_ratelimit(&keymax_warn_time
, 10 * 1000))
508 "evdev.c(EVIOCGBIT): Suspicious buffer size %u, "
509 "limiting output to %zu bytes. See "
510 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
512 BITS_TO_LONGS(OLD_KEY_MAX
) * sizeof(long));
515 return bits_to_user(bits
, len
, _IOC_SIZE(cmd
), p
, compat_mode
);
519 static long evdev_do_ioctl(struct file
*file
, unsigned int cmd
,
520 void __user
*p
, int compat_mode
)
522 struct evdev_client
*client
= file
->private_data
;
523 struct evdev
*evdev
= client
->evdev
;
524 struct input_dev
*dev
= evdev
->handle
.dev
;
525 struct input_absinfo abs
;
526 struct ff_effect effect
;
527 int __user
*ip
= (int __user
*)p
;
534 return put_user(EV_VERSION
, ip
);
537 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
542 if (!test_bit(EV_REP
, dev
->evbit
))
544 if (put_user(dev
->rep
[REP_DELAY
], ip
))
546 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
551 if (!test_bit(EV_REP
, dev
->evbit
))
555 if (get_user(v
, ip
+ 1))
558 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
559 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
567 error
= input_get_keycode(dev
, t
, &v
);
571 if (put_user(v
, ip
+ 1))
577 if (get_user(t
, ip
) || get_user(v
, ip
+ 1))
580 return input_set_keycode(dev
, t
, v
);
583 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
586 i
= test_bit(EV_FF
, dev
->evbit
) ?
587 dev
->ff
->max_effects
: 0;
594 return evdev_grab(evdev
, client
);
596 return evdev_ungrab(evdev
, client
);
600 if (_IOC_TYPE(cmd
) != 'E')
603 if (_IOC_DIR(cmd
) == _IOC_READ
) {
605 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0, 0)))
606 return handle_eviocgbit(dev
, cmd
, p
, compat_mode
);
608 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGKEY(0)))
609 return bits_to_user(dev
->key
, KEY_MAX
, _IOC_SIZE(cmd
),
612 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGLED(0)))
613 return bits_to_user(dev
->led
, LED_MAX
, _IOC_SIZE(cmd
),
616 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSND(0)))
617 return bits_to_user(dev
->snd
, SND_MAX
, _IOC_SIZE(cmd
),
620 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSW(0)))
621 return bits_to_user(dev
->sw
, SW_MAX
, _IOC_SIZE(cmd
),
624 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGNAME(0)))
625 return str_to_user(dev
->name
, _IOC_SIZE(cmd
), p
);
627 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGPHYS(0)))
628 return str_to_user(dev
->phys
, _IOC_SIZE(cmd
), p
);
630 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGUNIQ(0)))
631 return str_to_user(dev
->uniq
, _IOC_SIZE(cmd
), p
);
633 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
635 t
= _IOC_NR(cmd
) & ABS_MAX
;
637 abs
.value
= dev
->abs
[t
];
638 abs
.minimum
= dev
->absmin
[t
];
639 abs
.maximum
= dev
->absmax
[t
];
640 abs
.fuzz
= dev
->absfuzz
[t
];
641 abs
.flat
= dev
->absflat
[t
];
642 abs
.resolution
= dev
->absres
[t
];
644 if (copy_to_user(p
, &abs
, min_t(size_t,
646 sizeof(struct input_absinfo
))))
654 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
656 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCSFF
)) {
658 if (input_ff_effect_from_user(p
, _IOC_SIZE(cmd
), &effect
))
661 error
= input_ff_upload(dev
, &effect
, file
);
663 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
669 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
671 t
= _IOC_NR(cmd
) & ABS_MAX
;
673 if (copy_from_user(&abs
, p
, min_t(size_t,
675 sizeof(struct input_absinfo
))))
679 * Take event lock to ensure that we are not
680 * changing device parameters in the middle
683 spin_lock_irq(&dev
->event_lock
);
685 dev
->abs
[t
] = abs
.value
;
686 dev
->absmin
[t
] = abs
.minimum
;
687 dev
->absmax
[t
] = abs
.maximum
;
688 dev
->absfuzz
[t
] = abs
.fuzz
;
689 dev
->absflat
[t
] = abs
.flat
;
690 dev
->absres
[t
] = _IOC_SIZE(cmd
) < sizeof(struct input_absinfo
) ?
693 spin_unlock_irq(&dev
->event_lock
);
702 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
703 void __user
*p
, int compat_mode
)
705 struct evdev_client
*client
= file
->private_data
;
706 struct evdev
*evdev
= client
->evdev
;
709 retval
= mutex_lock_interruptible(&evdev
->mutex
);
718 retval
= evdev_do_ioctl(file
, cmd
, p
, compat_mode
);
721 mutex_unlock(&evdev
->mutex
);
725 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
727 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
731 static long evdev_ioctl_compat(struct file
*file
,
732 unsigned int cmd
, unsigned long arg
)
734 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
738 static const struct file_operations evdev_fops
= {
739 .owner
= THIS_MODULE
,
741 .write
= evdev_write
,
744 .release
= evdev_release
,
745 .unlocked_ioctl
= evdev_ioctl
,
747 .compat_ioctl
= evdev_ioctl_compat
,
749 .fasync
= evdev_fasync
,
753 static int evdev_install_chrdev(struct evdev
*evdev
)
756 * No need to do any locking here as calls to connect and
757 * disconnect are serialized by the input core
759 evdev_table
[evdev
->minor
] = evdev
;
763 static void evdev_remove_chrdev(struct evdev
*evdev
)
766 * Lock evdev table to prevent race with evdev_open()
768 mutex_lock(&evdev_table_mutex
);
769 evdev_table
[evdev
->minor
] = NULL
;
770 mutex_unlock(&evdev_table_mutex
);
774 * Mark device non-existent. This disables writes, ioctls and
775 * prevents new users from opening the device. Already posted
776 * blocking reads will stay, however new ones will fail.
778 static void evdev_mark_dead(struct evdev
*evdev
)
780 mutex_lock(&evdev
->mutex
);
782 mutex_unlock(&evdev
->mutex
);
785 static void evdev_cleanup(struct evdev
*evdev
)
787 struct input_handle
*handle
= &evdev
->handle
;
789 evdev_mark_dead(evdev
);
791 evdev_remove_chrdev(evdev
);
793 /* evdev is marked dead so no one else accesses evdev->open */
795 input_flush_device(handle
, NULL
);
796 input_close_device(handle
);
801 * Create new evdev device. Note that input core serializes calls
802 * to connect and disconnect so we don't need to lock evdev_table here.
804 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
805 const struct input_device_id
*id
)
811 for (minor
= 0; minor
< EVDEV_MINORS
; minor
++)
812 if (!evdev_table
[minor
])
815 if (minor
== EVDEV_MINORS
) {
816 printk(KERN_ERR
"evdev: no more free evdev devices\n");
820 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
824 INIT_LIST_HEAD(&evdev
->client_list
);
825 spin_lock_init(&evdev
->client_lock
);
826 mutex_init(&evdev
->mutex
);
827 init_waitqueue_head(&evdev
->wait
);
829 dev_set_name(&evdev
->dev
, "event%d", minor
);
831 evdev
->minor
= minor
;
833 evdev
->handle
.dev
= input_get_device(dev
);
834 evdev
->handle
.name
= dev_name(&evdev
->dev
);
835 evdev
->handle
.handler
= handler
;
836 evdev
->handle
.private = evdev
;
838 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
);
839 evdev
->dev
.class = &input_class
;
840 evdev
->dev
.parent
= &dev
->dev
;
841 evdev
->dev
.release
= evdev_free
;
842 device_initialize(&evdev
->dev
);
844 error
= input_register_handle(&evdev
->handle
);
848 error
= evdev_install_chrdev(evdev
);
850 goto err_unregister_handle
;
852 error
= device_add(&evdev
->dev
);
854 goto err_cleanup_evdev
;
859 evdev_cleanup(evdev
);
860 err_unregister_handle
:
861 input_unregister_handle(&evdev
->handle
);
863 put_device(&evdev
->dev
);
867 static void evdev_disconnect(struct input_handle
*handle
)
869 struct evdev
*evdev
= handle
->private;
871 device_del(&evdev
->dev
);
872 evdev_cleanup(evdev
);
873 input_unregister_handle(handle
);
874 put_device(&evdev
->dev
);
877 static const struct input_device_id evdev_ids
[] = {
878 { .driver_info
= 1 }, /* Matches all devices */
879 { }, /* Terminating zero entry */
882 MODULE_DEVICE_TABLE(input
, evdev_ids
);
884 static struct input_handler evdev_handler
= {
885 .event
= evdev_event
,
886 .connect
= evdev_connect
,
887 .disconnect
= evdev_disconnect
,
889 .minor
= EVDEV_MINOR_BASE
,
891 .id_table
= evdev_ids
,
894 static int __init
evdev_init(void)
896 return input_register_handler(&evdev_handler
);
899 static void __exit
evdev_exit(void)
901 input_unregister_handler(&evdev_handler
);
904 module_init(evdev_init
);
905 module_exit(evdev_exit
);
907 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
908 MODULE_DESCRIPTION("Input driver event char devices");
909 MODULE_LICENSE("GPL");