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/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 fuzz = evdev->ev_absinfo[code].fuzz; 705 if (fuzz == 0 || code == ABS_MT_SLOT) 706 break; 707 else if (!ABS_IS_MT(code)) 708 old_value = evdev->ev_absinfo[code].value; 709 else if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 710 old_value = evdev_get_mt_value(evdev, 711 evdev_get_last_mt_slot(evdev), code); 712 else /* Pass MT protocol type A events as is */ 713 break; 714 715 abs_change = abs(*value - old_value); 716 if (abs_change < fuzz / 2) 717 *value = old_value; 718 else if (abs_change < fuzz) 719 *value = (old_value * 3 + *value) / 4; 720 else if (abs_change < fuzz * 2) 721 *value = (old_value + *value) / 2; 722 break; 723 } 724 } 725 726 static enum evdev_sparse_result 727 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 728 int32_t value) 729 { 730 int32_t last_mt_slot; 731 732 EVDEV_LOCK_ASSERT(evdev); 733 734 /* 735 * For certain event types, update device state bits 736 * and convert level reporting to edge reporting 737 */ 738 switch (type) { 739 case EV_KEY: 740 switch (value) { 741 case KEY_EVENT_UP: 742 case KEY_EVENT_DOWN: 743 if (bit_test(evdev->ev_key_states, code) == value) 744 return (EV_SKIP_EVENT); 745 bit_change(evdev->ev_key_states, code, value); 746 break; 747 748 case KEY_EVENT_REPEAT: 749 if (bit_test(evdev->ev_key_states, code) == 0 || 750 !evdev_event_supported(evdev, EV_REP)) 751 return (EV_SKIP_EVENT); 752 break; 753 754 default: 755 return (EV_SKIP_EVENT); 756 } 757 break; 758 759 case EV_LED: 760 if (bit_test(evdev->ev_led_states, code) == value) 761 return (EV_SKIP_EVENT); 762 bit_change(evdev->ev_led_states, code, value); 763 break; 764 765 case EV_SND: 766 bit_change(evdev->ev_snd_states, code, value); 767 break; 768 769 case EV_SW: 770 if (bit_test(evdev->ev_sw_states, code) == value) 771 return (EV_SKIP_EVENT); 772 bit_change(evdev->ev_sw_states, code, value); 773 break; 774 775 case EV_REP: 776 if (evdev->ev_rep[code] == value) 777 return (EV_SKIP_EVENT); 778 evdev_set_repeat_params(evdev, code, value); 779 break; 780 781 case EV_REL: 782 if (value == 0) 783 return (EV_SKIP_EVENT); 784 break; 785 786 /* For EV_ABS, save last value in absinfo and ev_mt_states */ 787 case EV_ABS: 788 switch (code) { 789 case ABS_MT_SLOT: 790 /* Postpone ABS_MT_SLOT till next event */ 791 evdev_set_last_mt_slot(evdev, value); 792 return (EV_SKIP_EVENT); 793 794 case ABS_MT_FIRST ... ABS_MT_LAST: 795 /* Pass MT protocol type A events as is */ 796 if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT)) 797 break; 798 /* Don`t repeat MT protocol type B events */ 799 last_mt_slot = evdev_get_last_mt_slot(evdev); 800 if (evdev_get_mt_value(evdev, last_mt_slot, code) 801 == value) 802 return (EV_SKIP_EVENT); 803 evdev_set_mt_value(evdev, last_mt_slot, code, value); 804 if (last_mt_slot != CURRENT_MT_SLOT(evdev)) { 805 CURRENT_MT_SLOT(evdev) = last_mt_slot; 806 evdev->ev_report_opened = true; 807 return (EV_REPORT_MT_SLOT); 808 } 809 break; 810 811 default: 812 if (evdev->ev_absinfo[code].value == value) 813 return (EV_SKIP_EVENT); 814 evdev->ev_absinfo[code].value = value; 815 } 816 break; 817 818 case EV_SYN: 819 if (code == SYN_REPORT) { 820 /* Count empty reports as well as non empty */ 821 evdev->ev_report_count++; 822 /* Skip empty reports */ 823 if (!evdev->ev_report_opened) 824 return (EV_SKIP_EVENT); 825 evdev->ev_report_opened = false; 826 return (EV_REPORT_EVENT); 827 } 828 break; 829 } 830 831 evdev->ev_report_opened = true; 832 return (EV_REPORT_EVENT); 833 } 834 835 static void 836 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 837 int32_t value) 838 { 839 struct epoch_tracker et; 840 struct evdev_client *client; 841 842 debugf(evdev, "%s pushed event %d/%d/%d", 843 evdev->ev_shortname, type, code, value); 844 845 EVDEV_LOCK_ASSERT(evdev); 846 847 /* Propagate event through all clients */ 848 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 849 epoch_enter_preempt(INPUT_EPOCH, &et); 850 851 KASSERT( 852 evdev->ev_lock_type == EV_LOCK_MTX || in_epoch(INPUT_EPOCH) != 0, 853 ("Input epoch has not been entered\n")); 854 855 CK_SLIST_FOREACH(client, &evdev->ev_clients, ec_link) { 856 if (evdev->ev_grabber != NULL && evdev->ev_grabber != client) 857 continue; 858 859 EVDEV_CLIENT_LOCKQ(client); 860 evdev_client_push(client, type, code, value); 861 if (type == EV_SYN && code == SYN_REPORT) 862 evdev_notify_event(client); 863 EVDEV_CLIENT_UNLOCKQ(client); 864 } 865 if (evdev->ev_lock_type == EV_LOCK_INTERNAL) 866 epoch_exit_preempt(INPUT_EPOCH, &et); 867 868 evdev->ev_event_count++; 869 } 870 871 void 872 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 873 int32_t value) 874 { 875 enum evdev_sparse_result sparse; 876 877 EVDEV_LOCK_ASSERT(evdev); 878 879 sparse = evdev_sparse_event(evdev, type, code, value); 880 switch (sparse) { 881 case EV_REPORT_MT_SLOT: 882 /* report postponed ABS_MT_SLOT */ 883 evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT, 884 CURRENT_MT_SLOT(evdev)); 885 /* FALLTHROUGH */ 886 case EV_REPORT_EVENT: 887 evdev_propagate_event(evdev, type, code, value); 888 /* FALLTHROUGH */ 889 case EV_SKIP_EVENT: 890 break; 891 } 892 } 893 894 void 895 evdev_restore_after_kdb(struct evdev_dev *evdev) 896 { 897 int code; 898 899 EVDEV_LOCK_ASSERT(evdev); 900 901 /* Report postponed leds */ 902 bit_foreach(evdev->ev_kdb_led_states, LED_CNT, code) 903 evdev_send_event(evdev, EV_LED, code, 904 !bit_test(evdev->ev_led_states, code)); 905 bit_nclear(evdev->ev_kdb_led_states, 0, LED_MAX); 906 907 /* Release stuck keys (CTRL + ALT + ESC) */ 908 evdev_stop_repeat(evdev); 909 bit_foreach(evdev->ev_key_states, KEY_CNT, code) 910 evdev_send_event(evdev, EV_KEY, code, KEY_EVENT_UP); 911 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); 912 } 913 914 int 915 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 916 int32_t value) 917 { 918 919 if (evdev_check_event(evdev, type, code, value) != 0) 920 return (EINVAL); 921 922 /* 923 * Discard all but LEDs kdb events as unrelated to userspace. 924 * Aggregate LED updates and postpone reporting until kdb deactivation. 925 */ 926 if (kdb_active || SCHEDULER_STOPPED()) { 927 evdev->ev_kdb_active = true; 928 if (type == EV_LED) 929 bit_set(evdev->ev_kdb_led_states, 930 bit_test(evdev->ev_led_states, code) != value); 931 return (0); 932 } 933 934 EVDEV_ENTER(evdev); 935 936 /* Fix evdev state corrupted with discarding of kdb events */ 937 if (evdev->ev_kdb_active) { 938 evdev->ev_kdb_active = false; 939 evdev_restore_after_kdb(evdev); 940 } 941 942 evdev_modify_event(evdev, type, code, &value); 943 if (type == EV_SYN && code == SYN_REPORT && 944 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL)) 945 evdev_send_mt_autorel(evdev); 946 if (type == EV_SYN && code == SYN_REPORT && evdev->ev_report_opened && 947 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) 948 evdev_send_mt_compat(evdev); 949 evdev_send_event(evdev, type, code, value); 950 951 EVDEV_EXIT(evdev); 952 953 return (0); 954 } 955 956 int 957 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, 958 int32_t value) 959 { 960 struct epoch_tracker et; 961 int ret = 0; 962 963 switch (type) { 964 case EV_REP: 965 /* evdev repeats should not be processed by hardware driver */ 966 if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) 967 goto push; 968 /* FALLTHROUGH */ 969 case EV_LED: 970 case EV_MSC: 971 case EV_SND: 972 case EV_FF: 973 if (evdev->ev_methods != NULL && 974 evdev->ev_methods->ev_event != NULL) 975 evdev->ev_methods->ev_event(evdev, type, code, value); 976 /* 977 * Leds and driver repeats should be reported in ev_event 978 * method body to interoperate with kbdmux states and rates 979 * propagation so both ways (ioctl and evdev) of changing it 980 * will produce only one evdev event report to client. 981 */ 982 if (type == EV_LED || type == EV_REP) 983 break; 984 /* FALLTHROUGH */ 985 case EV_SYN: 986 case EV_KEY: 987 case EV_REL: 988 case EV_ABS: 989 case EV_SW: 990 push: 991 if (evdev->ev_lock_type == EV_LOCK_MTX) 992 EVDEV_LOCK(evdev); 993 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 994 epoch_enter_preempt(INPUT_EPOCH, &et); 995 ret = evdev_push_event(evdev, type, code, value); 996 if (evdev->ev_lock_type == EV_LOCK_MTX) 997 EVDEV_UNLOCK(evdev); 998 else if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 999 epoch_exit_preempt(INPUT_EPOCH, &et); 1000 1001 break; 1002 1003 default: 1004 ret = EINVAL; 1005 } 1006 1007 return (ret); 1008 } 1009 1010 int 1011 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client) 1012 { 1013 int ret = 0; 1014 1015 debugf(evdev, "adding new client for device %s", evdev->ev_shortname); 1016 1017 EVDEV_LIST_LOCK_ASSERT(evdev); 1018 1019 if (CK_SLIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL && 1020 evdev->ev_methods->ev_open != NULL) { 1021 debugf(evdev, "calling ev_open() on device %s", 1022 evdev->ev_shortname); 1023 ret = evdev->ev_methods->ev_open(evdev); 1024 } 1025 if (ret == 0) 1026 CK_SLIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link); 1027 return (ret); 1028 } 1029 1030 void 1031 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client) 1032 { 1033 debugf(evdev, "removing client for device %s", evdev->ev_shortname); 1034 1035 EVDEV_LIST_LOCK_ASSERT(evdev); 1036 1037 CK_SLIST_REMOVE(&evdev->ev_clients, client, evdev_client, ec_link); 1038 if (CK_SLIST_EMPTY(&evdev->ev_clients)) { 1039 if (evdev->ev_methods != NULL && 1040 evdev->ev_methods->ev_close != NULL) 1041 (void)evdev->ev_methods->ev_close(evdev); 1042 if (evdev_event_supported(evdev, EV_REP) && 1043 bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) { 1044 if (evdev->ev_lock_type != EV_LOCK_MTX) 1045 EVDEV_LOCK(evdev); 1046 evdev_stop_repeat(evdev); 1047 if (evdev->ev_lock_type != EV_LOCK_MTX) 1048 EVDEV_UNLOCK(evdev); 1049 } 1050 } 1051 if (evdev->ev_lock_type != EV_LOCK_MTX) 1052 EVDEV_LOCK(evdev); 1053 evdev_release_client(evdev, client); 1054 if (evdev->ev_lock_type != EV_LOCK_MTX) 1055 EVDEV_UNLOCK(evdev); 1056 } 1057 1058 int 1059 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client) 1060 { 1061 1062 EVDEV_LOCK_ASSERT(evdev); 1063 1064 if (evdev->ev_grabber != NULL) 1065 return (EBUSY); 1066 1067 evdev->ev_grabber = client; 1068 1069 return (0); 1070 } 1071 1072 int 1073 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client) 1074 { 1075 1076 EVDEV_LOCK_ASSERT(evdev); 1077 1078 if (evdev->ev_grabber != client) 1079 return (EINVAL); 1080 1081 evdev->ev_grabber = NULL; 1082 1083 return (0); 1084 } 1085 1086 static void 1087 evdev_repeat_callout(void *arg) 1088 { 1089 struct epoch_tracker et; 1090 struct evdev_dev *evdev = (struct evdev_dev *)arg; 1091 1092 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 1093 epoch_enter_preempt(INPUT_EPOCH, &et); 1094 evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT); 1095 evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1); 1096 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 1097 epoch_exit_preempt(INPUT_EPOCH, &et); 1098 1099 if (evdev->ev_rep[REP_PERIOD]) 1100 callout_reset(&evdev->ev_rep_callout, 1101 evdev->ev_rep[REP_PERIOD] * hz / 1000, 1102 evdev_repeat_callout, evdev); 1103 else 1104 evdev->ev_rep_key = KEY_RESERVED; 1105 } 1106 1107 static void 1108 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key) 1109 { 1110 1111 EVDEV_LOCK_ASSERT(evdev); 1112 1113 if (evdev->ev_rep[REP_DELAY]) { 1114 evdev->ev_rep_key = key; 1115 callout_reset(&evdev->ev_rep_callout, 1116 evdev->ev_rep[REP_DELAY] * hz / 1000, 1117 evdev_repeat_callout, evdev); 1118 } 1119 } 1120 1121 static void 1122 evdev_stop_repeat(struct evdev_dev *evdev) 1123 { 1124 1125 EVDEV_LOCK_ASSERT(evdev); 1126 1127 if (evdev->ev_rep_key != KEY_RESERVED) { 1128 callout_stop(&evdev->ev_rep_callout); 1129 evdev->ev_rep_key = KEY_RESERVED; 1130 } 1131 } 1132 1133 MODULE_VERSION(evdev, 1); 1134