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/epoch.h> 36 #include <sys/filio.h> 37 #include <sys/fcntl.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/poll.h> 41 #include <sys/proc.h> 42 #include <sys/selinfo.h> 43 #include <sys/systm.h> 44 #include <sys/time.h> 45 #include <sys/uio.h> 46 47 #include <dev/evdev/evdev.h> 48 #include <dev/evdev/evdev_private.h> 49 #include <dev/evdev/input.h> 50 51 #ifdef COMPAT_FREEBSD32 52 #include <sys/mount.h> 53 #include <sys/sysent.h> 54 #include <compat/freebsd32/freebsd32.h> 55 struct input_event32 { 56 struct timeval32 time; 57 uint16_t type; 58 uint16_t code; 59 int32_t value; 60 }; 61 #endif 62 63 #ifdef EVDEV_DEBUG 64 #define debugf(client, fmt, args...) printf("evdev cdev: "fmt"\n", ##args) 65 #else 66 #define debugf(client, fmt, args...) 67 #endif 68 69 #define DEF_RING_REPORTS 8 70 71 static d_open_t evdev_open; 72 static d_read_t evdev_read; 73 static d_write_t evdev_write; 74 static d_ioctl_t evdev_ioctl; 75 static d_poll_t evdev_poll; 76 static d_kqfilter_t evdev_kqfilter; 77 78 static int evdev_kqread(struct knote *kn, long hint); 79 static void evdev_kqdetach(struct knote *kn); 80 static void evdev_dtor(void *); 81 static int evdev_ioctl_eviocgbit(struct evdev_dev *, int, int, caddr_t); 82 static void evdev_client_filter_queue(struct evdev_client *, uint16_t); 83 84 static struct cdevsw evdev_cdevsw = { 85 .d_version = D_VERSION, 86 .d_open = evdev_open, 87 .d_read = evdev_read, 88 .d_write = evdev_write, 89 .d_ioctl = evdev_ioctl, 90 .d_poll = evdev_poll, 91 .d_kqfilter = evdev_kqfilter, 92 .d_name = "evdev", 93 }; 94 95 static struct filterops evdev_cdev_filterops = { 96 .f_isfd = 1, 97 .f_attach = NULL, 98 .f_detach = evdev_kqdetach, 99 .f_event = evdev_kqread, 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 strlcpy(data, evdev->ev_name, len); 580 return (0); 581 582 case EVIOCGPHYS(0): 583 if (evdev->ev_shortname[0] == 0) 584 return (ENOENT); 585 586 strlcpy(data, evdev->ev_shortname, len); 587 return (0); 588 589 case EVIOCGUNIQ(0): 590 if (evdev->ev_serial[0] == 0) 591 return (ENOENT); 592 593 strlcpy(data, evdev->ev_serial, len); 594 return (0); 595 596 case EVIOCGPROP(0): 597 limit = MIN(len, bitstr_size(INPUT_PROP_CNT)); 598 memcpy(data, evdev->ev_prop_flags, limit); 599 return (0); 600 601 case EVIOCGMTSLOTS(0): 602 if (evdev->ev_mt == NULL) 603 return (EINVAL); 604 if (len < sizeof(uint32_t)) 605 return (EINVAL); 606 code = *(uint32_t *)data; 607 if (!ABS_IS_MT(code)) 608 return (EINVAL); 609 610 nvalues = 611 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1); 612 for (int i = 0; i < nvalues; i++) 613 ((int32_t *)data)[i + 1] = 614 evdev_get_mt_value(evdev, i, code); 615 return (0); 616 617 case EVIOCGKEY(0): 618 limit = MIN(len, bitstr_size(KEY_CNT)); 619 EVDEV_LOCK(evdev); 620 evdev_client_filter_queue(client, EV_KEY); 621 memcpy(data, evdev->ev_key_states, limit); 622 EVDEV_UNLOCK(evdev); 623 return (0); 624 625 case EVIOCGLED(0): 626 limit = MIN(len, bitstr_size(LED_CNT)); 627 EVDEV_LOCK(evdev); 628 evdev_client_filter_queue(client, EV_LED); 629 memcpy(data, evdev->ev_led_states, limit); 630 EVDEV_UNLOCK(evdev); 631 return (0); 632 633 case EVIOCGSND(0): 634 limit = MIN(len, bitstr_size(SND_CNT)); 635 EVDEV_LOCK(evdev); 636 evdev_client_filter_queue(client, EV_SND); 637 memcpy(data, evdev->ev_snd_states, limit); 638 EVDEV_UNLOCK(evdev); 639 return (0); 640 641 case EVIOCGSW(0): 642 limit = MIN(len, bitstr_size(SW_CNT)); 643 EVDEV_LOCK(evdev); 644 evdev_client_filter_queue(client, EV_SW); 645 memcpy(data, evdev->ev_sw_states, limit); 646 EVDEV_UNLOCK(evdev); 647 return (0); 648 649 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0): 650 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0); 651 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num, 652 data, len); 653 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data)); 654 } 655 656 return (EINVAL); 657 } 658 659 static int 660 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data) 661 { 662 unsigned long *bitmap; 663 int limit; 664 665 switch (type) { 666 case 0: 667 bitmap = evdev->ev_type_flags; 668 limit = EV_CNT; 669 break; 670 case EV_KEY: 671 bitmap = evdev->ev_key_flags; 672 limit = KEY_CNT; 673 break; 674 case EV_REL: 675 bitmap = evdev->ev_rel_flags; 676 limit = REL_CNT; 677 break; 678 case EV_ABS: 679 bitmap = evdev->ev_abs_flags; 680 limit = ABS_CNT; 681 break; 682 case EV_MSC: 683 bitmap = evdev->ev_msc_flags; 684 limit = MSC_CNT; 685 break; 686 case EV_LED: 687 bitmap = evdev->ev_led_flags; 688 limit = LED_CNT; 689 break; 690 case EV_SND: 691 bitmap = evdev->ev_snd_flags; 692 limit = SND_CNT; 693 break; 694 case EV_SW: 695 bitmap = evdev->ev_sw_flags; 696 limit = SW_CNT; 697 break; 698 case EV_FF: 699 /* 700 * We don't support EV_FF now, so let's 701 * just fake it returning only zeros. 702 */ 703 bzero(data, len); 704 return (0); 705 default: 706 return (ENOTTY); 707 } 708 709 /* 710 * Clear ioctl data buffer in case it's bigger than 711 * bitmap size 712 */ 713 bzero(data, len); 714 715 limit = bitstr_size(limit); 716 len = MIN(limit, len); 717 memcpy(data, bitmap, len); 718 return (0); 719 } 720 721 void 722 evdev_revoke_client(struct evdev_client *client) 723 { 724 725 EVDEV_LIST_LOCK_ASSERT(client->ec_evdev); 726 727 client->ec_revoked = true; 728 } 729 730 void 731 evdev_notify_event(struct evdev_client *client) 732 { 733 734 EVDEV_CLIENT_LOCKQ_ASSERT(client); 735 736 if (client->ec_blocked) { 737 client->ec_blocked = false; 738 wakeup(client); 739 } 740 if (client->ec_selected) { 741 client->ec_selected = false; 742 selwakeup(&client->ec_selp); 743 } 744 KNOTE_LOCKED(&client->ec_selp.si_note, 0); 745 746 if (client->ec_async && client->ec_sigio != NULL) 747 pgsigio(&client->ec_sigio, SIGIO, 0); 748 } 749 750 int 751 evdev_cdev_create(struct evdev_dev *evdev) 752 { 753 struct make_dev_args mda; 754 int ret, unit = 0; 755 756 make_dev_args_init(&mda); 757 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; 758 mda.mda_devsw = &evdev_cdevsw; 759 mda.mda_uid = UID_ROOT; 760 mda.mda_gid = GID_WHEEL; 761 mda.mda_mode = 0600; 762 mda.mda_si_drv1 = evdev; 763 764 /* Try to coexist with cuse-backed input/event devices */ 765 while ((ret = make_dev_s(&mda, &evdev->ev_cdev, "input/event%d", unit)) 766 == EEXIST) 767 unit++; 768 769 if (ret == 0) 770 evdev->ev_unit = unit; 771 772 return (ret); 773 } 774 775 int 776 evdev_cdev_destroy(struct evdev_dev *evdev) 777 { 778 779 destroy_dev(evdev->ev_cdev); 780 return (0); 781 } 782 783 static void 784 evdev_client_gettime(struct evdev_client *client, struct timeval *tv) 785 { 786 787 switch (client->ec_clock_id) { 788 case EV_CLOCK_BOOTTIME: 789 /* 790 * XXX: FreeBSD does not support true POSIX monotonic clock. 791 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC. 792 */ 793 case EV_CLOCK_MONOTONIC: 794 microuptime(tv); 795 break; 796 797 case EV_CLOCK_REALTIME: 798 default: 799 microtime(tv); 800 break; 801 } 802 } 803 804 void 805 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code, 806 int32_t value) 807 { 808 struct timeval time; 809 size_t count, head, tail, ready; 810 811 EVDEV_CLIENT_LOCKQ_ASSERT(client); 812 head = client->ec_buffer_head; 813 tail = client->ec_buffer_tail; 814 ready = client->ec_buffer_ready; 815 count = client->ec_buffer_size; 816 817 /* If queue is full drop its content and place SYN_DROPPED event */ 818 if ((tail + 1) % count == head) { 819 debugf(client, "client %p: buffer overflow", client); 820 821 head = (tail + count - 1) % count; 822 client->ec_buffer[head] = (struct input_event) { 823 .type = EV_SYN, 824 .code = SYN_DROPPED, 825 .value = 0 826 }; 827 /* 828 * XXX: Here is a small race window from now till the end of 829 * report. The queue is empty but client has been already 830 * notified of data readyness. Can be fixed in two ways: 831 * 1. Implement bulk insert so queue lock would not be dropped 832 * till the SYN_REPORT event. 833 * 2. Insert SYN_REPORT just now and skip remaining events 834 */ 835 client->ec_buffer_head = head; 836 client->ec_buffer_ready = head; 837 } 838 839 client->ec_buffer[tail].type = type; 840 client->ec_buffer[tail].code = code; 841 client->ec_buffer[tail].value = value; 842 client->ec_buffer_tail = (tail + 1) % count; 843 844 /* Allow users to read events only after report has been completed */ 845 if (type == EV_SYN && code == SYN_REPORT) { 846 evdev_client_gettime(client, &time); 847 for (; ready != client->ec_buffer_tail; 848 ready = (ready + 1) % count) 849 client->ec_buffer[ready].time = time; 850 client->ec_buffer_ready = client->ec_buffer_tail; 851 } 852 } 853 854 void 855 evdev_client_dumpqueue(struct evdev_client *client) 856 { 857 struct input_event *event; 858 size_t i, head, tail, ready, size; 859 860 head = client->ec_buffer_head; 861 tail = client->ec_buffer_tail; 862 ready = client->ec_buffer_ready; 863 size = client->ec_buffer_size; 864 865 printf("evdev client: %p\n", client); 866 printf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n", 867 head, ready, tail, size); 868 869 printf("queue contents:\n"); 870 871 for (i = 0; i < size; i++) { 872 event = &client->ec_buffer[i]; 873 printf("%zu: ", i); 874 875 if (i < head || i > tail) 876 printf("unused\n"); 877 else 878 printf("type=%d code=%d value=%d ", event->type, 879 event->code, event->value); 880 881 if (i == head) 882 printf("<- head\n"); 883 else if (i == tail) 884 printf("<- tail\n"); 885 else if (i == ready) 886 printf("<- ready\n"); 887 else 888 printf("\n"); 889 } 890 } 891 892 static void 893 evdev_client_filter_queue(struct evdev_client *client, uint16_t type) 894 { 895 struct input_event *event; 896 size_t head, tail, count, i; 897 bool last_was_syn = false; 898 899 EVDEV_CLIENT_LOCKQ(client); 900 901 i = head = client->ec_buffer_head; 902 tail = client->ec_buffer_tail; 903 count = client->ec_buffer_size; 904 client->ec_buffer_ready = client->ec_buffer_tail; 905 906 while (i != client->ec_buffer_tail) { 907 event = &client->ec_buffer[i]; 908 i = (i + 1) % count; 909 910 /* Skip event of given type */ 911 if (event->type == type) 912 continue; 913 914 /* Remove empty SYN_REPORT events */ 915 if (event->type == EV_SYN && event->code == SYN_REPORT) { 916 if (last_was_syn) 917 continue; 918 else 919 client->ec_buffer_ready = (tail + 1) % count; 920 } 921 922 /* Rewrite entry */ 923 memcpy(&client->ec_buffer[tail], event, 924 sizeof(struct input_event)); 925 926 last_was_syn = (event->type == EV_SYN && 927 event->code == SYN_REPORT); 928 929 tail = (tail + 1) % count; 930 } 931 932 client->ec_buffer_head = i; 933 client->ec_buffer_tail = tail; 934 935 EVDEV_CLIENT_UNLOCKQ(client); 936 } 937