Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.114.2.2
      1 /*	$NetBSD: usbdi.c,v 1.114.2.2 2007/04/06 18:43:51 bouyer 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.114.2.2 2007/04/06 18:43:51 bouyer 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 	}
    778 #endif
    779 
    780 #ifdef DIAGNOSTIC
    781 	if (pipe == NULL) {
    782 		printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer);
    783 		return;
    784 	}
    785 #endif
    786 	repeat = pipe->repeat;
    787 	polling = pipe->device->bus->use_polling;
    788 	/* XXXX */
    789 	if (polling)
    790 		pipe->running = 0;
    791 
    792 	if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 &&
    793 	    usbd_xfer_isread(xfer)) {
    794 #ifdef DIAGNOSTIC
    795 		if (xfer->actlen > xfer->length) {
    796 			printf("usb_transfer_complete: actlen > len %d > %d\n",
    797 			       xfer->actlen, xfer->length);
    798 			xfer->actlen = xfer->length;
    799 		}
    800 #endif
    801 		memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen);
    802 	}
    803 
    804 	/* if we allocated the buffer in usbd_transfer() we free it here. */
    805 	if (xfer->rqflags & URQ_AUTO_DMABUF) {
    806 		if (!repeat) {
    807 			struct usbd_bus *bus = pipe->device->bus;
    808 			bus->methods->freem(bus, dmap);
    809 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    810 		}
    811 	}
    812 
    813 	if (!repeat) {
    814 		/* Remove request from queue. */
    815 #ifdef DIAGNOSTIC
    816 		if (xfer != SIMPLEQ_FIRST(&pipe->queue))
    817 			printf("usb_transfer_complete: bad dequeue %p != %p\n",
    818 			       xfer, SIMPLEQ_FIRST(&pipe->queue));
    819 		xfer->busy_free = XFER_BUSY;
    820 #endif
    821 		SIMPLEQ_REMOVE_HEAD(&pipe->queue, next);
    822 	}
    823 	DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n",
    824 		    repeat, SIMPLEQ_FIRST(&pipe->queue)));
    825 
    826 	/* Count completed transfers. */
    827 	++pipe->device->bus->stats.uds_requests
    828 		[pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE];
    829 
    830 	xfer->done = 1;
    831 	if (!xfer->status && xfer->actlen < xfer->length &&
    832 	    !(xfer->flags & USBD_SHORT_XFER_OK)) {
    833 		DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n",
    834 			     xfer->actlen, xfer->length));
    835 		xfer->status = USBD_SHORT_XFER;
    836 	}
    837 
    838 	if (xfer->callback)
    839 		xfer->callback(xfer, xfer->priv, xfer->status);
    840 
    841 #ifdef DIAGNOSTIC
    842 	if (pipe->methods->done != NULL)
    843 		pipe->methods->done(xfer);
    844 	else
    845 		printf("usb_transfer_complete: pipe->methods->done == NULL\n");
    846 #else
    847 	pipe->methods->done(xfer);
    848 #endif
    849 
    850 	if (sync && !polling)
    851 		wakeup(xfer);
    852 
    853 	if (!repeat) {
    854 		/* XXX should we stop the queue on all errors? */
    855 		if (erred && pipe->iface != NULL)	/* not control pipe */
    856 			pipe->running = 0;
    857 		else
    858 			usbd_start_next(pipe);
    859 	}
    860 }
    861 
    862 usbd_status
    863 usb_insert_transfer(usbd_xfer_handle xfer)
    864 {
    865 	usbd_pipe_handle pipe = xfer->pipe;
    866 	usbd_status err;
    867 	int s;
    868 
    869 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    870 		    pipe, pipe->running, xfer->timeout));
    871 #ifdef DIAGNOSTIC
    872 	if (xfer->busy_free != XFER_BUSY) {
    873 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
    874 		       xfer, xfer->busy_free);
    875 		return (USBD_INVAL);
    876 	}
    877 	xfer->busy_free = XFER_ONQU;
    878 #endif
    879 	s = splusb();
    880 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    881 	if (pipe->running)
    882 		err = USBD_IN_PROGRESS;
    883 	else {
    884 		pipe->running = 1;
    885 		err = USBD_NORMAL_COMPLETION;
    886 	}
    887 	splx(s);
    888 	return (err);
    889 }
    890 
    891 /* Called at splusb() */
    892 void
    893 usbd_start_next(usbd_pipe_handle pipe)
    894 {
    895 	usbd_xfer_handle xfer;
    896 	usbd_status err;
    897 
    898 	SPLUSBCHECK;
    899 
    900 #ifdef DIAGNOSTIC
    901 	if (pipe == NULL) {
    902 		printf("usbd_start_next: pipe == NULL\n");
    903 		return;
    904 	}
    905 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    906 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    907 		return;
    908 	}
    909 #endif
    910 
    911 	/* Get next request in queue. */
    912 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    913 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    914 	if (xfer == NULL) {
    915 		pipe->running = 0;
    916 	} else {
    917 		err = pipe->methods->start(xfer);
    918 		if (err != USBD_IN_PROGRESS) {
    919 			printf("usbd_start_next: error=%d\n", err);
    920 			pipe->running = 0;
    921 			/* XXX do what? */
    922 		}
    923 	}
    924 }
    925 
    926 usbd_status
    927 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
    928 {
    929 	return (usbd_do_request_flags(dev, req, data, 0, 0,
    930 				      USBD_DEFAULT_TIMEOUT));
    931 }
    932 
    933 usbd_status
    934 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
    935 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
    936 {
    937 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
    938 					   data, flags, actlen, timo));
    939 }
    940 
    941 usbd_status
    942 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
    943 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
    944 	u_int32_t timeout)
    945 {
    946 	usbd_xfer_handle xfer;
    947 	usbd_status err;
    948 
    949 #ifdef DIAGNOSTIC
    950 #if defined(__i386__) && defined(__FreeBSD__)
    951 	KASSERT(intr_nesting_level == 0,
    952 	       	("usbd_do_request: in interrupt context"));
    953 #endif
    954 	if (dev->bus->intr_context) {
    955 		printf("usbd_do_request: not in process context\n");
    956 		return (USBD_INVAL);
    957 	}
    958 #endif
    959 
    960 	xfer = usbd_alloc_xfer(dev);
    961 	if (xfer == NULL)
    962 		return (USBD_NOMEM);
    963 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
    964 				data, UGETW(req->wLength), flags, 0);
    965 	xfer->pipe = pipe;
    966 	err = usbd_sync_transfer(xfer);
    967 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    968 	if (xfer->actlen > xfer->length) {
    969 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
    970 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    971 			 dev->address, xfer->request.bmRequestType,
    972 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    973 			 UGETW(xfer->request.wIndex),
    974 			 UGETW(xfer->request.wLength),
    975 			 xfer->length, xfer->actlen));
    976 	}
    977 #endif
    978 	if (actlen != NULL)
    979 		*actlen = xfer->actlen;
    980 	if (err == USBD_STALLED) {
    981 		/*
    982 		 * The control endpoint has stalled.  Control endpoints
    983 		 * should not halt, but some may do so anyway so clear
    984 		 * any halt condition.
    985 		 */
    986 		usb_device_request_t treq;
    987 		usb_status_t status;
    988 		u_int16_t s;
    989 		usbd_status nerr;
    990 
    991 		treq.bmRequestType = UT_READ_ENDPOINT;
    992 		treq.bRequest = UR_GET_STATUS;
    993 		USETW(treq.wValue, 0);
    994 		USETW(treq.wIndex, 0);
    995 		USETW(treq.wLength, sizeof(usb_status_t));
    996 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    997 					   &treq, &status,sizeof(usb_status_t),
    998 					   0, 0);
    999 		nerr = usbd_sync_transfer(xfer);
   1000 		if (nerr)
   1001 			goto bad;
   1002 		s = UGETW(status.wStatus);
   1003 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
   1004 		if (!(s & UES_HALT))
   1005 			goto bad;
   1006 		treq.bmRequestType = UT_WRITE_ENDPOINT;
   1007 		treq.bRequest = UR_CLEAR_FEATURE;
   1008 		USETW(treq.wValue, UF_ENDPOINT_HALT);
   1009 		USETW(treq.wIndex, 0);
   1010 		USETW(treq.wLength, 0);
   1011 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
   1012 					   &treq, &status, 0, 0, 0);
   1013 		nerr = usbd_sync_transfer(xfer);
   1014 		if (nerr)
   1015 			goto bad;
   1016 	}
   1017 
   1018  bad:
   1019 	usbd_free_xfer(xfer);
   1020 	return (err);
   1021 }
   1022 
   1023 void
   1024 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1025     usbd_private_handle priv, usbd_status status)
   1026 {
   1027 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1028 	if (xfer->actlen > xfer->length) {
   1029 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1030 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1031 			 xfer->pipe->device->address,
   1032 			 xfer->request.bmRequestType,
   1033 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1034 			 UGETW(xfer->request.wIndex),
   1035 			 UGETW(xfer->request.wLength),
   1036 			 xfer->length, xfer->actlen));
   1037 	}
   1038 #endif
   1039 	usbd_free_xfer(xfer);
   1040 }
   1041 
   1042 /*
   1043  * Execute a request without waiting for completion.
   1044  * Can be used from interrupt context.
   1045  */
   1046 usbd_status
   1047 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1048 		      void *data)
   1049 {
   1050 	usbd_xfer_handle xfer;
   1051 	usbd_status err;
   1052 
   1053 	xfer = usbd_alloc_xfer(dev);
   1054 	if (xfer == NULL)
   1055 		return (USBD_NOMEM);
   1056 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1057 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1058 	err = usbd_transfer(xfer);
   1059 	if (err != USBD_IN_PROGRESS) {
   1060 		usbd_free_xfer(xfer);
   1061 		return (err);
   1062 	}
   1063 	return (USBD_NORMAL_COMPLETION);
   1064 }
   1065 
   1066 const struct usbd_quirks *
   1067 usbd_get_quirks(usbd_device_handle dev)
   1068 {
   1069 #ifdef DIAGNOSTIC
   1070 	if (dev == NULL) {
   1071 		printf("usbd_get_quirks: dev == NULL\n");
   1072 		return 0;
   1073 	}
   1074 #endif
   1075 	return (dev->quirks);
   1076 }
   1077 
   1078 /* XXX do periodic free() of free list */
   1079 
   1080 /*
   1081  * Called from keyboard driver when in polling mode.
   1082  */
   1083 void
   1084 usbd_dopoll(usbd_interface_handle iface)
   1085 {
   1086 	iface->device->bus->methods->do_poll(iface->device->bus);
   1087 }
   1088 
   1089 void
   1090 usbd_set_polling(usbd_device_handle dev, int on)
   1091 {
   1092 	if (on)
   1093 		dev->bus->use_polling++;
   1094 	else
   1095 		dev->bus->use_polling--;
   1096 	/* When polling we need to make sure there is nothing pending to do. */
   1097 	if (dev->bus->use_polling)
   1098 		dev->bus->methods->soft_intr(dev->bus);
   1099 }
   1100 
   1101 
   1102 usb_endpoint_descriptor_t *
   1103 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1104 {
   1105 	struct usbd_endpoint *ep;
   1106 	int i;
   1107 
   1108 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1109 		ep = &iface->endpoints[i];
   1110 		if (ep->edesc->bEndpointAddress == address)
   1111 			return (iface->endpoints[i].edesc);
   1112 	}
   1113 	return (0);
   1114 }
   1115 
   1116 /*
   1117  * usbd_ratecheck() can limit the number of error messages that occurs.
   1118  * When a device is unplugged it may take up to 0.25s for the hub driver
   1119  * to notice it.  If the driver continuosly tries to do I/O operations
   1120  * this can generate a large number of messages.
   1121  */
   1122 int
   1123 usbd_ratecheck(struct timeval *last)
   1124 {
   1125 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1126 
   1127 	return (ratecheck(last, &errinterval));
   1128 }
   1129 
   1130 /*
   1131  * Search for a vendor/product pair in an array.  The item size is
   1132  * given as an argument.
   1133  */
   1134 const struct usb_devno *
   1135 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1136 		 u_int16_t vendor, u_int16_t product)
   1137 {
   1138 	while (nentries-- > 0) {
   1139 		u_int16_t tproduct = tbl->ud_product;
   1140 		if (tbl->ud_vendor == vendor &&
   1141 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1142 			return (tbl);
   1143 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1144 	}
   1145 	return (NULL);
   1146 }
   1147 
   1148 
   1149 void
   1150 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1151 {
   1152 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1153 
   1154         iter->cur = (const uByte *)cd;
   1155         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1156 }
   1157 
   1158 const usb_descriptor_t *
   1159 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1160 {
   1161 	const usb_descriptor_t *desc;
   1162 
   1163 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1164 		if (iter->cur != iter->end)
   1165 			printf("usb_desc_iter_next: bad descriptor\n");
   1166 		return NULL;
   1167 	}
   1168 	desc = (const usb_descriptor_t *)iter->cur;
   1169 	if (desc->bLength == 0) {
   1170 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1171 		return NULL;
   1172 	}
   1173 	iter->cur += desc->bLength;
   1174 	if (iter->cur > iter->end) {
   1175 		printf("usb_desc_iter_next: descriptor length too large\n");
   1176 		return NULL;
   1177 	}
   1178 	return desc;
   1179 }
   1180 
   1181 usbd_status
   1182 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1183 {
   1184 	return usbd_get_string0(dev, si, buf, 1);
   1185 }
   1186 
   1187 usbd_status
   1188 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1189 {
   1190 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1191 	usb_string_descriptor_t us;
   1192 	char *s;
   1193 	int i, n;
   1194 	u_int16_t c;
   1195 	usbd_status err;
   1196 	int size;
   1197 
   1198 	buf[0] = '\0';
   1199 	if (si == 0)
   1200 		return (USBD_INVAL);
   1201 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1202 		return (USBD_STALLED);
   1203 	if (dev->langid == USBD_NOLANG) {
   1204 		/* Set up default language */
   1205 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1206 		    &size);
   1207 		if (err || size < 4) {
   1208 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1209 			dev->langid = 0; /* Well, just pick something then */
   1210 		} else {
   1211 			/* Pick the first language as the default. */
   1212 			dev->langid = UGETW(us.bString[0]);
   1213 		}
   1214 	}
   1215 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1216 	if (err)
   1217 		return (err);
   1218 	s = buf;
   1219 	n = size / 2 - 1;
   1220 	if (unicode) {
   1221 		for (i = 0; i < n; i++) {
   1222 			c = UGETW(us.bString[i]);
   1223 			if (swap)
   1224 				c = (c >> 8) | (c << 8);
   1225 			if (c < 0x0080) {
   1226 				*s++ = c;
   1227 			} else if (c < 0x0800) {
   1228 				*s++ = 0xc0 | (c >> 6);
   1229 				*s++ = 0x80 | (c & 0x3f);
   1230 			} else {
   1231 				*s++ = 0xe0 | (c >> 12);
   1232 				*s++ = 0x80 | ((c >> 6) & 0x3f);
   1233 				*s++ = 0x80 | (c & 0x3f);
   1234 			}
   1235 		}
   1236 		*s++ = 0;
   1237 	}
   1238 #ifdef COMPAT_30
   1239 	else {
   1240 		int j;
   1241 		for (i = j = 0; i < n && j < USB_MAX_STRING_LEN - 1; i++) {
   1242 			c = UGETW(us.bString[i]);
   1243 			if (swap)
   1244 				c = (c >> 8) | (c << 8);
   1245 			/* Encode (16-bit) Unicode as UTF8. */
   1246 			s[j++] = (c < 0x80) ? c : '?';
   1247 		}
   1248 		s[j] = 0;
   1249 	}
   1250 #endif
   1251 	return (USBD_NORMAL_COMPLETION);
   1252 }
   1253 
   1254 #if defined(__FreeBSD__)
   1255 int
   1256 usbd_driver_load(module_t mod, int what, void *arg)
   1257 {
   1258 	/* XXX should implement something like a function that removes all generic devices */
   1259 
   1260  	return (0);
   1261 }
   1262 
   1263 #endif
   1264