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