Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.139.2.2
      1 /*	$NetBSD: usbdi.c,v 1.139.2.2 2013/06/23 06:20:22 tls 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.139.2.2 2013/06/23 06:20:22 tls 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) || defined(OHCI_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 polling = pipe->device->bus->use_polling;
    794 	int repeat;
    795 
    796 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    797 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    798 
    799 	KASSERT(polling || pipe->device->bus->lock == NULL ||
    800 	    mutex_owned(pipe->device->bus->lock));
    801 
    802 #ifdef DIAGNOSTIC
    803 	if (xfer->busy_free != XFER_ONQU) {
    804 		printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n",
    805 		       xfer, xfer->busy_free);
    806 	}
    807 #endif
    808 
    809 #ifdef DIAGNOSTIC
    810 	if (pipe == NULL) {
    811 		printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
    812 		return;
    813 	}
    814 #endif
    815 	repeat = pipe->repeat;
    816 	/* XXXX */
    817 	if (polling)
    818 		pipe->running = 0;
    819 
    820 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    821 	    usbd_xfer_isread(xfer)) {
    822 #ifdef DIAGNOSTIC
    823 		if (xfer->actlen > xfer->length) {
    824 			printf("%s: actlen (%d) > len (%d)\n", __func__,
    825 			       xfer->actlen, xfer->length);
    826 			xfer->actlen = xfer->length;
    827 		}
    828 #endif
    829 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
    830 	}
    831 
    832 	/* if we allocated the buffer in usbd_transfer() we free it here. */
    833 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
    834 		if (!repeat) {
    835 			struct usbd_bus *bus = pipe->device->bus;
    836 			bus->methods->freem(bus, dmap);
    837 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    838 		}
    839 	}
    840 
    841 	if (!repeat) {
    842 		/* Remove request from queue. */
    843 
    844 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->queue),
    845 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
    846 		     xfer);
    847 #ifdef DIAGNOSTIC
    848 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
    849 			printf("%s: bad dequeue %p != %p\n", __func__,
    850 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
    851 		xfer->busy_free = XFER_BUSY;
    852 #endif
    853 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
    854 	}
    855 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
    856 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
    857 
    858 	/* Count completed transfers. */
    859 	++pipe->device->bus->stats.uds_requests
    860 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    861 
    862 	xfer->done = 1;
    863 	if (!xfer->status && xfer->actlen < xfer->length &&
    864 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
    865 		DPRINTFN(-1,("usb_transfer_complete: short transfer %d<%d\n",
    866 			     xfer->actlen, xfer->length));
    867 		xfer->status = USBD_SHORT_XFER;
    868 	}
    869 
    870 	if (repeat) {
    871 		if (xfer->callback) {
    872 			if (pipe->device->bus->lock && !polling)
    873 				mutex_exit(pipe->device->bus->lock);
    874 
    875 			if (!(pipe->flags & USBD_MPSAFE))
    876 				KERNEL_LOCK(1, curlwp);
    877 			xfer->callback(xfer, xfer->priv, xfer->status);
    878 			if (!(pipe->flags & USBD_MPSAFE))
    879 				KERNEL_UNLOCK_ONE(curlwp);
    880 
    881 			if (pipe->device->bus->lock && !polling)
    882 				mutex_enter(pipe->device->bus->lock);
    883 		}
    884 		pipe->methods->done(xfer);
    885 	} else {
    886 		pipe->methods->done(xfer);
    887 		if (xfer->callback) {
    888 			if (pipe->device->bus->lock && !polling)
    889 				mutex_exit(pipe->device->bus->lock);
    890 
    891 			if (!(pipe->flags & USBD_MPSAFE))
    892 				KERNEL_LOCK(1, curlwp);
    893 			xfer->callback(xfer, xfer->priv, xfer->status);
    894 			if (!(pipe->flags & USBD_MPSAFE))
    895 				KERNEL_UNLOCK_ONE(curlwp);
    896 
    897 			if (pipe->device->bus->lock && !polling)
    898 				mutex_enter(pipe->device->bus->lock);
    899 		}
    900 	}
    901 
    902 	if (sync && !polling) {
    903 		if (pipe->device->bus->lock)
    904 			cv_broadcast(&xfer->cv);
    905 		else
    906 			wakeup(xfer);	/* XXXSMP ok */
    907 	}
    908 
    909 	if (!repeat) {
    910 		/* XXX should we stop the queue on all errors? */
    911 		if (erred && pipe->iface != NULL)	/* not control pipe */
    912 			pipe->running = 0;
    913 		else
    914 			usbd_start_next(pipe);
    915 	}
    916 }
    917 
    918 /* Called with USB lock held. */
    919 usbd_status
    920 usb_insert_transfer(usbd_xfer_handle xfer)
    921 {
    922 	usbd_pipe_handle pipe = xfer->pipe;
    923 	usbd_status err;
    924 
    925 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    926 		    pipe, pipe->running, xfer->timeout));
    927 
    928 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    929 
    930 #ifdef DIAGNOSTIC
    931 	if (xfer->busy_free != XFER_BUSY) {
    932 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
    933 		       xfer, xfer->busy_free);
    934 		return (USBD_INVAL);
    935 	}
    936 	xfer->busy_free = XFER_ONQU;
    937 #endif
    938 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    939 	if (pipe->running)
    940 		err = USBD_IN_PROGRESS;
    941 	else {
    942 		pipe->running = 1;
    943 		err = USBD_NORMAL_COMPLETION;
    944 	}
    945 	return (err);
    946 }
    947 
    948 /* Called with USB lock held. */
    949 void
    950 usbd_start_next(usbd_pipe_handle pipe)
    951 {
    952 	usbd_xfer_handle xfer;
    953 	usbd_status err;
    954 
    955 #ifdef DIAGNOSTIC
    956 	if (pipe == NULL) {
    957 		printf("usbd_start_next: pipe == NULL\n");
    958 		return;
    959 	}
    960 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    961 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    962 		return;
    963 	}
    964 #endif
    965 
    966 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    967 
    968 	/* Get next request in queue. */
    969 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    970 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    971 	if (xfer == NULL) {
    972 		pipe->running = 0;
    973 	} else {
    974 		if (pipe->device->bus->lock)
    975 			mutex_exit(pipe->device->bus->lock);
    976 		err = pipe->methods->start(xfer);
    977 		if (pipe->device->bus->lock)
    978 			mutex_enter(pipe->device->bus->lock);
    979 		if (err != USBD_IN_PROGRESS) {
    980 			printf("usbd_start_next: error=%d\n", err);
    981 			pipe->running = 0;
    982 			/* XXX do what? */
    983 		}
    984 	}
    985 
    986 	KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock));
    987 }
    988 
    989 usbd_status
    990 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
    991 {
    992 	return (usbd_do_request_flags(dev, req, data, 0, 0,
    993 				      USBD_DEFAULT_TIMEOUT));
    994 }
    995 
    996 usbd_status
    997 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
    998 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
    999 {
   1000 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
   1001 					   data, flags, actlen, timo));
   1002 }
   1003 
   1004 usbd_status
   1005 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
   1006 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
   1007 	u_int32_t timeout)
   1008 {
   1009 	usbd_xfer_handle xfer;
   1010 	usbd_status err;
   1011 
   1012 #ifdef DIAGNOSTIC
   1013 	if (cpu_intr_p() || cpu_softintr_p()) {
   1014 		printf("usbd_do_request: not in process context\n");
   1015 		return (USBD_INVAL);
   1016 	}
   1017 #endif
   1018 
   1019 	xfer = usbd_alloc_xfer(dev);
   1020 	if (xfer == NULL)
   1021 		return (USBD_NOMEM);
   1022 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
   1023 				data, UGETW(req->wLength), flags, 0);
   1024 	xfer->pipe = pipe;
   1025 	err = usbd_sync_transfer(xfer);
   1026 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1027 	if (xfer->actlen > xfer->length) {
   1028 		DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
   1029 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1030 			 __func__, dev->address, xfer->request.bmRequestType,
   1031 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1032 			 UGETW(xfer->request.wIndex),
   1033 			 UGETW(xfer->request.wLength),
   1034 			 xfer->length, xfer->actlen));
   1035 	}
   1036 #endif
   1037 	if (actlen != NULL)
   1038 		*actlen = xfer->actlen;
   1039 	if (err == USBD_STALLED) {
   1040 		/*
   1041 		 * The control endpoint has stalled.  Control endpoints
   1042 		 * should not halt, but some may do so anyway so clear
   1043 		 * any halt condition.
   1044 		 */
   1045 		usb_device_request_t treq;
   1046 		usb_status_t status;
   1047 		u_int16_t s;
   1048 		usbd_status nerr;
   1049 
   1050 		treq.bmRequestType = UT_READ_ENDPOINT;
   1051 		treq.bRequest = UR_GET_STATUS;
   1052 		USETW(treq.wValue, 0);
   1053 		USETW(treq.wIndex, 0);
   1054 		USETW(treq.wLength, sizeof(usb_status_t));
   1055 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1056 					   &treq, &status,sizeof(usb_status_t),
   1057 					   0, 0);
   1058 		nerr = usbd_sync_transfer(xfer);
   1059 		if (nerr)
   1060 			goto bad;
   1061 		s = UGETW(status.wStatus);
   1062 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
   1063 		if (!(s & UES_HALT))
   1064 			goto bad;
   1065 		treq.bmRequestType = UT_WRITE_ENDPOINT;
   1066 		treq.bRequest = UR_CLEAR_FEATURE;
   1067 		USETW(treq.wValue, UF_ENDPOINT_HALT);
   1068 		USETW(treq.wIndex, 0);
   1069 		USETW(treq.wLength, 0);
   1070 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1071 					   &treq, &status, 0, 0, 0);
   1072 		nerr = usbd_sync_transfer(xfer);
   1073 		if (nerr)
   1074 			goto bad;
   1075 	}
   1076 
   1077  bad:
   1078 	if (err) {
   1079 		DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
   1080 	}
   1081 	usbd_free_xfer(xfer);
   1082 	return (err);
   1083 }
   1084 
   1085 void
   1086 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1087     usbd_private_handle priv, usbd_status status)
   1088 {
   1089 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1090 	if (xfer->actlen > xfer->length) {
   1091 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1092 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1093 			 xfer->pipe->device->address,
   1094 			 xfer->request.bmRequestType,
   1095 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1096 			 UGETW(xfer->request.wIndex),
   1097 			 UGETW(xfer->request.wLength),
   1098 			 xfer->length, xfer->actlen));
   1099 	}
   1100 #endif
   1101 	usbd_free_xfer(xfer);
   1102 }
   1103 
   1104 /*
   1105  * Execute a request without waiting for completion.
   1106  * Can be used from interrupt context.
   1107  */
   1108 usbd_status
   1109 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1110 		      void *data)
   1111 {
   1112 	usbd_xfer_handle xfer;
   1113 	usbd_status err;
   1114 
   1115 	xfer = usbd_alloc_xfer(dev);
   1116 	if (xfer == NULL)
   1117 		return (USBD_NOMEM);
   1118 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1119 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1120 	err = usbd_transfer(xfer);
   1121 	if (err != USBD_IN_PROGRESS) {
   1122 		usbd_free_xfer(xfer);
   1123 		return (err);
   1124 	}
   1125 	return (USBD_NORMAL_COMPLETION);
   1126 }
   1127 
   1128 const struct usbd_quirks *
   1129 usbd_get_quirks(usbd_device_handle dev)
   1130 {
   1131 #ifdef DIAGNOSTIC
   1132 	if (dev == NULL) {
   1133 		printf("usbd_get_quirks: dev == NULL\n");
   1134 		return 0;
   1135 	}
   1136 #endif
   1137 	return (dev->quirks);
   1138 }
   1139 
   1140 /* XXX do periodic free() of free list */
   1141 
   1142 /*
   1143  * Called from keyboard driver when in polling mode.
   1144  */
   1145 void
   1146 usbd_dopoll(usbd_interface_handle iface)
   1147 {
   1148 	iface->device->bus->methods->do_poll(iface->device->bus);
   1149 }
   1150 
   1151 /*
   1152  * XXX use this more???  use_polling it touched manually all over
   1153  */
   1154 void
   1155 usbd_set_polling(usbd_device_handle dev, int on)
   1156 {
   1157 	if (on)
   1158 		dev->bus->use_polling++;
   1159 	else
   1160 		dev->bus->use_polling--;
   1161 
   1162 	/* Kick the host controller when switching modes */
   1163 	if (dev->bus->lock)
   1164 		mutex_enter(dev->bus->lock);
   1165 	(*dev->bus->methods->soft_intr)(dev->bus);
   1166 	if (dev->bus->lock)
   1167 		mutex_exit(dev->bus->lock);
   1168 
   1169 }
   1170 
   1171 
   1172 usb_endpoint_descriptor_t *
   1173 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1174 {
   1175 	struct usbd_endpoint *ep;
   1176 	int i;
   1177 
   1178 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1179 		ep = &iface->endpoints[i];
   1180 		if (ep->edesc->bEndpointAddress == address)
   1181 			return (iface->endpoints[i].edesc);
   1182 	}
   1183 	return (0);
   1184 }
   1185 
   1186 /*
   1187  * usbd_ratecheck() can limit the number of error messages that occurs.
   1188  * When a device is unplugged it may take up to 0.25s for the hub driver
   1189  * to notice it.  If the driver continuosly tries to do I/O operations
   1190  * this can generate a large number of messages.
   1191  */
   1192 int
   1193 usbd_ratecheck(struct timeval *last)
   1194 {
   1195 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1196 
   1197 	return (ratecheck(last, &errinterval));
   1198 }
   1199 
   1200 /*
   1201  * Search for a vendor/product pair in an array.  The item size is
   1202  * given as an argument.
   1203  */
   1204 const struct usb_devno *
   1205 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1206 		 u_int16_t vendor, u_int16_t product)
   1207 {
   1208 	while (nentries-- > 0) {
   1209 		u_int16_t tproduct = tbl->ud_product;
   1210 		if (tbl->ud_vendor == vendor &&
   1211 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1212 			return (tbl);
   1213 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1214 	}
   1215 	return (NULL);
   1216 }
   1217 
   1218 
   1219 void
   1220 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1221 {
   1222 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1223 
   1224         iter->cur = (const uByte *)cd;
   1225         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1226 }
   1227 
   1228 const usb_descriptor_t *
   1229 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1230 {
   1231 	const usb_descriptor_t *desc;
   1232 
   1233 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1234 		if (iter->cur != iter->end)
   1235 			printf("usb_desc_iter_next: bad descriptor\n");
   1236 		return NULL;
   1237 	}
   1238 	desc = (const usb_descriptor_t *)iter->cur;
   1239 	if (desc->bLength == 0) {
   1240 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1241 		return NULL;
   1242 	}
   1243 	iter->cur += desc->bLength;
   1244 	if (iter->cur > iter->end) {
   1245 		printf("usb_desc_iter_next: descriptor length too large\n");
   1246 		return NULL;
   1247 	}
   1248 	return desc;
   1249 }
   1250 
   1251 usbd_status
   1252 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1253 {
   1254 	return usbd_get_string0(dev, si, buf, 1);
   1255 }
   1256 
   1257 usbd_status
   1258 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1259 {
   1260 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1261 	usb_string_descriptor_t us;
   1262 	char *s;
   1263 	int i, n;
   1264 	u_int16_t c;
   1265 	usbd_status err;
   1266 	int size;
   1267 
   1268 	buf[0] = '\0';
   1269 	if (si == 0)
   1270 		return (USBD_INVAL);
   1271 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1272 		return (USBD_STALLED);
   1273 	if (dev->langid == USBD_NOLANG) {
   1274 		/* Set up default language */
   1275 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1276 		    &size);
   1277 		if (err || size < 4) {
   1278 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1279 			dev->langid = 0; /* Well, just pick something then */
   1280 		} else {
   1281 			/* Pick the first language as the default. */
   1282 			dev->langid = UGETW(us.bString[0]);
   1283 		}
   1284 	}
   1285 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1286 	if (err)
   1287 		return (err);
   1288 	s = buf;
   1289 	n = size / 2 - 1;
   1290 	if (unicode) {
   1291 		for (i = 0; i < n; i++) {
   1292 			c = UGETW(us.bString[i]);
   1293 			if (swap)
   1294 				c = (c >> 8) | (c << 8);
   1295 			s += wput_utf8(s, 3, c);
   1296 		}
   1297 		*s++ = 0;
   1298 	}
   1299 #ifdef COMPAT_30
   1300 	else {
   1301 		for (i = 0; i < n; i++) {
   1302 			c = UGETW(us.bString[i]);
   1303 			if (swap)
   1304 				c = (c >> 8) | (c << 8);
   1305 			*s++ = (c < 0x80) ? c : '?';
   1306 		}
   1307 		*s++ = 0;
   1308 	}
   1309 #endif
   1310 	return (USBD_NORMAL_COMPLETION);
   1311 }
   1312