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/slab.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/major.h>
21 #include <linux/device.h>
22 #include <linux/compat.h>
29 struct input_handle handle
;
30 wait_queue_head_t wait
;
31 struct evdev_client
*grab
;
32 struct list_head client_list
;
37 struct input_event buffer
[EVDEV_BUFFER_SIZE
];
40 struct fasync_struct
*fasync
;
42 struct list_head node
;
45 static struct evdev
*evdev_table
[EVDEV_MINORS
];
47 static void evdev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
49 struct evdev
*evdev
= handle
->private;
50 struct evdev_client
*client
;
55 do_gettimeofday(&client
->buffer
[client
->head
].time
);
56 client
->buffer
[client
->head
].type
= type
;
57 client
->buffer
[client
->head
].code
= code
;
58 client
->buffer
[client
->head
].value
= value
;
59 client
->head
= (client
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
61 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
63 list_for_each_entry(client
, &evdev
->client_list
, node
) {
65 do_gettimeofday(&client
->buffer
[client
->head
].time
);
66 client
->buffer
[client
->head
].type
= type
;
67 client
->buffer
[client
->head
].code
= code
;
68 client
->buffer
[client
->head
].value
= value
;
69 client
->head
= (client
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
71 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
74 wake_up_interruptible(&evdev
->wait
);
77 static int evdev_fasync(int fd
, struct file
*file
, int on
)
79 struct evdev_client
*client
= file
->private_data
;
82 retval
= fasync_helper(fd
, file
, on
, &client
->fasync
);
84 return retval
< 0 ? retval
: 0;
87 static int evdev_flush(struct file
*file
, fl_owner_t id
)
89 struct evdev_client
*client
= file
->private_data
;
90 struct evdev
*evdev
= client
->evdev
;
95 return input_flush_device(&evdev
->handle
, file
);
98 static void evdev_free(struct device
*dev
)
100 struct evdev
*evdev
= container_of(dev
, struct evdev
, dev
);
102 evdev_table
[evdev
->minor
] = NULL
;
106 static int evdev_release(struct inode
*inode
, struct file
*file
)
108 struct evdev_client
*client
= file
->private_data
;
109 struct evdev
*evdev
= client
->evdev
;
111 if (evdev
->grab
== client
) {
112 input_release_device(&evdev
->handle
);
116 evdev_fasync(-1, file
, 0);
117 list_del(&client
->node
);
120 if (!--evdev
->open
&& evdev
->exist
)
121 input_close_device(&evdev
->handle
);
123 put_device(&evdev
->dev
);
128 static int evdev_open(struct inode
*inode
, struct file
*file
)
130 struct evdev_client
*client
;
132 int i
= iminor(inode
) - EVDEV_MINOR_BASE
;
135 if (i
>= EVDEV_MINORS
)
138 evdev
= evdev_table
[i
];
140 if (!evdev
|| !evdev
->exist
)
143 get_device(&evdev
->dev
);
145 client
= kzalloc(sizeof(struct evdev_client
), GFP_KERNEL
);
151 client
->evdev
= evdev
;
152 list_add_tail(&client
->node
, &evdev
->client_list
);
154 if (!evdev
->open
++ && evdev
->exist
) {
155 error
= input_open_device(&evdev
->handle
);
157 goto err_free_client
;
160 file
->private_data
= client
;
164 list_del(&client
->node
);
167 put_device(&evdev
->dev
);
173 struct input_event_compat
{
174 struct compat_timeval time
;
180 /* Note to the author of this code: did it ever occur to
181 you why the ifdefs are needed? Think about it again. -AK */
183 # define COMPAT_TEST is_compat_task()
184 #elif defined(CONFIG_IA64)
185 # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
186 #elif defined(CONFIG_S390)
187 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
188 #elif defined(CONFIG_MIPS)
189 # define COMPAT_TEST test_thread_flag(TIF_32BIT_ADDR)
191 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
194 static inline size_t evdev_event_size(void)
197 sizeof(struct input_event_compat
) : sizeof(struct input_event
);
200 static int evdev_event_from_user(const char __user
*buffer
, struct input_event
*event
)
203 struct input_event_compat compat_event
;
205 if (copy_from_user(&compat_event
, buffer
, sizeof(struct input_event_compat
)))
208 event
->time
.tv_sec
= compat_event
.time
.tv_sec
;
209 event
->time
.tv_usec
= compat_event
.time
.tv_usec
;
210 event
->type
= compat_event
.type
;
211 event
->code
= compat_event
.code
;
212 event
->value
= compat_event
.value
;
215 if (copy_from_user(event
, buffer
, sizeof(struct input_event
)))
222 static int evdev_event_to_user(char __user
*buffer
, const struct input_event
*event
)
225 struct input_event_compat compat_event
;
227 compat_event
.time
.tv_sec
= event
->time
.tv_sec
;
228 compat_event
.time
.tv_usec
= event
->time
.tv_usec
;
229 compat_event
.type
= event
->type
;
230 compat_event
.code
= event
->code
;
231 compat_event
.value
= event
->value
;
233 if (copy_to_user(buffer
, &compat_event
, sizeof(struct input_event_compat
)))
237 if (copy_to_user(buffer
, event
, sizeof(struct input_event
)))
246 static inline size_t evdev_event_size(void)
248 return sizeof(struct input_event
);
251 static int evdev_event_from_user(const char __user
*buffer
, struct input_event
*event
)
253 if (copy_from_user(event
, buffer
, sizeof(struct input_event
)))
259 static int evdev_event_to_user(char __user
*buffer
, const struct input_event
*event
)
261 if (copy_to_user(buffer
, event
, sizeof(struct input_event
)))
267 #endif /* CONFIG_COMPAT */
269 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
271 struct evdev_client
*client
= file
->private_data
;
272 struct evdev
*evdev
= client
->evdev
;
273 struct input_event event
;
279 while (retval
< count
) {
281 if (evdev_event_from_user(buffer
+ retval
, &event
))
283 input_inject_event(&evdev
->handle
, event
.type
, event
.code
, event
.value
);
284 retval
+= evdev_event_size();
290 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
292 struct evdev_client
*client
= file
->private_data
;
293 struct evdev
*evdev
= client
->evdev
;
296 if (count
< evdev_event_size())
299 if (client
->head
== client
->tail
&& evdev
->exist
&& (file
->f_flags
& O_NONBLOCK
))
302 retval
= wait_event_interruptible(evdev
->wait
,
303 client
->head
!= client
->tail
|| !evdev
->exist
);
310 while (client
->head
!= client
->tail
&& retval
+ evdev_event_size() <= count
) {
312 struct input_event
*event
= (struct input_event
*) client
->buffer
+ client
->tail
;
314 if (evdev_event_to_user(buffer
+ retval
, event
))
317 client
->tail
= (client
->tail
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
318 retval
+= evdev_event_size();
324 /* No kernel lock - fine */
325 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
327 struct evdev_client
*client
= file
->private_data
;
328 struct evdev
*evdev
= client
->evdev
;
330 poll_wait(file
, &evdev
->wait
, wait
);
331 return ((client
->head
== client
->tail
) ? 0 : (POLLIN
| POLLRDNORM
)) |
332 (evdev
->exist
? 0 : (POLLHUP
| POLLERR
));
337 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
338 #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
341 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
342 unsigned int maxlen
, void __user
*p
, int compat
)
347 len
= NBITS_COMPAT(maxbit
) * sizeof(compat_long_t
);
351 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
352 if (copy_to_user((compat_long_t __user
*) p
+ i
,
353 (compat_long_t
*) bits
+
354 i
+ 1 - ((i
% 2) << 1),
355 sizeof(compat_long_t
)))
358 len
= NBITS(maxbit
) * sizeof(long);
362 if (copy_to_user(p
, bits
, len
))
369 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
370 unsigned int maxlen
, void __user
*p
, int compat
)
373 NBITS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
374 NBITS(maxbit
) * sizeof(long);
379 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
381 #endif /* __BIG_ENDIAN */
385 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
386 unsigned int maxlen
, void __user
*p
, int compat
)
388 int len
= NBITS(maxbit
) * sizeof(long);
393 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
396 #endif /* CONFIG_COMPAT */
398 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
405 len
= strlen(str
) + 1;
409 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
412 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
413 void __user
*p
, int compat_mode
)
415 struct evdev_client
*client
= file
->private_data
;
416 struct evdev
*evdev
= client
->evdev
;
417 struct input_dev
*dev
= evdev
->handle
.dev
;
418 struct input_absinfo abs
;
419 struct ff_effect effect
;
420 int __user
*ip
= (int __user
*)p
;
430 return put_user(EV_VERSION
, ip
);
433 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
438 if (!test_bit(EV_REP
, dev
->evbit
))
440 if (put_user(dev
->rep
[REP_DELAY
], ip
))
442 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
447 if (!test_bit(EV_REP
, dev
->evbit
))
451 if (get_user(v
, ip
+ 1))
454 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
455 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
463 error
= dev
->getkeycode(dev
, t
, &v
);
467 if (put_user(v
, ip
+ 1))
473 if (get_user(t
, ip
) || get_user(v
, ip
+ 1))
476 return dev
->setkeycode(dev
, t
, v
);
479 if (copy_from_user(&effect
, p
, sizeof(effect
)))
482 error
= input_ff_upload(dev
, &effect
, file
);
484 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
490 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
493 i
= test_bit(EV_FF
, dev
->evbit
) ? dev
->ff
->max_effects
: 0;
502 if (input_grab_device(&evdev
->handle
))
504 evdev
->grab
= client
;
507 if (evdev
->grab
!= client
)
509 input_release_device(&evdev
->handle
);
516 if (_IOC_TYPE(cmd
) != 'E')
519 if (_IOC_DIR(cmd
) == _IOC_READ
) {
521 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0,0))) {
526 switch (_IOC_NR(cmd
) & EV_MAX
) {
527 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
528 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
529 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
530 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
531 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
532 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
533 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
534 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
535 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
536 default: return -EINVAL
;
538 return bits_to_user(bits
, len
, _IOC_SIZE(cmd
), p
, compat_mode
);
541 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGKEY(0)))
542 return bits_to_user(dev
->key
, KEY_MAX
, _IOC_SIZE(cmd
),
545 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGLED(0)))
546 return bits_to_user(dev
->led
, LED_MAX
, _IOC_SIZE(cmd
),
549 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSND(0)))
550 return bits_to_user(dev
->snd
, SND_MAX
, _IOC_SIZE(cmd
),
553 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSW(0)))
554 return bits_to_user(dev
->sw
, SW_MAX
, _IOC_SIZE(cmd
),
557 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGNAME(0)))
558 return str_to_user(dev
->name
, _IOC_SIZE(cmd
), p
);
560 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGPHYS(0)))
561 return str_to_user(dev
->phys
, _IOC_SIZE(cmd
), p
);
563 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGUNIQ(0)))
564 return str_to_user(dev
->uniq
, _IOC_SIZE(cmd
), p
);
566 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
568 t
= _IOC_NR(cmd
) & ABS_MAX
;
570 abs
.value
= dev
->abs
[t
];
571 abs
.minimum
= dev
->absmin
[t
];
572 abs
.maximum
= dev
->absmax
[t
];
573 abs
.fuzz
= dev
->absfuzz
[t
];
574 abs
.flat
= dev
->absflat
[t
];
576 if (copy_to_user(p
, &abs
, sizeof(struct input_absinfo
)))
584 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
586 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
588 t
= _IOC_NR(cmd
) & ABS_MAX
;
590 if (copy_from_user(&abs
, p
, sizeof(struct input_absinfo
)))
593 dev
->abs
[t
] = abs
.value
;
594 dev
->absmin
[t
] = abs
.minimum
;
595 dev
->absmax
[t
] = abs
.maximum
;
596 dev
->absfuzz
[t
] = abs
.fuzz
;
597 dev
->absflat
[t
] = abs
.flat
;
606 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
608 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
612 static long evdev_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
614 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
618 static const struct file_operations evdev_fops
= {
619 .owner
= THIS_MODULE
,
621 .write
= evdev_write
,
624 .release
= evdev_release
,
625 .unlocked_ioctl
= evdev_ioctl
,
627 .compat_ioctl
= evdev_ioctl_compat
,
629 .fasync
= evdev_fasync
,
633 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
634 const struct input_device_id
*id
)
640 for (minor
= 0; minor
< EVDEV_MINORS
&& evdev_table
[minor
]; minor
++);
641 if (minor
== EVDEV_MINORS
) {
642 printk(KERN_ERR
"evdev: no more free evdev devices\n");
646 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
650 INIT_LIST_HEAD(&evdev
->client_list
);
651 init_waitqueue_head(&evdev
->wait
);
654 evdev
->minor
= minor
;
655 evdev
->handle
.dev
= dev
;
656 evdev
->handle
.name
= evdev
->name
;
657 evdev
->handle
.handler
= handler
;
658 evdev
->handle
.private = evdev
;
659 snprintf(evdev
->name
, sizeof(evdev
->name
), "event%d", minor
);
661 snprintf(evdev
->dev
.bus_id
, sizeof(evdev
->dev
.bus_id
),
663 evdev
->dev
.class = &input_class
;
664 evdev
->dev
.parent
= &dev
->dev
;
665 evdev
->dev
.devt
= MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
);
666 evdev
->dev
.release
= evdev_free
;
667 device_initialize(&evdev
->dev
);
669 evdev_table
[minor
] = evdev
;
671 error
= device_add(&evdev
->dev
);
675 error
= input_register_handle(&evdev
->handle
);
677 goto err_delete_evdev
;
682 device_del(&evdev
->dev
);
684 put_device(&evdev
->dev
);
688 static void evdev_disconnect(struct input_handle
*handle
)
690 struct evdev
*evdev
= handle
->private;
691 struct evdev_client
*client
;
693 input_unregister_handle(handle
);
694 device_del(&evdev
->dev
);
699 input_flush_device(handle
, NULL
);
700 input_close_device(handle
);
701 list_for_each_entry(client
, &evdev
->client_list
, node
)
702 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
703 wake_up_interruptible(&evdev
->wait
);
706 put_device(&evdev
->dev
);
709 static const struct input_device_id evdev_ids
[] = {
710 { .driver_info
= 1 }, /* Matches all devices */
711 { }, /* Terminating zero entry */
714 MODULE_DEVICE_TABLE(input
, evdev_ids
);
716 static struct input_handler evdev_handler
= {
717 .event
= evdev_event
,
718 .connect
= evdev_connect
,
719 .disconnect
= evdev_disconnect
,
721 .minor
= EVDEV_MINOR_BASE
,
723 .id_table
= evdev_ids
,
726 static int __init
evdev_init(void)
728 return input_register_handler(&evdev_handler
);
731 static void __exit
evdev_exit(void)
733 input_unregister_handler(&evdev_handler
);
736 module_init(evdev_init
);
737 module_exit(evdev_exit
);
739 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
740 MODULE_DESCRIPTION("Input driver event char devices");
741 MODULE_LICENSE("GPL");