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