1 /* $NetBSD: usb_subr.c,v 1.249 2021/02/17 06:30:57 mlelstv 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.249 2021/02/17 06:30:57 mlelstv 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 usbd_status 407 usbd_fill_iface_data(struct usbd_device *dev, int ifaceidx, int altidx) 408 { 409 USBHIST_FUNC(); 410 USBHIST_CALLARGS(usbdebug, "ifaceidx=%jd altidx=%jd", 411 ifaceidx, altidx, 0, 0); 412 struct usbd_interface *ifc = &dev->ud_ifaces[ifaceidx]; 413 usb_interface_descriptor_t *idesc; 414 char *p, *end; 415 int endpt, nendpt; 416 417 idesc = usbd_find_idesc(dev->ud_cdesc, ifaceidx, altidx); 418 if (idesc == NULL) 419 return USBD_INVAL; 420 ifc->ui_dev = dev; 421 ifc->ui_idesc = idesc; 422 ifc->ui_index = ifaceidx; 423 ifc->ui_altindex = altidx; 424 nendpt = ifc->ui_idesc->bNumEndpoints; 425 DPRINTFN(4, "found idesc nendpt=%jd", nendpt, 0, 0, 0); 426 if (nendpt != 0) { 427 ifc->ui_endpoints = kmem_alloc(nendpt * sizeof(struct usbd_endpoint), 428 KM_SLEEP); 429 } else 430 ifc->ui_endpoints = NULL; 431 ifc->ui_priv = NULL; 432 p = (char *)ifc->ui_idesc + ifc->ui_idesc->bLength; 433 end = (char *)dev->ud_cdesc + UGETW(dev->ud_cdesc->wTotalLength); 434 #define ed ((usb_endpoint_descriptor_t *)p) 435 for (endpt = 0; endpt < nendpt; endpt++) { 436 DPRINTFN(10, "endpt=%jd", endpt, 0, 0, 0); 437 for (; p < end; p += ed->bLength) { 438 DPRINTFN(10, "p=%#jx end=%#jx len=%jd type=%jd", 439 (uintptr_t)p, (uintptr_t)end, ed->bLength, 440 ed->bDescriptorType); 441 if (p + ed->bLength <= end && 442 ed->bLength >= USB_ENDPOINT_DESCRIPTOR_SIZE && 443 ed->bDescriptorType == UDESC_ENDPOINT) 444 goto found; 445 if (ed->bLength == 0 || 446 ed->bDescriptorType == UDESC_INTERFACE) 447 break; 448 } 449 /* passed end, or bad desc */ 450 if (p < end) { 451 if (ed->bLength == 0) { 452 printf("%s: bad descriptor: 0 length\n", 453 __func__); 454 } else { 455 printf("%s: bad descriptor: iface desc\n", 456 __func__); 457 } 458 } else { 459 printf("%s: no desc found\n", __func__); 460 } 461 goto bad; 462 found: 463 ifc->ui_endpoints[endpt].ue_edesc = ed; 464 if (dev->ud_speed == USB_SPEED_HIGH) { 465 u_int mps; 466 /* Control and bulk endpoints have max packet limits. */ 467 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 468 case UE_CONTROL: 469 mps = USB_2_MAX_CTRL_PACKET; 470 goto check; 471 case UE_BULK: 472 mps = USB_2_MAX_BULK_PACKET; 473 check: 474 if (UGETW(ed->wMaxPacketSize) != mps) { 475 USETW(ed->wMaxPacketSize, mps); 476 #ifdef DIAGNOSTIC 477 printf("usbd_fill_iface_data: bad max " 478 "packet size\n"); 479 #endif 480 } 481 break; 482 default: 483 break; 484 } 485 } 486 ifc->ui_endpoints[endpt].ue_refcnt = 0; 487 ifc->ui_endpoints[endpt].ue_toggle = 0; 488 p += ed->bLength; 489 } 490 #undef ed 491 LIST_INIT(&ifc->ui_pipes); 492 return USBD_NORMAL_COMPLETION; 493 494 bad: 495 if (ifc->ui_endpoints != NULL) { 496 kmem_free(ifc->ui_endpoints, nendpt * sizeof(struct usbd_endpoint)); 497 ifc->ui_endpoints = NULL; 498 } 499 return USBD_INVAL; 500 } 501 502 void 503 usbd_free_iface_data(struct usbd_device *dev, int ifcno) 504 { 505 struct usbd_interface *ifc = &dev->ud_ifaces[ifcno]; 506 if (ifc->ui_endpoints) { 507 int nendpt = ifc->ui_idesc->bNumEndpoints; 508 size_t sz = nendpt * sizeof(struct usbd_endpoint); 509 kmem_free(ifc->ui_endpoints, sz); 510 ifc->ui_endpoints = NULL; 511 } 512 } 513 514 usbd_status 515 usbd_set_config_no(struct usbd_device *dev, int no, int msg) 516 { 517 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "%jd", no, 0, 0, 0); 518 usb_config_descriptor_t cd; 519 usbd_status err; 520 int index; 521 522 if (no == USB_UNCONFIG_NO) 523 return usbd_set_config_index(dev, USB_UNCONFIG_INDEX, msg); 524 525 /* Figure out what config index to use. */ 526 for (index = 0; index < dev->ud_ddesc.bNumConfigurations; index++) { 527 err = usbd_get_config_desc(dev, index, &cd); 528 if (err) 529 return err; 530 if (cd.bConfigurationValue == no) 531 return usbd_set_config_index(dev, index, msg); 532 } 533 return USBD_INVAL; 534 } 535 536 usbd_status 537 usbd_set_config_index(struct usbd_device *dev, int index, int msg) 538 { 539 USBHIST_FUNC(); 540 USBHIST_CALLARGS(usbdebug, "dev=%#jx index=%jd", 541 (uintptr_t)dev, index, 0, 0); 542 usb_config_descriptor_t cd, *cdp; 543 usb_bos_descriptor_t *bdp = NULL; 544 usbd_status err; 545 int i, ifcidx, nifc, len, selfpowered, power; 546 547 548 if (index >= dev->ud_ddesc.bNumConfigurations && 549 index != USB_UNCONFIG_INDEX) { 550 /* panic? */ 551 printf("usbd_set_config_index: illegal index\n"); 552 return USBD_INVAL; 553 } 554 555 /* XXX check that all interfaces are idle */ 556 if (dev->ud_config != USB_UNCONFIG_NO) { 557 DPRINTF("free old config", 0, 0, 0, 0); 558 /* Free all configuration data structures. */ 559 nifc = dev->ud_cdesc->bNumInterface; 560 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 561 usbd_free_iface_data(dev, ifcidx); 562 kmem_free(dev->ud_ifaces, nifc * sizeof(struct usbd_interface)); 563 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength)); 564 if (dev->ud_bdesc != NULL) 565 kmem_free(dev->ud_bdesc, 566 UGETW(dev->ud_bdesc->wTotalLength)); 567 dev->ud_ifaces = NULL; 568 dev->ud_cdesc = NULL; 569 dev->ud_bdesc = NULL; 570 dev->ud_config = USB_UNCONFIG_NO; 571 } 572 573 if (index == USB_UNCONFIG_INDEX) { 574 /* We are unconfiguring the device, so leave unallocated. */ 575 DPRINTF("set config 0", 0, 0, 0, 0); 576 err = usbd_set_config(dev, USB_UNCONFIG_NO); 577 if (err) { 578 DPRINTF("setting config=0 failed, err = %jd", err, 579 0, 0, 0); 580 } 581 return err; 582 } 583 584 /* Get the short descriptor. */ 585 err = usbd_get_config_desc(dev, index, &cd); 586 if (err) { 587 DPRINTF("get_config_desc=%jd", err, 0, 0, 0); 588 return err; 589 } 590 len = UGETW(cd.wTotalLength); 591 if (len < USB_CONFIG_DESCRIPTOR_SIZE) { 592 DPRINTF("empty short descriptor", 0, 0, 0, 0); 593 return USBD_INVAL; 594 } 595 cdp = kmem_alloc(len, KM_SLEEP); 596 597 /* Get the full descriptor. Try a few times for slow devices. */ 598 for (i = 0; i < 3; i++) { 599 err = usbd_get_desc(dev, UDESC_CONFIG, index, len, cdp); 600 if (!err) 601 break; 602 usbd_delay_ms(dev, 200); 603 } 604 if (err) { 605 DPRINTF("get_desc=%jd", err, 0, 0, 0); 606 goto bad; 607 } 608 if (cdp->bDescriptorType != UDESC_CONFIG) { 609 DPRINTF("bad desc %jd", cdp->bDescriptorType, 0, 0, 0); 610 err = USBD_INVAL; 611 goto bad; 612 } 613 if (UGETW(cdp->wTotalLength) != UGETW(cd.wTotalLength)) { 614 DPRINTF("bad len %jd", UGETW(cdp->wTotalLength), 0, 0, 0); 615 err = USBD_INVAL; 616 goto bad; 617 } 618 619 if (USB_IS_SS(dev->ud_speed)) { 620 usb_bos_descriptor_t bd; 621 622 /* get short bos desc */ 623 err = usbd_get_bos_desc(dev, index, &bd); 624 if (!err) { 625 int blen = UGETW(bd.wTotalLength); 626 if (blen < USB_BOS_DESCRIPTOR_SIZE) { 627 DPRINTF("empty bos descriptor", 0, 0, 0, 0); 628 err = USBD_INVAL; 629 goto bad; 630 } 631 bdp = kmem_alloc(blen, KM_SLEEP); 632 633 /* Get the full desc */ 634 for (i = 0; i < 3; i++) { 635 err = usbd_get_desc(dev, UDESC_BOS, index, blen, 636 bdp); 637 if (!err) 638 break; 639 usbd_delay_ms(dev, 200); 640 } 641 if (err || bdp->bDescriptorType != UDESC_BOS || 642 UGETW(bdp->wTotalLength) != UGETW(bd.wTotalLength)) { 643 DPRINTF("error %jd or bad desc %jd", err, 644 bdp->bDescriptorType, 0, 0); 645 kmem_free(bdp, blen); 646 bdp = NULL; 647 } 648 } 649 } 650 dev->ud_bdesc = bdp; 651 652 /* 653 * Figure out if the device is self or bus powered. 654 */ 655 #if 0 /* XXX various devices don't report the power state correctly */ 656 selfpowered = 0; 657 err = usbd_get_device_status(dev, &ds); 658 if (!err && (UGETW(ds.wStatus) & UDS_SELF_POWERED)) 659 selfpowered = 1; 660 #endif 661 /* 662 * Use the power state in the configuration we are going 663 * to set. This doesn't necessarily reflect the actual 664 * power state of the device; the driver can control this 665 * by choosing the appropriate configuration. 666 */ 667 selfpowered = !!(cdp->bmAttributes & UC_SELF_POWERED); 668 669 DPRINTF("addr %jd cno=%jd attr=0x%02jx, selfpowered=%jd", 670 dev->ud_addr, cdp->bConfigurationValue, cdp->bmAttributes, 671 selfpowered); 672 DPRINTF("max power=%jd", cdp->bMaxPower * 2, 0, 0, 0); 673 674 /* Check if we have enough power. */ 675 #if 0 /* this is a no-op, see above */ 676 if ((cdp->bmAttributes & UC_SELF_POWERED) && !selfpowered) { 677 if (msg) 678 printf("%s: device addr %d (config %d): " 679 "can't set self powered configuration\n", 680 device_xname(dev->ud_bus->bdev), dev->ud_addr, 681 cdp->bConfigurationValue); 682 err = USBD_NO_POWER; 683 goto bad; 684 } 685 #endif 686 #ifdef USB_DEBUG 687 if (dev->ud_powersrc == NULL) { 688 DPRINTF("No power source?", 0, 0, 0, 0); 689 err = USBD_IOERROR; 690 goto bad; 691 } 692 #endif 693 power = cdp->bMaxPower * 2; 694 if (power > dev->ud_powersrc->up_power) { 695 DPRINTF("power exceeded %jd %jd", power, 696 dev->ud_powersrc->up_power, 0, 0); 697 /* XXX print nicer message. */ 698 if (msg) 699 printf("%s: device addr %d (config %d) exceeds power " 700 "budget, %d mA > %d mA\n", 701 device_xname(dev->ud_bus->ub_usbctl), dev->ud_addr, 702 cdp->bConfigurationValue, 703 power, dev->ud_powersrc->up_power); 704 err = USBD_NO_POWER; 705 goto bad; 706 } 707 dev->ud_power = power; 708 dev->ud_selfpowered = selfpowered; 709 710 /* Set the actual configuration value. */ 711 DPRINTF("set config %jd", cdp->bConfigurationValue, 0, 0, 0); 712 err = usbd_set_config(dev, cdp->bConfigurationValue); 713 if (err) { 714 DPRINTF("setting config=%jd failed, error=%jd", 715 cdp->bConfigurationValue, err, 0, 0); 716 goto bad; 717 } 718 719 /* Allocate and fill interface data. */ 720 nifc = cdp->bNumInterface; 721 if (nifc == 0) { 722 DPRINTF("no interfaces", 0, 0, 0, 0); 723 err = USBD_INVAL; 724 goto bad; 725 } 726 dev->ud_ifaces = kmem_alloc(nifc * sizeof(struct usbd_interface), 727 KM_SLEEP); 728 DPRINTFN(5, "dev=%#jx cdesc=%#jx", (uintptr_t)dev, (uintptr_t)cdp, 729 0, 0); 730 dev->ud_cdesc = cdp; 731 dev->ud_config = cdp->bConfigurationValue; 732 for (ifcidx = 0; ifcidx < nifc; ifcidx++) { 733 err = usbd_fill_iface_data(dev, ifcidx, 0); 734 if (err) { 735 while (--ifcidx >= 0) 736 usbd_free_iface_data(dev, ifcidx); 737 kmem_free(dev->ud_ifaces, 738 nifc * sizeof(struct usbd_interface)); 739 dev->ud_ifaces = NULL; 740 goto bad; 741 } 742 } 743 744 return USBD_NORMAL_COMPLETION; 745 746 bad: 747 /* XXX Use usbd_set_config() to reset the config? */ 748 /* XXX Should we forbid USB_UNCONFIG_NO from bConfigurationValue? */ 749 dev->ud_config = USB_UNCONFIG_NO; 750 kmem_free(cdp, len); 751 dev->ud_cdesc = NULL; 752 if (bdp != NULL) { 753 kmem_free(bdp, UGETW(bdp->wTotalLength)); 754 dev->ud_bdesc = NULL; 755 } 756 return err; 757 } 758 759 /* XXX add function for alternate settings */ 760 761 usbd_status 762 usbd_setup_pipe(struct usbd_device *dev, struct usbd_interface *iface, 763 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe) 764 { 765 return usbd_setup_pipe_flags(dev, iface, ep, ival, pipe, 0); 766 } 767 768 usbd_status 769 usbd_setup_pipe_flags(struct usbd_device *dev, struct usbd_interface *iface, 770 struct usbd_endpoint *ep, int ival, struct usbd_pipe **pipe, uint8_t flags) 771 { 772 USBHIST_FUNC(); 773 USBHIST_CALLARGS(usbdebug, "dev=%#jx addr=%jd iface=%#jx ep=%#jx", 774 (uintptr_t)dev, dev->ud_addr, (uintptr_t)iface, (uintptr_t)ep); 775 struct usbd_pipe *p; 776 usbd_status err; 777 778 p = kmem_alloc(dev->ud_bus->ub_pipesize, KM_SLEEP); 779 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0); 780 p->up_dev = dev; 781 p->up_iface = iface; 782 p->up_endpoint = ep; 783 ep->ue_refcnt++; 784 p->up_intrxfer = NULL; 785 p->up_running = 0; 786 p->up_aborting = 0; 787 p->up_serialise = true; 788 p->up_repeat = 0; 789 p->up_interval = ival; 790 p->up_flags = flags; 791 SIMPLEQ_INIT(&p->up_queue); 792 err = dev->ud_bus->ub_methods->ubm_open(p); 793 if (err) { 794 DPRINTF("endpoint=%#jx failed, error=%jd", 795 (uintptr_t)ep->ue_edesc->bEndpointAddress, err, 0, 0); 796 kmem_free(p, dev->ud_bus->ub_pipesize); 797 return err; 798 } 799 800 KASSERT(p->up_methods->upm_start || p->up_serialise == false); 801 802 usb_init_task(&p->up_async_task, usbd_clear_endpoint_stall_task, p, 803 USB_TASKQ_MPSAFE); 804 DPRINTFN(1, "pipe=%#jx", (uintptr_t)p, 0, 0, 0); 805 *pipe = p; 806 return USBD_NORMAL_COMPLETION; 807 } 808 809 /* Abort the device control pipe. */ 810 void 811 usbd_kill_pipe(struct usbd_pipe *pipe) 812 { 813 usbd_abort_pipe(pipe); 814 usbd_lock_pipe(pipe); 815 pipe->up_methods->upm_close(pipe); 816 usbd_unlock_pipe(pipe); 817 usb_rem_task_wait(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER, 818 NULL); 819 pipe->up_endpoint->ue_refcnt--; 820 kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize); 821 } 822 823 int 824 usbd_getnewaddr(struct usbd_bus *bus) 825 { 826 int addr; 827 828 for (addr = 1; addr < USB_MAX_DEVICES; addr++) { 829 size_t dindex = usb_addr2dindex(addr); 830 if (bus->ub_devices[dindex] == NULL) 831 return addr; 832 } 833 return -1; 834 } 835 836 usbd_status 837 usbd_attach_roothub(device_t parent, struct usbd_device *dev) 838 { 839 struct usb_attach_arg uaa; 840 usb_device_descriptor_t *dd = &dev->ud_ddesc; 841 device_t dv; 842 843 uaa.uaa_device = dev; 844 uaa.uaa_usegeneric = 0; 845 uaa.uaa_port = 0; 846 uaa.uaa_vendor = UGETW(dd->idVendor); 847 uaa.uaa_product = UGETW(dd->idProduct); 848 uaa.uaa_release = UGETW(dd->bcdDevice); 849 uaa.uaa_class = dd->bDeviceClass; 850 uaa.uaa_subclass = dd->bDeviceSubClass; 851 uaa.uaa_proto = dd->bDeviceProtocol; 852 853 KERNEL_LOCK(1, curlwp); 854 dv = config_found_ia(parent, "usbroothubif", &uaa, 0); 855 KERNEL_UNLOCK_ONE(curlwp); 856 if (dv) { 857 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP); 858 dev->ud_subdevs[0] = dv; 859 dev->ud_subdevlen = 1; 860 } 861 return USBD_NORMAL_COMPLETION; 862 } 863 864 static void 865 usbd_properties(device_t dv, struct usbd_device *dev) 866 { 867 usb_device_descriptor_t *dd = &dev->ud_ddesc; 868 prop_dictionary_t dict = device_properties(dv); 869 int class, subclass, release, proto, vendor, product; 870 871 class = dd->bDeviceClass; 872 subclass = dd->bDeviceSubClass; 873 release = UGETW(dd->bcdDevice); 874 proto = dd->bDeviceProtocol; 875 vendor = UGETW(dd->idVendor); 876 product = UGETW(dd->idProduct); 877 878 prop_dictionary_set_uint16(dict, "class", class); 879 prop_dictionary_set_uint16(dict, "subclass", subclass); 880 prop_dictionary_set_uint16(dict, "release", release); 881 prop_dictionary_set_uint16(dict, "proto", proto); 882 prop_dictionary_set_uint16(dict, "vendor", vendor); 883 prop_dictionary_set_uint16(dict, "product", product); 884 885 if (dev->ud_vendor) { 886 prop_dictionary_set_string(dict, 887 "vendor-string", dev->ud_vendor); 888 } 889 if (dev->ud_product) { 890 prop_dictionary_set_string(dict, 891 "product-string", dev->ud_product); 892 } 893 if (dev->ud_serial) { 894 prop_dictionary_set_string(dict, 895 "serialnumber", dev->ud_serial); 896 } 897 } 898 899 static usbd_status 900 usbd_attachwholedevice(device_t parent, struct usbd_device *dev, int port, 901 int usegeneric) 902 { 903 struct usb_attach_arg uaa; 904 usb_device_descriptor_t *dd = &dev->ud_ddesc; 905 device_t dv; 906 int dlocs[USBDEVIFCF_NLOCS]; 907 908 uaa.uaa_device = dev; 909 uaa.uaa_usegeneric = usegeneric; 910 uaa.uaa_port = port; 911 uaa.uaa_vendor = UGETW(dd->idVendor); 912 uaa.uaa_product = UGETW(dd->idProduct); 913 uaa.uaa_release = UGETW(dd->bcdDevice); 914 uaa.uaa_class = dd->bDeviceClass; 915 uaa.uaa_subclass = dd->bDeviceSubClass; 916 uaa.uaa_proto = dd->bDeviceProtocol; 917 918 dlocs[USBDEVIFCF_PORT] = uaa.uaa_port; 919 dlocs[USBDEVIFCF_VENDOR] = uaa.uaa_vendor; 920 dlocs[USBDEVIFCF_PRODUCT] = uaa.uaa_product; 921 dlocs[USBDEVIFCF_RELEASE] = uaa.uaa_release; 922 /* the rest is historical ballast */ 923 dlocs[USBDEVIFCF_CONFIGURATION] = -1; 924 dlocs[USBDEVIFCF_INTERFACE] = -1; 925 926 KERNEL_LOCK(1, curlwp); 927 config_pending_incr(parent); 928 dv = config_found_sm_loc(parent, "usbdevif", dlocs, &uaa, usbd_print, 929 config_stdsubmatch); 930 KERNEL_UNLOCK_ONE(curlwp); 931 if (dv) { 932 dev->ud_subdevs = kmem_alloc(sizeof(dv), KM_SLEEP); 933 dev->ud_subdevs[0] = dv; 934 dev->ud_subdevlen = 1; 935 dev->ud_nifaces_claimed = 1; /* XXX */ 936 usbd_properties(dv, dev); 937 } 938 config_pending_decr(parent); 939 return USBD_NORMAL_COMPLETION; 940 } 941 942 static usbd_status 943 usbd_attachinterfaces(device_t parent, struct usbd_device *dev, 944 int port, const int *locators) 945 { 946 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 947 struct usbif_attach_arg uiaa; 948 int ilocs[USBIFIFCF_NLOCS]; 949 usb_device_descriptor_t *dd = &dev->ud_ddesc; 950 int nifaces; 951 struct usbd_interface **ifaces; 952 int i, j, loc; 953 device_t dv; 954 955 nifaces = dev->ud_cdesc->bNumInterface; 956 ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP); 957 for (i = 0; i < nifaces; i++) { 958 if (!dev->ud_subdevs[i]) { 959 ifaces[i] = &dev->ud_ifaces[i]; 960 } 961 DPRINTF("interface %jd %#jx", i, (uintptr_t)ifaces[i], 0, 0); 962 } 963 964 965 uiaa.uiaa_device = dev; 966 uiaa.uiaa_port = port; 967 uiaa.uiaa_vendor = UGETW(dd->idVendor); 968 uiaa.uiaa_product = UGETW(dd->idProduct); 969 uiaa.uiaa_release = UGETW(dd->bcdDevice); 970 uiaa.uiaa_configno = dev->ud_cdesc->bConfigurationValue; 971 uiaa.uiaa_ifaces = ifaces; 972 uiaa.uiaa_nifaces = nifaces; 973 ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port; 974 ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor; 975 ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product; 976 ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release; 977 ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno; 978 979 for (i = 0; i < nifaces; i++) { 980 if (!ifaces[i]) { 981 DPRINTF("interface %jd claimed", i, 0, 0, 0); 982 continue; /* interface already claimed */ 983 } 984 uiaa.uiaa_iface = ifaces[i]; 985 uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass; 986 uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass; 987 uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol; 988 uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber; 989 990 DPRINTF("searching for interface %jd...", i, 0, 0, 0); 991 DPRINTF("class %jx subclass %jx proto %jx ifaceno %jd", 992 uiaa.uiaa_class, uiaa.uiaa_subclass, uiaa.uiaa_proto, 993 uiaa.uiaa_ifaceno); 994 ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno; 995 if (locators != NULL) { 996 loc = locators[USBIFIFCF_CONFIGURATION]; 997 if (loc != USBIFIFCF_CONFIGURATION_DEFAULT && 998 loc != uiaa.uiaa_configno) 999 continue; 1000 loc = locators[USBIFIFCF_INTERFACE]; 1001 if (loc != USBIFIFCF_INTERFACE_DEFAULT && 1002 loc != uiaa.uiaa_ifaceno) 1003 continue; 1004 } 1005 KERNEL_LOCK(1, curlwp); 1006 dv = config_found_sm_loc(parent, "usbifif", ilocs, &uiaa, 1007 usbd_ifprint, config_stdsubmatch); 1008 KERNEL_UNLOCK_ONE(curlwp); 1009 if (!dv) 1010 continue; 1011 1012 usbd_properties(dv, dev); 1013 1014 /* claim */ 1015 ifaces[i] = NULL; 1016 /* account for ifaces claimed by the driver behind our back */ 1017 for (j = 0; j < nifaces; j++) { 1018 1019 if (!ifaces[j] && !dev->ud_subdevs[j]) { 1020 DPRINTF("interface %jd claimed behind our back", 1021 j, 0, 0, 0); 1022 dev->ud_subdevs[j] = dv; 1023 dev->ud_nifaces_claimed++; 1024 } 1025 } 1026 } 1027 1028 kmem_free(ifaces, nifaces * sizeof(*ifaces)); 1029 return USBD_NORMAL_COMPLETION; 1030 } 1031 1032 usbd_status 1033 usbd_probe_and_attach(device_t parent, struct usbd_device *dev, 1034 int port, int addr) 1035 { 1036 USBHIST_FUNC(); 1037 USBHIST_CALLARGS(usbdebug, "trying device specific drivers", 0, 0, 0, 0); 1038 usb_device_descriptor_t *dd = &dev->ud_ddesc; 1039 int confi, nifaces; 1040 usbd_status err; 1041 1042 /* First try with device specific drivers. */ 1043 err = usbd_attachwholedevice(parent, dev, port, 0); 1044 if (dev->ud_nifaces_claimed || err) 1045 return err; 1046 DPRINTF("no device specific driver found", 0, 0, 0, 0); 1047 1048 DPRINTF("looping over %jd configurations", dd->bNumConfigurations, 1049 0, 0, 0); 1050 for (confi = 0; confi < dd->bNumConfigurations; confi++) { 1051 DPRINTFN(1, "trying config idx=%jd", confi, 0, 0, 0); 1052 err = usbd_set_config_index(dev, confi, 1); 1053 if (err) { 1054 DPRINTF("port %jd, set config at addr %jd failed, " 1055 "error=%jd", port, addr, err, 0); 1056 printf("%s: port %d, set config at addr %d failed\n", 1057 device_xname(parent), port, addr); 1058 return err; 1059 } 1060 nifaces = dev->ud_cdesc->bNumInterface; 1061 dev->ud_subdevs = kmem_zalloc(nifaces * sizeof(device_t), 1062 KM_SLEEP); 1063 if (dev->ud_subdevs == NULL) 1064 return USBD_NOMEM; 1065 dev->ud_subdevlen = nifaces; 1066 1067 err = usbd_attachinterfaces(parent, dev, port, NULL); 1068 1069 if (!dev->ud_nifaces_claimed) { 1070 kmem_free(dev->ud_subdevs, 1071 dev->ud_subdevlen * sizeof(device_t)); 1072 dev->ud_subdevs = 0; 1073 dev->ud_subdevlen = 0; 1074 } 1075 if (dev->ud_nifaces_claimed || err) 1076 return err; 1077 } 1078 /* No interfaces were attached in any of the configurations. */ 1079 1080 if (dd->bNumConfigurations > 1) /* don't change if only 1 config */ 1081 usbd_set_config_index(dev, 0, 0); 1082 1083 DPRINTF("no interface drivers found", 0, 0, 0, 0); 1084 1085 /* Finally try the generic driver. */ 1086 err = usbd_attachwholedevice(parent, dev, port, 1); 1087 1088 /* 1089 * The generic attach failed, but leave the device as it is. 1090 * We just did not find any drivers, that's all. The device is 1091 * fully operational and not harming anyone. 1092 */ 1093 DPRINTF("generic attach failed", 0, 0, 0, 0); 1094 1095 return USBD_NORMAL_COMPLETION; 1096 } 1097 1098 /** 1099 * Called from uhub_rescan(). usbd_new_device() for the target dev must be 1100 * called before calling this. 1101 */ 1102 usbd_status 1103 usbd_reattach_device(device_t parent, struct usbd_device *dev, 1104 int port, const int *locators) 1105 { 1106 int i, loc; 1107 1108 if (locators != NULL) { 1109 loc = locators[USBIFIFCF_PORT]; 1110 if (loc != USBIFIFCF_PORT_DEFAULT && loc != port) 1111 return USBD_NORMAL_COMPLETION; 1112 loc = locators[USBIFIFCF_VENDOR]; 1113 if (loc != USBIFIFCF_VENDOR_DEFAULT && 1114 loc != UGETW(dev->ud_ddesc.idVendor)) 1115 return USBD_NORMAL_COMPLETION; 1116 loc = locators[USBIFIFCF_PRODUCT]; 1117 if (loc != USBIFIFCF_PRODUCT_DEFAULT && 1118 loc != UGETW(dev->ud_ddesc.idProduct)) 1119 return USBD_NORMAL_COMPLETION; 1120 loc = locators[USBIFIFCF_RELEASE]; 1121 if (loc != USBIFIFCF_RELEASE_DEFAULT && 1122 loc != UGETW(dev->ud_ddesc.bcdDevice)) 1123 return USBD_NORMAL_COMPLETION; 1124 } 1125 if (dev->ud_subdevlen == 0) { 1126 /* XXX: check USBIFIFCF_CONFIGURATION and 1127 * USBIFIFCF_INTERFACE too */ 1128 return usbd_probe_and_attach(parent, dev, port, dev->ud_addr); 1129 } else if (dev->ud_subdevlen != dev->ud_cdesc->bNumInterface) { 1130 /* device-specific or generic driver is already attached. */ 1131 return USBD_NORMAL_COMPLETION; 1132 } 1133 /* Does the device have unconfigured interfaces? */ 1134 for (i = 0; i < dev->ud_subdevlen; i++) { 1135 if (dev->ud_subdevs[i] == NULL) { 1136 break; 1137 } 1138 } 1139 if (i >= dev->ud_subdevlen) 1140 return USBD_NORMAL_COMPLETION; 1141 return usbd_attachinterfaces(parent, dev, port, locators); 1142 } 1143 1144 /* 1145 * Called when a new device has been put in the powered state, 1146 * but not yet in the addressed state. 1147 * Get initial descriptor, set the address, get full descriptor, 1148 * and attach a driver. 1149 */ 1150 usbd_status 1151 usbd_new_device(device_t parent, struct usbd_bus *bus, int depth, int speed, 1152 int port, struct usbd_port *up) 1153 { 1154 USBHIST_FUNC(); 1155 USBHIST_CALLARGS(usbdebug, "bus=%#jx port=%jd depth=%jd speed=%jd", 1156 (uintptr_t)bus, port, depth, speed); 1157 struct usbd_device *dev, *adev; 1158 struct usbd_device *hub; 1159 usb_device_descriptor_t *dd; 1160 usb_port_status_t ps; 1161 usbd_status err; 1162 int addr; 1163 int i; 1164 int p; 1165 1166 if (bus->ub_methods->ubm_newdev != NULL) 1167 return (bus->ub_methods->ubm_newdev)(parent, bus, depth, speed, 1168 port, up); 1169 1170 addr = usbd_getnewaddr(bus); 1171 if (addr < 0) { 1172 printf("%s: No free USB addresses, new device ignored.\n", 1173 device_xname(bus->ub_usbctl)); 1174 return USBD_NO_ADDR; 1175 } 1176 1177 dev = kmem_zalloc(sizeof(*dev), KM_SLEEP); 1178 dev->ud_bus = bus; 1179 1180 /* Set up default endpoint handle. */ 1181 dev->ud_ep0.ue_edesc = &dev->ud_ep0desc; 1182 1183 /* Set up default endpoint descriptor. */ 1184 dev->ud_ep0desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1185 dev->ud_ep0desc.bDescriptorType = UDESC_ENDPOINT; 1186 dev->ud_ep0desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1187 dev->ud_ep0desc.bmAttributes = UE_CONTROL; 1188 /* 1189 * temporary, will be fixed after first descriptor fetch 1190 * (which uses 64 bytes so it shouldn't be less), 1191 * highspeed devices must support 64 byte packets anyway 1192 */ 1193 if (speed == USB_SPEED_HIGH || speed == USB_SPEED_FULL) 1194 USETW(dev->ud_ep0desc.wMaxPacketSize, 64); 1195 else 1196 USETW(dev->ud_ep0desc.wMaxPacketSize, USB_MAX_IPACKET); 1197 1198 dev->ud_ep0desc.bInterval = 0; 1199 1200 /* doesn't matter, just don't leave it uninitialized */ 1201 dev->ud_ep0.ue_toggle = 0; 1202 1203 dev->ud_quirks = &usbd_no_quirk; 1204 dev->ud_addr = USB_START_ADDR; 1205 dev->ud_ddesc.bMaxPacketSize = 0; 1206 dev->ud_depth = depth; 1207 dev->ud_powersrc = up; 1208 dev->ud_myhub = up->up_parent; 1209 1210 up->up_dev = dev; 1211 1212 /* Locate port on upstream high speed hub */ 1213 for (adev = dev, hub = up->up_parent; 1214 hub != NULL && hub->ud_speed != USB_SPEED_HIGH; 1215 adev = hub, hub = hub->ud_myhub) 1216 ; 1217 if (hub) { 1218 for (p = 1; p <= hub->ud_hub->uh_hubdesc.bNbrPorts; p++) { 1219 if (hub->ud_hub->uh_ports[p - 1].up_dev == adev) { 1220 dev->ud_myhsport = 1221 &hub->ud_hub->uh_ports[p - 1]; 1222 goto found; 1223 } 1224 } 1225 panic("usbd_new_device: cannot find HS port"); 1226 found: 1227 DPRINTFN(1, "high speed port %jd", p, 0, 0, 0); 1228 } else { 1229 dev->ud_myhsport = NULL; 1230 } 1231 dev->ud_speed = speed; 1232 dev->ud_langid = USBD_NOLANG; 1233 dev->ud_cookie.cookie = ++usb_cookie_no; 1234 1235 /* Establish the default pipe. */ 1236 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1237 &dev->ud_pipe0, USBD_MPSAFE); 1238 if (err) { 1239 usbd_remove_device(dev, up); 1240 return err; 1241 } 1242 1243 dd = &dev->ud_ddesc; 1244 /* Try a few times in case the device is slow (i.e. outside specs.) */ 1245 for (i = 0; i < 10; i++) { 1246 /* Get the first 8 bytes of the device descriptor. */ 1247 err = usbd_get_initial_ddesc(dev, dd); 1248 if (!err) 1249 break; 1250 /* 1251 * The root hub can never fail to give the initial descriptor, 1252 * but assert it just in case. 1253 */ 1254 KASSERT(up->up_parent); 1255 usbd_delay_ms(dev, 200); 1256 if ((i & 3) == 3) 1257 usbd_reset_port(up->up_parent, port, &ps); 1258 } 1259 if (err) { 1260 DPRINTF("addr=%jd, getting first desc failed: %jd", addr, err, 1261 0, 0); 1262 usbd_remove_device(dev, up); 1263 return err; 1264 } 1265 1266 /* Windows resets the port here, do likewise */ 1267 if (up->up_parent) 1268 usbd_reset_port(up->up_parent, port, &ps); 1269 1270 if (speed == USB_SPEED_HIGH) { 1271 /* Max packet size must be 64 (sec 5.5.3). */ 1272 if (dd->bMaxPacketSize != USB_2_MAX_CTRL_PACKET) { 1273 #ifdef DIAGNOSTIC 1274 printf("usbd_new_device: addr=%d bad max packet " 1275 "size=%d. adjusting to %d.\n", 1276 addr, dd->bMaxPacketSize, USB_2_MAX_CTRL_PACKET); 1277 #endif 1278 dd->bMaxPacketSize = USB_2_MAX_CTRL_PACKET; 1279 } 1280 } 1281 1282 DPRINTF("adding unit addr=%jd, rev=%02jx, class=%jd, subclass=%jd", 1283 addr, UGETW(dd->bcdUSB), dd->bDeviceClass, dd->bDeviceSubClass); 1284 DPRINTF("protocol=%jd, maxpacket=%jd, len=%jd, speed=%jd", 1285 dd->bDeviceProtocol, dd->bMaxPacketSize, dd->bLength, dev->ud_speed); 1286 1287 if (dd->bDescriptorType != UDESC_DEVICE) { 1288 /* Illegal device descriptor */ 1289 DPRINTF("illegal descriptor %jd", dd->bDescriptorType, 0, 0, 0); 1290 usbd_remove_device(dev, up); 1291 return USBD_INVAL; 1292 } 1293 1294 if (dd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) { 1295 DPRINTF("bad length %jd", dd->bLength, 0, 0, 0); 1296 usbd_remove_device(dev, up); 1297 return USBD_INVAL; 1298 } 1299 1300 USETW(dev->ud_ep0desc.wMaxPacketSize, dd->bMaxPacketSize); 1301 1302 /* Re-establish the default pipe with the new MPS. */ 1303 usbd_kill_pipe(dev->ud_pipe0); 1304 dev->ud_pipe0 = NULL; 1305 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1306 &dev->ud_pipe0, USBD_MPSAFE); 1307 if (err) { 1308 DPRINTF("setup default pipe failed err %jd", err, 0, 0, 0); 1309 usbd_remove_device(dev, up); 1310 return err; 1311 } 1312 1313 /* Set the address */ 1314 DPRINTFN(5, "setting device address=%jd", addr, 0, 0, 0); 1315 err = usbd_set_address(dev, addr); 1316 if (err) { 1317 DPRINTF("set address %jd failed, err = %jd", addr, err, 0, 0); 1318 err = USBD_SET_ADDR_FAILED; 1319 usbd_remove_device(dev, up); 1320 return err; 1321 } 1322 1323 /* Allow device time to set new address */ 1324 usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE); 1325 dev->ud_addr = addr; /* new device address now */ 1326 bus->ub_devices[usb_addr2dindex(addr)] = dev; 1327 1328 /* Re-establish the default pipe with the new address. */ 1329 usbd_kill_pipe(dev->ud_pipe0); 1330 dev->ud_pipe0 = NULL; 1331 err = usbd_setup_pipe_flags(dev, 0, &dev->ud_ep0, USBD_DEFAULT_INTERVAL, 1332 &dev->ud_pipe0, USBD_MPSAFE); 1333 if (err) { 1334 DPRINTF("setup default pipe failed, err = %jd", err, 0, 0, 0); 1335 usbd_remove_device(dev, up); 1336 return err; 1337 } 1338 1339 err = usbd_reload_device_desc(dev); 1340 if (err) { 1341 DPRINTF("addr=%jd, getting full desc failed, err = %jd", addr, 1342 err, 0, 0); 1343 usbd_remove_device(dev, up); 1344 return err; 1345 } 1346 1347 /* Assume 100mA bus powered for now. Changed when configured. */ 1348 dev->ud_power = USB_MIN_POWER; 1349 dev->ud_selfpowered = 0; 1350 1351 DPRINTF("new dev (addr %jd), dev=%#jx, parent=%#jx", 1352 addr, (uintptr_t)dev, (uintptr_t)parent, 0); 1353 1354 usbd_get_device_strings(dev); 1355 1356 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1357 1358 if (port == 0) { /* root hub */ 1359 KASSERT(addr == 1); 1360 usbd_attach_roothub(parent, dev); 1361 return USBD_NORMAL_COMPLETION; 1362 } 1363 1364 err = usbd_probe_and_attach(parent, dev, port, addr); 1365 if (err) { 1366 usbd_remove_device(dev, up); 1367 return err; 1368 } 1369 1370 return USBD_NORMAL_COMPLETION; 1371 } 1372 1373 usbd_status 1374 usbd_reload_device_desc(struct usbd_device *dev) 1375 { 1376 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1377 usb_device_descriptor_t *udd = &dev->ud_ddesc; 1378 usbd_status err; 1379 1380 /* Get the full device descriptor. */ 1381 err = usbd_get_device_desc(dev, udd); 1382 if (err) 1383 return err; 1384 if (udd->bDescriptorType != UDESC_DEVICE) 1385 return USBD_INVAL; 1386 if (udd->bLength < USB_DEVICE_DESCRIPTOR_SIZE) 1387 return USBD_INVAL; 1388 1389 DPRINTFN(15, "bLength %5ju", udd->bLength, 0, 0, 0); 1390 DPRINTFN(15, "bDescriptorType %5ju", udd->bDescriptorType, 0, 0, 0); 1391 DPRINTFN(15, "bcdUSB %2jx.%02jx", udd->bcdUSB[1], 1392 udd->bcdUSB[0], 0, 0); 1393 DPRINTFN(15, "bDeviceClass %5ju", udd->bDeviceClass, 0, 0, 0); 1394 DPRINTFN(15, "bDeviceSubClass %5ju", udd->bDeviceSubClass, 0, 0, 0); 1395 DPRINTFN(15, "bDeviceProtocol %5ju", udd->bDeviceProtocol, 0, 0, 0); 1396 DPRINTFN(15, "bMaxPacketSize0 %5ju", udd->bMaxPacketSize, 0, 0, 0); 1397 DPRINTFN(15, "idVendor 0x%02jx 0x%02jx", 1398 udd->idVendor[0], 1399 udd->idVendor[1], 0, 0); 1400 DPRINTFN(15, "idProduct 0x%02jx 0x%02jx", 1401 udd->idProduct[0], 1402 udd->idProduct[1], 0, 0); 1403 DPRINTFN(15, "bcdDevice %2jx.%02jx", udd->bcdDevice[1], 1404 udd->bcdDevice[0], 0, 0); 1405 DPRINTFN(15, "iManufacturer %5ju", udd->iManufacturer, 0, 0, 0); 1406 DPRINTFN(15, "iProduct %5ju", udd->iProduct, 0, 0, 0); 1407 DPRINTFN(15, "iSerial %5ju", udd->iSerialNumber, 0, 0, 0); 1408 DPRINTFN(15, "bNumConfigurations %5ju", udd->bNumConfigurations, 0, 0, 1409 0); 1410 1411 /* Figure out what's wrong with this device. */ 1412 dev->ud_quirks = usbd_find_quirk(udd); 1413 1414 return USBD_NORMAL_COMPLETION; 1415 } 1416 1417 void 1418 usbd_remove_device(struct usbd_device *dev, struct usbd_port *up) 1419 { 1420 1421 USBHIST_FUNC(); 1422 USBHIST_CALLARGS(usbdebug, "dev %#jx up %#jx", 1423 (uintptr_t)dev, (uintptr_t)up, 0, 0); 1424 1425 if (dev->ud_pipe0 != NULL) 1426 usbd_kill_pipe(dev->ud_pipe0); 1427 up->up_dev = NULL; 1428 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL; 1429 1430 if (dev->ud_vendor != NULL) { 1431 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN); 1432 } 1433 if (dev->ud_product != NULL) { 1434 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN); 1435 } 1436 if (dev->ud_serial != NULL) { 1437 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN); 1438 } 1439 kmem_free(dev, sizeof(*dev)); 1440 } 1441 1442 int 1443 usbd_print(void *aux, const char *pnp) 1444 { 1445 struct usb_attach_arg *uaa = aux; 1446 1447 if (pnp) { 1448 #define USB_DEVINFO 1024 1449 char *devinfo; 1450 if (!uaa->uaa_usegeneric) 1451 return QUIET; 1452 devinfo = kmem_alloc(USB_DEVINFO, KM_SLEEP); 1453 usbd_devinfo(uaa->uaa_device, 1, devinfo, USB_DEVINFO); 1454 aprint_normal("%s, %s", devinfo, pnp); 1455 kmem_free(devinfo, USB_DEVINFO); 1456 } 1457 aprint_normal(" port %d", uaa->uaa_port); 1458 #if 0 1459 /* 1460 * It gets very crowded with these locators on the attach line. 1461 * They are not really needed since they are printed in the clear 1462 * by each driver. 1463 */ 1464 if (uaa->uaa_vendor != UHUB_UNK_VENDOR) 1465 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor); 1466 if (uaa->uaa_product != UHUB_UNK_PRODUCT) 1467 aprint_normal(" product 0x%04x", uaa->uaa_product); 1468 if (uaa->uaa_release != UHUB_UNK_RELEASE) 1469 aprint_normal(" release 0x%04x", uaa->uaa_release); 1470 #endif 1471 return UNCONF; 1472 } 1473 1474 int 1475 usbd_ifprint(void *aux, const char *pnp) 1476 { 1477 struct usbif_attach_arg *uiaa = aux; 1478 1479 if (pnp) 1480 return QUIET; 1481 aprint_normal(" port %d", uiaa->uiaa_port); 1482 aprint_normal(" configuration %d", uiaa->uiaa_configno); 1483 aprint_normal(" interface %d", uiaa->uiaa_ifaceno); 1484 #if 0 1485 /* 1486 * It gets very crowded with these locators on the attach line. 1487 * They are not really needed since they are printed in the clear 1488 * by each driver. 1489 */ 1490 if (uaa->uaa_vendor != UHUB_UNK_VENDOR) 1491 aprint_normal(" vendor 0x%04x", uaa->uaa_vendor); 1492 if (uaa->uaa_product != UHUB_UNK_PRODUCT) 1493 aprint_normal(" product 0x%04x", uaa->uaa_product); 1494 if (uaa->uaa_release != UHUB_UNK_RELEASE) 1495 aprint_normal(" release 0x%04x", uaa->uaa_release); 1496 #endif 1497 return UNCONF; 1498 } 1499 1500 void 1501 usbd_fill_deviceinfo(struct usbd_device *dev, struct usb_device_info *di, 1502 int usedev) 1503 { 1504 struct usbd_port *p; 1505 int i, j, err; 1506 1507 di->udi_bus = device_unit(dev->ud_bus->ub_usbctl); 1508 di->udi_addr = dev->ud_addr; 1509 di->udi_cookie = dev->ud_cookie; 1510 usbd_devinfo_vp(dev, di->udi_vendor, sizeof(di->udi_vendor), 1511 di->udi_product, sizeof(di->udi_product), usedev, 1); 1512 usbd_printBCD(di->udi_release, sizeof(di->udi_release), 1513 UGETW(dev->ud_ddesc.bcdDevice)); 1514 if (usedev) { 1515 usbd_status uerr = usbd_get_string(dev, 1516 dev->ud_ddesc.iSerialNumber, di->udi_serial); 1517 if (uerr != USBD_NORMAL_COMPLETION) { 1518 di->udi_serial[0] = '\0'; 1519 } else { 1520 usbd_trim_spaces(di->udi_serial); 1521 } 1522 } else { 1523 di->udi_serial[0] = '\0'; 1524 if (dev->ud_serial) { 1525 strlcpy(di->udi_serial, dev->ud_serial, 1526 sizeof(di->udi_serial)); 1527 } 1528 } 1529 1530 di->udi_vendorNo = UGETW(dev->ud_ddesc.idVendor); 1531 di->udi_productNo = UGETW(dev->ud_ddesc.idProduct); 1532 di->udi_releaseNo = UGETW(dev->ud_ddesc.bcdDevice); 1533 di->udi_class = dev->ud_ddesc.bDeviceClass; 1534 di->udi_subclass = dev->ud_ddesc.bDeviceSubClass; 1535 di->udi_protocol = dev->ud_ddesc.bDeviceProtocol; 1536 di->udi_config = dev->ud_config; 1537 di->udi_power = dev->ud_selfpowered ? 0 : dev->ud_power; 1538 di->udi_speed = dev->ud_speed; 1539 1540 if (dev->ud_subdevlen > 0) { 1541 for (i = 0, j = 0; i < dev->ud_subdevlen && 1542 j < USB_MAX_DEVNAMES; i++) { 1543 if (!dev->ud_subdevs[i]) 1544 continue; 1545 strncpy(di->udi_devnames[j], 1546 device_xname(dev->ud_subdevs[i]), USB_MAX_DEVNAMELEN); 1547 di->udi_devnames[j][USB_MAX_DEVNAMELEN-1] = '\0'; 1548 j++; 1549 } 1550 } else { 1551 j = 0; 1552 } 1553 for (/* j is set */; j < USB_MAX_DEVNAMES; j++) 1554 di->udi_devnames[j][0] = 0; /* empty */ 1555 1556 if (!dev->ud_hub) { 1557 di->udi_nports = 0; 1558 return; 1559 } 1560 1561 const int nports = dev->ud_hub->uh_hubdesc.bNbrPorts; 1562 for (i = 1; i <= __arraycount(di->udi_ports) && i <= nports; i++) { 1563 p = &dev->ud_hub->uh_ports[i - 1]; 1564 if (p->up_dev) 1565 err = p->up_dev->ud_addr; 1566 else { 1567 const int s = UGETW(p->up_status.wPortStatus); 1568 const bool sshub_p = USB_IS_SS(dev->ud_speed); 1569 if (s & UPS_PORT_ENABLED) 1570 err = USB_PORT_ENABLED; 1571 else if (s & UPS_SUSPEND) 1572 err = USB_PORT_SUSPENDED; 1573 /* 1574 * Note: UPS_PORT_POWER_SS is available only 1575 * on 3.x, and UPS_PORT_POWER is available 1576 * only on 2.0 or 1.1. 1577 */ 1578 else if (sshub_p && (s & UPS_PORT_POWER_SS)) 1579 err = USB_PORT_POWERED; 1580 else if (!sshub_p && (s & UPS_PORT_POWER)) 1581 err = USB_PORT_POWERED; 1582 else 1583 err = USB_PORT_DISABLED; 1584 } 1585 di->udi_ports[i - 1] = err; 1586 } 1587 di->udi_nports = nports; 1588 } 1589 1590 void 1591 usb_free_device(struct usbd_device *dev) 1592 { 1593 int ifcidx, nifc; 1594 1595 if (dev->ud_pipe0 != NULL) 1596 usbd_kill_pipe(dev->ud_pipe0); 1597 if (dev->ud_ifaces != NULL) { 1598 nifc = dev->ud_cdesc->bNumInterface; 1599 for (ifcidx = 0; ifcidx < nifc; ifcidx++) 1600 usbd_free_iface_data(dev, ifcidx); 1601 kmem_free(dev->ud_ifaces, 1602 nifc * sizeof(struct usbd_interface)); 1603 } 1604 if (dev->ud_cdesc != NULL) 1605 kmem_free(dev->ud_cdesc, UGETW(dev->ud_cdesc->wTotalLength)); 1606 if (dev->ud_bdesc != NULL) 1607 kmem_free(dev->ud_bdesc, UGETW(dev->ud_bdesc->wTotalLength)); 1608 if (dev->ud_subdevlen > 0) { 1609 kmem_free(dev->ud_subdevs, 1610 dev->ud_subdevlen * sizeof(device_t)); 1611 dev->ud_subdevlen = 0; 1612 } 1613 if (dev->ud_vendor) { 1614 kmem_free(dev->ud_vendor, USB_MAX_ENCODED_STRING_LEN); 1615 } 1616 if (dev->ud_product) { 1617 kmem_free(dev->ud_product, USB_MAX_ENCODED_STRING_LEN); 1618 } 1619 if (dev->ud_serial) { 1620 kmem_free(dev->ud_serial, USB_MAX_ENCODED_STRING_LEN); 1621 } 1622 kmem_free(dev, sizeof(*dev)); 1623 } 1624 1625 /* 1626 * The general mechanism for detaching drivers works as follows: Each 1627 * driver is responsible for maintaining a reference count on the 1628 * number of outstanding references to its softc (e.g. from 1629 * processing hanging in a read or write). The detach method of the 1630 * driver decrements this counter and flags in the softc that the 1631 * driver is dying and then wakes any sleepers. It then sleeps on the 1632 * softc. Each place that can sleep must maintain the reference 1633 * count. When the reference count drops to -1 (0 is the normal value 1634 * of the reference count) then a wakeup on the softc is performed 1635 * signaling to the detach waiter that all references are gone. 1636 */ 1637 1638 /* 1639 * Called from process context when we discover that a port has 1640 * been disconnected. 1641 */ 1642 int 1643 usb_disconnect_port(struct usbd_port *up, device_t parent, int flags) 1644 { 1645 struct usbd_device *dev = up->up_dev; 1646 device_t subdev; 1647 char subdevname[16]; 1648 const char *hubname = device_xname(parent); 1649 int i, rc; 1650 1651 USBHIST_FUNC(); 1652 USBHIST_CALLARGS(usbdebug, "up=%#jx dev=%#jx port=%jd", 1653 (uintptr_t)up, (uintptr_t)dev, up->up_portno, 0); 1654 1655 if (dev == NULL) { 1656 return 0; 1657 } 1658 1659 if (dev->ud_subdevlen > 0) { 1660 DPRINTFN(3, "disconnect subdevs", 0, 0, 0, 0); 1661 for (i = 0; i < dev->ud_subdevlen; i++) { 1662 if ((subdev = dev->ud_subdevs[i]) == NULL) 1663 continue; 1664 strlcpy(subdevname, device_xname(subdev), 1665 sizeof(subdevname)); 1666 KERNEL_LOCK(1, curlwp); 1667 rc = config_detach(subdev, flags); 1668 KERNEL_UNLOCK_ONE(curlwp); 1669 if (rc != 0) 1670 return rc; 1671 printf("%s: at %s", subdevname, hubname); 1672 if (up->up_portno != 0) 1673 printf(" port %d", up->up_portno); 1674 printf(" (addr %d) disconnected\n", dev->ud_addr); 1675 } 1676 KASSERT(!dev->ud_nifaces_claimed); 1677 } 1678 1679 mutex_enter(dev->ud_bus->ub_lock); 1680 dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL; 1681 up->up_dev = NULL; 1682 mutex_exit(dev->ud_bus->ub_lock); 1683 1684 usbd_add_dev_event(USB_EVENT_DEVICE_DETACH, dev); 1685 1686 usb_free_device(dev); 1687 1688 return 0; 1689 } 1690