1 /* $NetBSD: usbdi.c,v 1.162 2014/09/12 16:40:38 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.162 2014/09/12 16:40:38 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 static inline int 69 usbd_xfer_isread(usbd_xfer_handle xfer) 70 { 71 if (xfer->rqflags & URQ_REQUEST) 72 return (xfer->request.bmRequestType & UT_READ); 73 else 74 return (xfer->pipe->endpoint->edesc->bEndpointAddress & 75 UE_DIR_IN); 76 } 77 78 #if defined(USB_DEBUG) 79 void 80 usbd_dump_iface(struct usbd_interface *iface) 81 { 82 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 83 84 USBHIST_LOG(usbdebug, "iface %p\n", iface, 0, 0, 0); 85 if (iface == NULL) 86 return; 87 USBHIST_LOG(usbdebug, " device = %p idesc = %p index = %d", 88 iface->device, iface->idesc, iface->index, 0); 89 USBHIST_LOG(usbdebug, " altindex=%d priv=%p", 90 iface->altindex, iface->priv, 0, 0); 91 } 92 93 void 94 usbd_dump_device(struct usbd_device *dev) 95 { 96 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 97 98 USBHIST_LOG(usbdebug, "dev = %p", dev, 0, 0, 0); 99 if (dev == NULL) 100 return; 101 USBHIST_LOG(usbdebug, " bus = %p default_pipe = %p", 102 dev->bus, dev->default_pipe, 0, 0); 103 USBHIST_LOG(usbdebug, " address = %d config = %d depth = %d ", 104 dev->address, dev->config, dev->depth, 0); 105 USBHIST_LOG(usbdebug, " speed = %d self_powered = %d " 106 "power = %d langid = %d", 107 dev->speed, dev->self_powered, dev->power, dev->langid); 108 } 109 110 void 111 usbd_dump_endpoint(struct usbd_endpoint *endp) 112 { 113 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 114 115 USBHIST_LOG(usbdebug, "endp = %p", endp, 0, 0, 0); 116 if (endp == NULL) 117 return; 118 USBHIST_LOG(usbdebug, " edesc = %p refcnt = %d", 119 endp->edesc, endp->refcnt, 0, 0); 120 if (endp->edesc) 121 USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02x", 122 endp->edesc->bEndpointAddress, 0, 0, 0); 123 } 124 125 void 126 usbd_dump_queue(usbd_pipe_handle pipe) 127 { 128 usbd_xfer_handle xfer; 129 130 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 131 132 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0); 133 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) { 134 USBHIST_LOG(usbdebug, " xfer = %p", xfer, 0, 0, 0); 135 } 136 } 137 138 void 139 usbd_dump_pipe(usbd_pipe_handle pipe) 140 { 141 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 142 143 USBHIST_LOG(usbdebug, "pipe = %p", pipe, 0, 0, 0); 144 if (pipe == NULL) 145 return; 146 usbd_dump_iface(pipe->iface); 147 usbd_dump_device(pipe->device); 148 usbd_dump_endpoint(pipe->endpoint); 149 USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0); 150 USBHIST_LOG(usbdebug, " refcnt = %d running = %d aborting = %d", 151 pipe->refcnt, pipe->running, pipe->aborting, 0); 152 USBHIST_LOG(usbdebug, " intrxfer = %p, repeat = %d, interval = %d", 153 pipe->intrxfer, pipe->repeat, pipe->interval, 0); 154 } 155 #endif 156 157 usbd_status 158 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address, 159 u_int8_t flags, usbd_pipe_handle *pipe) 160 { 161 return (usbd_open_pipe_ival(iface, address, flags, pipe, 162 USBD_DEFAULT_INTERVAL)); 163 } 164 165 usbd_status 166 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address, 167 u_int8_t flags, usbd_pipe_handle *pipe, int ival) 168 { 169 usbd_pipe_handle p; 170 struct usbd_endpoint *ep; 171 usbd_status err; 172 int i; 173 174 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 175 176 USBHIST_LOG(usbdebug, "iface = %p address = 0x%x flags = 0x%x", 177 iface, address, flags, 0); 178 179 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 180 ep = &iface->endpoints[i]; 181 if (ep->edesc == NULL) 182 return (USBD_IOERROR); 183 if (ep->edesc->bEndpointAddress == address) 184 goto found; 185 } 186 return (USBD_BAD_ADDRESS); 187 found: 188 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0) 189 return (USBD_IN_USE); 190 err = usbd_setup_pipe_flags(iface->device, iface, ep, ival, &p, flags); 191 if (err) 192 return (err); 193 LIST_INSERT_HEAD(&iface->pipes, p, next); 194 *pipe = p; 195 return (USBD_NORMAL_COMPLETION); 196 } 197 198 usbd_status 199 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address, 200 u_int8_t flags, usbd_pipe_handle *pipe, 201 usbd_private_handle priv, void *buffer, u_int32_t len, 202 usbd_callback cb, int ival) 203 { 204 usbd_status err; 205 usbd_xfer_handle xfer; 206 usbd_pipe_handle ipipe; 207 208 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 209 210 USBHIST_LOG(usbdebug, "address = 0x%x flags = 0x%x len = %d", 211 address, flags, len, 0); 212 213 err = usbd_open_pipe_ival(iface, address, 214 USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE), 215 &ipipe, ival); 216 if (err) 217 return (err); 218 xfer = usbd_alloc_xfer(iface->device); 219 if (xfer == NULL) { 220 err = USBD_NOMEM; 221 goto bad1; 222 } 223 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags, 224 USBD_NO_TIMEOUT, cb); 225 ipipe->intrxfer = xfer; 226 ipipe->repeat = 1; 227 err = usbd_transfer(xfer); 228 *pipe = ipipe; 229 if (err != USBD_IN_PROGRESS) 230 goto bad2; 231 return (USBD_NORMAL_COMPLETION); 232 233 bad2: 234 ipipe->intrxfer = NULL; 235 ipipe->repeat = 0; 236 usbd_free_xfer(xfer); 237 bad1: 238 usbd_close_pipe(ipipe); 239 return (err); 240 } 241 242 usbd_status 243 usbd_close_pipe(usbd_pipe_handle pipe) 244 { 245 246 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 247 248 #ifdef DIAGNOSTIC 249 if (pipe == NULL) { 250 USBHIST_LOG(usbdebug, "pipe == NULL", 0, 0, 0, 0); 251 return (USBD_NORMAL_COMPLETION); 252 } 253 #endif 254 255 usbd_lock_pipe(pipe); 256 if (--pipe->refcnt != 0) { 257 usbd_unlock_pipe(pipe); 258 return (USBD_NORMAL_COMPLETION); 259 } 260 if (! SIMPLEQ_EMPTY(&pipe->queue)) { 261 usbd_unlock_pipe(pipe); 262 return (USBD_PENDING_REQUESTS); 263 } 264 LIST_REMOVE(pipe, next); 265 pipe->endpoint->refcnt--; 266 pipe->methods->close(pipe); 267 usbd_unlock_pipe(pipe); 268 if (pipe->intrxfer != NULL) 269 usbd_free_xfer(pipe->intrxfer); 270 free(pipe, M_USB); 271 return (USBD_NORMAL_COMPLETION); 272 } 273 274 usbd_status 275 usbd_transfer(usbd_xfer_handle xfer) 276 { 277 usbd_pipe_handle pipe = xfer->pipe; 278 usb_dma_t *dmap = &xfer->dmabuf; 279 usbd_status err; 280 unsigned int size, flags; 281 282 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 283 284 USBHIST_LOG(usbdebug, 285 "xfer = %p, flags = %#x, pipe = %p, running = %d", 286 xfer, xfer->flags, pipe, pipe->running); 287 288 #ifdef USB_DEBUG 289 if (usbdebug > 5) 290 usbd_dump_queue(pipe); 291 #endif 292 xfer->done = 0; 293 294 if (pipe->aborting) { 295 USBHIST_LOG(usbdebug, "<- done xfer %p, aborting", xfer, 0, 0, 296 0); 297 return (USBD_CANCELLED); 298 } 299 300 size = xfer->length; 301 /* If there is no buffer, allocate one. */ 302 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) { 303 struct usbd_bus *bus = pipe->device->bus; 304 305 #ifdef DIAGNOSTIC 306 if (xfer->rqflags & URQ_AUTO_DMABUF) 307 printf("usbd_transfer: has old buffer!\n"); 308 #endif 309 err = bus->methods->allocm(bus, dmap, size); 310 if (err) { 311 USBHIST_LOG(usbdebug, 312 "<- done xfer %p, no mem", xfer, 0, 0, 0); 313 return (err); 314 } 315 xfer->rqflags |= URQ_AUTO_DMABUF; 316 } 317 318 flags = xfer->flags; 319 320 /* Copy data if going out. */ 321 if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer)) 322 memcpy(KERNADDR(dmap, 0), xfer->buffer, size); 323 324 /* xfer is not valid after the transfer method unless synchronous */ 325 err = pipe->methods->transfer(xfer); 326 USBHIST_LOG(usbdebug, "<- done transfer %p, err = %d", xfer, err, 0, 0); 327 328 if (err != USBD_IN_PROGRESS && err) { 329 /* The transfer has not been queued, so free buffer. */ 330 if (xfer->rqflags & URQ_AUTO_DMABUF) { 331 struct usbd_bus *bus = pipe->device->bus; 332 333 bus->methods->freem(bus, &xfer->dmabuf); 334 xfer->rqflags &= ~URQ_AUTO_DMABUF; 335 } 336 } 337 338 if (!(flags & USBD_SYNCHRONOUS)) { 339 USBHIST_LOG(usbdebug, "<- done xfer %p, not sync", xfer, 0, 0, 340 0); 341 return (err); 342 } 343 344 /* Sync transfer, wait for completion. */ 345 if (err != USBD_IN_PROGRESS) { 346 USBHIST_LOG(usbdebug, "<- done xfer %p, not in progress", xfer, 347 0, 0, 0); 348 return (err); 349 } 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 #ifdef DIAGNOSTIC 853 if (xfer->actlen > xfer->length) { 854 printf("%s: actlen (%d) > len (%d)\n", __func__, 855 xfer->actlen, xfer->length); 856 xfer->actlen = xfer->length; 857 } 858 #endif 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 KASSERT(mutex_owned(pipe->device->bus->lock)); 1010 1011 /* Get next request in queue. */ 1012 xfer = SIMPLEQ_FIRST(&pipe->queue); 1013 USBHIST_LOG(usbdebug, "pipe = %p, xfer = %p", pipe, xfer, 0, 0); 1014 if (xfer == NULL) { 1015 pipe->running = 0; 1016 } else { 1017 mutex_exit(pipe->device->bus->lock); 1018 err = pipe->methods->start(xfer); 1019 mutex_enter(pipe->device->bus->lock); 1020 1021 if (err != USBD_IN_PROGRESS) { 1022 USBHIST_LOG(usbdebug, "error = %d", err, 0, 0, 0); 1023 pipe->running = 0; 1024 /* XXX do what? */ 1025 } 1026 } 1027 1028 KASSERT(mutex_owned(pipe->device->bus->lock)); 1029 } 1030 1031 usbd_status 1032 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data) 1033 { 1034 return (usbd_do_request_flags(dev, req, data, 0, 0, 1035 USBD_DEFAULT_TIMEOUT)); 1036 } 1037 1038 usbd_status 1039 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req, 1040 void *data, u_int16_t flags, int *actlen, u_int32_t timo) 1041 { 1042 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req, 1043 data, flags, actlen, timo)); 1044 } 1045 1046 usbd_status 1047 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe, 1048 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen, 1049 u_int32_t timeout) 1050 { 1051 usbd_xfer_handle xfer; 1052 usbd_status err; 1053 1054 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1055 1056 #ifdef DIAGNOSTIC 1057 if (cpu_intr_p() || cpu_softintr_p()) { 1058 USBHIST_LOG(usbdebug, "not in process context", 0, 0, 0, 0); 1059 return (USBD_INVAL); 1060 } 1061 #endif 1062 1063 xfer = usbd_alloc_xfer(dev); 1064 if (xfer == NULL) 1065 return (USBD_NOMEM); 1066 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, 1067 data, UGETW(req->wLength), flags, 0); 1068 xfer->pipe = pipe; 1069 err = usbd_sync_transfer(xfer); 1070 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 1071 if (xfer->actlen > xfer->length) { 1072 USBHIST_LOG(usbdebug, "overrun addr = %d type = 0x%02x", 1073 dev->address, xfer->request.bmRequestType, 0, 0); 1074 USBHIST_LOG(usbdebug, " req = 0x%02x val = %d index = %d", 1075 xfer->request.bRequest, UGETW(xfer->request.wValue), 1076 UGETW(xfer->request.wIndex), 0); 1077 USBHIST_LOG(usbdebug, " rlen = %d length = %d actlen = %d", 1078 UGETW(xfer->request.wLength), 1079 xfer->length, xfer->actlen, 0); 1080 } 1081 #endif 1082 if (actlen != NULL) 1083 *actlen = xfer->actlen; 1084 if (err == USBD_STALLED) { 1085 /* 1086 * The control endpoint has stalled. Control endpoints 1087 * should not halt, but some may do so anyway so clear 1088 * any halt condition. 1089 */ 1090 usb_device_request_t treq; 1091 usb_status_t status; 1092 u_int16_t s; 1093 usbd_status nerr; 1094 1095 treq.bmRequestType = UT_READ_ENDPOINT; 1096 treq.bRequest = UR_GET_STATUS; 1097 USETW(treq.wValue, 0); 1098 USETW(treq.wIndex, 0); 1099 USETW(treq.wLength, sizeof(usb_status_t)); 1100 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1101 &treq, &status,sizeof(usb_status_t), 1102 0, 0); 1103 nerr = usbd_sync_transfer(xfer); 1104 if (nerr) 1105 goto bad; 1106 s = UGETW(status.wStatus); 1107 USBHIST_LOG(usbdebug, "status = 0x%04x", s, 0, 0, 0); 1108 if (!(s & UES_HALT)) 1109 goto bad; 1110 treq.bmRequestType = UT_WRITE_ENDPOINT; 1111 treq.bRequest = UR_CLEAR_FEATURE; 1112 USETW(treq.wValue, UF_ENDPOINT_HALT); 1113 USETW(treq.wIndex, 0); 1114 USETW(treq.wLength, 0); 1115 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 1116 &treq, &status, 0, 0, 0); 1117 nerr = usbd_sync_transfer(xfer); 1118 if (nerr) 1119 goto bad; 1120 } 1121 1122 bad: 1123 if (err) { 1124 USBHIST_LOG(usbdebug, "returning err = %s", 1125 usbd_errstr(err), 0, 0, 0); 1126 } 1127 usbd_free_xfer(xfer); 1128 return (err); 1129 } 1130 1131 const struct usbd_quirks * 1132 usbd_get_quirks(usbd_device_handle dev) 1133 { 1134 #ifdef DIAGNOSTIC 1135 if (dev == NULL) { 1136 printf("usbd_get_quirks: dev == NULL\n"); 1137 return 0; 1138 } 1139 #endif 1140 return (dev->quirks); 1141 } 1142 1143 /* XXX do periodic free() of free list */ 1144 1145 /* 1146 * Called from keyboard driver when in polling mode. 1147 */ 1148 void 1149 usbd_dopoll(usbd_interface_handle iface) 1150 { 1151 iface->device->bus->methods->do_poll(iface->device->bus); 1152 } 1153 1154 /* 1155 * XXX use this more??? use_polling it touched manually all over 1156 */ 1157 void 1158 usbd_set_polling(usbd_device_handle dev, int on) 1159 { 1160 if (on) 1161 dev->bus->use_polling++; 1162 else 1163 dev->bus->use_polling--; 1164 1165 /* Kick the host controller when switching modes */ 1166 mutex_enter(dev->bus->lock); 1167 (*dev->bus->methods->soft_intr)(dev->bus); 1168 mutex_exit(dev->bus->lock); 1169 } 1170 1171 1172 usb_endpoint_descriptor_t * 1173 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address) 1174 { 1175 struct usbd_endpoint *ep; 1176 int i; 1177 1178 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 1179 ep = &iface->endpoints[i]; 1180 if (ep->edesc->bEndpointAddress == address) 1181 return (iface->endpoints[i].edesc); 1182 } 1183 return (NULL); 1184 } 1185 1186 /* 1187 * usbd_ratecheck() can limit the number of error messages that occurs. 1188 * When a device is unplugged it may take up to 0.25s for the hub driver 1189 * to notice it. If the driver continuosly tries to do I/O operations 1190 * this can generate a large number of messages. 1191 */ 1192 int 1193 usbd_ratecheck(struct timeval *last) 1194 { 1195 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/ 1196 1197 return (ratecheck(last, &errinterval)); 1198 } 1199 1200 /* 1201 * Search for a vendor/product pair in an array. The item size is 1202 * given as an argument. 1203 */ 1204 const struct usb_devno * 1205 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz, 1206 u_int16_t vendor, u_int16_t product) 1207 { 1208 while (nentries-- > 0) { 1209 u_int16_t tproduct = tbl->ud_product; 1210 if (tbl->ud_vendor == vendor && 1211 (tproduct == product || tproduct == USB_PRODUCT_ANY)) 1212 return (tbl); 1213 tbl = (const struct usb_devno *)((const char *)tbl + sz); 1214 } 1215 return (NULL); 1216 } 1217 1218 1219 void 1220 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter) 1221 { 1222 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 1223 1224 iter->cur = (const uByte *)cd; 1225 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength); 1226 } 1227 1228 const usb_descriptor_t * 1229 usb_desc_iter_next(usbd_desc_iter_t *iter) 1230 { 1231 const usb_descriptor_t *desc; 1232 1233 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) { 1234 if (iter->cur != iter->end) 1235 printf("usb_desc_iter_next: bad descriptor\n"); 1236 return NULL; 1237 } 1238 desc = (const usb_descriptor_t *)iter->cur; 1239 if (desc->bLength == 0) { 1240 printf("usb_desc_iter_next: descriptor length = 0\n"); 1241 return NULL; 1242 } 1243 iter->cur += desc->bLength; 1244 if (iter->cur > iter->end) { 1245 printf("usb_desc_iter_next: descriptor length too large\n"); 1246 return NULL; 1247 } 1248 return desc; 1249 } 1250 1251 usbd_status 1252 usbd_get_string(usbd_device_handle dev, int si, char *buf) 1253 { 1254 return usbd_get_string0(dev, si, buf, 1); 1255 } 1256 1257 usbd_status 1258 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode) 1259 { 1260 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE; 1261 usb_string_descriptor_t us; 1262 char *s; 1263 int i, n; 1264 u_int16_t c; 1265 usbd_status err; 1266 int size; 1267 1268 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 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 USBHIST_LOG(usbdebug, "getting lang failed, using 0", 1281 0, 0, 0, 0); 1282 dev->langid = 0; /* Well, just pick something then */ 1283 } else { 1284 /* Pick the first language as the default. */ 1285 dev->langid = UGETW(us.bString[0]); 1286 } 1287 } 1288 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 1289 if (err) 1290 return (err); 1291 s = buf; 1292 n = size / 2 - 1; 1293 if (unicode) { 1294 for (i = 0; i < n; i++) { 1295 c = UGETW(us.bString[i]); 1296 if (swap) 1297 c = (c >> 8) | (c << 8); 1298 s += wput_utf8(s, 3, c); 1299 } 1300 *s++ = 0; 1301 } 1302 #ifdef COMPAT_30 1303 else { 1304 for (i = 0; i < n; i++) { 1305 c = UGETW(us.bString[i]); 1306 if (swap) 1307 c = (c >> 8) | (c << 8); 1308 *s++ = (c < 0x80) ? c : '?'; 1309 } 1310 *s++ = 0; 1311 } 1312 #endif 1313 return (USBD_NORMAL_COMPLETION); 1314 } 1315