Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.119.12.4
      1 /*	$NetBSD: usbdi.c,v 1.119.12.4 2007/06/22 10:36:42 itohy Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usbdi.c,v 1.99 2006/11/27 18:39:02 marius Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (lennart (at) augustsson.net) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.119.12.4 2007/06/22 10:36:42 itohy Exp $");
     43 /* __FBSDID("$FreeBSD: src/sys/dev/usb/usbdi.c,v 1.99 2006/11/27 18:39:02 marius Exp $"); */
     44 
     45 #ifdef __NetBSD__
     46 #include "opt_compat_netbsd.h"
     47 #endif
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #if defined(__NetBSD__) || defined(__OpenBSD__)
     52 #include <sys/kernel.h>
     53 #include <sys/device.h>
     54 #elif defined(__FreeBSD__)
     55 #include <sys/module.h>
     56 #include <sys/bus.h>
     57 #include "usb_if.h"
     58 #if defined(DIAGNOSTIC) && defined(__i386__)
     59 #include <machine/cpu.h>
     60 #endif
     61 #endif
     62 #include <sys/malloc.h>
     63 #include <sys/mbuf.h>
     64 #include <sys/proc.h>
     65 
     66 #include <machine/bus.h>
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 #include <dev/usb/usbdivar.h>
     72 /* #include <dev/usb/usb_mem.h> */
     73 #include <dev/usb/usb_quirks.h>
     74 
     75 #if defined(__FreeBSD__)
     76 #include "usb_if.h"
     77 #define delay(d)	DELAY(d)
     78 #endif
     79 
     80 /* UTF-8 encoding stuff */
     81 #include <fs/unicode.h>
     82 
     83 #ifdef USB_DEBUG
     84 #define DPRINTF(x)	if (usbdebug) logprintf x
     85 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     86 extern int usbdebug;
     87 #else
     88 #define DPRINTF(x)
     89 #define DPRINTFN(n,x)
     90 #endif
     91 
     92 Static usbd_xfer_handle usbd_alloc_xfer_flag(usbd_device_handle,
     93 	usbd_pipe_handle pipe, enum usbd_waitflg);
     94 Static void usbd_free_buffer_flag(usbd_xfer_handle, enum usbd_waitflg);
     95 Static usbd_status usbd_free_xfer_flag(usbd_xfer_handle, enum usbd_waitflg);
     96 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
     97 Static void usbd_callback_task(void *);
     98 Static void usbd_do_request_async_cb
     99 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
    100 Static void usbd_do_request_async_task(void *);
    101 Static void usbd_start_next(usbd_pipe_handle pipe);
    102 Static usbd_status usbd_open_pipe_ival
    103 	(usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
    104 
    105 int
    106 usbd_xfer_isread(usbd_xfer_handle xfer)
    107 {
    108 	if (xfer->rqflags & URQ_REQUEST)
    109 		return (xfer->request.bmRequestType & UT_READ);
    110 	else
    111 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
    112 			UE_DIR_IN);
    113 }
    114 
    115 #ifdef USB_DEBUG
    116 void
    117 usbd_dump_iface(struct usbd_interface *iface)
    118 {
    119 	printf("usbd_dump_iface: iface=%p\n", iface);
    120 	if (iface == NULL)
    121 		return;
    122 	printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
    123 	       iface->device, iface->idesc, iface->index, iface->altindex,
    124 	       iface->priv);
    125 }
    126 
    127 void
    128 usbd_dump_device(struct usbd_device *dev)
    129 {
    130 	printf("usbd_dump_device: dev=%p\n", dev);
    131 	if (dev == NULL)
    132 		return;
    133 	printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
    134 	printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
    135 	       "power=%d langid=%d\n",
    136 	       dev->address, dev->config, dev->depth, dev->speed,
    137 	       dev->self_powered, dev->power, dev->langid);
    138 }
    139 
    140 void
    141 usbd_dump_endpoint(struct usbd_endpoint *endp)
    142 {
    143 	printf("usbd_dump_endpoint: endp=%p\n", endp);
    144 	if (endp == NULL)
    145 		return;
    146 	printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
    147 	if (endp->edesc)
    148 		printf(" bEndpointAddress=0x%02x\n",
    149 		       endp->edesc->bEndpointAddress);
    150 }
    151 
    152 void
    153 usbd_dump_queue(usbd_pipe_handle pipe)
    154 {
    155 	usbd_xfer_handle xfer;
    156 
    157 	printf("usbd_dump_queue: pipe=%p\n", pipe);
    158 	SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
    159 		printf("  xfer=%p\n", xfer);
    160 	}
    161 }
    162 
    163 void
    164 usbd_dump_pipe(usbd_pipe_handle pipe)
    165 {
    166 	printf("usbd_dump_pipe: pipe=%p\n", pipe);
    167 	if (pipe == NULL)
    168 		return;
    169 	usbd_dump_iface(pipe->iface);
    170 	usbd_dump_device(pipe->device);
    171 	usbd_dump_endpoint(pipe->endpoint);
    172 	printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
    173 	       pipe->refcnt, pipe->running, pipe->aborting);
    174 	printf(" intrxfer=%p, repeat=%d, interval=%d\n",
    175 	       pipe->intrxfer, pipe->repeat, pipe->interval);
    176 }
    177 #endif
    178 
    179 usbd_status
    180 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
    181 	       u_int8_t flags, usbd_pipe_handle *pipe)
    182 {
    183 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    184 				    USBD_DEFAULT_INTERVAL));
    185 }
    186 
    187 usbd_status
    188 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
    189 		    u_int8_t flags, usbd_pipe_handle *pipe, int ival)
    190 {
    191 	usbd_pipe_handle p;
    192 	struct usbd_endpoint *ep;
    193 	usbd_status err;
    194 	int i;
    195 
    196 	DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
    197 		    iface, address, flags));
    198 
    199 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    200 		ep = &iface->endpoints[i];
    201 		if (ep->edesc == NULL)
    202 			return (USBD_IOERROR);
    203 		if (ep->edesc->bEndpointAddress == address)
    204 			goto found;
    205 	}
    206 	return (USBD_BAD_ADDRESS);
    207  found:
    208 	if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
    209 		return (USBD_IN_USE);
    210 	err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
    211 	if (err)
    212 		return (err);
    213 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    214 	*pipe = p;
    215 	return (USBD_NORMAL_COMPLETION);
    216 }
    217 
    218 usbd_status
    219 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
    220 		    u_int8_t flags, usbd_pipe_handle *pipe,
    221 		    usbd_private_handle priv, void *buffer, u_int32_t len,
    222 		    usbd_callback cb, int ival)
    223 {
    224 	usbd_status err;
    225 	usbd_xfer_handle xfer;
    226 	usbd_pipe_handle ipipe;
    227 
    228 	DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
    229 		    address, flags, len));
    230 
    231 	err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
    232 				  &ipipe, ival);
    233 	if (err)
    234 		return (err);
    235 	xfer = usbd_alloc_xfer(iface->device, ipipe);
    236 	if (xfer == NULL) {
    237 		err = USBD_NOMEM;
    238 		goto bad1;
    239 	}
    240 	usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
    241 	    USBD_NO_TIMEOUT, cb);
    242 	ipipe->intrxfer = xfer;
    243 	ipipe->repeat = 1;
    244 	err = usbd_transfer(xfer);
    245 	*pipe = ipipe;
    246 	if (err != USBD_IN_PROGRESS && err)
    247 		goto bad2;
    248 	return (USBD_NORMAL_COMPLETION);
    249 
    250  bad2:
    251 	ipipe->intrxfer = NULL;
    252 	ipipe->repeat = 0;
    253 	usbd_free_xfer(xfer);
    254  bad1:
    255 	usbd_close_pipe(ipipe);
    256 	return (err);
    257 }
    258 
    259 usbd_status
    260 usbd_close_pipe(usbd_pipe_handle pipe)
    261 {
    262 #ifdef DIAGNOSTIC
    263 	if (pipe == NULL) {
    264 		printf("usbd_close_pipe: pipe==NULL\n");
    265 		return (USBD_NORMAL_COMPLETION);
    266 	}
    267 #endif
    268 
    269 	if (--pipe->refcnt != 0)
    270 		return (USBD_NORMAL_COMPLETION);
    271 	if (! SIMPLEQ_EMPTY(&pipe->queue))
    272 		return (USBD_PENDING_REQUESTS);
    273 	LIST_REMOVE(pipe, next);
    274 	pipe->endpoint->refcnt--;
    275 	pipe->methods->close(pipe);
    276 	if (pipe->intrxfer != NULL)
    277 		usbd_free_xfer(pipe->intrxfer);
    278 	free(pipe, M_USB);
    279 	return (USBD_NORMAL_COMPLETION);
    280 }
    281 
    282 usbd_status
    283 usbd_transfer(usbd_xfer_handle xfer)
    284 {
    285 	usbd_pipe_handle pipe = xfer->pipe;
    286 	usbd_status err;
    287 	u_int size;
    288 	int s;
    289 
    290 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
    291 		    xfer, xfer->flags, pipe, pipe->running));
    292 #ifdef USB_DEBUG
    293 	if (usbdebug > 5)
    294 		usbd_dump_queue(pipe);
    295 #endif
    296 	xfer->done = 0;
    297 
    298 	if (pipe->aborting)
    299 		return (USBD_CANCELLED);
    300 
    301 	size = xfer->length;
    302 	/* If there is no buffer, allocate one. */
    303 	if (!(xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) && size != 0) {
    304 		struct usbd_bus *bus = pipe->device->bus;
    305 
    306 #ifdef DIAGNOSTIC
    307 		if (xfer->rqflags & URQ_AUTO_BUF)
    308 			printf("usbd_transfer: has old buffer!\n");
    309 		if (xfer->device->bus->intr_context)
    310 			panic("usbd_transfer: allocm not in process context");
    311 #endif
    312 		err = bus->methods->allocm(bus, xfer,
    313 		    (xfer->flags & USBD_NO_COPY) ? xfer->buffer : NULL,
    314 		    size);
    315 		if (err)
    316 			return (err);
    317 		xfer->rqflags |= URQ_AUTO_BUF;
    318 	}
    319 #ifdef DIAGNOSTIC
    320 	if ((xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) != 0 &&
    321 	    size > xfer->bufsize) {
    322 		panic("usbd_transfer: buffer size overrun size=%u bufsize=%lu rqflags=%x",
    323 		    size, (unsigned long)xfer->bufsize, xfer->rqflags);
    324 	}
    325 #endif
    326 
    327 #ifdef DIAGNOSTIC
    328 	if ((xfer->flags & USBD_NO_COPY) == 0 && size != 0) {
    329 		if (xfer->rqflags & URQ_DEV_MAP_BUFFER)
    330 			printf("usbd_transfer: USBD_NO_COPY recommended with mapped buffer");
    331 		if (xfer->rqflags & URQ_DEV_MAP_MBUF)
    332 			panic("usbd_transfer: USBD_NO_COPY required with mapped mbuf");
    333 	}
    334 #endif
    335 	/* Copy data if going out. */
    336 	if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
    337 	    !usbd_xfer_isread(xfer) && xfer->buffer != xfer->hcbuffer) {
    338 		DPRINTFN(5, ("usbd_transfer: copy %p (alloc) <- %p (buffer), %u bytes\n",
    339 			     xfer->hcbuffer, xfer->buffer, (unsigned)size));
    340 		memcpy(xfer->hcbuffer, xfer->buffer, size);
    341 	}
    342 
    343 	err = pipe->methods->transfer(xfer);
    344 
    345 	if (err != USBD_IN_PROGRESS && err) {
    346 		/* The transfer has not been queued, so free buffer. */
    347 		if (xfer->rqflags & URQ_AUTO_BUF) {
    348 			struct usbd_bus *bus = pipe->device->bus;
    349 
    350 #ifdef DIAGNOSTIC
    351 			if (bus->intr_context)
    352 				panic("usbd_transfer: freem not in process context");
    353 #endif
    354 			bus->methods->freem(bus, xfer, U_WAITOK);
    355 			xfer->rqflags &= ~URQ_AUTO_BUF;
    356 		}
    357 	}
    358 
    359 	if (!(xfer->flags & USBD_SYNCHRONOUS))
    360 		return (xfer->done ? 0 : USBD_IN_PROGRESS);
    361 
    362 	/* Sync transfer, wait for completion. */
    363 	s = splusb();
    364 	while (!xfer->done) {
    365 		if (pipe->device->bus->use_polling)
    366 			panic("usbd_transfer: not done");
    367 		tsleep(xfer, PRIBIO, "usbsyn", 0);
    368 	}
    369 	splx(s);
    370 	return (xfer->status);
    371 }
    372 
    373 /* Like usbd_transfer(), but waits for completion. */
    374 usbd_status
    375 usbd_sync_transfer(usbd_xfer_handle xfer)
    376 {
    377 	xfer->flags |= USBD_SYNCHRONOUS;
    378 	return (usbd_transfer(xfer));
    379 }
    380 
    381 void *
    382 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
    383 {
    384 	struct usbd_bus *bus = xfer->device->bus;
    385 	usbd_status err;
    386 
    387 	USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == 0,
    388 	    ("usbd_alloc_buffer: xfer already has a buffer"));
    389 
    390 #ifdef DIAGNOSTIC
    391 	if (xfer->device->bus->intr_context)
    392 		panic("usbd_alloc_buffer: allocm not in process context");
    393 #endif
    394 	err = bus->methods->allocm(bus, xfer, NULL, size);
    395 	if (err)
    396 		return (NULL);
    397 	xfer->bufsize = size;
    398 	xfer->rqflags |= URQ_DEV_BUF;
    399 	return xfer->hcbuffer;
    400 }
    401 
    402 Static void
    403 usbd_free_buffer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
    404 {
    405 	struct usbd_bus *bus;
    406 
    407 	USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == URQ_DEV_BUF,
    408 	    ("usbd_free_buffer_flag: no/auto buffer"));
    409 
    410 	xfer->rqflags &= ~(URQ_DEV_BUF | URQ_AUTO_BUF);
    411 	xfer->bufsize = 0;
    412 	bus = xfer->device->bus;
    413 #ifdef DIAGNOSTIC
    414 	if (waitflg == U_WAITOK && bus->intr_context)
    415 		panic("usbd_free_buffer: freem not in process context");
    416 #endif
    417 	bus->methods->freem(bus, xfer, waitflg);
    418 
    419 	xfer->hcbuffer = NULL;
    420 }
    421 
    422 void
    423 usbd_free_buffer(usbd_xfer_handle xfer)
    424 {
    425 	usbd_free_buffer_flag(xfer, U_WAITOK);
    426 }
    427 
    428 void *
    429 usbd_get_buffer(usbd_xfer_handle xfer)
    430 {
    431 	if (!(xfer->rqflags & URQ_DEV_BUF))
    432 		return (NULL);
    433 	return (xfer->hcbuffer);
    434 }
    435 
    436 /*
    437  * map buffer handlings
    438  */
    439 
    440 /* Allocate mapping resources to prepare buffer mapping. */
    441 usbd_status
    442 usbd_map_alloc(usbd_xfer_handle xfer)
    443 {
    444 	struct usbd_bus *bus = xfer->device->bus;
    445 	usbd_status err;
    446 
    447 #ifdef DIAGNOSTIC
    448 	if (bus->intr_context)
    449 		panic("usbd_map_alloc: map_alloc not in process context");
    450 #endif
    451 	USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0,
    452 	    ("usbd_map_alloc: map already allocated"));
    453 
    454 	err = bus->methods->map_alloc(xfer);
    455 	if (err != USBD_NORMAL_COMPLETION)
    456 		return (err);
    457 
    458 	xfer->rqflags |= URQ_DEV_MAP_PREPARED;
    459 
    460 	return (USBD_NORMAL_COMPLETION);
    461 }
    462 
    463 /* Free mapping resources. */
    464 void
    465 usbd_map_free(usbd_xfer_handle xfer)
    466 {
    467 	struct usbd_bus *bus = xfer->device->bus;
    468 
    469 #ifdef DIAGNOSTIC
    470 	if (bus->intr_context)
    471 		panic("usbd_map_free: map_free not in process context");
    472 #endif
    473 	USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED),
    474 	    ("usbd_map_free: map not allocated"));
    475 
    476 	bus->methods->map_free(xfer);
    477 
    478 	xfer->rqflags &= ~URQ_DEV_MAP_PREPARED;
    479 }
    480 
    481 /* Map plain buffer. */
    482 void
    483 usbd_map_buffer(usbd_xfer_handle xfer, void *buf, u_int32_t size)
    484 {
    485 	struct usbd_bus *bus = xfer->device->bus;
    486 
    487 #ifdef DIAGNOSTIC
    488 	if ((xfer->rqflags & URQ_MASK_BUF))
    489 		panic("usbd_map_buffer: already has buffer (rqflags=%x)",
    490 		    xfer->rqflags);
    491 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    492 		panic("usbd_map_buffer: mapm (without map_alloc) not in process context");
    493 #endif
    494 
    495 	bus->methods->mapm(xfer, buf, size);
    496 
    497 	xfer->bufsize = size;
    498 	xfer->rqflags |= URQ_DEV_MAP_BUFFER;
    499 }
    500 
    501 /* Map mbuf(9) chain. */
    502 usbd_status
    503 usbd_map_buffer_mbuf(usbd_xfer_handle xfer, struct mbuf *chain)
    504 {
    505 	struct usbd_bus *bus = xfer->device->bus;
    506 	usbd_status err;
    507 
    508 #ifdef DIAGNOSTIC
    509 	if ((xfer->rqflags & URQ_MASK_BUF))
    510 		panic("usbd_map_buffer_mbuf: already has buffer (rqflags=%x)",
    511 		    xfer->rqflags);
    512 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    513 		panic("usbd_map_buffer_mbuf: mapm_mbuf (without map_alloc) not in process context");
    514 #endif
    515 
    516 	err = bus->methods->mapm_mbuf(xfer, chain);
    517 	if (err != USBD_NORMAL_COMPLETION)
    518 		return (err);
    519 
    520 	xfer->bufsize = chain->m_pkthdr.len;
    521 	xfer->rqflags |= URQ_DEV_MAP_MBUF;
    522 
    523 	return (err);
    524 }
    525 
    526 /* Unmap plain buffer or mbuf(9) chain. */
    527 void
    528 usbd_unmap_buffer(usbd_xfer_handle xfer)
    529 {
    530 	struct usbd_bus *bus = xfer->device->bus;
    531 
    532 	USB_KASSERT2((xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF)),
    533 	    ("usbd_unmap_buffer: no map"));
    534 #ifdef DIAGNOSTIC
    535 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    536 		panic("usbd_unmap_buffer: unmapm (without map_alloc) not in process context");
    537 #endif
    538 
    539 	bus->methods->unmapm(xfer);
    540 
    541 	xfer->rqflags &= ~(URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF);
    542 	xfer->bufsize = 0;
    543 }
    544 
    545 Static usbd_xfer_handle
    546 usbd_alloc_xfer_flag(usbd_device_handle dev, usbd_pipe_handle pipe,
    547 	enum usbd_waitflg waitflg)
    548 {
    549 	usbd_xfer_handle xfer;
    550 
    551 #ifdef DIAGNOSTIC
    552 	if (waitflg == U_WAITOK && dev->bus->intr_context)
    553 		panic("usbd_alloc_xfer: allocx not in process context");
    554 	if (pipe == NULL)
    555 		panic("usbd_alloc_xfer: pipe == NULL");
    556 #endif
    557 	xfer = dev->bus->methods->allocx(dev->bus, pipe, waitflg);
    558 	if (xfer == NULL)
    559 		return (NULL);
    560 	xfer->pipe = pipe;
    561 	xfer->device = dev;
    562 	usb_callout_init(xfer->timeout_handle);
    563 	DPRINTFN(5,("usbd_alloc_xfer_flag() = %p\n", xfer));
    564 	return (xfer);
    565 }
    566 
    567 usbd_xfer_handle
    568 usbd_alloc_xfer(usbd_device_handle dev, usbd_pipe_handle pipe)
    569 {
    570 	return (usbd_alloc_xfer_flag(dev, pipe, U_WAITOK));
    571 }
    572 
    573 usbd_xfer_handle
    574 usbd_alloc_default_xfer(usbd_device_handle dev)
    575 {
    576 	return (usbd_alloc_xfer_flag(dev, dev->default_pipe, U_WAITOK));
    577 }
    578 
    579 Static usbd_status
    580 usbd_free_xfer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
    581 {
    582 	DPRINTFN(5,("usbd_free_xfer_flag: %p %d\n", xfer, waitflg));
    583 	usb_rem_task(xfer->device, &xfer->task);
    584 	if (xfer->rqflags & (URQ_DEV_BUF | URQ_AUTO_BUF))
    585 		usbd_free_buffer_flag(xfer, waitflg);
    586 	if (xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF))
    587 		usbd_unmap_buffer(xfer);
    588 	if (waitflg == U_WAITOK && (xfer->rqflags & URQ_DEV_MAP_PREPARED))
    589 		usbd_map_free(xfer);
    590 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
    591 	if (callout_pending(&xfer->timeout_handle)) {
    592 		callout_stop(&xfer->timeout_handle);
    593 		printf("usbd_free_xfer_flag: timout_handle pending");
    594 	}
    595 #endif
    596 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
    597 	return (USBD_NORMAL_COMPLETION);
    598 }
    599 
    600 usbd_status
    601 usbd_free_xfer(usbd_xfer_handle xfer)
    602 {
    603 
    604 	return (usbd_free_xfer_flag(xfer, U_WAITOK));
    605 }
    606 
    607 void
    608 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    609 		usbd_private_handle priv, void *buffer, u_int32_t length,
    610 		u_int16_t flags, u_int32_t timeout,
    611 		usbd_callback callback)
    612 {
    613 	USB_KASSERT(xfer->pipe == pipe);	/* XXX pipe arg should go */
    614 	xfer->priv = priv;
    615 	xfer->buffer = buffer;
    616 	xfer->length = length;
    617 	xfer->actlen = 0;
    618 	xfer->flags = flags;
    619 	xfer->timeout = timeout;
    620 	xfer->status = USBD_NOT_STARTED;
    621 	xfer->callback = callback;
    622 	xfer->rqflags &= ~URQ_REQUEST;
    623 	xfer->nframes = 0;
    624 }
    625 
    626 void
    627 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
    628 			usbd_private_handle priv, u_int32_t timeout,
    629 			usb_device_request_t *req, void *buffer,
    630 			u_int32_t length, u_int16_t flags,
    631 			usbd_callback callback)
    632 {
    633 	USB_KASSERT(xfer->pipe == dev->default_pipe);	/* XXX */
    634 	xfer->priv = priv;
    635 	xfer->buffer = buffer;
    636 	xfer->length = length;
    637 	xfer->actlen = 0;
    638 	xfer->flags = flags;
    639 	xfer->timeout = timeout;
    640 	xfer->status = USBD_NOT_STARTED;
    641 	xfer->callback = callback;
    642 	xfer->request = *req;
    643 	xfer->rqflags |= URQ_REQUEST;
    644 	xfer->nframes = 0;
    645 }
    646 
    647 void
    648 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    649 		     usbd_private_handle priv, u_int16_t *frlengths,
    650 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
    651 {
    652 	int i;
    653 
    654 	USB_KASSERT(xfer->pipe == pipe);	/* XXX pipe arg should go */
    655 	xfer->priv = priv;
    656 	xfer->buffer = 0;
    657 	xfer->length = 0;
    658 	for (i = 0; i < nframes; i++)
    659 		xfer->length += frlengths[i];
    660 	xfer->actlen = 0;
    661 	xfer->flags = flags;
    662 	xfer->timeout = USBD_NO_TIMEOUT;
    663 	xfer->status = USBD_NOT_STARTED;
    664 	xfer->callback = callback;
    665 	xfer->rqflags &= ~URQ_REQUEST;
    666 	xfer->frlengths = frlengths;
    667 	xfer->nframes = nframes;
    668 }
    669 
    670 void
    671 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
    672 		     void **buffer, u_int32_t *count, usbd_status *status)
    673 {
    674 	if (priv != NULL)
    675 		*priv = xfer->priv;
    676 	if (buffer != NULL)
    677 		*buffer = xfer->buffer;
    678 	if (count != NULL)
    679 		*count = xfer->actlen;
    680 	if (status != NULL)
    681 		*status = xfer->status;
    682 }
    683 
    684 usb_config_descriptor_t *
    685 usbd_get_config_descriptor(usbd_device_handle dev)
    686 {
    687 #ifdef DIAGNOSTIC
    688 	if (dev == NULL) {
    689 		printf("usbd_get_config_descriptor: dev == NULL\n");
    690 		return (NULL);
    691 	}
    692 #endif
    693 	return (dev->cdesc);
    694 }
    695 
    696 int
    697 usbd_get_speed(usbd_device_handle dev)
    698 {
    699 	return (dev->speed);
    700 }
    701 
    702 usb_interface_descriptor_t *
    703 usbd_get_interface_descriptor(usbd_interface_handle iface)
    704 {
    705 #ifdef DIAGNOSTIC
    706 	if (iface == NULL) {
    707 		printf("usbd_get_interface_descriptor: dev == NULL\n");
    708 		return (NULL);
    709 	}
    710 #endif
    711 	return (iface->idesc);
    712 }
    713 
    714 usb_device_descriptor_t *
    715 usbd_get_device_descriptor(usbd_device_handle dev)
    716 {
    717 	return (&dev->ddesc);
    718 }
    719 
    720 usb_endpoint_descriptor_t *
    721 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
    722 {
    723 	if (index >= iface->idesc->bNumEndpoints)
    724 		return (0);
    725 	return (iface->endpoints[index].edesc);
    726 }
    727 
    728 usbd_status
    729 usbd_abort_pipe(usbd_pipe_handle pipe)
    730 {
    731 	usbd_status err;
    732 	int s;
    733 
    734 #ifdef DIAGNOSTIC
    735 	if (pipe == NULL) {
    736 		printf("usbd_abort_pipe: pipe==NULL\n");
    737 		return (USBD_NORMAL_COMPLETION);
    738 	}
    739 #endif
    740 	s = splusb();
    741 	err = usbd_ar_pipe(pipe);
    742 	splx(s);
    743 	return (err);
    744 }
    745 
    746 usbd_status
    747 usbd_abort_default_pipe(usbd_device_handle dev)
    748 {
    749 	return (usbd_abort_pipe(dev->default_pipe));
    750 }
    751 
    752 usbd_status
    753 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
    754 {
    755 	usbd_device_handle dev = pipe->device;
    756 	usb_device_request_t req;
    757 	usbd_status err;
    758 
    759 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    760 
    761 	/*
    762 	 * Clearing en endpoint stall resets the endpoint toggle, so
    763 	 * do the same to the HC toggle.
    764 	 */
    765 	pipe->methods->cleartoggle(pipe);
    766 
    767 	req.bmRequestType = UT_WRITE_ENDPOINT;
    768 	req.bRequest = UR_CLEAR_FEATURE;
    769 	USETW(req.wValue, UF_ENDPOINT_HALT);
    770 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    771 	USETW(req.wLength, 0);
    772 	err = usbd_do_request(dev, &req, 0);
    773 #if 0
    774 XXX should we do this?
    775 	if (!err) {
    776 		pipe->state = USBD_PIPE_ACTIVE;
    777 		/* XXX activate pipe */
    778 	}
    779 #endif
    780 	return (err);
    781 }
    782 
    783 usbd_status
    784 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
    785 {
    786 	usbd_device_handle dev = pipe->device;
    787 	usb_device_request_t req;
    788 	usbd_status err;
    789 
    790 	pipe->methods->cleartoggle(pipe);
    791 
    792 	req.bmRequestType = UT_WRITE_ENDPOINT;
    793 	req.bRequest = UR_CLEAR_FEATURE;
    794 	USETW(req.wValue, UF_ENDPOINT_HALT);
    795 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    796 	USETW(req.wLength, 0);
    797 	err = usbd_do_request_async(dev, &req, 0);
    798 	return (err);
    799 }
    800 
    801 void
    802 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
    803 {
    804 	pipe->methods->cleartoggle(pipe);
    805 }
    806 
    807 usbd_status
    808 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
    809 {
    810 #ifdef DIAGNOSTIC
    811 	if (iface == NULL || iface->idesc == NULL) {
    812 		printf("usbd_endpoint_count: NULL pointer\n");
    813 		return (USBD_INVAL);
    814 	}
    815 #endif
    816 	*count = iface->idesc->bNumEndpoints;
    817 	return (USBD_NORMAL_COMPLETION);
    818 }
    819 
    820 usbd_status
    821 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
    822 {
    823 	if (dev->cdesc == NULL)
    824 		return (USBD_NOT_CONFIGURED);
    825 	*count = dev->cdesc->bNumInterface;
    826 	return (USBD_NORMAL_COMPLETION);
    827 }
    828 
    829 void
    830 usbd_interface2device_handle(usbd_interface_handle iface,
    831 			     usbd_device_handle *dev)
    832 {
    833 	*dev = iface->device;
    834 }
    835 
    836 usbd_status
    837 usbd_device2interface_handle(usbd_device_handle dev,
    838 			     u_int8_t ifaceno, usbd_interface_handle *iface)
    839 {
    840 	if (dev->cdesc == NULL)
    841 		return (USBD_NOT_CONFIGURED);
    842 	if (ifaceno >= dev->cdesc->bNumInterface)
    843 		return (USBD_INVAL);
    844 	*iface = &dev->ifaces[ifaceno];
    845 	return (USBD_NORMAL_COMPLETION);
    846 }
    847 
    848 usbd_device_handle
    849 usbd_pipe2device_handle(usbd_pipe_handle pipe)
    850 {
    851 	return (pipe->device);
    852 }
    853 
    854 /* XXXX use altno */
    855 usbd_status
    856 usbd_set_interface(usbd_interface_handle iface, int altidx)
    857 {
    858 	usb_device_request_t req;
    859 	usbd_status err;
    860 	void *endpoints;
    861 
    862 	if (LIST_FIRST(&iface->pipes) != 0)
    863 		return (USBD_IN_USE);
    864 
    865 	endpoints = iface->endpoints;
    866 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
    867 	if (err)
    868 		return (err);
    869 
    870 	/* new setting works, we can free old endpoints */
    871 	if (endpoints != NULL)
    872 		free(endpoints, M_USB);
    873 
    874 #ifdef DIAGNOSTIC
    875 	if (iface->idesc == NULL) {
    876 		printf("usbd_set_interface: NULL pointer\n");
    877 		return (USBD_INVAL);
    878 	}
    879 #endif
    880 
    881 	req.bmRequestType = UT_WRITE_INTERFACE;
    882 	req.bRequest = UR_SET_INTERFACE;
    883 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    884 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    885 	USETW(req.wLength, 0);
    886 	return (usbd_do_request(iface->device, &req, 0));
    887 }
    888 
    889 int
    890 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    891 {
    892 	char *p = (char *)cdesc;
    893 	char *end = p + UGETW(cdesc->wTotalLength);
    894 	usb_interface_descriptor_t *d;
    895 	int n;
    896 
    897 	for (n = 0; p < end; p += d->bLength) {
    898 		d = (usb_interface_descriptor_t *)p;
    899 		if (p + d->bLength <= end &&
    900 		    d->bDescriptorType == UDESC_INTERFACE &&
    901 		    d->bInterfaceNumber == ifaceno)
    902 			n++;
    903 	}
    904 	return (n);
    905 }
    906 
    907 int
    908 usbd_get_interface_altindex(usbd_interface_handle iface)
    909 {
    910 	return (iface->altindex);
    911 }
    912 
    913 usbd_status
    914 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
    915 {
    916 	usb_device_request_t req;
    917 
    918 	req.bmRequestType = UT_READ_INTERFACE;
    919 	req.bRequest = UR_GET_INTERFACE;
    920 	USETW(req.wValue, 0);
    921 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    922 	USETW(req.wLength, 1);
    923 	return (usbd_do_request(iface->device, &req, aiface));
    924 }
    925 
    926 /*** Internal routines ***/
    927 
    928 /* Dequeue all pipe operations, called at splusb(). */
    929 Static usbd_status
    930 usbd_ar_pipe(usbd_pipe_handle pipe)
    931 {
    932 	usbd_xfer_handle xfer;
    933 
    934 	SPLUSBCHECK;
    935 
    936 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    937 #ifdef USB_DEBUG
    938 	if (usbdebug > 5)
    939 		usbd_dump_queue(pipe);
    940 #endif
    941 	pipe->repeat = 0;
    942 	pipe->aborting = 1;
    943 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
    944 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
    945 			    pipe, xfer, pipe->methods));
    946 		/* Make the HC abort it (and invoke the callback). */
    947 		pipe->methods->abort(xfer);
    948 		USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
    949 		    ("usbd_ar_pipe"));
    950 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    951 	}
    952 	pipe->aborting = 0;
    953 	return (USBD_NORMAL_COMPLETION);
    954 }
    955 
    956 /* Called at splusb() */
    957 void
    958 usb_transfer_complete(usbd_xfer_handle xfer)
    959 {
    960 	usbd_pipe_handle pipe = xfer->pipe;
    961 	int sync = xfer->flags & USBD_SYNCHRONOUS;
    962 	int erred = xfer->status == USBD_CANCELLED ||
    963 	    xfer->status == USBD_TIMEOUT;
    964 	int repeat, polling;
    965 
    966 	SPLUSBCHECK;
    967 
    968 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    969 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    970 #ifdef DIAGNOSTIC
    971 	if (xfer->busy_free != XFER_ONQU) {
    972 		printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
    973 		       xfer, xfer->busy_free);
    974 	}
    975 #endif
    976 
    977 #ifdef DIAGNOSTIC
    978 	if (pipe == NULL) {
    979 		printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
    980 		return;
    981 	}
    982 #endif
    983 	repeat = pipe->repeat;
    984 	polling = pipe->device->bus->use_polling;
    985 	/* XXXX */
    986 	if (polling)
    987 		pipe->running = 0;
    988 
    989 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    990 	    usbd_xfer_isread(xfer)) {
    991 #ifdef DIAGNOSTIC
    992 		if (xfer->actlen > xfer->length) {
    993 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    994 			       xfer->actlen, xfer->length);
    995 			xfer->actlen = xfer->length;
    996 		}
    997 #endif
    998 		/* Copy data if it is not already in the correct buffer. */
    999 		if (!(xfer->flags & USBD_NO_COPY) && xfer->hcbuffer != NULL) {
   1000 			DPRINTFN(5, ("usb_transfer_complete: copy %p (buffer) <- %p (alloc), %u bytes\n",
   1001 				     xfer->buffer, xfer->hcbuffer, (unsigned)xfer->actlen));
   1002 #ifdef DIAGNOSTIC
   1003 			if ((xfer->rqflags & (URQ_AUTO_BUF | URQ_DEV_BUF)) == 0)
   1004 				panic("usbd_transfer: USBD_NO_COPY required with mapped buffer");
   1005 #endif
   1006 			memcpy(xfer->buffer, xfer->hcbuffer, xfer->actlen);
   1007 		}
   1008 	}
   1009 
   1010 	/*
   1011 	 * if we allocated or mapped the buffer in usbd_transfer()
   1012 	 * we unmap it here.
   1013 	 */
   1014 	if (xfer->rqflags & URQ_AUTO_BUF) {
   1015 		if (!repeat) {
   1016 			struct usbd_bus *bus = pipe->device->bus;
   1017 			bus->methods->freem(bus, xfer, U_NOWAIT);
   1018 			xfer->rqflags &= ~URQ_AUTO_BUF;
   1019 		}
   1020 	}
   1021 
   1022 	if (!repeat) {
   1023 		/* Remove request from queue. */
   1024 #ifdef DIAGNOSTIC
   1025 		xfer->busy_free = XFER_BUSY;
   1026 #endif
   1027 		USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) == xfer,
   1028 		    ("usb_transfer_complete: bad dequeue"));
   1029 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
   1030 	}
   1031 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
   1032 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
   1033 
   1034 	/* Count completed transfers. */
   1035 	++pipe->device->bus->stats.uds_requests
   1036 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
   1037 
   1038 	xfer->done = 1;
   1039 	if (!xfer->status && xfer->actlen < xfer->length &&
   1040 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
   1041 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
   1042 			     xfer->actlen, xfer->length));
   1043 		xfer->status = USBD_SHORT_XFER;
   1044 	}
   1045 
   1046 	/*
   1047 	 * For repeat operations, call the callback first, as the xfer
   1048 	 * will not go away and the "done" method may modify it. Otherwise
   1049 	 * reverse the order in case the callback wants to free or reuse
   1050 	 * the xfer.
   1051 	 */
   1052 	if (repeat) {
   1053 		if (xfer->callback)
   1054 			xfer->callback(xfer, xfer->priv, xfer->status);
   1055 		pipe->methods->done(xfer);
   1056 	} else {
   1057 		pipe->methods->done(xfer);
   1058 		if (xfer->callback) {
   1059 			if (xfer->flags & USBD_CALLBACK_AS_TASK) {
   1060 				/*
   1061 				 * Callback as a task (in thread context).
   1062 				 */
   1063 				usb_init_task(&xfer->task, usbd_callback_task,
   1064 				    xfer);
   1065 				usb_add_task(xfer->device, &xfer->task,
   1066 				    USB_TASKQ_DRIVER);
   1067 			} else
   1068 				xfer->callback(xfer, xfer->priv, xfer->status);
   1069 		}
   1070 	}
   1071 
   1072 	if (sync && !polling)
   1073 		wakeup(xfer);
   1074 
   1075 	if (!repeat) {
   1076 		/* XXX should we stop the queue on all errors? */
   1077 		if (erred && pipe->iface != NULL)	/* not control pipe */
   1078 			pipe->running = 0;
   1079 		else
   1080 			usbd_start_next(pipe);
   1081 	}
   1082 }
   1083 
   1084 Static void
   1085 usbd_callback_task(void *arg)
   1086 {
   1087 	usbd_xfer_handle xfer = arg;
   1088 
   1089 	xfer->callback(xfer, xfer->priv, xfer->status);
   1090 }
   1091 
   1092 usbd_status
   1093 usb_insert_transfer(usbd_xfer_handle xfer)
   1094 {
   1095 	usbd_pipe_handle pipe = xfer->pipe;
   1096 	usbd_status err;
   1097 	int s;
   1098 
   1099 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
   1100 		    pipe, pipe->running, xfer->timeout));
   1101 #ifdef DIAGNOSTIC
   1102 	if (xfer->busy_free != XFER_BUSY) {
   1103 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
   1104 		       xfer, xfer->busy_free);
   1105 		return (USBD_INVAL);
   1106 	}
   1107 	xfer->busy_free = XFER_ONQU;
   1108 #endif
   1109 	s = splusb();
   1110 	USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
   1111 	    ("usb_insert_transfer"));
   1112 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
   1113 	if (pipe->running)
   1114 		err = USBD_IN_PROGRESS;
   1115 	else {
   1116 		pipe->running = 1;
   1117 		err = USBD_NORMAL_COMPLETION;
   1118 	}
   1119 	splx(s);
   1120 	return (err);
   1121 }
   1122 
   1123 /* Called at splusb() */
   1124 void
   1125 usbd_start_next(usbd_pipe_handle pipe)
   1126 {
   1127 	usbd_xfer_handle xfer;
   1128 	usbd_status err;
   1129 
   1130 	SPLUSBCHECK;
   1131 
   1132 #ifdef DIAGNOSTIC
   1133 	if (pipe == NULL) {
   1134 		printf("usbd_start_next: pipe == NULL\n");
   1135 		return;
   1136 	}
   1137 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
   1138 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
   1139 		return;
   1140 	}
   1141 #endif
   1142 
   1143 	/* Get next request in queue. */
   1144 	xfer = SIMPLEQ_FIRST(&pipe->queue);
   1145 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
   1146 	if (xfer == NULL) {
   1147 		pipe->running = 0;
   1148 	} else {
   1149 		err = pipe->methods->start(xfer);
   1150 		if (err != USBD_IN_PROGRESS) {
   1151 			printf("usbd_start_next: error=%d\n", err);
   1152 			pipe->running = 0;
   1153 			/* XXX do what? */
   1154 		}
   1155 	}
   1156 }
   1157 
   1158 usbd_status
   1159 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
   1160 {
   1161 	return (usbd_do_request_flags(dev, req, data, 0, 0,
   1162 				      USBD_DEFAULT_TIMEOUT));
   1163 }
   1164 
   1165 usbd_status
   1166 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
   1167 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
   1168 {
   1169 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
   1170 					   data, flags, actlen, timo));
   1171 }
   1172 
   1173 usbd_status
   1174 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
   1175 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
   1176 	u_int32_t timeout)
   1177 {
   1178 	usbd_xfer_handle xfer;
   1179 	usbd_status err;
   1180 
   1181 #ifdef DIAGNOSTIC
   1182 #if defined(__i386__) && defined(__FreeBSD__)
   1183 	KASSERT(curthread->td_intr_nesting_level == 0,
   1184 	       	("usbd_do_request: in interrupt context"));
   1185 #endif
   1186 	if (dev->bus->intr_context) {
   1187 		panic("usbd_do_request: not in process context");
   1188 	}
   1189 #endif
   1190 
   1191 	xfer = usbd_alloc_xfer(dev, pipe);
   1192 	if (xfer == NULL)
   1193 		return (USBD_NOMEM);
   1194 	xfer->pipe = dev->default_pipe;	/* XXX */
   1195 	/* XXX */
   1196 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
   1197 				data, UGETW(req->wLength), flags, 0);
   1198 	/* XXX */
   1199 	xfer->pipe = pipe;
   1200 	err = usbd_sync_transfer(xfer);
   1201 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1202 	if (xfer->actlen > xfer->length) {
   1203 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1204 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1205 			 dev->address, xfer->request.bmRequestType,
   1206 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1207 			 UGETW(xfer->request.wIndex),
   1208 			 UGETW(xfer->request.wLength),
   1209 			 xfer->length, xfer->actlen));
   1210 	}
   1211 #endif
   1212 	if (actlen != NULL)
   1213 		*actlen = xfer->actlen;
   1214 	if (err == USBD_STALLED) {
   1215 		/*
   1216 		 * The control endpoint has stalled.  Control endpoints
   1217 		 * should not halt, but some may do so anyway so clear
   1218 		 * any halt condition.
   1219 		 */
   1220 		usb_device_request_t treq;
   1221 		usb_status_t status;
   1222 		u_int16_t s;
   1223 		usbd_status nerr;
   1224 
   1225 		treq.bmRequestType = UT_READ_ENDPOINT;
   1226 		treq.bRequest = UR_GET_STATUS;
   1227 		USETW(treq.wValue, 0);
   1228 		USETW(treq.wIndex, 0);
   1229 		USETW(treq.wLength, sizeof(usb_status_t));
   1230 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1231 					   &treq, &status,sizeof(usb_status_t),
   1232 					   0, 0);
   1233 		nerr = usbd_sync_transfer(xfer);
   1234 		if (nerr)
   1235 			goto bad;
   1236 		s = UGETW(status.wStatus);
   1237 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
   1238 		if (!(s & UES_HALT))
   1239 			goto bad;
   1240 		treq.bmRequestType = UT_WRITE_ENDPOINT;
   1241 		treq.bRequest = UR_CLEAR_FEATURE;
   1242 		USETW(treq.wValue, UF_ENDPOINT_HALT);
   1243 		USETW(treq.wIndex, 0);
   1244 		USETW(treq.wLength, 0);
   1245 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1246 					   &treq, &status, 0, 0, 0);
   1247 		nerr = usbd_sync_transfer(xfer);
   1248 		if (nerr)
   1249 			goto bad;
   1250 	}
   1251 
   1252  bad:
   1253 	usbd_free_xfer(xfer);
   1254 	return (err);
   1255 }
   1256 
   1257 void
   1258 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1259     usbd_private_handle priv, usbd_status status)
   1260 {
   1261 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1262 	if (xfer->actlen > xfer->length) {
   1263 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1264 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1265 			 xfer->pipe->device->address,
   1266 			 xfer->request.bmRequestType,
   1267 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1268 			 UGETW(xfer->request.wIndex),
   1269 			 UGETW(xfer->request.wLength),
   1270 			 xfer->length, xfer->actlen));
   1271 	}
   1272 #endif
   1273 	usbd_free_xfer_flag(xfer, U_NOWAIT);
   1274 }
   1275 
   1276 /*
   1277  * Execute a request without waiting for completion.
   1278  * Can be used from interrupt context.
   1279  */
   1280 usbd_status
   1281 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1282 		      void *data)
   1283 {
   1284 	usbd_xfer_handle xfer;
   1285 
   1286 	xfer = usbd_alloc_xfer_flag(dev, dev->default_pipe, U_NOWAIT);
   1287 	if (xfer == NULL)
   1288 		return (USBD_NOMEM);
   1289 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1290 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1291 
   1292 	/*
   1293 	 * Request will be processed later as a task (in thread context).
   1294 	 */
   1295 	usb_init_task(&xfer->task, usbd_do_request_async_task, xfer);
   1296 	usb_add_task(dev, &xfer->task, USB_TASKQ_HC);
   1297 
   1298 	return (USBD_IN_PROGRESS);
   1299 }
   1300 
   1301 Static void
   1302 usbd_do_request_async_task(void *arg)
   1303 {
   1304 	usbd_xfer_handle xfer = arg;
   1305 	usbd_status err;
   1306 
   1307 	/* XXX impossible to notify caller the error */
   1308 	err = usbd_transfer(xfer);
   1309 	if (err != USBD_IN_PROGRESS && err) {
   1310 		usbd_free_xfer(xfer);
   1311 	}
   1312 }
   1313 
   1314 const struct usbd_quirks *
   1315 usbd_get_quirks(usbd_device_handle dev)
   1316 {
   1317 #ifdef DIAGNOSTIC
   1318 	if (dev == NULL) {
   1319 		printf("usbd_get_quirks: dev == NULL\n");
   1320 		return 0;
   1321 	}
   1322 #endif
   1323 	return (dev->quirks);
   1324 }
   1325 
   1326 /* XXX do periodic free() of free list */
   1327 
   1328 /*
   1329  * Called from keyboard driver when in polling mode.
   1330  */
   1331 void
   1332 usbd_dopoll(usbd_interface_handle iface)
   1333 {
   1334 	iface->device->bus->methods->do_poll(iface->device->bus);
   1335 }
   1336 
   1337 void
   1338 usbd_set_polling(usbd_device_handle dev, int on)
   1339 {
   1340 	if (on)
   1341 		dev->bus->use_polling++;
   1342 	else
   1343 		dev->bus->use_polling--;
   1344 	/* When polling we need to make sure there is nothing pending to do. */
   1345 	if (dev->bus->use_polling)
   1346 		dev->bus->methods->soft_intr(dev->bus);
   1347 }
   1348 
   1349 
   1350 usb_endpoint_descriptor_t *
   1351 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1352 {
   1353 	struct usbd_endpoint *ep;
   1354 	int i;
   1355 
   1356 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1357 		ep = &iface->endpoints[i];
   1358 		if (ep->edesc->bEndpointAddress == address)
   1359 			return (iface->endpoints[i].edesc);
   1360 	}
   1361 	return (0);
   1362 }
   1363 
   1364 /*
   1365  * usbd_ratecheck() can limit the number of error messages that occurs.
   1366  * When a device is unplugged it may take up to 0.25s for the hub driver
   1367  * to notice it.  If the driver continuosly tries to do I/O operations
   1368  * this can generate a large number of messages.
   1369  */
   1370 int
   1371 usbd_ratecheck(struct timeval *last)
   1372 {
   1373 #if defined(__NetBSD__)
   1374 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1375 
   1376 	return (ratecheck(last, &errinterval));
   1377 #elif defined(__FreeBSD__)
   1378 	if (last->tv_sec == time_second)
   1379 		return (0);
   1380 	last->tv_sec = time_second;
   1381 	return (1);
   1382 #endif
   1383 }
   1384 
   1385 /*
   1386  * Search for a vendor/product pair in an array.  The item size is
   1387  * given as an argument.
   1388  */
   1389 const struct usb_devno *
   1390 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1391 		 u_int16_t vendor, u_int16_t product)
   1392 {
   1393 	while (nentries-- > 0) {
   1394 		u_int16_t tproduct = tbl->ud_product;
   1395 		if (tbl->ud_vendor == vendor &&
   1396 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1397 			return (tbl);
   1398 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1399 	}
   1400 	return (NULL);
   1401 }
   1402 
   1403 
   1404 void
   1405 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1406 {
   1407 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1408 
   1409         iter->cur = (const uByte *)cd;
   1410         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1411 }
   1412 
   1413 const usb_descriptor_t *
   1414 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1415 {
   1416 	const usb_descriptor_t *desc;
   1417 
   1418 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1419 		if (iter->cur != iter->end)
   1420 			printf("usb_desc_iter_next: bad descriptor\n");
   1421 		return NULL;
   1422 	}
   1423 	desc = (const usb_descriptor_t *)iter->cur;
   1424 	if (desc->bLength == 0) {
   1425 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1426 		return NULL;
   1427 	}
   1428 	iter->cur += desc->bLength;
   1429 	if (iter->cur > iter->end) {
   1430 		printf("usb_desc_iter_next: descriptor length too large\n");
   1431 		return NULL;
   1432 	}
   1433 	return desc;
   1434 }
   1435 
   1436 usbd_status
   1437 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1438 {
   1439 	return usbd_get_string0(dev, si, buf, 1);
   1440 }
   1441 
   1442 usbd_status
   1443 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1444 {
   1445 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1446 	usb_string_descriptor_t us;
   1447 	char *s;
   1448 	int i, n;
   1449 	u_int16_t c;
   1450 	usbd_status err;
   1451 	int size;
   1452 
   1453 	buf[0] = '\0';
   1454 	if (si == 0)
   1455 		return (USBD_INVAL);
   1456 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1457 		return (USBD_STALLED);
   1458 	if (dev->langid == USBD_NOLANG) {
   1459 		/* Set up default language */
   1460 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1461 		    &size);
   1462 		if (err || size < 4) {
   1463 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1464 			dev->langid = 0; /* Well, just pick something then */
   1465 		} else {
   1466 			/* Pick the first language as the default. */
   1467 			dev->langid = UGETW(us.bString[0]);
   1468 		}
   1469 	}
   1470 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1471 	if (err)
   1472 		return (err);
   1473 	s = buf;
   1474 	n = size / 2 - 1;
   1475 	if (unicode) {
   1476 		for (i = 0; i < n; i++) {
   1477 			c = UGETW(us.bString[i]);
   1478 			if (swap)
   1479 				c = (c >> 8) | (c << 8);
   1480 			s += wput_utf8(s, 3, c);
   1481 		}
   1482 		*s++ = 0;
   1483 	}
   1484 #if defined(__NetBSD__) && defined(COMPAT_30)
   1485 	else {
   1486 		for (i = 0; i < n; i++) {
   1487 			c = UGETW(us.bString[i]);
   1488 			if (swap)
   1489 				c = (c >> 8) | (c << 8);
   1490 			*s++ = (c < 0x80) ? c : '?';
   1491 		}
   1492 		*s++ = 0;
   1493 	}
   1494 #endif
   1495 	return (USBD_NORMAL_COMPLETION);
   1496 }
   1497 
   1498 #if defined(__FreeBSD__)
   1499 int
   1500 usbd_driver_load(module_t mod, int what, void *arg)
   1501 {
   1502 	/* XXX should implement something like a function that removes all generic devices */
   1503 
   1504  	return (0);
   1505 }
   1506 
   1507 #endif
   1508