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