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