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 DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
54 static int input_devices_state;
57 static inline unsigned int ms_to_jiffies(unsigned int ms)
60 j = (ms * HZ + 500) / 1000;
61 return (j > 0) ? j : 1;
65 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
67 struct input_handle *handle;
69 if (type > EV_MAX || !test_bit(type, dev->evbit))
72 add_input_randomness(type, code, value);
79 if (dev->event) dev->event(dev, type, code, value);
83 if (dev->sync) return;
91 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
97 change_bit(code, dev->key);
99 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->timer.data && value) {
100 dev->repeat_key = code;
101 mod_timer(&dev->timer, jiffies + ms_to_jiffies(dev->rep[REP_DELAY]));
108 if (code > ABS_MAX || !test_bit(code, dev->absbit))
111 if (dev->absfuzz[code]) {
112 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
113 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
116 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
117 (value < dev->abs[code] + dev->absfuzz[code]))
118 value = (dev->abs[code] * 3 + value) >> 2;
120 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
121 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
122 value = (dev->abs[code] + value) >> 1;
125 if (dev->abs[code] == value)
128 dev->abs[code] = value;
133 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
140 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
143 if (dev->event) dev->event(dev, type, code, value);
149 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
152 change_bit(code, dev->led);
153 if (dev->event) dev->event(dev, type, code, value);
159 if (code > SND_MAX || !test_bit(code, dev->sndbit))
162 if (dev->event) dev->event(dev, type, code, value);
168 if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
170 dev->rep[code] = value;
171 if (dev->event) dev->event(dev, type, code, value);
176 if (dev->event) dev->event(dev, type, code, value);
184 dev->grab->handler->event(dev->grab, type, code, value);
186 list_for_each_entry(handle, &dev->h_list, d_node)
188 handle->handler->event(handle, type, code, value);
191 static void input_repeat_key(unsigned long data)
193 struct input_dev *dev = (void *) data;
195 if (!test_bit(dev->repeat_key, dev->key))
198 input_event(dev, EV_KEY, dev->repeat_key, 2);
201 mod_timer(&dev->timer, jiffies + ms_to_jiffies(dev->rep[REP_PERIOD]));
204 int input_accept_process(struct input_handle *handle, struct file *file)
206 if (handle->dev->accept)
207 return handle->dev->accept(handle->dev, file);
212 int input_grab_device(struct input_handle *handle)
214 if (handle->dev->grab)
217 handle->dev->grab = handle;
221 void input_release_device(struct input_handle *handle)
223 if (handle->dev->grab == handle)
224 handle->dev->grab = NULL;
227 int input_open_device(struct input_handle *handle)
230 if (handle->dev->open)
231 return handle->dev->open(handle->dev);
235 int input_flush_device(struct input_handle* handle, struct file* file)
237 if (handle->dev->flush)
238 return handle->dev->flush(handle->dev, file);
243 void input_close_device(struct input_handle *handle)
245 input_release_device(handle);
246 if (handle->dev->close)
247 handle->dev->close(handle->dev);
251 static void input_link_handle(struct input_handle *handle)
253 list_add_tail(&handle->d_node, &handle->dev->h_list);
254 list_add_tail(&handle->h_node, &handle->handler->h_list);
257 #define MATCH_BIT(bit, max) \
258 for (i = 0; i < NBITS(max); i++) \
259 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
261 if (i != NBITS(max)) \
264 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
268 for (; id->flags || id->driver_info; id++) {
270 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
271 if (id->id.bustype != dev->id.bustype)
274 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
275 if (id->id.vendor != dev->id.vendor)
278 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
279 if (id->id.product != dev->id.product)
282 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
283 if (id->id.version != dev->id.version)
286 MATCH_BIT(evbit, EV_MAX);
287 MATCH_BIT(keybit, KEY_MAX);
288 MATCH_BIT(relbit, REL_MAX);
289 MATCH_BIT(absbit, ABS_MAX);
290 MATCH_BIT(mscbit, MSC_MAX);
291 MATCH_BIT(ledbit, LED_MAX);
292 MATCH_BIT(sndbit, SND_MAX);
293 MATCH_BIT(ffbit, FF_MAX);
302 * Input hotplugging interface - loading event handlers based on
306 #ifdef CONFIG_HOTPLUG
309 * Input hotplugging invokes what /proc/sys/kernel/hotplug says
310 * (normally /sbin/hotplug) when input devices get added or removed.
312 * This invokes a user mode policy agent, typically helping to load driver
313 * or other modules, configure the device, and more. Drivers can provide
314 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
318 #define SPRINTF_BIT_A(bit, name, max) \
320 envp[i++] = scratch; \
321 scratch += sprintf(scratch, name); \
322 for (j = NBITS(max) - 1; j >= 0; j--) \
323 if (dev->bit[j]) break; \
324 for (; j >= 0; j--) \
325 scratch += sprintf(scratch, "%lx ", dev->bit[j]); \
329 #define SPRINTF_BIT_A2(bit, name, max, ev) \
331 if (test_bit(ev, dev->evbit)) \
332 SPRINTF_BIT_A(bit, name, max); \
335 static void input_call_hotplug(char *verb, struct input_dev *dev)
337 char *argv[3], **envp, *buf, *scratch;
340 if (!hotplug_path[0]) {
341 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
344 if (in_interrupt()) {
345 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
348 if (!current->fs->root) {
349 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
352 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
353 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
356 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
358 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
362 argv[0] = hotplug_path;
366 envp[i++] = "HOME=/";
367 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
372 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
375 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
376 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
380 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
385 scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1;
388 SPRINTF_BIT_A(evbit, "EV=", EV_MAX);
389 SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY);
390 SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL);
391 SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS);
392 SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC);
393 SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED);
394 SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND);
395 SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF);
400 printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n",
401 argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]);
404 value = call_usermodehelper(argv [0], argv, envp, 0);
411 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
417 void input_register_device(struct input_dev *dev)
419 struct input_handle *handle;
420 struct input_handler *handler;
421 struct input_device_id *id;
423 set_bit(EV_SYN, dev->evbit);
426 * If delay and period are pre-set by the driver, then autorepeating
427 * is handled by the driver itself and we don't do it in input.c.
430 init_timer(&dev->timer);
431 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
432 dev->timer.data = (long) dev;
433 dev->timer.function = input_repeat_key;
434 dev->rep[REP_DELAY] = 250;
435 dev->rep[REP_PERIOD] = 33;
438 INIT_LIST_HEAD(&dev->h_list);
439 list_add_tail(&dev->node, &input_dev_list);
441 list_for_each_entry(handler, &input_handler_list, node)
442 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
443 if ((id = input_match_device(handler->id_table, dev)))
444 if ((handle = handler->connect(handler, dev, id)))
445 input_link_handle(handle);
447 #ifdef CONFIG_HOTPLUG
448 input_call_hotplug("add", dev);
451 #ifdef CONFIG_PROC_FS
452 input_devices_state++;
453 wake_up(&input_devices_poll_wait);
457 void input_unregister_device(struct input_dev *dev)
459 struct list_head * node, * next;
463 del_timer_sync(&dev->timer);
465 list_for_each_safe(node, next, &dev->h_list) {
466 struct input_handle * handle = to_handle(node);
467 list_del_init(&handle->d_node);
468 list_del_init(&handle->h_node);
469 handle->handler->disconnect(handle);
472 #ifdef CONFIG_HOTPLUG
473 input_call_hotplug("remove", dev);
476 list_del_init(&dev->node);
478 #ifdef CONFIG_PROC_FS
479 input_devices_state++;
480 wake_up(&input_devices_poll_wait);
484 void input_register_handler(struct input_handler *handler)
486 struct input_dev *dev;
487 struct input_handle *handle;
488 struct input_device_id *id;
490 if (!handler) return;
492 INIT_LIST_HEAD(&handler->h_list);
494 if (handler->fops != NULL)
495 input_table[handler->minor >> 5] = handler;
497 list_add_tail(&handler->node, &input_handler_list);
499 list_for_each_entry(dev, &input_dev_list, node)
500 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
501 if ((id = input_match_device(handler->id_table, dev)))
502 if ((handle = handler->connect(handler, dev, id)))
503 input_link_handle(handle);
505 #ifdef CONFIG_PROC_FS
506 input_devices_state++;
507 wake_up(&input_devices_poll_wait);
511 void input_unregister_handler(struct input_handler *handler)
513 struct list_head * node, * next;
515 list_for_each_safe(node, next, &handler->h_list) {
516 struct input_handle * handle = to_handle_h(node);
517 list_del_init(&handle->h_node);
518 list_del_init(&handle->d_node);
519 handler->disconnect(handle);
522 list_del_init(&handler->node);
524 if (handler->fops != NULL)
525 input_table[handler->minor >> 5] = NULL;
527 #ifdef CONFIG_PROC_FS
528 input_devices_state++;
529 wake_up(&input_devices_poll_wait);
533 static int input_open_file(struct inode *inode, struct file *file)
535 struct input_handler *handler = input_table[iminor(inode) >> 5];
536 struct file_operations *old_fops, *new_fops = NULL;
539 /* No load-on-demand here? */
540 if (!handler || !(new_fops = fops_get(handler->fops)))
544 * That's _really_ odd. Usually NULL ->open means "nothing special",
545 * not "no device". Oh, well...
547 if (!new_fops->open) {
551 old_fops = file->f_op;
552 file->f_op = new_fops;
554 err = new_fops->open(inode, file);
557 fops_put(file->f_op);
558 file->f_op = fops_get(old_fops);
564 static struct file_operations input_fops = {
565 .owner = THIS_MODULE,
566 .open = input_open_file,
569 #ifdef CONFIG_PROC_FS
571 #define SPRINTF_BIT_B(bit, name, max) \
573 len += sprintf(buf + len, "B: %s", name); \
574 for (i = NBITS(max) - 1; i >= 0; i--) \
575 if (dev->bit[i]) break; \
576 for (; i >= 0; i--) \
577 len += sprintf(buf + len, "%lx ", dev->bit[i]); \
578 len += sprintf(buf + len, "\n"); \
581 #define SPRINTF_BIT_B2(bit, name, max, ev) \
583 if (test_bit(ev, dev->evbit)) \
584 SPRINTF_BIT_B(bit, name, max); \
588 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
590 int state = input_devices_state;
591 poll_wait(file, &input_devices_poll_wait, wait);
592 if (state != input_devices_state)
593 return POLLIN | POLLRDNORM;
597 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
599 struct input_dev *dev;
600 struct input_handle *handle;
605 list_for_each_entry(dev, &input_dev_list, node) {
607 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
608 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
610 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
611 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
612 len += sprintf(buf + len, "H: Handlers=");
614 list_for_each_entry(handle, &dev->h_list, d_node)
615 len += sprintf(buf + len, "%s ", handle->name);
617 len += sprintf(buf + len, "\n");
619 SPRINTF_BIT_B(evbit, "EV=", EV_MAX);
620 SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY);
621 SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL);
622 SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS);
623 SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC);
624 SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED);
625 SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND);
626 SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF);
628 len += sprintf(buf + len, "\n");
634 *start = buf + (pos - (at - len));
643 if (&dev->node == &input_dev_list)
646 return (count > cnt) ? cnt : count;
649 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
651 struct input_handler *handler;
654 int len = 0, cnt = 0;
657 list_for_each_entry(handler, &input_handler_list, node) {
660 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
661 i++, handler->name, handler->minor);
663 len = sprintf(buf, "N: Number=%d Name=%s\n",
670 *start = buf + (pos - (at - len));
678 if (&handler->node == &input_handler_list)
681 return (count > cnt) ? cnt : count;
684 static int __init input_proc_init(void)
686 struct proc_dir_entry *entry;
688 proc_bus_input_dir = proc_mkdir("input", proc_bus);
689 if (proc_bus_input_dir == NULL)
691 proc_bus_input_dir->owner = THIS_MODULE;
692 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
694 remove_proc_entry("input", proc_bus);
697 entry->owner = THIS_MODULE;
698 entry->proc_fops->poll = input_devices_poll;
699 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
701 remove_proc_entry("devices", proc_bus_input_dir);
702 remove_proc_entry("input", proc_bus);
705 entry->owner = THIS_MODULE;
708 #else /* !CONFIG_PROC_FS */
709 static inline int input_proc_init(void) { return 0; }
712 struct class_simple *input_class;
714 static int __init input_init(void)
716 int retval = -ENOMEM;
718 input_class = class_simple_create(THIS_MODULE, "input");
719 if (IS_ERR(input_class))
720 return PTR_ERR(input_class);
722 retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
724 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
725 remove_proc_entry("devices", proc_bus_input_dir);
726 remove_proc_entry("handlers", proc_bus_input_dir);
727 remove_proc_entry("input", proc_bus);
728 class_simple_destroy(input_class);
732 retval = devfs_mk_dir("input");
734 remove_proc_entry("devices", proc_bus_input_dir);
735 remove_proc_entry("handlers", proc_bus_input_dir);
736 remove_proc_entry("input", proc_bus);
737 unregister_chrdev(INPUT_MAJOR, "input");
738 class_simple_destroy(input_class);
743 static void __exit input_exit(void)
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);
749 devfs_remove("input");
750 unregister_chrdev(INPUT_MAJOR, "input");
751 class_simple_destroy(input_class);
754 subsys_initcall(input_init);
755 module_exit(input_exit);