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