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