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/smp_lock.h>
22 #include <linux/device.h>
23 #include <linux/devfs_fs_kernel.h>
30 struct input_handle handle
;
31 wait_queue_head_t wait
;
32 struct evdev_list
*grab
;
33 struct list_head 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_list
*list
;
55 do_gettimeofday(&list
->buffer
[list
->head
].time
);
56 list
->buffer
[list
->head
].type
= type
;
57 list
->buffer
[list
->head
].code
= code
;
58 list
->buffer
[list
->head
].value
= value
;
59 list
->head
= (list
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
61 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
63 list_for_each_entry(list
, &evdev
->list
, node
) {
65 do_gettimeofday(&list
->buffer
[list
->head
].time
);
66 list
->buffer
[list
->head
].type
= type
;
67 list
->buffer
[list
->head
].code
= code
;
68 list
->buffer
[list
->head
].value
= value
;
69 list
->head
= (list
->head
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
71 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
74 wake_up_interruptible(&evdev
->wait
);
77 static int evdev_fasync(int fd
, struct file
*file
, int on
)
80 struct evdev_list
*list
= file
->private_data
;
81 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
82 return retval
< 0 ? retval
: 0;
85 static int evdev_flush(struct file
* file
)
87 struct evdev_list
*list
= file
->private_data
;
88 if (!list
->evdev
->exist
) return -ENODEV
;
89 return input_flush_device(&list
->evdev
->handle
, file
);
92 static void evdev_free(struct evdev
*evdev
)
94 devfs_remove("input/event%d", evdev
->minor
);
95 evdev_table
[evdev
->minor
] = NULL
;
99 static int evdev_release(struct inode
* inode
, struct file
* file
)
101 struct evdev_list
*list
= file
->private_data
;
103 if (list
->evdev
->grab
== list
) {
104 input_release_device(&list
->evdev
->handle
);
105 list
->evdev
->grab
= NULL
;
108 evdev_fasync(-1, file
, 0);
109 list_del(&list
->node
);
111 if (!--list
->evdev
->open
) {
112 if (list
->evdev
->exist
)
113 input_close_device(&list
->evdev
->handle
);
115 evdev_free(list
->evdev
);
122 static int evdev_open(struct inode
* inode
, struct file
* file
)
124 struct evdev_list
*list
;
125 int i
= minor(inode
->i_rdev
) - EVDEV_MINOR_BASE
;
128 if (i
>= EVDEV_MINORS
|| !evdev_table
[i
])
131 if ((accept_err
= input_accept_process(&(evdev_table
[i
]->handle
), file
)))
134 if (!(list
= kmalloc(sizeof(struct evdev_list
), GFP_KERNEL
)))
136 memset(list
, 0, sizeof(struct evdev_list
));
138 list
->evdev
= evdev_table
[i
];
139 list_add_tail(&list
->node
, &evdev_table
[i
]->list
);
140 file
->private_data
= list
;
142 if (!list
->evdev
->open
++)
143 if (list
->evdev
->exist
)
144 input_open_device(&list
->evdev
->handle
);
149 static ssize_t
evdev_write(struct file
* file
, const char * buffer
, size_t count
, loff_t
*ppos
)
151 struct evdev_list
*list
= file
->private_data
;
152 struct input_event event
;
155 if (!list
->evdev
->exist
) return -ENODEV
;
157 while (retval
< count
) {
159 if (copy_from_user(&event
, buffer
+ retval
, sizeof(struct input_event
)))
161 input_event(list
->evdev
->handle
.dev
, event
.type
, event
.code
, event
.value
);
162 retval
+= sizeof(struct input_event
);
168 static ssize_t
evdev_read(struct file
* file
, char * buffer
, size_t count
, loff_t
*ppos
)
170 struct evdev_list
*list
= file
->private_data
;
173 if (list
->head
== list
->tail
&& list
->evdev
->exist
&& (file
->f_flags
& O_NONBLOCK
))
176 retval
= wait_event_interruptible(list
->evdev
->wait
,
177 list
->head
!= list
->tail
&& list
->evdev
->exist
);
182 if (!list
->evdev
->exist
)
185 while (list
->head
!= list
->tail
&& retval
+ sizeof(struct input_event
) <= count
) {
186 if (copy_to_user(buffer
+ retval
, list
->buffer
+ list
->tail
,
187 sizeof(struct input_event
))) return -EFAULT
;
188 list
->tail
= (list
->tail
+ 1) & (EVDEV_BUFFER_SIZE
- 1);
189 retval
+= sizeof(struct input_event
);
195 /* No kernel lock - fine */
196 static unsigned int evdev_poll(struct file
*file
, poll_table
*wait
)
198 struct evdev_list
*list
= file
->private_data
;
199 poll_wait(file
, &list
->evdev
->wait
, wait
);
200 if (list
->head
!= list
->tail
)
201 return POLLIN
| POLLRDNORM
;
205 static int evdev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
207 struct evdev_list
*list
= file
->private_data
;
208 struct evdev
*evdev
= list
->evdev
;
209 struct input_dev
*dev
= evdev
->handle
.dev
;
210 struct input_absinfo abs
;
213 if (!evdev
->exist
) return -ENODEV
;
218 return put_user(EV_VERSION
, (int *) arg
);
221 return copy_to_user((void *) arg
, &dev
->id
, sizeof(struct input_id
));
224 if (put_user(dev
->rep
[0], ((int *) arg
) + 0)) return -EFAULT
;
225 if (put_user(dev
->rep
[1], ((int *) arg
) + 1)) return -EFAULT
;
229 if (get_user(dev
->rep
[0], ((int *) arg
) + 0)) return -EFAULT
;
230 if (get_user(dev
->rep
[1], ((int *) arg
) + 1)) return -EFAULT
;
234 if (get_user(t
, ((int *) arg
) + 0)) return -EFAULT
;
235 if (t
< 0 || t
> dev
->keycodemax
|| !dev
->keycodesize
) return -EINVAL
;
236 if (put_user(INPUT_KEYCODE(dev
, t
), ((int *) arg
) + 1)) return -EFAULT
;
240 if (get_user(t
, ((int *) arg
) + 0)) return -EFAULT
;
241 if (t
< 0 || t
> dev
->keycodemax
|| !dev
->keycodesize
) return -EINVAL
;
242 u
= INPUT_KEYCODE(dev
, t
);
243 if (get_user(INPUT_KEYCODE(dev
, t
), ((int *) arg
) + 1)) return -EFAULT
;
245 for (i
= 0; i
< dev
->keycodemax
; i
++)
246 if(INPUT_KEYCODE(dev
, t
) == u
) break;
247 if (i
== dev
->keycodemax
) clear_bit(u
, dev
->keybit
);
248 set_bit(INPUT_KEYCODE(dev
, t
), dev
->keybit
);
253 if (dev
->upload_effect
) {
254 struct ff_effect effect
;
257 if (copy_from_user((void*)(&effect
), (void*)arg
, sizeof(effect
)))
259 err
= dev
->upload_effect(dev
, &effect
);
260 if (put_user(effect
.id
, &(((struct ff_effect
*)arg
)->id
)))
267 if (dev
->erase_effect
) {
268 return dev
->erase_effect(dev
, (int)arg
);
273 if (put_user(dev
->ff_effects_max
, (int*) arg
))
281 if (input_grab_device(&evdev
->handle
))
286 if (evdev
->grab
!= list
)
288 input_release_device(&evdev
->handle
);
295 if (_IOC_TYPE(cmd
) != 'E' || _IOC_DIR(cmd
) != _IOC_READ
)
298 if ((_IOC_NR(cmd
) & ~EV_MAX
) == _IOC_NR(EVIOCGBIT(0,0))) {
303 switch (_IOC_NR(cmd
) & EV_MAX
) {
304 case 0: bits
= dev
->evbit
; len
= EV_MAX
; break;
305 case EV_KEY
: bits
= dev
->keybit
; len
= KEY_MAX
; break;
306 case EV_REL
: bits
= dev
->relbit
; len
= REL_MAX
; break;
307 case EV_ABS
: bits
= dev
->absbit
; len
= ABS_MAX
; break;
308 case EV_LED
: bits
= dev
->ledbit
; len
= LED_MAX
; break;
309 case EV_SND
: bits
= dev
->sndbit
; len
= SND_MAX
; break;
310 case EV_FF
: bits
= dev
->ffbit
; len
= FF_MAX
; break;
311 default: return -EINVAL
;
313 len
= NBITS(len
) * sizeof(long);
314 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
315 return copy_to_user((char *) arg
, bits
, len
) ? -EFAULT
: len
;
318 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGKEY(0))) {
320 len
= NBITS(KEY_MAX
) * sizeof(long);
321 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
322 return copy_to_user((char *) arg
, dev
->key
, len
) ? -EFAULT
: len
;
325 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGLED(0))) {
327 len
= NBITS(LED_MAX
) * sizeof(long);
328 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
329 return copy_to_user((char *) arg
, dev
->led
, len
) ? -EFAULT
: len
;
332 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGSND(0))) {
334 len
= NBITS(SND_MAX
) * sizeof(long);
335 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
336 return copy_to_user((char *) arg
, dev
->snd
, len
) ? -EFAULT
: len
;
339 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGNAME(0))) {
341 if (!dev
->name
) return -ENOENT
;
342 len
= strlen(dev
->name
) + 1;
343 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
344 return copy_to_user((char *) arg
, dev
->name
, len
) ? -EFAULT
: len
;
347 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGPHYS(0))) {
349 if (!dev
->phys
) return -ENOENT
;
350 len
= strlen(dev
->phys
) + 1;
351 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
352 return copy_to_user((char *) arg
, dev
->phys
, len
) ? -EFAULT
: len
;
355 if (_IOC_NR(cmd
) == _IOC_NR(EVIOCGUNIQ(0))) {
357 if (!dev
->uniq
) return -ENOENT
;
358 len
= strlen(dev
->uniq
) + 1;
359 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
360 return copy_to_user((char *) arg
, dev
->uniq
, len
) ? -EFAULT
: len
;
363 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCGABS(0))) {
365 int t
= _IOC_NR(cmd
) & ABS_MAX
;
367 abs
.value
= dev
->abs
[t
];
368 abs
.minimum
= dev
->absmin
[t
];
369 abs
.maximum
= dev
->absmax
[t
];
370 abs
.fuzz
= dev
->absfuzz
[t
];
371 abs
.flat
= dev
->absflat
[t
];
373 if (copy_to_user((void *) arg
, &abs
, sizeof(struct input_absinfo
)))
379 if ((_IOC_NR(cmd
) & ~ABS_MAX
) == _IOC_NR(EVIOCSABS(0))) {
381 int t
= _IOC_NR(cmd
) & ABS_MAX
;
383 if (copy_from_user(&abs
, (void *) arg
, sizeof(struct input_absinfo
)))
386 dev
->abs
[t
] = abs
.value
;
387 dev
->absmin
[t
] = abs
.minimum
;
388 dev
->absmax
[t
] = abs
.maximum
;
389 dev
->absfuzz
[t
] = abs
.fuzz
;
390 dev
->absflat
[t
] = abs
.flat
;
398 static struct file_operations evdev_fops
= {
399 .owner
= THIS_MODULE
,
401 .write
= evdev_write
,
404 .release
= evdev_release
,
405 .ioctl
= evdev_ioctl
,
406 .fasync
= evdev_fasync
,
410 static struct input_handle
*evdev_connect(struct input_handler
*handler
, struct input_dev
*dev
, struct input_device_id
*id
)
415 for (minor
= 0; minor
< EVDEV_MINORS
&& evdev_table
[minor
]; minor
++);
416 if (minor
== EVDEV_MINORS
) {
417 printk(KERN_ERR
"evdev: no more free evdev devices\n");
421 if (!(evdev
= kmalloc(sizeof(struct evdev
), GFP_KERNEL
)))
423 memset(evdev
, 0, sizeof(struct evdev
));
425 INIT_LIST_HEAD(&evdev
->list
);
426 init_waitqueue_head(&evdev
->wait
);
429 evdev
->minor
= minor
;
430 evdev
->handle
.dev
= dev
;
431 evdev
->handle
.name
= evdev
->name
;
432 evdev
->handle
.handler
= handler
;
433 evdev
->handle
.private = evdev
;
434 sprintf(evdev
->name
, "event%d", minor
);
436 evdev_table
[minor
] = evdev
;
438 devfs_mk_cdev(MKDEV(INPUT_MAJOR
, EVDEV_MINOR_BASE
+ minor
),
439 S_IFCHR
|S_IRUGO
|S_IWUSR
, "input/event%d", minor
);
441 return &evdev
->handle
;
444 static void evdev_disconnect(struct input_handle
*handle
)
446 struct evdev
*evdev
= handle
->private;
451 input_close_device(handle
);
452 wake_up_interruptible(&evdev
->wait
);
457 static struct input_device_id evdev_ids
[] = {
458 { .driver_info
= 1 }, /* Matches all devices */
459 { }, /* Terminating zero entry */
462 MODULE_DEVICE_TABLE(input
, evdev_ids
);
464 static struct input_handler evdev_handler
= {
465 .event
= evdev_event
,
466 .connect
= evdev_connect
,
467 .disconnect
= evdev_disconnect
,
469 .minor
= EVDEV_MINOR_BASE
,
471 .id_table
= evdev_ids
,
474 static int __init
evdev_init(void)
476 input_register_handler(&evdev_handler
);
480 static void __exit
evdev_exit(void)
482 input_unregister_handler(&evdev_handler
);
485 module_init(evdev_init
);
486 module_exit(evdev_exit
);
488 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
489 MODULE_DESCRIPTION("Input driver event char devices");
490 MODULE_LICENSE("GPL");