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