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