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