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/types.h>
15 #include <linux/input.h>
16 #include <linux/module.h>
17 #include <linux/random.h>
18 #include <linux/major.h>
19 #include <linux/proc_fs.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/poll.h>
23 #include <linux/device.h>
24 #include <linux/mutex.h>
25 #include <linux/rcupdate.h>
26 #include <linux/smp_lock.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
29 MODULE_DESCRIPTION("Input core");
30 MODULE_LICENSE("GPL");
32 #define INPUT_DEVICES 256
35 * EV_ABS events which should not be cached are listed here.
37 static unsigned int input_abs_bypass_init_data[] __initdata = {
50 static unsigned long input_abs_bypass[BITS_TO_LONGS(ABS_CNT)];
52 static LIST_HEAD(input_dev_list);
53 static LIST_HEAD(input_handler_list);
56 * input_mutex protects access to both input_dev_list and input_handler_list.
57 * This also causes input_[un]register_device and input_[un]register_handler
58 * be mutually exclusive which simplifies locking in drivers implementing
61 static DEFINE_MUTEX(input_mutex);
63 static struct input_handler *input_table[8];
65 static inline int is_event_supported(unsigned int code,
66 unsigned long *bm, unsigned int max)
68 return code <= max && test_bit(code, bm);
71 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
74 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
77 if (value > old_val - fuzz && value < old_val + fuzz)
78 return (old_val * 3 + value) / 4;
80 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
81 return (old_val + value) / 2;
88 * Pass event through all open handles. This function is called with
89 * dev->event_lock held and interrupts disabled.
91 static void input_pass_event(struct input_dev *dev,
92 unsigned int type, unsigned int code, int value)
94 struct input_handle *handle;
98 handle = rcu_dereference(dev->grab);
100 handle->handler->event(handle, type, code, value);
102 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
104 handle->handler->event(handle,
110 * Generate software autorepeat event. Note that we take
111 * dev->event_lock here to avoid racing with input_event
112 * which may cause keys get "stuck".
114 static void input_repeat_key(unsigned long data)
116 struct input_dev *dev = (void *) data;
119 spin_lock_irqsave(&dev->event_lock, flags);
121 if (test_bit(dev->repeat_key, dev->key) &&
122 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
124 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
128 * Only send SYN_REPORT if we are not in a middle
129 * of driver parsing a new hardware packet.
130 * Otherwise assume that the driver will send
131 * SYN_REPORT once it's done.
133 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
136 if (dev->rep[REP_PERIOD])
137 mod_timer(&dev->timer, jiffies +
138 msecs_to_jiffies(dev->rep[REP_PERIOD]));
141 spin_unlock_irqrestore(&dev->event_lock, flags);
144 static void input_start_autorepeat(struct input_dev *dev, int code)
146 if (test_bit(EV_REP, dev->evbit) &&
147 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
149 dev->repeat_key = code;
150 mod_timer(&dev->timer,
151 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
155 static void input_stop_autorepeat(struct input_dev *dev)
157 del_timer(&dev->timer);
160 #define INPUT_IGNORE_EVENT 0
161 #define INPUT_PASS_TO_HANDLERS 1
162 #define INPUT_PASS_TO_DEVICE 2
163 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
165 static void input_handle_event(struct input_dev *dev,
166 unsigned int type, unsigned int code, int value)
168 int disposition = INPUT_IGNORE_EVENT;
175 disposition = INPUT_PASS_TO_ALL;
181 disposition = INPUT_PASS_TO_HANDLERS;
186 disposition = INPUT_PASS_TO_HANDLERS;
192 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
193 !!test_bit(code, dev->key) != value) {
196 __change_bit(code, dev->key);
198 input_start_autorepeat(dev, code);
200 input_stop_autorepeat(dev);
203 disposition = INPUT_PASS_TO_HANDLERS;
208 if (is_event_supported(code, dev->swbit, SW_MAX) &&
209 !!test_bit(code, dev->sw) != value) {
211 __change_bit(code, dev->sw);
212 disposition = INPUT_PASS_TO_HANDLERS;
217 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
219 if (test_bit(code, input_abs_bypass)) {
220 disposition = INPUT_PASS_TO_HANDLERS;
224 value = input_defuzz_abs_event(value,
225 dev->abs[code], dev->absfuzz[code]);
227 if (dev->abs[code] != value) {
228 dev->abs[code] = value;
229 disposition = INPUT_PASS_TO_HANDLERS;
235 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
236 disposition = INPUT_PASS_TO_HANDLERS;
241 if (is_event_supported(code, dev->mscbit, MSC_MAX))
242 disposition = INPUT_PASS_TO_ALL;
247 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
248 !!test_bit(code, dev->led) != value) {
250 __change_bit(code, dev->led);
251 disposition = INPUT_PASS_TO_ALL;
256 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
258 if (!!test_bit(code, dev->snd) != !!value)
259 __change_bit(code, dev->snd);
260 disposition = INPUT_PASS_TO_ALL;
265 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
266 dev->rep[code] = value;
267 disposition = INPUT_PASS_TO_ALL;
273 disposition = INPUT_PASS_TO_ALL;
277 disposition = INPUT_PASS_TO_ALL;
281 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
284 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
285 dev->event(dev, type, code, value);
287 if (disposition & INPUT_PASS_TO_HANDLERS)
288 input_pass_event(dev, type, code, value);
292 * input_event() - report new input event
293 * @dev: device that generated the event
294 * @type: type of the event
296 * @value: value of the event
298 * This function should be used by drivers implementing various input
299 * devices to report input events. See also input_inject_event().
301 * NOTE: input_event() may be safely used right after input device was
302 * allocated with input_allocate_device(), even before it is registered
303 * with input_register_device(), but the event will not reach any of the
304 * input handlers. Such early invocation of input_event() may be used
305 * to 'seed' initial state of a switch or initial position of absolute
308 void input_event(struct input_dev *dev,
309 unsigned int type, unsigned int code, int value)
313 if (is_event_supported(type, dev->evbit, EV_MAX)) {
315 spin_lock_irqsave(&dev->event_lock, flags);
316 add_input_randomness(type, code, value);
317 input_handle_event(dev, type, code, value);
318 spin_unlock_irqrestore(&dev->event_lock, flags);
321 EXPORT_SYMBOL(input_event);
324 * input_inject_event() - send input event from input handler
325 * @handle: input handle to send event through
326 * @type: type of the event
328 * @value: value of the event
330 * Similar to input_event() but will ignore event if device is
331 * "grabbed" and handle injecting event is not the one that owns
334 void input_inject_event(struct input_handle *handle,
335 unsigned int type, unsigned int code, int value)
337 struct input_dev *dev = handle->dev;
338 struct input_handle *grab;
341 if (is_event_supported(type, dev->evbit, EV_MAX)) {
342 spin_lock_irqsave(&dev->event_lock, flags);
345 grab = rcu_dereference(dev->grab);
346 if (!grab || grab == handle)
347 input_handle_event(dev, type, code, value);
350 spin_unlock_irqrestore(&dev->event_lock, flags);
353 EXPORT_SYMBOL(input_inject_event);
356 * input_grab_device - grabs device for exclusive use
357 * @handle: input handle that wants to own the device
359 * When a device is grabbed by an input handle all events generated by
360 * the device are delivered only to this handle. Also events injected
361 * by other input handles are ignored while device is grabbed.
363 int input_grab_device(struct input_handle *handle)
365 struct input_dev *dev = handle->dev;
368 retval = mutex_lock_interruptible(&dev->mutex);
377 rcu_assign_pointer(dev->grab, handle);
381 mutex_unlock(&dev->mutex);
384 EXPORT_SYMBOL(input_grab_device);
386 static void __input_release_device(struct input_handle *handle)
388 struct input_dev *dev = handle->dev;
390 if (dev->grab == handle) {
391 rcu_assign_pointer(dev->grab, NULL);
392 /* Make sure input_pass_event() notices that grab is gone */
395 list_for_each_entry(handle, &dev->h_list, d_node)
396 if (handle->open && handle->handler->start)
397 handle->handler->start(handle);
402 * input_release_device - release previously grabbed device
403 * @handle: input handle that owns the device
405 * Releases previously grabbed device so that other input handles can
406 * start receiving input events. Upon release all handlers attached
407 * to the device have their start() method called so they have a change
408 * to synchronize device state with the rest of the system.
410 void input_release_device(struct input_handle *handle)
412 struct input_dev *dev = handle->dev;
414 mutex_lock(&dev->mutex);
415 __input_release_device(handle);
416 mutex_unlock(&dev->mutex);
418 EXPORT_SYMBOL(input_release_device);
421 * input_open_device - open input device
422 * @handle: handle through which device is being accessed
424 * This function should be called by input handlers when they
425 * want to start receive events from given input device.
427 int input_open_device(struct input_handle *handle)
429 struct input_dev *dev = handle->dev;
432 retval = mutex_lock_interruptible(&dev->mutex);
436 if (dev->going_away) {
443 if (!dev->users++ && dev->open)
444 retval = dev->open(dev);
448 if (!--handle->open) {
450 * Make sure we are not delivering any more events
451 * through this handle
458 mutex_unlock(&dev->mutex);
461 EXPORT_SYMBOL(input_open_device);
463 int input_flush_device(struct input_handle *handle, struct file *file)
465 struct input_dev *dev = handle->dev;
468 retval = mutex_lock_interruptible(&dev->mutex);
473 retval = dev->flush(dev, file);
475 mutex_unlock(&dev->mutex);
478 EXPORT_SYMBOL(input_flush_device);
481 * input_close_device - close input device
482 * @handle: handle through which device is being accessed
484 * This function should be called by input handlers when they
485 * want to stop receive events from given input device.
487 void input_close_device(struct input_handle *handle)
489 struct input_dev *dev = handle->dev;
491 mutex_lock(&dev->mutex);
493 __input_release_device(handle);
495 if (!--dev->users && dev->close)
498 if (!--handle->open) {
500 * synchronize_rcu() makes sure that input_pass_event()
501 * completed and that no more input events are delivered
502 * through this handle
507 mutex_unlock(&dev->mutex);
509 EXPORT_SYMBOL(input_close_device);
512 * Prepare device for unregistering
514 static void input_disconnect_device(struct input_dev *dev)
516 struct input_handle *handle;
520 * Mark device as going away. Note that we take dev->mutex here
521 * not to protect access to dev->going_away but rather to ensure
522 * that there are no threads in the middle of input_open_device()
524 mutex_lock(&dev->mutex);
525 dev->going_away = true;
526 mutex_unlock(&dev->mutex);
528 spin_lock_irq(&dev->event_lock);
531 * Simulate keyup events for all pressed keys so that handlers
532 * are not left with "stuck" keys. The driver may continue
533 * generate events even after we done here but they will not
534 * reach any handlers.
536 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
537 for (code = 0; code <= KEY_MAX; code++) {
538 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
539 __test_and_clear_bit(code, dev->key)) {
540 input_pass_event(dev, EV_KEY, code, 0);
543 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
546 list_for_each_entry(handle, &dev->h_list, d_node)
549 spin_unlock_irq(&dev->event_lock);
552 static int input_fetch_keycode(struct input_dev *dev, int scancode)
554 switch (dev->keycodesize) {
556 return ((u8 *)dev->keycode)[scancode];
559 return ((u16 *)dev->keycode)[scancode];
562 return ((u32 *)dev->keycode)[scancode];
566 static int input_default_getkeycode(struct input_dev *dev,
567 int scancode, int *keycode)
569 if (!dev->keycodesize)
572 if (scancode >= dev->keycodemax)
575 *keycode = input_fetch_keycode(dev, scancode);
580 static int input_default_setkeycode(struct input_dev *dev,
581 int scancode, int keycode)
586 if (scancode >= dev->keycodemax)
589 if (!dev->keycodesize)
592 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
595 switch (dev->keycodesize) {
597 u8 *k = (u8 *)dev->keycode;
598 old_keycode = k[scancode];
599 k[scancode] = keycode;
603 u16 *k = (u16 *)dev->keycode;
604 old_keycode = k[scancode];
605 k[scancode] = keycode;
609 u32 *k = (u32 *)dev->keycode;
610 old_keycode = k[scancode];
611 k[scancode] = keycode;
616 __clear_bit(old_keycode, dev->keybit);
617 __set_bit(keycode, dev->keybit);
619 for (i = 0; i < dev->keycodemax; i++) {
620 if (input_fetch_keycode(dev, i) == old_keycode) {
621 __set_bit(old_keycode, dev->keybit);
622 break; /* Setting the bit twice is useless, so break */
630 * input_get_keycode - retrieve keycode currently mapped to a given scancode
631 * @dev: input device which keymap is being queried
632 * @scancode: scancode (or its equivalent for device in question) for which
636 * This function should be called by anyone interested in retrieving current
637 * keymap. Presently keyboard and evdev handlers use it.
639 int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
644 return dev->getkeycode(dev, scancode, keycode);
646 EXPORT_SYMBOL(input_get_keycode);
649 * input_get_keycode - assign new keycode to a given scancode
650 * @dev: input device which keymap is being updated
651 * @scancode: scancode (or its equivalent for device in question)
652 * @keycode: new keycode to be assigned to the scancode
654 * This function should be called by anyone needing to update current
655 * keymap. Presently keyboard and evdev handlers use it.
657 int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
666 if (keycode < 0 || keycode > KEY_MAX)
669 spin_lock_irqsave(&dev->event_lock, flags);
671 retval = dev->getkeycode(dev, scancode, &old_keycode);
675 retval = dev->setkeycode(dev, scancode, keycode);
679 /* Make sure KEY_RESERVED did not get enabled. */
680 __clear_bit(KEY_RESERVED, dev->keybit);
683 * Simulate keyup event if keycode is not present
684 * in the keymap anymore
686 if (test_bit(EV_KEY, dev->evbit) &&
687 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
688 __test_and_clear_bit(old_keycode, dev->key)) {
690 input_pass_event(dev, EV_KEY, old_keycode, 0);
692 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
696 spin_unlock_irqrestore(&dev->event_lock, flags);
700 EXPORT_SYMBOL(input_set_keycode);
702 #define MATCH_BIT(bit, max) \
703 for (i = 0; i < BITS_TO_LONGS(max); i++) \
704 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
706 if (i != BITS_TO_LONGS(max)) \
709 static const struct input_device_id *input_match_device(const struct input_device_id *id,
710 struct input_dev *dev)
714 for (; id->flags || id->driver_info; id++) {
716 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
717 if (id->bustype != dev->id.bustype)
720 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
721 if (id->vendor != dev->id.vendor)
724 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
725 if (id->product != dev->id.product)
728 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
729 if (id->version != dev->id.version)
732 MATCH_BIT(evbit, EV_MAX);
733 MATCH_BIT(keybit, KEY_MAX);
734 MATCH_BIT(relbit, REL_MAX);
735 MATCH_BIT(absbit, ABS_MAX);
736 MATCH_BIT(mscbit, MSC_MAX);
737 MATCH_BIT(ledbit, LED_MAX);
738 MATCH_BIT(sndbit, SND_MAX);
739 MATCH_BIT(ffbit, FF_MAX);
740 MATCH_BIT(swbit, SW_MAX);
748 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
750 const struct input_device_id *id;
753 if (handler->blacklist && input_match_device(handler->blacklist, dev))
756 id = input_match_device(handler->id_table, dev);
760 error = handler->connect(handler, dev, id);
761 if (error && error != -ENODEV)
763 "input: failed to attach handler %s to device %s, "
765 handler->name, kobject_name(&dev->dev.kobj), error);
771 #ifdef CONFIG_PROC_FS
773 static struct proc_dir_entry *proc_bus_input_dir;
774 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
775 static int input_devices_state;
777 static inline void input_wakeup_procfs_readers(void)
779 input_devices_state++;
780 wake_up(&input_devices_poll_wait);
783 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
785 poll_wait(file, &input_devices_poll_wait, wait);
786 if (file->f_version != input_devices_state) {
787 file->f_version = input_devices_state;
788 return POLLIN | POLLRDNORM;
794 union input_seq_state {
802 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
804 union input_seq_state *state = (union input_seq_state *)&seq->private;
807 /* We need to fit into seq->private pointer */
808 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
810 error = mutex_lock_interruptible(&input_mutex);
812 state->mutex_acquired = false;
813 return ERR_PTR(error);
816 state->mutex_acquired = true;
818 return seq_list_start(&input_dev_list, *pos);
821 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
823 return seq_list_next(v, &input_dev_list, pos);
826 static void input_seq_stop(struct seq_file *seq, void *v)
828 union input_seq_state *state = (union input_seq_state *)&seq->private;
830 if (state->mutex_acquired)
831 mutex_unlock(&input_mutex);
834 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
835 unsigned long *bitmap, int max)
839 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
843 seq_printf(seq, "B: %s=", name);
845 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
849 static int input_devices_seq_show(struct seq_file *seq, void *v)
851 struct input_dev *dev = container_of(v, struct input_dev, node);
852 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
853 struct input_handle *handle;
855 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
856 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
858 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
859 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
860 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
861 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
862 seq_printf(seq, "H: Handlers=");
864 list_for_each_entry(handle, &dev->h_list, d_node)
865 seq_printf(seq, "%s ", handle->name);
868 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
869 if (test_bit(EV_KEY, dev->evbit))
870 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
871 if (test_bit(EV_REL, dev->evbit))
872 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
873 if (test_bit(EV_ABS, dev->evbit))
874 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
875 if (test_bit(EV_MSC, dev->evbit))
876 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
877 if (test_bit(EV_LED, dev->evbit))
878 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
879 if (test_bit(EV_SND, dev->evbit))
880 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
881 if (test_bit(EV_FF, dev->evbit))
882 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
883 if (test_bit(EV_SW, dev->evbit))
884 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
892 static const struct seq_operations input_devices_seq_ops = {
893 .start = input_devices_seq_start,
894 .next = input_devices_seq_next,
895 .stop = input_seq_stop,
896 .show = input_devices_seq_show,
899 static int input_proc_devices_open(struct inode *inode, struct file *file)
901 return seq_open(file, &input_devices_seq_ops);
904 static const struct file_operations input_devices_fileops = {
905 .owner = THIS_MODULE,
906 .open = input_proc_devices_open,
907 .poll = input_proc_devices_poll,
910 .release = seq_release,
913 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
915 union input_seq_state *state = (union input_seq_state *)&seq->private;
918 /* We need to fit into seq->private pointer */
919 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
921 error = mutex_lock_interruptible(&input_mutex);
923 state->mutex_acquired = false;
924 return ERR_PTR(error);
927 state->mutex_acquired = true;
930 return seq_list_start(&input_handler_list, *pos);
933 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
935 union input_seq_state *state = (union input_seq_state *)&seq->private;
937 state->pos = *pos + 1;
938 return seq_list_next(v, &input_handler_list, pos);
941 static int input_handlers_seq_show(struct seq_file *seq, void *v)
943 struct input_handler *handler = container_of(v, struct input_handler, node);
944 union input_seq_state *state = (union input_seq_state *)&seq->private;
946 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
948 seq_printf(seq, " Minor=%d", handler->minor);
954 static const struct seq_operations input_handlers_seq_ops = {
955 .start = input_handlers_seq_start,
956 .next = input_handlers_seq_next,
957 .stop = input_seq_stop,
958 .show = input_handlers_seq_show,
961 static int input_proc_handlers_open(struct inode *inode, struct file *file)
963 return seq_open(file, &input_handlers_seq_ops);
966 static const struct file_operations input_handlers_fileops = {
967 .owner = THIS_MODULE,
968 .open = input_proc_handlers_open,
971 .release = seq_release,
974 static int __init input_proc_init(void)
976 struct proc_dir_entry *entry;
978 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
979 if (!proc_bus_input_dir)
982 entry = proc_create("devices", 0, proc_bus_input_dir,
983 &input_devices_fileops);
987 entry = proc_create("handlers", 0, proc_bus_input_dir,
988 &input_handlers_fileops);
994 fail2: remove_proc_entry("devices", proc_bus_input_dir);
995 fail1: remove_proc_entry("bus/input", NULL);
999 static void input_proc_exit(void)
1001 remove_proc_entry("devices", proc_bus_input_dir);
1002 remove_proc_entry("handlers", proc_bus_input_dir);
1003 remove_proc_entry("bus/input", NULL);
1006 #else /* !CONFIG_PROC_FS */
1007 static inline void input_wakeup_procfs_readers(void) { }
1008 static inline int input_proc_init(void) { return 0; }
1009 static inline void input_proc_exit(void) { }
1012 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
1013 static ssize_t input_dev_show_##name(struct device *dev, \
1014 struct device_attribute *attr, \
1017 struct input_dev *input_dev = to_input_dev(dev); \
1019 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1020 input_dev->name ? input_dev->name : ""); \
1022 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1024 INPUT_DEV_STRING_ATTR_SHOW(name);
1025 INPUT_DEV_STRING_ATTR_SHOW(phys);
1026 INPUT_DEV_STRING_ATTR_SHOW(uniq);
1028 static int input_print_modalias_bits(char *buf, int size,
1029 char name, unsigned long *bm,
1030 unsigned int min_bit, unsigned int max_bit)
1034 len += snprintf(buf, max(size, 0), "%c", name);
1035 for (i = min_bit; i < max_bit; i++)
1036 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1037 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1041 static int input_print_modalias(char *buf, int size, struct input_dev *id,
1046 len = snprintf(buf, max(size, 0),
1047 "input:b%04Xv%04Xp%04Xe%04X-",
1048 id->id.bustype, id->id.vendor,
1049 id->id.product, id->id.version);
1051 len += input_print_modalias_bits(buf + len, size - len,
1052 'e', id->evbit, 0, EV_MAX);
1053 len += input_print_modalias_bits(buf + len, size - len,
1054 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1055 len += input_print_modalias_bits(buf + len, size - len,
1056 'r', id->relbit, 0, REL_MAX);
1057 len += input_print_modalias_bits(buf + len, size - len,
1058 'a', id->absbit, 0, ABS_MAX);
1059 len += input_print_modalias_bits(buf + len, size - len,
1060 'm', id->mscbit, 0, MSC_MAX);
1061 len += input_print_modalias_bits(buf + len, size - len,
1062 'l', id->ledbit, 0, LED_MAX);
1063 len += input_print_modalias_bits(buf + len, size - len,
1064 's', id->sndbit, 0, SND_MAX);
1065 len += input_print_modalias_bits(buf + len, size - len,
1066 'f', id->ffbit, 0, FF_MAX);
1067 len += input_print_modalias_bits(buf + len, size - len,
1068 'w', id->swbit, 0, SW_MAX);
1071 len += snprintf(buf + len, max(size - len, 0), "\n");
1076 static ssize_t input_dev_show_modalias(struct device *dev,
1077 struct device_attribute *attr,
1080 struct input_dev *id = to_input_dev(dev);
1083 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1085 return min_t(int, len, PAGE_SIZE);
1087 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1089 static struct attribute *input_dev_attrs[] = {
1090 &dev_attr_name.attr,
1091 &dev_attr_phys.attr,
1092 &dev_attr_uniq.attr,
1093 &dev_attr_modalias.attr,
1097 static struct attribute_group input_dev_attr_group = {
1098 .attrs = input_dev_attrs,
1101 #define INPUT_DEV_ID_ATTR(name) \
1102 static ssize_t input_dev_show_id_##name(struct device *dev, \
1103 struct device_attribute *attr, \
1106 struct input_dev *input_dev = to_input_dev(dev); \
1107 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1109 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1111 INPUT_DEV_ID_ATTR(bustype);
1112 INPUT_DEV_ID_ATTR(vendor);
1113 INPUT_DEV_ID_ATTR(product);
1114 INPUT_DEV_ID_ATTR(version);
1116 static struct attribute *input_dev_id_attrs[] = {
1117 &dev_attr_bustype.attr,
1118 &dev_attr_vendor.attr,
1119 &dev_attr_product.attr,
1120 &dev_attr_version.attr,
1124 static struct attribute_group input_dev_id_attr_group = {
1126 .attrs = input_dev_id_attrs,
1129 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1130 int max, int add_cr)
1135 for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
1140 len += snprintf(buf + len, max(buf_size - len, 0),
1141 "%lx%s", bitmap[i], i > 0 ? " " : "");
1144 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1149 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1150 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1151 struct device_attribute *attr, \
1154 struct input_dev *input_dev = to_input_dev(dev); \
1155 int len = input_print_bitmap(buf, PAGE_SIZE, \
1156 input_dev->bm##bit, ev##_MAX, 1); \
1157 return min_t(int, len, PAGE_SIZE); \
1159 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1161 INPUT_DEV_CAP_ATTR(EV, ev);
1162 INPUT_DEV_CAP_ATTR(KEY, key);
1163 INPUT_DEV_CAP_ATTR(REL, rel);
1164 INPUT_DEV_CAP_ATTR(ABS, abs);
1165 INPUT_DEV_CAP_ATTR(MSC, msc);
1166 INPUT_DEV_CAP_ATTR(LED, led);
1167 INPUT_DEV_CAP_ATTR(SND, snd);
1168 INPUT_DEV_CAP_ATTR(FF, ff);
1169 INPUT_DEV_CAP_ATTR(SW, sw);
1171 static struct attribute *input_dev_caps_attrs[] = {
1184 static struct attribute_group input_dev_caps_attr_group = {
1185 .name = "capabilities",
1186 .attrs = input_dev_caps_attrs,
1189 static const struct attribute_group *input_dev_attr_groups[] = {
1190 &input_dev_attr_group,
1191 &input_dev_id_attr_group,
1192 &input_dev_caps_attr_group,
1196 static void input_dev_release(struct device *device)
1198 struct input_dev *dev = to_input_dev(device);
1200 input_ff_destroy(dev);
1203 module_put(THIS_MODULE);
1207 * Input uevent interface - loading event handlers based on
1210 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1211 const char *name, unsigned long *bitmap, int max)
1215 if (add_uevent_var(env, "%s=", name))
1218 len = input_print_bitmap(&env->buf[env->buflen - 1],
1219 sizeof(env->buf) - env->buflen,
1221 if (len >= (sizeof(env->buf) - env->buflen))
1228 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1229 struct input_dev *dev)
1233 if (add_uevent_var(env, "MODALIAS="))
1236 len = input_print_modalias(&env->buf[env->buflen - 1],
1237 sizeof(env->buf) - env->buflen,
1239 if (len >= (sizeof(env->buf) - env->buflen))
1246 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1248 int err = add_uevent_var(env, fmt, val); \
1253 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1255 int err = input_add_uevent_bm_var(env, name, bm, max); \
1260 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1262 int err = input_add_uevent_modalias_var(env, dev); \
1267 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1269 struct input_dev *dev = to_input_dev(device);
1271 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1272 dev->id.bustype, dev->id.vendor,
1273 dev->id.product, dev->id.version);
1275 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1277 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1279 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1281 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1282 if (test_bit(EV_KEY, dev->evbit))
1283 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1284 if (test_bit(EV_REL, dev->evbit))
1285 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1286 if (test_bit(EV_ABS, dev->evbit))
1287 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1288 if (test_bit(EV_MSC, dev->evbit))
1289 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1290 if (test_bit(EV_LED, dev->evbit))
1291 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1292 if (test_bit(EV_SND, dev->evbit))
1293 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1294 if (test_bit(EV_FF, dev->evbit))
1295 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1296 if (test_bit(EV_SW, dev->evbit))
1297 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1299 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1304 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1309 if (!test_bit(EV_##type, dev->evbit)) \
1312 for (i = 0; i < type##_MAX; i++) { \
1313 if (!test_bit(i, dev->bits##bit)) \
1316 active = test_bit(i, dev->bits); \
1317 if (!active && !on) \
1320 dev->event(dev, EV_##type, i, on ? active : 0); \
1325 static void input_dev_reset(struct input_dev *dev, bool activate)
1330 INPUT_DO_TOGGLE(dev, LED, led, activate);
1331 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1333 if (activate && test_bit(EV_REP, dev->evbit)) {
1334 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1335 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1339 static int input_dev_suspend(struct device *dev)
1341 struct input_dev *input_dev = to_input_dev(dev);
1343 mutex_lock(&input_dev->mutex);
1344 input_dev_reset(input_dev, false);
1345 mutex_unlock(&input_dev->mutex);
1350 static int input_dev_resume(struct device *dev)
1352 struct input_dev *input_dev = to_input_dev(dev);
1354 mutex_lock(&input_dev->mutex);
1355 input_dev_reset(input_dev, true);
1356 mutex_unlock(&input_dev->mutex);
1361 static const struct dev_pm_ops input_dev_pm_ops = {
1362 .suspend = input_dev_suspend,
1363 .resume = input_dev_resume,
1364 .poweroff = input_dev_suspend,
1365 .restore = input_dev_resume,
1367 #endif /* CONFIG_PM */
1369 static struct device_type input_dev_type = {
1370 .groups = input_dev_attr_groups,
1371 .release = input_dev_release,
1372 .uevent = input_dev_uevent,
1374 .pm = &input_dev_pm_ops,
1378 static char *input_devnode(struct device *dev, mode_t *mode)
1380 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1383 struct class input_class = {
1385 .devnode = input_devnode,
1387 EXPORT_SYMBOL_GPL(input_class);
1390 * input_allocate_device - allocate memory for new input device
1392 * Returns prepared struct input_dev or NULL.
1394 * NOTE: Use input_free_device() to free devices that have not been
1395 * registered; input_unregister_device() should be used for already
1396 * registered devices.
1398 struct input_dev *input_allocate_device(void)
1400 struct input_dev *dev;
1402 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1404 dev->dev.type = &input_dev_type;
1405 dev->dev.class = &input_class;
1406 device_initialize(&dev->dev);
1407 mutex_init(&dev->mutex);
1408 spin_lock_init(&dev->event_lock);
1409 INIT_LIST_HEAD(&dev->h_list);
1410 INIT_LIST_HEAD(&dev->node);
1412 __module_get(THIS_MODULE);
1417 EXPORT_SYMBOL(input_allocate_device);
1420 * input_free_device - free memory occupied by input_dev structure
1421 * @dev: input device to free
1423 * This function should only be used if input_register_device()
1424 * was not called yet or if it failed. Once device was registered
1425 * use input_unregister_device() and memory will be freed once last
1426 * reference to the device is dropped.
1428 * Device should be allocated by input_allocate_device().
1430 * NOTE: If there are references to the input device then memory
1431 * will not be freed until last reference is dropped.
1433 void input_free_device(struct input_dev *dev)
1436 input_put_device(dev);
1438 EXPORT_SYMBOL(input_free_device);
1441 * input_set_capability - mark device as capable of a certain event
1442 * @dev: device that is capable of emitting or accepting event
1443 * @type: type of the event (EV_KEY, EV_REL, etc...)
1446 * In addition to setting up corresponding bit in appropriate capability
1447 * bitmap the function also adjusts dev->evbit.
1449 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1453 __set_bit(code, dev->keybit);
1457 __set_bit(code, dev->relbit);
1461 __set_bit(code, dev->absbit);
1465 __set_bit(code, dev->mscbit);
1469 __set_bit(code, dev->swbit);
1473 __set_bit(code, dev->ledbit);
1477 __set_bit(code, dev->sndbit);
1481 __set_bit(code, dev->ffbit);
1490 "input_set_capability: unknown type %u (code %u)\n",
1496 __set_bit(type, dev->evbit);
1498 EXPORT_SYMBOL(input_set_capability);
1501 * input_register_device - register device with input core
1502 * @dev: device to be registered
1504 * This function registers device with input core. The device must be
1505 * allocated with input_allocate_device() and all it's capabilities
1506 * set up before registering.
1507 * If function fails the device must be freed with input_free_device().
1508 * Once device has been successfully registered it can be unregistered
1509 * with input_unregister_device(); input_free_device() should not be
1510 * called in this case.
1512 int input_register_device(struct input_dev *dev)
1514 static atomic_t input_no = ATOMIC_INIT(0);
1515 struct input_handler *handler;
1519 /* Every input device generates EV_SYN/SYN_REPORT events. */
1520 __set_bit(EV_SYN, dev->evbit);
1522 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
1523 __clear_bit(KEY_RESERVED, dev->keybit);
1526 * If delay and period are pre-set by the driver, then autorepeating
1527 * is handled by the driver itself and we don't do it in input.c.
1529 init_timer(&dev->timer);
1530 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1531 dev->timer.data = (long) dev;
1532 dev->timer.function = input_repeat_key;
1533 dev->rep[REP_DELAY] = 250;
1534 dev->rep[REP_PERIOD] = 33;
1537 if (!dev->getkeycode)
1538 dev->getkeycode = input_default_getkeycode;
1540 if (!dev->setkeycode)
1541 dev->setkeycode = input_default_setkeycode;
1543 dev_set_name(&dev->dev, "input%ld",
1544 (unsigned long) atomic_inc_return(&input_no) - 1);
1546 error = device_add(&dev->dev);
1550 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1551 printk(KERN_INFO "input: %s as %s\n",
1552 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1555 error = mutex_lock_interruptible(&input_mutex);
1557 device_del(&dev->dev);
1561 list_add_tail(&dev->node, &input_dev_list);
1563 list_for_each_entry(handler, &input_handler_list, node)
1564 input_attach_handler(dev, handler);
1566 input_wakeup_procfs_readers();
1568 mutex_unlock(&input_mutex);
1572 EXPORT_SYMBOL(input_register_device);
1575 * input_unregister_device - unregister previously registered device
1576 * @dev: device to be unregistered
1578 * This function unregisters an input device. Once device is unregistered
1579 * the caller should not try to access it as it may get freed at any moment.
1581 void input_unregister_device(struct input_dev *dev)
1583 struct input_handle *handle, *next;
1585 input_disconnect_device(dev);
1587 mutex_lock(&input_mutex);
1589 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1590 handle->handler->disconnect(handle);
1591 WARN_ON(!list_empty(&dev->h_list));
1593 del_timer_sync(&dev->timer);
1594 list_del_init(&dev->node);
1596 input_wakeup_procfs_readers();
1598 mutex_unlock(&input_mutex);
1600 device_unregister(&dev->dev);
1602 EXPORT_SYMBOL(input_unregister_device);
1605 * input_register_handler - register a new input handler
1606 * @handler: handler to be registered
1608 * This function registers a new input handler (interface) for input
1609 * devices in the system and attaches it to all input devices that
1610 * are compatible with the handler.
1612 int input_register_handler(struct input_handler *handler)
1614 struct input_dev *dev;
1617 retval = mutex_lock_interruptible(&input_mutex);
1621 INIT_LIST_HEAD(&handler->h_list);
1623 if (handler->fops != NULL) {
1624 if (input_table[handler->minor >> 5]) {
1628 input_table[handler->minor >> 5] = handler;
1631 list_add_tail(&handler->node, &input_handler_list);
1633 list_for_each_entry(dev, &input_dev_list, node)
1634 input_attach_handler(dev, handler);
1636 input_wakeup_procfs_readers();
1639 mutex_unlock(&input_mutex);
1642 EXPORT_SYMBOL(input_register_handler);
1645 * input_unregister_handler - unregisters an input handler
1646 * @handler: handler to be unregistered
1648 * This function disconnects a handler from its input devices and
1649 * removes it from lists of known handlers.
1651 void input_unregister_handler(struct input_handler *handler)
1653 struct input_handle *handle, *next;
1655 mutex_lock(&input_mutex);
1657 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1658 handler->disconnect(handle);
1659 WARN_ON(!list_empty(&handler->h_list));
1661 list_del_init(&handler->node);
1663 if (handler->fops != NULL)
1664 input_table[handler->minor >> 5] = NULL;
1666 input_wakeup_procfs_readers();
1668 mutex_unlock(&input_mutex);
1670 EXPORT_SYMBOL(input_unregister_handler);
1673 * input_handler_for_each_handle - handle iterator
1674 * @handler: input handler to iterate
1675 * @data: data for the callback
1676 * @fn: function to be called for each handle
1678 * Iterate over @bus's list of devices, and call @fn for each, passing
1679 * it @data and stop when @fn returns a non-zero value. The function is
1680 * using RCU to traverse the list and therefore may be usind in atonic
1681 * contexts. The @fn callback is invoked from RCU critical section and
1682 * thus must not sleep.
1684 int input_handler_for_each_handle(struct input_handler *handler, void *data,
1685 int (*fn)(struct input_handle *, void *))
1687 struct input_handle *handle;
1692 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
1693 retval = fn(handle, data);
1702 EXPORT_SYMBOL(input_handler_for_each_handle);
1705 * input_register_handle - register a new input handle
1706 * @handle: handle to register
1708 * This function puts a new input handle onto device's
1709 * and handler's lists so that events can flow through
1710 * it once it is opened using input_open_device().
1712 * This function is supposed to be called from handler's
1715 int input_register_handle(struct input_handle *handle)
1717 struct input_handler *handler = handle->handler;
1718 struct input_dev *dev = handle->dev;
1722 * We take dev->mutex here to prevent race with
1723 * input_release_device().
1725 error = mutex_lock_interruptible(&dev->mutex);
1728 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1729 mutex_unlock(&dev->mutex);
1732 * Since we are supposed to be called from ->connect()
1733 * which is mutually exclusive with ->disconnect()
1734 * we can't be racing with input_unregister_handle()
1735 * and so separate lock is not needed here.
1737 list_add_tail_rcu(&handle->h_node, &handler->h_list);
1740 handler->start(handle);
1744 EXPORT_SYMBOL(input_register_handle);
1747 * input_unregister_handle - unregister an input handle
1748 * @handle: handle to unregister
1750 * This function removes input handle from device's
1751 * and handler's lists.
1753 * This function is supposed to be called from handler's
1754 * disconnect() method.
1756 void input_unregister_handle(struct input_handle *handle)
1758 struct input_dev *dev = handle->dev;
1760 list_del_rcu(&handle->h_node);
1763 * Take dev->mutex to prevent race with input_release_device().
1765 mutex_lock(&dev->mutex);
1766 list_del_rcu(&handle->d_node);
1767 mutex_unlock(&dev->mutex);
1771 EXPORT_SYMBOL(input_unregister_handle);
1773 static int input_open_file(struct inode *inode, struct file *file)
1775 struct input_handler *handler;
1776 const struct file_operations *old_fops, *new_fops = NULL;
1780 /* No load-on-demand here? */
1781 handler = input_table[iminor(inode) >> 5];
1782 if (!handler || !(new_fops = fops_get(handler->fops))) {
1788 * That's _really_ odd. Usually NULL ->open means "nothing special",
1789 * not "no device". Oh, well...
1791 if (!new_fops->open) {
1796 old_fops = file->f_op;
1797 file->f_op = new_fops;
1799 err = new_fops->open(inode, file);
1802 fops_put(file->f_op);
1803 file->f_op = fops_get(old_fops);
1811 static const struct file_operations input_fops = {
1812 .owner = THIS_MODULE,
1813 .open = input_open_file,
1816 static void __init input_init_abs_bypass(void)
1818 const unsigned int *p;
1820 for (p = input_abs_bypass_init_data; *p; p++)
1821 input_abs_bypass[BIT_WORD(*p)] |= BIT_MASK(*p);
1824 static int __init input_init(void)
1828 input_init_abs_bypass();
1830 err = class_register(&input_class);
1832 printk(KERN_ERR "input: unable to register input_dev class\n");
1836 err = input_proc_init();
1840 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1842 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
1848 fail2: input_proc_exit();
1849 fail1: class_unregister(&input_class);
1853 static void __exit input_exit(void)
1856 unregister_chrdev(INPUT_MAJOR, "input");
1857 class_unregister(&input_class);
1860 subsys_initcall(input_init);
1861 module_exit(input_exit);