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