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
;
36 struct input_event buffer
[EVDEV_BUFFER_SIZE
];
39 struct fasync_struct
*fasync
;
41 struct list_head node
;
44 static struct evdev
*evdev_table
[EVDEV_MINORS
];
46 static void evdev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
48 struct evdev
*evdev
= handle
->private;
49 struct evdev_client
*client
;
54 do_gettimeofday(&client
->buffer
[client
->head
].time
);
55 client
->buffer
[client
->head
].type
= type
;
56 client
->buffer
[client
->head
].code
= code
;
57 client
->buffer
[client
->head
].value
= value
;
58 client
->head
= (client
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
60 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
62 list_for_each_entry(client
, &evdev
->client_list
, node
) {
64 do_gettimeofday(&client
->buffer
[client
->head
].time
);
65 client
->buffer
[client
->head
].type
= type
;
66 client
->buffer
[client
->head
].code
= code
;
67 client
->buffer
[client
->head
].value
= value
;
68 client
->head
= (client
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
70 kill_fasync(&client
->fasync
, SIGIO
, POLL_IN
);
73 wake_up_interruptible(&evdev
->wait
);
76 static int evdev_fasync(int fd
, struct file
*file
, int on
)
78 struct evdev_client
*client
= file
->private_data
;
81 retval
= fasync_helper(fd
, file
, on
, &client
->fasync
);
83 return retval
< 0 ? retval
: 0;
86 static int evdev_flush(struct file
*file
, fl_owner_t id
)
88 struct evdev_client
*client
= file
->private_data
;
89 struct evdev
*evdev
= client
->evdev
;
94 return input_flush_device(&evdev
->handle
, file
);
97 static void evdev_free(struct evdev
*evdev
)
99 evdev_table
[evdev
->minor
] = NULL
;
103 static int evdev_release(struct inode
*inode
, struct file
*file
)
105 struct evdev_client
*client
= file
->private_data
;
106 struct evdev
*evdev
= client
->evdev
;
108 if (evdev
->grab
== client
) {
109 input_release_device(&evdev
->handle
);
113 evdev_fasync(-1, file
, 0);
114 list_del(&client
->node
);
117 if (!--evdev
->open
) {
119 input_close_device(&evdev
->handle
);
127 static int evdev_open(struct inode
*inode
, struct file
*file
)
129 struct evdev_client
*client
;
131 int i
= iminor(inode
) - EVDEV_MINOR_BASE
;
134 if (i
>= EVDEV_MINORS
)
137 evdev
= evdev_table
[i
];
139 if (!evdev
|| !evdev
->exist
)
142 client
= kzalloc(sizeof(struct evdev_client
), GFP_KERNEL
);
146 client
->evdev
= evdev
;
147 list_add_tail(&client
->node
, &evdev
->client_list
);
149 if (!evdev
->open
++ && evdev
->exist
) {
150 error
= input_open_device(&evdev
->handle
);
152 list_del(&client
->node
);
158 file
->private_data
= client
;
164 struct input_event_compat
{
165 struct compat_timeval time
;
171 /* Note to the author of this code: did it ever occur to
172 you why the ifdefs are needed? Think about it again. -AK */
174 # define COMPAT_TEST is_compat_task()
175 #elif defined(CONFIG_IA64)
176 # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
177 #elif defined(CONFIG_S390)
178 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
179 #elif defined(CONFIG_MIPS)
180 # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
182 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
185 static inline size_t evdev_event_size(void)
188 sizeof(struct input_event_compat
) : sizeof(struct input_event
);
191 static int evdev_event_from_user(const char __user
*buffer
, struct input_event
*event
)
194 struct input_event_compat compat_event
;
196 if (copy_from_user(&compat_event
, buffer
, sizeof(struct input_event_compat
)))
199 event
->time
.tv_sec
= compat_event
.time
.tv_sec
;
200 event
->time
.tv_usec
= compat_event
.time
.tv_usec
;
201 event
->type
= compat_event
.type
;
202 event
->code
= compat_event
.code
;
203 event
->value
= compat_event
.value
;
206 if (copy_from_user(event
, buffer
, sizeof(struct input_event
)))
213 static int evdev_event_to_user(char __user
*buffer
, const struct input_event
*event
)
216 struct input_event_compat compat_event
;
218 compat_event
.time
.tv_sec
= event
->time
.tv_sec
;
219 compat_event
.time
.tv_usec
= event
->time
.tv_usec
;
220 compat_event
.type
= event
->type
;
221 compat_event
.code
= event
->code
;
222 compat_event
.value
= event
->value
;
224 if (copy_to_user(buffer
, &compat_event
, sizeof(struct input_event_compat
)))
228 if (copy_to_user(buffer
, event
, sizeof(struct input_event
)))
237 static inline size_t evdev_event_size(void)
239 return sizeof(struct input_event
);
242 static int evdev_event_from_user(const char __user
*buffer
, struct input_event
*event
)
244 if (copy_from_user(event
, buffer
, sizeof(struct input_event
)))
250 static int evdev_event_to_user(char __user
*buffer
, const struct input_event
*event
)
252 if (copy_to_user(buffer
, event
, sizeof(struct input_event
)))
258 #endif /* CONFIG_COMPAT */
260 static ssize_t
evdev_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
262 struct evdev_client
*client
= file
->private_data
;
263 struct evdev
*evdev
= client
->evdev
;
264 struct input_event event
;
270 while (retval
< count
) {
272 if (evdev_event_from_user(buffer
+ retval
, &event
))
274 input_inject_event(&evdev
->handle
, event
.type
, event
.code
, event
.value
);
275 retval
+= evdev_event_size();
281 static ssize_t
evdev_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
283 struct evdev_client
*client
= file
->private_data
;
284 struct evdev
*evdev
= client
->evdev
;
287 if (count
< evdev_event_size())
290 if (client
->head
== client
->tail
&& evdev
->exist
&& (file
->f_flags
& O_NONBLOCK
))
293 retval
= wait_event_interruptible(evdev
->wait
,
294 client
->head
!= client
->tail
|| !evdev
->exist
);
301 while (client
->head
!= client
->tail
&& retval
+ evdev_event_size() <= count
) {
303 struct input_event
*event
= (struct input_event
*) client
->buffer
+ client
->tail
;
305 if (evdev_event_to_user(buffer
+ retval
, event
))
308 client
->tail
= (client
->tail
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
309 retval
+= evdev_event_size();
315 /* No kernel lock - fine */
316 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
318 struct evdev_client
*client
= file
->private_data
;
319 struct evdev
*evdev
= client
->evdev
;
321 poll_wait(file
, &evdev
->wait
, wait
);
322 return ((client
->head
== client
->tail
) ? 0 : (POLLIN
| POLLRDNORM
)) |
323 (evdev
->exist
? 0 : (POLLHUP
| POLLERR
));
328 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
329 #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
332 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
333 unsigned int maxlen
, void __user
*p
, int compat
)
338 len
= NBITS_COMPAT(maxbit
) * sizeof(compat_long_t
);
342 for (i
= 0; i
< len
/ sizeof(compat_long_t
); i
++)
343 if (copy_to_user((compat_long_t __user
*) p
+ i
,
344 (compat_long_t
*) bits
+
345 i
+ 1 - ((i
% 2) << 1),
346 sizeof(compat_long_t
)))
349 len
= NBITS(maxbit
) * sizeof(long);
353 if (copy_to_user(p
, bits
, len
))
360 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
361 unsigned int maxlen
, void __user
*p
, int compat
)
364 NBITS_COMPAT(maxbit
) * sizeof(compat_long_t
) :
365 NBITS(maxbit
) * sizeof(long);
370 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
372 #endif /* __BIG_ENDIAN */
376 static int bits_to_user(unsigned long *bits
, unsigned int maxbit
,
377 unsigned int maxlen
, void __user
*p
, int compat
)
379 int len
= NBITS(maxbit
) * sizeof(long);
384 return copy_to_user(p
, bits
, len
) ? -EFAULT
: len
;
387 #endif /* CONFIG_COMPAT */
389 static int str_to_user(const char *str
, unsigned int maxlen
, void __user
*p
)
396 len
= strlen(str
) + 1;
400 return copy_to_user(p
, str
, len
) ? -EFAULT
: len
;
403 static long evdev_ioctl_handler(struct file
*file
, unsigned int cmd
,
404 void __user
*p
, int compat_mode
)
406 struct evdev_client
*client
= file
->private_data
;
407 struct evdev
*evdev
= client
->evdev
;
408 struct input_dev
*dev
= evdev
->handle
.dev
;
409 struct input_absinfo abs
;
410 struct ff_effect effect
;
411 int __user
*ip
= (int __user
*)p
;
421 return put_user(EV_VERSION
, ip
);
424 if (copy_to_user(p
, &dev
->id
, sizeof(struct input_id
)))
429 if (!test_bit(EV_REP
, dev
->evbit
))
431 if (put_user(dev
->rep
[REP_DELAY
], ip
))
433 if (put_user(dev
->rep
[REP_PERIOD
], ip
+ 1))
438 if (!test_bit(EV_REP
, dev
->evbit
))
442 if (get_user(v
, ip
+ 1))
445 input_inject_event(&evdev
->handle
, EV_REP
, REP_DELAY
, u
);
446 input_inject_event(&evdev
->handle
, EV_REP
, REP_PERIOD
, v
);
454 error
= dev
->getkeycode(dev
, t
, &v
);
458 if (put_user(v
, ip
+ 1))
464 if (get_user(t
, ip
) || get_user(v
, ip
+ 1))
467 return dev
->setkeycode(dev
, t
, v
);
470 if (copy_from_user(&effect
, p
, sizeof(effect
)))
473 error
= input_ff_upload(dev
, &effect
, file
);
475 if (put_user(effect
.id
, &(((struct ff_effect __user
*)p
)->id
)))
481 return input_ff_erase(dev
, (int)(unsigned long) p
, file
);
484 i
= test_bit(EV_FF
, dev
->evbit
) ? dev
->ff
->max_effects
: 0;
493 if (input_grab_device(&evdev
->handle
))
495 evdev
->grab
= client
;
498 if (evdev
->grab
!= client
)
500 input_release_device(&evdev
->handle
);
507 if (_IOC_TYPE(cmd
) != 'E')
510 if (_IOC_DIR(cmd
) == _IOC_READ
) {
512 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0,0))) {
517 switch (_IOC_NR(cmd
) & EV_MAX
) {
518 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
519 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
520 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
521 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
522 case EV_MSC
: bits
= dev
->mscbit
; len
= MSC_MAX
; break;
523 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
524 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
525 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
526 case EV_SW
: bits
= dev
->swbit
; len
= SW_MAX
; break;
527 default: return -EINVAL
;
529 return bits_to_user(bits
, len
, _IOC_SIZE(cmd
), p
, compat_mode
);
532 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGKEY(0)))
533 return bits_to_user(dev
->key
, KEY_MAX
, _IOC_SIZE(cmd
),
536 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGLED(0)))
537 return bits_to_user(dev
->led
, LED_MAX
, _IOC_SIZE(cmd
),
540 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSND(0)))
541 return bits_to_user(dev
->snd
, SND_MAX
, _IOC_SIZE(cmd
),
544 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSW(0)))
545 return bits_to_user(dev
->sw
, SW_MAX
, _IOC_SIZE(cmd
),
548 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGNAME(0)))
549 return str_to_user(dev
->name
, _IOC_SIZE(cmd
), p
);
551 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGPHYS(0)))
552 return str_to_user(dev
->phys
, _IOC_SIZE(cmd
), p
);
554 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGUNIQ(0)))
555 return str_to_user(dev
->uniq
, _IOC_SIZE(cmd
), p
);
557 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
559 t
= _IOC_NR(cmd
) & ABS_MAX
;
561 abs
.value
= dev
->abs
[t
];
562 abs
.minimum
= dev
->absmin
[t
];
563 abs
.maximum
= dev
->absmax
[t
];
564 abs
.fuzz
= dev
->absfuzz
[t
];
565 abs
.flat
= dev
->absflat
[t
];
567 if (copy_to_user(p
, &abs
, sizeof(struct input_absinfo
)))
575 if (_IOC_DIR(cmd
) == _IOC_WRITE
) {
577 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
579 t
= _IOC_NR(cmd
) & ABS_MAX
;
581 if (copy_from_user(&abs
, p
, sizeof(struct input_absinfo
)))
584 dev
->abs
[t
] = abs
.value
;
585 dev
->absmin
[t
] = abs
.minimum
;
586 dev
->absmax
[t
] = abs
.maximum
;
587 dev
->absfuzz
[t
] = abs
.fuzz
;
588 dev
->absflat
[t
] = abs
.flat
;
597 static long evdev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
599 return evdev_ioctl_handler(file
, cmd
, (void __user
*)arg
, 0);
603 static long evdev_ioctl_compat(struct file
*file
, unsigned int cmd
, unsigned long arg
)
605 return evdev_ioctl_handler(file
, cmd
, compat_ptr(arg
), 1);
609 static const struct file_operations evdev_fops
= {
610 .owner
= THIS_MODULE
,
612 .write
= evdev_write
,
615 .release
= evdev_release
,
616 .unlocked_ioctl
= evdev_ioctl
,
618 .compat_ioctl
= evdev_ioctl_compat
,
620 .fasync
= evdev_fasync
,
624 static int evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
,
625 const struct input_device_id
*id
)
628 struct class_device
*cdev
;
633 for (minor
= 0; minor
< EVDEV_MINORS
&& evdev_table
[minor
]; minor
++);
634 if (minor
== EVDEV_MINORS
) {
635 printk(KERN_ERR
"evdev: no more free evdev devices\n");
639 evdev
= kzalloc(sizeof(struct evdev
), GFP_KERNEL
);
643 INIT_LIST_HEAD(&evdev
->client_list
);
644 init_waitqueue_head(&evdev
->wait
);
647 evdev
->minor
= minor
;
648 evdev
->handle
.dev
= dev
;
649 evdev
->handle
.name
= evdev
->name
;
650 evdev
->handle
.handler
= handler
;
651 evdev
->handle
.private = evdev
;
652 sprintf(evdev
->name
, "event%d", minor
);
654 evdev_table
[minor
] = evdev
;
656 devt
= MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
),
658 cdev
= class_device_create(&input_class
, &dev
->cdev
, devt
,
659 dev
->cdev
.dev
, evdev
->name
);
661 error
= PTR_ERR(cdev
);
665 /* temporary symlink to keep userspace happy */
666 error
= sysfs_create_link(&input_class
.subsys
.kobj
,
667 &cdev
->kobj
, evdev
->name
);
669 goto err_cdev_destroy
;
671 error
= input_register_handle(&evdev
->handle
);
673 goto err_remove_link
;
678 sysfs_remove_link(&input_class
.subsys
.kobj
, evdev
->name
);
680 class_device_destroy(&input_class
, devt
);
683 evdev_table
[minor
] = NULL
;
687 static void evdev_disconnect(struct input_handle
*handle
)
689 struct evdev
*evdev
= handle
->private;
690 struct evdev_client
*client
;
692 input_unregister_handle(handle
);
694 sysfs_remove_link(&input_class
.subsys
.kobj
, evdev
->name
);
695 class_device_destroy(&input_class
,
696 MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ evdev
->minor
));
700 input_flush_device(handle
, NULL
);
701 input_close_device(handle
);
702 list_for_each_entry(client
, &evdev
->client_list
, node
)
703 kill_fasync(&client
->fasync
, SIGIO
, POLL_HUP
);
704 wake_up_interruptible(&evdev
->wait
);
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");