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