1 /* $NetBSD: usb_subr.c,v 1.120 2004/10/23 16:17:56 augustss Exp $ */ 2 /* $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $ */ 3 4 /* 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.120 2004/10/23 16:17:56 augustss Exp $"); 43 44 #include "opt_usbverbose.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #if defined(__NetBSD__) || defined(__OpenBSD__) 51 #include <sys/device.h> 52 #include <sys/select.h> 53 #elif defined(__FreeBSD__) 54 #include <sys/module.h> 55 #include <sys/bus.h> 56 #endif 57 #include <sys/proc.h> 58 59 #include <machine/bus.h> 60 61 #include <dev/usb/usb.h> 62 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 #include <dev/usb/usbdivar.h> 66 #include <dev/usb/usbdevs.h> 67 #include <dev/usb/usb_quirks.h> 68 69 #if defined(__FreeBSD__) 70 #include <machine/clock.h> 71 #define delay(d) DELAY(d) 72 #endif 73 74 #ifdef USB_DEBUG 75 #define DPRINTF(x) if (usbdebug) logprintf x 76 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x 77 extern int usbdebug; 78 #else 79 #define DPRINTF(x) 80 #define DPRINTFN(n,x) 81 #endif 82 83 Static usbd_status usbd_set_config(usbd_device_handle, int); 84 Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *, 85 size_t, int); 86 Static int usbd_getnewaddr(usbd_bus_handle bus); 87 #if defined(__NetBSD__) 88 Static int usbd_print(void *, const char *); 89 Static int usbd_submatch(device_ptr_t, struct cfdata *, 90 const locdesc_t *, void *); 91 #elif defined(__OpenBSD__) 92 Static int usbd_print(void *aux, const char *pnp); 93 Static int usbd_submatch(device_ptr_t, void *, void *); 94 #endif 95 Static void usbd_free_iface_data(usbd_device_handle dev, int ifcno); 96 Static void usbd_kill_pipe(usbd_pipe_handle); 97 Static usbd_status usbd_probe_and_attach(device_ptr_t parent, 98 usbd_device_handle dev, int port, int addr); 99 100 Static u_int32_t usb_cookie_no = 0; 101 102 #ifdef USBVERBOSE 103 typedef u_int16_t usb_vendor_id_t; 104 typedef u_int16_t usb_product_id_t; 105 106 /* 107 * Descriptions of of known vendors and devices ("products"). 108 */ 109 struct usb_knowndev { 110 usb_vendor_id_t vendor; 111 usb_product_id_t product; 112 int flags; 113 char *vendorname, *productname; 114 }; 115 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 116 117 #include <dev/usb/usbdevs_data.h> 118 #endif /* USBVERBOSE */ 119 120 Static const char * const usbd_error_strs[] = { 121 "NORMAL_COMPLETION", 122 "IN_PROGRESS", 123 "PENDING_REQUESTS", 124 "NOT_STARTED", 125 "INVAL", 126 "NOMEM", 127 "CANCELLED", 128 "BAD_ADDRESS", 129 "IN_USE", 130 "NO_ADDR", 131 "SET_ADDR_FAILED", 132 "NO_POWER", 133 "TOO_DEEP", 134 "IOERROR", 135 "NOT_CONFIGURED", 136 "TIMEOUT", 137 "SHORT_XFER", 138 "STALLED", 139 "INTERRUPTED", 140 "XXX", 141 }; 142 143 const char * 144 usbd_errstr(usbd_status err) 145 { 146 static char buffer[5]; 147 148 if (err < USBD_ERROR_MAX) { 149 return usbd_error_strs[err]; 150 } else { 151 snprintf(buffer, sizeof buffer, "%d", err); 152 return buffer; 153 } 154 } 155 156 usbd_status 157 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid, 158 usb_string_descriptor_t *sdesc, int *sizep) 159 { 160 usb_device_request_t req; 161 usbd_status err; 162 int actlen; 163 164 req.bmRequestType = UT_READ_DEVICE; 165 req.bRequest = UR_GET_DESCRIPTOR; 166 USETW2(req.wValue, UDESC_STRING, sindex); 167 USETW(req.wIndex, langid); 168 USETW(req.wLength, 2); /* only size byte first */ 169 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 170 &actlen, USBD_DEFAULT_TIMEOUT); 171 if (err) 172 return (err); 173 174 if (actlen < 2) 175 return (USBD_SHORT_XFER); 176 177 USETW(req.wLength, sdesc->bLength); /* the whole string */ 178 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 179 &actlen, USBD_DEFAULT_TIMEOUT); 180 if (err) 181 return (err); 182 183 if (actlen != sdesc->bLength) { 184 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n", 185 sdesc->bLength, actlen)); 186 } 187 188 *sizep = actlen; 189 return (USBD_NORMAL_COMPLETION); 190 } 191 192 static void 193 usbd_trim_spaces(char *p) 194 { 195 char *q, *e; 196 197 if (p == NULL) 198 return; 199 q = e = p; 200 while (*q == ' ') /* skip leading spaces */ 201 q++; 202 while ((*p = *q++)) /* copy string */ 203 if (*p++ != ' ') /* remember last non-space */ 204 e = p; 205 *e = 0; /* kill trailing spaces */ 206 } 207 208 void 209 usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t lv, char *p, size_t lp, 210 int usedev) 211 { 212 usb_device_descriptor_t *udd = &dev->ddesc; 213 char *vendor = NULL, *product = NULL; 214 #ifdef USBVERBOSE 215 const struct usb_knowndev *kdp; 216 #endif 217 218 if (dev == NULL) { 219 v[0] = p[0] = '\0'; 220 return; 221 } 222 223 if (usedev) { 224 if (usbd_get_string(dev, udd->iManufacturer, v)) 225 vendor = NULL; 226 else 227 vendor = v; 228 usbd_trim_spaces(vendor); 229 if (usbd_get_string(dev, udd->iProduct, p)) 230 product = NULL; 231 else 232 product = p; 233 usbd_trim_spaces(product); 234 if (vendor && !*vendor) 235 vendor = NULL; 236 if (product && !*product) 237 product = NULL; 238 } else { 239 vendor = NULL; 240 product = NULL; 241 } 242 #ifdef USBVERBOSE 243 if (vendor == NULL || product == NULL) { 244 for(kdp = usb_knowndevs; 245 kdp->vendorname != NULL; 246 kdp++) { 247 if (kdp->vendor == UGETW(udd->idVendor) && 248 (kdp->product == UGETW(udd->idProduct) || 249 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 250 break; 251 } 252 if (kdp->vendorname != NULL) { 253 if (vendor == NULL) 254 vendor = kdp->vendorname; 255 if (product == NULL) 256 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ? 257 kdp->productname : NULL; 258 } 259 } 260 #endif 261 if (vendor != NULL && *vendor) 262 strlcpy(v, vendor, lv); 263 else 264 snprintf(v, lv, "vendor 0x%04x", UGETW(udd->idVendor)); 265 if (product != NULL && *product) 266 strlcpy(p, product, lp); 267 else 268 snprintf(p, lp, "product 0x%04x", UGETW(udd->idProduct)); 269 } 270 271 int 272 usbd_printBCD(char *cp, size_t l, int bcd) 273 { 274 return (snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff)); 275 } 276 277 void 278 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l) 279 { 280 usb_device_descriptor_t *udd = &dev->ddesc; 281 char vendor[USB_MAX_STRING_LEN]; 282 char product[USB_MAX_STRING_LEN]; 283 int bcdDevice, bcdUSB; 284 char *ep; 285 286 ep = cp + l; 287 288 usbd_devinfo_vp(dev, vendor, sizeof(vendor), product, 289 sizeof(product), 1); 290 cp += snprintf(cp, ep - cp, "%s %s", vendor, product); 291 if (showclass) 292 cp += snprintf(cp, ep - cp, ", class %d/%d", 293 udd->bDeviceClass, udd->bDeviceSubClass); 294 bcdUSB = UGETW(udd->bcdUSB); 295 bcdDevice = UGETW(udd->bcdDevice); 296 cp += snprintf(cp, ep - cp, ", rev "); 297 cp += usbd_printBCD(cp, ep - cp, bcdUSB); 298 *cp++ = '/'; 299 cp += usbd_printBCD(cp, ep - cp, bcdDevice); 300 cp += snprintf(cp, ep - cp, ", addr %d", dev->address); 301 *cp = 0; 302 } 303 304 /* Delay for a certain number of ms */ 305 void 306 usb_delay_ms(usbd_bus_handle bus, u_int ms) 307 { 308 /* Wait at least two clock ticks so we know the time has passed. */ 309 if (bus->use_polling || cold) 310 delay((ms+1) * 1000); 311 else 312 tsleep(&ms, PRIBIO, "usbdly", (ms*hz+999)/1000 + 1); 313 } 314 315 /* Delay given a device handle. */ 316 void 317 usbd_delay_ms(usbd_device_handle dev, u_int ms) 318 { 319 usb_delay_ms(dev->bus, ms); 320 } 321 322 usbd_status 323 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps) 324 { 325 usb_device_request_t req; 326 usbd_status err; 327 int n; 328 329 req.bmRequestType = UT_WRITE_CLASS_OTHER; 330 req.bRequest = UR_SET_FEATURE; 331 USETW(req.wValue, UHF_PORT_RESET); 332 USETW(req.wIndex, port); 333 USETW(req.wLength, 0); 334 err = usbd_do_request(dev, &req, 0); 335 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n", 336 port, usbd_errstr(err))); 337 if (err) 338 return (err); 339 n = 10; 340 do { 341 /* Wait for device to recover from reset. */ 342 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 343 err = usbd_get_port_status(dev, port, ps); 344 if (err) { 345 DPRINTF(("usbd_reset_port: get status failed %d\n", 346 err)); 347 return (err); 348 } 349 /* If the device disappeared, just give up. */ 350 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 351 return (USBD_NORMAL_COMPLETION); 352 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 353 if (n == 0) 354 return (USBD_TIMEOUT); 355 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET); 356 #ifdef USB_DEBUG 357 if (err) 358 DPRINTF(("usbd_reset_port: clear port feature failed %d\n", 359 err)); 360 #endif 361 362 /* Wait for the device to recover from reset. */ 363 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY); 364 return (err); 365 } 366 367 usb_interface_descriptor_t * 368 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx) 369 { 370 char *p = (char *)cd; 371 char *end = p + UGETW(cd->wTotalLength); 372 usb_interface_descriptor_t *d; 373 int curidx, lastidx, curaidx = 0; 374 375 for (curidx = lastidx = -1; p < end; ) { 376 d = (usb_interface_descriptor_t *)p; 377 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d " 378 "type=%d\n", 379 ifaceidx, curidx, altidx, curaidx, 380 d->bLength, d->bDescriptorType)); 381 if (d->bLength == 0) /* bad descriptor */ 382 break; 383 p += d->bLength; 384 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) { 385 if (d->bInterfaceNumber != lastidx) { 386 lastidx = d->bInterfaceNumber; 387 curidx++; 388 curaidx = 0; 389 } else 390 curaidx++; 391 if (ifaceidx == curidx && altidx == curaidx) 392 return (d); 393 } 394 } 395 return (NULL); 396 } 397 398 usb_endpoint_descriptor_t * 399 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx, 400 int endptidx) 401 { 402 char *p = (char *)cd; 403 char *end = p + UGETW(cd->wTotalLength); 404 usb_interface_descriptor_t *d; 405 usb_endpoint_descriptor_t *e; 406 int curidx; 407 408 d = usbd_find_idesc(cd, ifaceidx, altidx); 409 if (d == NULL) 410 return (NULL); 411 if (endptidx >= d->bNumEndpoints) /* quick exit */ 412 return (NULL); 413 414 curidx = -1; 415 for (p = (char *)d + d->bLength; p < end; ) { 416 e = (usb_endpoint_descriptor_t *)p; 417 if (e->bLength == 0) /* bad descriptor */ 418 break; 419 p += e->bLength; 420 if (p <= end && e->bDescriptorType == UDESC_INTERFACE) 421 return (NULL); 422 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) { 423 curidx++; 424 if (curidx == endptidx) 425 return (e); 426 } 427 } 428 return (NULL); 429 } 430 431 usbd_status 432 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx) 433 { 434 usbd_interface_handle ifc = &dev->ifaces[ifaceidx]; 435 usb_interface_descriptor_t *idesc; 436 char *p, *end; 437 int endpt, nendpt; 438 439 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n", 440 ifaceidx, altidx)); 441 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx); 442 if (idesc == NULL) 443 return (USBD_INVAL); 444 ifc->device = dev; 445 ifc->idesc = idesc; 446 ifc->index = ifaceidx; 447 ifc->altindex = altidx; 448 nendpt = ifc->idesc->bNumEndpoints; 449 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt)); 450 if (nendpt != 0) { 451 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint), 452 M_USB, M_NOWAIT); 453 if (ifc->endpoints == NULL) 454 return (USBD_NOMEM); 455 } else 456 ifc->endpoints = NULL; 457 ifc->priv = NULL; 458 p = (char *)ifc->idesc + ifc->idesc->bLength; 459 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength); 460 #define ed ((usb_endpoint_descriptor_t *)p) 461 for (endpt = 0; endpt < nendpt; endpt++) { 462 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt)); 463 for (; p < end; p += ed->bLength) { 464 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p " 465 "len=%d type=%d\n", 466 p, end, ed->bLength, ed->bDescriptorType)); 467 if (p + ed->bLength <= end && ed->bLength != 0 && 468 ed->bDescriptorType == UDESC_ENDPOINT) 469 goto found; 470 if (ed->bLength == 0 || 471 ed->bDescriptorType == UDESC_INTERFACE) 472 break; 473 } 474 /* passed end, or bad desc */ 475 printf("usbd_fill_iface_data: bad descriptor(s): %s\n", 476 ed->bLength == 0 ? "0 length" : 477 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc": 478 "out of data"); 479 goto bad; 480 found: 481 ifc->endpoints[endpt].edesc = ed; 482 if (dev->speed == USB_SPEED_HIGH) { 483 u_int mps; 484 /* Control and bulk endpoints have max packet limits. */ 485 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 486 case UE_CONTROL: 487 mps = USB_2_MAX_CTRL_PACKET; 488 goto check; 489 case UE_BULK: 490 mps = USB_2_MAX_BULK_PACKET; 491 check: 492 if (UGETW(ed->wMaxPacketSize) != mps) { 493 USETW(ed->wMaxPacketSize, mps); 494 #ifdef DIAGNOSTIC 495 printf("usbd_fill_iface_data: bad max " 496 "packet size\n"); 497 #endif 498 } 499 break; 500 default: 501 break; 502 } 503 } 504 ifc->endpoints[endpt].refcnt = 0; 505 p += ed->bLength; 506 } 507 #undef ed 508 LIST_INIT(&ifc->pipes); 509 return (USBD_NORMAL_COMPLETION); 510 511 bad: 512 if (ifc->endpoints != NULL) { 513 free(ifc->endpoints, M_USB); 514 ifc->endpoints = NULL; 515 } 516 return (USBD_INVAL); 517 } 518 519 void 520 usbd_free_iface_data(usbd_device_handle dev, int ifcno) 521 { 522 usbd_interface_handle ifc = &dev->ifaces[ifcno]; 523 if (ifc->endpoints) 524 free(ifc->endpoints, M_USB); 525 } 526 527 Static usbd_status 528 usbd_set_config(usbd_device_handle dev, int conf) 529 { 530 usb_device_request_t req; 531 532 req.bmRequestType = UT_WRITE_DEVICE; 533 req.bRequest = UR_SET_CONFIG; 534 USETW(req.wValue, conf); 535 USETW(req.wIndex, 0); 536 USETW(req.wLength, 0); 537 return (usbd_do_request(dev, &req, 0)); 538 } 539 540 usbd_status 541 usbd_set_config_no(usbd_device_handle dev, int no, int msg) 542 { 543 int index; 544 usb_config_descriptor_t cd; 545 usbd_status err; 546 547 if (no == USB_UNCONFIG_NO) 548 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg)); 549 550 DPRINTFN(5,("usbd_set_config_no: %d\n", no)); 551 /* Figure out what config index to use. */ 552 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) { 553 err = usbd_get_config_desc(dev, index, &cd); 554 if (err) 555 return (err); 556 if (cd.bConfigurationValue == no) 557 return (usbd_set_config_index(dev, index, msg)); 558 } 559 return (USBD_INVAL); 560 } 561 562 usbd_status 563 usbd_set_config_index(usbd_device_handle dev, int index, int msg) 564 { 565 usb_status_t ds; 566 usb_config_descriptor_t cd, *cdp; 567 usbd_status err; 568 int i, ifcidx, nifc, len, selfpowered, power; 569 570 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index)); 571 572 /* XXX check that all interfaces are idle */ 573 if (dev->config != USB_UNCONFIG_NO) { 574 DPRINTF(("usbd_set_config_index: free old config\n")); 575 /* Free all configuration data structures. */ 576 nifc = dev->cdesc->bNumInterface; 577 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 578 usbd_free_iface_data(dev, ifcidx); 579 free(dev->ifaces, M_USB); 580 free(dev->cdesc, M_USB); 581 dev->ifaces = NULL; 582 dev->cdesc = NULL; 583 dev->config = USB_UNCONFIG_NO; 584 } 585 586 if (index == USB_UNCONFIG_INDEX) { 587 /* We are unconfiguring the device, so leave unallocated. */ 588 DPRINTF(("usbd_set_config_index: set config 0\n")); 589 err = usbd_set_config(dev, USB_UNCONFIG_NO); 590 if (err) 591 DPRINTF(("usbd_set_config_index: setting config=0 " 592 "failed, error=%s\n", usbd_errstr(err))); 593 return (err); 594 } 595 596 /* Get the short descriptor. */ 597 err = usbd_get_config_desc(dev, index, &cd); 598 if (err) 599 return (err); 600 len = UGETW(cd.wTotalLength); 601 cdp = malloc(len, M_USB, M_NOWAIT); 602 if (cdp == NULL) 603 return (USBD_NOMEM); 604 605 /* Get the full descriptor. Try a few times for slow devices. */ 606 for (i = 0; i < 3; i++) { 607 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp); 608 if (!err) 609 break; 610 usbd_delay_ms(dev, 200); 611 } 612 if (err) 613 goto bad; 614 615 if (cdp->bDescriptorType != UDESC_CONFIG) { 616 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n", 617 cdp->bDescriptorType)); 618 err = USBD_INVAL; 619 goto bad; 620 } 621 622 /* Figure out if the device is self or bus powered. */ 623 selfpowered = 0; 624 if (!(dev->quirks->uq_flags & UQ_BUS_POWERED) && 625 (cdp->bmAttributes & UC_SELF_POWERED)) { 626 /* May be self powered. */ 627 if (cdp->bmAttributes & UC_BUS_POWERED) { 628 /* Must ask device. */ 629 if (dev->quirks->uq_flags & UQ_POWER_CLAIM) { 630 /* 631 * Hub claims to be self powered, but isn't. 632 * It seems that the power status can be 633 * determined by the hub characteristics. 634 */ 635 usb_hub_descriptor_t hd; 636 usb_device_request_t req; 637 req.bmRequestType = UT_READ_CLASS_DEVICE; 638 req.bRequest = UR_GET_DESCRIPTOR; 639 USETW(req.wValue, 0); 640 USETW(req.wIndex, 0); 641 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE); 642 err = usbd_do_request(dev, &req, &hd); 643 if (!err && 644 (UGETW(hd.wHubCharacteristics) & 645 UHD_PWR_INDIVIDUAL)) 646 selfpowered = 1; 647 DPRINTF(("usbd_set_config_index: charac=0x%04x" 648 ", error=%s\n", 649 UGETW(hd.wHubCharacteristics), 650 usbd_errstr(err))); 651 } else { 652 err = usbd_get_device_status(dev, &ds); 653 if (!err && 654 (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 655 selfpowered = 1; 656 DPRINTF(("usbd_set_config_index: status=0x%04x" 657 ", error=%s\n", 658 UGETW(ds.wStatus), usbd_errstr(err))); 659 } 660 } else 661 selfpowered = 1; 662 } 663 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, " 664 "selfpowered=%d, power=%d\n", 665 cdp->bConfigurationValue, dev->address, cdp->bmAttributes, 666 selfpowered, cdp->bMaxPower * 2)); 667 668 /* Check if we have enough power. */ 669 #ifdef USB_DEBUG 670 if (dev->powersrc == NULL) { 671 DPRINTF(("usbd_set_config_index: No power source?\n")); 672 return (USBD_IOERROR); 673 } 674 #endif 675 power = cdp->bMaxPower * 2; 676 if (power > dev->powersrc->power) { 677 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power)); 678 /* XXX print nicer message. */ 679 if (msg) 680 printf("%s: device addr %d (config %d) exceeds power " 681 "budget, %d mA > %d mA\n", 682 USBDEVNAME(dev->bus->bdev), dev->address, 683 cdp->bConfigurationValue, 684 power, dev->powersrc->power); 685 err = USBD_NO_POWER; 686 goto bad; 687 } 688 dev->power = power; 689 dev->self_powered = selfpowered; 690 691 /* Set the actual configuration value. */ 692 DPRINTF(("usbd_set_config_index: set config %d\n", 693 cdp->bConfigurationValue)); 694 err = usbd_set_config(dev, cdp->bConfigurationValue); 695 if (err) { 696 DPRINTF(("usbd_set_config_index: setting config=%d failed, " 697 "error=%s\n", 698 cdp->bConfigurationValue, usbd_errstr(err))); 699 goto bad; 700 } 701 702 /* Allocate and fill interface data. */ 703 nifc = cdp->bNumInterface; 704 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface), 705 M_USB, M_NOWAIT); 706 if (dev->ifaces == NULL) { 707 err = USBD_NOMEM; 708 goto bad; 709 } 710 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp)); 711 dev->cdesc = cdp; 712 dev->config = cdp->bConfigurationValue; 713 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 714 err = usbd_fill_iface_data(dev, ifcidx, 0); 715 if (err) { 716 while (--ifcidx >= 0) 717 usbd_free_iface_data(dev, ifcidx); 718 goto bad; 719 } 720 } 721 722 return (USBD_NORMAL_COMPLETION); 723 724 bad: 725 free(cdp, M_USB); 726 return (err); 727 } 728 729 /* XXX add function for alternate settings */ 730 731 usbd_status 732 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface, 733 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe) 734 { 735 usbd_pipe_handle p; 736 usbd_status err; 737 738 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n", 739 dev, iface, ep, pipe)); 740 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT); 741 if (p == NULL) 742 return (USBD_NOMEM); 743 p->device = dev; 744 p->iface = iface; 745 p->endpoint = ep; 746 ep->refcnt++; 747 p->refcnt = 1; 748 p->intrxfer = 0; 749 p->running = 0; 750 p->aborting = 0; 751 p->repeat = 0; 752 p->interval = ival; 753 SIMPLEQ_INIT(&p->queue); 754 err = dev->bus->methods->open_pipe(p); 755 if (err) { 756 DPRINTFN(-1,("usbd_setup_pipe: endpoint=0x%x failed, error=" 757 "%s\n", 758 ep->edesc->bEndpointAddress, usbd_errstr(err))); 759 free(p, M_USB); 760 return (err); 761 } 762 /* Clear any stall and make sure DATA0 toggle will be used next. */ 763 if (UE_GET_ADDR(ep->edesc->bEndpointAddress) != USB_CONTROL_ENDPOINT) { 764 err = usbd_clear_endpoint_stall(p); 765 /* Some devices reject this command, so ignore a STALL. */ 766 if (err && err != USBD_STALLED) { 767 printf("usbd_setup_pipe: failed to start endpoint, %s\n", usbd_errstr(err)); 768 return (err); 769 } 770 } 771 *pipe = p; 772 return (USBD_NORMAL_COMPLETION); 773 } 774 775 /* Abort the device control pipe. */ 776 void 777 usbd_kill_pipe(usbd_pipe_handle pipe) 778 { 779 usbd_abort_pipe(pipe); 780 pipe->methods->close(pipe); 781 pipe->endpoint->refcnt--; 782 free(pipe, M_USB); 783 } 784 785 int 786 usbd_getnewaddr(usbd_bus_handle bus) 787 { 788 int addr; 789 790 for (addr = 1; addr < USB_MAX_DEVICES; addr++) 791 if (bus->devices[addr] == 0) 792 return (addr); 793 return (-1); 794 } 795 796 797 usbd_status 798 usbd_probe_and_attach(device_ptr_t parent, usbd_device_handle dev, 799 int port, int addr) 800 { 801 struct usb_attach_arg uaa; 802 usb_device_descriptor_t *dd = &dev->ddesc; 803 int found, i, confi, nifaces; 804 usbd_status err; 805 device_ptr_t dv; 806 usbd_interface_handle ifaces[256]; /* 256 is the absolute max */ 807 808 #if defined(__FreeBSD__) 809 /* 810 * XXX uaa is a static var. Not a problem as it _should_ be used only 811 * during probe and attach. Should be changed however. 812 */ 813 device_t bdev; 814 bdev = device_add_child(parent, NULL, -1, &uaa); 815 if (!bdev) { 816 printf("%s: Device creation failed\n", USBDEVNAME(dev->bus->bdev)); 817 return (USBD_INVAL); 818 } 819 device_quiet(bdev); 820 #endif 821 822 uaa.device = dev; 823 uaa.iface = NULL; 824 uaa.ifaces = NULL; 825 uaa.nifaces = 0; 826 uaa.usegeneric = 0; 827 uaa.port = port; 828 uaa.configno = UHUB_UNK_CONFIGURATION; 829 uaa.ifaceno = UHUB_UNK_INTERFACE; 830 uaa.vendor = UGETW(dd->idVendor); 831 uaa.product = UGETW(dd->idProduct); 832 uaa.release = UGETW(dd->bcdDevice); 833 834 /* First try with device specific drivers. */ 835 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n")); 836 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch); 837 if (dv) { 838 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT); 839 if (dev->subdevs == NULL) 840 return (USBD_NOMEM); 841 dev->subdevs[0] = dv; 842 dev->subdevs[1] = 0; 843 return (USBD_NORMAL_COMPLETION); 844 } 845 846 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n")); 847 848 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n", 849 dd->bNumConfigurations)); 850 /* Next try with interface drivers. */ 851 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 852 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n", 853 confi)); 854 err = usbd_set_config_index(dev, confi, 1); 855 if (err) { 856 #ifdef USB_DEBUG 857 DPRINTF(("%s: port %d, set config at addr %d failed, " 858 "error=%s\n", USBDEVPTRNAME(parent), port, 859 addr, usbd_errstr(err))); 860 #else 861 printf("%s: port %d, set config at addr %d failed\n", 862 USBDEVPTRNAME(parent), port, addr); 863 #endif 864 #if defined(__FreeBSD__) 865 device_delete_child(parent, bdev); 866 #endif 867 868 return (err); 869 } 870 nifaces = dev->cdesc->bNumInterface; 871 uaa.configno = dev->cdesc->bConfigurationValue; 872 for (i = 0; i < nifaces; i++) 873 ifaces[i] = &dev->ifaces[i]; 874 uaa.ifaces = ifaces; 875 uaa.nifaces = nifaces; 876 dev->subdevs = malloc((nifaces+1) * sizeof dv, M_USB,M_NOWAIT); 877 if (dev->subdevs == NULL) { 878 #if defined(__FreeBSD__) 879 device_delete_child(parent, bdev); 880 #endif 881 return (USBD_NOMEM); 882 } 883 884 found = 0; 885 for (i = 0; i < nifaces; i++) { 886 if (ifaces[i] == NULL) 887 continue; /* interface already claimed */ 888 uaa.iface = ifaces[i]; 889 uaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber; 890 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, 891 usbd_submatch); 892 if (dv != NULL) { 893 dev->subdevs[found++] = dv; 894 dev->subdevs[found] = 0; 895 ifaces[i] = 0; /* consumed */ 896 897 #if defined(__FreeBSD__) 898 /* create another child for the next iface */ 899 bdev = device_add_child(parent, NULL, -1,&uaa); 900 if (!bdev) { 901 printf("%s: Device creation failed\n", 902 USBDEVNAME(dev->bus->bdev)); 903 return (USBD_NORMAL_COMPLETION); 904 } 905 device_quiet(bdev); 906 #endif 907 } 908 } 909 if (found != 0) { 910 #if defined(__FreeBSD__) 911 /* remove the last created child again; it is unused */ 912 device_delete_child(parent, bdev); 913 #endif 914 return (USBD_NORMAL_COMPLETION); 915 } 916 free(dev->subdevs, M_USB); 917 dev->subdevs = 0; 918 } 919 /* No interfaces were attached in any of the configurations. */ 920 921 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 922 usbd_set_config_index(dev, 0, 0); 923 924 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n")); 925 926 /* Finally try the generic driver. */ 927 uaa.iface = NULL; 928 uaa.usegeneric = 1; 929 uaa.configno = UHUB_UNK_CONFIGURATION; 930 uaa.ifaceno = UHUB_UNK_INTERFACE; 931 dv = USB_DO_ATTACH(dev, bdev, parent, &uaa, usbd_print, usbd_submatch); 932 if (dv != NULL) { 933 dev->subdevs = malloc(2 * sizeof dv, M_USB, M_NOWAIT); 934 if (dev->subdevs == 0) 935 return (USBD_NOMEM); 936 dev->subdevs[0] = dv; 937 dev->subdevs[1] = 0; 938 return (USBD_NORMAL_COMPLETION); 939 } 940 941 /* 942 * The generic attach failed, but leave the device as it is. 943 * We just did not find any drivers, that's all. The device is 944 * fully operational and not harming anyone. 945 */ 946 DPRINTF(("usbd_probe_and_attach: generic attach failed\n")); 947 #if defined(__FreeBSD__) 948 device_delete_child(parent, bdev); 949 #endif 950 return (USBD_NORMAL_COMPLETION); 951 } 952 953 954 /* 955 * Called when a new device has been put in the powered state, 956 * but not yet in the addressed state. 957 * Get initial descriptor, set the address, get full descriptor, 958 * and attach a driver. 959 */ 960 usbd_status 961 usbd_new_device(device_ptr_t parent, usbd_bus_handle bus, int depth, 962 int speed, int port, struct usbd_port *up) 963 { 964 usbd_device_handle dev, adev; 965 struct usbd_device *hub; 966 usb_device_descriptor_t *dd; 967 usb_port_status_t ps; 968 usbd_status err; 969 int addr; 970 int i; 971 int p; 972 973 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n", 974 bus, port, depth, speed)); 975 addr = usbd_getnewaddr(bus); 976 if (addr < 0) { 977 printf("%s: No free USB addresses, new device ignored.\n", 978 USBDEVNAME(bus->bdev)); 979 return (USBD_NO_ADDR); 980 } 981 982 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO); 983 if (dev == NULL) 984 return (USBD_NOMEM); 985 986 dev->bus = bus; 987 988 /* Set up default endpoint handle. */ 989 dev->def_ep.edesc = &dev->def_ep_desc; 990 991 /* Set up default endpoint descriptor. */ 992 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 993 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 994 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 995 dev->def_ep_desc.bmAttributes = UE_CONTROL; 996 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 997 dev->def_ep_desc.bInterval = 0; 998 999 dev->quirks = &usbd_no_quirk; 1000 dev->address = USB_START_ADDR; 1001 dev->ddesc.bMaxPacketSize = 0; 1002 dev->depth = depth; 1003 dev->powersrc = up; 1004 dev->myhub = up->parent; 1005 1006 up->device = dev; 1007 1008 /* Locate port on upstream high speed hub */ 1009 for (adev = dev, hub = up->parent; 1010 hub != NULL && hub->speed != USB_SPEED_HIGH; 1011 adev = hub, hub = hub->myhub) 1012 ; 1013 if (hub) { 1014 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) { 1015 if (hub->hub->ports[p].device == adev) { 1016 dev->myhsport = &hub->hub->ports[p]; 1017 goto found; 1018 } 1019 } 1020 panic("usbd_new_device: cannot find HS port\n"); 1021 found: 1022 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p)); 1023 } else { 1024 dev->myhsport = NULL; 1025 } 1026 dev->speed = speed; 1027 dev->langid = USBD_NOLANG; 1028 dev->cookie.cookie = ++usb_cookie_no; 1029 1030 /* Establish the default pipe. */ 1031 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1032 &dev->default_pipe); 1033 if (err) { 1034 usbd_remove_device(dev, up); 1035 return (err); 1036 } 1037 1038 /* Set the address. Do this early; some devices need that. */ 1039 err = usbd_set_address(dev, addr); 1040 DPRINTFN(5,("usbd_new_device: setting device address=%d\n", addr)); 1041 if (err) { 1042 DPRINTFN(-1,("usb_new_device: set address %d failed\n", addr)); 1043 err = USBD_SET_ADDR_FAILED; 1044 usbd_remove_device(dev, up); 1045 return (err); 1046 } 1047 /* Allow device time to set new address */ 1048 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 1049 dev->address = addr; /* New device address now */ 1050 bus->devices[addr] = dev; 1051 1052 dd = &dev->ddesc; 1053 /* Try a few times in case the device is slow (i.e. outside specs.) */ 1054 for (i = 0; i < 10; i++) { 1055 /* Get the first 8 bytes of the device descriptor. */ 1056 err = usbd_get_desc(dev, UDESC_DEVICE, 0, USB_MAX_IPACKET, dd); 1057 if (!err) 1058 break; 1059 usbd_delay_ms(dev, 200); 1060 if ((i & 3) == 3) 1061 usbd_reset_port(up->parent, port, &ps); 1062 } 1063 if (err) { 1064 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc " 1065 "failed\n", addr)); 1066 usbd_remove_device(dev, up); 1067 return (err); 1068 } 1069 1070 if (speed == USB_SPEED_HIGH) { 1071 /* Max packet size must be 64 (sec 5.5.3). */ 1072 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { 1073 #ifdef DIAGNOSTIC 1074 printf("usbd_new_device: addr=%d bad max packet size\n", 1075 addr); 1076 #endif 1077 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; 1078 } 1079 } 1080 1081 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, " 1082 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1083 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, 1084 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, 1085 dev->speed)); 1086 1087 if (dd->bDescriptorType != UDESC_DEVICE) { 1088 /* Illegal device descriptor */ 1089 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n", 1090 dd->bDescriptorType)); 1091 usbd_remove_device(dev, up); 1092 return (USBD_INVAL); 1093 } 1094 1095 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { 1096 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength)); 1097 usbd_remove_device(dev, up); 1098 return (USBD_INVAL); 1099 } 1100 1101 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize); 1102 1103 err = usbd_reload_device_desc(dev); 1104 if (err) { 1105 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc " 1106 "failed\n", addr)); 1107 usbd_remove_device(dev, up); 1108 return (err); 1109 } 1110 1111 /* Assume 100mA bus powered for now. Changed when configured. */ 1112 dev->power = USB_MIN_POWER; 1113 dev->self_powered = 0; 1114 1115 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n", 1116 addr, dev, parent)); 1117 1118 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1119 1120 err = usbd_probe_and_attach(parent, dev, port, addr); 1121 if (err) { 1122 usbd_remove_device(dev, up); 1123 return (err); 1124 } 1125 1126 return (USBD_NORMAL_COMPLETION); 1127 } 1128 1129 usbd_status 1130 usbd_reload_device_desc(usbd_device_handle dev) 1131 { 1132 usbd_status err; 1133 1134 /* Get the full device descriptor. */ 1135 err = usbd_get_device_desc(dev, &dev->ddesc); 1136 if (err) 1137 return (err); 1138 1139 /* Figure out what's wrong with this device. */ 1140 dev->quirks = usbd_find_quirk(&dev->ddesc); 1141 1142 return (USBD_NORMAL_COMPLETION); 1143 } 1144 1145 void 1146 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up) 1147 { 1148 DPRINTF(("usbd_remove_device: %p\n", dev)); 1149 1150 if (dev->default_pipe != NULL) 1151 usbd_kill_pipe(dev->default_pipe); 1152 up->device = NULL; 1153 dev->bus->devices[dev->address] = NULL; 1154 1155 free(dev, M_USB); 1156 } 1157 1158 #if defined(__NetBSD__) || defined(__OpenBSD__) 1159 int 1160 usbd_print(void *aux, const char *pnp) 1161 { 1162 struct usb_attach_arg *uaa = aux; 1163 char devinfo[1024]; 1164 1165 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1166 if (pnp) { 1167 if (!uaa->usegeneric) 1168 return (QUIET); 1169 usbd_devinfo(uaa->device, 1, devinfo, sizeof(devinfo)); 1170 aprint_normal("%s, %s", devinfo, pnp); 1171 } 1172 if (uaa->port != 0) 1173 aprint_normal(" port %d", uaa->port); 1174 if (uaa->configno != UHUB_UNK_CONFIGURATION) 1175 aprint_normal(" configuration %d", uaa->configno); 1176 if (uaa->ifaceno != UHUB_UNK_INTERFACE) 1177 aprint_normal(" interface %d", uaa->ifaceno); 1178 #if 0 1179 /* 1180 * It gets very crowded with these locators on the attach line. 1181 * They are not really needed since they are printed in the clear 1182 * by each driver. 1183 */ 1184 if (uaa->vendor != UHUB_UNK_VENDOR) 1185 aprint_normal(" vendor 0x%04x", uaa->vendor); 1186 if (uaa->product != UHUB_UNK_PRODUCT) 1187 aprint_normal(" product 0x%04x", uaa->product); 1188 if (uaa->release != UHUB_UNK_RELEASE) 1189 aprint_normal(" release 0x%04x", uaa->release); 1190 #endif 1191 return (UNCONF); 1192 } 1193 1194 #if defined(__NetBSD__) 1195 int 1196 usbd_submatch(struct device *parent, struct cfdata *cf, 1197 const locdesc_t *ldesc, void *aux) 1198 { 1199 #elif defined(__OpenBSD__) 1200 int 1201 usbd_submatch(struct device *parent, void *match, void *aux) 1202 { 1203 struct cfdata *cf = match; 1204 #endif 1205 struct usb_attach_arg *uaa = aux; 1206 1207 DPRINTFN(5,("usbd_submatch port=%d,%d configno=%d,%d " 1208 "ifaceno=%d,%d vendor=%d,%d product=%d,%d release=%d,%d\n", 1209 uaa->port, cf->uhubcf_port, 1210 uaa->configno, cf->uhubcf_configuration, 1211 uaa->ifaceno, cf->uhubcf_interface, 1212 uaa->vendor, cf->uhubcf_vendor, 1213 uaa->product, cf->uhubcf_product, 1214 uaa->release, cf->uhubcf_release)); 1215 if (uaa->port != 0 && /* root hub has port 0, it should match */ 1216 ((cf->uhubcf_port != UHUB_UNK_PORT && 1217 cf->uhubcf_port != uaa->port) || 1218 (uaa->configno != UHUB_UNK_CONFIGURATION && 1219 cf->uhubcf_configuration != UHUB_UNK_CONFIGURATION && 1220 cf->uhubcf_configuration != uaa->configno) || 1221 (uaa->ifaceno != UHUB_UNK_INTERFACE && 1222 cf->uhubcf_interface != UHUB_UNK_INTERFACE && 1223 cf->uhubcf_interface != uaa->ifaceno) || 1224 (uaa->vendor != UHUB_UNK_VENDOR && 1225 cf->uhubcf_vendor != UHUB_UNK_VENDOR && 1226 cf->uhubcf_vendor != uaa->vendor) || 1227 (uaa->product != UHUB_UNK_PRODUCT && 1228 cf->uhubcf_product != UHUB_UNK_PRODUCT && 1229 cf->uhubcf_product != uaa->product) || 1230 (uaa->release != UHUB_UNK_RELEASE && 1231 cf->uhubcf_release != UHUB_UNK_RELEASE && 1232 cf->uhubcf_release != uaa->release) 1233 ) 1234 ) 1235 return 0; 1236 if (cf->uhubcf_vendor != UHUB_UNK_VENDOR && 1237 cf->uhubcf_vendor == uaa->vendor && 1238 cf->uhubcf_product != UHUB_UNK_PRODUCT && 1239 cf->uhubcf_product == uaa->product) { 1240 /* We have a vendor&product locator match */ 1241 if (cf->uhubcf_release != UHUB_UNK_RELEASE && 1242 cf->uhubcf_release == uaa->release) 1243 uaa->matchlvl = UMATCH_VENDOR_PRODUCT_REV; 1244 else 1245 uaa->matchlvl = UMATCH_VENDOR_PRODUCT; 1246 } else 1247 uaa->matchlvl = 0; 1248 return (config_match(parent, cf, aux)); 1249 } 1250 1251 #endif 1252 1253 void 1254 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di, 1255 int usedev) 1256 { 1257 struct usbd_port *p; 1258 int i, err, s; 1259 1260 di->udi_bus = USBDEVUNIT(dev->bus->bdev); 1261 di->udi_addr = dev->address; 1262 di->udi_cookie = dev->cookie; 1263 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1264 di->udi_product, sizeof(di->udi_product), usedev); 1265 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1266 UGETW(dev->ddesc.bcdDevice)); 1267 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1268 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1269 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1270 di->udi_class = dev->ddesc.bDeviceClass; 1271 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1272 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1273 di->udi_config = dev->config; 1274 di->udi_power = dev->self_powered ? 0 : dev->power; 1275 di->udi_speed = dev->speed; 1276 1277 if (dev->subdevs != NULL) { 1278 for (i = 0; dev->subdevs[i] && 1279 i < USB_MAX_DEVNAMES; i++) { 1280 strncpy(di->udi_devnames[i], USBDEVPTRNAME(dev->subdevs[i]), 1281 USB_MAX_DEVNAMELEN); 1282 di->udi_devnames[i][USB_MAX_DEVNAMELEN-1] = '\0'; 1283 } 1284 } else { 1285 i = 0; 1286 } 1287 for (/*i is set */; i < USB_MAX_DEVNAMES; i++) 1288 di->udi_devnames[i][0] = 0; /* empty */ 1289 1290 if (dev->hub) { 1291 for (i = 0; 1292 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) && 1293 i < dev->hub->hubdesc.bNbrPorts; 1294 i++) { 1295 p = &dev->hub->ports[i]; 1296 if (p->device) 1297 err = p->device->address; 1298 else { 1299 s = UGETW(p->status.wPortStatus); 1300 if (s & UPS_PORT_ENABLED) 1301 err = USB_PORT_ENABLED; 1302 else if (s & UPS_SUSPEND) 1303 err = USB_PORT_SUSPENDED; 1304 else if (s & UPS_PORT_POWER) 1305 err = USB_PORT_POWERED; 1306 else 1307 err = USB_PORT_DISABLED; 1308 } 1309 di->udi_ports[i] = err; 1310 } 1311 di->udi_nports = dev->hub->hubdesc.bNbrPorts; 1312 } else 1313 di->udi_nports = 0; 1314 } 1315 1316 void 1317 usb_free_device(usbd_device_handle dev) 1318 { 1319 int ifcidx, nifc; 1320 1321 if (dev->default_pipe != NULL) 1322 usbd_kill_pipe(dev->default_pipe); 1323 if (dev->ifaces != NULL) { 1324 nifc = dev->cdesc->bNumInterface; 1325 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1326 usbd_free_iface_data(dev, ifcidx); 1327 free(dev->ifaces, M_USB); 1328 } 1329 if (dev->cdesc != NULL) 1330 free(dev->cdesc, M_USB); 1331 if (dev->subdevs != NULL) 1332 free(dev->subdevs, M_USB); 1333 free(dev, M_USB); 1334 } 1335 1336 /* 1337 * The general mechanism for detaching drivers works as follows: Each 1338 * driver is responsible for maintaining a reference count on the 1339 * number of outstanding references to its softc (e.g. from 1340 * processing hanging in a read or write). The detach method of the 1341 * driver decrements this counter and flags in the softc that the 1342 * driver is dying and then wakes any sleepers. It then sleeps on the 1343 * softc. Each place that can sleep must maintain the reference 1344 * count. When the reference count drops to -1 (0 is the normal value 1345 * of the reference count) the a wakeup on the softc is performed 1346 * signaling to the detach waiter that all references are gone. 1347 */ 1348 1349 /* 1350 * Called from process context when we discover that a port has 1351 * been disconnected. 1352 */ 1353 void 1354 usb_disconnect_port(struct usbd_port *up, device_ptr_t parent) 1355 { 1356 usbd_device_handle dev = up->device; 1357 char *hubname = USBDEVPTRNAME(parent); 1358 int i; 1359 1360 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n", 1361 up, dev, up->portno)); 1362 1363 #ifdef DIAGNOSTIC 1364 if (dev == NULL) { 1365 printf("usb_disconnect_port: no device\n"); 1366 return; 1367 } 1368 #endif 1369 1370 if (dev->subdevs != NULL) { 1371 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n")); 1372 for (i = 0; dev->subdevs[i]; i++) { 1373 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]), 1374 hubname); 1375 if (up->portno != 0) 1376 printf(" port %d", up->portno); 1377 printf(" (addr %d) disconnected\n", dev->address); 1378 config_detach(dev->subdevs[i], DETACH_FORCE); 1379 dev->subdevs[i] = 0; 1380 } 1381 } 1382 1383 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev); 1384 dev->bus->devices[dev->address] = NULL; 1385 up->device = NULL; 1386 usb_free_device(dev); 1387 } 1388 1389 #ifdef __OpenBSD__ 1390 void *usb_realloc(void *p, u_int size, int pool, int flags) 1391 { 1392 void *q; 1393 1394 q = malloc(size, pool, flags); 1395 if (q == NULL) 1396 return (NULL); 1397 bcopy(p, q, size); 1398 free(p, pool); 1399 return (q); 1400 } 1401 #endif 1402