Home | History | Annotate | Line # | Download | only in usb
uhid.c revision 1.37
      1 /*	$NetBSD: uhid.c,v 1.37 2000/04/14 14:12:47 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 #include <sys/signalvar.h>
     50 #if defined(__NetBSD__) || defined(__OpenBSD__)
     51 #include <sys/device.h>
     52 #include <sys/ioctl.h>
     53 #elif defined(__FreeBSD__)
     54 #include <sys/ioccom.h>
     55 #include <sys/filio.h>
     56 #include <sys/module.h>
     57 #include <sys/bus.h>
     58 #include <sys/ioccom.h>
     59 #endif
     60 #include <sys/conf.h>
     61 #include <sys/tty.h>
     62 #include <sys/file.h>
     63 #include <sys/select.h>
     64 #include <sys/proc.h>
     65 #include <sys/vnode.h>
     66 #include <sys/poll.h>
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbhid.h>
     70 
     71 #include <dev/usb/usbdi.h>
     72 #include <dev/usb/usbdi_util.h>
     73 #include <dev/usb/hid.h>
     74 #include <dev/usb/usb_quirks.h>
     75 
     76 #ifdef UHID_DEBUG
     77 #define DPRINTF(x)	if (uhiddebug) logprintf x
     78 #define DPRINTFN(n,x)	if (uhiddebug>(n)) logprintf x
     79 int	uhiddebug = 0;
     80 #else
     81 #define DPRINTF(x)
     82 #define DPRINTFN(n,x)
     83 #endif
     84 
     85 struct uhid_softc {
     86 	USBBASEDEVICE sc_dev;			/* base device */
     87 	usbd_device_handle sc_udev;
     88 	usbd_interface_handle sc_iface;	/* interface */
     89 	usbd_pipe_handle sc_intrpipe;	/* interrupt pipe */
     90 	int sc_ep_addr;
     91 
     92 	int sc_isize;
     93 	int sc_osize;
     94 	int sc_fsize;
     95 	u_int8_t sc_iid;
     96 	u_int8_t sc_oid;
     97 	u_int8_t sc_fid;
     98 
     99 	u_char *sc_ibuf;
    100 	u_char *sc_obuf;
    101 
    102 	void *sc_repdesc;
    103 	int sc_repdesc_size;
    104 
    105 	struct clist sc_q;
    106 	struct selinfo sc_rsel;
    107 	struct proc *sc_async;	/* process that wants SIGIO */
    108 	u_char sc_state;	/* driver state */
    109 #define	UHID_OPEN	0x01	/* device is open */
    110 #define	UHID_ASLP	0x02	/* waiting for device data */
    111 #define UHID_NEEDCLEAR	0x04	/* needs clearing endpoint stall */
    112 #define UHID_IMMED	0x08	/* return read data immediately */
    113 
    114 	int sc_refcnt;
    115 	u_char sc_dying;
    116 };
    117 
    118 #define	UHIDUNIT(dev)	(minor(dev))
    119 #define	UHID_CHUNK	128	/* chunk size for read */
    120 #define	UHID_BSIZE	1020	/* buffer size */
    121 
    122 #if defined(__NetBSD__) || defined(__OpenBSD__)
    123 cdev_decl(uhid);
    124 #elif defined(__FreeBSD__)
    125 d_open_t	uhidopen;
    126 d_close_t	uhidclose;
    127 d_read_t	uhidread;
    128 d_write_t	uhidwrite;
    129 d_ioctl_t	uhidioctl;
    130 d_poll_t	uhidpoll;
    131 
    132 #define		UHID_CDEV_MAJOR 122
    133 
    134 Static struct cdevsw uhid_cdevsw = {
    135 	/* open */	uhidopen,
    136 	/* close */	uhidclose,
    137 	/* read */	uhidread,
    138 	/* write */	uhidwrite,
    139 	/* ioctl */	uhidioctl,
    140 	/* poll */	uhidpoll,
    141 	/* mmap */	nommap,
    142 	/* strategy */	nostrategy,
    143 	/* name */	"uhid",
    144 	/* maj */	UHID_CDEV_MAJOR,
    145 	/* dump */	nodump,
    146 	/* psize */	nopsize,
    147 	/* flags */	0,
    148 	/* bmaj */	-1
    149 };
    150 #endif
    151 
    152 Static void uhid_intr __P((usbd_xfer_handle, usbd_private_handle,
    153 			   usbd_status));
    154 
    155 Static int uhid_do_read __P((struct uhid_softc *, struct uio *uio, int));
    156 Static int uhid_do_write __P((struct uhid_softc *, struct uio *uio, int));
    157 Static int uhid_do_ioctl __P((struct uhid_softc *, u_long, caddr_t, int,
    158 			      struct proc *));
    159 
    160 USB_DECLARE_DRIVER(uhid);
    161 
    162 USB_MATCH(uhid)
    163 {
    164 	USB_MATCH_START(uhid, uaa);
    165 	usb_interface_descriptor_t *id;
    166 
    167 	if (uaa->iface == NULL)
    168 		return (UMATCH_NONE);
    169 	id = usbd_get_interface_descriptor(uaa->iface);
    170 	if (id == NULL || id->bInterfaceClass != UICLASS_HID)
    171 		return (UMATCH_NONE);
    172 	return (UMATCH_IFACECLASS_GENERIC);
    173 }
    174 
    175 USB_ATTACH(uhid)
    176 {
    177 	USB_ATTACH_START(uhid, sc, uaa);
    178 	usbd_interface_handle iface = uaa->iface;
    179 	usb_interface_descriptor_t *id;
    180 	usb_endpoint_descriptor_t *ed;
    181 	int size;
    182 	void *desc;
    183 	usbd_status err;
    184 	char devinfo[1024];
    185 
    186 	sc->sc_udev = uaa->device;
    187 	sc->sc_iface = iface;
    188 	id = usbd_get_interface_descriptor(iface);
    189 	usbd_devinfo(uaa->device, 0, devinfo);
    190 	USB_ATTACH_SETUP;
    191 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    192 	       devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    193 
    194 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    195 	if (ed == NULL) {
    196 		printf("%s: could not read endpoint descriptor\n",
    197 		       USBDEVNAME(sc->sc_dev));
    198 		sc->sc_dying = 1;
    199 		USB_ATTACH_ERROR_RETURN;
    200 	}
    201 
    202 	DPRINTFN(10,("uhid_attach: bLength=%d bDescriptorType=%d "
    203 		     "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    204 		     " bInterval=%d\n",
    205 		     ed->bLength, ed->bDescriptorType,
    206 		     ed->bEndpointAddress & UE_ADDR,
    207 		     UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    208 		     ed->bmAttributes & UE_XFERTYPE,
    209 		     UGETW(ed->wMaxPacketSize), ed->bInterval));
    210 
    211 	if (UE_GET_DIR(ed->bEndpointAddress) != UE_DIR_IN ||
    212 	    (ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    213 		printf("%s: unexpected endpoint\n", USBDEVNAME(sc->sc_dev));
    214 		sc->sc_dying = 1;
    215 		USB_ATTACH_ERROR_RETURN;
    216 	}
    217 
    218 	sc->sc_ep_addr = ed->bEndpointAddress;
    219 
    220 	desc = 0;
    221 	err = usbd_alloc_report_desc(uaa->iface, &desc, &size, M_USBDEV);
    222 	if (err) {
    223 		printf("%s: no report descriptor\n", USBDEVNAME(sc->sc_dev));
    224 		sc->sc_dying = 1;
    225 		USB_ATTACH_ERROR_RETURN;
    226 	}
    227 
    228 	(void)usbd_set_idle(iface, 0, 0);
    229 
    230 	sc->sc_isize = hid_report_size(desc, size, hid_input,   &sc->sc_iid);
    231 	sc->sc_osize = hid_report_size(desc, size, hid_output,  &sc->sc_oid);
    232 	sc->sc_fsize = hid_report_size(desc, size, hid_feature, &sc->sc_fid);
    233 
    234 	sc->sc_repdesc = desc;
    235 	sc->sc_repdesc_size = size;
    236 
    237 #ifdef __FreeBSD__
    238 	{
    239 		static int global_init_done = 0;
    240 
    241 		if (!global_init_done) {
    242 			cdevsw_add(&uhid_cdevsw);
    243 			global_init_done = 1;
    244 		}
    245 	}
    246 #endif
    247 
    248 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    249 			   USBDEV(sc->sc_dev));
    250 
    251 	USB_ATTACH_SUCCESS_RETURN;
    252 }
    253 
    254 #if defined(__NetBSD__) || defined(__OpenBSD__)
    255 int
    256 uhid_activate(self, act)
    257 	device_ptr_t self;
    258 	enum devact act;
    259 {
    260 	struct uhid_softc *sc = (struct uhid_softc *)self;
    261 
    262 	switch (act) {
    263 	case DVACT_ACTIVATE:
    264 		return (EOPNOTSUPP);
    265 		break;
    266 
    267 	case DVACT_DEACTIVATE:
    268 		sc->sc_dying = 1;
    269 		break;
    270 	}
    271 	return (0);
    272 }
    273 #endif
    274 
    275 USB_DETACH(uhid)
    276 {
    277 	USB_DETACH_START(uhid, sc);
    278 	int s;
    279 #if defined(__NetBSD__) || defined(__OpenBSD__)
    280 	int maj, mn;
    281 
    282 	DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
    283 #else
    284 	DPRINTF(("uhid_detach: sc=%p\n", sc));
    285 #endif
    286 
    287 	sc->sc_dying = 1;
    288 	if (sc->sc_intrpipe != NULL)
    289 		usbd_abort_pipe(sc->sc_intrpipe);
    290 
    291 	if (sc->sc_state & UHID_OPEN) {
    292 		s = splusb();
    293 		if (--sc->sc_refcnt >= 0) {
    294 			/* Wake everyone */
    295 			wakeup(&sc->sc_q);
    296 			/* Wait for processes to go away. */
    297 			usb_detach_wait(USBDEV(sc->sc_dev));
    298 		}
    299 		splx(s);
    300 	}
    301 
    302 #if defined(__NetBSD__) || defined(__OpenBSD__)
    303 	/* locate the major number */
    304 	for (maj = 0; maj < nchrdev; maj++)
    305 		if (cdevsw[maj].d_open == uhidopen)
    306 			break;
    307 
    308 	/* Nuke the vnodes for any open instances (calls close). */
    309 	mn = self->dv_unit;
    310 	vdevgone(maj, mn, mn, VCHR);
    311 #elif defined(__FreeBSD__)
    312 	/* XXX not implemented yet */
    313 #endif
    314 
    315 	free(sc->sc_repdesc, M_USBDEV);
    316 
    317 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    318 			   USBDEV(sc->sc_dev));
    319 
    320 	return (0);
    321 }
    322 
    323 void
    324 uhid_intr(xfer, addr, status)
    325 	usbd_xfer_handle xfer;
    326 	usbd_private_handle addr;
    327 	usbd_status status;
    328 {
    329 	struct uhid_softc *sc = addr;
    330 
    331 #ifdef UHID_DEBUG
    332 	if (uhiddebug > 5) {
    333 		u_int32_t cc, i;
    334 
    335 		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    336 		DPRINTF(("uhid_intr: status=%d cc=%d\n", status, cc));
    337 		DPRINTF(("uhid_intr: data ="));
    338 		for (i = 0; i < cc; i++)
    339 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    340 		DPRINTF(("\n"));
    341 	}
    342 #endif
    343 
    344 	if (status == USBD_CANCELLED)
    345 		return;
    346 
    347 	if (status != USBD_NORMAL_COMPLETION) {
    348 		DPRINTF(("uhid_intr: status=%d\n", status));
    349 		sc->sc_state |= UHID_NEEDCLEAR;
    350 		return;
    351 	}
    352 
    353 	(void) b_to_q(sc->sc_ibuf, sc->sc_isize, &sc->sc_q);
    354 
    355 	if (sc->sc_state & UHID_ASLP) {
    356 		sc->sc_state &= ~UHID_ASLP;
    357 		DPRINTFN(5, ("uhid_intr: waking %p\n", sc));
    358 		wakeup(&sc->sc_q);
    359 	}
    360 	selwakeup(&sc->sc_rsel);
    361 	if (sc->sc_async != NULL) {
    362 		DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
    363 		psignal(sc->sc_async, SIGIO);
    364 	}
    365 }
    366 
    367 int
    368 uhidopen(dev, flag, mode, p)
    369 	dev_t dev;
    370 	int flag;
    371 	int mode;
    372 	struct proc *p;
    373 {
    374 	struct uhid_softc *sc;
    375 	usbd_status err;
    376 
    377 	USB_GET_SC_OPEN(uhid, UHIDUNIT(dev), sc);
    378 
    379 	DPRINTF(("uhidopen: sc=%p\n", sc));
    380 
    381 	if (sc->sc_dying)
    382 		return (ENXIO);
    383 
    384 	if (sc->sc_state & UHID_OPEN)
    385 		return (EBUSY);
    386 	sc->sc_state |= UHID_OPEN;
    387 
    388 	if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
    389 		sc->sc_state &= ~UHID_OPEN;
    390 		return (ENOMEM);
    391 	}
    392 
    393 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    394 	sc->sc_obuf = malloc(sc->sc_osize, M_USBDEV, M_WAITOK);
    395 
    396 	/* Set up interrupt pipe. */
    397 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ep_addr,
    398 		  USBD_SHORT_XFER_OK, &sc->sc_intrpipe, sc, sc->sc_ibuf,
    399 		  sc->sc_isize, uhid_intr, USBD_DEFAULT_INTERVAL);
    400 	if (err) {
    401 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    402 			 "error=%d\n",err));
    403 		free(sc->sc_ibuf, M_USBDEV);
    404 		free(sc->sc_obuf, M_USBDEV);
    405 		sc->sc_state &= ~UHID_OPEN;
    406 		return (EIO);
    407 	}
    408 
    409 	sc->sc_state &= ~UHID_IMMED;
    410 
    411 	sc->sc_async = 0;
    412 
    413 	return (0);
    414 }
    415 
    416 int
    417 uhidclose(dev, flag, mode, p)
    418 	dev_t dev;
    419 	int flag;
    420 	int mode;
    421 	struct proc *p;
    422 {
    423 	struct uhid_softc *sc;
    424 
    425 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    426 
    427 	DPRINTF(("uhidclose: sc=%p\n", sc));
    428 
    429 	/* Disable interrupts. */
    430 	usbd_abort_pipe(sc->sc_intrpipe);
    431 	usbd_close_pipe(sc->sc_intrpipe);
    432 	sc->sc_intrpipe = 0;
    433 
    434 	clfree(&sc->sc_q);
    435 
    436 	free(sc->sc_ibuf, M_USBDEV);
    437 	free(sc->sc_obuf, M_USBDEV);
    438 
    439 	sc->sc_state &= ~UHID_OPEN;
    440 
    441 	sc->sc_async = 0;
    442 
    443 	return (0);
    444 }
    445 
    446 int
    447 uhid_do_read(sc, uio, flag)
    448 	struct uhid_softc *sc;
    449 	struct uio *uio;
    450 	int flag;
    451 {
    452 	int s;
    453 	int error = 0;
    454 	size_t length;
    455 	u_char buffer[UHID_CHUNK];
    456 	usbd_status err;
    457 
    458 	DPRINTFN(1, ("uhidread\n"));
    459 	if (sc->sc_state & UHID_IMMED) {
    460 		DPRINTFN(1, ("uhidread immed\n"));
    461 
    462 		err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    463 				    sc->sc_iid, buffer, sc->sc_isize);
    464 		if (err)
    465 			return (EIO);
    466 		return (uiomove(buffer, sc->sc_isize, uio));
    467 	}
    468 
    469 	s = splusb();
    470 	while (sc->sc_q.c_cc == 0) {
    471 		if (flag & IO_NDELAY) {
    472 			splx(s);
    473 			return (EWOULDBLOCK);
    474 		}
    475 		sc->sc_state |= UHID_ASLP;
    476 		DPRINTFN(5, ("uhidread: sleep on %p\n", sc));
    477 		error = tsleep(&sc->sc_q, PZERO | PCATCH, "uhidrea", 0);
    478 		DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
    479 		if (sc->sc_dying)
    480 			error = EIO;
    481 		if (error) {
    482 			sc->sc_state &= ~UHID_ASLP;
    483 			break;
    484 		}
    485 		if (sc->sc_state & UHID_NEEDCLEAR) {
    486 			DPRINTFN(-1,("uhidread: clearing stall\n"));
    487 			sc->sc_state &= ~UHID_NEEDCLEAR;
    488 			usbd_clear_endpoint_stall(sc->sc_intrpipe);
    489 		}
    490 	}
    491 	splx(s);
    492 
    493 	/* Transfer as many chunks as possible. */
    494 	while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    495 		length = min(sc->sc_q.c_cc, uio->uio_resid);
    496 		if (length > sizeof(buffer))
    497 			length = sizeof(buffer);
    498 
    499 		/* Remove a small chunk from the input queue. */
    500 		(void) q_to_b(&sc->sc_q, buffer, length);
    501 		DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
    502 
    503 		/* Copy the data to the user process. */
    504 		if ((error = uiomove(buffer, length, uio)) != 0)
    505 			break;
    506 	}
    507 
    508 	return (error);
    509 }
    510 
    511 int
    512 uhidread(dev, uio, flag)
    513 	dev_t dev;
    514 	struct uio *uio;
    515 	int flag;
    516 {
    517 	struct uhid_softc *sc;
    518 	int error;
    519 
    520 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    521 
    522 	sc->sc_refcnt++;
    523 	error = uhid_do_read(sc, uio, flag);
    524 	if (--sc->sc_refcnt < 0)
    525 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    526 	return (error);
    527 }
    528 
    529 int
    530 uhid_do_write(sc, uio, flag)
    531 	struct uhid_softc *sc;
    532 	struct uio *uio;
    533 	int flag;
    534 {
    535 	int error;
    536 	int size;
    537 	usbd_status err;
    538 
    539 	DPRINTFN(1, ("uhidwrite\n"));
    540 
    541 	if (sc->sc_dying)
    542 		return (EIO);
    543 
    544 	size = sc->sc_osize;
    545 	error = 0;
    546 	if (uio->uio_resid != size)
    547 		return (EINVAL);
    548 	error = uiomove(sc->sc_obuf, size, uio);
    549 	if (!error) {
    550 		if (sc->sc_oid)
    551 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    552 				  sc->sc_obuf[0], sc->sc_obuf+1, size-1);
    553 		else
    554 			err = usbd_set_report(sc->sc_iface, UHID_OUTPUT_REPORT,
    555 				  0, sc->sc_obuf, size);
    556 		if (err)
    557 			error = EIO;
    558 	}
    559 
    560 	return (error);
    561 }
    562 
    563 int
    564 uhidwrite(dev, uio, flag)
    565 	dev_t dev;
    566 	struct uio *uio;
    567 	int flag;
    568 {
    569 	struct uhid_softc *sc;
    570 	int error;
    571 
    572 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    573 
    574 	sc->sc_refcnt++;
    575 	error = uhid_do_write(sc, uio, flag);
    576 	if (--sc->sc_refcnt < 0)
    577 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    578 	return (error);
    579 }
    580 
    581 int
    582 uhid_do_ioctl(sc, cmd, addr, flag, p)
    583 	struct uhid_softc *sc;
    584 	u_long cmd;
    585 	caddr_t addr;
    586 	int flag;
    587 	struct proc *p;
    588 {
    589 	struct usb_ctl_report_desc *rd;
    590 	struct usb_ctl_report *re;
    591 	int size, id;
    592 	usbd_status err;
    593 
    594 	DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
    595 
    596 	if (sc->sc_dying)
    597 		return (EIO);
    598 
    599 	switch (cmd) {
    600 	case FIONBIO:
    601 		/* All handled in the upper FS layer. */
    602 		break;
    603 
    604 	case FIOASYNC:
    605 		if (*(int *)addr) {
    606 			if (sc->sc_async != NULL)
    607 				return (EBUSY);
    608 			sc->sc_async = p;
    609 			DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", p));
    610 		} else
    611 			sc->sc_async = NULL;
    612 		break;
    613 
    614 	/* XXX this is not the most general solution. */
    615 	case TIOCSPGRP:
    616 		if (sc->sc_async == NULL)
    617 			return (EINVAL);
    618 		if (*(int *)addr != sc->sc_async->p_pgid)
    619 			return (EPERM);
    620 		break;
    621 
    622 	case USB_GET_REPORT_DESC:
    623 		rd = (struct usb_ctl_report_desc *)addr;
    624 		size = min(sc->sc_repdesc_size, sizeof rd->data);
    625 		rd->size = size;
    626 		memcpy(rd->data, sc->sc_repdesc, size);
    627 		break;
    628 
    629 	case USB_SET_IMMED:
    630 		if (*(int *)addr) {
    631 			/* XXX should read into ibuf, but does it matter? */
    632 			err = usbd_get_report(sc->sc_iface, UHID_INPUT_REPORT,
    633 				  sc->sc_iid, sc->sc_ibuf, sc->sc_isize);
    634 			if (err)
    635 				return (EOPNOTSUPP);
    636 
    637 			sc->sc_state |=  UHID_IMMED;
    638 		} else
    639 			sc->sc_state &= ~UHID_IMMED;
    640 		break;
    641 
    642 	case USB_GET_REPORT:
    643 		re = (struct usb_ctl_report *)addr;
    644 		switch (re->report) {
    645 		case UHID_INPUT_REPORT:
    646 			size = sc->sc_isize;
    647 			id = sc->sc_iid;
    648 			break;
    649 		case UHID_OUTPUT_REPORT:
    650 			size = sc->sc_osize;
    651 			id = sc->sc_oid;
    652 			break;
    653 		case UHID_FEATURE_REPORT:
    654 			size = sc->sc_fsize;
    655 			id = sc->sc_fid;
    656 			break;
    657 		default:
    658 			return (EINVAL);
    659 		}
    660 		err = usbd_get_report(sc->sc_iface, re->report, id, re->data,
    661 			  size);
    662 		if (err)
    663 			return (EIO);
    664 		break;
    665 
    666 	case USB_SET_REPORT:
    667 		re = (struct usb_ctl_report *)addr;
    668 		switch (re->report) {
    669 		case UHID_INPUT_REPORT:
    670 			size = sc->sc_isize;
    671 			id = sc->sc_iid;
    672 			break;
    673 		case UHID_OUTPUT_REPORT:
    674 			size = sc->sc_osize;
    675 			id = sc->sc_oid;
    676 			break;
    677 		case UHID_FEATURE_REPORT:
    678 			size = sc->sc_fsize;
    679 			id = sc->sc_fid;
    680 			break;
    681 		default:
    682 			return (EINVAL);
    683 		}
    684 		err = usbd_set_report(sc->sc_iface, re->report, id, re->data,
    685 			  size);
    686 		if (err)
    687 			return (EIO);
    688 		break;
    689 
    690 	default:
    691 		return (EINVAL);
    692 	}
    693 	return (0);
    694 }
    695 
    696 int
    697 uhidioctl(dev, cmd, addr, flag, p)
    698 	dev_t dev;
    699 	u_long cmd;
    700 	caddr_t addr;
    701 	int flag;
    702 	struct proc *p;
    703 {
    704 	struct uhid_softc *sc;
    705 	int error;
    706 
    707 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    708 
    709 	sc->sc_refcnt++;
    710 	error = uhid_do_ioctl(sc, cmd, addr, flag, p);
    711 	if (--sc->sc_refcnt < 0)
    712 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    713 	return (error);
    714 }
    715 
    716 int
    717 uhidpoll(dev, events, p)
    718 	dev_t dev;
    719 	int events;
    720 	struct proc *p;
    721 {
    722 	struct uhid_softc *sc;
    723 	int revents = 0;
    724 	int s;
    725 
    726 	USB_GET_SC(uhid, UHIDUNIT(dev), sc);
    727 
    728 	if (sc->sc_dying)
    729 		return (EIO);
    730 
    731 	s = splusb();
    732 	if (events & (POLLOUT | POLLWRNORM))
    733 		revents |= events & (POLLOUT | POLLWRNORM);
    734 	if (events & (POLLIN | POLLRDNORM)) {
    735 		if (sc->sc_q.c_cc > 0)
    736 			revents |= events & (POLLIN | POLLRDNORM);
    737 		else
    738 			selrecord(p, &sc->sc_rsel);
    739 	}
    740 
    741 	splx(s);
    742 	return (revents);
    743 }
    744 
    745 #if defined(__FreeBSD__)
    746 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, usbd_driver_load, 0);
    747 #endif
    748