Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.32
      1 /*	$NetBSD: usbdi.c,v 1.32 1999/08/23 22:55:14 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #if defined(__NetBSD__) || defined(__OpenBSD__)
     44 #include <sys/device.h>
     45 #else
     46 #include <sys/module.h>
     47 #include <sys/bus.h>
     48 #include <sys/conf.h>
     49 #endif
     50 #include <sys/malloc.h>
     51 #include <sys/proc.h>
     52 
     53 #include <dev/usb/usb.h>
     54 
     55 #include <dev/usb/usbdi.h>
     56 #include <dev/usb/usbdi_util.h>
     57 #include <dev/usb/usbdivar.h>
     58 
     59 #if defined(__FreeBSD__)
     60 #include "usb_if.h"
     61 #endif
     62 
     63 #ifdef USB_DEBUG
     64 #define DPRINTF(x)	if (usbdebug) logprintf x
     65 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     66 extern int usbdebug;
     67 #else
     68 #define DPRINTF(x)
     69 #define DPRINTFN(n,x)
     70 #endif
     71 
     72 static usbd_status usbd_ar_pipe  __P((usbd_pipe_handle pipe));
     73 void usbd_do_request_async_cb
     74 	__P((usbd_request_handle, usbd_private_handle, usbd_status));
     75 void usbd_start_next __P((usbd_pipe_handle pipe));
     76 
     77 static SIMPLEQ_HEAD(, usbd_request) usbd_free_requests;
     78 
     79 #if defined(__FreeBSD__)
     80 #define USB_CDEV_MAJOR	108
     81 
     82 extern struct cdevsw usb_cdevsw;
     83 #endif
     84 
     85 #ifdef USB_DEBUG
     86 void usbd_dump_queue __P((usbd_pipe_handle));
     87 
     88 void
     89 usbd_dump_queue(pipe)
     90 	usbd_pipe_handle pipe;
     91 {
     92 	usbd_request_handle reqh;
     93 
     94 	printf("usbd_dump_queue: pipe=%p\n", pipe);
     95 	for (reqh = SIMPLEQ_FIRST(&pipe->queue);
     96 	     reqh;
     97 	     reqh = SIMPLEQ_NEXT(reqh, next)) {
     98 		printf("  reqh=%p\n", reqh);
     99 	}
    100 }
    101 #endif
    102 
    103 usbd_status
    104 usbd_open_pipe(iface, address, flags, pipe)
    105 	usbd_interface_handle iface;
    106 	u_int8_t address;
    107 	u_int8_t flags;
    108 	usbd_pipe_handle *pipe;
    109 {
    110 	usbd_pipe_handle p;
    111 	struct usbd_endpoint *ep;
    112 	usbd_status r;
    113 	int i;
    114 
    115 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    116 		ep = &iface->endpoints[i];
    117 		if (ep->edesc->bEndpointAddress == address)
    118 			goto found;
    119 	}
    120 	return (USBD_BAD_ADDRESS);
    121  found:
    122 	if ((flags & USBD_EXCLUSIVE_USE) &&
    123 	    ep->refcnt != 0)
    124 		return (USBD_IN_USE);
    125 	r = usbd_setup_pipe(iface->device, iface, ep, &p);
    126 	if (r != USBD_NORMAL_COMPLETION)
    127 		return (r);
    128 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    129 	*pipe = p;
    130 	return (USBD_NORMAL_COMPLETION);
    131 }
    132 
    133 usbd_status
    134 usbd_open_pipe_intr(iface, address, flags, pipe, priv, buffer, length, cb)
    135 	usbd_interface_handle iface;
    136 	u_int8_t address;
    137 	u_int8_t flags;
    138 	usbd_pipe_handle *pipe;
    139 	usbd_private_handle priv;
    140 	void *buffer;
    141 	u_int32_t length;
    142 	usbd_callback cb;
    143 {
    144 	usbd_status r;
    145 	usbd_request_handle reqh;
    146 	usbd_pipe_handle ipipe;
    147 
    148 	reqh = usbd_alloc_request();
    149 	if (reqh == 0)
    150 		return (USBD_NOMEM);
    151 	r = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &ipipe);
    152 	if (r != USBD_NORMAL_COMPLETION)
    153 		goto bad1;
    154 	r = usbd_setup_request(reqh, ipipe, priv, buffer, length,
    155 			       USBD_XFER_IN | flags, USBD_NO_TIMEOUT, cb);
    156 	if (r != USBD_NORMAL_COMPLETION)
    157 		goto bad2;
    158 	ipipe->intrreqh = reqh;
    159 	ipipe->repeat = 1;
    160 	r = usbd_transfer(reqh);
    161 	*pipe = ipipe;
    162 	if (r != USBD_IN_PROGRESS)
    163 		goto bad3;
    164 	return (USBD_NORMAL_COMPLETION);
    165 
    166  bad3:
    167 	ipipe->intrreqh = 0;
    168 	ipipe->repeat = 0;
    169  bad2:
    170 	usbd_close_pipe(ipipe);
    171  bad1:
    172 	usbd_free_request(reqh);
    173 	return r;
    174 }
    175 
    176 usbd_status
    177 usbd_open_pipe_iso(iface, address, flags, pipe, priv, bufsize, nbuf)
    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 	u_int32_t bufsize;
    184 	u_int32_t nbuf;
    185 {
    186 	usbd_status r;
    187 	usbd_pipe_handle p;
    188 
    189 	r = usbd_open_pipe(iface, address, USBD_EXCLUSIVE_USE, &p);
    190 	if (r != USBD_NORMAL_COMPLETION)
    191 		return (r);
    192 	if (!p->methods->isobuf) {
    193 		usbd_close_pipe(p);
    194 		return (USBD_INVAL);
    195 	}
    196 	r = p->methods->isobuf(p, bufsize, nbuf);
    197 	if (r != USBD_NORMAL_COMPLETION) {
    198 		usbd_close_pipe(p);
    199 		return (r);
    200 	}
    201 	*pipe = p;
    202 	return r;
    203 }
    204 
    205 usbd_status
    206 usbd_close_pipe(pipe)
    207 	usbd_pipe_handle pipe;
    208 {
    209 #ifdef DIAGNOSTIC
    210 	if (pipe == 0) {
    211 		printf("usbd_close_pipe: pipe==NULL\n");
    212 		return (USBD_NORMAL_COMPLETION);
    213 	}
    214 #endif
    215 
    216 	if (--pipe->refcnt != 0)
    217 		return (USBD_NORMAL_COMPLETION);
    218 	if (SIMPLEQ_FIRST(&pipe->queue) != 0)
    219 		return (USBD_PENDING_REQUESTS);
    220 	LIST_REMOVE(pipe, next);
    221 	pipe->endpoint->refcnt--;
    222 	pipe->methods->close(pipe);
    223 	if (pipe->intrreqh)
    224 		usbd_free_request(pipe->intrreqh);
    225 	free(pipe, M_USB);
    226 	return (USBD_NORMAL_COMPLETION);
    227 }
    228 
    229 usbd_status
    230 usbd_transfer(reqh)
    231 	usbd_request_handle reqh;
    232 {
    233 	usbd_pipe_handle pipe = reqh->pipe;
    234 	usbd_status r;
    235 	int s;
    236 
    237 	DPRINTFN(5,("usbd_transfer: reqh=%p, flags=%d, pipe=%p, running=%d\n",
    238 		    reqh, reqh->flags, pipe, pipe->running));
    239 #ifdef USB_DEBUG
    240 	if (usbdebug > 5)
    241 		usbd_dump_queue(pipe);
    242 #endif
    243 	reqh->done = 0;
    244 
    245 	r = pipe->methods->transfer(reqh);
    246 	if (!(reqh->flags & USBD_SYNCHRONOUS))
    247 		return r;
    248 
    249 	/* Sync transfer, wait for completion. */
    250 	if (r != USBD_IN_PROGRESS)
    251 		return (r);
    252 	s = splusb();
    253 	if (!reqh->done) {
    254 		if (reqh->pipe->device->bus->use_polling)
    255 			panic("usbd_transfer: not done\n");
    256 		tsleep(reqh, PRIBIO, "usbsyn", 0);
    257 	}
    258 	splx(s);
    259 	return (reqh->status);
    260 }
    261 
    262 /* Like usbd_transfer(), but waits for completion. */
    263 usbd_status
    264 usbd_sync_transfer(reqh)
    265 	usbd_request_handle reqh;
    266 {
    267 	reqh->flags |= USBD_SYNCHRONOUS;
    268 	return (usbd_transfer(reqh));
    269 }
    270 
    271 usbd_request_handle
    272 usbd_alloc_request()
    273 {
    274 	usbd_request_handle reqh;
    275 
    276 	reqh = SIMPLEQ_FIRST(&usbd_free_requests);
    277 	if (reqh)
    278 		SIMPLEQ_REMOVE_HEAD(&usbd_free_requests, reqh, next);
    279 	else
    280 		reqh = malloc(sizeof(*reqh), M_USB, M_NOWAIT);
    281 	if (!reqh)
    282 		return (0);
    283 	memset(reqh, 0, sizeof *reqh);
    284 	DPRINTFN(1,("usbd_alloc_request() = %p\n", reqh));
    285 	return (reqh);
    286 }
    287 
    288 usbd_status
    289 usbd_free_request(reqh)
    290 	usbd_request_handle reqh;
    291 {
    292 	DPRINTFN(1,("usbd_free_request: %p\n", reqh));
    293 	SIMPLEQ_INSERT_HEAD(&usbd_free_requests, reqh, next);
    294 	return (USBD_NORMAL_COMPLETION);
    295 }
    296 
    297 usbd_status
    298 usbd_setup_request(reqh, pipe, priv, buffer, length, flags, timeout, callback)
    299 	usbd_request_handle reqh;
    300 	usbd_pipe_handle pipe;
    301 	usbd_private_handle priv;
    302 	void *buffer;
    303 	u_int32_t length;
    304 	u_int16_t flags;
    305 	u_int32_t timeout;
    306 	void (*callback) __P((usbd_request_handle,
    307 			      usbd_private_handle,
    308 			      usbd_status));
    309 {
    310 	reqh->pipe = pipe;
    311 	reqh->priv = priv;
    312 	reqh->buffer = buffer;
    313 	reqh->length = length;
    314 	reqh->actlen = 0;
    315 	reqh->flags = flags;
    316 	reqh->timeout = timeout;
    317 	reqh->status = USBD_NOT_STARTED;
    318 	reqh->callback = callback;
    319 	reqh->isreq = 0;
    320 	return (USBD_NORMAL_COMPLETION);
    321 }
    322 
    323 usbd_status
    324 usbd_setup_default_request(reqh, dev, priv, timeout, req, buffer,
    325 			   length, flags, callback)
    326 	usbd_request_handle reqh;
    327 	usbd_device_handle dev;
    328 	usbd_private_handle priv;
    329 	u_int32_t timeout;
    330 	usb_device_request_t *req;
    331 	void *buffer;
    332 	u_int32_t length;
    333 	u_int16_t flags;
    334 	void (*callback) __P((usbd_request_handle,
    335 			      usbd_private_handle,
    336 			      usbd_status));
    337 {
    338 	reqh->pipe = dev->default_pipe;
    339 	reqh->priv = priv;
    340 	reqh->buffer = buffer;
    341 	reqh->length = length;
    342 	reqh->actlen = 0;
    343 	reqh->flags = flags;
    344 	reqh->timeout = timeout;
    345 	reqh->status = USBD_NOT_STARTED;
    346 	reqh->callback = callback;
    347 	reqh->request = *req;
    348 	reqh->isreq = 1;
    349 	return (USBD_NORMAL_COMPLETION);
    350 }
    351 
    352 void
    353 usbd_get_request_status(reqh, priv, buffer, count, status)
    354 	usbd_request_handle reqh;
    355 	usbd_private_handle *priv;
    356 	void **buffer;
    357 	u_int32_t *count;
    358 	usbd_status *status;
    359 {
    360 	if (priv)
    361 		*priv = reqh->priv;
    362 	if (buffer)
    363 		*buffer = reqh->buffer;
    364 	if (count)
    365 		*count = reqh->actlen;
    366 	if (status)
    367 		*status = reqh->status;
    368 }
    369 
    370 usb_config_descriptor_t *
    371 usbd_get_config_descriptor(dev)
    372 	usbd_device_handle dev;
    373 {
    374 	return (dev->cdesc);
    375 }
    376 
    377 usb_interface_descriptor_t *
    378 usbd_get_interface_descriptor(iface)
    379 	usbd_interface_handle iface;
    380 {
    381 	return (iface->idesc);
    382 }
    383 
    384 usb_device_descriptor_t *
    385 usbd_get_device_descriptor(dev)
    386 	usbd_device_handle dev;
    387 {
    388 	return (&dev->ddesc);
    389 }
    390 
    391 usb_endpoint_descriptor_t *
    392 usbd_interface2endpoint_descriptor(iface, index)
    393 	usbd_interface_handle iface;
    394 	u_int8_t index;
    395 {
    396 	if (index >= iface->idesc->bNumEndpoints)
    397 		return (0);
    398 	return (iface->endpoints[index].edesc);
    399 }
    400 
    401 usbd_status
    402 usbd_abort_pipe(pipe)
    403 	usbd_pipe_handle pipe;
    404 {
    405 	usbd_status r;
    406 	int s;
    407 
    408 #ifdef DIAGNOSTIC
    409 	if (pipe == 0) {
    410 		printf("usbd_close_pipe: pipe==NULL\n");
    411 		return (USBD_NORMAL_COMPLETION);
    412 	}
    413 #endif
    414 	s = splusb();
    415 	r = usbd_ar_pipe(pipe);
    416 	splx(s);
    417 	return (r);
    418 }
    419 
    420 usbd_status
    421 usbd_clear_endpoint_stall(pipe)
    422 	usbd_pipe_handle pipe;
    423 {
    424 	usbd_device_handle dev = pipe->device;
    425 	usb_device_request_t req;
    426 	usbd_status r;
    427 
    428 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    429 	pipe->methods->cleartoggle(pipe);
    430 	req.bmRequestType = UT_WRITE_ENDPOINT;
    431 	req.bRequest = UR_CLEAR_FEATURE;
    432 	USETW(req.wValue, UF_ENDPOINT_HALT);
    433 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    434 	USETW(req.wLength, 0);
    435 	r = usbd_do_request(dev, &req, 0);
    436 #if 0
    437 XXX should we do this?
    438 	if (r == USBD_NORMAL_COMPLETION) {
    439 		pipe->state = USBD_PIPE_ACTIVE;
    440 		/* XXX activate pipe */
    441 	}
    442 #endif
    443 	return (r);
    444 }
    445 
    446 usbd_status
    447 usbd_clear_endpoint_stall_async(pipe)
    448 	usbd_pipe_handle pipe;
    449 {
    450 	usbd_device_handle dev = pipe->device;
    451 	usb_device_request_t req;
    452 	usbd_status r;
    453 
    454 	pipe->methods->cleartoggle(pipe);
    455 	req.bmRequestType = UT_WRITE_ENDPOINT;
    456 	req.bRequest = UR_CLEAR_FEATURE;
    457 	USETW(req.wValue, UF_ENDPOINT_HALT);
    458 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    459 	USETW(req.wLength, 0);
    460 	r = usbd_do_request_async(dev, &req, 0);
    461 	return (r);
    462 }
    463 
    464 usbd_status
    465 usbd_endpoint_count(iface, count)
    466 	usbd_interface_handle iface;
    467 	u_int8_t *count;
    468 {
    469 	*count = iface->idesc->bNumEndpoints;
    470 	return (USBD_NORMAL_COMPLETION);
    471 }
    472 
    473 usbd_status
    474 usbd_interface_count(dev, count)
    475 	usbd_device_handle dev;
    476 	u_int8_t *count;
    477 {
    478 	if (!dev->cdesc)
    479 		return (USBD_NOT_CONFIGURED);
    480 	*count = dev->cdesc->bNumInterface;
    481 	return (USBD_NORMAL_COMPLETION);
    482 }
    483 
    484 usbd_status
    485 usbd_interface2device_handle(iface, dev)
    486 	usbd_interface_handle iface;
    487 	usbd_device_handle *dev;
    488 {
    489 	*dev = iface->device;
    490 	return (USBD_NORMAL_COMPLETION);
    491 }
    492 
    493 usbd_status
    494 usbd_device2interface_handle(dev, ifaceno, iface)
    495 	usbd_device_handle dev;
    496 	u_int8_t ifaceno;
    497 	usbd_interface_handle *iface;
    498 {
    499 	if (!dev->cdesc)
    500 		return (USBD_NOT_CONFIGURED);
    501 	if (ifaceno >= dev->cdesc->bNumInterface)
    502 		return (USBD_INVAL);
    503 	*iface = &dev->ifaces[ifaceno];
    504 	return (USBD_NORMAL_COMPLETION);
    505 }
    506 
    507 /* XXXX use altno */
    508 usbd_status
    509 usbd_set_interface(iface, altidx)
    510 	usbd_interface_handle iface;
    511 	int altidx;
    512 {
    513 	usb_device_request_t req;
    514 	usbd_status r;
    515 
    516 	if (LIST_FIRST(&iface->pipes) != 0)
    517 		return (USBD_IN_USE);
    518 
    519 	if (iface->endpoints)
    520 		free(iface->endpoints, M_USB);
    521 	iface->endpoints = 0;
    522 	iface->idesc = 0;
    523 
    524 	r = usbd_fill_iface_data(iface->device, iface->index, altidx);
    525 	if (r != USBD_NORMAL_COMPLETION)
    526 		return (r);
    527 
    528 	req.bmRequestType = UT_WRITE_INTERFACE;
    529 	req.bRequest = UR_SET_INTERFACE;
    530 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    531 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    532 	USETW(req.wLength, 0);
    533 	return usbd_do_request(iface->device, &req, 0);
    534 }
    535 
    536 int
    537 usbd_get_no_alts(cdesc, ifaceno)
    538 	usb_config_descriptor_t *cdesc;
    539 	int ifaceno;
    540 {
    541 	char *p = (char *)cdesc;
    542 	char *end = p + UGETW(cdesc->wTotalLength);
    543 	usb_interface_descriptor_t *d;
    544 	int n;
    545 
    546 	for (n = 0; p < end; p += d->bLength) {
    547 		d = (usb_interface_descriptor_t *)p;
    548 		if (p + d->bLength <= end &&
    549 		    d->bDescriptorType == UDESC_INTERFACE &&
    550 		    d->bInterfaceNumber == ifaceno)
    551 			n++;
    552 	}
    553 	return (n);
    554 }
    555 
    556 int
    557 usbd_get_interface_altindex(iface)
    558 	usbd_interface_handle iface;
    559 {
    560 	return (iface->altindex);
    561 }
    562 
    563 usbd_status
    564 usbd_get_interface(iface, aiface)
    565 	usbd_interface_handle iface;
    566 	u_int8_t *aiface;
    567 {
    568 	usb_device_request_t req;
    569 
    570 	req.bmRequestType = UT_READ_INTERFACE;
    571 	req.bRequest = UR_GET_INTERFACE;
    572 	USETW(req.wValue, 0);
    573 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    574 	USETW(req.wLength, 1);
    575 	return usbd_do_request(iface->device, &req, aiface);
    576 }
    577 
    578 /*** Internal routines ***/
    579 
    580 /* Dequeue all pipe operations, called at splusb(). */
    581 static usbd_status
    582 usbd_ar_pipe(pipe)
    583 	usbd_pipe_handle pipe;
    584 {
    585 	usbd_request_handle reqh;
    586 
    587 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    588 #ifdef USB_DEBUG
    589 	if (usbdebug > 5)
    590 		usbd_dump_queue(pipe);
    591 #endif
    592 	while ((reqh = SIMPLEQ_FIRST(&pipe->queue))) {
    593 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p reqh=%p (methods=%p)\n",
    594 			    pipe, reqh, pipe->methods));
    595 		/* Make the HC abort it (and invoke the callback). */
    596 		pipe->methods->abort(reqh);
    597 	}
    598 	return (USBD_NORMAL_COMPLETION);
    599 }
    600 
    601 static int usbd_global_init_done = 0;
    602 
    603 void
    604 usbd_init()
    605 {
    606 #if defined(__FreeBSD__)
    607 	dev_t dev;
    608 #endif
    609 
    610 	if (!usbd_global_init_done) {
    611 		usbd_global_init_done = 1;
    612 		SIMPLEQ_INIT(&usbd_free_requests);
    613 
    614 #if defined(__FreeBSD__)
    615 		dev = makedev(USB_CDEV_MAJOR, 0);
    616 		cdevsw_add(&dev, &usb_cdevsw, NULL);
    617 #endif
    618 	}
    619 }
    620 
    621 void
    622 usb_transfer_complete(reqh)
    623 	usbd_request_handle reqh;
    624 {
    625 	usbd_pipe_handle pipe = reqh->pipe;
    626 	int polling;
    627 
    628 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p reqh=%p actlen=%d\n",
    629 		     pipe, reqh, reqh->actlen));
    630 
    631 #ifdef DIAGNOSTIC
    632 	if (!pipe) {
    633 		printf("usbd_transfer_cb: pipe==0, reqh=%p\n", reqh);
    634 		return;
    635 	}
    636 #endif
    637 	polling = reqh->pipe->device->bus->use_polling;
    638 	/* XXXX */
    639 	if (polling)
    640 		pipe->running = 0;
    641 
    642 	if (reqh->pipe->methods->done)
    643 		reqh->pipe->methods->done(reqh);
    644 
    645 	/* Remove request from queue. */
    646 	SIMPLEQ_REMOVE_HEAD(&pipe->queue, reqh, next);
    647 
    648 	/* Count completed transfers. */
    649 	++pipe->device->bus->stats.requests
    650 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    651 
    652 	reqh->done = 1;
    653 	if (reqh->status == USBD_NORMAL_COMPLETION &&
    654 	    reqh->actlen < reqh->length &&
    655 	    !(reqh->flags & USBD_SHORT_XFER_OK)) {
    656 		DPRINTFN(-1, ("usbd_transfer_cb: short xfer %d<%d (bytes)\n",
    657 			      reqh->actlen, reqh->length));
    658 		reqh->status = USBD_SHORT_XFER;
    659 	}
    660 
    661 	if (reqh->callback)
    662 		reqh->callback(reqh, reqh->priv, reqh->status);
    663 
    664 	if ((reqh->flags & USBD_SYNCHRONOUS) && !polling)
    665 		wakeup(reqh);
    666 
    667 	if (!pipe->repeat &&
    668 	    reqh->status != USBD_CANCELLED && reqh->status != USBD_TIMEOUT)
    669 		usbd_start_next(pipe);
    670 }
    671 
    672 usbd_status
    673 usb_insert_transfer(reqh)
    674 	usbd_request_handle reqh;
    675 {
    676 	usbd_pipe_handle pipe = reqh->pipe;
    677 
    678 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d\n", pipe,
    679 		    pipe->running));
    680 	SIMPLEQ_INSERT_TAIL(&pipe->queue, reqh, next);
    681 	if (pipe->running)
    682 		return (USBD_IN_PROGRESS);
    683 	pipe->running = 1;
    684 	return (USBD_NORMAL_COMPLETION);
    685 }
    686 
    687 void
    688 usbd_start_next(pipe)
    689 	usbd_pipe_handle pipe;
    690 {
    691 	usbd_request_handle reqh;
    692 	usbd_status r;
    693 
    694 	DPRINTFN(10, ("usbd_start_next: pipe=%p\n", pipe));
    695 
    696 #ifdef DIAGNOSTIC
    697 	if (!pipe) {
    698 		printf("usbd_start_next: pipe == 0\n");
    699 		return;
    700 	}
    701 	if (!pipe->methods || !pipe->methods->start) {
    702 		printf("usbd_start_next:  no start method\n");
    703 		return;
    704 	}
    705 #endif
    706 
    707 	/* Get next request in queue. */
    708 	reqh = SIMPLEQ_FIRST(&pipe->queue);
    709 	DPRINTFN(5, ("usbd_start_next: pipe=%p start reqh=%p\n", pipe, reqh));
    710 	if (!reqh)
    711 		pipe->running = 0;
    712 	else {
    713 		r = pipe->methods->start(reqh);
    714 		if (r != USBD_IN_PROGRESS) {
    715 			printf("usbd_start_next: error=%d\n", r);
    716 			pipe->running = 0;
    717 			/* XXX do what? */
    718 		}
    719 	}
    720 }
    721 
    722 usbd_status
    723 usbd_do_request(dev, req, data)
    724 	usbd_device_handle dev;
    725 	usb_device_request_t *req;
    726 	void *data;
    727 {
    728 	return (usbd_do_request_flags(dev, req, data, 0, 0));
    729 }
    730 
    731 usbd_status
    732 usbd_do_request_flags(dev, req, data, flags, actlen)
    733 	usbd_device_handle dev;
    734 	usb_device_request_t *req;
    735 	void *data;
    736 	u_int16_t flags;
    737 	int *actlen;
    738 {
    739 	usbd_request_handle reqh;
    740 	usbd_status r;
    741 
    742 #ifdef DIAGNOSTIC
    743 	if (!curproc) {
    744 		printf("usbd_do_request: not in process context\n");
    745 		return (USBD_XXX);
    746 	}
    747 #endif
    748 
    749 	reqh = usbd_alloc_request();
    750 	if (reqh == 0)
    751 		return (USBD_NOMEM);
    752 	r = usbd_setup_default_request(
    753 		reqh, dev, 0, USBD_DEFAULT_TIMEOUT, req, data,
    754 		UGETW(req->wLength), flags, 0);
    755 	if (r != USBD_NORMAL_COMPLETION)
    756 		goto bad;
    757 	r = usbd_sync_transfer(reqh);
    758 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    759 	if (reqh->actlen > reqh->length)
    760 		printf("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    761 		       "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    762 		       dev->address, reqh->request.bmRequestType,
    763 		       reqh->request.bRequest, UGETW(reqh->request.wValue),
    764 		       UGETW(reqh->request.wIndex),
    765 		       UGETW(reqh->request.wLength),
    766 		       reqh->length, reqh->actlen);
    767 #endif
    768 	if (actlen)
    769 		*actlen = reqh->actlen;
    770 	if (r == USBD_STALLED) {
    771 		/*
    772 		 * The control endpoint has stalled.  Control endpoints
    773 		 * should not halt, but some may do so anyway so clear
    774 		 * any halt condition.
    775 		 */
    776 		usb_device_request_t treq;
    777 		usb_status_t status;
    778 		u_int16_t s;
    779 		usbd_status nr;
    780 
    781 		treq.bmRequestType = UT_READ_ENDPOINT;
    782 		treq.bRequest = UR_GET_STATUS;
    783 		USETW(treq.wValue, 0);
    784 		USETW(treq.wIndex, 0);
    785 		USETW(treq.wLength, sizeof(usb_status_t));
    786 		nr = usbd_setup_default_request(
    787 			reqh, dev, 0, USBD_DEFAULT_TIMEOUT, &treq, &status,
    788 			sizeof(usb_status_t), 0, 0);
    789 		if (nr != USBD_NORMAL_COMPLETION)
    790 			goto bad;
    791 		nr = usbd_sync_transfer(reqh);
    792 		if (nr != USBD_NORMAL_COMPLETION)
    793 			goto bad;
    794 		s = UGETW(status.wStatus);
    795 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
    796 		if (!(s & UES_HALT))
    797 			goto bad;
    798 		treq.bmRequestType = UT_WRITE_ENDPOINT;
    799 		treq.bRequest = UR_CLEAR_FEATURE;
    800 		USETW(treq.wValue, UF_ENDPOINT_HALT);
    801 		USETW(treq.wIndex, 0);
    802 		USETW(treq.wLength, 0);
    803 		nr = usbd_setup_default_request(
    804 			reqh, dev, 0, USBD_DEFAULT_TIMEOUT, &treq, &status,
    805 			0, 0, 0);
    806 		if (nr != USBD_NORMAL_COMPLETION)
    807 			goto bad;
    808 		nr = usbd_sync_transfer(reqh);
    809 		if (nr != USBD_NORMAL_COMPLETION)
    810 			goto bad;
    811 	}
    812 
    813  bad:
    814 	usbd_free_request(reqh);
    815 	return (r);
    816 }
    817 
    818 void
    819 usbd_do_request_async_cb(reqh, priv, status)
    820 	usbd_request_handle reqh;
    821 	usbd_private_handle priv;
    822 	usbd_status status;
    823 {
    824 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    825 	if (reqh->actlen > reqh->length)
    826 		printf("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    827 		       "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    828 		       reqh->pipe->device->address,
    829 		       reqh->request.bmRequestType,
    830 		       reqh->request.bRequest, UGETW(reqh->request.wValue),
    831 		       UGETW(reqh->request.wIndex),
    832 		       UGETW(reqh->request.wLength),
    833 		       reqh->length, reqh->actlen);
    834 #endif
    835 	usbd_free_request(reqh);
    836 }
    837 
    838 /*
    839  * Execute a request without waiting for completion.
    840  * Can be used from interrupt context.
    841  */
    842 usbd_status
    843 usbd_do_request_async(dev, req, data)
    844 	usbd_device_handle dev;
    845 	usb_device_request_t *req;
    846 	void *data;
    847 {
    848 	usbd_request_handle reqh;
    849 	usbd_status r;
    850 
    851 	reqh = usbd_alloc_request();
    852 	if (reqh == 0)
    853 		return (USBD_NOMEM);
    854 	r = usbd_setup_default_request(
    855 		reqh, dev, 0, USBD_DEFAULT_TIMEOUT, req, data,
    856 		UGETW(req->wLength), 0, usbd_do_request_async_cb);
    857 	if (r != USBD_NORMAL_COMPLETION) {
    858 		usbd_free_request(reqh);
    859 		return (r);
    860 	}
    861 	r = usbd_transfer(reqh);
    862 	if (r != USBD_IN_PROGRESS)
    863 		return (r);
    864 	return (USBD_NORMAL_COMPLETION);
    865 }
    866 
    867 struct usbd_quirks *
    868 usbd_get_quirks(dev)
    869 	usbd_device_handle dev;
    870 {
    871 	return (dev->quirks);
    872 }
    873 
    874 /* XXX do periodic free() of free list */
    875 
    876 /*
    877  * Called from keyboard driver when in polling mode.
    878  */
    879 void
    880 usbd_dopoll(iface)
    881 	usbd_interface_handle iface;
    882 {
    883 	iface->device->bus->do_poll(iface->device->bus);
    884 }
    885 
    886 void
    887 usbd_set_polling(iface, on)
    888 	usbd_interface_handle iface;
    889 	int on;
    890 {
    891 	iface->device->bus->use_polling = on;
    892 }
    893 
    894 
    895 usb_endpoint_descriptor_t *
    896 usbd_get_endpoint_descriptor(iface, address)
    897 	usbd_interface_handle iface;
    898 	u_int8_t address;
    899 {
    900 	struct usbd_endpoint *ep;
    901 	int i;
    902 
    903 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    904 		ep = &iface->endpoints[i];
    905 		if (ep->edesc->bEndpointAddress == address)
    906 			return (iface->endpoints[i].edesc);
    907 	}
    908 	return (0);
    909 }
    910 
    911 #if defined(__FreeBSD__)
    912 void
    913 usbd_print_child(device_t parent, device_t child)
    914 {
    915 	/*
    916 	struct usb_softc *sc = device_get_softc(child);
    917 	*/
    918 
    919 	printf(" at %s%d", device_get_name(parent), device_get_unit(parent));
    920 
    921 	/* XXX How do we get to the usbd_device_handle???
    922 	usbd_device_handle dev = invalidadosch;
    923 
    924 	printf(" addr %d", dev->addr);
    925 
    926 	if (bootverbose) {
    927 		if (dev->lowspeed)
    928 			printf(", lowspeed");
    929 		if (dev->self_powered)
    930 			printf(", self powered");
    931 		else
    932 			printf(", %dmA", dev->power);
    933 		printf(", config %d", dev->config);
    934 	}
    935 	 */
    936 }
    937 
    938 /* Reconfigure all the USB busses in the system. */
    939 int
    940 usbd_driver_load(module_t mod, int what, void *arg)
    941 {
    942 	devclass_t usb_devclass = devclass_find("usb");
    943 	devclass_t ugen_devclass = devclass_find("ugen");
    944 	device_t *devlist;
    945 	int devcount;
    946 	int error;
    947 
    948 	switch (what) {
    949 	case MOD_LOAD:
    950 	case MOD_UNLOAD:
    951 		if (!usb_devclass)
    952 			return 0;	/* just ignore call */
    953 
    954 		if (ugen_devclass) {
    955 			/* detach devices from generic driver if possible */
    956 			error = devclass_get_devices(ugen_devclass, &devlist,
    957 						     &devcount);
    958 			if (!error)
    959 				for (devcount--; devcount >= 0; devcount--)
    960 					(void)DEVICE_DETACH(devlist[devcount]);
    961 		}
    962 
    963 		error = devclass_get_devices(usb_devclass, &devlist, &devcount);
    964 		if (error)
    965 			return 0;	/* XXX maybe transient, or error? */
    966 
    967 		for (devcount--; devcount >= 0; devcount--)
    968 			USB_RECONFIGURE(devlist[devcount]);
    969 
    970 		free(devlist, M_TEMP);
    971 		return 0;
    972 	}
    973 
    974 	return 0;			/* nothing to do by us */
    975 }
    976 
    977 /* Set the description of the device including a malloc and copy. */
    978 void
    979 usbd_device_set_desc(device_t device, char *devinfo)
    980 {
    981 	size_t l;
    982 	char *desc;
    983 
    984 	if ( devinfo ) {
    985 		l = strlen(devinfo);
    986 		desc = malloc(l+1, M_USB, M_NOWAIT);
    987 		if (desc)
    988 			memcpy(desc, devinfo, l+1);
    989 	} else
    990 		desc = NULL;
    991 
    992 	device_set_desc(device, desc);
    993 }
    994 
    995 char *
    996 usbd_devname(bdevice *bdev)
    997 {
    998 	static char buf[20];
    999 	/*
   1000 	 * A static buffer is a loss if this routine is used from an interrupt,
   1001 	 * but it's not fatal.
   1002 	 */
   1003 
   1004 	sprintf(buf, "%s%d", device_get_name(*bdev), device_get_unit(*bdev));
   1005 	return (buf);
   1006 }
   1007 
   1008 #endif
   1009