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