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