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