1 /*- 2 * Copyright (c) 2014 Jakub Wojciech Klama <[email protected]> 3 * Copyright (c) 2015-2016 Vladimir Kondratyev <[email protected]> 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/ck.h> 35 #include <sys/conf.h> 36 #include <sys/epoch.h> 37 #include <sys/kdb.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/proc.h> 42 #include <sys/sx.h> 43 #include <sys/sysctl.h> 44 #include <sys/systm.h> 45 46 #include <dev/evdev/evdev.h> 47 #include <dev/evdev/evdev_private.h> 48 #include <dev/evdev/input.h> 49 50 #ifdef EVDEV_DEBUG 51 #define debugf(evdev, fmt, args...) printf("evdev: " fmt "\n", ##args) 52 #else 53 #define debugf(evdev, fmt, args...) 54 #endif 55 56 #ifdef FEATURE 57 FEATURE(evdev, "Input event devices support"); 58 #ifdef EVDEV_SUPPORT 59 FEATURE(evdev_support, "Evdev support in hybrid drivers"); 60 #endif 61 #endif 62 63 enum evdev_sparse_result 64 { 65 EV_SKIP_EVENT, /* Event value not changed */ 66 EV_REPORT_EVENT, /* Event value changed */ 67 EV_REPORT_MT_SLOT, /* Event value and MT slot number changed */ 68 }; 69 70 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory"); 71 72 /* adb keyboard driver used on powerpc does not support evdev yet */ 73 #if defined(__powerpc__) && !defined(__powerpc64__) 74 int evdev_rcpt_mask = EVDEV_RCPT_KBDMUX | EVDEV_RCPT_HW_MOUSE; 75 #else 76 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD; 77 #endif 78 int evdev_sysmouse_t_axis = 0; 79 80 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 81 "Evdev args"); 82 #ifdef EVDEV_SUPPORT 83 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RWTUN, &evdev_rcpt_mask, 0, 84 "Who is receiving events: bit0 - sysmouse, bit1 - kbdmux, " 85 "bit2 - mouse hardware, bit3 - keyboard hardware"); 86 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RWTUN, 87 &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm"); 88 #endif 89 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 90 "Evdev input devices"); 91 92 static void evdev_start_repeat(struct evdev_dev *, uint16_t); 93 static void evdev_stop_repeat(struct evdev_dev *); 94 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t); 95 96 struct evdev_dev * 97 evdev_alloc(void) 98 { 99 100 return malloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO); 101 } 102 103 void 104 evdev_free(struct evdev_dev *evdev) 105 { 106 107 if (evdev != NULL && evdev->ev_cdev != NULL && 108 evdev->ev_cdev->si_drv1 != NULL) 109 evdev_unregister(evdev); 110 111 free(evdev, M_EVDEV); 112 } 113 114 static struct input_absinfo * 115 evdev_alloc_absinfo(void) 116 { 117 118 return (malloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV, 119 M_WAITOK | M_ZERO)); 120 } 121 122 static void 123 evdev_free_absinfo(struct input_absinfo *absinfo) 124 { 125 126 free(absinfo, M_EVDEV); 127 } 128 129 int 130 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size) 131 { 132 if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT + 133 MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT) 134 return (EINVAL); 135 136 evdev->ev_report_size = report_size; 137 return (0); 138 } 139 140 static size_t 141 evdev_estimate_report_size(struct evdev_dev *evdev) 142 { 143 size_t size = 0; 144 int res; 145 146 /* 147 * Keyboards generate one event per report but other devices with 148 * buttons like mouses can report events simultaneously 149 */ 150 bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res); 151 if (res == -1) 152 bit_ffs(evdev->ev_key_flags, BTN_MISC, &res); 153 size += (res != -1); 154 bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res); 155 size += res; 156 157 /* All relative axes can be reported simultaneously */ 158 bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res); 159 size += res; 160 161 /* 162 * All absolute axes can be reported simultaneously. 163 * Multitouch axes can be reported ABS_MT_SLOT times 164 */ 165 if (evdev->ev_absinfo != NULL) { 166 bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res); 167 size += res; 168 bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res); 169 if (res > 0) { 170 res++; /* ABS_MT_SLOT or SYN_MT_REPORT */ 171 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 172 /* MT type B */ 173 size += res * MAXIMAL_MT_SLOT(evdev); 174 else 175 /* MT type A */ 176 size += res * (MAX_MT_REPORTS - 1); 177 } 178 } 179 180 /* All misc events can be reported simultaneously */ 181 bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res); 182 size += res; 183 184 /* All leds can be reported simultaneously */ 185 bit_count(evdev->ev_led_flags, 0, LED_CNT, &res); 186 size += res; 187 188 /* Assume other events are generated once per report */ 189 bit_ffs(evdev->ev_snd_flags, SND_CNT, &res); 190 size += (res != -1); 191 192 bit_ffs(evdev->ev_sw_flags, SW_CNT, &res); 193 size += (res != -1); 194 195 /* XXX: FF part is not implemented yet */ 196 197 size++; /* SYN_REPORT */ 198 return (size); 199 } 200 201 static void 202 evdev_sysctl_create(struct evdev_dev *evdev) 203 { 204 struct sysctl_oid *ev_sysctl_tree; 205 char ev_unit_str[8]; 206 207 snprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit); 208 sysctl_ctx_init(&evdev->ev_sysctl_ctx); 209 210 ev_sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&evdev->ev_sysctl_ctx, 211 SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO, 212 ev_unit_str, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "", 213 "device index"); 214 215 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, 216 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD, 217 evdev->ev_name, 0, 218 "Input device name"); 219 220 SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx, 221 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD, 222 &evdev->ev_id, input_id, 223 "Input device identification"); 224 225 /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */ 226 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, 227 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD, 228 evdev->ev_shortname, 0, 229 "Input device short name"); 230 231 /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */ 232 SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx, 233 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD, 234 evdev->ev_serial, 0, 235 "Input device unique number"); 236 237 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 238 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD, 239 evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "", 240 "Input device properties"); 241 242 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 243 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD, 244 evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "", 245 "Input device supported events types"); 246 247 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 248 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD, 249 evdev->ev_key_flags, sizeof(evdev->ev_key_flags), 250 "", "Input device supported keys"); 251 252 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 253 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD, 254 evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "", 255 "Input device supported relative events"); 256 257 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 258 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD, 259 evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "", 260 "Input device supported absolute events"); 261 262 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 263 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD, 264 evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "", 265 "Input device supported miscellaneous events"); 266 267 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 268 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD, 269 evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "", 270 "Input device supported LED events"); 271 272 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 273 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD, 274 evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "", 275 "Input device supported sound events"); 276 277 SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx, 278 SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD, 279 evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "", 280 "Input device supported switch events"); 281 } 282 283 static int 284 evdev_register_common(struct evdev_dev *evdev) 285 { 286 int ret; 287 288 debugf(evdev, "%s: registered evdev provider: %s <%s>\n", 289 evdev->ev_shortname, evdev->ev_name, evdev->ev_serial); 290 291 /* Initialize internal structures */ 292 CK_SLIST_INIT(&evdev->ev_clients); 293 sx_init(&evdev->ev_list_lock, "evsx"); 294 295 if (evdev_event_supported(evdev, EV_REP) && 296 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 297 /* Initialize callout */ 298 callout_init_mtx(&evdev->ev_rep_callout, 299 evdev->ev_state_lock, 0); 300 301 if (evdev->ev_rep[REP_DELAY] == 0 && 302 evdev->ev_rep[REP_PERIOD] == 0) { 303 /* Supply default values */ 304 evdev->ev_rep[REP_DELAY] = 250; 305 evdev->ev_rep[REP_PERIOD] = 33; 306 } 307 } 308 309 /* Initialize multitouch protocol type B states */ 310 if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 311 evdev_mt_init(evdev); 312 313 /* Estimate maximum report size */ 314 if (evdev->ev_report_size == 0) { 315 ret = evdev_set_report_size(evdev, 316 evdev_estimate_report_size(evdev)); 317 if (ret != 0) 318 goto bail_out; 319 } 320 321 /* Create char device node */ 322 ret = evdev_cdev_create(evdev); 323 if (ret != 0) 324 goto bail_out; 325 326 /* Create sysctls (for device enumeration without /dev/input access rights) */ 327 evdev_sysctl_create(evdev); 328 329 bail_out: 330 if (ret != 0) 331 sx_destroy(&evdev->ev_list_lock); 332 return (ret); 333 } 334 335 int 336 evdev_register(struct evdev_dev *evdev) 337 { 338 int ret; 339 340 if (bit_test(evdev->ev_flags, EVDEV_FLAG_EXT_EPOCH)) 341 evdev->ev_lock_type = EV_LOCK_EXT_EPOCH; 342 else 343 evdev->ev_lock_type = EV_LOCK_INTERNAL; 344 evdev->ev_state_lock = &evdev->ev_mtx; 345 mtx_init(&evdev->ev_mtx, "evmtx", NULL, MTX_DEF); 346 347 ret = evdev_register_common(evdev); 348 if (ret != 0) 349 mtx_destroy(&evdev->ev_mtx); 350 351 return (ret); 352 } 353 354 int 355 evdev_register_mtx(struct evdev_dev *evdev, struct mtx *mtx) 356 { 357 358 evdev->ev_lock_type = EV_LOCK_MTX; 359 evdev->ev_state_lock = mtx; 360 return (evdev_register_common(evdev)); 361 } 362 363 int 364 evdev_unregister(struct evdev_dev *evdev) 365 { 366 struct evdev_client *client, *tmp; 367 int ret; 368 debugf(evdev, "%s: unregistered evdev provider: %s\n", 369 evdev->ev_shortname, evdev->ev_name); 370 371 sysctl_ctx_free(&evdev->ev_sysctl_ctx); 372 373 EVDEV_LIST_LOCK(evdev); 374 evdev->ev_cdev->si_drv1 = NULL; 375 /* Wake up sleepers */ 376 CK_SLIST_FOREACH_SAFE(client, &evdev->ev_clients, ec_link, tmp) { 377 evdev_revoke_client(client); 378 evdev_dispose_client(evdev, client); 379 EVDEV_CLIENT_LOCKQ(client); 380 evdev_notify_event(client); 381 EVDEV_CLIENT_UNLOCKQ(client); 382 } 383 EVDEV_LIST_UNLOCK(evdev); 384 385 /* release lock to avoid deadlock with evdev_dtor */ 386 ret = evdev_cdev_destroy(evdev); 387 evdev->ev_cdev = NULL; 388 sx_destroy(&evdev->ev_list_lock); 389 if (ret == 0 && evdev->ev_lock_type != EV_LOCK_MTX) 390 mtx_destroy(&evdev->ev_mtx); 391 392 evdev_free_absinfo(evdev->ev_absinfo); 393 evdev_mt_free(evdev); 394 395 return (ret); 396 } 397 398 inline void 399 evdev_set_name(struct evdev_dev *evdev, const char *name) 400 { 401 402 snprintf(evdev->ev_name, NAMELEN, "%s", name); 403 } 404 405 inline void 406 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor, 407 uint16_t product, uint16_t version) 408 { 409 410 evdev->ev_id = (struct input_id) { 411 .bustype = bustype, 412 .vendor = vendor, 413 .product = product, 414 .version = version 415 }; 416 } 417 418 inline void 419 evdev_set_phys(struct evdev_dev *evdev, const char *name) 420 { 421 422 snprintf(evdev->ev_shortname, NAMELEN, "%s", name); 423 } 424 425 inline void 426 evdev_set_serial(struct evdev_dev *evdev, const char *serial) 427 { 428 429 snprintf(evdev->ev_serial, NAMELEN, "%s", serial); 430 } 431 432 inline void 433 evdev_set_methods(struct evdev_dev *evdev, void *softc, 434 const struct evdev_methods *methods) 435 { 436 437 evdev->ev_methods = methods; 438 evdev->ev_softc = softc; 439 } 440 441 inline void * 442 evdev_get_softc(struct evdev_dev *evdev) 443 { 444 445 return (evdev->ev_softc); 446 } 447 448 inline void 449 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop) 450 { 451 452 KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property")); 453 bit_set(evdev->ev_prop_flags, prop); 454 } 455 456 inline void 457 evdev_support_event(struct evdev_dev *evdev, uint16_t type) 458 { 459 460 KASSERT(type < EV_CNT, ("invalid evdev event property")); 461 bit_set(evdev->ev_type_flags, type); 462 } 463 464 inline void 465 evdev_support_key(struct evdev_dev *evdev, uint16_t code) 466 { 467 468 KASSERT(code < KEY_CNT, ("invalid evdev key property")); 469 bit_set(evdev->ev_key_flags, code); 470 } 471 472 inline void 473 evdev_support_rel(struct evdev_dev *evdev, uint16_t code) 474 { 475 476 KASSERT(code < REL_CNT, ("invalid evdev rel property")); 477 bit_set(evdev->ev_rel_flags, code); 478 } 479 480 inline void 481 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum, 482 int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution) 483 { 484 struct input_absinfo absinfo; 485 486 KASSERT(code < ABS_CNT, ("invalid evdev abs property")); 487 488 absinfo = (struct input_absinfo) { 489 .value = 0, 490 .minimum = minimum, 491 .maximum = maximum, 492 .fuzz = fuzz, 493 .flat = flat, 494 .resolution = resolution, 495 }; 496 evdev_set_abs_bit(evdev, code); 497 evdev_set_absinfo(evdev, code, &absinfo); 498 } 499 500 inline void 501 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code) 502 { 503 504 KASSERT(code < ABS_CNT, ("invalid evdev abs property")); 505 if (evdev->ev_absinfo == NULL) 506 evdev->ev_absinfo = evdev_alloc_absinfo(); 507 bit_set(evdev->ev_abs_flags, code); 508 } 509 510 inline void 511 evdev_support_msc(struct evdev_dev *evdev, uint16_t code) 512 { 513 514 KASSERT(code < MSC_CNT, ("invalid evdev msc property")); 515 bit_set(evdev->ev_msc_flags, code); 516 } 517 518 519 inline void 520 evdev_support_led(struct evdev_dev *evdev, uint16_t code) 521 { 522 523 KASSERT(code < LED_CNT, ("invalid evdev led property")); 524 bit_set(evdev->ev_led_flags, code); 525 } 526 527 inline void 528 evdev_support_snd(struct evdev_dev *evdev, uint16_t code) 529 { 530 531 KASSERT(code < SND_CNT, ("invalid evdev snd property")); 532 bit_set(evdev->ev_snd_flags, code); 533 } 534 535 inline void 536 evdev_support_sw(struct evdev_dev *evdev, uint16_t code) 537 { 538 539 KASSERT(code < SW_CNT, ("invalid evdev sw property")); 540 bit_set(evdev->ev_sw_flags, code); 541 } 542 543 bool 544 evdev_event_supported(struct evdev_dev *evdev, uint16_t type) 545 { 546 547 KASSERT(type < EV_CNT, ("invalid evdev event property")); 548 return (bit_test(evdev->ev_type_flags, type)); 549 } 550 551 inline void 552 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis, 553 struct input_absinfo *absinfo) 554 { 555 556 KASSERT(axis < ABS_CNT, ("invalid evdev abs property")); 557 558 if (axis == ABS_MT_SLOT && 559 (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS)) 560 return; 561 562 if (evdev->ev_absinfo == NULL) 563 evdev->ev_absinfo = evdev_alloc_absinfo(); 564 565 if (axis == ABS_MT_SLOT) 566 evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum; 567 else 568 memcpy(&evdev->ev_absinfo[axis], absinfo, 569 sizeof(struct input_absinfo)); 570 } 571 572 inline void 573 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value) 574 { 575 576 KASSERT(property < REP_CNT, ("invalid evdev repeat property")); 577 evdev->ev_rep[property] = value; 578 } 579 580 inline void 581 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag) 582 { 583 584 KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property")); 585 bit_set(evdev->ev_flags, flag); 586 } 587 588 static int 589 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 590 int32_t value) 591 { 592 593 if (type >= EV_CNT) 594 return (EINVAL); 595 596 /* Allow SYN events implicitly */ 597 if (type != EV_SYN && !evdev_event_supported(evdev, type)) 598 return (EINVAL); 599 600 switch (type) { 601 case EV_SYN: 602 if (code >= SYN_CNT) 603 return (EINVAL); 604 break; 605 606 case EV_KEY: 607 if (code >= KEY_CNT) 608 return (EINVAL); 609 if (!bit_test(evdev->ev_key_flags, code)) 610 return (EINVAL); 611 break; 612 613 case EV_REL: 614 if (code >= REL_CNT) 615 return (EINVAL); 616 if (!bit_test(evdev->ev_rel_flags, code)) 617 return (EINVAL); 618 break; 619 620 case EV_ABS: 621 if (code >= ABS_CNT) 622 return (EINVAL); 623 if (!bit_test(evdev->ev_abs_flags, code)) 624 return (EINVAL); 625 if (code == ABS_MT_SLOT && 626 (value < 0 || value > MAXIMAL_MT_SLOT(evdev))) 627 return (EINVAL); 628 if (ABS_IS_MT(code) && evdev->ev_mt == NULL && 629 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 630 return (EINVAL); 631 break; 632 633 case EV_MSC: 634 if (code >= MSC_CNT) 635 return (EINVAL); 636 if (!bit_test(evdev->ev_msc_flags, code)) 637 return (EINVAL); 638 break; 639 640 case EV_LED: 641 if (code >= LED_CNT) 642 return (EINVAL); 643 if (!bit_test(evdev->ev_led_flags, code)) 644 return (EINVAL); 645 break; 646 647 case EV_SND: 648 if (code >= SND_CNT) 649 return (EINVAL); 650 if (!bit_test(evdev->ev_snd_flags, code)) 651 return (EINVAL); 652 break; 653 654 case EV_SW: 655 if (code >= SW_CNT) 656 return (EINVAL); 657 if (!bit_test(evdev->ev_sw_flags, code)) 658 return (EINVAL); 659 break; 660 661 case EV_REP: 662 if (code >= REP_CNT) 663 return (EINVAL); 664 break; 665 666 default: 667 return (EINVAL); 668 } 669 670 return (0); 671 } 672 673 static void 674 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 675 int32_t *value) 676 { 677 int32_t fuzz, old_value, abs_change; 678 679 EVDEV_LOCK_ASSERT(evdev); 680 681 switch (type) { 682 case EV_KEY: 683 if (!evdev_event_supported(evdev, EV_REP)) 684 break; 685 686 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 687 /* Detect driver key repeats. */ 688 if (bit_test(evdev->ev_key_states, code) && 689 *value == KEY_EVENT_DOWN) 690 *value = KEY_EVENT_REPEAT; 691 } else { 692 /* Start/stop callout for evdev repeats */ 693 if (bit_test(evdev->ev_key_states, code) == !*value && 694 !CK_SLIST_EMPTY(&evdev->ev_clients)) { 695 if (*value == KEY_EVENT_DOWN) 696 evdev_start_repeat(evdev, code); 697 else 698 evdev_stop_repeat(evdev); 699 } 700 } 701 break; 702 703 case EV_ABS: 704 if (code == ABS_MT_SLOT) 705 break; 706 else if (!ABS_IS_MT(code)) 707 old_value = evdev->ev_absinfo[code].value; 708 else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 709 /* Pass MT protocol type A events as is */ 710 break; 711 else if (code == ABS_MT_TRACKING_ID) { 712 *value = evdev_mt_reassign_id(evdev, 713 evdev_mt_get_last_slot(evdev), *value); 714 break; 715 } else 716 old_value = evdev_mt_get_value(evdev, 717 evdev_mt_get_last_slot(evdev), code); 718 719 fuzz = evdev->ev_absinfo[code].fuzz; 720 if (fuzz == 0) 721 break; 722 723 abs_change = abs(*value - old_value); 724 if (abs_change < fuzz / 2) 725 *value = old_value; 726 else if (abs_change < fuzz) 727 *value = (old_value * 3 + *value) / 4; 728 else if (abs_change < fuzz * 2) 729 *value = (old_value + *value) / 2; 730 break; 731 } 732 } 733 734 static enum evdev_sparse_result 735 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 736 int32_t value) 737 { 738 int32_t last_mt_slot; 739 740 EVDEV_LOCK_ASSERT(evdev); 741 742 /* 743 * For certain event types, update device state bits 744 * and convert level reporting to edge reporting 745 */ 746 switch (type) { 747 case EV_KEY: 748 switch (value) { 749 case KEY_EVENT_UP: 750 case KEY_EVENT_DOWN: 751 if (bit_test(evdev->ev_key_states, code) == value) 752 return (EV_SKIP_EVENT); 753 bit_change(evdev->ev_key_states, code, value); 754 break; 755 756 case KEY_EVENT_REPEAT: 757 if (bit_test(evdev->ev_key_states, code) == 0 || 758 !evdev_event_supported(evdev, EV_REP)) 759 return (EV_SKIP_EVENT); 760 break; 761 762 default: 763 return (EV_SKIP_EVENT); 764 } 765 break; 766 767 case EV_LED: 768 if (bit_test(evdev->ev_led_states, code) == value) 769 return (EV_SKIP_EVENT); 770 bit_change(evdev->ev_led_states, code, value); 771 break; 772 773 case EV_SND: 774 bit_change(evdev->ev_snd_states, code, value); 775 break; 776 777 case EV_SW: 778 if (bit_test(evdev->ev_sw_states, code) == value) 779 return (EV_SKIP_EVENT); 780 bit_change(evdev->ev_sw_states, code, value); 781 break; 782 783 case EV_REP: 784 if (evdev->ev_rep[code] == value) 785 return (EV_SKIP_EVENT); 786 evdev_set_repeat_params(evdev, code, value); 787 break; 788 789 case EV_REL: 790 if (value == 0) 791 return (EV_SKIP_EVENT); 792 break; 793 794 /* For EV_ABS, save last value in absinfo and ev_mt_states */ 795 case EV_ABS: 796 switch (code) { 797 case ABS_MT_SLOT: 798 /* Postpone ABS_MT_SLOT till next event */ 799 evdev_mt_set_last_slot(evdev, value); 800 return (EV_SKIP_EVENT); 801 802 case ABS_MT_FIRST ... ABS_MT_LAST: 803 /* Pass MT protocol type A events as is */ 804 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 805 break; 806 /* Don`t repeat MT protocol type B events */ 807 last_mt_slot = evdev_mt_get_last_slot(evdev); 808 if (evdev_mt_get_value(evdev, last_mt_slot, code) 809 == value) 810 return (EV_SKIP_EVENT); 811 evdev_mt_set_value(evdev, last_mt_slot, code, value); 812 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) { 813 CURRENT_MT_SLOT(evdev) = last_mt_slot; 814 evdev->ev_report_opened = true; 815 return (EV_REPORT_MT_SLOT); 816 } 817 break; 818 819 default: 820 if (evdev->ev_absinfo[code].value == value) 821 return (EV_SKIP_EVENT); 822 evdev->ev_absinfo[code].value = value; 823 } 824 break; 825 826 case EV_SYN: 827 if (code == SYN_REPORT) { 828 /* Count empty reports as well as non empty */ 829 evdev->ev_report_count++; 830 /* Skip empty reports */ 831 if (!evdev->ev_report_opened) 832 return (EV_SKIP_EVENT); 833 evdev->ev_report_opened = false; 834 return (EV_REPORT_EVENT); 835 } 836 break; 837 } 838 839 evdev->ev_report_opened = true; 840 return (EV_REPORT_EVENT); 841 } 842 843 static void 844 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 845 int32_t value) 846 { 847 struct epoch_tracker et; 848 struct evdev_client *client; 849 850 debugf(evdev, "%s pushed event %d/%d/%d", 851 evdev->ev_shortname, type, code, value); 852 853 EVDEV_LOCK_ASSERT(evdev); 854 855 /* Propagate event through all clients */ 856 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 857 epoch_enter_preempt(INPUT_EPOCH, &et); 858 859 KASSERT( 860 evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0, 861 ("Input epoch has not been entered\n")); 862 863 CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) { 864 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client) 865 continue; 866 867 EVDEV_CLIENT_LOCKQ(client); 868 evdev_client_push(client, type, code, value); 869 if (type == EV_SYN && code == SYN_REPORT) 870 evdev_notify_event(client); 871 EVDEV_CLIENT_UNLOCKQ(client); 872 } 873 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 874 epoch_exit_preempt(INPUT_EPOCH, &et); 875 876 evdev->ev_event_count++; 877 } 878 879 void 880 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 881 int32_t value) 882 { 883 enum evdev_sparse_result sparse; 884 885 EVDEV_LOCK_ASSERT(evdev); 886 887 evdev_modify_event(evdev, type, code, &value); 888 sparse = evdev_sparse_event(evdev, type, code, value); 889 switch (sparse) { 890 case EV_REPORT_MT_SLOT: 891 /* report postponed ABS_MT_SLOT */ 892 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT, 893 CURRENT_MT_SLOT(evdev)); 894 /* FALLTHROUGH */ 895 case EV_REPORT_EVENT: 896 evdev_propagate_event(evdev, type, code, value); 897 /* FALLTHROUGH */ 898 case EV_SKIP_EVENT: 899 break; 900 } 901 } 902 903 void 904 evdev_restore_after_kdb(struct evdev_dev *evdev) 905 { 906 int code; 907 908 EVDEV_LOCK_ASSERT(evdev); 909 910 /* Report postponed leds */ 911 bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code) 912 evdev_send_event(evdev, EV_LED, code, 913 !bit_test(evdev->ev_led_states, code)); 914 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX); 915 916 /* Release stuck keys (CTRL + ALT + ESC) */ 917 evdev_stop_repeat(evdev); 918 bit_foreach(evdev->ev_key_states, KEY_CNT, code) 919 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP); 920 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); 921 } 922 923 int 924 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 925 int32_t value) 926 { 927 928 if (evdev_check_event(evdev, type, code, value) != 0) 929 return (EINVAL); 930 931 /* 932 * Discard all but LEDs kdb events as unrelated to userspace. 933 * Aggregate LED updates and postpone reporting until kdb deactivation. 934 */ 935 if (kdb_active || SCHEDULER_STOPPED()) { 936 evdev->ev_kdb_active = true; 937 if (type == EV_LED) 938 bit_set(evdev->ev_kdb_led_states, 939 bit_test(evdev->ev_led_states, code) != value); 940 return (0); 941 } 942 943 EVDEV_ENTER(evdev); 944 945 /* Fix evdev state corrupted with discarding of kdb events */ 946 if (evdev->ev_kdb_active) { 947 evdev->ev_kdb_active = false; 948 evdev_restore_after_kdb(evdev); 949 } 950 951 if (type == EV_SYN && code == SYN_REPORT && 952 bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 953 evdev_mt_sync_frame(evdev); 954 evdev_send_event(evdev, type, code, value); 955 956 EVDEV_EXIT(evdev); 957 958 return (0); 959 } 960 961 int 962 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 963 int32_t value) 964 { 965 struct epoch_tracker et; 966 int ret = 0; 967 968 switch (type) { 969 case EV_REP: 970 /* evdev repeats should not be processed by hardware driver */ 971 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) 972 goto push; 973 /* FALLTHROUGH */ 974 case EV_LED: 975 case EV_MSC: 976 case EV_SND: 977 case EV_FF: 978 if (evdev->ev_methods != NULL && 979 evdev->ev_methods->ev_event != NULL) 980 evdev->ev_methods->ev_event(evdev, type, code, value); 981 /* 982 * Leds and driver repeats should be reported in ev_event 983 * method body to interoperate with kbdmux states and rates 984 * propagation so both ways (ioctl and evdev) of changing it 985 * will produce only one evdev event report to client. 986 */ 987 if (type == EV_LED || type == EV_REP) 988 break; 989 /* FALLTHROUGH */ 990 case EV_SYN: 991 case EV_KEY: 992 case EV_REL: 993 case EV_ABS: 994 case EV_SW: 995 push: 996 if (evdev->ev_lock_type == EV_LOCK_MTX) 997 EVDEV_LOCK(evdev); 998 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 999 epoch_enter_preempt(INPUT_EPOCH, &et); 1000 ret = evdev_push_event(evdev, type, code, value); 1001 if (evdev->ev_lock_type == EV_LOCK_MTX) 1002 EVDEV_UNLOCK(evdev); 1003 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 1004 epoch_exit_preempt(INPUT_EPOCH, &et); 1005 1006 break; 1007 1008 default: 1009 ret = EINVAL; 1010 } 1011 1012 return (ret); 1013 } 1014 1015 int 1016 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client) 1017 { 1018 int ret = 0; 1019 1020 debugf(evdev, "adding new client for device %s", evdev->ev_shortname); 1021 1022 EVDEV_LIST_LOCK_ASSERT(evdev); 1023 1024 if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL && 1025 evdev->ev_methods->ev_open != NULL) { 1026 debugf(evdev, "calling ev_open() on device %s", 1027 evdev->ev_shortname); 1028 ret = evdev->ev_methods->ev_open(evdev); 1029 } 1030 if (ret == 0) 1031 CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link); 1032 return (ret); 1033 } 1034 1035 void 1036 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client) 1037 { 1038 debugf(evdev, "removing client for device %s", evdev->ev_shortname); 1039 1040 EVDEV_LIST_LOCK_ASSERT(evdev); 1041 1042 CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link); 1043 if (CK_SLIST_EMPTY(&evdev->ev_clients)) { 1044 if (evdev->ev_methods != NULL && 1045 evdev->ev_methods->ev_close != NULL) 1046 (void)evdev->ev_methods->ev_close(evdev); 1047 if (evdev_event_supported(evdev, EV_REP) && 1048 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 1049 if (evdev->ev_lock_type != EV_LOCK_MTX) 1050 EVDEV_LOCK(evdev); 1051 evdev_stop_repeat(evdev); 1052 if (evdev->ev_lock_type != EV_LOCK_MTX) 1053 EVDEV_UNLOCK(evdev); 1054 } 1055 } 1056 if (evdev->ev_lock_type != EV_LOCK_MTX) 1057 EVDEV_LOCK(evdev); 1058 evdev_release_client(evdev, client); 1059 if (evdev->ev_lock_type != EV_LOCK_MTX) 1060 EVDEV_UNLOCK(evdev); 1061 } 1062 1063 int 1064 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client) 1065 { 1066 1067 EVDEV_LOCK_ASSERT(evdev); 1068 1069 if (evdev->ev_grabber != NULL) 1070 return (EBUSY); 1071 1072 evdev->ev_grabber = client; 1073 1074 return (0); 1075 } 1076 1077 int 1078 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client) 1079 { 1080 1081 EVDEV_LOCK_ASSERT(evdev); 1082 1083 if (evdev->ev_grabber != client) 1084 return (EINVAL); 1085 1086 evdev->ev_grabber = NULL; 1087 1088 return (0); 1089 } 1090 1091 static void 1092 evdev_repeat_callout(void *arg) 1093 { 1094 struct epoch_tracker et; 1095 struct evdev_dev *evdev = (struct evdev_dev *)arg; 1096 1097 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 1098 epoch_enter_preempt(INPUT_EPOCH, &et); 1099 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT); 1100 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); 1101 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 1102 epoch_exit_preempt(INPUT_EPOCH, &et); 1103 1104 if (evdev->ev_rep[REP_PERIOD]) 1105 callout_reset(&evdev->ev_rep_callout, 1106 evdev->ev_rep[REP_PERIOD] * hz / 1000, 1107 evdev_repeat_callout, evdev); 1108 else 1109 evdev->ev_rep_key = KEY_RESERVED; 1110 } 1111 1112 static void 1113 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key) 1114 { 1115 1116 EVDEV_LOCK_ASSERT(evdev); 1117 1118 if (evdev->ev_rep[REP_DELAY]) { 1119 evdev->ev_rep_key = key; 1120 callout_reset(&evdev->ev_rep_callout, 1121 evdev->ev_rep[REP_DELAY] * hz / 1000, 1122 evdev_repeat_callout, evdev); 1123 } 1124 } 1125 1126 static void 1127 evdev_stop_repeat(struct evdev_dev *evdev) 1128 { 1129 1130 EVDEV_LOCK_ASSERT(evdev); 1131 1132 if (evdev->ev_rep_key != KEY_RESERVED) { 1133 callout_stop(&evdev->ev_rep_callout); 1134 evdev->ev_rep_key = KEY_RESERVED; 1135 } 1136 } 1137 1138 MODULE_VERSION(evdev, 1); 1139