4 * Copyright (c) 1999-2002 Vojtech Pavlik
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/smp_lock.h>
16 #include <linux/input.h>
17 #include <linux/module.h>
18 #include <linux/random.h>
19 #include <linux/major.h>
20 #include <linux/proc_fs.h>
21 #include <linux/kobject_uevent.h>
22 #include <linux/interrupt.h>
23 #include <linux/poll.h>
24 #include <linux/device.h>
25 #include <linux/devfs_fs_kernel.h>
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28 MODULE_DESCRIPTION("Input core");
29 MODULE_LICENSE("GPL");
31 EXPORT_SYMBOL(input_register_device
);
32 EXPORT_SYMBOL(input_unregister_device
);
33 EXPORT_SYMBOL(input_register_handler
);
34 EXPORT_SYMBOL(input_unregister_handler
);
35 EXPORT_SYMBOL(input_grab_device
);
36 EXPORT_SYMBOL(input_release_device
);
37 EXPORT_SYMBOL(input_open_device
);
38 EXPORT_SYMBOL(input_close_device
);
39 EXPORT_SYMBOL(input_accept_process
);
40 EXPORT_SYMBOL(input_flush_device
);
41 EXPORT_SYMBOL(input_event
);
42 EXPORT_SYMBOL(input_class
);
44 #define INPUT_DEVICES 256
46 static LIST_HEAD(input_dev_list
);
47 static LIST_HEAD(input_handler_list
);
49 static struct input_handler
*input_table
[8];
52 static struct proc_dir_entry
*proc_bus_input_dir
;
53 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait
);
54 static int input_devices_state
;
57 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
59 struct input_handle
*handle
;
61 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
64 add_input_randomness(type
, code
, value
);
71 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
75 if (dev
->sync
) return;
83 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
89 change_bit(code
, dev
->key
);
91 if (test_bit(EV_REP
, dev
->evbit
) && dev
->rep
[REP_PERIOD
] && dev
->rep
[REP_DELAY
] && dev
->timer
.data
&& value
) {
92 dev
->repeat_key
= code
;
93 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_DELAY
]));
100 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
103 if (dev
->absfuzz
[code
]) {
104 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
105 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
108 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
109 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
110 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
112 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
113 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
114 value
= (dev
->abs
[code
] + value
) >> 1;
117 if (dev
->abs
[code
] == value
)
120 dev
->abs
[code
] = value
;
125 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
132 if (code
> MSC_MAX
|| !test_bit(code
, dev
->mscbit
))
135 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
141 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
144 change_bit(code
, dev
->led
);
145 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
151 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
))
154 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
160 if (code
> REP_MAX
|| value
< 0 || dev
->rep
[code
] == value
) return;
162 dev
->rep
[code
] = value
;
163 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
168 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
176 dev
->grab
->handler
->event(dev
->grab
, type
, code
, value
);
178 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
180 handle
->handler
->event(handle
, type
, code
, value
);
183 static void input_repeat_key(unsigned long data
)
185 struct input_dev
*dev
= (void *) data
;
187 if (!test_bit(dev
->repeat_key
, dev
->key
))
190 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
193 if (dev
->rep
[REP_PERIOD
])
194 mod_timer(&dev
->timer
, jiffies
+ msecs_to_jiffies(dev
->rep
[REP_PERIOD
]));
197 int input_accept_process(struct input_handle
*handle
, struct file
*file
)
199 if (handle
->dev
->accept
)
200 return handle
->dev
->accept(handle
->dev
, file
);
205 int input_grab_device(struct input_handle
*handle
)
207 if (handle
->dev
->grab
)
210 handle
->dev
->grab
= handle
;
214 void input_release_device(struct input_handle
*handle
)
216 if (handle
->dev
->grab
== handle
)
217 handle
->dev
->grab
= NULL
;
220 int input_open_device(struct input_handle
*handle
)
223 if (handle
->dev
->open
)
224 return handle
->dev
->open(handle
->dev
);
228 int input_flush_device(struct input_handle
* handle
, struct file
* file
)
230 if (handle
->dev
->flush
)
231 return handle
->dev
->flush(handle
->dev
, file
);
236 void input_close_device(struct input_handle
*handle
)
238 input_release_device(handle
);
239 if (handle
->dev
->close
)
240 handle
->dev
->close(handle
->dev
);
244 static void input_link_handle(struct input_handle
*handle
)
246 list_add_tail(&handle
->d_node
, &handle
->dev
->h_list
);
247 list_add_tail(&handle
->h_node
, &handle
->handler
->h_list
);
250 #define MATCH_BIT(bit, max) \
251 for (i = 0; i < NBITS(max); i++) \
252 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
254 if (i != NBITS(max)) \
257 static struct input_device_id
*input_match_device(struct input_device_id
*id
, struct input_dev
*dev
)
261 for (; id
->flags
|| id
->driver_info
; id
++) {
263 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
264 if (id
->id
.bustype
!= dev
->id
.bustype
)
267 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
268 if (id
->id
.vendor
!= dev
->id
.vendor
)
271 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
272 if (id
->id
.product
!= dev
->id
.product
)
275 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
276 if (id
->id
.version
!= dev
->id
.version
)
279 MATCH_BIT(evbit
, EV_MAX
);
280 MATCH_BIT(keybit
, KEY_MAX
);
281 MATCH_BIT(relbit
, REL_MAX
);
282 MATCH_BIT(absbit
, ABS_MAX
);
283 MATCH_BIT(mscbit
, MSC_MAX
);
284 MATCH_BIT(ledbit
, LED_MAX
);
285 MATCH_BIT(sndbit
, SND_MAX
);
286 MATCH_BIT(ffbit
, FF_MAX
);
295 * Input hotplugging interface - loading event handlers based on
299 #ifdef CONFIG_HOTPLUG
302 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
303 * (normally /sbin/hotplug) when input devices get added or removed.
305 * This invokes a user mode policy agent, typically helping to load driver
306 * or other modules, configure the device, and more. Drivers can provide
307 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
311 #define SPRINTF_BIT_A(bit, name, max) \
313 envp[i++] = scratch; \
314 scratch += sprintf(scratch, name); \
315 for (j = NBITS(max) - 1; j >= 0; j--) \
316 if (dev->bit[j]) break; \
317 for (; j >= 0; j--) \
318 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
322 #define SPRINTF_BIT_A2(bit, name, max, ev) \
324 if (test_bit(ev, dev->evbit)) \
325 SPRINTF_BIT_A(bit, name, max); \
328 static void input_call_hotplug(char *verb
, struct input_dev
*dev
)
330 char *argv
[3], **envp
, *buf
, *scratch
;
333 if (!hotplug_path
[0]) {
334 printk(KERN_ERR
"input.c: calling hotplug without a hotplug agent defined\n");
337 if (in_interrupt()) {
338 printk(KERN_ERR
"input.c: calling hotplug from interrupt\n");
341 if (!current
->fs
->root
) {
342 printk(KERN_WARNING
"input.c: calling hotplug without valid filesystem\n");
345 if (!(envp
= (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL
))) {
346 printk(KERN_ERR
"input.c: not enough memory allocating hotplug environment\n");
349 if (!(buf
= kmalloc(1024, GFP_KERNEL
))) {
351 printk(KERN_ERR
"input.c: not enough memory allocating hotplug environment\n");
355 argv
[0] = hotplug_path
;
359 envp
[i
++] = "HOME=/";
360 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
365 scratch
+= sprintf(scratch
, "ACTION=%s", verb
) + 1;
368 scratch
+= sprintf(scratch
, "PRODUCT=%x/%x/%x/%x",
369 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
) + 1;
373 scratch
+= sprintf(scratch
, "NAME=%s", dev
->name
) + 1;
378 scratch
+= sprintf(scratch
, "PHYS=%s", dev
->phys
) + 1;
381 SPRINTF_BIT_A(evbit
, "EV=", EV_MAX
);
382 SPRINTF_BIT_A2(keybit
, "KEY=", KEY_MAX
, EV_KEY
);
383 SPRINTF_BIT_A2(relbit
, "REL=", REL_MAX
, EV_REL
);
384 SPRINTF_BIT_A2(absbit
, "ABS=", ABS_MAX
, EV_ABS
);
385 SPRINTF_BIT_A2(mscbit
, "MSC=", MSC_MAX
, EV_MSC
);
386 SPRINTF_BIT_A2(ledbit
, "LED=", LED_MAX
, EV_LED
);
387 SPRINTF_BIT_A2(sndbit
, "SND=", SND_MAX
, EV_SND
);
388 SPRINTF_BIT_A2(ffbit
, "FF=", FF_MAX
, EV_FF
);
393 printk(KERN_DEBUG
"input.c: calling %s %s [%s %s %s %s %s]\n",
394 argv
[0], argv
[1], envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
397 value
= call_usermodehelper(argv
[0], argv
, envp
, 0);
404 printk(KERN_DEBUG
"input.c: hotplug returned %d\n", value
);
410 void input_register_device(struct input_dev
*dev
)
412 struct input_handle
*handle
;
413 struct input_handler
*handler
;
414 struct input_device_id
*id
;
416 set_bit(EV_SYN
, dev
->evbit
);
419 * If delay and period are pre-set by the driver, then autorepeating
420 * is handled by the driver itself and we don't do it in input.c.
423 init_timer(&dev
->timer
);
424 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
425 dev
->timer
.data
= (long) dev
;
426 dev
->timer
.function
= input_repeat_key
;
427 dev
->rep
[REP_DELAY
] = 250;
428 dev
->rep
[REP_PERIOD
] = 33;
431 INIT_LIST_HEAD(&dev
->h_list
);
432 list_add_tail(&dev
->node
, &input_dev_list
);
434 list_for_each_entry(handler
, &input_handler_list
, node
)
435 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
436 if ((id
= input_match_device(handler
->id_table
, dev
)))
437 if ((handle
= handler
->connect(handler
, dev
, id
)))
438 input_link_handle(handle
);
440 #ifdef CONFIG_HOTPLUG
441 input_call_hotplug("add", dev
);
444 #ifdef CONFIG_PROC_FS
445 input_devices_state
++;
446 wake_up(&input_devices_poll_wait
);
450 void input_unregister_device(struct input_dev
*dev
)
452 struct list_head
* node
, * next
;
456 del_timer_sync(&dev
->timer
);
458 list_for_each_safe(node
, next
, &dev
->h_list
) {
459 struct input_handle
* handle
= to_handle(node
);
460 list_del_init(&handle
->d_node
);
461 list_del_init(&handle
->h_node
);
462 handle
->handler
->disconnect(handle
);
465 #ifdef CONFIG_HOTPLUG
466 input_call_hotplug("remove", dev
);
469 list_del_init(&dev
->node
);
471 #ifdef CONFIG_PROC_FS
472 input_devices_state
++;
473 wake_up(&input_devices_poll_wait
);
477 void input_register_handler(struct input_handler
*handler
)
479 struct input_dev
*dev
;
480 struct input_handle
*handle
;
481 struct input_device_id
*id
;
483 if (!handler
) return;
485 INIT_LIST_HEAD(&handler
->h_list
);
487 if (handler
->fops
!= NULL
)
488 input_table
[handler
->minor
>> 5] = handler
;
490 list_add_tail(&handler
->node
, &input_handler_list
);
492 list_for_each_entry(dev
, &input_dev_list
, node
)
493 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
494 if ((id
= input_match_device(handler
->id_table
, dev
)))
495 if ((handle
= handler
->connect(handler
, dev
, id
)))
496 input_link_handle(handle
);
498 #ifdef CONFIG_PROC_FS
499 input_devices_state
++;
500 wake_up(&input_devices_poll_wait
);
504 void input_unregister_handler(struct input_handler
*handler
)
506 struct list_head
* node
, * next
;
508 list_for_each_safe(node
, next
, &handler
->h_list
) {
509 struct input_handle
* handle
= to_handle_h(node
);
510 list_del_init(&handle
->h_node
);
511 list_del_init(&handle
->d_node
);
512 handler
->disconnect(handle
);
515 list_del_init(&handler
->node
);
517 if (handler
->fops
!= NULL
)
518 input_table
[handler
->minor
>> 5] = NULL
;
520 #ifdef CONFIG_PROC_FS
521 input_devices_state
++;
522 wake_up(&input_devices_poll_wait
);
526 static int input_open_file(struct inode
*inode
, struct file
*file
)
528 struct input_handler
*handler
= input_table
[iminor(inode
) >> 5];
529 struct file_operations
*old_fops
, *new_fops
= NULL
;
532 /* No load-on-demand here? */
533 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
537 * That's _really_ odd. Usually NULL ->open means "nothing special",
538 * not "no device". Oh, well...
540 if (!new_fops
->open
) {
544 old_fops
= file
->f_op
;
545 file
->f_op
= new_fops
;
547 err
= new_fops
->open(inode
, file
);
550 fops_put(file
->f_op
);
551 file
->f_op
= fops_get(old_fops
);
557 static struct file_operations input_fops
= {
558 .owner
= THIS_MODULE
,
559 .open
= input_open_file
,
562 #ifdef CONFIG_PROC_FS
564 #define SPRINTF_BIT_B(bit, name, max) \
566 len += sprintf(buf + len, "B: %s", name); \
567 for (i = NBITS(max) - 1; i >= 0; i--) \
568 if (dev->bit[i]) break; \
569 for (; i >= 0; i--) \
570 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
571 len += sprintf(buf + len, "\n"); \
574 #define SPRINTF_BIT_B2(bit, name, max, ev) \
576 if (test_bit(ev, dev->evbit)) \
577 SPRINTF_BIT_B(bit, name, max); \
581 static unsigned int input_devices_poll(struct file
*file
, poll_table
*wait
)
583 int state
= input_devices_state
;
584 poll_wait(file
, &input_devices_poll_wait
, wait
);
585 if (state
!= input_devices_state
)
586 return POLLIN
| POLLRDNORM
;
590 static int input_devices_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
592 struct input_dev
*dev
;
593 struct input_handle
*handle
;
598 list_for_each_entry(dev
, &input_dev_list
, node
) {
600 len
= sprintf(buf
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
601 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
603 len
+= sprintf(buf
+ len
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
604 len
+= sprintf(buf
+ len
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
605 len
+= sprintf(buf
+ len
, "H: Handlers=");
607 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
608 len
+= sprintf(buf
+ len
, "%s ", handle
->name
);
610 len
+= sprintf(buf
+ len
, "\n");
612 SPRINTF_BIT_B(evbit
, "EV=", EV_MAX
);
613 SPRINTF_BIT_B2(keybit
, "KEY=", KEY_MAX
, EV_KEY
);
614 SPRINTF_BIT_B2(relbit
, "REL=", REL_MAX
, EV_REL
);
615 SPRINTF_BIT_B2(absbit
, "ABS=", ABS_MAX
, EV_ABS
);
616 SPRINTF_BIT_B2(mscbit
, "MSC=", MSC_MAX
, EV_MSC
);
617 SPRINTF_BIT_B2(ledbit
, "LED=", LED_MAX
, EV_LED
);
618 SPRINTF_BIT_B2(sndbit
, "SND=", SND_MAX
, EV_SND
);
619 SPRINTF_BIT_B2(ffbit
, "FF=", FF_MAX
, EV_FF
);
621 len
+= sprintf(buf
+ len
, "\n");
627 *start
= buf
+ (pos
- (at
- len
));
636 if (&dev
->node
== &input_dev_list
)
639 return (count
> cnt
) ? cnt
: count
;
642 static int input_handlers_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
644 struct input_handler
*handler
;
647 int len
= 0, cnt
= 0;
650 list_for_each_entry(handler
, &input_handler_list
, node
) {
653 len
= sprintf(buf
, "N: Number=%d Name=%s Minor=%d\n",
654 i
++, handler
->name
, handler
->minor
);
656 len
= sprintf(buf
, "N: Number=%d Name=%s\n",
663 *start
= buf
+ (pos
- (at
- len
));
671 if (&handler
->node
== &input_handler_list
)
674 return (count
> cnt
) ? cnt
: count
;
677 static int __init
input_proc_init(void)
679 struct proc_dir_entry
*entry
;
681 proc_bus_input_dir
= proc_mkdir("input", proc_bus
);
682 if (proc_bus_input_dir
== NULL
)
684 proc_bus_input_dir
->owner
= THIS_MODULE
;
685 entry
= create_proc_read_entry("devices", 0, proc_bus_input_dir
, input_devices_read
, NULL
);
687 remove_proc_entry("input", proc_bus
);
690 entry
->owner
= THIS_MODULE
;
691 entry
->proc_fops
->poll
= input_devices_poll
;
692 entry
= create_proc_read_entry("handlers", 0, proc_bus_input_dir
, input_handlers_read
, NULL
);
694 remove_proc_entry("devices", proc_bus_input_dir
);
695 remove_proc_entry("input", proc_bus
);
698 entry
->owner
= THIS_MODULE
;
701 #else /* !CONFIG_PROC_FS */
702 static inline int input_proc_init(void) { return 0; }
705 struct class_simple
*input_class
;
707 static int __init
input_init(void)
709 int retval
= -ENOMEM
;
711 input_class
= class_simple_create(THIS_MODULE
, "input");
712 if (IS_ERR(input_class
))
713 return PTR_ERR(input_class
);
715 retval
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
717 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
718 remove_proc_entry("devices", proc_bus_input_dir
);
719 remove_proc_entry("handlers", proc_bus_input_dir
);
720 remove_proc_entry("input", proc_bus
);
721 class_simple_destroy(input_class
);
725 retval
= devfs_mk_dir("input");
727 remove_proc_entry("devices", proc_bus_input_dir
);
728 remove_proc_entry("handlers", proc_bus_input_dir
);
729 remove_proc_entry("input", proc_bus
);
730 unregister_chrdev(INPUT_MAJOR
, "input");
731 class_simple_destroy(input_class
);
736 static void __exit
input_exit(void)
738 remove_proc_entry("devices", proc_bus_input_dir
);
739 remove_proc_entry("handlers", proc_bus_input_dir
);
740 remove_proc_entry("input", proc_bus
);
742 devfs_remove("input");
743 unregister_chrdev(INPUT_MAJOR
, "input");
744 class_simple_destroy(input_class
);
747 subsys_initcall(input_init
);
748 module_exit(input_exit
);