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