1 /* $NetBSD: usbdi.c,v 1.154 2013/07/11 19:46:44 aymeric Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg@eterna.com.au). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.154 2013/07/11 19:46:44 aymeric Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_compat_netbsd.h" 38 #endif 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 #include <sys/bus.h> 47 #include <sys/cpu.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) || defined(OHCI_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_flags(iface->device, iface, ep, ival, &p, flags); 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, 202 USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE), 203 &ipipe, ival); 204 if (err) 205 return (err); 206 xfer = usbd_alloc_xfer(iface->device); 207 if (xfer == NULL) { 208 err = USBD_NOMEM; 209 goto bad1; 210 } 211 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags, 212 USBD_NO_TIMEOUT, cb); 213 ipipe->intrxfer = xfer; 214 ipipe->repeat = 1; 215 err = usbd_transfer(xfer); 216 *pipe = ipipe; 217 if (err != USBD_IN_PROGRESS) 218 goto bad2; 219 return (USBD_NORMAL_COMPLETION); 220 221 bad2: 222 ipipe->intrxfer = NULL; 223 ipipe->repeat = 0; 224 usbd_free_xfer(xfer); 225 bad1: 226 usbd_close_pipe(ipipe); 227 return (err); 228 } 229 230 usbd_status 231 usbd_close_pipe(usbd_pipe_handle pipe) 232 { 233 int s; 234 235 #ifdef DIAGNOSTIC 236 if (pipe == NULL) { 237 printf("usbd_close_pipe: pipe==NULL\n"); 238 return (USBD_NORMAL_COMPLETION); 239 } 240 #endif 241 242 usbd_lock_pipe(pipe); 243 if (--pipe->refcnt != 0) { 244 usbd_unlock_pipe(pipe); 245 return (USBD_NORMAL_COMPLETION); 246 } 247 if (! SIMPLEQ_EMPTY(&pipe->queue)) { 248 usbd_unlock_pipe(pipe); 249 return (USBD_PENDING_REQUESTS); 250 } 251 LIST_REMOVE(pipe, next); 252 pipe->endpoint->refcnt--; 253 pipe->methods->close(pipe); 254 usbd_unlock_pipe(pipe); 255 if (pipe->intrxfer != NULL) 256 usbd_free_xfer(pipe->intrxfer); 257 free(pipe, M_USB); 258 return (USBD_NORMAL_COMPLETION); 259 } 260 261 usbd_status 262 usbd_transfer(usbd_xfer_handle xfer) 263 { 264 usbd_pipe_handle pipe = xfer->pipe; 265 usb_dma_t *dmap = &xfer->dmabuf; 266 usbd_status err; 267 unsigned int size, flags; 268 int s; 269 270 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n", 271 xfer, xfer->flags, pipe, pipe->running)); 272 273 #ifdef USB_DEBUG 274 if (usbdebug > 5) 275 usbd_dump_queue(pipe); 276 #endif 277 xfer->done = 0; 278 279 if (pipe->aborting) 280 return (USBD_CANCELLED); 281 282 size = xfer->length; 283 /* If there is no buffer, allocate one. */ 284 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) { 285 struct usbd_bus *bus = pipe->device->bus; 286 287 #ifdef DIAGNOSTIC 288 if (xfer->rqflags & URQ_AUTO_DMABUF) 289 printf("usbd_transfer: has old buffer!\n"); 290 #endif 291 err = bus->methods->allocm(bus, dmap, size); 292 if (err) 293 return (err); 294 xfer->rqflags |= URQ_AUTO_DMABUF; 295 } 296 297 flags = xfer->flags; 298 299 /* Copy data if going out. */ 300 if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer)) 301 memcpy(KERNADDR(dmap, 0), xfer->buffer, size); 302 303 /* xfer is not valid after the transfer method unless synchronous */ 304 err = pipe->methods->transfer(xfer); 305 306 if (err != USBD_IN_PROGRESS && err) { 307 /* The transfer has not been queued, so free buffer. */ 308 if (xfer->rqflags & URQ_AUTO_DMABUF) { 309 struct usbd_bus *bus = pipe->device->bus; 310 311 bus->methods->freem(bus, &xfer->dmabuf); 312 xfer->rqflags &= ~URQ_AUTO_DMABUF; 313 } 314 } 315 316 if (!(flags & USBD_SYNCHRONOUS)) 317 return (err); 318 319 /* Sync transfer, wait for completion. */ 320 if (err != USBD_IN_PROGRESS) 321 return (err); 322 usbd_lock_pipe(pipe); 323 while (!xfer->done) { 324 if (pipe->device->bus->use_polling) 325 panic("usbd_transfer: not done"); 326 327 err = 0; 328 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) { 329 if (pipe->device->bus->lock) 330 err = cv_wait_sig(&xfer->cv, pipe->device->bus->lock); 331 else 332 err = tsleep(xfer, PZERO|PCATCH, "usbsyn", 0); 333 } else { 334 if (pipe->device->bus->lock) 335 cv_wait(&xfer->cv, pipe->device->bus->lock); 336 else 337 err = tsleep(xfer, PRIBIO, "usbsyn", 0); 338 } 339 if (err) { 340 pipe->methods->abort(xfer); 341 break; 342 } 343 } 344 usbd_unlock_pipe(pipe); 345 return (xfer->status); 346 } 347 348 /* Like usbd_transfer(), but waits for completion. */ 349 usbd_status 350 usbd_sync_transfer(usbd_xfer_handle xfer) 351 { 352 xfer->flags |= USBD_SYNCHRONOUS; 353 return (usbd_transfer(xfer)); 354 } 355 356 /* Like usbd_transfer(), but waits for completion and listens for signals. */ 357 usbd_status 358 usbd_sync_transfer_sig(usbd_xfer_handle xfer) 359 { 360 xfer->flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG; 361 return (usbd_transfer(xfer)); 362 } 363 364 void * 365 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size) 366 { 367 struct usbd_bus *bus = xfer->device->bus; 368 usbd_status err; 369 370 #ifdef DIAGNOSTIC 371 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 372 printf("usbd_alloc_buffer: xfer already has a buffer\n"); 373 #endif 374 err = bus->methods->allocm(bus, &xfer->dmabuf, size); 375 if (err) 376 return (NULL); 377 xfer->rqflags |= URQ_DEV_DMABUF; 378 return (KERNADDR(&xfer->dmabuf, 0)); 379 } 380 381 void 382 usbd_free_buffer(usbd_xfer_handle xfer) 383 { 384 #ifdef DIAGNOSTIC 385 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) { 386 printf("usbd_free_buffer: no buffer\n"); 387 return; 388 } 389 #endif 390 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF); 391 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf); 392 } 393 394 void * 395 usbd_get_buffer(usbd_xfer_handle xfer) 396 { 397 if (!(xfer->rqflags & URQ_DEV_DMABUF)) 398 return (NULL); 399 return (KERNADDR(&xfer->dmabuf, 0)); 400 } 401 402 usbd_xfer_handle 403 usbd_alloc_xfer(usbd_device_handle dev) 404 { 405 usbd_xfer_handle xfer; 406 407 xfer = dev->bus->methods->allocx(dev->bus); 408 if (xfer == NULL) 409 return (NULL); 410 xfer->device = dev; 411 callout_init(&xfer->timeout_handle, 412 dev->bus->methods->get_lock ? CALLOUT_MPSAFE : 0); 413 cv_init(&xfer->cv, "usbxfer"); 414 cv_init(&xfer->hccv, "usbhcxfer"); 415 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer)); 416 return (xfer); 417 } 418 419 usbd_status 420 usbd_free_xfer(usbd_xfer_handle xfer) 421 { 422 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer)); 423 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 424 usbd_free_buffer(xfer); 425 #if defined(DIAGNOSTIC) 426 if (callout_pending(&xfer->timeout_handle)) { 427 callout_stop(&xfer->timeout_handle); 428 printf("usbd_free_xfer: timeout_handle pending\n"); 429 } 430 #endif 431 cv_destroy(&xfer->cv); 432 cv_destroy(&xfer->hccv); 433 xfer->device->bus->methods->freex(xfer->device->bus, xfer); 434 return (USBD_NORMAL_COMPLETION); 435 } 436 437 void 438 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 439 usbd_private_handle priv, void *buffer, u_int32_t length, 440 u_int16_t flags, u_int32_t timeout, 441 usbd_callback callback) 442 { 443 xfer->pipe = pipe; 444 xfer->priv = priv; 445 xfer->buffer = buffer; 446 xfer->length = length; 447 xfer->actlen = 0; 448 xfer->flags = flags; 449 xfer->timeout = timeout; 450 xfer->status = USBD_NOT_STARTED; 451 xfer->callback = callback; 452 xfer->rqflags &= ~URQ_REQUEST; 453 xfer->nframes = 0; 454 } 455 456 void 457 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev, 458 usbd_private_handle priv, u_int32_t timeout, 459 usb_device_request_t *req, void *buffer, 460 u_int32_t length, u_int16_t flags, 461 usbd_callback callback) 462 { 463 xfer->pipe = dev->default_pipe; 464 xfer->priv = priv; 465 xfer->buffer = buffer; 466 xfer->length = length; 467 xfer->actlen = 0; 468 xfer->flags = flags; 469 xfer->timeout = timeout; 470 xfer->status = USBD_NOT_STARTED; 471 xfer->callback = callback; 472 xfer->request = *req; 473 xfer->rqflags |= URQ_REQUEST; 474 xfer->nframes = 0; 475 } 476 477 void 478 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 479 usbd_private_handle priv, u_int16_t *frlengths, 480 u_int32_t nframes, u_int16_t flags, usbd_callback callback) 481 { 482 xfer->pipe = pipe; 483 xfer->priv = priv; 484 xfer->buffer = 0; 485 xfer->length = 0; 486 xfer->actlen = 0; 487 xfer->flags = flags; 488 xfer->timeout = USBD_NO_TIMEOUT; 489 xfer->status = USBD_NOT_STARTED; 490 xfer->callback = callback; 491 xfer->rqflags &= ~URQ_REQUEST; 492 xfer->frlengths = frlengths; 493 xfer->nframes = nframes; 494 } 495 496 void 497 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv, 498 void **buffer, u_int32_t *count, usbd_status *status) 499 { 500 if (priv != NULL) 501 *priv = xfer->priv; 502 if (buffer != NULL) 503 *buffer = xfer->buffer; 504 if (count != NULL) 505 *count = xfer->actlen; 506 if (status != NULL) 507 *status = xfer->status; 508 } 509 510 usb_config_descriptor_t * 511 usbd_get_config_descriptor(usbd_device_handle dev) 512 { 513 #ifdef DIAGNOSTIC 514 if (dev == NULL) { 515 printf("usbd_get_config_descriptor: dev == NULL\n"); 516 return (NULL); 517 } 518 #endif 519 return (dev->cdesc); 520 } 521 522 usb_interface_descriptor_t * 523 usbd_get_interface_descriptor(usbd_interface_handle iface) 524 { 525 #ifdef DIAGNOSTIC 526 if (iface == NULL) { 527 printf("usbd_get_interface_descriptor: dev == NULL\n"); 528 return (NULL); 529 } 530 #endif 531 return (iface->idesc); 532 } 533 534 usb_device_descriptor_t * 535 usbd_get_device_descriptor(usbd_device_handle dev) 536 { 537 return (&dev->ddesc); 538 } 539 540 usb_endpoint_descriptor_t * 541 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index) 542 { 543 if (index >= iface->idesc->bNumEndpoints) 544 return (NULL); 545 return (iface->endpoints[index].edesc); 546 } 547 548 /* Some drivers may wish to abort requests on the default pipe, * 549 * but there is no mechanism for getting a handle on it. */ 550 usbd_status 551 usbd_abort_default_pipe(struct usbd_device *device) 552 { 553 554 return usbd_abort_pipe(device->default_pipe); 555 } 556 557 usbd_status 558 usbd_abort_pipe(usbd_pipe_handle pipe) 559 { 560 usbd_status err; 561 int s; 562 usbd_xfer_handle intrxfer = pipe->intrxfer; 563 564 #ifdef DIAGNOSTIC 565 if (pipe == NULL) { 566 printf("usbd_abort_pipe: pipe==NULL\n"); 567 return (USBD_NORMAL_COMPLETION); 568 } 569 #endif 570 usbd_lock_pipe(pipe); 571 err = usbd_ar_pipe(pipe); 572 usbd_unlock_pipe(pipe); 573 if (pipe->intrxfer != intrxfer) 574 usbd_free_xfer(intrxfer); 575 return (err); 576 } 577 578 usbd_status 579 usbd_clear_endpoint_stall(usbd_pipe_handle pipe) 580 { 581 usbd_device_handle dev = pipe->device; 582 usb_device_request_t req; 583 usbd_status err; 584 585 DPRINTFN(8, ("usbd_clear_endpoint_stall\n")); 586 587 /* 588 * Clearing en endpoint stall resets the endpoint toggle, so 589 * do the same to the HC toggle. 590 */ 591 pipe->methods->cleartoggle(pipe); 592 593 req.bmRequestType = UT_WRITE_ENDPOINT; 594 req.bRequest = UR_CLEAR_FEATURE; 595 USETW(req.wValue, UF_ENDPOINT_HALT); 596 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 597 USETW(req.wLength, 0); 598 err = usbd_do_request(dev, &req, 0); 599 #if 0 600 XXX should we do this? 601 if (!err) { 602 pipe->state = USBD_PIPE_ACTIVE; 603 /* XXX activate pipe */ 604 } 605 #endif 606 return (err); 607 } 608 609 void 610 usbd_clear_endpoint_stall_async_cb(void *arg) 611 { 612 usbd_pipe_handle pipe = arg; 613 usbd_device_handle dev = pipe->device; 614 usb_device_request_t req; 615 616 pipe->methods->cleartoggle(pipe); 617 618 req.bmRequestType = UT_WRITE_ENDPOINT; 619 req.bRequest = UR_CLEAR_FEATURE; 620 USETW(req.wValue, UF_ENDPOINT_HALT); 621 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 622 USETW(req.wLength, 0); 623 (void)usbd_do_request_async(dev, &req, 0); 624 } 625 626 void 627 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe) 628 { 629 630 usb_add_task(pipe->device, &pipe->async_task, USB_TASKQ_DRIVER); 631 } 632 633 void 634 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe) 635 { 636 pipe->methods->cleartoggle(pipe); 637 } 638 639 usbd_status 640 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count) 641 { 642 #ifdef DIAGNOSTIC 643 if (iface == NULL || iface->idesc == NULL) { 644 printf("usbd_endpoint_count: NULL pointer\n"); 645 return (USBD_INVAL); 646 } 647 #endif 648 *count = iface->idesc->bNumEndpoints; 649 return (USBD_NORMAL_COMPLETION); 650 } 651 652 usbd_status 653 usbd_interface_count(usbd_device_handle dev, u_int8_t *count) 654 { 655 if (dev->cdesc == NULL) 656 return (USBD_NOT_CONFIGURED); 657 *count = dev->cdesc->bNumInterface; 658 return (USBD_NORMAL_COMPLETION); 659 } 660 661 void 662 usbd_interface2device_handle(usbd_interface_handle iface, 663 usbd_device_handle *dev) 664 { 665 *dev = iface->device; 666 } 667 668 usbd_status 669 usbd_device2interface_handle(usbd_device_handle dev, 670 u_int8_t ifaceno, usbd_interface_handle *iface) 671 { 672 if (dev->cdesc == NULL) 673 return (USBD_NOT_CONFIGURED); 674 if (ifaceno >= dev->cdesc->bNumInterface) 675 return (USBD_INVAL); 676 *iface = &dev->ifaces[ifaceno]; 677 return (USBD_NORMAL_COMPLETION); 678 } 679 680 usbd_device_handle 681 usbd_pipe2device_handle(usbd_pipe_handle pipe) 682 { 683 return (pipe->device); 684 } 685 686 /* XXXX use altno */ 687 usbd_status 688 usbd_set_interface(usbd_interface_handle iface, int altidx) 689 { 690 usb_device_request_t req; 691 usbd_status err; 692 void *endpoints; 693 694 if (LIST_FIRST(&iface->pipes) != 0) 695 return (USBD_IN_USE); 696 697 endpoints = iface->endpoints; 698 err = usbd_fill_iface_data(iface->device, iface->index, altidx); 699 if (err) 700 return (err); 701 702 /* new setting works, we can free old endpoints */ 703 if (endpoints != NULL) 704 free(endpoints, M_USB); 705 706 #ifdef DIAGNOSTIC 707 if (iface->idesc == NULL) { 708 printf("usbd_set_interface: NULL pointer\n"); 709 return (USBD_INVAL); 710 } 711 #endif 712 713 req.bmRequestType = UT_WRITE_INTERFACE; 714 req.bRequest = UR_SET_INTERFACE; 715 USETW(req.wValue, iface->idesc->bAlternateSetting); 716 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 717 USETW(req.wLength, 0); 718 return (usbd_do_request(iface->device, &req, 0)); 719 } 720 721 int 722 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno) 723 { 724 char *p = (char *)cdesc; 725 char *end = p + UGETW(cdesc->wTotalLength); 726 usb_interface_descriptor_t *d; 727 int n; 728 729 for (n = 0; p < end; p += d->bLength) { 730 d = (usb_interface_descriptor_t *)p; 731 if (p + d->bLength <= end && 732 d->bDescriptorType == UDESC_INTERFACE && 733 d->bInterfaceNumber == ifaceno) 734 n++; 735 } 736 return (n); 737 } 738 739 int 740 usbd_get_interface_altindex(usbd_interface_handle iface) 741 { 742 return (iface->altindex); 743 } 744 745 usbd_status 746 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface) 747 { 748 usb_device_request_t req; 749 750 req.bmRequestType = UT_READ_INTERFACE; 751 req.bRequest = UR_GET_INTERFACE; 752 USETW(req.wValue, 0); 753 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 754 USETW(req.wLength, 1); 755 return (usbd_do_request(iface->device, &req, aiface)); 756 } 757 758 /*** Internal routines ***/ 759 760 /* Dequeue all pipe operations, called at splusb(). */ 761 Static usbd_status 762 usbd_ar_pipe(usbd_pipe_handle pipe) 763 { 764 usbd_xfer_handle xfer; 765 766 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock)); 767 768 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe)); 769 #ifdef USB_DEBUG 770 if (usbdebug > 5) 771 usbd_dump_queue(pipe); 772 #endif 773 pipe->repeat = 0; 774 pipe->aborting = 1; 775 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) { 776 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n", 777 pipe, xfer, pipe->methods)); 778 /* Make the HC abort it (and invoke the callback). */ 779 pipe->methods->abort(xfer); 780 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */ 781 } 782 pipe->aborting = 0; 783 return (USBD_NORMAL_COMPLETION); 784 } 785 786 /* Called with USB lock held. */ 787 void 788 usb_transfer_complete(usbd_xfer_handle xfer) 789 { 790 usbd_pipe_handle pipe = xfer->pipe; 791 usb_dma_t *dmap = &xfer->dmabuf; 792 int sync = xfer->flags & USBD_SYNCHRONOUS; 793 int erred = xfer->status == USBD_CANCELLED || 794 xfer->status == USBD_TIMEOUT; 795 int polling = pipe->device->bus->use_polling; 796 int repeat; 797 798 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d " 799 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen)); 800 801 KASSERT(polling || pipe->device->bus->lock == NULL || 802 mutex_owned(pipe->device->bus->lock)); 803 804 #ifdef DIAGNOSTIC 805 if (xfer->busy_free != XFER_ONQU) { 806 printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n", 807 xfer, xfer->busy_free); 808 } 809 #endif 810 811 #ifdef DIAGNOSTIC 812 if (pipe == NULL) { 813 printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer); 814 return; 815 } 816 #endif 817 repeat = pipe->repeat; 818 /* XXXX */ 819 if (polling) 820 pipe->running = 0; 821 822 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 && 823 usbd_xfer_isread(xfer)) { 824 #ifdef DIAGNOSTIC 825 if (xfer->actlen > xfer->length) { 826 printf("%s: actlen (%d) > len (%d)\n", __func__, 827 xfer->actlen, xfer->length); 828 xfer->actlen = xfer->length; 829 } 830 #endif 831 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen); 832 } 833 834 /* if we allocated the buffer in usbd_transfer() we free it here. */ 835 if (xfer->rqflags & URQ_AUTO_DMABUF) { 836 if (!repeat) { 837 struct usbd_bus *bus = pipe->device->bus; 838 bus->methods->freem(bus, dmap); 839 xfer->rqflags &= ~URQ_AUTO_DMABUF; 840 } 841 } 842 843 if (!repeat) { 844 /* Remove request from queue. */ 845 846 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->queue), 847 "pipe %p is empty, but xfer %p wants to complete", pipe, 848 xfer); 849 #ifdef DIAGNOSTIC 850 if (xfer != SIMPLEQ_FIRST(&pipe->queue)) 851 printf("%s: bad dequeue %p != %p\n", __func__, 852 xfer, SIMPLEQ_FIRST(&pipe->queue)); 853 xfer->busy_free = XFER_BUSY; 854 #endif 855 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next); 856 } 857 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n", 858 repeat, SIMPLEQ_FIRST(&pipe->queue))); 859 860 /* Count completed transfers. */ 861 ++pipe->device->bus->stats.uds_requests 862 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE]; 863 864 xfer->done = 1; 865 if (!xfer->status && xfer->actlen < xfer->length && 866 !(xfer->flags & USBD_SHORT_XFER_OK)) { 867 DPRINTFN(-1,("usb_transfer_complete: short transfer %d<%d\n", 868 xfer->actlen, xfer->length)); 869 xfer->status = USBD_SHORT_XFER; 870 } 871 872 if (repeat) { 873 if (xfer->callback) { 874 if (pipe->device->bus->lock && !polling) 875 mutex_exit(pipe->device->bus->lock); 876 877 if (!(pipe->flags & USBD_MPSAFE)) 878 KERNEL_LOCK(1, curlwp); 879 xfer->callback(xfer, xfer->priv, xfer->status); 880 if (!(pipe->flags & USBD_MPSAFE)) 881 KERNEL_UNLOCK_ONE(curlwp); 882 883 if (pipe->device->bus->lock && !polling) 884 mutex_enter(pipe->device->bus->lock); 885 } 886 pipe->methods->done(xfer); 887 } else { 888 pipe->methods->done(xfer); 889 if (xfer->callback) { 890 if (pipe->device->bus->lock && !polling) 891 mutex_exit(pipe->device->bus->lock); 892 893 if (!(pipe->flags & USBD_MPSAFE)) 894 KERNEL_LOCK(1, curlwp); 895 xfer->callback(xfer, xfer->priv, xfer->status); 896 if (!(pipe->flags & USBD_MPSAFE)) 897 KERNEL_UNLOCK_ONE(curlwp); 898 899 if (pipe->device->bus->lock && !polling) 900 mutex_enter(pipe->device->bus->lock); 901 } 902 } 903 904 if (sync && !polling) { 905 if (pipe->device->bus->lock) 906 cv_broadcast(&xfer->cv); 907 else 908 wakeup(xfer); /* XXXSMP ok */ 909 } 910 911 if (!repeat) { 912 /* XXX should we stop the queue on all errors? */ 913 if (erred && pipe->iface != NULL) /* not control pipe */ 914 pipe->running = 0; 915 else 916 usbd_start_next(pipe); 917 } 918 } 919 920 /* Called with USB lock held. */ 921 usbd_status 922 usb_insert_transfer(usbd_xfer_handle xfer) 923 { 924 usbd_pipe_handle pipe = xfer->pipe; 925 usbd_status err; 926 927 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n", 928 pipe, pipe->running, xfer->timeout)); 929 930 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock)); 931 932 #ifdef DIAGNOSTIC 933 if (xfer->busy_free != XFER_BUSY) { 934 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n", 935 xfer, xfer->busy_free); 936 return (USBD_INVAL); 937 } 938 xfer->busy_free = XFER_ONQU; 939 #endif 940 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next); 941 if (pipe->running) 942 err = USBD_IN_PROGRESS; 943 else { 944 pipe->running = 1; 945 err = USBD_NORMAL_COMPLETION; 946 } 947 return (err); 948 } 949 950 /* Called with USB lock held. */ 951 void 952 usbd_start_next(usbd_pipe_handle pipe) 953 { 954 usbd_xfer_handle xfer; 955 usbd_status err; 956 957 #ifdef DIAGNOSTIC 958 if (pipe == NULL) { 959 printf("usbd_start_next: pipe == NULL\n"); 960 return; 961 } 962 if (pipe->methods == NULL || pipe->methods->start == NULL) { 963 printf("usbd_start_next: pipe=%p no start method\n", pipe); 964 return; 965 } 966 #endif 967 968 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock)); 969 970 /* Get next request in queue. */ 971 xfer = SIMPLEQ_FIRST(&pipe->queue); 972 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer)); 973 if (xfer == NULL) { 974 pipe->running = 0; 975 } else { 976 if (pipe->device->bus->lock) 977 mutex_exit(pipe->device->bus->lock); 978 err = pipe->methods->start(xfer); 979 if (pipe->device->bus->lock) 980 mutex_enter(pipe->device->bus->lock); 981 if (err != USBD_IN_PROGRESS) { 982 printf("usbd_start_next: error=%d\n", err); 983 pipe->running = 0; 984 /* XXX do what? */ 985 } 986 } 987 988 KASSERT(pipe->device->bus->lock == NULL || mutex_owned(pipe->device->bus->lock)); 989 } 990 991 usbd_status 992 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data) 993 { 994 return (usbd_do_request_flags(dev, req, data, 0, 0, 995 USBD_DEFAULT_TIMEOUT)); 996 } 997 998 usbd_status 999 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req, 1000 void *data, u_int16_t flags, int *actlen, u_int32_t timo) 1001 { 1002 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req, 1003 data, flags, actlen, timo)); 1004 } 1005 1006 usbd_status 1007 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe, 1008 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen, 1009 u_int32_t timeout) 1010 { 1011 usbd_xfer_handle xfer; 1012 usbd_status err; 1013 1014 #ifdef DIAGNOSTIC 1015 if (cpu_intr_p() || cpu_softintr_p()) { 1016 printf("usbd_do_request: not in process context\n"); 1017 return (USBD_INVAL); 1018 } 1019 #endif 1020 1021 xfer = usbd_alloc_xfer(dev); 1022 if (xfer == NULL) 1023 return (USBD_NOMEM); 1024 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, 1025 data, UGETW(req->wLength), flags, 0); 1026 xfer->pipe = pipe; 1027 err = usbd_sync_transfer(xfer); 1028 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 1029 if (xfer->actlen > xfer->length) { 1030 DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x" 1031 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 1032 __func__, dev->address, xfer->request.bmRequestType, 1033 xfer->request.bRequest, UGETW(xfer->request.wValue), 1034 UGETW(xfer->request.wIndex), 1035 UGETW(xfer->request.wLength), 1036 xfer->length, xfer->actlen)); 1037 } 1038 #endif 1039 if (actlen != NULL) 1040 *actlen = xfer->actlen; 1041 if (err == USBD_STALLED) { 1042 /* 1043 * The control endpoint has stalled. Control endpoints 1044 * should not halt, but some may do so anyway so clear 1045 * any halt condition. 1046 */ 1047 usb_device_request_t treq; 1048 usb_status_t status; 1049 u_int16_t s; 1050 usbd_status nerr; 1051 1052 treq.bmRequestType = UT_READ_ENDPOINT; 1053 treq.bRequest = UR_GET_STATUS; 1054 USETW(treq.wValue, 0); 1055 USETW(treq.wIndex, 0); 1056 USETW(treq.wLength, sizeof(usb_status_t)); 1057 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1058 &treq, &status,sizeof(usb_status_t), 1059 0, 0); 1060 nerr = usbd_sync_transfer(xfer); 1061 if (nerr) 1062 goto bad; 1063 s = UGETW(status.wStatus); 1064 DPRINTF(("usbd_do_request: status = 0x%04x\n", s)); 1065 if (!(s & UES_HALT)) 1066 goto bad; 1067 treq.bmRequestType = UT_WRITE_ENDPOINT; 1068 treq.bRequest = UR_CLEAR_FEATURE; 1069 USETW(treq.wValue, UF_ENDPOINT_HALT); 1070 USETW(treq.wIndex, 0); 1071 USETW(treq.wLength, 0); 1072 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1073 &treq, &status, 0, 0, 0); 1074 nerr = usbd_sync_transfer(xfer); 1075 if (nerr) 1076 goto bad; 1077 } 1078 1079 bad: 1080 if (err) { 1081 DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err))); 1082 } 1083 usbd_free_xfer(xfer); 1084 return (err); 1085 } 1086 1087 void 1088 usbd_do_request_async_cb(usbd_xfer_handle xfer, 1089 usbd_private_handle priv, usbd_status status) 1090 { 1091 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 1092 if (xfer->actlen > xfer->length) { 1093 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x" 1094 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 1095 xfer->pipe->device->address, 1096 xfer->request.bmRequestType, 1097 xfer->request.bRequest, UGETW(xfer->request.wValue), 1098 UGETW(xfer->request.wIndex), 1099 UGETW(xfer->request.wLength), 1100 xfer->length, xfer->actlen)); 1101 } 1102 #endif 1103 usbd_free_xfer(xfer); 1104 } 1105 1106 /* 1107 * Execute a request without waiting for completion. 1108 * Can be used from interrupt context. 1109 */ 1110 usbd_status 1111 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req, 1112 void *data) 1113 { 1114 usbd_xfer_handle xfer; 1115 usbd_status err; 1116 1117 xfer = usbd_alloc_xfer(dev); 1118 if (xfer == NULL) 1119 return (USBD_NOMEM); 1120 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req, 1121 data, UGETW(req->wLength), 0, usbd_do_request_async_cb); 1122 err = usbd_transfer(xfer); 1123 if (err != USBD_IN_PROGRESS) { 1124 usbd_free_xfer(xfer); 1125 return (err); 1126 } 1127 return (USBD_NORMAL_COMPLETION); 1128 } 1129 1130 const struct usbd_quirks * 1131 usbd_get_quirks(usbd_device_handle dev) 1132 { 1133 #ifdef DIAGNOSTIC 1134 if (dev == NULL) { 1135 printf("usbd_get_quirks: dev == NULL\n"); 1136 return 0; 1137 } 1138 #endif 1139 return (dev->quirks); 1140 } 1141 1142 /* XXX do periodic free() of free list */ 1143 1144 /* 1145 * Called from keyboard driver when in polling mode. 1146 */ 1147 void 1148 usbd_dopoll(usbd_interface_handle iface) 1149 { 1150 iface->device->bus->methods->do_poll(iface->device->bus); 1151 } 1152 1153 /* 1154 * XXX use this more??? use_polling it touched manually all over 1155 */ 1156 void 1157 usbd_set_polling(usbd_device_handle dev, int on) 1158 { 1159 if (on) 1160 dev->bus->use_polling++; 1161 else 1162 dev->bus->use_polling--; 1163 1164 /* Kick the host controller when switching modes */ 1165 if (dev->bus->lock) 1166 mutex_enter(dev->bus->lock); 1167 (*dev->bus->methods->soft_intr)(dev->bus); 1168 if (dev->bus->lock) 1169 mutex_exit(dev->bus->lock); 1170 1171 } 1172 1173 1174 usb_endpoint_descriptor_t * 1175 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address) 1176 { 1177 struct usbd_endpoint *ep; 1178 int i; 1179 1180 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 1181 ep = &iface->endpoints[i]; 1182 if (ep->edesc->bEndpointAddress == address) 1183 return (iface->endpoints[i].edesc); 1184 } 1185 return (NULL); 1186 } 1187 1188 /* 1189 * usbd_ratecheck() can limit the number of error messages that occurs. 1190 * When a device is unplugged it may take up to 0.25s for the hub driver 1191 * to notice it. If the driver continuosly tries to do I/O operations 1192 * this can generate a large number of messages. 1193 */ 1194 int 1195 usbd_ratecheck(struct timeval *last) 1196 { 1197 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/ 1198 1199 return (ratecheck(last, &errinterval)); 1200 } 1201 1202 /* 1203 * Search for a vendor/product pair in an array. The item size is 1204 * given as an argument. 1205 */ 1206 const struct usb_devno * 1207 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz, 1208 u_int16_t vendor, u_int16_t product) 1209 { 1210 while (nentries-- > 0) { 1211 u_int16_t tproduct = tbl->ud_product; 1212 if (tbl->ud_vendor == vendor && 1213 (tproduct == product || tproduct == USB_PRODUCT_ANY)) 1214 return (tbl); 1215 tbl = (const struct usb_devno *)((const char *)tbl + sz); 1216 } 1217 return (NULL); 1218 } 1219 1220 1221 void 1222 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter) 1223 { 1224 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 1225 1226 iter->cur = (const uByte *)cd; 1227 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength); 1228 } 1229 1230 const usb_descriptor_t * 1231 usb_desc_iter_next(usbd_desc_iter_t *iter) 1232 { 1233 const usb_descriptor_t *desc; 1234 1235 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) { 1236 if (iter->cur != iter->end) 1237 printf("usb_desc_iter_next: bad descriptor\n"); 1238 return NULL; 1239 } 1240 desc = (const usb_descriptor_t *)iter->cur; 1241 if (desc->bLength == 0) { 1242 printf("usb_desc_iter_next: descriptor length = 0\n"); 1243 return NULL; 1244 } 1245 iter->cur += desc->bLength; 1246 if (iter->cur > iter->end) { 1247 printf("usb_desc_iter_next: descriptor length too large\n"); 1248 return NULL; 1249 } 1250 return desc; 1251 } 1252 1253 usbd_status 1254 usbd_get_string(usbd_device_handle dev, int si, char *buf) 1255 { 1256 return usbd_get_string0(dev, si, buf, 1); 1257 } 1258 1259 usbd_status 1260 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode) 1261 { 1262 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE; 1263 usb_string_descriptor_t us; 1264 char *s; 1265 int i, n; 1266 u_int16_t c; 1267 usbd_status err; 1268 int size; 1269 1270 buf[0] = '\0'; 1271 if (si == 0) 1272 return (USBD_INVAL); 1273 if (dev->quirks->uq_flags & UQ_NO_STRINGS) 1274 return (USBD_STALLED); 1275 if (dev->langid == USBD_NOLANG) { 1276 /* Set up default language */ 1277 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us, 1278 &size); 1279 if (err || size < 4) { 1280 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n")); 1281 dev->langid = 0; /* Well, just pick something then */ 1282 } else { 1283 /* Pick the first language as the default. */ 1284 dev->langid = UGETW(us.bString[0]); 1285 } 1286 } 1287 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 1288 if (err) 1289 return (err); 1290 s = buf; 1291 n = size / 2 - 1; 1292 if (unicode) { 1293 for (i = 0; i < n; i++) { 1294 c = UGETW(us.bString[i]); 1295 if (swap) 1296 c = (c >> 8) | (c << 8); 1297 s += wput_utf8(s, 3, c); 1298 } 1299 *s++ = 0; 1300 } 1301 #ifdef COMPAT_30 1302 else { 1303 for (i = 0; i < n; i++) { 1304 c = UGETW(us.bString[i]); 1305 if (swap) 1306 c = (c >> 8) | (c << 8); 1307 *s++ = (c < 0x80) ? c : '?'; 1308 } 1309 *s++ = 0; 1310 } 1311 #endif 1312 return (USBD_NORMAL_COMPLETION); 1313 } 1314