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