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