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 | |
| 11 | #include <linux/jiffies.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/input-polldev.h> |
| 14 | |
Eric Piel | 36bd52a | 2007-05-22 23:28:03 -0400 | [diff] [blame] | 15 | MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>"); |
| 16 | MODULE_DESCRIPTION("Generic implementation of a polled input device"); |
| 17 | MODULE_LICENSE("GPL v2"); |
| 18 | MODULE_VERSION("0.1"); |
| 19 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 20 | static DEFINE_MUTEX(polldev_mutex); |
| 21 | static int polldev_users; |
| 22 | static struct workqueue_struct *polldev_wq; |
| 23 | |
| 24 | static int input_polldev_start_workqueue(void) |
| 25 | { |
| 26 | int retval; |
| 27 | |
| 28 | retval = mutex_lock_interruptible(&polldev_mutex); |
| 29 | if (retval) |
| 30 | return retval; |
| 31 | |
| 32 | if (!polldev_users) { |
| 33 | polldev_wq = create_singlethread_workqueue("ipolldevd"); |
| 34 | if (!polldev_wq) { |
| 35 | printk(KERN_ERR "input-polldev: failed to create " |
| 36 | "ipolldevd workqueue\n"); |
| 37 | retval = -ENOMEM; |
| 38 | goto out; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | polldev_users++; |
| 43 | |
| 44 | out: |
| 45 | mutex_unlock(&polldev_mutex); |
| 46 | return retval; |
| 47 | } |
| 48 | |
| 49 | static void input_polldev_stop_workqueue(void) |
| 50 | { |
| 51 | mutex_lock(&polldev_mutex); |
| 52 | |
| 53 | if (!--polldev_users) |
| 54 | destroy_workqueue(polldev_wq); |
| 55 | |
| 56 | mutex_unlock(&polldev_mutex); |
| 57 | } |
| 58 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 59 | static void input_polldev_queue_work(struct input_polled_dev *dev) |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 60 | { |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 61 | unsigned long delay; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 62 | |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 63 | delay = msecs_to_jiffies(dev->poll_interval); |
| 64 | if (delay >= HZ) |
| 65 | delay = round_jiffies_relative(delay); |
| 66 | |
| 67 | queue_delayed_work(polldev_wq, &dev->work, delay); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 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 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 79 | static int input_open_polled_device(struct input_dev *input) |
| 80 | { |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 81 | struct input_polled_dev *dev = input_get_drvdata(input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 82 | int error; |
| 83 | |
| 84 | error = input_polldev_start_workqueue(); |
| 85 | if (error) |
| 86 | return error; |
| 87 | |
Samu Onkalo | b0aba1e | 2009-10-18 00:38:57 -0700 | [diff] [blame] | 88 | if (dev->open) |
| 89 | dev->open(dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 90 | |
Samu Onkalo | 11bb4cc | 2009-11-23 10:01:33 -0800 | [diff] [blame] | 91 | /* Only start polling if polling is enabled */ |
| 92 | if (dev->poll_interval > 0) |
| 93 | queue_delayed_work(polldev_wq, &dev->work, 0); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static void input_close_polled_device(struct input_dev *input) |
| 99 | { |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 100 | struct input_polled_dev *dev = input_get_drvdata(input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 101 | |
Stephen Hemminger | 374766b | 2007-11-21 14:03:37 -0500 | [diff] [blame] | 102 | cancel_delayed_work_sync(&dev->work); |
Samu Onkalo | d9c4f84 | 2010-02-19 23:17:58 -0800 | [diff] [blame] | 103 | /* |
| 104 | * Clean up work struct to remove references to the workqueue. |
| 105 | * It may be destroyed by the next call. This causes problems |
| 106 | * at next device open-close in case of poll_interval == 0. |
| 107 | */ |
| 108 | INIT_DELAYED_WORK(&dev->work, dev->work.work.func); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 109 | input_polldev_stop_workqueue(); |
Samu Onkalo | b0aba1e | 2009-10-18 00:38:57 -0700 | [diff] [blame] | 110 | |
| 111 | if (dev->close) |
| 112 | dev->close(dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 115 | /* SYSFS interface */ |
| 116 | |
| 117 | static ssize_t input_polldev_get_poll(struct device *dev, |
| 118 | struct device_attribute *attr, char *buf) |
| 119 | { |
| 120 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 121 | |
| 122 | return sprintf(buf, "%d\n", polldev->poll_interval); |
| 123 | } |
| 124 | |
| 125 | static ssize_t input_polldev_set_poll(struct device *dev, |
| 126 | struct device_attribute *attr, const char *buf, |
| 127 | size_t count) |
| 128 | { |
| 129 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 130 | struct input_dev *input = polldev->input; |
| 131 | unsigned long interval; |
| 132 | |
| 133 | if (strict_strtoul(buf, 0, &interval)) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | if (interval < polldev->poll_interval_min) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | if (interval > polldev->poll_interval_max) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | mutex_lock(&input->mutex); |
| 143 | |
| 144 | polldev->poll_interval = interval; |
| 145 | |
| 146 | if (input->users) { |
| 147 | cancel_delayed_work_sync(&polldev->work); |
| 148 | if (polldev->poll_interval > 0) |
| 149 | input_polldev_queue_work(polldev); |
| 150 | } |
| 151 | |
| 152 | mutex_unlock(&input->mutex); |
| 153 | |
| 154 | return count; |
| 155 | } |
| 156 | |
| 157 | static DEVICE_ATTR(poll, S_IRUGO | S_IWUSR, input_polldev_get_poll, |
| 158 | input_polldev_set_poll); |
| 159 | |
| 160 | |
| 161 | static ssize_t input_polldev_get_max(struct device *dev, |
| 162 | struct device_attribute *attr, char *buf) |
| 163 | { |
| 164 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 165 | |
| 166 | return sprintf(buf, "%d\n", polldev->poll_interval_max); |
| 167 | } |
| 168 | |
| 169 | static DEVICE_ATTR(max, S_IRUGO, input_polldev_get_max, NULL); |
| 170 | |
| 171 | static ssize_t input_polldev_get_min(struct device *dev, |
| 172 | struct device_attribute *attr, char *buf) |
| 173 | { |
| 174 | struct input_polled_dev *polldev = dev_get_drvdata(dev); |
| 175 | |
| 176 | return sprintf(buf, "%d\n", polldev->poll_interval_min); |
| 177 | } |
| 178 | |
| 179 | static DEVICE_ATTR(min, S_IRUGO, input_polldev_get_min, NULL); |
| 180 | |
| 181 | static struct attribute *sysfs_attrs[] = { |
| 182 | &dev_attr_poll.attr, |
| 183 | &dev_attr_max.attr, |
| 184 | &dev_attr_min.attr, |
| 185 | NULL |
| 186 | }; |
| 187 | |
| 188 | static struct attribute_group input_polldev_attribute_group = { |
| 189 | .attrs = sysfs_attrs |
| 190 | }; |
| 191 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 192 | /** |
| 193 | * input_allocate_polled_device - allocated memory polled device |
| 194 | * |
| 195 | * The function allocates memory for a polled device and also |
| 196 | * for an input device associated with this polled device. |
| 197 | */ |
| 198 | struct input_polled_dev *input_allocate_polled_device(void) |
| 199 | { |
| 200 | struct input_polled_dev *dev; |
| 201 | |
| 202 | dev = kzalloc(sizeof(struct input_polled_dev), GFP_KERNEL); |
| 203 | if (!dev) |
| 204 | return NULL; |
| 205 | |
| 206 | dev->input = input_allocate_device(); |
| 207 | if (!dev->input) { |
| 208 | kfree(dev); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | return dev; |
| 213 | } |
| 214 | EXPORT_SYMBOL(input_allocate_polled_device); |
| 215 | |
| 216 | /** |
| 217 | * input_free_polled_device - free memory allocated for polled device |
| 218 | * @dev: device to free |
| 219 | * |
| 220 | * The function frees memory allocated for polling device and drops |
Dmitry Torokhov | 36203c4 | 2009-12-04 10:22:23 -0800 | [diff] [blame] | 221 | * reference to the associated input device. |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 222 | */ |
| 223 | void input_free_polled_device(struct input_polled_dev *dev) |
| 224 | { |
| 225 | if (dev) { |
| 226 | input_free_device(dev->input); |
| 227 | kfree(dev); |
| 228 | } |
| 229 | } |
| 230 | EXPORT_SYMBOL(input_free_polled_device); |
| 231 | |
| 232 | /** |
| 233 | * input_register_polled_device - register polled device |
| 234 | * @dev: device to register |
| 235 | * |
| 236 | * The function registers previously initialized polled input device |
| 237 | * with input layer. The device should be allocated with call to |
| 238 | * input_allocate_polled_device(). Callers should also set up poll() |
| 239 | * method and set up capabilities (id, name, phys, bits) of the |
| 240 | * corresponing input_dev structure. |
| 241 | */ |
| 242 | int input_register_polled_device(struct input_polled_dev *dev) |
| 243 | { |
| 244 | struct input_dev *input = dev->input; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 245 | int error; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 246 | |
Dmitry Torokhov | 3797fec | 2008-04-02 00:41:00 -0400 | [diff] [blame] | 247 | input_set_drvdata(input, dev); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 248 | INIT_DELAYED_WORK(&dev->work, input_polled_device_work); |
| 249 | if (!dev->poll_interval) |
| 250 | dev->poll_interval = 500; |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 251 | if (!dev->poll_interval_max) |
| 252 | dev->poll_interval_max = dev->poll_interval; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 253 | input->open = input_open_polled_device; |
| 254 | input->close = input_close_polled_device; |
| 255 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 256 | error = input_register_device(input); |
| 257 | if (error) |
| 258 | return error; |
| 259 | |
| 260 | error = sysfs_create_group(&input->dev.kobj, |
| 261 | &input_polldev_attribute_group); |
| 262 | if (error) { |
| 263 | input_unregister_device(input); |
| 264 | return error; |
| 265 | } |
| 266 | |
Dmitry Torokhov | 36203c4 | 2009-12-04 10:22:23 -0800 | [diff] [blame] | 267 | /* |
| 268 | * Take extra reference to the underlying input device so |
| 269 | * that it survives call to input_unregister_polled_device() |
| 270 | * and is deleted only after input_free_polled_device() |
| 271 | * has been invoked. This is needed to ease task of freeing |
| 272 | * sparse keymaps. |
| 273 | */ |
| 274 | input_get_device(input); |
| 275 | |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 276 | return 0; |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 277 | } |
| 278 | EXPORT_SYMBOL(input_register_polled_device); |
| 279 | |
| 280 | /** |
| 281 | * input_unregister_polled_device - unregister polled device |
| 282 | * @dev: device to unregister |
| 283 | * |
| 284 | * The function unregisters previously registered polled input |
| 285 | * device from input layer. Polling is stopped and device is |
| 286 | * ready to be freed with call to input_free_polled_device(). |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 287 | */ |
| 288 | void input_unregister_polled_device(struct input_polled_dev *dev) |
| 289 | { |
Samu Onkalo | dad725d | 2009-11-13 21:13:22 -0800 | [diff] [blame] | 290 | sysfs_remove_group(&dev->input->dev.kobj, |
| 291 | &input_polldev_attribute_group); |
| 292 | |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 293 | input_unregister_device(dev->input); |
Dmitry Torokhov | 0dcd807 | 2007-04-29 23:42:45 -0400 | [diff] [blame] | 294 | } |
| 295 | EXPORT_SYMBOL(input_unregister_polled_device); |
| 296 | |