1 /* $NetBSD: usb_subr.c,v 1.201 2015/04/11 10:10:14 skrll 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, 2004 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 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.201 2015/04/11 10:10:14 skrll Exp $"); 36 37 #ifdef _KERNEL_OPT 38 #include "opt_compat_netbsd.h" 39 #include "opt_usb.h" 40 #include "opt_usbverbose.h" 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/device.h> 48 #include <sys/select.h> 49 #include <sys/proc.h> 50 51 #include <sys/bus.h> 52 #include <sys/module.h> 53 54 #include <dev/usb/usb.h> 55 56 #include <dev/usb/usbdi.h> 57 #include <dev/usb/usbdi_util.h> 58 #include <dev/usb/usbdivar.h> 59 #include <dev/usb/usbdevs.h> 60 #include <dev/usb/usb_quirks.h> 61 #include <dev/usb/usb_verbose.h> 62 63 #include "locators.h" 64 65 #ifdef USB_DEBUG 66 #define DPRINTF(x) if (usbdebug) printf x 67 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x 68 extern int usbdebug; 69 #else 70 #define DPRINTF(x) 71 #define DPRINTFN(n,x) 72 #endif 73 74 MALLOC_DEFINE(M_USB, "USB", "USB misc. memory"); 75 MALLOC_DEFINE(M_USBDEV, "USB device", "USB device driver"); 76 MALLOC_DEFINE(M_USBHC, "USB HC", "USB host controller"); 77 78 Static usbd_status usbd_set_config(usbd_device_handle, int); 79 Static void usbd_devinfo(usbd_device_handle, int, char *, size_t); 80 Static void usbd_devinfo_vp(usbd_device_handle, char *, size_t, char *, size_t, 81 int, int); 82 Static int usbd_getnewaddr(usbd_bus_handle); 83 Static int usbd_print(void *, const char *); 84 Static int usbd_ifprint(void *, const char *); 85 Static void usbd_free_iface_data(usbd_device_handle, int); 86 87 uint32_t usb_cookie_no = 0; 88 89 Static const char * const usbd_error_strs[] = { 90 "NORMAL_COMPLETION", 91 "IN_PROGRESS", 92 "PENDING_REQUESTS", 93 "NOT_STARTED", 94 "INVAL", 95 "NOMEM", 96 "CANCELLED", 97 "BAD_ADDRESS", 98 "IN_USE", 99 "NO_ADDR", 100 "SET_ADDR_FAILED", 101 "NO_POWER", 102 "TOO_DEEP", 103 "IOERROR", 104 "NOT_CONFIGURED", 105 "TIMEOUT", 106 "SHORT_XFER", 107 "STALLED", 108 "INTERRUPTED", 109 "XXX", 110 }; 111 112 DEV_VERBOSE_DEFINE(usb); 113 114 const char * 115 usbd_errstr(usbd_status err) 116 { 117 static char buffer[5]; 118 119 if (err < USBD_ERROR_MAX) { 120 return usbd_error_strs[err]; 121 } else { 122 snprintf(buffer, sizeof buffer, "%d", err); 123 return buffer; 124 } 125 } 126 127 usbd_status 128 usbd_get_string_desc(usbd_device_handle dev, int sindex, int langid, 129 usb_string_descriptor_t *sdesc, int *sizep) 130 { 131 usb_device_request_t req; 132 usbd_status err; 133 int actlen; 134 135 req.bmRequestType = UT_READ_DEVICE; 136 req.bRequest = UR_GET_DESCRIPTOR; 137 USETW2(req.wValue, UDESC_STRING, sindex); 138 USETW(req.wIndex, langid); 139 USETW(req.wLength, 2); /* only size byte first */ 140 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 141 &actlen, USBD_DEFAULT_TIMEOUT); 142 if (err) 143 return (err); 144 145 if (actlen < 2) 146 return (USBD_SHORT_XFER); 147 148 USETW(req.wLength, sdesc->bLength); /* the whole string */ 149 err = usbd_do_request_flags(dev, &req, sdesc, USBD_SHORT_XFER_OK, 150 &actlen, USBD_DEFAULT_TIMEOUT); 151 if (err) 152 return (err); 153 154 if (actlen != sdesc->bLength) { 155 DPRINTFN(-1, ("usbd_get_string_desc: expected %d, got %d\n", 156 sdesc->bLength, actlen)); 157 } 158 159 *sizep = actlen; 160 return (USBD_NORMAL_COMPLETION); 161 } 162 163 static void 164 usbd_trim_spaces(char *p) 165 { 166 char *q, *e; 167 168 q = e = p; 169 while (*q == ' ') /* skip leading spaces */ 170 q++; 171 while ((*p = *q++)) /* copy string */ 172 if (*p++ != ' ') /* remember last non-space */ 173 e = p; 174 *e = '\0'; /* kill trailing spaces */ 175 } 176 177 Static void 178 usbd_devinfo_vp(usbd_device_handle dev, char *v, size_t vl, char *p, 179 size_t pl, int usedev, int useencoded) 180 { 181 usb_device_descriptor_t *udd = &dev->ddesc; 182 if (dev == NULL) 183 return; 184 185 v[0] = p[0] = '\0'; 186 187 if (usedev) { 188 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) == 189 USBD_NORMAL_COMPLETION) 190 usbd_trim_spaces(v); 191 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) == 192 USBD_NORMAL_COMPLETION) 193 usbd_trim_spaces(p); 194 } 195 if (v[0] == '\0') 196 usb_findvendor(v, vl, UGETW(udd->idVendor)); 197 if (p[0] == '\0') 198 usb_findproduct(p, pl, UGETW(udd->idVendor), 199 UGETW(udd->idProduct)); 200 } 201 202 int 203 usbd_printBCD(char *cp, size_t l, int bcd) 204 { 205 return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff); 206 } 207 208 Static void 209 usbd_devinfo(usbd_device_handle dev, int showclass, char *cp, size_t l) 210 { 211 usb_device_descriptor_t *udd = &dev->ddesc; 212 char *vendor, *product; 213 int bcdDevice, bcdUSB; 214 char *ep; 215 216 vendor = malloc(USB_MAX_ENCODED_STRING_LEN * 2, M_USB, M_NOWAIT); 217 if (vendor == NULL) { 218 *cp = '\0'; 219 return; 220 } 221 product = &vendor[USB_MAX_ENCODED_STRING_LEN]; 222 223 ep = cp + l; 224 225 usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN, 226 product, USB_MAX_ENCODED_STRING_LEN, 1, 1); 227 cp += snprintf(cp, ep - cp, "%s %s", vendor, product); 228 if (showclass) 229 cp += snprintf(cp, ep - cp, ", class %d/%d", 230 udd->bDeviceClass, udd->bDeviceSubClass); 231 bcdUSB = UGETW(udd->bcdUSB); 232 bcdDevice = UGETW(udd->bcdDevice); 233 cp += snprintf(cp, ep - cp, ", rev "); 234 cp += usbd_printBCD(cp, ep - cp, bcdUSB); 235 *cp++ = '/'; 236 cp += usbd_printBCD(cp, ep - cp, bcdDevice); 237 cp += snprintf(cp, ep - cp, ", addr %d", dev->address); 238 *cp = 0; 239 free(vendor, M_USB); 240 } 241 242 char * 243 usbd_devinfo_alloc(usbd_device_handle dev, int showclass) 244 { 245 char *devinfop; 246 247 devinfop = malloc(DEVINFOSIZE, M_TEMP, M_WAITOK); 248 usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE); 249 return devinfop; 250 } 251 252 void 253 usbd_devinfo_free(char *devinfop) 254 { 255 free(devinfop, M_TEMP); 256 } 257 258 /* Delay for a certain number of ms */ 259 void 260 usb_delay_ms_locked(usbd_bus_handle bus, u_int ms, kmutex_t *lock) 261 { 262 /* Wait at least two clock ticks so we know the time has passed. */ 263 if (bus->use_polling || cold) 264 delay((ms+1) * 1000); 265 else 266 kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock); 267 } 268 269 void 270 usb_delay_ms(usbd_bus_handle bus, u_int ms) 271 { 272 usb_delay_ms_locked(bus, ms, NULL); 273 } 274 275 /* Delay given a device handle. */ 276 void 277 usbd_delay_ms_locked(usbd_device_handle dev, u_int ms, kmutex_t *lock) 278 { 279 usb_delay_ms_locked(dev->bus, ms, lock); 280 } 281 282 /* Delay given a device handle. */ 283 void 284 usbd_delay_ms(usbd_device_handle dev, u_int ms) 285 { 286 usb_delay_ms_locked(dev->bus, ms, NULL); 287 } 288 289 usbd_status 290 usbd_reset_port(usbd_device_handle dev, int port, usb_port_status_t *ps) 291 { 292 usb_device_request_t req; 293 usbd_status err; 294 int n; 295 296 req.bmRequestType = UT_WRITE_CLASS_OTHER; 297 req.bRequest = UR_SET_FEATURE; 298 USETW(req.wValue, UHF_PORT_RESET); 299 USETW(req.wIndex, port); 300 USETW(req.wLength, 0); 301 err = usbd_do_request(dev, &req, 0); 302 DPRINTFN(1,("usbd_reset_port: port %d reset done, error=%s\n", 303 port, usbd_errstr(err))); 304 if (err) 305 return (err); 306 n = 10; 307 do { 308 /* Wait for device to recover from reset. */ 309 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 310 err = usbd_get_port_status(dev, port, ps); 311 if (err) { 312 DPRINTF(("usbd_reset_port: get status failed %d\n", 313 err)); 314 return (err); 315 } 316 /* If the device disappeared, just give up. */ 317 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 318 return (USBD_NORMAL_COMPLETION); 319 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 320 if (n == 0) 321 return (USBD_TIMEOUT); 322 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET); 323 #ifdef USB_DEBUG 324 if (err) 325 DPRINTF(("usbd_reset_port: clear port feature failed %d\n", 326 err)); 327 #endif 328 329 /* Wait for the device to recover from reset. */ 330 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY); 331 return (err); 332 } 333 334 usb_interface_descriptor_t * 335 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx) 336 { 337 char *p = (char *)cd; 338 char *end = p + UGETW(cd->wTotalLength); 339 usb_interface_descriptor_t *d; 340 int curidx, lastidx, curaidx = 0; 341 342 for (curidx = lastidx = -1; p < end; ) { 343 d = (usb_interface_descriptor_t *)p; 344 DPRINTFN(4,("usbd_find_idesc: idx=%d(%d) altidx=%d(%d) len=%d " 345 "type=%d\n", 346 ifaceidx, curidx, altidx, curaidx, 347 d->bLength, d->bDescriptorType)); 348 if (d->bLength == 0) /* bad descriptor */ 349 break; 350 p += d->bLength; 351 if (p <= end && d->bDescriptorType == UDESC_INTERFACE) { 352 if (d->bInterfaceNumber != lastidx) { 353 lastidx = d->bInterfaceNumber; 354 curidx++; 355 curaidx = 0; 356 } else 357 curaidx++; 358 if (ifaceidx == curidx && altidx == curaidx) 359 return (d); 360 } 361 } 362 return (NULL); 363 } 364 365 usb_endpoint_descriptor_t * 366 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx, 367 int endptidx) 368 { 369 char *p = (char *)cd; 370 char *end = p + UGETW(cd->wTotalLength); 371 usb_interface_descriptor_t *d; 372 usb_endpoint_descriptor_t *e; 373 int curidx; 374 375 d = usbd_find_idesc(cd, ifaceidx, altidx); 376 if (d == NULL) 377 return (NULL); 378 if (endptidx >= d->bNumEndpoints) /* quick exit */ 379 return (NULL); 380 381 curidx = -1; 382 for (p = (char *)d + d->bLength; p < end; ) { 383 e = (usb_endpoint_descriptor_t *)p; 384 if (e->bLength == 0) /* bad descriptor */ 385 break; 386 p += e->bLength; 387 if (p <= end && e->bDescriptorType == UDESC_INTERFACE) 388 return (NULL); 389 if (p <= end && e->bDescriptorType == UDESC_ENDPOINT) { 390 curidx++; 391 if (curidx == endptidx) 392 return (e); 393 } 394 } 395 return (NULL); 396 } 397 398 usbd_status 399 usbd_fill_iface_data(usbd_device_handle dev, int ifaceidx, int altidx) 400 { 401 usbd_interface_handle ifc = &dev->ifaces[ifaceidx]; 402 usb_interface_descriptor_t *idesc; 403 char *p, *end; 404 int endpt, nendpt; 405 406 DPRINTFN(4,("usbd_fill_iface_data: ifaceidx=%d altidx=%d\n", 407 ifaceidx, altidx)); 408 idesc = usbd_find_idesc(dev->cdesc, ifaceidx, altidx); 409 if (idesc == NULL) 410 return (USBD_INVAL); 411 ifc->device = dev; 412 ifc->idesc = idesc; 413 ifc->index = ifaceidx; 414 ifc->altindex = altidx; 415 nendpt = ifc->idesc->bNumEndpoints; 416 DPRINTFN(4,("usbd_fill_iface_data: found idesc nendpt=%d\n", nendpt)); 417 if (nendpt != 0) { 418 ifc->endpoints = malloc(nendpt * sizeof(struct usbd_endpoint), 419 M_USB, M_NOWAIT); 420 if (ifc->endpoints == NULL) 421 return (USBD_NOMEM); 422 } else 423 ifc->endpoints = NULL; 424 ifc->priv = NULL; 425 p = (char *)ifc->idesc + ifc->idesc->bLength; 426 end = (char *)dev->cdesc + UGETW(dev->cdesc->wTotalLength); 427 #define ed ((usb_endpoint_descriptor_t *)p) 428 for (endpt = 0; endpt < nendpt; endpt++) { 429 DPRINTFN(10,("usbd_fill_iface_data: endpt=%d\n", endpt)); 430 for (; p < end; p += ed->bLength) { 431 DPRINTFN(10,("usbd_fill_iface_data: p=%p end=%p " 432 "len=%d type=%d\n", 433 p, end, ed->bLength, ed->bDescriptorType)); 434 if (p + ed->bLength <= end && ed->bLength != 0 && 435 ed->bDescriptorType == UDESC_ENDPOINT) 436 goto found; 437 if (ed->bLength == 0 || 438 ed->bDescriptorType == UDESC_INTERFACE) 439 break; 440 } 441 /* passed end, or bad desc */ 442 printf("usbd_fill_iface_data: bad descriptor(s): %s\n", 443 ed->bLength == 0 ? "0 length" : 444 ed->bDescriptorType == UDESC_INTERFACE ? "iface desc": 445 "out of data"); 446 goto bad; 447 found: 448 ifc->endpoints[endpt].edesc = ed; 449 if (dev->speed == USB_SPEED_HIGH) { 450 u_int mps; 451 /* Control and bulk endpoints have max packet limits. */ 452 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 453 case UE_CONTROL: 454 mps = USB_2_MAX_CTRL_PACKET; 455 goto check; 456 case UE_BULK: 457 mps = USB_2_MAX_BULK_PACKET; 458 check: 459 if (UGETW(ed->wMaxPacketSize) != mps) { 460 USETW(ed->wMaxPacketSize, mps); 461 #ifdef DIAGNOSTIC 462 printf("usbd_fill_iface_data: bad max " 463 "packet size\n"); 464 #endif 465 } 466 break; 467 default: 468 break; 469 } 470 } 471 ifc->endpoints[endpt].refcnt = 0; 472 ifc->endpoints[endpt].datatoggle = 0; 473 p += ed->bLength; 474 } 475 #undef ed 476 LIST_INIT(&ifc->pipes); 477 return (USBD_NORMAL_COMPLETION); 478 479 bad: 480 if (ifc->endpoints != NULL) { 481 free(ifc->endpoints, M_USB); 482 ifc->endpoints = NULL; 483 } 484 return (USBD_INVAL); 485 } 486 487 void 488 usbd_free_iface_data(usbd_device_handle dev, int ifcno) 489 { 490 usbd_interface_handle ifc = &dev->ifaces[ifcno]; 491 if (ifc->endpoints) 492 free(ifc->endpoints, M_USB); 493 } 494 495 Static usbd_status 496 usbd_set_config(usbd_device_handle dev, int conf) 497 { 498 usb_device_request_t req; 499 500 req.bmRequestType = UT_WRITE_DEVICE; 501 req.bRequest = UR_SET_CONFIG; 502 USETW(req.wValue, conf); 503 USETW(req.wIndex, 0); 504 USETW(req.wLength, 0); 505 return (usbd_do_request(dev, &req, 0)); 506 } 507 508 usbd_status 509 usbd_set_config_no(usbd_device_handle dev, int no, int msg) 510 { 511 int index; 512 usb_config_descriptor_t cd; 513 usbd_status err; 514 515 if (no == USB_UNCONFIG_NO) 516 return (usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg)); 517 518 DPRINTFN(5,("usbd_set_config_no: %d\n", no)); 519 /* Figure out what config index to use. */ 520 for (index = 0; index < dev->ddesc.bNumConfigurations; index++) { 521 err = usbd_get_config_desc(dev, index, &cd); 522 if (err) 523 return (err); 524 if (cd.bConfigurationValue == no) 525 return (usbd_set_config_index(dev, index, msg)); 526 } 527 return (USBD_INVAL); 528 } 529 530 usbd_status 531 usbd_set_config_index(usbd_device_handle dev, int index, int msg) 532 { 533 usb_config_descriptor_t cd, *cdp; 534 usbd_status err; 535 int i, ifcidx, nifc, len, selfpowered, power; 536 537 DPRINTFN(5,("usbd_set_config_index: dev=%p index=%d\n", dev, index)); 538 539 if (index >= dev->ddesc.bNumConfigurations && 540 index != USB_UNCONFIG_INDEX) { 541 /* panic? */ 542 printf("usbd_set_config_index: illegal index\n"); 543 return (USBD_INVAL); 544 } 545 546 /* XXX check that all interfaces are idle */ 547 if (dev->config != USB_UNCONFIG_NO) { 548 DPRINTF(("usbd_set_config_index: free old config\n")); 549 /* Free all configuration data structures. */ 550 nifc = dev->cdesc->bNumInterface; 551 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 552 usbd_free_iface_data(dev, ifcidx); 553 free(dev->ifaces, M_USB); 554 free(dev->cdesc, M_USB); 555 dev->ifaces = NULL; 556 dev->cdesc = NULL; 557 dev->config = USB_UNCONFIG_NO; 558 } 559 560 if (index == USB_UNCONFIG_INDEX) { 561 /* We are unconfiguring the device, so leave unallocated. */ 562 DPRINTF(("usbd_set_config_index: set config 0\n")); 563 err = usbd_set_config(dev, USB_UNCONFIG_NO); 564 if (err) { 565 DPRINTF(("usbd_set_config_index: setting config=0 " 566 "failed, error=%s\n", usbd_errstr(err))); 567 } 568 return (err); 569 } 570 571 /* Get the short descriptor. */ 572 err = usbd_get_config_desc(dev, index, &cd); 573 if (err) { 574 DPRINTF(("usbd_set_config_index: get_config_desc=%d\n", err)); 575 return (err); 576 } 577 len = UGETW(cd.wTotalLength); 578 cdp = malloc(len, M_USB, M_NOWAIT); 579 if (cdp == NULL) 580 return (USBD_NOMEM); 581 582 /* Get the full descriptor. Try a few times for slow devices. */ 583 for (i = 0; i < 3; i++) { 584 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp); 585 if (!err) 586 break; 587 usbd_delay_ms(dev, 200); 588 } 589 if (err) { 590 DPRINTF(("usbd_set_config_index: get_desc=%d\n", err)); 591 goto bad; 592 } 593 if (cdp->bDescriptorType != UDESC_CONFIG) { 594 DPRINTFN(-1,("usbd_set_config_index: bad desc %d\n", 595 cdp->bDescriptorType)); 596 err = USBD_INVAL; 597 goto bad; 598 } 599 600 /* 601 * Figure out if the device is self or bus powered. 602 */ 603 #if 0 /* XXX various devices don't report the power state correctly */ 604 selfpowered = 0; 605 err = usbd_get_device_status(dev, &ds); 606 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 607 selfpowered = 1; 608 #endif 609 /* 610 * Use the power state in the configuration we are going 611 * to set. This doesn't necessarily reflect the actual 612 * power state of the device; the driver can control this 613 * by choosing the appropriate configuration. 614 */ 615 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED); 616 617 DPRINTF(("usbd_set_config_index: (addr %d) cno=%d attr=0x%02x, " 618 "selfpowered=%d, power=%d\n", 619 cdp->bConfigurationValue, dev->address, cdp->bmAttributes, 620 selfpowered, cdp->bMaxPower * 2)); 621 622 /* Check if we have enough power. */ 623 #if 0 /* this is a no-op, see above */ 624 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) { 625 if (msg) 626 printf("%s: device addr %d (config %d): " 627 "can't set self powered configuration\n", 628 device_xname(dev->bus->bdev), dev->address, 629 cdp->bConfigurationValue); 630 err = USBD_NO_POWER; 631 goto bad; 632 } 633 #endif 634 #ifdef USB_DEBUG 635 if (dev->powersrc == NULL) { 636 DPRINTF(("usbd_set_config_index: No power source?\n")); 637 err = USBD_IOERROR; 638 goto bad; 639 } 640 #endif 641 power = cdp->bMaxPower * 2; 642 if (power > dev->powersrc->power) { 643 DPRINTF(("power exceeded %d %d\n", power,dev->powersrc->power)); 644 /* XXX print nicer message. */ 645 if (msg) 646 printf("%s: device addr %d (config %d) exceeds power " 647 "budget, %d mA > %d mA\n", 648 device_xname(dev->bus->usbctl), dev->address, 649 cdp->bConfigurationValue, 650 power, dev->powersrc->power); 651 err = USBD_NO_POWER; 652 goto bad; 653 } 654 dev->power = power; 655 dev->self_powered = selfpowered; 656 657 /* Set the actual configuration value. */ 658 DPRINTF(("usbd_set_config_index: set config %d\n", 659 cdp->bConfigurationValue)); 660 err = usbd_set_config(dev, cdp->bConfigurationValue); 661 if (err) { 662 DPRINTF(("usbd_set_config_index: setting config=%d failed, " 663 "error=%s\n", 664 cdp->bConfigurationValue, usbd_errstr(err))); 665 goto bad; 666 } 667 668 /* Allocate and fill interface data. */ 669 nifc = cdp->bNumInterface; 670 dev->ifaces = malloc(nifc * sizeof(struct usbd_interface), 671 M_USB, M_NOWAIT); 672 if (dev->ifaces == NULL) { 673 err = USBD_NOMEM; 674 goto bad; 675 } 676 DPRINTFN(5,("usbd_set_config_index: dev=%p cdesc=%p\n", dev, cdp)); 677 dev->cdesc = cdp; 678 dev->config = cdp->bConfigurationValue; 679 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 680 err = usbd_fill_iface_data(dev, ifcidx, 0); 681 if (err) { 682 while (--ifcidx >= 0) 683 usbd_free_iface_data(dev, ifcidx); 684 goto bad; 685 } 686 } 687 688 return (USBD_NORMAL_COMPLETION); 689 690 bad: 691 free(cdp, M_USB); 692 return (err); 693 } 694 695 /* XXX add function for alternate settings */ 696 697 usbd_status 698 usbd_setup_pipe(usbd_device_handle dev, usbd_interface_handle iface, 699 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe) 700 { 701 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0); 702 } 703 704 usbd_status 705 usbd_setup_pipe_flags(usbd_device_handle dev, usbd_interface_handle iface, 706 struct usbd_endpoint *ep, int ival, usbd_pipe_handle *pipe, uint8_t flags) 707 { 708 usbd_pipe_handle p; 709 usbd_status err; 710 711 p = malloc(dev->bus->pipe_size, M_USB, M_NOWAIT); 712 DPRINTFN(1,("usbd_setup_pipe: dev=%p iface=%p ep=%p pipe=%p\n", 713 dev, iface, ep, p)); 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 = NULL; 722 p->running = 0; 723 p->aborting = 0; 724 p->repeat = 0; 725 p->interval = ival; 726 p->flags = flags; 727 SIMPLEQ_INIT(&p->queue); 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 usb_init_task(&p->async_task, usbd_clear_endpoint_stall_task, p, 737 USB_TASKQ_MPSAFE); 738 *pipe = p; 739 return (USBD_NORMAL_COMPLETION); 740 } 741 742 /* Abort the device control pipe. */ 743 void 744 usbd_kill_pipe(usbd_pipe_handle pipe) 745 { 746 usbd_abort_pipe(pipe); 747 usbd_lock_pipe(pipe); 748 pipe->methods->close(pipe); 749 usbd_unlock_pipe(pipe); 750 usb_rem_task(pipe->device, &pipe->async_task); 751 pipe->endpoint->refcnt--; 752 free(pipe, M_USB); 753 } 754 755 int 756 usbd_getnewaddr(usbd_bus_handle bus) 757 { 758 int addr; 759 760 for (addr = 1; addr < USB_MAX_DEVICES; addr++) 761 if (bus->devices[addr] == NULL) 762 return (addr); 763 return (-1); 764 } 765 766 usbd_status 767 usbd_attach_roothub(device_t parent, usbd_device_handle dev) 768 { 769 struct usb_attach_arg uaa; 770 usb_device_descriptor_t *dd = &dev->ddesc; 771 device_t dv; 772 773 uaa.device = dev; 774 uaa.usegeneric = 0; 775 uaa.port = 0; 776 uaa.vendor = UGETW(dd->idVendor); 777 uaa.product = UGETW(dd->idProduct); 778 uaa.release = UGETW(dd->bcdDevice); 779 uaa.class = dd->bDeviceClass; 780 uaa.subclass = dd->bDeviceSubClass; 781 uaa.proto = dd->bDeviceProtocol; 782 783 dv = config_found_ia(parent, "usbroothubif", &uaa, 0); 784 if (dv) { 785 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT); 786 if (dev->subdevs == NULL) 787 return (USBD_NOMEM); 788 dev->subdevs[0] = dv; 789 dev->subdevlen = 1; 790 } 791 return (USBD_NORMAL_COMPLETION); 792 } 793 794 static usbd_status 795 usbd_attachwholedevice(device_t parent, usbd_device_handle dev, int port, 796 int usegeneric) 797 { 798 struct usb_attach_arg uaa; 799 usb_device_descriptor_t *dd = &dev->ddesc; 800 device_t dv; 801 int dlocs[USBDEVIFCF_NLOCS]; 802 803 uaa.device = dev; 804 uaa.usegeneric = usegeneric; 805 uaa.port = port; 806 uaa.vendor = UGETW(dd->idVendor); 807 uaa.product = UGETW(dd->idProduct); 808 uaa.release = UGETW(dd->bcdDevice); 809 uaa.class = dd->bDeviceClass; 810 uaa.subclass = dd->bDeviceSubClass; 811 uaa.proto = dd->bDeviceProtocol; 812 813 dlocs[USBDEVIFCF_PORT] = uaa.port; 814 dlocs[USBDEVIFCF_VENDOR] = uaa.vendor; 815 dlocs[USBDEVIFCF_PRODUCT] = uaa.product; 816 dlocs[USBDEVIFCF_RELEASE] = uaa.release; 817 /* the rest is historical ballast */ 818 dlocs[USBDEVIFCF_CONFIGURATION] = -1; 819 dlocs[USBDEVIFCF_INTERFACE] = -1; 820 821 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print, 822 config_stdsubmatch); 823 if (dv) { 824 dev->subdevs = malloc(sizeof dv, M_USB, M_NOWAIT); 825 if (dev->subdevs == NULL) 826 return (USBD_NOMEM); 827 dev->subdevs[0] = dv; 828 dev->subdevlen = 1; 829 dev->nifaces_claimed = 1; /* XXX */ 830 } 831 return (USBD_NORMAL_COMPLETION); 832 } 833 834 static usbd_status 835 usbd_attachinterfaces(device_t parent, usbd_device_handle dev, 836 int port, const int *locators) 837 { 838 struct usbif_attach_arg uiaa; 839 int ilocs[USBIFIFCF_NLOCS]; 840 usb_device_descriptor_t *dd = &dev->ddesc; 841 int nifaces; 842 usbd_interface_handle *ifaces; 843 int i, j, loc; 844 device_t dv; 845 846 nifaces = dev->cdesc->bNumInterface; 847 ifaces = malloc(nifaces * sizeof(*ifaces), M_USB, M_NOWAIT|M_ZERO); 848 if (!ifaces) 849 return (USBD_NOMEM); 850 for (i = 0; i < nifaces; i++) { 851 if (!dev->subdevs[i]) { 852 ifaces[i] = &dev->ifaces[i]; 853 } 854 DPRINTF(("%s: interface %d %p\n", __func__, i, ifaces[i])); 855 } 856 857 uiaa.device = dev; 858 uiaa.port = port; 859 uiaa.vendor = UGETW(dd->idVendor); 860 uiaa.product = UGETW(dd->idProduct); 861 uiaa.release = UGETW(dd->bcdDevice); 862 uiaa.configno = dev->cdesc->bConfigurationValue; 863 uiaa.ifaces = ifaces; 864 uiaa.nifaces = nifaces; 865 ilocs[USBIFIFCF_PORT] = uiaa.port; 866 ilocs[USBIFIFCF_VENDOR] = uiaa.vendor; 867 ilocs[USBIFIFCF_PRODUCT] = uiaa.product; 868 ilocs[USBIFIFCF_RELEASE] = uiaa.release; 869 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.configno; 870 871 for (i = 0; i < nifaces; i++) { 872 if (!ifaces[i]) { 873 DPRINTF(("%s: interface %d claimed\n", __func__, i)); 874 continue; /* interface already claimed */ 875 } 876 uiaa.iface = ifaces[i]; 877 uiaa.class = ifaces[i]->idesc->bInterfaceClass; 878 uiaa.subclass = ifaces[i]->idesc->bInterfaceSubClass; 879 uiaa.proto = ifaces[i]->idesc->bInterfaceProtocol; 880 uiaa.ifaceno = ifaces[i]->idesc->bInterfaceNumber; 881 882 DPRINTF(("%s: searching for interface %d " 883 "class %x subclass %x proto %x ifaceno %d\n", __func__, i, 884 uiaa.class, uiaa.subclass, uiaa.proto, uiaa.ifaceno)); 885 ilocs[USBIFIFCF_INTERFACE] = uiaa.ifaceno; 886 if (locators != NULL) { 887 loc = locators[USBIFIFCF_CONFIGURATION]; 888 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT && 889 loc != uiaa.configno) 890 continue; 891 loc = locators[USBIFIFCF_INTERFACE]; 892 if (loc != USBIFIFCF_INTERFACE && loc != uiaa.ifaceno) 893 continue; 894 } 895 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa, 896 usbd_ifprint, config_stdsubmatch); 897 if (!dv) 898 continue; 899 /* claim */ 900 ifaces[i] = NULL; 901 /* account for ifaces claimed by the driver behind our back */ 902 for (j = 0; j < nifaces; j++) { 903 if (!ifaces[j] && !dev->subdevs[j]) { 904 DPRINTF(("%s: interface %d claimed " 905 "behind our back\n", __func__, j)); 906 dev->subdevs[j] = dv; 907 dev->nifaces_claimed++; 908 } 909 } 910 } 911 912 free(ifaces, M_USB); 913 return (USBD_NORMAL_COMPLETION); 914 } 915 916 usbd_status 917 usbd_probe_and_attach(device_t parent, usbd_device_handle dev, 918 int port, int addr) 919 { 920 usb_device_descriptor_t *dd = &dev->ddesc; 921 int confi, nifaces; 922 usbd_status err; 923 924 /* First try with device specific drivers. */ 925 DPRINTF(("usbd_probe_and_attach: trying device specific drivers\n")); 926 err = usbd_attachwholedevice(parent, dev, port, 0); 927 if (dev->nifaces_claimed || err) 928 return (err); 929 DPRINTF(("usbd_probe_and_attach: no device specific driver found\n")); 930 931 DPRINTF(("usbd_probe_and_attach: looping over %d configurations\n", 932 dd->bNumConfigurations)); 933 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 934 DPRINTFN(1,("usbd_probe_and_attach: trying config idx=%d\n", 935 confi)); 936 err = usbd_set_config_index(dev, confi, 1); 937 if (err) { 938 #ifdef USB_DEBUG 939 DPRINTF(("%s: port %d, set config at addr %d failed, " 940 "error=%s\n", device_xname(parent), port, 941 addr, usbd_errstr(err))); 942 #else 943 printf("%s: port %d, set config at addr %d failed\n", 944 device_xname(parent), port, addr); 945 #endif 946 return (err); 947 } 948 nifaces = dev->cdesc->bNumInterface; 949 dev->subdevs = malloc(nifaces * sizeof(device_t), M_USB, 950 M_NOWAIT|M_ZERO); 951 if (dev->subdevs == NULL) 952 return (USBD_NOMEM); 953 dev->subdevlen = nifaces; 954 955 err = usbd_attachinterfaces(parent, dev, port, NULL); 956 957 if (!dev->nifaces_claimed) { 958 free(dev->subdevs, M_USB); 959 dev->subdevs = 0; 960 dev->subdevlen = 0; 961 } 962 if (dev->nifaces_claimed || err) 963 return (err); 964 } 965 /* No interfaces were attached in any of the configurations. */ 966 967 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 968 usbd_set_config_index(dev, 0, 0); 969 970 DPRINTF(("usbd_probe_and_attach: no interface drivers found\n")); 971 972 /* Finally try the generic driver. */ 973 err = usbd_attachwholedevice(parent, dev, port, 1); 974 975 /* 976 * The generic attach failed, but leave the device as it is. 977 * We just did not find any drivers, that's all. The device is 978 * fully operational and not harming anyone. 979 */ 980 DPRINTF(("usbd_probe_and_attach: generic attach failed\n")); 981 return (USBD_NORMAL_COMPLETION); 982 } 983 984 /** 985 * Called from uhub_rescan(). usbd_new_device() for the target dev must be 986 * called before calling this. 987 */ 988 usbd_status 989 usbd_reattach_device(device_t parent, usbd_device_handle dev, 990 int port, const int *locators) 991 { 992 int i, loc; 993 994 if (locators != NULL) { 995 loc = locators[USBIFIFCF_PORT]; 996 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port) 997 return USBD_NORMAL_COMPLETION; 998 loc = locators[USBIFIFCF_VENDOR]; 999 if (loc != USBIFIFCF_VENDOR_DEFAULT && 1000 loc != UGETW(dev->ddesc.idVendor)) 1001 return USBD_NORMAL_COMPLETION; 1002 loc = locators[USBIFIFCF_PRODUCT]; 1003 if (loc != USBIFIFCF_PRODUCT_DEFAULT && 1004 loc != UGETW(dev->ddesc.idProduct)) 1005 return USBD_NORMAL_COMPLETION; 1006 loc = locators[USBIFIFCF_RELEASE]; 1007 if (loc != USBIFIFCF_RELEASE_DEFAULT && 1008 loc != UGETW(dev->ddesc.bcdDevice)) 1009 return USBD_NORMAL_COMPLETION; 1010 } 1011 if (dev->subdevlen == 0) { 1012 /* XXX: check USBIFIFCF_CONFIGURATION and 1013 * USBIFIFCF_INTERFACE too */ 1014 return usbd_probe_and_attach(parent, dev, port, dev->address); 1015 } else if (dev->subdevlen != dev->cdesc->bNumInterface) { 1016 /* device-specific or generic driver is already attached. */ 1017 return USBD_NORMAL_COMPLETION; 1018 } 1019 /* Does the device have unconfigured interfaces? */ 1020 for (i = 0; i < dev->subdevlen; i++) { 1021 if (dev->subdevs[i] == NULL) { 1022 break; 1023 } 1024 } 1025 if (i >= dev->subdevlen) 1026 return USBD_NORMAL_COMPLETION; 1027 return usbd_attachinterfaces(parent, dev, port, locators); 1028 } 1029 1030 /* 1031 * Get the first 8 bytes of the device descriptor. 1032 * Do as Windows does: try to read 64 bytes -- there are devices which 1033 * recognize the initial descriptor fetch (before the control endpoint's 1034 * MaxPacketSize is known by the host) by exactly this length. 1035 */ 1036 usbd_status 1037 usbd_get_initial_ddesc(usbd_device_handle dev, usb_device_descriptor_t *desc) 1038 { 1039 usb_device_request_t req; 1040 char buf[64]; 1041 int res, actlen; 1042 1043 req.bmRequestType = UT_READ_DEVICE; 1044 req.bRequest = UR_GET_DESCRIPTOR; 1045 USETW2(req.wValue, UDESC_DEVICE, 0); 1046 USETW(req.wIndex, 0); 1047 USETW(req.wLength, 64); 1048 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK, 1049 &actlen, USBD_DEFAULT_TIMEOUT); 1050 if (res) 1051 return res; 1052 if (actlen < 8) 1053 return USBD_SHORT_XFER; 1054 memcpy(desc, buf, 8); 1055 return USBD_NORMAL_COMPLETION; 1056 } 1057 1058 /* 1059 * Called when a new device has been put in the powered state, 1060 * but not yet in the addressed state. 1061 * Get initial descriptor, set the address, get full descriptor, 1062 * and attach a driver. 1063 */ 1064 usbd_status 1065 usbd_new_device(device_t parent, usbd_bus_handle bus, int depth, 1066 int speed, int port, struct usbd_port *up) 1067 { 1068 usbd_device_handle dev, adev; 1069 struct usbd_device *hub; 1070 usb_device_descriptor_t *dd; 1071 usb_port_status_t ps; 1072 usbd_status err; 1073 int addr; 1074 int i; 1075 int p; 1076 1077 DPRINTF(("usbd_new_device bus=%p port=%d depth=%d speed=%d\n", 1078 bus, port, depth, speed)); 1079 1080 if (bus->methods->new_device != NULL) 1081 return (bus->methods->new_device)(parent, bus, depth, speed, 1082 port, up); 1083 1084 addr = usbd_getnewaddr(bus); 1085 if (addr < 0) { 1086 printf("%s: No free USB addresses, new device ignored.\n", 1087 device_xname(bus->usbctl)); 1088 return (USBD_NO_ADDR); 1089 } 1090 1091 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO); 1092 if (dev == NULL) 1093 return (USBD_NOMEM); 1094 1095 dev->bus = bus; 1096 1097 /* Set up default endpoint handle. */ 1098 dev->def_ep.edesc = &dev->def_ep_desc; 1099 1100 /* Set up default endpoint descriptor. */ 1101 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1102 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1103 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1104 dev->def_ep_desc.bmAttributes = UE_CONTROL; 1105 /* 1106 * temporary, will be fixed after first descriptor fetch 1107 * (which uses 64 bytes so it shouldn't be less), 1108 * highspeed devices must support 64 byte packets anyway 1109 */ 1110 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL) 1111 USETW(dev->def_ep_desc.wMaxPacketSize, 64); 1112 else 1113 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 1114 1115 dev->def_ep_desc.bInterval = 0; 1116 1117 /* doesn't matter, just don't let it uninitialized */ 1118 dev->def_ep.datatoggle = 0; 1119 1120 dev->quirks = &usbd_no_quirk; 1121 dev->address = USB_START_ADDR; 1122 dev->ddesc.bMaxPacketSize = 0; 1123 dev->depth = depth; 1124 dev->powersrc = up; 1125 dev->myhub = up->parent; 1126 1127 up->device = dev; 1128 1129 /* Locate port on upstream high speed hub */ 1130 for (adev = dev, hub = up->parent; 1131 hub != NULL && hub->speed != USB_SPEED_HIGH; 1132 adev = hub, hub = hub->myhub) 1133 ; 1134 if (hub) { 1135 for (p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) { 1136 if (hub->hub->ports[p].device == adev) { 1137 dev->myhsport = &hub->hub->ports[p]; 1138 goto found; 1139 } 1140 } 1141 panic("usbd_new_device: cannot find HS port"); 1142 found: 1143 DPRINTFN(1,("usbd_new_device: high speed port %d\n", p)); 1144 } else { 1145 dev->myhsport = NULL; 1146 } 1147 dev->speed = speed; 1148 dev->langid = USBD_NOLANG; 1149 dev->cookie.cookie = ++usb_cookie_no; 1150 1151 /* Establish the default pipe. */ 1152 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1153 &dev->default_pipe, USBD_MPSAFE); 1154 if (err) { 1155 usbd_remove_device(dev, up); 1156 return (err); 1157 } 1158 1159 dd = &dev->ddesc; 1160 /* Try a few times in case the device is slow (i.e. outside specs.) */ 1161 for (i = 0; i < 10; i++) { 1162 /* Get the first 8 bytes of the device descriptor. */ 1163 err = usbd_get_initial_ddesc(dev, dd); 1164 if (!err) 1165 break; 1166 usbd_delay_ms(dev, 200); 1167 if ((i & 3) == 3) 1168 usbd_reset_port(up->parent, port, &ps); 1169 } 1170 if (err) { 1171 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting first desc " 1172 "failed: %d\n", addr, err)); 1173 usbd_remove_device(dev, up); 1174 return (err); 1175 } 1176 1177 /* Windows resets the port here, do likewise */ 1178 if (up->parent) 1179 usbd_reset_port(up->parent, port, &ps); 1180 1181 if (speed == USB_SPEED_HIGH) { 1182 /* Max packet size must be 64 (sec 5.5.3). */ 1183 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { 1184 #ifdef DIAGNOSTIC 1185 printf("usbd_new_device: addr=%d bad max packet " 1186 "size=%d. adjusting to %d.\n", 1187 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET); 1188 #endif 1189 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; 1190 } 1191 } 1192 1193 DPRINTF(("usbd_new_device: adding unit addr=%d, rev=%02x, class=%d, " 1194 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1195 addr,UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass, 1196 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, 1197 dev->speed)); 1198 1199 if (dd->bDescriptorType != UDESC_DEVICE) { 1200 /* Illegal device descriptor */ 1201 DPRINTFN(-1,("usbd_new_device: illegal descriptor %d\n", 1202 dd->bDescriptorType)); 1203 usbd_remove_device(dev, up); 1204 return (USBD_INVAL); 1205 } 1206 1207 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { 1208 DPRINTFN(-1,("usbd_new_device: bad length %d\n", dd->bLength)); 1209 usbd_remove_device(dev, up); 1210 return (USBD_INVAL); 1211 } 1212 1213 USETW(dev->def_ep_desc.wMaxPacketSize, dd->bMaxPacketSize); 1214 1215 /* Re-establish the default pipe with the new MPS. */ 1216 usbd_kill_pipe(dev->default_pipe); 1217 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1218 &dev->default_pipe, USBD_MPSAFE); 1219 if (err) { 1220 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n")); 1221 usbd_remove_device(dev, up); 1222 return err; 1223 } 1224 1225 /* Set the address */ 1226 DPRINTFN(5, ("usbd_new_device: setting device address=%d\n", addr)); 1227 err = usbd_set_address(dev, addr); 1228 if (err) { 1229 DPRINTFN(-1, ("usbd_new_device: set address %d failed\n", addr)); 1230 err = USBD_SET_ADDR_FAILED; 1231 usbd_remove_device(dev, up); 1232 return err; 1233 } 1234 1235 /* Allow device time to set new address */ 1236 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 1237 dev->address = addr; /* new device address now */ 1238 bus->devices[addr] = dev; 1239 1240 /* Re-establish the default pipe with the new address. */ 1241 usbd_kill_pipe(dev->default_pipe); 1242 err = usbd_setup_pipe_flags(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1243 &dev->default_pipe, USBD_MPSAFE); 1244 if (err) { 1245 DPRINTFN(-1, ("usbd_new_device: setup default pipe failed\n")); 1246 usbd_remove_device(dev, up); 1247 return err; 1248 } 1249 1250 err = usbd_reload_device_desc(dev); 1251 if (err) { 1252 DPRINTFN(-1, ("usbd_new_device: addr=%d, getting full desc " 1253 "failed\n", addr)); 1254 usbd_remove_device(dev, up); 1255 return (err); 1256 } 1257 1258 /* Assume 100mA bus powered for now. Changed when configured. */ 1259 dev->power = USB_MIN_POWER; 1260 dev->self_powered = 0; 1261 1262 DPRINTF(("usbd_new_device: new dev (addr %d), dev=%p, parent=%p\n", 1263 addr, dev, parent)); 1264 1265 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1266 1267 if (port == 0) { /* root hub */ 1268 KASSERT(addr == 1); 1269 usbd_attach_roothub(parent, dev); 1270 return (USBD_NORMAL_COMPLETION); 1271 } 1272 1273 err = usbd_probe_and_attach(parent, dev, port, addr); 1274 if (err) { 1275 usbd_remove_device(dev, up); 1276 return (err); 1277 } 1278 1279 return (USBD_NORMAL_COMPLETION); 1280 } 1281 1282 usbd_status 1283 usbd_reload_device_desc(usbd_device_handle dev) 1284 { 1285 usbd_status err; 1286 1287 /* Get the full device descriptor. */ 1288 err = usbd_get_device_desc(dev, &dev->ddesc); 1289 if (err) 1290 return (err); 1291 1292 /* Figure out what's wrong with this device. */ 1293 dev->quirks = usbd_find_quirk(&dev->ddesc); 1294 1295 return (USBD_NORMAL_COMPLETION); 1296 } 1297 1298 void 1299 usbd_remove_device(usbd_device_handle dev, struct usbd_port *up) 1300 { 1301 1302 DPRINTF(("usbd_remove_device: %p\n", dev)); 1303 1304 if (dev->default_pipe != NULL) 1305 usbd_kill_pipe(dev->default_pipe); 1306 up->device = NULL; 1307 dev->bus->devices[dev->address] = NULL; 1308 1309 free(dev, M_USB); 1310 } 1311 1312 int 1313 usbd_print(void *aux, const char *pnp) 1314 { 1315 struct usb_attach_arg *uaa = aux; 1316 1317 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1318 if (pnp) { 1319 #define USB_DEVINFO 1024 1320 char *devinfo; 1321 if (!uaa->usegeneric) 1322 return (QUIET); 1323 devinfo = malloc(USB_DEVINFO, M_TEMP, M_WAITOK); 1324 usbd_devinfo(uaa->device, 1, devinfo, USB_DEVINFO); 1325 aprint_normal("%s, %s", devinfo, pnp); 1326 free(devinfo, M_TEMP); 1327 } 1328 aprint_normal(" port %d", uaa->port); 1329 #if 0 1330 /* 1331 * It gets very crowded with these locators on the attach line. 1332 * They are not really needed since they are printed in the clear 1333 * by each driver. 1334 */ 1335 if (uaa->vendor != UHUB_UNK_VENDOR) 1336 aprint_normal(" vendor 0x%04x", uaa->vendor); 1337 if (uaa->product != UHUB_UNK_PRODUCT) 1338 aprint_normal(" product 0x%04x", uaa->product); 1339 if (uaa->release != UHUB_UNK_RELEASE) 1340 aprint_normal(" release 0x%04x", uaa->release); 1341 #endif 1342 return (UNCONF); 1343 } 1344 1345 int 1346 usbd_ifprint(void *aux, const char *pnp) 1347 { 1348 struct usbif_attach_arg *uaa = aux; 1349 1350 DPRINTFN(15, ("usbd_print dev=%p\n", uaa->device)); 1351 if (pnp) 1352 return (QUIET); 1353 aprint_normal(" port %d", uaa->port); 1354 aprint_normal(" configuration %d", uaa->configno); 1355 aprint_normal(" interface %d", uaa->ifaceno); 1356 #if 0 1357 /* 1358 * It gets very crowded with these locators on the attach line. 1359 * They are not really needed since they are printed in the clear 1360 * by each driver. 1361 */ 1362 if (uaa->vendor != UHUB_UNK_VENDOR) 1363 aprint_normal(" vendor 0x%04x", uaa->vendor); 1364 if (uaa->product != UHUB_UNK_PRODUCT) 1365 aprint_normal(" product 0x%04x", uaa->product); 1366 if (uaa->release != UHUB_UNK_RELEASE) 1367 aprint_normal(" release 0x%04x", uaa->release); 1368 #endif 1369 return (UNCONF); 1370 } 1371 1372 void 1373 usbd_fill_deviceinfo(usbd_device_handle dev, struct usb_device_info *di, 1374 int usedev) 1375 { 1376 struct usbd_port *p; 1377 int i, j, err, s; 1378 1379 di->udi_bus = device_unit(dev->bus->usbctl); 1380 di->udi_addr = dev->address; 1381 di->udi_cookie = dev->cookie; 1382 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1383 di->udi_product, sizeof(di->udi_product), usedev, 1); 1384 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1385 UGETW(dev->ddesc.bcdDevice)); 1386 di->udi_serial[0] = 0; 1387 if (usedev) 1388 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, 1389 di->udi_serial); 1390 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1391 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1392 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1393 di->udi_class = dev->ddesc.bDeviceClass; 1394 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1395 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1396 di->udi_config = dev->config; 1397 di->udi_power = dev->self_powered ? 0 : dev->power; 1398 di->udi_speed = dev->speed; 1399 1400 if (dev->subdevlen > 0) { 1401 for (i = 0, j = 0; i < dev->subdevlen && 1402 j < USB_MAX_DEVNAMES; i++) { 1403 if (!dev->subdevs[i]) 1404 continue; 1405 strncpy(di->udi_devnames[j], 1406 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN); 1407 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1408 j++; 1409 } 1410 } else { 1411 j = 0; 1412 } 1413 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1414 di->udi_devnames[j][0] = 0; /* empty */ 1415 1416 if (dev->hub) { 1417 for (i = 0; 1418 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) && 1419 i < dev->hub->hubdesc.bNbrPorts; 1420 i++) { 1421 p = &dev->hub->ports[i]; 1422 if (p->device) 1423 err = p->device->address; 1424 else { 1425 s = UGETW(p->status.wPortStatus); 1426 if (s & UPS_PORT_ENABLED) 1427 err = USB_PORT_ENABLED; 1428 else if (s & UPS_SUSPEND) 1429 err = USB_PORT_SUSPENDED; 1430 else if (s & UPS_PORT_POWER) 1431 err = USB_PORT_POWERED; 1432 else 1433 err = USB_PORT_DISABLED; 1434 } 1435 di->udi_ports[i] = err; 1436 } 1437 di->udi_nports = dev->hub->hubdesc.bNbrPorts; 1438 } else 1439 di->udi_nports = 0; 1440 } 1441 1442 #ifdef COMPAT_30 1443 void 1444 usbd_fill_deviceinfo_old(usbd_device_handle dev, struct usb_device_info_old *di, 1445 int usedev) 1446 { 1447 struct usbd_port *p; 1448 int i, j, err, s; 1449 1450 di->udi_bus = device_unit(dev->bus->usbctl); 1451 di->udi_addr = dev->address; 1452 di->udi_cookie = dev->cookie; 1453 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1454 di->udi_product, sizeof(di->udi_product), usedev, 0); 1455 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1456 UGETW(dev->ddesc.bcdDevice)); 1457 di->udi_vendorNo = UGETW(dev->ddesc.idVendor); 1458 di->udi_productNo = UGETW(dev->ddesc.idProduct); 1459 di->udi_releaseNo = UGETW(dev->ddesc.bcdDevice); 1460 di->udi_class = dev->ddesc.bDeviceClass; 1461 di->udi_subclass = dev->ddesc.bDeviceSubClass; 1462 di->udi_protocol = dev->ddesc.bDeviceProtocol; 1463 di->udi_config = dev->config; 1464 di->udi_power = dev->self_powered ? 0 : dev->power; 1465 di->udi_speed = dev->speed; 1466 1467 if (dev->subdevlen > 0) { 1468 for (i = 0, j = 0; i < dev->subdevlen && 1469 j < USB_MAX_DEVNAMES; i++) { 1470 if (!dev->subdevs[i]) 1471 continue; 1472 strncpy(di->udi_devnames[j], 1473 device_xname(dev->subdevs[i]), USB_MAX_DEVNAMELEN); 1474 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1475 j++; 1476 } 1477 } else { 1478 j = 0; 1479 } 1480 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1481 di->udi_devnames[j][0] = 0; /* empty */ 1482 1483 if (dev->hub) { 1484 for (i = 0; 1485 i < sizeof(di->udi_ports) / sizeof(di->udi_ports[0]) && 1486 i < dev->hub->hubdesc.bNbrPorts; 1487 i++) { 1488 p = &dev->hub->ports[i]; 1489 if (p->device) 1490 err = p->device->address; 1491 else { 1492 s = UGETW(p->status.wPortStatus); 1493 if (s & UPS_PORT_ENABLED) 1494 err = USB_PORT_ENABLED; 1495 else if (s & UPS_SUSPEND) 1496 err = USB_PORT_SUSPENDED; 1497 else if (s & UPS_PORT_POWER) 1498 err = USB_PORT_POWERED; 1499 else 1500 err = USB_PORT_DISABLED; 1501 } 1502 di->udi_ports[i] = err; 1503 } 1504 di->udi_nports = dev->hub->hubdesc.bNbrPorts; 1505 } else 1506 di->udi_nports = 0; 1507 } 1508 #endif 1509 1510 1511 void 1512 usb_free_device(usbd_device_handle dev) 1513 { 1514 int ifcidx, nifc; 1515 1516 if (dev->default_pipe != NULL) 1517 usbd_kill_pipe(dev->default_pipe); 1518 if (dev->ifaces != NULL) { 1519 nifc = dev->cdesc->bNumInterface; 1520 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1521 usbd_free_iface_data(dev, ifcidx); 1522 free(dev->ifaces, M_USB); 1523 } 1524 if (dev->cdesc != NULL) 1525 free(dev->cdesc, M_USB); 1526 if (dev->subdevlen > 0) { 1527 free(dev->subdevs, M_USB); 1528 dev->subdevlen = 0; 1529 } 1530 free(dev, M_USB); 1531 } 1532 1533 /* 1534 * The general mechanism for detaching drivers works as follows: Each 1535 * driver is responsible for maintaining a reference count on the 1536 * number of outstanding references to its softc (e.g. from 1537 * processing hanging in a read or write). The detach method of the 1538 * driver decrements this counter and flags in the softc that the 1539 * driver is dying and then wakes any sleepers. It then sleeps on the 1540 * softc. Each place that can sleep must maintain the reference 1541 * count. When the reference count drops to -1 (0 is the normal value 1542 * of the reference count) the a wakeup on the softc is performed 1543 * signaling to the detach waiter that all references are gone. 1544 */ 1545 1546 /* 1547 * Called from process context when we discover that a port has 1548 * been disconnected. 1549 */ 1550 int 1551 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags) 1552 { 1553 usbd_device_handle dev = up->device; 1554 device_t subdev; 1555 char subdevname[16]; 1556 const char *hubname = device_xname(parent); 1557 int i, rc; 1558 1559 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n", 1560 up, dev, up->portno)); 1561 1562 if (dev == NULL) { 1563 #ifdef DIAGNOSTIC 1564 printf("usb_disconnect_port: no device\n"); 1565 #endif 1566 return 0; 1567 } 1568 1569 if (dev->subdevlen > 0) { 1570 DPRINTFN(3,("usb_disconnect_port: disconnect subdevs\n")); 1571 for (i = 0; i < dev->subdevlen; i++) { 1572 if ((subdev = dev->subdevs[i]) == NULL) 1573 continue; 1574 strlcpy(subdevname, device_xname(subdev), 1575 sizeof(subdevname)); 1576 if ((rc = config_detach(subdev, flags)) != 0) 1577 return rc; 1578 printf("%s: at %s", subdevname, hubname); 1579 if (up->portno != 0) 1580 printf(" port %d", up->portno); 1581 printf(" (addr %d) disconnected\n", dev->address); 1582 } 1583 KASSERT(!dev->nifaces_claimed); 1584 } 1585 1586 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev); 1587 dev->bus->devices[dev->address] = NULL; 1588 up->device = NULL; 1589 usb_free_device(dev); 1590 return 0; 1591 } 1592