OMAP2+: sdrc: fix compile break on OMAP4-only config on current omap-for-linus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / input-polldev.c
blob0559e309bac9a443230e66d3f3cfff31e7963f0b
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/input-polldev.h>
18 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
19 MODULE_DESCRIPTION("Generic implementation of a polled input device");
20 MODULE_LICENSE("GPL v2");
21 MODULE_VERSION("0.1");
23 static DEFINE_MUTEX(polldev_mutex);
24 static int polldev_users;
25 static struct workqueue_struct *polldev_wq;
27 static int input_polldev_start_workqueue(void)
29 int retval;
31 retval = mutex_lock_interruptible(&polldev_mutex);
32 if (retval)
33 return retval;
35 if (!polldev_users) {
36 polldev_wq = create_singlethread_workqueue("ipolldevd");
37 if (!polldev_wq) {
38 pr_err("failed to create ipolldevd workqueue\n");
39 retval = -ENOMEM;
40 goto out;
44 polldev_users++;
46 out:
47 mutex_unlock(&polldev_mutex);
48 return retval;
51 static void input_polldev_stop_workqueue(void)
53 mutex_lock(&polldev_mutex);
55 if (!--polldev_users)
56 destroy_workqueue(polldev_wq);
58 mutex_unlock(&polldev_mutex);
61 static void input_polldev_queue_work(struct input_polled_dev *dev)
63 unsigned long delay;
65 delay = msecs_to_jiffies(dev->poll_interval);
66 if (delay >= HZ)
67 delay = round_jiffies_relative(delay);
69 queue_delayed_work(polldev_wq, &dev->work, delay);
72 static void input_polled_device_work(struct work_struct *work)
74 struct input_polled_dev *dev =
75 container_of(work, struct input_polled_dev, work.work);
77 dev->poll(dev);
78 input_polldev_queue_work(dev);
81 static int input_open_polled_device(struct input_dev *input)
83 struct input_polled_dev *dev = input_get_drvdata(input);
84 int error;
86 error = input_polldev_start_workqueue();
87 if (error)
88 return error;
90 if (dev->open)
91 dev->open(dev);
93 /* Only start polling if polling is enabled */
94 if (dev->poll_interval > 0)
95 queue_delayed_work(polldev_wq, &dev->work, 0);
97 return 0;
100 static void input_close_polled_device(struct input_dev *input)
102 struct input_polled_dev *dev = input_get_drvdata(input);
104 cancel_delayed_work_sync(&dev->work);
106 * Clean up work struct to remove references to the workqueue.
107 * It may be destroyed by the next call. This causes problems
108 * at next device open-close in case of poll_interval == 0.
110 INIT_DELAYED_WORK(&dev->work, dev->work.work.func);
111 input_polldev_stop_workqueue();
113 if (dev->close)
114 dev->close(dev);
117 /* SYSFS interface */
119 static ssize_t input_polldev_get_poll(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);
127 static ssize_t input_polldev_set_poll(struct device *dev,
128 struct device_attribute *attr, const char *buf,
129 size_t count)
131 struct input_polled_dev *polldev = dev_get_drvdata(dev);
132 struct input_dev *input = polldev->input;
133 unsigned long interval;
135 if (strict_strtoul(buf, 0, &interval))
136 return -EINVAL;
138 if (interval < polldev->poll_interval_min)
139 return -EINVAL;
141 if (interval > polldev->poll_interval_max)
142 return -EINVAL;
144 mutex_lock(&input->mutex);
146 polldev->poll_interval = interval;
148 if (input->users) {
149 cancel_delayed_work_sync(&polldev->work);
150 if (polldev->poll_interval > 0)
151 input_polldev_queue_work(polldev);
154 mutex_unlock(&input->mutex);
156 return count;
159 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
160 input_polldev_set_poll);
163 static ssize_t input_polldev_get_max(struct device *dev,
164 struct device_attribute *attr, char *buf)
166 struct input_polled_dev *polldev = dev_get_drvdata(dev);
168 return sprintf(buf, "%d\n", polldev->poll_interval_max);
171 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
173 static ssize_t input_polldev_get_min(struct device *dev,
174 struct device_attribute *attr, char *buf)
176 struct input_polled_dev *polldev = dev_get_drvdata(dev);
178 return sprintf(buf, "%d\n", polldev->poll_interval_min);
181 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
183 static struct attribute *sysfs_attrs[] = {
184 &dev_attr_poll.attr,
185 &dev_attr_max.attr,
186 &dev_attr_min.attr,
187 NULL
190 static struct attribute_group input_polldev_attribute_group = {
191 .attrs = sysfs_attrs
195 * input_allocate_polled_device - allocated memory polled device
197 * The function allocates memory for a polled device and also
198 * for an input device associated with this polled device.
200 struct input_polled_dev *input_allocate_polled_device(void)
202 struct input_polled_dev *dev;
204 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
205 if (!dev)
206 return NULL;
208 dev->input = input_allocate_device();
209 if (!dev->input) {
210 kfree(dev);
211 return NULL;
214 return dev;
216 EXPORT_SYMBOL(input_allocate_polled_device);
219 * input_free_polled_device - free memory allocated for polled device
220 * @dev: device to free
222 * The function frees memory allocated for polling device and drops
223 * reference to the associated input device.
225 void input_free_polled_device(struct input_polled_dev *dev)
227 if (dev) {
228 input_free_device(dev->input);
229 kfree(dev);
232 EXPORT_SYMBOL(input_free_polled_device);
235 * input_register_polled_device - register polled device
236 * @dev: device to register
238 * The function registers previously initialized polled input device
239 * with input layer. The device should be allocated with call to
240 * input_allocate_polled_device(). Callers should also set up poll()
241 * method and set up capabilities (id, name, phys, bits) of the
242 * corresponing input_dev structure.
244 int input_register_polled_device(struct input_polled_dev *dev)
246 struct input_dev *input = dev->input;
247 int error;
249 input_set_drvdata(input, dev);
250 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
251 if (!dev->poll_interval)
252 dev->poll_interval = 500;
253 if (!dev->poll_interval_max)
254 dev->poll_interval_max = dev->poll_interval;
255 input->open = input_open_polled_device;
256 input->close = input_close_polled_device;
258 error = input_register_device(input);
259 if (error)
260 return error;
262 error = sysfs_create_group(&input->dev.kobj,
263 &input_polldev_attribute_group);
264 if (error) {
265 input_unregister_device(input);
266 return error;
270 * Take extra reference to the underlying input device so
271 * that it survives call to input_unregister_polled_device()
272 * and is deleted only after input_free_polled_device()
273 * has been invoked. This is needed to ease task of freeing
274 * sparse keymaps.
276 input_get_device(input);
278 return 0;
280 EXPORT_SYMBOL(input_register_polled_device);
283 * input_unregister_polled_device - unregister polled device
284 * @dev: device to unregister
286 * The function unregisters previously registered polled input
287 * device from input layer. Polling is stopped and device is
288 * ready to be freed with call to input_free_polled_device().
290 void input_unregister_polled_device(struct input_polled_dev *dev)
292 sysfs_remove_group(&dev->input->dev.kobj,
293 &input_polldev_attribute_group);
295 input_unregister_device(dev->input);
297 EXPORT_SYMBOL(input_unregister_polled_device);