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