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