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