2 * Generic implementation of a polled input device
4 * Copyright (c) 2007 Dmitry Torokhov
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
20 MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
21 MODULE_DESCRIPTION("Generic implementation of a polled input device");
22 MODULE_LICENSE("GPL v2");
23 MODULE_VERSION("0.1");
25 static void input_polldev_queue_work(struct input_polled_dev
*dev
)
29 delay
= msecs_to_jiffies(dev
->poll_interval
);
31 delay
= round_jiffies_relative(delay
);
33 queue_delayed_work(system_freezable_wq
, &dev
->work
, delay
);
36 static void input_polled_device_work(struct work_struct
*work
)
38 struct input_polled_dev
*dev
=
39 container_of(work
, struct input_polled_dev
, work
.work
);
42 input_polldev_queue_work(dev
);
45 static int input_open_polled_device(struct input_dev
*input
)
47 struct input_polled_dev
*dev
= input_get_drvdata(input
);
52 /* Only start polling if polling is enabled */
53 if (dev
->poll_interval
> 0)
54 queue_delayed_work(system_freezable_wq
, &dev
->work
, 0);
59 static void input_close_polled_device(struct input_dev
*input
)
61 struct input_polled_dev
*dev
= input_get_drvdata(input
);
63 cancel_delayed_work_sync(&dev
->work
);
71 static ssize_t
input_polldev_get_poll(struct device
*dev
,
72 struct device_attribute
*attr
, char *buf
)
74 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
76 return sprintf(buf
, "%d\n", polldev
->poll_interval
);
79 static ssize_t
input_polldev_set_poll(struct device
*dev
,
80 struct device_attribute
*attr
, const char *buf
,
83 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
84 struct input_dev
*input
= polldev
->input
;
85 unsigned long interval
;
87 if (strict_strtoul(buf
, 0, &interval
))
90 if (interval
< polldev
->poll_interval_min
)
93 if (interval
> polldev
->poll_interval_max
)
96 mutex_lock(&input
->mutex
);
98 polldev
->poll_interval
= interval
;
101 cancel_delayed_work_sync(&polldev
->work
);
102 if (polldev
->poll_interval
> 0)
103 input_polldev_queue_work(polldev
);
106 mutex_unlock(&input
->mutex
);
111 static DEVICE_ATTR(poll
, S_IRUGO
| S_IWUSR
, input_polldev_get_poll
,
112 input_polldev_set_poll
);
115 static ssize_t
input_polldev_get_max(struct device
*dev
,
116 struct device_attribute
*attr
, char *buf
)
118 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
120 return sprintf(buf
, "%d\n", polldev
->poll_interval_max
);
123 static DEVICE_ATTR(max
, S_IRUGO
, input_polldev_get_max
, NULL
);
125 static ssize_t
input_polldev_get_min(struct device
*dev
,
126 struct device_attribute
*attr
, char *buf
)
128 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
130 return sprintf(buf
, "%d\n", polldev
->poll_interval_min
);
133 static DEVICE_ATTR(min
, S_IRUGO
, input_polldev_get_min
, NULL
);
135 static struct attribute
*sysfs_attrs
[] = {
142 static struct attribute_group input_polldev_attribute_group
= {
147 * input_allocate_polled_device - allocate memory for polled device
149 * The function allocates memory for a polled device and also
150 * for an input device associated with this polled device.
152 struct input_polled_dev
*input_allocate_polled_device(void)
154 struct input_polled_dev
*dev
;
156 dev
= kzalloc(sizeof(struct input_polled_dev
), GFP_KERNEL
);
160 dev
->input
= input_allocate_device();
168 EXPORT_SYMBOL(input_allocate_polled_device
);
171 * input_free_polled_device - free memory allocated for polled device
172 * @dev: device to free
174 * The function frees memory allocated for polling device and drops
175 * reference to the associated input device.
177 void input_free_polled_device(struct input_polled_dev
*dev
)
180 input_free_device(dev
->input
);
184 EXPORT_SYMBOL(input_free_polled_device
);
187 * input_register_polled_device - register polled device
188 * @dev: device to register
190 * The function registers previously initialized polled input device
191 * with input layer. The device should be allocated with call to
192 * input_allocate_polled_device(). Callers should also set up poll()
193 * method and set up capabilities (id, name, phys, bits) of the
194 * corresponding input_dev structure.
196 int input_register_polled_device(struct input_polled_dev
*dev
)
198 struct input_dev
*input
= dev
->input
;
201 input_set_drvdata(input
, dev
);
202 INIT_DELAYED_WORK(&dev
->work
, input_polled_device_work
);
203 if (!dev
->poll_interval
)
204 dev
->poll_interval
= 500;
205 if (!dev
->poll_interval_max
)
206 dev
->poll_interval_max
= dev
->poll_interval
;
207 input
->open
= input_open_polled_device
;
208 input
->close
= input_close_polled_device
;
210 error
= input_register_device(input
);
214 error
= sysfs_create_group(&input
->dev
.kobj
,
215 &input_polldev_attribute_group
);
217 input_unregister_device(input
);
222 * Take extra reference to the underlying input device so
223 * that it survives call to input_unregister_polled_device()
224 * and is deleted only after input_free_polled_device()
225 * has been invoked. This is needed to ease task of freeing
228 input_get_device(input
);
232 EXPORT_SYMBOL(input_register_polled_device
);
235 * input_unregister_polled_device - unregister polled device
236 * @dev: device to unregister
238 * The function unregisters previously registered polled input
239 * device from input layer. Polling is stopped and device is
240 * ready to be freed with call to input_free_polled_device().
242 void input_unregister_polled_device(struct input_polled_dev
*dev
)
244 sysfs_remove_group(&dev
->input
->dev
.kobj
,
245 &input_polldev_attribute_group
);
247 input_unregister_device(dev
->input
);
249 EXPORT_SYMBOL(input_unregister_polled_device
);