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