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