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