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