1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support for polling mode for input devices.
6 #include <linux/device.h>
7 #include <linux/input.h>
8 #include <linux/jiffies.h>
9 #include <linux/mutex.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/workqueue.h>
13 #include "input-poller.h"
15 struct input_dev_poller
{
16 void (*poll
)(struct input_dev
*dev
);
18 unsigned int poll_interval
; /* msec */
19 unsigned int poll_interval_max
; /* msec */
20 unsigned int poll_interval_min
; /* msec */
22 struct input_dev
*input
;
23 struct delayed_work work
;
26 static void input_dev_poller_queue_work(struct input_dev_poller
*poller
)
30 delay
= msecs_to_jiffies(poller
->poll_interval
);
32 delay
= round_jiffies_relative(delay
);
34 queue_delayed_work(system_freezable_wq
, &poller
->work
, delay
);
37 static void input_dev_poller_work(struct work_struct
*work
)
39 struct input_dev_poller
*poller
=
40 container_of(work
, struct input_dev_poller
, work
.work
);
42 poller
->poll(poller
->input
);
43 input_dev_poller_queue_work(poller
);
46 void input_dev_poller_finalize(struct input_dev_poller
*poller
)
48 if (!poller
->poll_interval
)
49 poller
->poll_interval
= 500;
50 if (!poller
->poll_interval_max
)
51 poller
->poll_interval_max
= poller
->poll_interval
;
54 void input_dev_poller_start(struct input_dev_poller
*poller
)
56 /* Only start polling if polling is enabled */
57 if (poller
->poll_interval
> 0) {
58 poller
->poll(poller
->input
);
59 input_dev_poller_queue_work(poller
);
63 void input_dev_poller_stop(struct input_dev_poller
*poller
)
65 cancel_delayed_work_sync(&poller
->work
);
68 int input_setup_polling(struct input_dev
*dev
,
69 void (*poll_fn
)(struct input_dev
*dev
))
71 struct input_dev_poller
*poller
;
73 poller
= kzalloc(sizeof(*poller
), GFP_KERNEL
);
76 * We want to show message even though kzalloc() may have
77 * printed backtrace as knowing what instance of input
78 * device we were dealing with is helpful.
80 dev_err(dev
->dev
.parent
?: &dev
->dev
,
81 "%s: unable to allocate poller structure\n", __func__
);
85 INIT_DELAYED_WORK(&poller
->work
, input_dev_poller_work
);
87 poller
->poll
= poll_fn
;
92 EXPORT_SYMBOL(input_setup_polling
);
94 static bool input_dev_ensure_poller(struct input_dev
*dev
)
97 dev_err(dev
->dev
.parent
?: &dev
->dev
,
98 "poller structure has not been set up\n");
105 void input_set_poll_interval(struct input_dev
*dev
, unsigned int interval
)
107 if (input_dev_ensure_poller(dev
))
108 dev
->poller
->poll_interval
= interval
;
110 EXPORT_SYMBOL(input_set_poll_interval
);
112 void input_set_min_poll_interval(struct input_dev
*dev
, unsigned int interval
)
114 if (input_dev_ensure_poller(dev
))
115 dev
->poller
->poll_interval_min
= interval
;
117 EXPORT_SYMBOL(input_set_min_poll_interval
);
119 void input_set_max_poll_interval(struct input_dev
*dev
, unsigned int interval
)
121 if (input_dev_ensure_poller(dev
))
122 dev
->poller
->poll_interval_max
= interval
;
124 EXPORT_SYMBOL(input_set_max_poll_interval
);
126 int input_get_poll_interval(struct input_dev
*dev
)
131 return dev
->poller
->poll_interval
;
133 EXPORT_SYMBOL(input_get_poll_interval
);
135 /* SYSFS interface */
137 static ssize_t
input_dev_get_poll_interval(struct device
*dev
,
138 struct device_attribute
*attr
,
141 struct input_dev
*input
= to_input_dev(dev
);
143 return sprintf(buf
, "%d\n", input
->poller
->poll_interval
);
146 static ssize_t
input_dev_set_poll_interval(struct device
*dev
,
147 struct device_attribute
*attr
,
148 const char *buf
, size_t count
)
150 struct input_dev
*input
= to_input_dev(dev
);
151 struct input_dev_poller
*poller
= input
->poller
;
152 unsigned int interval
;
155 err
= kstrtouint(buf
, 0, &interval
);
159 if (interval
< poller
->poll_interval_min
)
162 if (interval
> poller
->poll_interval_max
)
165 mutex_lock(&input
->mutex
);
167 poller
->poll_interval
= interval
;
169 if (input_device_enabled(input
)) {
170 cancel_delayed_work_sync(&poller
->work
);
171 if (poller
->poll_interval
> 0)
172 input_dev_poller_queue_work(poller
);
175 mutex_unlock(&input
->mutex
);
180 static DEVICE_ATTR(poll
, 0644,
181 input_dev_get_poll_interval
, input_dev_set_poll_interval
);
183 static ssize_t
input_dev_get_poll_max(struct device
*dev
,
184 struct device_attribute
*attr
, char *buf
)
186 struct input_dev
*input
= to_input_dev(dev
);
188 return sprintf(buf
, "%d\n", input
->poller
->poll_interval_max
);
191 static DEVICE_ATTR(max
, 0444, input_dev_get_poll_max
, NULL
);
193 static ssize_t
input_dev_get_poll_min(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
196 struct input_dev
*input
= to_input_dev(dev
);
198 return sprintf(buf
, "%d\n", input
->poller
->poll_interval_min
);
201 static DEVICE_ATTR(min
, 0444, input_dev_get_poll_min
, NULL
);
203 static umode_t
input_poller_attrs_visible(struct kobject
*kobj
,
204 struct attribute
*attr
, int n
)
206 struct device
*dev
= kobj_to_dev(kobj
);
207 struct input_dev
*input
= to_input_dev(dev
);
209 return input
->poller
? attr
->mode
: 0;
212 static struct attribute
*input_poller_attrs
[] = {
219 struct attribute_group input_poller_attribute_group
= {
220 .is_visible
= input_poller_attrs_visible
,
221 .attrs
= input_poller_attrs
,