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