blob: 3037842a60d8a10e0533033b665aca490c10b030 [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
Joe Perchesda0c4902010-11-29 23:33:07 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040013#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040015#include <linux/mutex.h>
16#include <linux/input-polldev.h>
17
Eric Piel36bd52a2007-05-22 23:28:03 -040018MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
19MODULE_DESCRIPTION("Generic implementation of a polled input device");
20MODULE_LICENSE("GPL v2");
21MODULE_VERSION("0.1");
22
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040023static DEFINE_MUTEX(polldev_mutex);
24static int polldev_users;
25static struct workqueue_struct *polldev_wq;
26
27static int input_polldev_start_workqueue(void)
28{
29 int retval;
30
31 retval = mutex_lock_interruptible(&polldev_mutex);
32 if (retval)
33 return retval;
34
35 if (!polldev_users) {
36 polldev_wq = create_singlethread_workqueue("ipolldevd");
37 if (!polldev_wq) {
Joe Perchesda0c4902010-11-29 23:33:07 -080038 pr_err("failed to create ipolldevd workqueue\n");
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040039 retval = -ENOMEM;
40 goto out;
41 }
42 }
43
44 polldev_users++;
45
46 out:
47 mutex_unlock(&polldev_mutex);
48 return retval;
49}
50
51static void input_polldev_stop_workqueue(void)
52{
53 mutex_lock(&polldev_mutex);
54
55 if (!--polldev_users)
56 destroy_workqueue(polldev_wq);
57
58 mutex_unlock(&polldev_mutex);
59}
60
Samu Onkalodad725d2009-11-13 21:13:22 -080061static void input_polldev_queue_work(struct input_polled_dev *dev)
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040062{
Stephen Hemminger374766b2007-11-21 14:03:37 -050063 unsigned long delay;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040064
Stephen Hemminger374766b2007-11-21 14:03:37 -050065 delay = msecs_to_jiffies(dev->poll_interval);
66 if (delay >= HZ)
67 delay = round_jiffies_relative(delay);
68
69 queue_delayed_work(polldev_wq, &dev->work, delay);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040070}
71
Samu Onkalodad725d2009-11-13 21:13:22 -080072static void input_polled_device_work(struct work_struct *work)
73{
74 struct input_polled_dev *dev =
75 container_of(work, struct input_polled_dev, work.work);
76
77 dev->poll(dev);
78 input_polldev_queue_work(dev);
79}
80
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040081static int input_open_polled_device(struct input_dev *input)
82{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040083 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040084 int error;
85
86 error = input_polldev_start_workqueue();
87 if (error)
88 return error;
89
Samu Onkalob0aba1e2009-10-18 00:38:57 -070090 if (dev->open)
91 dev->open(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040092
Samu Onkalo11bb4cc2009-11-23 10:01:33 -080093 /* Only start polling if polling is enabled */
94 if (dev->poll_interval > 0)
95 queue_delayed_work(polldev_wq, &dev->work, 0);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040096
97 return 0;
98}
99
100static void input_close_polled_device(struct input_dev *input)
101{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400102 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400103
Stephen Hemminger374766b2007-11-21 14:03:37 -0500104 cancel_delayed_work_sync(&dev->work);
Samu Onkalod9c4f842010-02-19 23:17:58 -0800105 /*
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.
109 */
110 INIT_DELAYED_WORK(&dev->work, dev->work.work.func);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400111 input_polldev_stop_workqueue();
Samu Onkalob0aba1e2009-10-18 00:38:57 -0700112
113 if (dev->close)
114 dev->close(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400115}
116
Samu Onkalodad725d2009-11-13 21:13:22 -0800117/* SYSFS interface */
118
119static ssize_t input_polldev_get_poll(struct device *dev,
120 struct device_attribute *attr, char *buf)
121{
122 struct input_polled_dev *polldev = dev_get_drvdata(dev);
123
124 return sprintf(buf, "%d\n", polldev->poll_interval);
125}
126
127static ssize_t input_polldev_set_poll(struct device *dev,
128 struct device_attribute *attr, const char *buf,
129 size_t count)
130{
131 struct input_polled_dev *polldev = dev_get_drvdata(dev);
132 struct input_dev *input = polldev->input;
133 unsigned long interval;
134
135 if (strict_strtoul(buf, 0, &interval))
136 return -EINVAL;
137
138 if (interval < polldev->poll_interval_min)
139 return -EINVAL;
140
141 if (interval > polldev->poll_interval_max)
142 return -EINVAL;
143
144 mutex_lock(&input->mutex);
145
146 polldev->poll_interval = interval;
147
148 if (input->users) {
149 cancel_delayed_work_sync(&polldev->work);
150 if (polldev->poll_interval > 0)
151 input_polldev_queue_work(polldev);
152 }
153
154 mutex_unlock(&input->mutex);
155
156 return count;
157}
158
159static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
160 input_polldev_set_poll);
161
162
163static ssize_t input_polldev_get_max(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_max);
169}
170
171static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
172
173static ssize_t input_polldev_get_min(struct device *dev,
174 struct device_attribute *attr, char *buf)
175{
176 struct input_polled_dev *polldev = dev_get_drvdata(dev);
177
178 return sprintf(buf, "%d\n", polldev->poll_interval_min);
179}
180
181static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
182
183static struct attribute *sysfs_attrs[] = {
184 &dev_attr_poll.attr,
185 &dev_attr_max.attr,
186 &dev_attr_min.attr,
187 NULL
188};
189
190static struct attribute_group input_polldev_attribute_group = {
191 .attrs = sysfs_attrs
192};
193
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400194/**
Dmitry Torokhov2546bcc2011-01-31 21:06:34 -0800195 * input_allocate_polled_device - allocate memory for polled device
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400196 *
197 * The function allocates memory for a polled device and also
198 * for an input device associated with this polled device.
199 */
200struct input_polled_dev *input_allocate_polled_device(void)
201{
202 struct input_polled_dev *dev;
203
204 dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
205 if (!dev)
206 return NULL;
207
208 dev->input = input_allocate_device();
209 if (!dev->input) {
210 kfree(dev);
211 return NULL;
212 }
213
214 return dev;
215}
216EXPORT_SYMBOL(input_allocate_polled_device);
217
218/**
219 * input_free_polled_device - free memory allocated for polled device
220 * @dev: device to free
221 *
222 * The function frees memory allocated for polling device and drops
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800223 * reference to the associated input device.
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400224 */
225void input_free_polled_device(struct input_polled_dev *dev)
226{
227 if (dev) {
228 input_free_device(dev->input);
229 kfree(dev);
230 }
231}
232EXPORT_SYMBOL(input_free_polled_device);
233
234/**
235 * input_register_polled_device - register polled device
236 * @dev: device to register
237 *
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
Dmitry Torokhov2546bcc2011-01-31 21:06:34 -0800242 * corresponding input_dev structure.
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400243 */
244int input_register_polled_device(struct input_polled_dev *dev)
245{
246 struct input_dev *input = dev->input;
Samu Onkalodad725d2009-11-13 21:13:22 -0800247 int error;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400248
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400249 input_set_drvdata(input, dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400250 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
251 if (!dev->poll_interval)
252 dev->poll_interval = 500;
Samu Onkalodad725d2009-11-13 21:13:22 -0800253 if (!dev->poll_interval_max)
254 dev->poll_interval_max = dev->poll_interval;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400255 input->open = input_open_polled_device;
256 input->close = input_close_polled_device;
257
Samu Onkalodad725d2009-11-13 21:13:22 -0800258 error = input_register_device(input);
259 if (error)
260 return error;
261
262 error = sysfs_create_group(&input->dev.kobj,
263 &input_polldev_attribute_group);
264 if (error) {
265 input_unregister_device(input);
266 return error;
267 }
268
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800269 /*
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.
275 */
276 input_get_device(input);
277
Samu Onkalodad725d2009-11-13 21:13:22 -0800278 return 0;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400279}
280EXPORT_SYMBOL(input_register_polled_device);
281
282/**
283 * input_unregister_polled_device - unregister polled device
284 * @dev: device to unregister
285 *
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().
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400289 */
290void input_unregister_polled_device(struct input_polled_dev *dev)
291{
Samu Onkalodad725d2009-11-13 21:13:22 -0800292 sysfs_remove_group(&dev->input->dev.kobj,
293 &input_polldev_attribute_group);
294
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400295 input_unregister_device(dev->input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400296}
297EXPORT_SYMBOL(input_unregister_polled_device);
298