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