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