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