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