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