1 /* $OpenBSD: usb_subr.c,v 1.158 2022/02/16 06:23:42 anton Exp $ */ 2 /* $NetBSD: usb_subr.c,v 1.103 2003/01/10 11:19:13 augustss Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/device.h> 40 #include <sys/selinfo.h> 41 #include <sys/rwlock.h> 42 43 #include <machine/bus.h> 44 45 #include <dev/usb/usb.h> 46 47 #include <dev/usb/usbdi.h> 48 #include <dev/usb/usbdi_util.h> 49 #include <dev/usb/usbdivar.h> 50 #include <dev/usb/usbdevs.h> 51 #include <dev/usb/usb_quirks.h> 52 53 #ifdef USB_DEBUG 54 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0) 55 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0) 56 extern int usbdebug; 57 #else 58 #define DPRINTF(x) 59 #define DPRINTFN(n,x) 60 #endif 61 62 usbd_status usbd_set_config(struct usbd_device *, int); 63 void usbd_devinfo(struct usbd_device *, int, char *, size_t); 64 char *usbd_get_string(struct usbd_device *, int, char *, size_t); 65 int usbd_getnewaddr(struct usbd_bus *); 66 int usbd_print(void *, const char *); 67 void usbd_free_iface_data(struct usbd_device *, int); 68 int usbd_cache_devinfo(struct usbd_device *); 69 usbd_status usbd_probe_and_attach(struct device *, 70 struct usbd_device *, int, int); 71 72 int usbd_printBCD(char *cp, size_t len, int bcd); 73 void usb_free_device(struct usbd_device *); 74 int usbd_parse_idesc(struct usbd_device *, struct usbd_interface *); 75 76 #ifdef USBVERBOSE 77 #include <dev/usb/usbdevs_data.h> 78 #endif /* USBVERBOSE */ 79 80 const char * const usbd_error_strs[] = { 81 "NORMAL_COMPLETION", 82 "IN_PROGRESS", 83 "PENDING_REQUESTS", 84 "NOT_STARTED", 85 "INVAL", 86 "NOMEM", 87 "CANCELLED", 88 "BAD_ADDRESS", 89 "IN_USE", 90 "NO_ADDR", 91 "SET_ADDR_FAILED", 92 "NO_POWER", 93 "TOO_DEEP", 94 "IOERROR", 95 "NOT_CONFIGURED", 96 "TIMEOUT", 97 "SHORT_XFER", 98 "STALLED", 99 "INTERRUPTED", 100 "XXX", 101 }; 102 103 const char * 104 usbd_errstr(usbd_status err) 105 { 106 static char buffer[5]; 107 108 if (err < USBD_ERROR_MAX) 109 return (usbd_error_strs[err]); 110 else { 111 snprintf(buffer, sizeof(buffer), "%d", err); 112 return (buffer); 113 } 114 } 115 116 usbd_status 117 usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid, 118 usb_string_descriptor_t *sdesc, int *sizep) 119 { 120 usb_device_request_t req; 121 usbd_status err; 122 int actlen; 123 124 req.bmRequestType = UT_READ_DEVICE; 125 req.bRequest = UR_GET_DESCRIPTOR; 126 USETW2(req.wValue, UDESC_STRING, sindex); 127 USETW(req.wIndex, langid); 128 USETW(req.wLength, 2); /* size and descriptor type first */ 129 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 130 &actlen, USBD_DEFAULT_TIMEOUT); 131 if (err) 132 return (err); 133 134 if (actlen < 2) 135 return (USBD_SHORT_XFER); 136 137 USETW(req.wLength, sdesc->bLength); /* the whole string */ 138 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 139 &actlen, USBD_DEFAULT_TIMEOUT); 140 if (err) 141 return (err); 142 143 if (actlen != sdesc->bLength) { 144 DPRINTFN(-1, ("%s: expected %d, got %d\n", __func__, 145 sdesc->bLength, actlen)); 146 } 147 148 *sizep = actlen; 149 return (USBD_NORMAL_COMPLETION); 150 } 151 152 char * 153 usbd_get_string(struct usbd_device *dev, int si, char *buf, size_t buflen) 154 { 155 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE; 156 usb_string_descriptor_t us; 157 char *s; 158 int i, n; 159 u_int16_t c; 160 usbd_status err; 161 int size; 162 163 if (si == 0) 164 return (0); 165 if (dev->quirks->uq_flags & UQ_NO_STRINGS) 166 return (0); 167 if (dev->langid == USBD_NOLANG) { 168 /* Set up default language */ 169 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us, 170 &size); 171 if (err || size < 4) 172 dev->langid = 0; /* Well, just pick English then */ 173 else { 174 /* Pick the first language as the default. */ 175 dev->langid = UGETW(us.bString[0]); 176 } 177 } 178 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 179 if (err) 180 return (0); 181 s = buf; 182 n = size / 2 - 1; 183 for (i = 0; i < n && i < buflen ; i++) { 184 c = UGETW(us.bString[i]); 185 /* Convert from Unicode, handle buggy strings. */ 186 if ((c & 0xff00) == 0) 187 *s++ = c; 188 else if ((c & 0x00ff) == 0 && swap) 189 *s++ = c >> 8; 190 else 191 *s++ = '?'; 192 } 193 if (buflen > 0) 194 *s++ = 0; 195 return (buf); 196 } 197 198 static void 199 usbd_trim_spaces(char *p) 200 { 201 char *q, *e; 202 203 if (p == NULL) 204 return; 205 q = e = p; 206 while (*q == ' ') /* skip leading spaces */ 207 q++; 208 while ((*p = *q++)) /* copy string */ 209 if (*p++ != ' ') /* remember last non-space */ 210 e = p; 211 *e = 0; /* kill trailing spaces */ 212 } 213 214 int 215 usbd_cache_devinfo(struct usbd_device *dev) 216 { 217 usb_device_descriptor_t *udd = &dev->ddesc; 218 219 dev->serial = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT); 220 if (dev->serial == NULL) 221 return (ENOMEM); 222 223 if (usbd_get_string(dev, udd->iSerialNumber, dev->serial, USB_MAX_STRING_LEN) != NULL) { 224 usbd_trim_spaces(dev->serial); 225 } else { 226 free(dev->serial, M_USB, USB_MAX_STRING_LEN); 227 dev->serial = NULL; 228 } 229 230 dev->vendor = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT); 231 if (dev->vendor == NULL) 232 return (ENOMEM); 233 234 if (usbd_get_string(dev, udd->iManufacturer, dev->vendor, USB_MAX_STRING_LEN) != NULL) { 235 usbd_trim_spaces(dev->vendor); 236 } else { 237 #ifdef USBVERBOSE 238 const struct usb_known_vendor *ukv; 239 240 for (ukv = usb_known_vendors; ukv->vendorname != NULL; ukv++) { 241 if (ukv->vendor == UGETW(udd->idVendor)) { 242 strlcpy(dev->vendor, ukv->vendorname, 243 USB_MAX_STRING_LEN); 244 break; 245 } 246 } 247 if (ukv->vendorname == NULL) 248 #endif 249 snprintf(dev->vendor, USB_MAX_STRING_LEN, "vendor 0x%04x", 250 UGETW(udd->idVendor)); 251 } 252 253 dev->product = malloc(USB_MAX_STRING_LEN, M_USB, M_NOWAIT); 254 if (dev->product == NULL) 255 return (ENOMEM); 256 257 if (usbd_get_string(dev, udd->iProduct, dev->product, USB_MAX_STRING_LEN) != NULL) { 258 usbd_trim_spaces(dev->product); 259 } else { 260 #ifdef USBVERBOSE 261 const struct usb_known_product *ukp; 262 263 for (ukp = usb_known_products; ukp->productname != NULL; ukp++) { 264 if (ukp->vendor == UGETW(udd->idVendor) && 265 (ukp->product == UGETW(udd->idProduct))) { 266 strlcpy(dev->product, ukp->productname, 267 USB_MAX_STRING_LEN); 268 break; 269 } 270 } 271 if (ukp->productname == NULL) 272 #endif 273 snprintf(dev->product, USB_MAX_STRING_LEN, "product 0x%04x", 274 UGETW(udd->idProduct)); 275 } 276 277 return (0); 278 } 279 280 int 281 usbd_printBCD(char *cp, size_t len, int bcd) 282 { 283 int l; 284 285 l = snprintf(cp, len, "%x.%02x", bcd >> 8, bcd & 0xff); 286 if (l == -1 || len == 0) 287 return (0); 288 if (l >= len) 289 return len - 1; 290 return (l); 291 } 292 293 void 294 usbd_devinfo(struct usbd_device *dev, int showclass, char *base, size_t len) 295 { 296 usb_device_descriptor_t *udd = &dev->ddesc; 297 char *cp = base; 298 int bcdDevice, bcdUSB; 299 300 snprintf(cp, len, "\"%s %s\"", dev->vendor, dev->product); 301 cp += strlen(cp); 302 if (showclass) { 303 snprintf(cp, base + len - cp, ", class %d/%d", 304 udd->bDeviceClass, udd->bDeviceSubClass); 305 cp += strlen(cp); 306 } 307 bcdUSB = UGETW(udd->bcdUSB); 308 bcdDevice = UGETW(udd->bcdDevice); 309 snprintf(cp, base + len - cp, " rev "); 310 cp += strlen(cp); 311 usbd_printBCD(cp, base + len - cp, bcdUSB); 312 cp += strlen(cp); 313 snprintf(cp, base + len - cp, "/"); 314 cp += strlen(cp); 315 usbd_printBCD(cp, base + len - cp, bcdDevice); 316 cp += strlen(cp); 317 snprintf(cp, base + len - cp, " addr %d", dev->address); 318 } 319 320 /* Delay for a certain number of ms */ 321 void 322 usb_delay_ms(struct usbd_bus *bus, u_int ms) 323 { 324 static int usb_delay_wchan; 325 326 if (bus->use_polling || cold) 327 delay((ms+1) * 1000); 328 else 329 tsleep_nsec(&usb_delay_wchan, PRIBIO, "usbdly", 330 MSEC_TO_NSEC(ms)); 331 } 332 333 /* Delay given a device handle. */ 334 void 335 usbd_delay_ms(struct usbd_device *dev, u_int ms) 336 { 337 if (usbd_is_dying(dev)) 338 return; 339 340 usb_delay_ms(dev->bus, ms); 341 } 342 343 usbd_status 344 usbd_port_disown_to_1_1(struct usbd_device *dev, int port) 345 { 346 usb_port_status_t ps; 347 usbd_status err; 348 int n; 349 350 err = usbd_set_port_feature(dev, port, UHF_PORT_DISOWN_TO_1_1); 351 DPRINTF(("%s: port %d disown request done, error=%s\n", __func__, 352 port, usbd_errstr(err))); 353 if (err) 354 return (err); 355 n = 10; 356 do { 357 /* Wait for device to recover from reset. */ 358 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 359 err = usbd_get_port_status(dev, port, &ps); 360 if (err) { 361 DPRINTF(("%s: get status failed %d\n", __func__, err)); 362 return (err); 363 } 364 /* If the device disappeared, just give up. */ 365 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 366 return (USBD_NORMAL_COMPLETION); 367 } while ((UGETW(ps.wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 368 if (n == 0) 369 return (USBD_TIMEOUT); 370 371 return (err); 372 } 373 374 int 375 usbd_reset_port(struct usbd_device *dev, int port) 376 { 377 usb_port_status_t ps; 378 int n; 379 380 if (usbd_set_port_feature(dev, port, UHF_PORT_RESET)) 381 return (EIO); 382 DPRINTF(("%s: port %d reset done\n", __func__, port)); 383 n = 10; 384 do { 385 /* Wait for device to recover from reset. */ 386 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 387 if (usbd_get_port_status(dev, port, &ps)) { 388 DPRINTF(("%s: get status failed\n", __func__)); 389 return (EIO); 390 } 391 /* If the device disappeared, just give up. */ 392 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 393 return (0); 394 } while ((UGETW(ps.wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 395 396 /* Clear port reset even if a timeout occurred. */ 397 if (usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET)) { 398 DPRINTF(("%s: clear port feature failed\n", __func__)); 399 return (EIO); 400 } 401 402 if (n == 0) 403 return (ETIMEDOUT); 404 405 /* Wait for the device to recover from reset. */ 406 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY); 407 return (0); 408 } 409 410 usb_interface_descriptor_t * 411 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceno, int altno) 412 { 413 char *p = (char *)cd; 414 char *end = p + UGETW(cd->wTotalLength); 415 usb_interface_descriptor_t *d; 416 int curidx, lastidx, curaidx = 0; 417 418 for (curidx = lastidx = -1; p < end; ) { 419 d = (usb_interface_descriptor_t *)p; 420 DPRINTFN(4,("usbd_find_idesc: ifaceno=%d(%d) altno=%d(%d) " 421 "len=%d type=%d\n", 422 ifaceno, curidx, altno, curaidx, 423 d->bLength, d->bDescriptorType)); 424 if (d->bLength == 0) /* bad descriptor */ 425 break; 426 p += d->bLength; 427 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) { 428 if (d->bInterfaceNumber != lastidx) { 429 lastidx = d->bInterfaceNumber; 430 curidx++; 431 curaidx = 0; 432 } else 433 curaidx++; 434 if (ifaceno == curidx && altno == curaidx) 435 return (d); 436 } 437 } 438 return (NULL); 439 } 440 441 usb_endpoint_descriptor_t * 442 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceno, int altno, 443 int endptidx) 444 { 445 char *p = (char *)cd; 446 char *end = p + UGETW(cd->wTotalLength); 447 usb_interface_descriptor_t *d; 448 usb_endpoint_descriptor_t *e; 449 int curidx; 450 451 d = usbd_find_idesc(cd, ifaceno, altno); 452 if (d == NULL) 453 return (NULL); 454 if (endptidx >= d->bNumEndpoints) /* quick exit */ 455 return (NULL); 456 457 curidx = -1; 458 for (p = (char *)d + d->bLength; p < end; ) { 459 e = (usb_endpoint_descriptor_t *)p; 460 if (e->bLength == 0) /* bad descriptor */ 461 break; 462 p += e->bLength; 463 if (p <= end && e->bDescriptorType == UDESC_INTERFACE) 464 return (NULL); 465 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) { 466 curidx++; 467 if (curidx == endptidx) 468 return (e); 469 } 470 } 471 return (NULL); 472 } 473 474 usbd_status 475 usbd_fill_iface_data(struct usbd_device *dev, int ifaceno, int altno) 476 { 477 struct usbd_interface *ifc = &dev->ifaces[ifaceno]; 478 usb_interface_descriptor_t *idesc; 479 int nendpt; 480 481 DPRINTFN(4,("%s: ifaceno=%d altno=%d\n", __func__, ifaceno, altno)); 482 483 idesc = usbd_find_idesc(dev->cdesc, ifaceno, altno); 484 if (idesc == NULL) 485 return (USBD_INVAL); 486 487 nendpt = idesc->bNumEndpoints; 488 DPRINTFN(4,("%s: found idesc nendpt=%d\n", __func__, nendpt)); 489 490 ifc->device = dev; 491 ifc->idesc = idesc; 492 ifc->index = ifaceno; 493 ifc->altindex = altno; 494 ifc->endpoints = NULL; 495 ifc->priv = NULL; 496 LIST_INIT(&ifc->pipes); 497 ifc->nendpt = nendpt; 498 499 if (nendpt != 0) { 500 ifc->endpoints = mallocarray(nendpt, sizeof(*ifc->endpoints), 501 M_USB, M_NOWAIT | M_ZERO); 502 if (ifc->endpoints == NULL) 503 return (USBD_NOMEM); 504 } 505 506 if (usbd_parse_idesc(dev, ifc)) { 507 free(ifc->endpoints, M_USB, nendpt * sizeof(*ifc->endpoints)); 508 ifc->endpoints = NULL; 509 return (USBD_INVAL); 510 } 511 512 return (USBD_NORMAL_COMPLETION); 513 } 514 515 int 516 usbd_parse_idesc(struct usbd_device *dev, struct usbd_interface *ifc) 517 { 518 #define ed ((usb_endpoint_descriptor_t *)p) 519 char *p, *end; 520 int i; 521 522 p = (char *)ifc->idesc + ifc->idesc->bLength; 523 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength); 524 525 for (i = 0; i < ifc->idesc->bNumEndpoints; i++) { 526 for (; p < end; p += ed->bLength) { 527 if (p + ed->bLength <= end && ed->bLength != 0 && 528 ed->bDescriptorType == UDESC_ENDPOINT) 529 break; 530 531 if (ed->bLength == 0 || 532 ed->bDescriptorType == UDESC_INTERFACE) 533 return (-1); 534 } 535 536 if (p >= end) 537 return (-1); 538 539 if (dev->speed == USB_SPEED_HIGH) { 540 unsigned int mps; 541 542 /* Control and bulk endpoints have max packet limits. */ 543 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 544 case UE_CONTROL: 545 mps = USB_2_MAX_CTRL_PACKET; 546 goto check; 547 case UE_BULK: 548 mps = USB_2_MAX_BULK_PACKET; 549 check: 550 if (UGETW(ed->wMaxPacketSize) != mps) { 551 USETW(ed->wMaxPacketSize, mps); 552 DPRINTF(("%s: bad max packet size\n", 553 __func__)); 554 } 555 break; 556 default: 557 break; 558 } 559 } 560 561 ifc->endpoints[i].edesc = ed; 562 ifc->endpoints[i].refcnt = 0; 563 ifc->endpoints[i].savedtoggle = 0; 564 p += ed->bLength; 565 } 566 567 return (0); 568 #undef ed 569 } 570 571 void 572 usbd_free_iface_data(struct usbd_device *dev, int ifcno) 573 { 574 struct usbd_interface *ifc = &dev->ifaces[ifcno]; 575 576 free(ifc->endpoints, M_USB, ifc->nendpt * sizeof(*ifc->endpoints)); 577 ifc->endpoints = NULL; 578 } 579 580 usbd_status 581 usbd_set_config(struct usbd_device *dev, int conf) 582 { 583 usb_device_request_t req; 584 585 req.bmRequestType = UT_WRITE_DEVICE; 586 req.bRequest = UR_SET_CONFIG; 587 USETW(req.wValue, conf); 588 USETW(req.wIndex, 0); 589 USETW(req.wLength, 0); 590 return (usbd_do_request(dev, &req, 0)); 591 } 592 593 usbd_status 594 usbd_set_config_no(struct usbd_device *dev, int no, int msg) 595 { 596 int index; 597 usb_config_descriptor_t cd; 598 usbd_status err; 599 600 DPRINTFN(5,("%s: %d\n", __func__, no)); 601 /* Figure out what config index to use. */ 602 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) { 603 err = usbd_get_desc(dev, UDESC_CONFIG, index, 604 USB_CONFIG_DESCRIPTOR_SIZE, &cd); 605 if (err || cd.bDescriptorType != UDESC_CONFIG) 606 return (err); 607 if (cd.bConfigurationValue == no) 608 return (usbd_set_config_index(dev, index, msg)); 609 } 610 return (USBD_INVAL); 611 } 612 613 usbd_status 614 usbd_set_config_index(struct usbd_device *dev, int index, int msg) 615 { 616 usb_status_t ds; 617 usb_config_descriptor_t cd, *cdp; 618 usbd_status err; 619 int i, ifcidx, nifc, cdplen, selfpowered, power; 620 621 DPRINTFN(5,("%s: dev=%p index=%d\n", __func__, dev, index)); 622 623 /* XXX check that all interfaces are idle */ 624 if (dev->config != USB_UNCONFIG_NO) { 625 DPRINTF(("%s: free old config\n", __func__)); 626 /* Free all configuration data structures. */ 627 nifc = dev->cdesc->bNumInterfaces; 628 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 629 usbd_free_iface_data(dev, ifcidx); 630 free(dev->ifaces, M_USB, nifc * sizeof(*dev->ifaces)); 631 free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength)); 632 dev->ifaces = NULL; 633 dev->cdesc = NULL; 634 dev->config = USB_UNCONFIG_NO; 635 } 636 637 if (index == USB_UNCONFIG_INDEX) { 638 /* We are unconfiguring the device, so leave unallocated. */ 639 DPRINTF(("%s: set config 0\n", __func__)); 640 err = usbd_set_config(dev, USB_UNCONFIG_NO); 641 if (err) 642 DPRINTF(("%s: setting config=0 failed, error=%s\n", 643 __func__, usbd_errstr(err))); 644 return (err); 645 } 646 647 /* Get the short descriptor. */ 648 err = usbd_get_desc(dev, UDESC_CONFIG, index, 649 USB_CONFIG_DESCRIPTOR_SIZE, &cd); 650 if (err) 651 return (err); 652 if (cd.bDescriptorType != UDESC_CONFIG) 653 return (USBD_INVAL); 654 cdplen = UGETW(cd.wTotalLength); 655 cdp = malloc(cdplen, M_USB, M_NOWAIT); 656 if (cdp == NULL) 657 return (USBD_NOMEM); 658 /* Get the full descriptor. */ 659 for (i = 0; i < 3; i++) { 660 err = usbd_get_desc(dev, UDESC_CONFIG, index, cdplen, cdp); 661 if (!err) 662 break; 663 usbd_delay_ms(dev, 200); 664 } 665 if (err) 666 goto bad; 667 668 if (cdp->bDescriptorType != UDESC_CONFIG) { 669 DPRINTFN(-1,("%s: bad desc %d\n", __func__, 670 cdp->bDescriptorType)); 671 err = USBD_INVAL; 672 goto bad; 673 } 674 675 /* Figure out if the device is self or bus powered. */ 676 selfpowered = 0; 677 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) && 678 (cdp->bmAttributes & UC_SELF_POWERED)) { 679 /* May be self powered. */ 680 if (cdp->bmAttributes & UC_BUS_POWERED) { 681 /* Must ask device. */ 682 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) { 683 /* 684 * Hub claims to be self powered, but isn't. 685 * It seems that the power status can be 686 * determined by the hub characteristics. 687 */ 688 usb_hub_descriptor_t hd; 689 usb_device_request_t req; 690 req.bmRequestType = UT_READ_CLASS_DEVICE; 691 req.bRequest = UR_GET_DESCRIPTOR; 692 USETW(req.wValue, 0); 693 USETW(req.wIndex, 0); 694 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 695 err = usbd_do_request(dev, &req, &hd); 696 if (!err && 697 (UGETW(hd.wHubCharacteristics) & 698 UHD_PWR_INDIVIDUAL)) 699 selfpowered = 1; 700 DPRINTF(("%s: charac=0x%04x, error=%s\n", 701 __func__, UGETW(hd.wHubCharacteristics), 702 usbd_errstr(err))); 703 } else { 704 err = usbd_get_device_status(dev, &ds); 705 if (!err && 706 (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 707 selfpowered = 1; 708 DPRINTF(("%s: status=0x%04x, error=%s\n", 709 __func__, UGETW(ds.wStatus), 710 usbd_errstr(err))); 711 } 712 } else 713 selfpowered = 1; 714 } 715 DPRINTF(("%s: (addr %d) cno=%d attr=0x%02x, selfpowered=%d, power=%d\n", 716 __func__, dev->address, cdp->bConfigurationValue, cdp->bmAttributes, 717 selfpowered, cdp->bMaxPower * 2)); 718 719 /* Check if we have enough power. */ 720 #ifdef USB_DEBUG 721 if (dev->powersrc == NULL) { 722 DPRINTF(("%s: No power source?\n", __func__)); 723 err = USBD_IOERROR; 724 goto bad; 725 } 726 #endif 727 power = cdp->bMaxPower * 2; 728 if (power > dev->powersrc->power) { 729 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power)); 730 /* XXX print nicer message. */ 731 if (msg) 732 printf("%s: device addr %d (config %d) exceeds power " 733 "budget, %d mA > %d mA\n", 734 dev->bus->bdev.dv_xname, dev->address, 735 cdp->bConfigurationValue, 736 power, dev->powersrc->power); 737 err = USBD_NO_POWER; 738 goto bad; 739 } 740 dev->power = power; 741 dev->self_powered = selfpowered; 742 743 /* Set the actual configuration value. */ 744 DPRINTF(("%s: set config %d\n", __func__, cdp->bConfigurationValue)); 745 err = usbd_set_config(dev, cdp->bConfigurationValue); 746 if (err) { 747 DPRINTF(("%s: setting config=%d failed, error=%s\n", __func__, 748 cdp->bConfigurationValue, usbd_errstr(err))); 749 goto bad; 750 } 751 752 /* Allocate and fill interface data. */ 753 nifc = cdp->bNumInterfaces; 754 dev->ifaces = mallocarray(nifc, sizeof(*dev->ifaces), M_USB, 755 M_NOWAIT | M_ZERO); 756 if (dev->ifaces == NULL) { 757 err = USBD_NOMEM; 758 goto bad; 759 } 760 DPRINTFN(5,("%s: dev=%p cdesc=%p\n", __func__, dev, cdp)); 761 dev->cdesc = cdp; 762 dev->config = cdp->bConfigurationValue; 763 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 764 err = usbd_fill_iface_data(dev, ifcidx, 0); 765 if (err) 766 return (err); 767 } 768 769 return (USBD_NORMAL_COMPLETION); 770 771 bad: 772 free(cdp, M_USB, cdplen); 773 return (err); 774 } 775 776 /* XXX add function for alternate settings */ 777 778 usbd_status 779 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface, 780 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe) 781 { 782 struct usbd_pipe *p; 783 usbd_status err; 784 785 DPRINTF(("%s: dev=%p iface=%p ep=%p pipe=%p\n", __func__, 786 dev, iface, ep, pipe)); 787 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT|M_ZERO); 788 if (p == NULL) 789 return (USBD_NOMEM); 790 p->pipe_size = dev->bus->pipe_size; 791 p->device = dev; 792 p->iface = iface; 793 p->endpoint = ep; 794 ep->refcnt++; 795 p->interval = ival; 796 SIMPLEQ_INIT(&p->queue); 797 err = dev->bus->methods->open_pipe(p); 798 if (err) { 799 DPRINTF(("%s: endpoint=0x%x failed, error=%s\n", __func__, 800 ep->edesc->bEndpointAddress, usbd_errstr(err))); 801 free(p, M_USB, dev->bus->pipe_size); 802 return (err); 803 } 804 *pipe = p; 805 return (USBD_NORMAL_COMPLETION); 806 } 807 808 int 809 usbd_set_address(struct usbd_device *dev, int addr) 810 { 811 usb_device_request_t req; 812 813 req.bmRequestType = UT_WRITE_DEVICE; 814 req.bRequest = UR_SET_ADDRESS; 815 USETW(req.wValue, addr); 816 USETW(req.wIndex, 0); 817 USETW(req.wLength, 0); 818 if (usbd_do_request(dev, &req, 0)) 819 return (1); 820 821 /* Allow device time to set new address */ 822 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 823 824 return (0); 825 } 826 827 int 828 usbd_getnewaddr(struct usbd_bus *bus) 829 { 830 int addr; 831 832 for (addr = 1; addr < USB_MAX_DEVICES; addr++) 833 if (bus->devices[addr] == NULL) 834 return (addr); 835 return (-1); 836 } 837 838 usbd_status 839 usbd_probe_and_attach(struct device *parent, struct usbd_device *dev, int port, 840 int addr) 841 { 842 /* 843 * Used to correlate audio and wskbd devices as this is the common point 844 * of attachment between the two. 845 */ 846 static char *cookie = 0; 847 struct usb_attach_arg uaa; 848 usb_device_descriptor_t *dd = &dev->ddesc; 849 int i, confi, nifaces; 850 usbd_status err; 851 struct device *dv; 852 struct usbd_interface **ifaces; 853 extern struct rwlock usbpalock; 854 855 rw_enter_write(&usbpalock); 856 857 uaa.device = dev; 858 uaa.iface = NULL; 859 uaa.ifaces = NULL; 860 uaa.nifaces = 0; 861 uaa.usegeneric = 0; 862 uaa.port = port; 863 uaa.configno = UHUB_UNK_CONFIGURATION; 864 uaa.ifaceno = UHUB_UNK_INTERFACE; 865 uaa.vendor = UGETW(dd->idVendor); 866 uaa.product = UGETW(dd->idProduct); 867 uaa.release = UGETW(dd->bcdDevice); 868 uaa.cookie = ++cookie; 869 870 /* First try with device specific drivers. */ 871 DPRINTF(("usbd_probe_and_attach trying device specific drivers\n")); 872 dv = config_found(parent, &uaa, usbd_print); 873 if (dv) { 874 dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT); 875 if (dev->subdevs == NULL) { 876 err = USBD_NOMEM; 877 goto fail; 878 } 879 dev->nsubdev = 2; 880 dev->subdevs[dev->ndevs++] = dv; 881 dev->subdevs[dev->ndevs] = 0; 882 err = USBD_NORMAL_COMPLETION; 883 goto fail; 884 } 885 886 DPRINTF(("%s: no device specific driver found\n", __func__)); 887 888 DPRINTF(("%s: looping over %d configurations\n", __func__, 889 dd->bNumConfigurations)); 890 /* Next try with interface drivers. */ 891 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 892 DPRINTFN(1,("%s: trying config idx=%d\n", __func__, 893 confi)); 894 err = usbd_set_config_index(dev, confi, 1); 895 if (err) { 896 #ifdef USB_DEBUG 897 DPRINTF(("%s: port %d, set config at addr %d failed, " 898 "error=%s\n", parent->dv_xname, port, 899 addr, usbd_errstr(err))); 900 #else 901 printf("%s: port %d, set config %d at addr %d failed\n", 902 parent->dv_xname, port, confi, addr); 903 #endif 904 905 goto fail; 906 } 907 nifaces = dev->cdesc->bNumInterfaces; 908 uaa.configno = dev->cdesc->bConfigurationValue; 909 ifaces = mallocarray(nifaces, sizeof(*ifaces), M_USB, M_NOWAIT); 910 if (ifaces == NULL) { 911 err = USBD_NOMEM; 912 goto fail; 913 } 914 for (i = 0; i < nifaces; i++) 915 ifaces[i] = &dev->ifaces[i]; 916 uaa.ifaces = ifaces; 917 uaa.nifaces = nifaces; 918 919 /* add 1 for possible ugen and 1 for NULL terminator */ 920 dev->subdevs = mallocarray(nifaces + 2, sizeof(dv), M_USB, 921 M_NOWAIT | M_ZERO); 922 if (dev->subdevs == NULL) { 923 free(ifaces, M_USB, nifaces * sizeof(*ifaces)); 924 err = USBD_NOMEM; 925 goto fail; 926 } 927 dev->nsubdev = nifaces + 2; 928 929 for (i = 0; i < nifaces; i++) { 930 if (usbd_iface_claimed(dev, i)) 931 continue; 932 uaa.iface = ifaces[i]; 933 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber; 934 dv = config_found(parent, &uaa, usbd_print); 935 if (dv != NULL) { 936 dev->subdevs[dev->ndevs++] = dv; 937 usbd_claim_iface(dev, i); 938 } 939 } 940 free(ifaces, M_USB, nifaces * sizeof(*ifaces)); 941 942 if (dev->ndevs > 0) { 943 for (i = 0; i < nifaces; i++) { 944 if (!usbd_iface_claimed(dev, i)) 945 break; 946 } 947 if (i < nifaces) 948 goto generic; 949 else 950 goto fail; 951 } 952 953 free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs)); 954 dev->subdevs = NULL; 955 dev->nsubdev = 0; 956 } 957 /* No interfaces were attached in any of the configurations. */ 958 959 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 960 usbd_set_config_index(dev, 0, 0); 961 962 DPRINTF(("%s: no interface drivers found\n", __func__)); 963 964 generic: 965 /* Finally try the generic driver. */ 966 uaa.iface = NULL; 967 uaa.usegeneric = 1; 968 uaa.configno = dev->ndevs == 0 ? UHUB_UNK_CONFIGURATION : 969 dev->cdesc->bConfigurationValue; 970 uaa.ifaceno = UHUB_UNK_INTERFACE; 971 dv = config_found(parent, &uaa, usbd_print); 972 if (dv != NULL) { 973 if (dev->ndevs == 0) { 974 dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT); 975 if (dev->subdevs == NULL) { 976 err = USBD_NOMEM; 977 goto fail; 978 } 979 dev->nsubdev = 2; 980 } 981 dev->subdevs[dev->ndevs++] = dv; 982 dev->subdevs[dev->ndevs] = 0; 983 err = USBD_NORMAL_COMPLETION; 984 goto fail; 985 } 986 987 /* 988 * The generic attach failed, but leave the device as it is. 989 * We just did not find any drivers, that's all. The device is 990 * fully operational and not harming anyone. 991 */ 992 DPRINTF(("%s: generic attach failed\n", __func__)); 993 err = USBD_NORMAL_COMPLETION; 994 fail: 995 rw_exit_write(&usbpalock); 996 return (err); 997 } 998 999 1000 /* 1001 * Called when a new device has been put in the powered state, 1002 * but not yet in the addressed state. 1003 * Get initial descriptor, set the address, get full descriptor, 1004 * and attach a driver. 1005 */ 1006 usbd_status 1007 usbd_new_device(struct device *parent, struct usbd_bus *bus, int depth, 1008 int speed, int port, struct usbd_port *up) 1009 { 1010 struct usbd_device *dev, *adev, *hub; 1011 usb_device_descriptor_t *dd; 1012 usbd_status err; 1013 uint32_t mps, mps0; 1014 int addr, i, p; 1015 1016 DPRINTF(("%s: bus=%p port=%d depth=%d speed=%d\n", __func__, 1017 bus, port, depth, speed)); 1018 1019 /* 1020 * Fixed size for ep0 max packet, FULL device variable size is 1021 * handled below. 1022 */ 1023 switch (speed) { 1024 case USB_SPEED_LOW: 1025 mps0 = 8; 1026 break; 1027 case USB_SPEED_HIGH: 1028 case USB_SPEED_FULL: 1029 mps0 = 64; 1030 break; 1031 case USB_SPEED_SUPER: 1032 mps0 = 512; 1033 break; 1034 default: 1035 return (USBD_INVAL); 1036 } 1037 1038 addr = usbd_getnewaddr(bus); 1039 if (addr < 0) { 1040 printf("%s: No free USB addresses, new device ignored.\n", 1041 bus->bdev.dv_xname); 1042 return (USBD_NO_ADDR); 1043 } 1044 1045 dev = malloc(sizeof *dev, M_USB, M_NOWAIT | M_ZERO); 1046 if (dev == NULL) 1047 return (USBD_NOMEM); 1048 1049 dev->bus = bus; 1050 1051 /* Set up default endpoint handle. */ 1052 dev->def_ep.edesc = &dev->def_ep_desc; 1053 1054 /* Set up default endpoint descriptor. */ 1055 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1056 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1057 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1058 dev->def_ep_desc.bmAttributes = UE_CONTROL; 1059 dev->def_ep_desc.bInterval = 0; 1060 USETW(dev->def_ep_desc.wMaxPacketSize, mps0); 1061 1062 dev->quirks = &usbd_no_quirk; 1063 dev->address = USB_START_ADDR; 1064 dev->ddesc.bMaxPacketSize = 0; 1065 dev->depth = depth; 1066 dev->powersrc = up; 1067 dev->myhub = up->parent; 1068 dev->speed = speed; 1069 dev->langid = USBD_NOLANG; 1070 1071 up->device = dev; 1072 1073 /* Locate port on upstream high speed hub */ 1074 for (adev = dev, hub = up->parent; 1075 hub != NULL && hub->speed != USB_SPEED_HIGH; 1076 adev = hub, hub = hub->myhub) 1077 ; 1078 if (hub) { 1079 for (p = 0; p < hub->hub->nports; p++) { 1080 if (hub->hub->ports[p].device == adev) { 1081 dev->myhsport = &hub->hub->ports[p]; 1082 goto found; 1083 } 1084 } 1085 panic("usbd_new_device: cannot find HS port"); 1086 found: 1087 DPRINTFN(1,("%s: high speed port %d\n", __func__, p)); 1088 } else { 1089 dev->myhsport = NULL; 1090 } 1091 1092 /* Establish the default pipe. */ 1093 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1094 &dev->default_pipe); 1095 if (err) 1096 goto fail; 1097 1098 dd = &dev->ddesc; 1099 1100 /* Try to get device descriptor */ 1101 /* 1102 * some device will need small size query at first (XXX: out of spec) 1103 * we will get full size descriptor later, just determine the maximum 1104 * packet size of the control pipe at this moment. 1105 */ 1106 for (i = 0; i < 3; i++) { 1107 /* Get the first 8 bytes of the device descriptor. */ 1108 /* 8 byte is magic size, some device only return 8 byte for 1st 1109 * query (XXX: out of spec) */ 1110 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd); 1111 if (!err) 1112 break; 1113 if (err == USBD_TIMEOUT) 1114 goto fail; 1115 usbd_delay_ms(dev, 100+50*i); 1116 } 1117 1118 /* some device need actual size request for the query. try again */ 1119 if (err) { 1120 USETW(dev->def_ep_desc.wMaxPacketSize, 1121 USB_DEVICE_DESCRIPTOR_SIZE); 1122 usbd_reset_port(up->parent, port); 1123 for (i = 0; i < 3; i++) { 1124 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1125 USB_DEVICE_DESCRIPTOR_SIZE, dd); 1126 if (!err) 1127 break; 1128 if (err == USBD_TIMEOUT) 1129 goto fail; 1130 usbd_delay_ms(dev, 100+50*i); 1131 } 1132 } 1133 1134 /* XXX some devices need more time to wake up */ 1135 if (err) { 1136 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 1137 usbd_reset_port(up->parent, port); 1138 usbd_delay_ms(dev, 500); 1139 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1140 USB_MAX_IPACKET, dd); 1141 } 1142 1143 if (err) 1144 goto fail; 1145 1146 DPRINTF(("%s: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, " 1147 "protocol=%d, maxpacket=%d, len=%d, speed=%d\n", __func__, 1148 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, 1149 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, 1150 dev->speed)); 1151 1152 if ((dd->bDescriptorType != UDESC_DEVICE) || 1153 (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE)) { 1154 err = USBD_INVAL; 1155 goto fail; 1156 } 1157 1158 mps = dd->bMaxPacketSize; 1159 if (speed == USB_SPEED_SUPER) { 1160 if (mps == 0xff) 1161 mps = 9; 1162 /* xHCI Section 4.8.2.1 */ 1163 mps = (1 << mps); 1164 } 1165 1166 if (mps != mps0) { 1167 if ((speed == USB_SPEED_LOW) || 1168 (mps != 8 && mps != 16 && mps != 32 && mps != 64)) { 1169 err = USBD_INVAL; 1170 goto fail; 1171 } 1172 USETW(dev->def_ep_desc.wMaxPacketSize, mps); 1173 } 1174 1175 1176 /* Set the address if the HC didn't do it already. */ 1177 if (bus->methods->dev_setaddr != NULL && 1178 bus->methods->dev_setaddr(dev, addr)) { 1179 err = USBD_SET_ADDR_FAILED; 1180 goto fail; 1181 } 1182 1183 /* Wait for device to settle before reloading the descriptor. */ 1184 usbd_delay_ms(dev, 10); 1185 1186 /* 1187 * If this device is attached to an xHCI controller, this 1188 * address does not correspond to the hardware one. 1189 */ 1190 dev->address = addr; 1191 1192 err = usbd_reload_device_desc(dev); 1193 if (err) 1194 goto fail; 1195 1196 /* send disown request to handover 2.0 to 1.1. */ 1197 if (dev->quirks->uq_flags & UQ_EHCI_NEEDTO_DISOWN) { 1198 /* only effective when the target device is on ehci */ 1199 if (dev->bus->usbrev == USBREV_2_0) { 1200 DPRINTF(("%s: disown request issues to dev:%p on usb2.0 bus\n", 1201 __func__, dev)); 1202 usbd_port_disown_to_1_1(dev->myhub, port); 1203 /* reset_port required to finish disown request */ 1204 usbd_reset_port(dev->myhub, port); 1205 return (USBD_NORMAL_COMPLETION); 1206 } 1207 } 1208 1209 /* Assume 100mA bus powered for now. Changed when configured. */ 1210 dev->power = USB_MIN_POWER; 1211 dev->self_powered = 0; 1212 1213 DPRINTF(("%s: new dev (addr %d), dev=%p, parent=%p\n", __func__, 1214 addr, dev, parent)); 1215 1216 /* Get device info and cache it */ 1217 err = usbd_cache_devinfo(dev); 1218 if (err) 1219 goto fail; 1220 1221 bus->devices[addr] = dev; 1222 1223 err = usbd_probe_and_attach(parent, dev, port, addr); 1224 if (err) 1225 goto fail; 1226 1227 return (USBD_NORMAL_COMPLETION); 1228 1229 fail: 1230 usb_free_device(dev); 1231 up->device = NULL; 1232 return (err); 1233 } 1234 1235 usbd_status 1236 usbd_reload_device_desc(struct usbd_device *dev) 1237 { 1238 usbd_status err; 1239 1240 /* Get the full device descriptor. */ 1241 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1242 USB_DEVICE_DESCRIPTOR_SIZE, &dev->ddesc); 1243 if (err) 1244 return (err); 1245 1246 /* Figure out what's wrong with this device. */ 1247 dev->quirks = usbd_find_quirk(&dev->ddesc); 1248 1249 return (USBD_NORMAL_COMPLETION); 1250 } 1251 1252 int 1253 usbd_print(void *aux, const char *pnp) 1254 { 1255 struct usb_attach_arg *uaa = aux; 1256 char *devinfop; 1257 1258 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK); 1259 usbd_devinfo(uaa->device, 0, devinfop, DEVINFOSIZE); 1260 1261 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1262 if (pnp) { 1263 if (!uaa->usegeneric) { 1264 free(devinfop, M_TEMP, DEVINFOSIZE); 1265 return (QUIET); 1266 } 1267 printf("%s at %s", devinfop, pnp); 1268 } 1269 if (uaa->port != 0) 1270 printf(" port %d", uaa->port); 1271 if (uaa->configno != UHUB_UNK_CONFIGURATION) 1272 printf(" configuration %d", uaa->configno); 1273 if (uaa->ifaceno != UHUB_UNK_INTERFACE) 1274 printf(" interface %d", uaa->ifaceno); 1275 1276 if (!pnp) 1277 printf(" %s\n", devinfop); 1278 free(devinfop, M_TEMP, DEVINFOSIZE); 1279 return (UNCONF); 1280 } 1281 1282 void 1283 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di) 1284 { 1285 struct usbd_port *p; 1286 int i; 1287 1288 di->udi_bus = dev->bus->usbctl->dv_unit; 1289 di->udi_addr = dev->address; 1290 strlcpy(di->udi_vendor, dev->vendor, sizeof(di->udi_vendor)); 1291 strlcpy(di->udi_product, dev->product, sizeof(di->udi_product)); 1292 usbd_printBCD(di->udi_release, sizeof di->udi_release, 1293 UGETW(dev->ddesc.bcdDevice)); 1294 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1295 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1296 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1297 di->udi_class = dev->ddesc.bDeviceClass; 1298 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1299 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1300 di->udi_config = dev->config; 1301 di->udi_power = dev->self_powered ? 0 : dev->power; 1302 di->udi_speed = dev->speed; 1303 di->udi_port = dev->powersrc ? dev->powersrc->portno : 0; 1304 1305 if (dev->subdevs != NULL) { 1306 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) { 1307 strncpy(di->udi_devnames[i], 1308 dev->subdevs[i]->dv_xname, USB_MAX_DEVNAMELEN); 1309 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0'; 1310 } 1311 } else 1312 i = 0; 1313 1314 for (/*i is set */; i < USB_MAX_DEVNAMES; i++) 1315 di->udi_devnames[i][0] = 0; /* empty */ 1316 1317 if (dev->hub) { 1318 for (i = 0; 1319 i < nitems(di->udi_ports) && i < dev->hub->nports; i++) { 1320 p = &dev->hub->ports[i]; 1321 di->udi_ports[i] = UGETW(p->status.wPortChange) << 16 | 1322 UGETW(p->status.wPortStatus); 1323 } 1324 di->udi_nports = dev->hub->nports; 1325 } else 1326 di->udi_nports = 0; 1327 1328 bzero(di->udi_serial, sizeof(di->udi_serial)); 1329 if (dev->serial != NULL) 1330 strlcpy(di->udi_serial, dev->serial, 1331 sizeof(di->udi_serial)); 1332 } 1333 1334 /* Retrieve a complete descriptor for a certain device and index. */ 1335 usb_config_descriptor_t * 1336 usbd_get_cdesc(struct usbd_device *dev, int index, u_int *lenp) 1337 { 1338 usb_config_descriptor_t *cdesc, *tdesc, cdescr; 1339 u_int len; 1340 usbd_status err; 1341 1342 if (index == USB_CURRENT_CONFIG_INDEX) { 1343 tdesc = usbd_get_config_descriptor(dev); 1344 if (tdesc == NULL) 1345 return (NULL); 1346 len = UGETW(tdesc->wTotalLength); 1347 if (lenp) 1348 *lenp = len; 1349 cdesc = malloc(len, M_TEMP, M_WAITOK); 1350 memcpy(cdesc, tdesc, len); 1351 DPRINTFN(5,("%s: current, len=%u\n", __func__, len)); 1352 } else { 1353 err = usbd_get_desc(dev, UDESC_CONFIG, index, 1354 USB_CONFIG_DESCRIPTOR_SIZE, &cdescr); 1355 if (err || cdescr.bDescriptorType != UDESC_CONFIG) 1356 return (NULL); 1357 len = UGETW(cdescr.wTotalLength); 1358 DPRINTFN(5,("%s: index=%d, len=%u\n", __func__, index, len)); 1359 if (lenp) 1360 *lenp = len; 1361 cdesc = malloc(len, M_TEMP, M_WAITOK); 1362 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdesc); 1363 if (err) { 1364 free(cdesc, M_TEMP, len); 1365 return (NULL); 1366 } 1367 } 1368 return (cdesc); 1369 } 1370 1371 void 1372 usb_free_device(struct usbd_device *dev) 1373 { 1374 int ifcidx, nifc; 1375 1376 DPRINTF(("%s: %p\n", __func__, dev)); 1377 1378 if (dev->default_pipe != NULL) 1379 usbd_close_pipe(dev->default_pipe); 1380 if (dev->ifaces != NULL) { 1381 nifc = dev->cdesc->bNumInterfaces; 1382 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1383 usbd_free_iface_data(dev, ifcidx); 1384 free(dev->ifaces, M_USB, nifc * sizeof(*dev->ifaces)); 1385 } 1386 if (dev->cdesc != NULL) 1387 free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength)); 1388 free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs)); 1389 dev->bus->devices[dev->address] = NULL; 1390 1391 if (dev->vendor != NULL) 1392 free(dev->vendor, M_USB, USB_MAX_STRING_LEN); 1393 if (dev->product != NULL) 1394 free(dev->product, M_USB, USB_MAX_STRING_LEN); 1395 if (dev->serial != NULL) 1396 free(dev->serial, M_USB, USB_MAX_STRING_LEN); 1397 1398 free(dev, M_USB, sizeof *dev); 1399 } 1400 1401 /* 1402 * Should only be called by the USB thread doing bus exploration to 1403 * avoid connect/disconnect races. 1404 */ 1405 int 1406 usbd_detach(struct usbd_device *dev, struct device *parent) 1407 { 1408 int i, rv = 0; 1409 1410 usbd_deactivate(dev); 1411 1412 if (dev->ndevs > 0) { 1413 for (i = 0; dev->subdevs[i] != NULL; i++) 1414 rv |= config_detach(dev->subdevs[i], DETACH_FORCE); 1415 } 1416 1417 if (rv == 0) 1418 usb_free_device(dev); 1419 1420 return (rv); 1421 } 1422