blob: 31874275fed02c048b0d20a2d57d176e91035fdf [file] [log] [blame]
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -04001/*
2 * Generic implementation of a polled input device
3
4 * Copyright (c) 2007 Dmitry Torokhov
5 *
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 */
10
11#include <linux/jiffies.h>
12#include <linux/mutex.h>
13#include <linux/input-polldev.h>
14
Eric Piel36bd52a2007-05-22 23:28:03 -040015MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
16MODULE_DESCRIPTION("Generic implementation of a polled input device");
17MODULE_LICENSE("GPL v2");
18MODULE_VERSION("0.1");
19
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040020static DEFINE_MUTEX(polldev_mutex);
21static int polldev_users;
22static struct workqueue_struct *polldev_wq;
23
24static int input_polldev_start_workqueue(void)
25{
26 int retval;
27
28 retval = mutex_lock_interruptible(&polldev_mutex);
29 if (retval)
30 return retval;
31
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;
39 }
40 }
41
42 polldev_users++;
43
44 out:
45 mutex_unlock(&polldev_mutex);
46 return retval;
47}
48
49static void input_polldev_stop_workqueue(void)
50{
51 mutex_lock(&polldev_mutex);
52
53 if (!--polldev_users)
54 destroy_workqueue(polldev_wq);
55
56 mutex_unlock(&polldev_mutex);
57}
58
Samu Onkalodad725d2009-11-13 21:13:22 -080059static void input_polldev_queue_work(struct input_polled_dev *dev)
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040060{
Stephen Hemminger374766b2007-11-21 14:03:37 -050061 unsigned long delay;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040062
Stephen Hemminger374766b2007-11-21 14:03:37 -050063 delay = msecs_to_jiffies(dev->poll_interval);
64 if (delay >= HZ)
65 delay = round_jiffies_relative(delay);
66
67 queue_delayed_work(polldev_wq, &dev->work, delay);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040068}
69
Samu Onkalodad725d2009-11-13 21:13:22 -080070static void input_polled_device_work(struct work_struct *work)
71{
72 struct input_polled_dev *dev =
73 container_of(work, struct input_polled_dev, work.work);
74
75 dev->poll(dev);
76 input_polldev_queue_work(dev);
77}
78
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040079static int input_open_polled_device(struct input_dev *input)
80{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040081 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040082 int error;
83
84 error = input_polldev_start_workqueue();
85 if (error)
86 return error;
87
Samu Onkalob0aba1e2009-10-18 00:38:57 -070088 if (dev->open)
89 dev->open(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040090
Stephen Hemminger374766b2007-11-21 14:03:37 -050091 queue_delayed_work(polldev_wq, &dev->work,
92 msecs_to_jiffies(dev->poll_interval));
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040093
94 return 0;
95}
96
97static void input_close_polled_device(struct input_dev *input)
98{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040099 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400100
Stephen Hemminger374766b2007-11-21 14:03:37 -0500101 cancel_delayed_work_sync(&dev->work);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400102 input_polldev_stop_workqueue();
Samu Onkalob0aba1e2009-10-18 00:38:57 -0700103
104 if (dev->close)
105 dev->close(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400106}
107
Samu Onkalodad725d2009-11-13 21:13:22 -0800108/* SYSFS interface */
109
110static ssize_t input_polldev_get_poll(struct device *dev,
111 struct device_attribute *attr, char *buf)
112{
113 struct input_polled_dev *polldev = dev_get_drvdata(dev);
114
115 return sprintf(buf, "%d\n", polldev->poll_interval);
116}
117
118static ssize_t input_polldev_set_poll(struct device *dev,
119 struct device_attribute *attr, const char *buf,
120 size_t count)
121{
122 struct input_polled_dev *polldev = dev_get_drvdata(dev);
123 struct input_dev *input = polldev->input;
124 unsigned long interval;
125
126 if (strict_strtoul(buf, 0, &interval))
127 return -EINVAL;
128
129 if (interval < polldev->poll_interval_min)
130 return -EINVAL;
131
132 if (interval > polldev->poll_interval_max)
133 return -EINVAL;
134
135 mutex_lock(&input->mutex);
136
137 polldev->poll_interval = interval;
138
139 if (input->users) {
140 cancel_delayed_work_sync(&polldev->work);
141 if (polldev->poll_interval > 0)
142 input_polldev_queue_work(polldev);
143 }
144
145 mutex_unlock(&input->mutex);
146
147 return count;
148}
149
150static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
151 input_polldev_set_poll);
152
153
154static ssize_t input_polldev_get_max(struct device *dev,
155 struct device_attribute *attr, char *buf)
156{
157 struct input_polled_dev *polldev = dev_get_drvdata(dev);
158
159 return sprintf(buf, "%d\n", polldev->poll_interval_max);
160}
161
162static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
163
164static ssize_t input_polldev_get_min(struct device *dev,
165 struct device_attribute *attr, char *buf)
166{
167 struct input_polled_dev *polldev = dev_get_drvdata(dev);
168
169 return sprintf(buf, "%d\n", polldev->poll_interval_min);
170}
171
172static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
173
174static struct attribute *sysfs_attrs[] = {
175 &dev_attr_poll.attr,
176 &dev_attr_max.attr,
177 &dev_attr_min.attr,
178 NULL
179};
180
181static struct attribute_group input_polldev_attribute_group = {
182 .attrs = sysfs_attrs
183};
184
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400185/**
186 * input_allocate_polled_device - allocated memory polled device
187 *
188 * The function allocates memory for a polled device and also
189 * for an input device associated with this polled device.
190 */
191struct input_polled_dev *input_allocate_polled_device(void)
192{
193 struct input_polled_dev *dev;
194
195 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
196 if (!dev)
197 return NULL;
198
199 dev->input = input_allocate_device();
200 if (!dev->input) {
201 kfree(dev);
202 return NULL;
203 }
204
205 return dev;
206}
207EXPORT_SYMBOL(input_allocate_polled_device);
208
209/**
210 * input_free_polled_device - free memory allocated for polled device
211 * @dev: device to free
212 *
213 * The function frees memory allocated for polling device and drops
214 * reference to the associated input device (if present).
215 */
216void input_free_polled_device(struct input_polled_dev *dev)
217{
218 if (dev) {
219 input_free_device(dev->input);
220 kfree(dev);
221 }
222}
223EXPORT_SYMBOL(input_free_polled_device);
224
225/**
226 * input_register_polled_device - register polled device
227 * @dev: device to register
228 *
229 * The function registers previously initialized polled input device
230 * with input layer. The device should be allocated with call to
231 * input_allocate_polled_device(). Callers should also set up poll()
232 * method and set up capabilities (id, name, phys, bits) of the
233 * corresponing input_dev structure.
234 */
235int input_register_polled_device(struct input_polled_dev *dev)
236{
237 struct input_dev *input = dev->input;
Samu Onkalodad725d2009-11-13 21:13:22 -0800238 int error;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400239
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400240 input_set_drvdata(input, dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400241 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
242 if (!dev->poll_interval)
243 dev->poll_interval = 500;
Samu Onkalodad725d2009-11-13 21:13:22 -0800244 if (!dev->poll_interval_max)
245 dev->poll_interval_max = dev->poll_interval;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400246 input->open = input_open_polled_device;
247 input->close = input_close_polled_device;
248
Samu Onkalodad725d2009-11-13 21:13:22 -0800249 error = input_register_device(input);
250 if (error)
251 return error;
252
253 error = sysfs_create_group(&input->dev.kobj,
254 &input_polldev_attribute_group);
255 if (error) {
256 input_unregister_device(input);
257 return error;
258 }
259
260 return 0;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400261}
262EXPORT_SYMBOL(input_register_polled_device);
263
264/**
265 * input_unregister_polled_device - unregister polled device
266 * @dev: device to unregister
267 *
268 * The function unregisters previously registered polled input
269 * device from input layer. Polling is stopped and device is
270 * ready to be freed with call to input_free_polled_device().
271 * Callers should not attempt to access dev->input pointer
272 * after calling this function.
273 */
274void input_unregister_polled_device(struct input_polled_dev *dev)
275{
Samu Onkalodad725d2009-11-13 21:13:22 -0800276 sysfs_remove_group(&dev->input->dev.kobj,
277 &input_polldev_attribute_group);
278
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400279 input_unregister_device(dev->input);
280 dev->input = NULL;
281}
282EXPORT_SYMBOL(input_unregister_polled_device);
283