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