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/seq_file.h>
22 #include <linux/interrupt.h>
23 #include <linux/poll.h>
24 #include <linux/device.h>
25 #include <linux/mutex.h>
27 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
28 MODULE_DESCRIPTION("Input core");
29 MODULE_LICENSE("GPL");
31 #define INPUT_DEVICES 256
33 static LIST_HEAD(input_dev_list);
34 static LIST_HEAD(input_handler_list);
36 static struct input_handler *input_table[8];
38 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
40 struct input_handle *handle;
42 if (type > EV_MAX || !test_bit(type, dev->evbit))
45 add_input_randomness(type, code, value);
53 dev->event(dev, type, code, value);
66 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
72 change_bit(code, dev->key);
74 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
75 dev->repeat_key = code;
76 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
83 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
86 change_bit(code, dev->sw);
92 if (code > ABS_MAX || !test_bit(code, dev->absbit))
95 if (dev->absfuzz[code]) {
96 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
97 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
100 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
101 (value < dev->abs[code] + dev->absfuzz[code]))
102 value = (dev->abs[code] * 3 + value) >> 2;
104 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
106 value = (dev->abs[code] + value) >> 1;
109 if (dev->abs[code] == value)
112 dev->abs[code] = value;
117 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
124 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
128 dev->event(dev, type, code, value);
134 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
137 change_bit(code, dev->led);
140 dev->event(dev, type, code, value);
146 if (code > SND_MAX || !test_bit(code, dev->sndbit))
149 if (!!test_bit(code, dev->snd) != !!value)
150 change_bit(code, dev->snd);
153 dev->event(dev, type, code, value);
159 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
162 dev->rep[code] = value;
164 dev->event(dev, type, code, value);
170 dev->event(dev, type, code, value);
178 dev->grab->handler->event(dev->grab, type, code, value);
180 list_for_each_entry(handle, &dev->h_list, d_node)
182 handle->handler->event(handle, type, code, value);
184 EXPORT_SYMBOL(input_event);
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_grab_device(struct input_handle *handle)
202 if (handle->dev->grab)
205 handle->dev->grab = handle;
208 EXPORT_SYMBOL(input_grab_device);
210 void input_release_device(struct input_handle *handle)
212 if (handle->dev->grab == handle)
213 handle->dev->grab = NULL;
215 EXPORT_SYMBOL(input_release_device);
217 int input_open_device(struct input_handle *handle)
219 struct input_dev *dev = handle->dev;
222 err = mutex_lock_interruptible(&dev->mutex);
228 if (!dev->users++ && dev->open)
229 err = dev->open(dev);
234 mutex_unlock(&dev->mutex);
238 EXPORT_SYMBOL(input_open_device);
240 int input_flush_device(struct input_handle* handle, struct file* file)
242 if (handle->dev->flush)
243 return handle->dev->flush(handle->dev, file);
247 EXPORT_SYMBOL(input_flush_device);
249 void input_close_device(struct input_handle *handle)
251 struct input_dev *dev = handle->dev;
253 input_release_device(handle);
255 mutex_lock(&dev->mutex);
257 if (!--dev->users && dev->close)
261 mutex_unlock(&dev->mutex);
263 EXPORT_SYMBOL(input_close_device);
265 static void input_link_handle(struct input_handle *handle)
267 list_add_tail(&handle->d_node, &handle->dev->h_list);
268 list_add_tail(&handle->h_node, &handle->handler->h_list);
271 #define MATCH_BIT(bit, max) \
272 for (i = 0; i < NBITS(max); i++) \
273 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
275 if (i != NBITS(max)) \
278 static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev)
282 for (; id->flags || id->driver_info; id++) {
284 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
285 if (id->bustype != dev->id.bustype)
288 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
289 if (id->vendor != dev->id.vendor)
292 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
293 if (id->product != dev->id.product)
296 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
297 if (id->version != dev->id.version)
300 MATCH_BIT(evbit, EV_MAX);
301 MATCH_BIT(keybit, KEY_MAX);
302 MATCH_BIT(relbit, REL_MAX);
303 MATCH_BIT(absbit, ABS_MAX);
304 MATCH_BIT(mscbit, MSC_MAX);
305 MATCH_BIT(ledbit, LED_MAX);
306 MATCH_BIT(sndbit, SND_MAX);
307 MATCH_BIT(ffbit, FF_MAX);
308 MATCH_BIT(swbit, SW_MAX);
316 #ifdef CONFIG_PROC_FS
318 static struct proc_dir_entry *proc_bus_input_dir;
319 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
320 static int input_devices_state;
322 static inline void input_wakeup_procfs_readers(void)
324 input_devices_state++;
325 wake_up(&input_devices_poll_wait);
328 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
330 int state = input_devices_state;
332 poll_wait(file, &input_devices_poll_wait, wait);
333 if (state != input_devices_state)
334 return POLLIN | POLLRDNORM;
339 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
341 struct list_head *node;
344 list_for_each(node, list)
351 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
353 if (element->next == list)
357 return element->next;
360 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
362 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
364 return list_get_nth_element(&input_dev_list, pos);
367 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
369 return list_get_next_element(&input_dev_list, v, pos);
372 static void input_devices_seq_stop(struct seq_file *seq, void *v)
374 /* release lock here */
377 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
378 unsigned long *bitmap, int max)
382 for (i = NBITS(max) - 1; i > 0; i--)
386 seq_printf(seq, "B: %s=", name);
388 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
392 static int input_devices_seq_show(struct seq_file *seq, void *v)
394 struct input_dev *dev = container_of(v, struct input_dev, node);
395 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
396 struct input_handle *handle;
398 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
399 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
401 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
402 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
403 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
404 seq_printf(seq, "H: Handlers=");
406 list_for_each_entry(handle, &dev->h_list, d_node)
407 seq_printf(seq, "%s ", handle->name);
410 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
411 if (test_bit(EV_KEY, dev->evbit))
412 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
413 if (test_bit(EV_REL, dev->evbit))
414 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
415 if (test_bit(EV_ABS, dev->evbit))
416 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
417 if (test_bit(EV_MSC, dev->evbit))
418 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
419 if (test_bit(EV_LED, dev->evbit))
420 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
421 if (test_bit(EV_SND, dev->evbit))
422 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
423 if (test_bit(EV_FF, dev->evbit))
424 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
425 if (test_bit(EV_SW, dev->evbit))
426 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
434 static struct seq_operations input_devices_seq_ops = {
435 .start = input_devices_seq_start,
436 .next = input_devices_seq_next,
437 .stop = input_devices_seq_stop,
438 .show = input_devices_seq_show,
441 static int input_proc_devices_open(struct inode *inode, struct file *file)
443 return seq_open(file, &input_devices_seq_ops);
446 static struct file_operations input_devices_fileops = {
447 .owner = THIS_MODULE,
448 .open = input_proc_devices_open,
449 .poll = input_proc_devices_poll,
452 .release = seq_release,
455 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
457 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
458 seq->private = (void *)(unsigned long)*pos;
459 return list_get_nth_element(&input_handler_list, pos);
462 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
464 seq->private = (void *)(unsigned long)(*pos + 1);
465 return list_get_next_element(&input_handler_list, v, pos);
468 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
470 /* release lock here */
473 static int input_handlers_seq_show(struct seq_file *seq, void *v)
475 struct input_handler *handler = container_of(v, struct input_handler, node);
477 seq_printf(seq, "N: Number=%ld Name=%s",
478 (unsigned long)seq->private, handler->name);
480 seq_printf(seq, " Minor=%d", handler->minor);
485 static struct seq_operations input_handlers_seq_ops = {
486 .start = input_handlers_seq_start,
487 .next = input_handlers_seq_next,
488 .stop = input_handlers_seq_stop,
489 .show = input_handlers_seq_show,
492 static int input_proc_handlers_open(struct inode *inode, struct file *file)
494 return seq_open(file, &input_handlers_seq_ops);
497 static struct file_operations input_handlers_fileops = {
498 .owner = THIS_MODULE,
499 .open = input_proc_handlers_open,
502 .release = seq_release,
505 static int __init input_proc_init(void)
507 struct proc_dir_entry *entry;
509 proc_bus_input_dir = proc_mkdir("input", proc_bus);
510 if (!proc_bus_input_dir)
513 proc_bus_input_dir->owner = THIS_MODULE;
515 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
519 entry->owner = THIS_MODULE;
520 entry->proc_fops = &input_devices_fileops;
522 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
526 entry->owner = THIS_MODULE;
527 entry->proc_fops = &input_handlers_fileops;
531 fail2: remove_proc_entry("devices", proc_bus_input_dir);
532 fail1: remove_proc_entry("input", proc_bus);
536 static void input_proc_exit(void)
538 remove_proc_entry("devices", proc_bus_input_dir);
539 remove_proc_entry("handlers", proc_bus_input_dir);
540 remove_proc_entry("input", proc_bus);
543 #else /* !CONFIG_PROC_FS */
544 static inline void input_wakeup_procfs_readers(void) { }
545 static inline int input_proc_init(void) { return 0; }
546 static inline void input_proc_exit(void) { }
549 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
550 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
552 struct input_dev *input_dev = to_input_dev(dev); \
555 retval = mutex_lock_interruptible(&input_dev->mutex); \
559 retval = scnprintf(buf, PAGE_SIZE, \
560 "%s\n", input_dev->name ? input_dev->name : ""); \
562 mutex_unlock(&input_dev->mutex); \
566 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
568 INPUT_DEV_STRING_ATTR_SHOW(name);
569 INPUT_DEV_STRING_ATTR_SHOW(phys);
570 INPUT_DEV_STRING_ATTR_SHOW(uniq);
572 static int input_print_modalias_bits(char *buf, int size,
573 char name, unsigned long *bm,
574 unsigned int min_bit, unsigned int max_bit)
578 len += snprintf(buf, max(size, 0), "%c", name);
579 for (i = min_bit; i < max_bit; i++)
580 if (bm[LONG(i)] & BIT(i))
581 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
585 static int input_print_modalias(char *buf, int size, struct input_dev *id,
590 len = snprintf(buf, max(size, 0),
591 "input:b%04Xv%04Xp%04Xe%04X-",
592 id->id.bustype, id->id.vendor,
593 id->id.product, id->id.version);
595 len += input_print_modalias_bits(buf + len, size - len,
596 'e', id->evbit, 0, EV_MAX);
597 len += input_print_modalias_bits(buf + len, size - len,
598 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
599 len += input_print_modalias_bits(buf + len, size - len,
600 'r', id->relbit, 0, REL_MAX);
601 len += input_print_modalias_bits(buf + len, size - len,
602 'a', id->absbit, 0, ABS_MAX);
603 len += input_print_modalias_bits(buf + len, size - len,
604 'm', id->mscbit, 0, MSC_MAX);
605 len += input_print_modalias_bits(buf + len, size - len,
606 'l', id->ledbit, 0, LED_MAX);
607 len += input_print_modalias_bits(buf + len, size - len,
608 's', id->sndbit, 0, SND_MAX);
609 len += input_print_modalias_bits(buf + len, size - len,
610 'f', id->ffbit, 0, FF_MAX);
611 len += input_print_modalias_bits(buf + len, size - len,
612 'w', id->swbit, 0, SW_MAX);
615 len += snprintf(buf + len, max(size - len, 0), "\n");
620 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
622 struct input_dev *id = to_input_dev(dev);
625 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
627 return min_t(int, len, PAGE_SIZE);
629 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
631 static struct attribute *input_dev_attrs[] = {
632 &class_device_attr_name.attr,
633 &class_device_attr_phys.attr,
634 &class_device_attr_uniq.attr,
635 &class_device_attr_modalias.attr,
639 static struct attribute_group input_dev_attr_group = {
640 .attrs = input_dev_attrs,
643 #define INPUT_DEV_ID_ATTR(name) \
644 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
646 struct input_dev *input_dev = to_input_dev(dev); \
647 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
649 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
651 INPUT_DEV_ID_ATTR(bustype);
652 INPUT_DEV_ID_ATTR(vendor);
653 INPUT_DEV_ID_ATTR(product);
654 INPUT_DEV_ID_ATTR(version);
656 static struct attribute *input_dev_id_attrs[] = {
657 &class_device_attr_bustype.attr,
658 &class_device_attr_vendor.attr,
659 &class_device_attr_product.attr,
660 &class_device_attr_version.attr,
664 static struct attribute_group input_dev_id_attr_group = {
666 .attrs = input_dev_id_attrs,
669 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
675 for (i = NBITS(max) - 1; i > 0; i--)
680 len += snprintf(buf + len, max(buf_size - len, 0),
681 "%lx%s", bitmap[i], i > 0 ? " " : "");
684 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
689 #define INPUT_DEV_CAP_ATTR(ev, bm) \
690 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
692 struct input_dev *input_dev = to_input_dev(dev); \
693 int len = input_print_bitmap(buf, PAGE_SIZE, \
694 input_dev->bm##bit, ev##_MAX, 1); \
695 return min_t(int, len, PAGE_SIZE); \
697 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
699 INPUT_DEV_CAP_ATTR(EV, ev);
700 INPUT_DEV_CAP_ATTR(KEY, key);
701 INPUT_DEV_CAP_ATTR(REL, rel);
702 INPUT_DEV_CAP_ATTR(ABS, abs);
703 INPUT_DEV_CAP_ATTR(MSC, msc);
704 INPUT_DEV_CAP_ATTR(LED, led);
705 INPUT_DEV_CAP_ATTR(SND, snd);
706 INPUT_DEV_CAP_ATTR(FF, ff);
707 INPUT_DEV_CAP_ATTR(SW, sw);
709 static struct attribute *input_dev_caps_attrs[] = {
710 &class_device_attr_ev.attr,
711 &class_device_attr_key.attr,
712 &class_device_attr_rel.attr,
713 &class_device_attr_abs.attr,
714 &class_device_attr_msc.attr,
715 &class_device_attr_led.attr,
716 &class_device_attr_snd.attr,
717 &class_device_attr_ff.attr,
718 &class_device_attr_sw.attr,
722 static struct attribute_group input_dev_caps_attr_group = {
723 .name = "capabilities",
724 .attrs = input_dev_caps_attrs,
727 static void input_dev_release(struct class_device *class_dev)
729 struct input_dev *dev = to_input_dev(class_dev);
732 module_put(THIS_MODULE);
736 * Input uevent interface - loading event handlers based on
739 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
740 char *buffer, int buffer_size, int *cur_len,
741 const char *name, unsigned long *bitmap, int max)
743 if (*cur_index >= num_envp - 1)
746 envp[*cur_index] = buffer + *cur_len;
748 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
749 if (*cur_len >= buffer_size)
752 *cur_len += input_print_bitmap(buffer + *cur_len,
753 max(buffer_size - *cur_len, 0),
755 if (*cur_len > buffer_size)
762 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
763 char *buffer, int buffer_size, int *cur_len,
764 struct input_dev *dev)
766 if (*cur_index >= num_envp - 1)
769 envp[*cur_index] = buffer + *cur_len;
771 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
773 if (*cur_len >= buffer_size)
776 *cur_len += input_print_modalias(buffer + *cur_len,
777 max(buffer_size - *cur_len, 0),
779 if (*cur_len > buffer_size)
786 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
788 int err = add_uevent_var(envp, num_envp, &i, \
789 buffer, buffer_size, &len, \
795 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
797 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
798 buffer, buffer_size, &len, \
804 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
806 int err = input_add_uevent_modalias_var(envp, \
808 buffer, buffer_size, &len, \
814 static int input_dev_uevent(struct class_device *cdev, char **envp,
815 int num_envp, char *buffer, int buffer_size)
817 struct input_dev *dev = to_input_dev(cdev);
821 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
822 dev->id.bustype, dev->id.vendor,
823 dev->id.product, dev->id.version);
825 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
827 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
829 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
831 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
832 if (test_bit(EV_KEY, dev->evbit))
833 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
834 if (test_bit(EV_REL, dev->evbit))
835 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
836 if (test_bit(EV_ABS, dev->evbit))
837 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
838 if (test_bit(EV_MSC, dev->evbit))
839 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
840 if (test_bit(EV_LED, dev->evbit))
841 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
842 if (test_bit(EV_SND, dev->evbit))
843 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
844 if (test_bit(EV_FF, dev->evbit))
845 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
846 if (test_bit(EV_SW, dev->evbit))
847 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
849 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
855 struct class input_class = {
857 .release = input_dev_release,
858 .uevent = input_dev_uevent,
860 EXPORT_SYMBOL_GPL(input_class);
862 struct input_dev *input_allocate_device(void)
864 struct input_dev *dev;
866 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
869 dev->cdev.class = &input_class;
870 class_device_initialize(&dev->cdev);
871 mutex_init(&dev->mutex);
872 INIT_LIST_HEAD(&dev->h_list);
873 INIT_LIST_HEAD(&dev->node);
878 EXPORT_SYMBOL(input_allocate_device);
880 void input_free_device(struct input_dev *dev)
884 mutex_lock(&dev->mutex);
885 dev->name = dev->phys = dev->uniq = NULL;
886 mutex_unlock(&dev->mutex);
888 input_put_device(dev);
891 EXPORT_SYMBOL(input_free_device);
893 int input_register_device(struct input_dev *dev)
895 static atomic_t input_no = ATOMIC_INIT(0);
896 struct input_handle *handle;
897 struct input_handler *handler;
898 struct input_device_id *id;
902 if (!dev->dynalloc) {
903 printk(KERN_WARNING "input: device %s is statically allocated, will not register\n"
904 "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n",
905 dev->name ? dev->name : "<Unknown>");
909 set_bit(EV_SYN, dev->evbit);
912 * If delay and period are pre-set by the driver, then autorepeating
913 * is handled by the driver itself and we don't do it in input.c.
916 init_timer(&dev->timer);
917 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
918 dev->timer.data = (long) dev;
919 dev->timer.function = input_repeat_key;
920 dev->rep[REP_DELAY] = 250;
921 dev->rep[REP_PERIOD] = 33;
924 INIT_LIST_HEAD(&dev->h_list);
925 list_add_tail(&dev->node, &input_dev_list);
927 dev->cdev.class = &input_class;
928 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
929 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
931 error = class_device_add(&dev->cdev);
935 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
939 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
943 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
947 __module_get(THIS_MODULE);
949 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
950 printk(KERN_INFO "input: %s as %s\n",
951 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
954 list_for_each_entry(handler, &input_handler_list, node)
955 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
956 if ((id = input_match_device(handler->id_table, dev)))
957 if ((handle = handler->connect(handler, dev, id)))
958 input_link_handle(handle);
960 input_wakeup_procfs_readers();
964 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
965 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
966 fail1: class_device_del(&dev->cdev);
969 EXPORT_SYMBOL(input_register_device);
971 void input_unregister_device(struct input_dev *dev)
973 struct list_head *node, *next;
978 del_timer_sync(&dev->timer);
980 list_for_each_safe(node, next, &dev->h_list) {
981 struct input_handle * handle = to_handle(node);
982 list_del_init(&handle->d_node);
983 list_del_init(&handle->h_node);
984 handle->handler->disconnect(handle);
987 list_del_init(&dev->node);
989 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
990 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
991 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
993 mutex_lock(&dev->mutex);
994 dev->name = dev->phys = dev->uniq = NULL;
995 mutex_unlock(&dev->mutex);
997 class_device_unregister(&dev->cdev);
999 input_wakeup_procfs_readers();
1001 EXPORT_SYMBOL(input_unregister_device);
1003 void input_register_handler(struct input_handler *handler)
1005 struct input_dev *dev;
1006 struct input_handle *handle;
1007 struct input_device_id *id;
1012 INIT_LIST_HEAD(&handler->h_list);
1014 if (handler->fops != NULL)
1015 input_table[handler->minor >> 5] = handler;
1017 list_add_tail(&handler->node, &input_handler_list);
1019 list_for_each_entry(dev, &input_dev_list, node)
1020 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1021 if ((id = input_match_device(handler->id_table, dev)))
1022 if ((handle = handler->connect(handler, dev, id)))
1023 input_link_handle(handle);
1025 input_wakeup_procfs_readers();
1027 EXPORT_SYMBOL(input_register_handler);
1029 void input_unregister_handler(struct input_handler *handler)
1031 struct list_head *node, *next;
1033 list_for_each_safe(node, next, &handler->h_list) {
1034 struct input_handle * handle = to_handle_h(node);
1035 list_del_init(&handle->h_node);
1036 list_del_init(&handle->d_node);
1037 handler->disconnect(handle);
1040 list_del_init(&handler->node);
1042 if (handler->fops != NULL)
1043 input_table[handler->minor >> 5] = NULL;
1045 input_wakeup_procfs_readers();
1047 EXPORT_SYMBOL(input_unregister_handler);
1049 static int input_open_file(struct inode *inode, struct file *file)
1051 struct input_handler *handler = input_table[iminor(inode) >> 5];
1052 const struct file_operations *old_fops, *new_fops = NULL;
1055 /* No load-on-demand here? */
1056 if (!handler || !(new_fops = fops_get(handler->fops)))
1060 * That's _really_ odd. Usually NULL ->open means "nothing special",
1061 * not "no device". Oh, well...
1063 if (!new_fops->open) {
1067 old_fops = file->f_op;
1068 file->f_op = new_fops;
1070 err = new_fops->open(inode, file);
1073 fops_put(file->f_op);
1074 file->f_op = fops_get(old_fops);
1080 static struct file_operations input_fops = {
1081 .owner = THIS_MODULE,
1082 .open = input_open_file,
1085 static int __init input_init(void)
1089 err = class_register(&input_class);
1091 printk(KERN_ERR "input: unable to register input_dev class\n");
1095 err = input_proc_init();
1099 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1101 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1107 fail2: input_proc_exit();
1108 fail1: class_unregister(&input_class);
1112 static void __exit input_exit(void)
1115 unregister_chrdev(INPUT_MAJOR, "input");
1116 class_unregister(&input_class);
1119 subsys_initcall(input_init);
1120 module_exit(input_exit);