1 /* $OpenBSD: usb_subr.c,v 1.156 2021/12/04 07:01:59 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 occured. */ 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 struct usb_attach_arg uaa; 843 usb_device_descriptor_t *dd = &dev->ddesc; 844 int i, confi, nifaces; 845 usbd_status err; 846 struct device *dv; 847 struct usbd_interface **ifaces; 848 extern struct rwlock usbpalock; 849 850 rw_enter_write(&usbpalock); 851 852 uaa.device = dev; 853 uaa.iface = NULL; 854 uaa.ifaces = NULL; 855 uaa.nifaces = 0; 856 uaa.usegeneric = 0; 857 uaa.port = port; 858 uaa.configno = UHUB_UNK_CONFIGURATION; 859 uaa.ifaceno = UHUB_UNK_INTERFACE; 860 uaa.vendor = UGETW(dd->idVendor); 861 uaa.product = UGETW(dd->idProduct); 862 uaa.release = UGETW(dd->bcdDevice); 863 864 /* First try with device specific drivers. */ 865 DPRINTF(("usbd_probe_and_attach trying device specific drivers\n")); 866 dv = config_found(parent, &uaa, usbd_print); 867 if (dv) { 868 dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT); 869 if (dev->subdevs == NULL) { 870 err = USBD_NOMEM; 871 goto fail; 872 } 873 dev->nsubdev = 2; 874 dev->subdevs[dev->ndevs++] = dv; 875 dev->subdevs[dev->ndevs] = 0; 876 err = USBD_NORMAL_COMPLETION; 877 goto fail; 878 } 879 880 DPRINTF(("%s: no device specific driver found\n", __func__)); 881 882 DPRINTF(("%s: looping over %d configurations\n", __func__, 883 dd->bNumConfigurations)); 884 /* Next try with interface drivers. */ 885 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 886 DPRINTFN(1,("%s: trying config idx=%d\n", __func__, 887 confi)); 888 err = usbd_set_config_index(dev, confi, 1); 889 if (err) { 890 #ifdef USB_DEBUG 891 DPRINTF(("%s: port %d, set config at addr %d failed, " 892 "error=%s\n", parent->dv_xname, port, 893 addr, usbd_errstr(err))); 894 #else 895 printf("%s: port %d, set config %d at addr %d failed\n", 896 parent->dv_xname, port, confi, addr); 897 #endif 898 899 goto fail; 900 } 901 nifaces = dev->cdesc->bNumInterfaces; 902 uaa.configno = dev->cdesc->bConfigurationValue; 903 ifaces = mallocarray(nifaces, sizeof(*ifaces), M_USB, M_NOWAIT); 904 if (ifaces == NULL) { 905 err = USBD_NOMEM; 906 goto fail; 907 } 908 for (i = 0; i < nifaces; i++) 909 ifaces[i] = &dev->ifaces[i]; 910 uaa.ifaces = ifaces; 911 uaa.nifaces = nifaces; 912 913 /* add 1 for possible ugen and 1 for NULL terminator */ 914 dev->subdevs = mallocarray(nifaces + 2, sizeof(dv), M_USB, 915 M_NOWAIT | M_ZERO); 916 if (dev->subdevs == NULL) { 917 free(ifaces, M_USB, nifaces * sizeof(*ifaces)); 918 err = USBD_NOMEM; 919 goto fail; 920 } 921 dev->nsubdev = nifaces + 2; 922 923 for (i = 0; i < nifaces; i++) { 924 if (usbd_iface_claimed(dev, i)) 925 continue; 926 uaa.iface = ifaces[i]; 927 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber; 928 dv = config_found(parent, &uaa, usbd_print); 929 if (dv != NULL) { 930 dev->subdevs[dev->ndevs++] = dv; 931 usbd_claim_iface(dev, i); 932 } 933 } 934 free(ifaces, M_USB, nifaces * sizeof(*ifaces)); 935 936 if (dev->ndevs > 0) { 937 for (i = 0; i < nifaces; i++) { 938 if (!usbd_iface_claimed(dev, i)) 939 break; 940 } 941 if (i < nifaces) 942 goto generic; 943 else 944 goto fail; 945 } 946 947 free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs)); 948 dev->subdevs = NULL; 949 dev->nsubdev = 0; 950 } 951 /* No interfaces were attached in any of the configurations. */ 952 953 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 954 usbd_set_config_index(dev, 0, 0); 955 956 DPRINTF(("%s: no interface drivers found\n", __func__)); 957 958 generic: 959 /* Finally try the generic driver. */ 960 uaa.iface = NULL; 961 uaa.usegeneric = 1; 962 uaa.configno = dev->ndevs == 0 ? UHUB_UNK_CONFIGURATION : 963 dev->cdesc->bConfigurationValue; 964 uaa.ifaceno = UHUB_UNK_INTERFACE; 965 dv = config_found(parent, &uaa, usbd_print); 966 if (dv != NULL) { 967 if (dev->ndevs == 0) { 968 dev->subdevs = mallocarray(2, sizeof dv, M_USB, M_NOWAIT); 969 if (dev->subdevs == NULL) { 970 err = USBD_NOMEM; 971 goto fail; 972 } 973 dev->nsubdev = 2; 974 } 975 dev->subdevs[dev->ndevs++] = dv; 976 dev->subdevs[dev->ndevs] = 0; 977 err = USBD_NORMAL_COMPLETION; 978 goto fail; 979 } 980 981 /* 982 * The generic attach failed, but leave the device as it is. 983 * We just did not find any drivers, that's all. The device is 984 * fully operational and not harming anyone. 985 */ 986 DPRINTF(("%s: generic attach failed\n", __func__)); 987 err = USBD_NORMAL_COMPLETION; 988 fail: 989 rw_exit_write(&usbpalock); 990 return (err); 991 } 992 993 994 /* 995 * Called when a new device has been put in the powered state, 996 * but not yet in the addressed state. 997 * Get initial descriptor, set the address, get full descriptor, 998 * and attach a driver. 999 */ 1000 usbd_status 1001 usbd_new_device(struct device *parent, struct usbd_bus *bus, int depth, 1002 int speed, int port, struct usbd_port *up) 1003 { 1004 struct usbd_device *dev, *adev, *hub; 1005 usb_device_descriptor_t *dd; 1006 usbd_status err; 1007 uint32_t mps, mps0; 1008 int addr, i, p; 1009 1010 DPRINTF(("%s: bus=%p port=%d depth=%d speed=%d\n", __func__, 1011 bus, port, depth, speed)); 1012 1013 /* 1014 * Fixed size for ep0 max packet, FULL device variable size is 1015 * handled below. 1016 */ 1017 switch (speed) { 1018 case USB_SPEED_LOW: 1019 mps0 = 8; 1020 break; 1021 case USB_SPEED_HIGH: 1022 case USB_SPEED_FULL: 1023 mps0 = 64; 1024 break; 1025 case USB_SPEED_SUPER: 1026 mps0 = 512; 1027 break; 1028 default: 1029 return (USBD_INVAL); 1030 } 1031 1032 addr = usbd_getnewaddr(bus); 1033 if (addr < 0) { 1034 printf("%s: No free USB addresses, new device ignored.\n", 1035 bus->bdev.dv_xname); 1036 return (USBD_NO_ADDR); 1037 } 1038 1039 dev = malloc(sizeof *dev, M_USB, M_NOWAIT | M_ZERO); 1040 if (dev == NULL) 1041 return (USBD_NOMEM); 1042 1043 dev->bus = bus; 1044 1045 /* Set up default endpoint handle. */ 1046 dev->def_ep.edesc = &dev->def_ep_desc; 1047 1048 /* Set up default endpoint descriptor. */ 1049 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1050 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1051 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1052 dev->def_ep_desc.bmAttributes = UE_CONTROL; 1053 dev->def_ep_desc.bInterval = 0; 1054 USETW(dev->def_ep_desc.wMaxPacketSize, mps0); 1055 1056 dev->quirks = &usbd_no_quirk; 1057 dev->address = USB_START_ADDR; 1058 dev->ddesc.bMaxPacketSize = 0; 1059 dev->depth = depth; 1060 dev->powersrc = up; 1061 dev->myhub = up->parent; 1062 dev->speed = speed; 1063 dev->langid = USBD_NOLANG; 1064 1065 up->device = dev; 1066 1067 /* Locate port on upstream high speed hub */ 1068 for (adev = dev, hub = up->parent; 1069 hub != NULL && hub->speed != USB_SPEED_HIGH; 1070 adev = hub, hub = hub->myhub) 1071 ; 1072 if (hub) { 1073 for (p = 0; p < hub->hub->nports; p++) { 1074 if (hub->hub->ports[p].device == adev) { 1075 dev->myhsport = &hub->hub->ports[p]; 1076 goto found; 1077 } 1078 } 1079 panic("usbd_new_device: cannot find HS port"); 1080 found: 1081 DPRINTFN(1,("%s: high speed port %d\n", __func__, p)); 1082 } else { 1083 dev->myhsport = NULL; 1084 } 1085 1086 /* Establish the default pipe. */ 1087 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1088 &dev->default_pipe); 1089 if (err) 1090 goto fail; 1091 1092 dd = &dev->ddesc; 1093 1094 /* Try to get device descriptor */ 1095 /* 1096 * some device will need small size query at first (XXX: out of spec) 1097 * we will get full size descriptor later, just determin the maximum 1098 * packet size of the control pipe at this moment. 1099 */ 1100 for (i = 0; i < 3; i++) { 1101 /* Get the first 8 bytes of the device descriptor. */ 1102 /* 8 byte is magic size, some device only return 8 byte for 1st 1103 * query (XXX: out of spec) */ 1104 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd); 1105 if (!err) 1106 break; 1107 if (err == USBD_TIMEOUT) 1108 goto fail; 1109 usbd_delay_ms(dev, 100+50*i); 1110 } 1111 1112 /* some device need actual size request for the query. try again */ 1113 if (err) { 1114 USETW(dev->def_ep_desc.wMaxPacketSize, 1115 USB_DEVICE_DESCRIPTOR_SIZE); 1116 usbd_reset_port(up->parent, port); 1117 for (i = 0; i < 3; i++) { 1118 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1119 USB_DEVICE_DESCRIPTOR_SIZE, dd); 1120 if (!err) 1121 break; 1122 if (err == USBD_TIMEOUT) 1123 goto fail; 1124 usbd_delay_ms(dev, 100+50*i); 1125 } 1126 } 1127 1128 /* XXX some devices need more time to wake up */ 1129 if (err) { 1130 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 1131 usbd_reset_port(up->parent, port); 1132 usbd_delay_ms(dev, 500); 1133 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1134 USB_MAX_IPACKET, dd); 1135 } 1136 1137 if (err) 1138 goto fail; 1139 1140 DPRINTF(("%s: adding unit addr=%d, rev=%02x, class=%d, subclass=%d, " 1141 "protocol=%d, maxpacket=%d, len=%d, speed=%d\n", __func__, 1142 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, 1143 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, 1144 dev->speed)); 1145 1146 if ((dd->bDescriptorType != UDESC_DEVICE) || 1147 (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE)) { 1148 err = USBD_INVAL; 1149 goto fail; 1150 } 1151 1152 mps = dd->bMaxPacketSize; 1153 if (speed == USB_SPEED_SUPER) { 1154 if (mps == 0xff) 1155 mps = 9; 1156 /* xHCI Section 4.8.2.1 */ 1157 mps = (1 << mps); 1158 } 1159 1160 if (mps != mps0) { 1161 if ((speed == USB_SPEED_LOW) || 1162 (mps != 8 && mps != 16 && mps != 32 && mps != 64)) { 1163 err = USBD_INVAL; 1164 goto fail; 1165 } 1166 USETW(dev->def_ep_desc.wMaxPacketSize, mps); 1167 } 1168 1169 1170 /* Set the address if the HC didn't do it already. */ 1171 if (bus->methods->dev_setaddr != NULL && 1172 bus->methods->dev_setaddr(dev, addr)) { 1173 err = USBD_SET_ADDR_FAILED; 1174 goto fail; 1175 } 1176 1177 /* Wait for device to settle before reloading the descriptor. */ 1178 usbd_delay_ms(dev, 10); 1179 1180 /* 1181 * If this device is attached to an xHCI controller, this 1182 * address does not correspond to the hardware one. 1183 */ 1184 dev->address = addr; 1185 1186 err = usbd_reload_device_desc(dev); 1187 if (err) 1188 goto fail; 1189 1190 /* send disown request to handover 2.0 to 1.1. */ 1191 if (dev->quirks->uq_flags & UQ_EHCI_NEEDTO_DISOWN) { 1192 /* only effective when the target device is on ehci */ 1193 if (dev->bus->usbrev == USBREV_2_0) { 1194 DPRINTF(("%s: disown request issues to dev:%p on usb2.0 bus\n", 1195 __func__, dev)); 1196 usbd_port_disown_to_1_1(dev->myhub, port); 1197 /* reset_port required to finish disown request */ 1198 usbd_reset_port(dev->myhub, port); 1199 return (USBD_NORMAL_COMPLETION); 1200 } 1201 } 1202 1203 /* Assume 100mA bus powered for now. Changed when configured. */ 1204 dev->power = USB_MIN_POWER; 1205 dev->self_powered = 0; 1206 1207 DPRINTF(("%s: new dev (addr %d), dev=%p, parent=%p\n", __func__, 1208 addr, dev, parent)); 1209 1210 /* Get device info and cache it */ 1211 err = usbd_cache_devinfo(dev); 1212 if (err) 1213 goto fail; 1214 1215 bus->devices[addr] = dev; 1216 1217 err = usbd_probe_and_attach(parent, dev, port, addr); 1218 if (err) 1219 goto fail; 1220 1221 return (USBD_NORMAL_COMPLETION); 1222 1223 fail: 1224 usb_free_device(dev); 1225 up->device = NULL; 1226 return (err); 1227 } 1228 1229 usbd_status 1230 usbd_reload_device_desc(struct usbd_device *dev) 1231 { 1232 usbd_status err; 1233 1234 /* Get the full device descriptor. */ 1235 err = usbd_get_desc(dev, UDESC_DEVICE, 0, 1236 USB_DEVICE_DESCRIPTOR_SIZE, &dev->ddesc); 1237 if (err) 1238 return (err); 1239 1240 /* Figure out what's wrong with this device. */ 1241 dev->quirks = usbd_find_quirk(&dev->ddesc); 1242 1243 return (USBD_NORMAL_COMPLETION); 1244 } 1245 1246 int 1247 usbd_print(void *aux, const char *pnp) 1248 { 1249 struct usb_attach_arg *uaa = aux; 1250 char *devinfop; 1251 1252 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK); 1253 usbd_devinfo(uaa->device, 0, devinfop, DEVINFOSIZE); 1254 1255 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1256 if (pnp) { 1257 if (!uaa->usegeneric) { 1258 free(devinfop, M_TEMP, DEVINFOSIZE); 1259 return (QUIET); 1260 } 1261 printf("%s at %s", devinfop, pnp); 1262 } 1263 if (uaa->port != 0) 1264 printf(" port %d", uaa->port); 1265 if (uaa->configno != UHUB_UNK_CONFIGURATION) 1266 printf(" configuration %d", uaa->configno); 1267 if (uaa->ifaceno != UHUB_UNK_INTERFACE) 1268 printf(" interface %d", uaa->ifaceno); 1269 1270 if (!pnp) 1271 printf(" %s\n", devinfop); 1272 free(devinfop, M_TEMP, DEVINFOSIZE); 1273 return (UNCONF); 1274 } 1275 1276 void 1277 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di) 1278 { 1279 struct usbd_port *p; 1280 int i; 1281 1282 di->udi_bus = dev->bus->usbctl->dv_unit; 1283 di->udi_addr = dev->address; 1284 strlcpy(di->udi_vendor, dev->vendor, sizeof(di->udi_vendor)); 1285 strlcpy(di->udi_product, dev->product, sizeof(di->udi_product)); 1286 usbd_printBCD(di->udi_release, sizeof di->udi_release, 1287 UGETW(dev->ddesc.bcdDevice)); 1288 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1289 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1290 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1291 di->udi_class = dev->ddesc.bDeviceClass; 1292 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1293 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1294 di->udi_config = dev->config; 1295 di->udi_power = dev->self_powered ? 0 : dev->power; 1296 di->udi_speed = dev->speed; 1297 di->udi_port = dev->powersrc ? dev->powersrc->portno : 0; 1298 1299 if (dev->subdevs != NULL) { 1300 for (i = 0; dev->subdevs[i] && i < USB_MAX_DEVNAMES; i++) { 1301 strncpy(di->udi_devnames[i], 1302 dev->subdevs[i]->dv_xname, USB_MAX_DEVNAMELEN); 1303 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0'; 1304 } 1305 } else 1306 i = 0; 1307 1308 for (/*i is set */; i < USB_MAX_DEVNAMES; i++) 1309 di->udi_devnames[i][0] = 0; /* empty */ 1310 1311 if (dev->hub) { 1312 for (i = 0; 1313 i < nitems(di->udi_ports) && i < dev->hub->nports; i++) { 1314 p = &dev->hub->ports[i]; 1315 di->udi_ports[i] = UGETW(p->status.wPortChange) << 16 | 1316 UGETW(p->status.wPortStatus); 1317 } 1318 di->udi_nports = dev->hub->nports; 1319 } else 1320 di->udi_nports = 0; 1321 1322 bzero(di->udi_serial, sizeof(di->udi_serial)); 1323 if (dev->serial != NULL) 1324 strlcpy(di->udi_serial, dev->serial, 1325 sizeof(di->udi_serial)); 1326 } 1327 1328 /* Retrieve a complete descriptor for a certain device and index. */ 1329 usb_config_descriptor_t * 1330 usbd_get_cdesc(struct usbd_device *dev, int index, u_int *lenp) 1331 { 1332 usb_config_descriptor_t *cdesc, *tdesc, cdescr; 1333 u_int len; 1334 usbd_status err; 1335 1336 if (index == USB_CURRENT_CONFIG_INDEX) { 1337 tdesc = usbd_get_config_descriptor(dev); 1338 if (tdesc == NULL) 1339 return (NULL); 1340 len = UGETW(tdesc->wTotalLength); 1341 if (lenp) 1342 *lenp = len; 1343 cdesc = malloc(len, M_TEMP, M_WAITOK); 1344 memcpy(cdesc, tdesc, len); 1345 DPRINTFN(5,("%s: current, len=%u\n", __func__, len)); 1346 } else { 1347 err = usbd_get_desc(dev, UDESC_CONFIG, index, 1348 USB_CONFIG_DESCRIPTOR_SIZE, &cdescr); 1349 if (err || cdescr.bDescriptorType != UDESC_CONFIG) 1350 return (NULL); 1351 len = UGETW(cdescr.wTotalLength); 1352 DPRINTFN(5,("%s: index=%d, len=%u\n", __func__, index, len)); 1353 if (lenp) 1354 *lenp = len; 1355 cdesc = malloc(len, M_TEMP, M_WAITOK); 1356 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdesc); 1357 if (err) { 1358 free(cdesc, M_TEMP, len); 1359 return (NULL); 1360 } 1361 } 1362 return (cdesc); 1363 } 1364 1365 void 1366 usb_free_device(struct usbd_device *dev) 1367 { 1368 int ifcidx, nifc; 1369 1370 DPRINTF(("%s: %p\n", __func__, dev)); 1371 1372 if (dev->default_pipe != NULL) 1373 usbd_close_pipe(dev->default_pipe); 1374 if (dev->ifaces != NULL) { 1375 nifc = dev->cdesc->bNumInterfaces; 1376 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1377 usbd_free_iface_data(dev, ifcidx); 1378 free(dev->ifaces, M_USB, nifc * sizeof(*dev->ifaces)); 1379 } 1380 if (dev->cdesc != NULL) 1381 free(dev->cdesc, M_USB, UGETW(dev->cdesc->wTotalLength)); 1382 free(dev->subdevs, M_USB, dev->nsubdev * sizeof(*dev->subdevs)); 1383 dev->bus->devices[dev->address] = NULL; 1384 1385 if (dev->vendor != NULL) 1386 free(dev->vendor, M_USB, USB_MAX_STRING_LEN); 1387 if (dev->product != NULL) 1388 free(dev->product, M_USB, USB_MAX_STRING_LEN); 1389 if (dev->serial != NULL) 1390 free(dev->serial, M_USB, USB_MAX_STRING_LEN); 1391 1392 free(dev, M_USB, sizeof *dev); 1393 } 1394 1395 /* 1396 * Should only be called by the USB thread doing bus exploration to 1397 * avoid connect/disconnect races. 1398 */ 1399 int 1400 usbd_detach(struct usbd_device *dev, struct device *parent) 1401 { 1402 int i, rv = 0; 1403 1404 usbd_deactivate(dev); 1405 1406 if (dev->ndevs > 0) { 1407 for (i = 0; dev->subdevs[i] != NULL; i++) 1408 rv |= config_detach(dev->subdevs[i], DETACH_FORCE); 1409 } 1410 1411 if (rv == 0) 1412 usb_free_device(dev); 1413 1414 return (rv); 1415 } 1416