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