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)
222 struct input_dev *dev = handle->dev;
225 err = down_interruptible(&dev->sem);
231 if (!dev->users++ && dev->open)
232 err = dev->open(dev);
242 int input_flush_device(struct input_handle* handle, struct file* file)
244 if (handle->dev->flush)
245 return handle->dev->flush(handle->dev, file);
250 void input_close_device(struct input_handle *handle)
252 struct input_dev *dev = handle->dev;
254 input_release_device(handle);
258 if (!--dev->users && dev->close)
265 static void input_link_handle(struct input_handle *handle)
267 list_add_tail(&handle->d_node, &handle->dev->h_list);
268 list_add_tail(&handle->h_node, &handle->handler->h_list);
271 #define MATCH_BIT(bit, max) \
272 for (i = 0; i < NBITS(max); i++) \
273 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
275 if (i != NBITS(max)) \
278 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
282 for (; id->flags || id->driver_info; id++) {
284 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
285 if (id->id.bustype != dev->id.bustype)
288 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
289 if (id->id.vendor != dev->id.vendor)
292 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
293 if (id->id.product != dev->id.product)
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
297 if (id->id.version != dev->id.version)
300 MATCH_BIT(evbit, EV_MAX);
301 MATCH_BIT(keybit, KEY_MAX);
302 MATCH_BIT(relbit, REL_MAX);
303 MATCH_BIT(absbit, ABS_MAX);
304 MATCH_BIT(mscbit, MSC_MAX);
305 MATCH_BIT(ledbit, LED_MAX);
306 MATCH_BIT(sndbit, SND_MAX);
307 MATCH_BIT(ffbit, FF_MAX);
316 * Input hotplugging interface - loading event handlers based on
320 #ifdef CONFIG_HOTPLUG
323 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
324 * (normally /sbin/hotplug) when input devices get added or removed.
326 * This invokes a user mode policy agent, typically helping to load driver
327 * or other modules, configure the device, and more. Drivers can provide
328 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
332 #define SPRINTF_BIT_A(bit, name, max) \
334 envp[i++] = scratch; \
335 scratch += sprintf(scratch, name); \
336 for (j = NBITS(max) - 1; j >= 0; j--) \
337 if (dev->bit[j]) break; \
338 for (; j >= 0; j--) \
339 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
343 #define SPRINTF_BIT_A2(bit, name, max, ev) \
345 if (test_bit(ev, dev->evbit)) \
346 SPRINTF_BIT_A(bit, name, max); \
349 static void input_call_hotplug(char *verb, struct input_dev *dev)
351 char *argv[3], **envp, *buf, *scratch;
354 if (!hotplug_path[0]) {
355 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
358 if (in_interrupt()) {
359 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
362 if (!current->fs->root) {
363 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
366 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
367 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
370 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
372 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
376 argv[0] = hotplug_path;
380 envp[i++] = "HOME=/";
381 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
386 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
389 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
390 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
394 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
399 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
402 SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
403 SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
404 SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
405 SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
406 SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
407 SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
408 SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
409 SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
414 printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
415 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
418 value = call_usermodehelper(argv [0], argv, envp, 0);
425 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
431 void input_register_device(struct input_dev *dev)
433 struct input_handle *handle;
434 struct input_handler *handler;
435 struct input_device_id *id;
437 set_bit(EV_SYN, dev->evbit);
439 init_MUTEX(&dev->sem);
442 * If delay and period are pre-set by the driver, then autorepeating
443 * is handled by the driver itself and we don't do it in input.c.
446 init_timer(&dev->timer);
447 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
448 dev->timer.data = (long) dev;
449 dev->timer.function = input_repeat_key;
450 dev->rep[REP_DELAY] = 250;
451 dev->rep[REP_PERIOD] = 33;
454 INIT_LIST_HEAD(&dev->h_list);
455 list_add_tail(&dev->node, &input_dev_list);
457 list_for_each_entry(handler, &input_handler_list, node)
458 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
459 if ((id = input_match_device(handler->id_table, dev)))
460 if ((handle = handler->connect(handler, dev, id)))
461 input_link_handle(handle);
463 #ifdef CONFIG_HOTPLUG
464 input_call_hotplug("add", dev);
467 #ifdef CONFIG_PROC_FS
468 input_devices_state++;
469 wake_up(&input_devices_poll_wait);
473 void input_unregister_device(struct input_dev *dev)
475 struct list_head * node, * next;
479 del_timer_sync(&dev->timer);
481 list_for_each_safe(node, next, &dev->h_list) {
482 struct input_handle * handle = to_handle(node);
483 list_del_init(&handle->d_node);
484 list_del_init(&handle->h_node);
485 handle->handler->disconnect(handle);
488 #ifdef CONFIG_HOTPLUG
489 input_call_hotplug("remove", dev);
492 list_del_init(&dev->node);
494 #ifdef CONFIG_PROC_FS
495 input_devices_state++;
496 wake_up(&input_devices_poll_wait);
500 void input_register_handler(struct input_handler *handler)
502 struct input_dev *dev;
503 struct input_handle *handle;
504 struct input_device_id *id;
506 if (!handler) return;
508 INIT_LIST_HEAD(&handler->h_list);
510 if (handler->fops != NULL)
511 input_table[handler->minor >> 5] = handler;
513 list_add_tail(&handler->node, &input_handler_list);
515 list_for_each_entry(dev, &input_dev_list, node)
516 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
517 if ((id = input_match_device(handler->id_table, dev)))
518 if ((handle = handler->connect(handler, dev, id)))
519 input_link_handle(handle);
521 #ifdef CONFIG_PROC_FS
522 input_devices_state++;
523 wake_up(&input_devices_poll_wait);
527 void input_unregister_handler(struct input_handler *handler)
529 struct list_head * node, * next;
531 list_for_each_safe(node, next, &handler->h_list) {
532 struct input_handle * handle = to_handle_h(node);
533 list_del_init(&handle->h_node);
534 list_del_init(&handle->d_node);
535 handler->disconnect(handle);
538 list_del_init(&handler->node);
540 if (handler->fops != NULL)
541 input_table[handler->minor >> 5] = NULL;
543 #ifdef CONFIG_PROC_FS
544 input_devices_state++;
545 wake_up(&input_devices_poll_wait);
549 static int input_open_file(struct inode *inode, struct file *file)
551 struct input_handler *handler = input_table[iminor(inode) >> 5];
552 struct file_operations *old_fops, *new_fops = NULL;
555 /* No load-on-demand here? */
556 if (!handler || !(new_fops = fops_get(handler->fops)))
560 * That's _really_ odd. Usually NULL ->open means "nothing special",
561 * not "no device". Oh, well...
563 if (!new_fops->open) {
567 old_fops = file->f_op;
568 file->f_op = new_fops;
570 err = new_fops->open(inode, file);
573 fops_put(file->f_op);
574 file->f_op = fops_get(old_fops);
580 static struct file_operations input_fops = {
581 .owner = THIS_MODULE,
582 .open = input_open_file,
585 #ifdef CONFIG_PROC_FS
587 #define SPRINTF_BIT_B(bit, name, max) \
589 len += sprintf(buf + len, "B: %s", name); \
590 for (i = NBITS(max) - 1; i >= 0; i--) \
591 if (dev->bit[i]) break; \
592 for (; i >= 0; i--) \
593 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
594 len += sprintf(buf + len, "\n"); \
597 #define SPRINTF_BIT_B2(bit, name, max, ev) \
599 if (test_bit(ev, dev->evbit)) \
600 SPRINTF_BIT_B(bit, name, max); \
604 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
606 int state = input_devices_state;
607 poll_wait(file, &input_devices_poll_wait, wait);
608 if (state != input_devices_state)
609 return POLLIN | POLLRDNORM;
613 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
615 struct input_dev *dev;
616 struct input_handle *handle;
621 list_for_each_entry(dev, &input_dev_list, node) {
623 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
624 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
626 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
627 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
628 len += sprintf(buf + len, "H: Handlers=");
630 list_for_each_entry(handle, &dev->h_list, d_node)
631 len += sprintf(buf + len, "%s ", handle->name);
633 len += sprintf(buf + len, "\n");
635 SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
636 SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
637 SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
638 SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
639 SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
640 SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
641 SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
642 SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF);
644 len += sprintf(buf + len, "\n");
650 *start = buf + (pos - (at - len));
659 if (&dev->node == &input_dev_list)
662 return (count > cnt) ? cnt : count;
665 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
667 struct input_handler *handler;
670 int len = 0, cnt = 0;
673 list_for_each_entry(handler, &input_handler_list, node) {
676 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
677 i++, handler->name, handler->minor);
679 len = sprintf(buf, "N: Number=%d Name=%s\n",
686 *start = buf + (pos - (at - len));
694 if (&handler->node == &input_handler_list)
697 return (count > cnt) ? cnt : count;
700 static struct file_operations input_fileops;
702 static int __init input_proc_init(void)
704 struct proc_dir_entry *entry;
706 proc_bus_input_dir = proc_mkdir("input", proc_bus);
707 if (proc_bus_input_dir == NULL)
709 proc_bus_input_dir->owner = THIS_MODULE;
710 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
712 remove_proc_entry("input", proc_bus);
715 entry->owner = THIS_MODULE;
716 input_fileops = *entry->proc_fops;
717 entry->proc_fops = &input_fileops;
718 entry->proc_fops->poll = input_devices_poll;
719 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
721 remove_proc_entry("devices", proc_bus_input_dir);
722 remove_proc_entry("input", proc_bus);
725 entry->owner = THIS_MODULE;
728 #else /* !CONFIG_PROC_FS */
729 static inline int input_proc_init(void) { return 0; }
732 struct class *input_class;
734 static int __init input_init(void)
736 int retval = -ENOMEM;
738 input_class = class_create(THIS_MODULE, "input");
739 if (IS_ERR(input_class))
740 return PTR_ERR(input_class);
742 retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
744 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
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 class_destroy(input_class);
752 retval = devfs_mk_dir("input");
754 remove_proc_entry("devices", proc_bus_input_dir);
755 remove_proc_entry("handlers", proc_bus_input_dir);
756 remove_proc_entry("input", proc_bus);
757 unregister_chrdev(INPUT_MAJOR, "input");
758 class_destroy(input_class);
763 static void __exit input_exit(void)
765 remove_proc_entry("devices", proc_bus_input_dir);
766 remove_proc_entry("handlers", proc_bus_input_dir);
767 remove_proc_entry("input", proc_bus);
769 devfs_remove("input");
770 unregister_chrdev(INPUT_MAJOR, "input");
771 class_destroy(input_class);
774 subsys_initcall(input_init);
775 module_exit(input_exit);