1 /* $NetBSD: usbdi.c,v 1.168 2015/12/22 22:26:16 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.168 2015/12/22 22:26:16 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 * The transfer made it onto the pipe queue, but didn't get 328 * accepted by the HCD for some reason. It needs removing 329 * from the pipe queue. 330 */ 331 usbd_lock_pipe(pipe); 332 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next); 333 usbd_start_next(pipe); 334 usbd_unlock_pipe(pipe); 335 } 336 337 if (!(flags & USBD_SYNCHRONOUS)) { 338 USBHIST_LOG(usbdebug, "<- done xfer %p, not sync (err %d)", 339 xfer, err, 0, 0); 340 return (err); 341 } 342 343 if (err != USBD_IN_PROGRESS) { 344 USBHIST_LOG(usbdebug, "<- done xfer %p, not in progress " 345 "(err %d)", xfer, err, 0, 0); 346 return (err); 347 } 348 349 /* Sync transfer, wait for completion. */ 350 usbd_lock_pipe(pipe); 351 while (!xfer->done) { 352 if (pipe->device->bus->use_polling) 353 panic("usbd_transfer: not done"); 354 USBHIST_LOG(usbdebug, "<- sleeping on xfer %p", xfer, 0, 0, 0); 355 356 err = 0; 357 if ((flags & USBD_SYNCHRONOUS_SIG) != 0) { 358 err = cv_wait_sig(&xfer->cv, pipe->device->bus->lock); 359 } else { 360 cv_wait(&xfer->cv, pipe->device->bus->lock); 361 } 362 if (err) { 363 if (!xfer->done) 364 pipe->methods->abort(xfer); 365 break; 366 } 367 } 368 usbd_unlock_pipe(pipe); 369 return (xfer->status); 370 } 371 372 /* Like usbd_transfer(), but waits for completion. */ 373 usbd_status 374 usbd_sync_transfer(usbd_xfer_handle xfer) 375 { 376 xfer->flags |= USBD_SYNCHRONOUS; 377 return (usbd_transfer(xfer)); 378 } 379 380 /* Like usbd_transfer(), but waits for completion and listens for signals. */ 381 usbd_status 382 usbd_sync_transfer_sig(usbd_xfer_handle xfer) 383 { 384 xfer->flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG; 385 return (usbd_transfer(xfer)); 386 } 387 388 void * 389 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size) 390 { 391 struct usbd_bus *bus = xfer->device->bus; 392 usbd_status err; 393 394 #ifdef DIAGNOSTIC 395 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 396 printf("usbd_alloc_buffer: xfer already has a buffer\n"); 397 #endif 398 err = bus->methods->allocm(bus, &xfer->dmabuf, size); 399 if (err) 400 return (NULL); 401 xfer->rqflags |= URQ_DEV_DMABUF; 402 return (KERNADDR(&xfer->dmabuf, 0)); 403 } 404 405 void 406 usbd_free_buffer(usbd_xfer_handle xfer) 407 { 408 #ifdef DIAGNOSTIC 409 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) { 410 printf("usbd_free_buffer: no buffer\n"); 411 return; 412 } 413 #endif 414 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF); 415 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf); 416 } 417 418 void * 419 usbd_get_buffer(usbd_xfer_handle xfer) 420 { 421 if (!(xfer->rqflags & URQ_DEV_DMABUF)) 422 return (NULL); 423 return (KERNADDR(&xfer->dmabuf, 0)); 424 } 425 426 usbd_xfer_handle 427 usbd_alloc_xfer(usbd_device_handle dev) 428 { 429 usbd_xfer_handle xfer; 430 431 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 432 433 xfer = dev->bus->methods->allocx(dev->bus); 434 if (xfer == NULL) 435 return (NULL); 436 xfer->device = dev; 437 callout_init(&xfer->timeout_handle, CALLOUT_MPSAFE); 438 cv_init(&xfer->cv, "usbxfer"); 439 cv_init(&xfer->hccv, "usbhcxfer"); 440 441 USBHIST_LOG(usbdebug, "returns %p", xfer, 0, 0, 0); 442 443 return (xfer); 444 } 445 446 usbd_status 447 usbd_free_xfer(usbd_xfer_handle xfer) 448 { 449 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 450 451 USBHIST_LOG(usbdebug, "%p", xfer, 0, 0, 0); 452 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 453 usbd_free_buffer(xfer); 454 #if defined(DIAGNOSTIC) 455 if (callout_pending(&xfer->timeout_handle)) { 456 callout_stop(&xfer->timeout_handle); 457 printf("usbd_free_xfer: timeout_handle pending\n"); 458 } 459 #endif 460 cv_destroy(&xfer->cv); 461 cv_destroy(&xfer->hccv); 462 xfer->device->bus->methods->freex(xfer->device->bus, xfer); 463 return (USBD_NORMAL_COMPLETION); 464 } 465 466 void 467 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 468 usbd_private_handle priv, void *buffer, u_int32_t length, 469 u_int16_t flags, u_int32_t timeout, 470 usbd_callback callback) 471 { 472 xfer->pipe = pipe; 473 xfer->priv = priv; 474 xfer->buffer = buffer; 475 xfer->length = length; 476 xfer->actlen = 0; 477 xfer->flags = flags; 478 xfer->timeout = timeout; 479 xfer->status = USBD_NOT_STARTED; 480 xfer->callback = callback; 481 xfer->rqflags &= ~URQ_REQUEST; 482 xfer->nframes = 0; 483 } 484 485 void 486 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev, 487 usbd_private_handle priv, u_int32_t timeout, 488 usb_device_request_t *req, void *buffer, 489 u_int32_t length, u_int16_t flags, 490 usbd_callback callback) 491 { 492 xfer->pipe = dev->default_pipe; 493 xfer->priv = priv; 494 xfer->buffer = buffer; 495 xfer->length = length; 496 xfer->actlen = 0; 497 xfer->flags = flags; 498 xfer->timeout = timeout; 499 xfer->status = USBD_NOT_STARTED; 500 xfer->callback = callback; 501 xfer->request = *req; 502 xfer->rqflags |= URQ_REQUEST; 503 xfer->nframes = 0; 504 } 505 506 void 507 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 508 usbd_private_handle priv, u_int16_t *frlengths, 509 u_int32_t nframes, u_int16_t flags, usbd_callback callback) 510 { 511 xfer->pipe = pipe; 512 xfer->priv = priv; 513 xfer->buffer = 0; 514 xfer->length = 0; 515 xfer->actlen = 0; 516 xfer->flags = flags; 517 xfer->timeout = USBD_NO_TIMEOUT; 518 xfer->status = USBD_NOT_STARTED; 519 xfer->callback = callback; 520 xfer->rqflags &= ~URQ_REQUEST; 521 xfer->frlengths = frlengths; 522 xfer->nframes = nframes; 523 } 524 525 void 526 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv, 527 void **buffer, u_int32_t *count, usbd_status *status) 528 { 529 if (priv != NULL) 530 *priv = xfer->priv; 531 if (buffer != NULL) 532 *buffer = xfer->buffer; 533 if (count != NULL) 534 *count = xfer->actlen; 535 if (status != NULL) 536 *status = xfer->status; 537 } 538 539 usb_config_descriptor_t * 540 usbd_get_config_descriptor(usbd_device_handle dev) 541 { 542 #ifdef DIAGNOSTIC 543 if (dev == NULL) { 544 printf("usbd_get_config_descriptor: dev == NULL\n"); 545 return (NULL); 546 } 547 #endif 548 return (dev->cdesc); 549 } 550 551 usb_interface_descriptor_t * 552 usbd_get_interface_descriptor(usbd_interface_handle iface) 553 { 554 #ifdef DIAGNOSTIC 555 if (iface == NULL) { 556 printf("usbd_get_interface_descriptor: dev == NULL\n"); 557 return (NULL); 558 } 559 #endif 560 return (iface->idesc); 561 } 562 563 usb_device_descriptor_t * 564 usbd_get_device_descriptor(usbd_device_handle dev) 565 { 566 return (&dev->ddesc); 567 } 568 569 usb_endpoint_descriptor_t * 570 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index) 571 { 572 if (index >= iface->idesc->bNumEndpoints) 573 return (NULL); 574 return (iface->endpoints[index].edesc); 575 } 576 577 /* Some drivers may wish to abort requests on the default pipe, * 578 * but there is no mechanism for getting a handle on it. */ 579 usbd_status 580 usbd_abort_default_pipe(struct usbd_device *device) 581 { 582 583 return usbd_abort_pipe(device->default_pipe); 584 } 585 586 usbd_status 587 usbd_abort_pipe(usbd_pipe_handle pipe) 588 { 589 usbd_status err; 590 591 #ifdef DIAGNOSTIC 592 if (pipe == NULL) { 593 printf("usbd_abort_pipe: pipe==NULL\n"); 594 return (USBD_NORMAL_COMPLETION); 595 } 596 #endif 597 usbd_lock_pipe(pipe); 598 err = usbd_ar_pipe(pipe); 599 usbd_unlock_pipe(pipe); 600 return (err); 601 } 602 603 usbd_status 604 usbd_clear_endpoint_stall(usbd_pipe_handle pipe) 605 { 606 usbd_device_handle dev = pipe->device; 607 usb_device_request_t req; 608 usbd_status err; 609 610 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 611 612 /* 613 * Clearing en endpoint stall resets the endpoint toggle, so 614 * do the same to the HC toggle. 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 err = usbd_do_request(dev, &req, 0); 624 #if 0 625 XXX should we do this? 626 if (!err) { 627 pipe->state = USBD_PIPE_ACTIVE; 628 /* XXX activate pipe */ 629 } 630 #endif 631 return (err); 632 } 633 634 void 635 usbd_clear_endpoint_stall_task(void *arg) 636 { 637 usbd_pipe_handle pipe = arg; 638 usbd_device_handle dev = pipe->device; 639 usb_device_request_t req; 640 641 pipe->methods->cleartoggle(pipe); 642 643 req.bmRequestType = UT_WRITE_ENDPOINT; 644 req.bRequest = UR_CLEAR_FEATURE; 645 USETW(req.wValue, UF_ENDPOINT_HALT); 646 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 647 USETW(req.wLength, 0); 648 (void)usbd_do_request(dev, &req, 0); 649 } 650 651 void 652 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe) 653 { 654 655 usb_add_task(pipe->device, &pipe->async_task, USB_TASKQ_DRIVER); 656 } 657 658 void 659 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe) 660 { 661 pipe->methods->cleartoggle(pipe); 662 } 663 664 usbd_status 665 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count) 666 { 667 #ifdef DIAGNOSTIC 668 if (iface == NULL || iface->idesc == NULL) { 669 printf("usbd_endpoint_count: NULL pointer\n"); 670 return (USBD_INVAL); 671 } 672 #endif 673 *count = iface->idesc->bNumEndpoints; 674 return (USBD_NORMAL_COMPLETION); 675 } 676 677 usbd_status 678 usbd_interface_count(usbd_device_handle dev, u_int8_t *count) 679 { 680 if (dev->cdesc == NULL) 681 return (USBD_NOT_CONFIGURED); 682 *count = dev->cdesc->bNumInterface; 683 return (USBD_NORMAL_COMPLETION); 684 } 685 686 void 687 usbd_interface2device_handle(usbd_interface_handle iface, 688 usbd_device_handle *dev) 689 { 690 *dev = iface->device; 691 } 692 693 usbd_status 694 usbd_device2interface_handle(usbd_device_handle dev, 695 u_int8_t ifaceno, usbd_interface_handle *iface) 696 { 697 if (dev->cdesc == NULL) 698 return (USBD_NOT_CONFIGURED); 699 if (ifaceno >= dev->cdesc->bNumInterface) 700 return (USBD_INVAL); 701 *iface = &dev->ifaces[ifaceno]; 702 return (USBD_NORMAL_COMPLETION); 703 } 704 705 usbd_device_handle 706 usbd_pipe2device_handle(usbd_pipe_handle pipe) 707 { 708 return (pipe->device); 709 } 710 711 /* XXXX use altno */ 712 usbd_status 713 usbd_set_interface(usbd_interface_handle iface, int altidx) 714 { 715 usb_device_request_t req; 716 usbd_status err; 717 void *endpoints; 718 719 if (LIST_FIRST(&iface->pipes) != 0) 720 return (USBD_IN_USE); 721 722 endpoints = iface->endpoints; 723 err = usbd_fill_iface_data(iface->device, iface->index, altidx); 724 if (err) 725 return (err); 726 727 /* new setting works, we can free old endpoints */ 728 if (endpoints != NULL) 729 free(endpoints, M_USB); 730 731 #ifdef DIAGNOSTIC 732 if (iface->idesc == NULL) { 733 printf("usbd_set_interface: NULL pointer\n"); 734 return (USBD_INVAL); 735 } 736 #endif 737 738 req.bmRequestType = UT_WRITE_INTERFACE; 739 req.bRequest = UR_SET_INTERFACE; 740 USETW(req.wValue, iface->idesc->bAlternateSetting); 741 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 742 USETW(req.wLength, 0); 743 return (usbd_do_request(iface->device, &req, 0)); 744 } 745 746 int 747 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno) 748 { 749 char *p = (char *)cdesc; 750 char *end = p + UGETW(cdesc->wTotalLength); 751 usb_interface_descriptor_t *d; 752 int n; 753 754 for (n = 0; p < end; p += d->bLength) { 755 d = (usb_interface_descriptor_t *)p; 756 if (p + d->bLength <= end && 757 d->bDescriptorType == UDESC_INTERFACE && 758 d->bInterfaceNumber == ifaceno) 759 n++; 760 } 761 return (n); 762 } 763 764 int 765 usbd_get_interface_altindex(usbd_interface_handle iface) 766 { 767 return (iface->altindex); 768 } 769 770 usbd_status 771 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface) 772 { 773 usb_device_request_t req; 774 775 req.bmRequestType = UT_READ_INTERFACE; 776 req.bRequest = UR_GET_INTERFACE; 777 USETW(req.wValue, 0); 778 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 779 USETW(req.wLength, 1); 780 return (usbd_do_request(iface->device, &req, aiface)); 781 } 782 783 /*** Internal routines ***/ 784 785 /* Dequeue all pipe operations, called at splusb(). */ 786 Static usbd_status 787 usbd_ar_pipe(usbd_pipe_handle pipe) 788 { 789 usbd_xfer_handle xfer; 790 791 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 792 793 KASSERT(mutex_owned(pipe->device->bus->lock)); 794 795 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0); 796 #ifdef USB_DEBUG 797 if (usbdebug > 5) 798 usbd_dump_queue(pipe); 799 #endif 800 pipe->repeat = 0; 801 pipe->aborting = 1; 802 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) { 803 USBHIST_LOG(usbdebug, "pipe = %p xfer = %p (methods = %p)", 804 pipe, xfer, pipe->methods, 0); 805 /* Make the HC abort it (and invoke the callback). */ 806 pipe->methods->abort(xfer); 807 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */ 808 } 809 pipe->aborting = 0; 810 return (USBD_NORMAL_COMPLETION); 811 } 812 813 /* Called with USB lock held. */ 814 void 815 usb_transfer_complete(usbd_xfer_handle xfer) 816 { 817 usbd_pipe_handle pipe = xfer->pipe; 818 usb_dma_t *dmap = &xfer->dmabuf; 819 int sync = xfer->flags & USBD_SYNCHRONOUS; 820 int erred = xfer->status == USBD_CANCELLED || 821 xfer->status == USBD_TIMEOUT; 822 int polling = pipe->device->bus->use_polling; 823 int repeat; 824 825 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 826 827 USBHIST_LOG(usbdebug, "pipe = %p xfer = %p status = %d actlen = %d", 828 pipe, xfer, xfer->status, xfer->actlen); 829 830 KASSERT(polling || mutex_owned(pipe->device->bus->lock)); 831 832 #ifdef DIAGNOSTIC 833 if (xfer->busy_free != XFER_ONQU) { 834 printf("usb_transfer_complete: xfer=%p not queued 0x%08x\n", 835 xfer, xfer->busy_free); 836 } 837 #endif 838 839 #ifdef DIAGNOSTIC 840 if (pipe == NULL) { 841 printf("usb_transfer_complete: pipe==0, xfer=%p\n", xfer); 842 return; 843 } 844 #endif 845 repeat = pipe->repeat; 846 /* XXXX */ 847 if (polling) 848 pipe->running = 0; 849 850 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 && 851 usbd_xfer_isread(xfer)) { 852 if (xfer->actlen > xfer->length) { 853 #ifdef DIAGNOSTIC 854 printf("%s: actlen (%d) > len (%d)\n", __func__, 855 xfer->actlen, xfer->length); 856 #endif 857 xfer->actlen = xfer->length; 858 } 859 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen); 860 } 861 862 /* if we allocated the buffer in usbd_transfer() we free it here. */ 863 if (xfer->rqflags & URQ_AUTO_DMABUF) { 864 if (!repeat) { 865 struct usbd_bus *bus = pipe->device->bus; 866 bus->methods->freem(bus, dmap); 867 xfer->rqflags &= ~URQ_AUTO_DMABUF; 868 } 869 } 870 871 if (!repeat) { 872 /* Remove request from queue. */ 873 874 KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->queue), 875 "pipe %p is empty, but xfer %p wants to complete", pipe, 876 xfer); 877 #ifdef DIAGNOSTIC 878 if (xfer != SIMPLEQ_FIRST(&pipe->queue)) 879 printf("%s: bad dequeue %p != %p\n", __func__, 880 xfer, SIMPLEQ_FIRST(&pipe->queue)); 881 xfer->busy_free = XFER_BUSY; 882 #endif 883 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next); 884 } 885 USBHIST_LOG(usbdebug, "xfer %p: repeat %d new head = %p", 886 xfer, repeat, SIMPLEQ_FIRST(&pipe->queue), 0); 887 888 /* Count completed transfers. */ 889 ++pipe->device->bus->stats.uds_requests 890 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE]; 891 892 xfer->done = 1; 893 if (!xfer->status && xfer->actlen < xfer->length && 894 !(xfer->flags & USBD_SHORT_XFER_OK)) { 895 USBHIST_LOG(usbdebug, "short transfer %d < %d", 896 xfer->actlen, xfer->length, 0, 0); 897 xfer->status = USBD_SHORT_XFER; 898 } 899 900 if (repeat) { 901 USBHIST_LOG(usbdebug, "xfer %p doing callback %p status %x", 902 xfer, xfer->callback, xfer->status, 0); 903 if (xfer->callback) { 904 if (!polling) 905 mutex_exit(pipe->device->bus->lock); 906 907 if (!(pipe->flags & USBD_MPSAFE)) 908 KERNEL_LOCK(1, curlwp); 909 xfer->callback(xfer, xfer->priv, xfer->status); 910 USBHIST_LOG(usbdebug, "xfer %p doing done %p", xfer, 911 pipe->methods->done, 0, 0); 912 if (!(pipe->flags & USBD_MPSAFE)) 913 KERNEL_UNLOCK_ONE(curlwp); 914 915 if (!polling) 916 mutex_enter(pipe->device->bus->lock); 917 } 918 pipe->methods->done(xfer); 919 } else { 920 USBHIST_LOG(usbdebug, "xfer %p doing done %p", xfer, 921 pipe->methods->done, 0, 0); 922 pipe->methods->done(xfer); 923 USBHIST_LOG(usbdebug, "xfer %p doing callback %p status %x", 924 xfer, xfer->callback, xfer->status, 0); 925 if (xfer->callback) { 926 if (!polling) 927 mutex_exit(pipe->device->bus->lock); 928 929 if (!(pipe->flags & USBD_MPSAFE)) 930 KERNEL_LOCK(1, curlwp); 931 xfer->callback(xfer, xfer->priv, xfer->status); 932 if (!(pipe->flags & USBD_MPSAFE)) 933 KERNEL_UNLOCK_ONE(curlwp); 934 935 if (!polling) 936 mutex_enter(pipe->device->bus->lock); 937 } 938 } 939 940 if (sync && !polling) { 941 USBHIST_LOG(usbdebug, "<- done xfer %p, wakeup", xfer, 0, 0, 0); 942 cv_broadcast(&xfer->cv); 943 } 944 945 if (!repeat) { 946 /* XXX should we stop the queue on all errors? */ 947 if (erred && pipe->iface != NULL) /* not control pipe */ 948 pipe->running = 0; 949 else 950 usbd_start_next(pipe); 951 } 952 } 953 954 /* Called with USB lock held. */ 955 usbd_status 956 usb_insert_transfer(usbd_xfer_handle xfer) 957 { 958 usbd_pipe_handle pipe = xfer->pipe; 959 usbd_status err; 960 961 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 962 963 USBHIST_LOG(usbdebug, "pipe = %p running = %d timeout = %d", 964 pipe, pipe->running, xfer->timeout, 0); 965 966 KASSERT(mutex_owned(pipe->device->bus->lock)); 967 968 #ifdef DIAGNOSTIC 969 if (xfer->busy_free != XFER_BUSY) { 970 USBHIST_LOG(usbdebug, "<- done, xfer %p not busy", xfer, 0, 0, 971 0); 972 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n", 973 xfer, xfer->busy_free); 974 return (USBD_INVAL); 975 } 976 xfer->busy_free = XFER_ONQU; 977 #endif 978 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next); 979 if (pipe->running) 980 err = USBD_IN_PROGRESS; 981 else { 982 pipe->running = 1; 983 err = USBD_NORMAL_COMPLETION; 984 } 985 USBHIST_LOG(usbdebug, "<- done xfer %p, err %d", xfer, err, 0, 0); 986 return (err); 987 } 988 989 /* Called with USB lock held. */ 990 void 991 usbd_start_next(usbd_pipe_handle pipe) 992 { 993 usbd_xfer_handle xfer; 994 usbd_status err; 995 996 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 997 998 #ifdef DIAGNOSTIC 999 if (pipe == NULL) { 1000 printf("usbd_start_next: pipe == NULL\n"); 1001 return; 1002 } 1003 if (pipe->methods == NULL || pipe->methods->start == NULL) { 1004 printf("usbd_start_next: pipe=%p no start method\n", pipe); 1005 return; 1006 } 1007 #endif 1008 1009 int polling = pipe->device->bus->use_polling; 1010 KASSERT(polling || mutex_owned(pipe->device->bus->lock)); 1011 1012 /* Get next request in queue. */ 1013 xfer = SIMPLEQ_FIRST(&pipe->queue); 1014 USBHIST_LOG(usbdebug, "pipe = %p, xfer = %p", pipe, xfer, 0, 0); 1015 if (xfer == NULL) { 1016 pipe->running = 0; 1017 } else { 1018 if (!polling) 1019 mutex_exit(pipe->device->bus->lock); 1020 err = pipe->methods->start(xfer); 1021 if (!polling) 1022 mutex_enter(pipe->device->bus->lock); 1023 1024 if (err != USBD_IN_PROGRESS) { 1025 USBHIST_LOG(usbdebug, "error = %d", err, 0, 0, 0); 1026 pipe->running = 0; 1027 /* XXX do what? */ 1028 } 1029 } 1030 1031 KASSERT(polling || mutex_owned(pipe->device->bus->lock)); 1032 } 1033 1034 usbd_status 1035 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data) 1036 { 1037 return (usbd_do_request_flags(dev, req, data, 0, 0, 1038 USBD_DEFAULT_TIMEOUT)); 1039 } 1040 1041 usbd_status 1042 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req, 1043 void *data, u_int16_t flags, int *actlen, u_int32_t timo) 1044 { 1045 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req, 1046 data, flags, actlen, timo)); 1047 } 1048 1049 usbd_status 1050 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe, 1051 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen, 1052 u_int32_t timeout) 1053 { 1054 usbd_xfer_handle xfer; 1055 usbd_status err; 1056 1057 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1058 1059 #ifdef DIAGNOSTIC 1060 if (cpu_intr_p() || cpu_softintr_p()) { 1061 USBHIST_LOG(usbdebug, "not in process context", 0, 0, 0, 0); 1062 return (USBD_INVAL); 1063 } 1064 #endif 1065 1066 xfer = usbd_alloc_xfer(dev); 1067 if (xfer == NULL) 1068 return (USBD_NOMEM); 1069 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, 1070 data, UGETW(req->wLength), flags, 0); 1071 xfer->pipe = pipe; 1072 err = usbd_sync_transfer(xfer); 1073 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 1074 if (xfer->actlen > xfer->length) { 1075 USBHIST_LOG(usbdebug, "overrun addr = %d type = 0x%02x", 1076 dev->address, xfer->request.bmRequestType, 0, 0); 1077 USBHIST_LOG(usbdebug, " req = 0x%02x val = %d index = %d", 1078 xfer->request.bRequest, UGETW(xfer->request.wValue), 1079 UGETW(xfer->request.wIndex), 0); 1080 USBHIST_LOG(usbdebug, " rlen = %d length = %d actlen = %d", 1081 UGETW(xfer->request.wLength), 1082 xfer->length, xfer->actlen, 0); 1083 } 1084 #endif 1085 if (actlen != NULL) 1086 *actlen = xfer->actlen; 1087 if (err == USBD_STALLED) { 1088 /* 1089 * The control endpoint has stalled. Control endpoints 1090 * should not halt, but some may do so anyway so clear 1091 * any halt condition. 1092 */ 1093 usb_device_request_t treq; 1094 usb_status_t status; 1095 u_int16_t s; 1096 usbd_status nerr; 1097 1098 treq.bmRequestType = UT_READ_ENDPOINT; 1099 treq.bRequest = UR_GET_STATUS; 1100 USETW(treq.wValue, 0); 1101 USETW(treq.wIndex, 0); 1102 USETW(treq.wLength, sizeof(usb_status_t)); 1103 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1104 &treq, &status,sizeof(usb_status_t), 1105 0, 0); 1106 nerr = usbd_sync_transfer(xfer); 1107 if (nerr) 1108 goto bad; 1109 s = UGETW(status.wStatus); 1110 USBHIST_LOG(usbdebug, "status = 0x%04x", s, 0, 0, 0); 1111 if (!(s & UES_HALT)) 1112 goto bad; 1113 treq.bmRequestType = UT_WRITE_ENDPOINT; 1114 treq.bRequest = UR_CLEAR_FEATURE; 1115 USETW(treq.wValue, UF_ENDPOINT_HALT); 1116 USETW(treq.wIndex, 0); 1117 USETW(treq.wLength, 0); 1118 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1119 &treq, &status, 0, 0, 0); 1120 nerr = usbd_sync_transfer(xfer); 1121 if (nerr) 1122 goto bad; 1123 } 1124 1125 bad: 1126 if (err) { 1127 USBHIST_LOG(usbdebug, "returning err = %d", err, 0, 0, 0); 1128 } 1129 usbd_free_xfer(xfer); 1130 return (err); 1131 } 1132 1133 const struct usbd_quirks * 1134 usbd_get_quirks(usbd_device_handle dev) 1135 { 1136 #ifdef DIAGNOSTIC 1137 if (dev == NULL) { 1138 printf("usbd_get_quirks: dev == NULL\n"); 1139 return 0; 1140 } 1141 #endif 1142 return (dev->quirks); 1143 } 1144 1145 /* XXX do periodic free() of free list */ 1146 1147 /* 1148 * Called from keyboard driver when in polling mode. 1149 */ 1150 void 1151 usbd_dopoll(usbd_interface_handle iface) 1152 { 1153 iface->device->bus->methods->do_poll(iface->device->bus); 1154 } 1155 1156 /* 1157 * XXX use this more??? use_polling it touched manually all over 1158 */ 1159 void 1160 usbd_set_polling(usbd_device_handle dev, int on) 1161 { 1162 if (on) 1163 dev->bus->use_polling++; 1164 else 1165 dev->bus->use_polling--; 1166 1167 /* Kick the host controller when switching modes */ 1168 mutex_enter(dev->bus->lock); 1169 (*dev->bus->methods->soft_intr)(dev->bus); 1170 mutex_exit(dev->bus->lock); 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 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1271 1272 buf[0] = '\0'; 1273 if (si == 0) 1274 return (USBD_INVAL); 1275 if (dev->quirks->uq_flags & UQ_NO_STRINGS) 1276 return (USBD_STALLED); 1277 if (dev->langid == USBD_NOLANG) { 1278 /* Set up default language */ 1279 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us, 1280 &size); 1281 if (err || size < 4) { 1282 USBHIST_LOG(usbdebug, "getting lang failed, using 0", 1283 0, 0, 0, 0); 1284 dev->langid = 0; /* Well, just pick something then */ 1285 } else { 1286 /* Pick the first language as the default. */ 1287 dev->langid = UGETW(us.bString[0]); 1288 } 1289 } 1290 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 1291 if (err) 1292 return (err); 1293 s = buf; 1294 n = size / 2 - 1; 1295 if (unicode) { 1296 for (i = 0; i < n; i++) { 1297 c = UGETW(us.bString[i]); 1298 if (swap) 1299 c = (c >> 8) | (c << 8); 1300 s += wput_utf8(s, 3, c); 1301 } 1302 *s++ = 0; 1303 } 1304 #ifdef COMPAT_30 1305 else { 1306 for (i = 0; i < n; i++) { 1307 c = UGETW(us.bString[i]); 1308 if (swap) 1309 c = (c >> 8) | (c << 8); 1310 *s++ = (c < 0x80) ? c : '?'; 1311 } 1312 *s++ = 0; 1313 } 1314 #endif 1315 return (USBD_NORMAL_COMPLETION); 1316 } 1317