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