1 /*- 2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org> 3 * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@cicgroup.ru> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #include "opt_evdev.h" 31 32 #include <sys/types.h> 33 #include <sys/systm.h> 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/conf.h> 38 #include <sys/malloc.h> 39 #include <sys/bitstring.h> 40 #include <sys/sysctl.h> 41 42 #include <dev/evdev/input.h> 43 #include <dev/evdev/evdev.h> 44 #include <dev/evdev/evdev_private.h> 45 46 #ifdef EVDEV_DEBUG 47 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args) 48 #else 49 #define debugf(evdev, fmt, args...) 50 #endif 51 52 #ifdef FEATURE 53 FEATURE(evdev, "Input event devices support"); 54 #endif 55 56 enum evdev_sparse_result 57 { 58 EV_SKIP_EVENT, /* Event value not changed */ 59 EV_REPORT_EVENT, /* Event value changed */ 60 EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */ 61 }; 62 63 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory"); 64 65 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX; 66 67 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args"); 68 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0, 69 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, " 70 "bit2 - mouse hardware, bit3 - keyboard hardware"); 71 72 static void evdev_start_repeat(struct evdev_dev *, uint16_t); 73 static void evdev_stop_repeat(struct evdev_dev *); 74 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); 75 76 static inline void 77 bit_change(bitstr_t *bitstr, int bit, int value) 78 { 79 if (value) 80 bit_set(bitstr, bit); 81 else 82 bit_clear(bitstr, bit); 83 } 84 85 struct evdev_dev * 86 evdev_alloc(void) 87 { 88 89 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO); 90 } 91 92 void 93 evdev_free(struct evdev_dev *evdev) 94 { 95 96 if (evdev != NULL && evdev->ev_cdev != NULL && 97 evdev->ev_cdev->si_drv1 != NULL) 98 evdev_unregister(evdev); 99 100 free(evdev, M_EVDEV); 101 } 102 103 static struct input_absinfo * 104 evdev_alloc_absinfo(void) 105 { 106 107 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV, 108 M_WAITOK | M_ZERO)); 109 } 110 111 static void 112 evdev_free_absinfo(struct input_absinfo *absinfo) 113 { 114 115 free(absinfo, M_EVDEV); 116 } 117 118 int 119 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size) 120 { 121 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT + 122 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT) 123 return (EINVAL); 124 125 evdev->ev_report_size = report_size; 126 return (0); 127 } 128 129 static size_t 130 evdev_estimate_report_size(struct evdev_dev *evdev) 131 { 132 size_t size = 0; 133 int res; 134 135 /* 136 * Keyboards generate one event per report but other devices with 137 * buttons like mouses can report events simultaneously 138 */ 139 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res); 140 if (res == -1) 141 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res); 142 size += (res != -1); 143 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res); 144 size += res; 145 146 /* All relative axes can be reported simultaneously */ 147 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res); 148 size += res; 149 150 /* 151 * All absolute axes can be reported simultaneously. 152 * Multitouch axes can be reported ABS_MT_SLOT times 153 */ 154 if (evdev->ev_absinfo != NULL) { 155 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res); 156 size += res; 157 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res); 158 if (res > 0) { 159 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */ 160 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 161 /* MT type B */ 162 size += res * MAXIMAL_MT_SLOT(evdev); 163 else 164 /* MT type A */ 165 size += res * (MAX_MT_REPORTS - 1); 166 } 167 } 168 169 /* All misc events can be reported simultaneously */ 170 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res); 171 size += res; 172 173 /* All leds can be reported simultaneously */ 174 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res); 175 size += res; 176 177 /* Assume other events are generated once per report */ 178 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res); 179 size += (res != -1); 180 181 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res); 182 size += (res != -1); 183 184 /* XXX: FF part is not implemented yet */ 185 186 size++; /* SYN_REPORT */ 187 return (size); 188 } 189 190 static int 191 evdev_register_common(struct evdev_dev *evdev) 192 { 193 int ret; 194 195 debugf(evdev, "%s: registered evdev provider: %s <%s>\n", 196 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial); 197 198 /* Initialize internal structures */ 199 LIST_INIT(&evdev->ev_clients); 200 201 if (evdev_event_supported(evdev, EV_REP) && 202 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 203 /* Initialize callout */ 204 callout_init_mtx(&evdev->ev_rep_callout, &evdev->ev_mtx, 0); 205 206 if (evdev->ev_rep[REP_DELAY] == 0 && 207 evdev->ev_rep[REP_PERIOD] == 0) { 208 /* Supply default values */ 209 evdev->ev_rep[REP_DELAY] = 250; 210 evdev->ev_rep[REP_PERIOD] = 33; 211 } 212 } 213 214 /* Initialize multitouch protocol type B states */ 215 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) && 216 evdev->ev_absinfo != NULL && MAXIMAL_MT_SLOT(evdev) > 0) 217 evdev_mt_init(evdev); 218 219 /* Estimate maximum report size */ 220 if (evdev->ev_report_size == 0) { 221 ret = evdev_set_report_size(evdev, 222 evdev_estimate_report_size(evdev)); 223 if (ret != 0) 224 goto bail_out; 225 } 226 227 /* Create char device node */ 228 ret = evdev_cdev_create(evdev); 229 bail_out: 230 return (ret); 231 } 232 233 int 234 evdev_register(struct evdev_dev *evdev) 235 { 236 int ret; 237 238 evdev->ev_lock_type = EV_LOCK_INTERNAL; 239 evdev->ev_lock = &evdev->ev_mtx; 240 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF); 241 242 ret = evdev_register_common(evdev); 243 if (ret != 0) 244 mtx_destroy(&evdev->ev_mtx); 245 246 return (ret); 247 } 248 249 int 250 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx) 251 { 252 253 evdev->ev_lock_type = EV_LOCK_MTX; 254 evdev->ev_lock = mtx; 255 return (evdev_register_common(evdev)); 256 } 257 258 int 259 evdev_unregister(struct evdev_dev *evdev) 260 { 261 struct evdev_client *client; 262 int ret; 263 debugf(evdev, "%s: unregistered evdev provider: %s\n", 264 evdev->ev_shortname, evdev->ev_name); 265 266 EVDEV_LOCK(evdev); 267 evdev->ev_cdev->si_drv1 = NULL; 268 /* Wake up sleepers */ 269 LIST_FOREACH(client, &evdev->ev_clients, ec_link) { 270 evdev_revoke_client(client); 271 evdev_dispose_client(evdev, client); 272 EVDEV_CLIENT_LOCKQ(client); 273 evdev_notify_event(client); 274 EVDEV_CLIENT_UNLOCKQ(client); 275 } 276 EVDEV_UNLOCK(evdev); 277 278 /* destroy_dev can sleep so release lock */ 279 ret = evdev_cdev_destroy(evdev); 280 evdev->ev_cdev = NULL; 281 if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL) 282 mtx_destroy(&evdev->ev_mtx); 283 284 evdev_free_absinfo(evdev->ev_absinfo); 285 evdev_mt_free(evdev); 286 287 return (ret); 288 } 289 290 inline void 291 evdev_set_name(struct evdev_dev *evdev, const char *name) 292 { 293 294 snprintf(evdev->ev_name, NAMELEN, "%s", name); 295 } 296 297 inline void 298 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor, 299 uint16_t product, uint16_t version) 300 { 301 302 evdev->ev_id = (struct input_id) { 303 .bustype = bustype, 304 .vendor = vendor, 305 .product = product, 306 .version = version 307 }; 308 } 309 310 inline void 311 evdev_set_phys(struct evdev_dev *evdev, const char *name) 312 { 313 314 snprintf(evdev->ev_shortname, NAMELEN, "%s", name); 315 } 316 317 inline void 318 evdev_set_serial(struct evdev_dev *evdev, const char *serial) 319 { 320 321 snprintf(evdev->ev_serial, NAMELEN, "%s", serial); 322 } 323 324 inline void 325 evdev_set_methods(struct evdev_dev *evdev, void *softc, 326 const struct evdev_methods *methods) 327 { 328 329 evdev->ev_methods = methods; 330 evdev->ev_softc = softc; 331 } 332 333 inline void 334 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop) 335 { 336 337 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property")); 338 bit_set(evdev->ev_prop_flags, prop); 339 } 340 341 inline void 342 evdev_support_event(struct evdev_dev *evdev, uint16_t type) 343 { 344 345 KASSERT(type < EV_CNT, ("invalid evdev event property")); 346 bit_set(evdev->ev_type_flags, type); 347 } 348 349 inline void 350 evdev_support_key(struct evdev_dev *evdev, uint16_t code) 351 { 352 353 KASSERT(code < KEY_CNT, ("invalid evdev key property")); 354 bit_set(evdev->ev_key_flags, code); 355 } 356 357 inline void 358 evdev_support_rel(struct evdev_dev *evdev, uint16_t code) 359 { 360 361 KASSERT(code < REL_CNT, ("invalid evdev rel property")); 362 bit_set(evdev->ev_rel_flags, code); 363 } 364 365 inline void 366 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t value, 367 int32_t minimum, int32_t maximum, int32_t fuzz, int32_t flat, 368 int32_t resolution) 369 { 370 struct input_absinfo absinfo; 371 372 KASSERT(code < ABS_CNT, ("invalid evdev abs property")); 373 374 absinfo = (struct input_absinfo) { 375 .value = value, 376 .minimum = minimum, 377 .maximum = maximum, 378 .fuzz = fuzz, 379 .flat = flat, 380 .resolution = resolution, 381 }; 382 evdev_set_abs_bit(evdev, code); 383 evdev_set_absinfo(evdev, code, &absinfo); 384 } 385 386 inline void 387 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code) 388 { 389 390 KASSERT(code < ABS_CNT, ("invalid evdev abs property")); 391 if (evdev->ev_absinfo == NULL) 392 evdev->ev_absinfo = evdev_alloc_absinfo(); 393 bit_set(evdev->ev_abs_flags, code); 394 } 395 396 inline void 397 evdev_support_msc(struct evdev_dev *evdev, uint16_t code) 398 { 399 400 KASSERT(code < MSC_CNT, ("invalid evdev msc property")); 401 bit_set(evdev->ev_msc_flags, code); 402 } 403 404 405 inline void 406 evdev_support_led(struct evdev_dev *evdev, uint16_t code) 407 { 408 409 KASSERT(code < LED_CNT, ("invalid evdev led property")); 410 bit_set(evdev->ev_led_flags, code); 411 } 412 413 inline void 414 evdev_support_snd(struct evdev_dev *evdev, uint16_t code) 415 { 416 417 KASSERT(code < SND_CNT, ("invalid evdev snd property")); 418 bit_set(evdev->ev_snd_flags, code); 419 } 420 421 inline void 422 evdev_support_sw(struct evdev_dev *evdev, uint16_t code) 423 { 424 425 KASSERT(code < SW_CNT, ("invalid evdev sw property")); 426 bit_set(evdev->ev_sw_flags, code); 427 } 428 429 bool 430 evdev_event_supported(struct evdev_dev *evdev, uint16_t type) 431 { 432 433 KASSERT(type < EV_CNT, ("invalid evdev event property")); 434 return (bit_test(evdev->ev_type_flags, type)); 435 } 436 437 inline void 438 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis, 439 struct input_absinfo *absinfo) 440 { 441 442 KASSERT(axis < ABS_CNT, ("invalid evdev abs property")); 443 444 if (axis == ABS_MT_SLOT && 445 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS)) 446 return; 447 448 if (evdev->ev_absinfo == NULL) 449 evdev->ev_absinfo = evdev_alloc_absinfo(); 450 451 if (axis == ABS_MT_SLOT) 452 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum; 453 else 454 memcpy(&evdev->ev_absinfo[axis], absinfo, 455 sizeof(struct input_absinfo)); 456 } 457 458 inline void 459 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value) 460 { 461 462 KASSERT(property < REP_CNT, ("invalid evdev repeat property")); 463 evdev->ev_rep[property] = value; 464 } 465 466 inline void 467 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag) 468 { 469 470 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property")); 471 bit_set(evdev->ev_flags, flag); 472 } 473 474 static int 475 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 476 int32_t value) 477 { 478 479 if (type >= EV_CNT) 480 return (EINVAL); 481 482 /* Allow SYN events implicitly */ 483 if (type != EV_SYN && !evdev_event_supported(evdev, type)) 484 return (EINVAL); 485 486 switch (type) { 487 case EV_SYN: 488 if (code >= SYN_CNT) 489 return (EINVAL); 490 break; 491 492 case EV_KEY: 493 if (code >= KEY_CNT) 494 return (EINVAL); 495 if (!bit_test(evdev->ev_key_flags, code)) 496 return (EINVAL); 497 break; 498 499 case EV_REL: 500 if (code >= REL_CNT) 501 return (EINVAL); 502 if (!bit_test(evdev->ev_rel_flags, code)) 503 return (EINVAL); 504 break; 505 506 case EV_ABS: 507 if (code >= ABS_CNT) 508 return (EINVAL); 509 if (!bit_test(evdev->ev_abs_flags, code)) 510 return (EINVAL); 511 if (code == ABS_MT_SLOT && 512 (value < 0 || value > MAXIMAL_MT_SLOT(evdev))) 513 return (EINVAL); 514 if (ABS_IS_MT(code) && evdev->ev_mt == NULL && 515 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 516 return (EINVAL); 517 break; 518 519 case EV_MSC: 520 if (code >= MSC_CNT) 521 return (EINVAL); 522 if (!bit_test(evdev->ev_msc_flags, code)) 523 return (EINVAL); 524 break; 525 526 case EV_LED: 527 if (code >= LED_CNT) 528 return (EINVAL); 529 if (!bit_test(evdev->ev_led_flags, code)) 530 return (EINVAL); 531 break; 532 533 case EV_SND: 534 if (code >= SND_CNT) 535 return (EINVAL); 536 if (!bit_test(evdev->ev_snd_flags, code)) 537 return (EINVAL); 538 break; 539 540 case EV_SW: 541 if (code >= SW_CNT) 542 return (EINVAL); 543 if (!bit_test(evdev->ev_sw_flags, code)) 544 return (EINVAL); 545 break; 546 547 case EV_REP: 548 if (code >= REP_CNT) 549 return (EINVAL); 550 break; 551 552 default: 553 return (EINVAL); 554 } 555 556 return (0); 557 } 558 559 static void 560 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 561 int32_t *value) 562 { 563 564 EVDEV_LOCK_ASSERT(evdev); 565 566 switch (type) { 567 case EV_KEY: 568 if (!evdev_event_supported(evdev, EV_REP)) 569 break; 570 571 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 572 /* Detect driver key repeats. */ 573 if (bit_test(evdev->ev_key_states, code) && 574 *value == KEY_EVENT_DOWN) 575 *value = KEY_EVENT_REPEAT; 576 } else { 577 /* Start/stop callout for evdev repeats */ 578 if (bit_test(evdev->ev_key_states, code) == !*value) { 579 if (*value == KEY_EVENT_DOWN) 580 evdev_start_repeat(evdev, code); 581 else 582 evdev_stop_repeat(evdev); 583 } 584 } 585 break; 586 587 case EV_ABS: 588 /* TBD: implement fuzz */ 589 break; 590 } 591 } 592 593 static enum evdev_sparse_result 594 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 595 int32_t value) 596 { 597 int32_t last_mt_slot; 598 599 EVDEV_LOCK_ASSERT(evdev); 600 601 /* 602 * For certain event types, update device state bits 603 * and convert level reporting to edge reporting 604 */ 605 switch (type) { 606 case EV_KEY: 607 switch (value) { 608 case KEY_EVENT_UP: 609 case KEY_EVENT_DOWN: 610 if (bit_test(evdev->ev_key_states, code) == value) 611 return (EV_SKIP_EVENT); 612 bit_change(evdev->ev_key_states, code, value); 613 break; 614 615 case KEY_EVENT_REPEAT: 616 if (bit_test(evdev->ev_key_states, code) == 0 || 617 !evdev_event_supported(evdev, EV_REP)) 618 return (EV_SKIP_EVENT); 619 break; 620 621 default: 622 return (EV_SKIP_EVENT); 623 } 624 break; 625 626 case EV_LED: 627 if (bit_test(evdev->ev_led_states, code) == value) 628 return (EV_SKIP_EVENT); 629 bit_change(evdev->ev_led_states, code, value); 630 break; 631 632 case EV_SND: 633 if (bit_test(evdev->ev_snd_states, code) == value) 634 return (EV_SKIP_EVENT); 635 bit_change(evdev->ev_snd_states, code, value); 636 break; 637 638 case EV_SW: 639 if (bit_test(evdev->ev_sw_states, code) == value) 640 return (EV_SKIP_EVENT); 641 bit_change(evdev->ev_sw_states, code, value); 642 break; 643 644 case EV_REP: 645 if (evdev->ev_rep[code] == value) 646 return (EV_SKIP_EVENT); 647 evdev_set_repeat_params(evdev, code, value); 648 break; 649 650 case EV_REL: 651 if (value == 0) 652 return (EV_SKIP_EVENT); 653 break; 654 655 /* For EV_ABS, save last value in absinfo and ev_mt_states */ 656 case EV_ABS: 657 switch (code) { 658 case ABS_MT_SLOT: 659 /* Postpone ABS_MT_SLOT till next event */ 660 evdev_set_last_mt_slot(evdev, value); 661 return (EV_SKIP_EVENT); 662 663 case ABS_MT_FIRST ... ABS_MT_LAST: 664 /* Pass MT protocol type A events as is */ 665 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 666 break; 667 /* Don`t repeat MT protocol type B events */ 668 last_mt_slot = evdev_get_last_mt_slot(evdev); 669 if (evdev_get_mt_value(evdev, last_mt_slot, code) 670 == value) 671 return (EV_SKIP_EVENT); 672 evdev_set_mt_value(evdev, last_mt_slot, code, value); 673 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) { 674 CURRENT_MT_SLOT(evdev) = last_mt_slot; 675 evdev->ev_report_opened = true; 676 return (EV_REPORT_MT_SLOT); 677 } 678 break; 679 680 default: 681 if (evdev->ev_absinfo[code].value == value) 682 return (EV_SKIP_EVENT); 683 evdev->ev_absinfo[code].value = value; 684 } 685 break; 686 687 case EV_SYN: 688 if (code == SYN_REPORT) { 689 /* Count empty reports as well as non empty */ 690 evdev->ev_report_count++; 691 /* Skip empty reports */ 692 if (!evdev->ev_report_opened) 693 return (EV_SKIP_EVENT); 694 evdev->ev_report_opened = false; 695 return (EV_REPORT_EVENT); 696 } 697 break; 698 } 699 700 evdev->ev_report_opened = true; 701 return (EV_REPORT_EVENT); 702 } 703 704 static void 705 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 706 int32_t value) 707 { 708 struct evdev_client *client; 709 710 debugf(evdev, "%s pushed event %d/%d/%d", 711 evdev->ev_shortname, type, code, value); 712 713 EVDEV_LOCK_ASSERT(evdev); 714 715 /* Propagate event through all clients */ 716 LIST_FOREACH(client, &evdev->ev_clients, ec_link) { 717 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client) 718 continue; 719 720 EVDEV_CLIENT_LOCKQ(client); 721 evdev_client_push(client, type, code, value); 722 if (type == EV_SYN && code == SYN_REPORT) 723 evdev_notify_event(client); 724 EVDEV_CLIENT_UNLOCKQ(client); 725 } 726 727 evdev->ev_event_count++; 728 } 729 730 void 731 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 732 int32_t value) 733 { 734 enum evdev_sparse_result sparse; 735 736 EVDEV_LOCK_ASSERT(evdev); 737 738 sparse = evdev_sparse_event(evdev, type, code, value); 739 switch (sparse) { 740 case EV_REPORT_MT_SLOT: 741 /* report postponed ABS_MT_SLOT */ 742 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT, 743 CURRENT_MT_SLOT(evdev)); 744 /* FALLTHROUGH */ 745 case EV_REPORT_EVENT: 746 evdev_propagate_event(evdev, type, code, value); 747 /* FALLTHROUGH */ 748 case EV_SKIP_EVENT: 749 break; 750 } 751 } 752 753 int 754 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 755 int32_t value) 756 { 757 758 if (evdev->ev_lock_type != EV_LOCK_INTERNAL) 759 EVDEV_LOCK_ASSERT(evdev); 760 761 if (evdev_check_event(evdev, type, code, value) != 0) 762 return (EINVAL); 763 764 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 765 EVDEV_LOCK(evdev); 766 evdev_modify_event(evdev, type, code, &value); 767 if (type == EV_SYN && code == SYN_REPORT && 768 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL)) 769 evdev_send_mt_autorel(evdev); 770 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened && 771 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) 772 evdev_send_mt_compat(evdev); 773 evdev_send_event(evdev, type, code, value); 774 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 775 EVDEV_UNLOCK(evdev); 776 777 return (0); 778 } 779 780 int 781 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 782 int32_t value) 783 { 784 int ret = 0; 785 786 switch (type) { 787 case EV_REP: 788 /* evdev repeats should not be processed by hardware driver */ 789 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) 790 goto push; 791 /* FALLTHROUGH */ 792 case EV_LED: 793 case EV_MSC: 794 case EV_SND: 795 case EV_FF: 796 if (evdev->ev_methods != NULL && 797 evdev->ev_methods->ev_event != NULL) 798 evdev->ev_methods->ev_event(evdev, evdev->ev_softc, 799 type, code, value); 800 /* 801 * Leds and driver repeats should be reported in ev_event 802 * method body to interoperate with kbdmux states and rates 803 * propagation so both ways (ioctl and evdev) of changing it 804 * will produce only one evdev event report to client. 805 */ 806 if (type == EV_LED || type == EV_REP) 807 break; 808 /* FALLTHROUGH */ 809 case EV_SYN: 810 case EV_KEY: 811 case EV_REL: 812 case EV_ABS: 813 case EV_SW: 814 push: 815 ret = evdev_push_event(evdev, type, code, value); 816 break; 817 818 default: 819 ret = EINVAL; 820 } 821 822 return (ret); 823 } 824 825 int 826 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client) 827 { 828 int ret = 0; 829 830 debugf(evdev, "adding new client for device %s", evdev->ev_shortname); 831 832 EVDEV_LOCK_ASSERT(evdev); 833 834 if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL && 835 evdev->ev_methods->ev_open != NULL) { 836 debugf(evdev, "calling ev_open() on device %s", 837 evdev->ev_shortname); 838 ret = evdev->ev_methods->ev_open(evdev, evdev->ev_softc); 839 } 840 if (ret == 0) 841 LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link); 842 return (ret); 843 } 844 845 void 846 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client) 847 { 848 debugf(evdev, "removing client for device %s", evdev->ev_shortname); 849 850 EVDEV_LOCK_ASSERT(evdev); 851 852 LIST_REMOVE(client, ec_link); 853 if (LIST_EMPTY(&evdev->ev_clients)) { 854 if (evdev->ev_methods != NULL && 855 evdev->ev_methods->ev_close != NULL) 856 evdev->ev_methods->ev_close(evdev, evdev->ev_softc); 857 if (evdev_event_supported(evdev, EV_REP) && 858 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) 859 evdev_stop_repeat(evdev); 860 } 861 evdev_release_client(evdev, client); 862 } 863 864 int 865 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client) 866 { 867 868 EVDEV_LOCK_ASSERT(evdev); 869 870 if (evdev->ev_grabber != NULL) 871 return (EBUSY); 872 873 evdev->ev_grabber = client; 874 875 return (0); 876 } 877 878 int 879 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client) 880 { 881 882 EVDEV_LOCK_ASSERT(evdev); 883 884 if (evdev->ev_grabber != client) 885 return (EINVAL); 886 887 evdev->ev_grabber = NULL; 888 889 return (0); 890 } 891 892 static void 893 evdev_repeat_callout(void *arg) 894 { 895 struct evdev_dev *evdev = (struct evdev_dev *)arg; 896 897 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT); 898 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); 899 900 if (evdev->ev_rep[REP_PERIOD]) 901 callout_reset(&evdev->ev_rep_callout, 902 evdev->ev_rep[REP_PERIOD] * hz / 1000, 903 evdev_repeat_callout, evdev); 904 else 905 evdev->ev_rep_key = KEY_RESERVED; 906 } 907 908 static void 909 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key) 910 { 911 912 EVDEV_LOCK_ASSERT(evdev); 913 914 if (evdev->ev_rep[REP_DELAY]) { 915 evdev->ev_rep_key = key; 916 callout_reset(&evdev->ev_rep_callout, 917 evdev->ev_rep[REP_DELAY] * hz / 1000, 918 evdev_repeat_callout, evdev); 919 } 920 } 921 922 static void 923 evdev_stop_repeat(struct evdev_dev *evdev) 924 { 925 926 EVDEV_LOCK_ASSERT(evdev); 927 928 if (evdev->ev_rep_key != KEY_RESERVED) { 929 callout_stop(&evdev->ev_rep_callout); 930 evdev->ev_rep_key = KEY_RESERVED; 931 } 932 } 933 934 MODULE_VERSION(evdev, 1); 935