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 28 #include "opt_evdev.h" 29 30 #include <sys/param.h> 31 #include <sys/bitstring.h> 32 #include <sys/conf.h> 33 #include <sys/epoch.h> 34 #include <sys/filio.h> 35 #include <sys/fcntl.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/poll.h> 39 #include <sys/proc.h> 40 #include <sys/selinfo.h> 41 #include <sys/systm.h> 42 #include <sys/time.h> 43 #include <sys/uio.h> 44 45 #include <dev/evdev/evdev.h> 46 #include <dev/evdev/evdev_private.h> 47 #include <dev/evdev/input.h> 48 49 #ifdef COMPAT_FREEBSD32 50 #include <sys/mount.h> 51 #include <sys/sysent.h> 52 #include <compat/freebsd32/freebsd32.h> 53 struct input_event32 { 54 struct timeval32 time; 55 uint16_t type; 56 uint16_t code; 57 int32_t value; 58 }; 59 #endif 60 61 #ifdef EVDEV_DEBUG 62 #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args) 63 #else 64 #define debugf(client, fmt, args...) 65 #endif 66 67 #define DEF_RING_REPORTS 8 68 69 static d_open_t evdev_open; 70 static d_read_t evdev_read; 71 static d_write_t evdev_write; 72 static d_ioctl_t evdev_ioctl; 73 static d_poll_t evdev_poll; 74 static d_kqfilter_t evdev_kqfilter; 75 76 static int evdev_kqread(struct knote *kn, long hint); 77 static void evdev_kqdetach(struct knote *kn); 78 static void evdev_dtor(void *); 79 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t, 80 struct thread *); 81 static void evdev_client_filter_queue(struct evdev_client *, uint16_t); 82 83 static struct cdevsw evdev_cdevsw = { 84 .d_version = D_VERSION, 85 .d_open = evdev_open, 86 .d_read = evdev_read, 87 .d_write = evdev_write, 88 .d_ioctl = evdev_ioctl, 89 .d_poll = evdev_poll, 90 .d_kqfilter = evdev_kqfilter, 91 .d_name = "evdev", 92 }; 93 94 static const struct filterops evdev_cdev_filterops = { 95 .f_isfd = 1, 96 .f_attach = NULL, 97 .f_detach = evdev_kqdetach, 98 .f_event = evdev_kqread, 99 .f_copy = knote_triv_copy, 100 }; 101 102 static int 103 evdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 104 { 105 struct evdev_dev *evdev = dev->si_drv1; 106 struct evdev_client *client; 107 size_t buffer_size; 108 int ret; 109 110 if (evdev == NULL) 111 return (ENODEV); 112 113 /* Initialize client structure */ 114 buffer_size = evdev->ev_report_size * DEF_RING_REPORTS; 115 client = malloc(offsetof(struct evdev_client, ec_buffer) + 116 sizeof(struct input_event) * buffer_size, 117 M_EVDEV, M_WAITOK | M_ZERO); 118 119 /* Initialize ring buffer */ 120 client->ec_buffer_size = buffer_size; 121 client->ec_buffer_head = 0; 122 client->ec_buffer_tail = 0; 123 client->ec_buffer_ready = 0; 124 125 client->ec_evdev = evdev; 126 mtx_init(&client->ec_buffer_mtx, "evclient", "evdev", MTX_DEF); 127 knlist_init_mtx(&client->ec_selp.si_note, &client->ec_buffer_mtx); 128 129 ret = EVDEV_LIST_LOCK_SIG(evdev); 130 if (ret != 0) 131 goto out; 132 /* Avoid race with evdev_unregister */ 133 if (dev->si_drv1 == NULL) 134 ret = ENODEV; 135 else 136 ret = evdev_register_client(evdev, client); 137 EVDEV_LIST_UNLOCK(evdev); 138 out: 139 if (ret == 0) 140 ret = devfs_set_cdevpriv(client, evdev_dtor); 141 else 142 client->ec_revoked = true; 143 144 if (ret != 0) { 145 debugf(client, "cannot register evdev client"); 146 evdev_dtor(client); 147 } 148 149 return (ret); 150 } 151 152 static void 153 evdev_dtor(void *data) 154 { 155 struct evdev_client *client = (struct evdev_client *)data; 156 157 EVDEV_LIST_LOCK(client->ec_evdev); 158 if (!client->ec_revoked) 159 evdev_dispose_client(client->ec_evdev, client); 160 EVDEV_LIST_UNLOCK(client->ec_evdev); 161 162 if (client->ec_evdev->ev_lock_type != EV_LOCK_MTX) 163 epoch_wait_preempt(INPUT_EPOCH); 164 knlist_clear(&client->ec_selp.si_note, 0); 165 seldrain(&client->ec_selp); 166 knlist_destroy(&client->ec_selp.si_note); 167 funsetown(&client->ec_sigio); 168 mtx_destroy(&client->ec_buffer_mtx); 169 free(client, M_EVDEV); 170 } 171 172 static int 173 evdev_read(struct cdev *dev, struct uio *uio, int ioflag) 174 { 175 struct evdev_client *client; 176 union { 177 struct input_event t; 178 #ifdef COMPAT_FREEBSD32 179 struct input_event32 t32; 180 #endif 181 } event; 182 struct input_event *head; 183 size_t evsize; 184 int ret = 0; 185 int remaining; 186 187 ret = devfs_get_cdevpriv((void **)&client); 188 if (ret != 0) 189 return (ret); 190 191 debugf(client, "read %zd bytes by thread %d", uio->uio_resid, 192 uio->uio_td->td_tid); 193 194 if (client->ec_revoked) 195 return (ENODEV); 196 197 #ifdef COMPAT_FREEBSD32 198 if (SV_CURPROC_FLAG(SV_ILP32)) 199 evsize = sizeof(struct input_event32); 200 else 201 #endif 202 evsize = sizeof(struct input_event); 203 204 /* Zero-sized reads are allowed for error checking */ 205 if (uio->uio_resid != 0 && uio->uio_resid < evsize) 206 return (EINVAL); 207 208 remaining = uio->uio_resid / evsize; 209 210 EVDEV_CLIENT_LOCKQ(client); 211 212 if (EVDEV_CLIENT_EMPTYQ(client)) { 213 if (ioflag & O_NONBLOCK) 214 ret = EWOULDBLOCK; 215 else { 216 if (remaining != 0) { 217 client->ec_blocked = true; 218 ret = mtx_sleep(client, &client->ec_buffer_mtx, 219 PCATCH, "evread", 0); 220 if (ret == 0 && client->ec_revoked) 221 ret = ENODEV; 222 } 223 } 224 } 225 226 while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) { 227 head = client->ec_buffer + client->ec_buffer_head; 228 #ifdef COMPAT_FREEBSD32 229 if (SV_CURPROC_FLAG(SV_ILP32)) { 230 bzero(&event.t32, sizeof(struct input_event32)); 231 TV_CP(*head, event.t32, time); 232 CP(*head, event.t32, type); 233 CP(*head, event.t32, code); 234 CP(*head, event.t32, value); 235 } else 236 #endif 237 bcopy(head, &event.t, evsize); 238 239 client->ec_buffer_head = 240 (client->ec_buffer_head + 1) % client->ec_buffer_size; 241 remaining--; 242 243 EVDEV_CLIENT_UNLOCKQ(client); 244 ret = uiomove(&event, evsize, uio); 245 EVDEV_CLIENT_LOCKQ(client); 246 } 247 248 EVDEV_CLIENT_UNLOCKQ(client); 249 250 return (ret); 251 } 252 253 static int 254 evdev_write(struct cdev *dev, struct uio *uio, int ioflag) 255 { 256 struct evdev_dev *evdev = dev->si_drv1; 257 struct evdev_client *client; 258 union { 259 struct input_event t; 260 #ifdef COMPAT_FREEBSD32 261 struct input_event32 t32; 262 #endif 263 } event; 264 size_t evsize; 265 int ret = 0; 266 267 ret = devfs_get_cdevpriv((void **)&client); 268 if (ret != 0) 269 return (ret); 270 271 debugf(client, "write %zd bytes by thread %d", uio->uio_resid, 272 uio->uio_td->td_tid); 273 274 if (client->ec_revoked || evdev == NULL) 275 return (ENODEV); 276 277 #ifdef COMPAT_FREEBSD32 278 if (SV_CURPROC_FLAG(SV_ILP32)) 279 evsize = sizeof(struct input_event32); 280 else 281 #endif 282 evsize = sizeof(struct input_event); 283 284 if (uio->uio_resid % evsize != 0) { 285 debugf(client, "write size not multiple of input_event size"); 286 return (EINVAL); 287 } 288 289 while (uio->uio_resid > 0 && ret == 0) { 290 ret = uiomove(&event, evsize, uio); 291 if (ret == 0) { 292 #ifdef COMPAT_FREEBSD32 293 if (SV_CURPROC_FLAG(SV_ILP32)) 294 ret = evdev_inject_event(evdev, event.t32.type, 295 event.t32.code, event.t32.value); 296 else 297 #endif 298 ret = evdev_inject_event(evdev, event.t.type, 299 event.t.code, event.t.value); 300 } 301 } 302 303 return (ret); 304 } 305 306 static int 307 evdev_poll(struct cdev *dev, int events, struct thread *td) 308 { 309 struct evdev_client *client; 310 int ret; 311 int revents = 0; 312 313 ret = devfs_get_cdevpriv((void **)&client); 314 if (ret != 0) 315 return (POLLNVAL); 316 317 debugf(client, "poll by thread %d", td->td_tid); 318 319 if (client->ec_revoked) 320 return (POLLHUP); 321 322 if (events & (POLLIN | POLLRDNORM)) { 323 EVDEV_CLIENT_LOCKQ(client); 324 if (!EVDEV_CLIENT_EMPTYQ(client)) 325 revents = events & (POLLIN | POLLRDNORM); 326 else { 327 client->ec_selected = true; 328 selrecord(td, &client->ec_selp); 329 } 330 EVDEV_CLIENT_UNLOCKQ(client); 331 } 332 333 return (revents); 334 } 335 336 static int 337 evdev_kqfilter(struct cdev *dev, struct knote *kn) 338 { 339 struct evdev_client *client; 340 int ret; 341 342 ret = devfs_get_cdevpriv((void **)&client); 343 if (ret != 0) 344 return (ret); 345 346 if (client->ec_revoked) 347 return (ENODEV); 348 349 switch(kn->kn_filter) { 350 case EVFILT_READ: 351 kn->kn_fop = &evdev_cdev_filterops; 352 break; 353 default: 354 return(EINVAL); 355 } 356 kn->kn_hook = (caddr_t)client; 357 358 knlist_add(&client->ec_selp.si_note, kn, 0); 359 return (0); 360 } 361 362 static int 363 evdev_kqread(struct knote *kn, long hint) 364 { 365 struct evdev_client *client; 366 int ret; 367 368 client = (struct evdev_client *)kn->kn_hook; 369 370 EVDEV_CLIENT_LOCKQ_ASSERT(client); 371 372 if (client->ec_revoked) { 373 kn->kn_flags |= EV_EOF; 374 ret = 1; 375 } else { 376 kn->kn_data = EVDEV_CLIENT_SIZEQ(client) * 377 sizeof(struct input_event); 378 ret = !EVDEV_CLIENT_EMPTYQ(client); 379 } 380 return (ret); 381 } 382 383 static void 384 evdev_kqdetach(struct knote *kn) 385 { 386 struct evdev_client *client; 387 388 client = (struct evdev_client *)kn->kn_hook; 389 knlist_remove(&client->ec_selp.si_note, kn, 0); 390 } 391 392 static int 393 evdev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, 394 struct thread *td) 395 { 396 struct evdev_dev *evdev = dev->si_drv1; 397 struct evdev_client *client; 398 struct input_keymap_entry *ke; 399 struct epoch_tracker et; 400 int ret, len, limit, type_num; 401 uint32_t code; 402 size_t nvalues; 403 404 ret = devfs_get_cdevpriv((void **)&client); 405 if (ret != 0) 406 return (ret); 407 408 if (client->ec_revoked || evdev == NULL) 409 return (ENODEV); 410 411 /* 412 * Fix evdev state corrupted with discarding of kdb events. 413 * EVIOCGKEY and EVIOCGLED ioctls can suffer from this. 414 */ 415 if (evdev->ev_kdb_active) { 416 EVDEV_LOCK(evdev); 417 if (evdev->ev_kdb_active) { 418 evdev->ev_kdb_active = false; 419 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 420 epoch_enter_preempt(INPUT_EPOCH, &et); 421 evdev_restore_after_kdb(evdev); 422 if (evdev->ev_lock_type == EV_LOCK_EXT_EPOCH) 423 epoch_exit_preempt(INPUT_EPOCH, &et); 424 } 425 EVDEV_UNLOCK(evdev); 426 } 427 428 /* file I/O ioctl handling */ 429 switch (cmd) { 430 case FIOSETOWN: 431 return (fsetown(*(int *)data, &client->ec_sigio)); 432 433 case FIOGETOWN: 434 *(int *)data = fgetown(&client->ec_sigio); 435 return (0); 436 437 case FIONBIO: 438 return (0); 439 440 case FIOASYNC: 441 if (*(int *)data) 442 client->ec_async = true; 443 else 444 client->ec_async = false; 445 446 return (0); 447 448 case FIONREAD: 449 EVDEV_CLIENT_LOCKQ(client); 450 *(int *)data = 451 EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event); 452 EVDEV_CLIENT_UNLOCKQ(client); 453 return (0); 454 } 455 456 len = IOCPARM_LEN(cmd); 457 debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data); 458 459 /* evdev fixed-length ioctls handling */ 460 switch (cmd) { 461 case EVIOCGVERSION: 462 *(int *)data = EV_VERSION; 463 return (0); 464 465 case EVIOCGID: 466 debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x", 467 evdev->ev_id.bustype, evdev->ev_id.vendor, 468 evdev->ev_id.product); 469 memcpy(data, &evdev->ev_id, sizeof(struct input_id)); 470 return (0); 471 472 case EVIOCGREP: 473 if (!evdev_event_supported(evdev, EV_REP)) 474 return (ENOTSUP); 475 476 memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep)); 477 return (0); 478 479 case EVIOCSREP: 480 if (!evdev_event_supported(evdev, EV_REP)) 481 return (ENOTSUP); 482 483 evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]); 484 evdev_inject_event(evdev, EV_REP, REP_PERIOD, 485 ((int *)data)[1]); 486 return (0); 487 488 case EVIOCGKEYCODE: 489 /* Fake unsupported ioctl */ 490 return (0); 491 492 case EVIOCGKEYCODE_V2: 493 if (evdev->ev_methods == NULL || 494 evdev->ev_methods->ev_get_keycode == NULL) 495 return (ENOTSUP); 496 497 ke = (struct input_keymap_entry *)data; 498 evdev->ev_methods->ev_get_keycode(evdev, ke); 499 return (0); 500 501 case EVIOCSKEYCODE: 502 /* Fake unsupported ioctl */ 503 return (0); 504 505 case EVIOCSKEYCODE_V2: 506 if (evdev->ev_methods == NULL || 507 evdev->ev_methods->ev_set_keycode == NULL) 508 return (ENOTSUP); 509 510 ke = (struct input_keymap_entry *)data; 511 evdev->ev_methods->ev_set_keycode(evdev, ke); 512 return (0); 513 514 case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX): 515 if (evdev->ev_absinfo == NULL) 516 return (EINVAL); 517 518 memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)], 519 sizeof(struct input_absinfo)); 520 return (0); 521 522 case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX): 523 if (evdev->ev_absinfo == NULL) 524 return (EINVAL); 525 526 code = cmd - EVIOCSABS(0); 527 /* mt-slot number can not be changed */ 528 if (code == ABS_MT_SLOT) 529 return (EINVAL); 530 531 EVDEV_LOCK(evdev); 532 evdev_set_absinfo(evdev, code, (struct input_absinfo *)data); 533 EVDEV_UNLOCK(evdev); 534 return (0); 535 536 case EVIOCSFF: 537 case EVIOCRMFF: 538 case EVIOCGEFFECTS: 539 /* Fake unsupported ioctls */ 540 return (0); 541 542 case EVIOCGRAB: 543 EVDEV_LOCK(evdev); 544 if (*(int *)data) 545 ret = evdev_grab_client(evdev, client); 546 else 547 ret = evdev_release_client(evdev, client); 548 EVDEV_UNLOCK(evdev); 549 return (ret); 550 551 case EVIOCREVOKE: 552 if (*(int *)data != 0) 553 return (EINVAL); 554 555 EVDEV_LIST_LOCK(evdev); 556 if (dev->si_drv1 != NULL && !client->ec_revoked) { 557 evdev_dispose_client(evdev, client); 558 evdev_revoke_client(client); 559 } 560 EVDEV_LIST_UNLOCK(evdev); 561 return (0); 562 563 case EVIOCSCLOCKID: 564 switch (*(int *)data) { 565 case CLOCK_REALTIME: 566 client->ec_clock_id = EV_CLOCK_REALTIME; 567 return (0); 568 case CLOCK_MONOTONIC: 569 client->ec_clock_id = EV_CLOCK_MONOTONIC; 570 return (0); 571 default: 572 return (EINVAL); 573 } 574 } 575 576 /* evdev variable-length ioctls handling */ 577 switch (IOCBASECMD(cmd)) { 578 case EVIOCGNAME(0): 579 /* Linux evdev does not terminate truncated strings with 0 */ 580 limit = MIN(strlen(evdev->ev_name) + 1, len); 581 memcpy(data, evdev->ev_name, limit); 582 td->td_retval[0] = limit; 583 return (0); 584 585 case EVIOCGPHYS(0): 586 if (evdev->ev_shortname[0] == 0) 587 return (ENOENT); 588 589 limit = MIN(strlen(evdev->ev_shortname) + 1, len); 590 memcpy(data, evdev->ev_shortname, limit); 591 td->td_retval[0] = limit; 592 return (0); 593 594 case EVIOCGUNIQ(0): 595 if (evdev->ev_serial[0] == 0) 596 return (ENOENT); 597 598 limit = MIN(strlen(evdev->ev_serial) + 1, len); 599 memcpy(data, evdev->ev_serial, limit); 600 td->td_retval[0] = limit; 601 return (0); 602 603 case EVIOCGPROP(0): 604 limit = MIN(len, bitstr_size(INPUT_PROP_CNT)); 605 memcpy(data, evdev->ev_prop_flags, limit); 606 td->td_retval[0] = limit; 607 return (0); 608 609 case EVIOCGMTSLOTS(0): 610 /* EVIOCGMTSLOTS always returns 0 on success */ 611 if (evdev->ev_mt == NULL) 612 return (EINVAL); 613 if (len < sizeof(uint32_t)) 614 return (EINVAL); 615 code = *(uint32_t *)data; 616 if (!ABS_IS_MT(code)) 617 return (EINVAL); 618 619 nvalues = 620 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1); 621 for (int i = 0; i < nvalues; i++) 622 ((int32_t *)data)[i + 1] = 623 evdev_mt_get_value(evdev, i, code); 624 return (0); 625 626 case EVIOCGKEY(0): 627 limit = MIN(len, bitstr_size(KEY_CNT)); 628 EVDEV_LOCK(evdev); 629 evdev_client_filter_queue(client, EV_KEY); 630 memcpy(data, evdev->ev_key_states, limit); 631 EVDEV_UNLOCK(evdev); 632 td->td_retval[0] = limit; 633 return (0); 634 635 case EVIOCGLED(0): 636 limit = MIN(len, bitstr_size(LED_CNT)); 637 EVDEV_LOCK(evdev); 638 evdev_client_filter_queue(client, EV_LED); 639 memcpy(data, evdev->ev_led_states, limit); 640 EVDEV_UNLOCK(evdev); 641 td->td_retval[0] = limit; 642 return (0); 643 644 case EVIOCGSND(0): 645 limit = MIN(len, bitstr_size(SND_CNT)); 646 EVDEV_LOCK(evdev); 647 evdev_client_filter_queue(client, EV_SND); 648 memcpy(data, evdev->ev_snd_states, limit); 649 EVDEV_UNLOCK(evdev); 650 td->td_retval[0] = limit; 651 return (0); 652 653 case EVIOCGSW(0): 654 limit = MIN(len, bitstr_size(SW_CNT)); 655 EVDEV_LOCK(evdev); 656 evdev_client_filter_queue(client, EV_SW); 657 memcpy(data, evdev->ev_sw_states, limit); 658 EVDEV_UNLOCK(evdev); 659 td->td_retval[0] = limit; 660 return (0); 661 662 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0): 663 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0); 664 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num, 665 data, len); 666 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data, td)); 667 } 668 669 return (EINVAL); 670 } 671 672 static int 673 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data, 674 struct thread *td) 675 { 676 unsigned long *bitmap; 677 int limit; 678 679 switch (type) { 680 case 0: 681 bitmap = evdev->ev_type_flags; 682 limit = EV_CNT; 683 break; 684 case EV_KEY: 685 bitmap = evdev->ev_key_flags; 686 limit = KEY_CNT; 687 break; 688 case EV_REL: 689 bitmap = evdev->ev_rel_flags; 690 limit = REL_CNT; 691 break; 692 case EV_ABS: 693 bitmap = evdev->ev_abs_flags; 694 limit = ABS_CNT; 695 break; 696 case EV_MSC: 697 bitmap = evdev->ev_msc_flags; 698 limit = MSC_CNT; 699 break; 700 case EV_LED: 701 bitmap = evdev->ev_led_flags; 702 limit = LED_CNT; 703 break; 704 case EV_SND: 705 bitmap = evdev->ev_snd_flags; 706 limit = SND_CNT; 707 break; 708 case EV_SW: 709 bitmap = evdev->ev_sw_flags; 710 limit = SW_CNT; 711 break; 712 case EV_FF: 713 /* 714 * We don't support EV_FF now, so let's 715 * just fake it returning only zeros. 716 */ 717 bzero(data, len); 718 td->td_retval[0] = len; 719 return (0); 720 default: 721 return (ENOTTY); 722 } 723 724 /* 725 * Clear ioctl data buffer in case it's bigger than 726 * bitmap size 727 */ 728 bzero(data, len); 729 730 limit = bitstr_size(limit); 731 len = MIN(limit, len); 732 memcpy(data, bitmap, len); 733 td->td_retval[0] = len; 734 return (0); 735 } 736 737 void 738 evdev_revoke_client(struct evdev_client *client) 739 { 740 741 EVDEV_LIST_LOCK_ASSERT(client->ec_evdev); 742 743 client->ec_revoked = true; 744 } 745 746 void 747 evdev_notify_event(struct evdev_client *client) 748 { 749 750 EVDEV_CLIENT_LOCKQ_ASSERT(client); 751 752 if (client->ec_blocked) { 753 client->ec_blocked = false; 754 wakeup(client); 755 } 756 if (client->ec_selected) { 757 client->ec_selected = false; 758 selwakeup(&client->ec_selp); 759 } 760 KNOTE_LOCKED(&client->ec_selp.si_note, 0); 761 762 if (client->ec_async && client->ec_sigio != NULL) 763 pgsigio(&client->ec_sigio, SIGIO, 0); 764 } 765 766 int 767 evdev_cdev_create(struct evdev_dev *evdev) 768 { 769 struct make_dev_args mda; 770 int ret, unit = 0; 771 772 make_dev_args_init(&mda); 773 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; 774 mda.mda_devsw = &evdev_cdevsw; 775 mda.mda_uid = UID_ROOT; 776 mda.mda_gid = GID_WHEEL; 777 mda.mda_mode = 0600; 778 mda.mda_si_drv1 = evdev; 779 780 /* Try to coexist with cuse-backed input/event devices */ 781 while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit)) 782 == EEXIST) 783 unit++; 784 785 if (ret == 0) 786 evdev->ev_unit = unit; 787 788 return (ret); 789 } 790 791 int 792 evdev_cdev_destroy(struct evdev_dev *evdev) 793 { 794 795 destroy_dev(evdev->ev_cdev); 796 return (0); 797 } 798 799 static void 800 evdev_client_gettime(struct evdev_client *client, struct timeval *tv) 801 { 802 803 switch (client->ec_clock_id) { 804 case EV_CLOCK_BOOTTIME: 805 /* 806 * XXX: FreeBSD does not support true POSIX monotonic clock. 807 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC. 808 */ 809 case EV_CLOCK_MONOTONIC: 810 microuptime(tv); 811 break; 812 813 case EV_CLOCK_REALTIME: 814 default: 815 microtime(tv); 816 break; 817 } 818 } 819 820 void 821 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code, 822 int32_t value) 823 { 824 struct timeval time; 825 size_t count, head, tail, ready; 826 827 EVDEV_CLIENT_LOCKQ_ASSERT(client); 828 head = client->ec_buffer_head; 829 tail = client->ec_buffer_tail; 830 ready = client->ec_buffer_ready; 831 count = client->ec_buffer_size; 832 833 /* If queue is full drop its content and place SYN_DROPPED event */ 834 if ((tail + 1) % count == head) { 835 debugf(client, "client %p: buffer overflow", client); 836 837 head = (tail + count - 1) % count; 838 client->ec_buffer[head] = (struct input_event) { 839 .type = EV_SYN, 840 .code = SYN_DROPPED, 841 .value = 0 842 }; 843 /* 844 * XXX: Here is a small race window from now till the end of 845 * report. The queue is empty but client has been already 846 * notified of data readyness. Can be fixed in two ways: 847 * 1. Implement bulk insert so queue lock would not be dropped 848 * till the SYN_REPORT event. 849 * 2. Insert SYN_REPORT just now and skip remaining events 850 */ 851 client->ec_buffer_head = head; 852 client->ec_buffer_ready = head; 853 } 854 855 client->ec_buffer[tail].type = type; 856 client->ec_buffer[tail].code = code; 857 client->ec_buffer[tail].value = value; 858 client->ec_buffer_tail = (tail + 1) % count; 859 860 /* Allow users to read events only after report has been completed */ 861 if (type == EV_SYN && code == SYN_REPORT) { 862 evdev_client_gettime(client, &time); 863 for (; ready != client->ec_buffer_tail; 864 ready = (ready + 1) % count) 865 client->ec_buffer[ready].time = time; 866 client->ec_buffer_ready = client->ec_buffer_tail; 867 } 868 } 869 870 void 871 evdev_client_dumpqueue(struct evdev_client *client) 872 { 873 struct input_event *event; 874 size_t i, head, tail, ready, size; 875 876 head = client->ec_buffer_head; 877 tail = client->ec_buffer_tail; 878 ready = client->ec_buffer_ready; 879 size = client->ec_buffer_size; 880 881 printf("evdev client: %p\n", client); 882 printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n", 883 head, ready, tail, size); 884 885 printf("queue contents:\n"); 886 887 for (i = 0; i < size; i++) { 888 event = &client->ec_buffer[i]; 889 printf("%zu: ", i); 890 891 if (i < head || i > tail) 892 printf("unused\n"); 893 else 894 printf("type=%d code=%d value=%d ", event->type, 895 event->code, event->value); 896 897 if (i == head) 898 printf("<- head\n"); 899 else if (i == tail) 900 printf("<- tail\n"); 901 else if (i == ready) 902 printf("<- ready\n"); 903 else 904 printf("\n"); 905 } 906 } 907 908 static void 909 evdev_client_filter_queue(struct evdev_client *client, uint16_t type) 910 { 911 struct input_event *event; 912 size_t head, tail, count, i; 913 bool last_was_syn = false; 914 915 EVDEV_CLIENT_LOCKQ(client); 916 917 i = head = client->ec_buffer_head; 918 tail = client->ec_buffer_tail; 919 count = client->ec_buffer_size; 920 client->ec_buffer_ready = client->ec_buffer_tail; 921 922 while (i != client->ec_buffer_tail) { 923 event = &client->ec_buffer[i]; 924 i = (i + 1) % count; 925 926 /* Skip event of given type */ 927 if (event->type == type) 928 continue; 929 930 /* Remove empty SYN_REPORT events */ 931 if (event->type == EV_SYN && event->code == SYN_REPORT) { 932 if (last_was_syn) 933 continue; 934 else 935 client->ec_buffer_ready = (tail + 1) % count; 936 } 937 938 /* Rewrite entry */ 939 memcpy(&client->ec_buffer[tail], event, 940 sizeof(struct input_event)); 941 942 last_was_syn = (event->type == EV_SYN && 943 event->code == SYN_REPORT); 944 945 tail = (tail + 1) % count; 946 } 947 948 client->ec_buffer_head = i; 949 client->ec_buffer_tail = tail; 950 951 EVDEV_CLIENT_UNLOCKQ(client); 952 } 953