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/input-polldev.h>
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");
23 static DEFINE_MUTEX(polldev_mutex
);
24 static int polldev_users
;
25 static struct workqueue_struct
*polldev_wq
;
27 static int input_polldev_start_workqueue(void)
31 retval
= mutex_lock_interruptible(&polldev_mutex
);
36 polldev_wq
= create_singlethread_workqueue("ipolldevd");
38 pr_err("failed to create ipolldevd workqueue\n");
47 mutex_unlock(&polldev_mutex
);
51 static void input_polldev_stop_workqueue(void)
53 mutex_lock(&polldev_mutex
);
56 destroy_workqueue(polldev_wq
);
58 mutex_unlock(&polldev_mutex
);
61 static void input_polldev_queue_work(struct input_polled_dev
*dev
)
65 delay
= msecs_to_jiffies(dev
->poll_interval
);
67 delay
= round_jiffies_relative(delay
);
69 queue_delayed_work(polldev_wq
, &dev
->work
, delay
);
72 static void input_polled_device_work(struct work_struct
*work
)
74 struct input_polled_dev
*dev
=
75 container_of(work
, struct input_polled_dev
, work
.work
);
78 input_polldev_queue_work(dev
);
81 static int input_open_polled_device(struct input_dev
*input
)
83 struct input_polled_dev
*dev
= input_get_drvdata(input
);
86 error
= input_polldev_start_workqueue();
93 /* Only start polling if polling is enabled */
94 if (dev
->poll_interval
> 0)
95 queue_delayed_work(polldev_wq
, &dev
->work
, 0);
100 static void input_close_polled_device(struct input_dev
*input
)
102 struct input_polled_dev
*dev
= input_get_drvdata(input
);
104 cancel_delayed_work_sync(&dev
->work
);
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.
110 INIT_DELAYED_WORK(&dev
->work
, dev
->work
.work
.func
);
111 input_polldev_stop_workqueue();
117 /* SYSFS interface */
119 static ssize_t
input_polldev_get_poll(struct device
*dev
,
120 struct device_attribute
*attr
, char *buf
)
122 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
124 return sprintf(buf
, "%d\n", polldev
->poll_interval
);
127 static ssize_t
input_polldev_set_poll(struct device
*dev
,
128 struct device_attribute
*attr
, const char *buf
,
131 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
132 struct input_dev
*input
= polldev
->input
;
133 unsigned long interval
;
135 if (strict_strtoul(buf
, 0, &interval
))
138 if (interval
< polldev
->poll_interval_min
)
141 if (interval
> polldev
->poll_interval_max
)
144 mutex_lock(&input
->mutex
);
146 polldev
->poll_interval
= interval
;
149 cancel_delayed_work_sync(&polldev
->work
);
150 if (polldev
->poll_interval
> 0)
151 input_polldev_queue_work(polldev
);
154 mutex_unlock(&input
->mutex
);
159 static DEVICE_ATTR(poll
, S_IRUGO
| S_IWUSR
, input_polldev_get_poll
,
160 input_polldev_set_poll
);
163 static ssize_t
input_polldev_get_max(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
168 return sprintf(buf
, "%d\n", polldev
->poll_interval_max
);
171 static DEVICE_ATTR(max
, S_IRUGO
, input_polldev_get_max
, NULL
);
173 static ssize_t
input_polldev_get_min(struct device
*dev
,
174 struct device_attribute
*attr
, char *buf
)
176 struct input_polled_dev
*polldev
= dev_get_drvdata(dev
);
178 return sprintf(buf
, "%d\n", polldev
->poll_interval_min
);
181 static DEVICE_ATTR(min
, S_IRUGO
, input_polldev_get_min
, NULL
);
183 static struct attribute
*sysfs_attrs
[] = {
190 static struct attribute_group input_polldev_attribute_group
= {
195 * input_allocate_polled_device - allocated memory polled device
197 * The function allocates memory for a polled device and also
198 * for an input device associated with this polled device.
200 struct input_polled_dev
*input_allocate_polled_device(void)
202 struct input_polled_dev
*dev
;
204 dev
= kzalloc(sizeof(struct input_polled_dev
), GFP_KERNEL
);
208 dev
->input
= input_allocate_device();
216 EXPORT_SYMBOL(input_allocate_polled_device
);
219 * input_free_polled_device - free memory allocated for polled device
220 * @dev: device to free
222 * The function frees memory allocated for polling device and drops
223 * reference to the associated input device.
225 void input_free_polled_device(struct input_polled_dev
*dev
)
228 input_free_device(dev
->input
);
232 EXPORT_SYMBOL(input_free_polled_device
);
235 * input_register_polled_device - register polled device
236 * @dev: device to register
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 * corresponing input_dev structure.
244 int input_register_polled_device(struct input_polled_dev
*dev
)
246 struct input_dev
*input
= dev
->input
;
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
;
258 error
= input_register_device(input
);
262 error
= sysfs_create_group(&input
->dev
.kobj
,
263 &input_polldev_attribute_group
);
265 input_unregister_device(input
);
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
276 input_get_device(input
);
280 EXPORT_SYMBOL(input_register_polled_device
);
283 * input_unregister_polled_device - unregister polled device
284 * @dev: device to unregister
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().
290 void input_unregister_polled_device(struct input_polled_dev
*dev
)
292 sysfs_remove_group(&dev
->input
->dev
.kobj
,
293 &input_polldev_attribute_group
);
295 input_unregister_device(dev
->input
);
297 EXPORT_SYMBOL(input_unregister_polled_device
);