Linux Audio

Check our new training course

Loading...
v4.6
  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/workqueue.h>
 17#include <linux/module.h>
 18#include <linux/input-polldev.h>
 19
 20MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
 21MODULE_DESCRIPTION("Generic implementation of a polled input device");
 22MODULE_LICENSE("GPL v2");
 23MODULE_VERSION("0.1");
 24
 25static void input_polldev_queue_work(struct input_polled_dev *dev)
 26{
 27	unsigned long delay;
 28
 29	delay = msecs_to_jiffies(dev->poll_interval);
 30	if (delay >= HZ)
 31		delay = round_jiffies_relative(delay);
 32
 33	queue_delayed_work(system_freezable_wq, &dev->work, delay);
 34}
 35
 36static void input_polled_device_work(struct work_struct *work)
 37{
 38	struct input_polled_dev *dev =
 39		container_of(work, struct input_polled_dev, work.work);
 40
 41	dev->poll(dev);
 42	input_polldev_queue_work(dev);
 43}
 44
 45static int input_open_polled_device(struct input_dev *input)
 46{
 47	struct input_polled_dev *dev = input_get_drvdata(input);
 48
 49	if (dev->open)
 50		dev->open(dev);
 51
 52	/* Only start polling if polling is enabled */
 53	if (dev->poll_interval > 0) {
 54		dev->poll(dev);
 55		input_polldev_queue_work(dev);
 56	}
 57
 58	return 0;
 59}
 60
 61static void input_close_polled_device(struct input_dev *input)
 62{
 63	struct input_polled_dev *dev = input_get_drvdata(input);
 64
 65	cancel_delayed_work_sync(&dev->work);
 66
 67	if (dev->close)
 68		dev->close(dev);
 69}
 70
 71/* SYSFS interface */
 72
 73static ssize_t input_polldev_get_poll(struct device *dev,
 74				      struct device_attribute *attr, char *buf)
 75{
 76	struct input_polled_dev *polldev = dev_get_drvdata(dev);
 77
 78	return sprintf(buf, "%d\n", polldev->poll_interval);
 79}
 80
 81static ssize_t input_polldev_set_poll(struct device *dev,
 82				struct device_attribute *attr, const char *buf,
 83				size_t count)
 84{
 85	struct input_polled_dev *polldev = dev_get_drvdata(dev);
 86	struct input_dev *input = polldev->input;
 87	unsigned int interval;
 88	int err;
 89
 90	err = kstrtouint(buf, 0, &interval);
 91	if (err)
 92		return err;
 93
 94	if (interval < polldev->poll_interval_min)
 95		return -EINVAL;
 96
 97	if (interval > polldev->poll_interval_max)
 98		return -EINVAL;
 99
100	mutex_lock(&input->mutex);
101
102	polldev->poll_interval = interval;
103
104	if (input->users) {
105		cancel_delayed_work_sync(&polldev->work);
106		if (polldev->poll_interval > 0)
107			input_polldev_queue_work(polldev);
108	}
109
110	mutex_unlock(&input->mutex);
111
112	return count;
113}
114
115static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
116					    input_polldev_set_poll);
117
118
119static ssize_t input_polldev_get_max(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_max);
125}
126
127static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
128
129static ssize_t input_polldev_get_min(struct device *dev,
130				     struct device_attribute *attr, char *buf)
131{
132	struct input_polled_dev *polldev = dev_get_drvdata(dev);
133
134	return sprintf(buf, "%d\n", polldev->poll_interval_min);
135}
136
137static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
138
139static struct attribute *sysfs_attrs[] = {
140	&dev_attr_poll.attr,
141	&dev_attr_max.attr,
142	&dev_attr_min.attr,
143	NULL
144};
145
146static struct attribute_group input_polldev_attribute_group = {
147	.attrs = sysfs_attrs
148};
149
150static const struct attribute_group *input_polldev_attribute_groups[] = {
151	&input_polldev_attribute_group,
152	NULL
153};
154
155/**
156 * input_allocate_polled_device - allocate memory for polled device
157 *
158 * The function allocates memory for a polled device and also
159 * for an input device associated with this polled device.
160 */
161struct input_polled_dev *input_allocate_polled_device(void)
162{
163	struct input_polled_dev *dev;
164
165	dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
166	if (!dev)
167		return NULL;
168
169	dev->input = input_allocate_device();
170	if (!dev->input) {
171		kfree(dev);
172		return NULL;
173	}
174
175	return dev;
176}
177EXPORT_SYMBOL(input_allocate_polled_device);
178
179struct input_polled_devres {
180	struct input_polled_dev *polldev;
181};
182
183static int devm_input_polldev_match(struct device *dev, void *res, void *data)
184{
185	struct input_polled_devres *devres = res;
186
187	return devres->polldev == data;
188}
189
190static void devm_input_polldev_release(struct device *dev, void *res)
191{
192	struct input_polled_devres *devres = res;
193	struct input_polled_dev *polldev = devres->polldev;
194
195	dev_dbg(dev, "%s: dropping reference/freeing %s\n",
196		__func__, dev_name(&polldev->input->dev));
197
198	input_put_device(polldev->input);
199	kfree(polldev);
200}
201
202static void devm_input_polldev_unregister(struct device *dev, void *res)
203{
204	struct input_polled_devres *devres = res;
205	struct input_polled_dev *polldev = devres->polldev;
206
207	dev_dbg(dev, "%s: unregistering device %s\n",
208		__func__, dev_name(&polldev->input->dev));
209	input_unregister_device(polldev->input);
210
211	/*
212	 * Note that we are still holding extra reference to the input
213	 * device so it will stick around until devm_input_polldev_release()
214	 * is called.
215	 */
216}
217
218/**
219 * devm_input_allocate_polled_device - allocate managed polled device
220 * @dev: device owning the polled device being created
221 *
222 * Returns prepared &struct input_polled_dev or %NULL.
223 *
224 * Managed polled input devices do not need to be explicitly unregistered
225 * or freed as it will be done automatically when owner device unbinds
226 * from * its driver (or binding fails). Once such managed polled device
227 * is allocated, it is ready to be set up and registered in the same
228 * fashion as regular polled input devices (using
229 * input_register_polled_device() function).
230 *
231 * If you want to manually unregister and free such managed polled devices,
232 * it can be still done by calling input_unregister_polled_device() and
233 * input_free_polled_device(), although it is rarely needed.
234 *
235 * NOTE: the owner device is set up as parent of input device and users
236 * should not override it.
237 */
238struct input_polled_dev *devm_input_allocate_polled_device(struct device *dev)
239{
240	struct input_polled_dev *polldev;
241	struct input_polled_devres *devres;
242
243	devres = devres_alloc(devm_input_polldev_release, sizeof(*devres),
244			      GFP_KERNEL);
245	if (!devres)
246		return NULL;
247
248	polldev = input_allocate_polled_device();
249	if (!polldev) {
250		devres_free(devres);
251		return NULL;
252	}
253
254	polldev->input->dev.parent = dev;
255	polldev->devres_managed = true;
256
257	devres->polldev = polldev;
258	devres_add(dev, devres);
259
260	return polldev;
261}
262EXPORT_SYMBOL(devm_input_allocate_polled_device);
263
264/**
265 * input_free_polled_device - free memory allocated for polled device
266 * @dev: device to free
267 *
268 * The function frees memory allocated for polling device and drops
269 * reference to the associated input device.
270 */
271void input_free_polled_device(struct input_polled_dev *dev)
272{
273	if (dev) {
274		if (dev->devres_managed)
275			WARN_ON(devres_destroy(dev->input->dev.parent,
276						devm_input_polldev_release,
277						devm_input_polldev_match,
278						dev));
279		input_put_device(dev->input);
280		kfree(dev);
281	}
282}
283EXPORT_SYMBOL(input_free_polled_device);
284
285/**
286 * input_register_polled_device - register polled device
287 * @dev: device to register
288 *
289 * The function registers previously initialized polled input device
290 * with input layer. The device should be allocated with call to
291 * input_allocate_polled_device(). Callers should also set up poll()
292 * method and set up capabilities (id, name, phys, bits) of the
293 * corresponding input_dev structure.
294 */
295int input_register_polled_device(struct input_polled_dev *dev)
296{
297	struct input_polled_devres *devres = NULL;
298	struct input_dev *input = dev->input;
299	int error;
300
301	if (dev->devres_managed) {
302		devres = devres_alloc(devm_input_polldev_unregister,
303				      sizeof(*devres), GFP_KERNEL);
304		if (!devres)
305			return -ENOMEM;
306
307		devres->polldev = dev;
308	}
309
310	input_set_drvdata(input, dev);
311	INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
312
313	if (!dev->poll_interval)
314		dev->poll_interval = 500;
315	if (!dev->poll_interval_max)
316		dev->poll_interval_max = dev->poll_interval;
317
318	input->open = input_open_polled_device;
319	input->close = input_close_polled_device;
320
321	input->dev.groups = input_polldev_attribute_groups;
322
323	error = input_register_device(input);
 
 
 
 
 
324	if (error) {
325		devres_free(devres);
326		return error;
327	}
328
329	/*
330	 * Take extra reference to the underlying input device so
331	 * that it survives call to input_unregister_polled_device()
332	 * and is deleted only after input_free_polled_device()
333	 * has been invoked. This is needed to ease task of freeing
334	 * sparse keymaps.
335	 */
336	input_get_device(input);
337
338	if (dev->devres_managed) {
339		dev_dbg(input->dev.parent, "%s: registering %s with devres.\n",
340			__func__, dev_name(&input->dev));
341		devres_add(input->dev.parent, devres);
342	}
343
344	return 0;
345}
346EXPORT_SYMBOL(input_register_polled_device);
347
348/**
349 * input_unregister_polled_device - unregister polled device
350 * @dev: device to unregister
351 *
352 * The function unregisters previously registered polled input
353 * device from input layer. Polling is stopped and device is
354 * ready to be freed with call to input_free_polled_device().
355 */
356void input_unregister_polled_device(struct input_polled_dev *dev)
357{
358	if (dev->devres_managed)
359		WARN_ON(devres_destroy(dev->input->dev.parent,
360					devm_input_polldev_unregister,
361					devm_input_polldev_match,
362					dev));
363
364	input_unregister_device(dev->input);
365}
366EXPORT_SYMBOL(input_unregister_polled_device);
v3.1
  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/workqueue.h>
 
 17#include <linux/input-polldev.h>
 18
 19MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
 20MODULE_DESCRIPTION("Generic implementation of a polled input device");
 21MODULE_LICENSE("GPL v2");
 22MODULE_VERSION("0.1");
 23
 24static void input_polldev_queue_work(struct input_polled_dev *dev)
 25{
 26	unsigned long delay;
 27
 28	delay = msecs_to_jiffies(dev->poll_interval);
 29	if (delay >= HZ)
 30		delay = round_jiffies_relative(delay);
 31
 32	queue_delayed_work(system_freezable_wq, &dev->work, delay);
 33}
 34
 35static void input_polled_device_work(struct work_struct *work)
 36{
 37	struct input_polled_dev *dev =
 38		container_of(work, struct input_polled_dev, work.work);
 39
 40	dev->poll(dev);
 41	input_polldev_queue_work(dev);
 42}
 43
 44static int input_open_polled_device(struct input_dev *input)
 45{
 46	struct input_polled_dev *dev = input_get_drvdata(input);
 47
 48	if (dev->open)
 49		dev->open(dev);
 50
 51	/* Only start polling if polling is enabled */
 52	if (dev->poll_interval > 0)
 53		queue_delayed_work(system_freezable_wq, &dev->work, 0);
 
 
 54
 55	return 0;
 56}
 57
 58static void input_close_polled_device(struct input_dev *input)
 59{
 60	struct input_polled_dev *dev = input_get_drvdata(input);
 61
 62	cancel_delayed_work_sync(&dev->work);
 63
 64	if (dev->close)
 65		dev->close(dev);
 66}
 67
 68/* SYSFS interface */
 69
 70static ssize_t input_polldev_get_poll(struct device *dev,
 71				      struct device_attribute *attr, char *buf)
 72{
 73	struct input_polled_dev *polldev = dev_get_drvdata(dev);
 74
 75	return sprintf(buf, "%d\n", polldev->poll_interval);
 76}
 77
 78static ssize_t input_polldev_set_poll(struct device *dev,
 79				struct device_attribute *attr, const char *buf,
 80				size_t count)
 81{
 82	struct input_polled_dev *polldev = dev_get_drvdata(dev);
 83	struct input_dev *input = polldev->input;
 84	unsigned long interval;
 
 85
 86	if (strict_strtoul(buf, 0, &interval))
 87		return -EINVAL;
 
 88
 89	if (interval < polldev->poll_interval_min)
 90		return -EINVAL;
 91
 92	if (interval > polldev->poll_interval_max)
 93		return -EINVAL;
 94
 95	mutex_lock(&input->mutex);
 96
 97	polldev->poll_interval = interval;
 98
 99	if (input->users) {
100		cancel_delayed_work_sync(&polldev->work);
101		if (polldev->poll_interval > 0)
102			input_polldev_queue_work(polldev);
103	}
104
105	mutex_unlock(&input->mutex);
106
107	return count;
108}
109
110static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
111					    input_polldev_set_poll);
112
113
114static ssize_t input_polldev_get_max(struct device *dev,
115				     struct device_attribute *attr, char *buf)
116{
117	struct input_polled_dev *polldev = dev_get_drvdata(dev);
118
119	return sprintf(buf, "%d\n", polldev->poll_interval_max);
120}
121
122static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
123
124static ssize_t input_polldev_get_min(struct device *dev,
125				     struct device_attribute *attr, char *buf)
126{
127	struct input_polled_dev *polldev = dev_get_drvdata(dev);
128
129	return sprintf(buf, "%d\n", polldev->poll_interval_min);
130}
131
132static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
133
134static struct attribute *sysfs_attrs[] = {
135	&dev_attr_poll.attr,
136	&dev_attr_max.attr,
137	&dev_attr_min.attr,
138	NULL
139};
140
141static struct attribute_group input_polldev_attribute_group = {
142	.attrs = sysfs_attrs
143};
144
 
 
 
 
 
145/**
146 * input_allocate_polled_device - allocate memory for polled device
147 *
148 * The function allocates memory for a polled device and also
149 * for an input device associated with this polled device.
150 */
151struct input_polled_dev *input_allocate_polled_device(void)
152{
153	struct input_polled_dev *dev;
154
155	dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL);
156	if (!dev)
157		return NULL;
158
159	dev->input = input_allocate_device();
160	if (!dev->input) {
161		kfree(dev);
162		return NULL;
163	}
164
165	return dev;
166}
167EXPORT_SYMBOL(input_allocate_polled_device);
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169/**
170 * input_free_polled_device - free memory allocated for polled device
171 * @dev: device to free
172 *
173 * The function frees memory allocated for polling device and drops
174 * reference to the associated input device.
175 */
176void input_free_polled_device(struct input_polled_dev *dev)
177{
178	if (dev) {
179		input_free_device(dev->input);
 
 
 
 
 
180		kfree(dev);
181	}
182}
183EXPORT_SYMBOL(input_free_polled_device);
184
185/**
186 * input_register_polled_device - register polled device
187 * @dev: device to register
188 *
189 * The function registers previously initialized polled input device
190 * with input layer. The device should be allocated with call to
191 * input_allocate_polled_device(). Callers should also set up poll()
192 * method and set up capabilities (id, name, phys, bits) of the
193 * corresponding input_dev structure.
194 */
195int input_register_polled_device(struct input_polled_dev *dev)
196{
 
197	struct input_dev *input = dev->input;
198	int error;
199
 
 
 
 
 
 
 
 
 
200	input_set_drvdata(input, dev);
201	INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
 
202	if (!dev->poll_interval)
203		dev->poll_interval = 500;
204	if (!dev->poll_interval_max)
205		dev->poll_interval_max = dev->poll_interval;
 
206	input->open = input_open_polled_device;
207	input->close = input_close_polled_device;
208
 
 
209	error = input_register_device(input);
210	if (error)
211		return error;
212
213	error = sysfs_create_group(&input->dev.kobj,
214				   &input_polldev_attribute_group);
215	if (error) {
216		input_unregister_device(input);
217		return error;
218	}
219
220	/*
221	 * Take extra reference to the underlying input device so
222	 * that it survives call to input_unregister_polled_device()
223	 * and is deleted only after input_free_polled_device()
224	 * has been invoked. This is needed to ease task of freeing
225	 * sparse keymaps.
226	 */
227	input_get_device(input);
228
 
 
 
 
 
 
229	return 0;
230}
231EXPORT_SYMBOL(input_register_polled_device);
232
233/**
234 * input_unregister_polled_device - unregister polled device
235 * @dev: device to unregister
236 *
237 * The function unregisters previously registered polled input
238 * device from input layer. Polling is stopped and device is
239 * ready to be freed with call to input_free_polled_device().
240 */
241void input_unregister_polled_device(struct input_polled_dev *dev)
242{
243	sysfs_remove_group(&dev->input->dev.kobj,
244			   &input_polldev_attribute_group);
 
 
 
245
246	input_unregister_device(dev->input);
247}
248EXPORT_SYMBOL(input_unregister_polled_device);