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