Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 1 | /* |
| 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 Perches | da0c490 | 2010-11-29 23:33:07 -0800 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 13 | #include <linux/jiffies.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Dmitry Torokhov | e490ebd | 2011-04-27 23:20:16 -0700 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Paul Gortmaker | d2d8442 | 2011-07-03 13:53:48 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 18 | #include <linux/input-polldev.h> |
| 19 | |
Eric Piel | 36bd52a | 2007-05-22 23:28:03 -0400 | [diff] [blame] | 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"); |
| 24 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 25 | static void input_polldev_queue_work(struct input_polled_dev *dev) |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 26 | { |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 27 | unsigned long delay; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 28 | |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 29 | delay = msecs_to_jiffies(dev->poll_interval); |
| 30 | if (delay >= HZ) |
| 31 | delay = round_jiffies_relative(delay); |
| 32 | |
Dmitry Torokhov | e490ebd | 2011-04-27 23:20:16 -0700 | [diff] [blame] | 33 | queue_delayed_work(system_freezable_wq, &dev->work, delay); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 36 | static void input_polled_device_work(struct work_struct *work) |
| 37 | { |
| 38 | struct input_polled_dev *dev = |
| 39 | container_of(work, struct input_polled_dev, work.work); |
| 40 | |
| 41 | dev->poll(dev); |
| 42 | input_polldev_queue_work(dev); |
| 43 | } |
| 44 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 45 | static int input_open_polled_device(struct input_dev *input) |
| 46 | { |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 47 | struct input_polled_dev *dev = input_get_drvdata(input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 48 | |
Samu Onkalo | b0aba1e | 2009-10-18 00:38:57 -0700 | [diff] [blame] | 49 | if (dev->open) |
| 50 | dev->open(dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 51 | |
Samu Onkalo | 11bb4cc | 2009-11-23 10:01:33 -0800 | [diff] [blame] | 52 | /* Only start polling if polling is enabled */ |
Dmitry Torokhov | 5e3e4eb | 2011-08-02 22:22:46 -0700 | [diff] [blame] | 53 | if (dev->poll_interval > 0) { |
| 54 | dev->poll(dev); |
| 55 | input_polldev_queue_work(dev); |
| 56 | } |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static void input_close_polled_device(struct input_dev *input) |
| 62 | { |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 63 | struct input_polled_dev *dev = input_get_drvdata(input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 64 | |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 65 | cancel_delayed_work_sync(&dev->work); |
Samu Onkalo | b0aba1e | 2009-10-18 00:38:57 -0700 | [diff] [blame] | 66 | |
| 67 | if (dev->close) |
| 68 | dev->close(dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 71 | /* SYSFS interface */ |
| 72 | |
| 73 | static ssize_t input_polldev_get_poll(struct device *dev, |
| 74 | struct device_attribute *attr, char *buf) |
| 75 | { |
| 76 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 77 | |
| 78 | return sprintf(buf, "%d\n", polldev->poll_interval); |
| 79 | } |
| 80 | |
| 81 | static ssize_t input_polldev_set_poll(struct device *dev, |
| 82 | struct device_attribute *attr, const char *buf, |
| 83 | size_t count) |
| 84 | { |
| 85 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 86 | struct input_dev *input = polldev->input; |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 87 | unsigned int interval; |
| 88 | int err; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 89 | |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 90 | err = kstrtouint(buf, 0, &interval); |
| 91 | if (err) |
| 92 | return err; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 93 | |
| 94 | if (interval < polldev->poll_interval_min) |
| 95 | return -EINVAL; |
| 96 | |
| 97 | if (interval > polldev->poll_interval_max) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | mutex_lock(&input->mutex); |
| 101 | |
| 102 | polldev->poll_interval = interval; |
| 103 | |
| 104 | if (input->users) { |
| 105 | cancel_delayed_work_sync(&polldev->work); |
| 106 | if (polldev->poll_interval > 0) |
| 107 | input_polldev_queue_work(polldev); |
| 108 | } |
| 109 | |
| 110 | mutex_unlock(&input->mutex); |
| 111 | |
| 112 | return count; |
| 113 | } |
| 114 | |
| 115 | static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll, |
| 116 | input_polldev_set_poll); |
| 117 | |
| 118 | |
| 119 | static ssize_t input_polldev_get_max(struct device *dev, |
| 120 | struct device_attribute *attr, char *buf) |
| 121 | { |
| 122 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 123 | |
| 124 | return sprintf(buf, "%d\n", polldev->poll_interval_max); |
| 125 | } |
| 126 | |
| 127 | static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL); |
| 128 | |
| 129 | static ssize_t input_polldev_get_min(struct device *dev, |
| 130 | struct device_attribute *attr, char *buf) |
| 131 | { |
| 132 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 133 | |
| 134 | return sprintf(buf, "%d\n", polldev->poll_interval_min); |
| 135 | } |
| 136 | |
| 137 | static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL); |
| 138 | |
| 139 | static struct attribute *sysfs_attrs[] = { |
| 140 | &dev_attr_poll.attr, |
| 141 | &dev_attr_max.attr, |
| 142 | &dev_attr_min.attr, |
| 143 | NULL |
| 144 | }; |
| 145 | |
| 146 | static struct attribute_group input_polldev_attribute_group = { |
| 147 | .attrs = sysfs_attrs |
| 148 | }; |
| 149 | |
Dmitry Torokhov | d1fefd5 | 2014-04-28 13:16:00 -0700 | [diff] [blame^] | 150 | static const struct attribute_group *input_polldev_attribute_groups[] = { |
| 151 | &input_polldev_attribute_group, |
| 152 | NULL |
| 153 | }; |
| 154 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 155 | /** |
Dmitry Torokhov | 2546bcc | 2011-01-31 21:06:34 -0800 | [diff] [blame] | 156 | * input_allocate_polled_device - allocate memory for polled device |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 157 | * |
| 158 | * The function allocates memory for a polled device and also |
| 159 | * for an input device associated with this polled device. |
| 160 | */ |
| 161 | struct input_polled_dev *input_allocate_polled_device(void) |
| 162 | { |
| 163 | struct input_polled_dev *dev; |
| 164 | |
| 165 | dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL); |
| 166 | if (!dev) |
| 167 | return NULL; |
| 168 | |
| 169 | dev->input = input_allocate_device(); |
| 170 | if (!dev->input) { |
| 171 | kfree(dev); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | return dev; |
| 176 | } |
| 177 | EXPORT_SYMBOL(input_allocate_polled_device); |
| 178 | |
| 179 | /** |
| 180 | * input_free_polled_device - free memory allocated for polled device |
| 181 | * @dev: device to free |
| 182 | * |
| 183 | * The function frees memory allocated for polling device and drops |
Dmitry Torokhov | 36203c4 | 2009-12-04 10:22:23 -0800 | [diff] [blame] | 184 | * reference to the associated input device. |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 185 | */ |
| 186 | void input_free_polled_device(struct input_polled_dev *dev) |
| 187 | { |
| 188 | if (dev) { |
| 189 | input_free_device(dev->input); |
| 190 | kfree(dev); |
| 191 | } |
| 192 | } |
| 193 | EXPORT_SYMBOL(input_free_polled_device); |
| 194 | |
| 195 | /** |
| 196 | * input_register_polled_device - register polled device |
| 197 | * @dev: device to register |
| 198 | * |
| 199 | * The function registers previously initialized polled input device |
| 200 | * with input layer. The device should be allocated with call to |
| 201 | * input_allocate_polled_device(). Callers should also set up poll() |
| 202 | * method and set up capabilities (id, name, phys, bits) of the |
Dmitry Torokhov | 2546bcc | 2011-01-31 21:06:34 -0800 | [diff] [blame] | 203 | * corresponding input_dev structure. |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 204 | */ |
| 205 | int input_register_polled_device(struct input_polled_dev *dev) |
| 206 | { |
| 207 | struct input_dev *input = dev->input; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 208 | int error; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 209 | |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 210 | input_set_drvdata(input, dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 211 | INIT_DELAYED_WORK(&dev->work, input_polled_device_work); |
Dmitry Torokhov | d1fefd5 | 2014-04-28 13:16:00 -0700 | [diff] [blame^] | 212 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 213 | if (!dev->poll_interval) |
| 214 | dev->poll_interval = 500; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 215 | if (!dev->poll_interval_max) |
| 216 | dev->poll_interval_max = dev->poll_interval; |
Dmitry Torokhov | d1fefd5 | 2014-04-28 13:16:00 -0700 | [diff] [blame^] | 217 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 218 | input->open = input_open_polled_device; |
| 219 | input->close = input_close_polled_device; |
| 220 | |
Dmitry Torokhov | d1fefd5 | 2014-04-28 13:16:00 -0700 | [diff] [blame^] | 221 | input->dev.groups = input_polldev_attribute_groups; |
| 222 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 223 | error = input_register_device(input); |
| 224 | if (error) |
| 225 | return error; |
| 226 | |
Dmitry Torokhov | 36203c4 | 2009-12-04 10:22:23 -0800 | [diff] [blame] | 227 | /* |
| 228 | * Take extra reference to the underlying input device so |
| 229 | * that it survives call to input_unregister_polled_device() |
| 230 | * and is deleted only after input_free_polled_device() |
| 231 | * has been invoked. This is needed to ease task of freeing |
| 232 | * sparse keymaps. |
| 233 | */ |
| 234 | input_get_device(input); |
| 235 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 236 | return 0; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 237 | } |
| 238 | EXPORT_SYMBOL(input_register_polled_device); |
| 239 | |
| 240 | /** |
| 241 | * input_unregister_polled_device - unregister polled device |
| 242 | * @dev: device to unregister |
| 243 | * |
| 244 | * The function unregisters previously registered polled input |
| 245 | * device from input layer. Polling is stopped and device is |
| 246 | * ready to be freed with call to input_free_polled_device(). |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 247 | */ |
| 248 | void input_unregister_polled_device(struct input_polled_dev *dev) |
| 249 | { |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 250 | input_unregister_device(dev->input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 251 | } |
| 252 | EXPORT_SYMBOL(input_unregister_polled_device); |