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