1 /* $NetBSD: usb_subr.c,v 1.267 2021/09/07 10:44:18 riastradh 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.267 2021/09/07 10:44:18 riastradh 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/kmem.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 Static void usbd_devinfo(struct usbd_device *, int, char *, size_t); 70 Static int usbd_getnewaddr(struct usbd_bus *); 71 Static int usbd_print(void *, const char *); 72 Static int usbd_ifprint(void *, const char *); 73 Static void usbd_free_iface_data(struct usbd_device *, int); 74 75 uint32_t usb_cookie_no = 0; 76 77 Static const char * const usbd_error_strs[] = { 78 "NORMAL_COMPLETION", 79 "IN_PROGRESS", 80 "PENDING_REQUESTS", 81 "NOT_STARTED", 82 "INVAL", 83 "NOMEM", 84 "CANCELLED", 85 "BAD_ADDRESS", 86 "IN_USE", 87 "NO_ADDR", 88 "SET_ADDR_FAILED", 89 "NO_POWER", 90 "TOO_DEEP", 91 "IOERROR", 92 "NOT_CONFIGURED", 93 "TIMEOUT", 94 "SHORT_XFER", 95 "STALLED", 96 "INTERRUPTED", 97 "XXX", 98 }; 99 100 DEV_VERBOSE_DEFINE(usb); 101 102 const char * 103 usbd_errstr(usbd_status err) 104 { 105 static char buffer[5]; 106 107 if (err < USBD_ERROR_MAX) { 108 return usbd_error_strs[err]; 109 } else { 110 snprintf(buffer, sizeof(buffer), "%d", err); 111 return buffer; 112 } 113 } 114 115 static void 116 usbd_trim_spaces(char *p) 117 { 118 char *q, *e; 119 120 q = e = p; 121 while (*q == ' ') /* skip leading spaces */ 122 q++; 123 while ((*p = *q++)) /* copy string */ 124 if (*p++ != ' ') /* remember last non-space */ 125 e = p; 126 *e = '\0'; /* kill trailing spaces */ 127 } 128 129 static void 130 usbd_get_device_string(struct usbd_device *ud, uByte index, char **buf) 131 { 132 char *b; 133 usbd_status err; 134 135 b = kmem_alloc(USB_MAX_ENCODED_STRING_LEN, KM_SLEEP); 136 err = usbd_get_string0(ud, index, b, true); 137 if (err != USBD_NORMAL_COMPLETION) { 138 kmem_free(b, USB_MAX_ENCODED_STRING_LEN); 139 b = NULL; 140 } else { 141 usbd_trim_spaces(b); 142 } 143 144 *buf = b; 145 } 146 147 void 148 usbd_get_device_strings(struct usbd_device *ud) 149 { 150 usb_device_descriptor_t *udd = &ud->ud_ddesc; 151 152 usbd_get_device_string(ud, udd->iManufacturer, &ud->ud_vendor); 153 usbd_get_device_string(ud, udd->iProduct, &ud->ud_product); 154 usbd_get_device_string(ud, udd->iSerialNumber, &ud->ud_serial); 155 } 156 157 158 void 159 usbd_devinfo_vp(struct usbd_device *dev, char *v, size_t vl, char *p, 160 size_t pl, int usedev, int useencoded) 161 { 162 usb_device_descriptor_t *udd = &dev->ud_ddesc; 163 if (dev == NULL) 164 return; 165 166 v[0] = p[0] = '\0'; 167 168 if (usedev) { 169 if (usbd_get_string0(dev, udd->iManufacturer, v, useencoded) == 170 USBD_NORMAL_COMPLETION) 171 usbd_trim_spaces(v); 172 if (usbd_get_string0(dev, udd->iProduct, p, useencoded) == 173 USBD_NORMAL_COMPLETION) 174 usbd_trim_spaces(p); 175 } else { 176 if (dev->ud_vendor) { 177 strlcpy(v, dev->ud_vendor, vl); 178 } 179 if (dev->ud_product) { 180 strlcpy(p, dev->ud_product, pl); 181 } 182 } 183 if (v[0] == '\0') 184 usb_findvendor(v, vl, UGETW(udd->idVendor)); 185 if (p[0] == '\0') 186 usb_findproduct(p, pl, UGETW(udd->idVendor), 187 UGETW(udd->idProduct)); 188 } 189 190 int 191 usbd_printBCD(char *cp, size_t l, int bcd) 192 { 193 return snprintf(cp, l, "%x.%02x", bcd >> 8, bcd & 0xff); 194 } 195 196 Static void 197 usbd_devinfo(struct usbd_device *dev, int showclass, char *cp, size_t l) 198 { 199 usb_device_descriptor_t *udd = &dev->ud_ddesc; 200 char *vendor, *product; 201 int bcdDevice, bcdUSB; 202 char *ep; 203 204 vendor = kmem_alloc(USB_MAX_ENCODED_STRING_LEN * 2, KM_SLEEP); 205 product = &vendor[USB_MAX_ENCODED_STRING_LEN]; 206 207 ep = cp + l; 208 209 usbd_devinfo_vp(dev, vendor, USB_MAX_ENCODED_STRING_LEN, 210 product, USB_MAX_ENCODED_STRING_LEN, 0, 1); 211 cp += snprintf(cp, ep - cp, "%s (0x%04x) %s (0x%04x)", vendor, 212 UGETW(udd->idVendor), product, UGETW(udd->idProduct)); 213 if (showclass) 214 cp += snprintf(cp, ep - cp, ", class %d/%d", 215 udd->bDeviceClass, udd->bDeviceSubClass); 216 bcdUSB = UGETW(udd->bcdUSB); 217 bcdDevice = UGETW(udd->bcdDevice); 218 cp += snprintf(cp, ep - cp, ", rev "); 219 cp += usbd_printBCD(cp, ep - cp, bcdUSB); 220 *cp++ = '/'; 221 cp += usbd_printBCD(cp, ep - cp, bcdDevice); 222 cp += snprintf(cp, ep - cp, ", addr %d", dev->ud_addr); 223 *cp = 0; 224 kmem_free(vendor, USB_MAX_ENCODED_STRING_LEN * 2); 225 } 226 227 char * 228 usbd_devinfo_alloc(struct usbd_device *dev, int showclass) 229 { 230 char *devinfop; 231 232 devinfop = kmem_alloc(DEVINFOSIZE, KM_SLEEP); 233 usbd_devinfo(dev, showclass, devinfop, DEVINFOSIZE); 234 return devinfop; 235 } 236 237 void 238 usbd_devinfo_free(char *devinfop) 239 { 240 kmem_free(devinfop, DEVINFOSIZE); 241 } 242 243 /* Delay for a certain number of ms */ 244 void 245 usb_delay_ms_locked(struct usbd_bus *bus, u_int ms, kmutex_t *lock) 246 { 247 /* Wait at least two clock ticks so we know the time has passed. */ 248 if (bus->ub_usepolling || cold) 249 delay((ms+1) * 1000); 250 else 251 kpause("usbdly", false, (ms*hz+999)/1000 + 1, lock); 252 } 253 254 void 255 usb_delay_ms(struct usbd_bus *bus, u_int ms) 256 { 257 usb_delay_ms_locked(bus, ms, NULL); 258 } 259 260 /* Delay given a device handle. */ 261 void 262 usbd_delay_ms_locked(struct usbd_device *dev, u_int ms, kmutex_t *lock) 263 { 264 usb_delay_ms_locked(dev->ud_bus, ms, lock); 265 } 266 267 /* Delay given a device handle. */ 268 void 269 usbd_delay_ms(struct usbd_device *dev, u_int ms) 270 { 271 usb_delay_ms_locked(dev->ud_bus, ms, NULL); 272 } 273 274 usbd_status 275 usbd_reset_port(struct usbd_device *dev, int port, usb_port_status_t *ps) 276 { 277 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "port %jd", port, 0, 0, 0); 278 usb_device_request_t req; 279 usbd_status err; 280 int n; 281 282 req.bmRequestType = UT_WRITE_CLASS_OTHER; 283 req.bRequest = UR_SET_FEATURE; 284 USETW(req.wValue, UHF_PORT_RESET); 285 USETW(req.wIndex, port); 286 USETW(req.wLength, 0); 287 err = usbd_do_request(dev, &req, 0); 288 DPRINTFN(1, "port %jd reset done, error=%jd", port, err, 0, 0); 289 if (err) 290 return err; 291 n = 10; 292 do { 293 /* Wait for device to recover from reset. */ 294 usbd_delay_ms(dev, USB_PORT_RESET_DELAY); 295 err = usbd_get_port_status(dev, port, ps); 296 if (err) { 297 DPRINTF("get status failed %jd", err, 0, 0, 0); 298 return err; 299 } 300 /* If the device disappeared, just give up. */ 301 if (!(UGETW(ps->wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) 302 return USBD_NORMAL_COMPLETION; 303 } while ((UGETW(ps->wPortChange) & UPS_C_PORT_RESET) == 0 && --n > 0); 304 if (n == 0) 305 return USBD_TIMEOUT; 306 err = usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET); 307 #ifdef USB_DEBUG 308 if (err) 309 DPRINTF("clear port feature failed %jd", err, 0, 0, 0); 310 #endif 311 312 /* Wait for the device to recover from reset. */ 313 usbd_delay_ms(dev, USB_PORT_RESET_RECOVERY); 314 return err; 315 } 316 317 usb_interface_descriptor_t * 318 usbd_find_idesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx) 319 { 320 USBHIST_FUNC(); 321 USBHIST_CALLARGS(usbdebug, "iface/alt idx %jd/%jd", 322 ifaceidx, altidx, 0, 0); 323 char *p = (char *)cd; 324 char *end = p + UGETW(cd->wTotalLength); 325 usb_descriptor_t *desc; 326 usb_interface_descriptor_t *idesc; 327 int curidx, lastidx, curaidx = 0; 328 329 for (curidx = lastidx = -1; p < end; ) { 330 desc = (usb_descriptor_t *)p; 331 332 DPRINTFN(4, "idx=%jd(%jd) altidx=%jd(%jd)", ifaceidx, curidx, 333 altidx, curaidx); 334 DPRINTFN(4, "len=%jd type=%jd", desc->bLength, 335 desc->bDescriptorType, 0, 0); 336 337 if (desc->bLength < USB_DESCRIPTOR_SIZE) 338 break; 339 p += desc->bLength; 340 if (p > end) 341 break; 342 343 if (desc->bDescriptorType != UDESC_INTERFACE) 344 continue; 345 idesc = (usb_interface_descriptor_t *)desc; 346 if (idesc->bLength < USB_INTERFACE_DESCRIPTOR_SIZE) 347 break; 348 349 if (idesc->bInterfaceNumber != lastidx) { 350 lastidx = idesc->bInterfaceNumber; 351 curidx++; 352 curaidx = 0; 353 } else { 354 curaidx++; 355 } 356 if (ifaceidx == curidx && altidx == curaidx) 357 return idesc; 358 } 359 360 return NULL; 361 } 362 363 usb_endpoint_descriptor_t * 364 usbd_find_edesc(usb_config_descriptor_t *cd, int ifaceidx, int altidx, 365 int endptidx) 366 { 367 char *p = (char *)cd; 368 char *end = p + UGETW(cd->wTotalLength); 369 usb_interface_descriptor_t *idesc; 370 usb_endpoint_descriptor_t *edesc; 371 usb_descriptor_t *desc; 372 int curidx; 373 374 idesc = usbd_find_idesc(cd, ifaceidx, altidx); 375 if (idesc == NULL) 376 return NULL; 377 if (endptidx >= idesc->bNumEndpoints) /* quick exit */ 378 return NULL; 379 380 curidx = -1; 381 for (p = (char *)idesc + idesc->bLength; p < end; ) { 382 desc = (usb_descriptor_t *)p; 383 384 if (desc->bLength < USB_DESCRIPTOR_SIZE) 385 break; 386 p += desc->bLength; 387 if (p > end) 388 break; 389 390 if (desc->bDescriptorType == UDESC_INTERFACE) 391 break; 392 if (desc->bDescriptorType != UDESC_ENDPOINT) 393 continue; 394 395 edesc = (usb_endpoint_descriptor_t *)desc; 396 if (edesc->bLength < USB_ENDPOINT_DESCRIPTOR_SIZE) 397 break; 398 399 curidx++; 400 if (curidx == endptidx) 401 return edesc; 402 } 403 return NULL; 404 } 405 406 static void 407 usbd_iface_init(struct usbd_device *dev, int ifaceidx) 408 { 409 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx]; 410 411 memset(ifc, 0, sizeof(*ifc)); 412 413 ifc->ui_dev = dev; 414 ifc->ui_idesc = NULL; 415 ifc->ui_index = 0; 416 ifc->ui_altindex = 0; 417 ifc->ui_endpoints = NULL; 418 ifc->ui_busy = 0; 419 } 420 421 static void 422 usbd_iface_fini(struct usbd_device *dev, int ifaceidx) 423 { 424 struct usbd_interface *ifc __diagused = &dev->ud_ifaces[ifaceidx]; 425 426 KASSERT(ifc->ui_dev == dev); 427 KASSERT(ifc->ui_idesc == NULL); 428 KASSERT(ifc->ui_index == 0); 429 KASSERT(ifc->ui_altindex == 0); 430 KASSERT(ifc->ui_endpoints == NULL); 431 KASSERTMSG(ifc->ui_busy == 0, "%"PRId64, ifc->ui_busy); 432 } 433 434 /* 435 * usbd_iface_lock/locked/unlock, usbd_iface_piperef/pipeunref 436 * 437 * We lock the interface while we are setting it, and we acquire a 438 * reference to the interface for each pipe opened on it. 439 * 440 * Setting the interface while pipes are open is forbidden, and 441 * opening pipes while the interface is being set is forbidden. 442 */ 443 444 bool 445 usbd_iface_locked(struct usbd_interface *iface) 446 { 447 bool locked; 448 449 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 450 locked = (iface->ui_busy == -1); 451 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 452 453 return locked; 454 } 455 456 static void 457 usbd_iface_exlock(struct usbd_interface *iface) 458 { 459 460 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 461 KASSERTMSG(iface->ui_busy == 0, "interface is not idle," 462 " busy=%"PRId64, iface->ui_busy); 463 iface->ui_busy = -1; 464 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 465 } 466 467 usbd_status 468 usbd_iface_lock(struct usbd_interface *iface) 469 { 470 usbd_status err; 471 472 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 473 KASSERTMSG(iface->ui_busy != -1, "interface is locked"); 474 KASSERTMSG(iface->ui_busy >= 0, "%"PRId64, iface->ui_busy); 475 if (iface->ui_busy) { 476 err = USBD_IN_USE; 477 } else { 478 iface->ui_busy = -1; 479 err = 0; 480 } 481 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 482 483 return err; 484 } 485 486 void 487 usbd_iface_unlock(struct usbd_interface *iface) 488 { 489 490 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 491 KASSERTMSG(iface->ui_busy == -1, "interface is not locked," 492 " busy=%"PRId64, iface->ui_busy); 493 iface->ui_busy = 0; 494 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 495 } 496 497 usbd_status 498 usbd_iface_piperef(struct usbd_interface *iface) 499 { 500 usbd_status err; 501 502 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 503 KASSERTMSG(iface->ui_busy >= -1, "%"PRId64, iface->ui_busy); 504 if (iface->ui_busy == -1) { 505 err = USBD_IN_USE; 506 } else { 507 iface->ui_busy++; 508 err = 0; 509 } 510 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 511 512 return err; 513 } 514 515 void 516 usbd_iface_pipeunref(struct usbd_interface *iface) 517 { 518 519 mutex_enter(iface->ui_dev->ud_bus->ub_lock); 520 KASSERTMSG(iface->ui_busy != -1, "interface is locked"); 521 KASSERTMSG(iface->ui_busy != 0, "interface not in use"); 522 KASSERTMSG(iface->ui_busy >= 1, "%"PRId64, iface->ui_busy); 523 iface->ui_busy--; 524 mutex_exit(iface->ui_dev->ud_bus->ub_lock); 525 } 526 527 usbd_status 528 usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx) 529 { 530 USBHIST_FUNC(); 531 USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd", 532 ifaceidx, altidx, 0, 0); 533 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx]; 534 usb_interface_descriptor_t *idesc; 535 struct usbd_endpoint *endpoints; 536 char *p, *end; 537 int endpt, nendpt; 538 539 KASSERT(ifc->ui_dev == dev); 540 KASSERT(usbd_iface_locked(ifc)); 541 542 idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx); 543 if (idesc == NULL) 544 return USBD_INVAL; 545 546 nendpt = idesc->bNumEndpoints; 547 DPRINTFN(4, "found idesc nendpt=%jd", nendpt, 0, 0, 0); 548 if (nendpt != 0) { 549 endpoints = kmem_alloc(nendpt * sizeof(struct usbd_endpoint), 550 KM_SLEEP); 551 } else 552 endpoints = NULL; 553 554 p = (char *)idesc + idesc->bLength; 555 end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength); 556 #define ed ((usb_endpoint_descriptor_t *)p) 557 for (endpt = 0; endpt < nendpt; endpt++) { 558 DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0); 559 for (; p < end; p += ed->bLength) { 560 DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd", 561 (uintptr_t)p, (uintptr_t)end, ed->bLength, 562 ed->bDescriptorType); 563 if (p + ed->bLength <= end && 564 ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE && 565 ed->bDescriptorType == UDESC_ENDPOINT) 566 goto found; 567 if (ed->bLength == 0 || 568 ed->bDescriptorType == UDESC_INTERFACE) 569 break; 570 } 571 /* passed end, or bad desc */ 572 if (p < end) { 573 if (ed->bLength == 0) { 574 printf("%s: bad descriptor: 0 length\n", 575 __func__); 576 } else { 577 printf("%s: bad descriptor: iface desc\n", 578 __func__); 579 } 580 } else { 581 printf("%s: no desc found\n", __func__); 582 } 583 goto bad; 584 found: 585 endpoints[endpt].ue_edesc = ed; 586 if (dev->ud_speed == USB_SPEED_HIGH) { 587 u_int mps; 588 /* Control and bulk endpoints have max packet limits. */ 589 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 590 case UE_CONTROL: 591 mps = USB_2_MAX_CTRL_PACKET; 592 goto check; 593 case UE_BULK: 594 mps = USB_2_MAX_BULK_PACKET; 595 check: 596 if (UGETW(ed->wMaxPacketSize) != mps) { 597 USETW(ed->wMaxPacketSize, mps); 598 #ifdef DIAGNOSTIC 599 printf("usbd_fill_iface_data: bad max " 600 "packet size\n"); 601 #endif 602 } 603 break; 604 default: 605 break; 606 } 607 } 608 endpoints[endpt].ue_refcnt = 0; 609 endpoints[endpt].ue_toggle = 0; 610 p += ed->bLength; 611 } 612 #undef ed 613 614 /* Success! Free the old endpoints and commit the changes. */ 615 if (ifc->ui_endpoints) { 616 kmem_free(ifc->ui_endpoints, (sizeof(ifc->ui_endpoints[0]) * 617 ifc->ui_idesc->bNumEndpoints)); 618 } 619 620 ifc->ui_idesc = idesc; 621 ifc->ui_index = ifaceidx; 622 ifc->ui_altindex = altidx; 623 ifc->ui_endpoints = endpoints; 624 625 return USBD_NORMAL_COMPLETION; 626 627 bad: 628 if (endpoints) 629 kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint)); 630 return USBD_INVAL; 631 } 632 633 Static void 634 usbd_free_iface_data(struct usbd_device *dev, int ifcno) 635 { 636 struct usbd_interface *ifc = &dev->ud_ifaces[ifcno]; 637 638 KASSERT(ifc->ui_dev == dev); 639 KASSERT(ifc->ui_idesc != NULL); 640 KASSERT(usbd_iface_locked(ifc)); 641 642 if (ifc->ui_endpoints) { 643 int nendpt = ifc->ui_idesc->bNumEndpoints; 644 size_t sz = nendpt * sizeof(struct usbd_endpoint); 645 kmem_free(ifc->ui_endpoints, sz); 646 ifc->ui_endpoints = NULL; 647 } 648 649 ifc->ui_altindex = 0; 650 ifc->ui_index = 0; 651 ifc->ui_idesc = NULL; 652 } 653 654 usbd_status 655 usbd_set_config_no(struct usbd_device *dev, int no, int msg) 656 { 657 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0); 658 usb_config_descriptor_t cd; 659 usbd_status err; 660 int index; 661 662 if (no == USB_UNCONFIG_NO) 663 return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg); 664 665 /* Figure out what config index to use. */ 666 for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) { 667 err = usbd_get_config_desc(dev, index, &cd); 668 if (err) 669 return err; 670 if (cd.bConfigurationValue == no) 671 return usbd_set_config_index(dev, index, msg); 672 } 673 return USBD_INVAL; 674 } 675 676 usbd_status 677 usbd_set_config_index(struct usbd_device *dev, int index, int msg) 678 { 679 USBHIST_FUNC(); 680 USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd", 681 (uintptr_t)dev, index, 0, 0); 682 usb_config_descriptor_t cd, *cdp; 683 usb_bos_descriptor_t *bdp = NULL; 684 usbd_status err; 685 int i, ifcidx, nifc, len, selfpowered, power; 686 687 688 if (index >= dev->ud_ddesc.bNumConfigurations && 689 index != USB_UNCONFIG_INDEX) { 690 /* panic? */ 691 printf("usbd_set_config_index: illegal index\n"); 692 return USBD_INVAL; 693 } 694 695 /* XXX check that all interfaces are idle */ 696 if (dev->ud_config != USB_UNCONFIG_NO) { 697 DPRINTF("free old config", 0, 0, 0, 0); 698 /* Free all configuration data structures. */ 699 nifc = dev->ud_cdesc->bNumInterface; 700 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 701 usbd_iface_exlock(&dev->ud_ifaces[ifcidx]); 702 usbd_free_iface_data(dev, ifcidx); 703 usbd_iface_unlock(&dev->ud_ifaces[ifcidx]); 704 usbd_iface_fini(dev, ifcidx); 705 } 706 kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface)); 707 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength)); 708 if (dev->ud_bdesc != NULL) 709 kmem_free(dev->ud_bdesc, 710 UGETW(dev->ud_bdesc->wTotalLength)); 711 dev->ud_ifaces = NULL; 712 dev->ud_cdesc = NULL; 713 dev->ud_bdesc = NULL; 714 dev->ud_config = USB_UNCONFIG_NO; 715 } 716 717 if (index == USB_UNCONFIG_INDEX) { 718 /* We are unconfiguring the device, so leave unallocated. */ 719 DPRINTF("set config 0", 0, 0, 0, 0); 720 err = usbd_set_config(dev, USB_UNCONFIG_NO); 721 if (err) { 722 DPRINTF("setting config=0 failed, err = %jd", err, 723 0, 0, 0); 724 } 725 return err; 726 } 727 728 /* Get the short descriptor. */ 729 err = usbd_get_config_desc(dev, index, &cd); 730 if (err) { 731 DPRINTF("get_config_desc=%jd", err, 0, 0, 0); 732 return err; 733 } 734 len = UGETW(cd.wTotalLength); 735 if (len < USB_CONFIG_DESCRIPTOR_SIZE) { 736 DPRINTF("empty short descriptor", 0, 0, 0, 0); 737 return USBD_INVAL; 738 } 739 cdp = kmem_alloc(len, KM_SLEEP); 740 741 /* Get the full descriptor. Try a few times for slow devices. */ 742 for (i = 0; i < 3; i++) { 743 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp); 744 if (!err) 745 break; 746 usbd_delay_ms(dev, 200); 747 } 748 if (err) { 749 DPRINTF("get_desc=%jd", err, 0, 0, 0); 750 goto bad; 751 } 752 if (cdp->bDescriptorType != UDESC_CONFIG) { 753 DPRINTF("bad desc %jd", cdp->bDescriptorType, 0, 0, 0); 754 err = USBD_INVAL; 755 goto bad; 756 } 757 if (UGETW(cdp->wTotalLength) != UGETW(cd.wTotalLength)) { 758 DPRINTF("bad len %jd", UGETW(cdp->wTotalLength), 0, 0, 0); 759 err = USBD_INVAL; 760 goto bad; 761 } 762 763 if (USB_IS_SS(dev->ud_speed)) { 764 usb_bos_descriptor_t bd; 765 766 /* get short bos desc */ 767 err = usbd_get_bos_desc(dev, index, &bd); 768 if (!err) { 769 int blen = UGETW(bd.wTotalLength); 770 if (blen < USB_BOS_DESCRIPTOR_SIZE) { 771 DPRINTF("empty bos descriptor", 0, 0, 0, 0); 772 err = USBD_INVAL; 773 goto bad; 774 } 775 bdp = kmem_alloc(blen, KM_SLEEP); 776 777 /* Get the full desc */ 778 for (i = 0; i < 3; i++) { 779 err = usbd_get_desc(dev, UDESC_BOS, index, blen, 780 bdp); 781 if (!err) 782 break; 783 usbd_delay_ms(dev, 200); 784 } 785 if (err || bdp->bDescriptorType != UDESC_BOS || 786 UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) { 787 DPRINTF("error %jd or bad desc %jd", err, 788 bdp->bDescriptorType, 0, 0); 789 kmem_free(bdp, blen); 790 bdp = NULL; 791 } 792 } 793 } 794 dev->ud_bdesc = bdp; 795 796 /* 797 * Figure out if the device is self or bus powered. 798 */ 799 #if 0 /* XXX various devices don't report the power state correctly */ 800 selfpowered = 0; 801 err = usbd_get_device_status(dev, &ds); 802 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 803 selfpowered = 1; 804 #endif 805 /* 806 * Use the power state in the configuration we are going 807 * to set. This doesn't necessarily reflect the actual 808 * power state of the device; the driver can control this 809 * by choosing the appropriate configuration. 810 */ 811 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED); 812 813 DPRINTF("addr %jd cno=%jd attr=0x%02jx, selfpowered=%jd", 814 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes, 815 selfpowered); 816 DPRINTF("max power=%jd", cdp->bMaxPower * 2, 0, 0, 0); 817 818 /* Check if we have enough power. */ 819 #if 0 /* this is a no-op, see above */ 820 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) { 821 if (msg) 822 printf("%s: device addr %d (config %d): " 823 "can't set self powered configuration\n", 824 device_xname(dev->ud_bus->bdev), dev->ud_addr, 825 cdp->bConfigurationValue); 826 err = USBD_NO_POWER; 827 goto bad; 828 } 829 #endif 830 #ifdef USB_DEBUG 831 if (dev->ud_powersrc == NULL) { 832 DPRINTF("No power source?", 0, 0, 0, 0); 833 err = USBD_IOERROR; 834 goto bad; 835 } 836 #endif 837 power = cdp->bMaxPower * 2; 838 if (power > dev->ud_powersrc->up_power) { 839 DPRINTF("power exceeded %jd %jd", power, 840 dev->ud_powersrc->up_power, 0, 0); 841 /* XXX print nicer message. */ 842 if (msg) 843 printf("%s: device addr %d (config %d) exceeds power " 844 "budget, %d mA > %d mA\n", 845 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr, 846 cdp->bConfigurationValue, 847 power, dev->ud_powersrc->up_power); 848 err = USBD_NO_POWER; 849 goto bad; 850 } 851 dev->ud_power = power; 852 dev->ud_selfpowered = selfpowered; 853 854 /* Set the actual configuration value. */ 855 DPRINTF("set config %jd", cdp->bConfigurationValue, 0, 0, 0); 856 err = usbd_set_config(dev, cdp->bConfigurationValue); 857 if (err) { 858 DPRINTF("setting config=%jd failed, error=%jd", 859 cdp->bConfigurationValue, err, 0, 0); 860 goto bad; 861 } 862 863 /* Allocate and fill interface data. */ 864 nifc = cdp->bNumInterface; 865 if (nifc == 0) { 866 DPRINTF("no interfaces", 0, 0, 0, 0); 867 err = USBD_INVAL; 868 goto bad; 869 } 870 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface), 871 KM_SLEEP); 872 DPRINTFN(5, "dev=%#jx cdesc=%#jx", (uintptr_t)dev, (uintptr_t)cdp, 873 0, 0); 874 dev->ud_cdesc = cdp; 875 dev->ud_config = cdp->bConfigurationValue; 876 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 877 usbd_iface_init(dev, ifcidx); 878 usbd_iface_exlock(&dev->ud_ifaces[ifcidx]); 879 err = usbd_fill_iface_data(dev, ifcidx, 0); 880 usbd_iface_unlock(&dev->ud_ifaces[ifcidx]); 881 if (err) { 882 while (--ifcidx >= 0) { 883 usbd_iface_exlock(&dev->ud_ifaces[ifcidx]); 884 usbd_free_iface_data(dev, ifcidx); 885 usbd_iface_unlock(&dev->ud_ifaces[ifcidx]); 886 usbd_iface_fini(dev, ifcidx); 887 } 888 kmem_free(dev->ud_ifaces, 889 nifc * sizeof(struct usbd_interface)); 890 dev->ud_ifaces = NULL; 891 goto bad; 892 } 893 } 894 895 return USBD_NORMAL_COMPLETION; 896 897 bad: 898 /* XXX Use usbd_set_config() to reset the config? */ 899 /* XXX Should we forbid USB_UNCONFIG_NO from bConfigurationValue? */ 900 dev->ud_config = USB_UNCONFIG_NO; 901 kmem_free(cdp, len); 902 dev->ud_cdesc = NULL; 903 if (bdp != NULL) { 904 kmem_free(bdp, UGETW(bdp->wTotalLength)); 905 dev->ud_bdesc = NULL; 906 } 907 return err; 908 } 909 910 /* XXX add function for alternate settings */ 911 912 usbd_status 913 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface, 914 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe) 915 { 916 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0); 917 } 918 919 usbd_status 920 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface, 921 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags) 922 { 923 USBHIST_FUNC(); 924 USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx", 925 (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep); 926 struct usbd_pipe *p = NULL; 927 bool ep_acquired = false; 928 usbd_status err; 929 930 /* Block exclusive use of the endpoint by later pipes. */ 931 err = usbd_endpoint_acquire(dev, ep, flags & USBD_EXCLUSIVE_USE); 932 if (err) 933 goto out; 934 ep_acquired = true; 935 936 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP); 937 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0); 938 p->up_dev = dev; 939 p->up_iface = iface; 940 p->up_endpoint = ep; 941 p->up_intrxfer = NULL; 942 p->up_running = 0; 943 p->up_aborting = 0; 944 p->up_serialise = true; 945 p->up_repeat = 0; 946 p->up_interval = ival; 947 p->up_flags = flags; 948 SIMPLEQ_INIT(&p->up_queue); 949 p->up_callingxfer = NULL; 950 cv_init(&p->up_callingcv, "usbpipecb"); 951 952 err = dev->ud_bus->ub_methods->ubm_open(p); 953 if (err) { 954 DPRINTF("endpoint=%#jx failed, error=%jd", 955 (uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0); 956 goto out; 957 } 958 959 KASSERT(p->up_methods->upm_start || p->up_serialise == false); 960 961 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p, 962 USB_TASKQ_MPSAFE); 963 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0); 964 *pipe = p; 965 p = NULL; /* handed off to caller */ 966 ep_acquired = false; /* handed off to pipe */ 967 err = USBD_NORMAL_COMPLETION; 968 969 out: if (p) { 970 cv_destroy(&p->up_callingcv); 971 kmem_free(p, dev->ud_bus->ub_pipesize); 972 } 973 if (ep_acquired) 974 usbd_endpoint_release(dev, ep); 975 return err; 976 } 977 978 usbd_status 979 usbd_endpoint_acquire(struct usbd_device *dev, struct usbd_endpoint *ep, 980 int flags) 981 { 982 usbd_status err; 983 984 mutex_enter(dev->ud_bus->ub_lock); 985 if (ep->ue_refcnt == INT_MAX) { 986 err = USBD_IN_USE; /* XXX rule out or switch to 64-bit */ 987 } else if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt) { 988 err = USBD_IN_USE; 989 } else { 990 ep->ue_refcnt++; 991 err = 0; 992 } 993 mutex_exit(dev->ud_bus->ub_lock); 994 995 return err; 996 } 997 998 void 999 usbd_endpoint_release(struct usbd_device *dev, struct usbd_endpoint *ep) 1000 { 1001 1002 mutex_enter(dev->ud_bus->ub_lock); 1003 KASSERT(ep->ue_refcnt); 1004 ep->ue_refcnt--; 1005 mutex_exit(dev->ud_bus->ub_lock); 1006 } 1007 1008 /* Abort and close the device control pipe. */ 1009 void 1010 usbd_kill_pipe(struct usbd_pipe *pipe) 1011 { 1012 1013 usbd_abort_pipe(pipe); 1014 usbd_close_pipe(pipe); 1015 } 1016 1017 int 1018 usbd_getnewaddr(struct usbd_bus *bus) 1019 { 1020 int addr; 1021 1022 for (addr = 1; addr < USB_MAX_DEVICES; addr++) { 1023 size_t dindex = usb_addr2dindex(addr); 1024 if (bus->ub_devices[dindex] == NULL) 1025 return addr; 1026 } 1027 return -1; 1028 } 1029 1030 usbd_status 1031 usbd_attach_roothub(device_t parent, struct usbd_device *dev) 1032 { 1033 struct usb_attach_arg uaa; 1034 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1035 device_t dv; 1036 1037 uaa.uaa_device = dev; 1038 uaa.uaa_usegeneric = 0; 1039 uaa.uaa_port = 0; 1040 uaa.uaa_vendor = UGETW(dd->idVendor); 1041 uaa.uaa_product = UGETW(dd->idProduct); 1042 uaa.uaa_release = UGETW(dd->bcdDevice); 1043 uaa.uaa_class = dd->bDeviceClass; 1044 uaa.uaa_subclass = dd->bDeviceSubClass; 1045 uaa.uaa_proto = dd->bDeviceProtocol; 1046 1047 KERNEL_LOCK(1, curlwp); 1048 dv = config_found(parent, &uaa, NULL, 1049 CFARGS(.iattr = "usbroothubif")); 1050 KERNEL_UNLOCK_ONE(curlwp); 1051 if (dv) { 1052 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP); 1053 dev->ud_subdevs[0] = dv; 1054 dev->ud_subdevlen = 1; 1055 } 1056 return USBD_NORMAL_COMPLETION; 1057 } 1058 1059 static void 1060 usbd_properties(device_t dv, struct usbd_device *dev) 1061 { 1062 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1063 prop_dictionary_t dict = device_properties(dv); 1064 int class, subclass, release, proto, vendor, product; 1065 1066 class = dd->bDeviceClass; 1067 subclass = dd->bDeviceSubClass; 1068 release = UGETW(dd->bcdDevice); 1069 proto = dd->bDeviceProtocol; 1070 vendor = UGETW(dd->idVendor); 1071 product = UGETW(dd->idProduct); 1072 1073 prop_dictionary_set_uint8(dict, "class", class); 1074 prop_dictionary_set_uint8(dict, "subclass", subclass); 1075 prop_dictionary_set_uint16(dict, "release", release); 1076 prop_dictionary_set_uint8(dict, "proto", proto); 1077 prop_dictionary_set_uint16(dict, "vendor-id", vendor); 1078 prop_dictionary_set_uint16(dict, "product-id", product); 1079 1080 if (dev->ud_vendor) { 1081 prop_dictionary_set_string(dict, 1082 "vendor-string", dev->ud_vendor); 1083 } 1084 if (dev->ud_product) { 1085 prop_dictionary_set_string(dict, 1086 "product-string", dev->ud_product); 1087 } 1088 if (dev->ud_serial) { 1089 prop_dictionary_set_string(dict, 1090 "serialnumber", dev->ud_serial); 1091 } 1092 } 1093 1094 static usbd_status 1095 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port, 1096 int usegeneric) 1097 { 1098 struct usb_attach_arg uaa; 1099 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1100 device_t dv; 1101 int dlocs[USBDEVIFCF_NLOCS]; 1102 1103 KASSERT(usb_in_event_thread(parent)); 1104 1105 uaa.uaa_device = dev; 1106 uaa.uaa_usegeneric = usegeneric; 1107 uaa.uaa_port = port; 1108 uaa.uaa_vendor = UGETW(dd->idVendor); 1109 uaa.uaa_product = UGETW(dd->idProduct); 1110 uaa.uaa_release = UGETW(dd->bcdDevice); 1111 uaa.uaa_class = dd->bDeviceClass; 1112 uaa.uaa_subclass = dd->bDeviceSubClass; 1113 uaa.uaa_proto = dd->bDeviceProtocol; 1114 1115 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port; 1116 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor; 1117 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product; 1118 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release; 1119 /* the rest is historical ballast */ 1120 dlocs[USBDEVIFCF_CONFIGURATION] = -1; 1121 dlocs[USBDEVIFCF_INTERFACE] = -1; 1122 1123 KERNEL_LOCK(1, curlwp); 1124 config_pending_incr(parent); 1125 dv = config_found(parent, &uaa, usbd_print, 1126 CFARGS(.submatch = config_stdsubmatch, 1127 .iattr = "usbdevif", 1128 .locators = dlocs)); 1129 KERNEL_UNLOCK_ONE(curlwp); 1130 if (dv) { 1131 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP); 1132 dev->ud_subdevs[0] = dv; 1133 dev->ud_subdevlen = 1; 1134 dev->ud_nifaces_claimed = 1; /* XXX */ 1135 usbd_properties(dv, dev); 1136 } 1137 config_pending_decr(parent); 1138 return USBD_NORMAL_COMPLETION; 1139 } 1140 1141 static usbd_status 1142 usbd_attachinterfaces(device_t parent, struct usbd_device *dev, 1143 int port, const int *locators) 1144 { 1145 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1146 struct usbif_attach_arg uiaa; 1147 int ilocs[USBIFIFCF_NLOCS]; 1148 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1149 int nifaces; 1150 struct usbd_interface **ifaces; 1151 int i, j, loc; 1152 device_t dv; 1153 1154 KASSERT(usb_in_event_thread(parent)); 1155 1156 nifaces = dev->ud_cdesc->bNumInterface; 1157 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP); 1158 for (i = 0; i < nifaces; i++) { 1159 if (!dev->ud_subdevs[i]) { 1160 ifaces[i] = &dev->ud_ifaces[i]; 1161 } 1162 DPRINTF("interface %jd %#jx", i, (uintptr_t)ifaces[i], 0, 0); 1163 } 1164 1165 1166 uiaa.uiaa_device = dev; 1167 uiaa.uiaa_port = port; 1168 uiaa.uiaa_vendor = UGETW(dd->idVendor); 1169 uiaa.uiaa_product = UGETW(dd->idProduct); 1170 uiaa.uiaa_release = UGETW(dd->bcdDevice); 1171 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue; 1172 uiaa.uiaa_ifaces = ifaces; 1173 uiaa.uiaa_nifaces = nifaces; 1174 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port; 1175 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor; 1176 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product; 1177 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release; 1178 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno; 1179 1180 for (i = 0; i < nifaces; i++) { 1181 if (!ifaces[i]) { 1182 DPRINTF("interface %jd claimed", i, 0, 0, 0); 1183 continue; /* interface already claimed */ 1184 } 1185 uiaa.uiaa_iface = ifaces[i]; 1186 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass; 1187 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass; 1188 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol; 1189 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber; 1190 1191 DPRINTF("searching for interface %jd...", i, 0, 0, 0); 1192 DPRINTF("class %jx subclass %jx proto %jx ifaceno %jd", 1193 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto, 1194 uiaa.uiaa_ifaceno); 1195 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno; 1196 if (locators != NULL) { 1197 loc = locators[USBIFIFCF_CONFIGURATION]; 1198 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT && 1199 loc != uiaa.uiaa_configno) 1200 continue; 1201 loc = locators[USBIFIFCF_INTERFACE]; 1202 if (loc != USBIFIFCF_INTERFACE_DEFAULT && 1203 loc != uiaa.uiaa_ifaceno) 1204 continue; 1205 } 1206 KERNEL_LOCK(1, curlwp); 1207 dv = config_found(parent, &uiaa, usbd_ifprint, 1208 CFARGS(.submatch = config_stdsubmatch, 1209 .iattr = "usbifif", 1210 .locators = ilocs)); 1211 KERNEL_UNLOCK_ONE(curlwp); 1212 if (!dv) 1213 continue; 1214 1215 usbd_properties(dv, dev); 1216 1217 /* claim */ 1218 ifaces[i] = NULL; 1219 /* account for ifaces claimed by the driver behind our back */ 1220 for (j = 0; j < nifaces; j++) { 1221 1222 if (!ifaces[j] && !dev->ud_subdevs[j]) { 1223 DPRINTF("interface %jd claimed behind our back", 1224 j, 0, 0, 0); 1225 dev->ud_subdevs[j] = dv; 1226 dev->ud_nifaces_claimed++; 1227 } 1228 } 1229 } 1230 1231 kmem_free(ifaces, nifaces * sizeof(*ifaces)); 1232 return USBD_NORMAL_COMPLETION; 1233 } 1234 1235 usbd_status 1236 usbd_probe_and_attach(device_t parent, struct usbd_device *dev, 1237 int port, int addr) 1238 { 1239 USBHIST_FUNC(); 1240 USBHIST_CALLARGS(usbdebug, "trying device specific drivers", 0, 0, 0, 0); 1241 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1242 int confi, nifaces; 1243 usbd_status err; 1244 1245 KASSERT(usb_in_event_thread(parent)); 1246 1247 /* First try with device specific drivers. */ 1248 err = usbd_attachwholedevice(parent, dev, port, 0); 1249 if (dev->ud_nifaces_claimed || err) 1250 return err; 1251 DPRINTF("no device specific driver found", 0, 0, 0, 0); 1252 1253 DPRINTF("looping over %jd configurations", dd->bNumConfigurations, 1254 0, 0, 0); 1255 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 1256 DPRINTFN(1, "trying config idx=%jd", confi, 0, 0, 0); 1257 err = usbd_set_config_index(dev, confi, 1); 1258 if (err) { 1259 DPRINTF("port %jd, set config at addr %jd failed, " 1260 "error=%jd", port, addr, err, 0); 1261 printf("%s: port %d, set config at addr %d failed\n", 1262 device_xname(parent), port, addr); 1263 return err; 1264 } 1265 nifaces = dev->ud_cdesc->bNumInterface; 1266 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t), 1267 KM_SLEEP); 1268 dev->ud_subdevlen = nifaces; 1269 1270 err = usbd_attachinterfaces(parent, dev, port, NULL); 1271 1272 if (dev->ud_subdevs && dev->ud_nifaces_claimed == 0) { 1273 kmem_free(dev->ud_subdevs, 1274 dev->ud_subdevlen * sizeof(device_t)); 1275 dev->ud_subdevs = 0; 1276 dev->ud_subdevlen = 0; 1277 } 1278 if (dev->ud_nifaces_claimed || err) 1279 return err; 1280 } 1281 /* No interfaces were attached in any of the configurations. */ 1282 1283 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 1284 usbd_set_config_index(dev, 0, 0); 1285 1286 DPRINTF("no interface drivers found", 0, 0, 0, 0); 1287 1288 /* Finally try the generic driver. */ 1289 err = usbd_attachwholedevice(parent, dev, port, 1); 1290 1291 /* 1292 * The generic attach failed, but leave the device as it is. 1293 * We just did not find any drivers, that's all. The device is 1294 * fully operational and not harming anyone. 1295 */ 1296 DPRINTF("generic attach failed", 0, 0, 0, 0); 1297 1298 return USBD_NORMAL_COMPLETION; 1299 } 1300 1301 /** 1302 * Called from uhub_rescan(). usbd_new_device() for the target dev must be 1303 * called before calling this. 1304 */ 1305 usbd_status 1306 usbd_reattach_device(device_t parent, struct usbd_device *dev, 1307 int port, const int *locators) 1308 { 1309 int i, loc; 1310 1311 USBHIST_FUNC(); 1312 USBHIST_CALLARGS(usbdebug, "uhub%jd port=%jd", 1313 device_unit(parent), port, 0, 0); 1314 1315 KASSERT(usb_in_event_thread(parent)); 1316 1317 if (locators != NULL) { 1318 loc = locators[USBIFIFCF_PORT]; 1319 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port) 1320 return USBD_NORMAL_COMPLETION; 1321 loc = locators[USBIFIFCF_VENDOR]; 1322 if (loc != USBIFIFCF_VENDOR_DEFAULT && 1323 loc != UGETW(dev->ud_ddesc.idVendor)) 1324 return USBD_NORMAL_COMPLETION; 1325 loc = locators[USBIFIFCF_PRODUCT]; 1326 if (loc != USBIFIFCF_PRODUCT_DEFAULT && 1327 loc != UGETW(dev->ud_ddesc.idProduct)) 1328 return USBD_NORMAL_COMPLETION; 1329 loc = locators[USBIFIFCF_RELEASE]; 1330 if (loc != USBIFIFCF_RELEASE_DEFAULT && 1331 loc != UGETW(dev->ud_ddesc.bcdDevice)) 1332 return USBD_NORMAL_COMPLETION; 1333 } 1334 if (dev->ud_subdevlen == 0) { 1335 /* XXX: check USBIFIFCF_CONFIGURATION and 1336 * USBIFIFCF_INTERFACE too */ 1337 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr); 1338 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) { 1339 /* device-specific or generic driver is already attached. */ 1340 return USBD_NORMAL_COMPLETION; 1341 } 1342 /* Does the device have unconfigured interfaces? */ 1343 for (i = 0; i < dev->ud_subdevlen; i++) { 1344 if (dev->ud_subdevs[i] == NULL) { 1345 break; 1346 } 1347 } 1348 if (i >= dev->ud_subdevlen) 1349 return USBD_NORMAL_COMPLETION; 1350 return usbd_attachinterfaces(parent, dev, port, locators); 1351 } 1352 1353 /* 1354 * Called when a new device has been put in the powered state, 1355 * but not yet in the addressed state. 1356 * Get initial descriptor, set the address, get full descriptor, 1357 * and attach a driver. 1358 */ 1359 usbd_status 1360 usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed, 1361 int port, struct usbd_port *up) 1362 { 1363 USBHIST_FUNC(); 1364 USBHIST_CALLARGS(usbdebug, "bus=%#jx port=%jd depth=%jd speed=%jd", 1365 (uintptr_t)bus, port, depth, speed); 1366 struct usbd_device *dev, *adev; 1367 struct usbd_device *hub; 1368 usb_device_descriptor_t *dd; 1369 usb_port_status_t ps; 1370 usbd_status err; 1371 int addr; 1372 int i; 1373 int p; 1374 1375 KASSERT(usb_in_event_thread(parent)); 1376 1377 if (bus->ub_methods->ubm_newdev != NULL) 1378 return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed, 1379 port, up); 1380 1381 addr = usbd_getnewaddr(bus); 1382 if (addr < 0) { 1383 printf("%s: No free USB addresses, new device ignored.\n", 1384 device_xname(bus->ub_usbctl)); 1385 return USBD_NO_ADDR; 1386 } 1387 1388 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 1389 dev->ud_bus = bus; 1390 1391 /* Set up default endpoint handle. */ 1392 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc; 1393 1394 /* Set up default endpoint descriptor. */ 1395 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1396 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT; 1397 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1398 dev->ud_ep0desc.bmAttributes = UE_CONTROL; 1399 /* 1400 * temporary, will be fixed after first descriptor fetch 1401 * (which uses 64 bytes so it shouldn't be less), 1402 * highspeed devices must support 64 byte packets anyway 1403 */ 1404 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL) 1405 USETW(dev->ud_ep0desc.wMaxPacketSize, 64); 1406 else 1407 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET); 1408 1409 dev->ud_ep0desc.bInterval = 0; 1410 1411 /* doesn't matter, just don't leave it uninitialized */ 1412 dev->ud_ep0.ue_toggle = 0; 1413 1414 dev->ud_quirks = &usbd_no_quirk; 1415 dev->ud_addr = USB_START_ADDR; 1416 dev->ud_ddesc.bMaxPacketSize = 0; 1417 dev->ud_depth = depth; 1418 dev->ud_powersrc = up; 1419 dev->ud_myhub = up->up_parent; 1420 1421 up->up_dev = dev; 1422 1423 /* Locate port on upstream high speed hub */ 1424 for (adev = dev, hub = up->up_parent; 1425 hub != NULL && hub->ud_speed != USB_SPEED_HIGH; 1426 adev = hub, hub = hub->ud_myhub) 1427 ; 1428 if (hub) { 1429 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) { 1430 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) { 1431 dev->ud_myhsport = 1432 &hub->ud_hub->uh_ports[p - 1]; 1433 goto found; 1434 } 1435 } 1436 panic("usbd_new_device: cannot find HS port"); 1437 found: 1438 DPRINTFN(1, "high speed port %jd", p, 0, 0, 0); 1439 } else { 1440 dev->ud_myhsport = NULL; 1441 } 1442 dev->ud_speed = speed; 1443 dev->ud_langid = USBD_NOLANG; 1444 dev->ud_cookie.cookie = ++usb_cookie_no; 1445 1446 /* Establish the default pipe. */ 1447 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1448 &dev->ud_pipe0, USBD_MPSAFE); 1449 if (err) { 1450 usbd_remove_device(dev, up); 1451 return err; 1452 } 1453 1454 dd = &dev->ud_ddesc; 1455 /* Try a few times in case the device is slow (i.e. outside specs.) */ 1456 for (i = 0; i < 10; i++) { 1457 /* Get the first 8 bytes of the device descriptor. */ 1458 err = usbd_get_initial_ddesc(dev, dd); 1459 if (!err) 1460 break; 1461 /* 1462 * The root hub can never fail to give the initial descriptor, 1463 * but assert it just in case. 1464 */ 1465 KASSERT(up->up_parent); 1466 usbd_delay_ms(dev, 200); 1467 if ((i & 3) == 3) 1468 usbd_reset_port(up->up_parent, port, &ps); 1469 } 1470 if (err) { 1471 DPRINTF("addr=%jd, getting first desc failed: %jd", addr, err, 1472 0, 0); 1473 usbd_remove_device(dev, up); 1474 return err; 1475 } 1476 1477 /* Windows resets the port here, do likewise */ 1478 if (up->up_parent) 1479 usbd_reset_port(up->up_parent, port, &ps); 1480 1481 if (speed == USB_SPEED_HIGH) { 1482 /* Max packet size must be 64 (sec 5.5.3). */ 1483 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { 1484 #ifdef DIAGNOSTIC 1485 printf("usbd_new_device: addr=%d bad max packet " 1486 "size=%d. adjusting to %d.\n", 1487 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET); 1488 #endif 1489 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; 1490 } 1491 } 1492 1493 DPRINTF("adding unit addr=%jd, rev=%02jx, class=%jd, subclass=%jd", 1494 addr, UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass); 1495 DPRINTF("protocol=%jd, maxpacket=%jd, len=%jd, speed=%jd", 1496 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed); 1497 1498 if (dd->bDescriptorType != UDESC_DEVICE) { 1499 /* Illegal device descriptor */ 1500 DPRINTF("illegal descriptor %jd", dd->bDescriptorType, 0, 0, 0); 1501 usbd_remove_device(dev, up); 1502 return USBD_INVAL; 1503 } 1504 1505 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { 1506 DPRINTF("bad length %jd", dd->bLength, 0, 0, 0); 1507 usbd_remove_device(dev, up); 1508 return USBD_INVAL; 1509 } 1510 1511 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize); 1512 1513 /* Re-establish the default pipe with the new MPS. */ 1514 usbd_kill_pipe(dev->ud_pipe0); 1515 dev->ud_pipe0 = NULL; 1516 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1517 &dev->ud_pipe0, USBD_MPSAFE); 1518 if (err) { 1519 DPRINTF("setup default pipe failed err %jd", err, 0, 0, 0); 1520 usbd_remove_device(dev, up); 1521 return err; 1522 } 1523 1524 /* Set the address */ 1525 DPRINTFN(5, "setting device address=%jd", addr, 0, 0, 0); 1526 err = usbd_set_address(dev, addr); 1527 if (err) { 1528 DPRINTF("set address %jd failed, err = %jd", addr, err, 0, 0); 1529 err = USBD_SET_ADDR_FAILED; 1530 usbd_remove_device(dev, up); 1531 return err; 1532 } 1533 1534 /* Allow device time to set new address */ 1535 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 1536 dev->ud_addr = addr; /* new device address now */ 1537 bus->ub_devices[usb_addr2dindex(addr)] = dev; 1538 1539 /* Re-establish the default pipe with the new address. */ 1540 usbd_kill_pipe(dev->ud_pipe0); 1541 dev->ud_pipe0 = NULL; 1542 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1543 &dev->ud_pipe0, USBD_MPSAFE); 1544 if (err) { 1545 DPRINTF("setup default pipe failed, err = %jd", err, 0, 0, 0); 1546 usbd_remove_device(dev, up); 1547 return err; 1548 } 1549 1550 err = usbd_reload_device_desc(dev); 1551 if (err) { 1552 DPRINTF("addr=%jd, getting full desc failed, err = %jd", addr, 1553 err, 0, 0); 1554 usbd_remove_device(dev, up); 1555 return err; 1556 } 1557 1558 /* Assume 100mA bus powered for now. Changed when configured. */ 1559 dev->ud_power = USB_MIN_POWER; 1560 dev->ud_selfpowered = 0; 1561 1562 DPRINTF("new dev (addr %jd), dev=%#jx, parent=%#jx", 1563 addr, (uintptr_t)dev, (uintptr_t)parent, 0); 1564 1565 usbd_get_device_strings(dev); 1566 1567 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1568 1569 if (port == 0) { /* root hub */ 1570 KASSERT(addr == 1); 1571 usbd_attach_roothub(parent, dev); 1572 return USBD_NORMAL_COMPLETION; 1573 } 1574 1575 err = usbd_probe_and_attach(parent, dev, port, addr); 1576 if (err) { 1577 usbd_remove_device(dev, up); 1578 return err; 1579 } 1580 1581 return USBD_NORMAL_COMPLETION; 1582 } 1583 1584 usbd_status 1585 usbd_reload_device_desc(struct usbd_device *dev) 1586 { 1587 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1588 usb_device_descriptor_t *udd = &dev->ud_ddesc; 1589 usbd_status err; 1590 1591 /* Get the full device descriptor. */ 1592 err = usbd_get_device_desc(dev, udd); 1593 if (err) 1594 return err; 1595 if (udd->bDescriptorType != UDESC_DEVICE) 1596 return USBD_INVAL; 1597 if (udd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) 1598 return USBD_INVAL; 1599 1600 DPRINTFN(15, "bLength %5ju", udd->bLength, 0, 0, 0); 1601 DPRINTFN(15, "bDescriptorType %5ju", udd->bDescriptorType, 0, 0, 0); 1602 DPRINTFN(15, "bcdUSB %2jx.%02jx", udd->bcdUSB[1], 1603 udd->bcdUSB[0], 0, 0); 1604 DPRINTFN(15, "bDeviceClass %5ju", udd->bDeviceClass, 0, 0, 0); 1605 DPRINTFN(15, "bDeviceSubClass %5ju", udd->bDeviceSubClass, 0, 0, 0); 1606 DPRINTFN(15, "bDeviceProtocol %5ju", udd->bDeviceProtocol, 0, 0, 0); 1607 DPRINTFN(15, "bMaxPacketSize0 %5ju", udd->bMaxPacketSize, 0, 0, 0); 1608 DPRINTFN(15, "idVendor 0x%02jx 0x%02jx", 1609 udd->idVendor[0], 1610 udd->idVendor[1], 0, 0); 1611 DPRINTFN(15, "idProduct 0x%02jx 0x%02jx", 1612 udd->idProduct[0], 1613 udd->idProduct[1], 0, 0); 1614 DPRINTFN(15, "bcdDevice %2jx.%02jx", udd->bcdDevice[1], 1615 udd->bcdDevice[0], 0, 0); 1616 DPRINTFN(15, "iManufacturer %5ju", udd->iManufacturer, 0, 0, 0); 1617 DPRINTFN(15, "iProduct %5ju", udd->iProduct, 0, 0, 0); 1618 DPRINTFN(15, "iSerial %5ju", udd->iSerialNumber, 0, 0, 0); 1619 DPRINTFN(15, "bNumConfigurations %5ju", udd->bNumConfigurations, 0, 0, 1620 0); 1621 1622 /* Figure out what's wrong with this device. */ 1623 dev->ud_quirks = usbd_find_quirk(udd); 1624 1625 return USBD_NORMAL_COMPLETION; 1626 } 1627 1628 void 1629 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up) 1630 { 1631 1632 USBHIST_FUNC(); 1633 USBHIST_CALLARGS(usbdebug, "dev %#jx up %#jx", 1634 (uintptr_t)dev, (uintptr_t)up, 0, 0); 1635 1636 if (dev->ud_pipe0 != NULL) 1637 usbd_kill_pipe(dev->ud_pipe0); 1638 up->up_dev = NULL; 1639 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL; 1640 1641 if (dev->ud_vendor != NULL) { 1642 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN); 1643 } 1644 if (dev->ud_product != NULL) { 1645 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN); 1646 } 1647 if (dev->ud_serial != NULL) { 1648 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN); 1649 } 1650 kmem_free(dev, sizeof(*dev)); 1651 } 1652 1653 int 1654 usbd_print(void *aux, const char *pnp) 1655 { 1656 struct usb_attach_arg *uaa = aux; 1657 1658 if (pnp) { 1659 #define USB_DEVINFO 1024 1660 char *devinfo; 1661 if (!uaa->uaa_usegeneric) 1662 return QUIET; 1663 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP); 1664 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO); 1665 aprint_normal("%s, %s", devinfo, pnp); 1666 kmem_free(devinfo, USB_DEVINFO); 1667 } 1668 aprint_normal(" port %d", uaa->uaa_port); 1669 #if 0 1670 /* 1671 * It gets very crowded with these locators on the attach line. 1672 * They are not really needed since they are printed in the clear 1673 * by each driver. 1674 */ 1675 if (uaa->uaa_vendor != UHUB_UNK_VENDOR) 1676 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor); 1677 if (uaa->uaa_product != UHUB_UNK_PRODUCT) 1678 aprint_normal(" product 0x%04x", uaa->uaa_product); 1679 if (uaa->uaa_release != UHUB_UNK_RELEASE) 1680 aprint_normal(" release 0x%04x", uaa->uaa_release); 1681 #endif 1682 return UNCONF; 1683 } 1684 1685 int 1686 usbd_ifprint(void *aux, const char *pnp) 1687 { 1688 struct usbif_attach_arg *uiaa = aux; 1689 1690 if (pnp) 1691 return QUIET; 1692 aprint_normal(" port %d", uiaa->uiaa_port); 1693 aprint_normal(" configuration %d", uiaa->uiaa_configno); 1694 aprint_normal(" interface %d", uiaa->uiaa_ifaceno); 1695 #if 0 1696 /* 1697 * It gets very crowded with these locators on the attach line. 1698 * They are not really needed since they are printed in the clear 1699 * by each driver. 1700 */ 1701 if (uaa->uaa_vendor != UHUB_UNK_VENDOR) 1702 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor); 1703 if (uaa->uaa_product != UHUB_UNK_PRODUCT) 1704 aprint_normal(" product 0x%04x", uaa->uaa_product); 1705 if (uaa->uaa_release != UHUB_UNK_RELEASE) 1706 aprint_normal(" release 0x%04x", uaa->uaa_release); 1707 #endif 1708 return UNCONF; 1709 } 1710 1711 void 1712 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di, 1713 int usedev) 1714 { 1715 struct usbd_port *p; 1716 int i, j, err; 1717 1718 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl); 1719 di->udi_addr = dev->ud_addr; 1720 di->udi_cookie = dev->ud_cookie; 1721 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1722 di->udi_product, sizeof(di->udi_product), usedev, 1); 1723 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1724 UGETW(dev->ud_ddesc.bcdDevice)); 1725 if (usedev) { 1726 usbd_status uerr = usbd_get_string(dev, 1727 dev->ud_ddesc.iSerialNumber, di->udi_serial); 1728 if (uerr != USBD_NORMAL_COMPLETION) { 1729 di->udi_serial[0] = '\0'; 1730 } else { 1731 usbd_trim_spaces(di->udi_serial); 1732 } 1733 } else { 1734 di->udi_serial[0] = '\0'; 1735 if (dev->ud_serial) { 1736 strlcpy(di->udi_serial, dev->ud_serial, 1737 sizeof(di->udi_serial)); 1738 } 1739 } 1740 1741 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor); 1742 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct); 1743 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice); 1744 di->udi_class = dev->ud_ddesc.bDeviceClass; 1745 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass; 1746 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol; 1747 di->udi_config = dev->ud_config; 1748 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power; 1749 di->udi_speed = dev->ud_speed; 1750 1751 if (dev->ud_subdevlen > 0) { 1752 for (i = 0, j = 0; i < dev->ud_subdevlen && 1753 j < USB_MAX_DEVNAMES; i++) { 1754 if (!dev->ud_subdevs[i]) 1755 continue; 1756 strncpy(di->udi_devnames[j], 1757 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN); 1758 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1759 j++; 1760 } 1761 } else { 1762 j = 0; 1763 } 1764 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1765 di->udi_devnames[j][0] = 0; /* empty */ 1766 1767 if (!dev->ud_hub) { 1768 di->udi_nports = 0; 1769 return; 1770 } 1771 1772 const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts; 1773 for (i = 1; i <= __arraycount(di->udi_ports) && i <= nports; i++) { 1774 p = &dev->ud_hub->uh_ports[i - 1]; 1775 if (p->up_dev) 1776 err = p->up_dev->ud_addr; 1777 else { 1778 const int s = UGETW(p->up_status.wPortStatus); 1779 const bool sshub_p = USB_IS_SS(dev->ud_speed); 1780 if (s & UPS_PORT_ENABLED) 1781 err = USB_PORT_ENABLED; 1782 else if (s & UPS_SUSPEND) 1783 err = USB_PORT_SUSPENDED; 1784 /* 1785 * Note: UPS_PORT_POWER_SS is available only 1786 * on 3.x, and UPS_PORT_POWER is available 1787 * only on 2.0 or 1.1. 1788 */ 1789 else if (sshub_p && (s & UPS_PORT_POWER_SS)) 1790 err = USB_PORT_POWERED; 1791 else if (!sshub_p && (s & UPS_PORT_POWER)) 1792 err = USB_PORT_POWERED; 1793 else 1794 err = USB_PORT_DISABLED; 1795 } 1796 di->udi_ports[i - 1] = err; 1797 } 1798 di->udi_nports = nports; 1799 } 1800 1801 void 1802 usb_free_device(struct usbd_device *dev) 1803 { 1804 int ifcidx, nifc; 1805 1806 if (dev->ud_pipe0 != NULL) 1807 usbd_kill_pipe(dev->ud_pipe0); 1808 if (dev->ud_ifaces != NULL) { 1809 nifc = dev->ud_cdesc->bNumInterface; 1810 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 1811 usbd_iface_exlock(&dev->ud_ifaces[ifcidx]); 1812 usbd_free_iface_data(dev, ifcidx); 1813 usbd_iface_unlock(&dev->ud_ifaces[ifcidx]); 1814 usbd_iface_fini(dev, ifcidx); 1815 } 1816 kmem_free(dev->ud_ifaces, 1817 nifc * sizeof(struct usbd_interface)); 1818 } 1819 if (dev->ud_cdesc != NULL) 1820 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength)); 1821 if (dev->ud_bdesc != NULL) 1822 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength)); 1823 if (dev->ud_subdevlen > 0) { 1824 kmem_free(dev->ud_subdevs, 1825 dev->ud_subdevlen * sizeof(device_t)); 1826 dev->ud_subdevlen = 0; 1827 } 1828 if (dev->ud_vendor) { 1829 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN); 1830 } 1831 if (dev->ud_product) { 1832 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN); 1833 } 1834 if (dev->ud_serial) { 1835 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN); 1836 } 1837 kmem_free(dev, sizeof(*dev)); 1838 } 1839 1840 /* 1841 * The general mechanism for detaching drivers works as follows: Each 1842 * driver is responsible for maintaining a reference count on the 1843 * number of outstanding references to its softc (e.g. from 1844 * processing hanging in a read or write). The detach method of the 1845 * driver decrements this counter and flags in the softc that the 1846 * driver is dying and then wakes any sleepers. It then sleeps on the 1847 * softc. Each place that can sleep must maintain the reference 1848 * count. When the reference count drops to -1 (0 is the normal value 1849 * of the reference count) then a wakeup on the softc is performed 1850 * signaling to the detach waiter that all references are gone. 1851 */ 1852 1853 /* 1854 * Called from process context when we discover that a port has 1855 * been disconnected. 1856 */ 1857 int 1858 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags) 1859 { 1860 struct usbd_device *dev = up->up_dev; 1861 device_t subdev; 1862 char subdevname[16]; 1863 const char *hubname = device_xname(parent); 1864 int i, rc; 1865 1866 USBHIST_FUNC(); 1867 USBHIST_CALLARGS(usbdebug, "up=%#jx dev=%#jx port=%jd", 1868 (uintptr_t)up, (uintptr_t)dev, up->up_portno, 0); 1869 1870 if (dev == NULL) { 1871 return 0; 1872 } 1873 1874 if (dev->ud_subdevlen > 0) { 1875 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0); 1876 for (i = 0; i < dev->ud_subdevlen; i++) { 1877 if ((subdev = dev->ud_subdevs[i]) == NULL) 1878 continue; 1879 strlcpy(subdevname, device_xname(subdev), 1880 sizeof(subdevname)); 1881 KERNEL_LOCK(1, curlwp); 1882 rc = config_detach(subdev, flags); 1883 KERNEL_UNLOCK_ONE(curlwp); 1884 if (rc != 0) 1885 return rc; 1886 printf("%s: at %s", subdevname, hubname); 1887 if (up->up_portno != 0) 1888 printf(" port %d", up->up_portno); 1889 printf(" (addr %d) disconnected\n", dev->ud_addr); 1890 } 1891 KASSERT(!dev->ud_nifaces_claimed); 1892 } 1893 1894 mutex_enter(dev->ud_bus->ub_lock); 1895 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL; 1896 up->up_dev = NULL; 1897 mutex_exit(dev->ud_bus->ub_lock); 1898 1899 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev); 1900 1901 usb_free_device(dev); 1902 1903 return 0; 1904 } 1905