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>
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27 MODULE_DESCRIPTION("Input core");
28 MODULE_LICENSE("GPL");
30 EXPORT_SYMBOL(input_allocate_device);
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];
51 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
53 struct input_handle *handle;
55 if (type > EV_MAX || !test_bit(type, dev->evbit))
58 add_input_randomness(type, code, value);
65 if (dev->event) dev->event(dev, type, code, value);
69 if (dev->sync) return;
77 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
83 change_bit(code, dev->key);
85 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
86 dev->repeat_key = code;
87 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
94 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
97 change_bit(code, dev->sw);
103 if (code > ABS_MAX || !test_bit(code, dev->absbit))
106 if (dev->absfuzz[code]) {
107 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
108 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
111 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
112 (value < dev->abs[code] + dev->absfuzz[code]))
113 value = (dev->abs[code] * 3 + value) >> 2;
115 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
116 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
117 value = (dev->abs[code] + value) >> 1;
120 if (dev->abs[code] == value)
123 dev->abs[code] = value;
128 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
135 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
138 if (dev->event) dev->event(dev, type, code, value);
144 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
147 change_bit(code, dev->led);
148 if (dev->event) dev->event(dev, type, code, value);
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
157 if (dev->event) dev->event(dev, type, code, value);
163 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
165 dev->rep[code] = value;
166 if (dev->event) dev->event(dev, type, code, value);
171 if (dev->event) dev->event(dev, type, code, value);
179 dev->grab->handler->event(dev->grab, type, code, value);
181 list_for_each_entry(handle, &dev->h_list, d_node)
183 handle->handler->event(handle, type, code, value);
186 static void input_repeat_key(unsigned long data)
188 struct input_dev *dev = (void *) data;
190 if (!test_bit(dev->repeat_key, dev->key))
193 input_event(dev, EV_KEY, dev->repeat_key, 2);
196 if (dev->rep[REP_PERIOD])
197 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
200 int input_accept_process(struct input_handle *handle, struct file *file)
202 if (handle->dev->accept)
203 return handle->dev->accept(handle->dev, file);
208 int input_grab_device(struct input_handle *handle)
210 if (handle->dev->grab)
213 handle->dev->grab = handle;
217 void input_release_device(struct input_handle *handle)
219 if (handle->dev->grab == handle)
220 handle->dev->grab = NULL;
223 int input_open_device(struct input_handle *handle)
225 struct input_dev *dev = handle->dev;
228 err = down_interruptible(&dev->sem);
234 if (!dev->users++ && dev->open)
235 err = dev->open(dev);
245 int input_flush_device(struct input_handle* handle, struct file* file)
247 if (handle->dev->flush)
248 return handle->dev->flush(handle->dev, file);
253 void input_close_device(struct input_handle *handle)
255 struct input_dev *dev = handle->dev;
257 input_release_device(handle);
261 if (!--dev->users && dev->close)
268 static void input_link_handle(struct input_handle *handle)
270 list_add_tail(&handle->d_node, &handle->dev->h_list);
271 list_add_tail(&handle->h_node, &handle->handler->h_list);
274 #define MATCH_BIT(bit, max) \
275 for (i = 0; i < NBITS(max); i++) \
276 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
278 if (i != NBITS(max)) \
281 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
285 for (; id->flags || id->driver_info; id++) {
287 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
288 if (id->id.bustype != dev->id.bustype)
291 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
292 if (id->id.vendor != dev->id.vendor)
295 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
296 if (id->id.product != dev->id.product)
299 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
300 if (id->id.version != dev->id.version)
303 MATCH_BIT(evbit, EV_MAX);
304 MATCH_BIT(keybit, KEY_MAX);
305 MATCH_BIT(relbit, REL_MAX);
306 MATCH_BIT(absbit, ABS_MAX);
307 MATCH_BIT(mscbit, MSC_MAX);
308 MATCH_BIT(ledbit, LED_MAX);
309 MATCH_BIT(sndbit, SND_MAX);
310 MATCH_BIT(ffbit, FF_MAX);
311 MATCH_BIT(swbit, SW_MAX);
321 * Input hotplugging interface - loading event handlers based on
325 #ifdef CONFIG_HOTPLUG
328 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
329 * (normally /sbin/hotplug) when input devices get added or removed.
331 * This invokes a user mode policy agent, typically helping to load driver
332 * or other modules, configure the device, and more. Drivers can provide
333 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
337 #define SPRINTF_BIT_A(bit, name, max) \
339 envp[i++] = scratch; \
340 scratch += sprintf(scratch, name); \
341 for (j = NBITS(max) - 1; j >= 0; j--) \
342 if (dev->bit[j]) break; \
343 for (; j >= 0; j--) \
344 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
348 #define SPRINTF_BIT_A2(bit, name, max, ev) \
350 if (test_bit(ev, dev->evbit)) \
351 SPRINTF_BIT_A(bit, name, max); \
354 static void input_call_hotplug(char *verb, struct input_dev *dev)
356 char *argv[3], **envp, *buf, *scratch;
359 if (!hotplug_path[0]) {
360 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
363 if (in_interrupt()) {
364 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
367 if (!current->fs->root) {
368 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
371 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
372 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
375 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
377 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
381 argv[0] = hotplug_path;
385 envp[i++] = "HOME=/";
386 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
391 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
394 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
395 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
399 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
404 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
407 SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
408 SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
409 SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
410 SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
411 SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
412 SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
413 SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
414 SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
415 SPRINTF_BIT_A2(swbit, "SW=", SW_MAX, EV_SW);
420 printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
421 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
424 value = call_usermodehelper(argv [0], argv, envp, 0);
431 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
437 #ifdef CONFIG_PROC_FS
439 static struct proc_dir_entry *proc_bus_input_dir;
440 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
441 static int input_devices_state;
443 static inline void input_wakeup_procfs_readers(void)
445 input_devices_state++;
446 wake_up(&input_devices_poll_wait);
449 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
451 int state = input_devices_state;
452 poll_wait(file, &input_devices_poll_wait, wait);
453 if (state != input_devices_state)
454 return POLLIN | POLLRDNORM;
458 #define SPRINTF_BIT_B(bit, name, max) \
460 len += sprintf(buf + len, "B: %s", name); \
461 for (i = NBITS(max) - 1; i >= 0; i--) \
462 if (dev->bit[i]) break; \
463 for (; i >= 0; i--) \
464 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
465 len += sprintf(buf + len, "\n"); \
468 #define SPRINTF_BIT_B2(bit, name, max, ev) \
470 if (test_bit(ev, dev->evbit)) \
471 SPRINTF_BIT_B(bit, name, max); \
474 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
476 struct input_dev *dev;
477 struct input_handle *handle;
483 list_for_each_entry(dev, &input_dev_list, node) {
485 path = dev->dynalloc ? kobject_get_path(&dev->cdev.kobj, GFP_KERNEL) : NULL;
487 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
488 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
490 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
491 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
492 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
493 len += sprintf(buf + len, "H: Handlers=");
495 list_for_each_entry(handle, &dev->h_list, d_node)
496 len += sprintf(buf + len, "%s ", handle->name);
498 len += sprintf(buf + len, "\n");
500 SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
501 SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
502 SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
503 SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
504 SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
505 SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
506 SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
507 SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF);
508 SPRINTF_BIT_B2(swbit, "SW=", SW_MAX, EV_SW);
510 len += sprintf(buf + len, "\n");
516 *start = buf + (pos - (at - len));
527 if (&dev->node == &input_dev_list)
530 return (count > cnt) ? cnt : count;
533 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
535 struct input_handler *handler;
538 int len = 0, cnt = 0;
541 list_for_each_entry(handler, &input_handler_list, node) {
544 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
545 i++, handler->name, handler->minor);
547 len = sprintf(buf, "N: Number=%d Name=%s\n",
554 *start = buf + (pos - (at - len));
562 if (&handler->node == &input_handler_list)
565 return (count > cnt) ? cnt : count;
568 static struct file_operations input_fileops;
570 static int __init input_proc_init(void)
572 struct proc_dir_entry *entry;
574 proc_bus_input_dir = proc_mkdir("input", proc_bus);
575 if (!proc_bus_input_dir)
578 proc_bus_input_dir->owner = THIS_MODULE;
580 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
584 entry->owner = THIS_MODULE;
585 input_fileops = *entry->proc_fops;
586 entry->proc_fops = &input_fileops;
587 entry->proc_fops->poll = input_devices_poll;
589 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
593 entry->owner = THIS_MODULE;
597 fail2: remove_proc_entry("devices", proc_bus_input_dir);
598 fail1: remove_proc_entry("input", proc_bus);
602 static void input_proc_exit(void)
604 remove_proc_entry("devices", proc_bus_input_dir);
605 remove_proc_entry("handlers", proc_bus_input_dir);
606 remove_proc_entry("input", proc_bus);
609 #else /* !CONFIG_PROC_FS */
610 static inline void input_wakeup_procfs_readers(void) { }
611 static inline int input_proc_init(void) { return 0; }
612 static inline void input_proc_exit(void) { }
615 static void input_dev_release(struct class_device *class_dev)
617 struct input_dev *dev = to_input_dev(class_dev);
620 module_put(THIS_MODULE);
623 static struct class input_dev_class = {
625 .release = input_dev_release,
628 struct input_dev *input_allocate_device(void)
630 struct input_dev *dev;
632 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
635 dev->cdev.class = &input_dev_class;
636 class_device_initialize(&dev->cdev);
637 INIT_LIST_HEAD(&dev->h_list);
638 INIT_LIST_HEAD(&dev->node);
644 static void input_register_classdevice(struct input_dev *dev)
646 static atomic_t input_no = ATOMIC_INIT(0);
649 __module_get(THIS_MODULE);
651 dev->dev = dev->cdev.dev;
653 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
654 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
656 path = kobject_get_path(&dev->cdev.class->subsys.kset.kobj, GFP_KERNEL);
657 printk(KERN_INFO "input: %s/%s as %s\n",
658 dev->name ? dev->name : "Unspecified device",
659 path ? path : "", dev->cdev.class_id);
662 class_device_add(&dev->cdev);
665 void input_register_device(struct input_dev *dev)
667 struct input_handle *handle;
668 struct input_handler *handler;
669 struct input_device_id *id;
671 set_bit(EV_SYN, dev->evbit);
673 init_MUTEX(&dev->sem);
676 * If delay and period are pre-set by the driver, then autorepeating
677 * is handled by the driver itself and we don't do it in input.c.
680 init_timer(&dev->timer);
681 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
682 dev->timer.data = (long) dev;
683 dev->timer.function = input_repeat_key;
684 dev->rep[REP_DELAY] = 250;
685 dev->rep[REP_PERIOD] = 33;
688 INIT_LIST_HEAD(&dev->h_list);
689 list_add_tail(&dev->node, &input_dev_list);
691 list_for_each_entry(handler, &input_handler_list, node)
692 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
693 if ((id = input_match_device(handler->id_table, dev)))
694 if ((handle = handler->connect(handler, dev, id)))
695 input_link_handle(handle);
699 input_register_classdevice(dev);
701 #ifdef CONFIG_HOTPLUG
702 input_call_hotplug("add", dev);
705 input_wakeup_procfs_readers();
708 void input_unregister_device(struct input_dev *dev)
710 struct list_head * node, * next;
714 del_timer_sync(&dev->timer);
716 list_for_each_safe(node, next, &dev->h_list) {
717 struct input_handle * handle = to_handle(node);
718 list_del_init(&handle->d_node);
719 list_del_init(&handle->h_node);
720 handle->handler->disconnect(handle);
723 #ifdef CONFIG_HOTPLUG
724 input_call_hotplug("remove", dev);
727 list_del_init(&dev->node);
730 class_device_unregister(&dev->cdev);
732 input_wakeup_procfs_readers();
735 void input_register_handler(struct input_handler *handler)
737 struct input_dev *dev;
738 struct input_handle *handle;
739 struct input_device_id *id;
741 if (!handler) return;
743 INIT_LIST_HEAD(&handler->h_list);
745 if (handler->fops != NULL)
746 input_table[handler->minor >> 5] = handler;
748 list_add_tail(&handler->node, &input_handler_list);
750 list_for_each_entry(dev, &input_dev_list, node)
751 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
752 if ((id = input_match_device(handler->id_table, dev)))
753 if ((handle = handler->connect(handler, dev, id)))
754 input_link_handle(handle);
756 input_wakeup_procfs_readers();
759 void input_unregister_handler(struct input_handler *handler)
761 struct list_head * node, * next;
763 list_for_each_safe(node, next, &handler->h_list) {
764 struct input_handle * handle = to_handle_h(node);
765 list_del_init(&handle->h_node);
766 list_del_init(&handle->d_node);
767 handler->disconnect(handle);
770 list_del_init(&handler->node);
772 if (handler->fops != NULL)
773 input_table[handler->minor >> 5] = NULL;
775 input_wakeup_procfs_readers();
778 static int input_open_file(struct inode *inode, struct file *file)
780 struct input_handler *handler = input_table[iminor(inode) >> 5];
781 struct file_operations *old_fops, *new_fops = NULL;
784 /* No load-on-demand here? */
785 if (!handler || !(new_fops = fops_get(handler->fops)))
789 * That's _really_ odd. Usually NULL ->open means "nothing special",
790 * not "no device". Oh, well...
792 if (!new_fops->open) {
796 old_fops = file->f_op;
797 file->f_op = new_fops;
799 err = new_fops->open(inode, file);
802 fops_put(file->f_op);
803 file->f_op = fops_get(old_fops);
809 static struct file_operations input_fops = {
810 .owner = THIS_MODULE,
811 .open = input_open_file,
814 struct class *input_class;
816 static int __init input_init(void)
820 err = class_register(&input_dev_class);
822 printk(KERN_ERR "input: unable to register input_dev class\n");
826 input_class = class_create(THIS_MODULE, "input");
827 if (IS_ERR(input_class)) {
828 printk(KERN_ERR "input: unable to register input class\n");
829 err = PTR_ERR(input_class);
833 err = input_proc_init();
837 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
839 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
845 fail3: input_proc_exit();
846 fail2: class_destroy(input_class);
847 fail1: class_unregister(&input_dev_class);
851 static void __exit input_exit(void)
854 unregister_chrdev(INPUT_MAJOR, "input");
855 class_destroy(input_class);
856 class_unregister(&input_dev_class);
859 subsys_initcall(input_init);
860 module_exit(input_exit);