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>
21 #include <linux/proc_fs.h>
22 #include <linux/kmod.h>
23 #include <linux/interrupt.h>
24 #include <linux/poll.h>
25 #include <linux/device.h>
26 #include <linux/devfs_fs_kernel.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29 MODULE_DESCRIPTION("Input core");
30 MODULE_LICENSE("GPL");
32 EXPORT_SYMBOL(input_register_device
);
33 EXPORT_SYMBOL(input_unregister_device
);
34 EXPORT_SYMBOL(input_register_handler
);
35 EXPORT_SYMBOL(input_unregister_handler
);
36 EXPORT_SYMBOL(input_grab_device
);
37 EXPORT_SYMBOL(input_release_device
);
38 EXPORT_SYMBOL(input_open_device
);
39 EXPORT_SYMBOL(input_close_device
);
40 EXPORT_SYMBOL(input_accept_process
);
41 EXPORT_SYMBOL(input_flush_device
);
42 EXPORT_SYMBOL(input_event
);
43 EXPORT_SYMBOL(input_class
);
45 #define INPUT_DEVICES 256
47 static LIST_HEAD(input_dev_list
);
48 static LIST_HEAD(input_handler_list
);
50 static struct input_handler
*input_table
[8];
53 static struct proc_dir_entry
*proc_bus_input_dir
;
54 DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait
);
55 static int input_devices_state
;
58 static inline unsigned int ms_to_jiffies(unsigned int ms
)
61 j
= (ms
* HZ
+ 500) / 1000;
62 return (j
> 0) ? j
: 1;
66 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
68 struct input_handle
*handle
;
71 pm_access(dev
->pm_dev
);
73 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
76 add_mouse_randomness((type
<< 4) ^ code
^ (code
>> 4) ^ value
);
83 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
87 if (dev
->sync
) return;
95 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
101 change_bit(code
, dev
->key
);
103 if (test_bit(EV_REP
, dev
->evbit
) && dev
->rep
[REP_PERIOD
] && dev
->timer
.data
&& value
) {
104 dev
->repeat_key
= code
;
105 mod_timer(&dev
->timer
, jiffies
+ ms_to_jiffies(dev
->rep
[REP_DELAY
]));
112 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
115 if (dev
->absfuzz
[code
]) {
116 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
117 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
120 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
121 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
122 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
124 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
125 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
126 value
= (dev
->abs
[code
] + value
) >> 1;
129 if (dev
->abs
[code
] == value
)
132 dev
->abs
[code
] = value
;
137 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
144 if (code
> MSC_MAX
|| !test_bit(code
, dev
->mscbit
))
147 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
153 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
156 change_bit(code
, dev
->led
);
157 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
163 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
))
166 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
172 if (code
> REP_MAX
|| value
< 0 || dev
->rep
[code
] == value
) return;
174 dev
->rep
[code
] = value
;
175 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
180 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
188 dev
->grab
->handler
->event(dev
->grab
, type
, code
, value
);
190 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
192 handle
->handler
->event(handle
, type
, code
, value
);
195 static void input_repeat_key(unsigned long data
)
197 struct input_dev
*dev
= (void *) data
;
199 if (!test_bit(dev
->repeat_key
, dev
->key
))
202 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
205 mod_timer(&dev
->timer
, jiffies
+ ms_to_jiffies(dev
->rep
[REP_PERIOD
]));
208 int input_accept_process(struct input_handle
*handle
, struct file
*file
)
210 if (handle
->dev
->accept
)
211 return handle
->dev
->accept(handle
->dev
, file
);
216 int input_grab_device(struct input_handle
*handle
)
218 if (handle
->dev
->grab
)
221 handle
->dev
->grab
= handle
;
225 void input_release_device(struct input_handle
*handle
)
227 if (handle
->dev
->grab
== handle
)
228 handle
->dev
->grab
= NULL
;
231 int input_open_device(struct input_handle
*handle
)
233 if (handle
->dev
->pm_dev
)
234 pm_access(handle
->dev
->pm_dev
);
236 if (handle
->dev
->open
)
237 return handle
->dev
->open(handle
->dev
);
241 int input_flush_device(struct input_handle
* handle
, struct file
* file
)
243 if (handle
->dev
->flush
)
244 return handle
->dev
->flush(handle
->dev
, file
);
249 void input_close_device(struct input_handle
*handle
)
251 input_release_device(handle
);
252 if (handle
->dev
->pm_dev
)
253 pm_dev_idle(handle
->dev
->pm_dev
);
254 if (handle
->dev
->close
)
255 handle
->dev
->close(handle
->dev
);
259 static void input_link_handle(struct input_handle
*handle
)
261 list_add_tail(&handle
->d_node
, &handle
->dev
->h_list
);
262 list_add_tail(&handle
->h_node
, &handle
->handler
->h_list
);
265 #define MATCH_BIT(bit, max) \
266 for (i = 0; i < NBITS(max); i++) \
267 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
269 if (i != NBITS(max)) \
272 static struct input_device_id
*input_match_device(struct input_device_id
*id
, struct input_dev
*dev
)
276 for (; id
->flags
|| id
->driver_info
; id
++) {
278 if (id
->flags
& INPUT_DEVICE_ID_MATCH_BUS
)
279 if (id
->id
.bustype
!= dev
->id
.bustype
)
282 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VENDOR
)
283 if (id
->id
.vendor
!= dev
->id
.vendor
)
286 if (id
->flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
)
287 if (id
->id
.product
!= dev
->id
.product
)
290 if (id
->flags
& INPUT_DEVICE_ID_MATCH_VERSION
)
291 if (id
->id
.version
!= dev
->id
.version
)
294 MATCH_BIT(evbit
, EV_MAX
);
295 MATCH_BIT(keybit
, KEY_MAX
);
296 MATCH_BIT(relbit
, REL_MAX
);
297 MATCH_BIT(absbit
, ABS_MAX
);
298 MATCH_BIT(mscbit
, MSC_MAX
);
299 MATCH_BIT(ledbit
, LED_MAX
);
300 MATCH_BIT(sndbit
, SND_MAX
);
301 MATCH_BIT(ffbit
, FF_MAX
);
310 * Input hotplugging interface - loading event handlers based on
314 #ifdef CONFIG_HOTPLUG
317 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
318 * (normally /sbin/hotplug) when input devices get added or removed.
320 * This invokes a user mode policy agent, typically helping to load driver
321 * or other modules, configure the device, and more. Drivers can provide
322 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
326 #define SPRINTF_BIT_A(bit, name, max) \
328 envp[i++] = scratch; \
329 scratch += sprintf(scratch, name); \
330 for (j = NBITS(max) - 1; j >= 0; j--) \
331 if (dev->bit[j]) break; \
332 for (; j >= 0; j--) \
333 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
337 #define SPRINTF_BIT_A2(bit, name, max, ev) \
339 if (test_bit(ev, dev->evbit)) \
340 SPRINTF_BIT_A(bit, name, max); \
343 static void input_call_hotplug(char *verb
, struct input_dev
*dev
)
345 char *argv
[3], **envp
, *buf
, *scratch
;
348 if (!hotplug_path
[0]) {
349 printk(KERN_ERR
"input.c: calling hotplug without a hotplug agent defined\n");
352 if (in_interrupt()) {
353 printk(KERN_ERR
"input.c: calling hotplug from interrupt\n");
356 if (!current
->fs
->root
) {
357 printk(KERN_WARNING
"input.c: calling hotplug without valid filesystem\n");
360 if (!(envp
= (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL
))) {
361 printk(KERN_ERR
"input.c: not enough memory allocating hotplug environment\n");
364 if (!(buf
= kmalloc(1024, GFP_KERNEL
))) {
366 printk(KERN_ERR
"input.c: not enough memory allocating hotplug environment\n");
370 argv
[0] = hotplug_path
;
374 envp
[i
++] = "HOME=/";
375 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
380 scratch
+= sprintf(scratch
, "ACTION=%s", verb
) + 1;
383 scratch
+= sprintf(scratch
, "PRODUCT=%x/%x/%x/%x",
384 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
) + 1;
388 scratch
+= sprintf(scratch
, "NAME=%s", dev
->name
) + 1;
393 scratch
+= sprintf(scratch
, "PHYS=%s", dev
->phys
) + 1;
396 SPRINTF_BIT_A(evbit
, "EV=", EV_MAX
);
397 SPRINTF_BIT_A2(keybit
, "KEY=", KEY_MAX
, EV_KEY
);
398 SPRINTF_BIT_A2(relbit
, "REL=", REL_MAX
, EV_REL
);
399 SPRINTF_BIT_A2(absbit
, "ABS=", ABS_MAX
, EV_ABS
);
400 SPRINTF_BIT_A2(mscbit
, "MSC=", MSC_MAX
, EV_MSC
);
401 SPRINTF_BIT_A2(ledbit
, "LED=", LED_MAX
, EV_LED
);
402 SPRINTF_BIT_A2(sndbit
, "SND=", SND_MAX
, EV_SND
);
403 SPRINTF_BIT_A2(ffbit
, "FF=", FF_MAX
, EV_FF
);
408 printk(KERN_DEBUG
"input.c: calling %s %s [%s %s %s %s %s]\n",
409 argv
[0], argv
[1], envp
[0], envp
[1], envp
[2], envp
[3], envp
[4]);
412 value
= call_usermodehelper(argv
[0], argv
, envp
, 0);
419 printk(KERN_DEBUG
"input.c: hotplug returned %d\n", value
);
425 void input_register_device(struct input_dev
*dev
)
427 struct input_handle
*handle
;
428 struct input_handler
*handler
;
429 struct input_device_id
*id
;
431 set_bit(EV_SYN
, dev
->evbit
);
434 * If delay and period are pre-set by the driver, then autorepeating
435 * is handled by the driver itself and we don't do it in input.c.
438 init_timer(&dev
->timer
);
439 if (!dev
->rep
[REP_DELAY
] && !dev
->rep
[REP_PERIOD
]) {
440 dev
->timer
.data
= (long) dev
;
441 dev
->timer
.function
= input_repeat_key
;
442 dev
->rep
[REP_DELAY
] = 250;
443 dev
->rep
[REP_PERIOD
] = 33;
446 INIT_LIST_HEAD(&dev
->h_list
);
447 list_add_tail(&dev
->node
, &input_dev_list
);
449 list_for_each_entry(handler
, &input_handler_list
, node
)
450 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
451 if ((id
= input_match_device(handler
->id_table
, dev
)))
452 if ((handle
= handler
->connect(handler
, dev
, id
)))
453 input_link_handle(handle
);
455 #ifdef CONFIG_HOTPLUG
456 input_call_hotplug("add", dev
);
459 #ifdef CONFIG_PROC_FS
460 input_devices_state
++;
461 wake_up(&input_devices_poll_wait
);
465 void input_unregister_device(struct input_dev
*dev
)
467 struct list_head
* node
, * next
;
472 pm_unregister(dev
->pm_dev
);
474 del_timer_sync(&dev
->timer
);
476 list_for_each_safe(node
, next
, &dev
->h_list
) {
477 struct input_handle
* handle
= to_handle(node
);
478 list_del_init(&handle
->d_node
);
479 list_del_init(&handle
->h_node
);
480 handle
->handler
->disconnect(handle
);
483 #ifdef CONFIG_HOTPLUG
484 input_call_hotplug("remove", dev
);
487 list_del_init(&dev
->node
);
489 #ifdef CONFIG_PROC_FS
490 input_devices_state
++;
491 wake_up(&input_devices_poll_wait
);
495 void input_register_handler(struct input_handler
*handler
)
497 struct input_dev
*dev
;
498 struct input_handle
*handle
;
499 struct input_device_id
*id
;
501 if (!handler
) return;
503 INIT_LIST_HEAD(&handler
->h_list
);
505 if (handler
->fops
!= NULL
)
506 input_table
[handler
->minor
>> 5] = handler
;
508 list_add_tail(&handler
->node
, &input_handler_list
);
510 list_for_each_entry(dev
, &input_dev_list
, node
)
511 if (!handler
->blacklist
|| !input_match_device(handler
->blacklist
, dev
))
512 if ((id
= input_match_device(handler
->id_table
, dev
)))
513 if ((handle
= handler
->connect(handler
, dev
, id
)))
514 input_link_handle(handle
);
516 #ifdef CONFIG_PROC_FS
517 input_devices_state
++;
518 wake_up(&input_devices_poll_wait
);
522 void input_unregister_handler(struct input_handler
*handler
)
524 struct list_head
* node
, * next
;
526 list_for_each_safe(node
, next
, &handler
->h_list
) {
527 struct input_handle
* handle
= to_handle_h(node
);
528 list_del_init(&handle
->h_node
);
529 list_del_init(&handle
->d_node
);
530 handler
->disconnect(handle
);
533 list_del_init(&handler
->node
);
535 if (handler
->fops
!= NULL
)
536 input_table
[handler
->minor
>> 5] = NULL
;
538 #ifdef CONFIG_PROC_FS
539 input_devices_state
++;
540 wake_up(&input_devices_poll_wait
);
544 static int input_open_file(struct inode
*inode
, struct file
*file
)
546 struct input_handler
*handler
= input_table
[iminor(inode
) >> 5];
547 struct file_operations
*old_fops
, *new_fops
= NULL
;
550 /* No load-on-demand here? */
551 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
555 * That's _really_ odd. Usually NULL ->open means "nothing special",
556 * not "no device". Oh, well...
558 if (!new_fops
->open
) {
562 old_fops
= file
->f_op
;
563 file
->f_op
= new_fops
;
565 err
= new_fops
->open(inode
, file
);
568 fops_put(file
->f_op
);
569 file
->f_op
= fops_get(old_fops
);
575 static struct file_operations input_fops
= {
576 .owner
= THIS_MODULE
,
577 .open
= input_open_file
,
580 #ifdef CONFIG_PROC_FS
582 #define SPRINTF_BIT_B(bit, name, max) \
584 len += sprintf(buf + len, "B: %s", name); \
585 for (i = NBITS(max) - 1; i >= 0; i--) \
586 if (dev->bit[i]) break; \
587 for (; i >= 0; i--) \
588 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
589 len += sprintf(buf + len, "\n"); \
592 #define SPRINTF_BIT_B2(bit, name, max, ev) \
594 if (test_bit(ev, dev->evbit)) \
595 SPRINTF_BIT_B(bit, name, max); \
599 static unsigned int input_devices_poll(struct file
*file
, poll_table
*wait
)
601 int state
= input_devices_state
;
602 poll_wait(file
, &input_devices_poll_wait
, wait
);
603 if (state
!= input_devices_state
)
604 return POLLIN
| POLLRDNORM
;
608 static int input_devices_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
610 struct input_dev
*dev
;
611 struct input_handle
*handle
;
616 list_for_each_entry(dev
, &input_dev_list
, node
) {
618 len
= sprintf(buf
, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
619 dev
->id
.bustype
, dev
->id
.vendor
, dev
->id
.product
, dev
->id
.version
);
621 len
+= sprintf(buf
+ len
, "N: Name=\"%s\"\n", dev
->name
? dev
->name
: "");
622 len
+= sprintf(buf
+ len
, "P: Phys=%s\n", dev
->phys
? dev
->phys
: "");
623 len
+= sprintf(buf
+ len
, "H: Handlers=");
625 list_for_each_entry(handle
, &dev
->h_list
, d_node
)
626 len
+= sprintf(buf
+ len
, "%s ", handle
->name
);
628 len
+= sprintf(buf
+ len
, "\n");
630 SPRINTF_BIT_B(evbit
, "EV=", EV_MAX
);
631 SPRINTF_BIT_B2(keybit
, "KEY=", KEY_MAX
, EV_KEY
);
632 SPRINTF_BIT_B2(relbit
, "REL=", REL_MAX
, EV_REL
);
633 SPRINTF_BIT_B2(absbit
, "ABS=", ABS_MAX
, EV_ABS
);
634 SPRINTF_BIT_B2(mscbit
, "MSC=", MSC_MAX
, EV_MSC
);
635 SPRINTF_BIT_B2(ledbit
, "LED=", LED_MAX
, EV_LED
);
636 SPRINTF_BIT_B2(sndbit
, "SND=", SND_MAX
, EV_SND
);
637 SPRINTF_BIT_B2(ffbit
, "FF=", FF_MAX
, EV_FF
);
639 len
+= sprintf(buf
+ len
, "\n");
645 *start
= buf
+ (pos
- (at
- len
));
654 if (&dev
->node
== &input_dev_list
)
657 return (count
> cnt
) ? cnt
: count
;
660 static int input_handlers_read(char *buf
, char **start
, off_t pos
, int count
, int *eof
, void *data
)
662 struct input_handler
*handler
;
665 int len
= 0, cnt
= 0;
668 list_for_each_entry(handler
, &input_handler_list
, node
) {
671 len
= sprintf(buf
, "N: Number=%d Name=%s Minor=%d\n",
672 i
++, handler
->name
, handler
->minor
);
674 len
= sprintf(buf
, "N: Number=%d Name=%s\n",
681 *start
= buf
+ (pos
- (at
- len
));
689 if (&handler
->node
== &input_handler_list
)
692 return (count
> cnt
) ? cnt
: count
;
695 static int __init
input_proc_init(void)
697 struct proc_dir_entry
*entry
;
699 proc_bus_input_dir
= proc_mkdir("input", proc_bus
);
700 if (proc_bus_input_dir
== NULL
)
702 proc_bus_input_dir
->owner
= THIS_MODULE
;
703 entry
= create_proc_read_entry("devices", 0, proc_bus_input_dir
, input_devices_read
, NULL
);
705 remove_proc_entry("input", proc_bus
);
708 entry
->owner
= THIS_MODULE
;
709 entry
->proc_fops
->poll
= input_devices_poll
;
710 entry
= create_proc_read_entry("handlers", 0, proc_bus_input_dir
, input_handlers_read
, NULL
);
712 remove_proc_entry("devices", proc_bus_input_dir
);
713 remove_proc_entry("input", proc_bus
);
716 entry
->owner
= THIS_MODULE
;
719 #else /* !CONFIG_PROC_FS */
720 static inline int input_proc_init(void) { return 0; }
723 struct class_simple
*input_class
;
725 static int __init
input_init(void)
727 int retval
= -ENOMEM
;
729 input_class
= class_simple_create(THIS_MODULE
, "input");
730 if (IS_ERR(input_class
))
731 return PTR_ERR(input_class
);
733 retval
= register_chrdev(INPUT_MAJOR
, "input", &input_fops
);
735 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
736 remove_proc_entry("devices", proc_bus_input_dir
);
737 remove_proc_entry("handlers", proc_bus_input_dir
);
738 remove_proc_entry("input", proc_bus
);
739 class_simple_destroy(input_class
);
743 retval
= devfs_mk_dir("input");
745 remove_proc_entry("devices", proc_bus_input_dir
);
746 remove_proc_entry("handlers", proc_bus_input_dir
);
747 remove_proc_entry("input", proc_bus
);
748 unregister_chrdev(INPUT_MAJOR
, "input");
749 class_simple_destroy(input_class
);
754 static void __exit
input_exit(void)
756 remove_proc_entry("devices", proc_bus_input_dir
);
757 remove_proc_entry("handlers", proc_bus_input_dir
);
758 remove_proc_entry("input", proc_bus
);
760 devfs_remove("input");
761 unregister_chrdev(INPUT_MAJOR
, "input");
762 class_simple_destroy(input_class
);
765 subsys_initcall(input_init
);
766 module_exit(input_exit
);