2 * Generic implementation of a polled input device
4 * Copyright (c) 2007 Dmitry Torokhov
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/jiffies.h>
14 #include <linux/slab.h>
15 #include <linux/mutex.h>
16 #include <linux/workqueue.h>
17 #include <linux/input-polldev.h>
19 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
20 MODULE_DESCRIPTION("Generic implementation of a polled input device");
21 MODULE_LICENSE("GPL v2");
22 MODULE_VERSION("0.1");
24 static void input_polldev_queue_work(struct input_polled_dev *dev)
28 delay = msecs_to_jiffies(dev->poll_interval);
30 delay = round_jiffies_relative(delay);
32 queue_delayed_work(system_freezable_wq, &dev->work, delay);
35 static void input_polled_device_work(struct work_struct *work)
37 struct input_polled_dev *dev =
38 container_of(work, struct input_polled_dev, work.work);
41 input_polldev_queue_work(dev);
44 static int input_open_polled_device(struct input_dev *input)
46 struct input_polled_dev *dev = input_get_drvdata(input);
51 /* Only start polling if polling is enabled */
52 if (dev->poll_interval > 0) {
54 input_polldev_queue_work(dev);
60 static void input_close_polled_device(struct input_dev *input)
62 struct input_polled_dev *dev = input_get_drvdata(input);
64 cancel_delayed_work_sync(&dev->work);
72 static ssize_t input_polldev_get_poll(struct device *dev,
73 struct device_attribute *attr, char *buf)
75 struct input_polled_dev *polldev = dev_get_drvdata(dev);
77 return sprintf(buf, "%d\n", polldev->poll_interval);
80 static ssize_t input_polldev_set_poll(struct device *dev,
81 struct device_attribute *attr, const char *buf,
84 struct input_polled_dev *polldev = dev_get_drvdata(dev);
85 struct input_dev *input = polldev->input;
86 unsigned long interval;
88 if (strict_strtoul(buf, 0, &interval))
91 if (interval < polldev->poll_interval_min)
94 if (interval > polldev->poll_interval_max)
97 mutex_lock(&input->mutex);
99 polldev->poll_interval = interval;
102 cancel_delayed_work_sync(&polldev->work);
103 if (polldev->poll_interval > 0)
104 input_polldev_queue_work(polldev);
107 mutex_unlock(&input->mutex);
112 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
113 input_polldev_set_poll);
116 static ssize_t input_polldev_get_max(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct input_polled_dev *polldev = dev_get_drvdata(dev);
121 return sprintf(buf, "%d\n", polldev->poll_interval_max);
124 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
126 static ssize_t input_polldev_get_min(struct device *dev,
127 struct device_attribute *attr, char *buf)
129 struct input_polled_dev *polldev = dev_get_drvdata(dev);
131 return sprintf(buf, "%d\n", polldev->poll_interval_min);
134 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
136 static struct attribute *sysfs_attrs[] = {
143 static struct attribute_group input_polldev_attribute_group = {
148 * input_allocate_polled_device - allocate memory for polled device
150 * The function allocates memory for a polled device and also
151 * for an input device associated with this polled device.
153 struct input_polled_dev *input_allocate_polled_device(void)
155 struct input_polled_dev *dev;
157 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
161 dev->input = input_allocate_device();
169 EXPORT_SYMBOL(input_allocate_polled_device);
172 * input_free_polled_device - free memory allocated for polled device
173 * @dev: device to free
175 * The function frees memory allocated for polling device and drops
176 * reference to the associated input device.
178 void input_free_polled_device(struct input_polled_dev *dev)
181 input_free_device(dev->input);
185 EXPORT_SYMBOL(input_free_polled_device);
188 * input_register_polled_device - register polled device
189 * @dev: device to register
191 * The function registers previously initialized polled input device
192 * with input layer. The device should be allocated with call to
193 * input_allocate_polled_device(). Callers should also set up poll()
194 * method and set up capabilities (id, name, phys, bits) of the
195 * corresponding input_dev structure.
197 int input_register_polled_device(struct input_polled_dev *dev)
199 struct input_dev *input = dev->input;
202 input_set_drvdata(input, dev);
203 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
204 if (!dev->poll_interval)
205 dev->poll_interval = 500;
206 if (!dev->poll_interval_max)
207 dev->poll_interval_max = dev->poll_interval;
208 input->open = input_open_polled_device;
209 input->close = input_close_polled_device;
211 error = input_register_device(input);
215 error = sysfs_create_group(&input->dev.kobj,
216 &input_polldev_attribute_group);
218 input_unregister_device(input);
223 * Take extra reference to the underlying input device so
224 * that it survives call to input_unregister_polled_device()
225 * and is deleted only after input_free_polled_device()
226 * has been invoked. This is needed to ease task of freeing
229 input_get_device(input);
233 EXPORT_SYMBOL(input_register_polled_device);
236 * input_unregister_polled_device - unregister polled device
237 * @dev: device to unregister
239 * The function unregisters previously registered polled input
240 * device from input layer. Polling is stopped and device is
241 * ready to be freed with call to input_free_polled_device().
243 void input_unregister_polled_device(struct input_polled_dev *dev)
245 sysfs_remove_group(&dev->input->dev.kobj,
246 &input_polldev_attribute_group);
248 input_unregister_device(dev->input);
250 EXPORT_SYMBOL(input_unregister_polled_device);