Skip to content

Commit dad725d

Browse files
Samu Onkalodtor
authored andcommitted
Input: input-polldev - add sysfs interface for controlling poll interval
Sysfs entry for reading and setting of the polling interval. If the interval is set to 0, polling is stopped. Polling is restarted when interval is changed to non-zero. sysfs entries: poll = current polling interval in msec (RW) max = max allowed polling interval (RO) min = min allowed polling interval (RO) Minimum and maximum limit for interval can be set while setting up the device. Interval can be adjusted even if the input device is not currently open. [dtor@mail.ru: add kernel doc markup for the new fields] Signed-off-by: Samu Onkalo <samu.p.onkalo@nokia.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent bc8f1ea commit dad725d

2 files changed

Lines changed: 115 additions & 7 deletions

File tree

‎drivers/input/input-polldev.c‎

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,26 @@ static void input_polldev_stop_workqueue(void)
5656
mutex_unlock(&polldev_mutex);
5757
}
5858

59-
static void input_polled_device_work(struct work_struct *work)
59+
static void input_polldev_queue_work(struct input_polled_dev *dev)
6060
{
61-
struct input_polled_dev *dev =
62-
container_of(work, struct input_polled_dev, work.work);
6361
unsigned long delay;
6462

65-
dev->poll(dev);
66-
6763
delay = msecs_to_jiffies(dev->poll_interval);
6864
if (delay >= HZ)
6965
delay = round_jiffies_relative(delay);
7066

7167
queue_delayed_work(polldev_wq, &dev->work, delay);
7268
}
7369

70+
static void input_polled_device_work(struct work_struct *work)
71+
{
72+
struct input_polled_dev *dev =
73+
container_of(work, struct input_polled_dev, work.work);
74+
75+
dev->poll(dev);
76+
input_polldev_queue_work(dev);
77+
}
78+
7479
static int input_open_polled_device(struct input_dev *input)
7580
{
7681
struct input_polled_dev *dev = input_get_drvdata(input);
@@ -100,6 +105,83 @@ static void input_close_polled_device(struct input_dev *input)
100105
dev->close(dev);
101106
}
102107

108+
/* SYSFS interface */
109+
110+
static ssize_t input_polldev_get_poll(struct device *dev,
111+
struct device_attribute *attr, char *buf)
112+
{
113+
struct input_polled_dev *polldev = dev_get_drvdata(dev);
114+
115+
return sprintf(buf, "%d\n", polldev->poll_interval);
116+
}
117+
118+
static ssize_t input_polldev_set_poll(struct device *dev,
119+
struct device_attribute *attr, const char *buf,
120+
size_t count)
121+
{
122+
struct input_polled_dev *polldev = dev_get_drvdata(dev);
123+
struct input_dev *input = polldev->input;
124+
unsigned long interval;
125+
126+
if (strict_strtoul(buf, 0, &interval))
127+
return -EINVAL;
128+
129+
if (interval < polldev->poll_interval_min)
130+
return -EINVAL;
131+
132+
if (interval > polldev->poll_interval_max)
133+
return -EINVAL;
134+
135+
mutex_lock(&input->mutex);
136+
137+
polldev->poll_interval = interval;
138+
139+
if (input->users) {
140+
cancel_delayed_work_sync(&polldev->work);
141+
if (polldev->poll_interval > 0)
142+
input_polldev_queue_work(polldev);
143+
}
144+
145+
mutex_unlock(&input->mutex);
146+
147+
return count;
148+
}
149+
150+
static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll,
151+
input_polldev_set_poll);
152+
153+
154+
static ssize_t input_polldev_get_max(struct device *dev,
155+
struct device_attribute *attr, char *buf)
156+
{
157+
struct input_polled_dev *polldev = dev_get_drvdata(dev);
158+
159+
return sprintf(buf, "%d\n", polldev->poll_interval_max);
160+
}
161+
162+
static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL);
163+
164+
static ssize_t input_polldev_get_min(struct device *dev,
165+
struct device_attribute *attr, char *buf)
166+
{
167+
struct input_polled_dev *polldev = dev_get_drvdata(dev);
168+
169+
return sprintf(buf, "%d\n", polldev->poll_interval_min);
170+
}
171+
172+
static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL);
173+
174+
static struct attribute *sysfs_attrs[] = {
175+
&dev_attr_poll.attr,
176+
&dev_attr_max.attr,
177+
&dev_attr_min.attr,
178+
NULL
179+
};
180+
181+
static struct attribute_group input_polldev_attribute_group = {
182+
.attrs = sysfs_attrs
183+
};
184+
103185
/**
104186
* input_allocate_polled_device - allocated memory polled device
105187
*
@@ -153,15 +235,29 @@ EXPORT_SYMBOL(input_free_polled_device);
153235
int input_register_polled_device(struct input_polled_dev *dev)
154236
{
155237
struct input_dev *input = dev->input;
238+
int error;
156239

157240
input_set_drvdata(input, dev);
158241
INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
159242
if (!dev->poll_interval)
160243
dev->poll_interval = 500;
244+
if (!dev->poll_interval_max)
245+
dev->poll_interval_max = dev->poll_interval;
161246
input->open = input_open_polled_device;
162247
input->close = input_close_polled_device;
163248

164-
return input_register_device(input);
249+
error = input_register_device(input);
250+
if (error)
251+
return error;
252+
253+
error = sysfs_create_group(&input->dev.kobj,
254+
&input_polldev_attribute_group);
255+
if (error) {
256+
input_unregister_device(input);
257+
return error;
258+
}
259+
260+
return 0;
165261
}
166262
EXPORT_SYMBOL(input_register_polled_device);
167263

@@ -177,6 +273,9 @@ EXPORT_SYMBOL(input_register_polled_device);
177273
*/
178274
void input_unregister_polled_device(struct input_polled_dev *dev)
179275
{
276+
sysfs_remove_group(&dev->input->dev.kobj,
277+
&input_polldev_attribute_group);
278+
180279
input_unregister_device(dev->input);
181280
dev->input = NULL;
182281
}

‎include/linux/input-polldev.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
* longer being polled. Used to put device into low power mode.
2222
* @poll: driver-supplied method that polls the device and posts
2323
* input events (mandatory).
24-
* @poll_interval: specifies how often the poll() method shoudl be called.
24+
* @poll_interval: specifies how often the poll() method should be called.
25+
* Defaults to 500 msec unless overriden when registering the device.
26+
* @poll_interval_max: specifies upper bound for the poll interval.
27+
* Defaults to the initial value of @poll_interval.
28+
* @poll_interval_min: specifies lower bound for the poll interval.
29+
* Defaults to 0.
2530
* @input: input device structire associated with the polled device.
2631
* Must be properly initialized by the driver (id, name, phys, bits).
2732
*
@@ -36,8 +41,12 @@ struct input_polled_dev {
3641
void (*close)(struct input_polled_dev *dev);
3742
void (*poll)(struct input_polled_dev *dev);
3843
unsigned int poll_interval; /* msec */
44+
unsigned int poll_interval_max; /* msec */
45+
unsigned int poll_interval_min; /* msec */
3946

4047
struct input_dev *input;
48+
49+
/* private: */
4150
struct delayed_work work;
4251
};
4352

0 commit comments

Comments
 (0)