Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.119.4.1
      1 /*	$NetBSD: usbdi.c,v 1.119.4.1 2007/07/01 21:49:03 ad 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/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.119.4.1 2007/07/01 21:49:03 ad Exp $");
     43 
     44 #include "opt_compat_netbsd.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #if defined(__NetBSD__) || defined(__OpenBSD__)
     49 #include <sys/kernel.h>
     50 #include <sys/device.h>
     51 #elif defined(__FreeBSD__)
     52 #include <sys/module.h>
     53 #include <sys/bus.h>
     54 #include <sys/conf.h>
     55 #include "usb_if.h"
     56 #if defined(DIAGNOSTIC) && defined(__i386__)
     57 #include <machine/cpu.h>
     58 #endif
     59 #endif
     60 #include <sys/malloc.h>
     61 #include <sys/proc.h>
     62 
     63 #include <machine/bus.h>
     64 
     65 #include <dev/usb/usb.h>
     66 #include <dev/usb/usbdi.h>
     67 #include <dev/usb/usbdi_util.h>
     68 #include <dev/usb/usbdivar.h>
     69 #include <dev/usb/usb_mem.h>
     70 #include <dev/usb/usb_quirks.h>
     71 
     72 /* UTF-8 encoding stuff */
     73 #include <fs/unicode.h>
     74 
     75 #ifdef USB_DEBUG
     76 #define DPRINTF(x)	if (usbdebug) logprintf x
     77 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     78 extern int usbdebug;
     79 #else
     80 #define DPRINTF(x)
     81 #define DPRINTFN(n,x)
     82 #endif
     83 
     84 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
     85 Static void usbd_do_request_async_cb
     86 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
     87 Static void usbd_start_next(usbd_pipe_handle pipe);
     88 Static usbd_status usbd_open_pipe_ival
     89 	(usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
     90 
     91 static inline int
     92 usbd_xfer_isread(usbd_xfer_handle xfer)
     93 {
     94 	if (xfer->rqflags & URQ_REQUEST)
     95 		return (xfer->request.bmRequestType & UT_READ);
     96 	else
     97 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
     98 			UE_DIR_IN);
     99 }
    100 
    101 #ifdef USB_DEBUG
    102 void
    103 usbd_dump_iface(struct usbd_interface *iface)
    104 {
    105 	printf("usbd_dump_iface: iface=%p\n", iface);
    106 	if (iface == NULL)
    107 		return;
    108 	printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
    109 	       iface->device, iface->idesc, iface->index, iface->altindex,
    110 	       iface->priv);
    111 }
    112 
    113 void
    114 usbd_dump_device(struct usbd_device *dev)
    115 {
    116 	printf("usbd_dump_device: dev=%p\n", dev);
    117 	if (dev == NULL)
    118 		return;
    119 	printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
    120 	printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
    121 	       "power=%d langid=%d\n",
    122 	       dev->address, dev->config, dev->depth, dev->speed,
    123 	       dev->self_powered, dev->power, dev->langid);
    124 }
    125 
    126 void
    127 usbd_dump_endpoint(struct usbd_endpoint *endp)
    128 {
    129 	printf("usbd_dump_endpoint: endp=%p\n", endp);
    130 	if (endp == NULL)
    131 		return;
    132 	printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
    133 	if (endp->edesc)
    134 		printf(" bEndpointAddress=0x%02x\n",
    135 		       endp->edesc->bEndpointAddress);
    136 }
    137 
    138 void
    139 usbd_dump_queue(usbd_pipe_handle pipe)
    140 {
    141 	usbd_xfer_handle xfer;
    142 
    143 	printf("usbd_dump_queue: pipe=%p\n", pipe);
    144 	SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
    145 		printf("  xfer=%p\n", xfer);
    146 	}
    147 }
    148 
    149 void
    150 usbd_dump_pipe(usbd_pipe_handle pipe)
    151 {
    152 	printf("usbd_dump_pipe: pipe=%p\n", pipe);
    153 	if (pipe == NULL)
    154 		return;
    155 	usbd_dump_iface(pipe->iface);
    156 	usbd_dump_device(pipe->device);
    157 	usbd_dump_endpoint(pipe->endpoint);
    158 	printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
    159 	       pipe->refcnt, pipe->running, pipe->aborting);
    160 	printf(" intrxfer=%p, repeat=%d, interval=%d\n",
    161 	       pipe->intrxfer, pipe->repeat, pipe->interval);
    162 }
    163 #endif
    164 
    165 usbd_status
    166 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
    167 	       u_int8_t flags, usbd_pipe_handle *pipe)
    168 {
    169 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    170 				    USBD_DEFAULT_INTERVAL));
    171 }
    172 
    173 usbd_status
    174 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
    175 		    u_int8_t flags, usbd_pipe_handle *pipe, int ival)
    176 {
    177 	usbd_pipe_handle p;
    178 	struct usbd_endpoint *ep;
    179 	usbd_status err;
    180 	int i;
    181 
    182 	DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
    183 		    iface, address, flags));
    184 
    185 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    186 		ep = &iface->endpoints[i];
    187 		if (ep->edesc == NULL)
    188 			return (USBD_IOERROR);
    189 		if (ep->edesc->bEndpointAddress == address)
    190 			goto found;
    191 	}
    192 	return (USBD_BAD_ADDRESS);
    193  found:
    194 	if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
    195 		return (USBD_IN_USE);
    196 	err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
    197 	if (err)
    198 		return (err);
    199 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    200 	*pipe = p;
    201 	return (USBD_NORMAL_COMPLETION);
    202 }
    203 
    204 usbd_status
    205 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
    206 		    u_int8_t flags, usbd_pipe_handle *pipe,
    207 		    usbd_private_handle priv, void *buffer, u_int32_t len,
    208 		    usbd_callback cb, int ival)
    209 {
    210 	usbd_status err;
    211 	usbd_xfer_handle xfer;
    212 	usbd_pipe_handle ipipe;
    213 
    214 	DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
    215 		    address, flags, len));
    216 
    217 	err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
    218 				  &ipipe, ival);
    219 	if (err)
    220 		return (err);
    221 	xfer = usbd_alloc_xfer(iface->device);
    222 	if (xfer == NULL) {
    223 		err = USBD_NOMEM;
    224 		goto bad1;
    225 	}
    226 	usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
    227 	    USBD_NO_TIMEOUT, cb);
    228 	ipipe->intrxfer = xfer;
    229 	ipipe->repeat = 1;
    230 	err = usbd_transfer(xfer);
    231 	*pipe = ipipe;
    232 	if (err != USBD_IN_PROGRESS)
    233 		goto bad2;
    234 	return (USBD_NORMAL_COMPLETION);
    235 
    236  bad2:
    237 	ipipe->intrxfer = NULL;
    238 	ipipe->repeat = 0;
    239 	usbd_free_xfer(xfer);
    240  bad1:
    241 	usbd_close_pipe(ipipe);
    242 	return (err);
    243 }
    244 
    245 usbd_status
    246 usbd_close_pipe(usbd_pipe_handle pipe)
    247 {
    248 #ifdef DIAGNOSTIC
    249 	if (pipe == NULL) {
    250 		printf("usbd_close_pipe: pipe==NULL\n");
    251 		return (USBD_NORMAL_COMPLETION);
    252 	}
    253 #endif
    254 
    255 	if (--pipe->refcnt != 0)
    256 		return (USBD_NORMAL_COMPLETION);
    257 	if (! SIMPLEQ_EMPTY(&pipe->queue))
    258 		return (USBD_PENDING_REQUESTS);
    259 	LIST_REMOVE(pipe, next);
    260 	pipe->endpoint->refcnt--;
    261 	pipe->methods->close(pipe);
    262 	if (pipe->intrxfer != NULL)
    263 		usbd_free_xfer(pipe->intrxfer);
    264 	free(pipe, M_USB);
    265 	return (USBD_NORMAL_COMPLETION);
    266 }
    267 
    268 usbd_status
    269 usbd_transfer(usbd_xfer_handle xfer)
    270 {
    271 	usbd_pipe_handle pipe = xfer->pipe;
    272 	usb_dma_t *dmap = &xfer->dmabuf;
    273 	usbd_status err;
    274 	u_int size;
    275 	int s;
    276 
    277 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%d, pipe=%p, running=%d\n",
    278 		    xfer, xfer->flags, pipe, pipe->running));
    279 #ifdef USB_DEBUG
    280 	if (usbdebug > 5)
    281 		usbd_dump_queue(pipe);
    282 #endif
    283 	xfer->done = 0;
    284 
    285 	if (pipe->aborting)
    286 		return (USBD_CANCELLED);
    287 
    288 	size = xfer->length;
    289 	/* If there is no buffer, allocate one. */
    290 	if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
    291 		struct usbd_bus *bus = pipe->device->bus;
    292 
    293 #ifdef DIAGNOSTIC
    294 		if (xfer->rqflags & URQ_AUTO_DMABUF)
    295 			printf("usbd_transfer: has old buffer!\n");
    296 #endif
    297 		err = bus->methods->allocm(bus, dmap, size);
    298 		if (err)
    299 			return (err);
    300 		xfer->rqflags |= URQ_AUTO_DMABUF;
    301 	}
    302 
    303 	/* Copy data if going out. */
    304 	if (!(xfer->flags & USBD_NO_COPY) && size != 0 &&
    305 	    !usbd_xfer_isread(xfer))
    306 		memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
    307 
    308 	err = pipe->methods->transfer(xfer);
    309 
    310 	if (err != USBD_IN_PROGRESS && err) {
    311 		/* The transfer has not been queued, so free buffer. */
    312 		if (xfer->rqflags & URQ_AUTO_DMABUF) {
    313 			struct usbd_bus *bus = pipe->device->bus;
    314 
    315 			bus->methods->freem(bus, &xfer->dmabuf);
    316 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    317 		}
    318 	}
    319 
    320 	if (!(xfer->flags & USBD_SYNCHRONOUS))
    321 		return (err);
    322 
    323 	/* Sync transfer, wait for completion. */
    324 	if (err != USBD_IN_PROGRESS)
    325 		return (err);
    326 	s = splusb();
    327 	if (!xfer->done) {
    328 		if (pipe->device->bus->use_polling)
    329 			panic("usbd_transfer: not done");
    330 		tsleep(xfer, PRIBIO, "usbsyn", 0);
    331 	}
    332 	splx(s);
    333 	return (xfer->status);
    334 }
    335 
    336 /* Like usbd_transfer(), but waits for completion. */
    337 usbd_status
    338 usbd_sync_transfer(usbd_xfer_handle xfer)
    339 {
    340 	xfer->flags |= USBD_SYNCHRONOUS;
    341 	return (usbd_transfer(xfer));
    342 }
    343 
    344 void *
    345 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
    346 {
    347 	struct usbd_bus *bus = xfer->device->bus;
    348 	usbd_status err;
    349 
    350 #ifdef DIAGNOSTIC
    351 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    352 		printf("usbd_alloc_buffer: xfer already has a buffer\n");
    353 #endif
    354 	err = bus->methods->allocm(bus, &xfer->dmabuf, size);
    355 	if (err)
    356 		return (NULL);
    357 	xfer->rqflags |= URQ_DEV_DMABUF;
    358 	return (KERNADDR(&xfer->dmabuf, 0));
    359 }
    360 
    361 void
    362 usbd_free_buffer(usbd_xfer_handle xfer)
    363 {
    364 #ifdef DIAGNOSTIC
    365 	if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
    366 		printf("usbd_free_buffer: no buffer\n");
    367 		return;
    368 	}
    369 #endif
    370 	xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
    371 	xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
    372 }
    373 
    374 void *
    375 usbd_get_buffer(usbd_xfer_handle xfer)
    376 {
    377 	if (!(xfer->rqflags & URQ_DEV_DMABUF))
    378 		return (0);
    379 	return (KERNADDR(&xfer->dmabuf, 0));
    380 }
    381 
    382 usbd_xfer_handle
    383 usbd_alloc_xfer(usbd_device_handle dev)
    384 {
    385 	usbd_xfer_handle xfer;
    386 
    387 	xfer = dev->bus->methods->allocx(dev->bus);
    388 	if (xfer == NULL)
    389 		return (NULL);
    390 	xfer->device = dev;
    391 	usb_callout_init(xfer->timeout_handle);
    392 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
    393 	return (xfer);
    394 }
    395 
    396 usbd_status
    397 usbd_free_xfer(usbd_xfer_handle xfer)
    398 {
    399 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
    400 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    401 		usbd_free_buffer(xfer);
    402 #if defined(__NetBSD__)
    403 	callout_stop(&xfer->timeout_handle);
    404 	callout_destroy(&xfer->timeout_handle);
    405 #endif
    406 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
    407 	return (USBD_NORMAL_COMPLETION);
    408 }
    409 
    410 void
    411 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    412 		usbd_private_handle priv, void *buffer, u_int32_t length,
    413 		u_int16_t flags, u_int32_t timeout,
    414 		usbd_callback callback)
    415 {
    416 	xfer->pipe = pipe;
    417 	xfer->priv = priv;
    418 	xfer->buffer = buffer;
    419 	xfer->length = length;
    420 	xfer->actlen = 0;
    421 	xfer->flags = flags;
    422 	xfer->timeout = timeout;
    423 	xfer->status = USBD_NOT_STARTED;
    424 	xfer->callback = callback;
    425 	xfer->rqflags &= ~URQ_REQUEST;
    426 	xfer->nframes = 0;
    427 }
    428 
    429 void
    430 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
    431 			usbd_private_handle priv, u_int32_t timeout,
    432 			usb_device_request_t *req, void *buffer,
    433 			u_int32_t length, u_int16_t flags,
    434 			usbd_callback callback)
    435 {
    436 	xfer->pipe = dev->default_pipe;
    437 	xfer->priv = priv;
    438 	xfer->buffer = buffer;
    439 	xfer->length = length;
    440 	xfer->actlen = 0;
    441 	xfer->flags = flags;
    442 	xfer->timeout = timeout;
    443 	xfer->status = USBD_NOT_STARTED;
    444 	xfer->callback = callback;
    445 	xfer->request = *req;
    446 	xfer->rqflags |= URQ_REQUEST;
    447 	xfer->nframes = 0;
    448 }
    449 
    450 void
    451 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    452 		     usbd_private_handle priv, u_int16_t *frlengths,
    453 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
    454 {
    455 	xfer->pipe = pipe;
    456 	xfer->priv = priv;
    457 	xfer->buffer = 0;
    458 	xfer->length = 0;
    459 	xfer->actlen = 0;
    460 	xfer->flags = flags;
    461 	xfer->timeout = USBD_NO_TIMEOUT;
    462 	xfer->status = USBD_NOT_STARTED;
    463 	xfer->callback = callback;
    464 	xfer->rqflags &= ~URQ_REQUEST;
    465 	xfer->frlengths = frlengths;
    466 	xfer->nframes = nframes;
    467 }
    468 
    469 void
    470 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
    471 		     void **buffer, u_int32_t *count, usbd_status *status)
    472 {
    473 	if (priv != NULL)
    474 		*priv = xfer->priv;
    475 	if (buffer != NULL)
    476 		*buffer = xfer->buffer;
    477 	if (count != NULL)
    478 		*count = xfer->actlen;
    479 	if (status != NULL)
    480 		*status = xfer->status;
    481 }
    482 
    483 usb_config_descriptor_t *
    484 usbd_get_config_descriptor(usbd_device_handle dev)
    485 {
    486 #ifdef DIAGNOSTIC
    487 	if (dev == NULL) {
    488 		printf("usbd_get_config_descriptor: dev == NULL\n");
    489 		return (NULL);
    490 	}
    491 #endif
    492 	return (dev->cdesc);
    493 }
    494 
    495 usb_interface_descriptor_t *
    496 usbd_get_interface_descriptor(usbd_interface_handle iface)
    497 {
    498 #ifdef DIAGNOSTIC
    499 	if (iface == NULL) {
    500 		printf("usbd_get_interface_descriptor: dev == NULL\n");
    501 		return (NULL);
    502 	}
    503 #endif
    504 	return (iface->idesc);
    505 }
    506 
    507 usb_device_descriptor_t *
    508 usbd_get_device_descriptor(usbd_device_handle dev)
    509 {
    510 	return (&dev->ddesc);
    511 }
    512 
    513 usb_endpoint_descriptor_t *
    514 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
    515 {
    516 	if (index >= iface->idesc->bNumEndpoints)
    517 		return (0);
    518 	return (iface->endpoints[index].edesc);
    519 }
    520 
    521 usbd_status
    522 usbd_abort_pipe(usbd_pipe_handle pipe)
    523 {
    524 	usbd_status err;
    525 	int s;
    526 
    527 #ifdef DIAGNOSTIC
    528 	if (pipe == NULL) {
    529 		printf("usbd_close_pipe: pipe==NULL\n");
    530 		return (USBD_NORMAL_COMPLETION);
    531 	}
    532 #endif
    533 	s = splusb();
    534 	err = usbd_ar_pipe(pipe);
    535 	splx(s);
    536 	return (err);
    537 }
    538 
    539 usbd_status
    540 usbd_clear_endpoint_stall(usbd_pipe_handle pipe)
    541 {
    542 	usbd_device_handle dev = pipe->device;
    543 	usb_device_request_t req;
    544 	usbd_status err;
    545 
    546 	DPRINTFN(8, ("usbd_clear_endpoint_stall\n"));
    547 
    548 	/*
    549 	 * Clearing en endpoint stall resets the endpoint toggle, so
    550 	 * do the same to the HC toggle.
    551 	 */
    552 	pipe->methods->cleartoggle(pipe);
    553 
    554 	req.bmRequestType = UT_WRITE_ENDPOINT;
    555 	req.bRequest = UR_CLEAR_FEATURE;
    556 	USETW(req.wValue, UF_ENDPOINT_HALT);
    557 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    558 	USETW(req.wLength, 0);
    559 	err = usbd_do_request(dev, &req, 0);
    560 #if 0
    561 XXX should we do this?
    562 	if (!err) {
    563 		pipe->state = USBD_PIPE_ACTIVE;
    564 		/* XXX activate pipe */
    565 	}
    566 #endif
    567 	return (err);
    568 }
    569 
    570 usbd_status
    571 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe)
    572 {
    573 	usbd_device_handle dev = pipe->device;
    574 	usb_device_request_t req;
    575 	usbd_status err;
    576 
    577 	pipe->methods->cleartoggle(pipe);
    578 
    579 	req.bmRequestType = UT_WRITE_ENDPOINT;
    580 	req.bRequest = UR_CLEAR_FEATURE;
    581 	USETW(req.wValue, UF_ENDPOINT_HALT);
    582 	USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress);
    583 	USETW(req.wLength, 0);
    584 	err = usbd_do_request_async(dev, &req, 0);
    585 	return (err);
    586 }
    587 
    588 void
    589 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe)
    590 {
    591 	pipe->methods->cleartoggle(pipe);
    592 }
    593 
    594 usbd_status
    595 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count)
    596 {
    597 #ifdef DIAGNOSTIC
    598 	if (iface == NULL || iface->idesc == NULL) {
    599 		printf("usbd_endpoint_count: NULL pointer\n");
    600 		return (USBD_INVAL);
    601 	}
    602 #endif
    603 	*count = iface->idesc->bNumEndpoints;
    604 	return (USBD_NORMAL_COMPLETION);
    605 }
    606 
    607 usbd_status
    608 usbd_interface_count(usbd_device_handle dev, u_int8_t *count)
    609 {
    610 	if (dev->cdesc == NULL)
    611 		return (USBD_NOT_CONFIGURED);
    612 	*count = dev->cdesc->bNumInterface;
    613 	return (USBD_NORMAL_COMPLETION);
    614 }
    615 
    616 void
    617 usbd_interface2device_handle(usbd_interface_handle iface,
    618 			     usbd_device_handle *dev)
    619 {
    620 	*dev = iface->device;
    621 }
    622 
    623 usbd_status
    624 usbd_device2interface_handle(usbd_device_handle dev,
    625 			     u_int8_t ifaceno, usbd_interface_handle *iface)
    626 {
    627 	if (dev->cdesc == NULL)
    628 		return (USBD_NOT_CONFIGURED);
    629 	if (ifaceno >= dev->cdesc->bNumInterface)
    630 		return (USBD_INVAL);
    631 	*iface = &dev->ifaces[ifaceno];
    632 	return (USBD_NORMAL_COMPLETION);
    633 }
    634 
    635 usbd_device_handle
    636 usbd_pipe2device_handle(usbd_pipe_handle pipe)
    637 {
    638 	return (pipe->device);
    639 }
    640 
    641 /* XXXX use altno */
    642 usbd_status
    643 usbd_set_interface(usbd_interface_handle iface, int altidx)
    644 {
    645 	usb_device_request_t req;
    646 	usbd_status err;
    647 	void *endpoints;
    648 
    649 	if (LIST_FIRST(&iface->pipes) != 0)
    650 		return (USBD_IN_USE);
    651 
    652 	endpoints = iface->endpoints;
    653 	err = usbd_fill_iface_data(iface->device, iface->index, altidx);
    654 	if (err)
    655 		return (err);
    656 
    657 	/* new setting works, we can free old endpoints */
    658 	if (endpoints != NULL)
    659 		free(endpoints, M_USB);
    660 
    661 #ifdef DIAGNOSTIC
    662 	if (iface->idesc == NULL) {
    663 		printf("usbd_set_interface: NULL pointer\n");
    664 		return (USBD_INVAL);
    665 	}
    666 #endif
    667 
    668 	req.bmRequestType = UT_WRITE_INTERFACE;
    669 	req.bRequest = UR_SET_INTERFACE;
    670 	USETW(req.wValue, iface->idesc->bAlternateSetting);
    671 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    672 	USETW(req.wLength, 0);
    673 	return (usbd_do_request(iface->device, &req, 0));
    674 }
    675 
    676 int
    677 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    678 {
    679 	char *p = (char *)cdesc;
    680 	char *end = p + UGETW(cdesc->wTotalLength);
    681 	usb_interface_descriptor_t *d;
    682 	int n;
    683 
    684 	for (n = 0; p < end; p += d->bLength) {
    685 		d = (usb_interface_descriptor_t *)p;
    686 		if (p + d->bLength <= end &&
    687 		    d->bDescriptorType == UDESC_INTERFACE &&
    688 		    d->bInterfaceNumber == ifaceno)
    689 			n++;
    690 	}
    691 	return (n);
    692 }
    693 
    694 int
    695 usbd_get_interface_altindex(usbd_interface_handle iface)
    696 {
    697 	return (iface->altindex);
    698 }
    699 
    700 usbd_status
    701 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface)
    702 {
    703 	usb_device_request_t req;
    704 
    705 	req.bmRequestType = UT_READ_INTERFACE;
    706 	req.bRequest = UR_GET_INTERFACE;
    707 	USETW(req.wValue, 0);
    708 	USETW(req.wIndex, iface->idesc->bInterfaceNumber);
    709 	USETW(req.wLength, 1);
    710 	return (usbd_do_request(iface->device, &req, aiface));
    711 }
    712 
    713 /*** Internal routines ***/
    714 
    715 /* Dequeue all pipe operations, called at splusb(). */
    716 Static usbd_status
    717 usbd_ar_pipe(usbd_pipe_handle pipe)
    718 {
    719 	usbd_xfer_handle xfer;
    720 
    721 	SPLUSBCHECK;
    722 
    723 	DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe));
    724 #ifdef USB_DEBUG
    725 	if (usbdebug > 5)
    726 		usbd_dump_queue(pipe);
    727 #endif
    728 	pipe->repeat = 0;
    729 	pipe->aborting = 1;
    730 	while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) {
    731 		DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n",
    732 			    pipe, xfer, pipe->methods));
    733 		/* Make the HC abort it (and invoke the callback). */
    734 		pipe->methods->abort(xfer);
    735 		/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    736 	}
    737 	pipe->aborting = 0;
    738 	return (USBD_NORMAL_COMPLETION);
    739 }
    740 
    741 /* Called at splusb() */
    742 void
    743 usb_transfer_complete(usbd_xfer_handle xfer)
    744 {
    745 	usbd_pipe_handle pipe = xfer->pipe;
    746 	usb_dma_t *dmap = &xfer->dmabuf;
    747 	int sync = xfer->flags & USBD_SYNCHRONOUS;
    748 	int erred = xfer->status == USBD_CANCELLED ||
    749 	    xfer->status == USBD_TIMEOUT;
    750 	int repeat, polling;
    751 
    752 	SPLUSBCHECK;
    753 
    754 	DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d "
    755 		     "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen));
    756 #ifdef DIAGNOSTIC
    757 	if (xfer->busy_free != XFER_ONQU) {
    758 		printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n",
    759 		       xfer, xfer->busy_free);
    760 	}
    761 #endif
    762 
    763 #ifdef DIAGNOSTIC
    764 	if (pipe == NULL) {
    765 		printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
    766 		return;
    767 	}
    768 #endif
    769 	repeat = pipe->repeat;
    770 	polling = pipe->device->bus->use_polling;
    771 	/* XXXX */
    772 	if (polling)
    773 		pipe->running = 0;
    774 
    775 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    776 	    usbd_xfer_isread(xfer)) {
    777 #ifdef DIAGNOSTIC
    778 		if (xfer->actlen > xfer->length) {
    779 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    780 			       xfer->actlen, xfer->length);
    781 			xfer->actlen = xfer->length;
    782 		}
    783 #endif
    784 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
    785 	}
    786 
    787 	/* if we allocated the buffer in usbd_transfer() we free it here. */
    788 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
    789 		if (!repeat) {
    790 			struct usbd_bus *bus = pipe->device->bus;
    791 			bus->methods->freem(bus, dmap);
    792 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    793 		}
    794 	}
    795 
    796 	if (!repeat) {
    797 		/* Remove request from queue. */
    798 #ifdef DIAGNOSTIC
    799 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
    800 			printf("usb_transfer_complete: bad dequeue %p != %p\n",
    801 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
    802 		xfer->busy_free = XFER_BUSY;
    803 #endif
    804 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
    805 	}
    806 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
    807 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
    808 
    809 	/* Count completed transfers. */
    810 	++pipe->device->bus->stats.uds_requests
    811 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    812 
    813 	xfer->done = 1;
    814 	if (!xfer->status && xfer->actlen < xfer->length &&
    815 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
    816 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
    817 			     xfer->actlen, xfer->length));
    818 		xfer->status = USBD_SHORT_XFER;
    819 	}
    820 
    821 	if (xfer->callback)
    822 		xfer->callback(xfer, xfer->priv, xfer->status);
    823 
    824 #ifdef DIAGNOSTIC
    825 	if (pipe->methods->done != NULL)
    826 		pipe->methods->done(xfer);
    827 	else
    828 		printf("usb_transfer_complete: pipe->methods->done == NULL\n");
    829 #else
    830 	pipe->methods->done(xfer);
    831 #endif
    832 
    833 	if (sync && !polling)
    834 		wakeup(xfer);
    835 
    836 	if (!repeat) {
    837 		/* XXX should we stop the queue on all errors? */
    838 		if (erred && pipe->iface != NULL)	/* not control pipe */
    839 			pipe->running = 0;
    840 		else
    841 			usbd_start_next(pipe);
    842 	}
    843 }
    844 
    845 usbd_status
    846 usb_insert_transfer(usbd_xfer_handle xfer)
    847 {
    848 	usbd_pipe_handle pipe = xfer->pipe;
    849 	usbd_status err;
    850 	int s;
    851 
    852 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    853 		    pipe, pipe->running, xfer->timeout));
    854 #ifdef DIAGNOSTIC
    855 	if (xfer->busy_free != XFER_BUSY) {
    856 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
    857 		       xfer, xfer->busy_free);
    858 		return (USBD_INVAL);
    859 	}
    860 	xfer->busy_free = XFER_ONQU;
    861 #endif
    862 	s = splusb();
    863 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    864 	if (pipe->running)
    865 		err = USBD_IN_PROGRESS;
    866 	else {
    867 		pipe->running = 1;
    868 		err = USBD_NORMAL_COMPLETION;
    869 	}
    870 	splx(s);
    871 	return (err);
    872 }
    873 
    874 /* Called at splusb() */
    875 void
    876 usbd_start_next(usbd_pipe_handle pipe)
    877 {
    878 	usbd_xfer_handle xfer;
    879 	usbd_status err;
    880 
    881 	SPLUSBCHECK;
    882 
    883 #ifdef DIAGNOSTIC
    884 	if (pipe == NULL) {
    885 		printf("usbd_start_next: pipe == NULL\n");
    886 		return;
    887 	}
    888 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    889 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    890 		return;
    891 	}
    892 #endif
    893 
    894 	/* Get next request in queue. */
    895 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    896 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    897 	if (xfer == NULL) {
    898 		pipe->running = 0;
    899 	} else {
    900 		err = pipe->methods->start(xfer);
    901 		if (err != USBD_IN_PROGRESS) {
    902 			printf("usbd_start_next: error=%d\n", err);
    903 			pipe->running = 0;
    904 			/* XXX do what? */
    905 		}
    906 	}
    907 }
    908 
    909 usbd_status
    910 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
    911 {
    912 	return (usbd_do_request_flags(dev, req, data, 0, 0,
    913 				      USBD_DEFAULT_TIMEOUT));
    914 }
    915 
    916 usbd_status
    917 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
    918 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
    919 {
    920 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
    921 					   data, flags, actlen, timo));
    922 }
    923 
    924 usbd_status
    925 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
    926 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
    927 	u_int32_t timeout)
    928 {
    929 	usbd_xfer_handle xfer;
    930 	usbd_status err;
    931 
    932 #ifdef DIAGNOSTIC
    933 #if defined(__i386__) && defined(__FreeBSD__)
    934 	KASSERT(intr_nesting_level == 0,
    935 	       	("usbd_do_request: in interrupt context"));
    936 #endif
    937 	if (dev->bus->intr_context) {
    938 		printf("usbd_do_request: not in process context\n");
    939 		return (USBD_INVAL);
    940 	}
    941 #endif
    942 
    943 	xfer = usbd_alloc_xfer(dev);
    944 	if (xfer == NULL)
    945 		return (USBD_NOMEM);
    946 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
    947 				data, UGETW(req->wLength), flags, 0);
    948 	xfer->pipe = pipe;
    949 	err = usbd_sync_transfer(xfer);
    950 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    951 	if (xfer->actlen > xfer->length) {
    952 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    953 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    954 			 dev->address, xfer->request.bmRequestType,
    955 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    956 			 UGETW(xfer->request.wIndex),
    957 			 UGETW(xfer->request.wLength),
    958 			 xfer->length, xfer->actlen));
    959 	}
    960 #endif
    961 	if (actlen != NULL)
    962 		*actlen = xfer->actlen;
    963 	if (err == USBD_STALLED) {
    964 		/*
    965 		 * The control endpoint has stalled.  Control endpoints
    966 		 * should not halt, but some may do so anyway so clear
    967 		 * any halt condition.
    968 		 */
    969 		usb_device_request_t treq;
    970 		usb_status_t status;
    971 		u_int16_t s;
    972 		usbd_status nerr;
    973 
    974 		treq.bmRequestType = UT_READ_ENDPOINT;
    975 		treq.bRequest = UR_GET_STATUS;
    976 		USETW(treq.wValue, 0);
    977 		USETW(treq.wIndex, 0);
    978 		USETW(treq.wLength, sizeof(usb_status_t));
    979 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    980 					   &treq, &status,sizeof(usb_status_t),
    981 					   0, 0);
    982 		nerr = usbd_sync_transfer(xfer);
    983 		if (nerr)
    984 			goto bad;
    985 		s = UGETW(status.wStatus);
    986 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
    987 		if (!(s & UES_HALT))
    988 			goto bad;
    989 		treq.bmRequestType = UT_WRITE_ENDPOINT;
    990 		treq.bRequest = UR_CLEAR_FEATURE;
    991 		USETW(treq.wValue, UF_ENDPOINT_HALT);
    992 		USETW(treq.wIndex, 0);
    993 		USETW(treq.wLength, 0);
    994 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    995 					   &treq, &status, 0, 0, 0);
    996 		nerr = usbd_sync_transfer(xfer);
    997 		if (nerr)
    998 			goto bad;
    999 	}
   1000 
   1001  bad:
   1002 	usbd_free_xfer(xfer);
   1003 	return (err);
   1004 }
   1005 
   1006 void
   1007 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1008     usbd_private_handle priv, usbd_status status)
   1009 {
   1010 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1011 	if (xfer->actlen > xfer->length) {
   1012 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1013 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1014 			 xfer->pipe->device->address,
   1015 			 xfer->request.bmRequestType,
   1016 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1017 			 UGETW(xfer->request.wIndex),
   1018 			 UGETW(xfer->request.wLength),
   1019 			 xfer->length, xfer->actlen));
   1020 	}
   1021 #endif
   1022 	usbd_free_xfer(xfer);
   1023 }
   1024 
   1025 /*
   1026  * Execute a request without waiting for completion.
   1027  * Can be used from interrupt context.
   1028  */
   1029 usbd_status
   1030 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1031 		      void *data)
   1032 {
   1033 	usbd_xfer_handle xfer;
   1034 	usbd_status err;
   1035 
   1036 	xfer = usbd_alloc_xfer(dev);
   1037 	if (xfer == NULL)
   1038 		return (USBD_NOMEM);
   1039 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1040 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1041 	err = usbd_transfer(xfer);
   1042 	if (err != USBD_IN_PROGRESS) {
   1043 		usbd_free_xfer(xfer);
   1044 		return (err);
   1045 	}
   1046 	return (USBD_NORMAL_COMPLETION);
   1047 }
   1048 
   1049 const struct usbd_quirks *
   1050 usbd_get_quirks(usbd_device_handle dev)
   1051 {
   1052 #ifdef DIAGNOSTIC
   1053 	if (dev == NULL) {
   1054 		printf("usbd_get_quirks: dev == NULL\n");
   1055 		return 0;
   1056 	}
   1057 #endif
   1058 	return (dev->quirks);
   1059 }
   1060 
   1061 /* XXX do periodic free() of free list */
   1062 
   1063 /*
   1064  * Called from keyboard driver when in polling mode.
   1065  */
   1066 void
   1067 usbd_dopoll(usbd_interface_handle iface)
   1068 {
   1069 	iface->device->bus->methods->do_poll(iface->device->bus);
   1070 }
   1071 
   1072 void
   1073 usbd_set_polling(usbd_device_handle dev, int on)
   1074 {
   1075 	if (on)
   1076 		dev->bus->use_polling++;
   1077 	else
   1078 		dev->bus->use_polling--;
   1079 	/* When polling we need to make sure there is nothing pending to do. */
   1080 	if (dev->bus->use_polling)
   1081 		dev->bus->methods->soft_intr(dev->bus);
   1082 }
   1083 
   1084 
   1085 usb_endpoint_descriptor_t *
   1086 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1087 {
   1088 	struct usbd_endpoint *ep;
   1089 	int i;
   1090 
   1091 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1092 		ep = &iface->endpoints[i];
   1093 		if (ep->edesc->bEndpointAddress == address)
   1094 			return (iface->endpoints[i].edesc);
   1095 	}
   1096 	return (0);
   1097 }
   1098 
   1099 /*
   1100  * usbd_ratecheck() can limit the number of error messages that occurs.
   1101  * When a device is unplugged it may take up to 0.25s for the hub driver
   1102  * to notice it.  If the driver continuosly tries to do I/O operations
   1103  * this can generate a large number of messages.
   1104  */
   1105 int
   1106 usbd_ratecheck(struct timeval *last)
   1107 {
   1108 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1109 
   1110 	return (ratecheck(last, &errinterval));
   1111 }
   1112 
   1113 /*
   1114  * Search for a vendor/product pair in an array.  The item size is
   1115  * given as an argument.
   1116  */
   1117 const struct usb_devno *
   1118 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1119 		 u_int16_t vendor, u_int16_t product)
   1120 {
   1121 	while (nentries-- > 0) {
   1122 		u_int16_t tproduct = tbl->ud_product;
   1123 		if (tbl->ud_vendor == vendor &&
   1124 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1125 			return (tbl);
   1126 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1127 	}
   1128 	return (NULL);
   1129 }
   1130 
   1131 
   1132 void
   1133 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1134 {
   1135 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1136 
   1137         iter->cur = (const uByte *)cd;
   1138         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1139 }
   1140 
   1141 const usb_descriptor_t *
   1142 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1143 {
   1144 	const usb_descriptor_t *desc;
   1145 
   1146 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1147 		if (iter->cur != iter->end)
   1148 			printf("usb_desc_iter_next: bad descriptor\n");
   1149 		return NULL;
   1150 	}
   1151 	desc = (const usb_descriptor_t *)iter->cur;
   1152 	if (desc->bLength == 0) {
   1153 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1154 		return NULL;
   1155 	}
   1156 	iter->cur += desc->bLength;
   1157 	if (iter->cur > iter->end) {
   1158 		printf("usb_desc_iter_next: descriptor length too large\n");
   1159 		return NULL;
   1160 	}
   1161 	return desc;
   1162 }
   1163 
   1164 usbd_status
   1165 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1166 {
   1167 	return usbd_get_string0(dev, si, buf, 1);
   1168 }
   1169 
   1170 usbd_status
   1171 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1172 {
   1173 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1174 	usb_string_descriptor_t us;
   1175 	char *s;
   1176 	int i, n;
   1177 	u_int16_t c;
   1178 	usbd_status err;
   1179 	int size;
   1180 
   1181 	buf[0] = '\0';
   1182 	if (si == 0)
   1183 		return (USBD_INVAL);
   1184 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1185 		return (USBD_STALLED);
   1186 	if (dev->langid == USBD_NOLANG) {
   1187 		/* Set up default language */
   1188 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1189 		    &size);
   1190 		if (err || size < 4) {
   1191 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1192 			dev->langid = 0; /* Well, just pick something then */
   1193 		} else {
   1194 			/* Pick the first language as the default. */
   1195 			dev->langid = UGETW(us.bString[0]);
   1196 		}
   1197 	}
   1198 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1199 	if (err)
   1200 		return (err);
   1201 	s = buf;
   1202 	n = size / 2 - 1;
   1203 	if (unicode) {
   1204 		for (i = 0; i < n; i++) {
   1205 			c = UGETW(us.bString[i]);
   1206 			if (swap)
   1207 				c = (c >> 8) | (c << 8);
   1208 			s += wput_utf8(s, 3, c);
   1209 		}
   1210 		*s++ = 0;
   1211 	}
   1212 #ifdef COMPAT_30
   1213 	else {
   1214 		for (i = 0; i < n; i++) {
   1215 			c = UGETW(us.bString[i]);
   1216 			if (swap)
   1217 				c = (c >> 8) | (c << 8);
   1218 			*s++ = (c < 0x80) ? c : '?';
   1219 		}
   1220 		*s++ = 0;
   1221 	}
   1222 #endif
   1223 	return (USBD_NORMAL_COMPLETION);
   1224 }
   1225 
   1226 #if defined(__FreeBSD__)
   1227 int
   1228 usbd_driver_load(module_t mod, int what, void *arg)
   1229 {
   1230 	/* XXX should implement something like a function that removes all generic devices */
   1231 
   1232  	return (0);
   1233 }
   1234 
   1235 #endif
   1236