[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / input / input.c
blob3385dd03abfc17be819ce992eb8bc314eb64abc5
1 /*
2 * The input core
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
7 /*
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];
51 #ifdef CONFIG_PROC_FS
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;
55 #endif
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))
62 return;
64 add_input_randomness(type, code, value);
66 switch (type) {
68 case EV_SYN:
69 switch (code) {
70 case SYN_CONFIG:
71 if (dev->event) dev->event(dev, type, code, value);
72 break;
74 case SYN_REPORT:
75 if (dev->sync) return;
76 dev->sync = 1;
77 break;
79 break;
81 case EV_KEY:
83 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
84 return;
86 if (value == 2)
87 break;
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]));
96 break;
98 case EV_ABS:
100 if (code > ABS_MAX || !test_bit(code, dev->absbit))
101 return;
103 if (dev->absfuzz[code]) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
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)
118 return;
120 dev->abs[code] = value;
121 break;
123 case EV_REL:
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
126 return;
128 break;
130 case EV_MSC:
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
133 return;
135 if (dev->event) dev->event(dev, type, code, value);
137 break;
139 case EV_LED:
141 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
142 return;
144 change_bit(code, dev->led);
145 if (dev->event) dev->event(dev, type, code, value);
147 break;
149 case EV_SND:
151 if (code > SND_MAX || !test_bit(code, dev->sndbit))
152 return;
154 if (dev->event) dev->event(dev, type, code, value);
156 break;
158 case EV_REP:
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);
165 break;
167 case EV_FF:
168 if (dev->event) dev->event(dev, type, code, value);
169 break;
172 if (type != EV_SYN)
173 dev->sync = 0;
175 if (dev->grab)
176 dev->grab->handler->event(dev->grab, type, code, value);
177 else
178 list_for_each_entry(handle, &dev->h_list, d_node)
179 if (handle->open)
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))
188 return;
190 input_event(dev, EV_KEY, dev->repeat_key, 2);
191 input_sync(dev);
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);
202 return 0;
205 int input_grab_device(struct input_handle *handle)
207 if (handle->dev->grab)
208 return -EBUSY;
210 handle->dev->grab = handle;
211 return 0;
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 handle->open++;
223 if (handle->dev->open)
224 return handle->dev->open(handle->dev);
225 return 0;
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);
233 return 0;
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);
241 handle->open--;
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]) \
253 break; \
254 if (i != NBITS(max)) \
255 continue;
257 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
259 int i;
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)
265 continue;
267 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
268 if (id->id.vendor != dev->id.vendor)
269 continue;
271 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
272 if (id->id.product != dev->id.product)
273 continue;
275 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
276 if (id->id.version != dev->id.version)
277 continue;
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);
288 return id;
291 return NULL;
295 * Input hotplugging interface - loading event handlers based on
296 * device bitfields.
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) \
312 do { \
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]); \
319 scratch++; \
320 } while (0)
322 #define SPRINTF_BIT_A2(bit, name, max, ev) \
323 do { \
324 if (test_bit(ev, dev->evbit)) \
325 SPRINTF_BIT_A(bit, name, max); \
326 } while (0)
328 static void input_call_hotplug(char *verb, struct input_dev *dev)
330 char *argv[3], **envp, *buf, *scratch;
331 int i = 0, j, value;
333 if (!hotplug_path[0]) {
334 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
335 return;
337 if (in_interrupt()) {
338 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
339 return;
341 if (!current->fs->root) {
342 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
343 return;
345 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
346 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
347 return;
349 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
350 kfree (envp);
351 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
352 return;
355 argv[0] = hotplug_path;
356 argv[1] = "input";
357 argv[2] = NULL;
359 envp[i++] = "HOME=/";
360 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
362 scratch = buf;
364 envp[i++] = scratch;
365 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
367 envp[i++] = scratch;
368 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
369 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
371 if (dev->name) {
372 envp[i++] = scratch;
373 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
376 if (dev->phys) {
377 envp[i++] = scratch;
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);
390 envp[i++] = NULL;
392 #ifdef INPUT_DEBUG
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]);
395 #endif
397 value = call_usermodehelper(argv [0], argv, envp, 0);
399 kfree(buf);
400 kfree(envp);
402 #ifdef INPUT_DEBUG
403 if (value != 0)
404 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
405 #endif
408 #endif
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);
442 #endif
444 #ifdef CONFIG_PROC_FS
445 input_devices_state++;
446 wake_up(&input_devices_poll_wait);
447 #endif
450 void input_unregister_device(struct input_dev *dev)
452 struct list_head * node, * next;
454 if (!dev) return;
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);
467 #endif
469 list_del_init(&dev->node);
471 #ifdef CONFIG_PROC_FS
472 input_devices_state++;
473 wake_up(&input_devices_poll_wait);
474 #endif
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);
501 #endif
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);
523 #endif
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;
530 int err;
532 /* No load-on-demand here? */
533 if (!handler || !(new_fops = fops_get(handler->fops)))
534 return -ENODEV;
537 * That's _really_ odd. Usually NULL ->open means "nothing special",
538 * not "no device". Oh, well...
540 if (!new_fops->open) {
541 fops_put(new_fops);
542 return -ENODEV;
544 old_fops = file->f_op;
545 file->f_op = new_fops;
547 err = new_fops->open(inode, file);
549 if (err) {
550 fops_put(file->f_op);
551 file->f_op = fops_get(old_fops);
553 fops_put(old_fops);
554 return err;
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) \
565 do { \
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"); \
572 } while (0)
574 #define SPRINTF_BIT_B2(bit, name, max, ev) \
575 do { \
576 if (test_bit(ev, dev->evbit)) \
577 SPRINTF_BIT_B(bit, name, max); \
578 } while (0)
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;
587 return 0;
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;
595 off_t at = 0;
596 int i, len, cnt = 0;
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");
623 at += len;
625 if (at >= pos) {
626 if (!*start) {
627 *start = buf + (pos - (at - len));
628 cnt = at - pos;
629 } else cnt += len;
630 buf += len;
631 if (cnt >= count)
632 break;
636 if (&dev->node == &input_dev_list)
637 *eof = 1;
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;
646 off_t at = 0;
647 int len = 0, cnt = 0;
648 int i = 0;
650 list_for_each_entry(handler, &input_handler_list, node) {
652 if (handler->fops)
653 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
654 i++, handler->name, handler->minor);
655 else
656 len = sprintf(buf, "N: Number=%d Name=%s\n",
657 i++, handler->name);
659 at += len;
661 if (at >= pos) {
662 if (!*start) {
663 *start = buf + (pos - (at - len));
664 cnt = at - pos;
665 } else cnt += len;
666 buf += len;
667 if (cnt >= count)
668 break;
671 if (&handler->node == &input_handler_list)
672 *eof = 1;
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)
683 return -ENOMEM;
684 proc_bus_input_dir->owner = THIS_MODULE;
685 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
686 if (entry == NULL) {
687 remove_proc_entry("input", proc_bus);
688 return -ENOMEM;
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);
693 if (entry == NULL) {
694 remove_proc_entry("devices", proc_bus_input_dir);
695 remove_proc_entry("input", proc_bus);
696 return -ENOMEM;
698 entry->owner = THIS_MODULE;
699 return 0;
701 #else /* !CONFIG_PROC_FS */
702 static inline int input_proc_init(void) { return 0; }
703 #endif
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);
714 input_proc_init();
715 retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
716 if (retval) {
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);
722 return retval;
725 retval = devfs_mk_dir("input");
726 if (retval) {
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);
733 return retval;
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);