Home | History | Annotate | Line # | Download | only in usb
ugen.c revision 1.56
      1 /*	$NetBSD: ugen.c,v 1.56 2002/01/02 16:20:14 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/ugen.c,v 1.26 1999/11/17 22:33:41 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 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: ugen.c,v 1.56 2002/01/02 16:20:14 augustss Exp $");
     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/module.h>
     54 #include <sys/bus.h>
     55 #include <sys/ioccom.h>
     56 #include <sys/conf.h>
     57 #include <sys/fcntl.h>
     58 #include <sys/filio.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/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 
     72 #ifdef UGEN_DEBUG
     73 #define DPRINTF(x)	if (ugendebug) logprintf x
     74 #define DPRINTFN(n,x)	if (ugendebug>(n)) logprintf x
     75 int	ugendebug = 0;
     76 #else
     77 #define DPRINTF(x)
     78 #define DPRINTFN(n,x)
     79 #endif
     80 
     81 #define	UGEN_CHUNK	128	/* chunk size for read */
     82 #define	UGEN_IBSIZE	1020	/* buffer size */
     83 #define	UGEN_BBSIZE	1024
     84 
     85 #define	UGEN_NISOFRAMES	500	/* 0.5 seconds worth */
     86 #define UGEN_NISOREQS	6	/* number of outstanding xfer requests */
     87 #define UGEN_NISORFRMS	4	/* number of frames (miliseconds) per req */
     88 
     89 struct ugen_endpoint {
     90 	struct ugen_softc *sc;
     91 	usb_endpoint_descriptor_t *edesc;
     92 	usbd_interface_handle iface;
     93 	int state;
     94 #define	UGEN_ASLP	0x02	/* waiting for data */
     95 #define UGEN_SHORT_OK	0x04	/* short xfers are OK */
     96 	usbd_pipe_handle pipeh;
     97 	struct clist q;
     98 	struct selinfo rsel;
     99 	u_char *ibuf;		/* start of buffer (circular for isoc) */
    100 	u_char *fill;		/* location for input (isoc) */
    101 	u_char *limit;		/* end of circular buffer (isoc) */
    102 	u_char *cur;		/* current read location (isoc) */
    103 	u_int32_t timeout;
    104 	struct isoreq {
    105 		struct ugen_endpoint *sce;
    106 		usbd_xfer_handle xfer;
    107 		void *dmabuf;
    108 		u_int16_t sizes[UGEN_NISORFRMS];
    109 	} isoreqs[UGEN_NISOREQS];
    110 };
    111 
    112 struct ugen_softc {
    113 	USBBASEDEVICE sc_dev;		/* base device */
    114 	usbd_device_handle sc_udev;
    115 
    116 	char sc_is_open[USB_MAX_ENDPOINTS];
    117 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
    118 #define OUT 0
    119 #define IN  1
    120 
    121 	int sc_refcnt;
    122 	u_char sc_dying;
    123 };
    124 
    125 #if defined(__NetBSD__) || defined(__OpenBSD__)
    126 cdev_decl(ugen);
    127 #elif defined(__FreeBSD__)
    128 d_open_t  ugenopen;
    129 d_close_t ugenclose;
    130 d_read_t  ugenread;
    131 d_write_t ugenwrite;
    132 d_ioctl_t ugenioctl;
    133 d_poll_t  ugenpoll;
    134 
    135 #define UGEN_CDEV_MAJOR	114
    136 
    137 Static struct cdevsw ugen_cdevsw = {
    138 	/* open */	ugenopen,
    139 	/* close */	ugenclose,
    140 	/* read */	ugenread,
    141 	/* write */	ugenwrite,
    142 	/* ioctl */	ugenioctl,
    143 	/* poll */	ugenpoll,
    144 	/* mmap */	nommap,
    145 	/* strategy */	nostrategy,
    146 	/* name */	"ugen",
    147 	/* maj */	UGEN_CDEV_MAJOR,
    148 	/* dump */	nodump,
    149 	/* psize */	nopsize,
    150 	/* flags */	0,
    151 	/* bmaj */	-1
    152 };
    153 #endif
    154 
    155 Static void ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    156 		     usbd_status status);
    157 Static void ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    158 			    usbd_status status);
    159 Static int ugen_do_read(struct ugen_softc *, int, struct uio *, int);
    160 Static int ugen_do_write(struct ugen_softc *, int, struct uio *, int);
    161 Static int ugen_do_ioctl(struct ugen_softc *, int, u_long,
    162 			 caddr_t, int, usb_proc_ptr);
    163 Static int ugen_set_config(struct ugen_softc *sc, int configno);
    164 Static usb_config_descriptor_t *ugen_get_cdesc(struct ugen_softc *sc,
    165 					       int index, int *lenp);
    166 Static usbd_status ugen_set_interface(struct ugen_softc *, int, int);
    167 Static int ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx);
    168 
    169 #define UGENUNIT(n) ((minor(n) >> 4) & 0xf)
    170 #define UGENENDPOINT(n) (minor(n) & 0xf)
    171 #define UGENDEV(u, e) (makedev(0, ((u) << 4) | (e)))
    172 
    173 USB_DECLARE_DRIVER(ugen);
    174 
    175 USB_MATCH(ugen)
    176 {
    177 	USB_MATCH_START(ugen, uaa);
    178 
    179 #if 0
    180 	if (uaa->matchlvl)
    181 		return (uaa->matchlvl);
    182 #endif
    183 	if (uaa->usegeneric)
    184 		return (UMATCH_GENERIC);
    185 	else
    186 		return (UMATCH_NONE);
    187 }
    188 
    189 USB_ATTACH(ugen)
    190 {
    191 	USB_ATTACH_START(ugen, sc, uaa);
    192 	usbd_device_handle udev;
    193 	char devinfo[1024];
    194 	usbd_status err;
    195 	int conf;
    196 
    197 	usbd_devinfo(uaa->device, 0, devinfo);
    198 	USB_ATTACH_SETUP;
    199 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    200 
    201 	sc->sc_udev = udev = uaa->device;
    202 
    203 	/* First set configuration index 0, the default one for ugen. */
    204 	err = usbd_set_config_index(udev, 0, 0);
    205 	if (err) {
    206 		printf("%s: setting configuration index 0 failed\n",
    207 		       USBDEVNAME(sc->sc_dev));
    208 		sc->sc_dying = 1;
    209 		USB_ATTACH_ERROR_RETURN;
    210 	}
    211 	conf = usbd_get_config_descriptor(udev)->bConfigurationValue;
    212 
    213 	/* Set up all the local state for this configuration. */
    214 	err = ugen_set_config(sc, conf);
    215 	if (err) {
    216 		printf("%s: setting configuration %d failed\n",
    217 		       USBDEVNAME(sc->sc_dev), conf);
    218 		sc->sc_dying = 1;
    219 		USB_ATTACH_ERROR_RETURN;
    220 	}
    221 
    222 #ifdef __FreeBSD__
    223 	{
    224 		static int global_init_done = 0;
    225 		if (!global_init_done) {
    226 			cdevsw_add(&ugen_cdevsw);
    227 			global_init_done = 1;
    228 		}
    229 	}
    230 #endif
    231 
    232 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    233 			   USBDEV(sc->sc_dev));
    234 
    235 	USB_ATTACH_SUCCESS_RETURN;
    236 }
    237 
    238 Static int
    239 ugen_set_config(struct ugen_softc *sc, int configno)
    240 {
    241 	usbd_device_handle dev = sc->sc_udev;
    242 	usbd_interface_handle iface;
    243 	usb_endpoint_descriptor_t *ed;
    244 	struct ugen_endpoint *sce;
    245 	u_int8_t niface, nendpt;
    246 	int ifaceno, endptno, endpt;
    247 	usbd_status err;
    248 	int dir;
    249 
    250 	DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
    251 		    USBDEVNAME(sc->sc_dev), configno, sc));
    252 
    253 	/*
    254 	 * We start at 1, not 0, because we don't care whether the
    255 	 * control endpoint is open or not. It is always present.
    256 	 */
    257 	for (endptno = 1; endptno < USB_MAX_ENDPOINTS; endptno++)
    258 		if (sc->sc_is_open[endptno]) {
    259 			DPRINTFN(1,
    260 			     ("ugen_set_config: %s - endpoint %d is open\n",
    261 			      USBDEVNAME(sc->sc_dev), endptno));
    262 			return (USBD_IN_USE);
    263 		}
    264 
    265 	/* Avoid setting the current value. */
    266 	if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
    267 		err = usbd_set_config_no(dev, configno, 1);
    268 		if (err)
    269 			return (err);
    270 	}
    271 
    272 	err = usbd_interface_count(dev, &niface);
    273 	if (err)
    274 		return (err);
    275 	memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
    276 	for (ifaceno = 0; ifaceno < niface; ifaceno++) {
    277 		DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
    278 		err = usbd_device2interface_handle(dev, ifaceno, &iface);
    279 		if (err)
    280 			return (err);
    281 		err = usbd_endpoint_count(iface, &nendpt);
    282 		if (err)
    283 			return (err);
    284 		for (endptno = 0; endptno < nendpt; endptno++) {
    285 			ed = usbd_interface2endpoint_descriptor(iface,endptno);
    286 			endpt = ed->bEndpointAddress;
    287 			dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    288 			sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    289 			DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
    290 				    "(%d,%d), sce=%p\n",
    291 				    endptno, endpt, UE_GET_ADDR(endpt),
    292 				    UE_GET_DIR(endpt), sce));
    293 			sce->sc = sc;
    294 			sce->edesc = ed;
    295 			sce->iface = iface;
    296 		}
    297 	}
    298 	return (USBD_NORMAL_COMPLETION);
    299 }
    300 
    301 int
    302 ugenopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    303 {
    304 	struct ugen_softc *sc;
    305 	int unit = UGENUNIT(dev);
    306 	int endpt = UGENENDPOINT(dev);
    307 	usb_endpoint_descriptor_t *edesc;
    308 	struct ugen_endpoint *sce;
    309 	int dir, isize;
    310 	usbd_status err;
    311 	usbd_xfer_handle xfer;
    312 	void *buf;
    313 	int i, j;
    314 
    315 	USB_GET_SC_OPEN(ugen, unit, sc);
    316 
    317 	DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
    318 		     flag, mode, unit, endpt));
    319 
    320 	if (sc == NULL || sc->sc_dying)
    321 		return (ENXIO);
    322 
    323 	if (sc->sc_is_open[endpt])
    324 		return (EBUSY);
    325 
    326 	if (endpt == USB_CONTROL_ENDPOINT) {
    327 		sc->sc_is_open[USB_CONTROL_ENDPOINT] = 1;
    328 		return (0);
    329 	}
    330 
    331 	/* Make sure there are pipes for all directions. */
    332 	for (dir = OUT; dir <= IN; dir++) {
    333 		if (flag & (dir == OUT ? FWRITE : FREAD)) {
    334 			sce = &sc->sc_endpoints[endpt][dir];
    335 			if (sce == 0 || sce->edesc == 0)
    336 				return (ENXIO);
    337 		}
    338 	}
    339 
    340 	/* Actually open the pipes. */
    341 	/* XXX Should back out properly if it fails. */
    342 	for (dir = OUT; dir <= IN; dir++) {
    343 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    344 			continue;
    345 		sce = &sc->sc_endpoints[endpt][dir];
    346 		sce->state = 0;
    347 		sce->timeout = USBD_NO_TIMEOUT;
    348 		DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
    349 			     sc, endpt, dir, sce));
    350 		edesc = sce->edesc;
    351 		switch (edesc->bmAttributes & UE_XFERTYPE) {
    352 		case UE_INTERRUPT:
    353 			isize = UGETW(edesc->wMaxPacketSize);
    354 			if (isize == 0)	/* shouldn't happen */
    355 				return (EINVAL);
    356 			sce->ibuf = malloc(isize, M_USBDEV, M_WAITOK);
    357 			DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
    358 				     endpt, isize));
    359 			if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
    360 				return (ENOMEM);
    361 			err = usbd_open_pipe_intr(sce->iface,
    362 				  edesc->bEndpointAddress,
    363 				  USBD_SHORT_XFER_OK, &sce->pipeh, sce,
    364 				  sce->ibuf, isize, ugenintr,
    365 				  USBD_DEFAULT_INTERVAL);
    366 			if (err) {
    367 				free(sce->ibuf, M_USBDEV);
    368 				clfree(&sce->q);
    369 				return (EIO);
    370 			}
    371 			DPRINTFN(5, ("ugenopen: interrupt open done\n"));
    372 			break;
    373 		case UE_BULK:
    374 			err = usbd_open_pipe(sce->iface,
    375 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    376 			if (err)
    377 				return (EIO);
    378 			break;
    379 		case UE_ISOCHRONOUS:
    380 			if (dir == OUT)
    381 				return (EINVAL);
    382 			isize = UGETW(edesc->wMaxPacketSize);
    383 			if (isize == 0)	/* shouldn't happen */
    384 				return (EINVAL);
    385 			sce->ibuf = malloc(isize * UGEN_NISOFRAMES,
    386 				M_USBDEV, M_WAITOK);
    387 			sce->cur = sce->fill = sce->ibuf;
    388 			sce->limit = sce->ibuf + isize * UGEN_NISOFRAMES;
    389 			DPRINTFN(5, ("ugenopen: isoc endpt=%d, isize=%d\n",
    390 				     endpt, isize));
    391 			err = usbd_open_pipe(sce->iface,
    392 				  edesc->bEndpointAddress, 0, &sce->pipeh);
    393 			if (err) {
    394 				free(sce->ibuf, M_USBDEV);
    395 				return (EIO);
    396 			}
    397 			for(i = 0; i < UGEN_NISOREQS; ++i) {
    398 				sce->isoreqs[i].sce = sce;
    399 				xfer = usbd_alloc_xfer(sc->sc_udev);
    400 				if (xfer == 0)
    401 					goto bad;
    402 				sce->isoreqs[i].xfer = xfer;
    403 				buf = usbd_alloc_buffer
    404 					(xfer, isize * UGEN_NISORFRMS);
    405 				if (buf == 0) {
    406 					i++;
    407 					goto bad;
    408 				}
    409 				sce->isoreqs[i].dmabuf = buf;
    410 				for(j = 0; j < UGEN_NISORFRMS; ++j)
    411 					sce->isoreqs[i].sizes[j] = isize;
    412 				usbd_setup_isoc_xfer
    413 					(xfer, sce->pipeh, &sce->isoreqs[i],
    414 					 sce->isoreqs[i].sizes,
    415 					 UGEN_NISORFRMS, USBD_NO_COPY,
    416 					 ugen_isoc_rintr);
    417 				(void)usbd_transfer(xfer);
    418 			}
    419 			DPRINTFN(5, ("ugenopen: isoc open done\n"));
    420 			break;
    421 		bad:
    422 			while (--i >= 0) /* implicit buffer free */
    423 				usbd_free_xfer(sce->isoreqs[i].xfer);
    424 			return (ENOMEM);
    425 		case UE_CONTROL:
    426 			return (EINVAL);
    427 		}
    428 	}
    429 	sc->sc_is_open[endpt] = 1;
    430 	return (0);
    431 }
    432 
    433 int
    434 ugenclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    435 {
    436 	int endpt = UGENENDPOINT(dev);
    437 	struct ugen_softc *sc;
    438 	struct ugen_endpoint *sce;
    439 	int dir;
    440 	int i;
    441 
    442 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    443 
    444 	DPRINTFN(5, ("ugenclose: flag=%d, mode=%d, unit=%d, endpt=%d\n",
    445 		     flag, mode, UGENUNIT(dev), endpt));
    446 
    447 #ifdef DIAGNOSTIC
    448 	if (!sc->sc_is_open[endpt]) {
    449 		printf("ugenclose: not open\n");
    450 		return (EINVAL);
    451 	}
    452 #endif
    453 
    454 	if (endpt == USB_CONTROL_ENDPOINT) {
    455 		DPRINTFN(5, ("ugenclose: close control\n"));
    456 		sc->sc_is_open[endpt] = 0;
    457 		return (0);
    458 	}
    459 
    460 	for (dir = OUT; dir <= IN; dir++) {
    461 		if (!(flag & (dir == OUT ? FWRITE : FREAD)))
    462 			continue;
    463 		sce = &sc->sc_endpoints[endpt][dir];
    464 		if (sce == NULL || sce->pipeh == NULL)
    465 			continue;
    466 		DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
    467 			     endpt, dir, sce));
    468 
    469 		usbd_abort_pipe(sce->pipeh);
    470 		usbd_close_pipe(sce->pipeh);
    471 		sce->pipeh = NULL;
    472 
    473 		switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    474 		case UE_INTERRUPT:
    475 			ndflush(&sce->q, sce->q.c_cc);
    476 			clfree(&sce->q);
    477 			break;
    478 		case UE_ISOCHRONOUS:
    479 			for (i = 0; i < UGEN_NISOREQS; ++i)
    480 				usbd_free_xfer(sce->isoreqs[i].xfer);
    481 
    482 		default:
    483 			break;
    484 		}
    485 
    486 		if (sce->ibuf != NULL) {
    487 			free(sce->ibuf, M_USBDEV);
    488 			sce->ibuf = NULL;
    489 			clfree(&sce->q);
    490 		}
    491 	}
    492 	sc->sc_is_open[endpt] = 0;
    493 
    494 	return (0);
    495 }
    496 
    497 Static int
    498 ugen_do_read(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
    499 {
    500 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
    501 	u_int32_t n, tn;
    502 	char buf[UGEN_BBSIZE];
    503 	usbd_xfer_handle xfer;
    504 	usbd_status err;
    505 	int s;
    506 	int error = 0;
    507 	u_char buffer[UGEN_CHUNK];
    508 
    509 	DPRINTFN(5, ("%s: ugenread: %d\n", USBDEVNAME(sc->sc_dev), endpt));
    510 
    511 	if (sc->sc_dying)
    512 		return (EIO);
    513 
    514 	if (endpt == USB_CONTROL_ENDPOINT)
    515 		return (ENODEV);
    516 
    517 #ifdef DIAGNOSTIC
    518 	if (sce->edesc == NULL) {
    519 		printf("ugenread: no edesc\n");
    520 		return (EIO);
    521 	}
    522 	if (sce->pipeh == NULL) {
    523 		printf("ugenread: no pipe\n");
    524 		return (EIO);
    525 	}
    526 #endif
    527 
    528 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    529 	case UE_INTERRUPT:
    530 		/* Block until activity occurred. */
    531 		s = splusb();
    532 		while (sce->q.c_cc == 0) {
    533 			if (flag & IO_NDELAY) {
    534 				splx(s);
    535 				return (EWOULDBLOCK);
    536 			}
    537 			sce->state |= UGEN_ASLP;
    538 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    539 			error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
    540 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    541 			if (sc->sc_dying)
    542 				error = EIO;
    543 			if (error) {
    544 				sce->state &= ~UGEN_ASLP;
    545 				break;
    546 			}
    547 		}
    548 		splx(s);
    549 
    550 		/* Transfer as many chunks as possible. */
    551 		while (sce->q.c_cc > 0 && uio->uio_resid > 0 && !error) {
    552 			n = min(sce->q.c_cc, uio->uio_resid);
    553 			if (n > sizeof(buffer))
    554 				n = sizeof(buffer);
    555 
    556 			/* Remove a small chunk from the input queue. */
    557 			q_to_b(&sce->q, buffer, n);
    558 			DPRINTFN(5, ("ugenread: got %d chars\n", n));
    559 
    560 			/* Copy the data to the user process. */
    561 			error = uiomove(buffer, n, uio);
    562 			if (error)
    563 				break;
    564 		}
    565 		break;
    566 	case UE_BULK:
    567 		xfer = usbd_alloc_xfer(sc->sc_udev);
    568 		if (xfer == 0)
    569 			return (ENOMEM);
    570 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    571 			DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
    572 			tn = n;
    573 			err = usbd_bulk_transfer(
    574 				  xfer, sce->pipeh,
    575 				  sce->state & UGEN_SHORT_OK ?
    576 				      USBD_SHORT_XFER_OK : 0,
    577 				  sce->timeout, buf, &tn, "ugenrb");
    578 			if (err) {
    579 				if (err == USBD_INTERRUPTED)
    580 					error = EINTR;
    581 				else if (err == USBD_TIMEOUT)
    582 					error = ETIMEDOUT;
    583 				else
    584 					error = EIO;
    585 				break;
    586 			}
    587 			DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
    588 			error = uiomove(buf, tn, uio);
    589 			if (error || tn < n)
    590 				break;
    591 		}
    592 		usbd_free_xfer(xfer);
    593 		break;
    594 	case UE_ISOCHRONOUS:
    595 		s = splusb();
    596 		while (sce->cur == sce->fill) {
    597 			if (flag & IO_NDELAY) {
    598 				splx(s);
    599 				return (EWOULDBLOCK);
    600 			}
    601 			sce->state |= UGEN_ASLP;
    602 			DPRINTFN(5, ("ugenread: sleep on %p\n", sce));
    603 			error = tsleep(sce, PZERO | PCATCH, "ugenri", 0);
    604 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    605 			if (sc->sc_dying)
    606 				error = EIO;
    607 			if (error) {
    608 				sce->state &= ~UGEN_ASLP;
    609 				break;
    610 			}
    611 		}
    612 
    613 		while (sce->cur != sce->fill && uio->uio_resid > 0 && !error) {
    614 			if(sce->fill > sce->cur)
    615 				n = min(sce->fill - sce->cur, uio->uio_resid);
    616 			else
    617 				n = min(sce->limit - sce->cur, uio->uio_resid);
    618 
    619 			DPRINTFN(5, ("ugenread: isoc got %d chars\n", n));
    620 
    621 			/* Copy the data to the user process. */
    622 			error = uiomove(sce->cur, n, uio);
    623 			if (error)
    624 				break;
    625 			sce->cur += n;
    626 			if(sce->cur >= sce->limit)
    627 				sce->cur = sce->ibuf;
    628 		}
    629 		splx(s);
    630 		break;
    631 
    632 
    633 	default:
    634 		return (ENXIO);
    635 	}
    636 	return (error);
    637 }
    638 
    639 int
    640 ugenread(dev_t dev, struct uio *uio, int flag)
    641 {
    642 	int endpt = UGENENDPOINT(dev);
    643 	struct ugen_softc *sc;
    644 	int error;
    645 
    646 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    647 
    648 	sc->sc_refcnt++;
    649 	error = ugen_do_read(sc, endpt, uio, flag);
    650 	if (--sc->sc_refcnt < 0)
    651 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    652 	return (error);
    653 }
    654 
    655 Static int
    656 ugen_do_write(struct ugen_softc *sc, int endpt, struct uio *uio, int flag)
    657 {
    658 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
    659 	u_int32_t n;
    660 	int error = 0;
    661 	char buf[UGEN_BBSIZE];
    662 	usbd_xfer_handle xfer;
    663 	usbd_status err;
    664 
    665 	DPRINTFN(5, ("%s: ugenwrite: %d\n", USBDEVNAME(sc->sc_dev), endpt));
    666 
    667 	if (sc->sc_dying)
    668 		return (EIO);
    669 
    670 	if (endpt == USB_CONTROL_ENDPOINT)
    671 		return (ENODEV);
    672 
    673 #ifdef DIAGNOSTIC
    674 	if (sce->edesc == NULL) {
    675 		printf("ugenwrite: no edesc\n");
    676 		return (EIO);
    677 	}
    678 	if (sce->pipeh == NULL) {
    679 		printf("ugenwrite: no pipe\n");
    680 		return (EIO);
    681 	}
    682 #endif
    683 
    684 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    685 	case UE_BULK:
    686 		xfer = usbd_alloc_xfer(sc->sc_udev);
    687 		if (xfer == 0)
    688 			return (EIO);
    689 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    690 			error = uiomove(buf, n, uio);
    691 			if (error)
    692 				break;
    693 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
    694 			err = usbd_bulk_transfer(xfer, sce->pipeh, 0,
    695 				  sce->timeout, buf, &n,"ugenwb");
    696 			if (err) {
    697 				if (err == USBD_INTERRUPTED)
    698 					error = EINTR;
    699 				else if (err == USBD_TIMEOUT)
    700 					error = ETIMEDOUT;
    701 				else
    702 					error = EIO;
    703 				break;
    704 			}
    705 		}
    706 		usbd_free_xfer(xfer);
    707 		break;
    708 	default:
    709 		return (ENXIO);
    710 	}
    711 	return (error);
    712 }
    713 
    714 int
    715 ugenwrite(dev_t dev, struct uio *uio, int flag)
    716 {
    717 	int endpt = UGENENDPOINT(dev);
    718 	struct ugen_softc *sc;
    719 	int error;
    720 
    721 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
    722 
    723 	sc->sc_refcnt++;
    724 	error = ugen_do_write(sc, endpt, uio, flag);
    725 	if (--sc->sc_refcnt < 0)
    726 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    727 	return (error);
    728 }
    729 
    730 #if defined(__NetBSD__) || defined(__OpenBSD__)
    731 int
    732 ugen_activate(device_ptr_t self, enum devact act)
    733 {
    734 	struct ugen_softc *sc = (struct ugen_softc *)self;
    735 
    736 	switch (act) {
    737 	case DVACT_ACTIVATE:
    738 		return (EOPNOTSUPP);
    739 		break;
    740 
    741 	case DVACT_DEACTIVATE:
    742 		sc->sc_dying = 1;
    743 		break;
    744 	}
    745 	return (0);
    746 }
    747 #endif
    748 
    749 USB_DETACH(ugen)
    750 {
    751 	USB_DETACH_START(ugen, sc);
    752 	struct ugen_endpoint *sce;
    753 	int i, dir;
    754 	int s;
    755 #if defined(__NetBSD__) || defined(__OpenBSD__)
    756 	int maj, mn;
    757 
    758 	DPRINTF(("ugen_detach: sc=%p flags=%d\n", sc, flags));
    759 #elif defined(__FreeBSD__)
    760 	DPRINTF(("ugen_detach: sc=%p\n", sc));
    761 #endif
    762 
    763 	sc->sc_dying = 1;
    764 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    765 	for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
    766 		for (dir = OUT; dir <= IN; dir++) {
    767 			sce = &sc->sc_endpoints[i][dir];
    768 			if (sce && sce->pipeh)
    769 				usbd_abort_pipe(sce->pipeh);
    770 		}
    771 	}
    772 
    773 	s = splusb();
    774 	if (--sc->sc_refcnt >= 0) {
    775 		/* Wake everyone */
    776 		for (i = 0; i < USB_MAX_ENDPOINTS; i++)
    777 			wakeup(&sc->sc_endpoints[i][IN]);
    778 		/* Wait for processes to go away. */
    779 		usb_detach_wait(USBDEV(sc->sc_dev));
    780 	}
    781 	splx(s);
    782 
    783 #if defined(__NetBSD__) || defined(__OpenBSD__)
    784 	/* locate the major number */
    785 	for (maj = 0; maj < nchrdev; maj++)
    786 		if (cdevsw[maj].d_open == ugenopen)
    787 			break;
    788 
    789 	/* Nuke the vnodes for any open instances (calls close). */
    790 	mn = self->dv_unit * USB_MAX_ENDPOINTS;
    791 	vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
    792 #elif defined(__FreeBSD__)
    793 	/* XXX not implemented yet */
    794 #endif
    795 
    796 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    797 			   USBDEV(sc->sc_dev));
    798 
    799 	return (0);
    800 }
    801 
    802 Static void
    803 ugenintr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    804 {
    805 	struct ugen_endpoint *sce = addr;
    806 	/*struct ugen_softc *sc = sce->sc;*/
    807 	u_int32_t count;
    808 	u_char *ibuf;
    809 
    810 	if (status == USBD_CANCELLED)
    811 		return;
    812 
    813 	if (status != USBD_NORMAL_COMPLETION) {
    814 		DPRINTF(("ugenintr: status=%d\n", status));
    815 		if (status == USBD_STALLED)
    816 		    usbd_clear_endpoint_stall_async(sce->pipeh);
    817 		return;
    818 	}
    819 
    820 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
    821 	ibuf = sce->ibuf;
    822 
    823 	DPRINTFN(5, ("ugenintr: xfer=%p status=%d count=%d\n",
    824 		     xfer, status, count));
    825 	DPRINTFN(5, ("          data = %02x %02x %02x\n",
    826 		     ibuf[0], ibuf[1], ibuf[2]));
    827 
    828 	(void)b_to_q(ibuf, count, &sce->q);
    829 
    830 	if (sce->state & UGEN_ASLP) {
    831 		sce->state &= ~UGEN_ASLP;
    832 		DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
    833 		wakeup(sce);
    834 	}
    835 	selwakeup(&sce->rsel);
    836 }
    837 
    838 Static void
    839 ugen_isoc_rintr(usbd_xfer_handle xfer, usbd_private_handle addr,
    840 		usbd_status status)
    841 {
    842 	struct isoreq *req = addr;
    843 	struct ugen_endpoint *sce = req->sce;
    844 	u_int32_t count, n;
    845 	int i, isize;
    846 
    847 	/* Return if we are aborting. */
    848 	if (status == USBD_CANCELLED)
    849 		return;
    850 
    851 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
    852 	DPRINTFN(5,("ugen_isoc_rintr: xfer %d, count=%d\n", req - sce->isoreqs,
    853 		    count));
    854 
    855 	/* throw away oldest input if the buffer is full */
    856 	if(sce->fill < sce->cur && sce->cur <= sce->fill + count) {
    857 		sce->cur += count;
    858 		if(sce->cur >= sce->limit)
    859 			sce->cur = sce->ibuf + (sce->limit - sce->cur);
    860 		DPRINTFN(5, ("ugen_isoc_rintr: throwing away %d bytes\n",
    861 			     count));
    862 	}
    863 
    864 	isize = UGETW(sce->edesc->wMaxPacketSize);
    865 	for (i = 0; i < UGEN_NISORFRMS; i++) {
    866 		u_int32_t actlen = req->sizes[i];
    867 		char const *buf = (char const *)req->dmabuf + isize * i;
    868 
    869 		/* copy data to buffer */
    870 		while (actlen > 0) {
    871 			n = min(actlen, sce->limit - sce->fill);
    872 			memcpy(sce->fill, buf, n);
    873 
    874 			buf += n;
    875 			actlen -= n;
    876 			sce->fill += n;
    877 			if(sce->fill == sce->limit)
    878 				sce->fill = sce->ibuf;
    879 		}
    880 
    881 		/* setup size for next transfer */
    882 		req->sizes[i] = isize;
    883 	}
    884 
    885 	usbd_setup_isoc_xfer(xfer, sce->pipeh, req, req->sizes, UGEN_NISORFRMS,
    886 			     USBD_NO_COPY, ugen_isoc_rintr);
    887 	(void)usbd_transfer(xfer);
    888 
    889 	if (sce->state & UGEN_ASLP) {
    890 		sce->state &= ~UGEN_ASLP;
    891 		DPRINTFN(5, ("ugen_isoc_rintr: waking %p\n", sce));
    892 		wakeup(sce);
    893 	}
    894 	selwakeup(&sce->rsel);
    895 }
    896 
    897 Static usbd_status
    898 ugen_set_interface(struct ugen_softc *sc, int ifaceidx, int altno)
    899 {
    900 	usbd_interface_handle iface;
    901 	usb_endpoint_descriptor_t *ed;
    902 	usbd_status err;
    903 	struct ugen_endpoint *sce;
    904 	u_int8_t niface, nendpt, endptno, endpt;
    905 	int dir;
    906 
    907 	DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
    908 
    909 	err = usbd_interface_count(sc->sc_udev, &niface);
    910 	if (err)
    911 		return (err);
    912 	if (ifaceidx < 0 || ifaceidx >= niface)
    913 		return (USBD_INVAL);
    914 
    915 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    916 	if (err)
    917 		return (err);
    918 	err = usbd_endpoint_count(iface, &nendpt);
    919 	if (err)
    920 		return (err);
    921 	/* XXX should only do this after setting new altno has succeeded */
    922 	for (endptno = 0; endptno < nendpt; endptno++) {
    923 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    924 		endpt = ed->bEndpointAddress;
    925 		dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    926 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    927 		sce->sc = 0;
    928 		sce->edesc = 0;
    929 		sce->iface = 0;
    930 	}
    931 
    932 	/* change setting */
    933 	err = usbd_set_interface(iface, altno);
    934 	if (err)
    935 		return (err);
    936 
    937 	err = usbd_endpoint_count(iface, &nendpt);
    938 	if (err)
    939 		return (err);
    940 	for (endptno = 0; endptno < nendpt; endptno++) {
    941 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    942 		endpt = ed->bEndpointAddress;
    943 		dir = UE_GET_DIR(endpt) == UE_DIR_IN ? IN : OUT;
    944 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][dir];
    945 		sce->sc = sc;
    946 		sce->edesc = ed;
    947 		sce->iface = iface;
    948 	}
    949 	return (0);
    950 }
    951 
    952 /* Retrieve a complete descriptor for a certain device and index. */
    953 Static usb_config_descriptor_t *
    954 ugen_get_cdesc(struct ugen_softc *sc, int index, int *lenp)
    955 {
    956 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
    957 	int len;
    958 	usbd_status err;
    959 
    960 	if (index == USB_CURRENT_CONFIG_INDEX) {
    961 		tdesc = usbd_get_config_descriptor(sc->sc_udev);
    962 		len = UGETW(tdesc->wTotalLength);
    963 		if (lenp)
    964 			*lenp = len;
    965 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    966 		memcpy(cdesc, tdesc, len);
    967 		DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
    968 	} else {
    969 		err = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
    970 		if (err)
    971 			return (0);
    972 		len = UGETW(cdescr.wTotalLength);
    973 		DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
    974 		if (lenp)
    975 			*lenp = len;
    976 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    977 		err = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
    978 		if (err) {
    979 			free(cdesc, M_TEMP);
    980 			return (0);
    981 		}
    982 	}
    983 	return (cdesc);
    984 }
    985 
    986 Static int
    987 ugen_get_alt_index(struct ugen_softc *sc, int ifaceidx)
    988 {
    989 	usbd_interface_handle iface;
    990 	usbd_status err;
    991 
    992 	err = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    993 	if (err)
    994 		return (-1);
    995 	return (usbd_get_interface_altindex(iface));
    996 }
    997 
    998 Static int
    999 ugen_do_ioctl(struct ugen_softc *sc, int endpt, u_long cmd,
   1000 	      caddr_t addr, int flag, usb_proc_ptr p)
   1001 {
   1002 	struct ugen_endpoint *sce;
   1003 	usbd_status err;
   1004 	usbd_interface_handle iface;
   1005 	struct usb_config_desc *cd;
   1006 	usb_config_descriptor_t *cdesc;
   1007 	struct usb_interface_desc *id;
   1008 	usb_interface_descriptor_t *idesc;
   1009 	struct usb_endpoint_desc *ed;
   1010 	usb_endpoint_descriptor_t *edesc;
   1011 	struct usb_alt_interface *ai;
   1012 	struct usb_string_desc *si;
   1013 	u_int8_t conf, alt;
   1014 
   1015 	DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
   1016 	if (sc->sc_dying)
   1017 		return (EIO);
   1018 
   1019 	switch (cmd) {
   1020 	case FIONBIO:
   1021 		/* All handled in the upper FS layer. */
   1022 		return (0);
   1023 	case USB_SET_SHORT_XFER:
   1024 		if (endpt == USB_CONTROL_ENDPOINT)
   1025 			return (EINVAL);
   1026 		/* This flag only affects read */
   1027 		sce = &sc->sc_endpoints[endpt][IN];
   1028 		if (sce == NULL || sce->pipeh == NULL)
   1029 			return (EINVAL);
   1030 		if (*(int *)addr)
   1031 			sce->state |= UGEN_SHORT_OK;
   1032 		else
   1033 			sce->state &= ~UGEN_SHORT_OK;
   1034 		return (0);
   1035 	case USB_SET_TIMEOUT:
   1036 		if (endpt == USB_CONTROL_ENDPOINT) {
   1037 			/* XXX the lower levels don't support this yet. */
   1038 			return (EINVAL);
   1039 		}
   1040 		sce = &sc->sc_endpoints[endpt][IN];
   1041 		if (sce == NULL || sce->pipeh == NULL)
   1042 			return (EINVAL);
   1043 		sce->timeout = *(int *)addr;
   1044 		return (0);
   1045 	default:
   1046 		break;
   1047 	}
   1048 
   1049 	if (endpt != USB_CONTROL_ENDPOINT)
   1050 		return (EINVAL);
   1051 
   1052 	switch (cmd) {
   1053 #ifdef UGEN_DEBUG
   1054 	case USB_SETDEBUG:
   1055 		ugendebug = *(int *)addr;
   1056 		break;
   1057 #endif
   1058 	case USB_GET_CONFIG:
   1059 		err = usbd_get_config(sc->sc_udev, &conf);
   1060 		if (err)
   1061 			return (EIO);
   1062 		*(int *)addr = conf;
   1063 		break;
   1064 	case USB_SET_CONFIG:
   1065 		if (!(flag & FWRITE))
   1066 			return (EPERM);
   1067 		err = ugen_set_config(sc, *(int *)addr);
   1068 		switch (err) {
   1069 		case USBD_NORMAL_COMPLETION:
   1070 			break;
   1071 		case USBD_IN_USE:
   1072 			return (EBUSY);
   1073 		default:
   1074 			return (EIO);
   1075 		}
   1076 		break;
   1077 	case USB_GET_ALTINTERFACE:
   1078 		ai = (struct usb_alt_interface *)addr;
   1079 		err = usbd_device2interface_handle(sc->sc_udev,
   1080 			  ai->interface_index, &iface);
   1081 		if (err)
   1082 			return (EINVAL);
   1083 		idesc = usbd_get_interface_descriptor(iface);
   1084 		if (idesc == NULL)
   1085 			return (EIO);
   1086 		ai->alt_no = idesc->bAlternateSetting;
   1087 		break;
   1088 	case USB_SET_ALTINTERFACE:
   1089 		if (!(flag & FWRITE))
   1090 			return (EPERM);
   1091 		ai = (struct usb_alt_interface *)addr;
   1092 		err = usbd_device2interface_handle(sc->sc_udev,
   1093 			  ai->interface_index, &iface);
   1094 		if (err)
   1095 			return (EINVAL);
   1096 		err = ugen_set_interface(sc, ai->interface_index, ai->alt_no);
   1097 		if (err)
   1098 			return (EINVAL);
   1099 		break;
   1100 	case USB_GET_NO_ALT:
   1101 		ai = (struct usb_alt_interface *)addr;
   1102 		cdesc = ugen_get_cdesc(sc, ai->config_index, 0);
   1103 		if (cdesc == NULL)
   1104 			return (EINVAL);
   1105 		idesc = usbd_find_idesc(cdesc, ai->interface_index, 0);
   1106 		if (idesc == NULL) {
   1107 			free(cdesc, M_TEMP);
   1108 			return (EINVAL);
   1109 		}
   1110 		ai->alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
   1111 		free(cdesc, M_TEMP);
   1112 		break;
   1113 	case USB_GET_DEVICE_DESC:
   1114 		*(usb_device_descriptor_t *)addr =
   1115 			*usbd_get_device_descriptor(sc->sc_udev);
   1116 		break;
   1117 	case USB_GET_CONFIG_DESC:
   1118 		cd = (struct usb_config_desc *)addr;
   1119 		cdesc = ugen_get_cdesc(sc, cd->config_index, 0);
   1120 		if (cdesc == NULL)
   1121 			return (EINVAL);
   1122 		cd->desc = *cdesc;
   1123 		free(cdesc, M_TEMP);
   1124 		break;
   1125 	case USB_GET_INTERFACE_DESC:
   1126 		id = (struct usb_interface_desc *)addr;
   1127 		cdesc = ugen_get_cdesc(sc, id->config_index, 0);
   1128 		if (cdesc == NULL)
   1129 			return (EINVAL);
   1130 		if (id->config_index == USB_CURRENT_CONFIG_INDEX &&
   1131 		    id->alt_index == USB_CURRENT_ALT_INDEX)
   1132 			alt = ugen_get_alt_index(sc, id->interface_index);
   1133 		else
   1134 			alt = id->alt_index;
   1135 		idesc = usbd_find_idesc(cdesc, id->interface_index, alt);
   1136 		if (idesc == NULL) {
   1137 			free(cdesc, M_TEMP);
   1138 			return (EINVAL);
   1139 		}
   1140 		id->desc = *idesc;
   1141 		free(cdesc, M_TEMP);
   1142 		break;
   1143 	case USB_GET_ENDPOINT_DESC:
   1144 		ed = (struct usb_endpoint_desc *)addr;
   1145 		cdesc = ugen_get_cdesc(sc, ed->config_index, 0);
   1146 		if (cdesc == NULL)
   1147 			return (EINVAL);
   1148 		if (ed->config_index == USB_CURRENT_CONFIG_INDEX &&
   1149 		    ed->alt_index == USB_CURRENT_ALT_INDEX)
   1150 			alt = ugen_get_alt_index(sc, ed->interface_index);
   1151 		else
   1152 			alt = ed->alt_index;
   1153 		edesc = usbd_find_edesc(cdesc, ed->interface_index,
   1154 					alt, ed->endpoint_index);
   1155 		if (edesc == NULL) {
   1156 			free(cdesc, M_TEMP);
   1157 			return (EINVAL);
   1158 		}
   1159 		ed->desc = *edesc;
   1160 		free(cdesc, M_TEMP);
   1161 		break;
   1162 	case USB_GET_FULL_DESC:
   1163 	{
   1164 		int len;
   1165 		struct iovec iov;
   1166 		struct uio uio;
   1167 		struct usb_full_desc *fd = (struct usb_full_desc *)addr;
   1168 		int error;
   1169 
   1170 		cdesc = ugen_get_cdesc(sc, fd->config_index, &len);
   1171 		if (len > fd->size)
   1172 			len = fd->size;
   1173 		iov.iov_base = (caddr_t)fd->data;
   1174 		iov.iov_len = len;
   1175 		uio.uio_iov = &iov;
   1176 		uio.uio_iovcnt = 1;
   1177 		uio.uio_resid = len;
   1178 		uio.uio_offset = 0;
   1179 		uio.uio_segflg = UIO_USERSPACE;
   1180 		uio.uio_rw = UIO_READ;
   1181 		uio.uio_procp = p;
   1182 		error = uiomove((void *)cdesc, len, &uio);
   1183 		free(cdesc, M_TEMP);
   1184 		return (error);
   1185 	}
   1186 	case USB_GET_STRING_DESC:
   1187 		si = (struct usb_string_desc *)addr;
   1188 		err = usbd_get_string_desc(sc->sc_udev, si->string_index,
   1189 			  si->language_id, &si->desc);
   1190 		if (err)
   1191 			return (EINVAL);
   1192 		break;
   1193 	case USB_DO_REQUEST:
   1194 	{
   1195 		struct usb_ctl_request *ur = (void *)addr;
   1196 		int len = UGETW(ur->request.wLength);
   1197 		struct iovec iov;
   1198 		struct uio uio;
   1199 		void *ptr = 0;
   1200 		usbd_status err;
   1201 		int error = 0;
   1202 
   1203 		if (!(flag & FWRITE))
   1204 			return (EPERM);
   1205 		/* Avoid requests that would damage the bus integrity. */
   1206 		if ((ur->request.bmRequestType == UT_WRITE_DEVICE &&
   1207 		     ur->request.bRequest == UR_SET_ADDRESS) ||
   1208 		    (ur->request.bmRequestType == UT_WRITE_DEVICE &&
   1209 		     ur->request.bRequest == UR_SET_CONFIG) ||
   1210 		    (ur->request.bmRequestType == UT_WRITE_INTERFACE &&
   1211 		     ur->request.bRequest == UR_SET_INTERFACE))
   1212 			return (EINVAL);
   1213 
   1214 		if (len < 0 || len > 32767)
   1215 			return (EINVAL);
   1216 		if (len != 0) {
   1217 			iov.iov_base = (caddr_t)ur->data;
   1218 			iov.iov_len = len;
   1219 			uio.uio_iov = &iov;
   1220 			uio.uio_iovcnt = 1;
   1221 			uio.uio_resid = len;
   1222 			uio.uio_offset = 0;
   1223 			uio.uio_segflg = UIO_USERSPACE;
   1224 			uio.uio_rw =
   1225 				ur->request.bmRequestType & UT_READ ?
   1226 				UIO_READ : UIO_WRITE;
   1227 			uio.uio_procp = p;
   1228 			ptr = malloc(len, M_TEMP, M_WAITOK);
   1229 			if (uio.uio_rw == UIO_WRITE) {
   1230 				error = uiomove(ptr, len, &uio);
   1231 				if (error)
   1232 					goto ret;
   1233 			}
   1234 		}
   1235 		err = usbd_do_request_flags(sc->sc_udev, &ur->request,
   1236 			  ptr, ur->flags, &ur->actlen);
   1237 		if (err) {
   1238 			error = EIO;
   1239 			goto ret;
   1240 		}
   1241 		if (len != 0) {
   1242 			if (uio.uio_rw == UIO_READ) {
   1243 				error = uiomove(ptr, len, &uio);
   1244 				if (error)
   1245 					goto ret;
   1246 			}
   1247 		}
   1248 	ret:
   1249 		if (ptr)
   1250 			free(ptr, M_TEMP);
   1251 		return (error);
   1252 	}
   1253 	case USB_GET_DEVICEINFO:
   1254 		usbd_fill_deviceinfo(sc->sc_udev,
   1255 				     (struct usb_device_info *)addr, 1);
   1256 		break;
   1257 	default:
   1258 		return (EINVAL);
   1259 	}
   1260 	return (0);
   1261 }
   1262 
   1263 int
   1264 ugenioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
   1265 {
   1266 	int endpt = UGENENDPOINT(dev);
   1267 	struct ugen_softc *sc;
   1268 	int error;
   1269 
   1270 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1271 
   1272 	sc->sc_refcnt++;
   1273 	error = ugen_do_ioctl(sc, endpt, cmd, addr, flag, p);
   1274 	if (--sc->sc_refcnt < 0)
   1275 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1276 	return (error);
   1277 }
   1278 
   1279 int
   1280 ugenpoll(dev_t dev, int events, usb_proc_ptr p)
   1281 {
   1282 	struct ugen_softc *sc;
   1283 	struct ugen_endpoint *sce;
   1284 	int revents = 0;
   1285 	int s;
   1286 
   1287 	USB_GET_SC(ugen, UGENUNIT(dev), sc);
   1288 
   1289 	if (sc->sc_dying)
   1290 		return (EIO);
   1291 
   1292 	/* XXX always IN */
   1293 	sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
   1294 	if (sce == NULL)
   1295 		return (EINVAL);
   1296 #ifdef DIAGNOSTIC
   1297 	if (!sce->edesc) {
   1298 		printf("ugenpoll: no edesc\n");
   1299 		return (EIO);
   1300 	}
   1301 	if (!sce->pipeh) {
   1302 		printf("ugenpoll: no pipe\n");
   1303 		return (EIO);
   1304 	}
   1305 #endif
   1306 	s = splusb();
   1307 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
   1308 	case UE_INTERRUPT:
   1309 		if (events & (POLLIN | POLLRDNORM)) {
   1310 			if (sce->q.c_cc > 0)
   1311 				revents |= events & (POLLIN | POLLRDNORM);
   1312 			else
   1313 				selrecord(p, &sce->rsel);
   1314 		}
   1315 		break;
   1316 	case UE_ISOCHRONOUS:
   1317 		if (events & (POLLIN | POLLRDNORM)) {
   1318 			if (sce->cur != sce->fill)
   1319 				revents |= events & (POLLIN | POLLRDNORM);
   1320 			else
   1321 				selrecord(p, &sce->rsel);
   1322 		}
   1323 		break;
   1324 	case UE_BULK:
   1325 		/*
   1326 		 * We have no easy way of determining if a read will
   1327 		 * yield any data or a write will happen.
   1328 		 * Pretend they will.
   1329 		 */
   1330 		revents |= events &
   1331 			   (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
   1332 		break;
   1333 	default:
   1334 		break;
   1335 	}
   1336 	splx(s);
   1337 	return (revents);
   1338 }
   1339 
   1340 #if defined(__FreeBSD__)
   1341 DRIVER_MODULE(ugen, uhub, ugen_driver, ugen_devclass, usbd_driver_load, 0);
   1342 #endif
   1343