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