Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.119.12.5
      1 /*	$NetBSD: usbdi.c,v 1.119.12.5 2007/06/28 10:07:16 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.5 2007/06/28 10:07:16 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, flags;
    288 	int s;
    289 
    290 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%x, 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 	flags = xfer->flags;
    303 	/* If there is no buffer, allocate one. */
    304 	if (!(xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) && size != 0) {
    305 		struct usbd_bus *bus = pipe->device->bus;
    306 
    307 #ifdef DIAGNOSTIC
    308 		if (xfer->rqflags & URQ_AUTO_BUF)
    309 			printf("usbd_transfer: has old buffer!\n");
    310 		if (xfer->device->bus->intr_context)
    311 			panic("usbd_transfer: allocm not in process context");
    312 #endif
    313 		err = bus->methods->allocm(bus, xfer,
    314 		    (flags & USBD_NO_COPY) ? xfer->buffer : NULL,
    315 		    size);
    316 		if (err)
    317 			return (err);
    318 		xfer->rqflags |= URQ_AUTO_BUF;
    319 	}
    320 #ifdef DIAGNOSTIC
    321 	if ((xfer->rqflags & URQ_MASK_DRV_REQUESTED_BUF) != 0 &&
    322 	    size > xfer->bufsize) {
    323 		panic("usbd_transfer: buffer size overrun size=%u bufsize=%lu rqflags=%x",
    324 		    size, (unsigned long)xfer->bufsize, xfer->rqflags);
    325 	}
    326 #endif
    327 
    328 #ifdef DIAGNOSTIC
    329 	if ((flags & USBD_NO_COPY) == 0 && size != 0) {
    330 		if (xfer->rqflags & URQ_DEV_MAP_BUFFER)
    331 			printf("usbd_transfer: USBD_NO_COPY recommended with mapped buffer");
    332 		if (xfer->rqflags & URQ_DEV_MAP_MBUF)
    333 			panic("usbd_transfer: USBD_NO_COPY required with mapped mbuf");
    334 	}
    335 #endif
    336 	/* Copy data if going out. */
    337 	if (!(flags & USBD_NO_COPY) && size != 0 &&
    338 	    !usbd_xfer_isread(xfer) && xfer->buffer != xfer->hcbuffer) {
    339 		DPRINTFN(5, ("usbd_transfer: copy %p (alloc) <- %p (buffer), %u bytes\n",
    340 			     xfer->hcbuffer, xfer->buffer, (unsigned)size));
    341 		memcpy(xfer->hcbuffer, xfer->buffer, size);
    342 	}
    343 
    344 	err = pipe->methods->transfer(xfer);
    345 	/*
    346 	 * Note xfer may not be valid after non-error completion
    347 	 * of the transfer method unless synchronous.
    348 	 */
    349 
    350 	if (err != USBD_IN_PROGRESS && err) {
    351 		/* The transfer has not been queued, so free buffer. */
    352 		if (xfer->rqflags & URQ_AUTO_BUF) {
    353 			struct usbd_bus *bus = pipe->device->bus;
    354 
    355 #ifdef DIAGNOSTIC
    356 			if (bus->intr_context)
    357 				panic("usbd_transfer: freem not in process context");
    358 #endif
    359 			bus->methods->freem(bus, xfer, U_WAITOK);
    360 			xfer->rqflags &= ~URQ_AUTO_BUF;
    361 		}
    362 	}
    363 
    364 	if (!(flags & USBD_SYNCHRONOUS))
    365 		return (xfer->done ? 0 : USBD_IN_PROGRESS);
    366 
    367 	/* Sync transfer, wait for completion. */
    368 	s = splusb();
    369 	while (!xfer->done) {
    370 		if (pipe->device->bus->use_polling)
    371 			panic("usbd_transfer: not done");
    372 		tsleep(xfer, PRIBIO, "usbsyn", 0);
    373 	}
    374 	splx(s);
    375 	return (xfer->status);
    376 }
    377 
    378 /* Like usbd_transfer(), but waits for completion. */
    379 usbd_status
    380 usbd_sync_transfer(usbd_xfer_handle xfer)
    381 {
    382 	xfer->flags |= USBD_SYNCHRONOUS;
    383 	return (usbd_transfer(xfer));
    384 }
    385 
    386 void *
    387 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
    388 {
    389 	struct usbd_bus *bus = xfer->device->bus;
    390 	usbd_status err;
    391 
    392 	USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == 0,
    393 	    ("usbd_alloc_buffer: xfer already has a buffer"));
    394 
    395 #ifdef DIAGNOSTIC
    396 	if (xfer->device->bus->intr_context)
    397 		panic("usbd_alloc_buffer: allocm not in process context");
    398 #endif
    399 	err = bus->methods->allocm(bus, xfer, NULL, size);
    400 	if (err)
    401 		return (NULL);
    402 	xfer->bufsize = size;
    403 	xfer->rqflags |= URQ_DEV_BUF;
    404 	return xfer->hcbuffer;
    405 }
    406 
    407 Static void
    408 usbd_free_buffer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
    409 {
    410 	struct usbd_bus *bus;
    411 
    412 	USB_KASSERT2((xfer->rqflags & URQ_MASK_BUF) == URQ_DEV_BUF,
    413 	    ("usbd_free_buffer_flag: no/auto buffer"));
    414 
    415 	xfer->rqflags &= ~(URQ_DEV_BUF | URQ_AUTO_BUF);
    416 	xfer->bufsize = 0;
    417 	bus = xfer->device->bus;
    418 #ifdef DIAGNOSTIC
    419 	if (waitflg == U_WAITOK && bus->intr_context)
    420 		panic("usbd_free_buffer: freem not in process context");
    421 #endif
    422 	bus->methods->freem(bus, xfer, waitflg);
    423 
    424 	xfer->hcbuffer = NULL;
    425 }
    426 
    427 void
    428 usbd_free_buffer(usbd_xfer_handle xfer)
    429 {
    430 	usbd_free_buffer_flag(xfer, U_WAITOK);
    431 }
    432 
    433 void *
    434 usbd_get_buffer(usbd_xfer_handle xfer)
    435 {
    436 	if (!(xfer->rqflags & URQ_DEV_BUF))
    437 		return (NULL);
    438 	return (xfer->hcbuffer);
    439 }
    440 
    441 /*
    442  * map buffer handlings
    443  */
    444 
    445 /* Allocate mapping resources to prepare buffer mapping. */
    446 usbd_status
    447 usbd_map_alloc(usbd_xfer_handle xfer)
    448 {
    449 	struct usbd_bus *bus = xfer->device->bus;
    450 	usbd_status err;
    451 
    452 #ifdef DIAGNOSTIC
    453 	if (bus->intr_context)
    454 		panic("usbd_map_alloc: map_alloc not in process context");
    455 #endif
    456 	USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0,
    457 	    ("usbd_map_alloc: map already allocated"));
    458 
    459 	err = bus->methods->map_alloc(xfer);
    460 	if (err != USBD_NORMAL_COMPLETION)
    461 		return (err);
    462 
    463 	xfer->rqflags |= URQ_DEV_MAP_PREPARED;
    464 
    465 	return (USBD_NORMAL_COMPLETION);
    466 }
    467 
    468 /* Free mapping resources. */
    469 void
    470 usbd_map_free(usbd_xfer_handle xfer)
    471 {
    472 	struct usbd_bus *bus = xfer->device->bus;
    473 
    474 #ifdef DIAGNOSTIC
    475 	if (bus->intr_context)
    476 		panic("usbd_map_free: map_free not in process context");
    477 #endif
    478 	USB_KASSERT2((xfer->rqflags & URQ_DEV_MAP_PREPARED),
    479 	    ("usbd_map_free: map not allocated"));
    480 
    481 	bus->methods->map_free(xfer);
    482 
    483 	xfer->rqflags &= ~URQ_DEV_MAP_PREPARED;
    484 }
    485 
    486 /* Map plain buffer. */
    487 void
    488 usbd_map_buffer(usbd_xfer_handle xfer, void *buf, u_int32_t size)
    489 {
    490 	struct usbd_bus *bus = xfer->device->bus;
    491 
    492 #ifdef DIAGNOSTIC
    493 	if ((xfer->rqflags & URQ_MASK_BUF))
    494 		panic("usbd_map_buffer: already has buffer (rqflags=%x)",
    495 		    xfer->rqflags);
    496 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    497 		panic("usbd_map_buffer: mapm (without map_alloc) not in process context");
    498 #endif
    499 
    500 	bus->methods->mapm(xfer, buf, size);
    501 
    502 	xfer->bufsize = size;
    503 	xfer->rqflags |= URQ_DEV_MAP_BUFFER;
    504 }
    505 
    506 /* Map mbuf(9) chain. */
    507 usbd_status
    508 usbd_map_buffer_mbuf(usbd_xfer_handle xfer, struct mbuf *chain)
    509 {
    510 	struct usbd_bus *bus = xfer->device->bus;
    511 	usbd_status err;
    512 
    513 #ifdef DIAGNOSTIC
    514 	if ((xfer->rqflags & URQ_MASK_BUF))
    515 		panic("usbd_map_buffer_mbuf: already has buffer (rqflags=%x)",
    516 		    xfer->rqflags);
    517 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    518 		panic("usbd_map_buffer_mbuf: mapm_mbuf (without map_alloc) not in process context");
    519 #endif
    520 
    521 	err = bus->methods->mapm_mbuf(xfer, chain);
    522 	if (err != USBD_NORMAL_COMPLETION)
    523 		return (err);
    524 
    525 	xfer->bufsize = chain->m_pkthdr.len;
    526 	xfer->rqflags |= URQ_DEV_MAP_MBUF;
    527 
    528 	return (err);
    529 }
    530 
    531 /* Unmap plain buffer or mbuf(9) chain. */
    532 void
    533 usbd_unmap_buffer(usbd_xfer_handle xfer)
    534 {
    535 	struct usbd_bus *bus = xfer->device->bus;
    536 
    537 	USB_KASSERT2((xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF)),
    538 	    ("usbd_unmap_buffer: no map"));
    539 #ifdef DIAGNOSTIC
    540 	if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 && bus->intr_context)
    541 		panic("usbd_unmap_buffer: unmapm (without map_alloc) not in process context");
    542 #endif
    543 
    544 	bus->methods->unmapm(xfer);
    545 
    546 	xfer->rqflags &= ~(URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF);
    547 	xfer->bufsize = 0;
    548 }
    549 
    550 Static usbd_xfer_handle
    551 usbd_alloc_xfer_flag(usbd_device_handle dev, usbd_pipe_handle pipe,
    552 	enum usbd_waitflg waitflg)
    553 {
    554 	usbd_xfer_handle xfer;
    555 
    556 #ifdef DIAGNOSTIC
    557 	if (waitflg == U_WAITOK && dev->bus->intr_context)
    558 		panic("usbd_alloc_xfer: allocx not in process context");
    559 	if (pipe == NULL)
    560 		panic("usbd_alloc_xfer: pipe == NULL");
    561 #endif
    562 	xfer = dev->bus->methods->allocx(dev->bus, pipe, waitflg);
    563 	if (xfer == NULL)
    564 		return (NULL);
    565 	xfer->pipe = pipe;
    566 	xfer->device = dev;
    567 	usb_callout_init(xfer->timeout_handle);
    568 	DPRINTFN(5,("usbd_alloc_xfer_flag() = %p\n", xfer));
    569 	return (xfer);
    570 }
    571 
    572 usbd_xfer_handle
    573 usbd_alloc_xfer(usbd_device_handle dev, usbd_pipe_handle pipe)
    574 {
    575 	return (usbd_alloc_xfer_flag(dev, pipe, U_WAITOK));
    576 }
    577 
    578 usbd_xfer_handle
    579 usbd_alloc_default_xfer(usbd_device_handle dev)
    580 {
    581 	return (usbd_alloc_xfer_flag(dev, dev->default_pipe, U_WAITOK));
    582 }
    583 
    584 Static usbd_status
    585 usbd_free_xfer_flag(usbd_xfer_handle xfer, enum usbd_waitflg waitflg)
    586 {
    587 	DPRINTFN(5,("usbd_free_xfer_flag: %p %d\n", xfer, waitflg));
    588 	usb_rem_task(xfer->device, &xfer->task);
    589 	if (xfer->rqflags & (URQ_DEV_BUF | URQ_AUTO_BUF))
    590 		usbd_free_buffer_flag(xfer, waitflg);
    591 	if (xfer->rqflags & (URQ_DEV_MAP_BUFFER | URQ_DEV_MAP_MBUF))
    592 		usbd_unmap_buffer(xfer);
    593 	if (waitflg == U_WAITOK && (xfer->rqflags & URQ_DEV_MAP_PREPARED))
    594 		usbd_map_free(xfer);
    595 #if defined(__NetBSD__) && defined(DIAGNOSTIC)
    596 	if (callout_pending(&xfer->timeout_handle)) {
    597 		callout_stop(&xfer->timeout_handle);
    598 		printf("usbd_free_xfer_flag: timout_handle pending");
    599 	}
    600 #endif
    601 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
    602 	return (USBD_NORMAL_COMPLETION);
    603 }
    604 
    605 usbd_status
    606 usbd_free_xfer(usbd_xfer_handle xfer)
    607 {
    608 
    609 	return (usbd_free_xfer_flag(xfer, U_WAITOK));
    610 }
    611 
    612 void
    613 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    614 		usbd_private_handle priv, void *buffer, u_int32_t length,
    615 		u_int16_t flags, u_int32_t timeout,
    616 		usbd_callback callback)
    617 {
    618 	USB_KASSERT(xfer->pipe == pipe);	/* XXX pipe arg should go */
    619 	xfer->priv = priv;
    620 	xfer->buffer = buffer;
    621 	xfer->length = length;
    622 	xfer->actlen = 0;
    623 	xfer->flags = flags;
    624 	xfer->timeout = timeout;
    625 	xfer->status = USBD_NOT_STARTED;
    626 	xfer->callback = callback;
    627 	xfer->rqflags &= ~URQ_REQUEST;
    628 	xfer->nframes = 0;
    629 }
    630 
    631 void
    632 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
    633 			usbd_private_handle priv, u_int32_t timeout,
    634 			usb_device_request_t *req, void *buffer,
    635 			u_int32_t length, u_int16_t flags,
    636 			usbd_callback callback)
    637 {
    638 	USB_KASSERT(xfer->pipe == dev->default_pipe);	/* XXX */
    639 	xfer->priv = priv;
    640 	xfer->buffer = buffer;
    641 	xfer->length = length;
    642 	xfer->actlen = 0;
    643 	xfer->flags = flags;
    644 	xfer->timeout = timeout;
    645 	xfer->status = USBD_NOT_STARTED;
    646 	xfer->callback = callback;
    647 	xfer->request = *req;
    648 	xfer->rqflags |= URQ_REQUEST;
    649 	xfer->nframes = 0;
    650 }
    651 
    652 void
    653 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    654 		     usbd_private_handle priv, u_int16_t *frlengths,
    655 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
    656 {
    657 	int i;
    658 
    659 	USB_KASSERT(xfer->pipe == pipe);	/* XXX pipe arg should go */
    660 	xfer->priv = priv;
    661 	xfer->buffer = 0;
    662 	xfer->length = 0;
    663 	for (i = 0; i < nframes; i++)
    664 		xfer->length += frlengths[i];
    665 	xfer->actlen = 0;
    666 	xfer->flags = flags;
    667 	xfer->timeout = USBD_NO_TIMEOUT;
    668 	xfer->status = USBD_NOT_STARTED;
    669 	xfer->callback = callback;
    670 	xfer->rqflags &= ~URQ_REQUEST;
    671 	xfer->frlengths = frlengths;
    672 	xfer->nframes = nframes;
    673 }
    674 
    675 void
    676 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
    677 		     void **buffer, u_int32_t *count, usbd_status *status)
    678 {
    679 	if (priv != NULL)
    680 		*priv = xfer->priv;
    681 	if (buffer != NULL)
    682 		*buffer = xfer->buffer;
    683 	if (count != NULL)
    684 		*count = xfer->actlen;
    685 	if (status != NULL)
    686 		*status = xfer->status;
    687 }
    688 
    689 usb_config_descriptor_t *
    690 usbd_get_config_descriptor(usbd_device_handle dev)
    691 {
    692 #ifdef DIAGNOSTIC
    693 	if (dev == NULL) {
    694 		printf("usbd_get_config_descriptor: dev == NULL\n");
    695 		return (NULL);
    696 	}
    697 #endif
    698 	return (dev->cdesc);
    699 }
    700 
    701 int
    702 usbd_get_speed(usbd_device_handle dev)
    703 {
    704 	return (dev->speed);
    705 }
    706 
    707 usb_interface_descriptor_t *
    708 usbd_get_interface_descriptor(usbd_interface_handle iface)
    709 {
    710 #ifdef DIAGNOSTIC
    711 	if (iface == NULL) {
    712 		printf("usbd_get_interface_descriptor: dev == NULL\n");
    713 		return (NULL);
    714 	}
    715 #endif
    716 	return (iface->idesc);
    717 }
    718 
    719 usb_device_descriptor_t *
    720 usbd_get_device_descriptor(usbd_device_handle dev)
    721 {
    722 	return (&dev->ddesc);
    723 }
    724 
    725 usb_endpoint_descriptor_t *
    726 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
    727 {
    728 	if (index >= iface->idesc->bNumEndpoints)
    729 		return (0);
    730 	return (iface->endpoints[index].edesc);
    731 }
    732 
    733 usbd_status
    734 usbd_abort_pipe(usbd_pipe_handle pipe)
    735 {
    736 	usbd_status err;
    737 	int s;
    738 
    739 #ifdef DIAGNOSTIC
    740 	if (pipe == NULL) {
    741 		printf("usbd_abort_pipe: pipe==NULL\n");
    742 		return (USBD_NORMAL_COMPLETION);
    743 	}
    744 #endif
    745 	s = splusb();
    746 	err = usbd_ar_pipe(pipe);
    747 	splx(s);
    748 	return (err);
    749 }
    750 
    751 usbd_status
    752 usbd_abort_default_pipe(usbd_device_handle dev)
    753 {
    754 	return (usbd_abort_pipe(dev->default_pipe));
    755 }
    756 
    757 usbd_status
    758 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
    759 {
    760 	usbd_device_handle dev = pipe->device;
    761 	usb_device_request_t req;
    762 	usbd_status err;
    763 
    764 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    765 
    766 	/*
    767 	 * Clearing en endpoint stall resets the endpoint toggle, so
    768 	 * do the same to the HC toggle.
    769 	 */
    770 	pipe->methods->cleartoggle(pipe);
    771 
    772 	req.bmRequestType = UT_WRITE_ENDPOINT;
    773 	req.bRequest = UR_CLEAR_FEATURE;
    774 	USETW(req.wValue, UF_ENDPOINT_HALT);
    775 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    776 	USETW(req.wLength, 0);
    777 	err = usbd_do_request(dev, &req, 0);
    778 #if 0
    779 XXX should we do this?
    780 	if (!err) {
    781 		pipe->state = USBD_PIPE_ACTIVE;
    782 		/* XXX activate pipe */
    783 	}
    784 #endif
    785 	return (err);
    786 }
    787 
    788 usbd_status
    789 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
    790 {
    791 	usbd_device_handle dev = pipe->device;
    792 	usb_device_request_t req;
    793 	usbd_status err;
    794 
    795 	pipe->methods->cleartoggle(pipe);
    796 
    797 	req.bmRequestType = UT_WRITE_ENDPOINT;
    798 	req.bRequest = UR_CLEAR_FEATURE;
    799 	USETW(req.wValue, UF_ENDPOINT_HALT);
    800 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    801 	USETW(req.wLength, 0);
    802 	err = usbd_do_request_async(dev, &req, 0);
    803 	return (err);
    804 }
    805 
    806 void
    807 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
    808 {
    809 	pipe->methods->cleartoggle(pipe);
    810 }
    811 
    812 usbd_status
    813 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
    814 {
    815 #ifdef DIAGNOSTIC
    816 	if (iface == NULL || iface->idesc == NULL) {
    817 		printf("usbd_endpoint_count: NULL pointer\n");
    818 		return (USBD_INVAL);
    819 	}
    820 #endif
    821 	*count = iface->idesc->bNumEndpoints;
    822 	return (USBD_NORMAL_COMPLETION);
    823 }
    824 
    825 usbd_status
    826 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
    827 {
    828 	if (dev->cdesc == NULL)
    829 		return (USBD_NOT_CONFIGURED);
    830 	*count = dev->cdesc->bNumInterface;
    831 	return (USBD_NORMAL_COMPLETION);
    832 }
    833 
    834 void
    835 usbd_interface2device_handle(usbd_interface_handle iface,
    836 			     usbd_device_handle *dev)
    837 {
    838 	*dev = iface->device;
    839 }
    840 
    841 usbd_status
    842 usbd_device2interface_handle(usbd_device_handle dev,
    843 			     u_int8_t ifaceno, usbd_interface_handle *iface)
    844 {
    845 	if (dev->cdesc == NULL)
    846 		return (USBD_NOT_CONFIGURED);
    847 	if (ifaceno >= dev->cdesc->bNumInterface)
    848 		return (USBD_INVAL);
    849 	*iface = &dev->ifaces[ifaceno];
    850 	return (USBD_NORMAL_COMPLETION);
    851 }
    852 
    853 usbd_device_handle
    854 usbd_pipe2device_handle(usbd_pipe_handle pipe)
    855 {
    856 	return (pipe->device);
    857 }
    858 
    859 /* XXXX use altno */
    860 usbd_status
    861 usbd_set_interface(usbd_interface_handle iface, int altidx)
    862 {
    863 	usb_device_request_t req;
    864 	usbd_status err;
    865 	void *endpoints;
    866 
    867 	if (LIST_FIRST(&iface->pipes) != 0)
    868 		return (USBD_IN_USE);
    869 
    870 	endpoints = iface->endpoints;
    871 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
    872 	if (err)
    873 		return (err);
    874 
    875 	/* new setting works, we can free old endpoints */
    876 	if (endpoints != NULL)
    877 		free(endpoints, M_USB);
    878 
    879 #ifdef DIAGNOSTIC
    880 	if (iface->idesc == NULL) {
    881 		printf("usbd_set_interface: NULL pointer\n");
    882 		return (USBD_INVAL);
    883 	}
    884 #endif
    885 
    886 	req.bmRequestType = UT_WRITE_INTERFACE;
    887 	req.bRequest = UR_SET_INTERFACE;
    888 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    889 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    890 	USETW(req.wLength, 0);
    891 	return (usbd_do_request(iface->device, &req, 0));
    892 }
    893 
    894 int
    895 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    896 {
    897 	char *p = (char *)cdesc;
    898 	char *end = p + UGETW(cdesc->wTotalLength);
    899 	usb_interface_descriptor_t *d;
    900 	int n;
    901 
    902 	for (n = 0; p < end; p += d->bLength) {
    903 		d = (usb_interface_descriptor_t *)p;
    904 		if (p + d->bLength <= end &&
    905 		    d->bDescriptorType == UDESC_INTERFACE &&
    906 		    d->bInterfaceNumber == ifaceno)
    907 			n++;
    908 	}
    909 	return (n);
    910 }
    911 
    912 int
    913 usbd_get_interface_altindex(usbd_interface_handle iface)
    914 {
    915 	return (iface->altindex);
    916 }
    917 
    918 usbd_status
    919 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
    920 {
    921 	usb_device_request_t req;
    922 
    923 	req.bmRequestType = UT_READ_INTERFACE;
    924 	req.bRequest = UR_GET_INTERFACE;
    925 	USETW(req.wValue, 0);
    926 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    927 	USETW(req.wLength, 1);
    928 	return (usbd_do_request(iface->device, &req, aiface));
    929 }
    930 
    931 /*** Internal routines ***/
    932 
    933 /* Dequeue all pipe operations, called at splusb(). */
    934 Static usbd_status
    935 usbd_ar_pipe(usbd_pipe_handle pipe)
    936 {
    937 	usbd_xfer_handle xfer;
    938 
    939 	SPLUSBCHECK;
    940 
    941 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    942 #ifdef USB_DEBUG
    943 	if (usbdebug > 5)
    944 		usbd_dump_queue(pipe);
    945 #endif
    946 	pipe->repeat = 0;
    947 	pipe->aborting = 1;
    948 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
    949 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
    950 			    pipe, xfer, pipe->methods));
    951 		/* Make the HC abort it (and invoke the callback). */
    952 		pipe->methods->abort(xfer);
    953 		USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
    954 		    ("usbd_ar_pipe"));
    955 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    956 	}
    957 	pipe->aborting = 0;
    958 	return (USBD_NORMAL_COMPLETION);
    959 }
    960 
    961 /* Called at splusb() */
    962 void
    963 usb_transfer_complete(usbd_xfer_handle xfer)
    964 {
    965 	usbd_pipe_handle pipe = xfer->pipe;
    966 	int sync = xfer->flags & USBD_SYNCHRONOUS;
    967 	int erred = xfer->status == USBD_CANCELLED ||
    968 	    xfer->status == USBD_TIMEOUT;
    969 	int repeat, polling;
    970 
    971 	SPLUSBCHECK;
    972 
    973 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    974 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    975 #ifdef DIAGNOSTIC
    976 	if (xfer->busy_free != XFER_ONQU) {
    977 		printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
    978 		       xfer, xfer->busy_free);
    979 	}
    980 #endif
    981 
    982 #ifdef DIAGNOSTIC
    983 	if (pipe == NULL) {
    984 		printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer);
    985 		return;
    986 	}
    987 #endif
    988 	repeat = pipe->repeat;
    989 	polling = pipe->device->bus->use_polling;
    990 	/* XXXX */
    991 	if (polling)
    992 		pipe->running = 0;
    993 
    994 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    995 	    usbd_xfer_isread(xfer)) {
    996 #ifdef DIAGNOSTIC
    997 		if (xfer->actlen > xfer->length) {
    998 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    999 			       xfer->actlen, xfer->length);
   1000 			xfer->actlen = xfer->length;
   1001 		}
   1002 #endif
   1003 		/* Copy data if it is not already in the correct buffer. */
   1004 		if (!(xfer->flags & USBD_NO_COPY) && xfer->hcbuffer != NULL) {
   1005 			DPRINTFN(5, ("usb_transfer_complete: copy %p (buffer) <- %p (alloc), %u bytes\n",
   1006 				     xfer->buffer, xfer->hcbuffer, (unsigned)xfer->actlen));
   1007 #ifdef DIAGNOSTIC
   1008 			if ((xfer->rqflags & (URQ_AUTO_BUF | URQ_DEV_BUF)) == 0)
   1009 				panic("usb_transfer_complete: USBD_NO_COPY required with mapped buffer");
   1010 #endif
   1011 			memcpy(xfer->buffer, xfer->hcbuffer, xfer->actlen);
   1012 		}
   1013 	}
   1014 
   1015 	/*
   1016 	 * if we allocated or mapped the buffer in usbd_transfer()
   1017 	 * we unmap it here.
   1018 	 */
   1019 	if (xfer->rqflags & URQ_AUTO_BUF) {
   1020 		if (!repeat) {
   1021 			struct usbd_bus *bus = pipe->device->bus;
   1022 			bus->methods->freem(bus, xfer, U_NOWAIT);
   1023 			xfer->rqflags &= ~URQ_AUTO_BUF;
   1024 		}
   1025 	}
   1026 
   1027 	if (!repeat) {
   1028 		/* Remove request from queue. */
   1029 #ifdef DIAGNOSTIC
   1030 		xfer->busy_free = XFER_BUSY;
   1031 #endif
   1032 		USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) == xfer,
   1033 		    ("usb_transfer_complete: bad dequeue"));
   1034 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
   1035 	}
   1036 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
   1037 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
   1038 
   1039 	/* Count completed transfers. */
   1040 	++pipe->device->bus->stats.uds_requests
   1041 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
   1042 
   1043 	xfer->done = 1;
   1044 	if (!xfer->status && xfer->actlen < xfer->length &&
   1045 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
   1046 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
   1047 			     xfer->actlen, xfer->length));
   1048 		xfer->status = USBD_SHORT_XFER;
   1049 	}
   1050 
   1051 	/*
   1052 	 * For repeat operations, call the callback first, as the xfer
   1053 	 * will not go away and the "done" method may modify it. Otherwise
   1054 	 * reverse the order in case the callback wants to free or reuse
   1055 	 * the xfer.
   1056 	 */
   1057 	if (repeat) {
   1058 		if (xfer->callback)
   1059 			xfer->callback(xfer, xfer->priv, xfer->status);
   1060 		pipe->methods->done(xfer);
   1061 	} else {
   1062 		pipe->methods->done(xfer);
   1063 		if (xfer->callback) {
   1064 			if (xfer->flags & USBD_CALLBACK_AS_TASK) {
   1065 				/*
   1066 				 * Callback as a task (in thread context).
   1067 				 */
   1068 				usb_init_task(&xfer->task, usbd_callback_task,
   1069 				    xfer);
   1070 				usb_add_task(xfer->device, &xfer->task,
   1071 				    USB_TASKQ_DRIVER);
   1072 			} else
   1073 				xfer->callback(xfer, xfer->priv, xfer->status);
   1074 		}
   1075 	}
   1076 
   1077 	if (sync && !polling)
   1078 		wakeup(xfer);
   1079 
   1080 	if (!repeat) {
   1081 		/* XXX should we stop the queue on all errors? */
   1082 		if (erred && pipe->iface != NULL)	/* not control pipe */
   1083 			pipe->running = 0;
   1084 		else
   1085 			usbd_start_next(pipe);
   1086 	}
   1087 }
   1088 
   1089 Static void
   1090 usbd_callback_task(void *arg)
   1091 {
   1092 	usbd_xfer_handle xfer = arg;
   1093 
   1094 	xfer->callback(xfer, xfer->priv, xfer->status);
   1095 }
   1096 
   1097 usbd_status
   1098 usb_insert_transfer(usbd_xfer_handle xfer)
   1099 {
   1100 	usbd_pipe_handle pipe = xfer->pipe;
   1101 	usbd_status err;
   1102 	int s;
   1103 
   1104 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
   1105 		    pipe, pipe->running, xfer->timeout));
   1106 #ifdef DIAGNOSTIC
   1107 	if (xfer->busy_free != XFER_BUSY) {
   1108 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
   1109 		       xfer, xfer->busy_free);
   1110 		return (USBD_INVAL);
   1111 	}
   1112 	xfer->busy_free = XFER_ONQU;
   1113 #endif
   1114 	s = splusb();
   1115 	USB_KASSERT2(SIMPLEQ_FIRST(&pipe->queue) != xfer,
   1116 	    ("usb_insert_transfer"));
   1117 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
   1118 	if (pipe->running)
   1119 		err = USBD_IN_PROGRESS;
   1120 	else {
   1121 		pipe->running = 1;
   1122 		err = USBD_NORMAL_COMPLETION;
   1123 	}
   1124 	splx(s);
   1125 	return (err);
   1126 }
   1127 
   1128 /* Called at splusb() */
   1129 void
   1130 usbd_start_next(usbd_pipe_handle pipe)
   1131 {
   1132 	usbd_xfer_handle xfer;
   1133 	usbd_status err;
   1134 
   1135 	SPLUSBCHECK;
   1136 
   1137 #ifdef DIAGNOSTIC
   1138 	if (pipe == NULL) {
   1139 		printf("usbd_start_next: pipe == NULL\n");
   1140 		return;
   1141 	}
   1142 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
   1143 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
   1144 		return;
   1145 	}
   1146 #endif
   1147 
   1148 	/* Get next request in queue. */
   1149 	xfer = SIMPLEQ_FIRST(&pipe->queue);
   1150 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
   1151 	if (xfer == NULL) {
   1152 		pipe->running = 0;
   1153 	} else {
   1154 		err = pipe->methods->start(xfer);
   1155 		if (err != USBD_IN_PROGRESS) {
   1156 			printf("usbd_start_next: error=%d\n", err);
   1157 			pipe->running = 0;
   1158 			/* XXX do what? */
   1159 		}
   1160 	}
   1161 }
   1162 
   1163 usbd_status
   1164 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
   1165 {
   1166 	return (usbd_do_request_flags(dev, req, data, 0, 0,
   1167 				      USBD_DEFAULT_TIMEOUT));
   1168 }
   1169 
   1170 usbd_status
   1171 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
   1172 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
   1173 {
   1174 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
   1175 					   data, flags, actlen, timo));
   1176 }
   1177 
   1178 usbd_status
   1179 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
   1180 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
   1181 	u_int32_t timeout)
   1182 {
   1183 	usbd_xfer_handle xfer;
   1184 	usbd_status err;
   1185 
   1186 #ifdef DIAGNOSTIC
   1187 #if defined(__i386__) && defined(__FreeBSD__)
   1188 	KASSERT(curthread->td_intr_nesting_level == 0,
   1189 	       	("usbd_do_request: in interrupt context"));
   1190 #endif
   1191 	if (dev->bus->intr_context) {
   1192 		panic("usbd_do_request: not in process context");
   1193 	}
   1194 #endif
   1195 
   1196 	xfer = usbd_alloc_xfer(dev, pipe);
   1197 	if (xfer == NULL)
   1198 		return (USBD_NOMEM);
   1199 	xfer->pipe = dev->default_pipe;	/* XXX */
   1200 	/* XXX */
   1201 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
   1202 				data, UGETW(req->wLength), flags, 0);
   1203 	/* XXX */
   1204 	xfer->pipe = pipe;
   1205 	err = usbd_sync_transfer(xfer);
   1206 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1207 	if (xfer->actlen > xfer->length) {
   1208 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1209 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1210 			 dev->address, xfer->request.bmRequestType,
   1211 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1212 			 UGETW(xfer->request.wIndex),
   1213 			 UGETW(xfer->request.wLength),
   1214 			 xfer->length, xfer->actlen));
   1215 	}
   1216 #endif
   1217 	if (actlen != NULL)
   1218 		*actlen = xfer->actlen;
   1219 	if (err == USBD_STALLED) {
   1220 		/*
   1221 		 * The control endpoint has stalled.  Control endpoints
   1222 		 * should not halt, but some may do so anyway so clear
   1223 		 * any halt condition.
   1224 		 */
   1225 		usb_device_request_t treq;
   1226 		usb_status_t status;
   1227 		u_int16_t s;
   1228 		usbd_status nerr;
   1229 
   1230 		treq.bmRequestType = UT_READ_ENDPOINT;
   1231 		treq.bRequest = UR_GET_STATUS;
   1232 		USETW(treq.wValue, 0);
   1233 		USETW(treq.wIndex, 0);
   1234 		USETW(treq.wLength, sizeof(usb_status_t));
   1235 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1236 					   &treq, &status,sizeof(usb_status_t),
   1237 					   0, 0);
   1238 		nerr = usbd_sync_transfer(xfer);
   1239 		if (nerr)
   1240 			goto bad;
   1241 		s = UGETW(status.wStatus);
   1242 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
   1243 		if (!(s & UES_HALT))
   1244 			goto bad;
   1245 		treq.bmRequestType = UT_WRITE_ENDPOINT;
   1246 		treq.bRequest = UR_CLEAR_FEATURE;
   1247 		USETW(treq.wValue, UF_ENDPOINT_HALT);
   1248 		USETW(treq.wIndex, 0);
   1249 		USETW(treq.wLength, 0);
   1250 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1251 					   &treq, &status, 0, 0, 0);
   1252 		nerr = usbd_sync_transfer(xfer);
   1253 		if (nerr)
   1254 			goto bad;
   1255 	}
   1256 
   1257  bad:
   1258 	usbd_free_xfer(xfer);
   1259 	return (err);
   1260 }
   1261 
   1262 void
   1263 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1264     usbd_private_handle priv, usbd_status status)
   1265 {
   1266 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1267 	if (xfer->actlen > xfer->length) {
   1268 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1269 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1270 			 xfer->pipe->device->address,
   1271 			 xfer->request.bmRequestType,
   1272 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1273 			 UGETW(xfer->request.wIndex),
   1274 			 UGETW(xfer->request.wLength),
   1275 			 xfer->length, xfer->actlen));
   1276 	}
   1277 #endif
   1278 	usbd_free_xfer_flag(xfer, U_NOWAIT);
   1279 }
   1280 
   1281 /*
   1282  * Execute a request without waiting for completion.
   1283  * Can be used from interrupt context.
   1284  */
   1285 usbd_status
   1286 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1287 		      void *data)
   1288 {
   1289 	usbd_xfer_handle xfer;
   1290 
   1291 	xfer = usbd_alloc_xfer_flag(dev, dev->default_pipe, U_NOWAIT);
   1292 	if (xfer == NULL)
   1293 		return (USBD_NOMEM);
   1294 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1295 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1296 
   1297 	/*
   1298 	 * Request will be processed later as a task (in thread context).
   1299 	 */
   1300 	usb_init_task(&xfer->task, usbd_do_request_async_task, xfer);
   1301 	usb_add_task(dev, &xfer->task, USB_TASKQ_HC);
   1302 
   1303 	return (USBD_IN_PROGRESS);
   1304 }
   1305 
   1306 Static void
   1307 usbd_do_request_async_task(void *arg)
   1308 {
   1309 	usbd_xfer_handle xfer = arg;
   1310 	usbd_status err;
   1311 
   1312 	/* XXX impossible to notify caller the error */
   1313 	err = usbd_transfer(xfer);
   1314 	if (err != USBD_IN_PROGRESS && err) {
   1315 		usbd_free_xfer(xfer);
   1316 	}
   1317 }
   1318 
   1319 const struct usbd_quirks *
   1320 usbd_get_quirks(usbd_device_handle dev)
   1321 {
   1322 #ifdef DIAGNOSTIC
   1323 	if (dev == NULL) {
   1324 		printf("usbd_get_quirks: dev == NULL\n");
   1325 		return 0;
   1326 	}
   1327 #endif
   1328 	return (dev->quirks);
   1329 }
   1330 
   1331 /* XXX do periodic free() of free list */
   1332 
   1333 /*
   1334  * Called from keyboard driver when in polling mode.
   1335  */
   1336 void
   1337 usbd_dopoll(usbd_interface_handle iface)
   1338 {
   1339 	iface->device->bus->methods->do_poll(iface->device->bus);
   1340 }
   1341 
   1342 void
   1343 usbd_set_polling(usbd_device_handle dev, int on)
   1344 {
   1345 	if (on)
   1346 		dev->bus->use_polling++;
   1347 	else
   1348 		dev->bus->use_polling--;
   1349 	/* When polling we need to make sure there is nothing pending to do. */
   1350 	if (dev->bus->use_polling)
   1351 		dev->bus->methods->soft_intr(dev->bus);
   1352 }
   1353 
   1354 
   1355 usb_endpoint_descriptor_t *
   1356 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1357 {
   1358 	struct usbd_endpoint *ep;
   1359 	int i;
   1360 
   1361 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1362 		ep = &iface->endpoints[i];
   1363 		if (ep->edesc->bEndpointAddress == address)
   1364 			return (iface->endpoints[i].edesc);
   1365 	}
   1366 	return (0);
   1367 }
   1368 
   1369 /*
   1370  * usbd_ratecheck() can limit the number of error messages that occurs.
   1371  * When a device is unplugged it may take up to 0.25s for the hub driver
   1372  * to notice it.  If the driver continuosly tries to do I/O operations
   1373  * this can generate a large number of messages.
   1374  */
   1375 int
   1376 usbd_ratecheck(struct timeval *last)
   1377 {
   1378 #if defined(__NetBSD__)
   1379 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1380 
   1381 	return (ratecheck(last, &errinterval));
   1382 #elif defined(__FreeBSD__)
   1383 	if (last->tv_sec == time_second)
   1384 		return (0);
   1385 	last->tv_sec = time_second;
   1386 	return (1);
   1387 #endif
   1388 }
   1389 
   1390 /*
   1391  * Search for a vendor/product pair in an array.  The item size is
   1392  * given as an argument.
   1393  */
   1394 const struct usb_devno *
   1395 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1396 		 u_int16_t vendor, u_int16_t product)
   1397 {
   1398 	while (nentries-- > 0) {
   1399 		u_int16_t tproduct = tbl->ud_product;
   1400 		if (tbl->ud_vendor == vendor &&
   1401 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1402 			return (tbl);
   1403 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1404 	}
   1405 	return (NULL);
   1406 }
   1407 
   1408 
   1409 void
   1410 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1411 {
   1412 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1413 
   1414         iter->cur = (const uByte *)cd;
   1415         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1416 }
   1417 
   1418 const usb_descriptor_t *
   1419 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1420 {
   1421 	const usb_descriptor_t *desc;
   1422 
   1423 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1424 		if (iter->cur != iter->end)
   1425 			printf("usb_desc_iter_next: bad descriptor\n");
   1426 		return NULL;
   1427 	}
   1428 	desc = (const usb_descriptor_t *)iter->cur;
   1429 	if (desc->bLength == 0) {
   1430 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1431 		return NULL;
   1432 	}
   1433 	iter->cur += desc->bLength;
   1434 	if (iter->cur > iter->end) {
   1435 		printf("usb_desc_iter_next: descriptor length too large\n");
   1436 		return NULL;
   1437 	}
   1438 	return desc;
   1439 }
   1440 
   1441 usbd_status
   1442 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1443 {
   1444 	return usbd_get_string0(dev, si, buf, 1);
   1445 }
   1446 
   1447 usbd_status
   1448 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1449 {
   1450 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1451 	usb_string_descriptor_t us;
   1452 	char *s;
   1453 	int i, n;
   1454 	u_int16_t c;
   1455 	usbd_status err;
   1456 	int size;
   1457 
   1458 	buf[0] = '\0';
   1459 	if (si == 0)
   1460 		return (USBD_INVAL);
   1461 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1462 		return (USBD_STALLED);
   1463 	if (dev->langid == USBD_NOLANG) {
   1464 		/* Set up default language */
   1465 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1466 		    &size);
   1467 		if (err || size < 4) {
   1468 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1469 			dev->langid = 0; /* Well, just pick something then */
   1470 		} else {
   1471 			/* Pick the first language as the default. */
   1472 			dev->langid = UGETW(us.bString[0]);
   1473 		}
   1474 	}
   1475 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1476 	if (err)
   1477 		return (err);
   1478 	s = buf;
   1479 	n = size / 2 - 1;
   1480 	if (unicode) {
   1481 		for (i = 0; i < n; i++) {
   1482 			c = UGETW(us.bString[i]);
   1483 			if (swap)
   1484 				c = (c >> 8) | (c << 8);
   1485 			s += wput_utf8(s, 3, c);
   1486 		}
   1487 		*s++ = 0;
   1488 	}
   1489 #if defined(__NetBSD__) && defined(COMPAT_30)
   1490 	else {
   1491 		for (i = 0; i < n; i++) {
   1492 			c = UGETW(us.bString[i]);
   1493 			if (swap)
   1494 				c = (c >> 8) | (c << 8);
   1495 			*s++ = (c < 0x80) ? c : '?';
   1496 		}
   1497 		*s++ = 0;
   1498 	}
   1499 #endif
   1500 	return (USBD_NORMAL_COMPLETION);
   1501 }
   1502 
   1503 #if defined(__FreeBSD__)
   1504 int
   1505 usbd_driver_load(module_t mod, int what, void *arg)
   1506 {
   1507 	/* XXX should implement something like a function that removes all generic devices */
   1508 
   1509  	return (0);
   1510 }
   1511 
   1512 #endif
   1513