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