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