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