1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Support for polling mode for input devices.
4 */
5
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"
14
15 struct input_dev_poller {
16 void (*poll)(struct input_dev *dev);
17
18 unsigned int poll_interval; /* msec */
19 unsigned int poll_interval_max; /* msec */
20 unsigned int poll_interval_min; /* msec */
21
22 struct input_dev *input;
23 struct delayed_work work;
24 };
25
input_dev_poller_queue_work(struct input_dev_poller * poller)26 static void input_dev_poller_queue_work(struct input_dev_poller *poller)
27 {
28 unsigned long delay;
29
30 delay = msecs_to_jiffies(poller->poll_interval);
31 if (delay >= HZ)
32 delay = round_jiffies_relative(delay);
33
34 queue_delayed_work(system_freezable_wq, &poller->work, delay);
35 }
36
input_dev_poller_work(struct work_struct * work)37 static void input_dev_poller_work(struct work_struct *work)
38 {
39 struct input_dev_poller *poller =
40 container_of(work, struct input_dev_poller, work.work);
41
42 poller->poll(poller->input);
43 input_dev_poller_queue_work(poller);
44 }
45
input_dev_poller_finalize(struct input_dev_poller * poller)46 void input_dev_poller_finalize(struct input_dev_poller *poller)
47 {
48 if (!poller->poll_interval)
49 poller->poll_interval = 500;
50 if (!poller->poll_interval_max)
51 poller->poll_interval_max = poller->poll_interval;
52 }
53
input_dev_poller_start(struct input_dev_poller * poller)54 void input_dev_poller_start(struct input_dev_poller *poller)
55 {
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);
60 }
61 }
62
input_dev_poller_stop(struct input_dev_poller * poller)63 void input_dev_poller_stop(struct input_dev_poller *poller)
64 {
65 cancel_delayed_work_sync(&poller->work);
66 }
67
input_setup_polling(struct input_dev * dev,void (* poll_fn)(struct input_dev * dev))68 int input_setup_polling(struct input_dev *dev,
69 void (*poll_fn)(struct input_dev *dev))
70 {
71 struct input_dev_poller *poller;
72
73 poller = kzalloc(sizeof(*poller), GFP_KERNEL);
74 if (!poller) {
75 /*
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.
79 */
80 dev_err(dev->dev.parent ?: &dev->dev,
81 "%s: unable to allocate poller structure\n", __func__);
82 return -ENOMEM;
83 }
84
85 INIT_DELAYED_WORK(&poller->work, input_dev_poller_work);
86 poller->input = dev;
87 poller->poll = poll_fn;
88
89 dev->poller = poller;
90 return 0;
91 }
92 EXPORT_SYMBOL(input_setup_polling);
93
input_dev_ensure_poller(struct input_dev * dev)94 static bool input_dev_ensure_poller(struct input_dev *dev)
95 {
96 if (!dev->poller) {
97 dev_err(dev->dev.parent ?: &dev->dev,
98 "poller structure has not been set up\n");
99 return false;
100 }
101
102 return true;
103 }
104
input_set_poll_interval(struct input_dev * dev,unsigned int interval)105 void input_set_poll_interval(struct input_dev *dev, unsigned int interval)
106 {
107 if (input_dev_ensure_poller(dev))
108 dev->poller->poll_interval = interval;
109 }
110 EXPORT_SYMBOL(input_set_poll_interval);
111
input_set_min_poll_interval(struct input_dev * dev,unsigned int interval)112 void input_set_min_poll_interval(struct input_dev *dev, unsigned int interval)
113 {
114 if (input_dev_ensure_poller(dev))
115 dev->poller->poll_interval_min = interval;
116 }
117 EXPORT_SYMBOL(input_set_min_poll_interval);
118
input_set_max_poll_interval(struct input_dev * dev,unsigned int interval)119 void input_set_max_poll_interval(struct input_dev *dev, unsigned int interval)
120 {
121 if (input_dev_ensure_poller(dev))
122 dev->poller->poll_interval_max = interval;
123 }
124 EXPORT_SYMBOL(input_set_max_poll_interval);
125
126 /* SYSFS interface */
127
input_dev_get_poll_interval(struct device * dev,struct device_attribute * attr,char * buf)128 static ssize_t input_dev_get_poll_interval(struct device *dev,
129 struct device_attribute *attr,
130 char *buf)
131 {
132 struct input_dev *input = to_input_dev(dev);
133
134 return sprintf(buf, "%d\n", input->poller->poll_interval);
135 }
136
input_dev_set_poll_interval(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)137 static ssize_t input_dev_set_poll_interval(struct device *dev,
138 struct device_attribute *attr,
139 const char *buf, size_t count)
140 {
141 struct input_dev *input = to_input_dev(dev);
142 struct input_dev_poller *poller = input->poller;
143 unsigned int interval;
144 int err;
145
146 err = kstrtouint(buf, 0, &interval);
147 if (err)
148 return err;
149
150 if (interval < poller->poll_interval_min)
151 return -EINVAL;
152
153 if (interval > poller->poll_interval_max)
154 return -EINVAL;
155
156 mutex_lock(&input->mutex);
157
158 poller->poll_interval = interval;
159
160 if (input->users) {
161 cancel_delayed_work_sync(&poller->work);
162 if (poller->poll_interval > 0)
163 input_dev_poller_queue_work(poller);
164 }
165
166 mutex_unlock(&input->mutex);
167
168 return count;
169 }
170
171 static DEVICE_ATTR(poll, 0644,
172 input_dev_get_poll_interval, input_dev_set_poll_interval);
173
input_dev_get_poll_max(struct device * dev,struct device_attribute * attr,char * buf)174 static ssize_t input_dev_get_poll_max(struct device *dev,
175 struct device_attribute *attr, char *buf)
176 {
177 struct input_dev *input = to_input_dev(dev);
178
179 return sprintf(buf, "%d\n", input->poller->poll_interval_max);
180 }
181
182 static DEVICE_ATTR(max, 0444, input_dev_get_poll_max, NULL);
183
input_dev_get_poll_min(struct device * dev,struct device_attribute * attr,char * buf)184 static ssize_t input_dev_get_poll_min(struct device *dev,
185 struct device_attribute *attr, char *buf)
186 {
187 struct input_dev *input = to_input_dev(dev);
188
189 return sprintf(buf, "%d\n", input->poller->poll_interval_min);
190 }
191
192 static DEVICE_ATTR(min, 0444, input_dev_get_poll_min, NULL);
193
input_poller_attrs_visible(struct kobject * kobj,struct attribute * attr,int n)194 static umode_t input_poller_attrs_visible(struct kobject *kobj,
195 struct attribute *attr, int n)
196 {
197 struct device *dev = kobj_to_dev(kobj);
198 struct input_dev *input = to_input_dev(dev);
199
200 return input->poller ? attr->mode : 0;
201 }
202
203 static struct attribute *input_poller_attrs[] = {
204 &dev_attr_poll.attr,
205 &dev_attr_max.attr,
206 &dev_attr_min.attr,
207 NULL
208 };
209
210 struct attribute_group input_poller_attribute_group = {
211 .is_visible = input_poller_attrs_visible,
212 .attrs = input_poller_attrs,
213 };
214