Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.81
      1 /*	$NetBSD: uhid.c,v 1.81 2008/04/28 20:23:59 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2004, 2008 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 (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     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.81 2008/04/28 20:23:59 martin Exp $");
     39 
     40 #include "opt_compat_netbsd.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/malloc.h>
     46 #include <sys/signalvar.h>
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/conf.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/poll.h>
     56 #include <sys/intr.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbhid.h>
     60 
     61 #include <dev/usb/usbdevs.h>
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/hid.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/uhidev.h>
     68 
     69 #ifdef UHID_DEBUG
     70 #define DPRINTF(x)	if (uhiddebug) logprintf x
     71 #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
     72 int	uhiddebug = 0;
     73 #else
     74 #define DPRINTF(x)
     75 #define DPRINTFN(n,x)
     76 #endif
     77 
     78 struct uhid_softc {
     79 	struct uhidev sc_hdev;
     80 
     81 	int sc_isize;
     82 	int sc_osize;
     83 	int sc_fsize;
     84 
     85 	u_char *sc_obuf;
     86 
     87 	struct clist sc_q;
     88 	struct selinfo sc_rsel;
     89 	usb_proc_ptr sc_async;	/* process that wants SIGIO */
     90 	void *sc_sih;
     91 	u_char sc_state;	/* driver state */
     92 #define	UHID_ASLP	0x01	/* waiting for device data */
     93 #define UHID_IMMED	0x02	/* return read data immediately */
     94 
     95 	int sc_refcnt;
     96 	u_char sc_dying;
     97 };
     98 
     99 #define	UHIDUNIT(dev)	(minor(dev))
    100 #define	UHID_CHUNK	128	/* chunk size for read */
    101 #define	UHID_BSIZE	1020	/* buffer size */
    102 
    103 dev_type_open(uhidopen);
    104 dev_type_close(uhidclose);
    105 dev_type_read(uhidread);
    106 dev_type_write(uhidwrite);
    107 dev_type_ioctl(uhidioctl);
    108 dev_type_poll(uhidpoll);
    109 dev_type_kqfilter(uhidkqfilter);
    110 
    111 const struct cdevsw uhid_cdevsw = {
    112 	uhidopen, uhidclose, uhidread, uhidwrite, uhidioctl,
    113 	nostop, notty, uhidpoll, nommap, uhidkqfilter, D_OTHER,
    114 };
    115 
    116 Static void uhid_intr(struct uhidev *, void *, u_int len);
    117 Static void uhid_softintr(void *);
    118 
    119 Static int uhid_do_read(struct uhid_softc *, struct uio *uio, int);
    120 Static int uhid_do_write(struct uhid_softc *, struct uio *uio, int);
    121 Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *);
    122 
    123 USB_DECLARE_DRIVER(uhid);
    124 
    125 int
    126 uhid_match(struct device *parent, struct cfdata *match,
    127     void *aux)
    128 {
    129 #ifdef UHID_DEBUG
    130 	struct uhidev_attach_arg *uha = aux;
    131 #endif
    132 
    133 	DPRINTF(("uhid_match: report=%d\n", uha->reportid));
    134 
    135 	if (match->cf_flags & 1)
    136 		return (UMATCH_HIGHEST);
    137 	else
    138 		return (UMATCH_IFACECLASS_GENERIC);
    139 }
    140 
    141 void
    142 uhid_attach(struct device *parent, struct device *self, void *aux)
    143 {
    144 	struct uhid_softc *sc = (struct uhid_softc *)self;
    145 	struct uhidev_attach_arg *uha = aux;
    146 	int size, repid;
    147 	void *desc;
    148 
    149 	selinit(&sc->sc_rsel);
    150 	sc->sc_hdev.sc_intr = uhid_intr;
    151 	sc->sc_hdev.sc_parent = uha->parent;
    152 	sc->sc_hdev.sc_report_id = uha->reportid;
    153 	sc->sc_sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_CLOCK,
    154 	    uhid_softintr, sc);
    155 
    156 	uhidev_get_report_desc(uha->parent, &desc, &size);
    157 	repid = uha->reportid;
    158 	sc->sc_isize = hid_report_size(desc, size, hid_input,   repid);
    159 	sc->sc_osize = hid_report_size(desc, size, hid_output,  repid);
    160 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
    161 
    162 	printf(": input=%d, output=%d, feature=%d\n",
    163 	       sc->sc_isize, sc->sc_osize, sc->sc_fsize);
    164 
    165 	if (!pmf_device_register(self, NULL, NULL))
    166 		aprint_error_dev(self, "couldn't establish power handler\n");
    167 
    168 	USB_ATTACH_SUCCESS_RETURN;
    169 }
    170 
    171 int
    172 uhid_activate(device_ptr_t self, enum devact act)
    173 {
    174 	struct uhid_softc *sc = (struct uhid_softc *)self;
    175 
    176 	switch (act) {
    177 	case DVACT_ACTIVATE:
    178 		return (EOPNOTSUPP);
    179 
    180 	case DVACT_DEACTIVATE:
    181 		sc->sc_dying = 1;
    182 		break;
    183 	}
    184 	return (0);
    185 }
    186 
    187 int
    188 uhid_detach(struct device *self, int flags)
    189 {
    190 	struct uhid_softc *sc = (struct uhid_softc *)self;
    191 	int s;
    192 	int maj, mn;
    193 
    194 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    195 
    196 	sc->sc_dying = 1;
    197 
    198 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN) {
    199 		s = splusb();
    200 		if (--sc->sc_refcnt >= 0) {
    201 			/* Wake everyone */
    202 			wakeup(&sc->sc_q);
    203 			/* Wait for processes to go away. */
    204 			usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
    205 		}
    206 		splx(s);
    207 	}
    208 
    209 	/* locate the major number */
    210 #if defined(__NetBSD__)
    211 	maj = cdevsw_lookup_major(&uhid_cdevsw);
    212 #elif defined(__OpenBSD__)
    213 	for (maj = 0; maj < nchrdev; maj++)
    214 		if (cdevsw[maj].d_open == uhidopen)
    215 			break;
    216 #endif
    217 
    218 	/* Nuke the vnodes for any open instances (calls close). */
    219 	mn = device_unit(self);
    220 	vdevgone(maj, mn, mn, VCHR);
    221 
    222 #if 0
    223 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH,
    224 			   sc->sc_hdev.sc_parent->sc_udev,
    225 			   USBDEV(sc->sc_hdev.sc_dev));
    226 #endif
    227 	seldestroy(&sc->sc_rsel);
    228 	softint_disestablish(sc->sc_sih);
    229 
    230 	return (0);
    231 }
    232 
    233 void
    234 uhid_intr(struct uhidev *addr, void *data, u_int len)
    235 {
    236 	struct uhid_softc *sc = (struct uhid_softc *)addr;
    237 
    238 #ifdef UHID_DEBUG
    239 	if (uhiddebug > 5) {
    240 		u_int32_t i;
    241 
    242 		DPRINTF(("uhid_intr: data ="));
    243 		for (i = 0; i < len; i++)
    244 			DPRINTF((" %02x", ((u_char *)data)[i]));
    245 		DPRINTF(("\n"));
    246 	}
    247 #endif
    248 
    249 	(void)b_to_q(data, len, &sc->sc_q);
    250 
    251 	if (sc->sc_state & UHID_ASLP) {
    252 		sc->sc_state &= ~UHID_ASLP;
    253 		DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
    254 		wakeup(&sc->sc_q);
    255 	}
    256 	selnotify(&sc->sc_rsel, 0, 0);
    257 	if (sc->sc_async != NULL) {
    258 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
    259 		softint_schedule(sc->sc_sih);
    260 	}
    261 }
    262 
    263 void
    264 uhid_softintr(void *cookie)
    265 {
    266 	struct uhid_softc *sc;
    267 
    268 	sc = cookie;
    269 
    270 	mutex_enter(proc_lock);
    271 	if (sc->sc_async != NULL)
    272 		 psignal(sc->sc_async, SIGIO);
    273 	mutex_exit(proc_lock);
    274 }
    275 
    276 int
    277 uhidopen(dev_t dev, int flag, int mode,
    278     struct lwp *l)
    279 {
    280 	struct uhid_softc *sc;
    281 	int error;
    282 
    283 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    284 
    285 	DPRINTF(("uhidopen: sc=%p\n", sc));
    286 
    287 	if (sc->sc_dying)
    288 		return (ENXIO);
    289 
    290 	error = uhidev_open(&sc->sc_hdev);
    291 	if (error)
    292 		return (error);
    293 
    294 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    295 		uhidev_close(&sc->sc_hdev);
    296 		return (ENOMEM);
    297 	}
    298 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    299 	sc->sc_state &= ~UHID_IMMED;
    300 	mutex_enter(proc_lock);
    301 	sc->sc_async = NULL;
    302 	mutex_exit(proc_lock);
    303 
    304 	return (0);
    305 }
    306 
    307 int
    308 uhidclose(dev_t dev, int flag, int mode,
    309     struct lwp *l)
    310 {
    311 	struct uhid_softc *sc;
    312 
    313 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    314 
    315 	DPRINTF(("uhidclose: sc=%p\n", sc));
    316 
    317 	clfree(&sc->sc_q);
    318 	free(sc->sc_obuf, M_USBDEV);
    319 	mutex_enter(proc_lock);
    320 	sc->sc_async = NULL;
    321 	mutex_exit(proc_lock);
    322 	uhidev_close(&sc->sc_hdev);
    323 
    324 	return (0);
    325 }
    326 
    327 int
    328 uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
    329 {
    330 	int s;
    331 	int error = 0;
    332 	int extra;
    333 	size_t length;
    334 	u_char buffer[UHID_CHUNK];
    335 	usbd_status err;
    336 
    337 	DPRINTFN(1, ("uhidread\n"));
    338 	if (sc->sc_state & UHID_IMMED) {
    339 		DPRINTFN(1, ("uhidread immed\n"));
    340 		extra = sc->sc_hdev.sc_report_id != 0;
    341 		err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
    342 					buffer, sc->sc_isize + extra);
    343 		if (err)
    344 			return (EIO);
    345 		return (uiomove(buffer+extra, sc->sc_isize, uio));
    346 	}
    347 
    348 	s = splusb();
    349 	while (sc->sc_q.c_cc == 0) {
    350 		if (flag & IO_NDELAY) {
    351 			splx(s);
    352 			return (EWOULDBLOCK);
    353 		}
    354 		sc->sc_state |= UHID_ASLP;
    355 		DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
    356 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    357 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    358 		if (sc->sc_dying)
    359 			error = EIO;
    360 		if (error) {
    361 			sc->sc_state &= ~UHID_ASLP;
    362 			break;
    363 		}
    364 	}
    365 	splx(s);
    366 
    367 	/* Transfer as many chunks as possible. */
    368 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    369 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    370 		if (length > sizeof(buffer))
    371 			length = sizeof(buffer);
    372 
    373 		/* Remove a small chunk from the input queue. */
    374 		(void) q_to_b(&sc->sc_q, buffer, length);
    375 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    376 
    377 		/* Copy the data to the user process. */
    378 		if ((error = uiomove(buffer, length, uio)) != 0)
    379 			break;
    380 	}
    381 
    382 	return (error);
    383 }
    384 
    385 int
    386 uhidread(dev_t dev, struct uio *uio, int flag)
    387 {
    388 	struct uhid_softc *sc;
    389 	int error;
    390 
    391 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    392 
    393 	sc->sc_refcnt++;
    394 	error = uhid_do_read(sc, uio, flag);
    395 	if (--sc->sc_refcnt < 0)
    396 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    397 	return (error);
    398 }
    399 
    400 int
    401 uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
    402 {
    403 	int error;
    404 	int size;
    405 	usbd_status err;
    406 
    407 	DPRINTFN(1, ("uhidwrite\n"));
    408 
    409 	if (sc->sc_dying)
    410 		return (EIO);
    411 
    412 	size = sc->sc_osize;
    413 	error = 0;
    414 	if (uio->uio_resid != size)
    415 		return (EINVAL);
    416 	error = uiomove(sc->sc_obuf, size, uio);
    417 	if (!error) {
    418 		err = uhidev_set_report(&sc->sc_hdev, UHID_OUTPUT_REPORT,
    419 					sc->sc_obuf, size);
    420 		if (err)
    421 			error = EIO;
    422 	}
    423 
    424 	return (error);
    425 }
    426 
    427 int
    428 uhidwrite(dev_t dev, struct uio *uio, int flag)
    429 {
    430 	struct uhid_softc *sc;
    431 	int error;
    432 
    433 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    434 
    435 	sc->sc_refcnt++;
    436 	error = uhid_do_write(sc, uio, flag);
    437 	if (--sc->sc_refcnt < 0)
    438 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    439 	return (error);
    440 }
    441 
    442 int
    443 uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr,
    444     int flag, struct lwp *l)
    445 {
    446 	struct usb_ctl_report_desc *rd;
    447 	struct usb_ctl_report *re;
    448 	u_char buffer[UHID_CHUNK];
    449 	int size, extra;
    450 	usbd_status err;
    451 	void *desc;
    452 
    453 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    454 
    455 	if (sc->sc_dying)
    456 		return (EIO);
    457 
    458 	switch (cmd) {
    459 	case FIONBIO:
    460 		/* All handled in the upper FS layer. */
    461 		break;
    462 
    463 	case FIOASYNC:
    464 		mutex_enter(proc_lock);
    465 		if (*(int *)addr) {
    466 			if (sc->sc_async != NULL)
    467 				return (EBUSY);
    468 			sc->sc_async = l->l_proc;
    469 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
    470 		} else
    471 			sc->sc_async = NULL;
    472 		mutex_exit(proc_lock);
    473 		break;
    474 
    475 	/* XXX this is not the most general solution. */
    476 	case TIOCSPGRP:
    477 		mutex_enter(proc_lock);
    478 		if (sc->sc_async == NULL) {
    479 			mutex_exit(proc_lock);
    480 			return (EINVAL);
    481 		}
    482 		if (*(int *)addr != sc->sc_async->p_pgid) {
    483 			mutex_exit(proc_lock);
    484 			return (EPERM);
    485 		}
    486 		mutex_exit(proc_lock);
    487 		break;
    488 
    489 	case FIOSETOWN:
    490 		mutex_enter(proc_lock);
    491 		if (sc->sc_async == NULL) {
    492 			mutex_exit(proc_lock);
    493 			return (EINVAL);
    494 		}
    495 		if (-*(int *)addr != sc->sc_async->p_pgid
    496 		    && *(int *)addr != sc->sc_async->p_pid) {
    497 			mutex_exit(proc_lock);
    498 			return (EPERM);
    499 		}
    500 		mutex_exit(proc_lock);
    501 		break;
    502 
    503 	case USB_GET_REPORT_DESC:
    504 		uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
    505 		rd = (struct usb_ctl_report_desc *)addr;
    506 		size = min(size, sizeof rd->ucrd_data);
    507 		rd->ucrd_size = size;
    508 		memcpy(rd->ucrd_data, desc, size);
    509 		break;
    510 
    511 	case USB_SET_IMMED:
    512 		if (*(int *)addr) {
    513 			extra = sc->sc_hdev.sc_report_id != 0;
    514 			err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
    515 						buffer, sc->sc_isize + extra);
    516 			if (err)
    517 				return (EOPNOTSUPP);
    518 
    519 			sc->sc_state |=  UHID_IMMED;
    520 		} else
    521 			sc->sc_state &= ~UHID_IMMED;
    522 		break;
    523 
    524 	case USB_GET_REPORT:
    525 		re = (struct usb_ctl_report *)addr;
    526 		switch (re->ucr_report) {
    527 		case UHID_INPUT_REPORT:
    528 			size = sc->sc_isize;
    529 			break;
    530 		case UHID_OUTPUT_REPORT:
    531 			size = sc->sc_osize;
    532 			break;
    533 		case UHID_FEATURE_REPORT:
    534 			size = sc->sc_fsize;
    535 			break;
    536 		default:
    537 			return (EINVAL);
    538 		}
    539 		extra = sc->sc_hdev.sc_report_id != 0;
    540 		err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
    541 		    re->ucr_data, size + extra);
    542 		if (extra)
    543 			memcpy(re->ucr_data, re->ucr_data+1, size);
    544 		if (err)
    545 			return (EIO);
    546 		break;
    547 
    548 	case USB_SET_REPORT:
    549 		re = (struct usb_ctl_report *)addr;
    550 		switch (re->ucr_report) {
    551 		case UHID_INPUT_REPORT:
    552 			size = sc->sc_isize;
    553 			break;
    554 		case UHID_OUTPUT_REPORT:
    555 			size = sc->sc_osize;
    556 			break;
    557 		case UHID_FEATURE_REPORT:
    558 			size = sc->sc_fsize;
    559 			break;
    560 		default:
    561 			return (EINVAL);
    562 		}
    563 		err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
    564 		    re->ucr_data, size);
    565 		if (err)
    566 			return (EIO);
    567 		break;
    568 
    569 	case USB_GET_REPORT_ID:
    570 		*(int *)addr = sc->sc_hdev.sc_report_id;
    571 		break;
    572 
    573 	case USB_GET_DEVICEINFO:
    574 		usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
    575 			             (struct usb_device_info *)addr, 0);
    576 		break;
    577 #ifdef COMPAT_30
    578 	case USB_GET_DEVICEINFO_OLD:
    579 		usbd_fill_deviceinfo_old(sc->sc_hdev.sc_parent->sc_udev,
    580 					 (struct usb_device_info_old *)addr, 0);
    581 
    582 		break;
    583 #endif
    584         case USB_GET_STRING_DESC:
    585 	    {
    586                 struct usb_string_desc *si = (struct usb_string_desc *)addr;
    587                 err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
    588 			si->usd_string_index,
    589                 	si->usd_language_id, &si->usd_desc, &size);
    590                 if (err)
    591                         return (EINVAL);
    592                 break;
    593 	    }
    594 
    595 	default:
    596 		return (EINVAL);
    597 	}
    598 	return (0);
    599 }
    600 
    601 int
    602 uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    603 {
    604 	struct uhid_softc *sc;
    605 	int error;
    606 
    607 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    608 
    609 	sc->sc_refcnt++;
    610 	error = uhid_do_ioctl(sc, cmd, addr, flag, l);
    611 	if (--sc->sc_refcnt < 0)
    612 		usb_detach_wakeup(USBDEV(sc->sc_hdev.sc_dev));
    613 	return (error);
    614 }
    615 
    616 int
    617 uhidpoll(dev_t dev, int events, struct lwp *l)
    618 {
    619 	struct uhid_softc *sc;
    620 	int revents = 0;
    621 	int s;
    622 
    623 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    624 
    625 	if (sc->sc_dying)
    626 		return (POLLHUP);
    627 
    628 	s = splusb();
    629 	if (events & (POLLOUT | POLLWRNORM))
    630 		revents |= events & (POLLOUT | POLLWRNORM);
    631 	if (events & (POLLIN | POLLRDNORM)) {
    632 		if (sc->sc_q.c_cc > 0)
    633 			revents |= events & (POLLIN | POLLRDNORM);
    634 		else
    635 			selrecord(l, &sc->sc_rsel);
    636 	}
    637 
    638 	splx(s);
    639 	return (revents);
    640 }
    641 
    642 static void
    643 filt_uhidrdetach(struct knote *kn)
    644 {
    645 	struct uhid_softc *sc = kn->kn_hook;
    646 	int s;
    647 
    648 	s = splusb();
    649 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    650 	splx(s);
    651 }
    652 
    653 static int
    654 filt_uhidread(struct knote *kn, long hint)
    655 {
    656 	struct uhid_softc *sc = kn->kn_hook;
    657 
    658 	kn->kn_data = sc->sc_q.c_cc;
    659 	return (kn->kn_data > 0);
    660 }
    661 
    662 static const struct filterops uhidread_filtops =
    663 	{ 1, NULL, filt_uhidrdetach, filt_uhidread };
    664 
    665 static const struct filterops uhid_seltrue_filtops =
    666 	{ 1, NULL, filt_uhidrdetach, filt_seltrue };
    667 
    668 int
    669 uhidkqfilter(dev_t dev, struct knote *kn)
    670 {
    671 	struct uhid_softc *sc;
    672 	struct klist *klist;
    673 	int s;
    674 
    675 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    676 
    677 	if (sc->sc_dying)
    678 		return (ENXIO);
    679 
    680 	switch (kn->kn_filter) {
    681 	case EVFILT_READ:
    682 		klist = &sc->sc_rsel.sel_klist;
    683 		kn->kn_fop = &uhidread_filtops;
    684 		break;
    685 
    686 	case EVFILT_WRITE:
    687 		klist = &sc->sc_rsel.sel_klist;
    688 		kn->kn_fop = &uhid_seltrue_filtops;
    689 		break;
    690 
    691 	default:
    692 		return (EINVAL);
    693 	}
    694 
    695 	kn->kn_hook = sc;
    696 
    697 	s = splusb();
    698 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    699 	splx(s);
    700 
    701 	return (0);
    702 }
    703