Blackfin: SMP: don't start up core b until its state has been completely onlined
[linux/fpc-iii.git] / drivers / input / input-polldev.c
blobaa6713b4a9888e64ed99a92f78e1e1cd2bb9ca56
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 #include <linux/jiffies.h>
12 #include <linux/mutex.h>
13 #include <linux/input-polldev.h>
15 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
16 MODULE_DESCRIPTION("Generic implementation of a polled input device");
17 MODULE_LICENSE("GPL v2");
18 MODULE_VERSION("0.1");
20 static DEFINE_MUTEX(polldev_mutex);
21 static int polldev_users;
22 static struct workqueue_struct *polldev_wq;
24 static int input_polldev_start_workqueue(void)
26 int retval;
28 retval = mutex_lock_interruptible(&polldev_mutex);
29 if (retval)
30 return retval;
32 if (!polldev_users) {
33 polldev_wq = create_singlethread_workqueue("ipolldevd");
34 if (!polldev_wq) {
35 printk(KERN_ERR "input-polldev: failed to create "
36 "ipolldevd workqueue\n");
37 retval = -ENOMEM;
38 goto out;
42 polldev_users++;
44 out:
45 mutex_unlock(&polldev_mutex);
46 return retval;
49 static void input_polldev_stop_workqueue(void)
51 mutex_lock(&polldev_mutex);
53 if (!--polldev_users)
54 destroy_workqueue(polldev_wq);
56 mutex_unlock(&polldev_mutex);
59 static void input_polldev_queue_work(struct input_polled_dev *dev)
61 unsigned long delay;
63 delay = msecs_to_jiffies(dev->poll_interval);
64 if (delay >= HZ)
65 delay = round_jiffies_relative(delay);
67 queue_delayed_work(polldev_wq, &dev->work, delay);
70 static void input_polled_device_work(struct work_struct *work)
72 struct input_polled_dev *dev =
73 container_of(work, struct input_polled_dev, work.work);
75 dev->poll(dev);
76 input_polldev_queue_work(dev);
79 static int input_open_polled_device(struct input_dev *input)
81 struct input_polled_dev *dev = input_get_drvdata(input);
82 int error;
84 error = input_polldev_start_workqueue();
85 if (error)
86 return error;
88 if (dev->open)
89 dev->open(dev);
91 /* Only start polling if polling is enabled */
92 if (dev->poll_interval > 0)
93 queue_delayed_work(polldev_wq, &dev->work, 0);
95 return 0;
98 static void input_close_polled_device(struct input_dev *input)
100 struct input_polled_dev *dev = input_get_drvdata(input);
102 cancel_delayed_work_sync(&dev->work);
103 input_polldev_stop_workqueue();
105 if (dev->close)
106 dev->close(dev);
109 /* SYSFS interface */
111 static ssize_t input_polldev_get_poll(struct device *dev,
112 struct device_attribute *attr, char *buf)
114 struct input_polled_dev *polldev = dev_get_drvdata(dev);
116 return sprintf(buf, "%d\n", polldev->poll_interval);
119 static ssize_t input_polldev_set_poll(struct device *dev,
120 struct device_attribute *attr, const char *buf,
121 size_t count)
123 struct input_polled_dev *polldev = dev_get_drvdata(dev);
124 struct input_dev *input = polldev->input;
125 unsigned long interval;
127 if (strict_strtoul(buf, 0, &interval))
128 return -EINVAL;
130 if (interval < polldev->poll_interval_min)
131 return -EINVAL;
133 if (interval > polldev->poll_interval_max)
134 return -EINVAL;
136 mutex_lock(&input->mutex);
138 polldev->poll_interval = interval;
140 if (input->users) {
141 cancel_delayed_work_sync(&polldev->work);
142 if (polldev->poll_interval > 0)
143 input_polldev_queue_work(polldev);
146 mutex_unlock(&input->mutex);
148 return count;
151 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
152 input_polldev_set_poll);
155 static ssize_t input_polldev_get_max(struct device *dev,
156 struct device_attribute *attr, char *buf)
158 struct input_polled_dev *polldev = dev_get_drvdata(dev);
160 return sprintf(buf, "%d\n", polldev->poll_interval_max);
163 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
165 static ssize_t input_polldev_get_min(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct input_polled_dev *polldev = dev_get_drvdata(dev);
170 return sprintf(buf, "%d\n", polldev->poll_interval_min);
173 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
175 static struct attribute *sysfs_attrs[] = {
176 &dev_attr_poll.attr,
177 &dev_attr_max.attr,
178 &dev_attr_min.attr,
179 NULL
182 static struct attribute_group input_polldev_attribute_group = {
183 .attrs = sysfs_attrs
187 * input_allocate_polled_device - allocated memory polled device
189 * The function allocates memory for a polled device and also
190 * for an input device associated with this polled device.
192 struct input_polled_dev *input_allocate_polled_device(void)
194 struct input_polled_dev *dev;
196 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
197 if (!dev)
198 return NULL;
200 dev->input = input_allocate_device();
201 if (!dev->input) {
202 kfree(dev);
203 return NULL;
206 return dev;
208 EXPORT_SYMBOL(input_allocate_polled_device);
211 * input_free_polled_device - free memory allocated for polled device
212 * @dev: device to free
214 * The function frees memory allocated for polling device and drops
215 * reference to the associated input device.
217 void input_free_polled_device(struct input_polled_dev *dev)
219 if (dev) {
220 input_free_device(dev->input);
221 kfree(dev);
224 EXPORT_SYMBOL(input_free_polled_device);
227 * input_register_polled_device - register polled device
228 * @dev: device to register
230 * The function registers previously initialized polled input device
231 * with input layer. The device should be allocated with call to
232 * input_allocate_polled_device(). Callers should also set up poll()
233 * method and set up capabilities (id, name, phys, bits) of the
234 * corresponing input_dev structure.
236 int input_register_polled_device(struct input_polled_dev *dev)
238 struct input_dev *input = dev->input;
239 int error;
241 input_set_drvdata(input, dev);
242 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
243 if (!dev->poll_interval)
244 dev->poll_interval = 500;
245 if (!dev->poll_interval_max)
246 dev->poll_interval_max = dev->poll_interval;
247 input->open = input_open_polled_device;
248 input->close = input_close_polled_device;
250 error = input_register_device(input);
251 if (error)
252 return error;
254 error = sysfs_create_group(&input->dev.kobj,
255 &input_polldev_attribute_group);
256 if (error) {
257 input_unregister_device(input);
258 return error;
262 * Take extra reference to the underlying input device so
263 * that it survives call to input_unregister_polled_device()
264 * and is deleted only after input_free_polled_device()
265 * has been invoked. This is needed to ease task of freeing
266 * sparse keymaps.
268 input_get_device(input);
270 return 0;
272 EXPORT_SYMBOL(input_register_polled_device);
275 * input_unregister_polled_device - unregister polled device
276 * @dev: device to unregister
278 * The function unregisters previously registered polled input
279 * device from input layer. Polling is stopped and device is
280 * ready to be freed with call to input_free_polled_device().
282 void input_unregister_polled_device(struct input_polled_dev *dev)
284 sysfs_remove_group(&dev->input->dev.kobj,
285 &input_polldev_attribute_group);
287 input_unregister_device(dev->input);
289 EXPORT_SYMBOL(input_unregister_polled_device);