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