1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * The input core 4 * 5 * Copyright (c) 1999-2002 Vojtech Pavlik 6 */ 7 8 9 #define pr_fmt(fmt) KBUILD_BASENAME ": " fmt 10 11 #include <linux/export.h> 12 #include <linux/init.h> 13 #include <linux/types.h> 14 #include <linux/idr.h> 15 #include <linux/input/mt.h> 16 #include <linux/module.h> 17 #include <linux/slab.h> 18 #include <linux/random.h> 19 #include <linux/major.h> 20 #include <linux/proc_fs.h> 21 #include <linux/sched.h> 22 #include <linux/seq_file.h> 23 #include <linux/pm.h> 24 #include <linux/poll.h> 25 #include <linux/device.h> 26 #include <linux/kstrtox.h> 27 #include <linux/mutex.h> 28 #include <linux/rcupdate.h> 29 #include "input-compat.h" 30 #include "input-core-private.h" 31 #include "input-poller.h" 32 33 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 34 MODULE_DESCRIPTION("Input core"); 35 MODULE_LICENSE("GPL"); 36 37 #define INPUT_MAX_CHAR_DEVICES 1024 38 #define INPUT_FIRST_DYNAMIC_DEV 256 39 static DEFINE_IDA(input_ida); 40 41 static LIST_HEAD(input_dev_list); 42 static LIST_HEAD(input_handler_list); 43 44 /* 45 * input_mutex protects access to both input_dev_list and input_handler_list. 46 * This also causes input_[un]register_device and input_[un]register_handler 47 * be mutually exclusive which simplifies locking in drivers implementing 48 * input handlers. 49 */ 50 static DEFINE_MUTEX(input_mutex); 51 52 static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 }; 53 54 static const unsigned int input_max_code[EV_CNT] = { 55 [EV_KEY] = KEY_MAX, 56 [EV_REL] = REL_MAX, 57 [EV_ABS] = ABS_MAX, 58 [EV_MSC] = MSC_MAX, 59 [EV_SW] = SW_MAX, 60 [EV_LED] = LED_MAX, 61 [EV_SND] = SND_MAX, 62 [EV_FF] = FF_MAX, 63 }; 64 65 static inline int is_event_supported(unsigned int code, 66 unsigned long *bm, unsigned int max) 67 { 68 return code <= max && test_bit(code, bm); 69 } 70 71 static int input_defuzz_abs_event(int value, int old_val, int fuzz) 72 { 73 if (fuzz) { 74 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2) 75 return old_val; 76 77 if (value > old_val - fuzz && value < old_val + fuzz) 78 return (old_val * 3 + value) / 4; 79 80 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2) 81 return (old_val + value) / 2; 82 } 83 84 return value; 85 } 86 87 static void input_start_autorepeat(struct input_dev *dev, int code) 88 { 89 if (test_bit(EV_REP, dev->evbit) && 90 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && 91 dev->timer.function) { 92 dev->repeat_key = code; 93 mod_timer(&dev->timer, 94 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); 95 } 96 } 97 98 static void input_stop_autorepeat(struct input_dev *dev) 99 { 100 timer_delete(&dev->timer); 101 } 102 103 /* 104 * Pass values first through all filters and then, if event has not been 105 * filtered out, through all open handles. This order is achieved by placing 106 * filters at the head of the list of handles attached to the device, and 107 * placing regular handles at the tail of the list. 108 * 109 * This function is called with dev->event_lock held and interrupts disabled. 110 */ 111 static void input_pass_values(struct input_dev *dev, 112 struct input_value *vals, unsigned int count) 113 { 114 struct input_handle *handle; 115 struct input_value *v; 116 117 lockdep_assert_held(&dev->event_lock); 118 119 scoped_guard(rcu) { 120 handle = rcu_dereference(dev->grab); 121 if (handle) { 122 count = handle->handle_events(handle, vals, count); 123 break; 124 } 125 126 list_for_each_entry_rcu(handle, &dev->h_list, d_node) { 127 if (handle->open) { 128 count = handle->handle_events(handle, vals, 129 count); 130 if (!count) 131 break; 132 } 133 } 134 } 135 136 /* trigger auto repeat for key events */ 137 if (test_bit(EV_REP, dev->evbit) && test_bit(EV_KEY, dev->evbit)) { 138 for (v = vals; v != vals + count; v++) { 139 if (v->type == EV_KEY && v->value != 2) { 140 if (v->value) 141 input_start_autorepeat(dev, v->code); 142 else 143 input_stop_autorepeat(dev); 144 } 145 } 146 } 147 } 148 149 #define INPUT_IGNORE_EVENT 0 150 #define INPUT_PASS_TO_HANDLERS 1 151 #define INPUT_PASS_TO_DEVICE 2 152 #define INPUT_SLOT 4 153 #define INPUT_FLUSH 8 154 #define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) 155 156 static int input_handle_abs_event(struct input_dev *dev, 157 unsigned int code, int *pval) 158 { 159 struct input_mt *mt = dev->mt; 160 bool is_new_slot = false; 161 bool is_mt_event; 162 int *pold; 163 164 if (code == ABS_MT_SLOT) { 165 /* 166 * "Stage" the event; we'll flush it later, when we 167 * get actual touch data. 168 */ 169 if (mt && *pval >= 0 && *pval < mt->num_slots) 170 mt->slot = *pval; 171 172 return INPUT_IGNORE_EVENT; 173 } 174 175 is_mt_event = input_is_mt_value(code); 176 177 if (!is_mt_event) { 178 pold = &dev->absinfo[code].value; 179 } else if (mt) { 180 pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST]; 181 is_new_slot = mt->slot != dev->absinfo[ABS_MT_SLOT].value; 182 } else { 183 /* 184 * Bypass filtering for multi-touch events when 185 * not employing slots. 186 */ 187 pold = NULL; 188 } 189 190 if (pold) { 191 *pval = input_defuzz_abs_event(*pval, *pold, 192 dev->absinfo[code].fuzz); 193 if (*pold == *pval) 194 return INPUT_IGNORE_EVENT; 195 196 *pold = *pval; 197 } 198 199 /* Flush pending "slot" event */ 200 if (is_new_slot) { 201 dev->absinfo[ABS_MT_SLOT].value = mt->slot; 202 return INPUT_PASS_TO_HANDLERS | INPUT_SLOT; 203 } 204 205 return INPUT_PASS_TO_HANDLERS; 206 } 207 208 static int input_get_disposition(struct input_dev *dev, 209 unsigned int type, unsigned int code, int *pval) 210 { 211 int disposition = INPUT_IGNORE_EVENT; 212 int value = *pval; 213 214 /* filter-out events from inhibited devices */ 215 if (dev->inhibited) 216 return INPUT_IGNORE_EVENT; 217 218 switch (type) { 219 220 case EV_SYN: 221 switch (code) { 222 case SYN_CONFIG: 223 disposition = INPUT_PASS_TO_ALL; 224 break; 225 226 case SYN_REPORT: 227 disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH; 228 break; 229 case SYN_MT_REPORT: 230 disposition = INPUT_PASS_TO_HANDLERS; 231 break; 232 } 233 break; 234 235 case EV_KEY: 236 if (is_event_supported(code, dev->keybit, KEY_MAX)) { 237 238 /* auto-repeat bypasses state updates */ 239 if (value == 2) { 240 disposition = INPUT_PASS_TO_HANDLERS; 241 break; 242 } 243 244 if (!!test_bit(code, dev->key) != !!value) { 245 246 __change_bit(code, dev->key); 247 disposition = INPUT_PASS_TO_HANDLERS; 248 } 249 } 250 break; 251 252 case EV_SW: 253 if (is_event_supported(code, dev->swbit, SW_MAX) && 254 !!test_bit(code, dev->sw) != !!value) { 255 256 __change_bit(code, dev->sw); 257 disposition = INPUT_PASS_TO_HANDLERS; 258 } 259 break; 260 261 case EV_ABS: 262 if (is_event_supported(code, dev->absbit, ABS_MAX)) 263 disposition = input_handle_abs_event(dev, code, &value); 264 265 break; 266 267 case EV_REL: 268 if (is_event_supported(code, dev->relbit, REL_MAX) && value) 269 disposition = INPUT_PASS_TO_HANDLERS; 270 271 break; 272 273 case EV_MSC: 274 if (is_event_supported(code, dev->mscbit, MSC_MAX)) 275 disposition = INPUT_PASS_TO_ALL; 276 277 break; 278 279 case EV_LED: 280 if (is_event_supported(code, dev->ledbit, LED_MAX) && 281 !!test_bit(code, dev->led) != !!value) { 282 283 __change_bit(code, dev->led); 284 disposition = INPUT_PASS_TO_ALL; 285 } 286 break; 287 288 case EV_SND: 289 if (is_event_supported(code, dev->sndbit, SND_MAX)) { 290 291 if (!!test_bit(code, dev->snd) != !!value) 292 __change_bit(code, dev->snd); 293 disposition = INPUT_PASS_TO_ALL; 294 } 295 break; 296 297 case EV_REP: 298 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) { 299 dev->rep[code] = value; 300 disposition = INPUT_PASS_TO_ALL; 301 } 302 break; 303 304 case EV_FF: 305 if (value >= 0) 306 disposition = INPUT_PASS_TO_ALL; 307 break; 308 309 case EV_PWR: 310 disposition = INPUT_PASS_TO_ALL; 311 break; 312 } 313 314 *pval = value; 315 return disposition; 316 } 317 318 static void input_event_dispose(struct input_dev *dev, int disposition, 319 unsigned int type, unsigned int code, int value) 320 { 321 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) 322 dev->event(dev, type, code, value); 323 324 if (disposition & INPUT_PASS_TO_HANDLERS) { 325 struct input_value *v; 326 327 if (disposition & INPUT_SLOT) { 328 v = &dev->vals[dev->num_vals++]; 329 v->type = EV_ABS; 330 v->code = ABS_MT_SLOT; 331 v->value = dev->mt->slot; 332 } 333 334 v = &dev->vals[dev->num_vals++]; 335 v->type = type; 336 v->code = code; 337 v->value = value; 338 } 339 340 if (disposition & INPUT_FLUSH) { 341 if (dev->num_vals >= 2) 342 input_pass_values(dev, dev->vals, dev->num_vals); 343 dev->num_vals = 0; 344 /* 345 * Reset the timestamp on flush so we won't end up 346 * with a stale one. Note we only need to reset the 347 * monolithic one as we use its presence when deciding 348 * whether to generate a synthetic timestamp. 349 */ 350 dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0); 351 } else if (dev->num_vals >= dev->max_vals - 2) { 352 dev->vals[dev->num_vals++] = input_value_sync; 353 input_pass_values(dev, dev->vals, dev->num_vals); 354 dev->num_vals = 0; 355 } 356 } 357 358 void input_handle_event(struct input_dev *dev, 359 unsigned int type, unsigned int code, int value) 360 { 361 int disposition; 362 363 lockdep_assert_held(&dev->event_lock); 364 365 disposition = input_get_disposition(dev, type, code, &value); 366 if (disposition != INPUT_IGNORE_EVENT) { 367 if (type != EV_SYN) 368 add_input_randomness(type, code, value); 369 370 input_event_dispose(dev, disposition, type, code, value); 371 } 372 } 373 374 /** 375 * input_event() - report new input event 376 * @dev: device that generated the event 377 * @type: type of the event 378 * @code: event code 379 * @value: value of the event 380 * 381 * This function should be used by drivers implementing various input 382 * devices to report input events. See also input_inject_event(). 383 * 384 * NOTE: input_event() may be safely used right after input device was 385 * allocated with input_allocate_device(), even before it is registered 386 * with input_register_device(), but the event will not reach any of the 387 * input handlers. Such early invocation of input_event() may be used 388 * to 'seed' initial state of a switch or initial position of absolute 389 * axis, etc. 390 */ 391 void input_event(struct input_dev *dev, 392 unsigned int type, unsigned int code, int value) 393 { 394 if (is_event_supported(type, dev->evbit, EV_MAX)) { 395 guard(spinlock_irqsave)(&dev->event_lock); 396 input_handle_event(dev, type, code, value); 397 } 398 } 399 EXPORT_SYMBOL(input_event); 400 401 /** 402 * input_inject_event() - send input event from input handler 403 * @handle: input handle to send event through 404 * @type: type of the event 405 * @code: event code 406 * @value: value of the event 407 * 408 * Similar to input_event() but will ignore event if device is 409 * "grabbed" and handle injecting event is not the one that owns 410 * the device. 411 */ 412 void input_inject_event(struct input_handle *handle, 413 unsigned int type, unsigned int code, int value) 414 { 415 struct input_dev *dev = handle->dev; 416 struct input_handle *grab; 417 418 if (is_event_supported(type, dev->evbit, EV_MAX)) { 419 guard(spinlock_irqsave)(&dev->event_lock); 420 guard(rcu)(); 421 422 grab = rcu_dereference(dev->grab); 423 if (!grab || grab == handle) 424 input_handle_event(dev, type, code, value); 425 426 } 427 } 428 EXPORT_SYMBOL(input_inject_event); 429 430 /** 431 * input_alloc_absinfo - allocates array of input_absinfo structs 432 * @dev: the input device emitting absolute events 433 * 434 * If the absinfo struct the caller asked for is already allocated, this 435 * functions will not do anything. 436 */ 437 void input_alloc_absinfo(struct input_dev *dev) 438 { 439 if (dev->absinfo) 440 return; 441 442 dev->absinfo = kzalloc_objs(*dev->absinfo, ABS_CNT); 443 if (!dev->absinfo) { 444 dev_err(dev->dev.parent ?: &dev->dev, 445 "%s: unable to allocate memory\n", __func__); 446 /* 447 * We will handle this allocation failure in 448 * input_register_device() when we refuse to register input 449 * device with ABS bits but without absinfo. 450 */ 451 } 452 } 453 EXPORT_SYMBOL(input_alloc_absinfo); 454 455 void input_set_abs_params(struct input_dev *dev, unsigned int axis, 456 int min, int max, int fuzz, int flat) 457 { 458 struct input_absinfo *absinfo; 459 460 __set_bit(EV_ABS, dev->evbit); 461 __set_bit(axis, dev->absbit); 462 463 input_alloc_absinfo(dev); 464 if (!dev->absinfo) 465 return; 466 467 absinfo = &dev->absinfo[axis]; 468 absinfo->minimum = min; 469 absinfo->maximum = max; 470 absinfo->fuzz = fuzz; 471 absinfo->flat = flat; 472 } 473 EXPORT_SYMBOL(input_set_abs_params); 474 475 /** 476 * input_copy_abs - Copy absinfo from one input_dev to another 477 * @dst: Destination input device to copy the abs settings to 478 * @dst_axis: ABS_* value selecting the destination axis 479 * @src: Source input device to copy the abs settings from 480 * @src_axis: ABS_* value selecting the source axis 481 * 482 * Set absinfo for the selected destination axis by copying it from 483 * the specified source input device's source axis. 484 * This is useful to e.g. setup a pen/stylus input-device for combined 485 * touchscreen/pen hardware where the pen uses the same coordinates as 486 * the touchscreen. 487 */ 488 void input_copy_abs(struct input_dev *dst, unsigned int dst_axis, 489 const struct input_dev *src, unsigned int src_axis) 490 { 491 /* src must have EV_ABS and src_axis set */ 492 if (WARN_ON(!(test_bit(EV_ABS, src->evbit) && 493 test_bit(src_axis, src->absbit)))) 494 return; 495 496 /* 497 * input_alloc_absinfo() may have failed for the source. Our caller is 498 * expected to catch this when registering the input devices, which may 499 * happen after the input_copy_abs() call. 500 */ 501 if (!src->absinfo) 502 return; 503 504 input_set_capability(dst, EV_ABS, dst_axis); 505 if (!dst->absinfo) 506 return; 507 508 dst->absinfo[dst_axis] = src->absinfo[src_axis]; 509 } 510 EXPORT_SYMBOL(input_copy_abs); 511 512 /** 513 * input_grab_device - grabs device for exclusive use 514 * @handle: input handle that wants to own the device 515 * 516 * When a device is grabbed by an input handle all events generated by 517 * the device are delivered only to this handle. Also events injected 518 * by other input handles are ignored while device is grabbed. 519 */ 520 int input_grab_device(struct input_handle *handle) 521 { 522 struct input_dev *dev = handle->dev; 523 524 scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { 525 if (dev->grab) 526 return -EBUSY; 527 528 rcu_assign_pointer(dev->grab, handle); 529 } 530 531 return 0; 532 } 533 EXPORT_SYMBOL(input_grab_device); 534 535 static void __input_release_device(struct input_handle *handle) 536 { 537 struct input_dev *dev = handle->dev; 538 struct input_handle *grabber; 539 540 grabber = rcu_dereference_protected(dev->grab, 541 lockdep_is_held(&dev->mutex)); 542 if (grabber == handle) { 543 rcu_assign_pointer(dev->grab, NULL); 544 /* Make sure input_pass_values() notices that grab is gone */ 545 synchronize_rcu(); 546 547 list_for_each_entry(handle, &dev->h_list, d_node) 548 if (handle->open && handle->handler->start) 549 handle->handler->start(handle); 550 } 551 } 552 553 /** 554 * input_release_device - release previously grabbed device 555 * @handle: input handle that owns the device 556 * 557 * Releases previously grabbed device so that other input handles can 558 * start receiving input events. Upon release all handlers attached 559 * to the device have their start() method called so they have a change 560 * to synchronize device state with the rest of the system. 561 */ 562 void input_release_device(struct input_handle *handle) 563 { 564 struct input_dev *dev = handle->dev; 565 566 guard(mutex)(&dev->mutex); 567 __input_release_device(handle); 568 } 569 EXPORT_SYMBOL(input_release_device); 570 571 /** 572 * input_open_device - open input device 573 * @handle: handle through which device is being accessed 574 * 575 * This function should be called by input handlers when they 576 * want to start receive events from given input device. 577 */ 578 int input_open_device(struct input_handle *handle) 579 { 580 struct input_dev *dev = handle->dev; 581 int error; 582 583 scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { 584 if (dev->going_away) 585 return -ENODEV; 586 587 handle->open++; 588 589 if (handle->handler->passive_observer) 590 return 0; 591 592 if (dev->users++ || dev->inhibited) { 593 /* 594 * Device is already opened and/or inhibited, 595 * so we can exit immediately and report success. 596 */ 597 return 0; 598 } 599 600 if (dev->open) { 601 error = dev->open(dev); 602 if (error) { 603 dev->users--; 604 handle->open--; 605 /* 606 * Make sure we are not delivering any more 607 * events through this handle. 608 */ 609 synchronize_rcu(); 610 return error; 611 } 612 } 613 614 if (dev->poller) 615 input_dev_poller_start(dev->poller); 616 } 617 618 return 0; 619 } 620 EXPORT_SYMBOL(input_open_device); 621 622 int input_flush_device(struct input_handle *handle, struct file *file) 623 { 624 struct input_dev *dev = handle->dev; 625 626 scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { 627 if (dev->flush) 628 return dev->flush(dev, file); 629 } 630 631 return 0; 632 } 633 EXPORT_SYMBOL(input_flush_device); 634 635 /** 636 * input_close_device - close input device 637 * @handle: handle through which device is being accessed 638 * 639 * This function should be called by input handlers when they 640 * want to stop receive events from given input device. 641 */ 642 void input_close_device(struct input_handle *handle) 643 { 644 struct input_dev *dev = handle->dev; 645 646 guard(mutex)(&dev->mutex); 647 648 __input_release_device(handle); 649 650 if (!handle->handler->passive_observer) { 651 if (!--dev->users && !dev->inhibited) { 652 if (dev->poller) 653 input_dev_poller_stop(dev->poller); 654 if (dev->close) 655 dev->close(dev); 656 } 657 } 658 659 if (!--handle->open) { 660 /* 661 * synchronize_rcu() makes sure that input_pass_values() 662 * completed and that no more input events are delivered 663 * through this handle 664 */ 665 synchronize_rcu(); 666 } 667 } 668 EXPORT_SYMBOL(input_close_device); 669 670 /* 671 * Simulate keyup events for all keys that are marked as pressed. 672 * The function must be called with dev->event_lock held. 673 */ 674 static bool input_dev_release_keys(struct input_dev *dev) 675 { 676 bool need_sync = false; 677 int code; 678 679 lockdep_assert_held(&dev->event_lock); 680 681 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) { 682 for_each_set_bit(code, dev->key, KEY_CNT) { 683 input_handle_event(dev, EV_KEY, code, 0); 684 need_sync = true; 685 } 686 } 687 688 return need_sync; 689 } 690 691 /* 692 * Prepare device for unregistering 693 */ 694 static void input_disconnect_device(struct input_dev *dev) 695 { 696 struct input_handle *handle; 697 698 /* 699 * Mark device as going away. Note that we take dev->mutex here 700 * not to protect access to dev->going_away but rather to ensure 701 * that there are no threads in the middle of input_open_device() 702 */ 703 scoped_guard(mutex, &dev->mutex) 704 dev->going_away = true; 705 706 guard(spinlock_irq)(&dev->event_lock); 707 708 /* 709 * Simulate keyup events for all pressed keys so that handlers 710 * are not left with "stuck" keys. The driver may continue 711 * generate events even after we done here but they will not 712 * reach any handlers. 713 */ 714 if (input_dev_release_keys(dev)) 715 input_handle_event(dev, EV_SYN, SYN_REPORT, 1); 716 717 list_for_each_entry(handle, &dev->h_list, d_node) 718 handle->open = 0; 719 } 720 721 /** 722 * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry 723 * @ke: keymap entry containing scancode to be converted. 724 * @scancode: pointer to the location where converted scancode should 725 * be stored. 726 * 727 * This function is used to convert scancode stored in &struct keymap_entry 728 * into scalar form understood by legacy keymap handling methods. These 729 * methods expect scancodes to be represented as 'unsigned int'. 730 */ 731 int input_scancode_to_scalar(const struct input_keymap_entry *ke, 732 unsigned int *scancode) 733 { 734 switch (ke->len) { 735 case 1: 736 *scancode = *((u8 *)ke->scancode); 737 break; 738 739 case 2: 740 *scancode = *((u16 *)ke->scancode); 741 break; 742 743 case 4: 744 *scancode = *((u32 *)ke->scancode); 745 break; 746 747 default: 748 return -EINVAL; 749 } 750 751 return 0; 752 } 753 EXPORT_SYMBOL(input_scancode_to_scalar); 754 755 /* 756 * Those routines handle the default case where no [gs]etkeycode() is 757 * defined. In this case, an array indexed by the scancode is used. 758 */ 759 760 static unsigned int input_fetch_keycode(struct input_dev *dev, 761 unsigned int index) 762 { 763 switch (dev->keycodesize) { 764 case 1: 765 return ((u8 *)dev->keycode)[index]; 766 767 case 2: 768 return ((u16 *)dev->keycode)[index]; 769 770 default: 771 return ((u32 *)dev->keycode)[index]; 772 } 773 } 774 775 static int input_default_getkeycode(struct input_dev *dev, 776 struct input_keymap_entry *ke) 777 { 778 unsigned int index; 779 int error; 780 781 if (!dev->keycodesize) 782 return -EINVAL; 783 784 if (ke->flags & INPUT_KEYMAP_BY_INDEX) 785 index = ke->index; 786 else { 787 error = input_scancode_to_scalar(ke, &index); 788 if (error) 789 return error; 790 } 791 792 if (index >= dev->keycodemax) 793 return -EINVAL; 794 795 ke->keycode = input_fetch_keycode(dev, index); 796 ke->index = index; 797 ke->len = sizeof(index); 798 memcpy(ke->scancode, &index, sizeof(index)); 799 800 return 0; 801 } 802 803 /** 804 * input_default_setkeycode - default setkeycode method 805 * @dev: input device which keymap is being updated. 806 * @ke: new keymap entry. 807 * @old_keycode: pointer to the location where old keycode should be stored. 808 * 809 * This function is the default implementation of &input_dev.setkeycode() 810 * method. It is typically used when a driver does not provide its own 811 * implementation, but it is also exported so drivers can extend it. 812 * 813 * The function must be called with &input_dev.event_lock held. 814 * 815 * Return: 0 on success, or a negative error code on failure. 816 */ 817 int input_default_setkeycode(struct input_dev *dev, 818 const struct input_keymap_entry *ke, 819 unsigned int *old_keycode) 820 { 821 unsigned int index; 822 int error; 823 int i; 824 825 lockdep_assert_held(&dev->event_lock); 826 827 if (!dev->keycodesize) 828 return -EINVAL; 829 830 if (ke->flags & INPUT_KEYMAP_BY_INDEX) { 831 index = ke->index; 832 } else { 833 error = input_scancode_to_scalar(ke, &index); 834 if (error) 835 return error; 836 } 837 838 if (index >= dev->keycodemax) 839 return -EINVAL; 840 841 if (dev->keycodesize < sizeof(ke->keycode) && 842 (ke->keycode >> (dev->keycodesize * 8))) 843 return -EINVAL; 844 845 switch (dev->keycodesize) { 846 case 1: { 847 u8 *k = (u8 *)dev->keycode; 848 *old_keycode = k[index]; 849 k[index] = ke->keycode; 850 break; 851 } 852 case 2: { 853 u16 *k = (u16 *)dev->keycode; 854 *old_keycode = k[index]; 855 k[index] = ke->keycode; 856 break; 857 } 858 default: { 859 u32 *k = (u32 *)dev->keycode; 860 *old_keycode = k[index]; 861 k[index] = ke->keycode; 862 break; 863 } 864 } 865 866 if (*old_keycode <= KEY_MAX) { 867 __clear_bit(*old_keycode, dev->keybit); 868 for (i = 0; i < dev->keycodemax; i++) { 869 if (input_fetch_keycode(dev, i) == *old_keycode) { 870 __set_bit(*old_keycode, dev->keybit); 871 /* Setting the bit twice is useless, so break */ 872 break; 873 } 874 } 875 } 876 877 __set_bit(ke->keycode, dev->keybit); 878 return 0; 879 } 880 EXPORT_SYMBOL(input_default_setkeycode); 881 882 /** 883 * input_get_keycode - retrieve keycode currently mapped to a given scancode 884 * @dev: input device which keymap is being queried 885 * @ke: keymap entry 886 * 887 * This function should be called by anyone interested in retrieving current 888 * keymap. Presently evdev handlers use it. 889 */ 890 int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke) 891 { 892 guard(spinlock_irqsave)(&dev->event_lock); 893 894 return dev->getkeycode(dev, ke); 895 } 896 EXPORT_SYMBOL(input_get_keycode); 897 898 /** 899 * input_set_keycode - attribute a keycode to a given scancode 900 * @dev: input device which keymap is being updated 901 * @ke: new keymap entry 902 * 903 * This function should be called by anyone needing to update current 904 * keymap. Presently keyboard and evdev handlers use it. 905 */ 906 int input_set_keycode(struct input_dev *dev, 907 const struct input_keymap_entry *ke) 908 { 909 unsigned int old_keycode; 910 int error; 911 912 if (ke->keycode > KEY_MAX) 913 return -EINVAL; 914 915 guard(spinlock_irqsave)(&dev->event_lock); 916 917 error = dev->setkeycode(dev, ke, &old_keycode); 918 if (error) 919 return error; 920 921 /* Make sure KEY_RESERVED did not get enabled. */ 922 __clear_bit(KEY_RESERVED, dev->keybit); 923 924 /* 925 * Simulate keyup event if keycode is not present 926 * in the keymap anymore 927 */ 928 if (old_keycode > KEY_MAX) { 929 dev_warn(dev->dev.parent ?: &dev->dev, 930 "%s: got too big old keycode %#x\n", 931 __func__, old_keycode); 932 } else if (test_bit(EV_KEY, dev->evbit) && 933 !is_event_supported(old_keycode, dev->keybit, KEY_MAX) && 934 __test_and_clear_bit(old_keycode, dev->key)) { 935 /* 936 * We have to use input_event_dispose() here directly instead 937 * of input_handle_event() because the key we want to release 938 * here is considered no longer supported by the device and 939 * input_handle_event() will ignore it. 940 */ 941 input_event_dispose(dev, INPUT_PASS_TO_HANDLERS, 942 EV_KEY, old_keycode, 0); 943 input_event_dispose(dev, INPUT_PASS_TO_HANDLERS | INPUT_FLUSH, 944 EV_SYN, SYN_REPORT, 1); 945 } 946 947 return 0; 948 } 949 EXPORT_SYMBOL(input_set_keycode); 950 951 bool input_match_device_id(const struct input_dev *dev, 952 const struct input_device_id *id) 953 { 954 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) 955 if (id->bustype != dev->id.bustype) 956 return false; 957 958 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) 959 if (id->vendor != dev->id.vendor) 960 return false; 961 962 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) 963 if (id->product != dev->id.product) 964 return false; 965 966 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) 967 if (id->version != dev->id.version) 968 return false; 969 970 if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX) || 971 !bitmap_subset(id->keybit, dev->keybit, KEY_MAX) || 972 !bitmap_subset(id->relbit, dev->relbit, REL_MAX) || 973 !bitmap_subset(id->absbit, dev->absbit, ABS_MAX) || 974 !bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX) || 975 !bitmap_subset(id->ledbit, dev->ledbit, LED_MAX) || 976 !bitmap_subset(id->sndbit, dev->sndbit, SND_MAX) || 977 !bitmap_subset(id->ffbit, dev->ffbit, FF_MAX) || 978 !bitmap_subset(id->swbit, dev->swbit, SW_MAX) || 979 !bitmap_subset(id->propbit, dev->propbit, INPUT_PROP_MAX)) { 980 return false; 981 } 982 983 return true; 984 } 985 EXPORT_SYMBOL(input_match_device_id); 986 987 static const struct input_device_id *input_match_device(struct input_handler *handler, 988 struct input_dev *dev) 989 { 990 const struct input_device_id *id; 991 992 for (id = handler->id_table; id->flags; id++) { 993 if (input_match_device_id(dev, id) && 994 (!handler->match || handler->match(handler, dev))) { 995 return id; 996 } 997 } 998 999 return NULL; 1000 } 1001 1002 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) 1003 { 1004 const struct input_device_id *id; 1005 int error; 1006 1007 id = input_match_device(handler, dev); 1008 if (!id) 1009 return -ENODEV; 1010 1011 error = handler->connect(handler, dev, id); 1012 if (error && error != -ENODEV) 1013 pr_err("failed to attach handler %s to device %s, error: %d\n", 1014 handler->name, kobject_name(&dev->dev.kobj), error); 1015 1016 return error; 1017 } 1018 1019 #ifdef CONFIG_PROC_FS 1020 1021 static struct proc_dir_entry *proc_bus_input_dir; 1022 static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); 1023 static int input_devices_state; 1024 1025 static inline void input_wakeup_procfs_readers(void) 1026 { 1027 input_devices_state++; 1028 wake_up(&input_devices_poll_wait); 1029 } 1030 1031 struct input_seq_state { 1032 unsigned short pos; 1033 bool mutex_acquired; 1034 int input_devices_state; 1035 }; 1036 1037 static __poll_t input_proc_devices_poll(struct file *file, poll_table *wait) 1038 { 1039 struct seq_file *seq = file->private_data; 1040 struct input_seq_state *state = seq->private; 1041 1042 poll_wait(file, &input_devices_poll_wait, wait); 1043 if (state->input_devices_state != input_devices_state) { 1044 state->input_devices_state = input_devices_state; 1045 return EPOLLIN | EPOLLRDNORM; 1046 } 1047 1048 return 0; 1049 } 1050 1051 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) 1052 { 1053 struct input_seq_state *state = seq->private; 1054 int error; 1055 1056 error = mutex_lock_interruptible(&input_mutex); 1057 if (error) { 1058 state->mutex_acquired = false; 1059 return ERR_PTR(error); 1060 } 1061 1062 state->mutex_acquired = true; 1063 1064 return seq_list_start(&input_dev_list, *pos); 1065 } 1066 1067 static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1068 { 1069 return seq_list_next(v, &input_dev_list, pos); 1070 } 1071 1072 static void input_seq_stop(struct seq_file *seq, void *v) 1073 { 1074 struct input_seq_state *state = seq->private; 1075 1076 if (state->mutex_acquired) 1077 mutex_unlock(&input_mutex); 1078 } 1079 1080 static void input_seq_print_bitmap(struct seq_file *seq, const char *name, 1081 unsigned long *bitmap, int max) 1082 { 1083 int i; 1084 bool skip_empty = true; 1085 char buf[18]; 1086 1087 seq_printf(seq, "B: %s=", name); 1088 1089 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) { 1090 if (input_bits_to_string(buf, sizeof(buf), 1091 bitmap[i], skip_empty)) { 1092 skip_empty = false; 1093 seq_printf(seq, "%s%s", buf, i > 0 ? " " : ""); 1094 } 1095 } 1096 1097 /* 1098 * If no output was produced print a single 0. 1099 */ 1100 if (skip_empty) 1101 seq_putc(seq, '0'); 1102 1103 seq_putc(seq, '\n'); 1104 } 1105 1106 static int input_devices_seq_show(struct seq_file *seq, void *v) 1107 { 1108 struct input_dev *dev = container_of(v, struct input_dev, node); 1109 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); 1110 struct input_handle *handle; 1111 1112 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", 1113 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); 1114 1115 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); 1116 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); 1117 seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); 1118 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : ""); 1119 seq_puts(seq, "H: Handlers="); 1120 1121 list_for_each_entry(handle, &dev->h_list, d_node) 1122 seq_printf(seq, "%s ", handle->name); 1123 seq_putc(seq, '\n'); 1124 1125 input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX); 1126 1127 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX); 1128 if (test_bit(EV_KEY, dev->evbit)) 1129 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); 1130 if (test_bit(EV_REL, dev->evbit)) 1131 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX); 1132 if (test_bit(EV_ABS, dev->evbit)) 1133 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX); 1134 if (test_bit(EV_MSC, dev->evbit)) 1135 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX); 1136 if (test_bit(EV_LED, dev->evbit)) 1137 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX); 1138 if (test_bit(EV_SND, dev->evbit)) 1139 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX); 1140 if (test_bit(EV_FF, dev->evbit)) 1141 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX); 1142 if (test_bit(EV_SW, dev->evbit)) 1143 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX); 1144 1145 seq_putc(seq, '\n'); 1146 1147 kfree(path); 1148 return 0; 1149 } 1150 1151 static const struct seq_operations input_devices_seq_ops = { 1152 .start = input_devices_seq_start, 1153 .next = input_devices_seq_next, 1154 .stop = input_seq_stop, 1155 .show = input_devices_seq_show, 1156 }; 1157 1158 static int input_proc_devices_open(struct inode *inode, struct file *file) 1159 { 1160 return seq_open_private(file, &input_devices_seq_ops, 1161 sizeof(struct input_seq_state)); 1162 } 1163 1164 static const struct proc_ops input_devices_proc_ops = { 1165 .proc_open = input_proc_devices_open, 1166 .proc_poll = input_proc_devices_poll, 1167 .proc_read = seq_read, 1168 .proc_lseek = seq_lseek, 1169 .proc_release = seq_release_private, 1170 }; 1171 1172 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) 1173 { 1174 struct input_seq_state *state = seq->private; 1175 int error; 1176 1177 error = mutex_lock_interruptible(&input_mutex); 1178 if (error) { 1179 state->mutex_acquired = false; 1180 return ERR_PTR(error); 1181 } 1182 1183 state->mutex_acquired = true; 1184 state->pos = *pos; 1185 1186 return seq_list_start(&input_handler_list, *pos); 1187 } 1188 1189 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos) 1190 { 1191 struct input_seq_state *state = seq->private; 1192 1193 state->pos = *pos + 1; 1194 return seq_list_next(v, &input_handler_list, pos); 1195 } 1196 1197 static int input_handlers_seq_show(struct seq_file *seq, void *v) 1198 { 1199 struct input_handler *handler = container_of(v, struct input_handler, node); 1200 struct input_seq_state *state = seq->private; 1201 1202 seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name); 1203 if (handler->filter) 1204 seq_puts(seq, " (filter)"); 1205 if (handler->legacy_minors) 1206 seq_printf(seq, " Minor=%d", handler->minor); 1207 seq_putc(seq, '\n'); 1208 1209 return 0; 1210 } 1211 1212 static const struct seq_operations input_handlers_seq_ops = { 1213 .start = input_handlers_seq_start, 1214 .next = input_handlers_seq_next, 1215 .stop = input_seq_stop, 1216 .show = input_handlers_seq_show, 1217 }; 1218 1219 static int input_proc_handlers_open(struct inode *inode, struct file *file) 1220 { 1221 return seq_open_private(file, &input_handlers_seq_ops, 1222 sizeof(struct input_seq_state)); 1223 } 1224 1225 static const struct proc_ops input_handlers_proc_ops = { 1226 .proc_open = input_proc_handlers_open, 1227 .proc_read = seq_read, 1228 .proc_lseek = seq_lseek, 1229 .proc_release = seq_release_private, 1230 }; 1231 1232 static int __init input_proc_init(void) 1233 { 1234 struct proc_dir_entry *entry; 1235 1236 proc_bus_input_dir = proc_mkdir("bus/input", NULL); 1237 if (!proc_bus_input_dir) 1238 return -ENOMEM; 1239 1240 entry = proc_create("devices", 0, proc_bus_input_dir, 1241 &input_devices_proc_ops); 1242 if (!entry) 1243 goto fail1; 1244 1245 entry = proc_create("handlers", 0, proc_bus_input_dir, 1246 &input_handlers_proc_ops); 1247 if (!entry) 1248 goto fail2; 1249 1250 return 0; 1251 1252 fail2: remove_proc_entry("devices", proc_bus_input_dir); 1253 fail1: remove_proc_entry("bus/input", NULL); 1254 return -ENOMEM; 1255 } 1256 1257 static void input_proc_exit(void) 1258 { 1259 remove_proc_entry("devices", proc_bus_input_dir); 1260 remove_proc_entry("handlers", proc_bus_input_dir); 1261 remove_proc_entry("bus/input", NULL); 1262 } 1263 1264 #else /* !CONFIG_PROC_FS */ 1265 static inline void input_wakeup_procfs_readers(void) { } 1266 static inline int input_proc_init(void) { return 0; } 1267 static inline void input_proc_exit(void) { } 1268 #endif 1269 1270 #define INPUT_DEV_STRING_ATTR_SHOW(name) \ 1271 static ssize_t input_dev_show_##name(struct device *dev, \ 1272 struct device_attribute *attr, \ 1273 char *buf) \ 1274 { \ 1275 struct input_dev *input_dev = to_input_dev(dev); \ 1276 \ 1277 return sysfs_emit(buf, "%s\n", \ 1278 input_dev->name ? input_dev->name : ""); \ 1279 } \ 1280 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL) 1281 1282 INPUT_DEV_STRING_ATTR_SHOW(name); 1283 INPUT_DEV_STRING_ATTR_SHOW(phys); 1284 INPUT_DEV_STRING_ATTR_SHOW(uniq); 1285 1286 static int input_print_modalias_bits(char *buf, int size, 1287 char name, const unsigned long *bm, 1288 unsigned int min_bit, unsigned int max_bit) 1289 { 1290 int bit = min_bit; 1291 int len = 0; 1292 1293 len += snprintf(buf, max(size, 0), "%c", name); 1294 for_each_set_bit_from(bit, bm, max_bit) 1295 len += snprintf(buf + len, max(size - len, 0), "%X,", bit); 1296 return len; 1297 } 1298 1299 static int input_print_modalias_parts(char *buf, int size, int full_len, 1300 const struct input_dev *id) 1301 { 1302 int len, klen, remainder, space; 1303 1304 len = snprintf(buf, max(size, 0), 1305 "input:b%04Xv%04Xp%04Xe%04X-", 1306 id->id.bustype, id->id.vendor, 1307 id->id.product, id->id.version); 1308 1309 len += input_print_modalias_bits(buf + len, size - len, 1310 'e', id->evbit, 0, EV_MAX); 1311 1312 /* 1313 * Calculate the remaining space in the buffer making sure we 1314 * have place for the terminating 0. 1315 */ 1316 space = max(size - (len + 1), 0); 1317 1318 klen = input_print_modalias_bits(buf + len, size - len, 1319 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX); 1320 len += klen; 1321 1322 /* 1323 * If we have more data than we can fit in the buffer, check 1324 * if we can trim key data to fit in the rest. We will indicate 1325 * that key data is incomplete by adding "+" sign at the end, like 1326 * this: * "k1,2,3,45,+,". 1327 * 1328 * Note that we shortest key info (if present) is "k+," so we 1329 * can only try to trim if key data is longer than that. 1330 */ 1331 if (full_len && size < full_len + 1 && klen > 3) { 1332 remainder = full_len - len; 1333 /* 1334 * We can only trim if we have space for the remainder 1335 * and also for at least "k+," which is 3 more characters. 1336 */ 1337 if (remainder <= space - 3) { 1338 /* 1339 * We are guaranteed to have 'k' in the buffer, so 1340 * we need at least 3 additional bytes for storing 1341 * "+," in addition to the remainder. 1342 */ 1343 for (int i = size - 1 - remainder - 3; i >= 0; i--) { 1344 if (buf[i] == 'k' || buf[i] == ',') { 1345 strcpy(buf + i + 1, "+,"); 1346 len = i + 3; /* Not counting '\0' */ 1347 break; 1348 } 1349 } 1350 } 1351 } 1352 1353 len += input_print_modalias_bits(buf + len, size - len, 1354 'r', id->relbit, 0, REL_MAX); 1355 len += input_print_modalias_bits(buf + len, size - len, 1356 'a', id->absbit, 0, ABS_MAX); 1357 len += input_print_modalias_bits(buf + len, size - len, 1358 'm', id->mscbit, 0, MSC_MAX); 1359 len += input_print_modalias_bits(buf + len, size - len, 1360 'l', id->ledbit, 0, LED_MAX); 1361 len += input_print_modalias_bits(buf + len, size - len, 1362 's', id->sndbit, 0, SND_MAX); 1363 len += input_print_modalias_bits(buf + len, size - len, 1364 'f', id->ffbit, 0, FF_MAX); 1365 len += input_print_modalias_bits(buf + len, size - len, 1366 'w', id->swbit, 0, SW_MAX); 1367 1368 return len; 1369 } 1370 1371 static int input_print_modalias(char *buf, int size, const struct input_dev *id) 1372 { 1373 int full_len; 1374 1375 /* 1376 * Printing is done in 2 passes: first one figures out total length 1377 * needed for the modalias string, second one will try to trim key 1378 * data in case when buffer is too small for the entire modalias. 1379 * If the buffer is too small regardless, it will fill as much as it 1380 * can (without trimming key data) into the buffer and leave it to 1381 * the caller to figure out what to do with the result. 1382 */ 1383 full_len = input_print_modalias_parts(NULL, 0, 0, id); 1384 return input_print_modalias_parts(buf, size, full_len, id); 1385 } 1386 1387 static ssize_t input_dev_show_modalias(struct device *dev, 1388 struct device_attribute *attr, 1389 char *buf) 1390 { 1391 struct input_dev *id = to_input_dev(dev); 1392 ssize_t len; 1393 1394 len = input_print_modalias(buf, PAGE_SIZE, id); 1395 if (len < PAGE_SIZE - 2) 1396 len += snprintf(buf + len, PAGE_SIZE - len, "\n"); 1397 1398 return min_t(int, len, PAGE_SIZE); 1399 } 1400 static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); 1401 1402 static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap, 1403 int max, int add_cr); 1404 1405 static ssize_t input_dev_show_properties(struct device *dev, 1406 struct device_attribute *attr, 1407 char *buf) 1408 { 1409 struct input_dev *input_dev = to_input_dev(dev); 1410 int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit, 1411 INPUT_PROP_MAX, true); 1412 return min_t(int, len, PAGE_SIZE); 1413 } 1414 static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL); 1415 1416 static int input_inhibit_device(struct input_dev *dev); 1417 static int input_uninhibit_device(struct input_dev *dev); 1418 1419 static ssize_t inhibited_show(struct device *dev, 1420 struct device_attribute *attr, 1421 char *buf) 1422 { 1423 struct input_dev *input_dev = to_input_dev(dev); 1424 1425 return sysfs_emit(buf, "%d\n", input_dev->inhibited); 1426 } 1427 1428 static ssize_t inhibited_store(struct device *dev, 1429 struct device_attribute *attr, const char *buf, 1430 size_t len) 1431 { 1432 struct input_dev *input_dev = to_input_dev(dev); 1433 ssize_t rv; 1434 bool inhibited; 1435 1436 if (kstrtobool(buf, &inhibited)) 1437 return -EINVAL; 1438 1439 if (inhibited) 1440 rv = input_inhibit_device(input_dev); 1441 else 1442 rv = input_uninhibit_device(input_dev); 1443 1444 if (rv != 0) 1445 return rv; 1446 1447 return len; 1448 } 1449 1450 static DEVICE_ATTR_RW(inhibited); 1451 1452 static struct attribute *input_dev_attrs[] = { 1453 &dev_attr_name.attr, 1454 &dev_attr_phys.attr, 1455 &dev_attr_uniq.attr, 1456 &dev_attr_modalias.attr, 1457 &dev_attr_properties.attr, 1458 &dev_attr_inhibited.attr, 1459 NULL 1460 }; 1461 1462 static const struct attribute_group input_dev_attr_group = { 1463 .attrs = input_dev_attrs, 1464 }; 1465 1466 #define INPUT_DEV_ID_ATTR(name) \ 1467 static ssize_t input_dev_show_id_##name(struct device *dev, \ 1468 struct device_attribute *attr, \ 1469 char *buf) \ 1470 { \ 1471 struct input_dev *input_dev = to_input_dev(dev); \ 1472 return sysfs_emit(buf, "%04x\n", input_dev->id.name); \ 1473 } \ 1474 static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL) 1475 1476 INPUT_DEV_ID_ATTR(bustype); 1477 INPUT_DEV_ID_ATTR(vendor); 1478 INPUT_DEV_ID_ATTR(product); 1479 INPUT_DEV_ID_ATTR(version); 1480 1481 static struct attribute *input_dev_id_attrs[] = { 1482 &dev_attr_bustype.attr, 1483 &dev_attr_vendor.attr, 1484 &dev_attr_product.attr, 1485 &dev_attr_version.attr, 1486 NULL 1487 }; 1488 1489 static const struct attribute_group input_dev_id_attr_group = { 1490 .name = "id", 1491 .attrs = input_dev_id_attrs, 1492 }; 1493 1494 static int input_print_bitmap(char *buf, int buf_size, const unsigned long *bitmap, 1495 int max, int add_cr) 1496 { 1497 int i; 1498 int len = 0; 1499 bool skip_empty = true; 1500 1501 for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) { 1502 len += input_bits_to_string(buf + len, max(buf_size - len, 0), 1503 bitmap[i], skip_empty); 1504 if (len) { 1505 skip_empty = false; 1506 if (i > 0) 1507 len += snprintf(buf + len, max(buf_size - len, 0), " "); 1508 } 1509 } 1510 1511 /* 1512 * If no output was produced print a single 0. 1513 */ 1514 if (len == 0) 1515 len = snprintf(buf, buf_size, "%d", 0); 1516 1517 if (add_cr) 1518 len += snprintf(buf + len, max(buf_size - len, 0), "\n"); 1519 1520 return len; 1521 } 1522 1523 #define INPUT_DEV_CAP_ATTR(ev, bm) \ 1524 static ssize_t input_dev_show_cap_##bm(struct device *dev, \ 1525 struct device_attribute *attr, \ 1526 char *buf) \ 1527 { \ 1528 struct input_dev *input_dev = to_input_dev(dev); \ 1529 int len = input_print_bitmap(buf, PAGE_SIZE, \ 1530 input_dev->bm##bit, ev##_MAX, \ 1531 true); \ 1532 return min_t(int, len, PAGE_SIZE); \ 1533 } \ 1534 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL) 1535 1536 INPUT_DEV_CAP_ATTR(EV, ev); 1537 INPUT_DEV_CAP_ATTR(KEY, key); 1538 INPUT_DEV_CAP_ATTR(REL, rel); 1539 INPUT_DEV_CAP_ATTR(ABS, abs); 1540 INPUT_DEV_CAP_ATTR(MSC, msc); 1541 INPUT_DEV_CAP_ATTR(LED, led); 1542 INPUT_DEV_CAP_ATTR(SND, snd); 1543 INPUT_DEV_CAP_ATTR(FF, ff); 1544 INPUT_DEV_CAP_ATTR(SW, sw); 1545 1546 static struct attribute *input_dev_caps_attrs[] = { 1547 &dev_attr_ev.attr, 1548 &dev_attr_key.attr, 1549 &dev_attr_rel.attr, 1550 &dev_attr_abs.attr, 1551 &dev_attr_msc.attr, 1552 &dev_attr_led.attr, 1553 &dev_attr_snd.attr, 1554 &dev_attr_ff.attr, 1555 &dev_attr_sw.attr, 1556 NULL 1557 }; 1558 1559 static const struct attribute_group input_dev_caps_attr_group = { 1560 .name = "capabilities", 1561 .attrs = input_dev_caps_attrs, 1562 }; 1563 1564 static const struct attribute_group *input_dev_attr_groups[] = { 1565 &input_dev_attr_group, 1566 &input_dev_id_attr_group, 1567 &input_dev_caps_attr_group, 1568 &input_poller_attribute_group, 1569 NULL 1570 }; 1571 1572 static void input_dev_release(struct device *device) 1573 { 1574 struct input_dev *dev = to_input_dev(device); 1575 1576 input_ff_destroy(dev); 1577 input_mt_destroy_slots(dev); 1578 kfree(dev->poller); 1579 kfree(dev->absinfo); 1580 kfree(dev->vals); 1581 kfree(dev); 1582 1583 module_put(THIS_MODULE); 1584 } 1585 1586 /* 1587 * Input uevent interface - loading event handlers based on 1588 * device bitfields. 1589 */ 1590 static int input_add_uevent_bm_var(struct kobj_uevent_env *env, 1591 const char *name, const unsigned long *bitmap, int max) 1592 { 1593 int len; 1594 1595 if (add_uevent_var(env, "%s", name)) 1596 return -ENOMEM; 1597 1598 len = input_print_bitmap(&env->buf[env->buflen - 1], 1599 sizeof(env->buf) - env->buflen, 1600 bitmap, max, false); 1601 if (len >= (sizeof(env->buf) - env->buflen)) 1602 return -ENOMEM; 1603 1604 env->buflen += len; 1605 return 0; 1606 } 1607 1608 /* 1609 * This is a pretty gross hack. When building uevent data the driver core 1610 * may try adding more environment variables to kobj_uevent_env without 1611 * telling us, so we have no idea how much of the buffer we can use to 1612 * avoid overflows/-ENOMEM elsewhere. To work around this let's artificially 1613 * reduce amount of memory we will use for the modalias environment variable. 1614 * 1615 * The potential additions are: 1616 * 1617 * SEQNUM=18446744073709551615 - (%llu - 28 bytes) 1618 * HOME=/ (6 bytes) 1619 * PATH=/sbin:/bin:/usr/sbin:/usr/bin (34 bytes) 1620 * 1621 * 68 bytes total. Allow extra buffer - 96 bytes 1622 */ 1623 #define UEVENT_ENV_EXTRA_LEN 96 1624 1625 static int input_add_uevent_modalias_var(struct kobj_uevent_env *env, 1626 const struct input_dev *dev) 1627 { 1628 int len; 1629 1630 if (add_uevent_var(env, "MODALIAS=")) 1631 return -ENOMEM; 1632 1633 len = input_print_modalias(&env->buf[env->buflen - 1], 1634 (int)sizeof(env->buf) - env->buflen - 1635 UEVENT_ENV_EXTRA_LEN, 1636 dev); 1637 if (len >= ((int)sizeof(env->buf) - env->buflen - 1638 UEVENT_ENV_EXTRA_LEN)) 1639 return -ENOMEM; 1640 1641 env->buflen += len; 1642 return 0; 1643 } 1644 1645 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \ 1646 do { \ 1647 int err = add_uevent_var(env, fmt, val); \ 1648 if (err) \ 1649 return err; \ 1650 } while (0) 1651 1652 #define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \ 1653 do { \ 1654 int err = input_add_uevent_bm_var(env, name, bm, max); \ 1655 if (err) \ 1656 return err; \ 1657 } while (0) 1658 1659 #define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \ 1660 do { \ 1661 int err = input_add_uevent_modalias_var(env, dev); \ 1662 if (err) \ 1663 return err; \ 1664 } while (0) 1665 1666 static int input_dev_uevent(const struct device *device, struct kobj_uevent_env *env) 1667 { 1668 const struct input_dev *dev = to_input_dev(device); 1669 1670 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x", 1671 dev->id.bustype, dev->id.vendor, 1672 dev->id.product, dev->id.version); 1673 if (dev->name) 1674 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name); 1675 if (dev->phys) 1676 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys); 1677 if (dev->uniq) 1678 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); 1679 1680 INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX); 1681 1682 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX); 1683 if (test_bit(EV_KEY, dev->evbit)) 1684 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); 1685 if (test_bit(EV_REL, dev->evbit)) 1686 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX); 1687 if (test_bit(EV_ABS, dev->evbit)) 1688 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX); 1689 if (test_bit(EV_MSC, dev->evbit)) 1690 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX); 1691 if (test_bit(EV_LED, dev->evbit)) 1692 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX); 1693 if (test_bit(EV_SND, dev->evbit)) 1694 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX); 1695 if (test_bit(EV_FF, dev->evbit)) 1696 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX); 1697 if (test_bit(EV_SW, dev->evbit)) 1698 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX); 1699 1700 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev); 1701 1702 return 0; 1703 } 1704 1705 #define INPUT_DO_TOGGLE(dev, type, bits, on) \ 1706 do { \ 1707 int i; \ 1708 bool active; \ 1709 \ 1710 if (!test_bit(EV_##type, dev->evbit)) \ 1711 break; \ 1712 \ 1713 for_each_set_bit(i, dev->bits##bit, type##_CNT) { \ 1714 active = test_bit(i, dev->bits); \ 1715 if (!active && !on) \ 1716 continue; \ 1717 \ 1718 dev->event(dev, EV_##type, i, on ? active : 0); \ 1719 } \ 1720 } while (0) 1721 1722 static void input_dev_toggle(struct input_dev *dev, bool activate) 1723 { 1724 if (!dev->event) 1725 return; 1726 1727 INPUT_DO_TOGGLE(dev, LED, led, activate); 1728 INPUT_DO_TOGGLE(dev, SND, snd, activate); 1729 1730 if (activate && test_bit(EV_REP, dev->evbit)) { 1731 dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]); 1732 dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]); 1733 } 1734 } 1735 1736 /** 1737 * input_reset_device() - reset/restore the state of input device 1738 * @dev: input device whose state needs to be reset 1739 * 1740 * This function tries to reset the state of an opened input device and 1741 * bring internal state and state if the hardware in sync with each other. 1742 * We mark all keys as released, restore LED state, repeat rate, etc. 1743 */ 1744 void input_reset_device(struct input_dev *dev) 1745 { 1746 guard(mutex)(&dev->mutex); 1747 guard(spinlock_irqsave)(&dev->event_lock); 1748 1749 input_dev_toggle(dev, true); 1750 if (input_dev_release_keys(dev)) 1751 input_handle_event(dev, EV_SYN, SYN_REPORT, 1); 1752 } 1753 EXPORT_SYMBOL(input_reset_device); 1754 1755 static int input_inhibit_device(struct input_dev *dev) 1756 { 1757 guard(mutex)(&dev->mutex); 1758 1759 if (dev->inhibited) 1760 return 0; 1761 1762 if (dev->users) { 1763 if (dev->close) 1764 dev->close(dev); 1765 if (dev->poller) 1766 input_dev_poller_stop(dev->poller); 1767 } 1768 1769 scoped_guard(spinlock_irq, &dev->event_lock) { 1770 input_mt_release_slots(dev); 1771 input_dev_release_keys(dev); 1772 input_handle_event(dev, EV_SYN, SYN_REPORT, 1); 1773 input_dev_toggle(dev, false); 1774 } 1775 1776 dev->inhibited = true; 1777 1778 return 0; 1779 } 1780 1781 static int input_uninhibit_device(struct input_dev *dev) 1782 { 1783 int error; 1784 1785 guard(mutex)(&dev->mutex); 1786 1787 if (!dev->inhibited) 1788 return 0; 1789 1790 if (dev->users) { 1791 if (dev->open) { 1792 error = dev->open(dev); 1793 if (error) 1794 return error; 1795 } 1796 if (dev->poller) 1797 input_dev_poller_start(dev->poller); 1798 } 1799 1800 dev->inhibited = false; 1801 1802 scoped_guard(spinlock_irq, &dev->event_lock) 1803 input_dev_toggle(dev, true); 1804 1805 return 0; 1806 } 1807 1808 static int input_dev_suspend(struct device *dev) 1809 { 1810 struct input_dev *input_dev = to_input_dev(dev); 1811 1812 guard(spinlock_irq)(&input_dev->event_lock); 1813 1814 /* 1815 * Keys that are pressed now are unlikely to be 1816 * still pressed when we resume. 1817 */ 1818 if (input_dev_release_keys(input_dev)) 1819 input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1); 1820 1821 /* Turn off LEDs and sounds, if any are active. */ 1822 input_dev_toggle(input_dev, false); 1823 1824 return 0; 1825 } 1826 1827 static int input_dev_resume(struct device *dev) 1828 { 1829 struct input_dev *input_dev = to_input_dev(dev); 1830 1831 guard(spinlock_irq)(&input_dev->event_lock); 1832 1833 /* Restore state of LEDs and sounds, if any were active. */ 1834 input_dev_toggle(input_dev, true); 1835 1836 return 0; 1837 } 1838 1839 static int input_dev_freeze(struct device *dev) 1840 { 1841 struct input_dev *input_dev = to_input_dev(dev); 1842 1843 guard(spinlock_irq)(&input_dev->event_lock); 1844 1845 /* 1846 * Keys that are pressed now are unlikely to be 1847 * still pressed when we resume. 1848 */ 1849 if (input_dev_release_keys(input_dev)) 1850 input_handle_event(input_dev, EV_SYN, SYN_REPORT, 1); 1851 1852 return 0; 1853 } 1854 1855 static int input_dev_poweroff(struct device *dev) 1856 { 1857 struct input_dev *input_dev = to_input_dev(dev); 1858 1859 guard(spinlock_irq)(&input_dev->event_lock); 1860 1861 /* Turn off LEDs and sounds, if any are active. */ 1862 input_dev_toggle(input_dev, false); 1863 1864 return 0; 1865 } 1866 1867 static const struct dev_pm_ops input_dev_pm_ops = { 1868 .suspend = input_dev_suspend, 1869 .resume = input_dev_resume, 1870 .freeze = input_dev_freeze, 1871 .poweroff = input_dev_poweroff, 1872 .restore = input_dev_resume, 1873 }; 1874 1875 static const struct device_type input_dev_type = { 1876 .groups = input_dev_attr_groups, 1877 .release = input_dev_release, 1878 .uevent = input_dev_uevent, 1879 .pm = pm_sleep_ptr(&input_dev_pm_ops), 1880 }; 1881 1882 static char *input_devnode(const struct device *dev, umode_t *mode) 1883 { 1884 return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev)); 1885 } 1886 1887 const struct class input_class = { 1888 .name = "input", 1889 .devnode = input_devnode, 1890 }; 1891 EXPORT_SYMBOL_GPL(input_class); 1892 1893 /** 1894 * input_allocate_device - allocate memory for new input device 1895 * 1896 * Returns prepared struct input_dev or %NULL. 1897 * 1898 * NOTE: Use input_free_device() to free devices that have not been 1899 * registered; input_unregister_device() should be used for already 1900 * registered devices. 1901 */ 1902 struct input_dev *input_allocate_device(void) 1903 { 1904 static atomic_t input_no = ATOMIC_INIT(-1); 1905 struct input_dev *dev; 1906 1907 dev = kzalloc_obj(*dev); 1908 if (!dev) 1909 return NULL; 1910 1911 /* 1912 * Start with space for SYN_REPORT + 7 EV_KEY/EV_MSC events + 2 spare, 1913 * see input_estimate_events_per_packet(). We will tune the number 1914 * when we register the device. 1915 */ 1916 dev->max_vals = 10; 1917 dev->vals = kzalloc_objs(*dev->vals, dev->max_vals); 1918 if (!dev->vals) { 1919 kfree(dev); 1920 return NULL; 1921 } 1922 1923 mutex_init(&dev->mutex); 1924 spin_lock_init(&dev->event_lock); 1925 timer_setup(&dev->timer, NULL, 0); 1926 INIT_LIST_HEAD(&dev->h_list); 1927 INIT_LIST_HEAD(&dev->node); 1928 1929 dev->dev.type = &input_dev_type; 1930 dev->dev.class = &input_class; 1931 device_initialize(&dev->dev); 1932 /* 1933 * From this point on we can no longer simply "kfree(dev)", we need 1934 * to use input_free_device() so that device core properly frees its 1935 * resources associated with the input device. 1936 */ 1937 1938 dev_set_name(&dev->dev, "input%lu", 1939 (unsigned long)atomic_inc_return(&input_no)); 1940 1941 __module_get(THIS_MODULE); 1942 1943 return dev; 1944 } 1945 EXPORT_SYMBOL(input_allocate_device); 1946 1947 struct input_devres { 1948 struct input_dev *input; 1949 }; 1950 1951 static int devm_input_device_match(struct device *dev, void *res, void *data) 1952 { 1953 struct input_devres *devres = res; 1954 1955 return devres->input == data; 1956 } 1957 1958 static void devm_input_device_release(struct device *dev, void *res) 1959 { 1960 struct input_devres *devres = res; 1961 struct input_dev *input = devres->input; 1962 1963 dev_dbg(dev, "%s: dropping reference to %s\n", 1964 __func__, dev_name(&input->dev)); 1965 input_put_device(input); 1966 } 1967 1968 /** 1969 * devm_input_allocate_device - allocate managed input device 1970 * @dev: device owning the input device being created 1971 * 1972 * Returns prepared struct input_dev or %NULL. 1973 * 1974 * Managed input devices do not need to be explicitly unregistered or 1975 * freed as it will be done automatically when owner device unbinds from 1976 * its driver (or binding fails). Once managed input device is allocated, 1977 * it is ready to be set up and registered in the same fashion as regular 1978 * input device. There are no special devm_input_device_[un]register() 1979 * variants, regular ones work with both managed and unmanaged devices, 1980 * should you need them. In most cases however, managed input device need 1981 * not be explicitly unregistered or freed. 1982 * 1983 * NOTE: the owner device is set up as parent of input device and users 1984 * should not override it. 1985 */ 1986 struct input_dev *devm_input_allocate_device(struct device *dev) 1987 { 1988 struct input_dev *input; 1989 struct input_devres *devres; 1990 1991 devres = devres_alloc(devm_input_device_release, 1992 sizeof(*devres), GFP_KERNEL); 1993 if (!devres) 1994 return NULL; 1995 1996 input = input_allocate_device(); 1997 if (!input) { 1998 devres_free(devres); 1999 return NULL; 2000 } 2001 2002 input->dev.parent = dev; 2003 input->devres_managed = true; 2004 2005 devres->input = input; 2006 devres_add(dev, devres); 2007 2008 return input; 2009 } 2010 EXPORT_SYMBOL(devm_input_allocate_device); 2011 2012 /** 2013 * input_free_device - free memory occupied by input_dev structure 2014 * @dev: input device to free 2015 * 2016 * This function should only be used if input_register_device() 2017 * was not called yet or if it failed. Once device was registered 2018 * use input_unregister_device() and memory will be freed once last 2019 * reference to the device is dropped. 2020 * 2021 * Device should be allocated by input_allocate_device(). 2022 * 2023 * NOTE: If there are references to the input device then memory 2024 * will not be freed until last reference is dropped. 2025 */ 2026 void input_free_device(struct input_dev *dev) 2027 { 2028 if (dev) { 2029 if (dev->devres_managed) 2030 WARN_ON(devres_destroy(dev->dev.parent, 2031 devm_input_device_release, 2032 devm_input_device_match, 2033 dev)); 2034 input_put_device(dev); 2035 } 2036 } 2037 EXPORT_SYMBOL(input_free_device); 2038 2039 /** 2040 * input_set_timestamp - set timestamp for input events 2041 * @dev: input device to set timestamp for 2042 * @timestamp: the time at which the event has occurred 2043 * in CLOCK_MONOTONIC 2044 * 2045 * This function is intended to provide to the input system a more 2046 * accurate time of when an event actually occurred. The driver should 2047 * call this function as soon as a timestamp is acquired ensuring 2048 * clock conversions in input_set_timestamp are done correctly. 2049 * 2050 * The system entering suspend state between timestamp acquisition and 2051 * calling input_set_timestamp can result in inaccurate conversions. 2052 */ 2053 void input_set_timestamp(struct input_dev *dev, ktime_t timestamp) 2054 { 2055 dev->timestamp[INPUT_CLK_MONO] = timestamp; 2056 dev->timestamp[INPUT_CLK_REAL] = ktime_mono_to_real(timestamp); 2057 dev->timestamp[INPUT_CLK_BOOT] = ktime_mono_to_any(timestamp, 2058 TK_OFFS_BOOT); 2059 } 2060 EXPORT_SYMBOL(input_set_timestamp); 2061 2062 /** 2063 * input_get_timestamp - get timestamp for input events 2064 * @dev: input device to get timestamp from 2065 * 2066 * A valid timestamp is a timestamp of non-zero value. 2067 */ 2068 ktime_t *input_get_timestamp(struct input_dev *dev) 2069 { 2070 const ktime_t invalid_timestamp = ktime_set(0, 0); 2071 2072 if (!ktime_compare(dev->timestamp[INPUT_CLK_MONO], invalid_timestamp)) 2073 input_set_timestamp(dev, ktime_get()); 2074 2075 return dev->timestamp; 2076 } 2077 EXPORT_SYMBOL(input_get_timestamp); 2078 2079 /** 2080 * input_set_capability - mark device as capable of a certain event 2081 * @dev: device that is capable of emitting or accepting event 2082 * @type: type of the event (EV_KEY, EV_REL, etc...) 2083 * @code: event code 2084 * 2085 * In addition to setting up corresponding bit in appropriate capability 2086 * bitmap the function also adjusts dev->evbit. 2087 */ 2088 void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code) 2089 { 2090 if (type < EV_CNT && input_max_code[type] && 2091 code > input_max_code[type]) { 2092 pr_err("%s: invalid code %u for type %u\n", __func__, code, 2093 type); 2094 dump_stack(); 2095 return; 2096 } 2097 2098 switch (type) { 2099 case EV_KEY: 2100 __set_bit(code, dev->keybit); 2101 break; 2102 2103 case EV_REL: 2104 __set_bit(code, dev->relbit); 2105 break; 2106 2107 case EV_ABS: 2108 input_alloc_absinfo(dev); 2109 __set_bit(code, dev->absbit); 2110 break; 2111 2112 case EV_MSC: 2113 __set_bit(code, dev->mscbit); 2114 break; 2115 2116 case EV_SW: 2117 __set_bit(code, dev->swbit); 2118 break; 2119 2120 case EV_LED: 2121 __set_bit(code, dev->ledbit); 2122 break; 2123 2124 case EV_SND: 2125 __set_bit(code, dev->sndbit); 2126 break; 2127 2128 case EV_FF: 2129 __set_bit(code, dev->ffbit); 2130 break; 2131 2132 case EV_PWR: 2133 /* do nothing */ 2134 break; 2135 2136 default: 2137 pr_err("%s: unknown type %u (code %u)\n", __func__, type, code); 2138 dump_stack(); 2139 return; 2140 } 2141 2142 __set_bit(type, dev->evbit); 2143 } 2144 EXPORT_SYMBOL(input_set_capability); 2145 2146 static unsigned int input_estimate_events_per_packet(struct input_dev *dev) 2147 { 2148 int mt_slots; 2149 int i; 2150 unsigned int events; 2151 2152 if (dev->mt) { 2153 mt_slots = dev->mt->num_slots; 2154 } else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) { 2155 mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum - 2156 dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1; 2157 mt_slots = clamp(mt_slots, 2, 32); 2158 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) { 2159 mt_slots = 2; 2160 } else { 2161 mt_slots = 0; 2162 } 2163 2164 events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */ 2165 2166 if (test_bit(EV_ABS, dev->evbit)) 2167 for_each_set_bit(i, dev->absbit, ABS_CNT) 2168 events += input_is_mt_axis(i) ? mt_slots : 1; 2169 2170 if (test_bit(EV_REL, dev->evbit)) 2171 events += bitmap_weight(dev->relbit, REL_CNT); 2172 2173 /* Make room for KEY and MSC events */ 2174 events += 7; 2175 2176 return events; 2177 } 2178 2179 #define INPUT_CLEANSE_BITMASK(dev, type, bits) \ 2180 do { \ 2181 if (!test_bit(EV_##type, dev->evbit)) \ 2182 memset(dev->bits##bit, 0, \ 2183 sizeof(dev->bits##bit)); \ 2184 } while (0) 2185 2186 static void input_cleanse_bitmasks(struct input_dev *dev) 2187 { 2188 INPUT_CLEANSE_BITMASK(dev, KEY, key); 2189 INPUT_CLEANSE_BITMASK(dev, REL, rel); 2190 INPUT_CLEANSE_BITMASK(dev, ABS, abs); 2191 INPUT_CLEANSE_BITMASK(dev, MSC, msc); 2192 INPUT_CLEANSE_BITMASK(dev, LED, led); 2193 INPUT_CLEANSE_BITMASK(dev, SND, snd); 2194 INPUT_CLEANSE_BITMASK(dev, FF, ff); 2195 INPUT_CLEANSE_BITMASK(dev, SW, sw); 2196 } 2197 2198 static void __input_unregister_device(struct input_dev *dev) 2199 { 2200 struct input_handle *handle, *next; 2201 2202 input_disconnect_device(dev); 2203 2204 scoped_guard(mutex, &input_mutex) { 2205 list_for_each_entry_safe(handle, next, &dev->h_list, d_node) 2206 handle->handler->disconnect(handle); 2207 WARN_ON(!list_empty(&dev->h_list)); 2208 2209 timer_delete_sync(&dev->timer); 2210 list_del_init(&dev->node); 2211 2212 input_wakeup_procfs_readers(); 2213 } 2214 2215 device_del(&dev->dev); 2216 } 2217 2218 static void devm_input_device_unregister(struct device *dev, void *res) 2219 { 2220 struct input_devres *devres = res; 2221 struct input_dev *input = devres->input; 2222 2223 dev_dbg(dev, "%s: unregistering device %s\n", 2224 __func__, dev_name(&input->dev)); 2225 __input_unregister_device(input); 2226 } 2227 2228 /* 2229 * Generate software autorepeat event. Note that we take 2230 * dev->event_lock here to avoid racing with input_event 2231 * which may cause keys get "stuck". 2232 */ 2233 static void input_repeat_key(struct timer_list *t) 2234 { 2235 struct input_dev *dev = timer_container_of(dev, t, timer); 2236 2237 guard(spinlock_irqsave)(&dev->event_lock); 2238 2239 if (!dev->inhibited && 2240 test_bit(dev->repeat_key, dev->key) && 2241 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { 2242 2243 input_set_timestamp(dev, ktime_get()); 2244 input_handle_event(dev, EV_KEY, dev->repeat_key, 2); 2245 input_handle_event(dev, EV_SYN, SYN_REPORT, 1); 2246 2247 if (dev->rep[REP_PERIOD]) 2248 mod_timer(&dev->timer, jiffies + 2249 msecs_to_jiffies(dev->rep[REP_PERIOD])); 2250 } 2251 } 2252 2253 /** 2254 * input_enable_softrepeat - enable software autorepeat 2255 * @dev: input device 2256 * @delay: repeat delay 2257 * @period: repeat period 2258 * 2259 * Enable software autorepeat on the input device. 2260 */ 2261 void input_enable_softrepeat(struct input_dev *dev, int delay, int period) 2262 { 2263 dev->timer.function = input_repeat_key; 2264 dev->rep[REP_DELAY] = delay; 2265 dev->rep[REP_PERIOD] = period; 2266 } 2267 EXPORT_SYMBOL(input_enable_softrepeat); 2268 2269 bool input_device_enabled(struct input_dev *dev) 2270 { 2271 lockdep_assert_held(&dev->mutex); 2272 2273 return !dev->inhibited && dev->users > 0; 2274 } 2275 EXPORT_SYMBOL_GPL(input_device_enabled); 2276 2277 static int input_device_tune_vals(struct input_dev *dev) 2278 { 2279 struct input_value *vals; 2280 unsigned int packet_size; 2281 unsigned int max_vals; 2282 2283 packet_size = input_estimate_events_per_packet(dev); 2284 if (dev->hint_events_per_packet < packet_size) 2285 dev->hint_events_per_packet = packet_size; 2286 2287 max_vals = dev->hint_events_per_packet + 2; 2288 if (dev->max_vals >= max_vals) 2289 return 0; 2290 2291 vals = kcalloc(max_vals, sizeof(*vals), GFP_KERNEL); 2292 if (!vals) 2293 return -ENOMEM; 2294 2295 scoped_guard(spinlock_irq, &dev->event_lock) { 2296 dev->max_vals = max_vals; 2297 swap(dev->vals, vals); 2298 } 2299 2300 /* Because of swap() above, this frees the old vals memory */ 2301 kfree(vals); 2302 2303 return 0; 2304 } 2305 2306 /** 2307 * input_register_device - register device with input core 2308 * @dev: device to be registered 2309 * 2310 * This function registers device with input core. The device must be 2311 * allocated with input_allocate_device() and all it's capabilities 2312 * set up before registering. 2313 * If function fails the device must be freed with input_free_device(). 2314 * Once device has been successfully registered it can be unregistered 2315 * with input_unregister_device(); input_free_device() should not be 2316 * called in this case. 2317 * 2318 * Note that this function is also used to register managed input devices 2319 * (ones allocated with devm_input_allocate_device()). Such managed input 2320 * devices need not be explicitly unregistered or freed, their tear down 2321 * is controlled by the devres infrastructure. It is also worth noting 2322 * that tear down of managed input devices is internally a 2-step process: 2323 * registered managed input device is first unregistered, but stays in 2324 * memory and can still handle input_event() calls (although events will 2325 * not be delivered anywhere). The freeing of managed input device will 2326 * happen later, when devres stack is unwound to the point where device 2327 * allocation was made. 2328 */ 2329 int input_register_device(struct input_dev *dev) 2330 { 2331 struct input_devres *devres = NULL; 2332 struct input_handler *handler; 2333 const char *path; 2334 int error; 2335 2336 if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) { 2337 dev_err(&dev->dev, 2338 "Absolute device without dev->absinfo, refusing to register\n"); 2339 return -EINVAL; 2340 } 2341 2342 if (dev->devres_managed) { 2343 devres = devres_alloc(devm_input_device_unregister, 2344 sizeof(*devres), GFP_KERNEL); 2345 if (!devres) 2346 return -ENOMEM; 2347 2348 devres->input = dev; 2349 } 2350 2351 /* Every input device generates EV_SYN/SYN_REPORT events. */ 2352 __set_bit(EV_SYN, dev->evbit); 2353 2354 /* KEY_RESERVED is not supposed to be transmitted to userspace. */ 2355 __clear_bit(KEY_RESERVED, dev->keybit); 2356 2357 /* Make sure that bitmasks not mentioned in dev->evbit are clean. */ 2358 input_cleanse_bitmasks(dev); 2359 2360 error = input_device_tune_vals(dev); 2361 if (error) 2362 goto err_devres_free; 2363 2364 /* 2365 * If delay and period are pre-set by the driver, then autorepeating 2366 * is handled by the driver itself and we don't do it in input.c. 2367 */ 2368 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) 2369 input_enable_softrepeat(dev, 250, 33); 2370 2371 if (!dev->getkeycode) 2372 dev->getkeycode = input_default_getkeycode; 2373 2374 if (!dev->setkeycode) 2375 dev->setkeycode = input_default_setkeycode; 2376 2377 if (dev->poller) 2378 input_dev_poller_finalize(dev->poller); 2379 2380 error = device_add(&dev->dev); 2381 if (error) 2382 goto err_devres_free; 2383 2384 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); 2385 pr_info("%s as %s\n", 2386 dev->name ? dev->name : "Unspecified device", 2387 path ? path : "N/A"); 2388 kfree(path); 2389 2390 error = -EINTR; 2391 scoped_cond_guard(mutex_intr, goto err_device_del, &input_mutex) { 2392 list_add_tail(&dev->node, &input_dev_list); 2393 2394 list_for_each_entry(handler, &input_handler_list, node) 2395 input_attach_handler(dev, handler); 2396 2397 input_wakeup_procfs_readers(); 2398 } 2399 2400 if (dev->devres_managed) { 2401 dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n", 2402 __func__, dev_name(&dev->dev)); 2403 devres_add(dev->dev.parent, devres); 2404 } 2405 return 0; 2406 2407 err_device_del: 2408 device_del(&dev->dev); 2409 err_devres_free: 2410 devres_free(devres); 2411 return error; 2412 } 2413 EXPORT_SYMBOL(input_register_device); 2414 2415 /** 2416 * input_unregister_device - unregister previously registered device 2417 * @dev: device to be unregistered 2418 * 2419 * This function unregisters an input device. Once device is unregistered 2420 * the caller should not try to access it as it may get freed at any moment. 2421 */ 2422 void input_unregister_device(struct input_dev *dev) 2423 { 2424 if (dev->devres_managed) { 2425 WARN_ON(devres_destroy(dev->dev.parent, 2426 devm_input_device_unregister, 2427 devm_input_device_match, 2428 dev)); 2429 __input_unregister_device(dev); 2430 /* 2431 * We do not do input_put_device() here because it will be done 2432 * when 2nd devres fires up. 2433 */ 2434 } else { 2435 __input_unregister_device(dev); 2436 input_put_device(dev); 2437 } 2438 } 2439 EXPORT_SYMBOL(input_unregister_device); 2440 2441 static int input_handler_check_methods(const struct input_handler *handler) 2442 { 2443 int count = 0; 2444 2445 if (handler->filter) 2446 count++; 2447 if (handler->events) 2448 count++; 2449 if (handler->event) 2450 count++; 2451 2452 if (count > 1) { 2453 pr_err("%s: only one event processing method can be defined (%s)\n", 2454 __func__, handler->name); 2455 return -EINVAL; 2456 } 2457 2458 return 0; 2459 } 2460 2461 /** 2462 * input_register_handler - register a new input handler 2463 * @handler: handler to be registered 2464 * 2465 * This function registers a new input handler (interface) for input 2466 * devices in the system and attaches it to all input devices that 2467 * are compatible with the handler. 2468 */ 2469 int input_register_handler(struct input_handler *handler) 2470 { 2471 struct input_dev *dev; 2472 int error; 2473 2474 error = input_handler_check_methods(handler); 2475 if (error) 2476 return error; 2477 2478 scoped_cond_guard(mutex_intr, return -EINTR, &input_mutex) { 2479 INIT_LIST_HEAD(&handler->h_list); 2480 2481 list_add_tail(&handler->node, &input_handler_list); 2482 2483 list_for_each_entry(dev, &input_dev_list, node) 2484 input_attach_handler(dev, handler); 2485 2486 input_wakeup_procfs_readers(); 2487 } 2488 2489 return 0; 2490 } 2491 EXPORT_SYMBOL(input_register_handler); 2492 2493 /** 2494 * input_unregister_handler - unregisters an input handler 2495 * @handler: handler to be unregistered 2496 * 2497 * This function disconnects a handler from its input devices and 2498 * removes it from lists of known handlers. 2499 */ 2500 void input_unregister_handler(struct input_handler *handler) 2501 { 2502 struct input_handle *handle, *next; 2503 2504 guard(mutex)(&input_mutex); 2505 2506 list_for_each_entry_safe(handle, next, &handler->h_list, h_node) 2507 handler->disconnect(handle); 2508 WARN_ON(!list_empty(&handler->h_list)); 2509 2510 list_del_init(&handler->node); 2511 2512 input_wakeup_procfs_readers(); 2513 } 2514 EXPORT_SYMBOL(input_unregister_handler); 2515 2516 /** 2517 * input_handler_for_each_handle - handle iterator 2518 * @handler: input handler to iterate 2519 * @data: data for the callback 2520 * @fn: function to be called for each handle 2521 * 2522 * Iterate over @bus's list of devices, and call @fn for each, passing 2523 * it @data and stop when @fn returns a non-zero value. The function is 2524 * using RCU to traverse the list and therefore may be using in atomic 2525 * contexts. The @fn callback is invoked from RCU critical section and 2526 * thus must not sleep. 2527 */ 2528 int input_handler_for_each_handle(struct input_handler *handler, void *data, 2529 int (*fn)(struct input_handle *, void *)) 2530 { 2531 struct input_handle *handle; 2532 int retval; 2533 2534 guard(rcu)(); 2535 2536 list_for_each_entry_rcu(handle, &handler->h_list, h_node) { 2537 retval = fn(handle, data); 2538 if (retval) 2539 return retval; 2540 } 2541 2542 return 0; 2543 } 2544 EXPORT_SYMBOL(input_handler_for_each_handle); 2545 2546 /* 2547 * An implementation of input_handle's handle_events() method that simply 2548 * invokes handler->event() method for each event one by one. 2549 */ 2550 static unsigned int input_handle_events_default(struct input_handle *handle, 2551 struct input_value *vals, 2552 unsigned int count) 2553 { 2554 struct input_handler *handler = handle->handler; 2555 struct input_value *v; 2556 2557 for (v = vals; v != vals + count; v++) 2558 handler->event(handle, v->type, v->code, v->value); 2559 2560 return count; 2561 } 2562 2563 /* 2564 * An implementation of input_handle's handle_events() method that invokes 2565 * handler->filter() method for each event one by one and removes events 2566 * that were filtered out from the "vals" array. 2567 */ 2568 static unsigned int input_handle_events_filter(struct input_handle *handle, 2569 struct input_value *vals, 2570 unsigned int count) 2571 { 2572 struct input_handler *handler = handle->handler; 2573 struct input_value *end = vals; 2574 struct input_value *v; 2575 2576 for (v = vals; v != vals + count; v++) { 2577 if (handler->filter(handle, v->type, v->code, v->value)) 2578 continue; 2579 if (end != v) 2580 *end = *v; 2581 end++; 2582 } 2583 2584 return end - vals; 2585 } 2586 2587 /* 2588 * An implementation of input_handle's handle_events() method that does nothing. 2589 */ 2590 static unsigned int input_handle_events_null(struct input_handle *handle, 2591 struct input_value *vals, 2592 unsigned int count) 2593 { 2594 return count; 2595 } 2596 2597 /* 2598 * Sets up appropriate handle->event_handler based on the input_handler 2599 * associated with the handle. 2600 */ 2601 static void input_handle_setup_event_handler(struct input_handle *handle) 2602 { 2603 struct input_handler *handler = handle->handler; 2604 2605 if (handler->filter) 2606 handle->handle_events = input_handle_events_filter; 2607 else if (handler->event) 2608 handle->handle_events = input_handle_events_default; 2609 else if (handler->events) 2610 handle->handle_events = handler->events; 2611 else 2612 handle->handle_events = input_handle_events_null; 2613 } 2614 2615 /** 2616 * input_register_handle - register a new input handle 2617 * @handle: handle to register 2618 * 2619 * This function puts a new input handle onto device's 2620 * and handler's lists so that events can flow through 2621 * it once it is opened using input_open_device(). 2622 * 2623 * This function is supposed to be called from handler's 2624 * connect() method. 2625 */ 2626 int input_register_handle(struct input_handle *handle) 2627 { 2628 struct input_handler *handler = handle->handler; 2629 struct input_dev *dev = handle->dev; 2630 2631 input_handle_setup_event_handler(handle); 2632 /* 2633 * We take dev->mutex here to prevent race with 2634 * input_release_device(). 2635 */ 2636 scoped_cond_guard(mutex_intr, return -EINTR, &dev->mutex) { 2637 /* 2638 * Filters go to the head of the list, normal handlers 2639 * to the tail. 2640 */ 2641 if (handler->filter) 2642 list_add_rcu(&handle->d_node, &dev->h_list); 2643 else 2644 list_add_tail_rcu(&handle->d_node, &dev->h_list); 2645 } 2646 2647 /* 2648 * Since we are supposed to be called from ->connect() 2649 * which is mutually exclusive with ->disconnect() 2650 * we can't be racing with input_unregister_handle() 2651 * and so separate lock is not needed here. 2652 */ 2653 list_add_tail_rcu(&handle->h_node, &handler->h_list); 2654 2655 if (handler->start) 2656 handler->start(handle); 2657 2658 return 0; 2659 } 2660 EXPORT_SYMBOL(input_register_handle); 2661 2662 /** 2663 * input_unregister_handle - unregister an input handle 2664 * @handle: handle to unregister 2665 * 2666 * This function removes input handle from device's 2667 * and handler's lists. 2668 * 2669 * This function is supposed to be called from handler's 2670 * disconnect() method. 2671 */ 2672 void input_unregister_handle(struct input_handle *handle) 2673 { 2674 struct input_dev *dev = handle->dev; 2675 2676 list_del_rcu(&handle->h_node); 2677 2678 /* 2679 * Take dev->mutex to prevent race with input_release_device(). 2680 */ 2681 scoped_guard(mutex, &dev->mutex) 2682 list_del_rcu(&handle->d_node); 2683 2684 synchronize_rcu(); 2685 } 2686 EXPORT_SYMBOL(input_unregister_handle); 2687 2688 /** 2689 * input_get_new_minor - allocates a new input minor number 2690 * @legacy_base: beginning or the legacy range to be searched 2691 * @legacy_num: size of legacy range 2692 * @allow_dynamic: whether we can also take ID from the dynamic range 2693 * 2694 * This function allocates a new device minor for from input major namespace. 2695 * Caller can request legacy minor by specifying @legacy_base and @legacy_num 2696 * parameters and whether ID can be allocated from dynamic range if there are 2697 * no free IDs in legacy range. 2698 */ 2699 int input_get_new_minor(int legacy_base, unsigned int legacy_num, 2700 bool allow_dynamic) 2701 { 2702 /* 2703 * This function should be called from input handler's ->connect() 2704 * methods, which are serialized with input_mutex, so no additional 2705 * locking is needed here. 2706 */ 2707 if (legacy_base >= 0) { 2708 int minor = ida_alloc_range(&input_ida, legacy_base, 2709 legacy_base + legacy_num - 1, 2710 GFP_KERNEL); 2711 if (minor >= 0 || !allow_dynamic) 2712 return minor; 2713 } 2714 2715 return ida_alloc_range(&input_ida, INPUT_FIRST_DYNAMIC_DEV, 2716 INPUT_MAX_CHAR_DEVICES - 1, GFP_KERNEL); 2717 } 2718 EXPORT_SYMBOL(input_get_new_minor); 2719 2720 /** 2721 * input_free_minor - release previously allocated minor 2722 * @minor: minor to be released 2723 * 2724 * This function releases previously allocated input minor so that it can be 2725 * reused later. 2726 */ 2727 void input_free_minor(unsigned int minor) 2728 { 2729 ida_free(&input_ida, minor); 2730 } 2731 EXPORT_SYMBOL(input_free_minor); 2732 2733 static int __init input_init(void) 2734 { 2735 int err; 2736 2737 err = class_register(&input_class); 2738 if (err) { 2739 pr_err("unable to register input_dev class\n"); 2740 return err; 2741 } 2742 2743 err = input_proc_init(); 2744 if (err) 2745 goto fail1; 2746 2747 err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0), 2748 INPUT_MAX_CHAR_DEVICES, "input"); 2749 if (err) { 2750 pr_err("unable to register char major %d", INPUT_MAJOR); 2751 goto fail2; 2752 } 2753 2754 return 0; 2755 2756 fail2: input_proc_exit(); 2757 fail1: class_unregister(&input_class); 2758 return err; 2759 } 2760 2761 static void __exit input_exit(void) 2762 { 2763 input_proc_exit(); 2764 unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0), 2765 INPUT_MAX_CHAR_DEVICES); 2766 class_unregister(&input_class); 2767 } 2768 2769 subsys_initcall(input_init); 2770 module_exit(input_exit); 2771