Input: input-polldev - add sysfs interface for controlling poll interval
[safe/jmp/linux-2.6] / drivers / input / input-polldev.c
1 /*
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
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");
19
20 static DEFINE_MUTEX(polldev_mutex);
21 static int polldev_users;
22 static struct workqueue_struct *polldev_wq;
23
24 static 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
49 static 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
59 static void input_polldev_queue_work(struct input_polled_dev *dev)
60 {
61         unsigned long delay;
62
63         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);
68 }
69
70 static 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
79 static int input_open_polled_device(struct input_dev *input)
80 {
81         struct input_polled_dev *dev = input_get_drvdata(input);
82         int error;
83
84         error = input_polldev_start_workqueue();
85         if (error)
86                 return error;
87
88         if (dev->open)
89                 dev->open(dev);
90
91         queue_delayed_work(polldev_wq, &dev->work,
92                            msecs_to_jiffies(dev->poll_interval));
93
94         return 0;
95 }
96
97 static void input_close_polled_device(struct input_dev *input)
98 {
99         struct input_polled_dev *dev = input_get_drvdata(input);
100
101         cancel_delayed_work_sync(&dev->work);
102         input_polldev_stop_workqueue();
103
104         if (dev->close)
105                 dev->close(dev);
106 }
107
108 /* SYSFS interface */
109
110 static 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
118 static 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
150 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
151                                             input_polldev_set_poll);
152
153
154 static 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
162 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
163
164 static 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
172 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
173
174 static struct attribute *sysfs_attrs[] = {
175         &dev_attr_poll.attr,
176         &dev_attr_max.attr,
177         &dev_attr_min.attr,
178         NULL
179 };
180
181 static struct attribute_group input_polldev_attribute_group = {
182         .attrs = sysfs_attrs
183 };
184
185 /**
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  */
191 struct 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 }
207 EXPORT_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  */
216 void input_free_polled_device(struct input_polled_dev *dev)
217 {
218         if (dev) {
219                 input_free_device(dev->input);
220                 kfree(dev);
221         }
222 }
223 EXPORT_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  */
235 int input_register_polled_device(struct input_polled_dev *dev)
236 {
237         struct input_dev *input = dev->input;
238         int error;
239
240         input_set_drvdata(input, dev);
241         INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
242         if (!dev->poll_interval)
243                 dev->poll_interval = 500;
244         if (!dev->poll_interval_max)
245                 dev->poll_interval_max = dev->poll_interval;
246         input->open = input_open_polled_device;
247         input->close = input_close_polled_device;
248
249         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;
261 }
262 EXPORT_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  */
274 void input_unregister_polled_device(struct input_polled_dev *dev)
275 {
276         sysfs_remove_group(&dev->input->dev.kobj,
277                            &input_polldev_attribute_group);
278
279         input_unregister_device(dev->input);
280         dev->input = NULL;
281 }
282 EXPORT_SYMBOL(input_unregister_polled_device);
283