1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 1999-2002 Vojtech Pavlik
9 #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/idr.h>
14 #include <linux/input/mt.h>
15 #include <linux/module.h>
16 #include <linux/slab.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>
23 #include <linux/poll.h>
24 #include <linux/device.h>
25 #include <linux/kstrtox.h>
26 #include <linux/mutex.h>
27 #include <linux/rcupdate.h>
28 #include "input-compat.h"
29 #include "input-core-private.h"
30 #include "input-poller.h"
32 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
33 MODULE_DESCRIPTION("Input core");
34 MODULE_LICENSE("GPL");
36 #define INPUT_MAX_CHAR_DEVICES 1024
37 #define INPUT_FIRST_DYNAMIC_DEV 256
38 static DEFINE_IDA(input_ida);
40 static LIST_HEAD(input_dev_list);
41 static LIST_HEAD(input_handler_list);
44 * input_mutex protects access to both input_dev_list and input_handler_list.
45 * This also causes input_[un]register_device and input_[un]register_handler
46 * be mutually exclusive which simplifies locking in drivers implementing
49 static DEFINE_MUTEX(input_mutex);
51 static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };
53 static const unsigned int input_max_code[EV_CNT] = {
64 static inline int is_event_supported(unsigned int code,
65 unsigned long *bm, unsigned int max)
67 return code <= max && test_bit(code, bm);
70 static int input_defuzz_abs_event(int value, int old_val, int fuzz)
73 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
76 if (value > old_val - fuzz && value < old_val + fuzz)
77 return (old_val * 3 + value) / 4;
79 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
80 return (old_val + value) / 2;
86 static void input_start_autorepeat(struct input_dev *dev, int code)
88 if (test_bit(EV_REP, dev->evbit) &&
89 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
90 dev->timer.function) {
91 dev->repeat_key = code;
92 mod_timer(&dev->timer,
93 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
97 static void input_stop_autorepeat(struct input_dev *dev)
99 del_timer(&dev->timer);
103 * Pass event first through all filters and then, if event has not been
104 * filtered out, through all open handles. This function is called with
105 * dev->event_lock held and interrupts disabled.
107 static unsigned int input_to_handler(struct input_handle *handle,
108 struct input_value *vals, unsigned int count)
110 struct input_handler *handler = handle->handler;
111 struct input_value *end = vals;
112 struct input_value *v;
114 if (handler->filter) {
115 for (v = vals; v != vals + count; v++) {
116 if (handler->filter(handle, v->type, v->code, v->value))
129 handler->events(handle, vals, count);
130 else if (handler->event)
131 for (v = vals; v != vals + count; v++)
132 handler->event(handle, v->type, v->code, v->value);
138 * Pass values first through all filters and then, if event has not been
139 * filtered out, through all open handles. This function is called with
140 * dev->event_lock held and interrupts disabled.
142 static void input_pass_values(struct input_dev *dev,
143 struct input_value *vals, unsigned int count)
145 struct input_handle *handle;
146 struct input_value *v;
148 lockdep_assert_held(&dev->event_lock);
155 handle = rcu_dereference(dev->grab);
157 count = input_to_handler(handle, vals, count);
159 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
161 count = input_to_handler(handle, vals, count);
169 /* trigger auto repeat for key events */
170 if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
171 for (v = vals; v != vals + count; v++) {
172 if (v->type == EV_KEY && v->value != 2) {
174 input_start_autorepeat(dev, v->code);
176 input_stop_autorepeat(dev);
182 #define INPUT_IGNORE_EVENT 0
183 #define INPUT_PASS_TO_HANDLERS 1
184 #define INPUT_PASS_TO_DEVICE 2
186 #define INPUT_FLUSH 8
187 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
189 static int input_handle_abs_event(struct input_dev *dev,
190 unsigned int code, int *pval)
192 struct input_mt *mt = dev->mt;
196 if (code == ABS_MT_SLOT) {
198 * "Stage" the event; we'll flush it later, when we
199 * get actual touch data.
201 if (mt && *pval >= 0 && *pval < mt->num_slots)
204 return INPUT_IGNORE_EVENT;
207 is_mt_event = input_is_mt_value(code);
210 pold = &dev->absinfo[code].value;
212 pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
215 * Bypass filtering for multi-touch events when
216 * not employing slots.
222 *pval = input_defuzz_abs_event(*pval, *pold,
223 dev->absinfo[code].fuzz);
225 return INPUT_IGNORE_EVENT;
230 /* Flush pending "slot" event */
231 if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
232 input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
233 return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
236 return INPUT_PASS_TO_HANDLERS;
239 static int input_get_disposition(struct input_dev *dev,
240 unsigned int type, unsigned int code, int *pval)
242 int disposition = INPUT_IGNORE_EVENT;
245 /* filter-out events from inhibited devices */
247 return INPUT_IGNORE_EVENT;
254 disposition = INPUT_PASS_TO_ALL;
258 disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
261 disposition = INPUT_PASS_TO_HANDLERS;
267 if (is_event_supported(code, dev->keybit, KEY_MAX)) {
269 /* auto-repeat bypasses state updates */
271 disposition = INPUT_PASS_TO_HANDLERS;
275 if (!!test_bit(code, dev->key) != !!value) {
277 __change_bit(code, dev->key);
278 disposition = INPUT_PASS_TO_HANDLERS;
284 if (is_event_supported(code, dev->swbit, SW_MAX) &&
285 !!test_bit(code, dev->sw) != !!value) {
287 __change_bit(code, dev->sw);
288 disposition = INPUT_PASS_TO_HANDLERS;
293 if (is_event_supported(code, dev->absbit, ABS_MAX))
294 disposition = input_handle_abs_event(dev, code, &value);
299 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
300 disposition = INPUT_PASS_TO_HANDLERS;
305 if (is_event_supported(code, dev->mscbit, MSC_MAX))
306 disposition = INPUT_PASS_TO_ALL;
311 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
312 !!test_bit(code, dev->led) != !!value) {
314 __change_bit(code, dev->led);
315 disposition = INPUT_PASS_TO_ALL;
320 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
322 if (!!test_bit(code, dev->snd) != !!value)
323 __change_bit(code, dev->snd);
324 disposition = INPUT_PASS_TO_ALL;
329 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
330 dev->rep[code] = value;
331 disposition = INPUT_PASS_TO_ALL;
337 disposition = INPUT_PASS_TO_ALL;
341 disposition = INPUT_PASS_TO_ALL;
349 static void input_event_dispose(struct input_dev *dev, int disposition,
350 unsigned int type, unsigned int code, int value)
352 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
353 dev->event(dev, type, code, value);
358 if (disposition & INPUT_PASS_TO_HANDLERS) {
359 struct input_value *v;
361 if (disposition & INPUT_SLOT) {
362 v = &dev->vals[dev->num_vals++];
364 v->code = ABS_MT_SLOT;
365 v->value = dev->mt->slot;
368 v = &dev->vals[dev->num_vals++];
374 if (disposition & INPUT_FLUSH) {
375 if (dev->num_vals >= 2)
376 input_pass_values(dev, dev->vals, dev->num_vals);
379 * Reset the timestamp on flush so we won't end up
380 * with a stale one. Note we only need to reset the
381 * monolithic one as we use its presence when deciding
382 * whether to generate a synthetic timestamp.
384 dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0);
385 } else if (dev->num_vals >= dev->max_vals - 2) {
386 dev->vals[dev->num_vals++] = input_value_sync;
387 input_pass_values(dev, dev->vals, dev->num_vals);
392 void input_handle_event(struct input_dev *dev,
393 unsigned int type, unsigned int code, int value)
397 lockdep_assert_held(&dev->event_lock);
399 disposition = input_get_disposition(dev, type, code, &value);
400 if (disposition != INPUT_IGNORE_EVENT) {
402 add_input_randomness(type, code, value);
404 input_event_dispose(dev, disposition, type, code, value);
409 * input_event() - report new input event
410 * @dev: device that generated the event
411 * @type: type of the event
413 * @value: value of the event
415 * This function should be used by drivers implementing various input
416 * devices to report input events. See also input_inject_event().
418 * NOTE: input_event() may be safely used right after input device was
419 * allocated with input_allocate_device(), even before it is registered
420 * with input_register_device(), but the event will not reach any of the
421 * input handlers. Such early invocation of input_event() may be used
422 * to 'seed' initial state of a switch or initial position of absolute
425 void input_event(struct input_dev *dev,
426 unsigned int type, unsigned int code, int value)
430 if (is_event_supported(type, dev->evbit, EV_MAX)) {
432 spin_lock_irqsave(&dev->event_lock, flags);
433 input_handle_event(dev, type, code, value);
434 spin_unlock_irqrestore(&dev->event_lock, flags);
437 EXPORT_SYMBOL(input_event);
440 * input_inject_event() - send input event from input handler
441 * @handle: input handle to send event through
442 * @type: type of the event
444 * @value: value of the event
446 * Similar to input_event() but will ignore event if device is
447 * "grabbed" and handle injecting event is not the one that owns
450 void input_inject_event(struct input_handle *handle,
451 unsigned int type, unsigned int code, int value)
453 struct input_dev *dev = handle->dev;
454 struct input_handle *grab;
457 if (is_event_supported(type, dev->evbit, EV_MAX)) {
458 spin_lock_irqsave(&dev->event_lock, flags);
461 grab = rcu_dereference(dev->grab);
462 if (!grab || grab == handle)
463 input_handle_event(dev, type, code, value);
466 spin_unlock_irqrestore(&dev->event_lock, flags);
469 EXPORT_SYMBOL(input_inject_event);
472 * input_alloc_absinfo - allocates array of input_absinfo structs
473 * @dev: the input device emitting absolute events
475 * If the absinfo struct the caller asked for is already allocated, this
476 * functions will not do anything.
478 void input_alloc_absinfo(struct input_dev *dev)
483 dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL);
485 dev_err(dev->dev.parent ?: &dev->dev,
486 "%s: unable to allocate memory\n", __func__);
488 * We will handle this allocation failure in
489 * input_register_device() when we refuse to register input
490 * device with ABS bits but without absinfo.
494 EXPORT_SYMBOL(input_alloc_absinfo);
496 void input_set_abs_params(struct input_dev *dev, unsigned int axis,
497 int min, int max, int fuzz, int flat)
499 struct input_absinfo *absinfo;
501 __set_bit(EV_ABS, dev->evbit);
502 __set_bit(axis, dev->absbit);
504 input_alloc_absinfo(dev);
508 absinfo = &dev->absinfo[axis];
509 absinfo->minimum = min;
510 absinfo->maximum = max;
511 absinfo->fuzz = fuzz;
512 absinfo->flat = flat;
514 EXPORT_SYMBOL(input_set_abs_params);
517 * input_copy_abs - Copy absinfo from one input_dev to another
518 * @dst: Destination input device to copy the abs settings to
519 * @dst_axis: ABS_* value selecting the destination axis
520 * @src: Source input device to copy the abs settings from
521 * @src_axis: ABS_* value selecting the source axis
523 * Set absinfo for the selected destination axis by copying it from
524 * the specified source input device's source axis.
525 * This is useful to e.g. setup a pen/stylus input-device for combined
526 * touchscreen/pen hardware where the pen uses the same coordinates as
529 void input_copy_abs(struct input_dev *dst, unsigned int dst_axis,
530 const struct input_dev *src, unsigned int src_axis)
532 /* src must have EV_ABS and src_axis set */
533 if (WARN_ON(!(test_bit(EV_ABS, src->evbit) &&
534 test_bit(src_axis, src->absbit))))
538 * input_alloc_absinfo() may have failed for the source. Our caller is
539 * expected to catch this when registering the input devices, which may
540 * happen after the input_copy_abs() call.
545 input_set_capability(dst, EV_ABS, dst_axis);
549 dst->absinfo[dst_axis] = src->absinfo[src_axis];
551 EXPORT_SYMBOL(input_copy_abs);
554 * input_grab_device - grabs device for exclusive use
555 * @handle: input handle that wants to own the device
557 * When a device is grabbed by an input handle all events generated by
558 * the device are delivered only to this handle. Also events injected
559 * by other input handles are ignored while device is grabbed.
561 int input_grab_device(struct input_handle *handle)
563 struct input_dev *dev = handle->dev;
566 retval = mutex_lock_interruptible(&dev->mutex);
575 rcu_assign_pointer(dev->grab, handle);
578 mutex_unlock(&dev->mutex);
581 EXPORT_SYMBOL(input_grab_device);
583 static void __input_release_device(struct input_handle *handle)
585 struct input_dev *dev = handle->dev;
586 struct input_handle *grabber;
588 grabber = rcu_dereference_protected(dev->grab,
589 lockdep_is_held(&dev->mutex));
590 if (grabber == handle) {
591 rcu_assign_pointer(dev->grab, NULL);
592 /* Make sure input_pass_values() notices that grab is gone */
595 list_for_each_entry(handle, &dev->h_list, d_node)
596 if (handle->open && handle->handler->start)
597 handle->handler->start(handle);
602 * input_release_device - release previously grabbed device
603 * @handle: input handle that owns the device
605 * Releases previously grabbed device so that other input handles can
606 * start receiving input events. Upon release all handlers attached
607 * to the device have their start() method called so they have a change
608 * to synchronize device state with the rest of the system.
610 void input_release_device(struct input_handle *handle)
612 struct input_dev *dev = handle->dev;
614 mutex_lock(&dev->mutex);
615 __input_release_device(handle);
616 mutex_unlock(&dev->mutex);
618 EXPORT_SYMBOL(input_release_device);
621 * input_open_device - open input device
622 * @handle: handle through which device is being accessed
624 * This function should be called by input handlers when they
625 * want to start receive events from given input device.
627 int input_open_device(struct input_handle *handle)
629 struct input_dev *dev = handle->dev;
632 retval = mutex_lock_interruptible(&dev->mutex);
636 if (dev->going_away) {
643 if (dev->users++ || dev->inhibited) {
645 * Device is already opened and/or inhibited,
646 * so we can exit immediately and report success.
652 retval = dev->open(dev);
657 * Make sure we are not delivering any more events
658 * through this handle
666 input_dev_poller_start(dev->poller);
669 mutex_unlock(&dev->mutex);
672 EXPORT_SYMBOL(input_open_device);
674 int input_flush_device(struct input_handle *handle, struct file *file)
676 struct input_dev *dev = handle->dev;
679 retval = mutex_lock_interruptible(&dev->mutex);
684 retval = dev->flush(dev, file);
686 mutex_unlock(&dev->mutex);
689 EXPORT_SYMBOL(input_flush_device);
692 * input_close_device - close input device
693 * @handle: handle through which device is being accessed
695 * This function should be called by input handlers when they
696 * want to stop receive events from given input device.
698 void input_close_device(struct input_handle *handle)
700 struct input_dev *dev = handle->dev;
702 mutex_lock(&dev->mutex);
704 __input_release_device(handle);
706 if (!dev->inhibited && !--dev->users) {
708 input_dev_poller_stop(dev->poller);
713 if (!--handle->open) {
715 * synchronize_rcu() makes sure that input_pass_values()
716 * completed and that no more input events are delivered
717 * through this handle
722 mutex_unlock(&dev->mutex);
724 EXPORT_SYMBOL(input_close_device);
727 * Simulate keyup events for all keys that are marked as pressed.
728 * The function must be called with dev->event_lock held.
730 static bool input_dev_release_keys(struct input_dev *dev)
732 bool need_sync = false;
735 lockdep_assert_held(&dev->event_lock);
737 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
738 for_each_set_bit(code, dev->key, KEY_CNT) {
739 input_handle_event(dev, EV_KEY, code, 0);
748 * Prepare device for unregistering
750 static void input_disconnect_device(struct input_dev *dev)
752 struct input_handle *handle;
755 * Mark device as going away. Note that we take dev->mutex here
756 * not to protect access to dev->going_away but rather to ensure
757 * that there are no threads in the middle of input_open_device()
759 mutex_lock(&dev->mutex);
760 dev->going_away = true;
761 mutex_unlock(&dev->mutex);
763 spin_lock_irq(&dev->event_lock);
766 * Simulate keyup events for all pressed keys so that handlers
767 * are not left with "stuck" keys. The driver may continue
768 * generate events even after we done here but they will not
769 * reach any handlers.
771 if (input_dev_release_keys(dev))
772 input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
774 list_for_each_entry(handle, &dev->h_list, d_node)
777 spin_unlock_irq(&dev->event_lock);
781 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
782 * @ke: keymap entry containing scancode to be converted.
783 * @scancode: pointer to the location where converted scancode should
786 * This function is used to convert scancode stored in &struct keymap_entry
787 * into scalar form understood by legacy keymap handling methods. These
788 * methods expect scancodes to be represented as 'unsigned int'.
790 int input_scancode_to_scalar(const struct input_keymap_entry *ke,
791 unsigned int *scancode)
795 *scancode = *((u8 *)ke->scancode);
799 *scancode = *((u16 *)ke->scancode);
803 *scancode = *((u32 *)ke->scancode);
812 EXPORT_SYMBOL(input_scancode_to_scalar);
815 * Those routines handle the default case where no [gs]etkeycode() is
816 * defined. In this case, an array indexed by the scancode is used.
819 static unsigned int input_fetch_keycode(struct input_dev *dev,
822 switch (dev->keycodesize) {
824 return ((u8 *)dev->keycode)[index];
827 return ((u16 *)dev->keycode)[index];
830 return ((u32 *)dev->keycode)[index];
834 static int input_default_getkeycode(struct input_dev *dev,
835 struct input_keymap_entry *ke)
840 if (!dev->keycodesize)
843 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
846 error = input_scancode_to_scalar(ke, &index);
851 if (index >= dev->keycodemax)
854 ke->keycode = input_fetch_keycode(dev, index);
856 ke->len = sizeof(index);
857 memcpy(ke->scancode, &index, sizeof(index));
862 static int input_default_setkeycode(struct input_dev *dev,
863 const struct input_keymap_entry *ke,
864 unsigned int *old_keycode)
870 if (!dev->keycodesize)
873 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
876 error = input_scancode_to_scalar(ke, &index);
881 if (index >= dev->keycodemax)
884 if (dev->keycodesize < sizeof(ke->keycode) &&
885 (ke->keycode >> (dev->keycodesize * 8)))
888 switch (dev->keycodesize) {
890 u8 *k = (u8 *)dev->keycode;
891 *old_keycode = k[index];
892 k[index] = ke->keycode;
896 u16 *k = (u16 *)dev->keycode;
897 *old_keycode = k[index];
898 k[index] = ke->keycode;
902 u32 *k = (u32 *)dev->keycode;
903 *old_keycode = k[index];
904 k[index] = ke->keycode;
909 if (*old_keycode <= KEY_MAX) {
910 __clear_bit(*old_keycode, dev->keybit);
911 for (i = 0; i < dev->keycodemax; i++) {
912 if (input_fetch_keycode(dev, i) == *old_keycode) {
913 __set_bit(*old_keycode, dev->keybit);
914 /* Setting the bit twice is useless, so break */
920 __set_bit(ke->keycode, dev->keybit);
925 * input_get_keycode - retrieve keycode currently mapped to a given scancode
926 * @dev: input device which keymap is being queried
929 * This function should be called by anyone interested in retrieving current
930 * keymap. Presently evdev handlers use it.
932 int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
937 spin_lock_irqsave(&dev->event_lock, flags);
938 retval = dev->getkeycode(dev, ke);
939 spin_unlock_irqrestore(&dev->event_lock, flags);
943 EXPORT_SYMBOL(input_get_keycode);
946 * input_set_keycode - attribute a keycode to a given scancode
947 * @dev: input device which keymap is being updated
948 * @ke: new keymap entry
950 * This function should be called by anyone needing to update current
951 * keymap. Presently keyboard and evdev handlers use it.
953 int input_set_keycode(struct input_dev *dev,
954 const struct input_keymap_entry *ke)
957 unsigned int old_keycode;
960 if (ke->keycode > KEY_MAX)
963 spin_lock_irqsave(&dev->event_lock, flags);
965 retval = dev->setkeycode(dev, ke, &old_keycode);
969 /* Make sure KEY_RESERVED did not get enabled. */
970 __clear_bit(KEY_RESERVED, dev->keybit);
973 * Simulate keyup event if keycode is not present
974 * in the keymap anymore
976 if (old_keycode > KEY_MAX) {
977 dev_warn(dev->dev.parent ?: &dev->dev,
978 "%s: got too big old keycode %#x\n",
979 __func__, old_keycode);
980 } else if (test_bit(EV_KEY, dev->evbit) &&
981 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
982 __test_and_clear_bit(old_keycode, dev->key)) {
984 * We have to use input_event_dispose() here directly instead
985 * of input_handle_event() because the key we want to release
986 * here is considered no longer supported by the device and
987 * input_handle_event() will ignore it.
989 input_event_dispose(dev, INPUT_PASS_TO_HANDLERS,
990 EV_KEY, old_keycode, 0);
991 input_event_dispose(dev, INPUT_PASS_TO_HANDLERS | INPUT_FLUSH,
992 EV_SYN, SYN_REPORT, 1);
996 spin_unlock_irqrestore(&dev->event_lock, flags);
1000 EXPORT_SYMBOL(input_set_keycode);
1002 bool input_match_device_id(const struct input_dev *dev,
1003 const struct input_device_id *id)
1005 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
1006 if (id->bustype != dev->id.bustype)
1009 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
1010 if (id->vendor != dev->id.vendor)
1013 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
1014 if (id->product != dev->id.product)
1017 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
1018 if (id->version != dev->id.version)
1021 if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
1022 !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
1023 !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
1024 !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
1025 !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
1026 !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
1027 !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
1028 !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
1029 !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
1030 !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
1036 EXPORT_SYMBOL(input_match_device_id);
1038 static const struct input_device_id *input_match_device(struct input_handler *handler,
1039 struct input_dev *dev)
1041 const struct input_device_id *id;
1043 for (id = handler->id_table; id->flags || id->driver_info; id++) {
1044 if (input_match_device_id(dev, id) &&
1045 (!handler->match || handler->match(handler, dev))) {
1053 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
1055 const struct input_device_id *id;
1058 id = input_match_device(handler, dev);
1062 error = handler->connect(handler, dev, id);
1063 if (error && error != -ENODEV)
1064 pr_err("failed to attach handler %s to device %s, error: %d\n",
1065 handler->name, kobject_name(&dev->dev.kobj), error);
1070 #ifdef CONFIG_COMPAT
1072 static int input_bits_to_string(char *buf, int buf_size,
1073 unsigned long bits, bool skip_empty)
1077 if (in_compat_syscall()) {
1078 u32 dword = bits >> 32;
1079 if (dword || !skip_empty)
1080 len += snprintf(buf, buf_size, "%x ", dword);
1082 dword = bits & 0xffffffffUL;
1083 if (dword || !skip_empty || len)
1084 len += snprintf(buf + len, max(buf_size - len, 0),
1087 if (bits || !skip_empty)
1088 len += snprintf(buf, buf_size, "%lx", bits);
1094 #else /* !CONFIG_COMPAT */
1096 static int input_bits_to_string(char *buf, int buf_size,
1097 unsigned long bits, bool skip_empty)
1099 return bits || !skip_empty ?
1100 snprintf(buf, buf_size, "%lx", bits) : 0;
1105 #ifdef CONFIG_PROC_FS
1107 static struct proc_dir_entry *proc_bus_input_dir;
1108 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
1109 static int input_devices_state;
1111 static inline void input_wakeup_procfs_readers(void)
1113 input_devices_state++;
1114 wake_up(&input_devices_poll_wait);
1117 static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait)
1119 poll_wait(file, &input_devices_poll_wait, wait);
1120 if (file->f_version != input_devices_state) {
1121 file->f_version = input_devices_state;
1122 return EPOLLIN | EPOLLRDNORM;
1128 union input_seq_state {
1131 bool mutex_acquired;
1136 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
1138 union input_seq_state *state = (union input_seq_state *)&seq->private;
1141 /* We need to fit into seq->private pointer */
1142 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1144 error = mutex_lock_interruptible(&input_mutex);
1146 state->mutex_acquired = false;
1147 return ERR_PTR(error);
1150 state->mutex_acquired = true;
1152 return seq_list_start(&input_dev_list, *pos);
1155 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1157 return seq_list_next(v, &input_dev_list, pos);
1160 static void input_seq_stop(struct seq_file *seq, void *v)
1162 union input_seq_state *state = (union input_seq_state *)&seq->private;
1164 if (state->mutex_acquired)
1165 mutex_unlock(&input_mutex);
1168 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
1169 unsigned long *bitmap, int max)
1172 bool skip_empty = true;
1175 seq_printf(seq, "B: %s=", name);
1177 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1178 if (input_bits_to_string(buf, sizeof(buf),
1179 bitmap[i], skip_empty)) {
1181 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
1186 * If no output was produced print a single 0.
1191 seq_putc(seq, '\n');
1194 static int input_devices_seq_show(struct seq_file *seq, void *v)
1196 struct input_dev *dev = container_of(v, struct input_dev, node);
1197 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1198 struct input_handle *handle;
1200 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
1201 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
1203 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
1204 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
1205 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
1206 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
1207 seq_puts(seq, "H: Handlers=");
1209 list_for_each_entry(handle, &dev->h_list, d_node)
1210 seq_printf(seq, "%s ", handle->name);
1211 seq_putc(seq, '\n');
1213 input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
1215 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
1216 if (test_bit(EV_KEY, dev->evbit))
1217 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
1218 if (test_bit(EV_REL, dev->evbit))
1219 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
1220 if (test_bit(EV_ABS, dev->evbit))
1221 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
1222 if (test_bit(EV_MSC, dev->evbit))
1223 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
1224 if (test_bit(EV_LED, dev->evbit))
1225 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
1226 if (test_bit(EV_SND, dev->evbit))
1227 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
1228 if (test_bit(EV_FF, dev->evbit))
1229 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
1230 if (test_bit(EV_SW, dev->evbit))
1231 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
1233 seq_putc(seq, '\n');
1239 static const struct seq_operations input_devices_seq_ops = {
1240 .start = input_devices_seq_start,
1241 .next = input_devices_seq_next,
1242 .stop = input_seq_stop,
1243 .show = input_devices_seq_show,
1246 static int input_proc_devices_open(struct inode *inode, struct file *file)
1248 return seq_open(file, &input_devices_seq_ops);
1251 static const struct proc_ops input_devices_proc_ops = {
1252 .proc_open = input_proc_devices_open,
1253 .proc_poll = input_proc_devices_poll,
1254 .proc_read = seq_read,
1255 .proc_lseek = seq_lseek,
1256 .proc_release = seq_release,
1259 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
1261 union input_seq_state *state = (union input_seq_state *)&seq->private;
1264 /* We need to fit into seq->private pointer */
1265 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1267 error = mutex_lock_interruptible(&input_mutex);
1269 state->mutex_acquired = false;
1270 return ERR_PTR(error);
1273 state->mutex_acquired = true;
1276 return seq_list_start(&input_handler_list, *pos);
1279 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1281 union input_seq_state *state = (union input_seq_state *)&seq->private;
1283 state->pos = *pos + 1;
1284 return seq_list_next(v, &input_handler_list, pos);
1287 static int input_handlers_seq_show(struct seq_file *seq, void *v)
1289 struct input_handler *handler = container_of(v, struct input_handler, node);
1290 union input_seq_state *state = (union input_seq_state *)&seq->private;
1292 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
1293 if (handler->filter)
1294 seq_puts(seq, " (filter)");
1295 if (handler->legacy_minors)
1296 seq_printf(seq, " Minor=%d", handler->minor);
1297 seq_putc(seq, '\n');
1302 static const struct seq_operations input_handlers_seq_ops = {
1303 .start = input_handlers_seq_start,
1304 .next = input_handlers_seq_next,
1305 .stop = input_seq_stop,
1306 .show = input_handlers_seq_show,
1309 static int input_proc_handlers_open(struct inode *inode, struct file *file)
1311 return seq_open(file, &input_handlers_seq_ops);
1314 static const struct proc_ops input_handlers_proc_ops = {
1315 .proc_open = input_proc_handlers_open,
1316 .proc_read = seq_read,
1317 .proc_lseek = seq_lseek,
1318 .proc_release = seq_release,
1321 static int __init input_proc_init(void)
1323 struct proc_dir_entry *entry;
1325 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
1326 if (!proc_bus_input_dir)
1329 entry = proc_create("devices", 0, proc_bus_input_dir,
1330 &input_devices_proc_ops);
1334 entry = proc_create("handlers", 0, proc_bus_input_dir,
1335 &input_handlers_proc_ops);
1341 fail2: remove_proc_entry("devices", proc_bus_input_dir);
1342 fail1: remove_proc_entry("bus/input", NULL);
1346 static void input_proc_exit(void)
1348 remove_proc_entry("devices", proc_bus_input_dir);
1349 remove_proc_entry("handlers", proc_bus_input_dir);
1350 remove_proc_entry("bus/input", NULL);
1353 #else /* !CONFIG_PROC_FS */
1354 static inline void input_wakeup_procfs_readers(void) { }
1355 static inline int input_proc_init(void) { return 0; }
1356 static inline void input_proc_exit(void) { }
1359 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
1360 static ssize_t input_dev_show_##name(struct device *dev, \
1361 struct device_attribute *attr, \
1364 struct input_dev *input_dev = to_input_dev(dev); \
1366 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1367 input_dev->name ? input_dev->name : ""); \
1369 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1371 INPUT_DEV_STRING_ATTR_SHOW(name);
1372 INPUT_DEV_STRING_ATTR_SHOW(phys);
1373 INPUT_DEV_STRING_ATTR_SHOW(uniq);
1375 static int input_print_modalias_bits(char *buf, int size,
1376 char name, const unsigned long *bm,
1377 unsigned int min_bit, unsigned int max_bit)
1381 len += snprintf(buf, max(size, 0), "%c", name);
1382 for (i = min_bit; i < max_bit; i++)
1383 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1384 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1388 static int input_print_modalias(char *buf, int size, const struct input_dev *id,
1393 len = snprintf(buf, max(size, 0),
1394 "input:b%04Xv%04Xp%04Xe%04X-",
1395 id->id.bustype, id->id.vendor,
1396 id->id.product, id->id.version);
1398 len += input_print_modalias_bits(buf + len, size - len,
1399 'e', id->evbit, 0, EV_MAX);
1400 len += input_print_modalias_bits(buf + len, size - len,
1401 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1402 len += input_print_modalias_bits(buf + len, size - len,
1403 'r', id->relbit, 0, REL_MAX);
1404 len += input_print_modalias_bits(buf + len, size - len,
1405 'a', id->absbit, 0, ABS_MAX);
1406 len += input_print_modalias_bits(buf + len, size - len,
1407 'm', id->mscbit, 0, MSC_MAX);
1408 len += input_print_modalias_bits(buf + len, size - len,
1409 'l', id->ledbit, 0, LED_MAX);
1410 len += input_print_modalias_bits(buf + len, size - len,
1411 's', id->sndbit, 0, SND_MAX);
1412 len += input_print_modalias_bits(buf + len, size - len,
1413 'f', id->ffbit, 0, FF_MAX);
1414 len += input_print_modalias_bits(buf + len, size - len,
1415 'w', id->swbit, 0, SW_MAX);
1418 len += snprintf(buf + len, max(size - len, 0), "\n");
1423 static ssize_t input_dev_show_modalias(struct device *dev,
1424 struct device_attribute *attr,
1427 struct input_dev *id = to_input_dev(dev);
1430 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1432 return min_t(int, len, PAGE_SIZE);
1434 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1436 static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap,
1437 int max, int add_cr);
1439 static ssize_t input_dev_show_properties(struct device *dev,
1440 struct device_attribute *attr,
1443 struct input_dev *input_dev = to_input_dev(dev);
1444 int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
1445 INPUT_PROP_MAX, true);
1446 return min_t(int, len, PAGE_SIZE);
1448 static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
1450 static int input_inhibit_device(struct input_dev *dev);
1451 static int input_uninhibit_device(struct input_dev *dev);
1453 static ssize_t inhibited_show(struct device *dev,
1454 struct device_attribute *attr,
1457 struct input_dev *input_dev = to_input_dev(dev);
1459 return scnprintf(buf, PAGE_SIZE, "%d\n", input_dev->inhibited);
1462 static ssize_t inhibited_store(struct device *dev,
1463 struct device_attribute *attr, const char *buf,
1466 struct input_dev *input_dev = to_input_dev(dev);
1470 if (kstrtobool(buf, &inhibited))
1474 rv = input_inhibit_device(input_dev);
1476 rv = input_uninhibit_device(input_dev);
1484 static DEVICE_ATTR_RW(inhibited);
1486 static struct attribute *input_dev_attrs[] = {
1487 &dev_attr_name.attr,
1488 &dev_attr_phys.attr,
1489 &dev_attr_uniq.attr,
1490 &dev_attr_modalias.attr,
1491 &dev_attr_properties.attr,
1492 &dev_attr_inhibited.attr,
1496 static const struct attribute_group input_dev_attr_group = {
1497 .attrs = input_dev_attrs,
1500 #define INPUT_DEV_ID_ATTR(name) \
1501 static ssize_t input_dev_show_id_##name(struct device *dev, \
1502 struct device_attribute *attr, \
1505 struct input_dev *input_dev = to_input_dev(dev); \
1506 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1508 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1510 INPUT_DEV_ID_ATTR(bustype);
1511 INPUT_DEV_ID_ATTR(vendor);
1512 INPUT_DEV_ID_ATTR(product);
1513 INPUT_DEV_ID_ATTR(version);
1515 static struct attribute *input_dev_id_attrs[] = {
1516 &dev_attr_bustype.attr,
1517 &dev_attr_vendor.attr,
1518 &dev_attr_product.attr,
1519 &dev_attr_version.attr,
1523 static const struct attribute_group input_dev_id_attr_group = {
1525 .attrs = input_dev_id_attrs,
1528 static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap,
1529 int max, int add_cr)
1533 bool skip_empty = true;
1535 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1536 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1537 bitmap[i], skip_empty);
1541 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1546 * If no output was produced print a single 0.
1549 len = snprintf(buf, buf_size, "%d", 0);
1552 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1557 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1558 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1559 struct device_attribute *attr, \
1562 struct input_dev *input_dev = to_input_dev(dev); \
1563 int len = input_print_bitmap(buf, PAGE_SIZE, \
1564 input_dev->bm##bit, ev##_MAX, \
1566 return min_t(int, len, PAGE_SIZE); \
1568 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1570 INPUT_DEV_CAP_ATTR(EV, ev);
1571 INPUT_DEV_CAP_ATTR(KEY, key);
1572 INPUT_DEV_CAP_ATTR(REL, rel);
1573 INPUT_DEV_CAP_ATTR(ABS, abs);
1574 INPUT_DEV_CAP_ATTR(MSC, msc);
1575 INPUT_DEV_CAP_ATTR(LED, led);
1576 INPUT_DEV_CAP_ATTR(SND, snd);
1577 INPUT_DEV_CAP_ATTR(FF, ff);
1578 INPUT_DEV_CAP_ATTR(SW, sw);
1580 static struct attribute *input_dev_caps_attrs[] = {
1593 static const struct attribute_group input_dev_caps_attr_group = {
1594 .name = "capabilities",
1595 .attrs = input_dev_caps_attrs,
1598 static const struct attribute_group *input_dev_attr_groups[] = {
1599 &input_dev_attr_group,
1600 &input_dev_id_attr_group,
1601 &input_dev_caps_attr_group,
1602 &input_poller_attribute_group,
1606 static void input_dev_release(struct device *device)
1608 struct input_dev *dev = to_input_dev(device);
1610 input_ff_destroy(dev);
1611 input_mt_destroy_slots(dev);
1613 kfree(dev->absinfo);
1617 module_put(THIS_MODULE);
1621 * Input uevent interface - loading event handlers based on
1624 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1625 const char *name, const unsigned long *bitmap, int max)
1629 if (add_uevent_var(env, "%s", name))
1632 len = input_print_bitmap(&env->buf[env->buflen - 1],
1633 sizeof(env->buf) - env->buflen,
1634 bitmap, max, false);
1635 if (len >= (sizeof(env->buf) - env->buflen))
1642 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1643 const struct input_dev *dev)
1647 if (add_uevent_var(env, "MODALIAS="))
1650 len = input_print_modalias(&env->buf[env->buflen - 1],
1651 sizeof(env->buf) - env->buflen,
1653 if (len >= (sizeof(env->buf) - env->buflen))
1660 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1662 int err = add_uevent_var(env, fmt, val); \
1667 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1669 int err = input_add_uevent_bm_var(env, name, bm, max); \
1674 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1676 int err = input_add_uevent_modalias_var(env, dev); \
1681 static int input_dev_uevent(const struct device *device, struct kobj_uevent_env *env)
1683 const struct input_dev *dev = to_input_dev(device);
1685 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1686 dev->id.bustype, dev->id.vendor,
1687 dev->id.product, dev->id.version);
1689 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1691 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1693 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1695 INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
1697 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1698 if (test_bit(EV_KEY, dev->evbit))
1699 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1700 if (test_bit(EV_REL, dev->evbit))
1701 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1702 if (test_bit(EV_ABS, dev->evbit))
1703 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1704 if (test_bit(EV_MSC, dev->evbit))
1705 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1706 if (test_bit(EV_LED, dev->evbit))
1707 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1708 if (test_bit(EV_SND, dev->evbit))
1709 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1710 if (test_bit(EV_FF, dev->evbit))
1711 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1712 if (test_bit(EV_SW, dev->evbit))
1713 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1715 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1720 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1725 if (!test_bit(EV_##type, dev->evbit)) \
1728 for_each_set_bit(i, dev->bits##bit, type##_CNT) { \
1729 active = test_bit(i, dev->bits); \
1730 if (!active && !on) \
1733 dev->event(dev, EV_##type, i, on ? active : 0); \
1737 static void input_dev_toggle(struct input_dev *dev, bool activate)
1742 INPUT_DO_TOGGLE(dev, LED, led, activate);
1743 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1745 if (activate && test_bit(EV_REP, dev->evbit)) {
1746 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1747 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1752 * input_reset_device() - reset/restore the state of input device
1753 * @dev: input device whose state needs to be reset
1755 * This function tries to reset the state of an opened input device and
1756 * bring internal state and state if the hardware in sync with each other.
1757 * We mark all keys as released, restore LED state, repeat rate, etc.
1759 void input_reset_device(struct input_dev *dev)
1761 unsigned long flags;
1763 mutex_lock(&dev->mutex);
1764 spin_lock_irqsave(&dev->event_lock, flags);
1766 input_dev_toggle(dev, true);
1767 if (input_dev_release_keys(dev))
1768 input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
1770 spin_unlock_irqrestore(&dev->event_lock, flags);
1771 mutex_unlock(&dev->mutex);
1773 EXPORT_SYMBOL(input_reset_device);
1775 static int input_inhibit_device(struct input_dev *dev)
1777 mutex_lock(&dev->mutex);
1786 input_dev_poller_stop(dev->poller);
1789 spin_lock_irq(&dev->event_lock);
1790 input_mt_release_slots(dev);
1791 input_dev_release_keys(dev);
1792 input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
1793 input_dev_toggle(dev, false);
1794 spin_unlock_irq(&dev->event_lock);
1796 dev->inhibited = true;
1799 mutex_unlock(&dev->mutex);
1803 static int input_uninhibit_device(struct input_dev *dev)
1807 mutex_lock(&dev->mutex);
1809 if (!dev->inhibited)
1814 ret = dev->open(dev);
1819 input_dev_poller_start(dev->poller);
1822 dev->inhibited = false;
1823 spin_lock_irq(&dev->event_lock);
1824 input_dev_toggle(dev, true);
1825 spin_unlock_irq(&dev->event_lock);
1828 mutex_unlock(&dev->mutex);
1832 static int input_dev_suspend(struct device *dev)
1834 struct input_dev *input_dev = to_input_dev(dev);
1836 spin_lock_irq(&input_dev->event_lock);
1839 * Keys that are pressed now are unlikely to be
1840 * still pressed when we resume.
1842 if (input_dev_release_keys(input_dev))
1843 input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1);
1845 /* Turn off LEDs and sounds, if any are active. */
1846 input_dev_toggle(input_dev, false);
1848 spin_unlock_irq(&input_dev->event_lock);
1853 static int input_dev_resume(struct device *dev)
1855 struct input_dev *input_dev = to_input_dev(dev);
1857 spin_lock_irq(&input_dev->event_lock);
1859 /* Restore state of LEDs and sounds, if any were active. */
1860 input_dev_toggle(input_dev, true);
1862 spin_unlock_irq(&input_dev->event_lock);
1867 static int input_dev_freeze(struct device *dev)
1869 struct input_dev *input_dev = to_input_dev(dev);
1871 spin_lock_irq(&input_dev->event_lock);
1874 * Keys that are pressed now are unlikely to be
1875 * still pressed when we resume.
1877 if (input_dev_release_keys(input_dev))
1878 input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1);
1880 spin_unlock_irq(&input_dev->event_lock);
1885 static int input_dev_poweroff(struct device *dev)
1887 struct input_dev *input_dev = to_input_dev(dev);
1889 spin_lock_irq(&input_dev->event_lock);
1891 /* Turn off LEDs and sounds, if any are active. */
1892 input_dev_toggle(input_dev, false);
1894 spin_unlock_irq(&input_dev->event_lock);
1899 static const struct dev_pm_ops input_dev_pm_ops = {
1900 .suspend = input_dev_suspend,
1901 .resume = input_dev_resume,
1902 .freeze = input_dev_freeze,
1903 .poweroff = input_dev_poweroff,
1904 .restore = input_dev_resume,
1907 static const struct device_type input_dev_type = {
1908 .groups = input_dev_attr_groups,
1909 .release = input_dev_release,
1910 .uevent = input_dev_uevent,
1911 .pm = pm_sleep_ptr(&input_dev_pm_ops),
1914 static char *input_devnode(const struct device *dev, umode_t *mode)
1916 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1919 struct class input_class = {
1921 .devnode = input_devnode,
1923 EXPORT_SYMBOL_GPL(input_class);
1926 * input_allocate_device - allocate memory for new input device
1928 * Returns prepared struct input_dev or %NULL.
1930 * NOTE: Use input_free_device() to free devices that have not been
1931 * registered; input_unregister_device() should be used for already
1932 * registered devices.
1934 struct input_dev *input_allocate_device(void)
1936 static atomic_t input_no = ATOMIC_INIT(-1);
1937 struct input_dev *dev;
1939 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1941 dev->dev.type = &input_dev_type;
1942 dev->dev.class = &input_class;
1943 device_initialize(&dev->dev);
1944 mutex_init(&dev->mutex);
1945 spin_lock_init(&dev->event_lock);
1946 timer_setup(&dev->timer, NULL, 0);
1947 INIT_LIST_HEAD(&dev->h_list);
1948 INIT_LIST_HEAD(&dev->node);
1950 dev_set_name(&dev->dev, "input%lu",
1951 (unsigned long)atomic_inc_return(&input_no));
1953 __module_get(THIS_MODULE);
1958 EXPORT_SYMBOL(input_allocate_device);
1960 struct input_devres {
1961 struct input_dev *input;
1964 static int devm_input_device_match(struct device *dev, void *res, void *data)
1966 struct input_devres *devres = res;
1968 return devres->input == data;
1971 static void devm_input_device_release(struct device *dev, void *res)
1973 struct input_devres *devres = res;
1974 struct input_dev *input = devres->input;
1976 dev_dbg(dev, "%s: dropping reference to %s\n",
1977 __func__, dev_name(&input->dev));
1978 input_put_device(input);
1982 * devm_input_allocate_device - allocate managed input device
1983 * @dev: device owning the input device being created
1985 * Returns prepared struct input_dev or %NULL.
1987 * Managed input devices do not need to be explicitly unregistered or
1988 * freed as it will be done automatically when owner device unbinds from
1989 * its driver (or binding fails). Once managed input device is allocated,
1990 * it is ready to be set up and registered in the same fashion as regular
1991 * input device. There are no special devm_input_device_[un]register()
1992 * variants, regular ones work with both managed and unmanaged devices,
1993 * should you need them. In most cases however, managed input device need
1994 * not be explicitly unregistered or freed.
1996 * NOTE: the owner device is set up as parent of input device and users
1997 * should not override it.
1999 struct input_dev *devm_input_allocate_device(struct device *dev)
2001 struct input_dev *input;
2002 struct input_devres *devres;
2004 devres = devres_alloc(devm_input_device_release,
2005 sizeof(*devres), GFP_KERNEL);
2009 input = input_allocate_device();
2011 devres_free(devres);
2015 input->dev.parent = dev;
2016 input->devres_managed = true;
2018 devres->input = input;
2019 devres_add(dev, devres);
2023 EXPORT_SYMBOL(devm_input_allocate_device);
2026 * input_free_device - free memory occupied by input_dev structure
2027 * @dev: input device to free
2029 * This function should only be used if input_register_device()
2030 * was not called yet or if it failed. Once device was registered
2031 * use input_unregister_device() and memory will be freed once last
2032 * reference to the device is dropped.
2034 * Device should be allocated by input_allocate_device().
2036 * NOTE: If there are references to the input device then memory
2037 * will not be freed until last reference is dropped.
2039 void input_free_device(struct input_dev *dev)
2042 if (dev->devres_managed)
2043 WARN_ON(devres_destroy(dev->dev.parent,
2044 devm_input_device_release,
2045 devm_input_device_match,
2047 input_put_device(dev);
2050 EXPORT_SYMBOL(input_free_device);
2053 * input_set_timestamp - set timestamp for input events
2054 * @dev: input device to set timestamp for
2055 * @timestamp: the time at which the event has occurred
2056 * in CLOCK_MONOTONIC
2058 * This function is intended to provide to the input system a more
2059 * accurate time of when an event actually occurred. The driver should
2060 * call this function as soon as a timestamp is acquired ensuring
2061 * clock conversions in input_set_timestamp are done correctly.
2063 * The system entering suspend state between timestamp acquisition and
2064 * calling input_set_timestamp can result in inaccurate conversions.
2066 void input_set_timestamp(struct input_dev *dev, ktime_t timestamp)
2068 dev->timestamp[INPUT_CLK_MONO] = timestamp;
2069 dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp);
2070 dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp,
2073 EXPORT_SYMBOL(input_set_timestamp);
2076 * input_get_timestamp - get timestamp for input events
2077 * @dev: input device to get timestamp from
2079 * A valid timestamp is a timestamp of non-zero value.
2081 ktime_t *input_get_timestamp(struct input_dev *dev)
2083 const ktime_t invalid_timestamp = ktime_set(0, 0);
2085 if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp))
2086 input_set_timestamp(dev, ktime_get());
2088 return dev->timestamp;
2090 EXPORT_SYMBOL(input_get_timestamp);
2093 * input_set_capability - mark device as capable of a certain event
2094 * @dev: device that is capable of emitting or accepting event
2095 * @type: type of the event (EV_KEY, EV_REL, etc...)
2098 * In addition to setting up corresponding bit in appropriate capability
2099 * bitmap the function also adjusts dev->evbit.
2101 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
2103 if (type < EV_CNT && input_max_code[type] &&
2104 code > input_max_code[type]) {
2105 pr_err("%s: invalid code %u for type %u\n", __func__, code,
2113 __set_bit(code, dev->keybit);
2117 __set_bit(code, dev->relbit);
2121 input_alloc_absinfo(dev);
2122 __set_bit(code, dev->absbit);
2126 __set_bit(code, dev->mscbit);
2130 __set_bit(code, dev->swbit);
2134 __set_bit(code, dev->ledbit);
2138 __set_bit(code, dev->sndbit);
2142 __set_bit(code, dev->ffbit);
2150 pr_err("%s: unknown type %u (code %u)\n", __func__, type, code);
2155 __set_bit(type, dev->evbit);
2157 EXPORT_SYMBOL(input_set_capability);
2159 static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
2163 unsigned int events;
2166 mt_slots = dev->mt->num_slots;
2167 } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
2168 mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
2169 dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
2170 mt_slots = clamp(mt_slots, 2, 32);
2171 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
2177 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
2179 if (test_bit(EV_ABS, dev->evbit))
2180 for_each_set_bit(i, dev->absbit, ABS_CNT)
2181 events += input_is_mt_axis(i) ? mt_slots : 1;
2183 if (test_bit(EV_REL, dev->evbit))
2184 events += bitmap_weight(dev->relbit, REL_CNT);
2186 /* Make room for KEY and MSC events */
2192 #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
2194 if (!test_bit(EV_##type, dev->evbit)) \
2195 memset(dev->bits##bit, 0, \
2196 sizeof(dev->bits##bit)); \
2199 static void input_cleanse_bitmasks(struct input_dev *dev)
2201 INPUT_CLEANSE_BITMASK(dev, KEY, key);
2202 INPUT_CLEANSE_BITMASK(dev, REL, rel);
2203 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
2204 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
2205 INPUT_CLEANSE_BITMASK(dev, LED, led);
2206 INPUT_CLEANSE_BITMASK(dev, SND, snd);
2207 INPUT_CLEANSE_BITMASK(dev, FF, ff);
2208 INPUT_CLEANSE_BITMASK(dev, SW, sw);
2211 static void __input_unregister_device(struct input_dev *dev)
2213 struct input_handle *handle, *next;
2215 input_disconnect_device(dev);
2217 mutex_lock(&input_mutex);
2219 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
2220 handle->handler->disconnect(handle);
2221 WARN_ON(!list_empty(&dev->h_list));
2223 del_timer_sync(&dev->timer);
2224 list_del_init(&dev->node);
2226 input_wakeup_procfs_readers();
2228 mutex_unlock(&input_mutex);
2230 device_del(&dev->dev);
2233 static void devm_input_device_unregister(struct device *dev, void *res)
2235 struct input_devres *devres = res;
2236 struct input_dev *input = devres->input;
2238 dev_dbg(dev, "%s: unregistering device %s\n",
2239 __func__, dev_name(&input->dev));
2240 __input_unregister_device(input);
2244 * Generate software autorepeat event. Note that we take
2245 * dev->event_lock here to avoid racing with input_event
2246 * which may cause keys get "stuck".
2248 static void input_repeat_key(struct timer_list *t)
2250 struct input_dev *dev = from_timer(dev, t, timer);
2251 unsigned long flags;
2253 spin_lock_irqsave(&dev->event_lock, flags);
2255 if (!dev->inhibited &&
2256 test_bit(dev->repeat_key, dev->key) &&
2257 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
2259 input_set_timestamp(dev, ktime_get());
2260 input_handle_event(dev, EV_KEY, dev->repeat_key, 2);
2261 input_handle_event(dev, EV_SYN, SYN_REPORT, 1);
2263 if (dev->rep[REP_PERIOD])
2264 mod_timer(&dev->timer, jiffies +
2265 msecs_to_jiffies(dev->rep[REP_PERIOD]));
2268 spin_unlock_irqrestore(&dev->event_lock, flags);
2272 * input_enable_softrepeat - enable software autorepeat
2273 * @dev: input device
2274 * @delay: repeat delay
2275 * @period: repeat period
2277 * Enable software autorepeat on the input device.
2279 void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
2281 dev->timer.function = input_repeat_key;
2282 dev->rep[REP_DELAY] = delay;
2283 dev->rep[REP_PERIOD] = period;
2285 EXPORT_SYMBOL(input_enable_softrepeat);
2287 bool input_device_enabled(struct input_dev *dev)
2289 lockdep_assert_held(&dev->mutex);
2291 return !dev->inhibited && dev->users > 0;
2293 EXPORT_SYMBOL_GPL(input_device_enabled);
2296 * input_register_device - register device with input core
2297 * @dev: device to be registered
2299 * This function registers device with input core. The device must be
2300 * allocated with input_allocate_device() and all it's capabilities
2301 * set up before registering.
2302 * If function fails the device must be freed with input_free_device().
2303 * Once device has been successfully registered it can be unregistered
2304 * with input_unregister_device(); input_free_device() should not be
2305 * called in this case.
2307 * Note that this function is also used to register managed input devices
2308 * (ones allocated with devm_input_allocate_device()). Such managed input
2309 * devices need not be explicitly unregistered or freed, their tear down
2310 * is controlled by the devres infrastructure. It is also worth noting
2311 * that tear down of managed input devices is internally a 2-step process:
2312 * registered managed input device is first unregistered, but stays in
2313 * memory and can still handle input_event() calls (although events will
2314 * not be delivered anywhere). The freeing of managed input device will
2315 * happen later, when devres stack is unwound to the point where device
2316 * allocation was made.
2318 int input_register_device(struct input_dev *dev)
2320 struct input_devres *devres = NULL;
2321 struct input_handler *handler;
2322 unsigned int packet_size;
2326 if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
2328 "Absolute device without dev->absinfo, refusing to register\n");
2332 if (dev->devres_managed) {
2333 devres = devres_alloc(devm_input_device_unregister,
2334 sizeof(*devres), GFP_KERNEL);
2338 devres->input = dev;
2341 /* Every input device generates EV_SYN/SYN_REPORT events. */
2342 __set_bit(EV_SYN, dev->evbit);
2344 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
2345 __clear_bit(KEY_RESERVED, dev->keybit);
2347 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
2348 input_cleanse_bitmasks(dev);
2350 packet_size = input_estimate_events_per_packet(dev);
2351 if (dev->hint_events_per_packet < packet_size)
2352 dev->hint_events_per_packet = packet_size;
2354 dev->max_vals = dev->hint_events_per_packet + 2;
2355 dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
2358 goto err_devres_free;
2362 * If delay and period are pre-set by the driver, then autorepeating
2363 * is handled by the driver itself and we don't do it in input.c.
2365 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
2366 input_enable_softrepeat(dev, 250, 33);
2368 if (!dev->getkeycode)
2369 dev->getkeycode = input_default_getkeycode;
2371 if (!dev->setkeycode)
2372 dev->setkeycode = input_default_setkeycode;
2375 input_dev_poller_finalize(dev->poller);
2377 error = device_add(&dev->dev);
2381 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
2382 pr_info("%s as %s\n",
2383 dev->name ? dev->name : "Unspecified device",
2384 path ? path : "N/A");
2387 error = mutex_lock_interruptible(&input_mutex);
2389 goto err_device_del;
2391 list_add_tail(&dev->node, &input_dev_list);
2393 list_for_each_entry(handler, &input_handler_list, node)
2394 input_attach_handler(dev, handler);
2396 input_wakeup_procfs_readers();
2398 mutex_unlock(&input_mutex);
2400 if (dev->devres_managed) {
2401 dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
2402 __func__, dev_name(&dev->dev));
2403 devres_add(dev->dev.parent, devres);
2408 device_del(&dev->dev);
2413 devres_free(devres);
2416 EXPORT_SYMBOL(input_register_device);
2419 * input_unregister_device - unregister previously registered device
2420 * @dev: device to be unregistered
2422 * This function unregisters an input device. Once device is unregistered
2423 * the caller should not try to access it as it may get freed at any moment.
2425 void input_unregister_device(struct input_dev *dev)
2427 if (dev->devres_managed) {
2428 WARN_ON(devres_destroy(dev->dev.parent,
2429 devm_input_device_unregister,
2430 devm_input_device_match,
2432 __input_unregister_device(dev);
2434 * We do not do input_put_device() here because it will be done
2435 * when 2nd devres fires up.
2438 __input_unregister_device(dev);
2439 input_put_device(dev);
2442 EXPORT_SYMBOL(input_unregister_device);
2445 * input_register_handler - register a new input handler
2446 * @handler: handler to be registered
2448 * This function registers a new input handler (interface) for input
2449 * devices in the system and attaches it to all input devices that
2450 * are compatible with the handler.
2452 int input_register_handler(struct input_handler *handler)
2454 struct input_dev *dev;
2457 error = mutex_lock_interruptible(&input_mutex);
2461 INIT_LIST_HEAD(&handler->h_list);
2463 list_add_tail(&handler->node, &input_handler_list);
2465 list_for_each_entry(dev, &input_dev_list, node)
2466 input_attach_handler(dev, handler);
2468 input_wakeup_procfs_readers();
2470 mutex_unlock(&input_mutex);
2473 EXPORT_SYMBOL(input_register_handler);
2476 * input_unregister_handler - unregisters an input handler
2477 * @handler: handler to be unregistered
2479 * This function disconnects a handler from its input devices and
2480 * removes it from lists of known handlers.
2482 void input_unregister_handler(struct input_handler *handler)
2484 struct input_handle *handle, *next;
2486 mutex_lock(&input_mutex);
2488 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
2489 handler->disconnect(handle);
2490 WARN_ON(!list_empty(&handler->h_list));
2492 list_del_init(&handler->node);
2494 input_wakeup_procfs_readers();
2496 mutex_unlock(&input_mutex);
2498 EXPORT_SYMBOL(input_unregister_handler);
2501 * input_handler_for_each_handle - handle iterator
2502 * @handler: input handler to iterate
2503 * @data: data for the callback
2504 * @fn: function to be called for each handle
2506 * Iterate over @bus's list of devices, and call @fn for each, passing
2507 * it @data and stop when @fn returns a non-zero value. The function is
2508 * using RCU to traverse the list and therefore may be using in atomic
2509 * contexts. The @fn callback is invoked from RCU critical section and
2510 * thus must not sleep.
2512 int input_handler_for_each_handle(struct input_handler *handler, void *data,
2513 int (*fn)(struct input_handle *, void *))
2515 struct input_handle *handle;
2520 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
2521 retval = fn(handle, data);
2530 EXPORT_SYMBOL(input_handler_for_each_handle);
2533 * input_register_handle - register a new input handle
2534 * @handle: handle to register
2536 * This function puts a new input handle onto device's
2537 * and handler's lists so that events can flow through
2538 * it once it is opened using input_open_device().
2540 * This function is supposed to be called from handler's
2543 int input_register_handle(struct input_handle *handle)
2545 struct input_handler *handler = handle->handler;
2546 struct input_dev *dev = handle->dev;
2550 * We take dev->mutex here to prevent race with
2551 * input_release_device().
2553 error = mutex_lock_interruptible(&dev->mutex);
2558 * Filters go to the head of the list, normal handlers
2561 if (handler->filter)
2562 list_add_rcu(&handle->d_node, &dev->h_list);
2564 list_add_tail_rcu(&handle->d_node, &dev->h_list);
2566 mutex_unlock(&dev->mutex);
2569 * Since we are supposed to be called from ->connect()
2570 * which is mutually exclusive with ->disconnect()
2571 * we can't be racing with input_unregister_handle()
2572 * and so separate lock is not needed here.
2574 list_add_tail_rcu(&handle->h_node, &handler->h_list);
2577 handler->start(handle);
2581 EXPORT_SYMBOL(input_register_handle);
2584 * input_unregister_handle - unregister an input handle
2585 * @handle: handle to unregister
2587 * This function removes input handle from device's
2588 * and handler's lists.
2590 * This function is supposed to be called from handler's
2591 * disconnect() method.
2593 void input_unregister_handle(struct input_handle *handle)
2595 struct input_dev *dev = handle->dev;
2597 list_del_rcu(&handle->h_node);
2600 * Take dev->mutex to prevent race with input_release_device().
2602 mutex_lock(&dev->mutex);
2603 list_del_rcu(&handle->d_node);
2604 mutex_unlock(&dev->mutex);
2608 EXPORT_SYMBOL(input_unregister_handle);
2611 * input_get_new_minor - allocates a new input minor number
2612 * @legacy_base: beginning or the legacy range to be searched
2613 * @legacy_num: size of legacy range
2614 * @allow_dynamic: whether we can also take ID from the dynamic range
2616 * This function allocates a new device minor for from input major namespace.
2617 * Caller can request legacy minor by specifying @legacy_base and @legacy_num
2618 * parameters and whether ID can be allocated from dynamic range if there are
2619 * no free IDs in legacy range.
2621 int input_get_new_minor(int legacy_base, unsigned int legacy_num,
2625 * This function should be called from input handler's ->connect()
2626 * methods, which are serialized with input_mutex, so no additional
2627 * locking is needed here.
2629 if (legacy_base >= 0) {
2630 int minor = ida_simple_get(&input_ida,
2632 legacy_base + legacy_num,
2634 if (minor >= 0 || !allow_dynamic)
2638 return ida_simple_get(&input_ida,
2639 INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
2642 EXPORT_SYMBOL(input_get_new_minor);
2645 * input_free_minor - release previously allocated minor
2646 * @minor: minor to be released
2648 * This function releases previously allocated input minor so that it can be
2651 void input_free_minor(unsigned int minor)
2653 ida_simple_remove(&input_ida, minor);
2655 EXPORT_SYMBOL(input_free_minor);
2657 static int __init input_init(void)
2661 err = class_register(&input_class);
2663 pr_err("unable to register input_dev class\n");
2667 err = input_proc_init();
2671 err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2672 INPUT_MAX_CHAR_DEVICES, "input");
2674 pr_err("unable to register char major %d", INPUT_MAJOR);
2680 fail2: input_proc_exit();
2681 fail1: class_unregister(&input_class);
2685 static void __exit input_exit(void)
2688 unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2689 INPUT_MAX_CHAR_DEVICES);
2690 class_unregister(&input_class);
2693 subsys_initcall(input_init);
2694 module_exit(input_exit);