Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * The input core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (c) 1999-2002 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/input/mt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "input-compat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "input-poller.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) MODULE_DESCRIPTION("Input core");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define INPUT_MAX_CHAR_DEVICES		1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define INPUT_FIRST_DYNAMIC_DEV		256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) static DEFINE_IDA(input_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) static LIST_HEAD(input_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static LIST_HEAD(input_handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * input_mutex protects access to both input_dev_list and input_handler_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * This also causes input_[un]register_device and input_[un]register_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * be mutually exclusive which simplifies locking in drivers implementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * input handlers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static DEFINE_MUTEX(input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) static inline int is_event_supported(unsigned int code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 				     unsigned long *bm, unsigned int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	return code <= max && test_bit(code, bm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) static int input_defuzz_abs_event(int value, int old_val, int fuzz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	if (fuzz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 			return old_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		if (value > old_val - fuzz && value < old_val + fuzz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 			return (old_val * 3 + value) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 		if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 			return (old_val + value) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) static void input_start_autorepeat(struct input_dev *dev, int code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (test_bit(EV_REP, dev->evbit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	    dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	    dev->timer.function) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		dev->repeat_key = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		mod_timer(&dev->timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			  jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static void input_stop_autorepeat(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	del_timer(&dev->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * Pass event first through all filters and then, if event has not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * filtered out, through all open handles. This function is called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * dev->event_lock held and interrupts disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) static unsigned int input_to_handler(struct input_handle *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 			struct input_value *vals, unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	struct input_handler *handler = handle->handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct input_value *end = vals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct input_value *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	if (handler->filter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 		for (v = vals; v != vals + count; v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			if (handler->filter(handle, v->type, v->code, v->value))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 			if (end != v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 				*end = *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			end++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		count = end - vals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	if (handler->events)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		handler->events(handle, vals, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	else if (handler->event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		for (v = vals; v != vals + count; v++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			handler->event(handle, v->type, v->code, v->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  * Pass values first through all filters and then, if event has not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  * filtered out, through all open handles. This function is called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * dev->event_lock held and interrupts disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) static void input_pass_values(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			      struct input_value *vals, unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct input_value *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	handle = rcu_dereference(dev->grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	if (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		count = input_to_handler(handle, vals, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		list_for_each_entry_rcu(handle, &dev->h_list, d_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			if (handle->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 				count = input_to_handler(handle, vals, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 				if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	/* trigger auto repeat for key events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		for (v = vals; v != vals + count; v++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 			if (v->type == EV_KEY && v->value != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 				if (v->value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 					input_start_autorepeat(dev, v->code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 					input_stop_autorepeat(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static void input_pass_event(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			     unsigned int type, unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	struct input_value vals[] = { { type, code, value } };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	input_pass_values(dev, vals, ARRAY_SIZE(vals));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175)  * Generate software autorepeat event. Note that we take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176)  * dev->event_lock here to avoid racing with input_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * which may cause keys get "stuck".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) static void input_repeat_key(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct input_dev *dev = from_timer(dev, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (test_bit(dev->repeat_key, dev->key) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	    is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		struct input_value vals[] =  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 			{ EV_KEY, dev->repeat_key, 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 			input_value_sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		input_set_timestamp(dev, ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		input_pass_values(dev, vals, ARRAY_SIZE(vals));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		if (dev->rep[REP_PERIOD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			mod_timer(&dev->timer, jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 					msecs_to_jiffies(dev->rep[REP_PERIOD]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) #define INPUT_IGNORE_EVENT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) #define INPUT_PASS_TO_HANDLERS	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) #define INPUT_PASS_TO_DEVICE	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) #define INPUT_SLOT		4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #define INPUT_FLUSH		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) #define INPUT_PASS_TO_ALL	(INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) static int input_handle_abs_event(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 				  unsigned int code, int *pval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	struct input_mt *mt = dev->mt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	bool is_mt_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	int *pold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	if (code == ABS_MT_SLOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		 * "Stage" the event; we'll flush it later, when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		 * get actual touch data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		if (mt && *pval >= 0 && *pval < mt->num_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			mt->slot = *pval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return INPUT_IGNORE_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	is_mt_event = input_is_mt_value(code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (!is_mt_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		pold = &dev->absinfo[code].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	} else if (mt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		 * Bypass filtering for multi-touch events when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		 * not employing slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 		pold = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	if (pold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 		*pval = input_defuzz_abs_event(*pval, *pold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 						dev->absinfo[code].fuzz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		if (*pold == *pval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 			return INPUT_IGNORE_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		*pold = *pval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	/* Flush pending "slot" event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		input_abs_set_val(dev, ABS_MT_SLOT, mt->slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	return INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) static int input_get_disposition(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			  unsigned int type, unsigned int code, int *pval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	int disposition = INPUT_IGNORE_EVENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	int value = *pval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	case EV_SYN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		case SYN_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		case SYN_REPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		case SYN_MT_REPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			disposition = INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	case EV_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		if (is_event_supported(code, dev->keybit, KEY_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			/* auto-repeat bypasses state updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			if (value == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 				disposition = INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			if (!!test_bit(code, dev->key) != !!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 				__change_bit(code, dev->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 				disposition = INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	case EV_SW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		if (is_event_supported(code, dev->swbit, SW_MAX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		    !!test_bit(code, dev->sw) != !!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			__change_bit(code, dev->sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			disposition = INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	case EV_ABS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		if (is_event_supported(code, dev->absbit, ABS_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			disposition = input_handle_abs_event(dev, code, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	case EV_REL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		if (is_event_supported(code, dev->relbit, REL_MAX) && value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			disposition = INPUT_PASS_TO_HANDLERS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	case EV_MSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		if (is_event_supported(code, dev->mscbit, MSC_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	case EV_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		if (is_event_supported(code, dev->ledbit, LED_MAX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		    !!test_bit(code, dev->led) != !!value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			__change_bit(code, dev->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	case EV_SND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		if (is_event_supported(code, dev->sndbit, SND_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			if (!!test_bit(code, dev->snd) != !!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 				__change_bit(code, dev->snd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	case EV_REP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			dev->rep[code] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	case EV_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	case EV_PWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		disposition = INPUT_PASS_TO_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	*pval = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	return disposition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) static void input_handle_event(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			       unsigned int type, unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	int disposition = input_get_disposition(dev, type, code, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		add_input_randomness(type, code, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		dev->event(dev, type, code, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	if (!dev->vals)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	if (disposition & INPUT_PASS_TO_HANDLERS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		struct input_value *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		if (disposition & INPUT_SLOT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			v = &dev->vals[dev->num_vals++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 			v->type = EV_ABS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 			v->code = ABS_MT_SLOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			v->value = dev->mt->slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		v = &dev->vals[dev->num_vals++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		v->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		v->code = code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		v->value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (disposition & INPUT_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		if (dev->num_vals >= 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			input_pass_values(dev, dev->vals, dev->num_vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		dev->num_vals = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		 * Reset the timestamp on flush so we won't end up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		 * with a stale one. Note we only need to reset the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		 * monolithic one as we use its presence when deciding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		 * whether to generate a synthetic timestamp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	} else if (dev->num_vals >= dev->max_vals - 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		dev->vals[dev->num_vals++] = input_value_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		input_pass_values(dev, dev->vals, dev->num_vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		dev->num_vals = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * input_event() - report new input event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  * @dev: device that generated the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * @type: type of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * @code: event code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  * @value: value of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423)  * This function should be used by drivers implementing various input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424)  * devices to report input events. See also input_inject_event().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426)  * NOTE: input_event() may be safely used right after input device was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  * allocated with input_allocate_device(), even before it is registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  * with input_register_device(), but the event will not reach any of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * input handlers. Such early invocation of input_event() may be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * to 'seed' initial state of a switch or initial position of absolute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * axis, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) void input_event(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		 unsigned int type, unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		input_handle_event(dev, type, code, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) EXPORT_SYMBOL(input_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * input_inject_event() - send input event from input handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * @handle: input handle to send event through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * @type: type of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * @code: event code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  * @value: value of the event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454)  * Similar to input_event() but will ignore event if device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455)  * "grabbed" and handle injecting event is not the one that owns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456)  * the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) void input_inject_event(struct input_handle *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			unsigned int type, unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	struct input_handle *grab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	if (is_event_supported(type, dev->evbit, EV_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		grab = rcu_dereference(dev->grab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		if (!grab || grab == handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			input_handle_event(dev, type, code, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) EXPORT_SYMBOL(input_inject_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480)  * input_alloc_absinfo - allocates array of input_absinfo structs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  * @dev: the input device emitting absolute events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  * If the absinfo struct the caller asked for is already allocated, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * functions will not do anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) void input_alloc_absinfo(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	if (dev->absinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	dev->absinfo = kcalloc(ABS_CNT, sizeof(*dev->absinfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	if (!dev->absinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 		dev_err(dev->dev.parent ?: &dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			"%s: unable to allocate memory\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		 * We will handle this allocation failure in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		 * input_register_device() when we refuse to register input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		 * device with ABS bits but without absinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) EXPORT_SYMBOL(input_alloc_absinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) void input_set_abs_params(struct input_dev *dev, unsigned int axis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			  int min, int max, int fuzz, int flat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	struct input_absinfo *absinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	input_alloc_absinfo(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (!dev->absinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	absinfo = &dev->absinfo[axis];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	absinfo->minimum = min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	absinfo->maximum = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	absinfo->fuzz = fuzz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	absinfo->flat = flat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	__set_bit(EV_ABS, dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	__set_bit(axis, dev->absbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) EXPORT_SYMBOL(input_set_abs_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526)  * input_grab_device - grabs device for exclusive use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  * @handle: input handle that wants to own the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  * When a device is grabbed by an input handle all events generated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  * the device are delivered only to this handle. Also events injected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  * by other input handles are ignored while device is grabbed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) int input_grab_device(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	retval = mutex_lock_interruptible(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (dev->grab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		retval = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	rcu_assign_pointer(dev->grab, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) EXPORT_SYMBOL(input_grab_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) static void __input_release_device(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	struct input_handle *grabber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	grabber = rcu_dereference_protected(dev->grab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 					    lockdep_is_held(&dev->mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (grabber == handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		rcu_assign_pointer(dev->grab, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		/* Make sure input_pass_event() notices that grab is gone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		list_for_each_entry(handle, &dev->h_list, d_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			if (handle->open && handle->handler->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 				handle->handler->start(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574)  * input_release_device - release previously grabbed device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575)  * @handle: input handle that owns the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * Releases previously grabbed device so that other input handles can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * start receiving input events. Upon release all handlers attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  * to the device have their start() method called so they have a change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580)  * to synchronize device state with the rest of the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) void input_release_device(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	__input_release_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) EXPORT_SYMBOL(input_release_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593)  * input_open_device - open input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594)  * @handle: handle through which device is being accessed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  * This function should be called by input handlers when they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * want to start receive events from given input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) int input_open_device(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	retval = mutex_lock_interruptible(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if (dev->going_away) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	handle->open++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (dev->users++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		 * Device is already opened, so we can exit immediately and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		 * report success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (dev->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		retval = dev->open(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			dev->users--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 			handle->open--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			 * Make sure we are not delivering any more events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			 * through this handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (dev->poller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		input_dev_poller_start(dev->poller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) EXPORT_SYMBOL(input_open_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) int input_flush_device(struct input_handle *handle, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	retval = mutex_lock_interruptible(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	if (dev->flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		retval = dev->flush(dev, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) EXPORT_SYMBOL(input_flush_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * input_close_device - close input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * @handle: handle through which device is being accessed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * This function should be called by input handlers when they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * want to stop receive events from given input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) void input_close_device(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	__input_release_device(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (!--dev->users) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		if (dev->poller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			input_dev_poller_stop(dev->poller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		if (dev->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			dev->close(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	if (!--handle->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		 * synchronize_rcu() makes sure that input_pass_event()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		 * completed and that no more input events are delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		 * through this handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) EXPORT_SYMBOL(input_close_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700)  * Simulate keyup events for all keys that are marked as pressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701)  * The function must be called with dev->event_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) static void input_dev_release_keys(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	bool need_sync = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	int code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		for_each_set_bit(code, dev->key, KEY_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			input_pass_event(dev, EV_KEY, code, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			need_sync = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		if (need_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		memset(dev->key, 0, sizeof(dev->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722)  * Prepare device for unregistering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) static void input_disconnect_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 * Mark device as going away. Note that we take dev->mutex here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 * not to protect access to dev->going_away but rather to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 * that there are no threads in the middle of input_open_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	dev->going_away = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	spin_lock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	 * Simulate keyup events for all pressed keys so that handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	 * are not left with "stuck" keys. The driver may continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	 * generate events even after we done here but they will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	 * reach any handlers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	input_dev_release_keys(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	list_for_each_entry(handle, &dev->h_list, d_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		handle->open = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	spin_unlock_irq(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  * @ke: keymap entry containing scancode to be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * @scancode: pointer to the location where converted scancode should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  *	be stored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  * This function is used to convert scancode stored in &struct keymap_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  * into scalar form understood by legacy keymap handling methods. These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761)  * methods expect scancodes to be represented as 'unsigned int'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) int input_scancode_to_scalar(const struct input_keymap_entry *ke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			     unsigned int *scancode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	switch (ke->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		*scancode = *((u8 *)ke->scancode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		*scancode = *((u16 *)ke->scancode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		*scancode = *((u32 *)ke->scancode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) EXPORT_SYMBOL(input_scancode_to_scalar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788)  * Those routines handle the default case where no [gs]etkeycode() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789)  * defined. In this case, an array indexed by the scancode is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) static unsigned int input_fetch_keycode(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 					unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	switch (dev->keycodesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		return ((u8 *)dev->keycode)[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		return ((u16 *)dev->keycode)[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		return ((u32 *)dev->keycode)[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) static int input_default_getkeycode(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 				    struct input_keymap_entry *ke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (!dev->keycodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	if (ke->flags & INPUT_KEYMAP_BY_INDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		index = ke->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		error = input_scancode_to_scalar(ke, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (index >= dev->keycodemax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	ke->keycode = input_fetch_keycode(dev, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	ke->index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	ke->len = sizeof(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	memcpy(ke->scancode, &index, sizeof(index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) static int input_default_setkeycode(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 				    const struct input_keymap_entry *ke,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 				    unsigned int *old_keycode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	if (!dev->keycodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		index = ke->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		error = input_scancode_to_scalar(ke, &index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	if (index >= dev->keycodemax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	if (dev->keycodesize < sizeof(ke->keycode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			(ke->keycode >> (dev->keycodesize * 8)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	switch (dev->keycodesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		case 1: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			u8 *k = (u8 *)dev->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			*old_keycode = k[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			k[index] = ke->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		case 2: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			u16 *k = (u16 *)dev->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			*old_keycode = k[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			k[index] = ke->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		default: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			u32 *k = (u32 *)dev->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			*old_keycode = k[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 			k[index] = ke->keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	if (*old_keycode <= KEY_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		__clear_bit(*old_keycode, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		for (i = 0; i < dev->keycodemax; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			if (input_fetch_keycode(dev, i) == *old_keycode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				__set_bit(*old_keycode, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 				/* Setting the bit twice is useless, so break */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	__set_bit(ke->keycode, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * input_get_keycode - retrieve keycode currently mapped to a given scancode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * @dev: input device which keymap is being queried
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  * @ke: keymap entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * This function should be called by anyone interested in retrieving current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  * keymap. Presently evdev handlers use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	retval = dev->getkeycode(dev, ke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) EXPORT_SYMBOL(input_get_keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919)  * input_set_keycode - attribute a keycode to a given scancode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920)  * @dev: input device which keymap is being updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921)  * @ke: new keymap entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923)  * This function should be called by anyone needing to update current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  * keymap. Presently keyboard and evdev handlers use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) int input_set_keycode(struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		      const struct input_keymap_entry *ke)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	unsigned int old_keycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	if (ke->keycode > KEY_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	retval = dev->setkeycode(dev, ke, &old_keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	/* Make sure KEY_RESERVED did not get enabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	__clear_bit(KEY_RESERVED, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	 * Simulate keyup event if keycode is not present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	 * in the keymap anymore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (old_keycode > KEY_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		dev_warn(dev->dev.parent ?: &dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			 "%s: got too big old keycode %#x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			 __func__, old_keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	} else if (test_bit(EV_KEY, dev->evbit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		   !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		   __test_and_clear_bit(old_keycode, dev->key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		struct input_value vals[] =  {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			{ EV_KEY, old_keycode, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			input_value_sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		input_pass_values(dev, vals, ARRAY_SIZE(vals));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) EXPORT_SYMBOL(input_set_keycode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) bool input_match_device_id(const struct input_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			   const struct input_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		if (id->bustype != dev->id.bustype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		if (id->vendor != dev->id.vendor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if (id->product != dev->id.product)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		if (id->version != dev->id.version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	    !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	    !bitmap_subset(id->relbit, dev->relbit, REL_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	    !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	    !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	    !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	    !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	    !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	    !bitmap_subset(id->swbit, dev->swbit, SW_MAX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	    !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) EXPORT_SYMBOL(input_match_device_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) static const struct input_device_id *input_match_device(struct input_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 							struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	const struct input_device_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	for (id = handler->id_table; id->flags || id->driver_info; id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		if (input_match_device_id(dev, id) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		    (!handler->match || handler->match(handler, dev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 			return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	const struct input_device_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	id = input_match_device(handler, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (!id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	error = handler->connect(handler, dev, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (error && error != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		pr_err("failed to attach handler %s to device %s, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		       handler->name, kobject_name(&dev->dev.kobj), error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) static int input_bits_to_string(char *buf, int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 				unsigned long bits, bool skip_empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	if (in_compat_syscall()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		u32 dword = bits >> 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		if (dword || !skip_empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			len += snprintf(buf, buf_size, "%x ", dword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		dword = bits & 0xffffffffUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (dword || !skip_empty || len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			len += snprintf(buf + len, max(buf_size - len, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 					"%x", dword);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		if (bits || !skip_empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			len += snprintf(buf, buf_size, "%lx", bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) #else /* !CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) static int input_bits_to_string(char *buf, int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 				unsigned long bits, bool skip_empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	return bits || !skip_empty ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		snprintf(buf, buf_size, "%lx", bits) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) static struct proc_dir_entry *proc_bus_input_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static int input_devices_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static inline void input_wakeup_procfs_readers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	input_devices_state++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	wake_up(&input_devices_poll_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	poll_wait(file, &input_devices_poll_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	if (file->f_version != input_devices_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		file->f_version = input_devices_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) union input_seq_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		unsigned short pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		bool mutex_acquired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	union input_seq_state *state = (union input_seq_state *)&seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	/* We need to fit into seq->private pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	error = mutex_lock_interruptible(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		state->mutex_acquired = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	state->mutex_acquired = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	return seq_list_start(&input_dev_list, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	return seq_list_next(v, &input_dev_list, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static void input_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	union input_seq_state *state = (union input_seq_state *)&seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	if (state->mutex_acquired)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 		mutex_unlock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 				   unsigned long *bitmap, int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	bool skip_empty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	char buf[18];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	seq_printf(seq, "B: %s=", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		if (input_bits_to_string(buf, sizeof(buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 					 bitmap[i], skip_empty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 			skip_empty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	 * If no output was produced print a single 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	if (skip_empty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		seq_putc(seq, '0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) static int input_devices_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	struct input_dev *dev = container_of(v, struct input_dev, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		   dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	seq_puts(seq, "H: Handlers=");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	list_for_each_entry(handle, &dev->h_list, d_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		seq_printf(seq, "%s ", handle->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (test_bit(EV_KEY, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	if (test_bit(EV_REL, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	if (test_bit(EV_ABS, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 		input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	if (test_bit(EV_MSC, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (test_bit(EV_LED, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	if (test_bit(EV_SND, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	if (test_bit(EV_FF, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	if (test_bit(EV_SW, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	kfree(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) static const struct seq_operations input_devices_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	.start	= input_devices_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	.next	= input_devices_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	.stop	= input_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	.show	= input_devices_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static int input_proc_devices_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	return seq_open(file, &input_devices_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) static const struct proc_ops input_devices_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	.proc_open	= input_proc_devices_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	.proc_poll	= input_proc_devices_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	.proc_read	= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	.proc_lseek	= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	.proc_release	= seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	union input_seq_state *state = (union input_seq_state *)&seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	/* We need to fit into seq->private pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	error = mutex_lock_interruptible(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		state->mutex_acquired = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	state->mutex_acquired = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	state->pos = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	return seq_list_start(&input_handler_list, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	union input_seq_state *state = (union input_seq_state *)&seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	state->pos = *pos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	return seq_list_next(v, &input_handler_list, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) static int input_handlers_seq_show(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct input_handler *handler = container_of(v, struct input_handler, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	union input_seq_state *state = (union input_seq_state *)&seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	if (handler->filter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		seq_puts(seq, " (filter)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	if (handler->legacy_minors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		seq_printf(seq, " Minor=%d", handler->minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	seq_putc(seq, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) static const struct seq_operations input_handlers_seq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	.start	= input_handlers_seq_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	.next	= input_handlers_seq_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	.stop	= input_seq_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	.show	= input_handlers_seq_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) static int input_proc_handlers_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	return seq_open(file, &input_handlers_seq_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) static const struct proc_ops input_handlers_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	.proc_open	= input_proc_handlers_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	.proc_read	= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	.proc_lseek	= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	.proc_release	= seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static int __init input_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct proc_dir_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	proc_bus_input_dir = proc_mkdir("bus/input", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (!proc_bus_input_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	entry = proc_create("devices", 0, proc_bus_input_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 			    &input_devices_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	entry = proc_create("handlers", 0, proc_bus_input_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 			    &input_handlers_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)  fail2:	remove_proc_entry("devices", proc_bus_input_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)  fail1: remove_proc_entry("bus/input", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) static void input_proc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	remove_proc_entry("devices", proc_bus_input_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	remove_proc_entry("handlers", proc_bus_input_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	remove_proc_entry("bus/input", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) #else /* !CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) static inline void input_wakeup_procfs_readers(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) static inline int input_proc_init(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) static inline void input_proc_exit(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) #define INPUT_DEV_STRING_ATTR_SHOW(name)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) static ssize_t input_dev_show_##name(struct device *dev,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 				     struct device_attribute *attr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 				     char *buf)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct input_dev *input_dev = to_input_dev(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	return scnprintf(buf, PAGE_SIZE, "%s\n",			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			 input_dev->name ? input_dev->name : "");	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) INPUT_DEV_STRING_ATTR_SHOW(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) INPUT_DEV_STRING_ATTR_SHOW(phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) INPUT_DEV_STRING_ATTR_SHOW(uniq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static int input_print_modalias_bits(char *buf, int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 				     char name, unsigned long *bm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 				     unsigned int min_bit, unsigned int max_bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	int len = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	len += snprintf(buf, max(size, 0), "%c", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	for (i = min_bit; i < max_bit; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		if (bm[BIT_WORD(i)] & BIT_MASK(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			len += snprintf(buf + len, max(size - len, 0), "%X,", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) static int input_print_modalias(char *buf, int size, struct input_dev *id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 				int add_cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	len = snprintf(buf, max(size, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		       "input:b%04Xv%04Xp%04Xe%04X-",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		       id->id.bustype, id->id.vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		       id->id.product, id->id.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 				'e', id->evbit, 0, EV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				'r', id->relbit, 0, REL_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				'a', id->absbit, 0, ABS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 				'm', id->mscbit, 0, MSC_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 				'l', id->ledbit, 0, LED_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 				's', id->sndbit, 0, SND_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				'f', id->ffbit, 0, FF_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	len += input_print_modalias_bits(buf + len, size - len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 				'w', id->swbit, 0, SW_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	if (add_cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		len += snprintf(buf + len, max(size - len, 0), "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) static ssize_t input_dev_show_modalias(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 				       struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 				       char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	struct input_dev *id = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	len = input_print_modalias(buf, PAGE_SIZE, id, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	return min_t(int, len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 			      int max, int add_cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) static ssize_t input_dev_show_properties(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 					 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 					 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	struct input_dev *input_dev = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 				     INPUT_PROP_MAX, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	return min_t(int, len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) static struct attribute *input_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	&dev_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	&dev_attr_phys.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	&dev_attr_uniq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	&dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	&dev_attr_properties.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) static const struct attribute_group input_dev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	.attrs	= input_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) #define INPUT_DEV_ID_ATTR(name)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) static ssize_t input_dev_show_id_##name(struct device *dev,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 					struct device_attribute *attr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 					char *buf)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	struct input_dev *input_dev = to_input_dev(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) INPUT_DEV_ID_ATTR(bustype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) INPUT_DEV_ID_ATTR(vendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) INPUT_DEV_ID_ATTR(product);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) INPUT_DEV_ID_ATTR(version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) static struct attribute *input_dev_id_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	&dev_attr_bustype.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	&dev_attr_vendor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	&dev_attr_product.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	&dev_attr_version.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) static const struct attribute_group input_dev_id_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	.name	= "id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	.attrs	= input_dev_id_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 			      int max, int add_cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	bool skip_empty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		len += input_bits_to_string(buf + len, max(buf_size - len, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 					    bitmap[i], skip_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 			skip_empty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 			if (i > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				len += snprintf(buf + len, max(buf_size - len, 0), " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	 * If no output was produced print a single 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		len = snprintf(buf, buf_size, "%d", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	if (add_cr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		len += snprintf(buf + len, max(buf_size - len, 0), "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) #define INPUT_DEV_CAP_ATTR(ev, bm)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) static ssize_t input_dev_show_cap_##bm(struct device *dev,		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 				       struct device_attribute *attr,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 				       char *buf)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	struct input_dev *input_dev = to_input_dev(dev);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	int len = input_print_bitmap(buf, PAGE_SIZE,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 				     input_dev->bm##bit, ev##_MAX,	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 				     true);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	return min_t(int, len, PAGE_SIZE);				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) INPUT_DEV_CAP_ATTR(EV, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) INPUT_DEV_CAP_ATTR(KEY, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) INPUT_DEV_CAP_ATTR(REL, rel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) INPUT_DEV_CAP_ATTR(ABS, abs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) INPUT_DEV_CAP_ATTR(MSC, msc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) INPUT_DEV_CAP_ATTR(LED, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) INPUT_DEV_CAP_ATTR(SND, snd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) INPUT_DEV_CAP_ATTR(FF, ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) INPUT_DEV_CAP_ATTR(SW, sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) static struct attribute *input_dev_caps_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	&dev_attr_ev.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	&dev_attr_key.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	&dev_attr_rel.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	&dev_attr_abs.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	&dev_attr_msc.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	&dev_attr_led.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	&dev_attr_snd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	&dev_attr_ff.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	&dev_attr_sw.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) static const struct attribute_group input_dev_caps_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	.name	= "capabilities",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	.attrs	= input_dev_caps_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) static const struct attribute_group *input_dev_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	&input_dev_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	&input_dev_id_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	&input_dev_caps_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	&input_poller_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) static void input_dev_release(struct device *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	struct input_dev *dev = to_input_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	input_ff_destroy(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	input_mt_destroy_slots(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	kfree(dev->poller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	kfree(dev->absinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	kfree(dev->vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)  * Input uevent interface - loading event handlers based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)  * device bitfields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 				   const char *name, unsigned long *bitmap, int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	if (add_uevent_var(env, "%s", name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	len = input_print_bitmap(&env->buf[env->buflen - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 				 sizeof(env->buf) - env->buflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 				 bitmap, max, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	if (len >= (sizeof(env->buf) - env->buflen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	env->buflen += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 					 struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	if (add_uevent_var(env, "MODALIAS="))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	len = input_print_modalias(&env->buf[env->buflen - 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 				   sizeof(env->buf) - env->buflen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 				   dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	if (len >= (sizeof(env->buf) - env->buflen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	env->buflen += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 		int err = add_uevent_var(env, fmt, val);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 		if (err)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			return err;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		int err = input_add_uevent_bm_var(env, name, bm, max);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		if (err)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 			return err;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		int err = input_add_uevent_modalias_var(env, dev);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		if (err)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			return err;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	struct input_dev *dev = to_input_dev(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 				dev->id.bustype, dev->id.vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 				dev->id.product, dev->id.version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (dev->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	if (dev->phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	if (dev->uniq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	if (test_bit(EV_KEY, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (test_bit(EV_REL, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	if (test_bit(EV_ABS, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	if (test_bit(EV_MSC, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	if (test_bit(EV_LED, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	if (test_bit(EV_SND, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	if (test_bit(EV_FF, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	if (test_bit(EV_SW, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) #define INPUT_DO_TOGGLE(dev, type, bits, on)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		int i;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		bool active;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		if (!test_bit(EV_##type, dev->evbit))			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			break;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		for_each_set_bit(i, dev->bits##bit, type##_CNT) {	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			active = test_bit(i, dev->bits);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 			if (!active && !on)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 				continue;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			dev->event(dev, EV_##type, i, on ? active : 0);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		}							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) static void input_dev_toggle(struct input_dev *dev, bool activate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	if (!dev->event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	INPUT_DO_TOGGLE(dev, LED, led, activate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	INPUT_DO_TOGGLE(dev, SND, snd, activate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	if (activate && test_bit(EV_REP, dev->evbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)  * input_reset_device() - reset/restore the state of input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)  * @dev: input device whose state needs to be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)  * This function tries to reset the state of an opened input device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688)  * bring internal state and state if the hardware in sync with each other.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689)  * We mark all keys as released, restore LED state, repeat rate, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) void input_reset_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	spin_lock_irqsave(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	input_dev_toggle(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	input_dev_release_keys(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	spin_unlock_irqrestore(&dev->event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) EXPORT_SYMBOL(input_reset_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) static int input_dev_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	struct input_dev *input_dev = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	spin_lock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 	 * Keys that are pressed now are unlikely to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	 * still pressed when we resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	input_dev_release_keys(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	/* Turn off LEDs and sounds, if any are active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	input_dev_toggle(input_dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	spin_unlock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) static int input_dev_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	struct input_dev *input_dev = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	spin_lock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	/* Restore state of LEDs and sounds, if any were active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	input_dev_toggle(input_dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	spin_unlock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) static int input_dev_freeze(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	struct input_dev *input_dev = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	spin_lock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 	 * Keys that are pressed now are unlikely to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	 * still pressed when we resume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	input_dev_release_keys(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	spin_unlock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static int input_dev_poweroff(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	struct input_dev *input_dev = to_input_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	spin_lock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	/* Turn off LEDs and sounds, if any are active. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	input_dev_toggle(input_dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	spin_unlock_irq(&input_dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) static const struct dev_pm_ops input_dev_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	.suspend	= input_dev_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	.resume		= input_dev_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	.freeze		= input_dev_freeze,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	.poweroff	= input_dev_poweroff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	.restore	= input_dev_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) static const struct device_type input_dev_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	.groups		= input_dev_attr_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	.release	= input_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	.uevent		= input_dev_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	.pm		= &input_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) static char *input_devnode(struct device *dev, umode_t *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) struct class input_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	.name		= "input",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 	.devnode	= input_devnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) EXPORT_SYMBOL_GPL(input_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802)  * input_allocate_device - allocate memory for new input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804)  * Returns prepared struct input_dev or %NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)  * NOTE: Use input_free_device() to free devices that have not been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)  * registered; input_unregister_device() should be used for already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808)  * registered devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) struct input_dev *input_allocate_device(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	static atomic_t input_no = ATOMIC_INIT(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		dev->dev.type = &input_dev_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		dev->dev.class = &input_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 		device_initialize(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 		mutex_init(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 		spin_lock_init(&dev->event_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 		timer_setup(&dev->timer, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 		INIT_LIST_HEAD(&dev->h_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 		INIT_LIST_HEAD(&dev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 		dev_set_name(&dev->dev, "input%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			     (unsigned long)atomic_inc_return(&input_no));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) EXPORT_SYMBOL(input_allocate_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) struct input_devres {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) static int devm_input_device_match(struct device *dev, void *res, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	struct input_devres *devres = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	return devres->input == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) static void devm_input_device_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	struct input_devres *devres = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	struct input_dev *input = devres->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	dev_dbg(dev, "%s: dropping reference to %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		__func__, dev_name(&input->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	input_put_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858)  * devm_input_allocate_device - allocate managed input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)  * @dev: device owning the input device being created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)  * Returns prepared struct input_dev or %NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)  * Managed input devices do not need to be explicitly unregistered or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)  * freed as it will be done automatically when owner device unbinds from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)  * its driver (or binding fails). Once managed input device is allocated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866)  * it is ready to be set up and registered in the same fashion as regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)  * input device. There are no special devm_input_device_[un]register()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868)  * variants, regular ones work with both managed and unmanaged devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)  * should you need them. In most cases however, managed input device need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870)  * not be explicitly unregistered or freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872)  * NOTE: the owner device is set up as parent of input device and users
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)  * should not override it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) struct input_dev *devm_input_allocate_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	struct input_devres *devres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	devres = devres_alloc(devm_input_device_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			      sizeof(*devres), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	if (!devres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	input = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	if (!input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		devres_free(devres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	input->dev.parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	input->devres_managed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	devres->input = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	devres_add(dev, devres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	return input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) EXPORT_SYMBOL(devm_input_allocate_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902)  * input_free_device - free memory occupied by input_dev structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)  * @dev: input device to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905)  * This function should only be used if input_register_device()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)  * was not called yet or if it failed. Once device was registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)  * use input_unregister_device() and memory will be freed once last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)  * reference to the device is dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)  * Device should be allocated by input_allocate_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912)  * NOTE: If there are references to the input device then memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)  * will not be freed until last reference is dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) void input_free_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		if (dev->devres_managed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 			WARN_ON(devres_destroy(dev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 						devm_input_device_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 						devm_input_device_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 						dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		input_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) EXPORT_SYMBOL(input_free_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)  * input_set_timestamp - set timestamp for input events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930)  * @dev: input device to set timestamp for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931)  * @timestamp: the time at which the event has occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)  *   in CLOCK_MONOTONIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  * This function is intended to provide to the input system a more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)  * accurate time of when an event actually occurred. The driver should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)  * call this function as soon as a timestamp is acquired ensuring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)  * clock conversions in input_set_timestamp are done correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)  * The system entering suspend state between timestamp acquisition and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  * calling input_set_timestamp can result in inaccurate conversions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) void input_set_timestamp(struct input_dev *dev, ktime_t timestamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	dev->timestamp[INPUT_CLK_MONO] = timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 	dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 							   TK_OFFS_BOOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) EXPORT_SYMBOL(input_set_timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)  * input_get_timestamp - get timestamp for input events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953)  * @dev: input device to get timestamp from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)  * A valid timestamp is a timestamp of non-zero value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) ktime_t *input_get_timestamp(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	const ktime_t invalid_timestamp = ktime_set(0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 	if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 		input_set_timestamp(dev, ktime_get());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	return dev->timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) EXPORT_SYMBOL(input_get_timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969)  * input_set_capability - mark device as capable of a certain event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)  * @dev: device that is capable of emitting or accepting event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)  * @type: type of the event (EV_KEY, EV_REL, etc...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)  * @code: event code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)  * In addition to setting up corresponding bit in appropriate capability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)  * bitmap the function also adjusts dev->evbit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	case EV_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		__set_bit(code, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 	case EV_REL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		__set_bit(code, dev->relbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	case EV_ABS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		input_alloc_absinfo(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		if (!dev->absinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		__set_bit(code, dev->absbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	case EV_MSC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		__set_bit(code, dev->mscbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	case EV_SW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		__set_bit(code, dev->swbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	case EV_LED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		__set_bit(code, dev->ledbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 	case EV_SND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		__set_bit(code, dev->sndbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 	case EV_FF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		__set_bit(code, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	case EV_PWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		/* do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		pr_err("%s: unknown type %u (code %u)\n", __func__, type, code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	__set_bit(type, dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) EXPORT_SYMBOL(input_set_capability);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) static unsigned int input_estimate_events_per_packet(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	int mt_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	unsigned int events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	if (dev->mt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		mt_slots = dev->mt->num_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	} else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			   dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		mt_slots = clamp(mt_slots, 2, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	} else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		mt_slots = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		mt_slots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	if (test_bit(EV_ABS, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 		for_each_set_bit(i, dev->absbit, ABS_CNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 			events += input_is_mt_axis(i) ? mt_slots : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	if (test_bit(EV_REL, dev->evbit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		events += bitmap_weight(dev->relbit, REL_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	/* Make room for KEY and MSC events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	events += 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	return events;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) #define INPUT_CLEANSE_BITMASK(dev, type, bits)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 		if (!test_bit(EV_##type, dev->evbit))			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 			memset(dev->bits##bit, 0,			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 				sizeof(dev->bits##bit));		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) static void input_cleanse_bitmasks(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 	INPUT_CLEANSE_BITMASK(dev, KEY, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	INPUT_CLEANSE_BITMASK(dev, REL, rel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	INPUT_CLEANSE_BITMASK(dev, ABS, abs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 	INPUT_CLEANSE_BITMASK(dev, MSC, msc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 	INPUT_CLEANSE_BITMASK(dev, LED, led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 	INPUT_CLEANSE_BITMASK(dev, SND, snd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 	INPUT_CLEANSE_BITMASK(dev, FF, ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 	INPUT_CLEANSE_BITMASK(dev, SW, sw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) static void __input_unregister_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	struct input_handle *handle, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	input_disconnect_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	mutex_lock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		handle->handler->disconnect(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	WARN_ON(!list_empty(&dev->h_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	del_timer_sync(&dev->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	list_del_init(&dev->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	input_wakeup_procfs_readers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	mutex_unlock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	device_del(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) static void devm_input_device_unregister(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	struct input_devres *devres = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	struct input_dev *input = devres->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	dev_dbg(dev, "%s: unregistering device %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		__func__, dev_name(&input->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	__input_unregister_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115)  * input_enable_softrepeat - enable software autorepeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)  * @dev: input device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117)  * @delay: repeat delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118)  * @period: repeat period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120)  * Enable software autorepeat on the input device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) void input_enable_softrepeat(struct input_dev *dev, int delay, int period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	dev->timer.function = input_repeat_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	dev->rep[REP_DELAY] = delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	dev->rep[REP_PERIOD] = period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) EXPORT_SYMBOL(input_enable_softrepeat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131)  * input_register_device - register device with input core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)  * @dev: device to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134)  * This function registers device with input core. The device must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135)  * allocated with input_allocate_device() and all it's capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)  * set up before registering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137)  * If function fails the device must be freed with input_free_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138)  * Once device has been successfully registered it can be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139)  * with input_unregister_device(); input_free_device() should not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)  * called in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)  * Note that this function is also used to register managed input devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)  * (ones allocated with devm_input_allocate_device()). Such managed input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)  * devices need not be explicitly unregistered or freed, their tear down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * is controlled by the devres infrastructure. It is also worth noting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  * that tear down of managed input devices is internally a 2-step process:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147)  * registered managed input device is first unregistered, but stays in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148)  * memory and can still handle input_event() calls (although events will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)  * not be delivered anywhere). The freeing of managed input device will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  * happen later, when devres stack is unwound to the point where device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  * allocation was made.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) int input_register_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	struct input_devres *devres = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	struct input_handler *handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	unsigned int packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	const char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 		dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 			"Absolute device without dev->absinfo, refusing to register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	if (dev->devres_managed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		devres = devres_alloc(devm_input_device_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 				      sizeof(*devres), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		if (!devres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		devres->input = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 	/* Every input device generates EV_SYN/SYN_REPORT events. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	__set_bit(EV_SYN, dev->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 	/* KEY_RESERVED is not supposed to be transmitted to userspace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 	__clear_bit(KEY_RESERVED, dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 	/* Make sure that bitmasks not mentioned in dev->evbit are clean. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	input_cleanse_bitmasks(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	packet_size = input_estimate_events_per_packet(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	if (dev->hint_events_per_packet < packet_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 		dev->hint_events_per_packet = packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	dev->max_vals = dev->hint_events_per_packet + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 	dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 	if (!dev->vals) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		goto err_devres_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	 * If delay and period are pre-set by the driver, then autorepeating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	 * is handled by the driver itself and we don't do it in input.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		input_enable_softrepeat(dev, 250, 33);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	if (!dev->getkeycode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		dev->getkeycode = input_default_getkeycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	if (!dev->setkeycode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 		dev->setkeycode = input_default_setkeycode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 	if (dev->poller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 		input_dev_poller_finalize(dev->poller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	error = device_add(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		goto err_free_vals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	pr_info("%s as %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		dev->name ? dev->name : "Unspecified device",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		path ? path : "N/A");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	kfree(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	error = mutex_lock_interruptible(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		goto err_device_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	list_add_tail(&dev->node, &input_dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	list_for_each_entry(handler, &input_handler_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 		input_attach_handler(dev, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	input_wakeup_procfs_readers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	mutex_unlock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	if (dev->devres_managed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 			__func__, dev_name(&dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 		devres_add(dev->dev.parent, devres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) err_device_del:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	device_del(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) err_free_vals:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	kfree(dev->vals);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	dev->vals = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) err_devres_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	devres_free(devres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) EXPORT_SYMBOL(input_register_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254)  * input_unregister_device - unregister previously registered device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255)  * @dev: device to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257)  * This function unregisters an input device. Once device is unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258)  * the caller should not try to access it as it may get freed at any moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) void input_unregister_device(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	if (dev->devres_managed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		WARN_ON(devres_destroy(dev->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 					devm_input_device_unregister,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 					devm_input_device_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 					dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		__input_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		 * We do not do input_put_device() here because it will be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		 * when 2nd devres fires up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		__input_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		input_put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) EXPORT_SYMBOL(input_unregister_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)  * input_register_handler - register a new input handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281)  * @handler: handler to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)  * This function registers a new input handler (interface) for input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284)  * devices in the system and attaches it to all input devices that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285)  * are compatible with the handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) int input_register_handler(struct input_handler *handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	error = mutex_lock_interruptible(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	INIT_LIST_HEAD(&handler->h_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	list_add_tail(&handler->node, &input_handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	list_for_each_entry(dev, &input_dev_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 		input_attach_handler(dev, handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	input_wakeup_procfs_readers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	mutex_unlock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) EXPORT_SYMBOL(input_register_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)  * input_unregister_handler - unregisters an input handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)  * @handler: handler to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314)  * This function disconnects a handler from its input devices and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)  * removes it from lists of known handlers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) void input_unregister_handler(struct input_handler *handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	struct input_handle *handle, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	mutex_lock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 		handler->disconnect(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	WARN_ON(!list_empty(&handler->h_list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	list_del_init(&handler->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 	input_wakeup_procfs_readers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	mutex_unlock(&input_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) EXPORT_SYMBOL(input_unregister_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  * input_handler_for_each_handle - handle iterator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)  * @handler: input handler to iterate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)  * @data: data for the callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339)  * @fn: function to be called for each handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)  * Iterate over @bus's list of devices, and call @fn for each, passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)  * it @data and stop when @fn returns a non-zero value. The function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)  * using RCU to traverse the list and therefore may be using in atomic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344)  * contexts. The @fn callback is invoked from RCU critical section and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)  * thus must not sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) int input_handler_for_each_handle(struct input_handler *handler, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 				  int (*fn)(struct input_handle *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	struct input_handle *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 	list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		retval = fn(handle, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) EXPORT_SYMBOL(input_handler_for_each_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368)  * input_register_handle - register a new input handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369)  * @handle: handle to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371)  * This function puts a new input handle onto device's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372)  * and handler's lists so that events can flow through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373)  * it once it is opened using input_open_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375)  * This function is supposed to be called from handler's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376)  * connect() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) int input_register_handle(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 	struct input_handler *handler = handle->handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	 * We take dev->mutex here to prevent race with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	 * input_release_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	error = mutex_lock_interruptible(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 	 * Filters go to the head of the list, normal handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	 * to the tail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 	if (handler->filter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 		list_add_rcu(&handle->d_node, &dev->h_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		list_add_tail_rcu(&handle->d_node, &dev->h_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	 * Since we are supposed to be called from ->connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	 * which is mutually exclusive with ->disconnect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	 * we can't be racing with input_unregister_handle()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	 * and so separate lock is not needed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	list_add_tail_rcu(&handle->h_node, &handler->h_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	if (handler->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 		handler->start(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) EXPORT_SYMBOL(input_register_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419)  * input_unregister_handle - unregister an input handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)  * @handle: handle to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422)  * This function removes input handle from device's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)  * and handler's lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425)  * This function is supposed to be called from handler's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426)  * disconnect() method.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) void input_unregister_handle(struct input_handle *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	struct input_dev *dev = handle->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	list_del_rcu(&handle->h_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	 * Take dev->mutex to prevent race with input_release_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	mutex_lock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 	list_del_rcu(&handle->d_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 	synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) EXPORT_SYMBOL(input_unregister_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446)  * input_get_new_minor - allocates a new input minor number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447)  * @legacy_base: beginning or the legacy range to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448)  * @legacy_num: size of legacy range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)  * @allow_dynamic: whether we can also take ID from the dynamic range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)  * This function allocates a new device minor for from input major namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452)  * Caller can request legacy minor by specifying @legacy_base and @legacy_num
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453)  * parameters and whether ID can be allocated from dynamic range if there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)  * no free IDs in legacy range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) int input_get_new_minor(int legacy_base, unsigned int legacy_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 			bool allow_dynamic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	 * This function should be called from input handler's ->connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	 * methods, which are serialized with input_mutex, so no additional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	 * locking is needed here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	if (legacy_base >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 		int minor = ida_simple_get(&input_ida,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 					   legacy_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 					   legacy_base + legacy_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 					   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 		if (minor >= 0 || !allow_dynamic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 			return minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	return ida_simple_get(&input_ida,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 			      INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 			      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) EXPORT_SYMBOL(input_get_new_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480)  * input_free_minor - release previously allocated minor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481)  * @minor: minor to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483)  * This function releases previously allocated input minor so that it can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484)  * reused later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) void input_free_minor(unsigned int minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	ida_simple_remove(&input_ida, minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) EXPORT_SYMBOL(input_free_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) static int __init input_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	err = class_register(&input_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		pr_err("unable to register input_dev class\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	err = input_proc_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 	err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 				     INPUT_MAX_CHAR_DEVICES, "input");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		pr_err("unable to register char major %d", INPUT_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515)  fail2:	input_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)  fail1:	class_unregister(&input_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) static void __exit input_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	input_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 				 INPUT_MAX_CHAR_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	class_unregister(&input_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) subsys_initcall(input_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) module_exit(input_exit);