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