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> |
| 21 | #include <linux/kobject_uevent.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 27 | MODULE_DESCRIPTION("Input core"); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 30 | EXPORT_SYMBOL(input_allocate_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | EXPORT_SYMBOL(input_register_device); |
| 32 | EXPORT_SYMBOL(input_unregister_device); |
| 33 | EXPORT_SYMBOL(input_register_handler); |
| 34 | EXPORT_SYMBOL(input_unregister_handler); |
| 35 | EXPORT_SYMBOL(input_grab_device); |
| 36 | EXPORT_SYMBOL(input_release_device); |
| 37 | EXPORT_SYMBOL(input_open_device); |
| 38 | EXPORT_SYMBOL(input_close_device); |
| 39 | EXPORT_SYMBOL(input_accept_process); |
| 40 | EXPORT_SYMBOL(input_flush_device); |
| 41 | EXPORT_SYMBOL(input_event); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 42 | EXPORT_SYMBOL_GPL(input_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #define INPUT_DEVICES 256 |
| 45 | |
| 46 | static LIST_HEAD(input_dev_list); |
| 47 | static LIST_HEAD(input_handler_list); |
| 48 | |
| 49 | static struct input_handler *input_table[8]; |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 52 | { |
| 53 | struct input_handle *handle; |
| 54 | |
| 55 | if (type > EV_MAX || !test_bit(type, dev->evbit)) |
| 56 | return; |
| 57 | |
| 58 | add_input_randomness(type, code, value); |
| 59 | |
| 60 | switch (type) { |
| 61 | |
| 62 | case EV_SYN: |
| 63 | switch (code) { |
| 64 | case SYN_CONFIG: |
| 65 | if (dev->event) dev->event(dev, type, code, value); |
| 66 | break; |
| 67 | |
| 68 | case SYN_REPORT: |
| 69 | if (dev->sync) return; |
| 70 | dev->sync = 1; |
| 71 | break; |
| 72 | } |
| 73 | break; |
| 74 | |
| 75 | case EV_KEY: |
| 76 | |
| 77 | if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) |
| 78 | return; |
| 79 | |
| 80 | if (value == 2) |
| 81 | break; |
| 82 | |
| 83 | change_bit(code, dev->key); |
| 84 | |
| 85 | if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { |
| 86 | dev->repeat_key = code; |
| 87 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); |
| 88 | } |
| 89 | |
| 90 | break; |
| 91 | |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 92 | case EV_SW: |
| 93 | |
| 94 | if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) |
| 95 | return; |
| 96 | |
| 97 | change_bit(code, dev->sw); |
| 98 | |
| 99 | break; |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | case EV_ABS: |
| 102 | |
| 103 | if (code > ABS_MAX || !test_bit(code, dev->absbit)) |
| 104 | return; |
| 105 | |
| 106 | if (dev->absfuzz[code]) { |
| 107 | if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && |
| 108 | (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) |
| 109 | return; |
| 110 | |
| 111 | if ((value > dev->abs[code] - dev->absfuzz[code]) && |
| 112 | (value < dev->abs[code] + dev->absfuzz[code])) |
| 113 | value = (dev->abs[code] * 3 + value) >> 2; |
| 114 | |
| 115 | if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && |
| 116 | (value < dev->abs[code] + (dev->absfuzz[code] << 1))) |
| 117 | value = (dev->abs[code] + value) >> 1; |
| 118 | } |
| 119 | |
| 120 | if (dev->abs[code] == value) |
| 121 | return; |
| 122 | |
| 123 | dev->abs[code] = value; |
| 124 | break; |
| 125 | |
| 126 | case EV_REL: |
| 127 | |
| 128 | if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) |
| 129 | return; |
| 130 | |
| 131 | break; |
| 132 | |
| 133 | case EV_MSC: |
| 134 | |
| 135 | if (code > MSC_MAX || !test_bit(code, dev->mscbit)) |
| 136 | return; |
| 137 | |
| 138 | if (dev->event) dev->event(dev, type, code, value); |
| 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); |
| 148 | if (dev->event) dev->event(dev, type, code, value); |
| 149 | |
| 150 | break; |
| 151 | |
| 152 | case EV_SND: |
| 153 | |
| 154 | if (code > SND_MAX || !test_bit(code, dev->sndbit)) |
| 155 | return; |
| 156 | |
| 157 | if (dev->event) dev->event(dev, type, code, value); |
| 158 | |
| 159 | break; |
| 160 | |
| 161 | case EV_REP: |
| 162 | |
| 163 | if (code > REP_MAX || value < 0 || dev->rep[code] == value) return; |
| 164 | |
| 165 | dev->rep[code] = value; |
| 166 | if (dev->event) dev->event(dev, type, code, value); |
| 167 | |
| 168 | break; |
| 169 | |
| 170 | case EV_FF: |
| 171 | if (dev->event) dev->event(dev, type, code, value); |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | if (type != EV_SYN) |
| 176 | dev->sync = 0; |
| 177 | |
| 178 | if (dev->grab) |
| 179 | dev->grab->handler->event(dev->grab, type, code, value); |
| 180 | else |
| 181 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 182 | if (handle->open) |
| 183 | handle->handler->event(handle, type, code, value); |
| 184 | } |
| 185 | |
| 186 | static void input_repeat_key(unsigned long data) |
| 187 | { |
| 188 | struct input_dev *dev = (void *) data; |
| 189 | |
| 190 | if (!test_bit(dev->repeat_key, dev->key)) |
| 191 | return; |
| 192 | |
| 193 | input_event(dev, EV_KEY, dev->repeat_key, 2); |
| 194 | input_sync(dev); |
| 195 | |
| 196 | if (dev->rep[REP_PERIOD]) |
| 197 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); |
| 198 | } |
| 199 | |
| 200 | int input_accept_process(struct input_handle *handle, struct file *file) |
| 201 | { |
| 202 | if (handle->dev->accept) |
| 203 | return handle->dev->accept(handle->dev, file); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | int input_grab_device(struct input_handle *handle) |
| 209 | { |
| 210 | if (handle->dev->grab) |
| 211 | return -EBUSY; |
| 212 | |
| 213 | handle->dev->grab = handle; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | void input_release_device(struct input_handle *handle) |
| 218 | { |
| 219 | if (handle->dev->grab == handle) |
| 220 | handle->dev->grab = NULL; |
| 221 | } |
| 222 | |
| 223 | int input_open_device(struct input_handle *handle) |
| 224 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 225 | struct input_dev *dev = handle->dev; |
| 226 | int err; |
| 227 | |
| 228 | err = down_interruptible(&dev->sem); |
| 229 | if (err) |
| 230 | return err; |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | handle->open++; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 233 | |
| 234 | if (!dev->users++ && dev->open) |
| 235 | err = dev->open(dev); |
| 236 | |
| 237 | if (err) |
| 238 | handle->open--; |
| 239 | |
| 240 | up(&dev->sem); |
| 241 | |
| 242 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | int input_flush_device(struct input_handle* handle, struct file* file) |
| 246 | { |
| 247 | if (handle->dev->flush) |
| 248 | return handle->dev->flush(handle->dev, file); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | void input_close_device(struct input_handle *handle) |
| 254 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 255 | struct input_dev *dev = handle->dev; |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | input_release_device(handle); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 258 | |
| 259 | down(&dev->sem); |
| 260 | |
| 261 | if (!--dev->users && dev->close) |
| 262 | dev->close(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | handle->open--; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 264 | |
| 265 | up(&dev->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void input_link_handle(struct input_handle *handle) |
| 269 | { |
| 270 | list_add_tail(&handle->d_node, &handle->dev->h_list); |
| 271 | list_add_tail(&handle->h_node, &handle->handler->h_list); |
| 272 | } |
| 273 | |
| 274 | #define MATCH_BIT(bit, max) \ |
| 275 | for (i = 0; i < NBITS(max); i++) \ |
| 276 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
| 277 | break; \ |
| 278 | if (i != NBITS(max)) \ |
| 279 | continue; |
| 280 | |
| 281 | static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev) |
| 282 | { |
| 283 | int i; |
| 284 | |
| 285 | for (; id->flags || id->driver_info; id++) { |
| 286 | |
| 287 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) |
| 288 | if (id->id.bustype != dev->id.bustype) |
| 289 | continue; |
| 290 | |
| 291 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) |
| 292 | if (id->id.vendor != dev->id.vendor) |
| 293 | continue; |
| 294 | |
| 295 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) |
| 296 | if (id->id.product != dev->id.product) |
| 297 | continue; |
| 298 | |
| 299 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) |
| 300 | if (id->id.version != dev->id.version) |
| 301 | continue; |
| 302 | |
| 303 | MATCH_BIT(evbit, EV_MAX); |
| 304 | MATCH_BIT(keybit, KEY_MAX); |
| 305 | MATCH_BIT(relbit, REL_MAX); |
| 306 | MATCH_BIT(absbit, ABS_MAX); |
| 307 | MATCH_BIT(mscbit, MSC_MAX); |
| 308 | MATCH_BIT(ledbit, LED_MAX); |
| 309 | MATCH_BIT(sndbit, SND_MAX); |
| 310 | MATCH_BIT(ffbit, FF_MAX); |
Dmitry Torokhov | ff13f98 | 2005-09-24 02:02:29 -0500 | [diff] [blame] | 311 | MATCH_BIT(swbit, SW_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | return id; |
| 314 | } |
| 315 | |
| 316 | return NULL; |
| 317 | } |
| 318 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Input hotplugging interface - loading event handlers based on |
| 322 | * device bitfields. |
| 323 | */ |
| 324 | |
| 325 | #ifdef CONFIG_HOTPLUG |
| 326 | |
| 327 | /* |
| 328 | * Input hotplugging invokes what /proc/sys/kernel/hotplug says |
| 329 | * (normally /sbin/hotplug) when input devices get added or removed. |
| 330 | * |
| 331 | * This invokes a user mode policy agent, typically helping to load driver |
| 332 | * or other modules, configure the device, and more. Drivers can provide |
| 333 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
| 334 | * |
| 335 | */ |
| 336 | |
| 337 | #define SPRINTF_BIT_A(bit, name, max) \ |
| 338 | do { \ |
| 339 | envp[i++] = scratch; \ |
| 340 | scratch += sprintf(scratch, name); \ |
| 341 | for (j = NBITS(max) - 1; j >= 0; j--) \ |
| 342 | if (dev->bit[j]) break; \ |
| 343 | for (; j >= 0; j--) \ |
| 344 | scratch += sprintf(scratch, "%lx ", dev->bit[j]); \ |
| 345 | scratch++; \ |
| 346 | } while (0) |
| 347 | |
| 348 | #define SPRINTF_BIT_A2(bit, name, max, ev) \ |
| 349 | do { \ |
| 350 | if (test_bit(ev, dev->evbit)) \ |
| 351 | SPRINTF_BIT_A(bit, name, max); \ |
| 352 | } while (0) |
| 353 | |
| 354 | static void input_call_hotplug(char *verb, struct input_dev *dev) |
| 355 | { |
| 356 | char *argv[3], **envp, *buf, *scratch; |
| 357 | int i = 0, j, value; |
| 358 | |
| 359 | if (!hotplug_path[0]) { |
| 360 | printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n"); |
| 361 | return; |
| 362 | } |
| 363 | if (in_interrupt()) { |
| 364 | printk(KERN_ERR "input.c: calling hotplug from interrupt\n"); |
| 365 | return; |
| 366 | } |
| 367 | if (!current->fs->root) { |
| 368 | printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n"); |
| 369 | return; |
| 370 | } |
| 371 | if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) { |
| 372 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
| 373 | return; |
| 374 | } |
| 375 | if (!(buf = kmalloc(1024, GFP_KERNEL))) { |
| 376 | kfree (envp); |
| 377 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | argv[0] = hotplug_path; |
| 382 | argv[1] = "input"; |
| 383 | argv[2] = NULL; |
| 384 | |
| 385 | envp[i++] = "HOME=/"; |
| 386 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 387 | |
| 388 | scratch = buf; |
| 389 | |
| 390 | envp[i++] = scratch; |
| 391 | scratch += sprintf(scratch, "ACTION=%s", verb) + 1; |
| 392 | |
| 393 | envp[i++] = scratch; |
| 394 | scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x", |
| 395 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1; |
| 396 | |
| 397 | if (dev->name) { |
| 398 | envp[i++] = scratch; |
| 399 | scratch += sprintf(scratch, "NAME=%s", dev->name) + 1; |
| 400 | } |
| 401 | |
| 402 | if (dev->phys) { |
| 403 | envp[i++] = scratch; |
| 404 | scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1; |
| 405 | } |
| 406 | |
| 407 | SPRINTF_BIT_A(evbit, "EV=", EV_MAX); |
| 408 | SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY); |
| 409 | SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL); |
| 410 | SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS); |
| 411 | SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC); |
| 412 | SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED); |
| 413 | SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND); |
| 414 | SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF); |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 415 | SPRINTF_BIT_A2(swbit, "SW=", SW_MAX, EV_SW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | envp[i++] = NULL; |
| 418 | |
| 419 | #ifdef INPUT_DEBUG |
| 420 | printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n", |
| 421 | argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]); |
| 422 | #endif |
| 423 | |
| 424 | value = call_usermodehelper(argv [0], argv, envp, 0); |
| 425 | |
| 426 | kfree(buf); |
| 427 | kfree(envp); |
| 428 | |
| 429 | #ifdef INPUT_DEBUG |
| 430 | if (value != 0) |
| 431 | printk(KERN_DEBUG "input.c: hotplug returned %d\n", value); |
| 432 | #endif |
| 433 | } |
| 434 | |
| 435 | #endif |
| 436 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 437 | static int input_print_bitmap(char *buf, unsigned long *bitmap, int max) |
| 438 | { |
| 439 | int i; |
| 440 | int len = 0; |
| 441 | |
| 442 | for (i = NBITS(max) - 1; i > 0; i--) |
| 443 | if (bitmap[i]) |
| 444 | break; |
| 445 | |
| 446 | for (; i >= 0; i--) |
| 447 | len += sprintf(buf + len, "%lx%s", bitmap[i], i > 0 ? " " : ""); |
| 448 | |
| 449 | len += sprintf(buf + len, "\n"); |
| 450 | |
| 451 | return len; |
| 452 | } |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | #ifdef CONFIG_PROC_FS |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 455 | |
| 456 | static struct proc_dir_entry *proc_bus_input_dir; |
| 457 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); |
| 458 | static int input_devices_state; |
| 459 | |
| 460 | static inline void input_wakeup_procfs_readers(void) |
| 461 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | input_devices_state++; |
| 463 | wake_up(&input_devices_poll_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 466 | static unsigned int input_devices_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 468 | int state = input_devices_state; |
| 469 | poll_wait(file, &input_devices_poll_wait, wait); |
| 470 | if (state != input_devices_state) |
| 471 | return POLLIN | POLLRDNORM; |
| 472 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 475 | #define SPRINTF_BIT_B(ev, bm) \ |
| 476 | do { \ |
| 477 | len += sprintf(buf + len, "B: %s=", #ev); \ |
| 478 | len += input_print_bitmap(buf + len, \ |
| 479 | dev->bm##bit, ev##_MAX); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } while (0) |
| 481 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 482 | #define SPRINTF_BIT_B2(ev, bm) \ |
| 483 | do { \ |
| 484 | if (test_bit(EV_##ev, dev->evbit)) \ |
| 485 | SPRINTF_BIT_B(ev, bm); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } while (0) |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
| 489 | { |
| 490 | struct input_dev *dev; |
| 491 | struct input_handle *handle; |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 492 | const char *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | off_t at = 0; |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 495 | int len, cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | list_for_each_entry(dev, &input_dev_list, node) { |
| 498 | |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 499 | path = dev->dynalloc ? kobject_get_path(&dev->cdev.kobj, GFP_KERNEL) : NULL; |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", |
| 502 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); |
| 503 | |
| 504 | len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
| 505 | len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 506 | len += sprintf(buf + len, "S: Sysfs=%s\n", path ? path : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | len += sprintf(buf + len, "H: Handlers="); |
| 508 | |
| 509 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 510 | len += sprintf(buf + len, "%s ", handle->name); |
| 511 | |
| 512 | len += sprintf(buf + len, "\n"); |
| 513 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 514 | SPRINTF_BIT_B(EV, ev); |
| 515 | SPRINTF_BIT_B2(KEY, key); |
| 516 | SPRINTF_BIT_B2(REL, rel); |
| 517 | SPRINTF_BIT_B2(ABS, abs); |
| 518 | SPRINTF_BIT_B2(MSC, msc); |
| 519 | SPRINTF_BIT_B2(LED, led); |
| 520 | SPRINTF_BIT_B2(SND, snd); |
| 521 | SPRINTF_BIT_B2(FF, ff); |
| 522 | SPRINTF_BIT_B2(SW, sw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | len += sprintf(buf + len, "\n"); |
| 525 | |
| 526 | at += len; |
| 527 | |
| 528 | if (at >= pos) { |
| 529 | if (!*start) { |
| 530 | *start = buf + (pos - (at - len)); |
| 531 | cnt = at - pos; |
| 532 | } else cnt += len; |
| 533 | buf += len; |
| 534 | if (cnt >= count) |
| 535 | break; |
| 536 | } |
Dmitry Torokhov | 051b2fe | 2005-09-15 02:01:54 -0500 | [diff] [blame] | 537 | |
| 538 | kfree(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | if (&dev->node == &input_dev_list) |
| 542 | *eof = 1; |
| 543 | |
| 544 | return (count > cnt) ? cnt : count; |
| 545 | } |
| 546 | |
| 547 | static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
| 548 | { |
| 549 | struct input_handler *handler; |
| 550 | |
| 551 | off_t at = 0; |
| 552 | int len = 0, cnt = 0; |
| 553 | int i = 0; |
| 554 | |
| 555 | list_for_each_entry(handler, &input_handler_list, node) { |
| 556 | |
| 557 | if (handler->fops) |
| 558 | len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n", |
| 559 | i++, handler->name, handler->minor); |
| 560 | else |
| 561 | len = sprintf(buf, "N: Number=%d Name=%s\n", |
| 562 | i++, handler->name); |
| 563 | |
| 564 | at += len; |
| 565 | |
| 566 | if (at >= pos) { |
| 567 | if (!*start) { |
| 568 | *start = buf + (pos - (at - len)); |
| 569 | cnt = at - pos; |
| 570 | } else cnt += len; |
| 571 | buf += len; |
| 572 | if (cnt >= count) |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | if (&handler->node == &input_handler_list) |
| 577 | *eof = 1; |
| 578 | |
| 579 | return (count > cnt) ? cnt : count; |
| 580 | } |
| 581 | |
Luke Kosewski | e334016fc | 2005-06-01 02:39:28 -0500 | [diff] [blame] | 582 | static struct file_operations input_fileops; |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | static int __init input_proc_init(void) |
| 585 | { |
| 586 | struct proc_dir_entry *entry; |
| 587 | |
| 588 | proc_bus_input_dir = proc_mkdir("input", proc_bus); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 589 | if (!proc_bus_input_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return -ENOMEM; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | proc_bus_input_dir->owner = THIS_MODULE; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 595 | if (!entry) |
| 596 | goto fail1; |
| 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | entry->owner = THIS_MODULE; |
Luke Kosewski | e334016fc | 2005-06-01 02:39:28 -0500 | [diff] [blame] | 599 | input_fileops = *entry->proc_fops; |
| 600 | entry->proc_fops = &input_fileops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | entry->proc_fops->poll = input_devices_poll; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 604 | if (!entry) |
| 605 | goto fail2; |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | entry->owner = THIS_MODULE; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return 0; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 610 | |
| 611 | fail2: remove_proc_entry("devices", proc_bus_input_dir); |
| 612 | fail1: remove_proc_entry("input", proc_bus); |
| 613 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 615 | |
Andrew Morton | beffbdc | 2005-07-01 23:54:30 -0500 | [diff] [blame] | 616 | static void input_proc_exit(void) |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 617 | { |
| 618 | remove_proc_entry("devices", proc_bus_input_dir); |
| 619 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 620 | remove_proc_entry("input", proc_bus); |
| 621 | } |
| 622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | #else /* !CONFIG_PROC_FS */ |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 624 | static inline void input_wakeup_procfs_readers(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | static inline int input_proc_init(void) { return 0; } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 626 | static inline void input_proc_exit(void) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | #endif |
| 628 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 629 | #define INPUT_DEV_STRING_ATTR_SHOW(name) \ |
| 630 | static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \ |
| 631 | { \ |
| 632 | struct input_dev *input_dev = to_input_dev(dev); \ |
| 633 | int retval; \ |
| 634 | \ |
| 635 | retval = down_interruptible(&input_dev->sem); \ |
| 636 | if (retval) \ |
| 637 | return retval; \ |
| 638 | \ |
| 639 | retval = sprintf(buf, "%s\n", input_dev->name ? input_dev->name : ""); \ |
| 640 | \ |
| 641 | up(&input_dev->sem); \ |
| 642 | \ |
| 643 | return retval; \ |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 644 | } \ |
| 645 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL); |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 646 | |
| 647 | INPUT_DEV_STRING_ATTR_SHOW(name); |
| 648 | INPUT_DEV_STRING_ATTR_SHOW(phys); |
| 649 | INPUT_DEV_STRING_ATTR_SHOW(uniq); |
| 650 | |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 651 | static struct attribute *input_dev_attrs[] = { |
| 652 | &class_device_attr_name.attr, |
| 653 | &class_device_attr_phys.attr, |
| 654 | &class_device_attr_uniq.attr, |
| 655 | NULL |
| 656 | }; |
| 657 | |
| 658 | static struct attribute_group input_dev_group = { |
| 659 | .attrs = input_dev_attrs, |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | #define INPUT_DEV_ID_ATTR(name) \ |
| 663 | static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \ |
| 664 | { \ |
| 665 | struct input_dev *input_dev = to_input_dev(dev); \ |
| 666 | return sprintf(buf, "%04x\n", input_dev->id.name); \ |
| 667 | } \ |
| 668 | static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL); |
| 669 | |
| 670 | INPUT_DEV_ID_ATTR(bustype); |
| 671 | INPUT_DEV_ID_ATTR(vendor); |
| 672 | INPUT_DEV_ID_ATTR(product); |
| 673 | INPUT_DEV_ID_ATTR(version); |
| 674 | |
| 675 | static struct attribute *input_dev_id_attrs[] = { |
| 676 | &class_device_attr_bustype.attr, |
| 677 | &class_device_attr_vendor.attr, |
| 678 | &class_device_attr_product.attr, |
| 679 | &class_device_attr_version.attr, |
| 680 | NULL |
| 681 | }; |
| 682 | |
| 683 | static struct attribute_group input_dev_id_attr_group = { |
| 684 | .name = "id", |
| 685 | .attrs = input_dev_id_attrs, |
| 686 | }; |
| 687 | |
| 688 | #define INPUT_DEV_CAP_ATTR(ev, bm) \ |
| 689 | static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \ |
| 690 | { \ |
| 691 | struct input_dev *input_dev = to_input_dev(dev); \ |
| 692 | return input_print_bitmap(buf, input_dev->bm##bit, ev##_MAX); \ |
| 693 | } \ |
| 694 | static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL); |
| 695 | |
| 696 | INPUT_DEV_CAP_ATTR(EV, ev); |
| 697 | INPUT_DEV_CAP_ATTR(KEY, key); |
| 698 | INPUT_DEV_CAP_ATTR(REL, rel); |
| 699 | INPUT_DEV_CAP_ATTR(ABS, abs); |
| 700 | INPUT_DEV_CAP_ATTR(MSC, msc); |
| 701 | INPUT_DEV_CAP_ATTR(LED, led); |
| 702 | INPUT_DEV_CAP_ATTR(SND, snd); |
| 703 | INPUT_DEV_CAP_ATTR(FF, ff); |
| 704 | INPUT_DEV_CAP_ATTR(SW, sw); |
| 705 | |
| 706 | static struct attribute *input_dev_caps_attrs[] = { |
| 707 | &class_device_attr_ev.attr, |
| 708 | &class_device_attr_key.attr, |
| 709 | &class_device_attr_rel.attr, |
| 710 | &class_device_attr_abs.attr, |
| 711 | &class_device_attr_msc.attr, |
| 712 | &class_device_attr_led.attr, |
| 713 | &class_device_attr_snd.attr, |
| 714 | &class_device_attr_ff.attr, |
| 715 | &class_device_attr_sw.attr, |
| 716 | NULL |
| 717 | }; |
| 718 | |
| 719 | static struct attribute_group input_dev_caps_attr_group = { |
| 720 | .name = "capabilities", |
| 721 | .attrs = input_dev_caps_attrs, |
| 722 | }; |
| 723 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 724 | static void input_dev_release(struct class_device *class_dev) |
| 725 | { |
| 726 | struct input_dev *dev = to_input_dev(class_dev); |
| 727 | |
| 728 | kfree(dev); |
| 729 | module_put(THIS_MODULE); |
| 730 | } |
| 731 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 732 | struct class input_class = { |
| 733 | .name = "input", |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 734 | .release = input_dev_release, |
| 735 | }; |
| 736 | |
| 737 | struct input_dev *input_allocate_device(void) |
| 738 | { |
| 739 | struct input_dev *dev; |
| 740 | |
| 741 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); |
| 742 | if (dev) { |
| 743 | dev->dynalloc = 1; |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 744 | dev->cdev.class = &input_class; |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 745 | class_device_initialize(&dev->cdev); |
| 746 | INIT_LIST_HEAD(&dev->h_list); |
| 747 | INIT_LIST_HEAD(&dev->node); |
| 748 | } |
| 749 | |
| 750 | return dev; |
| 751 | } |
| 752 | |
| 753 | static void input_register_classdevice(struct input_dev *dev) |
| 754 | { |
| 755 | static atomic_t input_no = ATOMIC_INIT(0); |
| 756 | const char *path; |
| 757 | |
| 758 | __module_get(THIS_MODULE); |
| 759 | |
| 760 | dev->dev = dev->cdev.dev; |
| 761 | |
| 762 | snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id), |
| 763 | "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); |
| 764 | |
| 765 | path = kobject_get_path(&dev->cdev.class->subsys.kset.kobj, GFP_KERNEL); |
| 766 | printk(KERN_INFO "input: %s/%s as %s\n", |
| 767 | dev->name ? dev->name : "Unspecified device", |
| 768 | path ? path : "", dev->cdev.class_id); |
| 769 | kfree(path); |
| 770 | |
| 771 | class_device_add(&dev->cdev); |
Greg Kroah-Hartman | 629b77a | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 772 | sysfs_create_group(&dev->cdev.kobj, &input_dev_group); |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 773 | sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group); |
| 774 | sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 775 | } |
| 776 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 777 | void input_register_device(struct input_dev *dev) |
| 778 | { |
| 779 | struct input_handle *handle; |
| 780 | struct input_handler *handler; |
| 781 | struct input_device_id *id; |
| 782 | |
| 783 | set_bit(EV_SYN, dev->evbit); |
| 784 | |
| 785 | init_MUTEX(&dev->sem); |
| 786 | |
| 787 | /* |
| 788 | * If delay and period are pre-set by the driver, then autorepeating |
| 789 | * is handled by the driver itself and we don't do it in input.c. |
| 790 | */ |
| 791 | |
| 792 | init_timer(&dev->timer); |
| 793 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { |
| 794 | dev->timer.data = (long) dev; |
| 795 | dev->timer.function = input_repeat_key; |
| 796 | dev->rep[REP_DELAY] = 250; |
| 797 | dev->rep[REP_PERIOD] = 33; |
| 798 | } |
| 799 | |
| 800 | INIT_LIST_HEAD(&dev->h_list); |
| 801 | list_add_tail(&dev->node, &input_dev_list); |
| 802 | |
Greg Kroah-Hartman | 1020402 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 803 | if (dev->dynalloc) |
| 804 | input_register_classdevice(dev); |
| 805 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 806 | list_for_each_entry(handler, &input_handler_list, node) |
| 807 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 808 | if ((id = input_match_device(handler->id_table, dev))) |
| 809 | if ((handle = handler->connect(handler, dev, id))) |
| 810 | input_link_handle(handle); |
| 811 | |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 812 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 813 | #ifdef CONFIG_HOTPLUG |
| 814 | input_call_hotplug("add", dev); |
| 815 | #endif |
| 816 | |
| 817 | input_wakeup_procfs_readers(); |
| 818 | } |
| 819 | |
| 820 | void input_unregister_device(struct input_dev *dev) |
| 821 | { |
| 822 | struct list_head * node, * next; |
| 823 | |
| 824 | if (!dev) return; |
| 825 | |
| 826 | del_timer_sync(&dev->timer); |
| 827 | |
| 828 | list_for_each_safe(node, next, &dev->h_list) { |
| 829 | struct input_handle * handle = to_handle(node); |
| 830 | list_del_init(&handle->d_node); |
| 831 | list_del_init(&handle->h_node); |
| 832 | handle->handler->disconnect(handle); |
| 833 | } |
| 834 | |
| 835 | #ifdef CONFIG_HOTPLUG |
| 836 | input_call_hotplug("remove", dev); |
| 837 | #endif |
| 838 | |
| 839 | list_del_init(&dev->node); |
| 840 | |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 841 | if (dev->dynalloc) { |
| 842 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group); |
| 843 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 844 | class_device_unregister(&dev->cdev); |
Dmitry Torokhov | 5c1e9a6 | 2005-09-15 02:01:55 -0500 | [diff] [blame] | 845 | } |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 846 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 847 | input_wakeup_procfs_readers(); |
| 848 | } |
| 849 | |
| 850 | void input_register_handler(struct input_handler *handler) |
| 851 | { |
| 852 | struct input_dev *dev; |
| 853 | struct input_handle *handle; |
| 854 | struct input_device_id *id; |
| 855 | |
| 856 | if (!handler) return; |
| 857 | |
| 858 | INIT_LIST_HEAD(&handler->h_list); |
| 859 | |
| 860 | if (handler->fops != NULL) |
| 861 | input_table[handler->minor >> 5] = handler; |
| 862 | |
| 863 | list_add_tail(&handler->node, &input_handler_list); |
| 864 | |
| 865 | list_for_each_entry(dev, &input_dev_list, node) |
| 866 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 867 | if ((id = input_match_device(handler->id_table, dev))) |
| 868 | if ((handle = handler->connect(handler, dev, id))) |
| 869 | input_link_handle(handle); |
| 870 | |
| 871 | input_wakeup_procfs_readers(); |
| 872 | } |
| 873 | |
| 874 | void input_unregister_handler(struct input_handler *handler) |
| 875 | { |
| 876 | struct list_head * node, * next; |
| 877 | |
| 878 | list_for_each_safe(node, next, &handler->h_list) { |
| 879 | struct input_handle * handle = to_handle_h(node); |
| 880 | list_del_init(&handle->h_node); |
| 881 | list_del_init(&handle->d_node); |
| 882 | handler->disconnect(handle); |
| 883 | } |
| 884 | |
| 885 | list_del_init(&handler->node); |
| 886 | |
| 887 | if (handler->fops != NULL) |
| 888 | input_table[handler->minor >> 5] = NULL; |
| 889 | |
| 890 | input_wakeup_procfs_readers(); |
| 891 | } |
| 892 | |
| 893 | static int input_open_file(struct inode *inode, struct file *file) |
| 894 | { |
| 895 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |
| 896 | struct file_operations *old_fops, *new_fops = NULL; |
| 897 | int err; |
| 898 | |
| 899 | /* No load-on-demand here? */ |
| 900 | if (!handler || !(new_fops = fops_get(handler->fops))) |
| 901 | return -ENODEV; |
| 902 | |
| 903 | /* |
| 904 | * That's _really_ odd. Usually NULL ->open means "nothing special", |
| 905 | * not "no device". Oh, well... |
| 906 | */ |
| 907 | if (!new_fops->open) { |
| 908 | fops_put(new_fops); |
| 909 | return -ENODEV; |
| 910 | } |
| 911 | old_fops = file->f_op; |
| 912 | file->f_op = new_fops; |
| 913 | |
| 914 | err = new_fops->open(inode, file); |
| 915 | |
| 916 | if (err) { |
| 917 | fops_put(file->f_op); |
| 918 | file->f_op = fops_get(old_fops); |
| 919 | } |
| 920 | fops_put(old_fops); |
| 921 | return err; |
| 922 | } |
| 923 | |
| 924 | static struct file_operations input_fops = { |
| 925 | .owner = THIS_MODULE, |
| 926 | .open = input_open_file, |
| 927 | }; |
| 928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | static int __init input_init(void) |
| 930 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 931 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 933 | err = class_register(&input_class); |
Dmitry Torokhov | d19fbe8 | 2005-09-15 02:01:39 -0500 | [diff] [blame] | 934 | if (err) { |
| 935 | printk(KERN_ERR "input: unable to register input_dev class\n"); |
| 936 | return err; |
| 937 | } |
| 938 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 939 | err = input_proc_init(); |
| 940 | if (err) |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 941 | goto fail1; |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 942 | |
| 943 | err = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
| 944 | if (err) { |
| 945 | 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] | 946 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 948 | |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 949 | return 0; |
| 950 | |
Greg Kroah-Hartman | b0fdfeb | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 951 | fail2: input_proc_exit(); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 952 | fail1: class_unregister(&input_class); |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 953 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static void __exit input_exit(void) |
| 957 | { |
Dmitry Torokhov | f96b434 | 2005-06-30 00:50:29 -0500 | [diff] [blame] | 958 | input_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | unregister_chrdev(INPUT_MAJOR, "input"); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame^] | 960 | class_unregister(&input_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | subsys_initcall(input_init); |
| 964 | module_exit(input_exit); |