xref: /openbmc/linux/drivers/input/input-poller.c (revision d69f0a43)
1e95656eaSDmitry Torokhov // SPDX-License-Identifier: GPL-2.0-only
2e95656eaSDmitry Torokhov /*
3e95656eaSDmitry Torokhov  * Support for polling mode for input devices.
4e95656eaSDmitry Torokhov  */
5e95656eaSDmitry Torokhov 
6e95656eaSDmitry Torokhov #include <linux/device.h>
7e95656eaSDmitry Torokhov #include <linux/input.h>
8e95656eaSDmitry Torokhov #include <linux/jiffies.h>
9e95656eaSDmitry Torokhov #include <linux/mutex.h>
10e95656eaSDmitry Torokhov #include <linux/slab.h>
11e95656eaSDmitry Torokhov #include <linux/types.h>
12e95656eaSDmitry Torokhov #include <linux/workqueue.h>
13e95656eaSDmitry Torokhov #include "input-poller.h"
14e95656eaSDmitry Torokhov 
15e95656eaSDmitry Torokhov struct input_dev_poller {
16e95656eaSDmitry Torokhov 	void (*poll)(struct input_dev *dev);
17e95656eaSDmitry Torokhov 
18e95656eaSDmitry Torokhov 	unsigned int poll_interval; /* msec */
19e95656eaSDmitry Torokhov 	unsigned int poll_interval_max; /* msec */
20e95656eaSDmitry Torokhov 	unsigned int poll_interval_min; /* msec */
21e95656eaSDmitry Torokhov 
22e95656eaSDmitry Torokhov 	struct input_dev *input;
23e95656eaSDmitry Torokhov 	struct delayed_work work;
24e95656eaSDmitry Torokhov };
25e95656eaSDmitry Torokhov 
input_dev_poller_queue_work(struct input_dev_poller * poller)26e95656eaSDmitry Torokhov static void input_dev_poller_queue_work(struct input_dev_poller *poller)
27e95656eaSDmitry Torokhov {
28e95656eaSDmitry Torokhov 	unsigned long delay;
29e95656eaSDmitry Torokhov 
30e95656eaSDmitry Torokhov 	delay = msecs_to_jiffies(poller->poll_interval);
31e95656eaSDmitry Torokhov 	if (delay >= HZ)
32e95656eaSDmitry Torokhov 		delay = round_jiffies_relative(delay);
33e95656eaSDmitry Torokhov 
34e95656eaSDmitry Torokhov 	queue_delayed_work(system_freezable_wq, &poller->work, delay);
35e95656eaSDmitry Torokhov }
36e95656eaSDmitry Torokhov 
input_dev_poller_work(struct work_struct * work)37e95656eaSDmitry Torokhov static void input_dev_poller_work(struct work_struct *work)
38e95656eaSDmitry Torokhov {
39e95656eaSDmitry Torokhov 	struct input_dev_poller *poller =
40e95656eaSDmitry Torokhov 		container_of(work, struct input_dev_poller, work.work);
41e95656eaSDmitry Torokhov 
42e95656eaSDmitry Torokhov 	poller->poll(poller->input);
43e95656eaSDmitry Torokhov 	input_dev_poller_queue_work(poller);
44e95656eaSDmitry Torokhov }
45e95656eaSDmitry Torokhov 
input_dev_poller_finalize(struct input_dev_poller * poller)46e95656eaSDmitry Torokhov void input_dev_poller_finalize(struct input_dev_poller *poller)
47e95656eaSDmitry Torokhov {
48e95656eaSDmitry Torokhov 	if (!poller->poll_interval)
49e95656eaSDmitry Torokhov 		poller->poll_interval = 500;
50e95656eaSDmitry Torokhov 	if (!poller->poll_interval_max)
51e95656eaSDmitry Torokhov 		poller->poll_interval_max = poller->poll_interval;
52e95656eaSDmitry Torokhov }
53e95656eaSDmitry Torokhov 
input_dev_poller_start(struct input_dev_poller * poller)54e95656eaSDmitry Torokhov void input_dev_poller_start(struct input_dev_poller *poller)
55e95656eaSDmitry Torokhov {
56e95656eaSDmitry Torokhov 	/* Only start polling if polling is enabled */
57e95656eaSDmitry Torokhov 	if (poller->poll_interval > 0) {
58e95656eaSDmitry Torokhov 		poller->poll(poller->input);
59e95656eaSDmitry Torokhov 		input_dev_poller_queue_work(poller);
60e95656eaSDmitry Torokhov 	}
61e95656eaSDmitry Torokhov }
62e95656eaSDmitry Torokhov 
input_dev_poller_stop(struct input_dev_poller * poller)63e95656eaSDmitry Torokhov void input_dev_poller_stop(struct input_dev_poller *poller)
64e95656eaSDmitry Torokhov {
65e95656eaSDmitry Torokhov 	cancel_delayed_work_sync(&poller->work);
66e95656eaSDmitry Torokhov }
67e95656eaSDmitry Torokhov 
input_setup_polling(struct input_dev * dev,void (* poll_fn)(struct input_dev * dev))68e95656eaSDmitry Torokhov int input_setup_polling(struct input_dev *dev,
69e95656eaSDmitry Torokhov 			void (*poll_fn)(struct input_dev *dev))
70e95656eaSDmitry Torokhov {
71e95656eaSDmitry Torokhov 	struct input_dev_poller *poller;
72e95656eaSDmitry Torokhov 
73e95656eaSDmitry Torokhov 	poller = kzalloc(sizeof(*poller), GFP_KERNEL);
74e95656eaSDmitry Torokhov 	if (!poller) {
75e95656eaSDmitry Torokhov 		/*
76e95656eaSDmitry Torokhov 		 * We want to show message even though kzalloc() may have
77e95656eaSDmitry Torokhov 		 * printed backtrace as knowing what instance of input
78e95656eaSDmitry Torokhov 		 * device we were dealing with is helpful.
79e95656eaSDmitry Torokhov 		 */
80e95656eaSDmitry Torokhov 		dev_err(dev->dev.parent ?: &dev->dev,
81e95656eaSDmitry Torokhov 			"%s: unable to allocate poller structure\n", __func__);
82e95656eaSDmitry Torokhov 		return -ENOMEM;
83e95656eaSDmitry Torokhov 	}
84e95656eaSDmitry Torokhov 
85e95656eaSDmitry Torokhov 	INIT_DELAYED_WORK(&poller->work, input_dev_poller_work);
86e95656eaSDmitry Torokhov 	poller->input = dev;
87e95656eaSDmitry Torokhov 	poller->poll = poll_fn;
88e95656eaSDmitry Torokhov 
89e95656eaSDmitry Torokhov 	dev->poller = poller;
90e95656eaSDmitry Torokhov 	return 0;
91e95656eaSDmitry Torokhov }
92e95656eaSDmitry Torokhov EXPORT_SYMBOL(input_setup_polling);
93e95656eaSDmitry Torokhov 
input_dev_ensure_poller(struct input_dev * dev)94e95656eaSDmitry Torokhov static bool input_dev_ensure_poller(struct input_dev *dev)
95e95656eaSDmitry Torokhov {
96e95656eaSDmitry Torokhov 	if (!dev->poller) {
97e95656eaSDmitry Torokhov 		dev_err(dev->dev.parent ?: &dev->dev,
98e95656eaSDmitry Torokhov 			"poller structure has not been set up\n");
99e95656eaSDmitry Torokhov 		return false;
100e95656eaSDmitry Torokhov 	}
101e95656eaSDmitry Torokhov 
102e95656eaSDmitry Torokhov 	return true;
103e95656eaSDmitry Torokhov }
104e95656eaSDmitry Torokhov 
input_set_poll_interval(struct input_dev * dev,unsigned int interval)105e95656eaSDmitry Torokhov void input_set_poll_interval(struct input_dev *dev, unsigned int interval)
106e95656eaSDmitry Torokhov {
107e95656eaSDmitry Torokhov 	if (input_dev_ensure_poller(dev))
108e95656eaSDmitry Torokhov 		dev->poller->poll_interval = interval;
109e95656eaSDmitry Torokhov }
110e95656eaSDmitry Torokhov EXPORT_SYMBOL(input_set_poll_interval);
111e95656eaSDmitry Torokhov 
input_set_min_poll_interval(struct input_dev * dev,unsigned int interval)112e95656eaSDmitry Torokhov void input_set_min_poll_interval(struct input_dev *dev, unsigned int interval)
113e95656eaSDmitry Torokhov {
114e95656eaSDmitry Torokhov 	if (input_dev_ensure_poller(dev))
115e95656eaSDmitry Torokhov 		dev->poller->poll_interval_min = interval;
116e95656eaSDmitry Torokhov }
117e95656eaSDmitry Torokhov EXPORT_SYMBOL(input_set_min_poll_interval);
118e95656eaSDmitry Torokhov 
input_set_max_poll_interval(struct input_dev * dev,unsigned int interval)119e95656eaSDmitry Torokhov void input_set_max_poll_interval(struct input_dev *dev, unsigned int interval)
120e95656eaSDmitry Torokhov {
121e95656eaSDmitry Torokhov 	if (input_dev_ensure_poller(dev))
122e95656eaSDmitry Torokhov 		dev->poller->poll_interval_max = interval;
123e95656eaSDmitry Torokhov }
124e95656eaSDmitry Torokhov EXPORT_SYMBOL(input_set_max_poll_interval);
125e95656eaSDmitry Torokhov 
input_get_poll_interval(struct input_dev * dev)126894616f7SDmitry Torokhov int input_get_poll_interval(struct input_dev *dev)
127894616f7SDmitry Torokhov {
128894616f7SDmitry Torokhov 	if (!dev->poller)
129894616f7SDmitry Torokhov 		return -EINVAL;
130894616f7SDmitry Torokhov 
131894616f7SDmitry Torokhov 	return dev->poller->poll_interval;
132894616f7SDmitry Torokhov }
133894616f7SDmitry Torokhov EXPORT_SYMBOL(input_get_poll_interval);
134894616f7SDmitry Torokhov 
135e95656eaSDmitry Torokhov /* SYSFS interface */
136e95656eaSDmitry Torokhov 
input_dev_get_poll_interval(struct device * dev,struct device_attribute * attr,char * buf)137e95656eaSDmitry Torokhov static ssize_t input_dev_get_poll_interval(struct device *dev,
138e95656eaSDmitry Torokhov 					   struct device_attribute *attr,
139e95656eaSDmitry Torokhov 					   char *buf)
140e95656eaSDmitry Torokhov {
141e95656eaSDmitry Torokhov 	struct input_dev *input = to_input_dev(dev);
142e95656eaSDmitry Torokhov 
143e95656eaSDmitry Torokhov 	return sprintf(buf, "%d\n", input->poller->poll_interval);
144e95656eaSDmitry Torokhov }
145e95656eaSDmitry Torokhov 
input_dev_set_poll_interval(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)146e95656eaSDmitry Torokhov static ssize_t input_dev_set_poll_interval(struct device *dev,
147e95656eaSDmitry Torokhov 					   struct device_attribute *attr,
148e95656eaSDmitry Torokhov 					   const char *buf, size_t count)
149e95656eaSDmitry Torokhov {
150e95656eaSDmitry Torokhov 	struct input_dev *input = to_input_dev(dev);
151e95656eaSDmitry Torokhov 	struct input_dev_poller *poller = input->poller;
152e95656eaSDmitry Torokhov 	unsigned int interval;
153e95656eaSDmitry Torokhov 	int err;
154e95656eaSDmitry Torokhov 
155e95656eaSDmitry Torokhov 	err = kstrtouint(buf, 0, &interval);
156e95656eaSDmitry Torokhov 	if (err)
157e95656eaSDmitry Torokhov 		return err;
158e95656eaSDmitry Torokhov 
159e95656eaSDmitry Torokhov 	if (interval < poller->poll_interval_min)
160e95656eaSDmitry Torokhov 		return -EINVAL;
161e95656eaSDmitry Torokhov 
162e95656eaSDmitry Torokhov 	if (interval > poller->poll_interval_max)
163e95656eaSDmitry Torokhov 		return -EINVAL;
164e95656eaSDmitry Torokhov 
165e95656eaSDmitry Torokhov 	mutex_lock(&input->mutex);
166e95656eaSDmitry Torokhov 
167e95656eaSDmitry Torokhov 	poller->poll_interval = interval;
168e95656eaSDmitry Torokhov 
169*d69f0a43SAndrzej Pietrasiewicz 	if (input_device_enabled(input)) {
170e95656eaSDmitry Torokhov 		cancel_delayed_work_sync(&poller->work);
171e95656eaSDmitry Torokhov 		if (poller->poll_interval > 0)
172e95656eaSDmitry Torokhov 			input_dev_poller_queue_work(poller);
173e95656eaSDmitry Torokhov 	}
174e95656eaSDmitry Torokhov 
175e95656eaSDmitry Torokhov 	mutex_unlock(&input->mutex);
176e95656eaSDmitry Torokhov 
177e95656eaSDmitry Torokhov 	return count;
178e95656eaSDmitry Torokhov }
179e95656eaSDmitry Torokhov 
180e95656eaSDmitry Torokhov static DEVICE_ATTR(poll, 0644,
181e95656eaSDmitry Torokhov 		   input_dev_get_poll_interval, input_dev_set_poll_interval);
182e95656eaSDmitry Torokhov 
input_dev_get_poll_max(struct device * dev,struct device_attribute * attr,char * buf)183e95656eaSDmitry Torokhov static ssize_t input_dev_get_poll_max(struct device *dev,
184e95656eaSDmitry Torokhov 				      struct device_attribute *attr, char *buf)
185e95656eaSDmitry Torokhov {
186e95656eaSDmitry Torokhov 	struct input_dev *input = to_input_dev(dev);
187e95656eaSDmitry Torokhov 
188e95656eaSDmitry Torokhov 	return sprintf(buf, "%d\n", input->poller->poll_interval_max);
189e95656eaSDmitry Torokhov }
190e95656eaSDmitry Torokhov 
191e95656eaSDmitry Torokhov static DEVICE_ATTR(max, 0444, input_dev_get_poll_max, NULL);
192e95656eaSDmitry Torokhov 
input_dev_get_poll_min(struct device * dev,struct device_attribute * attr,char * buf)193e95656eaSDmitry Torokhov static ssize_t input_dev_get_poll_min(struct device *dev,
194e95656eaSDmitry Torokhov 				     struct device_attribute *attr, char *buf)
195e95656eaSDmitry Torokhov {
196e95656eaSDmitry Torokhov 	struct input_dev *input = to_input_dev(dev);
197e95656eaSDmitry Torokhov 
198e95656eaSDmitry Torokhov 	return sprintf(buf, "%d\n", input->poller->poll_interval_min);
199e95656eaSDmitry Torokhov }
200e95656eaSDmitry Torokhov 
201e95656eaSDmitry Torokhov static DEVICE_ATTR(min, 0444, input_dev_get_poll_min, NULL);
202e95656eaSDmitry Torokhov 
input_poller_attrs_visible(struct kobject * kobj,struct attribute * attr,int n)203e95656eaSDmitry Torokhov static umode_t input_poller_attrs_visible(struct kobject *kobj,
204e95656eaSDmitry Torokhov 					  struct attribute *attr, int n)
205e95656eaSDmitry Torokhov {
206e95656eaSDmitry Torokhov 	struct device *dev = kobj_to_dev(kobj);
207e95656eaSDmitry Torokhov 	struct input_dev *input = to_input_dev(dev);
208e95656eaSDmitry Torokhov 
209e95656eaSDmitry Torokhov 	return input->poller ? attr->mode : 0;
210e95656eaSDmitry Torokhov }
211e95656eaSDmitry Torokhov 
212e95656eaSDmitry Torokhov static struct attribute *input_poller_attrs[] = {
213e95656eaSDmitry Torokhov 	&dev_attr_poll.attr,
214e95656eaSDmitry Torokhov 	&dev_attr_max.attr,
215e95656eaSDmitry Torokhov 	&dev_attr_min.attr,
216e95656eaSDmitry Torokhov 	NULL
217e95656eaSDmitry Torokhov };
218e95656eaSDmitry Torokhov 
219e95656eaSDmitry Torokhov struct attribute_group input_poller_attribute_group = {
220e95656eaSDmitry Torokhov 	.is_visible	= input_poller_attrs_visible,
221e95656eaSDmitry Torokhov 	.attrs		= input_poller_attrs,
222e95656eaSDmitry Torokhov };
223