aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/input-polldev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/input-polldev.c')
-rw-r--r--drivers/input/input-polldev.c144
1 files changed, 130 insertions, 14 deletions
diff --git a/drivers/input/input-polldev.c b/drivers/input/input-polldev.c
index 0d3ce7a50fb1..10c9b0a845f0 100644
--- a/drivers/input/input-polldev.c
+++ b/drivers/input/input-polldev.c
@@ -9,6 +9,7 @@
9 */ 9 */
10 10
11#include <linux/jiffies.h> 11#include <linux/jiffies.h>
12#include <linux/slab.h>
12#include <linux/mutex.h> 13#include <linux/mutex.h>
13#include <linux/input-polldev.h> 14#include <linux/input-polldev.h>
14 15
@@ -56,14 +57,10 @@ static void input_polldev_stop_workqueue(void)
56 mutex_unlock(&polldev_mutex); 57 mutex_unlock(&polldev_mutex);
57} 58}
58 59
59static void input_polled_device_work(struct work_struct *work) 60static void input_polldev_queue_work(struct input_polled_dev *dev)
60{ 61{
61 struct input_polled_dev *dev =
62 container_of(work, struct input_polled_dev, work.work);
63 unsigned long delay; 62 unsigned long delay;
64 63
65 dev->poll(dev);
66
67 delay = msecs_to_jiffies(dev->poll_interval); 64 delay = msecs_to_jiffies(dev->poll_interval);
68 if (delay >= HZ) 65 if (delay >= HZ)
69 delay = round_jiffies_relative(delay); 66 delay = round_jiffies_relative(delay);
@@ -71,6 +68,15 @@ static void input_polled_device_work(struct work_struct *work)
71 queue_delayed_work(polldev_wq, &dev->work, delay); 68 queue_delayed_work(polldev_wq, &dev->work, delay);
72} 69}
73 70
71static void input_polled_device_work(struct work_struct *work)
72{
73 struct input_polled_dev *dev =
74 container_of(work, struct input_polled_dev, work.work);
75
76 dev->poll(dev);
77 input_polldev_queue_work(dev);
78}
79
74static int input_open_polled_device(struct input_dev *input) 80static int input_open_polled_device(struct input_dev *input)
75{ 81{
76 struct input_polled_dev *dev = input_get_drvdata(input); 82 struct input_polled_dev *dev = input_get_drvdata(input);
@@ -80,11 +86,12 @@ static int input_open_polled_device(struct input_dev *input)
80 if (error) 86 if (error)
81 return error; 87 return error;
82 88
83 if (dev->flush) 89 if (dev->open)
84 dev->flush(dev); 90 dev->open(dev);
85 91
86 queue_delayed_work(polldev_wq, &dev->work, 92 /* Only start polling if polling is enabled */
87 msecs_to_jiffies(dev->poll_interval)); 93 if (dev->poll_interval > 0)
94 queue_delayed_work(polldev_wq, &dev->work, 0);
88 95
89 return 0; 96 return 0;
90} 97}
@@ -94,9 +101,95 @@ static void input_close_polled_device(struct input_dev *input)
94 struct input_polled_dev *dev = input_get_drvdata(input); 101 struct input_polled_dev *dev = input_get_drvdata(input);
95 102
96 cancel_delayed_work_sync(&dev->work); 103 cancel_delayed_work_sync(&dev->work);
104 /*
105 * Clean up work struct to remove references to the workqueue.
106 * It may be destroyed by the next call. This causes problems
107 * at next device open-close in case of poll_interval == 0.
108 */
109 INIT_DELAYED_WORK(&dev->work, dev->work.work.func);
97 input_polldev_stop_workqueue(); 110 input_polldev_stop_workqueue();
111
112 if (dev->close)
113 dev->close(dev);
98} 114}
99 115
116/* SYSFS interface */
117
118static ssize_t input_polldev_get_poll(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct input_polled_dev *polldev = dev_get_drvdata(dev);
122
123 return sprintf(buf, "%d\n", polldev->poll_interval);
124}
125
126static ssize_t input_polldev_set_poll(struct device *dev,
127 struct device_attribute *attr, const char *buf,
128 size_t count)
129{
130 struct input_polled_dev *polldev = dev_get_drvdata(dev);
131 struct input_dev *input = polldev->input;
132 unsigned long interval;
133
134 if (strict_strtoul(buf, 0, &interval))
135 return -EINVAL;
136
137 if (interval < polldev->poll_interval_min)
138 return -EINVAL;
139
140 if (interval > polldev->poll_interval_max)
141 return -EINVAL;
142
143 mutex_lock(&input->mutex);
144
145 polldev->poll_interval = interval;
146
147 if (input->users) {
148 cancel_delayed_work_sync(&polldev->work);
149 if (polldev->poll_interval > 0)
150 input_polldev_queue_work(polldev);
151 }
152
153 mutex_unlock(&input->mutex);
154
155 return count;
156}
157
158static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
159 input_polldev_set_poll);
160
161
162static ssize_t input_polldev_get_max(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 struct input_polled_dev *polldev = dev_get_drvdata(dev);
166
167 return sprintf(buf, "%d\n", polldev->poll_interval_max);
168}
169
170static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
171
172static ssize_t input_polldev_get_min(struct device *dev,
173 struct device_attribute *attr, char *buf)
174{
175 struct input_polled_dev *polldev = dev_get_drvdata(dev);
176
177 return sprintf(buf, "%d\n", polldev->poll_interval_min);
178}
179
180static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
181
182static struct attribute *sysfs_attrs[] = {
183 &dev_attr_poll.attr,
184 &dev_attr_max.attr,
185 &dev_attr_min.attr,
186 NULL
187};
188
189static struct attribute_group input_polldev_attribute_group = {
190 .attrs = sysfs_attrs
191};
192
100/** 193/**
101 * input_allocate_polled_device - allocated memory polled device 194 * input_allocate_polled_device - allocated memory polled device
102 * 195 *
@@ -126,7 +219,7 @@ EXPORT_SYMBOL(input_allocate_polled_device);
126 * @dev: device to free 219 * @dev: device to free
127 * 220 *
128 * The function frees memory allocated for polling device and drops 221 * The function frees memory allocated for polling device and drops
129 * reference to the associated input device (if present). 222 * reference to the associated input device.
130 */ 223 */
131void input_free_polled_device(struct input_polled_dev *dev) 224void input_free_polled_device(struct input_polled_dev *dev)
132{ 225{
@@ -150,15 +243,38 @@ EXPORT_SYMBOL(input_free_polled_device);
150int input_register_polled_device(struct input_polled_dev *dev) 243int input_register_polled_device(struct input_polled_dev *dev)
151{ 244{
152 struct input_dev *input = dev->input; 245 struct input_dev *input = dev->input;
246 int error;
153 247
154 input_set_drvdata(input, dev); 248 input_set_drvdata(input, dev);
155 INIT_DELAYED_WORK(&dev->work, input_polled_device_work); 249 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
156 if (!dev->poll_interval) 250 if (!dev->poll_interval)
157 dev->poll_interval = 500; 251 dev->poll_interval = 500;
252 if (!dev->poll_interval_max)
253 dev->poll_interval_max = dev->poll_interval;
158 input->open = input_open_polled_device; 254 input->open = input_open_polled_device;
159 input->close = input_close_polled_device; 255 input->close = input_close_polled_device;
160 256
161 return input_register_device(input); 257 error = input_register_device(input);
258 if (error)
259 return error;
260
261 error = sysfs_create_group(&input->dev.kobj,
262 &input_polldev_attribute_group);
263 if (error) {
264 input_unregister_device(input);
265 return error;
266 }
267
268 /*
269 * Take extra reference to the underlying input device so
270 * that it survives call to input_unregister_polled_device()
271 * and is deleted only after input_free_polled_device()
272 * has been invoked. This is needed to ease task of freeing
273 * sparse keymaps.
274 */
275 input_get_device(input);
276
277 return 0;
162} 278}
163EXPORT_SYMBOL(input_register_polled_device); 279EXPORT_SYMBOL(input_register_polled_device);
164 280
@@ -169,13 +285,13 @@ EXPORT_SYMBOL(input_register_polled_device);
169 * The function unregisters previously registered polled input 285 * The function unregisters previously registered polled input
170 * device from input layer. Polling is stopped and device is 286 * device from input layer. Polling is stopped and device is
171 * ready to be freed with call to input_free_polled_device(). 287 * ready to be freed with call to input_free_polled_device().
172 * Callers should not attempt to access dev->input pointer
173 * after calling this function.
174 */ 288 */
175void input_unregister_polled_device(struct input_polled_dev *dev) 289void input_unregister_polled_device(struct input_polled_dev *dev)
176{ 290{
291 sysfs_remove_group(&dev->input->dev.kobj,
292 &input_polldev_attribute_group);
293
177 input_unregister_device(dev->input); 294 input_unregister_device(dev->input);
178 dev->input = NULL;
179} 295}
180EXPORT_SYMBOL(input_unregister_polled_device); 296EXPORT_SYMBOL(input_unregister_polled_device);
181 297