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