xhci 1.0: Set transfer burst count field.
[pandora-kernel.git] / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/jiffies.h>
14 #include <linux/slab.h>
15 #include <linux/mutex.h>
16 #include <linux/input-polldev.h>
17
18 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
19 MODULE_DESCRIPTION("Generic implementation of a polled input device");
20 MODULE_LICENSE("GPL v2");
21 MODULE_VERSION("0.1");
22
23 static DEFINE_MUTEX(polldev_mutex);
24 static int polldev_users;
25 static struct workqueue_struct *polldev_wq;
26
27 static 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) {
38                         pr_err("failed to create ipolldevd workqueue\n");
39                         retval = -ENOMEM;
40                         goto out;
41                 }
42         }
43
44         polldev_users++;
45
46  out:
47         mutex_unlock(&polldev_mutex);
48         return retval;
49 }
50
51 static 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
61 static void input_polldev_queue_work(struct input_polled_dev *dev)
62 {
63         unsigned long delay;
64
65         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);
70 }
71
72 static 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
81 static int input_open_polled_device(struct input_dev *input)
82 {
83         struct input_polled_dev *dev = input_get_drvdata(input);
84         int error;
85
86         error = input_polldev_start_workqueue();
87         if (error)
88                 return error;
89
90         if (dev->open)
91                 dev->open(dev);
92
93         /* Only start polling if polling is enabled */
94         if (dev->poll_interval > 0)
95                 queue_delayed_work(polldev_wq, &dev->work, 0);
96
97         return 0;
98 }
99
100 static void input_close_polled_device(struct input_dev *input)
101 {
102         struct input_polled_dev *dev = input_get_drvdata(input);
103
104         cancel_delayed_work_sync(&dev->work);
105         /*
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);
111         input_polldev_stop_workqueue();
112
113         if (dev->close)
114                 dev->close(dev);
115 }
116
117 /* SYSFS interface */
118
119 static 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
127 static 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
159 static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
160                                             input_polldev_set_poll);
161
162
163 static 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
171 static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
172
173 static 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
181 static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
182
183 static struct attribute *sysfs_attrs[] = {
184         &dev_attr_poll.attr,
185         &dev_attr_max.attr,
186         &dev_attr_min.attr,
187         NULL
188 };
189
190 static struct attribute_group input_polldev_attribute_group = {
191         .attrs = sysfs_attrs
192 };
193
194 /**
195  * input_allocate_polled_device - allocate memory for polled device
196  *
197  * The function allocates memory for a polled device and also
198  * for an input device associated with this polled device.
199  */
200 struct 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 }
216 EXPORT_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
223  * reference to the associated input device.
224  */
225 void input_free_polled_device(struct input_polled_dev *dev)
226 {
227         if (dev) {
228                 input_free_device(dev->input);
229                 kfree(dev);
230         }
231 }
232 EXPORT_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
242  * corresponding input_dev structure.
243  */
244 int input_register_polled_device(struct input_polled_dev *dev)
245 {
246         struct input_dev *input = dev->input;
247         int error;
248
249         input_set_drvdata(input, dev);
250         INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
251         if (!dev->poll_interval)
252                 dev->poll_interval = 500;
253         if (!dev->poll_interval_max)
254                 dev->poll_interval_max = dev->poll_interval;
255         input->open = input_open_polled_device;
256         input->close = input_close_polled_device;
257
258         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
269         /*
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
278         return 0;
279 }
280 EXPORT_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().
289  */
290 void input_unregister_polled_device(struct input_polled_dev *dev)
291 {
292         sysfs_remove_group(&dev->input->dev.kobj,
293                            &input_polldev_attribute_group);
294
295         input_unregister_device(dev->input);
296 }
297 EXPORT_SYMBOL(input_unregister_polled_device);
298