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