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