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