blob: 40cf0b058927324cf2860dfb6842dcd7e9ff1819 [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
Dmitry Torokhov381f3f12009-11-18 23:10:54 -080091 queue_delayed_work(polldev_wq, &dev->work, 0);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040092
93 return 0;
94}
95
96static void input_close_polled_device(struct input_dev *input)
97{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040098 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040099
Stephen Hemminger374766b2007-11-21 14:03:37 -0500100 cancel_delayed_work_sync(&dev->work);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400101 input_polldev_stop_workqueue();
Samu Onkalob0aba1e2009-10-18 00:38:57 -0700102
103 if (dev->close)
104 dev->close(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400105}
106
Samu Onkalodad725d2009-11-13 21:13:22 -0800107/* SYSFS interface */
108
109static ssize_t input_polldev_get_poll(struct device *dev,
110 struct device_attribute *attr, char *buf)
111{
112 struct input_polled_dev *polldev = dev_get_drvdata(dev);
113
114 return sprintf(buf, "%d\n", polldev->poll_interval);
115}
116
117static ssize_t input_polldev_set_poll(struct device *dev,
118 struct device_attribute *attr, const char *buf,
119 size_t count)
120{
121 struct input_polled_dev *polldev = dev_get_drvdata(dev);
122 struct input_dev *input = polldev->input;
123 unsigned long interval;
124
125 if (strict_strtoul(buf, 0, &interval))
126 return -EINVAL;
127
128 if (interval < polldev->poll_interval_min)
129 return -EINVAL;
130
131 if (interval > polldev->poll_interval_max)
132 return -EINVAL;
133
134 mutex_lock(&input->mutex);
135
136 polldev->poll_interval = interval;
137
138 if (input->users) {
139 cancel_delayed_work_sync(&polldev->work);
140 if (polldev->poll_interval > 0)
141 input_polldev_queue_work(polldev);
142 }
143
144 mutex_unlock(&input->mutex);
145
146 return count;
147}
148
149static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
150 input_polldev_set_poll);
151
152
153static ssize_t input_polldev_get_max(struct device *dev,
154 struct device_attribute *attr, char *buf)
155{
156 struct input_polled_dev *polldev = dev_get_drvdata(dev);
157
158 return sprintf(buf, "%d\n", polldev->poll_interval_max);
159}
160
161static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
162
163static ssize_t input_polldev_get_min(struct device *dev,
164 struct device_attribute *attr, char *buf)
165{
166 struct input_polled_dev *polldev = dev_get_drvdata(dev);
167
168 return sprintf(buf, "%d\n", polldev->poll_interval_min);
169}
170
171static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
172
173static struct attribute *sysfs_attrs[] = {
174 &dev_attr_poll.attr,
175 &dev_attr_max.attr,
176 &dev_attr_min.attr,
177 NULL
178};
179
180static struct attribute_group input_polldev_attribute_group = {
181 .attrs = sysfs_attrs
182};
183
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400184/**
185 * input_allocate_polled_device - allocated memory polled device
186 *
187 * The function allocates memory for a polled device and also
188 * for an input device associated with this polled device.
189 */
190struct input_polled_dev *input_allocate_polled_device(void)
191{
192 struct input_polled_dev *dev;
193
194 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
195 if (!dev)
196 return NULL;
197
198 dev->input = input_allocate_device();
199 if (!dev->input) {
200 kfree(dev);
201 return NULL;
202 }
203
204 return dev;
205}
206EXPORT_SYMBOL(input_allocate_polled_device);
207
208/**
209 * input_free_polled_device - free memory allocated for polled device
210 * @dev: device to free
211 *
212 * The function frees memory allocated for polling device and drops
213 * reference to the associated input device (if present).
214 */
215void input_free_polled_device(struct input_polled_dev *dev)
216{
217 if (dev) {
218 input_free_device(dev->input);
219 kfree(dev);
220 }
221}
222EXPORT_SYMBOL(input_free_polled_device);
223
224/**
225 * input_register_polled_device - register polled device
226 * @dev: device to register
227 *
228 * The function registers previously initialized polled input device
229 * with input layer. The device should be allocated with call to
230 * input_allocate_polled_device(). Callers should also set up poll()
231 * method and set up capabilities (id, name, phys, bits) of the
232 * corresponing input_dev structure.
233 */
234int input_register_polled_device(struct input_polled_dev *dev)
235{
236 struct input_dev *input = dev->input;
Samu Onkalodad725d2009-11-13 21:13:22 -0800237 int error;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400238
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400239 input_set_drvdata(input, dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400240 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
241 if (!dev->poll_interval)
242 dev->poll_interval = 500;
Samu Onkalodad725d2009-11-13 21:13:22 -0800243 if (!dev->poll_interval_max)
244 dev->poll_interval_max = dev->poll_interval;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400245 input->open = input_open_polled_device;
246 input->close = input_close_polled_device;
247
Samu Onkalodad725d2009-11-13 21:13:22 -0800248 error = input_register_device(input);
249 if (error)
250 return error;
251
252 error = sysfs_create_group(&input->dev.kobj,
253 &input_polldev_attribute_group);
254 if (error) {
255 input_unregister_device(input);
256 return error;
257 }
258
259 return 0;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400260}
261EXPORT_SYMBOL(input_register_polled_device);
262
263/**
264 * input_unregister_polled_device - unregister polled device
265 * @dev: device to unregister
266 *
267 * The function unregisters previously registered polled input
268 * device from input layer. Polling is stopped and device is
269 * ready to be freed with call to input_free_polled_device().
270 * Callers should not attempt to access dev->input pointer
271 * after calling this function.
272 */
273void input_unregister_polled_device(struct input_polled_dev *dev)
274{
Samu Onkalodad725d2009-11-13 21:13:22 -0800275 sysfs_remove_group(&dev->input->dev.kobj,
276 &input_polldev_attribute_group);
277
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400278 input_unregister_device(dev->input);
279 dev->input = NULL;
280}
281EXPORT_SYMBOL(input_unregister_polled_device);
282