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/input.h>
15 #include <linux/module.h>
16 #include <linux/random.h>
17 #include <linux/major.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/interrupt.h>
21 #include <linux/poll.h>
22 #include <linux/device.h>
23 #include <linux/mutex.h>
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26 MODULE_DESCRIPTION("Input core");
27 MODULE_LICENSE("GPL");
29 #define INPUT_DEVICES 256
31 static LIST_HEAD(input_dev_list);
32 static LIST_HEAD(input_handler_list);
34 static struct input_handler *input_table[8];
37 * input_event() - report new input event
38 * @dev: device that generated the event
39 * @type: type of the event
41 * @value: value of the event
43 * This function should be used by drivers implementing various input devices
44 * See also input_inject_event()
46 void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
48 struct input_handle *handle;
50 if (type > EV_MAX || !test_bit(type, dev->evbit))
53 add_input_randomness(type, code, value);
61 dev->event(dev, type, code, value);
74 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
80 change_bit(code, dev->key);
82 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
83 dev->repeat_key = code;
84 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
91 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
94 change_bit(code, dev->sw);
100 if (code > ABS_MAX || !test_bit(code, dev->absbit))
103 if (dev->absfuzz[code]) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
108 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
109 (value < dev->abs[code] + dev->absfuzz[code]))
110 value = (dev->abs[code] * 3 + value) >> 2;
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
114 value = (dev->abs[code] + value) >> 1;
117 if (dev->abs[code] == value)
120 dev->abs[code] = value;
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
136 dev->event(dev, type, code, value);
142 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
145 change_bit(code, dev->led);
148 dev->event(dev, type, code, value);
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
157 if (!!test_bit(code, dev->snd) != !!value)
158 change_bit(code, dev->snd);
161 dev->event(dev, type, code, value);
167 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
170 dev->rep[code] = value;
172 dev->event(dev, type, code, value);
182 dev->event(dev, type, code, value);
190 dev->grab->handler->event(dev->grab, type, code, value);
192 list_for_each_entry(handle, &dev->h_list, d_node)
194 handle->handler->event(handle, type, code, value);
196 EXPORT_SYMBOL(input_event);
199 * input_inject_event() - send input event from input handler
200 * @handle: input handle to send event through
201 * @type: type of the event
203 * @value: value of the event
205 * Similar to input_event() but will ignore event if device is "grabbed" and handle
206 * injecting event is not the one that owns the device.
208 void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
210 if (!handle->dev->grab || handle->dev->grab == handle)
211 input_event(handle->dev, type, code, value);
213 EXPORT_SYMBOL(input_inject_event);
215 static void input_repeat_key(unsigned long data)
217 struct input_dev *dev = (void *) data;
219 if (!test_bit(dev->repeat_key, dev->key))
222 input_event(dev, EV_KEY, dev->repeat_key, 2);
225 if (dev->rep[REP_PERIOD])
226 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
229 int input_grab_device(struct input_handle *handle)
231 if (handle->dev->grab)
234 handle->dev->grab = handle;
237 EXPORT_SYMBOL(input_grab_device);
239 void input_release_device(struct input_handle *handle)
241 struct input_dev *dev = handle->dev;
243 if (dev->grab == handle) {
246 list_for_each_entry(handle, &dev->h_list, d_node)
247 if (handle->handler->start)
248 handle->handler->start(handle);
251 EXPORT_SYMBOL(input_release_device);
253 int input_open_device(struct input_handle *handle)
255 struct input_dev *dev = handle->dev;
258 err = mutex_lock_interruptible(&dev->mutex);
264 if (!dev->users++ && dev->open)
265 err = dev->open(dev);
270 mutex_unlock(&dev->mutex);
274 EXPORT_SYMBOL(input_open_device);
276 int input_flush_device(struct input_handle* handle, struct file* file)
278 if (handle->dev->flush)
279 return handle->dev->flush(handle->dev, file);
283 EXPORT_SYMBOL(input_flush_device);
285 void input_close_device(struct input_handle *handle)
287 struct input_dev *dev = handle->dev;
289 input_release_device(handle);
291 mutex_lock(&dev->mutex);
293 if (!--dev->users && dev->close)
297 mutex_unlock(&dev->mutex);
299 EXPORT_SYMBOL(input_close_device);
301 static int input_fetch_keycode(struct input_dev *dev, int scancode)
303 switch (dev->keycodesize) {
305 return ((u8 *)dev->keycode)[scancode];
308 return ((u16 *)dev->keycode)[scancode];
311 return ((u32 *)dev->keycode)[scancode];
315 static int input_default_getkeycode(struct input_dev *dev,
316 int scancode, int *keycode)
318 if (!dev->keycodesize)
321 if (scancode < 0 || scancode >= dev->keycodemax)
324 *keycode = input_fetch_keycode(dev, scancode);
329 static int input_default_setkeycode(struct input_dev *dev,
330 int scancode, int keycode)
335 if (scancode < 0 || scancode >= dev->keycodemax)
338 if (keycode < 0 || keycode > KEY_MAX)
341 if (!dev->keycodesize)
344 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
347 switch (dev->keycodesize) {
349 u8 *k = (u8 *)dev->keycode;
350 old_keycode = k[scancode];
351 k[scancode] = keycode;
355 u16 *k = (u16 *)dev->keycode;
356 old_keycode = k[scancode];
357 k[scancode] = keycode;
361 u32 *k = (u32 *)dev->keycode;
362 old_keycode = k[scancode];
363 k[scancode] = keycode;
368 clear_bit(old_keycode, dev->keybit);
369 set_bit(keycode, dev->keybit);
371 for (i = 0; i < dev->keycodemax; i++) {
372 if (input_fetch_keycode(dev, i) == old_keycode) {
373 set_bit(old_keycode, dev->keybit);
374 break; /* Setting the bit twice is useless, so break */
382 #define MATCH_BIT(bit, max) \
383 for (i = 0; i < NBITS(max); i++) \
384 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
386 if (i != NBITS(max)) \
389 static const struct input_device_id *input_match_device(const struct input_device_id *id,
390 struct input_dev *dev)
394 for (; id->flags || id->driver_info; id++) {
396 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
397 if (id->bustype != dev->id.bustype)
400 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
401 if (id->vendor != dev->id.vendor)
404 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
405 if (id->product != dev->id.product)
408 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
409 if (id->version != dev->id.version)
412 MATCH_BIT(evbit, EV_MAX);
413 MATCH_BIT(keybit, KEY_MAX);
414 MATCH_BIT(relbit, REL_MAX);
415 MATCH_BIT(absbit, ABS_MAX);
416 MATCH_BIT(mscbit, MSC_MAX);
417 MATCH_BIT(ledbit, LED_MAX);
418 MATCH_BIT(sndbit, SND_MAX);
419 MATCH_BIT(ffbit, FF_MAX);
420 MATCH_BIT(swbit, SW_MAX);
428 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
430 const struct input_device_id *id;
433 if (handler->blacklist && input_match_device(handler->blacklist, dev))
436 id = input_match_device(handler->id_table, dev);
440 error = handler->connect(handler, dev, id);
441 if (error && error != -ENODEV)
443 "input: failed to attach handler %s to device %s, "
445 handler->name, kobject_name(&dev->cdev.kobj), error);
451 #ifdef CONFIG_PROC_FS
453 static struct proc_dir_entry *proc_bus_input_dir;
454 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
455 static int input_devices_state;
457 static inline void input_wakeup_procfs_readers(void)
459 input_devices_state++;
460 wake_up(&input_devices_poll_wait);
463 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
465 int state = input_devices_state;
467 poll_wait(file, &input_devices_poll_wait, wait);
468 if (state != input_devices_state)
469 return POLLIN | POLLRDNORM;
474 static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
476 struct list_head *node;
479 list_for_each(node, list)
486 static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
488 if (element->next == list)
492 return element->next;
495 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
497 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
499 return list_get_nth_element(&input_dev_list, pos);
502 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
504 return list_get_next_element(&input_dev_list, v, pos);
507 static void input_devices_seq_stop(struct seq_file *seq, void *v)
509 /* release lock here */
512 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
513 unsigned long *bitmap, int max)
517 for (i = NBITS(max) - 1; i > 0; i--)
521 seq_printf(seq, "B: %s=", name);
523 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
527 static int input_devices_seq_show(struct seq_file *seq, void *v)
529 struct input_dev *dev = container_of(v, struct input_dev, node);
530 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
531 struct input_handle *handle;
533 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
534 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
536 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
537 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
538 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
539 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
540 seq_printf(seq, "H: Handlers=");
542 list_for_each_entry(handle, &dev->h_list, d_node)
543 seq_printf(seq, "%s ", handle->name);
546 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
547 if (test_bit(EV_KEY, dev->evbit))
548 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
549 if (test_bit(EV_REL, dev->evbit))
550 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
551 if (test_bit(EV_ABS, dev->evbit))
552 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
553 if (test_bit(EV_MSC, dev->evbit))
554 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
555 if (test_bit(EV_LED, dev->evbit))
556 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
557 if (test_bit(EV_SND, dev->evbit))
558 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
559 if (test_bit(EV_FF, dev->evbit))
560 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
561 if (test_bit(EV_SW, dev->evbit))
562 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
570 static struct seq_operations input_devices_seq_ops = {
571 .start = input_devices_seq_start,
572 .next = input_devices_seq_next,
573 .stop = input_devices_seq_stop,
574 .show = input_devices_seq_show,
577 static int input_proc_devices_open(struct inode *inode, struct file *file)
579 return seq_open(file, &input_devices_seq_ops);
582 static const struct file_operations input_devices_fileops = {
583 .owner = THIS_MODULE,
584 .open = input_proc_devices_open,
585 .poll = input_proc_devices_poll,
588 .release = seq_release,
591 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
593 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
594 seq->private = (void *)(unsigned long)*pos;
595 return list_get_nth_element(&input_handler_list, pos);
598 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
600 seq->private = (void *)(unsigned long)(*pos + 1);
601 return list_get_next_element(&input_handler_list, v, pos);
604 static void input_handlers_seq_stop(struct seq_file *seq, void *v)
606 /* release lock here */
609 static int input_handlers_seq_show(struct seq_file *seq, void *v)
611 struct input_handler *handler = container_of(v, struct input_handler, node);
613 seq_printf(seq, "N: Number=%ld Name=%s",
614 (unsigned long)seq->private, handler->name);
616 seq_printf(seq, " Minor=%d", handler->minor);
621 static struct seq_operations input_handlers_seq_ops = {
622 .start = input_handlers_seq_start,
623 .next = input_handlers_seq_next,
624 .stop = input_handlers_seq_stop,
625 .show = input_handlers_seq_show,
628 static int input_proc_handlers_open(struct inode *inode, struct file *file)
630 return seq_open(file, &input_handlers_seq_ops);
633 static const struct file_operations input_handlers_fileops = {
634 .owner = THIS_MODULE,
635 .open = input_proc_handlers_open,
638 .release = seq_release,
641 static int __init input_proc_init(void)
643 struct proc_dir_entry *entry;
645 proc_bus_input_dir = proc_mkdir("input", proc_bus);
646 if (!proc_bus_input_dir)
649 proc_bus_input_dir->owner = THIS_MODULE;
651 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
655 entry->owner = THIS_MODULE;
656 entry->proc_fops = &input_devices_fileops;
658 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
662 entry->owner = THIS_MODULE;
663 entry->proc_fops = &input_handlers_fileops;
667 fail2: remove_proc_entry("devices", proc_bus_input_dir);
668 fail1: remove_proc_entry("input", proc_bus);
672 static void input_proc_exit(void)
674 remove_proc_entry("devices", proc_bus_input_dir);
675 remove_proc_entry("handlers", proc_bus_input_dir);
676 remove_proc_entry("input", proc_bus);
679 #else /* !CONFIG_PROC_FS */
680 static inline void input_wakeup_procfs_readers(void) { }
681 static inline int input_proc_init(void) { return 0; }
682 static inline void input_proc_exit(void) { }
685 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
686 static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
688 struct input_dev *input_dev = to_input_dev(dev); \
690 return scnprintf(buf, PAGE_SIZE, "%s\n", \
691 input_dev->name ? input_dev->name : ""); \
693 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
695 INPUT_DEV_STRING_ATTR_SHOW(name);
696 INPUT_DEV_STRING_ATTR_SHOW(phys);
697 INPUT_DEV_STRING_ATTR_SHOW(uniq);
699 static int input_print_modalias_bits(char *buf, int size,
700 char name, unsigned long *bm,
701 unsigned int min_bit, unsigned int max_bit)
705 len += snprintf(buf, max(size, 0), "%c", name);
706 for (i = min_bit; i < max_bit; i++)
707 if (bm[LONG(i)] & BIT(i))
708 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
712 static int input_print_modalias(char *buf, int size, struct input_dev *id,
717 len = snprintf(buf, max(size, 0),
718 "input:b%04Xv%04Xp%04Xe%04X-",
719 id->id.bustype, id->id.vendor,
720 id->id.product, id->id.version);
722 len += input_print_modalias_bits(buf + len, size - len,
723 'e', id->evbit, 0, EV_MAX);
724 len += input_print_modalias_bits(buf + len, size - len,
725 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
726 len += input_print_modalias_bits(buf + len, size - len,
727 'r', id->relbit, 0, REL_MAX);
728 len += input_print_modalias_bits(buf + len, size - len,
729 'a', id->absbit, 0, ABS_MAX);
730 len += input_print_modalias_bits(buf + len, size - len,
731 'm', id->mscbit, 0, MSC_MAX);
732 len += input_print_modalias_bits(buf + len, size - len,
733 'l', id->ledbit, 0, LED_MAX);
734 len += input_print_modalias_bits(buf + len, size - len,
735 's', id->sndbit, 0, SND_MAX);
736 len += input_print_modalias_bits(buf + len, size - len,
737 'f', id->ffbit, 0, FF_MAX);
738 len += input_print_modalias_bits(buf + len, size - len,
739 'w', id->swbit, 0, SW_MAX);
742 len += snprintf(buf + len, max(size - len, 0), "\n");
747 static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
749 struct input_dev *id = to_input_dev(dev);
752 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
754 return min_t(int, len, PAGE_SIZE);
756 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
758 static struct attribute *input_dev_attrs[] = {
759 &class_device_attr_name.attr,
760 &class_device_attr_phys.attr,
761 &class_device_attr_uniq.attr,
762 &class_device_attr_modalias.attr,
766 static struct attribute_group input_dev_attr_group = {
767 .attrs = input_dev_attrs,
770 #define INPUT_DEV_ID_ATTR(name) \
771 static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
773 struct input_dev *input_dev = to_input_dev(dev); \
774 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
776 static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
778 INPUT_DEV_ID_ATTR(bustype);
779 INPUT_DEV_ID_ATTR(vendor);
780 INPUT_DEV_ID_ATTR(product);
781 INPUT_DEV_ID_ATTR(version);
783 static struct attribute *input_dev_id_attrs[] = {
784 &class_device_attr_bustype.attr,
785 &class_device_attr_vendor.attr,
786 &class_device_attr_product.attr,
787 &class_device_attr_version.attr,
791 static struct attribute_group input_dev_id_attr_group = {
793 .attrs = input_dev_id_attrs,
796 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
802 for (i = NBITS(max) - 1; i > 0; i--)
807 len += snprintf(buf + len, max(buf_size - len, 0),
808 "%lx%s", bitmap[i], i > 0 ? " " : "");
811 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
816 #define INPUT_DEV_CAP_ATTR(ev, bm) \
817 static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
819 struct input_dev *input_dev = to_input_dev(dev); \
820 int len = input_print_bitmap(buf, PAGE_SIZE, \
821 input_dev->bm##bit, ev##_MAX, 1); \
822 return min_t(int, len, PAGE_SIZE); \
824 static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
826 INPUT_DEV_CAP_ATTR(EV, ev);
827 INPUT_DEV_CAP_ATTR(KEY, key);
828 INPUT_DEV_CAP_ATTR(REL, rel);
829 INPUT_DEV_CAP_ATTR(ABS, abs);
830 INPUT_DEV_CAP_ATTR(MSC, msc);
831 INPUT_DEV_CAP_ATTR(LED, led);
832 INPUT_DEV_CAP_ATTR(SND, snd);
833 INPUT_DEV_CAP_ATTR(FF, ff);
834 INPUT_DEV_CAP_ATTR(SW, sw);
836 static struct attribute *input_dev_caps_attrs[] = {
837 &class_device_attr_ev.attr,
838 &class_device_attr_key.attr,
839 &class_device_attr_rel.attr,
840 &class_device_attr_abs.attr,
841 &class_device_attr_msc.attr,
842 &class_device_attr_led.attr,
843 &class_device_attr_snd.attr,
844 &class_device_attr_ff.attr,
845 &class_device_attr_sw.attr,
849 static struct attribute_group input_dev_caps_attr_group = {
850 .name = "capabilities",
851 .attrs = input_dev_caps_attrs,
854 static struct attribute_group *input_dev_attr_groups[] = {
855 &input_dev_attr_group,
856 &input_dev_id_attr_group,
857 &input_dev_caps_attr_group,
861 static void input_dev_release(struct class_device *class_dev)
863 struct input_dev *dev = to_input_dev(class_dev);
865 input_ff_destroy(dev);
868 module_put(THIS_MODULE);
872 * Input uevent interface - loading event handlers based on
875 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
876 char *buffer, int buffer_size, int *cur_len,
877 const char *name, unsigned long *bitmap, int max)
879 if (*cur_index >= num_envp - 1)
882 envp[*cur_index] = buffer + *cur_len;
884 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
885 if (*cur_len >= buffer_size)
888 *cur_len += input_print_bitmap(buffer + *cur_len,
889 max(buffer_size - *cur_len, 0),
891 if (*cur_len > buffer_size)
898 static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
899 char *buffer, int buffer_size, int *cur_len,
900 struct input_dev *dev)
902 if (*cur_index >= num_envp - 1)
905 envp[*cur_index] = buffer + *cur_len;
907 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
909 if (*cur_len >= buffer_size)
912 *cur_len += input_print_modalias(buffer + *cur_len,
913 max(buffer_size - *cur_len, 0),
915 if (*cur_len > buffer_size)
922 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
924 int err = add_uevent_var(envp, num_envp, &i, \
925 buffer, buffer_size, &len, \
931 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
933 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
934 buffer, buffer_size, &len, \
940 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
942 int err = input_add_uevent_modalias_var(envp, \
944 buffer, buffer_size, &len, \
950 static int input_dev_uevent(struct class_device *cdev, char **envp,
951 int num_envp, char *buffer, int buffer_size)
953 struct input_dev *dev = to_input_dev(cdev);
957 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
958 dev->id.bustype, dev->id.vendor,
959 dev->id.product, dev->id.version);
961 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
963 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
965 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
967 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
968 if (test_bit(EV_KEY, dev->evbit))
969 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
970 if (test_bit(EV_REL, dev->evbit))
971 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
972 if (test_bit(EV_ABS, dev->evbit))
973 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
974 if (test_bit(EV_MSC, dev->evbit))
975 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
976 if (test_bit(EV_LED, dev->evbit))
977 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
978 if (test_bit(EV_SND, dev->evbit))
979 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
980 if (test_bit(EV_FF, dev->evbit))
981 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
982 if (test_bit(EV_SW, dev->evbit))
983 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
985 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
991 struct class input_class = {
993 .release = input_dev_release,
994 .uevent = input_dev_uevent,
996 EXPORT_SYMBOL_GPL(input_class);
999 * input_allocate_device - allocate memory for new input device
1001 * Returns prepared struct input_dev or NULL.
1003 * NOTE: Use input_free_device() to free devices that have not been
1004 * registered; input_unregister_device() should be used for already
1005 * registered devices.
1007 struct input_dev *input_allocate_device(void)
1009 struct input_dev *dev;
1011 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1013 dev->cdev.class = &input_class;
1014 dev->cdev.groups = input_dev_attr_groups;
1015 class_device_initialize(&dev->cdev);
1016 mutex_init(&dev->mutex);
1017 INIT_LIST_HEAD(&dev->h_list);
1018 INIT_LIST_HEAD(&dev->node);
1020 __module_get(THIS_MODULE);
1025 EXPORT_SYMBOL(input_allocate_device);
1028 * input_free_device - free memory occupied by input_dev structure
1029 * @dev: input device to free
1031 * This function should only be used if input_register_device()
1032 * was not called yet or if it failed. Once device was registered
1033 * use input_unregister_device() and memory will be freed once last
1034 * refrence to the device is dropped.
1036 * Device should be allocated by input_allocate_device().
1038 * NOTE: If there are references to the input device then memory
1039 * will not be freed until last reference is dropped.
1041 void input_free_device(struct input_dev *dev)
1044 input_put_device(dev);
1046 EXPORT_SYMBOL(input_free_device);
1049 * input_set_capability - mark device as capable of a certain event
1050 * @dev: device that is capable of emitting or accepting event
1051 * @type: type of the event (EV_KEY, EV_REL, etc...)
1054 * In addition to setting up corresponding bit in appropriate capability
1055 * bitmap the function also adjusts dev->evbit.
1057 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1061 __set_bit(code, dev->keybit);
1065 __set_bit(code, dev->relbit);
1069 __set_bit(code, dev->absbit);
1073 __set_bit(code, dev->mscbit);
1077 __set_bit(code, dev->swbit);
1081 __set_bit(code, dev->ledbit);
1085 __set_bit(code, dev->sndbit);
1089 __set_bit(code, dev->ffbit);
1094 "input_set_capability: unknown type %u (code %u)\n",
1100 __set_bit(type, dev->evbit);
1102 EXPORT_SYMBOL(input_set_capability);
1104 int input_register_device(struct input_dev *dev)
1106 static atomic_t input_no = ATOMIC_INIT(0);
1107 struct input_handler *handler;
1111 set_bit(EV_SYN, dev->evbit);
1114 * If delay and period are pre-set by the driver, then autorepeating
1115 * is handled by the driver itself and we don't do it in input.c.
1118 init_timer(&dev->timer);
1119 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1120 dev->timer.data = (long) dev;
1121 dev->timer.function = input_repeat_key;
1122 dev->rep[REP_DELAY] = 250;
1123 dev->rep[REP_PERIOD] = 33;
1126 if (!dev->getkeycode)
1127 dev->getkeycode = input_default_getkeycode;
1129 if (!dev->setkeycode)
1130 dev->setkeycode = input_default_setkeycode;
1132 list_add_tail(&dev->node, &input_dev_list);
1134 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
1135 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1138 dev->cdev.dev = dev->dev.parent;
1140 error = class_device_add(&dev->cdev);
1144 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
1145 printk(KERN_INFO "input: %s as %s\n",
1146 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1149 list_for_each_entry(handler, &input_handler_list, node)
1150 input_attach_handler(dev, handler);
1152 input_wakeup_procfs_readers();
1156 EXPORT_SYMBOL(input_register_device);
1158 void input_unregister_device(struct input_dev *dev)
1160 struct input_handle *handle, *next;
1163 for (code = 0; code <= KEY_MAX; code++)
1164 if (test_bit(code, dev->key))
1165 input_report_key(dev, code, 0);
1168 del_timer_sync(&dev->timer);
1170 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1171 handle->handler->disconnect(handle);
1172 WARN_ON(!list_empty(&dev->h_list));
1174 list_del_init(&dev->node);
1176 class_device_unregister(&dev->cdev);
1178 input_wakeup_procfs_readers();
1180 EXPORT_SYMBOL(input_unregister_device);
1182 int input_register_handler(struct input_handler *handler)
1184 struct input_dev *dev;
1186 INIT_LIST_HEAD(&handler->h_list);
1188 if (handler->fops != NULL) {
1189 if (input_table[handler->minor >> 5])
1192 input_table[handler->minor >> 5] = handler;
1195 list_add_tail(&handler->node, &input_handler_list);
1197 list_for_each_entry(dev, &input_dev_list, node)
1198 input_attach_handler(dev, handler);
1200 input_wakeup_procfs_readers();
1203 EXPORT_SYMBOL(input_register_handler);
1205 void input_unregister_handler(struct input_handler *handler)
1207 struct input_handle *handle, *next;
1209 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1210 handler->disconnect(handle);
1211 WARN_ON(!list_empty(&handler->h_list));
1213 list_del_init(&handler->node);
1215 if (handler->fops != NULL)
1216 input_table[handler->minor >> 5] = NULL;
1218 input_wakeup_procfs_readers();
1220 EXPORT_SYMBOL(input_unregister_handler);
1222 int input_register_handle(struct input_handle *handle)
1224 struct input_handler *handler = handle->handler;
1226 list_add_tail(&handle->d_node, &handle->dev->h_list);
1227 list_add_tail(&handle->h_node, &handler->h_list);
1230 handler->start(handle);
1234 EXPORT_SYMBOL(input_register_handle);
1236 void input_unregister_handle(struct input_handle *handle)
1238 list_del_init(&handle->h_node);
1239 list_del_init(&handle->d_node);
1241 EXPORT_SYMBOL(input_unregister_handle);
1243 static int input_open_file(struct inode *inode, struct file *file)
1245 struct input_handler *handler = input_table[iminor(inode) >> 5];
1246 const struct file_operations *old_fops, *new_fops = NULL;
1249 /* No load-on-demand here? */
1250 if (!handler || !(new_fops = fops_get(handler->fops)))
1254 * That's _really_ odd. Usually NULL ->open means "nothing special",
1255 * not "no device". Oh, well...
1257 if (!new_fops->open) {
1261 old_fops = file->f_op;
1262 file->f_op = new_fops;
1264 err = new_fops->open(inode, file);
1267 fops_put(file->f_op);
1268 file->f_op = fops_get(old_fops);
1274 static const struct file_operations input_fops = {
1275 .owner = THIS_MODULE,
1276 .open = input_open_file,
1279 static int __init input_init(void)
1283 err = class_register(&input_class);
1285 printk(KERN_ERR "input: unable to register input_dev class\n");
1289 err = input_proc_init();
1293 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1295 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1301 fail2: input_proc_exit();
1302 fail1: class_unregister(&input_class);
1306 static void __exit input_exit(void)
1309 unregister_chrdev(INPUT_MAJOR, "input");
1310 class_unregister(&input_class);
1313 subsys_initcall(input_init);
1314 module_exit(input_exit);