Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.134.2.6
      1 /*	$NetBSD: usbdi.c,v 1.134.2.6 2012/02/19 21:37:12 mrg Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usbdi.c,v 1.28 1999/11/17 22:33:49 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.134.2.6 2012/02/19 21:37:12 mrg Exp $");
     36 
     37 #include "opt_compat_netbsd.h"
     38 #include "opt_usb.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/proc.h>
     46 
     47 #include <sys/bus.h>
     48 
     49 #include <dev/usb/usb.h>
     50 #include <dev/usb/usbdi.h>
     51 #include <dev/usb/usbdi_util.h>
     52 #include <dev/usb/usbdivar.h>
     53 #include <dev/usb/usb_mem.h>
     54 #include <dev/usb/usb_quirks.h>
     55 
     56 /* UTF-8 encoding stuff */
     57 #include <fs/unicode.h>
     58 
     59 #ifdef USB_DEBUG
     60 #define DPRINTF(x)	if (usbdebug) printf x
     61 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     62 extern int usbdebug;
     63 #else
     64 #define DPRINTF(x)
     65 #define DPRINTFN(n,x)
     66 #endif
     67 
     68 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
     69 Static void usbd_do_request_async_cb
     70 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
     71 Static void usbd_start_next(usbd_pipe_handle pipe);
     72 Static usbd_status usbd_open_pipe_ival
     73 	(usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
     74 
     75 static inline int
     76 usbd_xfer_isread(usbd_xfer_handle xfer)
     77 {
     78 	if (xfer->rqflags & URQ_REQUEST)
     79 		return (xfer->request.bmRequestType & UT_READ);
     80 	else
     81 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
     82 			UE_DIR_IN);
     83 }
     84 
     85 #if defined(USB_DEBUG) || defined(EHCI_DEBUG)
     86 void
     87 usbd_dump_iface(struct usbd_interface *iface)
     88 {
     89 	printf("usbd_dump_iface: iface=%p\n", iface);
     90 	if (iface == NULL)
     91 		return;
     92 	printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
     93 	       iface->device, iface->idesc, iface->index, iface->altindex,
     94 	       iface->priv);
     95 }
     96 
     97 void
     98 usbd_dump_device(struct usbd_device *dev)
     99 {
    100 	printf("usbd_dump_device: dev=%p\n", dev);
    101 	if (dev == NULL)
    102 		return;
    103 	printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
    104 	printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
    105 	       "power=%d langid=%d\n",
    106 	       dev->address, dev->config, dev->depth, dev->speed,
    107 	       dev->self_powered, dev->power, dev->langid);
    108 }
    109 
    110 void
    111 usbd_dump_endpoint(struct usbd_endpoint *endp)
    112 {
    113 	printf("usbd_dump_endpoint: endp=%p\n", endp);
    114 	if (endp == NULL)
    115 		return;
    116 	printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
    117 	if (endp->edesc)
    118 		printf(" bEndpointAddress=0x%02x\n",
    119 		       endp->edesc->bEndpointAddress);
    120 }
    121 
    122 void
    123 usbd_dump_queue(usbd_pipe_handle pipe)
    124 {
    125 	usbd_xfer_handle xfer;
    126 
    127 	printf("usbd_dump_queue: pipe=%p\n", pipe);
    128 	SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
    129 		printf("  xfer=%p\n", xfer);
    130 	}
    131 }
    132 
    133 void
    134 usbd_dump_pipe(usbd_pipe_handle pipe)
    135 {
    136 	printf("usbd_dump_pipe: pipe=%p\n", pipe);
    137 	if (pipe == NULL)
    138 		return;
    139 	usbd_dump_iface(pipe->iface);
    140 	usbd_dump_device(pipe->device);
    141 	usbd_dump_endpoint(pipe->endpoint);
    142 	printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
    143 	       pipe->refcnt, pipe->running, pipe->aborting);
    144 	printf(" intrxfer=%p, repeat=%d, interval=%d\n",
    145 	       pipe->intrxfer, pipe->repeat, pipe->interval);
    146 }
    147 #endif
    148 
    149 usbd_status
    150 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
    151 	       u_int8_t flags, usbd_pipe_handle *pipe)
    152 {
    153 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    154 				    USBD_DEFAULT_INTERVAL));
    155 }
    156 
    157 usbd_status
    158 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
    159 		    u_int8_t flags, usbd_pipe_handle *pipe, int ival)
    160 {
    161 	usbd_pipe_handle p;
    162 	struct usbd_endpoint *ep;
    163 	usbd_status err;
    164 	int i;
    165 
    166 	DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
    167 		    iface, address, flags));
    168 
    169 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    170 		ep = &iface->endpoints[i];
    171 		if (ep->edesc == NULL)
    172 			return (USBD_IOERROR);
    173 		if (ep->edesc->bEndpointAddress == address)
    174 			goto found;
    175 	}
    176 	return (USBD_BAD_ADDRESS);
    177  found:
    178 	if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
    179 		return (USBD_IN_USE);
    180 	err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
    181 	if (err)
    182 		return (err);
    183 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    184 	*pipe = p;
    185 	return (USBD_NORMAL_COMPLETION);
    186 }
    187 
    188 usbd_status
    189 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
    190 		    u_int8_t flags, usbd_pipe_handle *pipe,
    191 		    usbd_private_handle priv, void *buffer, u_int32_t len,
    192 		    usbd_callback cb, int ival)
    193 {
    194 	usbd_status err;
    195 	usbd_xfer_handle xfer;
    196 	usbd_pipe_handle ipipe;
    197 
    198 	DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
    199 		    address, flags, len));
    200 
    201 	err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
    202 				  &ipipe, ival);
    203 	if (err)
    204 		return (err);
    205 	xfer = usbd_alloc_xfer(iface->device);
    206 	if (xfer == NULL) {
    207 		err = USBD_NOMEM;
    208 		goto bad1;
    209 	}
    210 	usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
    211 	    USBD_NO_TIMEOUT, cb);
    212 	ipipe->intrxfer = xfer;
    213 	ipipe->repeat = 1;
    214 	err = usbd_transfer(xfer);
    215 	*pipe = ipipe;
    216 	if (err != USBD_IN_PROGRESS)
    217 		goto bad2;
    218 	return (USBD_NORMAL_COMPLETION);
    219 
    220  bad2:
    221 	ipipe->intrxfer = NULL;
    222 	ipipe->repeat = 0;
    223 	usbd_free_xfer(xfer);
    224  bad1:
    225 	usbd_close_pipe(ipipe);
    226 	return (err);
    227 }
    228 
    229 usbd_status
    230 usbd_close_pipe(usbd_pipe_handle pipe)
    231 {
    232 	int s;
    233 
    234 #ifdef DIAGNOSTIC
    235 	if (pipe == NULL) {
    236 		printf("usbd_close_pipe: pipe==NULL\n");
    237 		return (USBD_NORMAL_COMPLETION);
    238 	}
    239 #endif
    240 
    241 	usbd_lock_pipe(pipe);
    242 	if (--pipe->refcnt != 0) {
    243 		usbd_unlock_pipe(pipe);
    244 		return (USBD_NORMAL_COMPLETION);
    245 	}
    246 	if (! SIMPLEQ_EMPTY(&pipe->queue)) {
    247 		usbd_unlock_pipe(pipe);
    248 		return (USBD_PENDING_REQUESTS);
    249 	}
    250 	LIST_REMOVE(pipe, next);
    251 	pipe->endpoint->refcnt--;
    252 	pipe->methods->close(pipe);
    253 	usbd_unlock_pipe(pipe);
    254 	if (pipe->intrxfer != NULL)
    255 		usbd_free_xfer(pipe->intrxfer);
    256 	free(pipe, M_USB);
    257 	return (USBD_NORMAL_COMPLETION);
    258 }
    259 
    260 usbd_status
    261 usbd_transfer(usbd_xfer_handle xfer)
    262 {
    263 	usbd_pipe_handle pipe = xfer->pipe;
    264 	usb_dma_t *dmap = &xfer->dmabuf;
    265 	usbd_status err;
    266 	unsigned int size, flags;
    267 	int s;
    268 
    269 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n",
    270 		    xfer, xfer->flags, pipe, pipe->running));
    271 
    272 #ifdef USB_DEBUG
    273 	if (usbdebug > 5)
    274 		usbd_dump_queue(pipe);
    275 #endif
    276 	xfer->done = 0;
    277 
    278 	if (pipe->aborting)
    279 		return (USBD_CANCELLED);
    280 
    281 	size = xfer->length;
    282 	/* If there is no buffer, allocate one. */
    283 	if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
    284 		struct usbd_bus *bus = pipe->device->bus;
    285 
    286 #ifdef DIAGNOSTIC
    287 		if (xfer->rqflags & URQ_AUTO_DMABUF)
    288 			printf("usbd_transfer: has old buffer!\n");
    289 #endif
    290 		err = bus->methods->allocm(bus, dmap, size);
    291 		if (err)
    292 			return (err);
    293 		xfer->rqflags |= URQ_AUTO_DMABUF;
    294 	}
    295 
    296 	flags = xfer->flags;
    297 
    298 	/* Copy data if going out. */
    299 	if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer))
    300 		memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
    301 
    302 	/* xfer is not valid after the transfer method unless synchronous */
    303 	err = pipe->methods->transfer(xfer);
    304 
    305 	if (err != USBD_IN_PROGRESS && err) {
    306 		/* The transfer has not been queued, so free buffer. */
    307 		if (xfer->rqflags & URQ_AUTO_DMABUF) {
    308 			struct usbd_bus *bus = pipe->device->bus;
    309 
    310 			bus->methods->freem(bus, &xfer->dmabuf);
    311 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    312 		}
    313 	}
    314 
    315 	if (!(flags & USBD_SYNCHRONOUS))
    316 		return (err);
    317 
    318 	/* Sync transfer, wait for completion. */
    319 	if (err != USBD_IN_PROGRESS)
    320 		return (err);
    321 	usbd_lock_pipe(pipe);
    322 	if (!xfer->done) {
    323 		if (pipe->device->bus->use_polling)
    324 			panic("usbd_transfer: not done");
    325 
    326 		if (pipe->device->bus->lock)
    327 			cv_wait(&xfer->cv, pipe->device->bus->lock);
    328 		else
    329 			tsleep(xfer, PRIBIO, "usbsyn", 0);
    330 	}
    331 	usbd_unlock_pipe(pipe);
    332 	return (xfer->status);
    333 }
    334 
    335 /* Like usbd_transfer(), but waits for completion. */
    336 usbd_status
    337 usbd_sync_transfer(usbd_xfer_handle xfer)
    338 {
    339 	xfer->flags |= USBD_SYNCHRONOUS;
    340 	return (usbd_transfer(xfer));
    341 }
    342 
    343 void *
    344 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
    345 {
    346 	struct usbd_bus *bus = xfer->device->bus;
    347 	usbd_status err;
    348 
    349 #ifdef DIAGNOSTIC
    350 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    351 		printf("usbd_alloc_buffer: xfer already has a buffer\n");
    352 #endif
    353 	err = bus->methods->allocm(bus, &xfer->dmabuf, size);
    354 	if (err)
    355 		return (NULL);
    356 	xfer->rqflags |= URQ_DEV_DMABUF;
    357 	return (KERNADDR(&xfer->dmabuf, 0));
    358 }
    359 
    360 void
    361 usbd_free_buffer(usbd_xfer_handle xfer)
    362 {
    363 #ifdef DIAGNOSTIC
    364 	if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
    365 		printf("usbd_free_buffer: no buffer\n");
    366 		return;
    367 	}
    368 #endif
    369 	xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
    370 	xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
    371 }
    372 
    373 void *
    374 usbd_get_buffer(usbd_xfer_handle xfer)
    375 {
    376 	if (!(xfer->rqflags & URQ_DEV_DMABUF))
    377 		return (0);
    378 	return (KERNADDR(&xfer->dmabuf, 0));
    379 }
    380 
    381 usbd_xfer_handle
    382 usbd_alloc_xfer(usbd_device_handle dev)
    383 {
    384 	usbd_xfer_handle xfer;
    385 
    386 	xfer = dev->bus->methods->allocx(dev->bus);
    387 	if (xfer == NULL)
    388 		return (NULL);
    389 	xfer->device = dev;
    390 	callout_init(&xfer->timeout_handle, 0);
    391 	cv_init(&xfer->cv, "usbxfer");
    392 	cv_init(&xfer->hccv, "usbhcxfer");
    393 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
    394 	return (xfer);
    395 }
    396 
    397 usbd_status
    398 usbd_free_xfer(usbd_xfer_handle xfer)
    399 {
    400 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
    401 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    402 		usbd_free_buffer(xfer);
    403 #if defined(DIAGNOSTIC)
    404 	if (callout_pending(&xfer->timeout_handle)) {
    405 		callout_stop(&xfer->timeout_handle);
    406 		printf("usbd_free_xfer: timout_handle pending");
    407 	}
    408 #endif
    409 	cv_destroy(&xfer->cv);
    410 	cv_destroy(&xfer->hccv);
    411 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
    412 	return (USBD_NORMAL_COMPLETION);
    413 }
    414 
    415 void
    416 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    417 		usbd_private_handle priv, void *buffer, u_int32_t length,
    418 		u_int16_t flags, u_int32_t timeout,
    419 		usbd_callback callback)
    420 {
    421 	xfer->pipe = pipe;
    422 	xfer->priv = priv;
    423 	xfer->buffer = buffer;
    424 	xfer->length = length;
    425 	xfer->actlen = 0;
    426 	xfer->flags = flags;
    427 	xfer->timeout = timeout;
    428 	xfer->status = USBD_NOT_STARTED;
    429 	xfer->callback = callback;
    430 	xfer->rqflags &= ~URQ_REQUEST;
    431 	xfer->nframes = 0;
    432 }
    433 
    434 void
    435 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
    436 			usbd_private_handle priv, u_int32_t timeout,
    437 			usb_device_request_t *req, void *buffer,
    438 			u_int32_t length, u_int16_t flags,
    439 			usbd_callback callback)
    440 {
    441 	xfer->pipe = dev->default_pipe;
    442 	xfer->priv = priv;
    443 	xfer->buffer = buffer;
    444 	xfer->length = length;
    445 	xfer->actlen = 0;
    446 	xfer->flags = flags;
    447 	xfer->timeout = timeout;
    448 	xfer->status = USBD_NOT_STARTED;
    449 	xfer->callback = callback;
    450 	xfer->request = *req;
    451 	xfer->rqflags |= URQ_REQUEST;
    452 	xfer->nframes = 0;
    453 }
    454 
    455 void
    456 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    457 		     usbd_private_handle priv, u_int16_t *frlengths,
    458 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
    459 {
    460 	xfer->pipe = pipe;
    461 	xfer->priv = priv;
    462 	xfer->buffer = 0;
    463 	xfer->length = 0;
    464 	xfer->actlen = 0;
    465 	xfer->flags = flags;
    466 	xfer->timeout = USBD_NO_TIMEOUT;
    467 	xfer->status = USBD_NOT_STARTED;
    468 	xfer->callback = callback;
    469 	xfer->rqflags &= ~URQ_REQUEST;
    470 	xfer->frlengths = frlengths;
    471 	xfer->nframes = nframes;
    472 }
    473 
    474 void
    475 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
    476 		     void **buffer, u_int32_t *count, usbd_status *status)
    477 {
    478 	if (priv != NULL)
    479 		*priv = xfer->priv;
    480 	if (buffer != NULL)
    481 		*buffer = xfer->buffer;
    482 	if (count != NULL)
    483 		*count = xfer->actlen;
    484 	if (status != NULL)
    485 		*status = xfer->status;
    486 }
    487 
    488 usb_config_descriptor_t *
    489 usbd_get_config_descriptor(usbd_device_handle dev)
    490 {
    491 #ifdef DIAGNOSTIC
    492 	if (dev == NULL) {
    493 		printf("usbd_get_config_descriptor: dev == NULL\n");
    494 		return (NULL);
    495 	}
    496 #endif
    497 	return (dev->cdesc);
    498 }
    499 
    500 usb_interface_descriptor_t *
    501 usbd_get_interface_descriptor(usbd_interface_handle iface)
    502 {
    503 #ifdef DIAGNOSTIC
    504 	if (iface == NULL) {
    505 		printf("usbd_get_interface_descriptor: dev == NULL\n");
    506 		return (NULL);
    507 	}
    508 #endif
    509 	return (iface->idesc);
    510 }
    511 
    512 usb_device_descriptor_t *
    513 usbd_get_device_descriptor(usbd_device_handle dev)
    514 {
    515 	return (&dev->ddesc);
    516 }
    517 
    518 usb_endpoint_descriptor_t *
    519 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
    520 {
    521 	if (index >= iface->idesc->bNumEndpoints)
    522 		return (0);
    523 	return (iface->endpoints[index].edesc);
    524 }
    525 
    526 /* Some drivers may wish to abort requests on the default pipe, *
    527  * but there is no mechanism for getting a handle on it.        */
    528 usbd_status
    529 usbd_abort_default_pipe(struct usbd_device *device)
    530 {
    531 
    532 	return usbd_abort_pipe(device->default_pipe);
    533 }
    534 
    535 usbd_status
    536 usbd_abort_pipe(usbd_pipe_handle pipe)
    537 {
    538 	usbd_status err;
    539 	int s;
    540 	usbd_xfer_handle intrxfer = pipe->intrxfer;
    541 
    542 #ifdef DIAGNOSTIC
    543 	if (pipe == NULL) {
    544 		printf("usbd_abort_pipe: pipe==NULL\n");
    545 		return (USBD_NORMAL_COMPLETION);
    546 	}
    547 #endif
    548 	usbd_lock_pipe(pipe);
    549 	err = usbd_ar_pipe(pipe);
    550 	usbd_unlock_pipe(pipe);
    551 	if (pipe->intrxfer != intrxfer)
    552 		usbd_free_xfer(intrxfer);
    553 	return (err);
    554 }
    555 
    556 usbd_status
    557 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
    558 {
    559 	usbd_device_handle dev = pipe->device;
    560 	usb_device_request_t req;
    561 	usbd_status err;
    562 
    563 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    564 
    565 	/*
    566 	 * Clearing en endpoint stall resets the endpoint toggle, so
    567 	 * do the same to the HC toggle.
    568 	 */
    569 	pipe->methods->cleartoggle(pipe);
    570 
    571 	req.bmRequestType = UT_WRITE_ENDPOINT;
    572 	req.bRequest = UR_CLEAR_FEATURE;
    573 	USETW(req.wValue, UF_ENDPOINT_HALT);
    574 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    575 	USETW(req.wLength, 0);
    576 	err = usbd_do_request(dev, &req, 0);
    577 #if 0
    578 XXX should we do this?
    579 	if (!err) {
    580 		pipe->state = USBD_PIPE_ACTIVE;
    581 		/* XXX activate pipe */
    582 	}
    583 #endif
    584 	return (err);
    585 }
    586 
    587 usbd_status
    588 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
    589 {
    590 	usbd_device_handle dev = pipe->device;
    591 	usb_device_request_t req;
    592 	usbd_status err;
    593 
    594 	pipe->methods->cleartoggle(pipe);
    595 
    596 	req.bmRequestType = UT_WRITE_ENDPOINT;
    597 	req.bRequest = UR_CLEAR_FEATURE;
    598 	USETW(req.wValue, UF_ENDPOINT_HALT);
    599 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    600 	USETW(req.wLength, 0);
    601 	err = usbd_do_request_async(dev, &req, 0);
    602 	return (err);
    603 }
    604 
    605 void
    606 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
    607 {
    608 	pipe->methods->cleartoggle(pipe);
    609 }
    610 
    611 usbd_status
    612 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
    613 {
    614 #ifdef DIAGNOSTIC
    615 	if (iface == NULL || iface->idesc == NULL) {
    616 		printf("usbd_endpoint_count: NULL pointer\n");
    617 		return (USBD_INVAL);
    618 	}
    619 #endif
    620 	*count = iface->idesc->bNumEndpoints;
    621 	return (USBD_NORMAL_COMPLETION);
    622 }
    623 
    624 usbd_status
    625 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
    626 {
    627 	if (dev->cdesc == NULL)
    628 		return (USBD_NOT_CONFIGURED);
    629 	*count = dev->cdesc->bNumInterface;
    630 	return (USBD_NORMAL_COMPLETION);
    631 }
    632 
    633 void
    634 usbd_interface2device_handle(usbd_interface_handle iface,
    635 			     usbd_device_handle *dev)
    636 {
    637 	*dev = iface->device;
    638 }
    639 
    640 usbd_status
    641 usbd_device2interface_handle(usbd_device_handle dev,
    642 			     u_int8_t ifaceno, usbd_interface_handle *iface)
    643 {
    644 	if (dev->cdesc == NULL)
    645 		return (USBD_NOT_CONFIGURED);
    646 	if (ifaceno >= dev->cdesc->bNumInterface)
    647 		return (USBD_INVAL);
    648 	*iface = &dev->ifaces[ifaceno];
    649 	return (USBD_NORMAL_COMPLETION);
    650 }
    651 
    652 usbd_device_handle
    653 usbd_pipe2device_handle(usbd_pipe_handle pipe)
    654 {
    655 	return (pipe->device);
    656 }
    657 
    658 /* XXXX use altno */
    659 usbd_status
    660 usbd_set_interface(usbd_interface_handle iface, int altidx)
    661 {
    662 	usb_device_request_t req;
    663 	usbd_status err;
    664 	void *endpoints;
    665 
    666 	if (LIST_FIRST(&iface->pipes) != 0)
    667 		return (USBD_IN_USE);
    668 
    669 	endpoints = iface->endpoints;
    670 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
    671 	if (err)
    672 		return (err);
    673 
    674 	/* new setting works, we can free old endpoints */
    675 	if (endpoints != NULL)
    676 		free(endpoints, M_USB);
    677 
    678 #ifdef DIAGNOSTIC
    679 	if (iface->idesc == NULL) {
    680 		printf("usbd_set_interface: NULL pointer\n");
    681 		return (USBD_INVAL);
    682 	}
    683 #endif
    684 
    685 	req.bmRequestType = UT_WRITE_INTERFACE;
    686 	req.bRequest = UR_SET_INTERFACE;
    687 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    688 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    689 	USETW(req.wLength, 0);
    690 	return (usbd_do_request(iface->device, &req, 0));
    691 }
    692 
    693 int
    694 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    695 {
    696 	char *p = (char *)cdesc;
    697 	char *end = p + UGETW(cdesc->wTotalLength);
    698 	usb_interface_descriptor_t *d;
    699 	int n;
    700 
    701 	for (n = 0; p < end; p += d->bLength) {
    702 		d = (usb_interface_descriptor_t *)p;
    703 		if (p + d->bLength <= end &&
    704 		    d->bDescriptorType == UDESC_INTERFACE &&
    705 		    d->bInterfaceNumber == ifaceno)
    706 			n++;
    707 	}
    708 	return (n);
    709 }
    710 
    711 int
    712 usbd_get_interface_altindex(usbd_interface_handle iface)
    713 {
    714 	return (iface->altindex);
    715 }
    716 
    717 usbd_status
    718 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
    719 {
    720 	usb_device_request_t req;
    721 
    722 	req.bmRequestType = UT_READ_INTERFACE;
    723 	req.bRequest = UR_GET_INTERFACE;
    724 	USETW(req.wValue, 0);
    725 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    726 	USETW(req.wLength, 1);
    727 	return (usbd_do_request(iface->device, &req, aiface));
    728 }
    729 
    730 /*** Internal routines ***/
    731 
    732 /* Dequeue all pipe operations, called at splusb(). */
    733 Static usbd_status
    734 usbd_ar_pipe(usbd_pipe_handle pipe)
    735 {
    736 	usbd_xfer_handle xfer;
    737 
    738 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    739 
    740 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    741 #ifdef USB_DEBUG
    742 	if (usbdebug > 5)
    743 		usbd_dump_queue(pipe);
    744 #endif
    745 	pipe->repeat = 0;
    746 	pipe->aborting = 1;
    747 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
    748 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
    749 			    pipe, xfer, pipe->methods));
    750 		/* Make the HC abort it (and invoke the callback). */
    751 		if (pipe->device->bus->lock)
    752 			mutex_exit(pipe->device->bus->lock);
    753 		pipe->methods->abort(xfer);
    754 		if (pipe->device->bus->lock)
    755 			mutex_enter(pipe->device->bus->lock);
    756 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    757 	}
    758 	pipe->aborting = 0;
    759 	return (USBD_NORMAL_COMPLETION);
    760 }
    761 
    762 /* Called at splusb() */
    763 void
    764 usb_transfer_complete(usbd_xfer_handle xfer)
    765 {
    766 	usbd_pipe_handle pipe = xfer->pipe;
    767 	usb_dma_t *dmap = &xfer->dmabuf;
    768 	int sync = xfer->flags & USBD_SYNCHRONOUS;
    769 	int erred = xfer->status == USBD_CANCELLED ||
    770 	    xfer->status == USBD_TIMEOUT;
    771 	int repeat, polling;
    772 
    773 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    774 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    775 
    776 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    777 
    778 #ifdef DIAGNOSTIC
    779 	if (xfer->busy_free != XFER_ONQU) {
    780 		printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
    781 		       xfer, xfer->busy_free);
    782 	}
    783 #endif
    784 
    785 #ifdef DIAGNOSTIC
    786 	if (pipe == NULL) {
    787 		printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
    788 		return;
    789 	}
    790 #endif
    791 	repeat = pipe->repeat;
    792 	polling = pipe->device->bus->use_polling;
    793 	/* XXXX */
    794 	if (polling)
    795 		pipe->running = 0;
    796 
    797 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    798 	    usbd_xfer_isread(xfer)) {
    799 #ifdef DIAGNOSTIC
    800 		if (xfer->actlen > xfer->length) {
    801 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    802 			       xfer->actlen, xfer->length);
    803 			xfer->actlen = xfer->length;
    804 		}
    805 #endif
    806 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
    807 	}
    808 
    809 	/* if we allocated the buffer in usbd_transfer() we free it here. */
    810 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
    811 		if (!repeat) {
    812 			struct usbd_bus *bus = pipe->device->bus;
    813 			bus->methods->freem(bus, dmap);
    814 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    815 		}
    816 	}
    817 
    818 	if (!repeat) {
    819 		/* Remove request from queue. */
    820 #ifdef DIAGNOSTIC
    821 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
    822 			printf("usb_transfer_complete: bad dequeue %p != %p\n",
    823 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
    824 		xfer->busy_free = XFER_BUSY;
    825 #endif
    826 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
    827 	}
    828 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
    829 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
    830 
    831 	/* Count completed transfers. */
    832 	++pipe->device->bus->stats.uds_requests
    833 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    834 
    835 	xfer->done = 1;
    836 	if (!xfer->status && xfer->actlen < xfer->length &&
    837 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
    838 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
    839 			     xfer->actlen, xfer->length));
    840 		xfer->status = USBD_SHORT_XFER;
    841 	}
    842 
    843 	if (repeat) {
    844 		if (xfer->callback) {
    845 			if (pipe->device->bus->lock)
    846 				mutex_exit(pipe->device->bus->lock);
    847 			xfer->callback(xfer, xfer->priv, xfer->status);
    848 			if (pipe->device->bus->lock)
    849 				mutex_enter(pipe->device->bus->lock);
    850 		}
    851 		pipe->methods->done(xfer);
    852 	} else {
    853 		pipe->methods->done(xfer);
    854 		if (xfer->callback) {
    855 			if (pipe->device->bus->lock)
    856 				mutex_exit(pipe->device->bus->lock);
    857 			xfer->callback(xfer, xfer->priv, xfer->status);
    858 			if (pipe->device->bus->lock)
    859 				mutex_enter(pipe->device->bus->lock);
    860 		}
    861 	}
    862 
    863 	if (sync && !polling) {
    864 		if (pipe->device->bus->lock)
    865 			cv_broadcast(&xfer->cv);
    866 		else
    867 			wakeup(xfer);
    868 	}
    869 
    870 	if (!repeat) {
    871 		/* XXX should we stop the queue on all errors? */
    872 		if (erred && pipe->iface != NULL)	/* not control pipe */
    873 			pipe->running = 0;
    874 		else
    875 			usbd_start_next(pipe);
    876 	}
    877 }
    878 
    879 usbd_status
    880 usb_insert_transfer(usbd_xfer_handle xfer)
    881 {
    882 	usbd_pipe_handle pipe = xfer->pipe;
    883 	usbd_status err;
    884 
    885 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    886 		    pipe, pipe->running, xfer->timeout));
    887 
    888 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    889 
    890 #ifdef DIAGNOSTIC
    891 	if (xfer->busy_free != XFER_BUSY) {
    892 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
    893 		       xfer, xfer->busy_free);
    894 		return (USBD_INVAL);
    895 	}
    896 	xfer->busy_free = XFER_ONQU;
    897 #endif
    898 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    899 	if (pipe->running)
    900 		err = USBD_IN_PROGRESS;
    901 	else {
    902 		pipe->running = 1;
    903 		err = USBD_NORMAL_COMPLETION;
    904 	}
    905 	return (err);
    906 }
    907 
    908 /* Called at splusb() */
    909 void
    910 usbd_start_next(usbd_pipe_handle pipe)
    911 {
    912 	usbd_xfer_handle xfer;
    913 	usbd_status err;
    914 
    915 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    916 
    917 #ifdef DIAGNOSTIC
    918 	if (pipe == NULL) {
    919 		printf("usbd_start_next: pipe == NULL\n");
    920 		return;
    921 	}
    922 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    923 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    924 		return;
    925 	}
    926 #endif
    927 
    928 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    929 
    930 	/* Get next request in queue. */
    931 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    932 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    933 	if (xfer == NULL) {
    934 		pipe->running = 0;
    935 	} else {
    936 		if (pipe->device->bus->lock)
    937 			mutex_exit(pipe->device->bus->lock);
    938 		err = pipe->methods->start(xfer);
    939 		if (pipe->device->bus->lock)
    940 			mutex_enter(pipe->device->bus->lock);
    941 		if (err != USBD_IN_PROGRESS) {
    942 			printf("usbd_start_next: error=%d\n", err);
    943 			pipe->running = 0;
    944 			/* XXX do what? */
    945 		}
    946 	}
    947 }
    948 
    949 usbd_status
    950 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
    951 {
    952 	return (usbd_do_request_flags(dev, req, data, 0, 0,
    953 				      USBD_DEFAULT_TIMEOUT));
    954 }
    955 
    956 usbd_status
    957 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
    958 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
    959 {
    960 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
    961 					   data, flags, actlen, timo));
    962 }
    963 
    964 usbd_status
    965 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
    966 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
    967 	u_int32_t timeout)
    968 {
    969 	usbd_xfer_handle xfer;
    970 	usbd_status err;
    971 
    972 #ifdef DIAGNOSTIC
    973 	if (dev->bus->intr_context) {
    974 		printf("usbd_do_request: not in process context\n");
    975 		return (USBD_INVAL);
    976 	}
    977 #endif
    978 
    979 	xfer = usbd_alloc_xfer(dev);
    980 	if (xfer == NULL)
    981 		return (USBD_NOMEM);
    982 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
    983 				data, UGETW(req->wLength), flags, 0);
    984 	xfer->pipe = pipe;
    985 	err = usbd_sync_transfer(xfer);
    986 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    987 	if (xfer->actlen > xfer->length) {
    988 		DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
    989 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    990 			 __func__, dev->address, xfer->request.bmRequestType,
    991 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    992 			 UGETW(xfer->request.wIndex),
    993 			 UGETW(xfer->request.wLength),
    994 			 xfer->length, xfer->actlen));
    995 	}
    996 #endif
    997 	if (actlen != NULL)
    998 		*actlen = xfer->actlen;
    999 	if (err == USBD_STALLED) {
   1000 		/*
   1001 		 * The control endpoint has stalled.  Control endpoints
   1002 		 * should not halt, but some may do so anyway so clear
   1003 		 * any halt condition.
   1004 		 */
   1005 		usb_device_request_t treq;
   1006 		usb_status_t status;
   1007 		u_int16_t s;
   1008 		usbd_status nerr;
   1009 
   1010 		treq.bmRequestType = UT_READ_ENDPOINT;
   1011 		treq.bRequest = UR_GET_STATUS;
   1012 		USETW(treq.wValue, 0);
   1013 		USETW(treq.wIndex, 0);
   1014 		USETW(treq.wLength, sizeof(usb_status_t));
   1015 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1016 					   &treq, &status,sizeof(usb_status_t),
   1017 					   0, 0);
   1018 		nerr = usbd_sync_transfer(xfer);
   1019 		if (nerr)
   1020 			goto bad;
   1021 		s = UGETW(status.wStatus);
   1022 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
   1023 		if (!(s & UES_HALT))
   1024 			goto bad;
   1025 		treq.bmRequestType = UT_WRITE_ENDPOINT;
   1026 		treq.bRequest = UR_CLEAR_FEATURE;
   1027 		USETW(treq.wValue, UF_ENDPOINT_HALT);
   1028 		USETW(treq.wIndex, 0);
   1029 		USETW(treq.wLength, 0);
   1030 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1031 					   &treq, &status, 0, 0, 0);
   1032 		nerr = usbd_sync_transfer(xfer);
   1033 		if (nerr)
   1034 			goto bad;
   1035 	}
   1036 
   1037  bad:
   1038 	if (err) {
   1039 		DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
   1040 	}
   1041 	usbd_free_xfer(xfer);
   1042 	return (err);
   1043 }
   1044 
   1045 void
   1046 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1047     usbd_private_handle priv, usbd_status status)
   1048 {
   1049 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1050 	if (xfer->actlen > xfer->length) {
   1051 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1052 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1053 			 xfer->pipe->device->address,
   1054 			 xfer->request.bmRequestType,
   1055 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1056 			 UGETW(xfer->request.wIndex),
   1057 			 UGETW(xfer->request.wLength),
   1058 			 xfer->length, xfer->actlen));
   1059 	}
   1060 #endif
   1061 	usbd_free_xfer(xfer);
   1062 }
   1063 
   1064 /*
   1065  * Execute a request without waiting for completion.
   1066  * Can be used from interrupt context.
   1067  */
   1068 usbd_status
   1069 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1070 		      void *data)
   1071 {
   1072 	usbd_xfer_handle xfer;
   1073 	usbd_status err;
   1074 
   1075 	xfer = usbd_alloc_xfer(dev);
   1076 	if (xfer == NULL)
   1077 		return (USBD_NOMEM);
   1078 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1079 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1080 	err = usbd_transfer(xfer);
   1081 	if (err != USBD_IN_PROGRESS) {
   1082 		usbd_free_xfer(xfer);
   1083 		return (err);
   1084 	}
   1085 	return (USBD_NORMAL_COMPLETION);
   1086 }
   1087 
   1088 const struct usbd_quirks *
   1089 usbd_get_quirks(usbd_device_handle dev)
   1090 {
   1091 #ifdef DIAGNOSTIC
   1092 	if (dev == NULL) {
   1093 		printf("usbd_get_quirks: dev == NULL\n");
   1094 		return 0;
   1095 	}
   1096 #endif
   1097 	return (dev->quirks);
   1098 }
   1099 
   1100 /* XXX do periodic free() of free list */
   1101 
   1102 /*
   1103  * Called from keyboard driver when in polling mode.
   1104  */
   1105 void
   1106 usbd_dopoll(usbd_interface_handle iface)
   1107 {
   1108 	iface->device->bus->methods->do_poll(iface->device->bus);
   1109 }
   1110 
   1111 void
   1112 usbd_set_polling(usbd_device_handle dev, int on)
   1113 {
   1114 	if (on)
   1115 		dev->bus->use_polling++;
   1116 	else
   1117 		dev->bus->use_polling--;
   1118 
   1119 	/* Kick the host controller when switching modes */
   1120 	dev->bus->methods->soft_intr(dev->bus);
   1121 }
   1122 
   1123 
   1124 usb_endpoint_descriptor_t *
   1125 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1126 {
   1127 	struct usbd_endpoint *ep;
   1128 	int i;
   1129 
   1130 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1131 		ep = &iface->endpoints[i];
   1132 		if (ep->edesc->bEndpointAddress == address)
   1133 			return (iface->endpoints[i].edesc);
   1134 	}
   1135 	return (0);
   1136 }
   1137 
   1138 /*
   1139  * usbd_ratecheck() can limit the number of error messages that occurs.
   1140  * When a device is unplugged it may take up to 0.25s for the hub driver
   1141  * to notice it.  If the driver continuosly tries to do I/O operations
   1142  * this can generate a large number of messages.
   1143  */
   1144 int
   1145 usbd_ratecheck(struct timeval *last)
   1146 {
   1147 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1148 
   1149 	return (ratecheck(last, &errinterval));
   1150 }
   1151 
   1152 /*
   1153  * Search for a vendor/product pair in an array.  The item size is
   1154  * given as an argument.
   1155  */
   1156 const struct usb_devno *
   1157 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1158 		 u_int16_t vendor, u_int16_t product)
   1159 {
   1160 	while (nentries-- > 0) {
   1161 		u_int16_t tproduct = tbl->ud_product;
   1162 		if (tbl->ud_vendor == vendor &&
   1163 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1164 			return (tbl);
   1165 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1166 	}
   1167 	return (NULL);
   1168 }
   1169 
   1170 
   1171 void
   1172 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1173 {
   1174 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1175 
   1176         iter->cur = (const uByte *)cd;
   1177         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1178 }
   1179 
   1180 const usb_descriptor_t *
   1181 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1182 {
   1183 	const usb_descriptor_t *desc;
   1184 
   1185 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1186 		if (iter->cur != iter->end)
   1187 			printf("usb_desc_iter_next: bad descriptor\n");
   1188 		return NULL;
   1189 	}
   1190 	desc = (const usb_descriptor_t *)iter->cur;
   1191 	if (desc->bLength == 0) {
   1192 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1193 		return NULL;
   1194 	}
   1195 	iter->cur += desc->bLength;
   1196 	if (iter->cur > iter->end) {
   1197 		printf("usb_desc_iter_next: descriptor length too large\n");
   1198 		return NULL;
   1199 	}
   1200 	return desc;
   1201 }
   1202 
   1203 usbd_status
   1204 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1205 {
   1206 	return usbd_get_string0(dev, si, buf, 1);
   1207 }
   1208 
   1209 usbd_status
   1210 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1211 {
   1212 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1213 	usb_string_descriptor_t us;
   1214 	char *s;
   1215 	int i, n;
   1216 	u_int16_t c;
   1217 	usbd_status err;
   1218 	int size;
   1219 
   1220 	buf[0] = '\0';
   1221 	if (si == 0)
   1222 		return (USBD_INVAL);
   1223 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1224 		return (USBD_STALLED);
   1225 	if (dev->langid == USBD_NOLANG) {
   1226 		/* Set up default language */
   1227 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1228 		    &size);
   1229 		if (err || size < 4) {
   1230 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1231 			dev->langid = 0; /* Well, just pick something then */
   1232 		} else {
   1233 			/* Pick the first language as the default. */
   1234 			dev->langid = UGETW(us.bString[0]);
   1235 		}
   1236 	}
   1237 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1238 	if (err)
   1239 		return (err);
   1240 	s = buf;
   1241 	n = size / 2 - 1;
   1242 	if (unicode) {
   1243 		for (i = 0; i < n; i++) {
   1244 			c = UGETW(us.bString[i]);
   1245 			if (swap)
   1246 				c = (c >> 8) | (c << 8);
   1247 			s += wput_utf8(s, 3, c);
   1248 		}
   1249 		*s++ = 0;
   1250 	}
   1251 #ifdef COMPAT_30
   1252 	else {
   1253 		for (i = 0; i < n; i++) {
   1254 			c = UGETW(us.bString[i]);
   1255 			if (swap)
   1256 				c = (c >> 8) | (c << 8);
   1257 			*s++ = (c < 0x80) ? c : '?';
   1258 		}
   1259 		*s++ = 0;
   1260 	}
   1261 #endif
   1262 	return (USBD_NORMAL_COMPLETION);
   1263 }
   1264