Home | History | Annotate | Line # | Download | only in usb
ugen.c revision 1.10
      1 /*	$NetBSD: ugen.c,v 1.10 1999/01/07 02:22:20 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #include <sys/device.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/tty.h>
     48 #include <sys/file.h>
     49 #include <sys/select.h>
     50 #include <sys/proc.h>
     51 #include <sys/vnode.h>
     52 #include <sys/poll.h>
     53 
     54 #include <dev/usb/usb.h>
     55 #include <dev/usb/usbdi.h>
     56 #include <dev/usb/usbdi_util.h>
     57 
     58 #ifdef USB_DEBUG
     59 #define DPRINTF(x)	if (ugendebug) printf x
     60 #define DPRINTFN(n,x)	if (ugendebug>(n)) printf x
     61 int	ugendebug = 0;
     62 #else
     63 #define DPRINTF(x)
     64 #define DPRINTFN(n,x)
     65 #endif
     66 
     67 struct ugen_endpoint {
     68 	struct ugen_softc *sc;
     69 	usb_endpoint_descriptor_t *edesc;
     70 	usbd_interface_handle iface;
     71 	int state;
     72 #define UGEN_OPEN	0x01	/* device is open */
     73 #define	UGEN_ASLP	0x02	/* waiting for data */
     74 #define UGEN_SHORT_OK	0x04	/* short xfers are OK */
     75 	usbd_pipe_handle pipeh;
     76 	struct clist q;
     77 	struct selinfo rsel;
     78 	void *ibuf;
     79 };
     80 
     81 #define	UGEN_CHUNK	128	/* chunk size for read */
     82 #define	UGEN_IBSIZE	1020	/* buffer size */
     83 #define	UGEN_BBSIZE	1024
     84 
     85 struct ugen_softc {
     86 	struct device sc_dev;		/* base device */
     87 	struct usbd_device *sc_udev;
     88 
     89 	struct ugen_endpoint sc_endpoints[USB_MAX_ENDPOINTS][2];
     90 #define OUT 0			/* index order is important, from UE_OUT */
     91 #define IN  1			/* from UE_IN */
     92 
     93 	int sc_disconnected;		/* device is gone */
     94 };
     95 
     96 int ugen_match __P((struct device *, struct cfdata *, void *));
     97 void ugen_attach __P((struct device *, struct device *, void *));
     98 
     99 int ugenopen __P((dev_t, int, int, struct proc *));
    100 int ugenclose __P((dev_t, int, int, struct proc *p));
    101 int ugenread __P((dev_t, struct uio *uio, int));
    102 int ugenwrite __P((dev_t, struct uio *uio, int));
    103 int ugenioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
    104 int ugenpoll __P((dev_t, int, struct proc *));
    105 void ugenintr __P((usbd_request_handle reqh, usbd_private_handle addr,
    106 		   usbd_status status));
    107 void ugen_disco __P((void *));
    108 
    109 int ugen_set_config __P((struct ugen_softc *sc, int configno));
    110 usb_config_descriptor_t *ugen_get_cdesc __P((struct ugen_softc *sc, int index,
    111 					     int *lenp));
    112 usbd_status ugen_set_interface __P((struct ugen_softc *, int, int));
    113 int ugen_get_alt_index __P((struct ugen_softc *sc, int ifaceidx));
    114 
    115 #define UGENUNIT(n) (((n) >> 4) & 0xf)
    116 #define UGENENDPOINT(n) ((n) & 0xf)
    117 
    118 USB_DECLARE_DRIVER(ugen);
    119 
    120 USB_MATCH(ugen)
    121 {
    122 	USB_MATCH_START(ugen, uaa);
    123 
    124 	if (uaa->usegeneric)
    125 		return (UMATCH_GENERIC);
    126 	else
    127 		return (UMATCH_NONE);
    128 }
    129 
    130 USB_ATTACH(ugen)
    131 {
    132 	USB_ATTACH_START(ugen, sc, uaa);
    133 	char devinfo[1024];
    134 	usbd_status r;
    135 	int conf;
    136 
    137 	usbd_devinfo(uaa->device, 0, devinfo);
    138 	USB_ATTACH_SETUP;
    139 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    140 
    141 	sc->sc_udev = uaa->device;
    142 	conf = 1;		/* XXX should not hard code 1 */
    143 	r = ugen_set_config(sc, conf);
    144 	if (r != USBD_NORMAL_COMPLETION) {
    145 		printf("%s: setting configuration %d failed\n",
    146 		       USBDEVNAME(sc->sc_dev), conf);
    147 		sc->sc_disconnected = 1;
    148 		USB_ATTACH_ERROR_RETURN;
    149 	}
    150 	USB_ATTACH_SUCCESS_RETURN;
    151 }
    152 
    153 int
    154 ugen_set_config(sc, configno)
    155 	struct ugen_softc *sc;
    156 	int configno;
    157 {
    158 	usbd_device_handle dev = sc->sc_udev;
    159 	usbd_interface_handle iface;
    160 	usb_endpoint_descriptor_t *ed;
    161 	struct ugen_endpoint *sce;
    162 	u_int8_t niface, nendpt;
    163 	int ifaceno, endptno, endpt;
    164 	usbd_status r;
    165 
    166 	DPRINTFN(1,("ugen_set_config: %s to configno %d, sc=%p\n",
    167 		    USBDEVNAME(sc->sc_dev), configno, sc));
    168 	if (usbd_get_config_descriptor(dev)->bConfigurationValue != configno) {
    169 		/* Avoid setting the current value. */
    170 		r = usbd_set_config_no(dev, configno, 0);
    171 		if (r != USBD_NORMAL_COMPLETION)
    172 			return (r);
    173 	}
    174 
    175 	r = usbd_interface_count(dev, &niface);
    176 	if (r != USBD_NORMAL_COMPLETION)
    177 		return (r);
    178 	memset(sc->sc_endpoints, 0, sizeof sc->sc_endpoints);
    179 	for (ifaceno = 0; ifaceno < niface; ifaceno++) {
    180 		DPRINTFN(1,("ugen_set_config: ifaceno %d\n", ifaceno));
    181 		r = usbd_device2interface_handle(dev, ifaceno, &iface);
    182 		if (r != USBD_NORMAL_COMPLETION)
    183 			return (r);
    184 		r = usbd_endpoint_count(iface, &nendpt);
    185 		if (r != USBD_NORMAL_COMPLETION)
    186 			return (r);
    187 		for (endptno = 0; endptno < nendpt; endptno++) {
    188 			ed = usbd_interface2endpoint_descriptor(iface,endptno);
    189 			endpt = ed->bEndpointAddress;
    190 			sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)]
    191 				               [UE_GET_IN(endpt)];
    192 			DPRINTFN(1,("ugen_set_config: endptno %d, endpt=0x%02x"
    193 				    "(%d,%d), sce=%p\n",
    194 				    endptno, endpt, UE_GET_ADDR(endpt),
    195 				    UE_GET_IN(endpt), sce));
    196 			sce->sc = sc;
    197 			sce->edesc = ed;
    198 			sce->iface = iface;
    199 		}
    200 	}
    201 	return (USBD_NORMAL_COMPLETION);
    202 }
    203 
    204 void
    205 ugen_disco(p)
    206 	void *p;
    207 {
    208 	struct ugen_softc *sc = p;
    209 	sc->sc_disconnected = 1;
    210 }
    211 
    212 int
    213 ugenopen(dev, flag, mode, p)
    214 	dev_t dev;
    215 	int flag;
    216 	int mode;
    217 	struct proc *p;
    218 {
    219 	int unit = UGENUNIT(dev);
    220 	int endpt = UGENENDPOINT(dev);
    221 	usb_endpoint_descriptor_t *edesc;
    222 	struct ugen_softc *sc;
    223 	struct ugen_endpoint *sce;
    224 	int dir, isize;
    225 	usbd_status r;
    226 
    227 	DPRINTFN(5, ("ugenopen: flag=%d, mode=%d, unit=%d endpt=%d\n",
    228 		     flag, mode, unit, endpt));
    229 	if (unit >= ugen_cd.cd_ndevs)
    230 		return (ENXIO);
    231 	sc = ugen_cd.cd_devs[unit];
    232 	if (!sc)
    233 		return (ENXIO);
    234 
    235 	if (sc->sc_disconnected)
    236 		return (EIO);
    237 
    238 	if (endpt == USB_CONTROL_ENDPOINT) {
    239 		/*if ((flag & (FWRITE|FREAD)) != (FWRITE|FREAD))
    240 		  return (EACCES);*/
    241 		sce = &sc->sc_endpoints[USB_CONTROL_ENDPOINT][OUT];
    242 		if (sce->state & UGEN_OPEN)
    243 			return (EBUSY);
    244 	} else {
    245 		switch (flag & (FWRITE|FREAD)) {
    246 		case FWRITE:
    247 			dir = OUT;
    248 			break;
    249 		case FREAD:
    250 			dir = IN;
    251 			break;
    252 		default:
    253 			return (EACCES);
    254 		}
    255 		sce = &sc->sc_endpoints[endpt][dir];
    256 		DPRINTFN(5, ("ugenopen: sc=%p, endpt=%d, dir=%d, sce=%p\n",
    257 			     sc, endpt, dir, sce));
    258 		if (sce->state & UGEN_OPEN)
    259 			return (EBUSY);
    260 		edesc = sce->edesc;
    261 		if (!edesc)
    262 			return (ENXIO);
    263 		switch (edesc->bmAttributes & UE_XFERTYPE) {
    264 		case UE_INTERRUPT:
    265 			isize = UGETW(edesc->wMaxPacketSize);
    266 			if (isize == 0)	/* shouldn't happen */
    267 				return (EINVAL);
    268 			sce->ibuf = malloc(isize, M_USB, M_WAITOK);
    269 			DPRINTFN(5, ("ugenopen: intr endpt=%d,isize=%d\n",
    270 				     endpt, isize));
    271 			if (clalloc(&sce->q, UGEN_IBSIZE, 0) == -1)
    272 				return (ENOMEM);
    273 			r = usbd_open_pipe_intr(sce->iface,
    274 				edesc->bEndpointAddress,
    275 				USBD_SHORT_XFER_OK, &sce->pipeh, sce,
    276 				sce->ibuf, isize, ugenintr);
    277 			if (r != USBD_NORMAL_COMPLETION) {
    278 				free(sce->ibuf, M_USB);
    279 				clfree(&sce->q);
    280 				return (EIO);
    281 			}
    282 			usbd_set_disco(sce->pipeh, ugen_disco, sc);
    283 			DPRINTFN(5, ("ugenopen: interrupt open done\n"));
    284 			break;
    285 		case UE_BULK:
    286 			r = usbd_open_pipe(sce->iface,
    287 					   edesc->bEndpointAddress, 0,
    288 					   &sce->pipeh);
    289 			if (r != USBD_NORMAL_COMPLETION)
    290 				return (EIO);
    291 			break;
    292 		case UE_CONTROL:
    293 		case UE_ISOCHRONOUS:
    294 			return (EINVAL);
    295 		}
    296 	}
    297 	sce->state |= UGEN_OPEN;
    298 	return (0);
    299 }
    300 
    301 int
    302 ugenclose(dev, flag, mode, p)
    303 	dev_t dev;
    304 	int flag;
    305 	int mode;
    306 	struct proc *p;
    307 {
    308 	struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)];
    309 	int endpt = UGENENDPOINT(dev);
    310 	struct ugen_endpoint *sce;
    311 	int dir;
    312 
    313 	DPRINTFN(5, ("ugenclose: flag=%d, mode=%d\n", flag, mode));
    314 	if (sc->sc_disconnected)
    315 		return (EIO);
    316 
    317 	if (endpt == USB_CONTROL_ENDPOINT) {
    318 		DPRINTFN(5, ("ugenclose: close control\n"));
    319 		sc->sc_endpoints[endpt][OUT].state = 0;
    320 		return (0);
    321 	}
    322 
    323 	flag = FWRITE | FREAD;	/* XXX bug if generic open/close */
    324 
    325 	/* The open modes have been joined, so check for both modes. */
    326 	for (dir = OUT; dir <= IN; dir++) {
    327 		if (flag & (dir == OUT ? FWRITE : FREAD)) {
    328 			sce = &sc->sc_endpoints[endpt][dir];
    329 			if (!sce || !sce->pipeh) /* XXX */
    330 				continue; /* XXX */
    331 			DPRINTFN(5, ("ugenclose: endpt=%d dir=%d sce=%p\n",
    332 				     endpt, dir, sce));
    333 			sce->state = 0;
    334 
    335 			usbd_abort_pipe(sce->pipeh);
    336 			usbd_close_pipe(sce->pipeh);
    337 			sce->pipeh = 0;
    338 
    339 			if (sce->ibuf) {
    340 				free(sce->ibuf, M_USB);
    341 				sce->ibuf = 0;
    342 			}
    343 		}
    344 	}
    345 
    346 	return (0);
    347 }
    348 
    349 int
    350 ugenread(dev, uio, flag)
    351 	dev_t dev;
    352 	struct uio *uio;
    353 	int flag;
    354 {
    355 	struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)];
    356 	int endpt = UGENENDPOINT(dev);
    357 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][IN];
    358 	u_int32_t n, tn;
    359 	char buf[UGEN_BBSIZE];
    360 	usbd_request_handle reqh;
    361 	usbd_status r;
    362 	int s;
    363 	int error = 0;
    364 	u_char buffer[UGEN_CHUNK];
    365 
    366 	DPRINTFN(5, ("ugenread: %d:%d\n", UGENUNIT(dev), UGENENDPOINT(dev)));
    367 	if (sc->sc_disconnected)
    368 		return (EIO);
    369 
    370 #ifdef DIAGNOSTIC
    371 	if (!sce->edesc) {
    372 		printf("ugenread: no edesc\n");
    373 		return (EIO);
    374 	}
    375 	if (!sce->pipeh) {
    376 		printf("ugenread: no pipe\n");
    377 		return (EIO);
    378 	}
    379 #endif
    380 
    381 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    382 	case UE_INTERRUPT:
    383 		/* Block until activity occured. */
    384 		s = splusb();
    385 		while (sce->q.c_cc == 0) {
    386 			if (flag & IO_NDELAY) {
    387 				splx(s);
    388 				return (EWOULDBLOCK);
    389 			}
    390 			sce->state |= UGEN_ASLP;
    391 			DPRINTFN(5, ("ugenread: sleep on %p\n", sc));
    392 			error = tsleep((caddr_t)sce, PZERO | PCATCH,
    393 				       "ugenri", 0);
    394 			DPRINTFN(5, ("ugenread: woke, error=%d\n", error));
    395 			if (error) {
    396 				sce->state &= ~UGEN_ASLP;
    397 				splx(s);
    398 				return (error);
    399 			}
    400 		}
    401 		splx(s);
    402 
    403 		/* Transfer as many chunks as possible. */
    404 		while (sce->q.c_cc > 0 && uio->uio_resid > 0) {
    405 			n = min(sce->q.c_cc, uio->uio_resid);
    406 			if (n > sizeof(buffer))
    407 				n = sizeof(buffer);
    408 
    409 			/* Remove a small chunk from the input queue. */
    410 			q_to_b(&sce->q, buffer, n);
    411 			DPRINTFN(5, ("ugenread: got %d chars\n", n));
    412 
    413 			/* Copy the data to the user process. */
    414 			error = uiomove(buffer, n, uio);
    415 			if (error)
    416 				break;
    417 		}
    418 		break;
    419 	case UE_BULK:
    420 		reqh = usbd_alloc_request();
    421 		if (reqh == 0)
    422 			return (ENOMEM);
    423 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    424 			DPRINTFN(1, ("ugenread: start transfer %d bytes\n",n));
    425 			tn = n;
    426 			r = usbd_bulk_transfer(reqh, sce->pipeh, 0, buf,
    427 					       &tn, "ugenrb");
    428 			if (r != USBD_NORMAL_COMPLETION) {
    429 				if (r == USBD_INTERRUPTED)
    430 					error = EINTR;
    431 				else
    432 					error = EIO;
    433 				break;
    434 			}
    435 			DPRINTFN(1, ("ugenread: got %d bytes\n", tn));
    436 			error = uiomove(buf, tn, uio);
    437 			if (error || tn < n)
    438 				break;
    439 		}
    440 		usbd_free_request(reqh);
    441 		break;
    442 	default:
    443 		return (ENXIO);
    444 	}
    445 
    446 	return (error);
    447 }
    448 
    449 int
    450 ugenwrite(dev, uio, flag)
    451 	dev_t dev;
    452 	struct uio *uio;
    453 	int flag;
    454 {
    455 	struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)];
    456 	int endpt = UGENENDPOINT(dev);
    457 	struct ugen_endpoint *sce = &sc->sc_endpoints[endpt][OUT];
    458 	size_t n;
    459 	int error = 0;
    460 	char buf[UGEN_BBSIZE];
    461 	usbd_request_handle reqh;
    462 	usbd_status r;
    463 
    464 	if (sc->sc_disconnected)
    465 		return (EIO);
    466 
    467 #ifdef DIAGNOSTIC
    468 	if (!sce->edesc) {
    469 		printf("ugenwrite: no edesc\n");
    470 		return (EIO);
    471 	}
    472 	if (!sce->pipeh) {
    473 		printf("ugenwrite: no pipe\n");
    474 		return (EIO);
    475 	}
    476 #endif
    477 
    478 	DPRINTF(("ugenwrite\n"));
    479 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    480 	case UE_BULK:
    481 		reqh = usbd_alloc_request();
    482 		if (reqh == 0)
    483 			return (EIO);
    484 		while ((n = min(UGEN_BBSIZE, uio->uio_resid)) != 0) {
    485 			error = uiomove(buf, n, uio);
    486 			if (error)
    487 				break;
    488 			DPRINTFN(1, ("ugenwrite: transfer %d bytes\n", n));
    489 			r = usbd_bulk_transfer(reqh, sce->pipeh, 0, buf,
    490 					       &n, "ugenwb");
    491 			if (r != USBD_NORMAL_COMPLETION) {
    492 				if (r == USBD_INTERRUPTED)
    493 					error = EINTR;
    494 				else
    495 					error = EIO;
    496 				break;
    497 			}
    498 		}
    499 		usbd_free_request(reqh);
    500 		break;
    501 	default:
    502 		return (ENXIO);
    503 	}
    504 	return (error);
    505 }
    506 
    507 
    508 void
    509 ugenintr(reqh, addr, status)
    510 	usbd_request_handle reqh;
    511 	usbd_private_handle addr;
    512 	usbd_status status;
    513 {
    514 	struct ugen_endpoint *sce = addr;
    515 	/*struct ugen_softc *sc = sce->sc;*/
    516 	usbd_private_handle priv;
    517 	void *buffer;
    518 	u_int32_t count;
    519 	usbd_status xstatus;
    520 	u_char *ibuf;
    521 
    522 	if (status == USBD_CANCELLED)
    523 		return;
    524 
    525 	if (status != USBD_NORMAL_COMPLETION) {
    526 		DPRINTF(("ugenintr: status=%d\n", status));
    527 		usbd_clear_endpoint_stall_async(sce->pipeh);
    528 		return;
    529 	}
    530 
    531 	(void)usbd_get_request_status(reqh, &priv, &buffer, &count, &xstatus);
    532 	ibuf = sce->ibuf;
    533 
    534 	DPRINTFN(5, ("ugenintr: reqh=%p status=%d count=%d\n",
    535 		     reqh, xstatus, count));
    536 	DPRINTFN(5, ("          data = %02x %02x %02x\n",
    537 		     ibuf[0], ibuf[1], ibuf[2]));
    538 
    539 	(void)b_to_q(ibuf, count, &sce->q);
    540 
    541 	if (sce->state & UGEN_ASLP) {
    542 		sce->state &= ~UGEN_ASLP;
    543 		DPRINTFN(5, ("ugen_intr: waking %p\n", sce));
    544 		wakeup((caddr_t)sce);
    545 	}
    546 	selwakeup(&sce->rsel);
    547 }
    548 
    549 usbd_status
    550 ugen_set_interface(sc, ifaceidx, altno)
    551 	struct ugen_softc *sc;
    552 	int ifaceidx, altno;
    553 {
    554 	usbd_interface_handle iface;
    555 	usb_endpoint_descriptor_t *ed;
    556 	usbd_status r;
    557 	struct ugen_endpoint *sce;
    558 	u_int8_t niface, nendpt, endptno, endpt;
    559 
    560 	DPRINTFN(15, ("ugen_set_interface %d %d\n", ifaceidx, altno));
    561 
    562 	r = usbd_interface_count(sc->sc_udev, &niface);
    563 	if (r != USBD_NORMAL_COMPLETION)
    564 		return (r);
    565 	if (ifaceidx < 0 || ifaceidx >= niface)
    566 		return (USBD_INVAL);
    567 
    568 	r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    569 	if (r != USBD_NORMAL_COMPLETION)
    570 		return (r);
    571 	r = usbd_endpoint_count(iface, &nendpt);
    572 	if (r != USBD_NORMAL_COMPLETION)
    573 		return (r);
    574 	for (endptno = 0; endptno < nendpt; endptno++) {
    575 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    576 		endpt = ed->bEndpointAddress;
    577 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][UE_GET_IN(endpt)];
    578 		sce->sc = 0;
    579 		sce->edesc = 0;
    580 		sce->iface = 0;
    581 	}
    582 
    583 	/* change setting */
    584 	r = usbd_set_interface(iface, altno);
    585 	if (r != USBD_NORMAL_COMPLETION)
    586 		return (r);
    587 
    588 	r = usbd_endpoint_count(iface, &nendpt);
    589 	if (r != USBD_NORMAL_COMPLETION)
    590 		return (r);
    591 	for (endptno = 0; endptno < nendpt; endptno++) {
    592 		ed = usbd_interface2endpoint_descriptor(iface,endptno);
    593 		endpt = ed->bEndpointAddress;
    594 		sce = &sc->sc_endpoints[UE_GET_ADDR(endpt)][UE_GET_IN(endpt)];
    595 		sce->sc = sc;
    596 		sce->edesc = ed;
    597 		sce->iface = iface;
    598 	}
    599 	return (0);
    600 }
    601 
    602 /* Retrieve a complete descriptor for a certain device and index. */
    603 usb_config_descriptor_t *
    604 ugen_get_cdesc(sc, index, lenp)
    605 	struct ugen_softc *sc;
    606 	int index;
    607 	int *lenp;
    608 {
    609 	usb_config_descriptor_t *cdesc, *tdesc, cdescr;
    610 	int len;
    611 	usbd_status r;
    612 
    613 	if (index == USB_CURRENT_CONFIG_INDEX) {
    614 		tdesc = usbd_get_config_descriptor(sc->sc_udev);
    615 		len = UGETW(tdesc->wTotalLength);
    616 		if (lenp)
    617 			*lenp = len;
    618 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    619 		memcpy(cdesc, tdesc, len);
    620 		DPRINTFN(5,("ugen_get_cdesc: current, len=%d\n", len));
    621 	} else {
    622 		r = usbd_get_config_desc(sc->sc_udev, index, &cdescr);
    623 		if (r != USBD_NORMAL_COMPLETION)
    624 			return (0);
    625 		len = UGETW(cdescr.wTotalLength);
    626 		DPRINTFN(5,("ugen_get_cdesc: index=%d, len=%d\n", index, len));
    627 		if (lenp)
    628 			*lenp = len;
    629 		cdesc = malloc(len, M_TEMP, M_WAITOK);
    630 		r = usbd_get_config_desc_full(sc->sc_udev, index, cdesc, len);
    631 		if (r != USBD_NORMAL_COMPLETION) {
    632 			free(cdesc, M_TEMP);
    633 			return (0);
    634 		}
    635 	}
    636 	return (cdesc);
    637 }
    638 
    639 int
    640 ugen_get_alt_index(sc, ifaceidx)
    641 	struct ugen_softc *sc;
    642 	int ifaceidx;
    643 {
    644 	usbd_interface_handle iface;
    645 	usbd_status r;
    646 
    647 	r = usbd_device2interface_handle(sc->sc_udev, ifaceidx, &iface);
    648 	if (r != USBD_NORMAL_COMPLETION)
    649 			return (-1);
    650 	return (usbd_get_interface_altindex(iface));
    651 }
    652 
    653 int
    654 ugenioctl(dev, cmd, addr, flag, p)
    655 	dev_t dev;
    656 	u_long cmd;
    657 	caddr_t addr;
    658 	int flag;
    659 	struct proc *p;
    660 {
    661 	struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)];
    662 	int endpt = UGENENDPOINT(dev);
    663 	struct ugen_endpoint *sce;
    664 	usbd_status r;
    665 	usbd_interface_handle iface;
    666 	struct usb_config_desc *cd;
    667 	usb_config_descriptor_t *cdesc;
    668 	struct usb_interface_desc *id;
    669 	usb_interface_descriptor_t *idesc;
    670 	struct usb_endpoint_desc *ed;
    671 	usb_endpoint_descriptor_t *edesc;
    672 	struct usb_alt_interface *ai;
    673 	struct usb_string_desc *si;
    674 	u_int8_t conf, alt;
    675 
    676 	DPRINTFN(5, ("ugenioctl: cmd=%08lx\n", cmd));
    677 	if (sc->sc_disconnected)
    678 		return (EIO);
    679 
    680 	switch (cmd) {
    681 	case FIONBIO:
    682 		/* All handled in the upper FS layer. */
    683 		return (0);
    684 	case USB_SET_SHORT_XFER:
    685 		/* This flag only affects read */
    686 		sce = &sc->sc_endpoints[endpt][IN];
    687 #ifdef DIAGNOSTIC
    688 		if (!sce->pipeh) {
    689 			printf("ugenioctl: no pipe\n");
    690 			return (EIO);
    691 		}
    692 #endif
    693 		if (*(int *)addr)
    694 			sce->state |= UGEN_SHORT_OK;
    695 		else
    696 			sce->state &= ~UGEN_SHORT_OK;
    697 		return (0);
    698 	default:
    699 		break;
    700 	}
    701 
    702 	if (endpt != USB_CONTROL_ENDPOINT)
    703 		return (EINVAL);
    704 
    705 	switch (cmd) {
    706 #ifdef USB_DEBUG
    707 	case USB_SETDEBUG:
    708 		ugendebug = *(int *)addr;
    709 		break;
    710 #endif
    711 	case USB_GET_CONFIG:
    712 		r = usbd_get_config(sc->sc_udev, &conf);
    713 		if (r != USBD_NORMAL_COMPLETION)
    714 			return (EIO);
    715 		*(int *)addr = conf;
    716 		break;
    717 	case USB_SET_CONFIG:
    718 		if (!(flag & FWRITE))
    719 			return (EPERM);
    720 		r = ugen_set_config(sc, *(int *)addr);
    721 		if (r != USBD_NORMAL_COMPLETION)
    722 			return (EIO);
    723 		break;
    724 	case USB_GET_ALTINTERFACE:
    725 		ai = (struct usb_alt_interface *)addr;
    726 		r = usbd_device2interface_handle(sc->sc_udev,
    727 						 ai->interface_index, &iface);
    728 		if (r != USBD_NORMAL_COMPLETION)
    729 			return (EINVAL);
    730 		idesc = usbd_get_interface_descriptor(iface);
    731 		if (!idesc)
    732 			return (EIO);
    733 		ai->alt_no = idesc->bAlternateSetting;
    734 		break;
    735 	case USB_SET_ALTINTERFACE:
    736 		if (!(flag & FWRITE))
    737 			return (EPERM);
    738 		ai = (struct usb_alt_interface *)addr;
    739 		r = usbd_device2interface_handle(sc->sc_udev,
    740 						 ai->interface_index, &iface);
    741 		if (r != USBD_NORMAL_COMPLETION)
    742 			return (EINVAL);
    743 		r = ugen_set_interface(sc, ai->interface_index, ai->alt_no);
    744 		if (r != USBD_NORMAL_COMPLETION)
    745 			return (EINVAL);
    746 		break;
    747 	case USB_GET_NO_ALT:
    748 		ai = (struct usb_alt_interface *)addr;
    749 		cdesc = ugen_get_cdesc(sc, ai->config_index, 0);
    750 		if (!cdesc)
    751 			return (EINVAL);
    752 		idesc = usbd_find_idesc(cdesc, ai->interface_index, 0);
    753 		if (!idesc)
    754 			return (EINVAL);
    755 		ai->alt_no = usbd_get_no_alts(cdesc, idesc->bInterfaceNumber);
    756 		break;
    757 	case USB_GET_DEVICE_DESC:
    758 		*(usb_device_descriptor_t *)addr =
    759 			*usbd_get_device_descriptor(sc->sc_udev);
    760 		break;
    761 	case USB_GET_CONFIG_DESC:
    762 		cd = (struct usb_config_desc *)addr;
    763 		cdesc = ugen_get_cdesc(sc, cd->config_index, 0);
    764 		if (!cdesc)
    765 			return (EINVAL);
    766 		cd->desc = *cdesc;
    767 		free(cdesc, M_TEMP);
    768 		break;
    769 	case USB_GET_INTERFACE_DESC:
    770 		id = (struct usb_interface_desc *)addr;
    771 		cdesc = ugen_get_cdesc(sc, id->config_index, 0);
    772 		if (!cdesc)
    773 			return (EINVAL);
    774 		if (id->config_index == USB_CURRENT_CONFIG_INDEX &&
    775 		    id->alt_index == USB_CURRENT_ALT_INDEX)
    776 			alt = ugen_get_alt_index(sc, id->interface_index);
    777 		else
    778 			alt = id->alt_index;
    779 		idesc = usbd_find_idesc(cdesc, id->interface_index, alt);
    780 		if (!idesc) {
    781 			free(cdesc, M_TEMP);
    782 			return (EINVAL);
    783 		}
    784 		id->desc = *idesc;
    785 		free(cdesc, M_TEMP);
    786 		break;
    787 	case USB_GET_ENDPOINT_DESC:
    788 		ed = (struct usb_endpoint_desc *)addr;
    789 		cdesc = ugen_get_cdesc(sc, ed->config_index, 0);
    790 		if (!cdesc)
    791 			return (EINVAL);
    792 		if (ed->config_index == USB_CURRENT_CONFIG_INDEX &&
    793 		    ed->alt_index == USB_CURRENT_ALT_INDEX)
    794 			alt = ugen_get_alt_index(sc, ed->interface_index);
    795 		else
    796 			alt = ed->alt_index;
    797 		edesc = usbd_find_edesc(cdesc, ed->interface_index,
    798 					alt, ed->endpoint_index);
    799 		if (!edesc) {
    800 			free(cdesc, M_TEMP);
    801 			return (EINVAL);
    802 		}
    803 		ed->desc = *edesc;
    804 		free(cdesc, M_TEMP);
    805 		break;
    806 	case USB_GET_FULL_DESC:
    807 	{
    808 		int len;
    809 		struct iovec iov;
    810 		struct uio uio;
    811 		struct usb_full_desc *fd = (struct usb_full_desc *)addr;
    812 		int error;
    813 
    814 		cdesc = ugen_get_cdesc(sc, fd->config_index, &len);
    815 		if (len > fd->size)
    816 			len = fd->size;
    817 		iov.iov_base = (caddr_t)fd->data;
    818 		iov.iov_len = len;
    819 		uio.uio_iov = &iov;
    820 		uio.uio_iovcnt = 1;
    821 		uio.uio_resid = len;
    822 		uio.uio_offset = 0;
    823 		uio.uio_segflg = UIO_USERSPACE;
    824 		uio.uio_rw = UIO_READ;
    825 		uio.uio_procp = p;
    826 		error = uiomove(cdesc, len, &uio);
    827 		free(cdesc, M_TEMP);
    828 		return (error);
    829 	}
    830 	case USB_GET_STRING_DESC:
    831 		si = (struct usb_string_desc *)addr;
    832 		r = usbd_get_string_desc(sc->sc_udev, si->string_index,
    833 					 si->language_id, &si->desc);
    834 		if (r != USBD_NORMAL_COMPLETION)
    835 			return (EINVAL);
    836 		break;
    837 	case USB_DO_REQUEST:
    838 	{
    839 		struct usb_ctl_request *ur = (void *)addr;
    840 		int len = UGETW(ur->request.wLength);
    841 		struct iovec iov;
    842 		struct uio uio;
    843 		void *ptr = 0;
    844 		usbd_status r;
    845 		int error = 0;
    846 
    847 		if (!(flag & FWRITE))
    848 			return (EPERM);
    849 		/* Avoid requests that would damage the bus integrity. */
    850 		if ((ur->request.bmRequestType == UT_WRITE_DEVICE &&
    851 		     ur->request.bRequest == UR_SET_ADDRESS) ||
    852 		    (ur->request.bmRequestType == UT_WRITE_DEVICE &&
    853 		     ur->request.bRequest == UR_SET_CONFIG) ||
    854 		    (ur->request.bmRequestType == UT_WRITE_INTERFACE &&
    855 		     ur->request.bRequest == UR_SET_INTERFACE))
    856 			return (EINVAL);
    857 
    858 		if (len < 0 || len > 32767)
    859 			return (EINVAL);
    860 		if (len != 0) {
    861 			iov.iov_base = (caddr_t)ur->data;
    862 			iov.iov_len = len;
    863 			uio.uio_iov = &iov;
    864 			uio.uio_iovcnt = 1;
    865 			uio.uio_resid = len;
    866 			uio.uio_offset = 0;
    867 			uio.uio_segflg = UIO_USERSPACE;
    868 			uio.uio_rw =
    869 				ur->request.bmRequestType & UT_READ ?
    870 				UIO_READ : UIO_WRITE;
    871 			uio.uio_procp = p;
    872 			ptr = malloc(len, M_TEMP, M_WAITOK);
    873 			if (uio.uio_rw == UIO_WRITE) {
    874 				error = uiomove(ptr, len, &uio);
    875 				if (error)
    876 					goto ret;
    877 			}
    878 		}
    879 		r = usbd_do_request_flags(sc->sc_udev, &ur->request,
    880 					  ptr, ur->flags, &ur->actlen);
    881 		if (r) {
    882 			error = EIO;
    883 			goto ret;
    884 		}
    885 		if (len != 0) {
    886 			if (uio.uio_rw == UIO_READ) {
    887 				error = uiomove(ptr, len, &uio);
    888 				if (error)
    889 					goto ret;
    890 			}
    891 		}
    892 	ret:
    893 		if (ptr)
    894 			free(ptr, M_TEMP);
    895 		return (error);
    896 	}
    897 	case USB_GET_DEVICEINFO:
    898 		usbd_fill_deviceinfo(sc->sc_udev,
    899 				     (struct usb_device_info *)addr);
    900 		break;
    901 	default:
    902 		return (EINVAL);
    903 	}
    904 	return (0);
    905 }
    906 
    907 int
    908 ugenpoll(dev, events, p)
    909 	dev_t dev;
    910 	int events;
    911 	struct proc *p;
    912 {
    913 	struct ugen_softc *sc = ugen_cd.cd_devs[UGENUNIT(dev)];
    914 	/* XXX */
    915 	struct ugen_endpoint *sce;
    916 	int revents = 0;
    917 	int s;
    918 
    919 	if (sc->sc_disconnected)
    920 		return (EIO);
    921 
    922 	sce = &sc->sc_endpoints[UGENENDPOINT(dev)][IN];
    923 #ifdef DIAGNOSTIC
    924 	if (!sce->edesc) {
    925 		printf("ugenwrite: no edesc\n");
    926 		return (EIO);
    927 	}
    928 	if (!sce->pipeh) {
    929 		printf("ugenpoll: no pipe\n");
    930 		return (EIO);
    931 	}
    932 #endif
    933 	s = splusb();
    934 	switch (sce->edesc->bmAttributes & UE_XFERTYPE) {
    935 	case UE_INTERRUPT:
    936 		if (events & (POLLIN | POLLRDNORM)) {
    937 			if (sce->q.c_cc > 0)
    938 				revents |= events & (POLLIN | POLLRDNORM);
    939 			else
    940 				selrecord(p, &sce->rsel);
    941 		}
    942 		break;
    943 	case UE_BULK:
    944 		/*
    945 		 * We have no easy way of determining if a read will
    946 		 * yield any data or a write will happen.
    947 		 * Pretend they will.
    948 		 */
    949 		revents |= events &
    950 			   (POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM);
    951 		break;
    952 	default:
    953 		break;
    954 	}
    955 	splx(s);
    956 	return (revents);
    957 }
    958 
    959 #if defined(__FreeBSD__)
    960 static int
    961 ugen_detach(device_t self)
    962 {
    963         struct ugen_softc *sc = device_get_softc(self);
    964 	char *devinfo = (char *) device_get_desc(self);
    965 
    966 	if (devinfo) {
    967 		device_set_desc(self, NULL);
    968 		free(devinfo, M_USB);
    969 	}
    970 	return 0;
    971 }
    972 #endif
    973