blob: b1aabde87523176f17ce0896e98403d4fda6f0a1 [file] [log] [blame]
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -04001/*
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
Joe Perchesda0c4902010-11-29 23:33:07 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040013#include <linux/jiffies.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040015#include <linux/mutex.h>
Dmitry Torokhove490ebd2011-04-27 23:20:16 -070016#include <linux/workqueue.h>
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040017#include <linux/input-polldev.h>
18
Eric Piel36bd52a2007-05-22 23:28:03 -040019MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
20MODULE_DESCRIPTION("Generic implementation of a polled input device");
21MODULE_LICENSE("GPL v2");
22MODULE_VERSION("0.1");
23
Samu Onkalodad725d2009-11-13 21:13:22 -080024static void input_polldev_queue_work(struct input_polled_dev *dev)
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040025{
Stephen Hemminger374766b2007-11-21 14:03:37 -050026 unsigned long delay;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040027
Stephen Hemminger374766b2007-11-21 14:03:37 -050028 delay = msecs_to_jiffies(dev->poll_interval);
29 if (delay >= HZ)
30 delay = round_jiffies_relative(delay);
31
Dmitry Torokhove490ebd2011-04-27 23:20:16 -070032 queue_delayed_work(system_freezable_wq, &dev->work, delay);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040033}
34
Samu Onkalodad725d2009-11-13 21:13:22 -080035static 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
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040044static int input_open_polled_device(struct input_dev *input)
45{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040046 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040047
Samu Onkalob0aba1e2009-10-18 00:38:57 -070048 if (dev->open)
49 dev->open(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040050
Samu Onkalo11bb4cc2009-11-23 10:01:33 -080051 /* Only start polling if polling is enabled */
52 if (dev->poll_interval > 0)
Dmitry Torokhove490ebd2011-04-27 23:20:16 -070053 queue_delayed_work(system_freezable_wq, &dev->work, 0);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040054
55 return 0;
56}
57
58static void input_close_polled_device(struct input_dev *input)
59{
Dmitry Torokhov3797fec2008-04-02 00:41:00 -040060 struct input_polled_dev *dev = input_get_drvdata(input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040061
Stephen Hemminger374766b2007-11-21 14:03:37 -050062 cancel_delayed_work_sync(&dev->work);
Samu Onkalob0aba1e2009-10-18 00:38:57 -070063
64 if (dev->close)
65 dev->close(dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -040066}
67
Samu Onkalodad725d2009-11-13 21:13:22 -080068/* 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
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400145/**
Dmitry Torokhov2546bcc2011-01-31 21:06:34 -0800146 * input_allocate_polled_device - allocate memory for polled device
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400147 *
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
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800174 * reference to the associated input device.
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400175 */
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
Dmitry Torokhov2546bcc2011-01-31 21:06:34 -0800193 * corresponding input_dev structure.
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400194 */
195int input_register_polled_device(struct input_polled_dev *dev)
196{
197 struct input_dev *input = dev->input;
Samu Onkalodad725d2009-11-13 21:13:22 -0800198 int error;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400199
Dmitry Torokhov3797fec2008-04-02 00:41:00 -0400200 input_set_drvdata(input, dev);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400201 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
202 if (!dev->poll_interval)
203 dev->poll_interval = 500;
Samu Onkalodad725d2009-11-13 21:13:22 -0800204 if (!dev->poll_interval_max)
205 dev->poll_interval_max = dev->poll_interval;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400206 input->open = input_open_polled_device;
207 input->close = input_close_polled_device;
208
Samu Onkalodad725d2009-11-13 21:13:22 -0800209 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
Dmitry Torokhov36203c42009-12-04 10:22:23 -0800220 /*
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
Samu Onkalodad725d2009-11-13 21:13:22 -0800229 return 0;
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400230}
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().
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400240 */
241void input_unregister_polled_device(struct input_polled_dev *dev)
242{
Samu Onkalodad725d2009-11-13 21:13:22 -0800243 sysfs_remove_group(&dev->input->dev.kobj,
244 &input_polldev_attribute_group);
245
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400246 input_unregister_device(dev->input);
Dmitry Torokhov0dcd8072007-04-29 23:42:45 -0400247}
248EXPORT_SYMBOL(input_unregister_polled_device);