Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.191
      1 /*	$NetBSD: usbdi.c,v 1.191 2020/02/12 16:00:17 riastradh 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.191 2020/02/12 16:00:17 riastradh 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();
     80 	USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
     81 
     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();
     95 	USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
     96 
     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();
    112 	USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
    113 
    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();
    129 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    130 
    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();
    141 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    142 
    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();
    175 	USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
    176 	    (uintptr_t)iface, address, flags, 0);
    177 
    178 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
    179 		ep = &iface->ui_endpoints[i];
    180 		if (ep->ue_edesc == NULL)
    181 			return USBD_IOERROR;
    182 		if (ep->ue_edesc->bEndpointAddress == address)
    183 			goto found;
    184 	}
    185 	return USBD_BAD_ADDRESS;
    186  found:
    187 	if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
    188 		return USBD_IN_USE;
    189 	err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
    190 	if (err)
    191 		return err;
    192 	LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
    193 	*pipe = p;
    194 	return USBD_NORMAL_COMPLETION;
    195 }
    196 
    197 usbd_status
    198 usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
    199 		    uint8_t flags, struct usbd_pipe **pipe,
    200 		    void *priv, void *buffer, uint32_t len,
    201 		    usbd_callback cb, int ival)
    202 {
    203 	usbd_status err;
    204 	struct usbd_xfer *xfer;
    205 	struct usbd_pipe *ipipe;
    206 
    207 	USBHIST_FUNC();
    208 	USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
    209 	    address, flags, len, 0);
    210 
    211 	err = usbd_open_pipe_ival(iface, address,
    212 				  USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
    213 				  &ipipe, ival);
    214 	if (err)
    215 		return err;
    216 	err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
    217 	if (err)
    218 		goto bad1;
    219 
    220 	usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
    221 	ipipe->up_intrxfer = xfer;
    222 	ipipe->up_repeat = 1;
    223 	err = usbd_transfer(xfer);
    224 	*pipe = ipipe;
    225 	if (err != USBD_IN_PROGRESS)
    226 		goto bad3;
    227 	return USBD_NORMAL_COMPLETION;
    228 
    229  bad3:
    230 	ipipe->up_intrxfer = NULL;
    231 	ipipe->up_repeat = 0;
    232 
    233 	usbd_destroy_xfer(xfer);
    234  bad1:
    235 	usbd_close_pipe(ipipe);
    236 	return err;
    237 }
    238 
    239 usbd_status
    240 usbd_close_pipe(struct usbd_pipe *pipe)
    241 {
    242 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    243 
    244 	KASSERT(pipe != NULL);
    245 
    246 	usbd_lock_pipe(pipe);
    247 
    248 	if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
    249 		printf("WARNING: pipe closed with active xfers on addr %d\n",
    250 		    pipe->up_dev->ud_addr);
    251 		usbd_ar_pipe(pipe);
    252 	}
    253 
    254 	KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
    255 
    256 	LIST_REMOVE(pipe, up_next);
    257 	pipe->up_endpoint->ue_refcnt--;
    258 
    259 	pipe->up_methods->upm_close(pipe);
    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 	usbd_unlock_pipe(pipe);
    268 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
    269 
    270 	return USBD_NORMAL_COMPLETION;
    271 }
    272 
    273 usbd_status
    274 usbd_transfer(struct usbd_xfer *xfer)
    275 {
    276 	struct usbd_pipe *pipe = xfer->ux_pipe;
    277 	usbd_status err;
    278 	unsigned int size, flags;
    279 
    280 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
    281 	    "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
    282 	    (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
    283 	KASSERT(xfer->ux_status == USBD_NOT_STARTED);
    284 
    285 #ifdef USB_DEBUG
    286 	if (usbdebug > 5)
    287 		usbd_dump_queue(pipe);
    288 #endif
    289 	xfer->ux_done = 0;
    290 
    291 	if (pipe->up_aborting) {
    292 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
    293 		    (uintptr_t)xfer, 0, 0, 0);
    294 		return USBD_CANCELLED;
    295 	}
    296 
    297 	KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
    298 
    299 	size = xfer->ux_length;
    300 	flags = xfer->ux_flags;
    301 
    302 	if (size != 0) {
    303 		/*
    304 		 * Use the xfer buffer if none specified in transfer setup.
    305 		 * isoc transfers always use the xfer buffer, i.e.
    306 		 * ux_buffer is always NULL for isoc.
    307 		 */
    308 		if (xfer->ux_buffer == NULL) {
    309 			xfer->ux_buffer = xfer->ux_buf;
    310 		}
    311 
    312 		/*
    313 		 * If not using the xfer buffer copy data to the
    314 		 * xfer buffer for OUT transfers of >0 length
    315 		 */
    316 		if (xfer->ux_buffer != xfer->ux_buf) {
    317 			KASSERT(xfer->ux_buf);
    318 			if (!usbd_xfer_isread(xfer)) {
    319 				memcpy(xfer->ux_buf, xfer->ux_buffer, size);
    320 			}
    321 		}
    322 	}
    323 
    324 	/* xfer is not valid after the transfer method unless synchronous */
    325 	err = pipe->up_methods->upm_transfer(xfer);
    326 
    327 	if (err != USBD_IN_PROGRESS && err) {
    328 		/*
    329 		 * The transfer made it onto the pipe queue, but didn't get
    330 		 * accepted by the HCD for some reason.  It needs removing
    331 		 * from the pipe queue.
    332 		 */
    333 		USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting",
    334 		    err, 0, 0, 0);
    335 		usbd_lock_pipe(pipe);
    336 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    337 		if (pipe->up_serialise)
    338 			usbd_start_next(pipe);
    339 		usbd_unlock_pipe(pipe);
    340 	}
    341 
    342 	if (!(flags & USBD_SYNCHRONOUS)) {
    343 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
    344 		    (uintptr_t)xfer, err, 0, 0);
    345 		return err;
    346 	}
    347 
    348 	if (err != USBD_IN_PROGRESS) {
    349 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
    350 		    (uintptr_t)xfer, err, 0, 0);
    351 		return err;
    352 	}
    353 
    354 	/* Sync transfer, wait for completion. */
    355 	usbd_lock_pipe(pipe);
    356 	while (!xfer->ux_done) {
    357 		if (pipe->up_dev->ud_bus->ub_usepolling)
    358 			panic("usbd_transfer: not done");
    359 		USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
    360 		    (uintptr_t)xfer, 0, 0, 0);
    361 
    362 		err = 0;
    363 		if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
    364 			err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    365 		} else {
    366 			cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    367 		}
    368 		if (err) {
    369 			if (!xfer->ux_done)
    370 				pipe->up_methods->upm_abort(xfer);
    371 			break;
    372 		}
    373 	}
    374 	usbd_unlock_pipe(pipe);
    375 	return xfer->ux_status;
    376 }
    377 
    378 /* Like usbd_transfer(), but waits for completion. */
    379 usbd_status
    380 usbd_sync_transfer(struct usbd_xfer *xfer)
    381 {
    382 	xfer->ux_flags |= USBD_SYNCHRONOUS;
    383 	return usbd_transfer(xfer);
    384 }
    385 
    386 /* Like usbd_transfer(), but waits for completion and listens for signals. */
    387 usbd_status
    388 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
    389 {
    390 	xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
    391 	return usbd_transfer(xfer);
    392 }
    393 
    394 static void *
    395 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
    396 {
    397 	KASSERT(xfer->ux_buf == NULL);
    398 	KASSERT(size != 0);
    399 
    400 	xfer->ux_bufsize = 0;
    401 #if NUSB_DMA > 0
    402 	struct usbd_bus *bus = xfer->ux_bus;
    403 
    404 	if (bus->ub_usedma) {
    405 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    406 
    407 		int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
    408 		if (err) {
    409 			return NULL;
    410 		}
    411 		xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
    412 		xfer->ux_bufsize = size;
    413 
    414 		return xfer->ux_buf;
    415 	}
    416 #endif
    417 	KASSERT(xfer->ux_bus->ub_usedma == false);
    418 	xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
    419 	xfer->ux_bufsize = size;
    420 	return xfer->ux_buf;
    421 }
    422 
    423 static void
    424 usbd_free_buffer(struct usbd_xfer *xfer)
    425 {
    426 	KASSERT(xfer->ux_buf != NULL);
    427 	KASSERT(xfer->ux_bufsize != 0);
    428 
    429 	void *buf = xfer->ux_buf;
    430 	uint32_t size = xfer->ux_bufsize;
    431 
    432 	xfer->ux_buf = NULL;
    433 	xfer->ux_bufsize = 0;
    434 
    435 #if NUSB_DMA > 0
    436 	struct usbd_bus *bus = xfer->ux_bus;
    437 
    438 	if (bus->ub_usedma) {
    439 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    440 
    441 		usb_freemem(bus, dmap);
    442 		return;
    443 	}
    444 #endif
    445 	KASSERT(xfer->ux_bus->ub_usedma == false);
    446 
    447 	kmem_free(buf, size);
    448 }
    449 
    450 void *
    451 usbd_get_buffer(struct usbd_xfer *xfer)
    452 {
    453 	return xfer->ux_buf;
    454 }
    455 
    456 struct usbd_pipe *
    457 usbd_get_pipe0(struct usbd_device *dev)
    458 {
    459 
    460 	return dev->ud_pipe0;
    461 }
    462 
    463 static struct usbd_xfer *
    464 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
    465 {
    466 	struct usbd_xfer *xfer;
    467 
    468 	USBHIST_FUNC();
    469 
    470 	ASSERT_SLEEPABLE();
    471 
    472 	xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
    473 	if (xfer == NULL)
    474 		goto out;
    475 	xfer->ux_bus = dev->ud_bus;
    476 	callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
    477 	cv_init(&xfer->ux_cv, "usbxfer");
    478 
    479 out:
    480 	USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
    481 
    482 	return xfer;
    483 }
    484 
    485 static usbd_status
    486 usbd_free_xfer(struct usbd_xfer *xfer)
    487 {
    488 	USBHIST_FUNC();
    489 	USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
    490 
    491 	if (xfer->ux_buf) {
    492 		usbd_free_buffer(xfer);
    493 	}
    494 
    495 	/* Wait for any straggling timeout to complete. */
    496 	mutex_enter(xfer->ux_bus->ub_lock);
    497 	xfer->ux_timeout_reset = false; /* do not resuscitate */
    498 	callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
    499 	usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
    500 	    USB_TASKQ_HC, xfer->ux_bus->ub_lock);
    501 	mutex_exit(xfer->ux_bus->ub_lock);
    502 
    503 	cv_destroy(&xfer->ux_cv);
    504 	xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
    505 	return USBD_NORMAL_COMPLETION;
    506 }
    507 
    508 int
    509 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
    510     unsigned int nframes, struct usbd_xfer **xp)
    511 {
    512 	KASSERT(xp != NULL);
    513 	void *buf = NULL;
    514 
    515 	struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
    516 	if (xfer == NULL)
    517 		return ENOMEM;
    518 
    519 	if (len) {
    520 		buf = usbd_alloc_buffer(xfer, len);
    521 		if (!buf) {
    522 			usbd_free_xfer(xfer);
    523 			return ENOMEM;
    524 		}
    525 	}
    526 	xfer->ux_pipe = pipe;
    527 	xfer->ux_flags = flags;
    528 	xfer->ux_nframes = nframes;
    529 	xfer->ux_methods = pipe->up_methods;
    530 
    531 	if (xfer->ux_methods->upm_init) {
    532 		int err = xfer->ux_methods->upm_init(xfer);
    533 		if (err) {
    534 			if (buf)
    535 				usbd_free_buffer(xfer);
    536 			usbd_free_xfer(xfer);
    537 			return err;
    538 		}
    539 	}
    540 
    541 	*xp = xfer;
    542 	return 0;
    543 }
    544 
    545 void
    546 usbd_destroy_xfer(struct usbd_xfer *xfer)
    547 {
    548 
    549 	if (xfer->ux_methods->upm_fini) {
    550 		xfer->ux_methods->upm_fini(xfer);
    551 	}
    552 
    553 	usbd_free_xfer(xfer);
    554 }
    555 
    556 void
    557 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
    558     uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
    559 {
    560 	KASSERT(xfer->ux_pipe);
    561 
    562 	xfer->ux_priv = priv;
    563 	xfer->ux_buffer = buffer;
    564 	xfer->ux_length = length;
    565 	xfer->ux_actlen = 0;
    566 	xfer->ux_flags = flags;
    567 	xfer->ux_timeout = timeout;
    568 	xfer->ux_status = USBD_NOT_STARTED;
    569 	xfer->ux_callback = callback;
    570 	xfer->ux_rqflags &= ~URQ_REQUEST;
    571 	xfer->ux_nframes = 0;
    572 }
    573 
    574 void
    575 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
    576     void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
    577     uint32_t length, uint16_t flags, usbd_callback callback)
    578 {
    579 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
    580 
    581 	xfer->ux_priv = priv;
    582 	xfer->ux_buffer = buffer;
    583 	xfer->ux_length = length;
    584 	xfer->ux_actlen = 0;
    585 	xfer->ux_flags = flags;
    586 	xfer->ux_timeout = timeout;
    587 	xfer->ux_status = USBD_NOT_STARTED;
    588 	xfer->ux_callback = callback;
    589 	xfer->ux_request = *req;
    590 	xfer->ux_rqflags |= URQ_REQUEST;
    591 	xfer->ux_nframes = 0;
    592 }
    593 
    594 void
    595 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
    596     uint32_t nframes, uint16_t flags, usbd_callback callback)
    597 {
    598 	xfer->ux_priv = priv;
    599 	xfer->ux_buffer = NULL;
    600 	xfer->ux_length = 0;
    601 	xfer->ux_actlen = 0;
    602 	xfer->ux_flags = flags;
    603 	xfer->ux_timeout = USBD_NO_TIMEOUT;
    604 	xfer->ux_status = USBD_NOT_STARTED;
    605 	xfer->ux_callback = callback;
    606 	xfer->ux_rqflags &= ~URQ_REQUEST;
    607 	xfer->ux_frlengths = frlengths;
    608 	xfer->ux_nframes = nframes;
    609 }
    610 
    611 void
    612 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
    613 		     void **buffer, uint32_t *count, usbd_status *status)
    614 {
    615 	if (priv != NULL)
    616 		*priv = xfer->ux_priv;
    617 	if (buffer != NULL)
    618 		*buffer = xfer->ux_buffer;
    619 	if (count != NULL)
    620 		*count = xfer->ux_actlen;
    621 	if (status != NULL)
    622 		*status = xfer->ux_status;
    623 }
    624 
    625 usb_config_descriptor_t *
    626 usbd_get_config_descriptor(struct usbd_device *dev)
    627 {
    628 	KASSERT(dev != NULL);
    629 
    630 	return dev->ud_cdesc;
    631 }
    632 
    633 usb_interface_descriptor_t *
    634 usbd_get_interface_descriptor(struct usbd_interface *iface)
    635 {
    636 	KASSERT(iface != NULL);
    637 
    638 	return iface->ui_idesc;
    639 }
    640 
    641 usb_device_descriptor_t *
    642 usbd_get_device_descriptor(struct usbd_device *dev)
    643 {
    644 	KASSERT(dev != NULL);
    645 
    646 	return &dev->ud_ddesc;
    647 }
    648 
    649 usb_endpoint_descriptor_t *
    650 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
    651 {
    652 
    653 	if (index >= iface->ui_idesc->bNumEndpoints)
    654 		return NULL;
    655 	return iface->ui_endpoints[index].ue_edesc;
    656 }
    657 
    658 /* Some drivers may wish to abort requests on the default pipe, *
    659  * but there is no mechanism for getting a handle on it.        */
    660 usbd_status
    661 usbd_abort_default_pipe(struct usbd_device *device)
    662 {
    663 	return usbd_abort_pipe(device->ud_pipe0);
    664 }
    665 
    666 usbd_status
    667 usbd_abort_pipe(struct usbd_pipe *pipe)
    668 {
    669 	usbd_status err;
    670 
    671 	KASSERT(pipe != NULL);
    672 
    673 	usbd_lock_pipe(pipe);
    674 	err = usbd_ar_pipe(pipe);
    675 	usbd_unlock_pipe(pipe);
    676 	return err;
    677 }
    678 
    679 usbd_status
    680 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
    681 {
    682 	struct usbd_device *dev = pipe->up_dev;
    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 	err = usbd_clear_endpoint_feature(dev,
    694 	    pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
    695 #if 0
    696 XXX should we do this?
    697 	if (!err) {
    698 		pipe->state = USBD_PIPE_ACTIVE;
    699 		/* XXX activate pipe */
    700 	}
    701 #endif
    702 	return err;
    703 }
    704 
    705 void
    706 usbd_clear_endpoint_stall_task(void *arg)
    707 {
    708 	struct usbd_pipe *pipe = arg;
    709 	struct usbd_device *dev = pipe->up_dev;
    710 
    711 	pipe->up_methods->upm_cleartoggle(pipe);
    712 
    713 	(void)usbd_clear_endpoint_feature(dev,
    714 	    pipe->up_endpoint->ue_edesc->bEndpointAddress, UF_ENDPOINT_HALT);
    715 }
    716 
    717 void
    718 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
    719 {
    720 	usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
    721 }
    722 
    723 void
    724 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
    725 {
    726 
    727 	pipe->up_methods->upm_cleartoggle(pipe);
    728 }
    729 
    730 usbd_status
    731 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
    732 {
    733 	KASSERT(iface != NULL);
    734 	KASSERT(iface->ui_idesc != NULL);
    735 
    736 	*count = iface->ui_idesc->bNumEndpoints;
    737 	return USBD_NORMAL_COMPLETION;
    738 }
    739 
    740 usbd_status
    741 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
    742 {
    743 
    744 	if (dev->ud_cdesc == NULL)
    745 		return USBD_NOT_CONFIGURED;
    746 	*count = dev->ud_cdesc->bNumInterface;
    747 	return USBD_NORMAL_COMPLETION;
    748 }
    749 
    750 void
    751 usbd_interface2device_handle(struct usbd_interface *iface,
    752 			     struct usbd_device **dev)
    753 {
    754 
    755 	*dev = iface->ui_dev;
    756 }
    757 
    758 usbd_status
    759 usbd_device2interface_handle(struct usbd_device *dev,
    760 			     uint8_t ifaceno, struct usbd_interface **iface)
    761 {
    762 
    763 	if (dev->ud_cdesc == NULL)
    764 		return USBD_NOT_CONFIGURED;
    765 	if (ifaceno >= dev->ud_cdesc->bNumInterface)
    766 		return USBD_INVAL;
    767 	*iface = &dev->ud_ifaces[ifaceno];
    768 	return USBD_NORMAL_COMPLETION;
    769 }
    770 
    771 struct usbd_device *
    772 usbd_pipe2device_handle(struct usbd_pipe *pipe)
    773 {
    774 	KASSERT(pipe != NULL);
    775 
    776 	return pipe->up_dev;
    777 }
    778 
    779 /* XXXX use altno */
    780 usbd_status
    781 usbd_set_interface(struct usbd_interface *iface, int altidx)
    782 {
    783 	usb_device_request_t req;
    784 	usbd_status err;
    785 	void *endpoints;
    786 
    787 	USBHIST_FUNC();
    788 
    789 	if (LIST_FIRST(&iface->ui_pipes) != NULL)
    790 		return USBD_IN_USE;
    791 
    792 	endpoints = iface->ui_endpoints;
    793 	int nendpt = iface->ui_idesc->bNumEndpoints;
    794 	USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    795 	    (uintptr_t)iface, (uintptr_t)endpoints,
    796 	    iface->ui_idesc->bNumEndpoints, 0);
    797 	err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
    798 	if (err)
    799 		return err;
    800 
    801 	/* new setting works, we can free old endpoints */
    802 	if (endpoints != NULL) {
    803 		USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    804 		    (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
    805 		kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
    806 	}
    807 	KASSERT(iface->ui_idesc != NULL);
    808 
    809 	req.bmRequestType = UT_WRITE_INTERFACE;
    810 	req.bRequest = UR_SET_INTERFACE;
    811 	USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
    812 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    813 	USETW(req.wLength, 0);
    814 	return usbd_do_request(iface->ui_dev, &req, 0);
    815 }
    816 
    817 int
    818 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    819 {
    820 	char *p = (char *)cdesc;
    821 	char *end = p + UGETW(cdesc->wTotalLength);
    822 	usb_interface_descriptor_t *d;
    823 	int n;
    824 
    825 	for (n = 0; p < end; p += d->bLength) {
    826 		d = (usb_interface_descriptor_t *)p;
    827 		if (p + d->bLength <= end &&
    828 		    d->bDescriptorType == UDESC_INTERFACE &&
    829 		    d->bInterfaceNumber == ifaceno)
    830 			n++;
    831 	}
    832 	return n;
    833 }
    834 
    835 int
    836 usbd_get_interface_altindex(struct usbd_interface *iface)
    837 {
    838 	return iface->ui_altindex;
    839 }
    840 
    841 usbd_status
    842 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
    843 {
    844 	usb_device_request_t req;
    845 
    846 	req.bmRequestType = UT_READ_INTERFACE;
    847 	req.bRequest = UR_GET_INTERFACE;
    848 	USETW(req.wValue, 0);
    849 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    850 	USETW(req.wLength, 1);
    851 	return usbd_do_request(iface->ui_dev, &req, aiface);
    852 }
    853 
    854 /*** Internal routines ***/
    855 
    856 /* Dequeue all pipe operations, called with bus lock held. */
    857 Static usbd_status
    858 usbd_ar_pipe(struct usbd_pipe *pipe)
    859 {
    860 	struct usbd_xfer *xfer;
    861 
    862 	USBHIST_FUNC();
    863 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    864 
    865 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    866 
    867 #ifdef USB_DEBUG
    868 	if (usbdebug > 5)
    869 		usbd_dump_queue(pipe);
    870 #endif
    871 	pipe->up_repeat = 0;
    872 	pipe->up_aborting = 1;
    873 	while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
    874 		USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
    875 		    "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
    876 		    (uintptr_t)pipe->up_methods, 0);
    877 		if (xfer->ux_status == USBD_NOT_STARTED) {
    878 			SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    879 		} else {
    880 			/* Make the HC abort it (and invoke the callback). */
    881 			pipe->up_methods->upm_abort(xfer);
    882 			/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    883 		}
    884 	}
    885 	pipe->up_aborting = 0;
    886 	return USBD_NORMAL_COMPLETION;
    887 }
    888 
    889 /* Called with USB lock held. */
    890 void
    891 usb_transfer_complete(struct usbd_xfer *xfer)
    892 {
    893 	struct usbd_pipe *pipe = xfer->ux_pipe;
    894 	struct usbd_bus *bus = pipe->up_dev->ud_bus;
    895 	int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
    896 	int erred;
    897 	int polling = bus->ub_usepolling;
    898 	int repeat = pipe->up_repeat;
    899 
    900 	USBHIST_FUNC();
    901 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
    902 	    "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
    903 	    xfer->ux_actlen);
    904 
    905 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    906 	KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
    907 	    xfer->ux_state);
    908 	KASSERT(pipe != NULL);
    909 
    910 	/*
    911 	 * If device is known to miss out ack, then pretend that
    912 	 * output timeout is a success. Userland should handle
    913 	 * the logic to verify that the operation succeeded.
    914 	 */
    915 	if (pipe->up_dev->ud_quirks &&
    916 	    pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
    917 	    xfer->ux_status == USBD_TIMEOUT &&
    918 	    !usbd_xfer_isread(xfer)) {
    919 		USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
    920 		    "hiding write timeout to %d.%s for %d bytes written",
    921 		    (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
    922 		    xfer->ux_length);
    923 
    924 		xfer->ux_status = USBD_NORMAL_COMPLETION;
    925 		xfer->ux_actlen = xfer->ux_length;
    926 	}
    927 
    928 	erred = xfer->ux_status == USBD_CANCELLED ||
    929 	        xfer->ux_status == USBD_TIMEOUT;
    930 
    931 	if (!repeat) {
    932 		/* Remove request from queue. */
    933 
    934 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
    935 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
    936 		     xfer);
    937 		KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
    938 		    "xfer %p is not start of queue (%p is at start)", xfer,
    939 		   SIMPLEQ_FIRST(&pipe->up_queue));
    940 
    941 #ifdef DIAGNOSTIC
    942 		xfer->ux_state = XFER_BUSY;
    943 #endif
    944 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    945 	}
    946 	USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
    947 	    (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
    948 	    0);
    949 
    950 	/* Count completed transfers. */
    951 	++pipe->up_dev->ud_bus->ub_stats.uds_requests
    952 		[pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
    953 
    954 	xfer->ux_done = 1;
    955 	if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
    956 	    !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
    957 		USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
    958 		    xfer->ux_actlen, xfer->ux_length, 0, 0);
    959 		xfer->ux_status = USBD_SHORT_XFER;
    960 	}
    961 
    962 	USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
    963 	    (uintptr_t)pipe->up_methods->upm_done, 0, 0);
    964 	pipe->up_methods->upm_done(xfer);
    965 
    966 	if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
    967 		KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
    968 		    "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
    969 
    970 		/* Only if IN transfer */
    971 		if (usbd_xfer_isread(xfer)) {
    972 			memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
    973 		}
    974 	}
    975 
    976 	USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
    977 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
    978 
    979 	if (xfer->ux_callback) {
    980 		if (!polling) {
    981 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
    982 			if (!(pipe->up_flags & USBD_MPSAFE))
    983 				KERNEL_LOCK(1, curlwp);
    984 		}
    985 
    986 		xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
    987 
    988 		if (!polling) {
    989 			if (!(pipe->up_flags & USBD_MPSAFE))
    990 				KERNEL_UNLOCK_ONE(curlwp);
    991 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
    992 		}
    993 	}
    994 
    995 	if (sync && !polling) {
    996 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
    997 		    (uintptr_t)xfer, 0, 0, 0);
    998 		cv_broadcast(&xfer->ux_cv);
    999 	}
   1000 
   1001 	if (repeat) {
   1002 		xfer->ux_actlen = 0;
   1003 		xfer->ux_status = USBD_NOT_STARTED;
   1004 	} else {
   1005 		/* XXX should we stop the queue on all errors? */
   1006 		if (erred && pipe->up_iface != NULL)	/* not control pipe */
   1007 			pipe->up_running = 0;
   1008 	}
   1009 	if (pipe->up_running && pipe->up_serialise)
   1010 		usbd_start_next(pipe);
   1011 }
   1012 
   1013 /* Called with USB lock held. */
   1014 usbd_status
   1015 usb_insert_transfer(struct usbd_xfer *xfer)
   1016 {
   1017 	struct usbd_pipe *pipe = xfer->ux_pipe;
   1018 	usbd_status err;
   1019 
   1020 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
   1021 	    "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
   1022 	    (uintptr_t)xfer, (uintptr_t)pipe,
   1023 	    pipe->up_running, xfer->ux_timeout);
   1024 
   1025 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1026 	KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
   1027 	    xfer->ux_state);
   1028 
   1029 #ifdef DIAGNOSTIC
   1030 	xfer->ux_state = XFER_ONQU;
   1031 #endif
   1032 	SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
   1033 	if (pipe->up_running && pipe->up_serialise)
   1034 		err = USBD_IN_PROGRESS;
   1035 	else {
   1036 		pipe->up_running = 1;
   1037 		err = USBD_NORMAL_COMPLETION;
   1038 	}
   1039 	USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
   1040 	    err, 0, 0);
   1041 	return err;
   1042 }
   1043 
   1044 /* Called with USB lock held. */
   1045 void
   1046 usbd_start_next(struct usbd_pipe *pipe)
   1047 {
   1048 	struct usbd_xfer *xfer;
   1049 	usbd_status err;
   1050 
   1051 	USBHIST_FUNC();
   1052 
   1053 	KASSERT(pipe != NULL);
   1054 	KASSERT(pipe->up_methods != NULL);
   1055 	KASSERT(pipe->up_methods->upm_start != NULL);
   1056 	KASSERT(pipe->up_serialise == true);
   1057 
   1058 	int polling = pipe->up_dev->ud_bus->ub_usepolling;
   1059 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1060 
   1061 	/* Get next request in queue. */
   1062 	xfer = SIMPLEQ_FIRST(&pipe->up_queue);
   1063 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
   1064 	    (uintptr_t)xfer, 0, 0);
   1065 	if (xfer == NULL) {
   1066 		pipe->up_running = 0;
   1067 	} else {
   1068 		if (!polling)
   1069 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
   1070 		err = pipe->up_methods->upm_start(xfer);
   1071 		if (!polling)
   1072 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
   1073 
   1074 		if (err != USBD_IN_PROGRESS) {
   1075 			USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
   1076 			pipe->up_running = 0;
   1077 			/* XXX do what? */
   1078 		}
   1079 	}
   1080 
   1081 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1082 }
   1083 
   1084 usbd_status
   1085 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
   1086 {
   1087 
   1088 	return usbd_do_request_flags(dev, req, data, 0, 0,
   1089 	    USBD_DEFAULT_TIMEOUT);
   1090 }
   1091 
   1092 usbd_status
   1093 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
   1094     void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1095 {
   1096 	size_t len = UGETW(req->wLength);
   1097 
   1098 	return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
   1099 }
   1100 
   1101 usbd_status
   1102 usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
   1103     size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1104 {
   1105 	struct usbd_xfer *xfer;
   1106 	usbd_status err;
   1107 
   1108 	KASSERT(len >= UGETW(req->wLength));
   1109 
   1110 	USBHIST_FUNC();
   1111 	USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
   1112 	    (uintptr_t)dev, (uintptr_t)req, flags, len);
   1113 
   1114 	ASSERT_SLEEPABLE();
   1115 
   1116 	int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
   1117 	if (error)
   1118 		return error;
   1119 
   1120 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
   1121 	    UGETW(req->wLength), flags, NULL);
   1122 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
   1123 	err = usbd_sync_transfer(xfer);
   1124 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1125 	if (xfer->ux_actlen > xfer->ux_length) {
   1126 		USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
   1127 		    dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
   1128 		USBHIST_LOG(usbdebug, "     req = 0x%02jx val = %jd "
   1129 		    "index = %jd",
   1130 		    xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
   1131 		    UGETW(xfer->ux_request.wIndex), 0);
   1132 		USBHIST_LOG(usbdebug, "     rlen = %jd length = %jd "
   1133 		    "actlen = %jd",
   1134 		    UGETW(xfer->ux_request.wLength),
   1135 		    xfer->ux_length, xfer->ux_actlen, 0);
   1136 	}
   1137 #endif
   1138 	if (actlen != NULL)
   1139 		*actlen = xfer->ux_actlen;
   1140 
   1141 	usbd_destroy_xfer(xfer);
   1142 
   1143 	if (err) {
   1144 		USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
   1145 	}
   1146 	return err;
   1147 }
   1148 
   1149 static void
   1150 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1151 {
   1152 	usbd_free_xfer(xfer);
   1153 }
   1154 
   1155 /*
   1156  * Execute a request without waiting for completion.
   1157  * Can be used from interrupt context.
   1158  */
   1159 usbd_status
   1160 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
   1161     usb_device_request_t *req, void *priv, usbd_callback callback)
   1162 {
   1163 	usbd_status err;
   1164 
   1165 	if (callback == NULL)
   1166 		callback = usbd_request_async_cb;
   1167 
   1168 	usbd_setup_default_xfer(xfer, dev, priv,
   1169 	    USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
   1170 	    callback);
   1171 	err = usbd_transfer(xfer);
   1172 	if (err != USBD_IN_PROGRESS) {
   1173 		usbd_free_xfer(xfer);
   1174 		return (err);
   1175 	}
   1176 	return (USBD_NORMAL_COMPLETION);
   1177 }
   1178 
   1179 const struct usbd_quirks *
   1180 usbd_get_quirks(struct usbd_device *dev)
   1181 {
   1182 #ifdef DIAGNOSTIC
   1183 	if (dev == NULL) {
   1184 		printf("usbd_get_quirks: dev == NULL\n");
   1185 		return 0;
   1186 	}
   1187 #endif
   1188 	return dev->ud_quirks;
   1189 }
   1190 
   1191 /* XXX do periodic free() of free list */
   1192 
   1193 /*
   1194  * Called from keyboard driver when in polling mode.
   1195  */
   1196 void
   1197 usbd_dopoll(struct usbd_interface *iface)
   1198 {
   1199 	iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
   1200 }
   1201 
   1202 /*
   1203  * This is for keyboard driver as well, which only operates in polling
   1204  * mode from the ask root, etc., prompt and from DDB.
   1205  */
   1206 void
   1207 usbd_set_polling(struct usbd_device *dev, int on)
   1208 {
   1209 	if (on)
   1210 		dev->ud_bus->ub_usepolling++;
   1211 	else
   1212 		dev->ud_bus->ub_usepolling--;
   1213 
   1214 	/* Kick the host controller when switching modes */
   1215 	mutex_enter(dev->ud_bus->ub_lock);
   1216 	dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
   1217 	mutex_exit(dev->ud_bus->ub_lock);
   1218 }
   1219 
   1220 
   1221 usb_endpoint_descriptor_t *
   1222 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
   1223 {
   1224 	struct usbd_endpoint *ep;
   1225 	int i;
   1226 
   1227 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
   1228 		ep = &iface->ui_endpoints[i];
   1229 		if (ep->ue_edesc->bEndpointAddress == address)
   1230 			return iface->ui_endpoints[i].ue_edesc;
   1231 	}
   1232 	return NULL;
   1233 }
   1234 
   1235 /*
   1236  * usbd_ratecheck() can limit the number of error messages that occurs.
   1237  * When a device is unplugged it may take up to 0.25s for the hub driver
   1238  * to notice it.  If the driver continuously tries to do I/O operations
   1239  * this can generate a large number of messages.
   1240  */
   1241 int
   1242 usbd_ratecheck(struct timeval *last)
   1243 {
   1244 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1245 
   1246 	return ratecheck(last, &errinterval);
   1247 }
   1248 
   1249 /*
   1250  * Search for a vendor/product pair in an array.  The item size is
   1251  * given as an argument.
   1252  */
   1253 const struct usb_devno *
   1254 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1255 		 uint16_t vendor, uint16_t product)
   1256 {
   1257 	while (nentries-- > 0) {
   1258 		uint16_t tproduct = tbl->ud_product;
   1259 		if (tbl->ud_vendor == vendor &&
   1260 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1261 			return tbl;
   1262 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1263 	}
   1264 	return NULL;
   1265 }
   1266 
   1267 
   1268 void
   1269 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
   1270 {
   1271 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1272 
   1273 	iter->cur = (const uByte *)cd;
   1274 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1275 }
   1276 
   1277 const usb_descriptor_t *
   1278 usb_desc_iter_peek(usbd_desc_iter_t *iter)
   1279 {
   1280 	const usb_descriptor_t *desc;
   1281 
   1282 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1283 		if (iter->cur != iter->end)
   1284 			printf("%s: bad descriptor\n", __func__);
   1285 		return NULL;
   1286 	}
   1287 	desc = (const usb_descriptor_t *)iter->cur;
   1288 	if (desc->bLength == 0) {
   1289 		printf("%s: descriptor length = 0\n", __func__);
   1290 		return NULL;
   1291 	}
   1292 	if (iter->cur + desc->bLength > iter->end) {
   1293 		printf("%s: descriptor length too large\n", __func__);
   1294 		return NULL;
   1295 	}
   1296 	return desc;
   1297 }
   1298 
   1299 const usb_descriptor_t *
   1300 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1301 {
   1302 	const usb_descriptor_t *desc = usb_desc_iter_peek(iter);
   1303 	if (desc == NULL)
   1304 		return NULL;
   1305 	iter->cur += desc->bLength;
   1306 	return desc;
   1307 }
   1308 
   1309 /* Return the next interface descriptor, skipping over any other
   1310  * descriptors.  Returns NULL at the end or on error. */
   1311 const usb_interface_descriptor_t *
   1312 usb_desc_iter_next_interface(usbd_desc_iter_t *iter)
   1313 {
   1314 	const usb_descriptor_t *desc;
   1315 
   1316 	while ((desc = usb_desc_iter_peek(iter)) != NULL &&
   1317 	       desc->bDescriptorType != UDESC_INTERFACE)
   1318 	{
   1319 		usb_desc_iter_next(iter);
   1320 	}
   1321 
   1322 	return (const usb_interface_descriptor_t *)usb_desc_iter_next(iter);
   1323 }
   1324 
   1325 /* Returns the next non-interface descriptor, returning NULL when the
   1326  * next descriptor would be an interface descriptor. */
   1327 const usb_descriptor_t *
   1328 usb_desc_iter_next_non_interface(usbd_desc_iter_t *iter)
   1329 {
   1330 	const usb_descriptor_t *desc;
   1331 
   1332 	if ((desc = usb_desc_iter_peek(iter)) != NULL &&
   1333 	    desc->bDescriptorType != UDESC_INTERFACE)
   1334 	{
   1335 		return usb_desc_iter_next(iter);
   1336 	} else {
   1337 		return NULL;
   1338 	}
   1339 }
   1340 
   1341 usbd_status
   1342 usbd_get_string(struct usbd_device *dev, int si, char *buf)
   1343 {
   1344 	return usbd_get_string0(dev, si, buf, 1);
   1345 }
   1346 
   1347 usbd_status
   1348 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
   1349 {
   1350 	int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
   1351 	usb_string_descriptor_t us;
   1352 	char *s;
   1353 	int i, n;
   1354 	uint16_t c;
   1355 	usbd_status err;
   1356 	int size;
   1357 
   1358 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1359 
   1360 	buf[0] = '\0';
   1361 	if (si == 0)
   1362 		return USBD_INVAL;
   1363 	if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
   1364 		return USBD_STALLED;
   1365 	if (dev->ud_langid == USBD_NOLANG) {
   1366 		/* Set up default language */
   1367 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1368 		    &size);
   1369 		if (err || size < 4) {
   1370 			USBHIST_LOG(usbdebug, "getting lang failed, using 0",
   1371 			    0, 0, 0, 0);
   1372 			dev->ud_langid = 0; /* Well, just pick something then */
   1373 		} else {
   1374 			/* Pick the first language as the default. */
   1375 			dev->ud_langid = UGETW(us.bString[0]);
   1376 		}
   1377 	}
   1378 	err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
   1379 	if (err)
   1380 		return err;
   1381 	s = buf;
   1382 	n = size / 2 - 1;
   1383 	if (unicode) {
   1384 		for (i = 0; i < n; i++) {
   1385 			c = UGETW(us.bString[i]);
   1386 			if (swap)
   1387 				c = (c >> 8) | (c << 8);
   1388 			s += wput_utf8(s, 3, c);
   1389 		}
   1390 		*s++ = 0;
   1391 	}
   1392 #ifdef COMPAT_30
   1393 	else {
   1394 		for (i = 0; i < n; i++) {
   1395 			c = UGETW(us.bString[i]);
   1396 			if (swap)
   1397 				c = (c >> 8) | (c << 8);
   1398 			*s++ = (c < 0x80) ? c : '?';
   1399 		}
   1400 		*s++ = 0;
   1401 	}
   1402 #endif
   1403 	return USBD_NORMAL_COMPLETION;
   1404 }
   1405