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