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 #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/idr.h>
18 #include <linux/input/mt.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/random.h>
22 #include <linux/major.h>
23 #include <linux/proc_fs.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/poll.h>
27 #include <linux/device.h>
28 #include <linux/mutex.h>
29 #include <linux/rcupdate.h>
30 #include "input-compat.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] &&
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;
153 handle = rcu_dereference(dev->grab);
155 count = input_to_handler(handle, vals, count);
157 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
159 count = input_to_handler(handle, vals, count);
167 /* trigger auto repeat for key events */
168 if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
169 for (v = vals; v != vals + count; v++) {
170 if (v->type == EV_KEY && v->value != 2) {
172 input_start_autorepeat(dev, v->code);
174 input_stop_autorepeat(dev);
180 static void input_pass_event(struct input_dev *dev,
181 unsigned int type, unsigned int code, int value)
183 struct input_value vals[] = { { type, code, value } };
185 input_pass_values(dev, vals, ARRAY_SIZE(vals));
189 * Generate software autorepeat event. Note that we take
190 * dev->event_lock here to avoid racing with input_event
191 * which may cause keys get "stuck".
193 static void input_repeat_key(unsigned long data)
195 struct input_dev *dev = (void *) data;
198 spin_lock_irqsave(&dev->event_lock, flags);
200 if (test_bit(dev->repeat_key, dev->key) &&
201 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
202 struct input_value vals[] = {
203 { EV_KEY, dev->repeat_key, 2 },
207 input_pass_values(dev, vals, ARRAY_SIZE(vals));
209 if (dev->rep[REP_PERIOD])
210 mod_timer(&dev->timer, jiffies +
211 msecs_to_jiffies(dev->rep[REP_PERIOD]));
214 spin_unlock_irqrestore(&dev->event_lock, flags);
217 #define INPUT_IGNORE_EVENT 0
218 #define INPUT_PASS_TO_HANDLERS 1
219 #define INPUT_PASS_TO_DEVICE 2
221 #define INPUT_FLUSH 8
222 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
224 static int input_handle_abs_event(struct input_dev *dev,
225 unsigned int code, int *pval)
227 struct input_mt *mt = dev->mt;
231 if (code == ABS_MT_SLOT) {
233 * "Stage" the event; we'll flush it later, when we
234 * get actual touch data.
236 if (mt && *pval >= 0 && *pval < mt->num_slots)
239 return INPUT_IGNORE_EVENT;
242 is_mt_event = input_is_mt_value(code);
245 pold = &dev->absinfo[code].value;
247 pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
250 * Bypass filtering for multi-touch events when
251 * not employing slots.
257 *pval = input_defuzz_abs_event(*pval, *pold,
258 dev->absinfo[code].fuzz);
260 return INPUT_IGNORE_EVENT;
265 /* Flush pending "slot" event */
266 if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
267 input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
268 return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
271 return INPUT_PASS_TO_HANDLERS;
274 static int input_get_disposition(struct input_dev *dev,
275 unsigned int type, unsigned int code, int *pval)
277 int disposition = INPUT_IGNORE_EVENT;
285 disposition = INPUT_PASS_TO_ALL;
289 disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
292 disposition = INPUT_PASS_TO_HANDLERS;
298 if (is_event_supported(code, dev->keybit, KEY_MAX)) {
300 /* auto-repeat bypasses state updates */
302 disposition = INPUT_PASS_TO_HANDLERS;
306 if (!!test_bit(code, dev->key) != !!value) {
308 __change_bit(code, dev->key);
309 disposition = INPUT_PASS_TO_HANDLERS;
315 if (is_event_supported(code, dev->swbit, SW_MAX) &&
316 !!test_bit(code, dev->sw) != !!value) {
318 __change_bit(code, dev->sw);
319 disposition = INPUT_PASS_TO_HANDLERS;
324 if (is_event_supported(code, dev->absbit, ABS_MAX))
325 disposition = input_handle_abs_event(dev, code, &value);
330 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
331 disposition = INPUT_PASS_TO_HANDLERS;
336 if (is_event_supported(code, dev->mscbit, MSC_MAX))
337 disposition = INPUT_PASS_TO_ALL;
342 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
343 !!test_bit(code, dev->led) != !!value) {
345 __change_bit(code, dev->led);
346 disposition = INPUT_PASS_TO_ALL;
351 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
353 if (!!test_bit(code, dev->snd) != !!value)
354 __change_bit(code, dev->snd);
355 disposition = INPUT_PASS_TO_ALL;
360 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
361 dev->rep[code] = value;
362 disposition = INPUT_PASS_TO_ALL;
368 disposition = INPUT_PASS_TO_ALL;
372 disposition = INPUT_PASS_TO_ALL;
380 static void input_handle_event(struct input_dev *dev,
381 unsigned int type, unsigned int code, int value)
383 int disposition = input_get_disposition(dev, type, code, &value);
385 if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
386 add_input_randomness(type, code, value);
388 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
389 dev->event(dev, type, code, value);
394 if (disposition & INPUT_PASS_TO_HANDLERS) {
395 struct input_value *v;
397 if (disposition & INPUT_SLOT) {
398 v = &dev->vals[dev->num_vals++];
400 v->code = ABS_MT_SLOT;
401 v->value = dev->mt->slot;
404 v = &dev->vals[dev->num_vals++];
410 if (disposition & INPUT_FLUSH) {
411 if (dev->num_vals >= 2)
412 input_pass_values(dev, dev->vals, dev->num_vals);
414 } else if (dev->num_vals >= dev->max_vals - 2) {
415 dev->vals[dev->num_vals++] = input_value_sync;
416 input_pass_values(dev, dev->vals, dev->num_vals);
423 * input_event() - report new input event
424 * @dev: device that generated the event
425 * @type: type of the event
427 * @value: value of the event
429 * This function should be used by drivers implementing various input
430 * devices to report input events. See also input_inject_event().
432 * NOTE: input_event() may be safely used right after input device was
433 * allocated with input_allocate_device(), even before it is registered
434 * with input_register_device(), but the event will not reach any of the
435 * input handlers. Such early invocation of input_event() may be used
436 * to 'seed' initial state of a switch or initial position of absolute
439 void input_event(struct input_dev *dev,
440 unsigned int type, unsigned int code, int value)
444 if (is_event_supported(type, dev->evbit, EV_MAX)) {
446 spin_lock_irqsave(&dev->event_lock, flags);
447 input_handle_event(dev, type, code, value);
448 spin_unlock_irqrestore(&dev->event_lock, flags);
451 EXPORT_SYMBOL(input_event);
454 * input_inject_event() - send input event from input handler
455 * @handle: input handle to send event through
456 * @type: type of the event
458 * @value: value of the event
460 * Similar to input_event() but will ignore event if device is
461 * "grabbed" and handle injecting event is not the one that owns
464 void input_inject_event(struct input_handle *handle,
465 unsigned int type, unsigned int code, int value)
467 struct input_dev *dev = handle->dev;
468 struct input_handle *grab;
471 if (is_event_supported(type, dev->evbit, EV_MAX)) {
472 spin_lock_irqsave(&dev->event_lock, flags);
475 grab = rcu_dereference(dev->grab);
476 if (!grab || grab == handle)
477 input_handle_event(dev, type, code, value);
480 spin_unlock_irqrestore(&dev->event_lock, flags);
483 EXPORT_SYMBOL(input_inject_event);
486 * input_alloc_absinfo - allocates array of input_absinfo structs
487 * @dev: the input device emitting absolute events
489 * If the absinfo struct the caller asked for is already allocated, this
490 * functions will not do anything.
492 void input_alloc_absinfo(struct input_dev *dev)
497 dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL);
499 dev_err(dev->dev.parent ?: &dev->dev,
500 "%s: unable to allocate memory\n", __func__);
502 * We will handle this allocation failure in
503 * input_register_device() when we refuse to register input
504 * device with ABS bits but without absinfo.
508 EXPORT_SYMBOL(input_alloc_absinfo);
510 void input_set_abs_params(struct input_dev *dev, unsigned int axis,
511 int min, int max, int fuzz, int flat)
513 struct input_absinfo *absinfo;
515 input_alloc_absinfo(dev);
519 absinfo = &dev->absinfo[axis];
520 absinfo->minimum = min;
521 absinfo->maximum = max;
522 absinfo->fuzz = fuzz;
523 absinfo->flat = flat;
525 __set_bit(EV_ABS, dev->evbit);
526 __set_bit(axis, dev->absbit);
528 EXPORT_SYMBOL(input_set_abs_params);
532 * input_grab_device - grabs device for exclusive use
533 * @handle: input handle that wants to own the device
535 * When a device is grabbed by an input handle all events generated by
536 * the device are delivered only to this handle. Also events injected
537 * by other input handles are ignored while device is grabbed.
539 int input_grab_device(struct input_handle *handle)
541 struct input_dev *dev = handle->dev;
544 retval = mutex_lock_interruptible(&dev->mutex);
553 rcu_assign_pointer(dev->grab, handle);
556 mutex_unlock(&dev->mutex);
559 EXPORT_SYMBOL(input_grab_device);
561 static void __input_release_device(struct input_handle *handle)
563 struct input_dev *dev = handle->dev;
564 struct input_handle *grabber;
566 grabber = rcu_dereference_protected(dev->grab,
567 lockdep_is_held(&dev->mutex));
568 if (grabber == handle) {
569 rcu_assign_pointer(dev->grab, NULL);
570 /* Make sure input_pass_event() notices that grab is gone */
573 list_for_each_entry(handle, &dev->h_list, d_node)
574 if (handle->open && handle->handler->start)
575 handle->handler->start(handle);
580 * input_release_device - release previously grabbed device
581 * @handle: input handle that owns the device
583 * Releases previously grabbed device so that other input handles can
584 * start receiving input events. Upon release all handlers attached
585 * to the device have their start() method called so they have a change
586 * to synchronize device state with the rest of the system.
588 void input_release_device(struct input_handle *handle)
590 struct input_dev *dev = handle->dev;
592 mutex_lock(&dev->mutex);
593 __input_release_device(handle);
594 mutex_unlock(&dev->mutex);
596 EXPORT_SYMBOL(input_release_device);
599 * input_open_device - open input device
600 * @handle: handle through which device is being accessed
602 * This function should be called by input handlers when they
603 * want to start receive events from given input device.
605 int input_open_device(struct input_handle *handle)
607 struct input_dev *dev = handle->dev;
610 retval = mutex_lock_interruptible(&dev->mutex);
614 if (dev->going_away) {
621 if (!dev->users++ && dev->open)
622 retval = dev->open(dev);
626 if (!--handle->open) {
628 * Make sure we are not delivering any more events
629 * through this handle
636 mutex_unlock(&dev->mutex);
639 EXPORT_SYMBOL(input_open_device);
641 int input_flush_device(struct input_handle *handle, struct file *file)
643 struct input_dev *dev = handle->dev;
646 retval = mutex_lock_interruptible(&dev->mutex);
651 retval = dev->flush(dev, file);
653 mutex_unlock(&dev->mutex);
656 EXPORT_SYMBOL(input_flush_device);
659 * input_close_device - close input device
660 * @handle: handle through which device is being accessed
662 * This function should be called by input handlers when they
663 * want to stop receive events from given input device.
665 void input_close_device(struct input_handle *handle)
667 struct input_dev *dev = handle->dev;
669 mutex_lock(&dev->mutex);
671 __input_release_device(handle);
673 if (!--dev->users && dev->close)
676 if (!--handle->open) {
678 * synchronize_rcu() makes sure that input_pass_event()
679 * completed and that no more input events are delivered
680 * through this handle
685 mutex_unlock(&dev->mutex);
687 EXPORT_SYMBOL(input_close_device);
690 * Simulate keyup events for all keys that are marked as pressed.
691 * The function must be called with dev->event_lock held.
693 static void input_dev_release_keys(struct input_dev *dev)
695 bool need_sync = false;
698 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
699 for_each_set_bit(code, dev->key, KEY_CNT) {
700 input_pass_event(dev, EV_KEY, code, 0);
705 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
707 memset(dev->key, 0, sizeof(dev->key));
712 * Prepare device for unregistering
714 static void input_disconnect_device(struct input_dev *dev)
716 struct input_handle *handle;
719 * Mark device as going away. Note that we take dev->mutex here
720 * not to protect access to dev->going_away but rather to ensure
721 * that there are no threads in the middle of input_open_device()
723 mutex_lock(&dev->mutex);
724 dev->going_away = true;
725 mutex_unlock(&dev->mutex);
727 spin_lock_irq(&dev->event_lock);
730 * Simulate keyup events for all pressed keys so that handlers
731 * are not left with "stuck" keys. The driver may continue
732 * generate events even after we done here but they will not
733 * reach any handlers.
735 input_dev_release_keys(dev);
737 list_for_each_entry(handle, &dev->h_list, d_node)
740 spin_unlock_irq(&dev->event_lock);
744 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
745 * @ke: keymap entry containing scancode to be converted.
746 * @scancode: pointer to the location where converted scancode should
749 * This function is used to convert scancode stored in &struct keymap_entry
750 * into scalar form understood by legacy keymap handling methods. These
751 * methods expect scancodes to be represented as 'unsigned int'.
753 int input_scancode_to_scalar(const struct input_keymap_entry *ke,
754 unsigned int *scancode)
758 *scancode = *((u8 *)ke->scancode);
762 *scancode = *((u16 *)ke->scancode);
766 *scancode = *((u32 *)ke->scancode);
775 EXPORT_SYMBOL(input_scancode_to_scalar);
778 * Those routines handle the default case where no [gs]etkeycode() is
779 * defined. In this case, an array indexed by the scancode is used.
782 static unsigned int input_fetch_keycode(struct input_dev *dev,
785 switch (dev->keycodesize) {
787 return ((u8 *)dev->keycode)[index];
790 return ((u16 *)dev->keycode)[index];
793 return ((u32 *)dev->keycode)[index];
797 static int input_default_getkeycode(struct input_dev *dev,
798 struct input_keymap_entry *ke)
803 if (!dev->keycodesize)
806 if (ke->flags & INPUT_KEYMAP_BY_INDEX)
809 error = input_scancode_to_scalar(ke, &index);
814 if (index >= dev->keycodemax)
817 ke->keycode = input_fetch_keycode(dev, index);
819 ke->len = sizeof(index);
820 memcpy(ke->scancode, &index, sizeof(index));
825 static int input_default_setkeycode(struct input_dev *dev,
826 const struct input_keymap_entry *ke,
827 unsigned int *old_keycode)
833 if (!dev->keycodesize)
836 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
839 error = input_scancode_to_scalar(ke, &index);
844 if (index >= dev->keycodemax)
847 if (dev->keycodesize < sizeof(ke->keycode) &&
848 (ke->keycode >> (dev->keycodesize * 8)))
851 switch (dev->keycodesize) {
853 u8 *k = (u8 *)dev->keycode;
854 *old_keycode = k[index];
855 k[index] = ke->keycode;
859 u16 *k = (u16 *)dev->keycode;
860 *old_keycode = k[index];
861 k[index] = ke->keycode;
865 u32 *k = (u32 *)dev->keycode;
866 *old_keycode = k[index];
867 k[index] = ke->keycode;
872 if (*old_keycode <= KEY_MAX) {
873 __clear_bit(*old_keycode, dev->keybit);
874 for (i = 0; i < dev->keycodemax; i++) {
875 if (input_fetch_keycode(dev, i) == *old_keycode) {
876 __set_bit(*old_keycode, dev->keybit);
877 /* Setting the bit twice is useless, so break */
883 __set_bit(ke->keycode, dev->keybit);
888 * input_get_keycode - retrieve keycode currently mapped to a given scancode
889 * @dev: input device which keymap is being queried
892 * This function should be called by anyone interested in retrieving current
893 * keymap. Presently evdev handlers use it.
895 int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
900 spin_lock_irqsave(&dev->event_lock, flags);
901 retval = dev->getkeycode(dev, ke);
902 spin_unlock_irqrestore(&dev->event_lock, flags);
906 EXPORT_SYMBOL(input_get_keycode);
909 * input_set_keycode - attribute a keycode to a given scancode
910 * @dev: input device which keymap is being updated
911 * @ke: new keymap entry
913 * This function should be called by anyone needing to update current
914 * keymap. Presently keyboard and evdev handlers use it.
916 int input_set_keycode(struct input_dev *dev,
917 const struct input_keymap_entry *ke)
920 unsigned int old_keycode;
923 if (ke->keycode > KEY_MAX)
926 spin_lock_irqsave(&dev->event_lock, flags);
928 retval = dev->setkeycode(dev, ke, &old_keycode);
932 /* Make sure KEY_RESERVED did not get enabled. */
933 __clear_bit(KEY_RESERVED, dev->keybit);
936 * Simulate keyup event if keycode is not present
937 * in the keymap anymore
939 if (old_keycode > KEY_MAX) {
940 dev_warn(dev->dev.parent ?: &dev->dev,
941 "%s: got too big old keycode %#x\n",
942 __func__, old_keycode);
943 } else if (test_bit(EV_KEY, dev->evbit) &&
944 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
945 __test_and_clear_bit(old_keycode, dev->key)) {
946 struct input_value vals[] = {
947 { EV_KEY, old_keycode, 0 },
951 input_pass_values(dev, vals, ARRAY_SIZE(vals));
955 spin_unlock_irqrestore(&dev->event_lock, flags);
959 EXPORT_SYMBOL(input_set_keycode);
961 bool input_match_device_id(const struct input_dev *dev,
962 const struct input_device_id *id)
964 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
965 if (id->bustype != dev->id.bustype)
968 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
969 if (id->vendor != dev->id.vendor)
972 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
973 if (id->product != dev->id.product)
976 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
977 if (id->version != dev->id.version)
980 if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
981 !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
982 !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
983 !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
984 !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
985 !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
986 !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
987 !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
988 !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
989 !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
995 EXPORT_SYMBOL(input_match_device_id);
997 static const struct input_device_id *input_match_device(struct input_handler *handler,
998 struct input_dev *dev)
1000 const struct input_device_id *id;
1002 for (id = handler->id_table; id->flags || id->driver_info; id++) {
1003 if (input_match_device_id(dev, id) &&
1004 (!handler->match || handler->match(handler, dev))) {
1012 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
1014 const struct input_device_id *id;
1017 id = input_match_device(handler, dev);
1021 error = handler->connect(handler, dev, id);
1022 if (error && error != -ENODEV)
1023 pr_err("failed to attach handler %s to device %s, error: %d\n",
1024 handler->name, kobject_name(&dev->dev.kobj), error);
1029 #ifdef CONFIG_COMPAT
1031 static int input_bits_to_string(char *buf, int buf_size,
1032 unsigned long bits, bool skip_empty)
1036 if (in_compat_syscall()) {
1037 u32 dword = bits >> 32;
1038 if (dword || !skip_empty)
1039 len += snprintf(buf, buf_size, "%x ", dword);
1041 dword = bits & 0xffffffffUL;
1042 if (dword || !skip_empty || len)
1043 len += snprintf(buf + len, max(buf_size - len, 0),
1046 if (bits || !skip_empty)
1047 len += snprintf(buf, buf_size, "%lx", bits);
1053 #else /* !CONFIG_COMPAT */
1055 static int input_bits_to_string(char *buf, int buf_size,
1056 unsigned long bits, bool skip_empty)
1058 return bits || !skip_empty ?
1059 snprintf(buf, buf_size, "%lx", bits) : 0;
1064 #ifdef CONFIG_PROC_FS
1066 static struct proc_dir_entry *proc_bus_input_dir;
1067 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
1068 static int input_devices_state;
1070 static inline void input_wakeup_procfs_readers(void)
1072 input_devices_state++;
1073 wake_up(&input_devices_poll_wait);
1076 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
1078 poll_wait(file, &input_devices_poll_wait, wait);
1079 if (file->f_version != input_devices_state) {
1080 file->f_version = input_devices_state;
1081 return POLLIN | POLLRDNORM;
1087 union input_seq_state {
1090 bool mutex_acquired;
1095 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
1097 union input_seq_state *state = (union input_seq_state *)&seq->private;
1100 /* We need to fit into seq->private pointer */
1101 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1103 error = mutex_lock_interruptible(&input_mutex);
1105 state->mutex_acquired = false;
1106 return ERR_PTR(error);
1109 state->mutex_acquired = true;
1111 return seq_list_start(&input_dev_list, *pos);
1114 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1116 return seq_list_next(v, &input_dev_list, pos);
1119 static void input_seq_stop(struct seq_file *seq, void *v)
1121 union input_seq_state *state = (union input_seq_state *)&seq->private;
1123 if (state->mutex_acquired)
1124 mutex_unlock(&input_mutex);
1127 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
1128 unsigned long *bitmap, int max)
1131 bool skip_empty = true;
1134 seq_printf(seq, "B: %s=", name);
1136 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1137 if (input_bits_to_string(buf, sizeof(buf),
1138 bitmap[i], skip_empty)) {
1140 seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
1145 * If no output was produced print a single 0.
1150 seq_putc(seq, '\n');
1153 static int input_devices_seq_show(struct seq_file *seq, void *v)
1155 struct input_dev *dev = container_of(v, struct input_dev, node);
1156 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1157 struct input_handle *handle;
1159 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
1160 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
1162 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
1163 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
1164 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
1165 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
1166 seq_puts(seq, "H: Handlers=");
1168 list_for_each_entry(handle, &dev->h_list, d_node)
1169 seq_printf(seq, "%s ", handle->name);
1170 seq_putc(seq, '\n');
1172 input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
1174 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
1175 if (test_bit(EV_KEY, dev->evbit))
1176 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
1177 if (test_bit(EV_REL, dev->evbit))
1178 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
1179 if (test_bit(EV_ABS, dev->evbit))
1180 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
1181 if (test_bit(EV_MSC, dev->evbit))
1182 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
1183 if (test_bit(EV_LED, dev->evbit))
1184 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
1185 if (test_bit(EV_SND, dev->evbit))
1186 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
1187 if (test_bit(EV_FF, dev->evbit))
1188 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
1189 if (test_bit(EV_SW, dev->evbit))
1190 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
1192 seq_putc(seq, '\n');
1198 static const struct seq_operations input_devices_seq_ops = {
1199 .start = input_devices_seq_start,
1200 .next = input_devices_seq_next,
1201 .stop = input_seq_stop,
1202 .show = input_devices_seq_show,
1205 static int input_proc_devices_open(struct inode *inode, struct file *file)
1207 return seq_open(file, &input_devices_seq_ops);
1210 static const struct file_operations input_devices_fileops = {
1211 .owner = THIS_MODULE,
1212 .open = input_proc_devices_open,
1213 .poll = input_proc_devices_poll,
1215 .llseek = seq_lseek,
1216 .release = seq_release,
1219 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
1221 union input_seq_state *state = (union input_seq_state *)&seq->private;
1224 /* We need to fit into seq->private pointer */
1225 BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
1227 error = mutex_lock_interruptible(&input_mutex);
1229 state->mutex_acquired = false;
1230 return ERR_PTR(error);
1233 state->mutex_acquired = true;
1236 return seq_list_start(&input_handler_list, *pos);
1239 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1241 union input_seq_state *state = (union input_seq_state *)&seq->private;
1243 state->pos = *pos + 1;
1244 return seq_list_next(v, &input_handler_list, pos);
1247 static int input_handlers_seq_show(struct seq_file *seq, void *v)
1249 struct input_handler *handler = container_of(v, struct input_handler, node);
1250 union input_seq_state *state = (union input_seq_state *)&seq->private;
1252 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
1253 if (handler->filter)
1254 seq_puts(seq, " (filter)");
1255 if (handler->legacy_minors)
1256 seq_printf(seq, " Minor=%d", handler->minor);
1257 seq_putc(seq, '\n');
1262 static const struct seq_operations input_handlers_seq_ops = {
1263 .start = input_handlers_seq_start,
1264 .next = input_handlers_seq_next,
1265 .stop = input_seq_stop,
1266 .show = input_handlers_seq_show,
1269 static int input_proc_handlers_open(struct inode *inode, struct file *file)
1271 return seq_open(file, &input_handlers_seq_ops);
1274 static const struct file_operations input_handlers_fileops = {
1275 .owner = THIS_MODULE,
1276 .open = input_proc_handlers_open,
1278 .llseek = seq_lseek,
1279 .release = seq_release,
1282 static int __init input_proc_init(void)
1284 struct proc_dir_entry *entry;
1286 proc_bus_input_dir = proc_mkdir("bus/input", NULL);
1287 if (!proc_bus_input_dir)
1290 entry = proc_create("devices", 0, proc_bus_input_dir,
1291 &input_devices_fileops);
1295 entry = proc_create("handlers", 0, proc_bus_input_dir,
1296 &input_handlers_fileops);
1302 fail2: remove_proc_entry("devices", proc_bus_input_dir);
1303 fail1: remove_proc_entry("bus/input", NULL);
1307 static void input_proc_exit(void)
1309 remove_proc_entry("devices", proc_bus_input_dir);
1310 remove_proc_entry("handlers", proc_bus_input_dir);
1311 remove_proc_entry("bus/input", NULL);
1314 #else /* !CONFIG_PROC_FS */
1315 static inline void input_wakeup_procfs_readers(void) { }
1316 static inline int input_proc_init(void) { return 0; }
1317 static inline void input_proc_exit(void) { }
1320 #define INPUT_DEV_STRING_ATTR_SHOW(name) \
1321 static ssize_t input_dev_show_##name(struct device *dev, \
1322 struct device_attribute *attr, \
1325 struct input_dev *input_dev = to_input_dev(dev); \
1327 return scnprintf(buf, PAGE_SIZE, "%s\n", \
1328 input_dev->name ? input_dev->name : ""); \
1330 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
1332 INPUT_DEV_STRING_ATTR_SHOW(name);
1333 INPUT_DEV_STRING_ATTR_SHOW(phys);
1334 INPUT_DEV_STRING_ATTR_SHOW(uniq);
1336 static int input_print_modalias_bits(char *buf, int size,
1337 char name, unsigned long *bm,
1338 unsigned int min_bit, unsigned int max_bit)
1342 len += snprintf(buf, max(size, 0), "%c", name);
1343 for (i = min_bit; i < max_bit; i++)
1344 if (bm[BIT_WORD(i)] & BIT_MASK(i))
1345 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
1349 static int input_print_modalias(char *buf, int size, struct input_dev *id,
1354 len = snprintf(buf, max(size, 0),
1355 "input:b%04Xv%04Xp%04Xe%04X-",
1356 id->id.bustype, id->id.vendor,
1357 id->id.product, id->id.version);
1359 len += input_print_modalias_bits(buf + len, size - len,
1360 'e', id->evbit, 0, EV_MAX);
1361 len += input_print_modalias_bits(buf + len, size - len,
1362 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
1363 len += input_print_modalias_bits(buf + len, size - len,
1364 'r', id->relbit, 0, REL_MAX);
1365 len += input_print_modalias_bits(buf + len, size - len,
1366 'a', id->absbit, 0, ABS_MAX);
1367 len += input_print_modalias_bits(buf + len, size - len,
1368 'm', id->mscbit, 0, MSC_MAX);
1369 len += input_print_modalias_bits(buf + len, size - len,
1370 'l', id->ledbit, 0, LED_MAX);
1371 len += input_print_modalias_bits(buf + len, size - len,
1372 's', id->sndbit, 0, SND_MAX);
1373 len += input_print_modalias_bits(buf + len, size - len,
1374 'f', id->ffbit, 0, FF_MAX);
1375 len += input_print_modalias_bits(buf + len, size - len,
1376 'w', id->swbit, 0, SW_MAX);
1379 len += snprintf(buf + len, max(size - len, 0), "\n");
1384 static ssize_t input_dev_show_modalias(struct device *dev,
1385 struct device_attribute *attr,
1388 struct input_dev *id = to_input_dev(dev);
1391 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
1393 return min_t(int, len, PAGE_SIZE);
1395 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
1397 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1398 int max, int add_cr);
1400 static ssize_t input_dev_show_properties(struct device *dev,
1401 struct device_attribute *attr,
1404 struct input_dev *input_dev = to_input_dev(dev);
1405 int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
1406 INPUT_PROP_MAX, true);
1407 return min_t(int, len, PAGE_SIZE);
1409 static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
1411 static struct attribute *input_dev_attrs[] = {
1412 &dev_attr_name.attr,
1413 &dev_attr_phys.attr,
1414 &dev_attr_uniq.attr,
1415 &dev_attr_modalias.attr,
1416 &dev_attr_properties.attr,
1420 static const struct attribute_group input_dev_attr_group = {
1421 .attrs = input_dev_attrs,
1424 #define INPUT_DEV_ID_ATTR(name) \
1425 static ssize_t input_dev_show_id_##name(struct device *dev, \
1426 struct device_attribute *attr, \
1429 struct input_dev *input_dev = to_input_dev(dev); \
1430 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
1432 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
1434 INPUT_DEV_ID_ATTR(bustype);
1435 INPUT_DEV_ID_ATTR(vendor);
1436 INPUT_DEV_ID_ATTR(product);
1437 INPUT_DEV_ID_ATTR(version);
1439 static struct attribute *input_dev_id_attrs[] = {
1440 &dev_attr_bustype.attr,
1441 &dev_attr_vendor.attr,
1442 &dev_attr_product.attr,
1443 &dev_attr_version.attr,
1447 static const struct attribute_group input_dev_id_attr_group = {
1449 .attrs = input_dev_id_attrs,
1452 static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
1453 int max, int add_cr)
1457 bool skip_empty = true;
1459 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
1460 len += input_bits_to_string(buf + len, max(buf_size - len, 0),
1461 bitmap[i], skip_empty);
1465 len += snprintf(buf + len, max(buf_size - len, 0), " ");
1470 * If no output was produced print a single 0.
1473 len = snprintf(buf, buf_size, "%d", 0);
1476 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
1481 #define INPUT_DEV_CAP_ATTR(ev, bm) \
1482 static ssize_t input_dev_show_cap_##bm(struct device *dev, \
1483 struct device_attribute *attr, \
1486 struct input_dev *input_dev = to_input_dev(dev); \
1487 int len = input_print_bitmap(buf, PAGE_SIZE, \
1488 input_dev->bm##bit, ev##_MAX, \
1490 return min_t(int, len, PAGE_SIZE); \
1492 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
1494 INPUT_DEV_CAP_ATTR(EV, ev);
1495 INPUT_DEV_CAP_ATTR(KEY, key);
1496 INPUT_DEV_CAP_ATTR(REL, rel);
1497 INPUT_DEV_CAP_ATTR(ABS, abs);
1498 INPUT_DEV_CAP_ATTR(MSC, msc);
1499 INPUT_DEV_CAP_ATTR(LED, led);
1500 INPUT_DEV_CAP_ATTR(SND, snd);
1501 INPUT_DEV_CAP_ATTR(FF, ff);
1502 INPUT_DEV_CAP_ATTR(SW, sw);
1504 static struct attribute *input_dev_caps_attrs[] = {
1517 static const struct attribute_group input_dev_caps_attr_group = {
1518 .name = "capabilities",
1519 .attrs = input_dev_caps_attrs,
1522 static const struct attribute_group *input_dev_attr_groups[] = {
1523 &input_dev_attr_group,
1524 &input_dev_id_attr_group,
1525 &input_dev_caps_attr_group,
1529 static void input_dev_release(struct device *device)
1531 struct input_dev *dev = to_input_dev(device);
1533 input_ff_destroy(dev);
1534 input_mt_destroy_slots(dev);
1535 kfree(dev->absinfo);
1539 module_put(THIS_MODULE);
1543 * Input uevent interface - loading event handlers based on
1546 static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
1547 const char *name, unsigned long *bitmap, int max)
1551 if (add_uevent_var(env, "%s", name))
1554 len = input_print_bitmap(&env->buf[env->buflen - 1],
1555 sizeof(env->buf) - env->buflen,
1556 bitmap, max, false);
1557 if (len >= (sizeof(env->buf) - env->buflen))
1564 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
1565 struct input_dev *dev)
1569 if (add_uevent_var(env, "MODALIAS="))
1572 len = input_print_modalias(&env->buf[env->buflen - 1],
1573 sizeof(env->buf) - env->buflen,
1575 if (len >= (sizeof(env->buf) - env->buflen))
1582 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
1584 int err = add_uevent_var(env, fmt, val); \
1589 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
1591 int err = input_add_uevent_bm_var(env, name, bm, max); \
1596 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
1598 int err = input_add_uevent_modalias_var(env, dev); \
1603 static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1605 struct input_dev *dev = to_input_dev(device);
1607 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
1608 dev->id.bustype, dev->id.vendor,
1609 dev->id.product, dev->id.version);
1611 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
1613 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
1615 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
1617 INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
1619 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
1620 if (test_bit(EV_KEY, dev->evbit))
1621 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
1622 if (test_bit(EV_REL, dev->evbit))
1623 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
1624 if (test_bit(EV_ABS, dev->evbit))
1625 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
1626 if (test_bit(EV_MSC, dev->evbit))
1627 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
1628 if (test_bit(EV_LED, dev->evbit))
1629 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
1630 if (test_bit(EV_SND, dev->evbit))
1631 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
1632 if (test_bit(EV_FF, dev->evbit))
1633 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
1634 if (test_bit(EV_SW, dev->evbit))
1635 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
1637 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
1642 #define INPUT_DO_TOGGLE(dev, type, bits, on) \
1647 if (!test_bit(EV_##type, dev->evbit)) \
1650 for_each_set_bit(i, dev->bits##bit, type##_CNT) { \
1651 active = test_bit(i, dev->bits); \
1652 if (!active && !on) \
1655 dev->event(dev, EV_##type, i, on ? active : 0); \
1659 static void input_dev_toggle(struct input_dev *dev, bool activate)
1664 INPUT_DO_TOGGLE(dev, LED, led, activate);
1665 INPUT_DO_TOGGLE(dev, SND, snd, activate);
1667 if (activate && test_bit(EV_REP, dev->evbit)) {
1668 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
1669 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
1674 * input_reset_device() - reset/restore the state of input device
1675 * @dev: input device whose state needs to be reset
1677 * This function tries to reset the state of an opened input device and
1678 * bring internal state and state if the hardware in sync with each other.
1679 * We mark all keys as released, restore LED state, repeat rate, etc.
1681 void input_reset_device(struct input_dev *dev)
1683 unsigned long flags;
1685 mutex_lock(&dev->mutex);
1686 spin_lock_irqsave(&dev->event_lock, flags);
1688 input_dev_toggle(dev, true);
1689 input_dev_release_keys(dev);
1691 spin_unlock_irqrestore(&dev->event_lock, flags);
1692 mutex_unlock(&dev->mutex);
1694 EXPORT_SYMBOL(input_reset_device);
1696 #ifdef CONFIG_PM_SLEEP
1697 static int input_dev_suspend(struct device *dev)
1699 struct input_dev *input_dev = to_input_dev(dev);
1701 spin_lock_irq(&input_dev->event_lock);
1704 * Keys that are pressed now are unlikely to be
1705 * still pressed when we resume.
1707 input_dev_release_keys(input_dev);
1709 /* Turn off LEDs and sounds, if any are active. */
1710 input_dev_toggle(input_dev, false);
1712 spin_unlock_irq(&input_dev->event_lock);
1717 static int input_dev_resume(struct device *dev)
1719 struct input_dev *input_dev = to_input_dev(dev);
1721 spin_lock_irq(&input_dev->event_lock);
1723 /* Restore state of LEDs and sounds, if any were active. */
1724 input_dev_toggle(input_dev, true);
1726 spin_unlock_irq(&input_dev->event_lock);
1731 static int input_dev_freeze(struct device *dev)
1733 struct input_dev *input_dev = to_input_dev(dev);
1735 spin_lock_irq(&input_dev->event_lock);
1738 * Keys that are pressed now are unlikely to be
1739 * still pressed when we resume.
1741 input_dev_release_keys(input_dev);
1743 spin_unlock_irq(&input_dev->event_lock);
1748 static int input_dev_poweroff(struct device *dev)
1750 struct input_dev *input_dev = to_input_dev(dev);
1752 spin_lock_irq(&input_dev->event_lock);
1754 /* Turn off LEDs and sounds, if any are active. */
1755 input_dev_toggle(input_dev, false);
1757 spin_unlock_irq(&input_dev->event_lock);
1762 static const struct dev_pm_ops input_dev_pm_ops = {
1763 .suspend = input_dev_suspend,
1764 .resume = input_dev_resume,
1765 .freeze = input_dev_freeze,
1766 .poweroff = input_dev_poweroff,
1767 .restore = input_dev_resume,
1769 #endif /* CONFIG_PM */
1771 static const struct device_type input_dev_type = {
1772 .groups = input_dev_attr_groups,
1773 .release = input_dev_release,
1774 .uevent = input_dev_uevent,
1775 #ifdef CONFIG_PM_SLEEP
1776 .pm = &input_dev_pm_ops,
1780 static char *input_devnode(struct device *dev, umode_t *mode)
1782 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
1785 struct class input_class = {
1787 .devnode = input_devnode,
1789 EXPORT_SYMBOL_GPL(input_class);
1792 * input_allocate_device - allocate memory for new input device
1794 * Returns prepared struct input_dev or %NULL.
1796 * NOTE: Use input_free_device() to free devices that have not been
1797 * registered; input_unregister_device() should be used for already
1798 * registered devices.
1800 struct input_dev *input_allocate_device(void)
1802 static atomic_t input_no = ATOMIC_INIT(-1);
1803 struct input_dev *dev;
1805 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1807 dev->dev.type = &input_dev_type;
1808 dev->dev.class = &input_class;
1809 device_initialize(&dev->dev);
1810 mutex_init(&dev->mutex);
1811 spin_lock_init(&dev->event_lock);
1812 init_timer(&dev->timer);
1813 INIT_LIST_HEAD(&dev->h_list);
1814 INIT_LIST_HEAD(&dev->node);
1816 dev_set_name(&dev->dev, "input%lu",
1817 (unsigned long)atomic_inc_return(&input_no));
1819 __module_get(THIS_MODULE);
1824 EXPORT_SYMBOL(input_allocate_device);
1826 struct input_devres {
1827 struct input_dev *input;
1830 static int devm_input_device_match(struct device *dev, void *res, void *data)
1832 struct input_devres *devres = res;
1834 return devres->input == data;
1837 static void devm_input_device_release(struct device *dev, void *res)
1839 struct input_devres *devres = res;
1840 struct input_dev *input = devres->input;
1842 dev_dbg(dev, "%s: dropping reference to %s\n",
1843 __func__, dev_name(&input->dev));
1844 input_put_device(input);
1848 * devm_input_allocate_device - allocate managed input device
1849 * @dev: device owning the input device being created
1851 * Returns prepared struct input_dev or %NULL.
1853 * Managed input devices do not need to be explicitly unregistered or
1854 * freed as it will be done automatically when owner device unbinds from
1855 * its driver (or binding fails). Once managed input device is allocated,
1856 * it is ready to be set up and registered in the same fashion as regular
1857 * input device. There are no special devm_input_device_[un]register()
1858 * variants, regular ones work with both managed and unmanaged devices,
1859 * should you need them. In most cases however, managed input device need
1860 * not be explicitly unregistered or freed.
1862 * NOTE: the owner device is set up as parent of input device and users
1863 * should not override it.
1865 struct input_dev *devm_input_allocate_device(struct device *dev)
1867 struct input_dev *input;
1868 struct input_devres *devres;
1870 devres = devres_alloc(devm_input_device_release,
1871 sizeof(*devres), GFP_KERNEL);
1875 input = input_allocate_device();
1877 devres_free(devres);
1881 input->dev.parent = dev;
1882 input->devres_managed = true;
1884 devres->input = input;
1885 devres_add(dev, devres);
1889 EXPORT_SYMBOL(devm_input_allocate_device);
1892 * input_free_device - free memory occupied by input_dev structure
1893 * @dev: input device to free
1895 * This function should only be used if input_register_device()
1896 * was not called yet or if it failed. Once device was registered
1897 * use input_unregister_device() and memory will be freed once last
1898 * reference to the device is dropped.
1900 * Device should be allocated by input_allocate_device().
1902 * NOTE: If there are references to the input device then memory
1903 * will not be freed until last reference is dropped.
1905 void input_free_device(struct input_dev *dev)
1908 if (dev->devres_managed)
1909 WARN_ON(devres_destroy(dev->dev.parent,
1910 devm_input_device_release,
1911 devm_input_device_match,
1913 input_put_device(dev);
1916 EXPORT_SYMBOL(input_free_device);
1919 * input_set_capability - mark device as capable of a certain event
1920 * @dev: device that is capable of emitting or accepting event
1921 * @type: type of the event (EV_KEY, EV_REL, etc...)
1924 * In addition to setting up corresponding bit in appropriate capability
1925 * bitmap the function also adjusts dev->evbit.
1927 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1929 if (type < EV_CNT && input_max_code[type] &&
1930 code > input_max_code[type]) {
1931 pr_err("%s: invalid code %u for type %u\n", __func__, code,
1939 __set_bit(code, dev->keybit);
1943 __set_bit(code, dev->relbit);
1947 input_alloc_absinfo(dev);
1951 __set_bit(code, dev->absbit);
1955 __set_bit(code, dev->mscbit);
1959 __set_bit(code, dev->swbit);
1963 __set_bit(code, dev->ledbit);
1967 __set_bit(code, dev->sndbit);
1971 __set_bit(code, dev->ffbit);
1979 pr_err("input_set_capability: unknown type %u (code %u)\n",
1985 __set_bit(type, dev->evbit);
1987 EXPORT_SYMBOL(input_set_capability);
1989 static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
1993 unsigned int events;
1996 mt_slots = dev->mt->num_slots;
1997 } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
1998 mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
1999 dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
2000 mt_slots = clamp(mt_slots, 2, 32);
2001 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
2007 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
2009 if (test_bit(EV_ABS, dev->evbit))
2010 for_each_set_bit(i, dev->absbit, ABS_CNT)
2011 events += input_is_mt_axis(i) ? mt_slots : 1;
2013 if (test_bit(EV_REL, dev->evbit))
2014 events += bitmap_weight(dev->relbit, REL_CNT);
2016 /* Make room for KEY and MSC events */
2022 #define INPUT_CLEANSE_BITMASK(dev, type, bits) \
2024 if (!test_bit(EV_##type, dev->evbit)) \
2025 memset(dev->bits##bit, 0, \
2026 sizeof(dev->bits##bit)); \
2029 static void input_cleanse_bitmasks(struct input_dev *dev)
2031 INPUT_CLEANSE_BITMASK(dev, KEY, key);
2032 INPUT_CLEANSE_BITMASK(dev, REL, rel);
2033 INPUT_CLEANSE_BITMASK(dev, ABS, abs);
2034 INPUT_CLEANSE_BITMASK(dev, MSC, msc);
2035 INPUT_CLEANSE_BITMASK(dev, LED, led);
2036 INPUT_CLEANSE_BITMASK(dev, SND, snd);
2037 INPUT_CLEANSE_BITMASK(dev, FF, ff);
2038 INPUT_CLEANSE_BITMASK(dev, SW, sw);
2041 static void __input_unregister_device(struct input_dev *dev)
2043 struct input_handle *handle, *next;
2045 input_disconnect_device(dev);
2047 mutex_lock(&input_mutex);
2049 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
2050 handle->handler->disconnect(handle);
2051 WARN_ON(!list_empty(&dev->h_list));
2053 del_timer_sync(&dev->timer);
2054 list_del_init(&dev->node);
2056 input_wakeup_procfs_readers();
2058 mutex_unlock(&input_mutex);
2060 device_del(&dev->dev);
2063 static void devm_input_device_unregister(struct device *dev, void *res)
2065 struct input_devres *devres = res;
2066 struct input_dev *input = devres->input;
2068 dev_dbg(dev, "%s: unregistering device %s\n",
2069 __func__, dev_name(&input->dev));
2070 __input_unregister_device(input);
2074 * input_enable_softrepeat - enable software autorepeat
2075 * @dev: input device
2076 * @delay: repeat delay
2077 * @period: repeat period
2079 * Enable software autorepeat on the input device.
2081 void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
2083 dev->timer.data = (unsigned long) dev;
2084 dev->timer.function = input_repeat_key;
2085 dev->rep[REP_DELAY] = delay;
2086 dev->rep[REP_PERIOD] = period;
2088 EXPORT_SYMBOL(input_enable_softrepeat);
2091 * input_register_device - register device with input core
2092 * @dev: device to be registered
2094 * This function registers device with input core. The device must be
2095 * allocated with input_allocate_device() and all it's capabilities
2096 * set up before registering.
2097 * If function fails the device must be freed with input_free_device().
2098 * Once device has been successfully registered it can be unregistered
2099 * with input_unregister_device(); input_free_device() should not be
2100 * called in this case.
2102 * Note that this function is also used to register managed input devices
2103 * (ones allocated with devm_input_allocate_device()). Such managed input
2104 * devices need not be explicitly unregistered or freed, their tear down
2105 * is controlled by the devres infrastructure. It is also worth noting
2106 * that tear down of managed input devices is internally a 2-step process:
2107 * registered managed input device is first unregistered, but stays in
2108 * memory and can still handle input_event() calls (although events will
2109 * not be delivered anywhere). The freeing of managed input device will
2110 * happen later, when devres stack is unwound to the point where device
2111 * allocation was made.
2113 int input_register_device(struct input_dev *dev)
2115 struct input_devres *devres = NULL;
2116 struct input_handler *handler;
2117 unsigned int packet_size;
2121 if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
2123 "Absolute device without dev->absinfo, refusing to register\n");
2127 if (dev->devres_managed) {
2128 devres = devres_alloc(devm_input_device_unregister,
2129 sizeof(*devres), GFP_KERNEL);
2133 devres->input = dev;
2136 /* Every input device generates EV_SYN/SYN_REPORT events. */
2137 __set_bit(EV_SYN, dev->evbit);
2139 /* KEY_RESERVED is not supposed to be transmitted to userspace. */
2140 __clear_bit(KEY_RESERVED, dev->keybit);
2142 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
2143 input_cleanse_bitmasks(dev);
2145 packet_size = input_estimate_events_per_packet(dev);
2146 if (dev->hint_events_per_packet < packet_size)
2147 dev->hint_events_per_packet = packet_size;
2149 dev->max_vals = dev->hint_events_per_packet + 2;
2150 dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
2153 goto err_devres_free;
2157 * If delay and period are pre-set by the driver, then autorepeating
2158 * is handled by the driver itself and we don't do it in input.c.
2160 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
2161 input_enable_softrepeat(dev, 250, 33);
2163 if (!dev->getkeycode)
2164 dev->getkeycode = input_default_getkeycode;
2166 if (!dev->setkeycode)
2167 dev->setkeycode = input_default_setkeycode;
2169 error = device_add(&dev->dev);
2173 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
2174 pr_info("%s as %s\n",
2175 dev->name ? dev->name : "Unspecified device",
2176 path ? path : "N/A");
2179 error = mutex_lock_interruptible(&input_mutex);
2181 goto err_device_del;
2183 list_add_tail(&dev->node, &input_dev_list);
2185 list_for_each_entry(handler, &input_handler_list, node)
2186 input_attach_handler(dev, handler);
2188 input_wakeup_procfs_readers();
2190 mutex_unlock(&input_mutex);
2192 if (dev->devres_managed) {
2193 dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
2194 __func__, dev_name(&dev->dev));
2195 devres_add(dev->dev.parent, devres);
2200 device_del(&dev->dev);
2205 devres_free(devres);
2208 EXPORT_SYMBOL(input_register_device);
2211 * input_unregister_device - unregister previously registered device
2212 * @dev: device to be unregistered
2214 * This function unregisters an input device. Once device is unregistered
2215 * the caller should not try to access it as it may get freed at any moment.
2217 void input_unregister_device(struct input_dev *dev)
2219 if (dev->devres_managed) {
2220 WARN_ON(devres_destroy(dev->dev.parent,
2221 devm_input_device_unregister,
2222 devm_input_device_match,
2224 __input_unregister_device(dev);
2226 * We do not do input_put_device() here because it will be done
2227 * when 2nd devres fires up.
2230 __input_unregister_device(dev);
2231 input_put_device(dev);
2234 EXPORT_SYMBOL(input_unregister_device);
2237 * input_register_handler - register a new input handler
2238 * @handler: handler to be registered
2240 * This function registers a new input handler (interface) for input
2241 * devices in the system and attaches it to all input devices that
2242 * are compatible with the handler.
2244 int input_register_handler(struct input_handler *handler)
2246 struct input_dev *dev;
2249 error = mutex_lock_interruptible(&input_mutex);
2253 INIT_LIST_HEAD(&handler->h_list);
2255 list_add_tail(&handler->node, &input_handler_list);
2257 list_for_each_entry(dev, &input_dev_list, node)
2258 input_attach_handler(dev, handler);
2260 input_wakeup_procfs_readers();
2262 mutex_unlock(&input_mutex);
2265 EXPORT_SYMBOL(input_register_handler);
2268 * input_unregister_handler - unregisters an input handler
2269 * @handler: handler to be unregistered
2271 * This function disconnects a handler from its input devices and
2272 * removes it from lists of known handlers.
2274 void input_unregister_handler(struct input_handler *handler)
2276 struct input_handle *handle, *next;
2278 mutex_lock(&input_mutex);
2280 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
2281 handler->disconnect(handle);
2282 WARN_ON(!list_empty(&handler->h_list));
2284 list_del_init(&handler->node);
2286 input_wakeup_procfs_readers();
2288 mutex_unlock(&input_mutex);
2290 EXPORT_SYMBOL(input_unregister_handler);
2293 * input_handler_for_each_handle - handle iterator
2294 * @handler: input handler to iterate
2295 * @data: data for the callback
2296 * @fn: function to be called for each handle
2298 * Iterate over @bus's list of devices, and call @fn for each, passing
2299 * it @data and stop when @fn returns a non-zero value. The function is
2300 * using RCU to traverse the list and therefore may be using in atomic
2301 * contexts. The @fn callback is invoked from RCU critical section and
2302 * thus must not sleep.
2304 int input_handler_for_each_handle(struct input_handler *handler, void *data,
2305 int (*fn)(struct input_handle *, void *))
2307 struct input_handle *handle;
2312 list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
2313 retval = fn(handle, data);
2322 EXPORT_SYMBOL(input_handler_for_each_handle);
2325 * input_register_handle - register a new input handle
2326 * @handle: handle to register
2328 * This function puts a new input handle onto device's
2329 * and handler's lists so that events can flow through
2330 * it once it is opened using input_open_device().
2332 * This function is supposed to be called from handler's
2335 int input_register_handle(struct input_handle *handle)
2337 struct input_handler *handler = handle->handler;
2338 struct input_dev *dev = handle->dev;
2342 * We take dev->mutex here to prevent race with
2343 * input_release_device().
2345 error = mutex_lock_interruptible(&dev->mutex);
2350 * Filters go to the head of the list, normal handlers
2353 if (handler->filter)
2354 list_add_rcu(&handle->d_node, &dev->h_list);
2356 list_add_tail_rcu(&handle->d_node, &dev->h_list);
2358 mutex_unlock(&dev->mutex);
2361 * Since we are supposed to be called from ->connect()
2362 * which is mutually exclusive with ->disconnect()
2363 * we can't be racing with input_unregister_handle()
2364 * and so separate lock is not needed here.
2366 list_add_tail_rcu(&handle->h_node, &handler->h_list);
2369 handler->start(handle);
2373 EXPORT_SYMBOL(input_register_handle);
2376 * input_unregister_handle - unregister an input handle
2377 * @handle: handle to unregister
2379 * This function removes input handle from device's
2380 * and handler's lists.
2382 * This function is supposed to be called from handler's
2383 * disconnect() method.
2385 void input_unregister_handle(struct input_handle *handle)
2387 struct input_dev *dev = handle->dev;
2389 list_del_rcu(&handle->h_node);
2392 * Take dev->mutex to prevent race with input_release_device().
2394 mutex_lock(&dev->mutex);
2395 list_del_rcu(&handle->d_node);
2396 mutex_unlock(&dev->mutex);
2400 EXPORT_SYMBOL(input_unregister_handle);
2403 * input_get_new_minor - allocates a new input minor number
2404 * @legacy_base: beginning or the legacy range to be searched
2405 * @legacy_num: size of legacy range
2406 * @allow_dynamic: whether we can also take ID from the dynamic range
2408 * This function allocates a new device minor for from input major namespace.
2409 * Caller can request legacy minor by specifying @legacy_base and @legacy_num
2410 * parameters and whether ID can be allocated from dynamic range if there are
2411 * no free IDs in legacy range.
2413 int input_get_new_minor(int legacy_base, unsigned int legacy_num,
2417 * This function should be called from input handler's ->connect()
2418 * methods, which are serialized with input_mutex, so no additional
2419 * locking is needed here.
2421 if (legacy_base >= 0) {
2422 int minor = ida_simple_get(&input_ida,
2424 legacy_base + legacy_num,
2426 if (minor >= 0 || !allow_dynamic)
2430 return ida_simple_get(&input_ida,
2431 INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
2434 EXPORT_SYMBOL(input_get_new_minor);
2437 * input_free_minor - release previously allocated minor
2438 * @minor: minor to be released
2440 * This function releases previously allocated input minor so that it can be
2443 void input_free_minor(unsigned int minor)
2445 ida_simple_remove(&input_ida, minor);
2447 EXPORT_SYMBOL(input_free_minor);
2449 static int __init input_init(void)
2453 err = class_register(&input_class);
2455 pr_err("unable to register input_dev class\n");
2459 err = input_proc_init();
2463 err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2464 INPUT_MAX_CHAR_DEVICES, "input");
2466 pr_err("unable to register char major %d", INPUT_MAJOR);
2472 fail2: input_proc_exit();
2473 fail1: class_unregister(&input_class);
2477 static void __exit input_exit(void)
2480 unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
2481 INPUT_MAX_CHAR_DEVICES);
2482 class_unregister(&input_class);
2485 subsys_initcall(input_init);
2486 module_exit(input_exit);