Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.32
      1 /*	$NetBSD: uhid.c,v 1.32 2000/02/02 13:18:45 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/uhid.c,v 1.22 1999/11/17 22:33:43 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (augustss (at) carlstedt.se) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * HID spec: http://www.usb.org/developers/data/usbhid10.pdf
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #if defined(__NetBSD__) || defined(__OpenBSD__)
     50 #include <sys/device.h>
     51 #include <sys/ioctl.h>
     52 #elif defined(__FreeBSD__)
     53 #include <sys/ioccom.h>
     54 #include <sys/filio.h>
     55 #include <sys/module.h>
     56 #include <sys/bus.h>
     57 #include <sys/ioccom.h>
     58 #endif
     59 #include <sys/conf.h>
     60 #include <sys/tty.h>
     61 #include <sys/file.h>
     62 #include <sys/select.h>
     63 #include <sys/proc.h>
     64 #include <sys/vnode.h>
     65 #include <sys/poll.h>
     66 
     67 #include <dev/usb/usb.h>
     68 #include <dev/usb/usbhid.h>
     69 
     70 #include <dev/usb/usbdi.h>
     71 #include <dev/usb/usbdi_util.h>
     72 #include <dev/usb/hid.h>
     73 #include <dev/usb/usb_quirks.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 	USBBASEDEVICE sc_dev;			/* base device */
     86 	usbd_device_handle sc_udev;
     87 	usbd_interface_handle sc_iface;	/* interface */
     88 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     89 	int sc_ep_addr;
     90 
     91 	int sc_isize;
     92 	int sc_osize;
     93 	int sc_fsize;
     94 	u_int8_t sc_iid;
     95 	u_int8_t sc_oid;
     96 	u_int8_t sc_fid;
     97 
     98 	char *sc_ibuf;
     99 	char *sc_obuf;
    100 
    101 	void *sc_repdesc;
    102 	int sc_repdesc_size;
    103 
    104 	struct clist sc_q;
    105 	struct selinfo sc_rsel;
    106 	u_char sc_state;	/* driver state */
    107 #define	UHID_OPEN	0x01	/* device is open */
    108 #define	UHID_ASLP	0x02	/* waiting for device data */
    109 #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
    110 #define UHID_IMMED	0x08	/* return read data immediately */
    111 
    112 	int sc_refcnt;
    113 	u_char sc_dying;
    114 };
    115 
    116 #define	UHIDUNIT(dev)	(minor(dev))
    117 #define	UHID_CHUNK	128	/* chunk size for read */
    118 #define	UHID_BSIZE	1020	/* buffer size */
    119 
    120 #if defined(__NetBSD__) || defined(__OpenBSD__)
    121 cdev_decl(uhid);
    122 #elif defined(__FreeBSD__)
    123 d_open_t	uhidopen;
    124 d_close_t	uhidclose;
    125 d_read_t	uhidread;
    126 d_write_t	uhidwrite;
    127 d_ioctl_t	uhidioctl;
    128 d_poll_t	uhidpoll;
    129 
    130 #define		UHID_CDEV_MAJOR 122
    131 
    132 static struct cdevsw uhid_cdevsw = {
    133 	/* open */	uhidopen,
    134 	/* close */	uhidclose,
    135 	/* read */	uhidread,
    136 	/* write */	uhidwrite,
    137 	/* ioctl */	uhidioctl,
    138 	/* poll */	uhidpoll,
    139 	/* mmap */	nommap,
    140 	/* strategy */	nostrategy,
    141 	/* name */	"uhid",
    142 	/* maj */	UHID_CDEV_MAJOR,
    143 	/* dump */	nodump,
    144 	/* psize */	nopsize,
    145 	/* flags */	0,
    146 	/* bmaj */	-1
    147 };
    148 #endif
    149 
    150 static void uhid_intr __P((usbd_xfer_handle, usbd_private_handle,
    151 			   usbd_status));
    152 
    153 static int uhid_do_read __P((struct uhid_softc *, struct uio *uio, int));
    154 static int uhid_do_write __P((struct uhid_softc *, struct uio *uio, int));
    155 static int uhid_do_ioctl __P((struct uhid_softc *, u_long, caddr_t, int,
    156 			      struct proc *));
    157 
    158 USB_DECLARE_DRIVER(uhid);
    159 
    160 USB_MATCH(uhid)
    161 {
    162 	USB_MATCH_START(uhid, uaa);
    163 	usb_interface_descriptor_t *id;
    164 
    165 	if (uaa->iface == NULL)
    166 		return (UMATCH_NONE);
    167 	id = usbd_get_interface_descriptor(uaa->iface);
    168 	if (id == NULL || id->bInterfaceClass != UCLASS_HID)
    169 		return (UMATCH_NONE);
    170 	return (UMATCH_IFACECLASS_GENERIC);
    171 }
    172 
    173 USB_ATTACH(uhid)
    174 {
    175 	USB_ATTACH_START(uhid, sc, uaa);
    176 	usbd_interface_handle iface = uaa->iface;
    177 	usb_interface_descriptor_t *id;
    178 	usb_endpoint_descriptor_t *ed;
    179 	int size;
    180 	void *desc;
    181 	usbd_status err;
    182 	char devinfo[1024];
    183 
    184 	sc->sc_udev = uaa->device;
    185 	sc->sc_iface = iface;
    186 	id = usbd_get_interface_descriptor(iface);
    187 	usbd_devinfo(uaa->device, 0, devinfo);
    188 	USB_ATTACH_SETUP;
    189 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    190 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    191 
    192 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    193 	if (ed == NULL) {
    194 		printf("%s: could not read endpoint descriptor\n",
    195 		       USBDEVNAME(sc->sc_dev));
    196 		sc->sc_dying = 1;
    197 		USB_ATTACH_ERROR_RETURN;
    198 	}
    199 
    200 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
    201 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    202 		     " bInterval=%d\n",
    203 		     ed->bLength, ed->bDescriptorType,
    204 		     ed->bEndpointAddress & UE_ADDR,
    205 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    206 		     ed->bmAttributes & UE_XFERTYPE,
    207 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    208 
    209 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    210 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    211 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    212 		sc->sc_dying = 1;
    213 		USB_ATTACH_ERROR_RETURN;
    214 	}
    215 
    216 	sc->sc_ep_addr = ed->bEndpointAddress;
    217 
    218 	desc = 0;
    219 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
    220 	if (err) {
    221 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    222 		sc->sc_dying = 1;
    223 		USB_ATTACH_ERROR_RETURN;
    224 	}
    225 
    226 	(void)usbd_set_idle(iface, 0, 0);
    227 
    228 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    229 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    230 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    231 
    232 	sc->sc_repdesc = desc;
    233 	sc->sc_repdesc_size = size;
    234 
    235 #ifdef __FreeBSD__
    236 	{
    237 		static int global_init_done = 0;
    238 
    239 		if (!global_init_done) {
    240 			cdevsw_add(&uhid_cdevsw);
    241 			global_init_done = 1;
    242 		}
    243 	}
    244 #endif
    245 
    246 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    247 			   USBDEV(sc->sc_dev));
    248 
    249 	USB_ATTACH_SUCCESS_RETURN;
    250 }
    251 
    252 #if defined(__NetBSD__) || defined(__OpenBSD__)
    253 int
    254 uhid_activate(self, act)
    255 	device_ptr_t self;
    256 	enum devact act;
    257 {
    258 	struct uhid_softc *sc = (struct uhid_softc *)self;
    259 
    260 	switch (act) {
    261 	case DVACT_ACTIVATE:
    262 		return (EOPNOTSUPP);
    263 		break;
    264 
    265 	case DVACT_DEACTIVATE:
    266 		sc->sc_dying = 1;
    267 		break;
    268 	}
    269 	return (0);
    270 }
    271 #endif
    272 
    273 USB_DETACH(uhid)
    274 {
    275 	USB_DETACH_START(uhid, sc);
    276 	int s;
    277 #if defined(__NetBSD__) || defined(__OpenBSD__)
    278 	int maj, mn;
    279 
    280 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    281 #else
    282 	DPRINTF(("uhid_detach: sc=%p\n", sc));
    283 #endif
    284 
    285 	sc->sc_dying = 1;
    286 	if (sc->sc_intrpipe != NULL)
    287 		usbd_abort_pipe(sc->sc_intrpipe);
    288 
    289 	if (sc->sc_state & UHID_OPEN) {
    290 		s = splusb();
    291 		if (--sc->sc_refcnt >= 0) {
    292 			/* Wake everyone */
    293 			wakeup(&sc->sc_q);
    294 			/* Wait for processes to go away. */
    295 			usb_detach_wait(USBDEV(sc->sc_dev));
    296 		}
    297 		splx(s);
    298 	}
    299 
    300 #if defined(__NetBSD__) || defined(__OpenBSD__)
    301 	/* locate the major number */
    302 	for (maj = 0; maj < nchrdev; maj++)
    303 		if (cdevsw[maj].d_open == uhidopen)
    304 			break;
    305 
    306 	/* Nuke the vnodes for any open instances (calls close). */
    307 	mn = self->dv_unit;
    308 	vdevgone(maj, mn, mn, VCHR);
    309 #elif defined(__FreeBSD__)
    310 	/* XXX not implemented yet */
    311 #endif
    312 
    313 	free(sc->sc_repdesc, M_USBDEV);
    314 
    315 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    316 			   USBDEV(sc->sc_dev));
    317 
    318 	return (0);
    319 }
    320 
    321 void
    322 uhid_intr(xfer, addr, status)
    323 	usbd_xfer_handle xfer;
    324 	usbd_private_handle addr;
    325 	usbd_status status;
    326 {
    327 	struct uhid_softc *sc = addr;
    328 
    329 	DPRINTFN(5, ("uhid_intr: status=%d\n", status));
    330 	DPRINTFN(5, ("uhid_intr: data = %02x %02x %02x\n",
    331 		     sc->sc_ibuf[0], sc->sc_ibuf[1], sc->sc_ibuf[2]));
    332 
    333 	if (status == USBD_CANCELLED)
    334 		return;
    335 
    336 	if (status != USBD_NORMAL_COMPLETION) {
    337 		DPRINTF(("uhid_intr: status=%d\n", status));
    338 		sc->sc_state |= UHID_NEEDCLEAR;
    339 		return;
    340 	}
    341 
    342 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    343 
    344 	if (sc->sc_state & UHID_ASLP) {
    345 		sc->sc_state &= ~UHID_ASLP;
    346 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    347 		wakeup(&sc->sc_q);
    348 	}
    349 	selwakeup(&sc->sc_rsel);
    350 }
    351 
    352 int
    353 uhidopen(dev, flag, mode, p)
    354 	dev_t dev;
    355 	int flag;
    356 	int mode;
    357 	struct proc *p;
    358 {
    359 	struct uhid_softc *sc;
    360 	usbd_status err;
    361 
    362 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    363 
    364 	DPRINTF(("uhidopen: sc=%p\n", sc));
    365 
    366 	if (sc->sc_dying)
    367 		return (ENXIO);
    368 
    369 	if (sc->sc_state & UHID_OPEN)
    370 		return (EBUSY);
    371 	sc->sc_state |= UHID_OPEN;
    372 
    373 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    374 		sc->sc_state &= ~UHID_OPEN;
    375 		return (ENOMEM);
    376 	}
    377 
    378 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    379 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    380 
    381 	/* Set up interrupt pipe. */
    382 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    383 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    384 		  sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
    385 	if (err) {
    386 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    387 			 "error=%d\n",err));
    388 		free(sc->sc_ibuf, M_USBDEV);
    389 		free(sc->sc_obuf, M_USBDEV);
    390 		sc->sc_state &= ~UHID_OPEN;
    391 		return (EIO);
    392 	}
    393 
    394 	sc->sc_state &= ~UHID_IMMED;
    395 
    396 	return (0);
    397 }
    398 
    399 int
    400 uhidclose(dev, flag, mode, p)
    401 	dev_t dev;
    402 	int flag;
    403 	int mode;
    404 	struct proc *p;
    405 {
    406 	struct uhid_softc *sc;
    407 
    408 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    409 
    410 	DPRINTF(("uhidclose: sc=%p\n", sc));
    411 
    412 	/* Disable interrupts. */
    413 	usbd_abort_pipe(sc->sc_intrpipe);
    414 	usbd_close_pipe(sc->sc_intrpipe);
    415 	sc->sc_intrpipe = 0;
    416 
    417 	clfree(&sc->sc_q);
    418 
    419 	free(sc->sc_ibuf, M_USBDEV);
    420 	free(sc->sc_obuf, M_USBDEV);
    421 
    422 	sc->sc_state &= ~UHID_OPEN;
    423 
    424 	return (0);
    425 }
    426 
    427 int
    428 uhid_do_read(sc, uio, flag)
    429 	struct uhid_softc *sc;
    430 	struct uio *uio;
    431 	int flag;
    432 {
    433 	int s;
    434 	int error = 0;
    435 	size_t length;
    436 	u_char buffer[UHID_CHUNK];
    437 	usbd_status err;
    438 
    439 	DPRINTFN(1, ("uhidread\n"));
    440 	if (sc->sc_state & UHID_IMMED) {
    441 		DPRINTFN(1, ("uhidread immed\n"));
    442 
    443 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    444 				    sc->sc_iid, buffer, sc->sc_isize);
    445 		if (err)
    446 			return (EIO);
    447 		return (uiomove(buffer, sc->sc_isize, uio));
    448 	}
    449 
    450 	s = splusb();
    451 	while (sc->sc_q.c_cc == 0) {
    452 		if (flag & IO_NDELAY) {
    453 			splx(s);
    454 			return (EWOULDBLOCK);
    455 		}
    456 		sc->sc_state |= UHID_ASLP;
    457 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    458 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    459 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    460 		if (sc->sc_dying)
    461 			error = EIO;
    462 		if (error) {
    463 			sc->sc_state &= ~UHID_ASLP;
    464 			break;
    465 		}
    466 		if (sc->sc_state & UHID_NEEDCLEAR) {
    467 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    468 			sc->sc_state &= ~UHID_NEEDCLEAR;
    469 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    470 		}
    471 	}
    472 	splx(s);
    473 
    474 	/* Transfer as many chunks as possible. */
    475 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    476 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    477 		if (length > sizeof(buffer))
    478 			length = sizeof(buffer);
    479 
    480 		/* Remove a small chunk from the input queue. */
    481 		(void) q_to_b(&sc->sc_q, buffer, length);
    482 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    483 
    484 		/* Copy the data to the user process. */
    485 		if ((error = uiomove(buffer, length, uio)) != 0)
    486 			break;
    487 	}
    488 
    489 	return (error);
    490 }
    491 
    492 int
    493 uhidread(dev, uio, flag)
    494 	dev_t dev;
    495 	struct uio *uio;
    496 	int flag;
    497 {
    498 	struct uhid_softc *sc;
    499 	int error;
    500 
    501 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    502 
    503 	sc->sc_refcnt++;
    504 	error = uhid_do_read(sc, uio, flag);
    505 	if (--sc->sc_refcnt < 0)
    506 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    507 	return (error);
    508 }
    509 
    510 int
    511 uhid_do_write(sc, uio, flag)
    512 	struct uhid_softc *sc;
    513 	struct uio *uio;
    514 	int flag;
    515 {
    516 	int error;
    517 	int size;
    518 	usbd_status err;
    519 
    520 	DPRINTFN(1, ("uhidwrite\n"));
    521 
    522 	if (sc->sc_dying)
    523 		return (EIO);
    524 
    525 	size = sc->sc_osize;
    526 	error = 0;
    527 	if (uio->uio_resid != size)
    528 		return (EINVAL);
    529 	error = uiomove(sc->sc_obuf, size, uio);
    530 	if (!error) {
    531 		if (sc->sc_oid)
    532 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    533 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
    534 		else
    535 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    536 				  0, sc->sc_obuf, size);
    537 		if (err)
    538 			error = EIO;
    539 	}
    540 
    541 	return (error);
    542 }
    543 
    544 int
    545 uhidwrite(dev, uio, flag)
    546 	dev_t dev;
    547 	struct uio *uio;
    548 	int flag;
    549 {
    550 	struct uhid_softc *sc;
    551 	int error;
    552 
    553 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    554 
    555 	sc->sc_refcnt++;
    556 	error = uhid_do_write(sc, uio, flag);
    557 	if (--sc->sc_refcnt < 0)
    558 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    559 	return (error);
    560 }
    561 
    562 int
    563 uhid_do_ioctl(sc, cmd, addr, flag, p)
    564 	struct uhid_softc *sc;
    565 	u_long cmd;
    566 	caddr_t addr;
    567 	int flag;
    568 	struct proc *p;
    569 {
    570 	struct usb_ctl_report_desc *rd;
    571 	struct usb_ctl_report *re;
    572 	int size, id;
    573 	usbd_status err;
    574 
    575 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    576 
    577 	if (sc->sc_dying)
    578 		return (EIO);
    579 
    580 	switch (cmd) {
    581 	case FIONBIO:
    582 		/* All handled in the upper FS layer. */
    583 		break;
    584 
    585 	case USB_GET_REPORT_DESC:
    586 		rd = (struct usb_ctl_report_desc *)addr;
    587 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    588 		rd->size = size;
    589 		memcpy(rd->data, sc->sc_repdesc, size);
    590 		break;
    591 
    592 	case USB_SET_IMMED:
    593 		if (*(int *)addr) {
    594 			/* XXX should read into ibuf, but does it matter? */
    595 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    596 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    597 			if (err)
    598 				return (EOPNOTSUPP);
    599 
    600 			sc->sc_state |=  UHID_IMMED;
    601 		} else
    602 			sc->sc_state &= ~UHID_IMMED;
    603 		break;
    604 
    605 	case USB_GET_REPORT:
    606 		re = (struct usb_ctl_report *)addr;
    607 		switch (re->report) {
    608 		case UHID_INPUT_REPORT:
    609 			size = sc->sc_isize;
    610 			id = sc->sc_iid;
    611 			break;
    612 		case UHID_OUTPUT_REPORT:
    613 			size = sc->sc_osize;
    614 			id = sc->sc_oid;
    615 			break;
    616 		case UHID_FEATURE_REPORT:
    617 			size = sc->sc_fsize;
    618 			id = sc->sc_fid;
    619 			break;
    620 		default:
    621 			return (EINVAL);
    622 		}
    623 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
    624 			  size);
    625 		if (err)
    626 			return (EIO);
    627 		break;
    628 
    629 	default:
    630 		return (EINVAL);
    631 	}
    632 	return (0);
    633 }
    634 
    635 int
    636 uhidioctl(dev, cmd, addr, flag, p)
    637 	dev_t dev;
    638 	u_long cmd;
    639 	caddr_t addr;
    640 	int flag;
    641 	struct proc *p;
    642 {
    643 	struct uhid_softc *sc;
    644 	int error;
    645 
    646 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    647 
    648 	sc->sc_refcnt++;
    649 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
    650 	if (--sc->sc_refcnt < 0)
    651 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    652 	return (error);
    653 }
    654 
    655 int
    656 uhidpoll(dev, events, p)
    657 	dev_t dev;
    658 	int events;
    659 	struct proc *p;
    660 {
    661 	struct uhid_softc *sc;
    662 	int revents = 0;
    663 	int s;
    664 
    665 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    666 
    667 	if (sc->sc_dying)
    668 		return (EIO);
    669 
    670 	s = splusb();
    671 	if (events & (POLLOUT | POLLWRNORM))
    672 		revents |= events & (POLLOUT | POLLWRNORM);
    673 	if (events & (POLLIN | POLLRDNORM)) {
    674 		if (sc->sc_q.c_cc > 0)
    675 			revents |= events & (POLLIN | POLLRDNORM);
    676 		else
    677 			selrecord(p, &sc->sc_rsel);
    678 	}
    679 
    680 	splx(s);
    681 	return (revents);
    682 }
    683 
    684 #if defined(__FreeBSD__)
    685 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
    686 #endif
    687