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/types.h> 33 #include <sys/module.h> 34 #include <sys/devfs.h> 35 36 #include <sys/param.h> 37 #include <sys/conf.h> 38 #include <sys/filio.h> 39 #include <sys/fcntl.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/poll.h> 43 #include <sys/proc.h> 44 #include <sys/systm.h> 45 #include <sys/time.h> 46 #include <sys/vnode.h> /* IO_NDELAY in read() */ 47 #include <sys/uio.h> 48 49 #include <sys/errno.h> 50 51 #include <sys/device.h> 52 #include <sys/bus.h> 53 #include "bus_if.h" 54 #include "device_if.h" 55 56 /* Use FreeBSD bitstring locally. */ 57 #include "freebsd-bitstring.h" 58 59 #include <dev/misc/evdev/evdev.h> 60 #include <dev/misc/evdev/evdev_private.h> 61 #include <dev/misc/evdev/input.h> 62 63 #ifdef EVDEV_DEBUG 64 #define debugf(client, fmt, args...) kprintf("evdev cdev: "fmt"\n", ##args) 65 #else 66 #define debugf(client, fmt, args...) 67 #endif 68 69 #define DEF_RING_REPORTS 8 70 #define GID_INPUT 107 /* input group */ 71 72 static d_open_t evdev_open; 73 static d_read_t evdev_read; 74 static d_write_t evdev_write; 75 static d_ioctl_t evdev_ioctl; 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 dev_ops evdev_cdevsw = { 85 { "evdev", 0, 0 }, 86 .d_open = evdev_open, 87 .d_read = evdev_read, 88 .d_write = evdev_write, 89 .d_ioctl = evdev_ioctl, 90 .d_kqfilter = evdev_kqfilter, 91 }; 92 93 static struct filterops evdev_cdev_filterops = { 94 .f_flags = FILTEROP_ISFD, 95 .f_attach = NULL, 96 .f_detach = evdev_kqdetach, 97 .f_event = evdev_kqread, 98 }; 99 100 static int 101 evdev_open(struct dev_open_args *ap) 102 { 103 cdev_t dev = ap->a_head.a_dev; 104 struct evdev_dev *evdev = dev->si_drv1; 105 struct evdev_client *client; 106 size_t buffer_size; 107 int ret; 108 109 if (evdev == NULL) 110 return (ENODEV); 111 112 /* Initialize client structure */ 113 buffer_size = evdev->ev_report_size * DEF_RING_REPORTS; 114 client = kmalloc(offsetof(struct evdev_client, ec_buffer) + 115 sizeof(struct input_event) * buffer_size, 116 M_EVDEV, M_WAITOK | M_ZERO); 117 118 /* Initialize ring buffer */ 119 client->ec_buffer_size = buffer_size; 120 client->ec_buffer_head = 0; 121 client->ec_buffer_tail = 0; 122 client->ec_buffer_ready = 0; 123 124 client->ec_evdev = evdev; 125 lockinit(&client->ec_buffer_mtx, "evclient", 0, LK_CANRECURSE); 126 127 /* Avoid race with evdev_unregister */ 128 EVDEV_LOCK(evdev); 129 if (dev->si_drv1 == NULL) 130 ret = ENODEV; 131 else 132 ret = evdev_register_client(evdev, client); 133 134 if (ret != 0) 135 evdev_revoke_client(client); 136 /* 137 * Unlock evdev here because non-sleepable lock held 138 * while calling devfs_set_cdevpriv upsets WITNESS 139 */ 140 EVDEV_UNLOCK(evdev); 141 142 if (!ret) 143 ret = devfs_set_cdevpriv(ap->a_fp, client, &evdev_dtor); 144 145 if (ret != 0) { 146 debugf(client, "cannot register evdev 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_LOCK(client->ec_evdev); 158 if (!client->ec_revoked) 159 evdev_dispose_client(client->ec_evdev, client); 160 EVDEV_UNLOCK(client->ec_evdev); 161 162 funsetown(&client->ec_sigio); 163 lockuninit(&client->ec_buffer_mtx); 164 kfree(client, M_EVDEV); 165 } 166 167 static int 168 evdev_read(struct dev_read_args *ap) 169 { 170 struct uio *uio = ap->a_uio; 171 int ioflag = ap->a_ioflag; 172 struct evdev_client *client; 173 struct input_event event; 174 int ret = 0; 175 int remaining; 176 177 ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client); 178 if (ret != 0) 179 return (ret); 180 181 debugf(client, "read %zd bytes by thread %d", uio->uio_resid, 0); 182 183 if (client->ec_revoked) 184 return (ENODEV); 185 186 /* Zero-sized reads are allowed for error checking */ 187 if (uio->uio_resid != 0 && uio->uio_resid < sizeof(struct input_event)) 188 return (EINVAL); 189 190 remaining = uio->uio_resid / sizeof(struct input_event); 191 192 EVDEV_CLIENT_LOCKQ(client); 193 194 if (EVDEV_CLIENT_EMPTYQ(client)) { 195 if (ioflag & IO_NDELAY) { 196 ret = EWOULDBLOCK; 197 } else { 198 if (remaining != 0) { 199 client->ec_blocked = true; 200 ret = lksleep(client, &client->ec_buffer_mtx, 201 PCATCH, "evread", 0); 202 } 203 } 204 } 205 206 while (ret == 0 && !EVDEV_CLIENT_EMPTYQ(client) && remaining > 0) { 207 memcpy(&event, &client->ec_buffer[client->ec_buffer_head], 208 sizeof(struct input_event)); 209 client->ec_buffer_head = 210 (client->ec_buffer_head + 1) % client->ec_buffer_size; 211 remaining--; 212 213 EVDEV_CLIENT_UNLOCKQ(client); 214 ret = uiomove((void *)&event, sizeof(struct input_event), uio); 215 EVDEV_CLIENT_LOCKQ(client); 216 } 217 218 EVDEV_CLIENT_UNLOCKQ(client); 219 220 return (ret); 221 } 222 223 static int 224 evdev_write(struct dev_write_args *ap) 225 { 226 cdev_t dev = ap->a_head.a_dev; 227 struct uio *uio = ap->a_uio; 228 struct evdev_dev *evdev = dev->si_drv1; 229 struct evdev_client *client; 230 struct input_event event; 231 int ret = 0; 232 233 ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client); 234 if (ret != 0) 235 return (ret); 236 237 debugf(client, "write %zd bytes by thread %d", uio->uio_resid, 0); 238 239 if (client->ec_revoked || evdev == NULL) 240 return (ENODEV); 241 242 if (uio->uio_resid % sizeof(struct input_event) != 0) { 243 debugf(client, "write size not multiple of input_event size"); 244 return (EINVAL); 245 } 246 247 while (uio->uio_resid > 0 && ret == 0) { 248 ret = uiomove((void *)&event, sizeof(struct input_event), uio); 249 if (ret == 0) 250 ret = evdev_inject_event(evdev, event.type, event.code, 251 event.value); 252 } 253 254 return (ret); 255 } 256 257 static int 258 evdev_kqfilter(struct dev_kqfilter_args *ap) 259 { 260 struct knote *kn = ap->a_kn; 261 struct klist *klist; 262 struct evdev_client *client; 263 int ret; 264 265 ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client); 266 if (ret != 0) 267 return (ret); 268 269 if (client->ec_revoked) 270 return (ENODEV); 271 272 switch(kn->kn_filter) { 273 case EVFILT_READ: 274 kn->kn_fop = &evdev_cdev_filterops; 275 break; 276 default: 277 return(EINVAL); 278 } 279 kn->kn_hook = (caddr_t)client; 280 281 klist = &client->kqinfo.ki_note; 282 knote_insert(klist, kn); 283 return (0); 284 } 285 286 static int 287 evdev_kqread(struct knote *kn, long hint) 288 { 289 struct evdev_client *client; 290 int ret; 291 int locked = 0; 292 293 client = (struct evdev_client *)kn->kn_hook; 294 295 /* NOTE on DragonFly v FreeBSD. 296 * FreeBSD locks the klist when calling f_event, i.e. evdev_kqread(). 297 * That's why the plain assertion EVDEV_CLIENT_LOCKQ_ASSERT(client) 298 * fails on DragonFly: DragonFly does not ensure the lock associated 299 * with the klist is locked. 300 * To mimic FreeBSD's behavior, we will lock ec_buffer_mtx if 301 * it was not locked, and unlock when leaving. 302 */ 303 locked = lockowned(&(client)->ec_buffer_mtx); 304 if (!locked) 305 EVDEV_CLIENT_LOCKQ(client); 306 307 EVDEV_CLIENT_LOCKQ_ASSERT(client); 308 309 if (client->ec_revoked) { 310 kn->kn_flags |= EV_EOF; 311 ret = 1; 312 } else { 313 kn->kn_data = EVDEV_CLIENT_SIZEQ(client) * 314 sizeof(struct input_event); 315 ret = !EVDEV_CLIENT_EMPTYQ(client); 316 } 317 318 /* Unlock if ec_buffer_mtx was not locked. */ 319 if (!locked) { 320 EVDEV_CLIENT_UNLOCKQ(client); 321 } 322 323 return (ret); 324 } 325 326 static void 327 evdev_kqdetach(struct knote *kn) 328 { 329 struct evdev_client *client; 330 331 client = (struct evdev_client *)kn->kn_hook; 332 knote_remove(&client->kqinfo.ki_note, kn); 333 } 334 335 static int 336 evdev_ioctl(struct dev_ioctl_args *ap) 337 { 338 cdev_t dev = ap->a_head.a_dev; 339 u_long cmd = ap->a_cmd; 340 caddr_t data = ap->a_data; 341 struct evdev_dev *evdev = dev->si_drv1; 342 struct evdev_client *client; 343 struct input_keymap_entry *ke; 344 int ret, len, limit, type_num; 345 uint32_t code; 346 size_t nvalues; 347 348 ret = devfs_get_cdevpriv(ap->a_fp, (void **)&client); 349 if (ret != 0) 350 return (ret); 351 352 if (client->ec_revoked || evdev == NULL) 353 return (ENODEV); 354 355 /* file I/O ioctl handling */ 356 switch (cmd) { 357 case FIOSETOWN: 358 return (fsetown(*(int *)data, &client->ec_sigio)); 359 360 case FIOGETOWN: 361 *(int *)data = fgetown(&client->ec_sigio); 362 return (0); 363 364 case FIONBIO: 365 return (0); 366 367 case FIOASYNC: 368 if (*(int *)data) 369 client->ec_async = true; 370 else 371 client->ec_async = false; 372 373 return (0); 374 375 case FIONREAD: 376 EVDEV_CLIENT_LOCKQ(client); 377 *(int *)data = 378 EVDEV_CLIENT_SIZEQ(client) * sizeof(struct input_event); 379 EVDEV_CLIENT_UNLOCKQ(client); 380 return (0); 381 } 382 383 len = IOCPARM_LEN(cmd); 384 debugf(client, "ioctl called: cmd=0x%08lx, data=%p", cmd, data); 385 386 /* evdev fixed-length ioctls handling */ 387 switch (cmd) { 388 case EVIOCGVERSION: 389 *(int *)data = EV_VERSION; 390 return (0); 391 392 case EVIOCGID: 393 debugf(client, "EVIOCGID: bus=%d vendor=0x%04x product=0x%04x", 394 evdev->ev_id.bustype, evdev->ev_id.vendor, 395 evdev->ev_id.product); 396 memcpy(data, &evdev->ev_id, sizeof(struct input_id)); 397 return (0); 398 399 case EVIOCGREP: 400 if (!evdev_event_supported(evdev, EV_REP)) 401 return (ENOTSUP); 402 403 memcpy(data, evdev->ev_rep, sizeof(evdev->ev_rep)); 404 return (0); 405 406 case EVIOCSREP: 407 if (!evdev_event_supported(evdev, EV_REP)) 408 return (ENOTSUP); 409 410 evdev_inject_event(evdev, EV_REP, REP_DELAY, ((int *)data)[0]); 411 evdev_inject_event(evdev, EV_REP, REP_PERIOD, 412 ((int *)data)[1]); 413 return (0); 414 415 case EVIOCGKEYCODE: 416 /* Fake unsupported ioctl */ 417 return (0); 418 419 case EVIOCGKEYCODE_V2: 420 if (evdev->ev_methods == NULL || 421 evdev->ev_methods->ev_get_keycode == NULL) 422 return (ENOTSUP); 423 424 ke = (struct input_keymap_entry *)data; 425 evdev->ev_methods->ev_get_keycode(evdev, evdev->ev_softc, ke); 426 return (0); 427 428 case EVIOCSKEYCODE: 429 /* Fake unsupported ioctl */ 430 return (0); 431 432 case EVIOCSKEYCODE_V2: 433 if (evdev->ev_methods == NULL || 434 evdev->ev_methods->ev_set_keycode == NULL) 435 return (ENOTSUP); 436 437 ke = (struct input_keymap_entry *)data; 438 evdev->ev_methods->ev_set_keycode(evdev, evdev->ev_softc, ke); 439 return (0); 440 441 case EVIOCGABS(0) ... EVIOCGABS(ABS_MAX): 442 if (evdev->ev_absinfo == NULL) 443 return (EINVAL); 444 445 memcpy(data, &evdev->ev_absinfo[cmd - EVIOCGABS(0)], 446 sizeof(struct input_absinfo)); 447 return (0); 448 449 case EVIOCSABS(0) ... EVIOCSABS(ABS_MAX): 450 if (evdev->ev_absinfo == NULL) 451 return (EINVAL); 452 453 code = cmd - EVIOCSABS(0); 454 /* mt-slot number can not be changed */ 455 if (code == ABS_MT_SLOT) 456 return (EINVAL); 457 458 EVDEV_LOCK(evdev); 459 evdev_set_absinfo(evdev, code, (struct input_absinfo *)data); 460 EVDEV_UNLOCK(evdev); 461 return (0); 462 463 case EVIOCSFF: 464 case EVIOCRMFF: 465 case EVIOCGEFFECTS: 466 /* Fake unsupported ioctls */ 467 return (0); 468 469 case EVIOCGRAB: 470 EVDEV_LOCK(evdev); 471 if (*(int *)data) 472 ret = evdev_grab_client(evdev, client); 473 else 474 ret = evdev_release_client(evdev, client); 475 EVDEV_UNLOCK(evdev); 476 return (ret); 477 478 case EVIOCREVOKE: 479 if (*(int *)data != 0) 480 return (EINVAL); 481 482 EVDEV_LOCK(evdev); 483 if (dev->si_drv1 != NULL && !client->ec_revoked) { 484 evdev_dispose_client(evdev, client); 485 evdev_revoke_client(client); 486 } 487 EVDEV_UNLOCK(evdev); 488 return (0); 489 490 case EVIOCSCLOCKID: 491 switch (*(int *)data) { 492 case CLOCK_REALTIME: 493 client->ec_clock_id = EV_CLOCK_REALTIME; 494 return (0); 495 case CLOCK_MONOTONIC: 496 client->ec_clock_id = EV_CLOCK_MONOTONIC; 497 return (0); 498 default: 499 return (EINVAL); 500 } 501 } 502 503 /* evdev variable-length ioctls handling */ 504 switch (IOCBASECMD(cmd)) { 505 case EVIOCGNAME(0): 506 strlcpy(data, evdev->ev_name, len); 507 return (0); 508 509 case EVIOCGPHYS(0): 510 if (evdev->ev_shortname[0] == 0) 511 return (ENOENT); 512 513 strlcpy(data, evdev->ev_shortname, len); 514 return (0); 515 516 case EVIOCGUNIQ(0): 517 if (evdev->ev_serial[0] == 0) 518 return (ENOENT); 519 520 strlcpy(data, evdev->ev_serial, len); 521 return (0); 522 523 case EVIOCGPROP(0): 524 limit = MIN(len, bitstr_size(INPUT_PROP_CNT)); 525 memcpy(data, evdev->ev_prop_flags, limit); 526 return (0); 527 528 case EVIOCGMTSLOTS(0): 529 if (evdev->ev_mt == NULL) 530 return (EINVAL); 531 if (len < sizeof(uint32_t)) 532 return (EINVAL); 533 code = *(uint32_t *)data; 534 if (!ABS_IS_MT(code)) 535 return (EINVAL); 536 537 nvalues = 538 MIN(len / sizeof(int32_t) - 1, MAXIMAL_MT_SLOT(evdev) + 1); 539 for (int i = 0; i < nvalues; i++) 540 ((int32_t *)data)[i + 1] = 541 evdev_get_mt_value(evdev, i, code); 542 return (0); 543 544 case EVIOCGKEY(0): 545 limit = MIN(len, bitstr_size(KEY_CNT)); 546 EVDEV_LOCK(evdev); 547 evdev_client_filter_queue(client, EV_KEY); 548 memcpy(data, evdev->ev_key_states, limit); 549 EVDEV_UNLOCK(evdev); 550 return (0); 551 552 case EVIOCGLED(0): 553 limit = MIN(len, bitstr_size(LED_CNT)); 554 EVDEV_LOCK(evdev); 555 evdev_client_filter_queue(client, EV_LED); 556 memcpy(data, evdev->ev_led_states, limit); 557 EVDEV_UNLOCK(evdev); 558 return (0); 559 560 case EVIOCGSND(0): 561 limit = MIN(len, bitstr_size(SND_CNT)); 562 EVDEV_LOCK(evdev); 563 evdev_client_filter_queue(client, EV_SND); 564 memcpy(data, evdev->ev_snd_states, limit); 565 EVDEV_UNLOCK(evdev); 566 return (0); 567 568 case EVIOCGSW(0): 569 limit = MIN(len, bitstr_size(SW_CNT)); 570 EVDEV_LOCK(evdev); 571 evdev_client_filter_queue(client, EV_SW); 572 memcpy(data, evdev->ev_sw_states, limit); 573 EVDEV_UNLOCK(evdev); 574 return (0); 575 576 case EVIOCGBIT(0, 0) ... EVIOCGBIT(EV_MAX, 0): 577 type_num = IOCBASECMD(cmd) - EVIOCGBIT(0, 0); 578 debugf(client, "EVIOCGBIT(%d): data=%p, len=%d", type_num, 579 data, len); 580 return (evdev_ioctl_eviocgbit(evdev, type_num, len, data)); 581 } 582 583 return (EINVAL); 584 } 585 586 static int 587 evdev_ioctl_eviocgbit(struct evdev_dev *evdev, int type, int len, caddr_t data) 588 { 589 /* 590 * We will use freebsd-bitstring.h locally. This ensures bitmap 591 * is of type (unsigned long *). DragonFly's original bitmap 592 * is (unsigned char *). 593 */ 594 unsigned long *bitmap; 595 int limit; 596 597 switch (type) { 598 case 0: 599 bitmap = evdev->ev_type_flags; 600 limit = EV_CNT; 601 break; 602 case EV_KEY: 603 bitmap = evdev->ev_key_flags; 604 limit = KEY_CNT; 605 break; 606 case EV_REL: 607 bitmap = evdev->ev_rel_flags; 608 limit = REL_CNT; 609 break; 610 case EV_ABS: 611 bitmap = evdev->ev_abs_flags; 612 limit = ABS_CNT; 613 break; 614 case EV_MSC: 615 bitmap = evdev->ev_msc_flags; 616 limit = MSC_CNT; 617 break; 618 case EV_LED: 619 bitmap = evdev->ev_led_flags; 620 limit = LED_CNT; 621 break; 622 case EV_SND: 623 bitmap = evdev->ev_snd_flags; 624 limit = SND_CNT; 625 break; 626 case EV_SW: 627 bitmap = evdev->ev_sw_flags; 628 limit = SW_CNT; 629 break; 630 case EV_FF: 631 /* 632 * We don't support EV_FF now, so let's 633 * just fake it returning only zeros. 634 */ 635 bzero(data, len); 636 return (0); 637 default: 638 return (ENOTTY); 639 } 640 641 /* 642 * Clear ioctl data buffer in case it's bigger than 643 * bitmap size 644 */ 645 bzero(data, len); 646 647 limit = bitstr_size(limit); 648 len = MIN(limit, len); 649 memcpy(data, bitmap, len); 650 return (0); 651 } 652 653 void 654 evdev_revoke_client(struct evdev_client *client) 655 { 656 657 EVDEV_LOCK_ASSERT(client->ec_evdev); 658 659 client->ec_revoked = true; 660 } 661 662 void 663 evdev_notify_event(struct evdev_client *client) 664 { 665 666 EVDEV_CLIENT_LOCKQ_ASSERT(client); 667 668 if (client->ec_blocked) { 669 client->ec_blocked = false; 670 wakeup(client); 671 } 672 if (client->ec_selected) { 673 client->ec_selected = false; 674 wakeup(&client->kqinfo); 675 } 676 677 KNOTE(&client->kqinfo.ki_note, 0); 678 679 if (client->ec_async && client->ec_sigio != NULL) 680 pgsigio(client->ec_sigio, SIGIO, 0); 681 } 682 683 int 684 evdev_cdev_create(struct evdev_dev *evdev) 685 { 686 cdev_t dev; 687 int ret, unit; 688 689 /* 690 * Iterate over devices input/eventX until we find a non-existing 691 * one and record its number in unit. 692 */ 693 unit = 0; 694 while (devfs_find_device_by_name("input/event%d", unit) != NULL) { 695 unit++; 696 } 697 698 /* 699 * Put unit as minor. Minor and major will determine st_rdev of 700 * eventX. Ensuring that all eventX have different major and minor 701 * will make st_rdev unique. This is needed by libinput, which 702 * determines eventX from st_rdev. 703 */ 704 dev = make_dev(&evdev_cdevsw, unit, UID_ROOT, GID_INPUT, 705 0660, "input/event%d", unit); 706 707 if (dev != NULL) { 708 dev->si_drv1 = evdev; 709 evdev->ev_cdev = dev; 710 evdev->ev_unit = unit; 711 ret = 0; 712 } else { 713 ret = ENODEV; 714 goto err; 715 } 716 717 reference_dev(evdev->ev_cdev); 718 719 err: 720 return (ret); 721 } 722 723 int 724 evdev_cdev_destroy(struct evdev_dev *evdev) 725 { 726 727 if (evdev->ev_cdev) { 728 dev_ops_remove_minor(&evdev_cdevsw, evdev->ev_unit); 729 } 730 731 return (0); 732 } 733 734 static void 735 evdev_client_gettime(struct evdev_client *client, struct timeval *tv) 736 { 737 738 switch (client->ec_clock_id) { 739 case EV_CLOCK_BOOTTIME: 740 /* 741 * XXX: FreeBSD does not support true POSIX monotonic clock. 742 * So aliase EV_CLOCK_BOOTTIME to EV_CLOCK_MONOTONIC. 743 */ 744 case EV_CLOCK_MONOTONIC: 745 microuptime(tv); 746 break; 747 748 case EV_CLOCK_REALTIME: 749 default: 750 microtime(tv); 751 break; 752 } 753 } 754 755 void 756 evdev_client_push(struct evdev_client *client, uint16_t type, uint16_t code, 757 int32_t value) 758 { 759 struct timeval time; 760 size_t count, head, tail, ready; 761 762 EVDEV_CLIENT_LOCKQ_ASSERT(client); 763 head = client->ec_buffer_head; 764 tail = client->ec_buffer_tail; 765 ready = client->ec_buffer_ready; 766 count = client->ec_buffer_size; 767 768 /* If queue is full drop its content and place SYN_DROPPED event */ 769 if ((tail + 1) % count == head) { 770 debugf(client, "client %p: buffer overflow", client); 771 772 head = (tail + count - 1) % count; 773 client->ec_buffer[head] = (struct input_event) { 774 .type = EV_SYN, 775 .code = SYN_DROPPED, 776 .value = 0 777 }; 778 /* 779 * XXX: Here is a small race window from now till the end of 780 * report. The queue is empty but client has been already 781 * notified of data readyness. Can be fixed in two ways: 782 * 1. Implement bulk insert so queue lock would not be dropped 783 * till the SYN_REPORT event. 784 * 2. Insert SYN_REPORT just now and skip remaining events 785 */ 786 client->ec_buffer_head = head; 787 client->ec_buffer_ready = head; 788 } 789 790 client->ec_buffer[tail].type = type; 791 client->ec_buffer[tail].code = code; 792 client->ec_buffer[tail].value = value; 793 client->ec_buffer_tail = (tail + 1) % count; 794 795 /* Allow users to read events only after report has been completed */ 796 if (type == EV_SYN && code == SYN_REPORT) { 797 evdev_client_gettime(client, &time); 798 for (; ready != client->ec_buffer_tail; 799 ready = (ready + 1) % count) 800 client->ec_buffer[ready].time = time; 801 client->ec_buffer_ready = client->ec_buffer_tail; 802 } 803 } 804 805 void 806 evdev_client_dumpqueue(struct evdev_client *client) 807 { 808 struct input_event *event; 809 size_t i, head, tail, ready, size; 810 811 head = client->ec_buffer_head; 812 tail = client->ec_buffer_tail; 813 ready = client->ec_buffer_ready; 814 size = client->ec_buffer_size; 815 816 kprintf("evdev client: %p\n", client); 817 kprintf("event queue: head=%zu ready=%zu tail=%zu size=%zu\n", 818 head, ready, tail, size); 819 820 kprintf("queue contents:\n"); 821 822 for (i = 0; i < size; i++) { 823 event = &client->ec_buffer[i]; 824 kprintf("%zu: ", i); 825 826 if (i < head || i > tail) 827 kprintf("unused\n"); 828 else 829 kprintf("type=%d code=%d value=%d ", event->type, 830 event->code, event->value); 831 832 if (i == head) 833 kprintf("<- head\n"); 834 else if (i == tail) 835 kprintf("<- tail\n"); 836 else if (i == ready) 837 kprintf("<- ready\n"); 838 else 839 kprintf("\n"); 840 } 841 } 842 843 static void 844 evdev_client_filter_queue(struct evdev_client *client, uint16_t type) 845 { 846 struct input_event *event; 847 size_t head, tail, count, i; 848 bool last_was_syn = false; 849 850 EVDEV_CLIENT_LOCKQ(client); 851 852 i = head = client->ec_buffer_head; 853 tail = client->ec_buffer_tail; 854 count = client->ec_buffer_size; 855 client->ec_buffer_ready = client->ec_buffer_tail; 856 857 while (i != client->ec_buffer_tail) { 858 event = &client->ec_buffer[i]; 859 i = (i + 1) % count; 860 861 /* Skip event of given type */ 862 if (event->type == type) 863 continue; 864 865 /* Remove empty SYN_REPORT events */ 866 if (event->type == EV_SYN && event->code == SYN_REPORT) { 867 if (last_was_syn) 868 continue; 869 else 870 client->ec_buffer_ready = (tail + 1) % count; 871 } 872 873 /* Rewrite entry */ 874 memcpy(&client->ec_buffer[tail], event, 875 sizeof(struct input_event)); 876 877 last_was_syn = (event->type == EV_SYN && 878 event->code == SYN_REPORT); 879 880 tail = (tail + 1) % count; 881 } 882 883 client->ec_buffer_head = i; 884 client->ec_buffer_tail = tail; 885 886 EVDEV_CLIENT_UNLOCKQ(client); 887 } 888