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