Commit dad725d089b94bce8bbc769b7471dcfba3fbda0e

Authored by Samu Onkalo
Committed by Dmitry Torokhov
1 parent bc8f1eaf68

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>

Showing 2 changed files with 115 additions and 7 deletions Side-by-side Diff

drivers/input/input-polldev.c
... ... @@ -56,14 +56,10 @@
56 56 mutex_unlock(&polldev_mutex);
57 57 }
58 58  
59   -static void input_polled_device_work(struct work_struct *work)
  59 +static void input_polldev_queue_work(struct input_polled_dev *dev)
60 60 {
61   - struct input_polled_dev *dev =
62   - container_of(work, struct input_polled_dev, work.work);
63 61 unsigned long delay;
64 62  
65   - dev->poll(dev);
66   -
67 63 delay = msecs_to_jiffies(dev->poll_interval);
68 64 if (delay >= HZ)
69 65 delay = round_jiffies_relative(delay);
... ... @@ -71,6 +67,15 @@
71 67 queue_delayed_work(polldev_wq, &dev->work, delay);
72 68 }
73 69  
  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 +
74 79 static int input_open_polled_device(struct input_dev *input)
75 80 {
76 81 struct input_polled_dev *dev = input_get_drvdata(input);
... ... @@ -100,6 +105,83 @@
100 105 dev->close(dev);
101 106 }
102 107  
  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 +
103 185 /**
104 186 * input_allocate_polled_device - allocated memory polled device
105 187 *
106 188  
107 189  
... ... @@ -153,15 +235,29 @@
153 235 int input_register_polled_device(struct input_polled_dev *dev)
154 236 {
155 237 struct input_dev *input = dev->input;
  238 + int error;
156 239  
157 240 input_set_drvdata(input, dev);
158 241 INIT_DELAYED_WORK(&dev->work, input_polled_device_work);
159 242 if (!dev->poll_interval)
160 243 dev->poll_interval = 500;
  244 + if (!dev->poll_interval_max)
  245 + dev->poll_interval_max = dev->poll_interval;
161 246 input->open = input_open_polled_device;
162 247 input->close = input_close_polled_device;
163 248  
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;
165 261 }
166 262 EXPORT_SYMBOL(input_register_polled_device);
167 263  
... ... @@ -177,6 +273,9 @@
177 273 */
178 274 void input_unregister_polled_device(struct input_polled_dev *dev)
179 275 {
  276 + sysfs_remove_group(&dev->input->dev.kobj,
  277 + &input_polldev_attribute_group);
  278 +
180 279 input_unregister_device(dev->input);
181 280 dev->input = NULL;
182 281 }
include/linux/input-polldev.h
... ... @@ -21,7 +21,12 @@
21 21 * longer being polled. Used to put device into low power mode.
22 22 * @poll: driver-supplied method that polls the device and posts
23 23 * 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.
25 30 * @input: input device structire associated with the polled device.
26 31 * Must be properly initialized by the driver (id, name, phys, bits).
27 32 *
28 33  
... ... @@ -36,8 +41,12 @@
36 41 void (*close)(struct input_polled_dev *dev);
37 42 void (*poll)(struct input_polled_dev *dev);
38 43 unsigned int poll_interval; /* msec */
  44 + unsigned int poll_interval_max; /* msec */
  45 + unsigned int poll_interval_min; /* msec */
39 46  
40 47 struct input_dev *input;
  48 +
  49 +/* private: */
41 50 struct delayed_work work;
42 51 };
43 52