MAINTAINERS: add myself as co-maintainer of the iwlwifi driver
[linux/fpc-iii.git] / drivers / input / input-polldev.c
blob3664f81655ca83d116733ecad3b70f481c5115d6
1 /*
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.
9 */
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/module.h>
18 #include <linux/input-polldev.h>
20 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21 MODULE_DESCRIPTION("Generic implementation of a polled input device");
22 MODULE_LICENSE("GPL v2");
23 MODULE_VERSION("0.1");
25 static void input_polldev_queue_work(struct input_polled_dev *dev)
27 unsigned long delay;
29 delay = msecs_to_jiffies(dev->poll_interval);
30 if (delay >= HZ)
31 delay = round_jiffies_relative(delay);
33 queue_delayed_work(system_freezable_wq, &dev->work, delay);
36 static void input_polled_device_work(struct work_struct *work)
38 struct input_polled_dev *dev =
39 container_of(work, struct input_polled_dev, work.work);
41 dev->poll(dev);
42 input_polldev_queue_work(dev);
45 static int input_open_polled_device(struct input_dev *input)
47 struct input_polled_dev *dev = input_get_drvdata(input);
49 if (dev->open)
50 dev->open(dev);
52 /* Only start polling if polling is enabled */
53 if (dev->poll_interval > 0) {
54 dev->poll(dev);
55 input_polldev_queue_work(dev);
58 return 0;
61 static void input_close_polled_device(struct input_dev *input)
63 struct input_polled_dev *dev = input_get_drvdata(input);
65 cancel_delayed_work_sync(&dev->work);
67 if (dev->close)
68 dev->close(dev);
71 /* SYSFS interface */
73 static ssize_t input_polldev_get_poll(struct device *dev,
74 struct device_attribute *attr, char *buf)
76 struct input_polled_dev *polldev = dev_get_drvdata(dev);
78 return sprintf(buf, "%d\n", polldev->poll_interval);
81 static ssize_t input_polldev_set_poll(struct device *dev,
82 struct device_attribute *attr, const char *buf,
83 size_t count)
85 struct input_polled_dev *polldev = dev_get_drvdata(dev);
86 struct input_dev *input = polldev->input;
87 unsigned int interval;
88 int err;
90 err = kstrtouint(buf, 0, &interval);
91 if (err)
92 return err;
94 if (interval < polldev->poll_interval_min)
95 return -EINVAL;
97 if (interval > polldev->poll_interval_max)
98 return -EINVAL;
100 mutex_lock(&input->mutex);
102 polldev->poll_interval = interval;
104 if (input->users) {
105 cancel_delayed_work_sync(&polldev->work);
106 if (polldev->poll_interval > 0)
107 input_polldev_queue_work(polldev);
110 mutex_unlock(&input->mutex);
112 return count;
115 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
116 input_polldev_set_poll);
119 static ssize_t input_polldev_get_max(struct device *dev,
120 struct device_attribute *attr, char *buf)
122 struct input_polled_dev *polldev = dev_get_drvdata(dev);
124 return sprintf(buf, "%d\n", polldev->poll_interval_max);
127 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
129 static ssize_t input_polldev_get_min(struct device *dev,
130 struct device_attribute *attr, char *buf)
132 struct input_polled_dev *polldev = dev_get_drvdata(dev);
134 return sprintf(buf, "%d\n", polldev->poll_interval_min);
137 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
139 static struct attribute *sysfs_attrs[] = {
140 &dev_attr_poll.attr,
141 &dev_attr_max.attr,
142 &dev_attr_min.attr,
143 NULL
146 static struct attribute_group input_polldev_attribute_group = {
147 .attrs = sysfs_attrs
150 static const struct attribute_group *input_polldev_attribute_groups[] = {
151 &input_polldev_attribute_group,
152 NULL
156 * input_allocate_polled_device - allocate memory for polled device
158 * The function allocates memory for a polled device and also
159 * for an input device associated with this polled device.
161 struct input_polled_dev *input_allocate_polled_device(void)
163 struct input_polled_dev *dev;
165 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
166 if (!dev)
167 return NULL;
169 dev->input = input_allocate_device();
170 if (!dev->input) {
171 kfree(dev);
172 return NULL;
175 return dev;
177 EXPORT_SYMBOL(input_allocate_polled_device);
179 struct input_polled_devres {
180 struct input_polled_dev *polldev;
183 static int devm_input_polldev_match(struct device *dev, void *res, void *data)
185 struct input_polled_devres *devres = res;
187 return devres->polldev == data;
190 static void devm_input_polldev_release(struct device *dev, void *res)
192 struct input_polled_devres *devres = res;
193 struct input_polled_dev *polldev = devres->polldev;
195 dev_dbg(dev, "%s: dropping reference/freeing %s\n",
196 __func__, dev_name(&polldev->input->dev));
198 input_put_device(polldev->input);
199 kfree(polldev);
202 static void devm_input_polldev_unregister(struct device *dev, void *res)
204 struct input_polled_devres *devres = res;
205 struct input_polled_dev *polldev = devres->polldev;
207 dev_dbg(dev, "%s: unregistering device %s\n",
208 __func__, dev_name(&polldev->input->dev));
209 input_unregister_device(polldev->input);
212 * Note that we are still holding extra reference to the input
213 * device so it will stick around until devm_input_polldev_release()
214 * is called.
219 * devm_input_allocate_polled_device - allocate managed polled device
220 * @dev: device owning the polled device being created
222 * Returns prepared &struct input_polled_dev or %NULL.
224 * Managed polled input devices do not need to be explicitly unregistered
225 * or freed as it will be done automatically when owner device unbinds
226 * from * its driver (or binding fails). Once such managed polled device
227 * is allocated, it is ready to be set up and registered in the same
228 * fashion as regular polled input devices (using
229 * input_register_polled_device() function).
231 * If you want to manually unregister and free such managed polled devices,
232 * it can be still done by calling input_unregister_polled_device() and
233 * input_free_polled_device(), although it is rarely needed.
235 * NOTE: the owner device is set up as parent of input device and users
236 * should not override it.
238 struct input_polled_dev *devm_input_allocate_polled_device(struct device *dev)
240 struct input_polled_dev *polldev;
241 struct input_polled_devres *devres;
243 devres = devres_alloc(devm_input_polldev_release, sizeof(*devres),
244 GFP_KERNEL);
245 if (!devres)
246 return NULL;
248 polldev = input_allocate_polled_device();
249 if (!polldev) {
250 devres_free(devres);
251 return NULL;
254 polldev->input->dev.parent = dev;
255 polldev->devres_managed = true;
257 devres->polldev = polldev;
258 devres_add(dev, devres);
260 return polldev;
262 EXPORT_SYMBOL(devm_input_allocate_polled_device);
265 * input_free_polled_device - free memory allocated for polled device
266 * @dev: device to free
268 * The function frees memory allocated for polling device and drops
269 * reference to the associated input device.
271 void input_free_polled_device(struct input_polled_dev *dev)
273 if (dev) {
274 if (dev->devres_managed)
275 WARN_ON(devres_destroy(dev->input->dev.parent,
276 devm_input_polldev_release,
277 devm_input_polldev_match,
278 dev));
279 input_put_device(dev->input);
280 kfree(dev);
283 EXPORT_SYMBOL(input_free_polled_device);
286 * input_register_polled_device - register polled device
287 * @dev: device to register
289 * The function registers previously initialized polled input device
290 * with input layer. The device should be allocated with call to
291 * input_allocate_polled_device(). Callers should also set up poll()
292 * method and set up capabilities (id, name, phys, bits) of the
293 * corresponding input_dev structure.
295 int input_register_polled_device(struct input_polled_dev *dev)
297 struct input_polled_devres *devres = NULL;
298 struct input_dev *input = dev->input;
299 int error;
301 if (dev->devres_managed) {
302 devres = devres_alloc(devm_input_polldev_unregister,
303 sizeof(*devres), GFP_KERNEL);
304 if (!devres)
305 return -ENOMEM;
307 devres->polldev = dev;
310 input_set_drvdata(input, dev);
311 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
313 if (!dev->poll_interval)
314 dev->poll_interval = 500;
315 if (!dev->poll_interval_max)
316 dev->poll_interval_max = dev->poll_interval;
318 input->open = input_open_polled_device;
319 input->close = input_close_polled_device;
321 input->dev.groups = input_polldev_attribute_groups;
323 error = input_register_device(input);
324 if (error) {
325 devres_free(devres);
326 return error;
330 * Take extra reference to the underlying input device so
331 * that it survives call to input_unregister_polled_device()
332 * and is deleted only after input_free_polled_device()
333 * has been invoked. This is needed to ease task of freeing
334 * sparse keymaps.
336 input_get_device(input);
338 if (dev->devres_managed) {
339 dev_dbg(input->dev.parent, "%s: registering %s with devres.\n",
340 __func__, dev_name(&input->dev));
341 devres_add(input->dev.parent, devres);
344 return 0;
346 EXPORT_SYMBOL(input_register_polled_device);
349 * input_unregister_polled_device - unregister polled device
350 * @dev: device to unregister
352 * The function unregisters previously registered polled input
353 * device from input layer. Polling is stopped and device is
354 * ready to be freed with call to input_free_polled_device().
356 void input_unregister_polled_device(struct input_polled_dev *dev)
358 if (dev->devres_managed)
359 WARN_ON(devres_destroy(dev->input->dev.parent,
360 devm_input_polldev_unregister,
361 devm_input_polldev_match,
362 dev));
364 input_unregister_device(dev->input);
366 EXPORT_SYMBOL(input_unregister_polled_device);