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> |
| 25 | #include <linux/devfs_fs_kernel.h> |
| 26 | |
| 27 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
| 28 | MODULE_DESCRIPTION("Input core"); |
| 29 | MODULE_LICENSE("GPL"); |
| 30 | |
| 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); |
| 42 | EXPORT_SYMBOL(input_class); |
| 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 | |
| 51 | #ifdef CONFIG_PROC_FS |
| 52 | static struct proc_dir_entry *proc_bus_input_dir; |
| 53 | static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); |
| 54 | static int input_devices_state; |
| 55 | #endif |
| 56 | |
| 57 | void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 58 | { |
| 59 | struct input_handle *handle; |
| 60 | |
| 61 | if (type > EV_MAX || !test_bit(type, dev->evbit)) |
| 62 | return; |
| 63 | |
| 64 | add_input_randomness(type, code, value); |
| 65 | |
| 66 | switch (type) { |
| 67 | |
| 68 | case EV_SYN: |
| 69 | switch (code) { |
| 70 | case SYN_CONFIG: |
| 71 | if (dev->event) dev->event(dev, type, code, value); |
| 72 | break; |
| 73 | |
| 74 | case SYN_REPORT: |
| 75 | if (dev->sync) return; |
| 76 | dev->sync = 1; |
| 77 | break; |
| 78 | } |
| 79 | break; |
| 80 | |
| 81 | case EV_KEY: |
| 82 | |
| 83 | if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) |
| 84 | return; |
| 85 | |
| 86 | if (value == 2) |
| 87 | break; |
| 88 | |
| 89 | change_bit(code, dev->key); |
| 90 | |
| 91 | if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { |
| 92 | dev->repeat_key = code; |
| 93 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); |
| 94 | } |
| 95 | |
| 96 | break; |
| 97 | |
| 98 | case EV_ABS: |
| 99 | |
| 100 | if (code > ABS_MAX || !test_bit(code, dev->absbit)) |
| 101 | return; |
| 102 | |
| 103 | if (dev->absfuzz[code]) { |
| 104 | if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && |
| 105 | (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) |
| 106 | return; |
| 107 | |
| 108 | if ((value > dev->abs[code] - dev->absfuzz[code]) && |
| 109 | (value < dev->abs[code] + dev->absfuzz[code])) |
| 110 | value = (dev->abs[code] * 3 + value) >> 2; |
| 111 | |
| 112 | if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && |
| 113 | (value < dev->abs[code] + (dev->absfuzz[code] << 1))) |
| 114 | value = (dev->abs[code] + value) >> 1; |
| 115 | } |
| 116 | |
| 117 | if (dev->abs[code] == value) |
| 118 | return; |
| 119 | |
| 120 | dev->abs[code] = value; |
| 121 | break; |
| 122 | |
| 123 | case EV_REL: |
| 124 | |
| 125 | if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) |
| 126 | return; |
| 127 | |
| 128 | break; |
| 129 | |
| 130 | case EV_MSC: |
| 131 | |
| 132 | if (code > MSC_MAX || !test_bit(code, dev->mscbit)) |
| 133 | return; |
| 134 | |
| 135 | if (dev->event) dev->event(dev, type, code, value); |
| 136 | |
| 137 | break; |
| 138 | |
| 139 | case EV_LED: |
| 140 | |
| 141 | if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) |
| 142 | return; |
| 143 | |
| 144 | change_bit(code, dev->led); |
| 145 | if (dev->event) dev->event(dev, type, code, value); |
| 146 | |
| 147 | break; |
| 148 | |
| 149 | case EV_SND: |
| 150 | |
| 151 | if (code > SND_MAX || !test_bit(code, dev->sndbit)) |
| 152 | return; |
| 153 | |
| 154 | if (dev->event) dev->event(dev, type, code, value); |
| 155 | |
| 156 | break; |
| 157 | |
| 158 | case EV_REP: |
| 159 | |
| 160 | if (code > REP_MAX || value < 0 || dev->rep[code] == value) return; |
| 161 | |
| 162 | dev->rep[code] = value; |
| 163 | if (dev->event) dev->event(dev, type, code, value); |
| 164 | |
| 165 | break; |
| 166 | |
| 167 | case EV_FF: |
| 168 | if (dev->event) dev->event(dev, type, code, value); |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | if (type != EV_SYN) |
| 173 | dev->sync = 0; |
| 174 | |
| 175 | if (dev->grab) |
| 176 | dev->grab->handler->event(dev->grab, type, code, value); |
| 177 | else |
| 178 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 179 | if (handle->open) |
| 180 | handle->handler->event(handle, type, code, value); |
| 181 | } |
| 182 | |
| 183 | static void input_repeat_key(unsigned long data) |
| 184 | { |
| 185 | struct input_dev *dev = (void *) data; |
| 186 | |
| 187 | if (!test_bit(dev->repeat_key, dev->key)) |
| 188 | return; |
| 189 | |
| 190 | input_event(dev, EV_KEY, dev->repeat_key, 2); |
| 191 | input_sync(dev); |
| 192 | |
| 193 | if (dev->rep[REP_PERIOD]) |
| 194 | mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); |
| 195 | } |
| 196 | |
| 197 | int input_accept_process(struct input_handle *handle, struct file *file) |
| 198 | { |
| 199 | if (handle->dev->accept) |
| 200 | return handle->dev->accept(handle->dev, file); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int input_grab_device(struct input_handle *handle) |
| 206 | { |
| 207 | if (handle->dev->grab) |
| 208 | return -EBUSY; |
| 209 | |
| 210 | handle->dev->grab = handle; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void input_release_device(struct input_handle *handle) |
| 215 | { |
| 216 | if (handle->dev->grab == handle) |
| 217 | handle->dev->grab = NULL; |
| 218 | } |
| 219 | |
| 220 | int input_open_device(struct input_handle *handle) |
| 221 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 222 | struct input_dev *dev = handle->dev; |
| 223 | int err; |
| 224 | |
| 225 | err = down_interruptible(&dev->sem); |
| 226 | if (err) |
| 227 | return err; |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | handle->open++; |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 230 | |
| 231 | if (!dev->users++ && dev->open) |
| 232 | err = dev->open(dev); |
| 233 | |
| 234 | if (err) |
| 235 | handle->open--; |
| 236 | |
| 237 | up(&dev->sem); |
| 238 | |
| 239 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | int input_flush_device(struct input_handle* handle, struct file* file) |
| 243 | { |
| 244 | if (handle->dev->flush) |
| 245 | return handle->dev->flush(handle->dev, file); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | void input_close_device(struct input_handle *handle) |
| 251 | { |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 252 | struct input_dev *dev = handle->dev; |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | input_release_device(handle); |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 255 | |
| 256 | down(&dev->sem); |
| 257 | |
| 258 | if (!--dev->users && dev->close) |
| 259 | dev->close(dev); |
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 | up(&dev->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void input_link_handle(struct input_handle *handle) |
| 266 | { |
| 267 | list_add_tail(&handle->d_node, &handle->dev->h_list); |
| 268 | list_add_tail(&handle->h_node, &handle->handler->h_list); |
| 269 | } |
| 270 | |
| 271 | #define MATCH_BIT(bit, max) \ |
| 272 | for (i = 0; i < NBITS(max); i++) \ |
| 273 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
| 274 | break; \ |
| 275 | if (i != NBITS(max)) \ |
| 276 | continue; |
| 277 | |
| 278 | static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev) |
| 279 | { |
| 280 | int i; |
| 281 | |
| 282 | for (; id->flags || id->driver_info; id++) { |
| 283 | |
| 284 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) |
| 285 | if (id->id.bustype != dev->id.bustype) |
| 286 | continue; |
| 287 | |
| 288 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) |
| 289 | if (id->id.vendor != dev->id.vendor) |
| 290 | continue; |
| 291 | |
| 292 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) |
| 293 | if (id->id.product != dev->id.product) |
| 294 | continue; |
| 295 | |
| 296 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) |
| 297 | if (id->id.version != dev->id.version) |
| 298 | continue; |
| 299 | |
| 300 | MATCH_BIT(evbit, EV_MAX); |
| 301 | MATCH_BIT(keybit, KEY_MAX); |
| 302 | MATCH_BIT(relbit, REL_MAX); |
| 303 | MATCH_BIT(absbit, ABS_MAX); |
| 304 | MATCH_BIT(mscbit, MSC_MAX); |
| 305 | MATCH_BIT(ledbit, LED_MAX); |
| 306 | MATCH_BIT(sndbit, SND_MAX); |
| 307 | MATCH_BIT(ffbit, FF_MAX); |
| 308 | |
| 309 | return id; |
| 310 | } |
| 311 | |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Input hotplugging interface - loading event handlers based on |
| 317 | * device bitfields. |
| 318 | */ |
| 319 | |
| 320 | #ifdef CONFIG_HOTPLUG |
| 321 | |
| 322 | /* |
| 323 | * Input hotplugging invokes what /proc/sys/kernel/hotplug says |
| 324 | * (normally /sbin/hotplug) when input devices get added or removed. |
| 325 | * |
| 326 | * This invokes a user mode policy agent, typically helping to load driver |
| 327 | * or other modules, configure the device, and more. Drivers can provide |
| 328 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
| 329 | * |
| 330 | */ |
| 331 | |
| 332 | #define SPRINTF_BIT_A(bit, name, max) \ |
| 333 | do { \ |
| 334 | envp[i++] = scratch; \ |
| 335 | scratch += sprintf(scratch, name); \ |
| 336 | for (j = NBITS(max) - 1; j >= 0; j--) \ |
| 337 | if (dev->bit[j]) break; \ |
| 338 | for (; j >= 0; j--) \ |
| 339 | scratch += sprintf(scratch, "%lx ", dev->bit[j]); \ |
| 340 | scratch++; \ |
| 341 | } while (0) |
| 342 | |
| 343 | #define SPRINTF_BIT_A2(bit, name, max, ev) \ |
| 344 | do { \ |
| 345 | if (test_bit(ev, dev->evbit)) \ |
| 346 | SPRINTF_BIT_A(bit, name, max); \ |
| 347 | } while (0) |
| 348 | |
| 349 | static void input_call_hotplug(char *verb, struct input_dev *dev) |
| 350 | { |
| 351 | char *argv[3], **envp, *buf, *scratch; |
| 352 | int i = 0, j, value; |
| 353 | |
| 354 | if (!hotplug_path[0]) { |
| 355 | printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n"); |
| 356 | return; |
| 357 | } |
| 358 | if (in_interrupt()) { |
| 359 | printk(KERN_ERR "input.c: calling hotplug from interrupt\n"); |
| 360 | return; |
| 361 | } |
| 362 | if (!current->fs->root) { |
| 363 | printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n"); |
| 364 | return; |
| 365 | } |
| 366 | if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) { |
| 367 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
| 368 | return; |
| 369 | } |
| 370 | if (!(buf = kmalloc(1024, GFP_KERNEL))) { |
| 371 | kfree (envp); |
| 372 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | argv[0] = hotplug_path; |
| 377 | argv[1] = "input"; |
| 378 | argv[2] = NULL; |
| 379 | |
| 380 | envp[i++] = "HOME=/"; |
| 381 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 382 | |
| 383 | scratch = buf; |
| 384 | |
| 385 | envp[i++] = scratch; |
| 386 | scratch += sprintf(scratch, "ACTION=%s", verb) + 1; |
| 387 | |
| 388 | envp[i++] = scratch; |
| 389 | scratch += sprintf(scratch, "PRODUCT=%x/%x/%x/%x", |
| 390 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1; |
| 391 | |
| 392 | if (dev->name) { |
| 393 | envp[i++] = scratch; |
| 394 | scratch += sprintf(scratch, "NAME=%s", dev->name) + 1; |
| 395 | } |
| 396 | |
| 397 | if (dev->phys) { |
| 398 | envp[i++] = scratch; |
| 399 | scratch += sprintf(scratch, "PHYS=%s", dev->phys) + 1; |
| 400 | } |
| 401 | |
| 402 | SPRINTF_BIT_A(evbit, "EV=", EV_MAX); |
| 403 | SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY); |
| 404 | SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL); |
| 405 | SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS); |
| 406 | SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC); |
| 407 | SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED); |
| 408 | SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND); |
| 409 | SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF); |
| 410 | |
| 411 | envp[i++] = NULL; |
| 412 | |
| 413 | #ifdef INPUT_DEBUG |
| 414 | printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n", |
| 415 | argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]); |
| 416 | #endif |
| 417 | |
| 418 | value = call_usermodehelper(argv [0], argv, envp, 0); |
| 419 | |
| 420 | kfree(buf); |
| 421 | kfree(envp); |
| 422 | |
| 423 | #ifdef INPUT_DEBUG |
| 424 | if (value != 0) |
| 425 | printk(KERN_DEBUG "input.c: hotplug returned %d\n", value); |
| 426 | #endif |
| 427 | } |
| 428 | |
| 429 | #endif |
| 430 | |
| 431 | void input_register_device(struct input_dev *dev) |
| 432 | { |
| 433 | struct input_handle *handle; |
| 434 | struct input_handler *handler; |
| 435 | struct input_device_id *id; |
| 436 | |
| 437 | set_bit(EV_SYN, dev->evbit); |
| 438 | |
Dmitry Torokhov | 0fbf87c | 2005-05-29 02:29:25 -0500 | [diff] [blame] | 439 | init_MUTEX(&dev->sem); |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | /* |
| 442 | * If delay and period are pre-set by the driver, then autorepeating |
| 443 | * is handled by the driver itself and we don't do it in input.c. |
| 444 | */ |
| 445 | |
| 446 | init_timer(&dev->timer); |
| 447 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { |
| 448 | dev->timer.data = (long) dev; |
| 449 | dev->timer.function = input_repeat_key; |
| 450 | dev->rep[REP_DELAY] = 250; |
| 451 | dev->rep[REP_PERIOD] = 33; |
| 452 | } |
| 453 | |
| 454 | INIT_LIST_HEAD(&dev->h_list); |
| 455 | list_add_tail(&dev->node, &input_dev_list); |
| 456 | |
| 457 | list_for_each_entry(handler, &input_handler_list, node) |
| 458 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 459 | if ((id = input_match_device(handler->id_table, dev))) |
| 460 | if ((handle = handler->connect(handler, dev, id))) |
| 461 | input_link_handle(handle); |
| 462 | |
| 463 | #ifdef CONFIG_HOTPLUG |
| 464 | input_call_hotplug("add", dev); |
| 465 | #endif |
| 466 | |
| 467 | #ifdef CONFIG_PROC_FS |
| 468 | input_devices_state++; |
| 469 | wake_up(&input_devices_poll_wait); |
| 470 | #endif |
| 471 | } |
| 472 | |
| 473 | void input_unregister_device(struct input_dev *dev) |
| 474 | { |
| 475 | struct list_head * node, * next; |
| 476 | |
| 477 | if (!dev) return; |
| 478 | |
| 479 | del_timer_sync(&dev->timer); |
| 480 | |
| 481 | list_for_each_safe(node, next, &dev->h_list) { |
| 482 | struct input_handle * handle = to_handle(node); |
| 483 | list_del_init(&handle->d_node); |
| 484 | list_del_init(&handle->h_node); |
| 485 | handle->handler->disconnect(handle); |
| 486 | } |
| 487 | |
| 488 | #ifdef CONFIG_HOTPLUG |
| 489 | input_call_hotplug("remove", dev); |
| 490 | #endif |
| 491 | |
| 492 | list_del_init(&dev->node); |
| 493 | |
| 494 | #ifdef CONFIG_PROC_FS |
| 495 | input_devices_state++; |
| 496 | wake_up(&input_devices_poll_wait); |
| 497 | #endif |
| 498 | } |
| 499 | |
| 500 | void input_register_handler(struct input_handler *handler) |
| 501 | { |
| 502 | struct input_dev *dev; |
| 503 | struct input_handle *handle; |
| 504 | struct input_device_id *id; |
| 505 | |
| 506 | if (!handler) return; |
| 507 | |
| 508 | INIT_LIST_HEAD(&handler->h_list); |
| 509 | |
| 510 | if (handler->fops != NULL) |
| 511 | input_table[handler->minor >> 5] = handler; |
| 512 | |
| 513 | list_add_tail(&handler->node, &input_handler_list); |
| 514 | |
| 515 | list_for_each_entry(dev, &input_dev_list, node) |
| 516 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) |
| 517 | if ((id = input_match_device(handler->id_table, dev))) |
| 518 | if ((handle = handler->connect(handler, dev, id))) |
| 519 | input_link_handle(handle); |
| 520 | |
| 521 | #ifdef CONFIG_PROC_FS |
| 522 | input_devices_state++; |
| 523 | wake_up(&input_devices_poll_wait); |
| 524 | #endif |
| 525 | } |
| 526 | |
| 527 | void input_unregister_handler(struct input_handler *handler) |
| 528 | { |
| 529 | struct list_head * node, * next; |
| 530 | |
| 531 | list_for_each_safe(node, next, &handler->h_list) { |
| 532 | struct input_handle * handle = to_handle_h(node); |
| 533 | list_del_init(&handle->h_node); |
| 534 | list_del_init(&handle->d_node); |
| 535 | handler->disconnect(handle); |
| 536 | } |
| 537 | |
| 538 | list_del_init(&handler->node); |
| 539 | |
| 540 | if (handler->fops != NULL) |
| 541 | input_table[handler->minor >> 5] = NULL; |
| 542 | |
| 543 | #ifdef CONFIG_PROC_FS |
| 544 | input_devices_state++; |
| 545 | wake_up(&input_devices_poll_wait); |
| 546 | #endif |
| 547 | } |
| 548 | |
| 549 | static int input_open_file(struct inode *inode, struct file *file) |
| 550 | { |
| 551 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |
| 552 | struct file_operations *old_fops, *new_fops = NULL; |
| 553 | int err; |
| 554 | |
| 555 | /* No load-on-demand here? */ |
| 556 | if (!handler || !(new_fops = fops_get(handler->fops))) |
| 557 | return -ENODEV; |
| 558 | |
| 559 | /* |
| 560 | * That's _really_ odd. Usually NULL ->open means "nothing special", |
| 561 | * not "no device". Oh, well... |
| 562 | */ |
| 563 | if (!new_fops->open) { |
| 564 | fops_put(new_fops); |
| 565 | return -ENODEV; |
| 566 | } |
| 567 | old_fops = file->f_op; |
| 568 | file->f_op = new_fops; |
| 569 | |
| 570 | err = new_fops->open(inode, file); |
| 571 | |
| 572 | if (err) { |
| 573 | fops_put(file->f_op); |
| 574 | file->f_op = fops_get(old_fops); |
| 575 | } |
| 576 | fops_put(old_fops); |
| 577 | return err; |
| 578 | } |
| 579 | |
| 580 | static struct file_operations input_fops = { |
| 581 | .owner = THIS_MODULE, |
| 582 | .open = input_open_file, |
| 583 | }; |
| 584 | |
| 585 | #ifdef CONFIG_PROC_FS |
| 586 | |
| 587 | #define SPRINTF_BIT_B(bit, name, max) \ |
| 588 | do { \ |
| 589 | len += sprintf(buf + len, "B: %s", name); \ |
| 590 | for (i = NBITS(max) - 1; i >= 0; i--) \ |
| 591 | if (dev->bit[i]) break; \ |
| 592 | for (; i >= 0; i--) \ |
| 593 | len += sprintf(buf + len, "%lx ", dev->bit[i]); \ |
| 594 | len += sprintf(buf + len, "\n"); \ |
| 595 | } while (0) |
| 596 | |
| 597 | #define SPRINTF_BIT_B2(bit, name, max, ev) \ |
| 598 | do { \ |
| 599 | if (test_bit(ev, dev->evbit)) \ |
| 600 | SPRINTF_BIT_B(bit, name, max); \ |
| 601 | } while (0) |
| 602 | |
| 603 | |
| 604 | static unsigned int input_devices_poll(struct file *file, poll_table *wait) |
| 605 | { |
| 606 | int state = input_devices_state; |
| 607 | poll_wait(file, &input_devices_poll_wait, wait); |
| 608 | if (state != input_devices_state) |
| 609 | return POLLIN | POLLRDNORM; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
| 614 | { |
| 615 | struct input_dev *dev; |
| 616 | struct input_handle *handle; |
| 617 | |
| 618 | off_t at = 0; |
| 619 | int i, len, cnt = 0; |
| 620 | |
| 621 | list_for_each_entry(dev, &input_dev_list, node) { |
| 622 | |
| 623 | len = sprintf(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", |
| 624 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); |
| 625 | |
| 626 | len += sprintf(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
| 627 | len += sprintf(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
| 628 | len += sprintf(buf + len, "H: Handlers="); |
| 629 | |
| 630 | list_for_each_entry(handle, &dev->h_list, d_node) |
| 631 | len += sprintf(buf + len, "%s ", handle->name); |
| 632 | |
| 633 | len += sprintf(buf + len, "\n"); |
| 634 | |
| 635 | SPRINTF_BIT_B(evbit, "EV=", EV_MAX); |
| 636 | SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY); |
| 637 | SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL); |
| 638 | SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS); |
| 639 | SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC); |
| 640 | SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED); |
| 641 | SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND); |
| 642 | SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF); |
| 643 | |
| 644 | len += sprintf(buf + len, "\n"); |
| 645 | |
| 646 | at += len; |
| 647 | |
| 648 | if (at >= pos) { |
| 649 | if (!*start) { |
| 650 | *start = buf + (pos - (at - len)); |
| 651 | cnt = at - pos; |
| 652 | } else cnt += len; |
| 653 | buf += len; |
| 654 | if (cnt >= count) |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if (&dev->node == &input_dev_list) |
| 660 | *eof = 1; |
| 661 | |
| 662 | return (count > cnt) ? cnt : count; |
| 663 | } |
| 664 | |
| 665 | static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
| 666 | { |
| 667 | struct input_handler *handler; |
| 668 | |
| 669 | off_t at = 0; |
| 670 | int len = 0, cnt = 0; |
| 671 | int i = 0; |
| 672 | |
| 673 | list_for_each_entry(handler, &input_handler_list, node) { |
| 674 | |
| 675 | if (handler->fops) |
| 676 | len = sprintf(buf, "N: Number=%d Name=%s Minor=%d\n", |
| 677 | i++, handler->name, handler->minor); |
| 678 | else |
| 679 | len = sprintf(buf, "N: Number=%d Name=%s\n", |
| 680 | i++, handler->name); |
| 681 | |
| 682 | at += len; |
| 683 | |
| 684 | if (at >= pos) { |
| 685 | if (!*start) { |
| 686 | *start = buf + (pos - (at - len)); |
| 687 | cnt = at - pos; |
| 688 | } else cnt += len; |
| 689 | buf += len; |
| 690 | if (cnt >= count) |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | if (&handler->node == &input_handler_list) |
| 695 | *eof = 1; |
| 696 | |
| 697 | return (count > cnt) ? cnt : count; |
| 698 | } |
| 699 | |
| 700 | static int __init input_proc_init(void) |
| 701 | { |
| 702 | struct proc_dir_entry *entry; |
| 703 | |
| 704 | proc_bus_input_dir = proc_mkdir("input", proc_bus); |
| 705 | if (proc_bus_input_dir == NULL) |
| 706 | return -ENOMEM; |
| 707 | proc_bus_input_dir->owner = THIS_MODULE; |
| 708 | entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL); |
| 709 | if (entry == NULL) { |
| 710 | remove_proc_entry("input", proc_bus); |
| 711 | return -ENOMEM; |
| 712 | } |
| 713 | entry->owner = THIS_MODULE; |
| 714 | entry->proc_fops->poll = input_devices_poll; |
| 715 | entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL); |
| 716 | if (entry == NULL) { |
| 717 | remove_proc_entry("devices", proc_bus_input_dir); |
| 718 | remove_proc_entry("input", proc_bus); |
| 719 | return -ENOMEM; |
| 720 | } |
| 721 | entry->owner = THIS_MODULE; |
| 722 | return 0; |
| 723 | } |
| 724 | #else /* !CONFIG_PROC_FS */ |
| 725 | static inline int input_proc_init(void) { return 0; } |
| 726 | #endif |
| 727 | |
| 728 | struct class_simple *input_class; |
| 729 | |
| 730 | static int __init input_init(void) |
| 731 | { |
| 732 | int retval = -ENOMEM; |
| 733 | |
| 734 | input_class = class_simple_create(THIS_MODULE, "input"); |
| 735 | if (IS_ERR(input_class)) |
| 736 | return PTR_ERR(input_class); |
| 737 | input_proc_init(); |
| 738 | retval = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
| 739 | if (retval) { |
| 740 | printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR); |
| 741 | remove_proc_entry("devices", proc_bus_input_dir); |
| 742 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 743 | remove_proc_entry("input", proc_bus); |
| 744 | class_simple_destroy(input_class); |
| 745 | return retval; |
| 746 | } |
| 747 | |
| 748 | retval = devfs_mk_dir("input"); |
| 749 | if (retval) { |
| 750 | remove_proc_entry("devices", proc_bus_input_dir); |
| 751 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 752 | remove_proc_entry("input", proc_bus); |
| 753 | unregister_chrdev(INPUT_MAJOR, "input"); |
| 754 | class_simple_destroy(input_class); |
| 755 | } |
| 756 | return retval; |
| 757 | } |
| 758 | |
| 759 | static void __exit input_exit(void) |
| 760 | { |
| 761 | remove_proc_entry("devices", proc_bus_input_dir); |
| 762 | remove_proc_entry("handlers", proc_bus_input_dir); |
| 763 | remove_proc_entry("input", proc_bus); |
| 764 | |
| 765 | devfs_remove("input"); |
| 766 | unregister_chrdev(INPUT_MAJOR, "input"); |
| 767 | class_simple_destroy(input_class); |
| 768 | } |
| 769 | |
| 770 | subsys_initcall(input_init); |
| 771 | module_exit(input_exit); |