Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.182.4.4
      1 /*	$NetBSD: usbdi.c,v 1.182.4.4 2020/04/03 12:27:55 martin 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.182.4.4 2020/04/03 12:27:55 martin 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 static void usbd_xfer_timeout(void *);
     75 static void usbd_xfer_timeout_task(void *);
     76 static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
     77 static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
     78 
     79 #if defined(USB_DEBUG)
     80 void
     81 usbd_dump_iface(struct usbd_interface *iface)
     82 {
     83 	USBHIST_FUNC();
     84 	USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
     85 
     86 	if (iface == NULL)
     87 		return;
     88 	USBHIST_LOG(usbdebug, "     device = %#jx idesc = %#jx index = %d",
     89 	    (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
     90 	    iface->ui_index, 0);
     91 	USBHIST_LOG(usbdebug, "     altindex=%d priv=%#jx",
     92 	    iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
     93 }
     94 
     95 void
     96 usbd_dump_device(struct usbd_device *dev)
     97 {
     98 	USBHIST_FUNC();
     99 	USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
    100 
    101 	if (dev == NULL)
    102 		return;
    103 	USBHIST_LOG(usbdebug, "     bus = %#jx default_pipe = %#jx",
    104 	    (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
    105 	USBHIST_LOG(usbdebug, "     address = %jd config = %jd depth = %jd ",
    106 	    dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
    107 	USBHIST_LOG(usbdebug, "     speed = %jd self_powered = %jd "
    108 	    "power = %jd langid = %jd",
    109 	    dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
    110 }
    111 
    112 void
    113 usbd_dump_endpoint(struct usbd_endpoint *endp)
    114 {
    115 	USBHIST_FUNC();
    116 	USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
    117 
    118 	if (endp == NULL)
    119 		return;
    120 	USBHIST_LOG(usbdebug, "    edesc = %#jx refcnt = %jd",
    121 	    (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
    122 	if (endp->ue_edesc)
    123 		USBHIST_LOG(usbdebug, "     bEndpointAddress=0x%02x",
    124 		    endp->ue_edesc->bEndpointAddress, 0, 0, 0);
    125 }
    126 
    127 void
    128 usbd_dump_queue(struct usbd_pipe *pipe)
    129 {
    130 	struct usbd_xfer *xfer;
    131 
    132 	USBHIST_FUNC();
    133 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    134 
    135 	SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
    136 		USBHIST_LOG(usbdebug, "     xfer = %#jx", (uintptr_t)xfer,
    137 		    0, 0, 0);
    138 	}
    139 }
    140 
    141 void
    142 usbd_dump_pipe(struct usbd_pipe *pipe)
    143 {
    144 	USBHIST_FUNC();
    145 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    146 
    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, "     running = %jd aborting = %jd",
    154 	    pipe->up_running, pipe->up_aborting, 0, 0);
    155 	USBHIST_LOG(usbdebug, "     intrxfer = %#jx, repeat = %jd, "
    156 	    "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
    157 	    pipe->up_interval, 0);
    158 }
    159 #endif
    160 
    161 usbd_status
    162 usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
    163 	       uint8_t flags, struct usbd_pipe **pipe)
    164 {
    165 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    166 				    USBD_DEFAULT_INTERVAL));
    167 }
    168 
    169 usbd_status
    170 usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
    171 		    uint8_t flags, struct usbd_pipe **pipe, int ival)
    172 {
    173 	struct usbd_pipe *p;
    174 	struct usbd_endpoint *ep;
    175 	usbd_status err;
    176 	int i;
    177 
    178 	USBHIST_FUNC();
    179 	USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
    180 	    (uintptr_t)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(struct usbd_interface *iface, uint8_t address,
    203 		    uint8_t flags, struct usbd_pipe **pipe,
    204 		    void *priv, void *buffer, uint32_t len,
    205 		    usbd_callback cb, int ival)
    206 {
    207 	usbd_status err;
    208 	struct usbd_xfer *xfer;
    209 	struct usbd_pipe *ipipe;
    210 
    211 	USBHIST_FUNC();
    212 	USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
    213 	    address, flags, len, 0);
    214 
    215 	err = usbd_open_pipe_ival(iface, address,
    216 				  USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
    217 				  &ipipe, ival);
    218 	if (err)
    219 		return err;
    220 	err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
    221 	if (err)
    222 		goto bad1;
    223 
    224 	usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
    225 	ipipe->up_intrxfer = xfer;
    226 	ipipe->up_repeat = 1;
    227 	err = usbd_transfer(xfer);
    228 	*pipe = ipipe;
    229 	if (err != USBD_IN_PROGRESS)
    230 		goto bad3;
    231 	return USBD_NORMAL_COMPLETION;
    232 
    233  bad3:
    234 	ipipe->up_intrxfer = NULL;
    235 	ipipe->up_repeat = 0;
    236 
    237 	usbd_destroy_xfer(xfer);
    238  bad1:
    239 	usbd_close_pipe(ipipe);
    240 	return err;
    241 }
    242 
    243 usbd_status
    244 usbd_close_pipe(struct usbd_pipe *pipe)
    245 {
    246 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    247 
    248 	KASSERT(pipe != NULL);
    249 
    250 	usbd_lock_pipe(pipe);
    251 
    252 	if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
    253 		printf("WARNING: pipe closed with active xfers on addr %d\n",
    254 		    pipe->up_dev->ud_addr);
    255 		usbd_ar_pipe(pipe);
    256 	}
    257 
    258 	KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
    259 
    260 	LIST_REMOVE(pipe, up_next);
    261 	pipe->up_endpoint->ue_refcnt--;
    262 
    263 	pipe->up_methods->upm_close(pipe);
    264 
    265 	if (pipe->up_intrxfer != NULL) {
    266 	    	usbd_unlock_pipe(pipe);
    267 		usbd_destroy_xfer(pipe->up_intrxfer);
    268 		usbd_lock_pipe(pipe);
    269 	}
    270 
    271 	usbd_unlock_pipe(pipe);
    272 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
    273 
    274 	return USBD_NORMAL_COMPLETION;
    275 }
    276 
    277 usbd_status
    278 usbd_transfer(struct usbd_xfer *xfer)
    279 {
    280 	struct usbd_pipe *pipe = xfer->ux_pipe;
    281 	usbd_status err;
    282 	unsigned int size, flags;
    283 
    284 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
    285 	    "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
    286 	    (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
    287 	KASSERT(xfer->ux_status == USBD_NOT_STARTED);
    288 
    289 #ifdef USB_DEBUG
    290 	if (usbdebug > 5)
    291 		usbd_dump_queue(pipe);
    292 #endif
    293 	xfer->ux_done = 0;
    294 
    295 	if (pipe->up_aborting) {
    296 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
    297 		    (uintptr_t)xfer, 0, 0, 0);
    298 		return USBD_CANCELLED;
    299 	}
    300 
    301 	KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
    302 
    303 	size = xfer->ux_length;
    304 	flags = xfer->ux_flags;
    305 
    306 	if (size != 0) {
    307 		/*
    308 		 * Use the xfer buffer if none specified in transfer setup.
    309 		 * isoc transfers always use the xfer buffer, i.e.
    310 		 * ux_buffer is always NULL for isoc.
    311 		 */
    312 		if (xfer->ux_buffer == NULL) {
    313 			xfer->ux_buffer = xfer->ux_buf;
    314 		}
    315 
    316 		/*
    317 		 * If not using the xfer buffer copy data to the
    318 		 * xfer buffer for OUT transfers of >0 length
    319 		 */
    320 		if (xfer->ux_buffer != xfer->ux_buf) {
    321 			KASSERT(xfer->ux_buf);
    322 			if (!usbd_xfer_isread(xfer)) {
    323 				memcpy(xfer->ux_buf, xfer->ux_buffer, size);
    324 			}
    325 		}
    326 	}
    327 
    328 	/* xfer is not valid after the transfer method unless synchronous */
    329 	err = pipe->up_methods->upm_transfer(xfer);
    330 
    331 	if (err != USBD_IN_PROGRESS && err) {
    332 		/*
    333 		 * The transfer made it onto the pipe queue, but didn't get
    334 		 * accepted by the HCD for some reason.  It needs removing
    335 		 * from the pipe queue.
    336 		 */
    337 		USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting",
    338 		    err, 0, 0, 0);
    339 		usbd_lock_pipe(pipe);
    340 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    341 		if (pipe->up_serialise)
    342 			usbd_start_next(pipe);
    343 		usbd_unlock_pipe(pipe);
    344 	}
    345 
    346 	if (!(flags & USBD_SYNCHRONOUS)) {
    347 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
    348 		    (uintptr_t)xfer, err, 0, 0);
    349 		return err;
    350 	}
    351 
    352 	if (err != USBD_IN_PROGRESS) {
    353 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
    354 		    (uintptr_t)xfer, err, 0, 0);
    355 		return err;
    356 	}
    357 
    358 	/* Sync transfer, wait for completion. */
    359 	usbd_lock_pipe(pipe);
    360 	while (!xfer->ux_done) {
    361 		if (pipe->up_dev->ud_bus->ub_usepolling)
    362 			panic("usbd_transfer: not done");
    363 		USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
    364 		    (uintptr_t)xfer, 0, 0, 0);
    365 
    366 		err = 0;
    367 		if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
    368 			err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    369 		} else {
    370 			cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    371 		}
    372 		if (err) {
    373 			if (!xfer->ux_done)
    374 				pipe->up_methods->upm_abort(xfer);
    375 			break;
    376 		}
    377 	}
    378 	usbd_unlock_pipe(pipe);
    379 	return xfer->ux_status;
    380 }
    381 
    382 /* Like usbd_transfer(), but waits for completion. */
    383 usbd_status
    384 usbd_sync_transfer(struct usbd_xfer *xfer)
    385 {
    386 	xfer->ux_flags |= USBD_SYNCHRONOUS;
    387 	return usbd_transfer(xfer);
    388 }
    389 
    390 /* Like usbd_transfer(), but waits for completion and listens for signals. */
    391 usbd_status
    392 usbd_sync_transfer_sig(struct usbd_xfer *xfer)
    393 {
    394 	xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
    395 	return usbd_transfer(xfer);
    396 }
    397 
    398 static void *
    399 usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
    400 {
    401 	KASSERT(xfer->ux_buf == NULL);
    402 	KASSERT(size != 0);
    403 
    404 	xfer->ux_bufsize = 0;
    405 #if NUSB_DMA > 0
    406 	struct usbd_bus *bus = xfer->ux_bus;
    407 
    408 	if (bus->ub_usedma) {
    409 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    410 
    411 		int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
    412 		if (err) {
    413 			return NULL;
    414 		}
    415 		xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
    416 		xfer->ux_bufsize = size;
    417 
    418 		return xfer->ux_buf;
    419 	}
    420 #endif
    421 	KASSERT(xfer->ux_bus->ub_usedma == false);
    422 	xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
    423 	xfer->ux_bufsize = size;
    424 	return xfer->ux_buf;
    425 }
    426 
    427 static void
    428 usbd_free_buffer(struct usbd_xfer *xfer)
    429 {
    430 	KASSERT(xfer->ux_buf != NULL);
    431 	KASSERT(xfer->ux_bufsize != 0);
    432 
    433 	void *buf = xfer->ux_buf;
    434 	uint32_t size = xfer->ux_bufsize;
    435 
    436 	xfer->ux_buf = NULL;
    437 	xfer->ux_bufsize = 0;
    438 
    439 #if NUSB_DMA > 0
    440 	struct usbd_bus *bus = xfer->ux_bus;
    441 
    442 	if (bus->ub_usedma) {
    443 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    444 
    445 		usb_freemem(bus, dmap);
    446 		return;
    447 	}
    448 #endif
    449 	KASSERT(xfer->ux_bus->ub_usedma == false);
    450 
    451 	kmem_free(buf, size);
    452 }
    453 
    454 void *
    455 usbd_get_buffer(struct usbd_xfer *xfer)
    456 {
    457 	return xfer->ux_buf;
    458 }
    459 
    460 struct usbd_pipe *
    461 usbd_get_pipe0(struct usbd_device *dev)
    462 {
    463 
    464 	return dev->ud_pipe0;
    465 }
    466 
    467 static struct usbd_xfer *
    468 usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
    469 {
    470 	struct usbd_xfer *xfer;
    471 
    472 	USBHIST_FUNC();
    473 
    474 	ASSERT_SLEEPABLE();
    475 
    476 	xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
    477 	if (xfer == NULL)
    478 		goto out;
    479 	xfer->ux_bus = dev->ud_bus;
    480 	callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
    481 	callout_setfunc(&xfer->ux_callout, usbd_xfer_timeout, xfer);
    482 	cv_init(&xfer->ux_cv, "usbxfer");
    483 	usb_init_task(&xfer->ux_aborttask, usbd_xfer_timeout_task, xfer,
    484 	    USB_TASKQ_MPSAFE);
    485 
    486 out:
    487 	USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
    488 
    489 	return xfer;
    490 }
    491 
    492 static usbd_status
    493 usbd_free_xfer(struct usbd_xfer *xfer)
    494 {
    495 	USBHIST_FUNC();
    496 	USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
    497 
    498 	if (xfer->ux_buf) {
    499 		usbd_free_buffer(xfer);
    500 	}
    501 
    502 	/* Wait for any straggling timeout to complete. */
    503 	mutex_enter(xfer->ux_bus->ub_lock);
    504 	xfer->ux_timeout_reset = false; /* do not resuscitate */
    505 	callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
    506 	usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
    507 	    USB_TASKQ_HC, xfer->ux_bus->ub_lock);
    508 	mutex_exit(xfer->ux_bus->ub_lock);
    509 
    510 	cv_destroy(&xfer->ux_cv);
    511 	xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
    512 	return USBD_NORMAL_COMPLETION;
    513 }
    514 
    515 int
    516 usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
    517     unsigned int nframes, struct usbd_xfer **xp)
    518 {
    519 	KASSERT(xp != NULL);
    520 	void *buf = NULL;
    521 
    522 	struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
    523 	if (xfer == NULL)
    524 		return ENOMEM;
    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 (len) {
    532 		buf = usbd_alloc_buffer(xfer, len);
    533 		if (!buf) {
    534 			usbd_free_xfer(xfer);
    535 			return ENOMEM;
    536 		}
    537 	}
    538 
    539 	if (xfer->ux_methods->upm_init) {
    540 		int err = xfer->ux_methods->upm_init(xfer);
    541 		if (err) {
    542 			usbd_free_xfer(xfer);
    543 			return err;
    544 		}
    545 	}
    546 
    547 	*xp = xfer;
    548 	return 0;
    549 }
    550 
    551 void
    552 usbd_destroy_xfer(struct usbd_xfer *xfer)
    553 {
    554 
    555 	if (xfer->ux_methods->upm_fini) {
    556 		xfer->ux_methods->upm_fini(xfer);
    557 	}
    558 
    559 	usbd_free_xfer(xfer);
    560 }
    561 
    562 void
    563 usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
    564     uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
    565 {
    566 	KASSERT(xfer->ux_pipe);
    567 
    568 	xfer->ux_priv = priv;
    569 	xfer->ux_buffer = buffer;
    570 	xfer->ux_length = length;
    571 	xfer->ux_actlen = 0;
    572 	xfer->ux_flags = flags;
    573 	xfer->ux_timeout = timeout;
    574 	xfer->ux_status = USBD_NOT_STARTED;
    575 	xfer->ux_callback = callback;
    576 	xfer->ux_rqflags &= ~URQ_REQUEST;
    577 	xfer->ux_nframes = 0;
    578 }
    579 
    580 void
    581 usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
    582     void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
    583     uint32_t length, uint16_t flags, usbd_callback callback)
    584 {
    585 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
    586 
    587 	xfer->ux_priv = priv;
    588 	xfer->ux_buffer = buffer;
    589 	xfer->ux_length = length;
    590 	xfer->ux_actlen = 0;
    591 	xfer->ux_flags = flags;
    592 	xfer->ux_timeout = timeout;
    593 	xfer->ux_status = USBD_NOT_STARTED;
    594 	xfer->ux_callback = callback;
    595 	xfer->ux_request = *req;
    596 	xfer->ux_rqflags |= URQ_REQUEST;
    597 	xfer->ux_nframes = 0;
    598 }
    599 
    600 void
    601 usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
    602     uint32_t nframes, uint16_t flags, usbd_callback callback)
    603 {
    604 	xfer->ux_priv = priv;
    605 	xfer->ux_buffer = NULL;
    606 	xfer->ux_length = 0;
    607 	xfer->ux_actlen = 0;
    608 	xfer->ux_flags = flags;
    609 	xfer->ux_timeout = USBD_NO_TIMEOUT;
    610 	xfer->ux_status = USBD_NOT_STARTED;
    611 	xfer->ux_callback = callback;
    612 	xfer->ux_rqflags &= ~URQ_REQUEST;
    613 	xfer->ux_frlengths = frlengths;
    614 	xfer->ux_nframes = nframes;
    615 }
    616 
    617 void
    618 usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
    619 		     void **buffer, uint32_t *count, usbd_status *status)
    620 {
    621 	if (priv != NULL)
    622 		*priv = xfer->ux_priv;
    623 	if (buffer != NULL)
    624 		*buffer = xfer->ux_buffer;
    625 	if (count != NULL)
    626 		*count = xfer->ux_actlen;
    627 	if (status != NULL)
    628 		*status = xfer->ux_status;
    629 }
    630 
    631 usb_config_descriptor_t *
    632 usbd_get_config_descriptor(struct usbd_device *dev)
    633 {
    634 	KASSERT(dev != NULL);
    635 
    636 	return dev->ud_cdesc;
    637 }
    638 
    639 usb_interface_descriptor_t *
    640 usbd_get_interface_descriptor(struct usbd_interface *iface)
    641 {
    642 	KASSERT(iface != NULL);
    643 
    644 	return iface->ui_idesc;
    645 }
    646 
    647 usb_device_descriptor_t *
    648 usbd_get_device_descriptor(struct usbd_device *dev)
    649 {
    650 	KASSERT(dev != NULL);
    651 
    652 	return &dev->ud_ddesc;
    653 }
    654 
    655 usb_endpoint_descriptor_t *
    656 usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
    657 {
    658 
    659 	if (index >= iface->ui_idesc->bNumEndpoints)
    660 		return NULL;
    661 	return iface->ui_endpoints[index].ue_edesc;
    662 }
    663 
    664 /* Some drivers may wish to abort requests on the default pipe, *
    665  * but there is no mechanism for getting a handle on it.        */
    666 usbd_status
    667 usbd_abort_default_pipe(struct usbd_device *device)
    668 {
    669 	return usbd_abort_pipe(device->ud_pipe0);
    670 }
    671 
    672 usbd_status
    673 usbd_abort_pipe(struct usbd_pipe *pipe)
    674 {
    675 	usbd_status err;
    676 
    677 	KASSERT(pipe != NULL);
    678 
    679 	usbd_lock_pipe(pipe);
    680 	err = usbd_ar_pipe(pipe);
    681 	usbd_unlock_pipe(pipe);
    682 	return err;
    683 }
    684 
    685 usbd_status
    686 usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
    687 {
    688 	struct usbd_device *dev = pipe->up_dev;
    689 	usb_device_request_t req;
    690 	usbd_status err;
    691 
    692 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    693 
    694 	/*
    695 	 * Clearing en endpoint stall resets the endpoint toggle, so
    696 	 * do the same to the HC toggle.
    697 	 */
    698 	pipe->up_methods->upm_cleartoggle(pipe);
    699 
    700 	req.bmRequestType = UT_WRITE_ENDPOINT;
    701 	req.bRequest = UR_CLEAR_FEATURE;
    702 	USETW(req.wValue, UF_ENDPOINT_HALT);
    703 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
    704 	USETW(req.wLength, 0);
    705 	err = usbd_do_request(dev, &req, 0);
    706 #if 0
    707 XXX should we do this?
    708 	if (!err) {
    709 		pipe->state = USBD_PIPE_ACTIVE;
    710 		/* XXX activate pipe */
    711 	}
    712 #endif
    713 	return err;
    714 }
    715 
    716 void
    717 usbd_clear_endpoint_stall_task(void *arg)
    718 {
    719 	struct usbd_pipe *pipe = arg;
    720 	struct usbd_device *dev = pipe->up_dev;
    721 	usb_device_request_t req;
    722 
    723 	pipe->up_methods->upm_cleartoggle(pipe);
    724 
    725 	req.bmRequestType = UT_WRITE_ENDPOINT;
    726 	req.bRequest = UR_CLEAR_FEATURE;
    727 	USETW(req.wValue, UF_ENDPOINT_HALT);
    728 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
    729 	USETW(req.wLength, 0);
    730 	(void)usbd_do_request(dev, &req, 0);
    731 }
    732 
    733 void
    734 usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
    735 {
    736 	usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
    737 }
    738 
    739 void
    740 usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
    741 {
    742 
    743 	pipe->up_methods->upm_cleartoggle(pipe);
    744 }
    745 
    746 usbd_status
    747 usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
    748 {
    749 	KASSERT(iface != NULL);
    750 	KASSERT(iface->ui_idesc != NULL);
    751 
    752 	*count = iface->ui_idesc->bNumEndpoints;
    753 	return USBD_NORMAL_COMPLETION;
    754 }
    755 
    756 usbd_status
    757 usbd_interface_count(struct usbd_device *dev, uint8_t *count)
    758 {
    759 
    760 	if (dev->ud_cdesc == NULL)
    761 		return USBD_NOT_CONFIGURED;
    762 	*count = dev->ud_cdesc->bNumInterface;
    763 	return USBD_NORMAL_COMPLETION;
    764 }
    765 
    766 void
    767 usbd_interface2device_handle(struct usbd_interface *iface,
    768 			     struct usbd_device **dev)
    769 {
    770 
    771 	*dev = iface->ui_dev;
    772 }
    773 
    774 usbd_status
    775 usbd_device2interface_handle(struct usbd_device *dev,
    776 			     uint8_t ifaceno, struct usbd_interface **iface)
    777 {
    778 
    779 	if (dev->ud_cdesc == NULL)
    780 		return USBD_NOT_CONFIGURED;
    781 	if (ifaceno >= dev->ud_cdesc->bNumInterface)
    782 		return USBD_INVAL;
    783 	*iface = &dev->ud_ifaces[ifaceno];
    784 	return USBD_NORMAL_COMPLETION;
    785 }
    786 
    787 struct usbd_device *
    788 usbd_pipe2device_handle(struct usbd_pipe *pipe)
    789 {
    790 	KASSERT(pipe != NULL);
    791 
    792 	return pipe->up_dev;
    793 }
    794 
    795 /* XXXX use altno */
    796 usbd_status
    797 usbd_set_interface(struct usbd_interface *iface, int altidx)
    798 {
    799 	usb_device_request_t req;
    800 	usbd_status err;
    801 	void *endpoints;
    802 
    803 	USBHIST_FUNC();
    804 
    805 	if (LIST_FIRST(&iface->ui_pipes) != NULL)
    806 		return USBD_IN_USE;
    807 
    808 	endpoints = iface->ui_endpoints;
    809 	int nendpt = iface->ui_idesc->bNumEndpoints;
    810 	USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    811 	    (uintptr_t)iface, (uintptr_t)endpoints,
    812 	    iface->ui_idesc->bNumEndpoints, 0);
    813 	err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
    814 	if (err)
    815 		return err;
    816 
    817 	/* new setting works, we can free old endpoints */
    818 	if (endpoints != NULL) {
    819 		USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    820 		    (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
    821 		kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
    822 	}
    823 	KASSERT(iface->ui_idesc != NULL);
    824 
    825 	req.bmRequestType = UT_WRITE_INTERFACE;
    826 	req.bRequest = UR_SET_INTERFACE;
    827 	USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
    828 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    829 	USETW(req.wLength, 0);
    830 	return usbd_do_request(iface->ui_dev, &req, 0);
    831 }
    832 
    833 int
    834 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    835 {
    836 	char *p = (char *)cdesc;
    837 	char *end = p + UGETW(cdesc->wTotalLength);
    838 	usb_interface_descriptor_t *d;
    839 	int n;
    840 
    841 	for (n = 0; p < end; p += d->bLength) {
    842 		d = (usb_interface_descriptor_t *)p;
    843 		if (p + d->bLength <= end &&
    844 		    d->bDescriptorType == UDESC_INTERFACE &&
    845 		    d->bInterfaceNumber == ifaceno)
    846 			n++;
    847 	}
    848 	return n;
    849 }
    850 
    851 int
    852 usbd_get_interface_altindex(struct usbd_interface *iface)
    853 {
    854 	return iface->ui_altindex;
    855 }
    856 
    857 usbd_status
    858 usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
    859 {
    860 	usb_device_request_t req;
    861 
    862 	req.bmRequestType = UT_READ_INTERFACE;
    863 	req.bRequest = UR_GET_INTERFACE;
    864 	USETW(req.wValue, 0);
    865 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    866 	USETW(req.wLength, 1);
    867 	return usbd_do_request(iface->ui_dev, &req, aiface);
    868 }
    869 
    870 /*** Internal routines ***/
    871 
    872 /* Dequeue all pipe operations, called with bus lock held. */
    873 Static usbd_status
    874 usbd_ar_pipe(struct usbd_pipe *pipe)
    875 {
    876 	struct usbd_xfer *xfer;
    877 
    878 	USBHIST_FUNC();
    879 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    880 
    881 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    882 
    883 #ifdef USB_DEBUG
    884 	if (usbdebug > 5)
    885 		usbd_dump_queue(pipe);
    886 #endif
    887 	pipe->up_repeat = 0;
    888 	pipe->up_running = 0;
    889 	pipe->up_aborting = 1;
    890 	while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
    891 		USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
    892 		    "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
    893 		    (uintptr_t)pipe->up_methods, 0);
    894 		if (xfer->ux_status == USBD_NOT_STARTED) {
    895 			SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    896 		} else {
    897 			/* Make the HC abort it (and invoke the callback). */
    898 			pipe->up_methods->upm_abort(xfer);
    899 			/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    900 		}
    901 	}
    902 	pipe->up_aborting = 0;
    903 	return USBD_NORMAL_COMPLETION;
    904 }
    905 
    906 /* Called with USB lock held. */
    907 void
    908 usb_transfer_complete(struct usbd_xfer *xfer)
    909 {
    910 	struct usbd_pipe *pipe = xfer->ux_pipe;
    911 	struct usbd_bus *bus = pipe->up_dev->ud_bus;
    912 	int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
    913 	int erred;
    914 	int polling = bus->ub_usepolling;
    915 	int repeat = pipe->up_repeat;
    916 
    917 	USBHIST_FUNC();
    918 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
    919 	    "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
    920 	    xfer->ux_actlen);
    921 
    922 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    923 	KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
    924 	    xfer->ux_state);
    925 	KASSERT(pipe != NULL);
    926 
    927 	/*
    928 	 * If device is known to miss out ack, then pretend that
    929 	 * output timeout is a success. Userland should handle
    930 	 * the logic to verify that the operation succeeded.
    931 	 */
    932 	if (pipe->up_dev->ud_quirks &&
    933 	    pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
    934 	    xfer->ux_status == USBD_TIMEOUT &&
    935 	    !usbd_xfer_isread(xfer)) {
    936 		USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
    937 		    "hiding write timeout to %d.%s for %d bytes written",
    938 		    (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
    939 		    xfer->ux_length);
    940 
    941 		xfer->ux_status = USBD_NORMAL_COMPLETION;
    942 		xfer->ux_actlen = xfer->ux_length;
    943 	}
    944 
    945 	erred = xfer->ux_status == USBD_CANCELLED ||
    946 	        xfer->ux_status == USBD_TIMEOUT;
    947 
    948 	if (!repeat) {
    949 		/* Remove request from queue. */
    950 
    951 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
    952 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
    953 		     xfer);
    954 		KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
    955 		    "xfer %p is not start of queue (%p is at start)", xfer,
    956 		   SIMPLEQ_FIRST(&pipe->up_queue));
    957 
    958 #ifdef DIAGNOSTIC
    959 		xfer->ux_state = XFER_BUSY;
    960 #endif
    961 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    962 	}
    963 	USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
    964 	    (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
    965 	    0);
    966 
    967 	/* Count completed transfers. */
    968 	++pipe->up_dev->ud_bus->ub_stats.uds_requests
    969 		[pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
    970 
    971 	xfer->ux_done = 1;
    972 	if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
    973 	    !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
    974 		USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
    975 		    xfer->ux_actlen, xfer->ux_length, 0, 0);
    976 		xfer->ux_status = USBD_SHORT_XFER;
    977 	}
    978 
    979 	USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
    980 	    (uintptr_t)pipe->up_methods->upm_done, 0, 0);
    981 	pipe->up_methods->upm_done(xfer);
    982 
    983 	if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
    984 		KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
    985 		    "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
    986 
    987 		/* Only if IN transfer */
    988 		if (usbd_xfer_isread(xfer)) {
    989 			memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
    990 		}
    991 	}
    992 
    993 	USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
    994 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
    995 
    996 	if (xfer->ux_callback) {
    997 		if (!polling) {
    998 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
    999 			if (!(pipe->up_flags & USBD_MPSAFE))
   1000 				KERNEL_LOCK(1, curlwp);
   1001 		}
   1002 
   1003 		xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
   1004 
   1005 		if (!polling) {
   1006 			if (!(pipe->up_flags & USBD_MPSAFE))
   1007 				KERNEL_UNLOCK_ONE(curlwp);
   1008 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
   1009 		}
   1010 	}
   1011 
   1012 	if (sync && !polling) {
   1013 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
   1014 		    (uintptr_t)xfer, 0, 0, 0);
   1015 		cv_broadcast(&xfer->ux_cv);
   1016 	}
   1017 
   1018 	if (repeat) {
   1019 		xfer->ux_actlen = 0;
   1020 		xfer->ux_status = USBD_NOT_STARTED;
   1021 	} else {
   1022 		/* XXX should we stop the queue on all errors? */
   1023 		if (erred && pipe->up_iface != NULL)	/* not control pipe */
   1024 			pipe->up_running = 0;
   1025 	}
   1026 	if (pipe->up_running && pipe->up_serialise)
   1027 		usbd_start_next(pipe);
   1028 }
   1029 
   1030 /* Called with USB lock held. */
   1031 usbd_status
   1032 usb_insert_transfer(struct usbd_xfer *xfer)
   1033 {
   1034 	struct usbd_pipe *pipe = xfer->ux_pipe;
   1035 	usbd_status err;
   1036 
   1037 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
   1038 	    "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
   1039 	    (uintptr_t)xfer, (uintptr_t)pipe,
   1040 	    pipe->up_running, xfer->ux_timeout);
   1041 
   1042 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1043 	KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
   1044 	    xfer->ux_state);
   1045 
   1046 #ifdef DIAGNOSTIC
   1047 	xfer->ux_state = XFER_ONQU;
   1048 #endif
   1049 	SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
   1050 	if (pipe->up_running && pipe->up_serialise)
   1051 		err = USBD_IN_PROGRESS;
   1052 	else {
   1053 		pipe->up_running = 1;
   1054 		err = USBD_NORMAL_COMPLETION;
   1055 	}
   1056 	USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
   1057 	    err, 0, 0);
   1058 	return err;
   1059 }
   1060 
   1061 /* Called with USB lock held. */
   1062 void
   1063 usbd_start_next(struct usbd_pipe *pipe)
   1064 {
   1065 	struct usbd_xfer *xfer;
   1066 	usbd_status err;
   1067 
   1068 	USBHIST_FUNC();
   1069 
   1070 	KASSERT(pipe != NULL);
   1071 	KASSERT(pipe->up_methods != NULL);
   1072 	KASSERT(pipe->up_methods->upm_start != NULL);
   1073 	KASSERT(pipe->up_serialise == true);
   1074 
   1075 	int polling = pipe->up_dev->ud_bus->ub_usepolling;
   1076 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1077 
   1078 	/* Get next request in queue. */
   1079 	xfer = SIMPLEQ_FIRST(&pipe->up_queue);
   1080 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
   1081 	    (uintptr_t)xfer, 0, 0);
   1082 	if (xfer == NULL) {
   1083 		pipe->up_running = 0;
   1084 	} else {
   1085 		if (!polling)
   1086 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
   1087 		err = pipe->up_methods->upm_start(xfer);
   1088 		if (!polling)
   1089 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
   1090 
   1091 		if (err != USBD_IN_PROGRESS) {
   1092 			USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
   1093 			pipe->up_running = 0;
   1094 			/* XXX do what? */
   1095 		}
   1096 	}
   1097 
   1098 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1099 }
   1100 
   1101 usbd_status
   1102 usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
   1103 {
   1104 
   1105 	return usbd_do_request_flags(dev, req, data, 0, 0,
   1106 	    USBD_DEFAULT_TIMEOUT);
   1107 }
   1108 
   1109 usbd_status
   1110 usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
   1111     void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1112 {
   1113 	size_t len = UGETW(req->wLength);
   1114 
   1115 	return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
   1116 }
   1117 
   1118 usbd_status
   1119 usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
   1120     size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1121 {
   1122 	struct usbd_xfer *xfer;
   1123 	usbd_status err;
   1124 
   1125 	KASSERT(len >= UGETW(req->wLength));
   1126 
   1127 	USBHIST_FUNC();
   1128 	USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
   1129 	    (uintptr_t)dev, (uintptr_t)req, flags, len);
   1130 
   1131 	ASSERT_SLEEPABLE();
   1132 
   1133 	int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
   1134 	if (error)
   1135 		return error;
   1136 
   1137 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
   1138 	    UGETW(req->wLength), flags, NULL);
   1139 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
   1140 	err = usbd_sync_transfer(xfer);
   1141 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1142 	if (xfer->ux_actlen > xfer->ux_length) {
   1143 		USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
   1144 		    dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
   1145 		USBHIST_LOG(usbdebug, "     req = 0x%02jx val = %jd "
   1146 		    "index = %jd",
   1147 		    xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
   1148 		    UGETW(xfer->ux_request.wIndex), 0);
   1149 		USBHIST_LOG(usbdebug, "     rlen = %jd length = %jd "
   1150 		    "actlen = %jd",
   1151 		    UGETW(xfer->ux_request.wLength),
   1152 		    xfer->ux_length, xfer->ux_actlen, 0);
   1153 	}
   1154 #endif
   1155 	if (actlen != NULL)
   1156 		*actlen = xfer->ux_actlen;
   1157 
   1158 	usbd_destroy_xfer(xfer);
   1159 
   1160 	if (err) {
   1161 		USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
   1162 	}
   1163 	return err;
   1164 }
   1165 
   1166 static void
   1167 usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1168 {
   1169 	usbd_free_xfer(xfer);
   1170 }
   1171 
   1172 /*
   1173  * Execute a request without waiting for completion.
   1174  * Can be used from interrupt context.
   1175  */
   1176 usbd_status
   1177 usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
   1178     usb_device_request_t *req, void *priv, usbd_callback callback)
   1179 {
   1180 	usbd_status err;
   1181 
   1182 	if (callback == NULL)
   1183 		callback = usbd_request_async_cb;
   1184 
   1185 	usbd_setup_default_xfer(xfer, dev, priv,
   1186 	    USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
   1187 	    callback);
   1188 	err = usbd_transfer(xfer);
   1189 	if (err != USBD_IN_PROGRESS) {
   1190 		usbd_free_xfer(xfer);
   1191 		return (err);
   1192 	}
   1193 	return (USBD_NORMAL_COMPLETION);
   1194 }
   1195 
   1196 const struct usbd_quirks *
   1197 usbd_get_quirks(struct usbd_device *dev)
   1198 {
   1199 #ifdef DIAGNOSTIC
   1200 	if (dev == NULL) {
   1201 		printf("usbd_get_quirks: dev == NULL\n");
   1202 		return 0;
   1203 	}
   1204 #endif
   1205 	return dev->ud_quirks;
   1206 }
   1207 
   1208 /* XXX do periodic free() of free list */
   1209 
   1210 /*
   1211  * Called from keyboard driver when in polling mode.
   1212  */
   1213 void
   1214 usbd_dopoll(struct usbd_interface *iface)
   1215 {
   1216 	iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
   1217 }
   1218 
   1219 /*
   1220  * This is for keyboard driver as well, which only operates in polling
   1221  * mode from the ask root, etc., prompt and from DDB.
   1222  */
   1223 void
   1224 usbd_set_polling(struct usbd_device *dev, int on)
   1225 {
   1226 	if (on)
   1227 		dev->ud_bus->ub_usepolling++;
   1228 	else
   1229 		dev->ud_bus->ub_usepolling--;
   1230 
   1231 	/* Kick the host controller when switching modes */
   1232 	mutex_enter(dev->ud_bus->ub_lock);
   1233 	dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
   1234 	mutex_exit(dev->ud_bus->ub_lock);
   1235 }
   1236 
   1237 
   1238 usb_endpoint_descriptor_t *
   1239 usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
   1240 {
   1241 	struct usbd_endpoint *ep;
   1242 	int i;
   1243 
   1244 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
   1245 		ep = &iface->ui_endpoints[i];
   1246 		if (ep->ue_edesc->bEndpointAddress == address)
   1247 			return iface->ui_endpoints[i].ue_edesc;
   1248 	}
   1249 	return NULL;
   1250 }
   1251 
   1252 /*
   1253  * usbd_ratecheck() can limit the number of error messages that occurs.
   1254  * When a device is unplugged it may take up to 0.25s for the hub driver
   1255  * to notice it.  If the driver continuously tries to do I/O operations
   1256  * this can generate a large number of messages.
   1257  */
   1258 int
   1259 usbd_ratecheck(struct timeval *last)
   1260 {
   1261 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1262 
   1263 	return ratecheck(last, &errinterval);
   1264 }
   1265 
   1266 /*
   1267  * Search for a vendor/product pair in an array.  The item size is
   1268  * given as an argument.
   1269  */
   1270 const struct usb_devno *
   1271 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1272 		 uint16_t vendor, uint16_t product)
   1273 {
   1274 	while (nentries-- > 0) {
   1275 		uint16_t tproduct = tbl->ud_product;
   1276 		if (tbl->ud_vendor == vendor &&
   1277 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1278 			return tbl;
   1279 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1280 	}
   1281 	return NULL;
   1282 }
   1283 
   1284 
   1285 void
   1286 usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
   1287 {
   1288 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1289 
   1290 	iter->cur = (const uByte *)cd;
   1291 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1292 }
   1293 
   1294 const usb_descriptor_t *
   1295 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1296 {
   1297 	const usb_descriptor_t *desc;
   1298 
   1299 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1300 		if (iter->cur != iter->end)
   1301 			printf("usb_desc_iter_next: bad descriptor\n");
   1302 		return NULL;
   1303 	}
   1304 	desc = (const usb_descriptor_t *)iter->cur;
   1305 	if (desc->bLength == 0) {
   1306 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1307 		return NULL;
   1308 	}
   1309 	iter->cur += desc->bLength;
   1310 	if (iter->cur > iter->end) {
   1311 		printf("usb_desc_iter_next: descriptor length too large\n");
   1312 		return NULL;
   1313 	}
   1314 	return desc;
   1315 }
   1316 
   1317 usbd_status
   1318 usbd_get_string(struct usbd_device *dev, int si, char *buf)
   1319 {
   1320 	return usbd_get_string0(dev, si, buf, 1);
   1321 }
   1322 
   1323 usbd_status
   1324 usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
   1325 {
   1326 	int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
   1327 	usb_string_descriptor_t us;
   1328 	char *s;
   1329 	int i, n;
   1330 	uint16_t c;
   1331 	usbd_status err;
   1332 	int size;
   1333 
   1334 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1335 
   1336 	buf[0] = '\0';
   1337 	if (si == 0)
   1338 		return USBD_INVAL;
   1339 	if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
   1340 		return USBD_STALLED;
   1341 	if (dev->ud_langid == USBD_NOLANG) {
   1342 		/* Set up default language */
   1343 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1344 		    &size);
   1345 		if (err || size < 4) {
   1346 			USBHIST_LOG(usbdebug, "getting lang failed, using 0",
   1347 			    0, 0, 0, 0);
   1348 			dev->ud_langid = 0; /* Well, just pick something then */
   1349 		} else {
   1350 			/* Pick the first language as the default. */
   1351 			dev->ud_langid = UGETW(us.bString[0]);
   1352 		}
   1353 	}
   1354 	err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
   1355 	if (err)
   1356 		return err;
   1357 	s = buf;
   1358 	n = size / 2 - 1;
   1359 	if (unicode) {
   1360 		for (i = 0; i < n; i++) {
   1361 			c = UGETW(us.bString[i]);
   1362 			if (swap)
   1363 				c = (c >> 8) | (c << 8);
   1364 			s += wput_utf8(s, 3, c);
   1365 		}
   1366 		*s++ = 0;
   1367 	}
   1368 #ifdef COMPAT_30
   1369 	else {
   1370 		for (i = 0; i < n; i++) {
   1371 			c = UGETW(us.bString[i]);
   1372 			if (swap)
   1373 				c = (c >> 8) | (c << 8);
   1374 			*s++ = (c < 0x80) ? c : '?';
   1375 		}
   1376 		*s++ = 0;
   1377 	}
   1378 #endif
   1379 	return USBD_NORMAL_COMPLETION;
   1380 }
   1381 
   1382 /*
   1383  * usbd_xfer_trycomplete(xfer)
   1384  *
   1385  *	Try to claim xfer for completion.  Return true if successful,
   1386  *	false if the xfer has been synchronously aborted or has timed
   1387  *	out.
   1388  *
   1389  *	If this returns true, caller is responsible for setting
   1390  *	xfer->ux_status and calling usb_transfer_complete.  To be used
   1391  *	in a host controller interrupt handler.
   1392  *
   1393  *	Caller must either hold the bus lock or have the bus in polling
   1394  *	mode.
   1395  */
   1396 bool
   1397 usbd_xfer_trycomplete(struct usbd_xfer *xfer)
   1398 {
   1399 	struct usbd_bus *bus __diagused = xfer->ux_bus;
   1400 
   1401 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1402 
   1403 	/*
   1404 	 * If software has completed it, either by synchronous abort or
   1405 	 * by timeout, too late.
   1406 	 */
   1407 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1408 		return false;
   1409 
   1410 	/*
   1411 	 * We are completing the xfer.  Cancel the timeout if we can,
   1412 	 * but only asynchronously.  See usbd_xfer_cancel_timeout_async
   1413 	 * for why we need not wait for the callout or task here.
   1414 	 */
   1415 	usbd_xfer_cancel_timeout_async(xfer);
   1416 
   1417 	/* Success!  Note: Caller must set xfer->ux_status afterwar.  */
   1418 	return true;
   1419 }
   1420 
   1421 /*
   1422  * usbd_xfer_abort(xfer)
   1423  *
   1424  *	Try to claim xfer to abort.  If successful, mark it completed
   1425  *	with USBD_CANCELLED and call the bus-specific method to abort
   1426  *	at the hardware level.
   1427  *
   1428  *	To be called in thread context from struct
   1429  *	usbd_pipe_methods::upm_abort.
   1430  *
   1431  *	Caller must hold the bus lock.
   1432  */
   1433 void
   1434 usbd_xfer_abort(struct usbd_xfer *xfer)
   1435 {
   1436 	struct usbd_bus *bus = xfer->ux_bus;
   1437 
   1438 	KASSERT(mutex_owned(bus->ub_lock));
   1439 
   1440 	/*
   1441 	 * If host controller interrupt or timer interrupt has
   1442 	 * completed it, too late.  But the xfer cannot be
   1443 	 * cancelled already -- only one caller can synchronously
   1444 	 * abort.
   1445 	 */
   1446 	KASSERT(xfer->ux_status != USBD_CANCELLED);
   1447 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1448 		return;
   1449 
   1450 	/*
   1451 	 * Cancel the timeout if we can, but only asynchronously; see
   1452 	 * usbd_xfer_cancel_timeout_async for why we need not wait for
   1453 	 * the callout or task here.
   1454 	 */
   1455 	usbd_xfer_cancel_timeout_async(xfer);
   1456 
   1457 	/*
   1458 	 * We beat everyone else.  Claim the status as cancelled and do
   1459 	 * the bus-specific dance to abort the hardware.
   1460 	 */
   1461 	xfer->ux_status = USBD_CANCELLED;
   1462 	bus->ub_methods->ubm_abortx(xfer);
   1463 }
   1464 
   1465 /*
   1466  * usbd_xfer_timeout(xfer)
   1467  *
   1468  *	Called at IPL_SOFTCLOCK when too much time has elapsed waiting
   1469  *	for xfer to complete.  Since we can't abort the xfer at
   1470  *	IPL_SOFTCLOCK, defer to a usb_task to run it in thread context,
   1471  *	unless the xfer has completed or aborted concurrently -- and if
   1472  *	the xfer has also been resubmitted, take care of rescheduling
   1473  *	the callout.
   1474  */
   1475 static void
   1476 usbd_xfer_timeout(void *cookie)
   1477 {
   1478 	struct usbd_xfer *xfer = cookie;
   1479 	struct usbd_bus *bus = xfer->ux_bus;
   1480 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
   1481 
   1482 	/* Acquire the lock so we can transition the timeout state.  */
   1483 	mutex_enter(bus->ub_lock);
   1484 
   1485 	/*
   1486 	 * Use usbd_xfer_probe_timeout to check whether the timeout is
   1487 	 * still valid, or to reschedule the callout if necessary.  If
   1488 	 * it is still valid, schedule the task.
   1489 	 */
   1490 	if (usbd_xfer_probe_timeout(xfer))
   1491 		usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
   1492 
   1493 	/*
   1494 	 * Notify usbd_xfer_cancel_timeout_async that we may have
   1495 	 * scheduled the task.  This causes callout_invoking to return
   1496 	 * false in usbd_xfer_cancel_timeout_async so that it can tell
   1497 	 * which stage in the callout->task->abort process we're at.
   1498 	 */
   1499 	callout_ack(&xfer->ux_callout);
   1500 
   1501 	/* All done -- release the lock.  */
   1502 	mutex_exit(bus->ub_lock);
   1503 }
   1504 
   1505 /*
   1506  * usbd_xfer_timeout_task(xfer)
   1507  *
   1508  *	Called in thread context when too much time has elapsed waiting
   1509  *	for xfer to complete.  Abort the xfer with USBD_TIMEOUT, unless
   1510  *	it has completed or aborted concurrently -- and if the xfer has
   1511  *	also been resubmitted, take care of rescheduling the callout.
   1512  */
   1513 static void
   1514 usbd_xfer_timeout_task(void *cookie)
   1515 {
   1516 	struct usbd_xfer *xfer = cookie;
   1517 	struct usbd_bus *bus = xfer->ux_bus;
   1518 
   1519 	/* Acquire the lock so we can transition the timeout state.  */
   1520 	mutex_enter(bus->ub_lock);
   1521 
   1522 	/*
   1523 	 * Use usbd_xfer_probe_timeout to check whether the timeout is
   1524 	 * still valid, or to reschedule the callout if necessary.  If
   1525 	 * it is not valid -- the timeout has been asynchronously
   1526 	 * cancelled, or the xfer has already been resubmitted -- then
   1527 	 * we're done here.
   1528 	 */
   1529 	if (!usbd_xfer_probe_timeout(xfer))
   1530 		goto out;
   1531 
   1532 	/*
   1533 	 * May have completed or been aborted, but we're the only one
   1534 	 * who can time it out.  If it has completed or been aborted,
   1535 	 * no need to timeout.
   1536 	 */
   1537 	KASSERT(xfer->ux_status != USBD_TIMEOUT);
   1538 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1539 		goto out;
   1540 
   1541 	/*
   1542 	 * We beat everyone else.  Claim the status as timed out and do
   1543 	 * the bus-specific dance to abort the hardware.
   1544 	 */
   1545 	xfer->ux_status = USBD_TIMEOUT;
   1546 	bus->ub_methods->ubm_abortx(xfer);
   1547 
   1548 out:	/* All done -- release the lock.  */
   1549 	mutex_exit(bus->ub_lock);
   1550 }
   1551 
   1552 /*
   1553  * usbd_xfer_probe_timeout(xfer)
   1554  *
   1555  *	Probe the status of xfer's timeout.  Acknowledge and process a
   1556  *	request to reschedule.  Return true if the timeout is still
   1557  *	valid and the caller should take further action (queueing a
   1558  *	task or aborting the xfer), false if it must stop here.
   1559  */
   1560 static bool
   1561 usbd_xfer_probe_timeout(struct usbd_xfer *xfer)
   1562 {
   1563 	struct usbd_bus *bus = xfer->ux_bus;
   1564 	bool valid;
   1565 
   1566 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1567 
   1568 	/* The timeout must be set.  */
   1569 	KASSERT(xfer->ux_timeout_set);
   1570 
   1571 	/*
   1572 	 * Neither callout nor task may be pending; they execute
   1573 	 * alternately in lock step.
   1574 	 */
   1575 	KASSERT(!callout_pending(&xfer->ux_callout));
   1576 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1577 
   1578 	/* There are a few cases... */
   1579 	if (bus->ub_methods->ubm_dying(bus)) {
   1580 		/* Host controller dying.  Drop it all on the floor.  */
   1581 		xfer->ux_timeout_set = false;
   1582 		xfer->ux_timeout_reset = false;
   1583 		valid = false;
   1584 	} else if (xfer->ux_timeout_reset) {
   1585 		/*
   1586 		 * The xfer completed _and_ got resubmitted while we
   1587 		 * waited for the lock.  Acknowledge the request to
   1588 		 * reschedule, and reschedule it if there is a timeout
   1589 		 * and the bus is not polling.
   1590 		 */
   1591 		xfer->ux_timeout_reset = false;
   1592 		if (xfer->ux_timeout && !bus->ub_usepolling) {
   1593 			KASSERT(xfer->ux_timeout_set);
   1594 			callout_schedule(&xfer->ux_callout,
   1595 			    mstohz(xfer->ux_timeout));
   1596 		} else {
   1597 			/* No more callout or task scheduled.  */
   1598 			xfer->ux_timeout_set = false;
   1599 		}
   1600 		valid = false;
   1601 	} else if (xfer->ux_status != USBD_IN_PROGRESS) {
   1602 		/*
   1603 		 * The xfer has completed by hardware completion or by
   1604 		 * software abort, and has not been resubmitted, so the
   1605 		 * timeout must be unset, and is no longer valid for
   1606 		 * the caller.
   1607 		 */
   1608 		xfer->ux_timeout_set = false;
   1609 		valid = false;
   1610 	} else {
   1611 		/*
   1612 		 * The xfer has not yet completed, so the timeout is
   1613 		 * valid.
   1614 		 */
   1615 		valid = true;
   1616 	}
   1617 
   1618 	/* Any reset must have been processed.  */
   1619 	KASSERT(!xfer->ux_timeout_reset);
   1620 
   1621 	/*
   1622 	 * Either we claim the timeout is set, or the callout is idle.
   1623 	 * If the timeout is still set, we may be handing off to the
   1624 	 * task instead, so this is an if but not an iff.
   1625 	 */
   1626 	KASSERT(xfer->ux_timeout_set || !callout_pending(&xfer->ux_callout));
   1627 
   1628 	/*
   1629 	 * The task must be idle now.
   1630 	 *
   1631 	 * - If the caller is the callout, _and_ the timeout is still
   1632 	 *   valid, the caller will schedule it, but it hasn't been
   1633 	 *   scheduled yet.  (If the timeout is not valid, the task
   1634 	 *   should not be scheduled.)
   1635 	 *
   1636 	 * - If the caller is the task, it cannot be scheduled again
   1637 	 *   until the callout runs again, which won't happen until we
   1638 	 *   next release the lock.
   1639 	 */
   1640 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1641 
   1642 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1643 
   1644 	return valid;
   1645 }
   1646 
   1647 /*
   1648  * usbd_xfer_schedule_timeout(xfer)
   1649  *
   1650  *	Ensure that xfer has a timeout.  If the callout is already
   1651  *	queued or the task is already running, request that they
   1652  *	reschedule the callout.  If not, and if we're not polling,
   1653  *	schedule the callout anew.
   1654  *
   1655  *	To be called in thread context from struct
   1656  *	usbd_pipe_methods::upm_start.
   1657  */
   1658 void
   1659 usbd_xfer_schedule_timeout(struct usbd_xfer *xfer)
   1660 {
   1661 	struct usbd_bus *bus = xfer->ux_bus;
   1662 
   1663 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1664 
   1665 	if (xfer->ux_timeout_set) {
   1666 		/*
   1667 		 * Callout or task has fired from a prior completed
   1668 		 * xfer but has not yet noticed that the xfer is done.
   1669 		 * Ask it to reschedule itself to ux_timeout.
   1670 		 */
   1671 		xfer->ux_timeout_reset = true;
   1672 	} else if (xfer->ux_timeout && !bus->ub_usepolling) {
   1673 		/* Callout is not scheduled.  Schedule it.  */
   1674 		KASSERT(!callout_pending(&xfer->ux_callout));
   1675 		callout_schedule(&xfer->ux_callout, mstohz(xfer->ux_timeout));
   1676 		xfer->ux_timeout_set = true;
   1677 	}
   1678 
   1679 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1680 }
   1681 
   1682 /*
   1683  * usbd_xfer_cancel_timeout_async(xfer)
   1684  *
   1685  *	Cancel the callout and the task of xfer, which have not yet run
   1686  *	to completion, but don't wait for the callout or task to finish
   1687  *	running.
   1688  *
   1689  *	If they have already fired, at worst they are waiting for the
   1690  *	bus lock.  They will see that the xfer is no longer in progress
   1691  *	and give up, or they will see that the xfer has been
   1692  *	resubmitted with a new timeout and reschedule the callout.
   1693  *
   1694  *	If a resubmitted request completed so fast that the callout
   1695  *	didn't have time to process a timer reset, just cancel the
   1696  *	timer reset.
   1697  */
   1698 static void
   1699 usbd_xfer_cancel_timeout_async(struct usbd_xfer *xfer)
   1700 {
   1701 	struct usbd_bus *bus __diagused = xfer->ux_bus;
   1702 
   1703 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1704 
   1705 	/*
   1706 	 * If the timer wasn't running anyway, forget about it.  This
   1707 	 * can happen if we are completing an isochronous transfer
   1708 	 * which doesn't use the same timeout logic.
   1709 	 */
   1710 	if (!xfer->ux_timeout_set)
   1711 		return;
   1712 
   1713 	xfer->ux_timeout_reset = false;
   1714 	if (!callout_stop(&xfer->ux_callout)) {
   1715 		/*
   1716 		 * We stopped the callout before it ran.  The timeout
   1717 		 * is no longer set.
   1718 		 */
   1719 		xfer->ux_timeout_set = false;
   1720 	} else if (callout_invoking(&xfer->ux_callout)) {
   1721 		/*
   1722 		 * The callout has begun to run but it has not yet
   1723 		 * acquired the lock and called callout_ack.  The task
   1724 		 * cannot be queued yet, and the callout cannot have
   1725 		 * been rescheduled yet.
   1726 		 *
   1727 		 * By the time the callout acquires the lock, we will
   1728 		 * have transitioned from USBD_IN_PROGRESS to a
   1729 		 * completed status, and possibly also resubmitted the
   1730 		 * xfer and set xfer->ux_timeout_reset = true.  In both
   1731 		 * cases, the callout will DTRT, so no further action
   1732 		 * is needed here.
   1733 		 */
   1734 	} else if (usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask)) {
   1735 		/*
   1736 		 * The callout had fired and scheduled the task, but we
   1737 		 * stopped the task before it could run.  The timeout
   1738 		 * is therefore no longer set -- the next resubmission
   1739 		 * of the xfer must schedule a new timeout.
   1740 		 *
   1741 		 * The callout should not be be pending at this point:
   1742 		 * it is scheduled only under the lock, and only when
   1743 		 * xfer->ux_timeout_set is false, or by the callout or
   1744 		 * task itself when xfer->ux_timeout_reset is true.
   1745 		 */
   1746 		xfer->ux_timeout_set = false;
   1747 	}
   1748 
   1749 	/*
   1750 	 * The callout cannot be scheduled and the task cannot be
   1751 	 * queued at this point.  Either we cancelled them, or they are
   1752 	 * already running and waiting for the bus lock.
   1753 	 */
   1754 	KASSERT(!callout_pending(&xfer->ux_callout));
   1755 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1756 
   1757 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1758 }
   1759