[PATCH] fix missing includes
[linux-2.6/zen-sources.git] / drivers / input / input.c
blob3b1685ff9d10b38db94defa977cf1e6c43effc38
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>
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_GPL(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))
56 return;
58 add_input_randomness(type, code, value);
60 switch (type) {
62 case EV_SYN:
63 switch (code) {
64 case SYN_CONFIG:
65 if (dev->event) dev->event(dev, type, code, value);
66 break;
68 case SYN_REPORT:
69 if (dev->sync) return;
70 dev->sync = 1;
71 break;
73 break;
75 case EV_KEY:
77 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
78 return;
80 if (value == 2)
81 break;
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]));
90 break;
92 case EV_SW:
94 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
95 return;
97 change_bit(code, dev->sw);
99 break;
101 case EV_ABS:
103 if (code > ABS_MAX || !test_bit(code, dev->absbit))
104 return;
106 if (dev->absfuzz[code]) {
107 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
108 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
109 return;
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)
121 return;
123 dev->abs[code] = value;
124 break;
126 case EV_REL:
128 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
129 return;
131 break;
133 case EV_MSC:
135 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
136 return;
138 if (dev->event) dev->event(dev, type, code, value);
140 break;
142 case EV_LED:
144 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
145 return;
147 change_bit(code, dev->led);
148 if (dev->event) dev->event(dev, type, code, value);
150 break;
152 case EV_SND:
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
155 return;
157 if (dev->event) dev->event(dev, type, code, value);
159 break;
161 case EV_REP:
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);
168 break;
170 case EV_FF:
171 if (dev->event) dev->event(dev, type, code, value);
172 break;
175 if (type != EV_SYN)
176 dev->sync = 0;
178 if (dev->grab)
179 dev->grab->handler->event(dev->grab, type, code, value);
180 else
181 list_for_each_entry(handle, &dev->h_list, d_node)
182 if (handle->open)
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))
191 return;
193 input_event(dev, EV_KEY, dev->repeat_key, 2);
194 input_sync(dev);
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);
205 return 0;
208 int input_grab_device(struct input_handle *handle)
210 if (handle->dev->grab)
211 return -EBUSY;
213 handle->dev->grab = handle;
214 return 0;
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;
226 int err;
228 err = down_interruptible(&dev->sem);
229 if (err)
230 return err;
232 handle->open++;
234 if (!dev->users++ && dev->open)
235 err = dev->open(dev);
237 if (err)
238 handle->open--;
240 up(&dev->sem);
242 return err;
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);
250 return 0;
253 void input_close_device(struct input_handle *handle)
255 struct input_dev *dev = handle->dev;
257 input_release_device(handle);
259 down(&dev->sem);
261 if (!--dev->users && dev->close)
262 dev->close(dev);
263 handle->open--;
265 up(&dev->sem);
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]) \
277 break; \
278 if (i != NBITS(max)) \
279 continue;
281 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
283 int i;
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)
289 continue;
291 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
292 if (id->id.vendor != dev->id.vendor)
293 continue;
295 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
296 if (id->id.product != dev->id.product)
297 continue;
299 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
300 if (id->id.version != dev->id.version)
301 continue;
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);
313 return id;
316 return NULL;
319 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, int max)
321 int i;
322 int len = 0;
324 for (i = NBITS(max) - 1; i > 0; i--)
325 if (bitmap[i])
326 break;
328 for (; i >= 0; i--)
329 len += snprintf(buf + len, max(buf_size - len, 0),
330 "%lx%s", bitmap[i], i > 0 ? " " : "");
331 return len;
334 #ifdef CONFIG_PROC_FS
336 static struct proc_dir_entry *proc_bus_input_dir;
337 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
338 static int input_devices_state;
340 static inline void input_wakeup_procfs_readers(void)
342 input_devices_state++;
343 wake_up(&input_devices_poll_wait);
346 static unsigned int input_devices_poll(struct file *file, poll_table *wait)
348 int state = input_devices_state;
349 poll_wait(file, &input_devices_poll_wait, wait);
350 if (state != input_devices_state)
351 return POLLIN | POLLRDNORM;
352 return 0;
355 #define SPRINTF_BIT(ev, bm) \
356 do { \
357 len += sprintf(buf + len, "B: %s=", #ev); \
358 len += input_print_bitmap(buf + len, INT_MAX, \
359 dev->bm##bit, ev##_MAX); \
360 len += sprintf(buf + len, "\n"); \
361 } while (0)
363 #define TEST_AND_SPRINTF_BIT(ev, bm) \
364 do { \
365 if (test_bit(EV_##ev, dev->evbit)) \
366 SPRINTF_BIT(ev, bm); \
367 } while (0)
369 static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
371 struct input_dev *dev;
372 struct input_handle *handle;
373 const char *path;
375 off_t at = 0;
376 int len, cnt = 0;
378 list_for_each_entry(dev, &input_dev_list, node) {
380 path = dev->dynalloc ? kobject_get_path(&dev->cdev.kobj, GFP_KERNEL) : NULL;
382 len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
383 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
385 len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
386 len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : "");
387 len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : "");
388 len += sprintf(buf + len, "H: Handlers=");
390 list_for_each_entry(handle, &dev->h_list, d_node)
391 len += sprintf(buf + len, "%s ", handle->name);
393 len += sprintf(buf + len, "\n");
395 SPRINTF_BIT(EV, ev);
396 TEST_AND_SPRINTF_BIT(KEY, key);
397 TEST_AND_SPRINTF_BIT(REL, rel);
398 TEST_AND_SPRINTF_BIT(ABS, abs);
399 TEST_AND_SPRINTF_BIT(MSC, msc);
400 TEST_AND_SPRINTF_BIT(LED, led);
401 TEST_AND_SPRINTF_BIT(SND, snd);
402 TEST_AND_SPRINTF_BIT(FF, ff);
403 TEST_AND_SPRINTF_BIT(SW, sw);
405 len += sprintf(buf + len, "\n");
407 at += len;
409 if (at >= pos) {
410 if (!*start) {
411 *start = buf + (pos - (at - len));
412 cnt = at - pos;
413 } else cnt += len;
414 buf += len;
415 if (cnt >= count)
416 break;
419 kfree(path);
422 if (&dev->node == &input_dev_list)
423 *eof = 1;
425 return (count > cnt) ? cnt : count;
428 static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data)
430 struct input_handler *handler;
432 off_t at = 0;
433 int len = 0, cnt = 0;
434 int i = 0;
436 list_for_each_entry(handler, &input_handler_list, node) {
438 if (handler->fops)
439 len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n",
440 i++, handler->name, handler->minor);
441 else
442 len = sprintf(buf, "N: Number=%d Name=%s\n",
443 i++, handler->name);
445 at += len;
447 if (at >= pos) {
448 if (!*start) {
449 *start = buf + (pos - (at - len));
450 cnt = at - pos;
451 } else cnt += len;
452 buf += len;
453 if (cnt >= count)
454 break;
457 if (&handler->node == &input_handler_list)
458 *eof = 1;
460 return (count > cnt) ? cnt : count;
463 static struct file_operations input_fileops;
465 static int __init input_proc_init(void)
467 struct proc_dir_entry *entry;
469 proc_bus_input_dir = proc_mkdir("input", proc_bus);
470 if (!proc_bus_input_dir)
471 return -ENOMEM;
473 proc_bus_input_dir->owner = THIS_MODULE;
475 entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL);
476 if (!entry)
477 goto fail1;
479 entry->owner = THIS_MODULE;
480 input_fileops = *entry->proc_fops;
481 entry->proc_fops = &input_fileops;
482 entry->proc_fops->poll = input_devices_poll;
484 entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL);
485 if (!entry)
486 goto fail2;
488 entry->owner = THIS_MODULE;
490 return 0;
492 fail2: remove_proc_entry("devices", proc_bus_input_dir);
493 fail1: remove_proc_entry("input", proc_bus);
494 return -ENOMEM;
497 static void input_proc_exit(void)
499 remove_proc_entry("devices", proc_bus_input_dir);
500 remove_proc_entry("handlers", proc_bus_input_dir);
501 remove_proc_entry("input", proc_bus);
504 #else /* !CONFIG_PROC_FS */
505 static inline void input_wakeup_procfs_readers(void) { }
506 static inline int input_proc_init(void) { return 0; }
507 static inline void input_proc_exit(void) { }
508 #endif
510 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
511 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
513 struct input_dev *input_dev = to_input_dev(dev); \
514 int retval; \
516 retval = down_interruptible(&input_dev->sem); \
517 if (retval) \
518 return retval; \
520 retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \
522 up(&input_dev->sem); \
524 return retval; \
526 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
528 INPUT_DEV_STRING_ATTR_SHOW(name);
529 INPUT_DEV_STRING_ATTR_SHOW(phys);
530 INPUT_DEV_STRING_ATTR_SHOW(uniq);
532 static struct attribute *input_dev_attrs[] = {
533 &class_device_attr_name.attr,
534 &class_device_attr_phys.attr,
535 &class_device_attr_uniq.attr,
536 NULL
539 static struct attribute_group input_dev_group = {
540 .attrs = input_dev_attrs,
543 #define INPUT_DEV_ID_ATTR(name) \
544 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
546 struct input_dev *input_dev = to_input_dev(dev); \
547 return sprintf(buf, "%04x\n", input_dev->id.name); \
549 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
551 INPUT_DEV_ID_ATTR(bustype);
552 INPUT_DEV_ID_ATTR(vendor);
553 INPUT_DEV_ID_ATTR(product);
554 INPUT_DEV_ID_ATTR(version);
556 static struct attribute *input_dev_id_attrs[] = {
557 &class_device_attr_bustype.attr,
558 &class_device_attr_vendor.attr,
559 &class_device_attr_product.attr,
560 &class_device_attr_version.attr,
561 NULL
564 static struct attribute_group input_dev_id_attr_group = {
565 .name = "id",
566 .attrs = input_dev_id_attrs,
569 #define INPUT_DEV_CAP_ATTR(ev, bm) \
570 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
572 struct input_dev *input_dev = to_input_dev(dev); \
573 return input_print_bitmap(buf, PAGE_SIZE, input_dev->bm##bit, ev##_MAX);\
575 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
577 INPUT_DEV_CAP_ATTR(EV, ev);
578 INPUT_DEV_CAP_ATTR(KEY, key);
579 INPUT_DEV_CAP_ATTR(REL, rel);
580 INPUT_DEV_CAP_ATTR(ABS, abs);
581 INPUT_DEV_CAP_ATTR(MSC, msc);
582 INPUT_DEV_CAP_ATTR(LED, led);
583 INPUT_DEV_CAP_ATTR(SND, snd);
584 INPUT_DEV_CAP_ATTR(FF, ff);
585 INPUT_DEV_CAP_ATTR(SW, sw);
587 static struct attribute *input_dev_caps_attrs[] = {
588 &class_device_attr_ev.attr,
589 &class_device_attr_key.attr,
590 &class_device_attr_rel.attr,
591 &class_device_attr_abs.attr,
592 &class_device_attr_msc.attr,
593 &class_device_attr_led.attr,
594 &class_device_attr_snd.attr,
595 &class_device_attr_ff.attr,
596 &class_device_attr_sw.attr,
597 NULL
600 static struct attribute_group input_dev_caps_attr_group = {
601 .name = "capabilities",
602 .attrs = input_dev_caps_attrs,
605 static void input_dev_release(struct class_device *class_dev)
607 struct input_dev *dev = to_input_dev(class_dev);
609 kfree(dev);
610 module_put(THIS_MODULE);
614 * Input hotplugging interface - loading event handlers based on
615 * device bitfields.
617 static int input_add_hotplug_bm_var(char **envp, int num_envp, int *cur_index,
618 char *buffer, int buffer_size, int *cur_len,
619 const char *name, unsigned long *bitmap, int max)
621 if (*cur_index >= num_envp - 1)
622 return -ENOMEM;
624 envp[*cur_index] = buffer + *cur_len;
626 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
627 if (*cur_len > buffer_size)
628 return -ENOMEM;
630 *cur_len += input_print_bitmap(buffer + *cur_len,
631 max(buffer_size - *cur_len, 0),
632 bitmap, max) + 1;
633 if (*cur_len > buffer_size)
634 return -ENOMEM;
636 (*cur_index)++;
637 return 0;
640 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
641 do { \
642 int err = add_hotplug_env_var(envp, num_envp, &i, \
643 buffer, buffer_size, &len, \
644 fmt, val); \
645 if (err) \
646 return err; \
647 } while (0)
649 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
650 do { \
651 int err = input_add_hotplug_bm_var(envp, num_envp, &i, \
652 buffer, buffer_size, &len, \
653 name, bm, max); \
654 if (err) \
655 return err; \
656 } while (0)
658 static int input_dev_hotplug(struct class_device *cdev, char **envp,
659 int num_envp, char *buffer, int buffer_size)
661 struct input_dev *dev = to_input_dev(cdev);
662 int i = 0;
663 int len = 0;
665 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
666 dev->id.bustype, dev->id.vendor,
667 dev->id.product, dev->id.version);
668 if (dev->name)
669 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
670 if (dev->phys)
671 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
672 if (dev->phys)
673 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
675 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
676 if (test_bit(EV_KEY, dev->evbit))
677 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
678 if (test_bit(EV_REL, dev->evbit))
679 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
680 if (test_bit(EV_ABS, dev->evbit))
681 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
682 if (test_bit(EV_MSC, dev->evbit))
683 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
684 if (test_bit(EV_LED, dev->evbit))
685 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
686 if (test_bit(EV_SND, dev->evbit))
687 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
688 if (test_bit(EV_FF, dev->evbit))
689 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
690 if (test_bit(EV_SW, dev->evbit))
691 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
693 envp[i] = NULL;
695 return 0;
698 struct class input_class = {
699 .name = "input",
700 .release = input_dev_release,
701 .hotplug = input_dev_hotplug,
704 struct input_dev *input_allocate_device(void)
706 struct input_dev *dev;
708 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
709 if (dev) {
710 dev->dynalloc = 1;
711 dev->cdev.class = &input_class;
712 class_device_initialize(&dev->cdev);
713 INIT_LIST_HEAD(&dev->h_list);
714 INIT_LIST_HEAD(&dev->node);
717 return dev;
720 static void input_register_classdevice(struct input_dev *dev)
722 static atomic_t input_no = ATOMIC_INIT(0);
723 const char *path;
725 __module_get(THIS_MODULE);
727 dev->dev = dev->cdev.dev;
729 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
730 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
732 path = kobject_get_path(&dev->cdev.class->subsys.kset.kobj, GFP_KERNEL);
733 printk(KERN_INFO "input: %s/%s as %s\n",
734 dev->name ? dev->name : "Unspecified device",
735 path ? path : "", dev->cdev.class_id);
736 kfree(path);
738 class_device_add(&dev->cdev);
739 sysfs_create_group(&dev->cdev.kobj, &input_dev_group);
740 sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
741 sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
744 void input_register_device(struct input_dev *dev)
746 struct input_handle *handle;
747 struct input_handler *handler;
748 struct input_device_id *id;
750 set_bit(EV_SYN, dev->evbit);
752 init_MUTEX(&dev->sem);
755 * If delay and period are pre-set by the driver, then autorepeating
756 * is handled by the driver itself and we don't do it in input.c.
759 init_timer(&dev->timer);
760 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
761 dev->timer.data = (long) dev;
762 dev->timer.function = input_repeat_key;
763 dev->rep[REP_DELAY] = 250;
764 dev->rep[REP_PERIOD] = 33;
767 INIT_LIST_HEAD(&dev->h_list);
768 list_add_tail(&dev->node, &input_dev_list);
770 if (dev->dynalloc)
771 input_register_classdevice(dev);
773 list_for_each_entry(handler, &input_handler_list, node)
774 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
775 if ((id = input_match_device(handler->id_table, dev)))
776 if ((handle = handler->connect(handler, dev, id)))
777 input_link_handle(handle);
780 input_wakeup_procfs_readers();
783 void input_unregister_device(struct input_dev *dev)
785 struct list_head * node, * next;
787 if (!dev) return;
789 del_timer_sync(&dev->timer);
791 list_for_each_safe(node, next, &dev->h_list) {
792 struct input_handle * handle = to_handle(node);
793 list_del_init(&handle->d_node);
794 list_del_init(&handle->h_node);
795 handle->handler->disconnect(handle);
798 list_del_init(&dev->node);
800 if (dev->dynalloc) {
801 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
802 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
803 class_device_unregister(&dev->cdev);
806 input_wakeup_procfs_readers();
809 void input_register_handler(struct input_handler *handler)
811 struct input_dev *dev;
812 struct input_handle *handle;
813 struct input_device_id *id;
815 if (!handler) return;
817 INIT_LIST_HEAD(&handler->h_list);
819 if (handler->fops != NULL)
820 input_table[handler->minor >> 5] = handler;
822 list_add_tail(&handler->node, &input_handler_list);
824 list_for_each_entry(dev, &input_dev_list, node)
825 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
826 if ((id = input_match_device(handler->id_table, dev)))
827 if ((handle = handler->connect(handler, dev, id)))
828 input_link_handle(handle);
830 input_wakeup_procfs_readers();
833 void input_unregister_handler(struct input_handler *handler)
835 struct list_head * node, * next;
837 list_for_each_safe(node, next, &handler->h_list) {
838 struct input_handle * handle = to_handle_h(node);
839 list_del_init(&handle->h_node);
840 list_del_init(&handle->d_node);
841 handler->disconnect(handle);
844 list_del_init(&handler->node);
846 if (handler->fops != NULL)
847 input_table[handler->minor >> 5] = NULL;
849 input_wakeup_procfs_readers();
852 static int input_open_file(struct inode *inode, struct file *file)
854 struct input_handler *handler = input_table[iminor(inode) >> 5];
855 struct file_operations *old_fops, *new_fops = NULL;
856 int err;
858 /* No load-on-demand here? */
859 if (!handler || !(new_fops = fops_get(handler->fops)))
860 return -ENODEV;
863 * That's _really_ odd. Usually NULL ->open means "nothing special",
864 * not "no device". Oh, well...
866 if (!new_fops->open) {
867 fops_put(new_fops);
868 return -ENODEV;
870 old_fops = file->f_op;
871 file->f_op = new_fops;
873 err = new_fops->open(inode, file);
875 if (err) {
876 fops_put(file->f_op);
877 file->f_op = fops_get(old_fops);
879 fops_put(old_fops);
880 return err;
883 static struct file_operations input_fops = {
884 .owner = THIS_MODULE,
885 .open = input_open_file,
888 static int __init input_init(void)
890 int err;
892 err = class_register(&input_class);
893 if (err) {
894 printk(KERN_ERR "input: unable to register input_dev class\n");
895 return err;
898 err = input_proc_init();
899 if (err)
900 goto fail1;
902 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
903 if (err) {
904 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
905 goto fail2;
908 return 0;
910 fail2: input_proc_exit();
911 fail1: class_unregister(&input_class);
912 return err;
915 static void __exit input_exit(void)
917 input_proc_exit();
918 unregister_chrdev(INPUT_MAJOR, "input");
919 class_unregister(&input_class);
922 subsys_initcall(input_init);
923 module_exit(input_exit);