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