Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * The input core |
| 3 | * |
| 4 | * Copyright (c) 1999-2002 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/smp_lock.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/random.h> |
| 19 | #include <linux/major.h> |
| 20 | #include <linux/proc_fs.h> |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/device.h> |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 25 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 28 | MODULE_DESCRIPTION("Input core"); |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #define INPUT_DEVICES 256 |
| 32 | |
| 33 | static LIST_HEAD(input_dev_list); |
| 34 | static LIST_HEAD(input_handler_list); |
| 35 | |
| 36 | static struct input_handler *input_table[8]; |
| 37 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 38 | /** |
| 39 | * input_event() - report new input event |
| 40 | * @handle: device that generated the event |
| 41 | * @type: type of the event |
| 42 | * @code: event code |
| 43 | * @value: value of the event |
| 44 | * |
| 45 | * This function should be used by drivers implementing various input devices |
| 46 | * See also input_inject_event() |
| 47 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 49 | { |
| 50 | struct input_handle *handle; |
| 51 | |
| 52 | if (type > EV_MAX || !test_bit(type, dev->evbit)) |
| 53 | return; |
| 54 | |
| 55 | add_input_randomness(type, code, value); |
| 56 | |
| 57 | switch (type) { |
| 58 | |
| 59 | case EV_SYN: |
| 60 | switch (code) { |
| 61 | case SYN_CONFIG: |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 62 | if (dev->event) |
| 63 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | break; |
| 65 | |
| 66 | case SYN_REPORT: |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 67 | if (dev->sync) |
| 68 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | dev->sync = 1; |
| 70 | break; |
| 71 | } |
| 72 | break; |
| 73 | |
| 74 | case EV_KEY: |
| 75 | |
| 76 | if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) |
| 77 | return; |
| 78 | |
| 79 | if (value == 2) |
| 80 | break; |
| 81 | |
| 82 | change_bit(code, dev->key); |
| 83 | |
| 84 | if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { |
| 85 | dev->repeat_key = code; |
| 86 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); |
| 87 | } |
| 88 | |
| 89 | break; |
| 90 | |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 91 | case EV_SW: |
| 92 | |
| 93 | if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) |
| 94 | return; |
| 95 | |
| 96 | change_bit(code, dev->sw); |
| 97 | |
| 98 | break; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | case EV_ABS: |
| 101 | |
| 102 | if (code > ABS_MAX || !test_bit(code, dev->absbit)) |
| 103 | return; |
| 104 | |
| 105 | if (dev->absfuzz[code]) { |
| 106 | if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && |
| 107 | (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) |
| 108 | return; |
| 109 | |
| 110 | if ((value > dev->abs[code] - dev->absfuzz[code]) && |
| 111 | (value < dev->abs[code] + dev->absfuzz[code])) |
| 112 | value = (dev->abs[code] * 3 + value) >> 2; |
| 113 | |
| 114 | if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && |
| 115 | (value < dev->abs[code] + (dev->absfuzz[code] << 1))) |
| 116 | value = (dev->abs[code] + value) >> 1; |
| 117 | } |
| 118 | |
| 119 | if (dev->abs[code] == value) |
| 120 | return; |
| 121 | |
| 122 | dev->abs[code] = value; |
| 123 | break; |
| 124 | |
| 125 | case EV_REL: |
| 126 | |
| 127 | if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) |
| 128 | return; |
| 129 | |
| 130 | break; |
| 131 | |
| 132 | case EV_MSC: |
| 133 | |
| 134 | if (code > MSC_MAX || !test_bit(code, dev->mscbit)) |
| 135 | return; |
| 136 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 137 | if (dev->event) |
| 138 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | break; |
| 141 | |
| 142 | case EV_LED: |
| 143 | |
| 144 | if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) |
| 145 | return; |
| 146 | |
| 147 | change_bit(code, dev->led); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 148 | |
| 149 | if (dev->event) |
| 150 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | break; |
| 153 | |
| 154 | case EV_SND: |
| 155 | |
| 156 | if (code > SND_MAX || !test_bit(code, dev->sndbit)) |
| 157 | return; |
| 158 | |
Dmitry Torokhov | 8fdc194 | 2006-04-29 01:13:48 -0400 | [diff] [blame] | 159 | if (!!test_bit(code, dev->snd) != !!value) |
| 160 | change_bit(code, dev->snd); |
| 161 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 162 | if (dev->event) |
| 163 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | break; |
| 166 | |
| 167 | case EV_REP: |
| 168 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 169 | if (code > REP_MAX || value < 0 || dev->rep[code] == value) |
| 170 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | dev->rep[code] = value; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 173 | if (dev->event) |
| 174 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | break; |
| 177 | |
| 178 | case EV_FF: |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 179 | if (dev->event) |
| 180 | dev->event(dev, type, code, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
| 184 | if (type != EV_SYN) |
| 185 | dev->sync = 0; |
| 186 | |
| 187 | if (dev->grab) |
| 188 | dev->grab->handler->event(dev->grab, type, code, value); |
| 189 | else |
| 190 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 191 | if (handle->open) |
| 192 | handle->handler->event(handle, type, code, value); |
| 193 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 194 | EXPORT_SYMBOL(input_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 196 | /** |
| 197 | * input_inject_event() - send input event from input handler |
| 198 | * @handle: input handle to send event through |
| 199 | * @type: type of the event |
| 200 | * @code: event code |
| 201 | * @value: value of the event |
| 202 | * |
| 203 | * Similar to input_event() but will ignore event if device is "grabbed" and handle |
| 204 | * injecting event is not the one that owns the device. |
| 205 | */ |
| 206 | void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 207 | { |
| 208 | if (!handle->dev->grab || handle->dev->grab == handle) |
| 209 | input_event(handle->dev, type, code, value); |
| 210 | } |
| 211 | EXPORT_SYMBOL(input_inject_event); |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | static void input_repeat_key(unsigned long data) |
| 214 | { |
| 215 | struct input_dev *dev = (void *) data; |
| 216 | |
| 217 | if (!test_bit(dev->repeat_key, dev->key)) |
| 218 | return; |
| 219 | |
| 220 | input_event(dev, EV_KEY, dev->repeat_key, 2); |
| 221 | input_sync(dev); |
| 222 | |
| 223 | if (dev->rep[REP_PERIOD]) |
| 224 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); |
| 225 | } |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | int input_grab_device(struct input_handle *handle) |
| 228 | { |
| 229 | if (handle->dev->grab) |
| 230 | return -EBUSY; |
| 231 | |
| 232 | handle->dev->grab = handle; |
| 233 | return 0; |
| 234 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 235 | EXPORT_SYMBOL(input_grab_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | void input_release_device(struct input_handle *handle) |
| 238 | { |
Andrew Morton | a2b2ed2 | 2006-07-15 01:17:38 -0400 | [diff] [blame] | 239 | struct input_dev *dev = handle->dev; |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 240 | |
Andrew Morton | a2b2ed2 | 2006-07-15 01:17:38 -0400 | [diff] [blame] | 241 | if (dev->grab == handle) { |
| 242 | dev->grab = NULL; |
| 243 | |
| 244 | list_for_each_entry(handle, &dev->h_list, d_node) |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 245 | if (handle->handler->start) |
| 246 | handle->handler->start(handle); |
| 247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 249 | EXPORT_SYMBOL(input_release_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | int input_open_device(struct input_handle *handle) |
| 252 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 253 | struct input_dev *dev = handle->dev; |
| 254 | int err; |
| 255 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 256 | err = mutex_lock_interruptible(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 257 | if (err) |
| 258 | return err; |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | handle->open++; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 261 | |
| 262 | if (!dev->users++ && dev->open) |
| 263 | err = dev->open(dev); |
| 264 | |
| 265 | if (err) |
| 266 | handle->open--; |
| 267 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 268 | mutex_unlock(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 269 | |
| 270 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 272 | EXPORT_SYMBOL(input_open_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | int input_flush_device(struct input_handle* handle, struct file* file) |
| 275 | { |
| 276 | if (handle->dev->flush) |
| 277 | return handle->dev->flush(handle->dev, file); |
| 278 | |
| 279 | return 0; |
| 280 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 281 | EXPORT_SYMBOL(input_flush_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | void input_close_device(struct input_handle *handle) |
| 284 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 285 | struct input_dev *dev = handle->dev; |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | input_release_device(handle); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 288 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 289 | mutex_lock(&dev->mutex); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 290 | |
| 291 | if (!--dev->users && dev->close) |
| 292 | dev->close(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | handle->open--; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 294 | |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 295 | mutex_unlock(&dev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 297 | EXPORT_SYMBOL(input_close_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | static void input_link_handle(struct input_handle *handle) |
| 300 | { |
| 301 | list_add_tail(&handle->d_node, &handle->dev->h_list); |
| 302 | list_add_tail(&handle->h_node, &handle->handler->h_list); |
| 303 | } |
| 304 | |
| 305 | #define MATCH_BIT(bit, max) \ |
| 306 | for (i = 0; i < NBITS(max); i++) \ |
| 307 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
| 308 | break; \ |
| 309 | if (i != NBITS(max)) \ |
| 310 | continue; |
| 311 | |
| 312 | static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev) |
| 313 | { |
| 314 | int i; |
| 315 | |
| 316 | for (; id->flags || id->driver_info; id++) { |
| 317 | |
| 318 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 319 | if (id->bustype != dev->id.bustype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | continue; |
| 321 | |
| 322 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 323 | if (id->vendor != dev->id.vendor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | continue; |
| 325 | |
| 326 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 327 | if (id->product != dev->id.product) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | continue; |
| 329 | |
| 330 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 331 | if (id->version != dev->id.version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | continue; |
| 333 | |
| 334 | MATCH_BIT(evbit, EV_MAX); |
| 335 | MATCH_BIT(keybit, KEY_MAX); |
| 336 | MATCH_BIT(relbit, REL_MAX); |
| 337 | MATCH_BIT(absbit, ABS_MAX); |
| 338 | MATCH_BIT(mscbit, MSC_MAX); |
| 339 | MATCH_BIT(ledbit, LED_MAX); |
| 340 | MATCH_BIT(sndbit, SND_MAX); |
| 341 | MATCH_BIT(ffbit, FF_MAX); |
Dmitry Torokhov | ff13f98 | 2005-09-24 02:02:29 -0500 | [diff] [blame] | 342 | MATCH_BIT(swbit, SW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | return id; |
| 345 | } |
| 346 | |
| 347 | return NULL; |
| 348 | } |
| 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | #ifdef CONFIG_PROC_FS |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 351 | |
| 352 | static struct proc_dir_entry *proc_bus_input_dir; |
| 353 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); |
| 354 | static int input_devices_state; |
| 355 | |
| 356 | static inline void input_wakeup_procfs_readers(void) |
| 357 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | input_devices_state++; |
| 359 | wake_up(&input_devices_poll_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 362 | static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 364 | int state = input_devices_state; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 365 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 366 | poll_wait(file, &input_devices_poll_wait, wait); |
| 367 | if (state != input_devices_state) |
| 368 | return POLLIN | POLLRDNORM; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 369 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 370 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 373 | static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 375 | struct list_head *node; |
| 376 | loff_t i = 0; |
| 377 | |
| 378 | list_for_each(node, list) |
| 379 | if (i++ == *pos) |
| 380 | return node; |
| 381 | |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos) |
| 386 | { |
| 387 | if (element->next == list) |
| 388 | return NULL; |
| 389 | |
| 390 | ++(*pos); |
| 391 | return element->next; |
| 392 | } |
| 393 | |
| 394 | static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) |
| 395 | { |
| 396 | /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ |
| 397 | |
| 398 | return list_get_nth_element(&input_dev_list, pos); |
| 399 | } |
| 400 | |
| 401 | static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 402 | { |
| 403 | return list_get_next_element(&input_dev_list, v, pos); |
| 404 | } |
| 405 | |
| 406 | static void input_devices_seq_stop(struct seq_file *seq, void *v) |
| 407 | { |
| 408 | /* release lock here */ |
| 409 | } |
| 410 | |
| 411 | static void input_seq_print_bitmap(struct seq_file *seq, const char *name, |
| 412 | unsigned long *bitmap, int max) |
| 413 | { |
| 414 | int i; |
| 415 | |
| 416 | for (i = NBITS(max) - 1; i > 0; i--) |
| 417 | if (bitmap[i]) |
| 418 | break; |
| 419 | |
| 420 | seq_printf(seq, "B: %s=", name); |
| 421 | for (; i >= 0; i--) |
| 422 | seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : ""); |
| 423 | seq_putc(seq, '\n'); |
| 424 | } |
| 425 | |
| 426 | static int input_devices_seq_show(struct seq_file *seq, void *v) |
| 427 | { |
| 428 | struct input_dev *dev = container_of(v, struct input_dev, node); |
| 429 | const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | struct input_handle *handle; |
| 431 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 432 | seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", |
| 433 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 435 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
| 436 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
| 437 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); |
| 438 | seq_printf(seq, "H: Handlers="); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 440 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 441 | seq_printf(seq, "%s ", handle->name); |
| 442 | seq_putc(seq, '\n'); |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 443 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 444 | input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX); |
| 445 | if (test_bit(EV_KEY, dev->evbit)) |
| 446 | input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); |
| 447 | if (test_bit(EV_REL, dev->evbit)) |
| 448 | input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX); |
| 449 | if (test_bit(EV_ABS, dev->evbit)) |
| 450 | input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX); |
| 451 | if (test_bit(EV_MSC, dev->evbit)) |
| 452 | input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX); |
| 453 | if (test_bit(EV_LED, dev->evbit)) |
| 454 | input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX); |
| 455 | if (test_bit(EV_SND, dev->evbit)) |
| 456 | input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX); |
| 457 | if (test_bit(EV_FF, dev->evbit)) |
| 458 | input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX); |
| 459 | if (test_bit(EV_SW, dev->evbit)) |
| 460 | input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 462 | seq_putc(seq, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 464 | kfree(path); |
| 465 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 468 | static struct seq_operations input_devices_seq_ops = { |
| 469 | .start = input_devices_seq_start, |
| 470 | .next = input_devices_seq_next, |
| 471 | .stop = input_devices_seq_stop, |
| 472 | .show = input_devices_seq_show, |
| 473 | }; |
| 474 | |
| 475 | static int input_proc_devices_open(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 477 | return seq_open(file, &input_devices_seq_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 480 | static struct file_operations input_devices_fileops = { |
| 481 | .owner = THIS_MODULE, |
| 482 | .open = input_proc_devices_open, |
| 483 | .poll = input_proc_devices_poll, |
| 484 | .read = seq_read, |
| 485 | .llseek = seq_lseek, |
| 486 | .release = seq_release, |
| 487 | }; |
| 488 | |
| 489 | static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) |
| 490 | { |
| 491 | /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ |
| 492 | seq->private = (void *)(unsigned long)*pos; |
| 493 | return list_get_nth_element(&input_handler_list, pos); |
| 494 | } |
| 495 | |
| 496 | static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 497 | { |
| 498 | seq->private = (void *)(unsigned long)(*pos + 1); |
| 499 | return list_get_next_element(&input_handler_list, v, pos); |
| 500 | } |
| 501 | |
| 502 | static void input_handlers_seq_stop(struct seq_file *seq, void *v) |
| 503 | { |
| 504 | /* release lock here */ |
| 505 | } |
| 506 | |
| 507 | static int input_handlers_seq_show(struct seq_file *seq, void *v) |
| 508 | { |
| 509 | struct input_handler *handler = container_of(v, struct input_handler, node); |
| 510 | |
| 511 | seq_printf(seq, "N: Number=%ld Name=%s", |
| 512 | (unsigned long)seq->private, handler->name); |
| 513 | if (handler->fops) |
| 514 | seq_printf(seq, " Minor=%d", handler->minor); |
| 515 | seq_putc(seq, '\n'); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | static struct seq_operations input_handlers_seq_ops = { |
| 520 | .start = input_handlers_seq_start, |
| 521 | .next = input_handlers_seq_next, |
| 522 | .stop = input_handlers_seq_stop, |
| 523 | .show = input_handlers_seq_show, |
| 524 | }; |
| 525 | |
| 526 | static int input_proc_handlers_open(struct inode *inode, struct file *file) |
| 527 | { |
| 528 | return seq_open(file, &input_handlers_seq_ops); |
| 529 | } |
| 530 | |
| 531 | static struct file_operations input_handlers_fileops = { |
| 532 | .owner = THIS_MODULE, |
| 533 | .open = input_proc_handlers_open, |
| 534 | .read = seq_read, |
| 535 | .llseek = seq_lseek, |
| 536 | .release = seq_release, |
| 537 | }; |
Luke Kosewski | e334016fc | 2005-06-01 02:39:28 -0500 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | static int __init input_proc_init(void) |
| 540 | { |
| 541 | struct proc_dir_entry *entry; |
| 542 | |
| 543 | proc_bus_input_dir = proc_mkdir("input", proc_bus); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 544 | if (!proc_bus_input_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | return -ENOMEM; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | proc_bus_input_dir->owner = THIS_MODULE; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 548 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 549 | entry = create_proc_entry("devices", 0, proc_bus_input_dir); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 550 | if (!entry) |
| 551 | goto fail1; |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | entry->owner = THIS_MODULE; |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 554 | entry->proc_fops = &input_devices_fileops; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 555 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 556 | entry = create_proc_entry("handlers", 0, proc_bus_input_dir); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 557 | if (!entry) |
| 558 | goto fail2; |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | entry->owner = THIS_MODULE; |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 561 | entry->proc_fops = &input_handlers_fileops; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return 0; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 564 | |
| 565 | fail2: remove_proc_entry("devices", proc_bus_input_dir); |
| 566 | fail1: remove_proc_entry("input", proc_bus); |
| 567 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 569 | |
Andrew Morton | beffbdc | 2005-07-01 23:54:30 -0500 | [diff] [blame] | 570 | static void input_proc_exit(void) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 571 | { |
| 572 | remove_proc_entry("devices", proc_bus_input_dir); |
| 573 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 574 | remove_proc_entry("input", proc_bus); |
| 575 | } |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | #else /* !CONFIG_PROC_FS */ |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 578 | static inline void input_wakeup_procfs_readers(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | static inline int input_proc_init(void) { return 0; } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 580 | static inline void input_proc_exit(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | #endif |
| 582 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 583 | #define INPUT_DEV_STRING_ATTR_SHOW(name) \ |
| 584 | static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \ |
| 585 | { \ |
| 586 | struct input_dev *input_dev = to_input_dev(dev); \ |
| 587 | int retval; \ |
| 588 | \ |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 589 | retval = mutex_lock_interruptible(&input_dev->mutex); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 590 | if (retval) \ |
| 591 | return retval; \ |
| 592 | \ |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 593 | retval = scnprintf(buf, PAGE_SIZE, \ |
| 594 | "%s\n", input_dev->name ? input_dev->name : ""); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 595 | \ |
Jes Sorensen | e676c23 | 2006-02-19 00:21:46 -0500 | [diff] [blame] | 596 | mutex_unlock(&input_dev->mutex); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 597 | \ |
| 598 | return retval; \ |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 599 | } \ |
| 600 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL); |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 601 | |
| 602 | INPUT_DEV_STRING_ATTR_SHOW(name); |
| 603 | INPUT_DEV_STRING_ATTR_SHOW(phys); |
| 604 | INPUT_DEV_STRING_ATTR_SHOW(uniq); |
| 605 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 606 | static int input_print_modalias_bits(char *buf, int size, |
| 607 | char name, unsigned long *bm, |
| 608 | unsigned int min_bit, unsigned int max_bit) |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 609 | { |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 610 | int len = 0, i; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 611 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 612 | len += snprintf(buf, max(size, 0), "%c", name); |
| 613 | for (i = min_bit; i < max_bit; i++) |
| 614 | if (bm[LONG(i)] & BIT(i)) |
| 615 | len += snprintf(buf + len, max(size - len, 0), "%X,", i); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 616 | return len; |
| 617 | } |
| 618 | |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 619 | static int input_print_modalias(char *buf, int size, struct input_dev *id, |
| 620 | int add_cr) |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 621 | { |
| 622 | int len; |
| 623 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 624 | len = snprintf(buf, max(size, 0), |
| 625 | "input:b%04Xv%04Xp%04Xe%04X-", |
| 626 | id->id.bustype, id->id.vendor, |
| 627 | id->id.product, id->id.version); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 628 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 629 | len += input_print_modalias_bits(buf + len, size - len, |
| 630 | 'e', id->evbit, 0, EV_MAX); |
| 631 | len += input_print_modalias_bits(buf + len, size - len, |
| 632 | 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX); |
| 633 | len += input_print_modalias_bits(buf + len, size - len, |
| 634 | 'r', id->relbit, 0, REL_MAX); |
| 635 | len += input_print_modalias_bits(buf + len, size - len, |
| 636 | 'a', id->absbit, 0, ABS_MAX); |
| 637 | len += input_print_modalias_bits(buf + len, size - len, |
| 638 | 'm', id->mscbit, 0, MSC_MAX); |
| 639 | len += input_print_modalias_bits(buf + len, size - len, |
| 640 | 'l', id->ledbit, 0, LED_MAX); |
| 641 | len += input_print_modalias_bits(buf + len, size - len, |
| 642 | 's', id->sndbit, 0, SND_MAX); |
| 643 | len += input_print_modalias_bits(buf + len, size - len, |
| 644 | 'f', id->ffbit, 0, FF_MAX); |
| 645 | len += input_print_modalias_bits(buf + len, size - len, |
| 646 | 'w', id->swbit, 0, SW_MAX); |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 647 | |
| 648 | if (add_cr) |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 649 | len += snprintf(buf + len, max(size - len, 0), "\n"); |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 650 | |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 651 | return len; |
| 652 | } |
| 653 | |
| 654 | static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf) |
| 655 | { |
| 656 | struct input_dev *id = to_input_dev(dev); |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 657 | ssize_t len; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 658 | |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 659 | len = input_print_modalias(buf, PAGE_SIZE, id, 1); |
| 660 | |
Richard Purdie | 8a3cf45 | 2006-06-26 01:48:21 -0400 | [diff] [blame] | 661 | return min_t(int, len, PAGE_SIZE); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 662 | } |
| 663 | static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); |
| 664 | |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 665 | static struct attribute *input_dev_attrs[] = { |
| 666 | &class_device_attr_name.attr, |
| 667 | &class_device_attr_phys.attr, |
| 668 | &class_device_attr_uniq.attr, |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 669 | &class_device_attr_modalias.attr, |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 670 | NULL |
| 671 | }; |
| 672 | |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 673 | static struct attribute_group input_dev_attr_group = { |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 674 | .attrs = input_dev_attrs, |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 675 | }; |
| 676 | |
| 677 | #define INPUT_DEV_ID_ATTR(name) \ |
| 678 | static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \ |
| 679 | { \ |
| 680 | struct input_dev *input_dev = to_input_dev(dev); \ |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 681 | return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 682 | } \ |
| 683 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL); |
| 684 | |
| 685 | INPUT_DEV_ID_ATTR(bustype); |
| 686 | INPUT_DEV_ID_ATTR(vendor); |
| 687 | INPUT_DEV_ID_ATTR(product); |
| 688 | INPUT_DEV_ID_ATTR(version); |
| 689 | |
| 690 | static struct attribute *input_dev_id_attrs[] = { |
| 691 | &class_device_attr_bustype.attr, |
| 692 | &class_device_attr_vendor.attr, |
| 693 | &class_device_attr_product.attr, |
| 694 | &class_device_attr_version.attr, |
| 695 | NULL |
| 696 | }; |
| 697 | |
| 698 | static struct attribute_group input_dev_id_attr_group = { |
| 699 | .name = "id", |
| 700 | .attrs = input_dev_id_attrs, |
| 701 | }; |
| 702 | |
Dmitry Torokhov | 969b21c | 2006-04-02 00:09:34 -0500 | [diff] [blame] | 703 | static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, |
| 704 | int max, int add_cr) |
| 705 | { |
| 706 | int i; |
| 707 | int len = 0; |
| 708 | |
| 709 | for (i = NBITS(max) - 1; i > 0; i--) |
| 710 | if (bitmap[i]) |
| 711 | break; |
| 712 | |
| 713 | for (; i >= 0; i--) |
| 714 | len += snprintf(buf + len, max(buf_size - len, 0), |
| 715 | "%lx%s", bitmap[i], i > 0 ? " " : ""); |
| 716 | |
| 717 | if (add_cr) |
| 718 | len += snprintf(buf + len, max(buf_size - len, 0), "\n"); |
| 719 | |
| 720 | return len; |
| 721 | } |
| 722 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 723 | #define INPUT_DEV_CAP_ATTR(ev, bm) \ |
| 724 | static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \ |
| 725 | { \ |
| 726 | struct input_dev *input_dev = to_input_dev(dev); \ |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 727 | int len = input_print_bitmap(buf, PAGE_SIZE, \ |
| 728 | input_dev->bm##bit, ev##_MAX, 1); \ |
| 729 | return min_t(int, len, PAGE_SIZE); \ |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 730 | } \ |
| 731 | static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL); |
| 732 | |
| 733 | INPUT_DEV_CAP_ATTR(EV, ev); |
| 734 | INPUT_DEV_CAP_ATTR(KEY, key); |
| 735 | INPUT_DEV_CAP_ATTR(REL, rel); |
| 736 | INPUT_DEV_CAP_ATTR(ABS, abs); |
| 737 | INPUT_DEV_CAP_ATTR(MSC, msc); |
| 738 | INPUT_DEV_CAP_ATTR(LED, led); |
| 739 | INPUT_DEV_CAP_ATTR(SND, snd); |
| 740 | INPUT_DEV_CAP_ATTR(FF, ff); |
| 741 | INPUT_DEV_CAP_ATTR(SW, sw); |
| 742 | |
| 743 | static struct attribute *input_dev_caps_attrs[] = { |
| 744 | &class_device_attr_ev.attr, |
| 745 | &class_device_attr_key.attr, |
| 746 | &class_device_attr_rel.attr, |
| 747 | &class_device_attr_abs.attr, |
| 748 | &class_device_attr_msc.attr, |
| 749 | &class_device_attr_led.attr, |
| 750 | &class_device_attr_snd.attr, |
| 751 | &class_device_attr_ff.attr, |
| 752 | &class_device_attr_sw.attr, |
| 753 | NULL |
| 754 | }; |
| 755 | |
| 756 | static struct attribute_group input_dev_caps_attr_group = { |
| 757 | .name = "capabilities", |
| 758 | .attrs = input_dev_caps_attrs, |
| 759 | }; |
| 760 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 761 | static void input_dev_release(struct class_device *class_dev) |
| 762 | { |
| 763 | struct input_dev *dev = to_input_dev(class_dev); |
| 764 | |
| 765 | kfree(dev); |
| 766 | module_put(THIS_MODULE); |
| 767 | } |
| 768 | |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 769 | /* |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 770 | * Input uevent interface - loading event handlers based on |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 771 | * device bitfields. |
| 772 | */ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 773 | static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index, |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 774 | char *buffer, int buffer_size, int *cur_len, |
| 775 | const char *name, unsigned long *bitmap, int max) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 776 | { |
| 777 | if (*cur_index >= num_envp - 1) |
| 778 | return -ENOMEM; |
| 779 | |
| 780 | envp[*cur_index] = buffer + *cur_len; |
| 781 | |
| 782 | *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name); |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 783 | if (*cur_len >= buffer_size) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 784 | return -ENOMEM; |
| 785 | |
| 786 | *cur_len += input_print_bitmap(buffer + *cur_len, |
| 787 | max(buffer_size - *cur_len, 0), |
Dmitry Torokhov | 2db6687 | 2006-04-02 00:09:26 -0500 | [diff] [blame] | 788 | bitmap, max, 0) + 1; |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 789 | if (*cur_len > buffer_size) |
| 790 | return -ENOMEM; |
| 791 | |
| 792 | (*cur_index)++; |
| 793 | return 0; |
| 794 | } |
| 795 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 796 | static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index, |
| 797 | char *buffer, int buffer_size, int *cur_len, |
| 798 | struct input_dev *dev) |
| 799 | { |
| 800 | if (*cur_index >= num_envp - 1) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | envp[*cur_index] = buffer + *cur_len; |
| 804 | |
| 805 | *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), |
| 806 | "MODALIAS="); |
| 807 | if (*cur_len >= buffer_size) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | *cur_len += input_print_modalias(buffer + *cur_len, |
| 811 | max(buffer_size - *cur_len, 0), |
| 812 | dev, 0) + 1; |
| 813 | if (*cur_len > buffer_size) |
| 814 | return -ENOMEM; |
| 815 | |
| 816 | (*cur_index)++; |
| 817 | return 0; |
| 818 | } |
| 819 | |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 820 | #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \ |
| 821 | do { \ |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 822 | int err = add_uevent_var(envp, num_envp, &i, \ |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 823 | buffer, buffer_size, &len, \ |
| 824 | fmt, val); \ |
| 825 | if (err) \ |
| 826 | return err; \ |
| 827 | } while (0) |
| 828 | |
| 829 | #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \ |
| 830 | do { \ |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 831 | int err = input_add_uevent_bm_var(envp, num_envp, &i, \ |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 832 | buffer, buffer_size, &len, \ |
| 833 | name, bm, max); \ |
| 834 | if (err) \ |
| 835 | return err; \ |
| 836 | } while (0) |
| 837 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 838 | #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \ |
| 839 | do { \ |
| 840 | int err = input_add_uevent_modalias_var(envp, \ |
| 841 | num_envp, &i, \ |
| 842 | buffer, buffer_size, &len, \ |
| 843 | dev); \ |
| 844 | if (err) \ |
| 845 | return err; \ |
| 846 | } while (0) |
| 847 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 848 | static int input_dev_uevent(struct class_device *cdev, char **envp, |
| 849 | int num_envp, char *buffer, int buffer_size) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 850 | { |
| 851 | struct input_dev *dev = to_input_dev(cdev); |
| 852 | int i = 0; |
| 853 | int len = 0; |
| 854 | |
| 855 | INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x", |
| 856 | dev->id.bustype, dev->id.vendor, |
| 857 | dev->id.product, dev->id.version); |
| 858 | if (dev->name) |
| 859 | INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name); |
| 860 | if (dev->phys) |
| 861 | INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys); |
Dmitry Torokhov | 08de1f0 | 2005-11-08 21:34:29 -0800 | [diff] [blame] | 862 | if (dev->uniq) |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 863 | INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); |
| 864 | |
| 865 | INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX); |
| 866 | if (test_bit(EV_KEY, dev->evbit)) |
| 867 | INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); |
| 868 | if (test_bit(EV_REL, dev->evbit)) |
| 869 | INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX); |
| 870 | if (test_bit(EV_ABS, dev->evbit)) |
| 871 | INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX); |
| 872 | if (test_bit(EV_MSC, dev->evbit)) |
| 873 | INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX); |
| 874 | if (test_bit(EV_LED, dev->evbit)) |
| 875 | INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX); |
| 876 | if (test_bit(EV_SND, dev->evbit)) |
| 877 | INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX); |
| 878 | if (test_bit(EV_FF, dev->evbit)) |
| 879 | INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX); |
| 880 | if (test_bit(EV_SW, dev->evbit)) |
| 881 | INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX); |
| 882 | |
Dmitry Torokhov | ac648a6 | 2006-04-02 00:09:51 -0500 | [diff] [blame] | 883 | INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev); |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 884 | |
Kay Sievers | bd37e5a | 2006-01-05 13:19:55 +0100 | [diff] [blame] | 885 | envp[i] = NULL; |
Dmitry Torokhov | a7fadbe | 2005-09-15 02:01:57 -0500 | [diff] [blame] | 886 | return 0; |
| 887 | } |
| 888 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 889 | struct class input_class = { |
| 890 | .name = "input", |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 891 | .release = input_dev_release, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 892 | .uevent = input_dev_uevent, |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 893 | }; |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 894 | EXPORT_SYMBOL_GPL(input_class); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 895 | |
| 896 | struct input_dev *input_allocate_device(void) |
| 897 | { |
| 898 | struct input_dev *dev; |
| 899 | |
| 900 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); |
| 901 | if (dev) { |
| 902 | dev->dynalloc = 1; |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 903 | dev->cdev.class = &input_class; |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 904 | class_device_initialize(&dev->cdev); |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 905 | mutex_init(&dev->mutex); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 906 | INIT_LIST_HEAD(&dev->h_list); |
| 907 | INIT_LIST_HEAD(&dev->node); |
| 908 | } |
| 909 | |
| 910 | return dev; |
| 911 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 912 | EXPORT_SYMBOL(input_allocate_device); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 913 | |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 914 | void input_free_device(struct input_dev *dev) |
| 915 | { |
| 916 | if (dev) { |
| 917 | |
| 918 | mutex_lock(&dev->mutex); |
| 919 | dev->name = dev->phys = dev->uniq = NULL; |
| 920 | mutex_unlock(&dev->mutex); |
| 921 | |
| 922 | input_put_device(dev); |
| 923 | } |
| 924 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 925 | EXPORT_SYMBOL(input_free_device); |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 926 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 927 | int input_register_device(struct input_dev *dev) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 928 | { |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 929 | static atomic_t input_no = ATOMIC_INIT(0); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 930 | struct input_handle *handle; |
| 931 | struct input_handler *handler; |
| 932 | struct input_device_id *id; |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 933 | const char *path; |
| 934 | int error; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 935 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 936 | if (!dev->dynalloc) { |
| 937 | printk(KERN_WARNING "input: device %s is statically allocated, will not register\n" |
| 938 | "Please convert to input_allocate_device() or contact dtor_core@ameritech.net\n", |
| 939 | dev->name ? dev->name : "<Unknown>"); |
| 940 | return -EINVAL; |
| 941 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 942 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 943 | set_bit(EV_SYN, dev->evbit); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 944 | |
| 945 | /* |
| 946 | * If delay and period are pre-set by the driver, then autorepeating |
| 947 | * is handled by the driver itself and we don't do it in input.c. |
| 948 | */ |
| 949 | |
| 950 | init_timer(&dev->timer); |
| 951 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { |
| 952 | dev->timer.data = (long) dev; |
| 953 | dev->timer.function = input_repeat_key; |
| 954 | dev->rep[REP_DELAY] = 250; |
| 955 | dev->rep[REP_PERIOD] = 33; |
| 956 | } |
| 957 | |
| 958 | INIT_LIST_HEAD(&dev->h_list); |
| 959 | list_add_tail(&dev->node, &input_dev_list); |
| 960 | |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 961 | dev->cdev.class = &input_class; |
| 962 | snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id), |
| 963 | "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); |
| 964 | |
| 965 | error = class_device_add(&dev->cdev); |
| 966 | if (error) |
| 967 | return error; |
| 968 | |
| 969 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group); |
| 970 | if (error) |
| 971 | goto fail1; |
| 972 | |
| 973 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group); |
| 974 | if (error) |
| 975 | goto fail2; |
| 976 | |
| 977 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group); |
| 978 | if (error) |
| 979 | goto fail3; |
| 980 | |
| 981 | __module_get(THIS_MODULE); |
| 982 | |
| 983 | path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); |
| 984 | printk(KERN_INFO "input: %s as %s\n", |
| 985 | dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); |
| 986 | kfree(path); |
Greg Kroah-Hartman | 1020402 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 987 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 988 | list_for_each_entry(handler, &input_handler_list, node) |
| 989 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 990 | if ((id = input_match_device(handler->id_table, dev))) |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 991 | if ((handle = handler->connect(handler, dev, id))) { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 992 | input_link_handle(handle); |
Dmitry Torokhov | c7e8dc6 | 2006-07-06 00:21:03 -0400 | [diff] [blame] | 993 | if (handler->start) |
| 994 | handler->start(handle); |
| 995 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 996 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 997 | input_wakeup_procfs_readers(); |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 998 | |
| 999 | return 0; |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1000 | |
| 1001 | fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group); |
| 1002 | fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group); |
| 1003 | fail1: class_device_del(&dev->cdev); |
| 1004 | return error; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1005 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1006 | EXPORT_SYMBOL(input_register_device); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1007 | |
| 1008 | void input_unregister_device(struct input_dev *dev) |
| 1009 | { |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1010 | struct list_head *node, *next; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1011 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1012 | if (!dev) |
| 1013 | return; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1014 | |
| 1015 | del_timer_sync(&dev->timer); |
| 1016 | |
| 1017 | list_for_each_safe(node, next, &dev->h_list) { |
| 1018 | struct input_handle * handle = to_handle(node); |
| 1019 | list_del_init(&handle->d_node); |
| 1020 | list_del_init(&handle->h_node); |
| 1021 | handle->handler->disconnect(handle); |
| 1022 | } |
| 1023 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1024 | list_del_init(&dev->node); |
| 1025 | |
Dmitry Torokhov | 5f94548 | 2005-11-02 22:51:46 -0500 | [diff] [blame] | 1026 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group); |
| 1027 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group); |
Dmitry Torokhov | bd0ef23 | 2005-11-20 00:56:31 -0500 | [diff] [blame] | 1028 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1029 | |
Dmitry Torokhov | f60d2b1 | 2006-06-26 01:48:36 -0400 | [diff] [blame] | 1030 | mutex_lock(&dev->mutex); |
| 1031 | dev->name = dev->phys = dev->uniq = NULL; |
| 1032 | mutex_unlock(&dev->mutex); |
| 1033 | |
Dmitry Torokhov | e7374e4 | 2006-06-27 08:30:31 -0400 | [diff] [blame] | 1034 | class_device_unregister(&dev->cdev); |
| 1035 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1036 | input_wakeup_procfs_readers(); |
| 1037 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1038 | EXPORT_SYMBOL(input_unregister_device); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1039 | |
| 1040 | void input_register_handler(struct input_handler *handler) |
| 1041 | { |
| 1042 | struct input_dev *dev; |
| 1043 | struct input_handle *handle; |
| 1044 | struct input_device_id *id; |
| 1045 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1046 | if (!handler) |
| 1047 | return; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1048 | |
| 1049 | INIT_LIST_HEAD(&handler->h_list); |
| 1050 | |
| 1051 | if (handler->fops != NULL) |
| 1052 | input_table[handler->minor >> 5] = handler; |
| 1053 | |
| 1054 | list_add_tail(&handler->node, &input_handler_list); |
| 1055 | |
| 1056 | list_for_each_entry(dev, &input_dev_list, node) |
| 1057 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 1058 | if ((id = input_match_device(handler->id_table, dev))) |
Dmitry Torokhov | b6d786d | 2006-07-19 01:08:51 -0400 | [diff] [blame^] | 1059 | if ((handle = handler->connect(handler, dev, id))) { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1060 | input_link_handle(handle); |
Dmitry Torokhov | b6d786d | 2006-07-19 01:08:51 -0400 | [diff] [blame^] | 1061 | if (handler->start) |
| 1062 | handler->start(handle); |
| 1063 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1064 | |
| 1065 | input_wakeup_procfs_readers(); |
| 1066 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1067 | EXPORT_SYMBOL(input_register_handler); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1068 | |
| 1069 | void input_unregister_handler(struct input_handler *handler) |
| 1070 | { |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 1071 | struct list_head *node, *next; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1072 | |
| 1073 | list_for_each_safe(node, next, &handler->h_list) { |
| 1074 | struct input_handle * handle = to_handle_h(node); |
| 1075 | list_del_init(&handle->h_node); |
| 1076 | list_del_init(&handle->d_node); |
| 1077 | handler->disconnect(handle); |
| 1078 | } |
| 1079 | |
| 1080 | list_del_init(&handler->node); |
| 1081 | |
| 1082 | if (handler->fops != NULL) |
| 1083 | input_table[handler->minor >> 5] = NULL; |
| 1084 | |
| 1085 | input_wakeup_procfs_readers(); |
| 1086 | } |
Dmitry Torokhov | ca56fe0 | 2006-06-26 01:49:21 -0400 | [diff] [blame] | 1087 | EXPORT_SYMBOL(input_unregister_handler); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1088 | |
| 1089 | static int input_open_file(struct inode *inode, struct file *file) |
| 1090 | { |
| 1091 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |
Arjan van de Ven | 99ac48f | 2006-03-28 01:56:41 -0800 | [diff] [blame] | 1092 | const struct file_operations *old_fops, *new_fops = NULL; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1093 | int err; |
| 1094 | |
| 1095 | /* No load-on-demand here? */ |
| 1096 | if (!handler || !(new_fops = fops_get(handler->fops))) |
| 1097 | return -ENODEV; |
| 1098 | |
| 1099 | /* |
| 1100 | * That's _really_ odd. Usually NULL ->open means "nothing special", |
| 1101 | * not "no device". Oh, well... |
| 1102 | */ |
| 1103 | if (!new_fops->open) { |
| 1104 | fops_put(new_fops); |
| 1105 | return -ENODEV; |
| 1106 | } |
| 1107 | old_fops = file->f_op; |
| 1108 | file->f_op = new_fops; |
| 1109 | |
| 1110 | err = new_fops->open(inode, file); |
| 1111 | |
| 1112 | if (err) { |
| 1113 | fops_put(file->f_op); |
| 1114 | file->f_op = fops_get(old_fops); |
| 1115 | } |
| 1116 | fops_put(old_fops); |
| 1117 | return err; |
| 1118 | } |
| 1119 | |
| 1120 | static struct file_operations input_fops = { |
| 1121 | .owner = THIS_MODULE, |
| 1122 | .open = input_open_file, |
| 1123 | }; |
| 1124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | static int __init input_init(void) |
| 1126 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1127 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1129 | err = class_register(&input_class); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 1130 | if (err) { |
| 1131 | printk(KERN_ERR "input: unable to register input_dev class\n"); |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1135 | err = input_proc_init(); |
| 1136 | if (err) |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1137 | goto fail1; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1138 | |
| 1139 | err = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
| 1140 | if (err) { |
| 1141 | printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR); |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1142 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1144 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1145 | return 0; |
| 1146 | |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1147 | fail2: input_proc_exit(); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1148 | fail1: class_unregister(&input_class); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1149 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static void __exit input_exit(void) |
| 1153 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 1154 | input_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | unregister_chrdev(INPUT_MAJOR, "input"); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 1156 | class_unregister(&input_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | subsys_initcall(input_init); |
| 1160 | module_exit(input_exit); |