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