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