Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.51
      1 /*	$NetBSD: usbdi.c,v 1.51 1999/11/18 23:32:33 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usbdi.c,v 1.28 1999/11/17 22:33:49 n_hibma 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 (augustss (at) carlstedt.se) 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/param.h>
     42 #include <sys/systm.h>
     43 #if defined(__NetBSD__) || defined(__OpenBSD__)
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 #elif defined(__FreeBSD__)
     47 #include <sys/module.h>
     48 #include <sys/bus.h>
     49 #include <sys/conf.h>
     50 #include "usb_if.h"
     51 #if defined(DIAGNOSTIC) && defined(__i386__)
     52 #include <machine/cpu.h>
     53 #endif
     54 #endif
     55 #include <sys/malloc.h>
     56 #include <sys/proc.h>
     57 
     58 #include <machine/bus.h>
     59 
     60 #include <dev/usb/usb.h>
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbdivar.h>
     64 #include <dev/usb/usb_mem.h>
     65 
     66 #if defined(__FreeBSD__)
     67 #include "usb_if.h"
     68 #endif
     69 
     70 #ifdef USB_DEBUG
     71 #define DPRINTF(x)	if (usbdebug) logprintf x
     72 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     73 extern int usbdebug;
     74 #else
     75 #define DPRINTF(x)
     76 #define DPRINTFN(n,x)
     77 #endif
     78 
     79 static usbd_status usbd_ar_pipe  __P((usbd_pipe_handle pipe));
     80 static void usbd_do_request_async_cb
     81 	__P((usbd_xfer_handle, usbd_private_handle, usbd_status));
     82 static void usbd_start_next __P((usbd_pipe_handle pipe));
     83 
     84 static SIMPLEQ_HEAD(, usbd_xfer) usbd_free_xfers =
     85 	SIMPLEQ_HEAD_INITIALIZER(usbd_free_xfers);
     86 
     87 static int usbd_nbuses = 0;
     88 
     89 void
     90 usbd_init()
     91 {
     92 	usbd_nbuses++;
     93 }
     94 
     95 void
     96 usbd_finish()
     97 {
     98 	usbd_xfer_handle xfer;
     99 
    100 	if (--usbd_nbuses == 0) {
    101 		/* Last controller is gone, free all xfers. */
    102 		for (;;) {
    103 			xfer = SIMPLEQ_FIRST(&usbd_free_xfers);
    104 			if (xfer == NULL)
    105 				break;
    106 			SIMPLEQ_REMOVE_HEAD(&usbd_free_xfers, xfer, next);
    107 			free(xfer, M_USB);
    108 		}
    109 	}
    110 }
    111 
    112 static __inline int usbd_xfer_isread __P((usbd_xfer_handle xfer));
    113 static __inline int
    114 usbd_xfer_isread(xfer)
    115 	usbd_xfer_handle xfer;
    116 {
    117 	if (xfer->rqflags & URQ_REQUEST)
    118 		return (xfer->request.bmRequestType & UT_READ);
    119 	else
    120 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
    121 			UE_DIR_IN);
    122 }
    123 
    124 #ifdef USB_DEBUG
    125 void usbd_dump_queue __P((usbd_pipe_handle));
    126 
    127 void
    128 usbd_dump_queue(pipe)
    129 	usbd_pipe_handle pipe;
    130 {
    131 	usbd_xfer_handle xfer;
    132 
    133 	printf("usbd_dump_queue: pipe=%p\n", pipe);
    134 	for (xfer = SIMPLEQ_FIRST(&pipe->queue);
    135 	     xfer;
    136 	     xfer = SIMPLEQ_NEXT(xfer, next)) {
    137 		printf("  xfer=%p\n", xfer);
    138 	}
    139 }
    140 #endif
    141 
    142 usbd_status
    143 usbd_open_pipe(iface, address, flags, pipe)
    144 	usbd_interface_handle iface;
    145 	u_int8_t address;
    146 	u_int8_t flags;
    147 	usbd_pipe_handle *pipe;
    148 {
    149 	usbd_pipe_handle p;
    150 	struct usbd_endpoint *ep;
    151 	usbd_status err;
    152 	int i;
    153 
    154 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    155 		ep = &iface->endpoints[i];
    156 		if (ep->edesc == NULL)
    157 			return (USBD_IOERROR);
    158 		if (ep->edesc->bEndpointAddress == address)
    159 			goto found;
    160 	}
    161 	return (USBD_BAD_ADDRESS);
    162  found:
    163 	if ((flags & USBD_EXCLUSIVE_USE) &&
    164 	    ep->refcnt != 0)
    165 		return (USBD_IN_USE);
    166 	err = usbd_setup_pipe(iface->device, iface, ep, &p);
    167 	if (err)
    168 		return (err);
    169 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    170 	*pipe = p;
    171 	return (USBD_NORMAL_COMPLETION);
    172 }
    173 
    174 usbd_status
    175 usbd_open_pipe_intr(iface, address, flags, pipe, priv, buffer, length, cb)
    176 	usbd_interface_handle iface;
    177 	u_int8_t address;
    178 	u_int8_t flags;
    179 	usbd_pipe_handle *pipe;
    180 	usbd_private_handle priv;
    181 	void *buffer;
    182 	u_int32_t length;
    183 	usbd_callback cb;
    184 {
    185 	usbd_status err;
    186 	usbd_xfer_handle xfer;
    187 	usbd_pipe_handle ipipe;
    188 
    189 	err = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &ipipe);
    190 	if (err)
    191 		return (err);
    192 	xfer = usbd_alloc_xfer(iface->device);
    193 	if (xfer == NULL) {
    194 		err = USBD_NOMEM;
    195 		goto bad1;
    196 	}
    197 	usbd_setup_xfer(xfer, ipipe, priv, buffer, length, flags,
    198 			   USBD_NO_TIMEOUT, cb);
    199 	ipipe->intrxfer = xfer;
    200 	ipipe->repeat = 1;
    201 	err = usbd_transfer(xfer);
    202 	*pipe = ipipe;
    203 	if (err != USBD_IN_PROGRESS)
    204 		goto bad2;
    205 	return (USBD_NORMAL_COMPLETION);
    206 
    207  bad2:
    208 	ipipe->intrxfer = NULL;
    209 	ipipe->repeat = 0;
    210 	usbd_free_xfer(xfer);
    211  bad1:
    212 	usbd_close_pipe(ipipe);
    213 	return (err);
    214 }
    215 
    216 usbd_status
    217 usbd_close_pipe(pipe)
    218 	usbd_pipe_handle pipe;
    219 {
    220 #ifdef DIAGNOSTIC
    221 	if (pipe == NULL) {
    222 		printf("usbd_close_pipe: pipe==NULL\n");
    223 		return (USBD_NORMAL_COMPLETION);
    224 	}
    225 #endif
    226 
    227 	if (--pipe->refcnt != 0)
    228 		return (USBD_NORMAL_COMPLETION);
    229 	if (SIMPLEQ_FIRST(&pipe->queue) != 0)
    230 		return (USBD_PENDING_REQUESTS);
    231 	LIST_REMOVE(pipe, next);
    232 	pipe->endpoint->refcnt--;
    233 	pipe->methods->close(pipe);
    234 	if (pipe->intrxfer != NULL)
    235 		usbd_free_xfer(pipe->intrxfer);
    236 	free(pipe, M_USB);
    237 	return (USBD_NORMAL_COMPLETION);
    238 }
    239 
    240 usbd_status
    241 usbd_transfer(xfer)
    242 	usbd_xfer_handle xfer;
    243 {
    244 	usbd_pipe_handle pipe = xfer->pipe;
    245 	usb_dma_t *dmap = &xfer->dmabuf;
    246 	usbd_status err;
    247 	u_int size;
    248 	int s;
    249 
    250 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
    251 		    xfer, xfer->flags, pipe, pipe->running));
    252 #ifdef USB_DEBUG
    253 	if (usbdebug > 5)
    254 		usbd_dump_queue(pipe);
    255 #endif
    256 	xfer->done = 0;
    257 
    258 	size = xfer->length;
    259 	/* If there is no buffer, allocate one. */
    260 	if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
    261 		struct usbd_bus *bus = pipe->device->bus;
    262 
    263 #ifdef DIAGNOSTIC
    264 		if (xfer->rqflags & URQ_AUTO_DMABUF)
    265 			printf("usbd_transfer: has old buffer!\n");
    266 #endif
    267 		err = bus->methods->allocm(bus, dmap, size);
    268 		if (err)
    269 			return (err);
    270 		xfer->rqflags |= URQ_AUTO_DMABUF;
    271 	}
    272 
    273 	/* Copy data if going out. */
    274 	if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
    275 	    !usbd_xfer_isread(xfer))
    276 		memcpy(KERNADDR(dmap), xfer->buffer, size);
    277 
    278 	err = pipe->methods->transfer(xfer);
    279 
    280 	if (err != USBD_IN_PROGRESS && err) {
    281 		/* The transfer has not been queued, so free buffer. */
    282 		if (xfer->rqflags & URQ_AUTO_DMABUF) {
    283 			struct usbd_bus *bus = pipe->device->bus;
    284 
    285 			bus->methods->freem(bus, &xfer->dmabuf);
    286 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    287 		}
    288 	}
    289 
    290 	if (!(xfer->flags & USBD_SYNCHRONOUS))
    291 		return (err);
    292 
    293 	/* Sync transfer, wait for completion. */
    294 	if (err != USBD_IN_PROGRESS)
    295 		return (err);
    296 	s = splusb();
    297 	if (!xfer->done) {
    298 		if (pipe->device->bus->use_polling)
    299 			panic("usbd_transfer: not done\n");
    300 		tsleep(xfer, PRIBIO, "usbsyn", 0);
    301 	}
    302 	splx(s);
    303 	return (xfer->status);
    304 }
    305 
    306 /* Like usbd_transfer(), but waits for completion. */
    307 usbd_status
    308 usbd_sync_transfer(xfer)
    309 	usbd_xfer_handle xfer;
    310 {
    311 	xfer->flags |= USBD_SYNCHRONOUS;
    312 	return (usbd_transfer(xfer));
    313 }
    314 
    315 void *
    316 usbd_alloc_buffer(xfer, size)
    317 	usbd_xfer_handle xfer;
    318 	u_int32_t size;
    319 {
    320 	struct usbd_bus *bus = xfer->device->bus;
    321 	usbd_status err;
    322 
    323 	err = bus->methods->allocm(bus, &xfer->dmabuf, size);
    324 	if (err)
    325 		return (0);
    326 	xfer->rqflags |= URQ_DEV_DMABUF;
    327 	return (KERNADDR(&xfer->dmabuf));
    328 }
    329 
    330 void
    331 usbd_free_buffer(xfer)
    332 	usbd_xfer_handle xfer;
    333 {
    334 #ifdef DIAGNOSTIC
    335 	if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
    336 		printf("usbd_free_buffer: no buffer\n");
    337 		return;
    338 	}
    339 #endif
    340 	xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
    341 	xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
    342 }
    343 
    344 void *
    345 usbd_get_buffer(xfer)
    346 	usbd_xfer_handle xfer;
    347 {
    348 	if (!(xfer->rqflags & URQ_DEV_DMABUF))
    349 		return (0);
    350 	return (KERNADDR(&xfer->dmabuf));
    351 }
    352 
    353 usbd_xfer_handle
    354 usbd_alloc_xfer(dev)
    355 	usbd_device_handle dev;
    356 {
    357 	usbd_xfer_handle xfer;
    358 
    359 	xfer = SIMPLEQ_FIRST(&usbd_free_xfers);
    360 	if (xfer != NULL)
    361 		SIMPLEQ_REMOVE_HEAD(&usbd_free_xfers, xfer, next);
    362 	else
    363 		xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
    364 	if (xfer == NULL)
    365 		return (0);
    366 	memset(xfer, 0, sizeof *xfer);
    367 	xfer->device = dev;
    368 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
    369 	return (xfer);
    370 }
    371 
    372 usbd_status
    373 usbd_free_xfer(xfer)
    374 	usbd_xfer_handle xfer;
    375 {
    376 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
    377 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    378 		usbd_free_buffer(xfer);
    379 	SIMPLEQ_INSERT_HEAD(&usbd_free_xfers, xfer, next);
    380 	return (USBD_NORMAL_COMPLETION);
    381 }
    382 
    383 void
    384 usbd_setup_xfer(xfer, pipe, priv, buffer, length, flags, timeout, callback)
    385 	usbd_xfer_handle xfer;
    386 	usbd_pipe_handle pipe;
    387 	usbd_private_handle priv;
    388 	void *buffer;
    389 	u_int32_t length;
    390 	u_int16_t flags;
    391 	u_int32_t timeout;
    392 	void (*callback) __P((usbd_xfer_handle,
    393 			      usbd_private_handle,
    394 			      usbd_status));
    395 {
    396 	xfer->pipe = pipe;
    397 	xfer->priv = priv;
    398 	xfer->buffer = buffer;
    399 	xfer->length = length;
    400 	xfer->actlen = 0;
    401 	xfer->flags = flags;
    402 	xfer->timeout = timeout;
    403 	xfer->status = USBD_NOT_STARTED;
    404 	xfer->callback = callback;
    405 	xfer->rqflags &= ~URQ_REQUEST;
    406 	xfer->nframes = 0;
    407 }
    408 
    409 void
    410 usbd_setup_default_xfer(xfer, dev, priv, timeout, req, buffer,
    411 			   length, flags, callback)
    412 	usbd_xfer_handle xfer;
    413 	usbd_device_handle dev;
    414 	usbd_private_handle priv;
    415 	u_int32_t timeout;
    416 	usb_device_request_t *req;
    417 	void *buffer;
    418 	u_int32_t length;
    419 	u_int16_t flags;
    420 	void (*callback) __P((usbd_xfer_handle,
    421 			      usbd_private_handle,
    422 			      usbd_status));
    423 {
    424 	xfer->pipe = dev->default_pipe;
    425 	xfer->priv = priv;
    426 	xfer->buffer = buffer;
    427 	xfer->length = length;
    428 	xfer->actlen = 0;
    429 	xfer->flags = flags;
    430 	xfer->timeout = timeout;
    431 	xfer->status = USBD_NOT_STARTED;
    432 	xfer->callback = callback;
    433 	xfer->request = *req;
    434 	xfer->rqflags |= URQ_REQUEST;
    435 	xfer->nframes = 0;
    436 }
    437 
    438 void
    439 usbd_setup_isoc_xfer(xfer, pipe, priv, frlengths, nframes, flags, callback)
    440 	usbd_xfer_handle xfer;
    441 	usbd_pipe_handle pipe;
    442 	usbd_private_handle priv;
    443 	u_int16_t *frlengths;
    444 	u_int32_t nframes;
    445 	u_int16_t flags;
    446 	usbd_callback callback;
    447 {
    448 	xfer->pipe = pipe;
    449 	xfer->priv = priv;
    450 	xfer->buffer = 0;
    451 	xfer->length = 0;
    452 	xfer->actlen = 0;
    453 	xfer->flags = flags;
    454 	xfer->timeout = USBD_NO_TIMEOUT;
    455 	xfer->status = USBD_NOT_STARTED;
    456 	xfer->callback = callback;
    457 	xfer->rqflags &= ~URQ_REQUEST;
    458 	xfer->frlengths = frlengths;
    459 	xfer->nframes = nframes;
    460 }
    461 
    462 void
    463 usbd_get_xfer_status(xfer, priv, buffer, count, status)
    464 	usbd_xfer_handle xfer;
    465 	usbd_private_handle *priv;
    466 	void **buffer;
    467 	u_int32_t *count;
    468 	usbd_status *status;
    469 {
    470 	if (priv != NULL)
    471 		*priv = xfer->priv;
    472 	if (buffer != NULL)
    473 		*buffer = xfer->buffer;
    474 	if (count != NULL)
    475 		*count = xfer->actlen;
    476 	if (status != NULL)
    477 		*status = xfer->status;
    478 }
    479 
    480 usb_config_descriptor_t *
    481 usbd_get_config_descriptor(dev)
    482 	usbd_device_handle dev;
    483 {
    484 	return (dev->cdesc);
    485 }
    486 
    487 usb_interface_descriptor_t *
    488 usbd_get_interface_descriptor(iface)
    489 	usbd_interface_handle iface;
    490 {
    491 	return (iface->idesc);
    492 }
    493 
    494 usb_device_descriptor_t *
    495 usbd_get_device_descriptor(dev)
    496 	usbd_device_handle dev;
    497 {
    498 	return (&dev->ddesc);
    499 }
    500 
    501 usb_endpoint_descriptor_t *
    502 usbd_interface2endpoint_descriptor(iface, index)
    503 	usbd_interface_handle iface;
    504 	u_int8_t index;
    505 {
    506 	if (index >= iface->idesc->bNumEndpoints)
    507 		return (0);
    508 	return (iface->endpoints[index].edesc);
    509 }
    510 
    511 usbd_status
    512 usbd_abort_pipe(pipe)
    513 	usbd_pipe_handle pipe;
    514 {
    515 	usbd_status err;
    516 	int s;
    517 
    518 #ifdef DIAGNOSTIC
    519 	if (pipe == NULL) {
    520 		printf("usbd_close_pipe: pipe==NULL\n");
    521 		return (USBD_NORMAL_COMPLETION);
    522 	}
    523 #endif
    524 	s = splusb();
    525 	err = usbd_ar_pipe(pipe);
    526 	splx(s);
    527 	return (err);
    528 }
    529 
    530 usbd_status
    531 usbd_clear_endpoint_stall(pipe)
    532 	usbd_pipe_handle pipe;
    533 {
    534 	usbd_device_handle dev = pipe->device;
    535 	usb_device_request_t req;
    536 	usbd_status err;
    537 
    538 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    539 
    540 	/*
    541 	 * Clearing en endpoint stall resets the enpoint toggle, so
    542 	 * do the same to the HC toggle.
    543 	 */
    544 	pipe->methods->cleartoggle(pipe);
    545 
    546 	req.bmRequestType = UT_WRITE_ENDPOINT;
    547 	req.bRequest = UR_CLEAR_FEATURE;
    548 	USETW(req.wValue, UF_ENDPOINT_HALT);
    549 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    550 	USETW(req.wLength, 0);
    551 	err = usbd_do_request(dev, &req, 0);
    552 #if 0
    553 XXX should we do this?
    554 	if (!err) {
    555 		pipe->state = USBD_PIPE_ACTIVE;
    556 		/* XXX activate pipe */
    557 	}
    558 #endif
    559 	return (err);
    560 }
    561 
    562 usbd_status
    563 usbd_clear_endpoint_stall_async(pipe)
    564 	usbd_pipe_handle pipe;
    565 {
    566 	usbd_device_handle dev = pipe->device;
    567 	usb_device_request_t req;
    568 	usbd_status err;
    569 
    570 	pipe->methods->cleartoggle(pipe);
    571 
    572 	req.bmRequestType = UT_WRITE_ENDPOINT;
    573 	req.bRequest = UR_CLEAR_FEATURE;
    574 	USETW(req.wValue, UF_ENDPOINT_HALT);
    575 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    576 	USETW(req.wLength, 0);
    577 	err = usbd_do_request_async(dev, &req, 0);
    578 	return (err);
    579 }
    580 
    581 usbd_status
    582 usbd_endpoint_count(iface, count)
    583 	usbd_interface_handle iface;
    584 	u_int8_t *count;
    585 {
    586 	*count = iface->idesc->bNumEndpoints;
    587 	return (USBD_NORMAL_COMPLETION);
    588 }
    589 
    590 usbd_status
    591 usbd_interface_count(dev, count)
    592 	usbd_device_handle dev;
    593 	u_int8_t *count;
    594 {
    595 	if (dev->cdesc == NULL)
    596 		return (USBD_NOT_CONFIGURED);
    597 	*count = dev->cdesc->bNumInterface;
    598 	return (USBD_NORMAL_COMPLETION);
    599 }
    600 
    601 usbd_status
    602 usbd_interface2device_handle(iface, dev)
    603 	usbd_interface_handle iface;
    604 	usbd_device_handle *dev;
    605 {
    606 	*dev = iface->device;
    607 	return (USBD_NORMAL_COMPLETION);
    608 }
    609 
    610 usbd_status
    611 usbd_device2interface_handle(dev, ifaceno, iface)
    612 	usbd_device_handle dev;
    613 	u_int8_t ifaceno;
    614 	usbd_interface_handle *iface;
    615 {
    616 	if (dev->cdesc == NULL)
    617 		return (USBD_NOT_CONFIGURED);
    618 	if (ifaceno >= dev->cdesc->bNumInterface)
    619 		return (USBD_INVAL);
    620 	*iface = &dev->ifaces[ifaceno];
    621 	return (USBD_NORMAL_COMPLETION);
    622 }
    623 
    624 usbd_device_handle
    625 usbd_pipe2device_handle(pipe)
    626 	usbd_pipe_handle pipe;
    627 {
    628 	return (pipe->device);
    629 }
    630 
    631 /* XXXX use altno */
    632 usbd_status
    633 usbd_set_interface(iface, altidx)
    634 	usbd_interface_handle iface;
    635 	int altidx;
    636 {
    637 	usb_device_request_t req;
    638 	usbd_status err;
    639 
    640 	if (LIST_FIRST(&iface->pipes) != 0)
    641 		return (USBD_IN_USE);
    642 
    643 	if (iface->endpoints)
    644 		free(iface->endpoints, M_USB);
    645 	iface->endpoints = 0;
    646 	iface->idesc = 0;
    647 
    648 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
    649 	if (err)
    650 		return (err);
    651 
    652 	req.bmRequestType = UT_WRITE_INTERFACE;
    653 	req.bRequest = UR_SET_INTERFACE;
    654 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    655 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    656 	USETW(req.wLength, 0);
    657 	return (usbd_do_request(iface->device, &req, 0));
    658 }
    659 
    660 int
    661 usbd_get_no_alts(cdesc, ifaceno)
    662 	usb_config_descriptor_t *cdesc;
    663 	int ifaceno;
    664 {
    665 	char *p = (char *)cdesc;
    666 	char *end = p + UGETW(cdesc->wTotalLength);
    667 	usb_interface_descriptor_t *d;
    668 	int n;
    669 
    670 	for (n = 0; p < end; p += d->bLength) {
    671 		d = (usb_interface_descriptor_t *)p;
    672 		if (p + d->bLength <= end &&
    673 		    d->bDescriptorType == UDESC_INTERFACE &&
    674 		    d->bInterfaceNumber == ifaceno)
    675 			n++;
    676 	}
    677 	return (n);
    678 }
    679 
    680 int
    681 usbd_get_interface_altindex(iface)
    682 	usbd_interface_handle iface;
    683 {
    684 	return (iface->altindex);
    685 }
    686 
    687 usbd_status
    688 usbd_get_interface(iface, aiface)
    689 	usbd_interface_handle iface;
    690 	u_int8_t *aiface;
    691 {
    692 	usb_device_request_t req;
    693 
    694 	req.bmRequestType = UT_READ_INTERFACE;
    695 	req.bRequest = UR_GET_INTERFACE;
    696 	USETW(req.wValue, 0);
    697 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    698 	USETW(req.wLength, 1);
    699 	return (usbd_do_request(iface->device, &req, aiface));
    700 }
    701 
    702 /*** Internal routines ***/
    703 
    704 /* Dequeue all pipe operations, called at splusb(). */
    705 static usbd_status
    706 usbd_ar_pipe(pipe)
    707 	usbd_pipe_handle pipe;
    708 {
    709 	usbd_xfer_handle xfer;
    710 
    711 	SPLUSBCHECK;
    712 
    713 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    714 #ifdef USB_DEBUG
    715 	if (usbdebug > 5)
    716 		usbd_dump_queue(pipe);
    717 #endif
    718 	pipe->repeat = 0;
    719 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
    720 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
    721 			    pipe, xfer, pipe->methods));
    722 		/* Make the HC abort it (and invoke the callback). */
    723 		pipe->methods->abort(xfer);
    724 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    725 	}
    726 	return (USBD_NORMAL_COMPLETION);
    727 }
    728 
    729 /* Called at splusb() */
    730 void
    731 usb_transfer_complete(xfer)
    732 	usbd_xfer_handle xfer;
    733 {
    734 	usbd_pipe_handle pipe = xfer->pipe;
    735 	usb_dma_t *dmap = &xfer->dmabuf;
    736 	int repeat = pipe->repeat;
    737 	int polling;
    738 
    739 	SPLUSBCHECK;
    740 
    741 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    742 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    743 
    744 #ifdef DIAGNOSTIC
    745 	if (pipe == NULL) {
    746 		printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
    747 		return;
    748 	}
    749 #endif
    750 	polling = pipe->device->bus->use_polling;
    751 	/* XXXX */
    752 	if (polling)
    753 		pipe->running = 0;
    754 
    755 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    756 	    usbd_xfer_isread(xfer)) {
    757 #ifdef DIAGNOSTIC
    758 		if (xfer->actlen > xfer->length) {
    759 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    760 			       xfer->actlen, xfer->length);
    761 			xfer->actlen = xfer->length;
    762 		}
    763 #endif
    764 		memcpy(xfer->buffer, KERNADDR(dmap), xfer->actlen);
    765 	}
    766 
    767 	/* if we allocated the buffer in usbd_transfer() we free it here. */
    768 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
    769 		if (!repeat) {
    770 			struct usbd_bus *bus = pipe->device->bus;
    771 			bus->methods->freem(bus, dmap);
    772 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    773 		}
    774 	}
    775 
    776 	if (pipe->methods->done != NULL)
    777 		pipe->methods->done(xfer);
    778 
    779 	if (!repeat) {
    780 		/* Remove request from queue. */
    781 #ifdef DIAGNOSTIC
    782 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
    783 			printf("usb_transfer_complete: bad dequeue %p != %p\n",
    784 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
    785 #endif
    786 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, xfer, next);
    787 	}
    788 
    789 	/* Count completed transfers. */
    790 	++pipe->device->bus->stats.requests
    791 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    792 
    793 	xfer->done = 1;
    794 	if (xfer->status == USBD_NORMAL_COMPLETION &&
    795 	    xfer->actlen < xfer->length &&
    796 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
    797 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
    798 			     xfer->actlen, xfer->length));
    799 		xfer->status = USBD_SHORT_XFER;
    800 	}
    801 
    802 	if (xfer->callback)
    803 		xfer->callback(xfer, xfer->priv, xfer->status);
    804 
    805 	if ((xfer->flags & USBD_SYNCHRONOUS) && !polling)
    806 		wakeup(xfer);
    807 
    808 	if (!repeat) {
    809 		/* XXX should we stop the queue on all errors? */
    810 		if (xfer->status == USBD_CANCELLED ||
    811 		    xfer->status == USBD_TIMEOUT)
    812 			pipe->running = 0;
    813 		else
    814 			usbd_start_next(pipe);
    815 	}
    816 }
    817 
    818 usbd_status
    819 usb_insert_transfer(xfer)
    820 	usbd_xfer_handle xfer;
    821 {
    822 	usbd_pipe_handle pipe = xfer->pipe;
    823 	usbd_status err;
    824 	int s;
    825 
    826 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    827 		    pipe, pipe->running, xfer->timeout));
    828 	s = splusb();
    829 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    830 	if (pipe->running)
    831 		err = USBD_IN_PROGRESS;
    832 	else {
    833 		pipe->running = 1;
    834 		err = USBD_NORMAL_COMPLETION;
    835 	}
    836 	splx(s);
    837 	return (err);
    838 }
    839 
    840 /* Called at splusb() */
    841 void
    842 usbd_start_next(pipe)
    843 	usbd_pipe_handle pipe;
    844 {
    845 	usbd_xfer_handle xfer;
    846 	usbd_status err;
    847 
    848 	SPLUSBCHECK;
    849 
    850 #ifdef DIAGNOSTIC
    851 	if (pipe == NULL) {
    852 		printf("usbd_start_next: pipe == NULL\n");
    853 		return;
    854 	}
    855 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    856 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    857 		return;
    858 	}
    859 #endif
    860 
    861 	/* Get next request in queue. */
    862 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    863 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    864 	if (xfer == NULL) {
    865 		pipe->running = 0;
    866 	} else {
    867 		err = pipe->methods->start(xfer);
    868 		if (err != USBD_IN_PROGRESS) {
    869 			printf("usbd_start_next: error=%d\n", err);
    870 			pipe->running = 0;
    871 			/* XXX do what? */
    872 		}
    873 	}
    874 }
    875 
    876 usbd_status
    877 usbd_do_request(dev, req, data)
    878 	usbd_device_handle dev;
    879 	usb_device_request_t *req;
    880 	void *data;
    881 {
    882 	return (usbd_do_request_flags(dev, req, data, 0, 0));
    883 }
    884 
    885 usbd_status
    886 usbd_do_request_flags(dev, req, data, flags, actlen)
    887 	usbd_device_handle dev;
    888 	usb_device_request_t *req;
    889 	void *data;
    890 	u_int16_t flags;
    891 	int *actlen;
    892 {
    893 	usbd_xfer_handle xfer;
    894 	usbd_status err;
    895 
    896 #ifdef DIAGNOSTIC
    897 #if defined(__i386__) && defined(__FreeBSD__)
    898 	KASSERT(intr_nesting_level == 0,
    899 	       	("ohci_abort_req in interrupt context"));
    900 #endif
    901 	if (dev->bus->intr_context) {
    902 		printf("usbd_do_request: not in process context\n");
    903 		return (USBD_INVAL);
    904 	}
    905 #endif
    906 
    907 	xfer = usbd_alloc_xfer(dev);
    908 	if (xfer == NULL)
    909 		return (USBD_NOMEM);
    910 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
    911 				   data, UGETW(req->wLength), flags, 0);
    912 	err = usbd_sync_transfer(xfer);
    913 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    914 	if (xfer->actlen > xfer->length)
    915 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    916 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    917 			 dev->address, xfer->request.bmRequestType,
    918 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    919 			 UGETW(xfer->request.wIndex),
    920 			 UGETW(xfer->request.wLength),
    921 			 xfer->length, xfer->actlen));
    922 #endif
    923 	if (actlen != NULL)
    924 		*actlen = xfer->actlen;
    925 	if (err == USBD_STALLED) {
    926 		/*
    927 		 * The control endpoint has stalled.  Control endpoints
    928 		 * should not halt, but some may do so anyway so clear
    929 		 * any halt condition.
    930 		 */
    931 		usb_device_request_t treq;
    932 		usb_status_t status;
    933 		u_int16_t s;
    934 		usbd_status nerr;
    935 
    936 		treq.bmRequestType = UT_READ_ENDPOINT;
    937 		treq.bRequest = UR_GET_STATUS;
    938 		USETW(treq.wValue, 0);
    939 		USETW(treq.wIndex, 0);
    940 		USETW(treq.wLength, sizeof(usb_status_t));
    941 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    942 					   &treq, &status,sizeof(usb_status_t),
    943 					   0, 0);
    944 		nerr = usbd_sync_transfer(xfer);
    945 		if (nerr)
    946 			goto bad;
    947 		s = UGETW(status.wStatus);
    948 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
    949 		if (!(s & UES_HALT))
    950 			goto bad;
    951 		treq.bmRequestType = UT_WRITE_ENDPOINT;
    952 		treq.bRequest = UR_CLEAR_FEATURE;
    953 		USETW(treq.wValue, UF_ENDPOINT_HALT);
    954 		USETW(treq.wIndex, 0);
    955 		USETW(treq.wLength, 0);
    956 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    957 					   &treq, &status, 0, 0, 0);
    958 		nerr = usbd_sync_transfer(xfer);
    959 		if (nerr)
    960 			goto bad;
    961 	}
    962 
    963  bad:
    964 	usbd_free_xfer(xfer);
    965 	return (err);
    966 }
    967 
    968 void
    969 usbd_do_request_async_cb(xfer, priv, status)
    970 	usbd_xfer_handle xfer;
    971 	usbd_private_handle priv;
    972 	usbd_status status;
    973 {
    974 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    975 	if (xfer->actlen > xfer->length)
    976 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    977 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    978 			 xfer->pipe->device->address,
    979 			 xfer->request.bmRequestType,
    980 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    981 			 UGETW(xfer->request.wIndex),
    982 			 UGETW(xfer->request.wLength),
    983 			 xfer->length, xfer->actlen));
    984 #endif
    985 	usbd_free_xfer(xfer);
    986 }
    987 
    988 /*
    989  * Execute a request without waiting for completion.
    990  * Can be used from interrupt context.
    991  */
    992 usbd_status
    993 usbd_do_request_async(dev, req, data)
    994 	usbd_device_handle dev;
    995 	usb_device_request_t *req;
    996 	void *data;
    997 {
    998 	usbd_xfer_handle xfer;
    999 	usbd_status err;
   1000 
   1001 	xfer = usbd_alloc_xfer(dev);
   1002 	if (xfer == NULL)
   1003 		return (USBD_NOMEM);
   1004 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1005 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1006 	err = usbd_transfer(xfer);
   1007 	if (err != USBD_IN_PROGRESS) {
   1008 		usbd_free_xfer(xfer);
   1009 		return (err);
   1010 	}
   1011 	return (USBD_NORMAL_COMPLETION);
   1012 }
   1013 
   1014 struct usbd_quirks *
   1015 usbd_get_quirks(dev)
   1016 	usbd_device_handle dev;
   1017 {
   1018 	return (dev->quirks);
   1019 }
   1020 
   1021 /* XXX do periodic free() of free list */
   1022 
   1023 /*
   1024  * Called from keyboard driver when in polling mode.
   1025  */
   1026 void
   1027 usbd_dopoll(iface)
   1028 	usbd_interface_handle iface;
   1029 {
   1030 	iface->device->bus->methods->do_poll(iface->device->bus);
   1031 }
   1032 
   1033 void
   1034 usbd_set_polling(iface, on)
   1035 	usbd_interface_handle iface;
   1036 	int on;
   1037 {
   1038 	if (on)
   1039 		iface->device->bus->use_polling++;
   1040 	else
   1041 		iface->device->bus->use_polling--;
   1042 }
   1043 
   1044 
   1045 usb_endpoint_descriptor_t *
   1046 usbd_get_endpoint_descriptor(iface, address)
   1047 	usbd_interface_handle iface;
   1048 	u_int8_t address;
   1049 {
   1050 	struct usbd_endpoint *ep;
   1051 	int i;
   1052 
   1053 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1054 		ep = &iface->endpoints[i];
   1055 		if (ep->edesc->bEndpointAddress == address)
   1056 			return (iface->endpoints[i].edesc);
   1057 	}
   1058 	return (0);
   1059 }
   1060 
   1061 #if defined(__FreeBSD__)
   1062 int
   1063 usbd_driver_load(module_t mod, int what, void *arg)
   1064 {
   1065 	/* XXX should implement something like a function that removes all generic devices */
   1066 
   1067  	return (0);
   1068 }
   1069 
   1070 #endif
   1071