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