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