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