1 /* $NetBSD: uhid.c,v 1.94 2015/03/20 03:04:48 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2004, 2008, 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg@eterna.com.au). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.94 2015/03/20 03:04:48 mrg Exp $"); 39 40 #ifdef _KERNEL_OPT 41 #include "opt_compat_netbsd.h" 42 #endif 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/signalvar.h> 49 #include <sys/device.h> 50 #include <sys/ioctl.h> 51 #include <sys/conf.h> 52 #include <sys/tty.h> 53 #include <sys/file.h> 54 #include <sys/select.h> 55 #include <sys/proc.h> 56 #include <sys/vnode.h> 57 #include <sys/poll.h> 58 #include <sys/intr.h> 59 60 #include <dev/usb/usb.h> 61 #include <dev/usb/usbhid.h> 62 63 #include <dev/usb/usbdevs.h> 64 #include <dev/usb/usbdi.h> 65 #include <dev/usb/usbdi_util.h> 66 #include <dev/usb/hid.h> 67 #include <dev/usb/usb_quirks.h> 68 69 #include <dev/usb/uhidev.h> 70 71 #ifdef UHID_DEBUG 72 #define DPRINTF(x) if (uhiddebug) printf x 73 #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x 74 int uhiddebug = 0; 75 #else 76 #define DPRINTF(x) 77 #define DPRINTFN(n,x) 78 #endif 79 80 struct uhid_softc { 81 struct uhidev sc_hdev; 82 83 kmutex_t sc_access_lock; /* serialises syscall accesses */ 84 kmutex_t sc_lock; /* protects refcnt, others */ 85 kcondvar_t sc_cv; 86 kcondvar_t sc_detach_cv; 87 88 int sc_isize; 89 int sc_osize; 90 int sc_fsize; 91 92 u_char *sc_obuf; 93 94 struct clist sc_q; /* protected by sc_lock */ 95 struct selinfo sc_rsel; 96 proc_t *sc_async; /* process that wants SIGIO */ 97 void *sc_sih; 98 u_char sc_state; /* driver state */ 99 #define UHID_ASLP 0x01 /* waiting for device data */ 100 #define UHID_IMMED 0x02 /* return read data immediately */ 101 102 int sc_refcnt; 103 u_char sc_dying; 104 }; 105 106 #define UHIDUNIT(dev) (minor(dev)) 107 #define UHID_CHUNK 128 /* chunk size for read */ 108 #define UHID_BSIZE 1020 /* buffer size */ 109 110 dev_type_open(uhidopen); 111 dev_type_close(uhidclose); 112 dev_type_read(uhidread); 113 dev_type_write(uhidwrite); 114 dev_type_ioctl(uhidioctl); 115 dev_type_poll(uhidpoll); 116 dev_type_kqfilter(uhidkqfilter); 117 118 const struct cdevsw uhid_cdevsw = { 119 .d_open = uhidopen, 120 .d_close = uhidclose, 121 .d_read = uhidread, 122 .d_write = uhidwrite, 123 .d_ioctl = uhidioctl, 124 .d_stop = nostop, 125 .d_tty = notty, 126 .d_poll = uhidpoll, 127 .d_mmap = nommap, 128 .d_kqfilter = uhidkqfilter, 129 .d_discard = nodiscard, 130 .d_flag = D_OTHER 131 }; 132 133 Static void uhid_intr(struct uhidev *, void *, u_int len); 134 Static void uhid_softintr(void *); 135 136 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int); 137 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int); 138 Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *); 139 140 int uhid_match(device_t, cfdata_t, void *); 141 void uhid_attach(device_t, device_t, void *); 142 int uhid_detach(device_t, int); 143 int uhid_activate(device_t, enum devact); 144 extern struct cfdriver uhid_cd; 145 CFATTACH_DECL_NEW(uhid, sizeof(struct uhid_softc), uhid_match, uhid_attach, uhid_detach, uhid_activate); 146 147 int 148 uhid_match(device_t parent, cfdata_t match, void *aux) 149 { 150 #ifdef UHID_DEBUG 151 struct uhidev_attach_arg *uha = aux; 152 #endif 153 154 DPRINTF(("uhid_match: report=%d\n", uha->reportid)); 155 156 if (match->cf_flags & 1) 157 return UMATCH_HIGHEST; 158 else 159 return UMATCH_IFACECLASS_GENERIC; 160 } 161 162 void 163 uhid_attach(device_t parent, device_t self, void *aux) 164 { 165 struct uhid_softc *sc = device_private(self); 166 struct uhidev_attach_arg *uha = aux; 167 int size, repid; 168 void *desc; 169 170 sc->sc_hdev.sc_dev = self; 171 selinit(&sc->sc_rsel); 172 sc->sc_hdev.sc_intr = uhid_intr; 173 sc->sc_hdev.sc_parent = uha->parent; 174 sc->sc_hdev.sc_report_id = uha->reportid; 175 sc->sc_sih = softint_establish(SOFTINT_CLOCK, 176 uhid_softintr, sc); 177 178 uhidev_get_report_desc(uha->parent, &desc, &size); 179 repid = uha->reportid; 180 sc->sc_isize = hid_report_size(desc, size, hid_input, repid); 181 sc->sc_osize = hid_report_size(desc, size, hid_output, repid); 182 sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid); 183 184 aprint_naive("\n"); 185 aprint_normal(": input=%d, output=%d, feature=%d\n", 186 sc->sc_isize, sc->sc_osize, sc->sc_fsize); 187 188 mutex_init(&sc->sc_access_lock, MUTEX_DEFAULT, IPL_NONE); 189 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_USB); 190 cv_init(&sc->sc_cv, "uhidrea"); 191 cv_init(&sc->sc_detach_cv, "uhiddet"); 192 193 if (!pmf_device_register(self, NULL, NULL)) 194 aprint_error_dev(self, "couldn't establish power handler\n"); 195 196 return; 197 } 198 199 int 200 uhid_activate(device_t self, enum devact act) 201 { 202 struct uhid_softc *sc = device_private(self); 203 204 switch (act) { 205 case DVACT_DEACTIVATE: 206 sc->sc_dying = 1; 207 return 0; 208 default: 209 return EOPNOTSUPP; 210 } 211 } 212 213 int 214 uhid_detach(device_t self, int flags) 215 { 216 struct uhid_softc *sc = device_private(self); 217 int maj, mn; 218 219 DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags)); 220 221 sc->sc_dying = 1; 222 223 pmf_device_deregister(self); 224 225 mutex_enter(&sc->sc_lock); 226 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) { 227 if (--sc->sc_refcnt >= 0) { 228 /* Wake everyone */ 229 cv_broadcast(&sc->sc_cv); 230 /* Wait for processes to go away. */ 231 usb_detach_wait(sc->sc_hdev.sc_dev, 232 &sc->sc_detach_cv, &sc->sc_lock); 233 } 234 } 235 mutex_exit(&sc->sc_lock); 236 237 /* locate the major number */ 238 maj = cdevsw_lookup_major(&uhid_cdevsw); 239 240 /* Nuke the vnodes for any open instances (calls close). */ 241 mn = device_unit(self); 242 vdevgone(maj, mn, mn, VCHR); 243 244 #if 0 245 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, 246 sc->sc_hdev.sc_parent->sc_udev, 247 sc->sc_hdev.sc_dev); 248 #endif 249 cv_destroy(&sc->sc_cv); 250 cv_destroy(&sc->sc_detach_cv); 251 mutex_destroy(&sc->sc_lock); 252 mutex_destroy(&sc->sc_access_lock); 253 seldestroy(&sc->sc_rsel); 254 softint_disestablish(sc->sc_sih); 255 256 return 0; 257 } 258 259 void 260 uhid_intr(struct uhidev *addr, void *data, u_int len) 261 { 262 struct uhid_softc *sc = (struct uhid_softc *)addr; 263 264 #ifdef UHID_DEBUG 265 if (uhiddebug > 5) { 266 u_int32_t i; 267 268 DPRINTF(("uhid_intr: data =")); 269 for (i = 0; i < len; i++) 270 DPRINTF((" %02x", ((u_char *)data)[i])); 271 DPRINTF(("\n")); 272 } 273 #endif 274 275 mutex_enter(&sc->sc_lock); 276 (void)b_to_q(data, len, &sc->sc_q); 277 278 if (sc->sc_state & UHID_ASLP) { 279 sc->sc_state &= ~UHID_ASLP; 280 DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q)); 281 cv_broadcast(&sc->sc_cv); 282 } 283 selnotify(&sc->sc_rsel, 0, 0); 284 if (sc->sc_async != NULL) { 285 DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async)); 286 softint_schedule(sc->sc_sih); 287 } 288 mutex_exit(&sc->sc_lock); 289 } 290 291 void 292 uhid_softintr(void *cookie) 293 { 294 struct uhid_softc *sc; 295 296 sc = cookie; 297 298 mutex_enter(proc_lock); 299 if (sc->sc_async != NULL) 300 psignal(sc->sc_async, SIGIO); 301 mutex_exit(proc_lock); 302 } 303 304 int 305 uhidopen(dev_t dev, int flag, int mode, struct lwp *l) 306 { 307 struct uhid_softc *sc; 308 int error; 309 310 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 311 if (sc == NULL) 312 return ENXIO; 313 314 DPRINTF(("uhidopen: sc=%p\n", sc)); 315 316 if (sc->sc_dying) 317 return ENXIO; 318 319 mutex_enter(&sc->sc_access_lock); 320 321 /* 322 * uhid interrupts aren't enabled yet, so setup sc_q now, as 323 * long as they're not already allocated. 324 */ 325 if (sc->sc_hdev.sc_state & UHIDEV_OPEN) { 326 mutex_exit(&sc->sc_access_lock); 327 return EBUSY; 328 } 329 if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) { 330 mutex_exit(&sc->sc_access_lock); 331 return ENOMEM; 332 } 333 334 error = uhidev_open(&sc->sc_hdev); 335 if (error) { 336 clfree(&sc->sc_q); 337 mutex_exit(&sc->sc_access_lock); 338 return error; 339 } 340 mutex_exit(&sc->sc_access_lock); 341 342 sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK); 343 sc->sc_state &= ~UHID_IMMED; 344 345 mutex_enter(proc_lock); 346 sc->sc_async = NULL; 347 mutex_exit(proc_lock); 348 349 return 0; 350 } 351 352 int 353 uhidclose(dev_t dev, int flag, int mode, struct lwp *l) 354 { 355 struct uhid_softc *sc; 356 357 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 358 359 DPRINTF(("uhidclose: sc=%p\n", sc)); 360 361 mutex_enter(proc_lock); 362 sc->sc_async = NULL; 363 mutex_exit(proc_lock); 364 365 mutex_enter(&sc->sc_access_lock); 366 367 uhidev_stop(&sc->sc_hdev); 368 369 clfree(&sc->sc_q); 370 free(sc->sc_obuf, M_USBDEV); 371 372 uhidev_close(&sc->sc_hdev); 373 374 mutex_exit(&sc->sc_access_lock); 375 376 return 0; 377 } 378 379 int 380 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag) 381 { 382 int error = 0; 383 int extra; 384 size_t length; 385 u_char buffer[UHID_CHUNK]; 386 usbd_status err; 387 388 DPRINTFN(1, ("uhidread\n")); 389 if (sc->sc_state & UHID_IMMED) { 390 DPRINTFN(1, ("uhidread immed\n")); 391 extra = sc->sc_hdev.sc_report_id != 0; 392 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 393 buffer, sc->sc_isize + extra); 394 if (err) 395 return EIO; 396 return uiomove(buffer+extra, sc->sc_isize, uio); 397 } 398 399 mutex_enter(&sc->sc_lock); 400 while (sc->sc_q.c_cc == 0) { 401 if (flag & IO_NDELAY) { 402 mutex_exit(&sc->sc_lock); 403 return EWOULDBLOCK; 404 } 405 sc->sc_state |= UHID_ASLP; 406 DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q)); 407 error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock); 408 DPRINTFN(5, ("uhidread: woke, error=%d\n", error)); 409 if (sc->sc_dying) 410 error = EIO; 411 if (error) { 412 sc->sc_state &= ~UHID_ASLP; 413 break; 414 } 415 } 416 417 /* Transfer as many chunks as possible. */ 418 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) { 419 length = min(sc->sc_q.c_cc, uio->uio_resid); 420 if (length > sizeof(buffer)) 421 length = sizeof(buffer); 422 423 /* Remove a small chunk from the input queue. */ 424 (void) q_to_b(&sc->sc_q, buffer, length); 425 DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length)); 426 427 /* Copy the data to the user process. */ 428 mutex_exit(&sc->sc_lock); 429 if ((error = uiomove(buffer, length, uio)) != 0) 430 return error; 431 mutex_enter(&sc->sc_lock); 432 } 433 434 mutex_exit(&sc->sc_lock); 435 return error; 436 } 437 438 int 439 uhidread(dev_t dev, struct uio *uio, int flag) 440 { 441 struct uhid_softc *sc; 442 int error; 443 444 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 445 446 mutex_enter(&sc->sc_lock); 447 sc->sc_refcnt++; 448 mutex_exit(&sc->sc_lock); 449 450 mutex_enter(&sc->sc_access_lock); 451 error = uhid_do_read(sc, uio, flag); 452 mutex_exit(&sc->sc_access_lock); 453 454 mutex_enter(&sc->sc_lock); 455 if (--sc->sc_refcnt < 0) 456 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv); 457 mutex_exit(&sc->sc_lock); 458 return error; 459 } 460 461 int 462 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag) 463 { 464 int error; 465 int size; 466 usbd_status err; 467 468 DPRINTFN(1, ("uhidwrite\n")); 469 470 if (sc->sc_dying) 471 return EIO; 472 473 size = sc->sc_osize; 474 error = 0; 475 if (uio->uio_resid != size) 476 return EINVAL; 477 error = uiomove(sc->sc_obuf, size, uio); 478 if (!error) { 479 err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT, 480 sc->sc_obuf, size); 481 if (err) 482 error = EIO; 483 } 484 485 return error; 486 } 487 488 int 489 uhidwrite(dev_t dev, struct uio *uio, int flag) 490 { 491 struct uhid_softc *sc; 492 int error; 493 494 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 495 496 mutex_enter(&sc->sc_lock); 497 sc->sc_refcnt++; 498 mutex_exit(&sc->sc_lock); 499 500 mutex_enter(&sc->sc_access_lock); 501 error = uhid_do_write(sc, uio, flag); 502 mutex_exit(&sc->sc_access_lock); 503 504 mutex_enter(&sc->sc_lock); 505 if (--sc->sc_refcnt < 0) 506 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv); 507 mutex_exit(&sc->sc_lock); 508 return error; 509 } 510 511 int 512 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr, 513 int flag, struct lwp *l) 514 { 515 struct usb_ctl_report_desc *rd; 516 struct usb_ctl_report *re; 517 u_char buffer[UHID_CHUNK]; 518 int size, extra; 519 usbd_status err; 520 void *desc; 521 522 DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd)); 523 524 if (sc->sc_dying) 525 return EIO; 526 527 switch (cmd) { 528 case FIONBIO: 529 /* All handled in the upper FS layer. */ 530 break; 531 532 case FIOASYNC: 533 mutex_enter(proc_lock); 534 if (*(int *)addr) { 535 if (sc->sc_async != NULL) 536 return EBUSY; 537 sc->sc_async = l->l_proc; 538 DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc)); 539 } else 540 sc->sc_async = NULL; 541 mutex_exit(proc_lock); 542 break; 543 544 /* XXX this is not the most general solution. */ 545 case TIOCSPGRP: 546 mutex_enter(proc_lock); 547 if (sc->sc_async == NULL) { 548 mutex_exit(proc_lock); 549 return EINVAL; 550 } 551 if (*(int *)addr != sc->sc_async->p_pgid) { 552 mutex_exit(proc_lock); 553 return EPERM; 554 } 555 mutex_exit(proc_lock); 556 break; 557 558 case FIOSETOWN: 559 mutex_enter(proc_lock); 560 if (sc->sc_async == NULL) { 561 mutex_exit(proc_lock); 562 return EINVAL; 563 } 564 if (-*(int *)addr != sc->sc_async->p_pgid 565 && *(int *)addr != sc->sc_async->p_pid) { 566 mutex_exit(proc_lock); 567 return EPERM; 568 } 569 mutex_exit(proc_lock); 570 break; 571 572 case USB_GET_REPORT_DESC: 573 uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size); 574 rd = (struct usb_ctl_report_desc *)addr; 575 size = min(size, sizeof rd->ucrd_data); 576 rd->ucrd_size = size; 577 memcpy(rd->ucrd_data, desc, size); 578 break; 579 580 case USB_SET_IMMED: 581 if (*(int *)addr) { 582 extra = sc->sc_hdev.sc_report_id != 0; 583 err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT, 584 buffer, sc->sc_isize + extra); 585 if (err) 586 return EOPNOTSUPP; 587 588 sc->sc_state |= UHID_IMMED; 589 } else 590 sc->sc_state &= ~UHID_IMMED; 591 break; 592 593 case USB_GET_REPORT: 594 re = (struct usb_ctl_report *)addr; 595 switch (re->ucr_report) { 596 case UHID_INPUT_REPORT: 597 size = sc->sc_isize; 598 break; 599 case UHID_OUTPUT_REPORT: 600 size = sc->sc_osize; 601 break; 602 case UHID_FEATURE_REPORT: 603 size = sc->sc_fsize; 604 break; 605 default: 606 return EINVAL; 607 } 608 extra = sc->sc_hdev.sc_report_id != 0; 609 err = uhidev_get_report(&sc->sc_hdev, re->ucr_report, 610 re->ucr_data, size + extra); 611 if (extra) 612 memcpy(re->ucr_data, re->ucr_data+1, size); 613 if (err) 614 return EIO; 615 break; 616 617 case USB_SET_REPORT: 618 re = (struct usb_ctl_report *)addr; 619 switch (re->ucr_report) { 620 case UHID_INPUT_REPORT: 621 size = sc->sc_isize; 622 break; 623 case UHID_OUTPUT_REPORT: 624 size = sc->sc_osize; 625 break; 626 case UHID_FEATURE_REPORT: 627 size = sc->sc_fsize; 628 break; 629 default: 630 return EINVAL; 631 } 632 err = uhidev_set_report(&sc->sc_hdev, re->ucr_report, 633 re->ucr_data, size); 634 if (err) 635 return EIO; 636 break; 637 638 case USB_GET_REPORT_ID: 639 *(int *)addr = sc->sc_hdev.sc_report_id; 640 break; 641 642 case USB_GET_DEVICE_DESC: 643 *(usb_device_descriptor_t *)addr = 644 *usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev); 645 break; 646 647 case USB_GET_DEVICEINFO: 648 usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev, 649 (struct usb_device_info *)addr, 0); 650 break; 651 #ifdef COMPAT_30 652 case USB_GET_DEVICEINFO_OLD: 653 usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev, 654 (struct usb_device_info_old *)addr, 0); 655 656 break; 657 #endif 658 case USB_GET_STRING_DESC: 659 { 660 struct usb_string_desc *si = (struct usb_string_desc *)addr; 661 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev, 662 si->usd_string_index, 663 si->usd_language_id, &si->usd_desc, &size); 664 if (err) 665 return EINVAL; 666 break; 667 } 668 669 default: 670 return EINVAL; 671 } 672 return 0; 673 } 674 675 int 676 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 677 { 678 struct uhid_softc *sc; 679 int error; 680 681 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 682 if (sc == NULL) 683 return ENXIO; 684 685 if (sc->sc_dying) 686 return EIO; 687 688 mutex_enter(&sc->sc_lock); 689 sc->sc_refcnt++; 690 mutex_exit(&sc->sc_lock); 691 692 mutex_enter(&sc->sc_access_lock); 693 error = uhid_do_ioctl(sc, cmd, addr, flag, l); 694 mutex_exit(&sc->sc_access_lock); 695 696 mutex_enter(&sc->sc_lock); 697 if (--sc->sc_refcnt < 0) 698 usb_detach_broadcast(sc->sc_hdev.sc_dev, &sc->sc_detach_cv); 699 mutex_exit(&sc->sc_lock); 700 return error; 701 } 702 703 int 704 uhidpoll(dev_t dev, int events, struct lwp *l) 705 { 706 struct uhid_softc *sc; 707 int revents = 0; 708 709 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 710 if (sc == NULL) 711 return ENXIO; 712 713 if (sc->sc_dying) 714 return EIO; 715 716 mutex_enter(&sc->sc_lock); 717 if (events & (POLLOUT | POLLWRNORM)) 718 revents |= events & (POLLOUT | POLLWRNORM); 719 if (events & (POLLIN | POLLRDNORM)) { 720 if (sc->sc_q.c_cc > 0) 721 revents |= events & (POLLIN | POLLRDNORM); 722 else 723 selrecord(l, &sc->sc_rsel); 724 } 725 mutex_exit(&sc->sc_lock); 726 727 return revents; 728 } 729 730 static void 731 filt_uhidrdetach(struct knote *kn) 732 { 733 struct uhid_softc *sc = kn->kn_hook; 734 735 mutex_enter(&sc->sc_lock); 736 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext); 737 mutex_exit(&sc->sc_lock); 738 } 739 740 static int 741 filt_uhidread(struct knote *kn, long hint) 742 { 743 struct uhid_softc *sc = kn->kn_hook; 744 745 kn->kn_data = sc->sc_q.c_cc; 746 return (kn->kn_data > 0); 747 } 748 749 static const struct filterops uhidread_filtops = 750 { 1, NULL, filt_uhidrdetach, filt_uhidread }; 751 752 static const struct filterops uhid_seltrue_filtops = 753 { 1, NULL, filt_uhidrdetach, filt_seltrue }; 754 755 int 756 uhidkqfilter(dev_t dev, struct knote *kn) 757 { 758 struct uhid_softc *sc; 759 struct klist *klist; 760 761 sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev)); 762 763 if (sc->sc_dying) 764 return ENXIO; 765 766 switch (kn->kn_filter) { 767 case EVFILT_READ: 768 klist = &sc->sc_rsel.sel_klist; 769 kn->kn_fop = &uhidread_filtops; 770 break; 771 772 case EVFILT_WRITE: 773 klist = &sc->sc_rsel.sel_klist; 774 kn->kn_fop = &uhid_seltrue_filtops; 775 break; 776 777 default: 778 return EINVAL; 779 } 780 781 kn->kn_hook = sc; 782 783 mutex_enter(&sc->sc_lock); 784 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 785 mutex_exit(&sc->sc_lock); 786 787 return 0; 788 } 789