Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.131.6.1
      1 /*	$NetBSD: usbdi.c,v 1.131.6.1 2011/06/23 14:20:12 cherry 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.131.6.1 2011/06/23 14:20:12 cherry Exp $");
     36 
     37 #include "opt_compat_netbsd.h"
     38 #include "opt_usb.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/proc.h>
     46 
     47 #include <sys/bus.h>
     48 
     49 #include <dev/usb/usb.h>
     50 #include <dev/usb/usbdi.h>
     51 #include <dev/usb/usbdi_util.h>
     52 #include <dev/usb/usbdivar.h>
     53 #include <dev/usb/usb_mem.h>
     54 #include <dev/usb/usb_quirks.h>
     55 
     56 /* UTF-8 encoding stuff */
     57 #include <fs/unicode.h>
     58 
     59 #ifdef USB_DEBUG
     60 #define DPRINTF(x)	if (usbdebug) printf x
     61 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     62 extern int usbdebug;
     63 #else
     64 #define DPRINTF(x)
     65 #define DPRINTFN(n,x)
     66 #endif
     67 
     68 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe);
     69 Static void usbd_do_request_async_cb
     70 	(usbd_xfer_handle, usbd_private_handle, usbd_status);
     71 Static void usbd_start_next(usbd_pipe_handle pipe);
     72 Static usbd_status usbd_open_pipe_ival
     73 	(usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int);
     74 
     75 static inline int
     76 usbd_xfer_isread(usbd_xfer_handle xfer)
     77 {
     78 	if (xfer->rqflags & URQ_REQUEST)
     79 		return (xfer->request.bmRequestType & UT_READ);
     80 	else
     81 		return (xfer->pipe->endpoint->edesc->bEndpointAddress &
     82 			UE_DIR_IN);
     83 }
     84 
     85 #if defined(USB_DEBUG) || defined(EHCI_DEBUG)
     86 void
     87 usbd_dump_iface(struct usbd_interface *iface)
     88 {
     89 	printf("usbd_dump_iface: iface=%p\n", iface);
     90 	if (iface == NULL)
     91 		return;
     92 	printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n",
     93 	       iface->device, iface->idesc, iface->index, iface->altindex,
     94 	       iface->priv);
     95 }
     96 
     97 void
     98 usbd_dump_device(struct usbd_device *dev)
     99 {
    100 	printf("usbd_dump_device: dev=%p\n", dev);
    101 	if (dev == NULL)
    102 		return;
    103 	printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe);
    104 	printf(" address=%d config=%d depth=%d speed=%d self_powered=%d "
    105 	       "power=%d langid=%d\n",
    106 	       dev->address, dev->config, dev->depth, dev->speed,
    107 	       dev->self_powered, dev->power, dev->langid);
    108 }
    109 
    110 void
    111 usbd_dump_endpoint(struct usbd_endpoint *endp)
    112 {
    113 	printf("usbd_dump_endpoint: endp=%p\n", endp);
    114 	if (endp == NULL)
    115 		return;
    116 	printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt);
    117 	if (endp->edesc)
    118 		printf(" bEndpointAddress=0x%02x\n",
    119 		       endp->edesc->bEndpointAddress);
    120 }
    121 
    122 void
    123 usbd_dump_queue(usbd_pipe_handle pipe)
    124 {
    125 	usbd_xfer_handle xfer;
    126 
    127 	printf("usbd_dump_queue: pipe=%p\n", pipe);
    128 	SIMPLEQ_FOREACH(xfer, &pipe->queue, next) {
    129 		printf("  xfer=%p\n", xfer);
    130 	}
    131 }
    132 
    133 void
    134 usbd_dump_pipe(usbd_pipe_handle pipe)
    135 {
    136 	printf("usbd_dump_pipe: pipe=%p\n", pipe);
    137 	if (pipe == NULL)
    138 		return;
    139 	usbd_dump_iface(pipe->iface);
    140 	usbd_dump_device(pipe->device);
    141 	usbd_dump_endpoint(pipe->endpoint);
    142 	printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n",
    143 	       pipe->refcnt, pipe->running, pipe->aborting);
    144 	printf(" intrxfer=%p, repeat=%d, interval=%d\n",
    145 	       pipe->intrxfer, pipe->repeat, pipe->interval);
    146 }
    147 #endif
    148 
    149 usbd_status
    150 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
    151 	       u_int8_t flags, usbd_pipe_handle *pipe)
    152 {
    153 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    154 				    USBD_DEFAULT_INTERVAL));
    155 }
    156 
    157 usbd_status
    158 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address,
    159 		    u_int8_t flags, usbd_pipe_handle *pipe, int ival)
    160 {
    161 	usbd_pipe_handle p;
    162 	struct usbd_endpoint *ep;
    163 	usbd_status err;
    164 	int i;
    165 
    166 	DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n",
    167 		    iface, address, flags));
    168 
    169 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
    170 		ep = &iface->endpoints[i];
    171 		if (ep->edesc == NULL)
    172 			return (USBD_IOERROR);
    173 		if (ep->edesc->bEndpointAddress == address)
    174 			goto found;
    175 	}
    176 	return (USBD_BAD_ADDRESS);
    177  found:
    178 	if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0)
    179 		return (USBD_IN_USE);
    180 	err = usbd_setup_pipe(iface->device, iface, ep, ival, &p);
    181 	if (err)
    182 		return (err);
    183 	LIST_INSERT_HEAD(&iface->pipes, p, next);
    184 	*pipe = p;
    185 	return (USBD_NORMAL_COMPLETION);
    186 }
    187 
    188 usbd_status
    189 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
    190 		    u_int8_t flags, usbd_pipe_handle *pipe,
    191 		    usbd_private_handle priv, void *buffer, u_int32_t len,
    192 		    usbd_callback cb, int ival)
    193 {
    194 	usbd_status err;
    195 	usbd_xfer_handle xfer;
    196 	usbd_pipe_handle ipipe;
    197 
    198 	DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n",
    199 		    address, flags, len));
    200 
    201 	err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE,
    202 				  &ipipe, ival);
    203 	if (err)
    204 		return (err);
    205 	xfer = usbd_alloc_xfer(iface->device);
    206 	if (xfer == NULL) {
    207 		err = USBD_NOMEM;
    208 		goto bad1;
    209 	}
    210 	usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags,
    211 	    USBD_NO_TIMEOUT, cb);
    212 	ipipe->intrxfer = xfer;
    213 	ipipe->repeat = 1;
    214 	err = usbd_transfer(xfer);
    215 	*pipe = ipipe;
    216 	if (err != USBD_IN_PROGRESS)
    217 		goto bad2;
    218 	return (USBD_NORMAL_COMPLETION);
    219 
    220  bad2:
    221 	ipipe->intrxfer = NULL;
    222 	ipipe->repeat = 0;
    223 	usbd_free_xfer(xfer);
    224  bad1:
    225 	usbd_close_pipe(ipipe);
    226 	return (err);
    227 }
    228 
    229 usbd_status
    230 usbd_close_pipe(usbd_pipe_handle pipe)
    231 {
    232 #ifdef DIAGNOSTIC
    233 	if (pipe == NULL) {
    234 		printf("usbd_close_pipe: pipe==NULL\n");
    235 		return (USBD_NORMAL_COMPLETION);
    236 	}
    237 #endif
    238 
    239 	if (--pipe->refcnt != 0)
    240 		return (USBD_NORMAL_COMPLETION);
    241 	if (! SIMPLEQ_EMPTY(&pipe->queue))
    242 		return (USBD_PENDING_REQUESTS);
    243 	LIST_REMOVE(pipe, next);
    244 	pipe->endpoint->refcnt--;
    245 	pipe->methods->close(pipe);
    246 	if (pipe->intrxfer != NULL)
    247 		usbd_free_xfer(pipe->intrxfer);
    248 	free(pipe, M_USB);
    249 	return (USBD_NORMAL_COMPLETION);
    250 }
    251 
    252 usbd_status
    253 usbd_transfer(usbd_xfer_handle xfer)
    254 {
    255 	usbd_pipe_handle pipe = xfer->pipe;
    256 	usb_dma_t *dmap = &xfer->dmabuf;
    257 	usbd_status err;
    258 	unsigned int size, flags;
    259 	int s;
    260 
    261 	DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n",
    262 		    xfer, xfer->flags, pipe, pipe->running));
    263 #ifdef USB_DEBUG
    264 	if (usbdebug > 5)
    265 		usbd_dump_queue(pipe);
    266 #endif
    267 	xfer->done = 0;
    268 
    269 	if (pipe->aborting)
    270 		return (USBD_CANCELLED);
    271 
    272 	size = xfer->length;
    273 	/* If there is no buffer, allocate one. */
    274 	if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) {
    275 		struct usbd_bus *bus = pipe->device->bus;
    276 
    277 #ifdef DIAGNOSTIC
    278 		if (xfer->rqflags & URQ_AUTO_DMABUF)
    279 			printf("usbd_transfer: has old buffer!\n");
    280 #endif
    281 		err = bus->methods->allocm(bus, dmap, size);
    282 		if (err)
    283 			return (err);
    284 		xfer->rqflags |= URQ_AUTO_DMABUF;
    285 	}
    286 
    287 	flags = xfer->flags;
    288 
    289 	/* Copy data if going out. */
    290 	if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer))
    291 		memcpy(KERNADDR(dmap, 0), xfer->buffer, size);
    292 
    293 	/* xfer is not valid after the transfer method unless synchronous */
    294 	err = pipe->methods->transfer(xfer);
    295 
    296 	if (err != USBD_IN_PROGRESS && err) {
    297 		/* The transfer has not been queued, so free buffer. */
    298 		if (xfer->rqflags & URQ_AUTO_DMABUF) {
    299 			struct usbd_bus *bus = pipe->device->bus;
    300 
    301 			bus->methods->freem(bus, &xfer->dmabuf);
    302 			xfer->rqflags &= ~URQ_AUTO_DMABUF;
    303 		}
    304 	}
    305 
    306 	if (!(flags & USBD_SYNCHRONOUS))
    307 		return (err);
    308 
    309 	/* Sync transfer, wait for completion. */
    310 	if (err != USBD_IN_PROGRESS)
    311 		return (err);
    312 	s = splusb();
    313 	if (!xfer->done) {
    314 		if (pipe->device->bus->use_polling)
    315 			panic("usbd_transfer: not done");
    316 		tsleep(xfer, PRIBIO, "usbsyn", 0);
    317 	}
    318 	splx(s);
    319 	return (xfer->status);
    320 }
    321 
    322 /* Like usbd_transfer(), but waits for completion. */
    323 usbd_status
    324 usbd_sync_transfer(usbd_xfer_handle xfer)
    325 {
    326 	xfer->flags |= USBD_SYNCHRONOUS;
    327 	return (usbd_transfer(xfer));
    328 }
    329 
    330 void *
    331 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size)
    332 {
    333 	struct usbd_bus *bus = xfer->device->bus;
    334 	usbd_status err;
    335 
    336 #ifdef DIAGNOSTIC
    337 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    338 		printf("usbd_alloc_buffer: xfer already has a buffer\n");
    339 #endif
    340 	err = bus->methods->allocm(bus, &xfer->dmabuf, size);
    341 	if (err)
    342 		return (NULL);
    343 	xfer->rqflags |= URQ_DEV_DMABUF;
    344 	return (KERNADDR(&xfer->dmabuf, 0));
    345 }
    346 
    347 void
    348 usbd_free_buffer(usbd_xfer_handle xfer)
    349 {
    350 #ifdef DIAGNOSTIC
    351 	if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) {
    352 		printf("usbd_free_buffer: no buffer\n");
    353 		return;
    354 	}
    355 #endif
    356 	xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF);
    357 	xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf);
    358 }
    359 
    360 void *
    361 usbd_get_buffer(usbd_xfer_handle xfer)
    362 {
    363 	if (!(xfer->rqflags & URQ_DEV_DMABUF))
    364 		return (0);
    365 	return (KERNADDR(&xfer->dmabuf, 0));
    366 }
    367 
    368 usbd_xfer_handle
    369 usbd_alloc_xfer(usbd_device_handle dev)
    370 {
    371 	usbd_xfer_handle xfer;
    372 
    373 	xfer = dev->bus->methods->allocx(dev->bus);
    374 	if (xfer == NULL)
    375 		return (NULL);
    376 	xfer->device = dev;
    377 	callout_init(&xfer->timeout_handle, 0);
    378 	DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer));
    379 	return (xfer);
    380 }
    381 
    382 usbd_status
    383 usbd_free_xfer(usbd_xfer_handle xfer)
    384 {
    385 	DPRINTFN(5,("usbd_free_xfer: %p\n", xfer));
    386 	if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))
    387 		usbd_free_buffer(xfer);
    388 #if defined(DIAGNOSTIC)
    389 	if (callout_pending(&xfer->timeout_handle)) {
    390 		callout_stop(&xfer->timeout_handle);
    391 		printf("usbd_free_xfer: timout_handle pending");
    392 	}
    393 #endif
    394 	xfer->device->bus->methods->freex(xfer->device->bus, xfer);
    395 	return (USBD_NORMAL_COMPLETION);
    396 }
    397 
    398 void
    399 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    400 		usbd_private_handle priv, void *buffer, u_int32_t length,
    401 		u_int16_t flags, u_int32_t timeout,
    402 		usbd_callback callback)
    403 {
    404 	xfer->pipe = pipe;
    405 	xfer->priv = priv;
    406 	xfer->buffer = buffer;
    407 	xfer->length = length;
    408 	xfer->actlen = 0;
    409 	xfer->flags = flags;
    410 	xfer->timeout = timeout;
    411 	xfer->status = USBD_NOT_STARTED;
    412 	xfer->callback = callback;
    413 	xfer->rqflags &= ~URQ_REQUEST;
    414 	xfer->nframes = 0;
    415 }
    416 
    417 void
    418 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
    419 			usbd_private_handle priv, u_int32_t timeout,
    420 			usb_device_request_t *req, void *buffer,
    421 			u_int32_t length, u_int16_t flags,
    422 			usbd_callback callback)
    423 {
    424 	xfer->pipe = dev->default_pipe;
    425 	xfer->priv = priv;
    426 	xfer->buffer = buffer;
    427 	xfer->length = length;
    428 	xfer->actlen = 0;
    429 	xfer->flags = flags;
    430 	xfer->timeout = timeout;
    431 	xfer->status = USBD_NOT_STARTED;
    432 	xfer->callback = callback;
    433 	xfer->request = *req;
    434 	xfer->rqflags |= URQ_REQUEST;
    435 	xfer->nframes = 0;
    436 }
    437 
    438 void
    439 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    440 		     usbd_private_handle priv, u_int16_t *frlengths,
    441 		     u_int32_t nframes, u_int16_t flags, usbd_callback callback)
    442 {
    443 	xfer->pipe = pipe;
    444 	xfer->priv = priv;
    445 	xfer->buffer = 0;
    446 	xfer->length = 0;
    447 	xfer->actlen = 0;
    448 	xfer->flags = flags;
    449 	xfer->timeout = USBD_NO_TIMEOUT;
    450 	xfer->status = USBD_NOT_STARTED;
    451 	xfer->callback = callback;
    452 	xfer->rqflags &= ~URQ_REQUEST;
    453 	xfer->frlengths = frlengths;
    454 	xfer->nframes = nframes;
    455 }
    456 
    457 void
    458 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
    459 		     void **buffer, u_int32_t *count, usbd_status *status)
    460 {
    461 	if (priv != NULL)
    462 		*priv = xfer->priv;
    463 	if (buffer != NULL)
    464 		*buffer = xfer->buffer;
    465 	if (count != NULL)
    466 		*count = xfer->actlen;
    467 	if (status != NULL)
    468 		*status = xfer->status;
    469 }
    470 
    471 usb_config_descriptor_t *
    472 usbd_get_config_descriptor(usbd_device_handle dev)
    473 {
    474 #ifdef DIAGNOSTIC
    475 	if (dev == NULL) {
    476 		printf("usbd_get_config_descriptor: dev == NULL\n");
    477 		return (NULL);
    478 	}
    479 #endif
    480 	return (dev->cdesc);
    481 }
    482 
    483 usb_interface_descriptor_t *
    484 usbd_get_interface_descriptor(usbd_interface_handle iface)
    485 {
    486 #ifdef DIAGNOSTIC
    487 	if (iface == NULL) {
    488 		printf("usbd_get_interface_descriptor: dev == NULL\n");
    489 		return (NULL);
    490 	}
    491 #endif
    492 	return (iface->idesc);
    493 }
    494 
    495 usb_device_descriptor_t *
    496 usbd_get_device_descriptor(usbd_device_handle dev)
    497 {
    498 	return (&dev->ddesc);
    499 }
    500 
    501 usb_endpoint_descriptor_t *
    502 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index)
    503 {
    504 	if (index >= iface->idesc->bNumEndpoints)
    505 		return (0);
    506 	return (iface->endpoints[index].edesc);
    507 }
    508 
    509 /* Some drivers may wish to abort requests on the default pipe, *
    510  * but there is no mechanism for getting a handle on it.        */
    511 usbd_status
    512 usbd_abort_default_pipe(struct usbd_device *device)
    513 {
    514 
    515 	return usbd_abort_pipe(device->default_pipe);
    516 }
    517 
    518 usbd_status
    519 usbd_abort_pipe(usbd_pipe_handle pipe)
    520 {
    521 	usbd_status err;
    522 	int s;
    523 	usbd_xfer_handle intrxfer = pipe->intrxfer;
    524 
    525 #ifdef DIAGNOSTIC
    526 	if (pipe == NULL) {
    527 		printf("usbd_abort_pipe: pipe==NULL\n");
    528 		return (USBD_NORMAL_COMPLETION);
    529 	}
    530 #endif
    531 	s = splusb();
    532 	err = usbd_ar_pipe(pipe);
    533 	splx(s);
    534 	if (pipe->intrxfer != intrxfer)
    535 		usbd_free_xfer(intrxfer);
    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 (repeat) {
    822 		if (xfer->callback)
    823 			xfer->callback(xfer, xfer->priv, xfer->status);
    824 		pipe->methods->done(xfer);
    825 	} else {
    826 		pipe->methods->done(xfer);
    827 		if (xfer->callback)
    828 			xfer->callback(xfer, xfer->priv, xfer->status);
    829 	}
    830 
    831 	if (sync && !polling)
    832 		wakeup(xfer);
    833 
    834 	if (!repeat) {
    835 		/* XXX should we stop the queue on all errors? */
    836 		if (erred && pipe->iface != NULL)	/* not control pipe */
    837 			pipe->running = 0;
    838 		else
    839 			usbd_start_next(pipe);
    840 	}
    841 }
    842 
    843 usbd_status
    844 usb_insert_transfer(usbd_xfer_handle xfer)
    845 {
    846 	usbd_pipe_handle pipe = xfer->pipe;
    847 	usbd_status err;
    848 	int s;
    849 
    850 	DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n",
    851 		    pipe, pipe->running, xfer->timeout));
    852 #ifdef DIAGNOSTIC
    853 	if (xfer->busy_free != XFER_BUSY) {
    854 		printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n",
    855 		       xfer, xfer->busy_free);
    856 		return (USBD_INVAL);
    857 	}
    858 	xfer->busy_free = XFER_ONQU;
    859 #endif
    860 	s = splusb();
    861 	SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next);
    862 	if (pipe->running)
    863 		err = USBD_IN_PROGRESS;
    864 	else {
    865 		pipe->running = 1;
    866 		err = USBD_NORMAL_COMPLETION;
    867 	}
    868 	splx(s);
    869 	return (err);
    870 }
    871 
    872 /* Called at splusb() */
    873 void
    874 usbd_start_next(usbd_pipe_handle pipe)
    875 {
    876 	usbd_xfer_handle xfer;
    877 	usbd_status err;
    878 
    879 	SPLUSBCHECK;
    880 
    881 #ifdef DIAGNOSTIC
    882 	if (pipe == NULL) {
    883 		printf("usbd_start_next: pipe == NULL\n");
    884 		return;
    885 	}
    886 	if (pipe->methods == NULL || pipe->methods->start == NULL) {
    887 		printf("usbd_start_next: pipe=%p no start method\n", pipe);
    888 		return;
    889 	}
    890 #endif
    891 
    892 	/* Get next request in queue. */
    893 	xfer = SIMPLEQ_FIRST(&pipe->queue);
    894 	DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer));
    895 	if (xfer == NULL) {
    896 		pipe->running = 0;
    897 	} else {
    898 		err = pipe->methods->start(xfer);
    899 		if (err != USBD_IN_PROGRESS) {
    900 			printf("usbd_start_next: error=%d\n", err);
    901 			pipe->running = 0;
    902 			/* XXX do what? */
    903 		}
    904 	}
    905 }
    906 
    907 usbd_status
    908 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
    909 {
    910 	return (usbd_do_request_flags(dev, req, data, 0, 0,
    911 				      USBD_DEFAULT_TIMEOUT));
    912 }
    913 
    914 usbd_status
    915 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req,
    916 		      void *data, u_int16_t flags, int *actlen, u_int32_t timo)
    917 {
    918 	return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req,
    919 					   data, flags, actlen, timo));
    920 }
    921 
    922 usbd_status
    923 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe,
    924 	usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
    925 	u_int32_t timeout)
    926 {
    927 	usbd_xfer_handle xfer;
    928 	usbd_status err;
    929 
    930 #ifdef DIAGNOSTIC
    931 	if (dev->bus->intr_context) {
    932 		printf("usbd_do_request: not in process context\n");
    933 		return (USBD_INVAL);
    934 	}
    935 #endif
    936 
    937 	xfer = usbd_alloc_xfer(dev);
    938 	if (xfer == NULL)
    939 		return (USBD_NOMEM);
    940 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req,
    941 				data, UGETW(req->wLength), flags, 0);
    942 	xfer->pipe = pipe;
    943 	err = usbd_sync_transfer(xfer);
    944 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
    945 	if (xfer->actlen > xfer->length) {
    946 		DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x"
    947 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
    948 			 __func__, dev->address, xfer->request.bmRequestType,
    949 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
    950 			 UGETW(xfer->request.wIndex),
    951 			 UGETW(xfer->request.wLength),
    952 			 xfer->length, xfer->actlen));
    953 	}
    954 #endif
    955 	if (actlen != NULL)
    956 		*actlen = xfer->actlen;
    957 	if (err == USBD_STALLED) {
    958 		/*
    959 		 * The control endpoint has stalled.  Control endpoints
    960 		 * should not halt, but some may do so anyway so clear
    961 		 * any halt condition.
    962 		 */
    963 		usb_device_request_t treq;
    964 		usb_status_t status;
    965 		u_int16_t s;
    966 		usbd_status nerr;
    967 
    968 		treq.bmRequestType = UT_READ_ENDPOINT;
    969 		treq.bRequest = UR_GET_STATUS;
    970 		USETW(treq.wValue, 0);
    971 		USETW(treq.wIndex, 0);
    972 		USETW(treq.wLength, sizeof(usb_status_t));
    973 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    974 					   &treq, &status,sizeof(usb_status_t),
    975 					   0, 0);
    976 		nerr = usbd_sync_transfer(xfer);
    977 		if (nerr)
    978 			goto bad;
    979 		s = UGETW(status.wStatus);
    980 		DPRINTF(("usbd_do_request: status = 0x%04x\n", s));
    981 		if (!(s & UES_HALT))
    982 			goto bad;
    983 		treq.bmRequestType = UT_WRITE_ENDPOINT;
    984 		treq.bRequest = UR_CLEAR_FEATURE;
    985 		USETW(treq.wValue, UF_ENDPOINT_HALT);
    986 		USETW(treq.wIndex, 0);
    987 		USETW(treq.wLength, 0);
    988 		usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT,
    989 					   &treq, &status, 0, 0, 0);
    990 		nerr = usbd_sync_transfer(xfer);
    991 		if (nerr)
    992 			goto bad;
    993 	}
    994 
    995  bad:
    996 	if (err) {
    997 		DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err)));
    998 	}
    999 	usbd_free_xfer(xfer);
   1000 	return (err);
   1001 }
   1002 
   1003 void
   1004 usbd_do_request_async_cb(usbd_xfer_handle xfer,
   1005     usbd_private_handle priv, usbd_status status)
   1006 {
   1007 #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1008 	if (xfer->actlen > xfer->length) {
   1009 		DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x"
   1010 			 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n",
   1011 			 xfer->pipe->device->address,
   1012 			 xfer->request.bmRequestType,
   1013 			 xfer->request.bRequest, UGETW(xfer->request.wValue),
   1014 			 UGETW(xfer->request.wIndex),
   1015 			 UGETW(xfer->request.wLength),
   1016 			 xfer->length, xfer->actlen));
   1017 	}
   1018 #endif
   1019 	usbd_free_xfer(xfer);
   1020 }
   1021 
   1022 /*
   1023  * Execute a request without waiting for completion.
   1024  * Can be used from interrupt context.
   1025  */
   1026 usbd_status
   1027 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req,
   1028 		      void *data)
   1029 {
   1030 	usbd_xfer_handle xfer;
   1031 	usbd_status err;
   1032 
   1033 	xfer = usbd_alloc_xfer(dev);
   1034 	if (xfer == NULL)
   1035 		return (USBD_NOMEM);
   1036 	usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req,
   1037 	    data, UGETW(req->wLength), 0, usbd_do_request_async_cb);
   1038 	err = usbd_transfer(xfer);
   1039 	if (err != USBD_IN_PROGRESS) {
   1040 		usbd_free_xfer(xfer);
   1041 		return (err);
   1042 	}
   1043 	return (USBD_NORMAL_COMPLETION);
   1044 }
   1045 
   1046 const struct usbd_quirks *
   1047 usbd_get_quirks(usbd_device_handle dev)
   1048 {
   1049 #ifdef DIAGNOSTIC
   1050 	if (dev == NULL) {
   1051 		printf("usbd_get_quirks: dev == NULL\n");
   1052 		return 0;
   1053 	}
   1054 #endif
   1055 	return (dev->quirks);
   1056 }
   1057 
   1058 /* XXX do periodic free() of free list */
   1059 
   1060 /*
   1061  * Called from keyboard driver when in polling mode.
   1062  */
   1063 void
   1064 usbd_dopoll(usbd_interface_handle iface)
   1065 {
   1066 	iface->device->bus->methods->do_poll(iface->device->bus);
   1067 }
   1068 
   1069 void
   1070 usbd_set_polling(usbd_device_handle dev, int on)
   1071 {
   1072 	if (on)
   1073 		dev->bus->use_polling++;
   1074 	else
   1075 		dev->bus->use_polling--;
   1076 	/* When polling we need to make sure there is nothing pending to do. */
   1077 	if (dev->bus->use_polling)
   1078 		dev->bus->methods->soft_intr(dev->bus);
   1079 }
   1080 
   1081 
   1082 usb_endpoint_descriptor_t *
   1083 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address)
   1084 {
   1085 	struct usbd_endpoint *ep;
   1086 	int i;
   1087 
   1088 	for (i = 0; i < iface->idesc->bNumEndpoints; i++) {
   1089 		ep = &iface->endpoints[i];
   1090 		if (ep->edesc->bEndpointAddress == address)
   1091 			return (iface->endpoints[i].edesc);
   1092 	}
   1093 	return (0);
   1094 }
   1095 
   1096 /*
   1097  * usbd_ratecheck() can limit the number of error messages that occurs.
   1098  * When a device is unplugged it may take up to 0.25s for the hub driver
   1099  * to notice it.  If the driver continuosly tries to do I/O operations
   1100  * this can generate a large number of messages.
   1101  */
   1102 int
   1103 usbd_ratecheck(struct timeval *last)
   1104 {
   1105 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1106 
   1107 	return (ratecheck(last, &errinterval));
   1108 }
   1109 
   1110 /*
   1111  * Search for a vendor/product pair in an array.  The item size is
   1112  * given as an argument.
   1113  */
   1114 const struct usb_devno *
   1115 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1116 		 u_int16_t vendor, u_int16_t product)
   1117 {
   1118 	while (nentries-- > 0) {
   1119 		u_int16_t tproduct = tbl->ud_product;
   1120 		if (tbl->ud_vendor == vendor &&
   1121 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1122 			return (tbl);
   1123 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1124 	}
   1125 	return (NULL);
   1126 }
   1127 
   1128 
   1129 void
   1130 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter)
   1131 {
   1132 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1133 
   1134         iter->cur = (const uByte *)cd;
   1135         iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1136 }
   1137 
   1138 const usb_descriptor_t *
   1139 usb_desc_iter_next(usbd_desc_iter_t *iter)
   1140 {
   1141 	const usb_descriptor_t *desc;
   1142 
   1143 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1144 		if (iter->cur != iter->end)
   1145 			printf("usb_desc_iter_next: bad descriptor\n");
   1146 		return NULL;
   1147 	}
   1148 	desc = (const usb_descriptor_t *)iter->cur;
   1149 	if (desc->bLength == 0) {
   1150 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1151 		return NULL;
   1152 	}
   1153 	iter->cur += desc->bLength;
   1154 	if (iter->cur > iter->end) {
   1155 		printf("usb_desc_iter_next: descriptor length too large\n");
   1156 		return NULL;
   1157 	}
   1158 	return desc;
   1159 }
   1160 
   1161 usbd_status
   1162 usbd_get_string(usbd_device_handle dev, int si, char *buf)
   1163 {
   1164 	return usbd_get_string0(dev, si, buf, 1);
   1165 }
   1166 
   1167 usbd_status
   1168 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode)
   1169 {
   1170 	int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE;
   1171 	usb_string_descriptor_t us;
   1172 	char *s;
   1173 	int i, n;
   1174 	u_int16_t c;
   1175 	usbd_status err;
   1176 	int size;
   1177 
   1178 	buf[0] = '\0';
   1179 	if (si == 0)
   1180 		return (USBD_INVAL);
   1181 	if (dev->quirks->uq_flags & UQ_NO_STRINGS)
   1182 		return (USBD_STALLED);
   1183 	if (dev->langid == USBD_NOLANG) {
   1184 		/* Set up default language */
   1185 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1186 		    &size);
   1187 		if (err || size < 4) {
   1188 			DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n"));
   1189 			dev->langid = 0; /* Well, just pick something then */
   1190 		} else {
   1191 			/* Pick the first language as the default. */
   1192 			dev->langid = UGETW(us.bString[0]);
   1193 		}
   1194 	}
   1195 	err = usbd_get_string_desc(dev, si, dev->langid, &us, &size);
   1196 	if (err)
   1197 		return (err);
   1198 	s = buf;
   1199 	n = size / 2 - 1;
   1200 	if (unicode) {
   1201 		for (i = 0; i < n; i++) {
   1202 			c = UGETW(us.bString[i]);
   1203 			if (swap)
   1204 				c = (c >> 8) | (c << 8);
   1205 			s += wput_utf8(s, 3, c);
   1206 		}
   1207 		*s++ = 0;
   1208 	}
   1209 #ifdef COMPAT_30
   1210 	else {
   1211 		for (i = 0; i < n; i++) {
   1212 			c = UGETW(us.bString[i]);
   1213 			if (swap)
   1214 				c = (c >> 8) | (c << 8);
   1215 			*s++ = (c < 0x80) ? c : '?';
   1216 		}
   1217 		*s++ = 0;
   1218 	}
   1219 #endif
   1220 	return (USBD_NORMAL_COMPLETION);
   1221 }
   1222