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];
39 * input_event() - report new input event
40 * @handle: device that generated the event
41 * @type: type of the event
43 * @value: value of the event
45 * This function should be used by drivers implementing various input devices
46 * See also input_inject_event()
48 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
50 struct input_handle *handle;
52 if (type > EV_MAX || !test_bit(type, dev->evbit))
55 add_input_randomness(type, code, value);
63 dev->event(dev, type, code, value);
76 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
82 change_bit(code, dev->key);
84 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
85 dev->repeat_key = code;
86 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
93 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
96 change_bit(code, dev->sw);
102 if (code > ABS_MAX || !test_bit(code, dev->absbit))
105 if (dev->absfuzz[code]) {
106 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
107 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
110 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
111 (value < dev->abs[code] + dev->absfuzz[code]))
112 value = (dev->abs[code] * 3 + value) >> 2;
114 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
115 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
116 value = (dev->abs[code] + value) >> 1;
119 if (dev->abs[code] == value)
122 dev->abs[code] = value;
127 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
134 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
138 dev->event(dev, type, code, value);
144 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
147 change_bit(code, dev->led);
150 dev->event(dev, type, code, value);
156 if (code > SND_MAX || !test_bit(code, dev->sndbit))
159 if (!!test_bit(code, dev->snd) != !!value)
160 change_bit(code, dev->snd);
163 dev->event(dev, type, code, value);
169 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
172 dev->rep[code] = value;
174 dev->event(dev, type, code, value);
184 dev->event(dev, type, code, value);
192 dev->grab->handler->event(dev->grab, type, code, value);
194 list_for_each_entry(handle, &dev->h_list, d_node)
196 handle->handler->event(handle, type, code, value);
198 EXPORT_SYMBOL(input_event);
201 * input_inject_event() - send input event from input handler
202 * @handle: input handle to send event through
203 * @type: type of the event
205 * @value: value of the event
207 * Similar to input_event() but will ignore event if device is "grabbed" and handle
208 * injecting event is not the one that owns the device.
210 void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
212 if (!handle->dev->grab || handle->dev->grab == handle)
213 input_event(handle->dev, type, code, value);
215 EXPORT_SYMBOL(input_inject_event);
217 static void input_repeat_key(unsigned long data)
219 struct input_dev *dev = (void *) data;
221 if (!test_bit(dev->repeat_key, dev->key))
224 input_event(dev, EV_KEY, dev->repeat_key, 2);
227 if (dev->rep[REP_PERIOD])
228 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
231 int input_grab_device(struct input_handle *handle)
233 if (handle->dev->grab)
236 handle->dev->grab = handle;
239 EXPORT_SYMBOL(input_grab_device);
241 void input_release_device(struct input_handle *handle)
243 struct input_dev *dev = handle->dev;
245 if (dev->grab == handle) {
248 list_for_each_entry(handle, &dev->h_list, d_node)
249 if (handle->handler->start)
250 handle->handler->start(handle);
253 EXPORT_SYMBOL(input_release_device);
255 int input_open_device(struct input_handle *handle)
257 struct input_dev *dev = handle->dev;
260 err = mutex_lock_interruptible(&dev->mutex);
266 if (!dev->users++ && dev->open)
267 err = dev->open(dev);
272 mutex_unlock(&dev->mutex);
276 EXPORT_SYMBOL(input_open_device);
278 int input_flush_device(struct input_handle* handle, struct file* file)
280 if (handle->dev->flush)
281 return handle->dev->flush(handle->dev, file);
285 EXPORT_SYMBOL(input_flush_device);
287 void input_close_device(struct input_handle *handle)
289 struct input_dev *dev = handle->dev;
291 input_release_device(handle);
293 mutex_lock(&dev->mutex);
295 if (!--dev->users && dev->close)
299 mutex_unlock(&dev->mutex);
301 EXPORT_SYMBOL(input_close_device);
303 static void input_link_handle(struct input_handle *handle)
305 list_add_tail(&handle->d_node, &handle->dev->h_list);
306 list_add_tail(&handle->h_node, &handle->handler->h_list);
309 #define MATCH_BIT(bit, max) \
310 for (i = 0; i < NBITS(max); i++) \
311 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
313 if (i != NBITS(max)) \
316 static const struct input_device_id *input_match_device(const struct input_device_id *id,
317 struct input_dev *dev)
321 for (; id->flags || id->driver_info; id++) {
323 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
324 if (id->bustype != dev->id.bustype)
327 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
328 if (id->vendor != dev->id.vendor)
331 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
332 if (id->product != dev->id.product)
335 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
336 if (id->version != dev->id.version)
339 MATCH_BIT(evbit, EV_MAX);
340 MATCH_BIT(keybit, KEY_MAX);
341 MATCH_BIT(relbit, REL_MAX);
342 MATCH_BIT(absbit, ABS_MAX);
343 MATCH_BIT(mscbit, MSC_MAX);
344 MATCH_BIT(ledbit, LED_MAX);
345 MATCH_BIT(sndbit, SND_MAX);
346 MATCH_BIT(ffbit, FF_MAX);
347 MATCH_BIT(swbit, SW_MAX);
355 #ifdef CONFIG_PROC_FS
357 static struct proc_dir_entry *proc_bus_input_dir;
358 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
359 static int input_devices_state;
361 static inline void input_wakeup_procfs_readers(void)
363 input_devices_state++;
364 wake_up(&input_devices_poll_wait);
367 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
369 int state = input_devices_state;
371 poll_wait(file, &input_devices_poll_wait, wait);
372 if (state != input_devices_state)
373 return POLLIN | POLLRDNORM;
378 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
380 struct list_head *node;
383 list_for_each(node, list)
390 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
392 if (element->next == list)
396 return element->next;
399 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
401 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
403 return list_get_nth_element(&input_dev_list, pos);
406 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
408 return list_get_next_element(&input_dev_list, v, pos);
411 static void input_devices_seq_stop(struct seq_file *seq, void *v)
413 /* release lock here */
416 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
417 unsigned long *bitmap, int max)
421 for (i = NBITS(max) - 1; i > 0; i--)
425 seq_printf(seq, "B: %s=", name);
427 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
431 static int input_devices_seq_show(struct seq_file *seq, void *v)
433 struct input_dev *dev = container_of(v, struct input_dev, node);
434 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
435 struct input_handle *handle;
437 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
438 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
440 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
441 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
442 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
443 seq_printf(seq, "H: Handlers=");
445 list_for_each_entry(handle, &dev->h_list, d_node)
446 seq_printf(seq, "%s ", handle->name);
449 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
450 if (test_bit(EV_KEY, dev->evbit))
451 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
452 if (test_bit(EV_REL, dev->evbit))
453 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
454 if (test_bit(EV_ABS, dev->evbit))
455 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
456 if (test_bit(EV_MSC, dev->evbit))
457 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
458 if (test_bit(EV_LED, dev->evbit))
459 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
460 if (test_bit(EV_SND, dev->evbit))
461 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
462 if (test_bit(EV_FF, dev->evbit))
463 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
464 if (test_bit(EV_SW, dev->evbit))
465 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
473 static struct seq_operations input_devices_seq_ops = {
474 .start = input_devices_seq_start,
475 .next = input_devices_seq_next,
476 .stop = input_devices_seq_stop,
477 .show = input_devices_seq_show,
480 static int input_proc_devices_open(struct inode *inode, struct file *file)
482 return seq_open(file, &input_devices_seq_ops);
485 static struct file_operations input_devices_fileops = {
486 .owner = THIS_MODULE,
487 .open = input_proc_devices_open,
488 .poll = input_proc_devices_poll,
491 .release = seq_release,
494 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
496 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
497 seq->private = (void *)(unsigned long)*pos;
498 return list_get_nth_element(&input_handler_list, pos);
501 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
503 seq->private = (void *)(unsigned long)(*pos + 1);
504 return list_get_next_element(&input_handler_list, v, pos);
507 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
509 /* release lock here */
512 static int input_handlers_seq_show(struct seq_file *seq, void *v)
514 struct input_handler *handler = container_of(v, struct input_handler, node);
516 seq_printf(seq, "N: Number=%ld Name=%s",
517 (unsigned long)seq->private, handler->name);
519 seq_printf(seq, " Minor=%d", handler->minor);
524 static struct seq_operations input_handlers_seq_ops = {
525 .start = input_handlers_seq_start,
526 .next = input_handlers_seq_next,
527 .stop = input_handlers_seq_stop,
528 .show = input_handlers_seq_show,
531 static int input_proc_handlers_open(struct inode *inode, struct file *file)
533 return seq_open(file, &input_handlers_seq_ops);
536 static struct file_operations input_handlers_fileops = {
537 .owner = THIS_MODULE,
538 .open = input_proc_handlers_open,
541 .release = seq_release,
544 static int __init input_proc_init(void)
546 struct proc_dir_entry *entry;
548 proc_bus_input_dir = proc_mkdir("input", proc_bus);
549 if (!proc_bus_input_dir)
552 proc_bus_input_dir->owner = THIS_MODULE;
554 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
558 entry->owner = THIS_MODULE;
559 entry->proc_fops = &input_devices_fileops;
561 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
565 entry->owner = THIS_MODULE;
566 entry->proc_fops = &input_handlers_fileops;
570 fail2: remove_proc_entry("devices", proc_bus_input_dir);
571 fail1: remove_proc_entry("input", proc_bus);
575 static void input_proc_exit(void)
577 remove_proc_entry("devices", proc_bus_input_dir);
578 remove_proc_entry("handlers", proc_bus_input_dir);
579 remove_proc_entry("input", proc_bus);
582 #else /* !CONFIG_PROC_FS */
583 static inline void input_wakeup_procfs_readers(void) { }
584 static inline int input_proc_init(void) { return 0; }
585 static inline void input_proc_exit(void) { }
588 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
589 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
591 struct input_dev *input_dev = to_input_dev(dev); \
594 retval = mutex_lock_interruptible(&input_dev->mutex); \
598 retval = scnprintf(buf, PAGE_SIZE, \
599 "%s\n", input_dev->name ? input_dev->name : ""); \
601 mutex_unlock(&input_dev->mutex); \
605 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
607 INPUT_DEV_STRING_ATTR_SHOW(name);
608 INPUT_DEV_STRING_ATTR_SHOW(phys);
609 INPUT_DEV_STRING_ATTR_SHOW(uniq);
611 static int input_print_modalias_bits(char *buf, int size,
612 char name, unsigned long *bm,
613 unsigned int min_bit, unsigned int max_bit)
617 len += snprintf(buf, max(size, 0), "%c", name);
618 for (i = min_bit; i < max_bit; i++)
619 if (bm[LONG(i)] & BIT(i))
620 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
624 static int input_print_modalias(char *buf, int size, struct input_dev *id,
629 len = snprintf(buf, max(size, 0),
630 "input:b%04Xv%04Xp%04Xe%04X-",
631 id->id.bustype, id->id.vendor,
632 id->id.product, id->id.version);
634 len += input_print_modalias_bits(buf + len, size - len,
635 'e', id->evbit, 0, EV_MAX);
636 len += input_print_modalias_bits(buf + len, size - len,
637 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
638 len += input_print_modalias_bits(buf + len, size - len,
639 'r', id->relbit, 0, REL_MAX);
640 len += input_print_modalias_bits(buf + len, size - len,
641 'a', id->absbit, 0, ABS_MAX);
642 len += input_print_modalias_bits(buf + len, size - len,
643 'm', id->mscbit, 0, MSC_MAX);
644 len += input_print_modalias_bits(buf + len, size - len,
645 'l', id->ledbit, 0, LED_MAX);
646 len += input_print_modalias_bits(buf + len, size - len,
647 's', id->sndbit, 0, SND_MAX);
648 len += input_print_modalias_bits(buf + len, size - len,
649 'f', id->ffbit, 0, FF_MAX);
650 len += input_print_modalias_bits(buf + len, size - len,
651 'w', id->swbit, 0, SW_MAX);
654 len += snprintf(buf + len, max(size - len, 0), "\n");
659 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
661 struct input_dev *id = to_input_dev(dev);
664 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
666 return min_t(int, len, PAGE_SIZE);
668 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
670 static struct attribute *input_dev_attrs[] = {
671 &class_device_attr_name.attr,
672 &class_device_attr_phys.attr,
673 &class_device_attr_uniq.attr,
674 &class_device_attr_modalias.attr,
678 static struct attribute_group input_dev_attr_group = {
679 .attrs = input_dev_attrs,
682 #define INPUT_DEV_ID_ATTR(name) \
683 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
685 struct input_dev *input_dev = to_input_dev(dev); \
686 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
688 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
690 INPUT_DEV_ID_ATTR(bustype);
691 INPUT_DEV_ID_ATTR(vendor);
692 INPUT_DEV_ID_ATTR(product);
693 INPUT_DEV_ID_ATTR(version);
695 static struct attribute *input_dev_id_attrs[] = {
696 &class_device_attr_bustype.attr,
697 &class_device_attr_vendor.attr,
698 &class_device_attr_product.attr,
699 &class_device_attr_version.attr,
703 static struct attribute_group input_dev_id_attr_group = {
705 .attrs = input_dev_id_attrs,
708 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
714 for (i = NBITS(max) - 1; i > 0; i--)
719 len += snprintf(buf + len, max(buf_size - len, 0),
720 "%lx%s", bitmap[i], i > 0 ? " " : "");
723 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
728 #define INPUT_DEV_CAP_ATTR(ev, bm) \
729 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
731 struct input_dev *input_dev = to_input_dev(dev); \
732 int len = input_print_bitmap(buf, PAGE_SIZE, \
733 input_dev->bm##bit, ev##_MAX, 1); \
734 return min_t(int, len, PAGE_SIZE); \
736 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
738 INPUT_DEV_CAP_ATTR(EV, ev);
739 INPUT_DEV_CAP_ATTR(KEY, key);
740 INPUT_DEV_CAP_ATTR(REL, rel);
741 INPUT_DEV_CAP_ATTR(ABS, abs);
742 INPUT_DEV_CAP_ATTR(MSC, msc);
743 INPUT_DEV_CAP_ATTR(LED, led);
744 INPUT_DEV_CAP_ATTR(SND, snd);
745 INPUT_DEV_CAP_ATTR(FF, ff);
746 INPUT_DEV_CAP_ATTR(SW, sw);
748 static struct attribute *input_dev_caps_attrs[] = {
749 &class_device_attr_ev.attr,
750 &class_device_attr_key.attr,
751 &class_device_attr_rel.attr,
752 &class_device_attr_abs.attr,
753 &class_device_attr_msc.attr,
754 &class_device_attr_led.attr,
755 &class_device_attr_snd.attr,
756 &class_device_attr_ff.attr,
757 &class_device_attr_sw.attr,
761 static struct attribute_group input_dev_caps_attr_group = {
762 .name = "capabilities",
763 .attrs = input_dev_caps_attrs,
766 static void input_dev_release(struct class_device *class_dev)
768 struct input_dev *dev = to_input_dev(class_dev);
770 input_ff_destroy(dev);
773 module_put(THIS_MODULE);
777 * Input uevent interface - loading event handlers based on
780 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
781 char *buffer, int buffer_size, int *cur_len,
782 const char *name, unsigned long *bitmap, int max)
784 if (*cur_index >= num_envp - 1)
787 envp[*cur_index] = buffer + *cur_len;
789 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
790 if (*cur_len >= buffer_size)
793 *cur_len += input_print_bitmap(buffer + *cur_len,
794 max(buffer_size - *cur_len, 0),
796 if (*cur_len > buffer_size)
803 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
804 char *buffer, int buffer_size, int *cur_len,
805 struct input_dev *dev)
807 if (*cur_index >= num_envp - 1)
810 envp[*cur_index] = buffer + *cur_len;
812 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
814 if (*cur_len >= buffer_size)
817 *cur_len += input_print_modalias(buffer + *cur_len,
818 max(buffer_size - *cur_len, 0),
820 if (*cur_len > buffer_size)
827 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
829 int err = add_uevent_var(envp, num_envp, &i, \
830 buffer, buffer_size, &len, \
836 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
838 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
839 buffer, buffer_size, &len, \
845 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
847 int err = input_add_uevent_modalias_var(envp, \
849 buffer, buffer_size, &len, \
855 static int input_dev_uevent(struct class_device *cdev, char **envp,
856 int num_envp, char *buffer, int buffer_size)
858 struct input_dev *dev = to_input_dev(cdev);
862 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
863 dev->id.bustype, dev->id.vendor,
864 dev->id.product, dev->id.version);
866 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
868 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
870 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
872 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
873 if (test_bit(EV_KEY, dev->evbit))
874 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
875 if (test_bit(EV_REL, dev->evbit))
876 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
877 if (test_bit(EV_ABS, dev->evbit))
878 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
879 if (test_bit(EV_MSC, dev->evbit))
880 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
881 if (test_bit(EV_LED, dev->evbit))
882 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
883 if (test_bit(EV_SND, dev->evbit))
884 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
885 if (test_bit(EV_FF, dev->evbit))
886 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
887 if (test_bit(EV_SW, dev->evbit))
888 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
890 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
896 struct class input_class = {
898 .release = input_dev_release,
899 .uevent = input_dev_uevent,
901 EXPORT_SYMBOL_GPL(input_class);
903 struct input_dev *input_allocate_device(void)
905 struct input_dev *dev;
907 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
909 dev->cdev.class = &input_class;
910 class_device_initialize(&dev->cdev);
911 mutex_init(&dev->mutex);
912 INIT_LIST_HEAD(&dev->h_list);
913 INIT_LIST_HEAD(&dev->node);
915 __module_get(THIS_MODULE);
920 EXPORT_SYMBOL(input_allocate_device);
922 void input_free_device(struct input_dev *dev)
926 mutex_lock(&dev->mutex);
927 dev->name = dev->phys = dev->uniq = NULL;
928 mutex_unlock(&dev->mutex);
930 input_put_device(dev);
933 EXPORT_SYMBOL(input_free_device);
935 int input_register_device(struct input_dev *dev)
937 static atomic_t input_no = ATOMIC_INIT(0);
938 struct input_handle *handle;
939 struct input_handler *handler;
940 const struct input_device_id *id;
944 set_bit(EV_SYN, dev->evbit);
947 * If delay and period are pre-set by the driver, then autorepeating
948 * is handled by the driver itself and we don't do it in input.c.
951 init_timer(&dev->timer);
952 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
953 dev->timer.data = (long) dev;
954 dev->timer.function = input_repeat_key;
955 dev->rep[REP_DELAY] = 250;
956 dev->rep[REP_PERIOD] = 33;
959 list_add_tail(&dev->node, &input_dev_list);
961 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
962 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
964 error = class_device_add(&dev->cdev);
968 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group);
972 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group);
976 error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
980 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
981 printk(KERN_INFO "input: %s as %s\n",
982 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
985 list_for_each_entry(handler, &input_handler_list, node)
986 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
987 if ((id = input_match_device(handler->id_table, dev)))
988 if ((handle = handler->connect(handler, dev, id))) {
989 input_link_handle(handle);
991 handler->start(handle);
994 input_wakeup_procfs_readers();
998 fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
999 fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
1000 fail1: class_device_del(&dev->cdev);
1003 EXPORT_SYMBOL(input_register_device);
1005 void input_unregister_device(struct input_dev *dev)
1007 struct list_head *node, *next;
1010 for (code = 0; code <= KEY_MAX; code++)
1011 if (test_bit(code, dev->key))
1012 input_report_key(dev, code, 0);
1015 del_timer_sync(&dev->timer);
1017 list_for_each_safe(node, next, &dev->h_list) {
1018 struct input_handle * handle = to_handle(node);
1019 list_del_init(&handle->d_node);
1020 list_del_init(&handle->h_node);
1021 handle->handler->disconnect(handle);
1024 list_del_init(&dev->node);
1026 sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group);
1027 sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group);
1028 sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
1030 mutex_lock(&dev->mutex);
1031 dev->name = dev->phys = dev->uniq = NULL;
1032 mutex_unlock(&dev->mutex);
1034 class_device_unregister(&dev->cdev);
1036 input_wakeup_procfs_readers();
1038 EXPORT_SYMBOL(input_unregister_device);
1040 int input_register_handler(struct input_handler *handler)
1042 struct input_dev *dev;
1043 struct input_handle *handle;
1044 const struct input_device_id *id;
1046 INIT_LIST_HEAD(&handler->h_list);
1048 if (handler->fops != NULL) {
1049 if (input_table[handler->minor >> 5])
1052 input_table[handler->minor >> 5] = handler;
1055 list_add_tail(&handler->node, &input_handler_list);
1057 list_for_each_entry(dev, &input_dev_list, node)
1058 if (!handler->blacklist || !input_match_device(handler->blacklist, dev))
1059 if ((id = input_match_device(handler->id_table, dev)))
1060 if ((handle = handler->connect(handler, dev, id))) {
1061 input_link_handle(handle);
1063 handler->start(handle);
1066 input_wakeup_procfs_readers();
1069 EXPORT_SYMBOL(input_register_handler);
1071 void input_unregister_handler(struct input_handler *handler)
1073 struct list_head *node, *next;
1075 list_for_each_safe(node, next, &handler->h_list) {
1076 struct input_handle * handle = to_handle_h(node);
1077 list_del_init(&handle->h_node);
1078 list_del_init(&handle->d_node);
1079 handler->disconnect(handle);
1082 list_del_init(&handler->node);
1084 if (handler->fops != NULL)
1085 input_table[handler->minor >> 5] = NULL;
1087 input_wakeup_procfs_readers();
1089 EXPORT_SYMBOL(input_unregister_handler);
1091 static int input_open_file(struct inode *inode, struct file *file)
1093 struct input_handler *handler = input_table[iminor(inode) >> 5];
1094 const struct file_operations *old_fops, *new_fops = NULL;
1097 /* No load-on-demand here? */
1098 if (!handler || !(new_fops = fops_get(handler->fops)))
1102 * That's _really_ odd. Usually NULL ->open means "nothing special",
1103 * not "no device". Oh, well...
1105 if (!new_fops->open) {
1109 old_fops = file->f_op;
1110 file->f_op = new_fops;
1112 err = new_fops->open(inode, file);
1115 fops_put(file->f_op);
1116 file->f_op = fops_get(old_fops);
1122 static struct file_operations input_fops = {
1123 .owner = THIS_MODULE,
1124 .open = input_open_file,
1127 static int __init input_init(void)
1131 err = class_register(&input_class);
1133 printk(KERN_ERR "input: unable to register input_dev class\n");
1137 err = input_proc_init();
1141 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1143 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1149 fail2: input_proc_exit();
1150 fail1: class_unregister(&input_class);
1154 static void __exit input_exit(void)
1157 unregister_chrdev(INPUT_MAJOR, "input");
1158 class_unregister(&input_class);
1161 subsys_initcall(input_init);
1162 module_exit(input_exit);