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