initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / input / input.c
blob416084b8065abf6716021c1a18875608a3f18d31
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/pm.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];
52 #ifdef CONFIG_PROC_FS
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;
56 #endif
58 static inline unsigned int ms_to_jiffies(unsigned int ms)
60 unsigned int j;
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;
70 if (dev->pm_dev)
71 pm_access(dev->pm_dev);
73 if (type > EV_MAX || !test_bit(type, dev->evbit))
74 return;
76 add_mouse_randomness((type << 4) ^ code ^ (code >> 4) ^ value);
78 switch (type) {
80 case EV_SYN:
81 switch (code) {
82 case SYN_CONFIG:
83 if (dev->event) dev->event(dev, type, code, value);
84 break;
86 case SYN_REPORT:
87 if (dev->sync) return;
88 dev->sync = 1;
89 break;
91 break;
93 case EV_KEY:
95 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
96 return;
98 if (value == 2)
99 break;
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]));
108 break;
110 case EV_ABS:
112 if (code > ABS_MAX || !test_bit(code, dev->absbit))
113 return;
115 if (dev->absfuzz[code]) {
116 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
117 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
118 return;
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)
130 return;
132 dev->abs[code] = value;
133 break;
135 case EV_REL:
137 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
138 return;
140 break;
142 case EV_MSC:
144 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
145 return;
147 if (dev->event) dev->event(dev, type, code, value);
149 break;
151 case EV_LED:
153 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
154 return;
156 change_bit(code, dev->led);
157 if (dev->event) dev->event(dev, type, code, value);
159 break;
161 case EV_SND:
163 if (code > SND_MAX || !test_bit(code, dev->sndbit))
164 return;
166 if (dev->event) dev->event(dev, type, code, value);
168 break;
170 case EV_REP:
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);
177 break;
179 case EV_FF:
180 if (dev->event) dev->event(dev, type, code, value);
181 break;
184 if (type != EV_SYN)
185 dev->sync = 0;
187 if (dev->grab)
188 dev->grab->handler->event(dev->grab, type, code, value);
189 else
190 list_for_each_entry(handle, &dev->h_list, d_node)
191 if (handle->open)
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))
200 return;
202 input_event(dev, EV_KEY, dev->repeat_key, 2);
203 input_sync(dev);
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);
213 return 0;
216 int input_grab_device(struct input_handle *handle)
218 if (handle->dev->grab)
219 return -EBUSY;
221 handle->dev->grab = handle;
222 return 0;
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);
235 handle->open++;
236 if (handle->dev->open)
237 return handle->dev->open(handle->dev);
238 return 0;
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);
246 return 0;
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);
256 handle->open--;
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]) \
268 break; \
269 if (i != NBITS(max)) \
270 continue;
272 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
274 int i;
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)
280 continue;
282 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
283 if (id->id.vendor != dev->id.vendor)
284 continue;
286 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
287 if (id->id.product != dev->id.product)
288 continue;
290 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
291 if (id->id.version != dev->id.version)
292 continue;
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);
303 return id;
306 return NULL;
310 * Input hotplugging interface - loading event handlers based on
311 * device bitfields.
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) \
327 do { \
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]); \
334 scratch++; \
335 } while (0)
337 #define SPRINTF_BIT_A2(bit, name, max, ev) \
338 do { \
339 if (test_bit(ev, dev->evbit)) \
340 SPRINTF_BIT_A(bit, name, max); \
341 } while (0)
343 static void input_call_hotplug(char *verb, struct input_dev *dev)
345 char *argv[3], **envp, *buf, *scratch;
346 int i = 0, j, value;
348 if (!hotplug_path[0]) {
349 printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n");
350 return;
352 if (in_interrupt()) {
353 printk(KERN_ERR "input.c: calling hotplug from interrupt\n");
354 return;
356 if (!current->fs->root) {
357 printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n");
358 return;
360 if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) {
361 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
362 return;
364 if (!(buf = kmalloc(1024, GFP_KERNEL))) {
365 kfree (envp);
366 printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n");
367 return;
370 argv[0] = hotplug_path;
371 argv[1] = "input";
372 argv[2] = NULL;
374 envp[i++] = "HOME=/";
375 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
377 scratch = buf;
379 envp[i++] = scratch;
380 scratch += sprintf(scratch, "ACTION=%s", verb) + 1;
382 envp[i++] = scratch;
383 scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x",
384 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1;
386 if (dev->name) {
387 envp[i++] = scratch;
388 scratch += sprintf(scratch, "NAME=%s", dev->name) + 1;
391 if (dev->phys) {
392 envp[i++] = scratch;
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);
405 envp[i++] = NULL;
407 #ifdef INPUT_DEBUG
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]);
410 #endif
412 value = call_usermodehelper(argv [0], argv, envp, 0);
414 kfree(buf);
415 kfree(envp);
417 #ifdef INPUT_DEBUG
418 if (value != 0)
419 printk(KERN_DEBUG "input.c: hotplug returned %d\n", value);
420 #endif
423 #endif
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);
457 #endif
459 #ifdef CONFIG_PROC_FS
460 input_devices_state++;
461 wake_up(&input_devices_poll_wait);
462 #endif
465 void input_unregister_device(struct input_dev *dev)
467 struct list_head * node, * next;
469 if (!dev) return;
471 if (dev->pm_dev)
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);
485 #endif
487 list_del_init(&dev->node);
489 #ifdef CONFIG_PROC_FS
490 input_devices_state++;
491 wake_up(&input_devices_poll_wait);
492 #endif
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);
519 #endif
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);
541 #endif
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;
548 int err;
550 /* No load-on-demand here? */
551 if (!handler || !(new_fops = fops_get(handler->fops)))
552 return -ENODEV;
555 * That's _really_ odd. Usually NULL ->open means "nothing special",
556 * not "no device". Oh, well...
558 if (!new_fops->open) {
559 fops_put(new_fops);
560 return -ENODEV;
562 old_fops = file->f_op;
563 file->f_op = new_fops;
565 err = new_fops->open(inode, file);
567 if (err) {
568 fops_put(file->f_op);
569 file->f_op = fops_get(old_fops);
571 fops_put(old_fops);
572 return err;
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) \
583 do { \
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"); \
590 } while (0)
592 #define SPRINTF_BIT_B2(bit, name, max, ev) \
593 do { \
594 if (test_bit(ev, dev->evbit)) \
595 SPRINTF_BIT_B(bit, name, max); \
596 } while (0)
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;
605 return 0;
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;
613 off_t at = 0;
614 int i, len, cnt = 0;
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");
641 at += len;
643 if (at >= pos) {
644 if (!*start) {
645 *start = buf + (pos - (at - len));
646 cnt = at - pos;
647 } else cnt += len;
648 buf += len;
649 if (cnt >= count)
650 break;
654 if (&dev->node == &input_dev_list)
655 *eof = 1;
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;
664 off_t at = 0;
665 int len = 0, cnt = 0;
666 int i = 0;
668 list_for_each_entry(handler, &input_handler_list, node) {
670 if (handler->fops)
671 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
672 i++, handler->name, handler->minor);
673 else
674 len = sprintf(buf, "N: Number=%d Name=%s\n",
675 i++, handler->name);
677 at += len;
679 if (at >= pos) {
680 if (!*start) {
681 *start = buf + (pos - (at - len));
682 cnt = at - pos;
683 } else cnt += len;
684 buf += len;
685 if (cnt >= count)
686 break;
689 if (&handler->node == &input_handler_list)
690 *eof = 1;
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)
701 return -ENOMEM;
702 proc_bus_input_dir->owner = THIS_MODULE;
703 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
704 if (entry == NULL) {
705 remove_proc_entry("input", proc_bus);
706 return -ENOMEM;
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);
711 if (entry == NULL) {
712 remove_proc_entry("devices", proc_bus_input_dir);
713 remove_proc_entry("input", proc_bus);
714 return -ENOMEM;
716 entry->owner = THIS_MODULE;
717 return 0;
719 #else /* !CONFIG_PROC_FS */
720 static inline int input_proc_init(void) { return 0; }
721 #endif
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);
732 input_proc_init();
733 retval = register_chrdev(INPUT_MAJOR, "input", &input_fops);
734 if (retval) {
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);
740 return retval;
743 retval = devfs_mk_dir("input");
744 if (retval) {
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);
751 return retval;
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);