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