1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/linker_set.h> 36 #include <sys/module.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/condvar.h> 40 #include <sys/sysctl.h> 41 #include <sys/sx.h> 42 #include <sys/unistd.h> 43 #include <sys/callout.h> 44 #include <sys/malloc.h> 45 #include <sys/priv.h> 46 #include <sys/conf.h> 47 #include <sys/fcntl.h> 48 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdi_util.h> 52 #include <dev/usb/usb_ioctl.h> 53 54 #if USB_HAVE_UGEN 55 #include <sys/sbuf.h> 56 #endif 57 58 #include "usbdevs.h" 59 60 #define USB_DEBUG_VAR usb_debug 61 62 #include <dev/usb/usb_core.h> 63 #include <dev/usb/usb_debug.h> 64 #include <dev/usb/usb_process.h> 65 #include <dev/usb/usb_device.h> 66 #include <dev/usb/usb_busdma.h> 67 #include <dev/usb/usb_transfer.h> 68 #include <dev/usb/usb_request.h> 69 #include <dev/usb/usb_dynamic.h> 70 #include <dev/usb/usb_hub.h> 71 #include <dev/usb/usb_util.h> 72 #include <dev/usb/usb_msctest.h> 73 #if USB_HAVE_UGEN 74 #include <dev/usb/usb_dev.h> 75 #include <dev/usb/usb_generic.h> 76 #endif 77 78 #include <dev/usb/quirk/usb_quirk.h> 79 80 #include <dev/usb/usb_controller.h> 81 #include <dev/usb/usb_bus.h> 82 83 /* function prototypes */ 84 85 static void usb_init_endpoint(struct usb_device *, uint8_t, 86 struct usb_endpoint_descriptor *, 87 struct usb_endpoint_ss_comp_descriptor *, 88 struct usb_endpoint *); 89 static void usb_unconfigure(struct usb_device *, uint8_t); 90 static void usb_detach_device_sub(struct usb_device *, device_t *, 91 char **, uint8_t); 92 static uint8_t usb_probe_and_attach_sub(struct usb_device *, 93 struct usb_attach_arg *); 94 static void usb_init_attach_arg(struct usb_device *, 95 struct usb_attach_arg *); 96 static void usb_suspend_resume_sub(struct usb_device *, device_t, 97 uint8_t); 98 static void usbd_clear_stall_proc(struct usb_proc_msg *_pm); 99 static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t); 100 static void usbd_set_device_strings(struct usb_device *); 101 #if USB_HAVE_DEVCTL 102 static void usb_notify_addq(const char *type, struct usb_device *); 103 #endif 104 #if USB_HAVE_UGEN 105 static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 106 static struct cdev *usb_make_dev(struct usb_device *, int, int); 107 static void usb_cdev_create(struct usb_device *); 108 static void usb_cdev_free(struct usb_device *); 109 static void usb_cdev_cleanup(void *); 110 #endif 111 112 /* This variable is global to allow easy access to it: */ 113 114 int usb_template = 0; 115 116 TUNABLE_INT("hw.usb.usb_template", &usb_template); 117 SYSCTL_INT(_hw_usb, OID_AUTO, template, CTLFLAG_RW, 118 &usb_template, 0, "Selected USB device side template"); 119 120 /* English is default language */ 121 122 static int usb_lang_id = 0x0009; 123 static int usb_lang_mask = 0x00FF; 124 125 TUNABLE_INT("hw.usb.usb_lang_id", &usb_lang_id); 126 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RW, 127 &usb_lang_id, 0, "Preferred USB language ID"); 128 129 TUNABLE_INT("hw.usb.usb_lang_mask", &usb_lang_mask); 130 SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RW, 131 &usb_lang_mask, 0, "Preferred USB language mask"); 132 133 static const char* statestr[USB_STATE_MAX] = { 134 [USB_STATE_DETACHED] = "DETACHED", 135 [USB_STATE_ATTACHED] = "ATTACHED", 136 [USB_STATE_POWERED] = "POWERED", 137 [USB_STATE_ADDRESSED] = "ADDRESSED", 138 [USB_STATE_CONFIGURED] = "CONFIGURED", 139 }; 140 141 const char * 142 usb_statestr(enum usb_dev_state state) 143 { 144 return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 145 } 146 147 const char * 148 usb_get_manufacturer(struct usb_device *udev) 149 { 150 return (udev->manufacturer ? udev->manufacturer : "Unknown"); 151 } 152 153 const char * 154 usb_get_product(struct usb_device *udev) 155 { 156 return (udev->product ? udev->product : ""); 157 } 158 159 const char * 160 usb_get_serial(struct usb_device *udev) 161 { 162 return (udev->serial ? udev->serial : ""); 163 } 164 165 /*------------------------------------------------------------------------* 166 * usbd_get_ep_by_addr 167 * 168 * This function searches for an USB ep by endpoint address and 169 * direction. 170 * 171 * Returns: 172 * NULL: Failure 173 * Else: Success 174 *------------------------------------------------------------------------*/ 175 struct usb_endpoint * 176 usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 177 { 178 struct usb_endpoint *ep = udev->endpoints; 179 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 180 enum { 181 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 182 }; 183 184 /* 185 * According to the USB specification not all bits are used 186 * for the endpoint address. Keep defined bits only: 187 */ 188 ea_val &= EA_MASK; 189 190 /* 191 * Iterate accross all the USB endpoints searching for a match 192 * based on the endpoint address: 193 */ 194 for (; ep != ep_end; ep++) { 195 196 if (ep->edesc == NULL) { 197 continue; 198 } 199 /* do the mask and check the value */ 200 if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 201 goto found; 202 } 203 } 204 205 /* 206 * The default endpoint is always present and is checked separately: 207 */ 208 if ((udev->ctrl_ep.edesc) && 209 ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 210 ep = &udev->ctrl_ep; 211 goto found; 212 } 213 return (NULL); 214 215 found: 216 return (ep); 217 } 218 219 /*------------------------------------------------------------------------* 220 * usbd_get_endpoint 221 * 222 * This function searches for an USB endpoint based on the information 223 * given by the passed "struct usb_config" pointer. 224 * 225 * Return values: 226 * NULL: No match. 227 * Else: Pointer to "struct usb_endpoint". 228 *------------------------------------------------------------------------*/ 229 struct usb_endpoint * 230 usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 231 const struct usb_config *setup) 232 { 233 struct usb_endpoint *ep = udev->endpoints; 234 struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 235 uint8_t index = setup->ep_index; 236 uint8_t ea_mask; 237 uint8_t ea_val; 238 uint8_t type_mask; 239 uint8_t type_val; 240 241 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 242 "type=0x%x dir=0x%x index=%d\n", 243 udev, iface_index, setup->endpoint, 244 setup->type, setup->direction, setup->ep_index); 245 246 /* check USB mode */ 247 248 if (setup->usb_mode != USB_MODE_DUAL && 249 udev->flags.usb_mode != setup->usb_mode) { 250 /* wrong mode - no endpoint */ 251 return (NULL); 252 } 253 254 /* setup expected endpoint direction mask and value */ 255 256 if (setup->direction == UE_DIR_RX) { 257 ea_mask = (UE_DIR_IN | UE_DIR_OUT); 258 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 259 UE_DIR_OUT : UE_DIR_IN; 260 } else if (setup->direction == UE_DIR_TX) { 261 ea_mask = (UE_DIR_IN | UE_DIR_OUT); 262 ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 263 UE_DIR_IN : UE_DIR_OUT; 264 } else if (setup->direction == UE_DIR_ANY) { 265 /* match any endpoint direction */ 266 ea_mask = 0; 267 ea_val = 0; 268 } else { 269 /* match the given endpoint direction */ 270 ea_mask = (UE_DIR_IN | UE_DIR_OUT); 271 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 272 } 273 274 /* setup expected endpoint address */ 275 276 if (setup->endpoint == UE_ADDR_ANY) { 277 /* match any endpoint address */ 278 } else { 279 /* match the given endpoint address */ 280 ea_mask |= UE_ADDR; 281 ea_val |= (setup->endpoint & UE_ADDR); 282 } 283 284 /* setup expected endpoint type */ 285 286 if (setup->type == UE_BULK_INTR) { 287 /* this will match BULK and INTERRUPT endpoints */ 288 type_mask = 2; 289 type_val = 2; 290 } else if (setup->type == UE_TYPE_ANY) { 291 /* match any endpoint type */ 292 type_mask = 0; 293 type_val = 0; 294 } else { 295 /* match the given endpoint type */ 296 type_mask = UE_XFERTYPE; 297 type_val = (setup->type & UE_XFERTYPE); 298 } 299 300 /* 301 * Iterate accross all the USB endpoints searching for a match 302 * based on the endpoint address. Note that we are searching 303 * the endpoints from the beginning of the "udev->endpoints" array. 304 */ 305 for (; ep != ep_end; ep++) { 306 307 if ((ep->edesc == NULL) || 308 (ep->iface_index != iface_index)) { 309 continue; 310 } 311 /* do the masks and check the values */ 312 313 if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 314 ((ep->edesc->bmAttributes & type_mask) == type_val)) { 315 if (!index--) { 316 goto found; 317 } 318 } 319 } 320 321 /* 322 * Match against default endpoint last, so that "any endpoint", "any 323 * address" and "any direction" returns the first endpoint of the 324 * interface. "iface_index" and "direction" is ignored: 325 */ 326 if ((udev->ctrl_ep.edesc) && 327 ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 328 ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 329 (!index)) { 330 ep = &udev->ctrl_ep; 331 goto found; 332 } 333 return (NULL); 334 335 found: 336 return (ep); 337 } 338 339 /*------------------------------------------------------------------------* 340 * usbd_interface_count 341 * 342 * This function stores the number of USB interfaces excluding 343 * alternate settings, which the USB config descriptor reports into 344 * the unsigned 8-bit integer pointed to by "count". 345 * 346 * Returns: 347 * 0: Success 348 * Else: Failure 349 *------------------------------------------------------------------------*/ 350 usb_error_t 351 usbd_interface_count(struct usb_device *udev, uint8_t *count) 352 { 353 if (udev->cdesc == NULL) { 354 *count = 0; 355 return (USB_ERR_NOT_CONFIGURED); 356 } 357 *count = udev->ifaces_max; 358 return (USB_ERR_NORMAL_COMPLETION); 359 } 360 361 362 /*------------------------------------------------------------------------* 363 * usb_init_endpoint 364 * 365 * This function will initialise the USB endpoint structure pointed to by 366 * the "endpoint" argument. The structure pointed to by "endpoint" must be 367 * zeroed before calling this function. 368 *------------------------------------------------------------------------*/ 369 static void 370 usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 371 struct usb_endpoint_descriptor *edesc, 372 struct usb_endpoint_ss_comp_descriptor *ecomp, 373 struct usb_endpoint *ep) 374 { 375 struct usb_bus_methods *methods; 376 377 methods = udev->bus->methods; 378 379 (methods->endpoint_init) (udev, edesc, ep); 380 381 /* initialise USB endpoint structure */ 382 ep->edesc = edesc; 383 ep->ecomp = ecomp; 384 ep->iface_index = iface_index; 385 TAILQ_INIT(&ep->endpoint_q.head); 386 ep->endpoint_q.command = &usbd_pipe_start; 387 388 /* the pipe is not supported by the hardware */ 389 if (ep->methods == NULL) 390 return; 391 392 /* clear stall, if any */ 393 if (methods->clear_stall != NULL) { 394 USB_BUS_LOCK(udev->bus); 395 (methods->clear_stall) (udev, ep); 396 USB_BUS_UNLOCK(udev->bus); 397 } 398 } 399 400 /*-----------------------------------------------------------------------* 401 * usb_endpoint_foreach 402 * 403 * This function will iterate all the USB endpoints except the control 404 * endpoint. This function is NULL safe. 405 * 406 * Return values: 407 * NULL: End of USB endpoints 408 * Else: Pointer to next USB endpoint 409 *------------------------------------------------------------------------*/ 410 struct usb_endpoint * 411 usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 412 { 413 struct usb_endpoint *ep_end; 414 415 /* be NULL safe */ 416 if (udev == NULL) 417 return (NULL); 418 419 ep_end = udev->endpoints + udev->endpoints_max; 420 421 /* get next endpoint */ 422 if (ep == NULL) 423 ep = udev->endpoints; 424 else 425 ep++; 426 427 /* find next allocated ep */ 428 while (ep != ep_end) { 429 if (ep->edesc != NULL) 430 return (ep); 431 ep++; 432 } 433 return (NULL); 434 } 435 436 /*------------------------------------------------------------------------* 437 * usb_unconfigure 438 * 439 * This function will free all USB interfaces and USB endpoints belonging 440 * to an USB device. 441 * 442 * Flag values, see "USB_UNCFG_FLAG_XXX". 443 *------------------------------------------------------------------------*/ 444 static void 445 usb_unconfigure(struct usb_device *udev, uint8_t flag) 446 { 447 uint8_t do_unlock; 448 449 /* automatic locking */ 450 if (usbd_enum_is_locked(udev)) { 451 do_unlock = 0; 452 } else { 453 do_unlock = 1; 454 usbd_enum_lock(udev); 455 } 456 457 /* detach all interface drivers */ 458 usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 459 460 #if USB_HAVE_UGEN 461 /* free all FIFOs except control endpoint FIFOs */ 462 usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 463 464 /* 465 * Free all cdev's, if any. 466 */ 467 usb_cdev_free(udev); 468 #endif 469 470 #if USB_HAVE_COMPAT_LINUX 471 /* free Linux compat device, if any */ 472 if (udev->linux_endpoint_start) { 473 usb_linux_free_device(udev); 474 udev->linux_endpoint_start = NULL; 475 } 476 #endif 477 478 usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 479 480 /* free "cdesc" after "ifaces" and "endpoints", if any */ 481 if (udev->cdesc != NULL) { 482 if (udev->flags.usb_mode != USB_MODE_DEVICE) 483 free(udev->cdesc, M_USB); 484 udev->cdesc = NULL; 485 } 486 /* set unconfigured state */ 487 udev->curr_config_no = USB_UNCONFIG_NO; 488 udev->curr_config_index = USB_UNCONFIG_INDEX; 489 490 if (do_unlock) 491 usbd_enum_unlock(udev); 492 } 493 494 /*------------------------------------------------------------------------* 495 * usbd_set_config_index 496 * 497 * This function selects configuration by index, independent of the 498 * actual configuration number. This function should not be used by 499 * USB drivers. 500 * 501 * Returns: 502 * 0: Success 503 * Else: Failure 504 *------------------------------------------------------------------------*/ 505 usb_error_t 506 usbd_set_config_index(struct usb_device *udev, uint8_t index) 507 { 508 struct usb_status ds; 509 struct usb_config_descriptor *cdp; 510 uint16_t power; 511 uint16_t max_power; 512 uint8_t selfpowered; 513 uint8_t do_unlock; 514 usb_error_t err; 515 516 DPRINTFN(6, "udev=%p index=%d\n", udev, index); 517 518 /* automatic locking */ 519 if (usbd_enum_is_locked(udev)) { 520 do_unlock = 0; 521 } else { 522 do_unlock = 1; 523 usbd_enum_lock(udev); 524 } 525 526 usb_unconfigure(udev, 0); 527 528 if (index == USB_UNCONFIG_INDEX) { 529 /* 530 * Leave unallocated when unconfiguring the 531 * device. "usb_unconfigure()" will also reset 532 * the current config number and index. 533 */ 534 err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 535 if (udev->state == USB_STATE_CONFIGURED) 536 usb_set_device_state(udev, USB_STATE_ADDRESSED); 537 goto done; 538 } 539 /* get the full config descriptor */ 540 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 541 /* save some memory */ 542 err = usbd_req_get_descriptor_ptr(udev, &cdp, 543 (UDESC_CONFIG << 8) | index); 544 } else { 545 /* normal request */ 546 err = usbd_req_get_config_desc_full(udev, 547 NULL, &cdp, M_USB, index); 548 } 549 if (err) { 550 goto done; 551 } 552 /* set the new config descriptor */ 553 554 udev->cdesc = cdp; 555 556 /* Figure out if the device is self or bus powered. */ 557 selfpowered = 0; 558 if ((!udev->flags.uq_bus_powered) && 559 (cdp->bmAttributes & UC_SELF_POWERED) && 560 (udev->flags.usb_mode == USB_MODE_HOST)) { 561 /* May be self powered. */ 562 if (cdp->bmAttributes & UC_BUS_POWERED) { 563 /* Must ask device. */ 564 err = usbd_req_get_device_status(udev, NULL, &ds); 565 if (err) { 566 DPRINTFN(0, "could not read " 567 "device status: %s\n", 568 usbd_errstr(err)); 569 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 570 selfpowered = 1; 571 } 572 DPRINTF("status=0x%04x \n", 573 UGETW(ds.wStatus)); 574 } else 575 selfpowered = 1; 576 } 577 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 578 "selfpowered=%d, power=%d\n", 579 udev, cdp, 580 udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 581 selfpowered, cdp->bMaxPower * 2); 582 583 /* Check if we have enough power. */ 584 power = cdp->bMaxPower * 2; 585 586 if (udev->parent_hub) { 587 max_power = udev->parent_hub->hub->portpower; 588 } else { 589 max_power = USB_MAX_POWER; 590 } 591 592 if (power > max_power) { 593 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 594 err = USB_ERR_NO_POWER; 595 goto done; 596 } 597 /* Only update "self_powered" in USB Host Mode */ 598 if (udev->flags.usb_mode == USB_MODE_HOST) { 599 udev->flags.self_powered = selfpowered; 600 } 601 udev->power = power; 602 udev->curr_config_no = cdp->bConfigurationValue; 603 udev->curr_config_index = index; 604 usb_set_device_state(udev, USB_STATE_CONFIGURED); 605 606 /* Set the actual configuration value. */ 607 err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 608 if (err) { 609 goto done; 610 } 611 612 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 613 if (err) { 614 goto done; 615 } 616 617 err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 618 if (err) { 619 goto done; 620 } 621 622 #if USB_HAVE_UGEN 623 /* create device nodes for each endpoint */ 624 usb_cdev_create(udev); 625 #endif 626 627 done: 628 DPRINTF("error=%s\n", usbd_errstr(err)); 629 if (err) { 630 usb_unconfigure(udev, 0); 631 } 632 if (do_unlock) 633 usbd_enum_unlock(udev); 634 return (err); 635 } 636 637 /*------------------------------------------------------------------------* 638 * usb_config_parse 639 * 640 * This function will allocate and free USB interfaces and USB endpoints, 641 * parse the USB configuration structure and initialise the USB endpoints 642 * and interfaces. If "iface_index" is not equal to 643 * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 644 * alternate_setting to be selected for the given interface. Else the 645 * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 646 * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 647 * is typically called when setting the configuration or when setting 648 * an alternate interface. 649 * 650 * Returns: 651 * 0: Success 652 * Else: Failure 653 *------------------------------------------------------------------------*/ 654 static usb_error_t 655 usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 656 { 657 struct usb_idesc_parse_state ips; 658 struct usb_interface_descriptor *id; 659 struct usb_endpoint_descriptor *ed; 660 struct usb_interface *iface; 661 struct usb_endpoint *ep; 662 usb_error_t err; 663 uint8_t ep_curr; 664 uint8_t ep_max; 665 uint8_t temp; 666 uint8_t do_init; 667 uint8_t alt_index; 668 669 if (iface_index != USB_IFACE_INDEX_ANY) { 670 /* parameter overload */ 671 alt_index = cmd; 672 cmd = USB_CFG_INIT; 673 } else { 674 /* not used */ 675 alt_index = 0; 676 } 677 678 err = 0; 679 680 DPRINTFN(5, "iface_index=%d cmd=%d\n", 681 iface_index, cmd); 682 683 if (cmd == USB_CFG_FREE) 684 goto cleanup; 685 686 if (cmd == USB_CFG_INIT) { 687 sx_assert(&udev->enum_sx, SA_LOCKED); 688 689 /* check for in-use endpoints */ 690 691 ep = udev->endpoints; 692 ep_max = udev->endpoints_max; 693 while (ep_max--) { 694 /* look for matching endpoints */ 695 if ((iface_index == USB_IFACE_INDEX_ANY) || 696 (iface_index == ep->iface_index)) { 697 if (ep->refcount_alloc != 0) { 698 /* 699 * This typically indicates a 700 * more serious error. 701 */ 702 err = USB_ERR_IN_USE; 703 } else { 704 /* reset endpoint */ 705 memset(ep, 0, sizeof(*ep)); 706 /* make sure we don't zero the endpoint again */ 707 ep->iface_index = USB_IFACE_INDEX_ANY; 708 } 709 } 710 ep++; 711 } 712 713 if (err) 714 return (err); 715 } 716 717 memset(&ips, 0, sizeof(ips)); 718 719 ep_curr = 0; 720 ep_max = 0; 721 722 while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 723 724 /* check for interface overflow */ 725 if (ips.iface_index == USB_IFACE_MAX) 726 break; /* crazy */ 727 728 iface = udev->ifaces + ips.iface_index; 729 730 /* check for specific interface match */ 731 732 if (cmd == USB_CFG_INIT) { 733 if ((iface_index != USB_IFACE_INDEX_ANY) && 734 (iface_index != ips.iface_index)) { 735 /* wrong interface */ 736 do_init = 0; 737 } else if (alt_index != ips.iface_index_alt) { 738 /* wrong alternate setting */ 739 do_init = 0; 740 } else { 741 /* initialise interface */ 742 do_init = 1; 743 } 744 } else 745 do_init = 0; 746 747 /* check for new interface */ 748 if (ips.iface_index_alt == 0) { 749 /* update current number of endpoints */ 750 ep_curr = ep_max; 751 } 752 /* check for init */ 753 if (do_init) { 754 /* setup the USB interface structure */ 755 iface->idesc = id; 756 /* default setting */ 757 iface->parent_iface_index = USB_IFACE_INDEX_ANY; 758 /* set alternate index */ 759 iface->alt_index = alt_index; 760 } 761 762 DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 763 764 ed = (struct usb_endpoint_descriptor *)id; 765 766 temp = ep_curr; 767 768 /* iterate all the endpoint descriptors */ 769 while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 770 771 if (temp == USB_EP_MAX) 772 break; /* crazy */ 773 774 ep = udev->endpoints + temp; 775 776 if (do_init) { 777 void *ecomp; 778 779 ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 780 if (ecomp != NULL) 781 DPRINTFN(5, "Found endpoint companion descriptor\n"); 782 783 usb_init_endpoint(udev, 784 ips.iface_index, ed, ecomp, ep); 785 } 786 787 temp ++; 788 789 /* find maximum number of endpoints */ 790 if (ep_max < temp) 791 ep_max = temp; 792 793 /* optimalisation */ 794 id = (struct usb_interface_descriptor *)ed; 795 } 796 } 797 798 /* NOTE: It is valid to have no interfaces and no endpoints! */ 799 800 if (cmd == USB_CFG_ALLOC) { 801 udev->ifaces_max = ips.iface_index; 802 udev->ifaces = NULL; 803 if (udev->ifaces_max != 0) { 804 udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 805 M_USB, M_WAITOK | M_ZERO); 806 if (udev->ifaces == NULL) { 807 err = USB_ERR_NOMEM; 808 goto done; 809 } 810 } 811 if (ep_max != 0) { 812 udev->endpoints = malloc(sizeof(*ep) * ep_max, 813 M_USB, M_WAITOK | M_ZERO); 814 if (udev->endpoints == NULL) { 815 err = USB_ERR_NOMEM; 816 goto done; 817 } 818 } else { 819 udev->endpoints = NULL; 820 } 821 USB_BUS_LOCK(udev->bus); 822 udev->endpoints_max = ep_max; 823 /* reset any ongoing clear-stall */ 824 udev->ep_curr = NULL; 825 USB_BUS_UNLOCK(udev->bus); 826 } 827 828 done: 829 if (err) { 830 if (cmd == USB_CFG_ALLOC) { 831 cleanup: 832 USB_BUS_LOCK(udev->bus); 833 udev->endpoints_max = 0; 834 /* reset any ongoing clear-stall */ 835 udev->ep_curr = NULL; 836 USB_BUS_UNLOCK(udev->bus); 837 838 /* cleanup */ 839 if (udev->ifaces != NULL) 840 free(udev->ifaces, M_USB); 841 if (udev->endpoints != NULL) 842 free(udev->endpoints, M_USB); 843 844 udev->ifaces = NULL; 845 udev->endpoints = NULL; 846 udev->ifaces_max = 0; 847 } 848 } 849 return (err); 850 } 851 852 /*------------------------------------------------------------------------* 853 * usbd_set_alt_interface_index 854 * 855 * This function will select an alternate interface index for the 856 * given interface index. The interface should not be in use when this 857 * function is called. That means there should not be any open USB 858 * transfers. Else an error is returned. If the alternate setting is 859 * already set this function will simply return success. This function 860 * is called in Host mode and Device mode! 861 * 862 * Returns: 863 * 0: Success 864 * Else: Failure 865 *------------------------------------------------------------------------*/ 866 usb_error_t 867 usbd_set_alt_interface_index(struct usb_device *udev, 868 uint8_t iface_index, uint8_t alt_index) 869 { 870 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 871 usb_error_t err; 872 uint8_t do_unlock; 873 874 /* automatic locking */ 875 if (usbd_enum_is_locked(udev)) { 876 do_unlock = 0; 877 } else { 878 do_unlock = 1; 879 usbd_enum_lock(udev); 880 } 881 if (iface == NULL) { 882 err = USB_ERR_INVAL; 883 goto done; 884 } 885 if (iface->alt_index == alt_index) { 886 /* 887 * Optimise away duplicate setting of 888 * alternate setting in USB Host Mode! 889 */ 890 err = 0; 891 goto done; 892 } 893 #if USB_HAVE_UGEN 894 /* 895 * Free all generic FIFOs for this interface, except control 896 * endpoint FIFOs: 897 */ 898 usb_fifo_free_wrap(udev, iface_index, 0); 899 #endif 900 901 err = usb_config_parse(udev, iface_index, alt_index); 902 if (err) { 903 goto done; 904 } 905 if (iface->alt_index != alt_index) { 906 /* the alternate setting does not exist */ 907 err = USB_ERR_INVAL; 908 goto done; 909 } 910 911 err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 912 iface->idesc->bAlternateSetting); 913 914 done: 915 if (do_unlock) 916 usbd_enum_unlock(udev); 917 918 return (err); 919 } 920 921 /*------------------------------------------------------------------------* 922 * usbd_set_endpoint_stall 923 * 924 * This function is used to make a BULK or INTERRUPT endpoint send 925 * STALL tokens in USB device mode. 926 * 927 * Returns: 928 * 0: Success 929 * Else: Failure 930 *------------------------------------------------------------------------*/ 931 usb_error_t 932 usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 933 uint8_t do_stall) 934 { 935 struct usb_xfer *xfer; 936 uint8_t et; 937 uint8_t was_stalled; 938 939 if (ep == NULL) { 940 /* nothing to do */ 941 DPRINTF("Cannot find endpoint\n"); 942 /* 943 * Pretend that the clear or set stall request is 944 * successful else some USB host stacks can do 945 * strange things, especially when a control endpoint 946 * stalls. 947 */ 948 return (0); 949 } 950 et = (ep->edesc->bmAttributes & UE_XFERTYPE); 951 952 if ((et != UE_BULK) && 953 (et != UE_INTERRUPT)) { 954 /* 955 * Should not stall control 956 * nor isochronous endpoints. 957 */ 958 DPRINTF("Invalid endpoint\n"); 959 return (0); 960 } 961 USB_BUS_LOCK(udev->bus); 962 963 /* store current stall state */ 964 was_stalled = ep->is_stalled; 965 966 /* check for no change */ 967 if (was_stalled && do_stall) { 968 /* if the endpoint is already stalled do nothing */ 969 USB_BUS_UNLOCK(udev->bus); 970 DPRINTF("No change\n"); 971 return (0); 972 } 973 /* set stalled state */ 974 ep->is_stalled = 1; 975 976 if (do_stall || (!was_stalled)) { 977 if (!was_stalled) { 978 /* lookup the current USB transfer, if any */ 979 xfer = ep->endpoint_q.curr; 980 } else { 981 xfer = NULL; 982 } 983 984 /* 985 * If "xfer" is non-NULL the "set_stall" method will 986 * complete the USB transfer like in case of a timeout 987 * setting the error code "USB_ERR_STALLED". 988 */ 989 (udev->bus->methods->set_stall) (udev, xfer, ep, &do_stall); 990 } 991 if (!do_stall) { 992 ep->toggle_next = 0; /* reset data toggle */ 993 ep->is_stalled = 0; /* clear stalled state */ 994 995 (udev->bus->methods->clear_stall) (udev, ep); 996 997 /* start up the current or next transfer, if any */ 998 usb_command_wrapper(&ep->endpoint_q, ep->endpoint_q.curr); 999 } 1000 USB_BUS_UNLOCK(udev->bus); 1001 return (0); 1002 } 1003 1004 /*------------------------------------------------------------------------* 1005 * usb_reset_iface_endpoints - used in USB device side mode 1006 *------------------------------------------------------------------------*/ 1007 usb_error_t 1008 usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 1009 { 1010 struct usb_endpoint *ep; 1011 struct usb_endpoint *ep_end; 1012 1013 ep = udev->endpoints; 1014 ep_end = udev->endpoints + udev->endpoints_max; 1015 1016 for (; ep != ep_end; ep++) { 1017 1018 if ((ep->edesc == NULL) || 1019 (ep->iface_index != iface_index)) { 1020 continue; 1021 } 1022 /* simulate a clear stall from the peer */ 1023 usbd_set_endpoint_stall(udev, ep, 0); 1024 } 1025 return (0); 1026 } 1027 1028 /*------------------------------------------------------------------------* 1029 * usb_detach_device_sub 1030 * 1031 * This function will try to detach an USB device. If it fails a panic 1032 * will result. 1033 * 1034 * Flag values, see "USB_UNCFG_FLAG_XXX". 1035 *------------------------------------------------------------------------*/ 1036 static void 1037 usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 1038 char **ppnpinfo, uint8_t flag) 1039 { 1040 device_t dev; 1041 char *pnpinfo; 1042 int err; 1043 1044 dev = *ppdev; 1045 if (dev) { 1046 /* 1047 * NOTE: It is important to clear "*ppdev" before deleting 1048 * the child due to some device methods being called late 1049 * during the delete process ! 1050 */ 1051 *ppdev = NULL; 1052 1053 device_printf(dev, "at %s, port %d, addr %d " 1054 "(disconnected)\n", 1055 device_get_nameunit(udev->parent_dev), 1056 udev->port_no, udev->address); 1057 1058 if (device_is_attached(dev)) { 1059 if (udev->flags.peer_suspended) { 1060 err = DEVICE_RESUME(dev); 1061 if (err) { 1062 device_printf(dev, "Resume failed\n"); 1063 } 1064 } 1065 if (device_detach(dev)) { 1066 goto error; 1067 } 1068 } 1069 if (device_delete_child(udev->parent_dev, dev)) { 1070 goto error; 1071 } 1072 } 1073 1074 pnpinfo = *ppnpinfo; 1075 if (pnpinfo != NULL) { 1076 *ppnpinfo = NULL; 1077 free(pnpinfo, M_USBDEV); 1078 } 1079 return; 1080 1081 error: 1082 /* Detach is not allowed to fail in the USB world */ 1083 panic("usb_detach_device_sub: A USB driver would not detach\n"); 1084 } 1085 1086 /*------------------------------------------------------------------------* 1087 * usb_detach_device 1088 * 1089 * The following function will detach the matching interfaces. 1090 * This function is NULL safe. 1091 * 1092 * Flag values, see "USB_UNCFG_FLAG_XXX". 1093 *------------------------------------------------------------------------*/ 1094 void 1095 usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1096 uint8_t flag) 1097 { 1098 struct usb_interface *iface; 1099 uint8_t i; 1100 1101 if (udev == NULL) { 1102 /* nothing to do */ 1103 return; 1104 } 1105 DPRINTFN(4, "udev=%p\n", udev); 1106 1107 sx_assert(&udev->enum_sx, SA_LOCKED); 1108 1109 /* 1110 * First detach the child to give the child's detach routine a 1111 * chance to detach the sub-devices in the correct order. 1112 * Then delete the child using "device_delete_child()" which 1113 * will detach all sub-devices from the bottom and upwards! 1114 */ 1115 if (iface_index != USB_IFACE_INDEX_ANY) { 1116 i = iface_index; 1117 iface_index = i + 1; 1118 } else { 1119 i = 0; 1120 iface_index = USB_IFACE_MAX; 1121 } 1122 1123 /* do the detach */ 1124 1125 for (; i != iface_index; i++) { 1126 1127 iface = usbd_get_iface(udev, i); 1128 if (iface == NULL) { 1129 /* looks like the end of the USB interfaces */ 1130 break; 1131 } 1132 usb_detach_device_sub(udev, &iface->subdev, 1133 &iface->pnpinfo, flag); 1134 } 1135 } 1136 1137 /*------------------------------------------------------------------------* 1138 * usb_probe_and_attach_sub 1139 * 1140 * Returns: 1141 * 0: Success 1142 * Else: Failure 1143 *------------------------------------------------------------------------*/ 1144 static uint8_t 1145 usb_probe_and_attach_sub(struct usb_device *udev, 1146 struct usb_attach_arg *uaa) 1147 { 1148 struct usb_interface *iface; 1149 device_t dev; 1150 int err; 1151 1152 iface = uaa->iface; 1153 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 1154 /* leave interface alone */ 1155 return (0); 1156 } 1157 dev = iface->subdev; 1158 if (dev) { 1159 1160 /* clean up after module unload */ 1161 1162 if (device_is_attached(dev)) { 1163 /* already a device there */ 1164 return (0); 1165 } 1166 /* clear "iface->subdev" as early as possible */ 1167 1168 iface->subdev = NULL; 1169 1170 if (device_delete_child(udev->parent_dev, dev)) { 1171 1172 /* 1173 * Panic here, else one can get a double call 1174 * to device_detach(). USB devices should 1175 * never fail on detach! 1176 */ 1177 panic("device_delete_child() failed\n"); 1178 } 1179 } 1180 if (uaa->temp_dev == NULL) { 1181 1182 /* create a new child */ 1183 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 1184 if (uaa->temp_dev == NULL) { 1185 device_printf(udev->parent_dev, 1186 "Device creation failed\n"); 1187 return (1); /* failure */ 1188 } 1189 device_set_ivars(uaa->temp_dev, uaa); 1190 device_quiet(uaa->temp_dev); 1191 } 1192 /* 1193 * Set "subdev" before probe and attach so that "devd" gets 1194 * the information it needs. 1195 */ 1196 iface->subdev = uaa->temp_dev; 1197 1198 if (device_probe_and_attach(iface->subdev) == 0) { 1199 /* 1200 * The USB attach arguments are only available during probe 1201 * and attach ! 1202 */ 1203 uaa->temp_dev = NULL; 1204 device_set_ivars(iface->subdev, NULL); 1205 1206 if (udev->flags.peer_suspended) { 1207 err = DEVICE_SUSPEND(iface->subdev); 1208 if (err) 1209 device_printf(iface->subdev, "Suspend failed\n"); 1210 } 1211 return (0); /* success */ 1212 } else { 1213 /* No USB driver found */ 1214 iface->subdev = NULL; 1215 } 1216 return (1); /* failure */ 1217 } 1218 1219 /*------------------------------------------------------------------------* 1220 * usbd_set_parent_iface 1221 * 1222 * Using this function will lock the alternate interface setting on an 1223 * interface. It is typically used for multi interface drivers. In USB 1224 * device side mode it is assumed that the alternate interfaces all 1225 * have the same endpoint descriptors. The default parent index value 1226 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1227 * locked. 1228 *------------------------------------------------------------------------*/ 1229 void 1230 usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 1231 uint8_t parent_index) 1232 { 1233 struct usb_interface *iface; 1234 1235 iface = usbd_get_iface(udev, iface_index); 1236 if (iface) { 1237 iface->parent_iface_index = parent_index; 1238 } 1239 } 1240 1241 static void 1242 usb_init_attach_arg(struct usb_device *udev, 1243 struct usb_attach_arg *uaa) 1244 { 1245 bzero(uaa, sizeof(*uaa)); 1246 1247 uaa->device = udev; 1248 uaa->usb_mode = udev->flags.usb_mode; 1249 uaa->port = udev->port_no; 1250 uaa->dev_state = UAA_DEV_READY; 1251 1252 uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1253 uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1254 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1255 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1256 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1257 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1258 uaa->info.bConfigIndex = udev->curr_config_index; 1259 uaa->info.bConfigNum = udev->curr_config_no; 1260 } 1261 1262 /*------------------------------------------------------------------------* 1263 * usb_probe_and_attach 1264 * 1265 * This function is called from "uhub_explore_sub()", 1266 * "usb_handle_set_config()" and "usb_handle_request()". 1267 * 1268 * Returns: 1269 * 0: Success 1270 * Else: A control transfer failed 1271 *------------------------------------------------------------------------*/ 1272 usb_error_t 1273 usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 1274 { 1275 struct usb_attach_arg uaa; 1276 struct usb_interface *iface; 1277 uint8_t i; 1278 uint8_t j; 1279 uint8_t do_unlock; 1280 1281 if (udev == NULL) { 1282 DPRINTF("udev == NULL\n"); 1283 return (USB_ERR_INVAL); 1284 } 1285 /* automatic locking */ 1286 if (usbd_enum_is_locked(udev)) { 1287 do_unlock = 0; 1288 } else { 1289 do_unlock = 1; 1290 usbd_enum_lock(udev); 1291 } 1292 1293 if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1294 /* do nothing - no configuration has been set */ 1295 goto done; 1296 } 1297 /* setup USB attach arguments */ 1298 1299 usb_init_attach_arg(udev, &uaa); 1300 1301 /* Check if only one interface should be probed: */ 1302 if (iface_index != USB_IFACE_INDEX_ANY) { 1303 i = iface_index; 1304 j = i + 1; 1305 } else { 1306 i = 0; 1307 j = USB_IFACE_MAX; 1308 } 1309 1310 /* Do the probe and attach */ 1311 for (; i != j; i++) { 1312 1313 iface = usbd_get_iface(udev, i); 1314 if (iface == NULL) { 1315 /* 1316 * Looks like the end of the USB 1317 * interfaces ! 1318 */ 1319 DPRINTFN(2, "end of interfaces " 1320 "at %u\n", i); 1321 break; 1322 } 1323 if (iface->idesc == NULL) { 1324 /* no interface descriptor */ 1325 continue; 1326 } 1327 uaa.iface = iface; 1328 1329 uaa.info.bInterfaceClass = 1330 iface->idesc->bInterfaceClass; 1331 uaa.info.bInterfaceSubClass = 1332 iface->idesc->bInterfaceSubClass; 1333 uaa.info.bInterfaceProtocol = 1334 iface->idesc->bInterfaceProtocol; 1335 uaa.info.bIfaceIndex = i; 1336 uaa.info.bIfaceNum = 1337 iface->idesc->bInterfaceNumber; 1338 uaa.use_generic = 0; 1339 uaa.driver_info = 0; /* reset driver_info */ 1340 1341 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1342 uaa.info.bInterfaceClass, 1343 uaa.info.bInterfaceSubClass, 1344 uaa.info.bInterfaceProtocol, 1345 uaa.info.bIfaceIndex, 1346 uaa.info.bIfaceNum); 1347 1348 /* try specific interface drivers first */ 1349 1350 if (usb_probe_and_attach_sub(udev, &uaa)) { 1351 /* ignore */ 1352 } 1353 /* try generic interface drivers last */ 1354 1355 uaa.use_generic = 1; 1356 uaa.driver_info = 0; /* reset driver_info */ 1357 1358 if (usb_probe_and_attach_sub(udev, &uaa)) { 1359 /* ignore */ 1360 } 1361 } 1362 1363 if (uaa.temp_dev) { 1364 /* remove the last created child; it is unused */ 1365 1366 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) { 1367 DPRINTFN(0, "device delete child failed\n"); 1368 } 1369 } 1370 done: 1371 if (do_unlock) 1372 usbd_enum_unlock(udev); 1373 1374 return (0); 1375 } 1376 1377 /*------------------------------------------------------------------------* 1378 * usb_suspend_resume_sub 1379 * 1380 * This function is called when the suspend or resume methods should 1381 * be executed on an USB device. 1382 *------------------------------------------------------------------------*/ 1383 static void 1384 usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 1385 { 1386 int err; 1387 1388 if (dev == NULL) { 1389 return; 1390 } 1391 if (!device_is_attached(dev)) { 1392 return; 1393 } 1394 if (do_suspend) { 1395 err = DEVICE_SUSPEND(dev); 1396 } else { 1397 err = DEVICE_RESUME(dev); 1398 } 1399 if (err) { 1400 device_printf(dev, "%s failed\n", 1401 do_suspend ? "Suspend" : "Resume"); 1402 } 1403 } 1404 1405 /*------------------------------------------------------------------------* 1406 * usb_suspend_resume 1407 * 1408 * The following function will suspend or resume the USB device. 1409 * 1410 * Returns: 1411 * 0: Success 1412 * Else: Failure 1413 *------------------------------------------------------------------------*/ 1414 usb_error_t 1415 usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 1416 { 1417 struct usb_interface *iface; 1418 uint8_t i; 1419 1420 if (udev == NULL) { 1421 /* nothing to do */ 1422 return (0); 1423 } 1424 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1425 1426 sx_assert(&udev->sr_sx, SA_LOCKED); 1427 1428 USB_BUS_LOCK(udev->bus); 1429 /* filter the suspend events */ 1430 if (udev->flags.peer_suspended == do_suspend) { 1431 USB_BUS_UNLOCK(udev->bus); 1432 /* nothing to do */ 1433 return (0); 1434 } 1435 udev->flags.peer_suspended = do_suspend; 1436 USB_BUS_UNLOCK(udev->bus); 1437 1438 /* do the suspend or resume */ 1439 1440 for (i = 0; i != USB_IFACE_MAX; i++) { 1441 1442 iface = usbd_get_iface(udev, i); 1443 if (iface == NULL) { 1444 /* looks like the end of the USB interfaces */ 1445 break; 1446 } 1447 usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 1448 } 1449 return (0); 1450 } 1451 1452 /*------------------------------------------------------------------------* 1453 * usbd_clear_stall_proc 1454 * 1455 * This function performs generic USB clear stall operations. 1456 *------------------------------------------------------------------------*/ 1457 static void 1458 usbd_clear_stall_proc(struct usb_proc_msg *_pm) 1459 { 1460 struct usb_clear_stall_msg *pm = (void *)_pm; 1461 struct usb_device *udev = pm->udev; 1462 1463 /* Change lock */ 1464 USB_BUS_UNLOCK(udev->bus); 1465 mtx_lock(&udev->device_mtx); 1466 1467 /* Start clear stall callback */ 1468 usbd_transfer_start(udev->ctrl_xfer[1]); 1469 1470 /* Change lock */ 1471 mtx_unlock(&udev->device_mtx); 1472 USB_BUS_LOCK(udev->bus); 1473 } 1474 1475 /*------------------------------------------------------------------------* 1476 * usb_alloc_device 1477 * 1478 * This function allocates a new USB device. This function is called 1479 * when a new device has been put in the powered state, but not yet in 1480 * the addressed state. Get initial descriptor, set the address, get 1481 * full descriptor and get strings. 1482 * 1483 * Return values: 1484 * 0: Failure 1485 * Else: Success 1486 *------------------------------------------------------------------------*/ 1487 struct usb_device * 1488 usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1489 struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 1490 uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 1491 { 1492 struct usb_attach_arg uaa; 1493 struct usb_device *udev; 1494 struct usb_device *adev; 1495 struct usb_device *hub; 1496 uint8_t *scratch_ptr; 1497 size_t scratch_size; 1498 usb_error_t err; 1499 uint8_t device_index; 1500 uint8_t config_index; 1501 uint8_t config_quirk; 1502 uint8_t set_config_failed; 1503 1504 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1505 "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 1506 parent_dev, bus, parent_hub, depth, port_index, port_no, 1507 speed, mode); 1508 1509 /* 1510 * Find an unused device index. In USB Host mode this is the 1511 * same as the device address. 1512 * 1513 * Device index zero is not used and device index 1 should 1514 * always be the root hub. 1515 */ 1516 for (device_index = USB_ROOT_HUB_ADDR; 1517 (device_index != bus->devices_max) && 1518 (bus->devices[device_index] != NULL); 1519 device_index++) /* nop */; 1520 1521 if (device_index == bus->devices_max) { 1522 device_printf(bus->bdev, 1523 "No free USB device index for new device\n"); 1524 return (NULL); 1525 } 1526 1527 if (depth > 0x10) { 1528 device_printf(bus->bdev, 1529 "Invalid device depth\n"); 1530 return (NULL); 1531 } 1532 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1533 if (udev == NULL) { 1534 return (NULL); 1535 } 1536 /* initialise our SX-lock */ 1537 sx_init_flags(&udev->ctrl_sx, "USB device SX lock", SX_DUPOK); 1538 1539 /* initialise our SX-lock */ 1540 sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1541 sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_DUPOK); 1542 1543 cv_init(&udev->ctrlreq_cv, "WCTRL"); 1544 cv_init(&udev->ref_cv, "UGONE"); 1545 1546 /* initialise our mutex */ 1547 mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 1548 1549 /* initialise generic clear stall */ 1550 udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 1551 udev->cs_msg[0].udev = udev; 1552 udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 1553 udev->cs_msg[1].udev = udev; 1554 1555 /* initialise some USB device fields */ 1556 udev->parent_hub = parent_hub; 1557 udev->parent_dev = parent_dev; 1558 udev->port_index = port_index; 1559 udev->port_no = port_no; 1560 udev->depth = depth; 1561 udev->bus = bus; 1562 udev->address = USB_START_ADDR; /* default value */ 1563 udev->plugtime = (usb_ticks_t)ticks; 1564 /* 1565 * We need to force the power mode to "on" because there are plenty 1566 * of USB devices out there that do not work very well with 1567 * automatic suspend and resume! 1568 */ 1569 udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 1570 udev->pwr_save.last_xfer_time = ticks; 1571 /* we are not ready yet */ 1572 udev->refcount = 1; 1573 1574 /* set up default endpoint descriptor */ 1575 udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 1576 udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1577 udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1578 udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 1579 udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1580 udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 1581 udev->ctrl_ep_desc.bInterval = 0; 1582 1583 /* set up default endpoint companion descriptor */ 1584 udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1585 udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1586 1587 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1588 1589 udev->speed = speed; 1590 udev->flags.usb_mode = mode; 1591 1592 /* search for our High Speed USB HUB, if any */ 1593 1594 adev = udev; 1595 hub = udev->parent_hub; 1596 1597 while (hub) { 1598 if (hub->speed == USB_SPEED_HIGH) { 1599 udev->hs_hub_addr = hub->address; 1600 udev->parent_hs_hub = hub; 1601 udev->hs_port_no = adev->port_no; 1602 break; 1603 } 1604 adev = hub; 1605 hub = hub->parent_hub; 1606 } 1607 1608 /* init the default endpoint */ 1609 usb_init_endpoint(udev, 0, 1610 &udev->ctrl_ep_desc, 1611 &udev->ctrl_ep_comp_desc, 1612 &udev->ctrl_ep); 1613 1614 /* set device index */ 1615 udev->device_index = device_index; 1616 1617 #if USB_HAVE_UGEN 1618 /* Create ugen name */ 1619 snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1620 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1621 device_index); 1622 LIST_INIT(&udev->pd_list); 1623 1624 /* Create the control endpoint device */ 1625 udev->ctrl_dev = usb_make_dev(udev, 0, FREAD|FWRITE); 1626 1627 /* Create a link from /dev/ugenX.X to the default endpoint */ 1628 make_dev_alias(udev->ctrl_dev, "%s", udev->ugen_name); 1629 #endif 1630 /* Initialise device */ 1631 if (bus->methods->device_init != NULL) { 1632 err = (bus->methods->device_init) (udev); 1633 if (err != 0) { 1634 DPRINTFN(0, "device init %d failed " 1635 "(%s, ignored)\n", device_index, 1636 usbd_errstr(err)); 1637 goto done; 1638 } 1639 } 1640 /* set powered device state after device init is complete */ 1641 usb_set_device_state(udev, USB_STATE_POWERED); 1642 1643 if (udev->flags.usb_mode == USB_MODE_HOST) { 1644 1645 err = usbd_req_set_address(udev, NULL, device_index); 1646 1647 /* 1648 * This is the new USB device address from now on, if 1649 * the set address request didn't set it already. 1650 */ 1651 if (udev->address == USB_START_ADDR) 1652 udev->address = device_index; 1653 1654 /* 1655 * We ignore any set-address errors, hence there are 1656 * buggy USB devices out there that actually receive 1657 * the SETUP PID, but manage to set the address before 1658 * the STATUS stage is ACK'ed. If the device responds 1659 * to the subsequent get-descriptor at the new 1660 * address, then we know that the set-address command 1661 * was successful. 1662 */ 1663 if (err) { 1664 DPRINTFN(0, "set address %d failed " 1665 "(%s, ignored)\n", udev->address, 1666 usbd_errstr(err)); 1667 } 1668 } else { 1669 /* We are not self powered */ 1670 udev->flags.self_powered = 0; 1671 1672 /* Set unconfigured state */ 1673 udev->curr_config_no = USB_UNCONFIG_NO; 1674 udev->curr_config_index = USB_UNCONFIG_INDEX; 1675 1676 /* Setup USB descriptors */ 1677 err = (usb_temp_setup_by_index_p) (udev, usb_template); 1678 if (err) { 1679 DPRINTFN(0, "setting up USB template failed maybe the USB " 1680 "template module has not been loaded\n"); 1681 goto done; 1682 } 1683 } 1684 usb_set_device_state(udev, USB_STATE_ADDRESSED); 1685 1686 /* setup the device descriptor and the initial "wMaxPacketSize" */ 1687 err = usbd_setup_device_desc(udev, NULL); 1688 1689 if (err != 0) { 1690 /* XXX try to re-enumerate the device */ 1691 err = usbd_req_re_enumerate(udev, NULL); 1692 if (err) 1693 goto done; 1694 } 1695 1696 /* 1697 * Setup temporary USB attach args so that we can figure out some 1698 * basic quirks for this device. 1699 */ 1700 usb_init_attach_arg(udev, &uaa); 1701 1702 if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 1703 udev->flags.uq_bus_powered = 1; 1704 } 1705 if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 1706 udev->flags.no_strings = 1; 1707 } 1708 /* 1709 * Workaround for buggy USB devices. 1710 * 1711 * It appears that some string-less USB chips will crash and 1712 * disappear if any attempts are made to read any string 1713 * descriptors. 1714 * 1715 * Try to detect such chips by checking the strings in the USB 1716 * device descriptor. If no strings are present there we 1717 * simply disable all USB strings. 1718 */ 1719 scratch_ptr = udev->bus->scratch[0].data; 1720 scratch_size = sizeof(udev->bus->scratch[0].data); 1721 1722 if (udev->ddesc.iManufacturer || 1723 udev->ddesc.iProduct || 1724 udev->ddesc.iSerialNumber) { 1725 /* read out the language ID string */ 1726 err = usbd_req_get_string_desc(udev, NULL, 1727 (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1728 } else { 1729 err = USB_ERR_INVAL; 1730 } 1731 1732 if (err || (scratch_ptr[0] < 4)) { 1733 udev->flags.no_strings = 1; 1734 } else { 1735 uint16_t langid; 1736 uint16_t pref; 1737 uint16_t mask; 1738 uint8_t x; 1739 1740 /* load preferred value and mask */ 1741 pref = usb_lang_id; 1742 mask = usb_lang_mask; 1743 1744 /* align length correctly */ 1745 scratch_ptr[0] &= ~1; 1746 1747 /* fix compiler warning */ 1748 langid = 0; 1749 1750 /* search for preferred language */ 1751 for (x = 2; (x < scratch_ptr[0]); x += 2) { 1752 langid = UGETW(scratch_ptr + x); 1753 if ((langid & mask) == pref) 1754 break; 1755 } 1756 if (x >= scratch_ptr[0]) { 1757 /* pick the first language as the default */ 1758 DPRINTFN(1, "Using first language\n"); 1759 langid = UGETW(scratch_ptr + 2); 1760 } 1761 1762 DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1763 udev->langid = langid; 1764 } 1765 1766 /* assume 100mA bus powered for now. Changed when configured. */ 1767 udev->power = USB_MIN_POWER; 1768 /* fetch the vendor and product strings from the device */ 1769 usbd_set_device_strings(udev); 1770 1771 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 1772 /* USB device mode setup is complete */ 1773 err = 0; 1774 goto config_done; 1775 } 1776 1777 /* 1778 * Most USB devices should attach to config index 0 by 1779 * default 1780 */ 1781 if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1782 config_index = 0; 1783 config_quirk = 1; 1784 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1785 config_index = 1; 1786 config_quirk = 1; 1787 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1788 config_index = 2; 1789 config_quirk = 1; 1790 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1791 config_index = 3; 1792 config_quirk = 1; 1793 } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1794 config_index = 4; 1795 config_quirk = 1; 1796 } else { 1797 config_index = 0; 1798 config_quirk = 0; 1799 } 1800 1801 set_config_failed = 0; 1802 repeat_set_config: 1803 1804 DPRINTF("setting config %u\n", config_index); 1805 1806 /* get the USB device configured */ 1807 err = usbd_set_config_index(udev, config_index); 1808 if (err) { 1809 if (udev->ddesc.bNumConfigurations != 0) { 1810 if (!set_config_failed) { 1811 set_config_failed = 1; 1812 /* XXX try to re-enumerate the device */ 1813 err = usbd_req_re_enumerate(udev, NULL); 1814 if (err == 0) 1815 goto repeat_set_config; 1816 } 1817 DPRINTFN(0, "Failure selecting configuration index %u:" 1818 "%s, port %u, addr %u (ignored)\n", 1819 config_index, usbd_errstr(err), udev->port_no, 1820 udev->address); 1821 } 1822 /* 1823 * Some USB devices do not have any configurations. Ignore any 1824 * set config failures! 1825 */ 1826 err = 0; 1827 goto config_done; 1828 } 1829 if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 1830 if ((udev->cdesc->bNumInterface < 2) && 1831 usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 1832 DPRINTFN(0, "Found no endpoints, trying next config\n"); 1833 config_index++; 1834 goto repeat_set_config; 1835 } 1836 if (config_index == 0) { 1837 /* 1838 * Try to figure out if we have an 1839 * auto-install disk there: 1840 */ 1841 if (usb_iface_is_cdrom(udev, 0)) { 1842 DPRINTFN(0, "Found possible auto-install " 1843 "disk (trying next config)\n"); 1844 config_index++; 1845 goto repeat_set_config; 1846 } 1847 } 1848 } 1849 EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1850 if (uaa.dev_state != UAA_DEV_READY) { 1851 /* leave device unconfigured */ 1852 usb_unconfigure(udev, 0); 1853 } 1854 1855 config_done: 1856 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1857 udev->address, udev, udev->parent_hub); 1858 1859 /* register our device - we are ready */ 1860 usb_bus_port_set_device(bus, parent_hub ? 1861 parent_hub->hub->ports + port_index : NULL, udev, device_index); 1862 1863 #if USB_HAVE_UGEN 1864 /* Symlink the ugen device name */ 1865 udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 1866 1867 /* Announce device */ 1868 printf("%s: <%s> at %s\n", udev->ugen_name, 1869 usb_get_manufacturer(udev), 1870 device_get_nameunit(udev->bus->bdev)); 1871 #endif 1872 1873 #if USB_HAVE_DEVCTL 1874 usb_notify_addq("ATTACH", udev); 1875 #endif 1876 done: 1877 if (err) { 1878 /* 1879 * Free USB device and all subdevices, if any. 1880 */ 1881 usb_free_device(udev, 0); 1882 udev = NULL; 1883 } 1884 return (udev); 1885 } 1886 1887 #if USB_HAVE_UGEN 1888 static struct cdev * 1889 usb_make_dev(struct usb_device *udev, int ep, int mode) 1890 { 1891 struct usb_fs_privdata* pd; 1892 char devname[20]; 1893 1894 /* Store information to locate ourselves again later */ 1895 pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 1896 M_WAITOK | M_ZERO); 1897 pd->bus_index = device_get_unit(udev->bus->bdev); 1898 pd->dev_index = udev->device_index; 1899 pd->ep_addr = ep; 1900 pd->mode = mode; 1901 1902 /* Now, create the device itself */ 1903 snprintf(devname, sizeof(devname), "%u.%u.%u", 1904 pd->bus_index, pd->dev_index, pd->ep_addr); 1905 pd->cdev = make_dev(&usb_devsw, 0, UID_ROOT, 1906 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname); 1907 pd->cdev->si_drv1 = pd; 1908 1909 return (pd->cdev); 1910 } 1911 1912 static void 1913 usb_cdev_create(struct usb_device *udev) 1914 { 1915 struct usb_config_descriptor *cd; 1916 struct usb_endpoint_descriptor *ed; 1917 struct usb_descriptor *desc; 1918 struct usb_fs_privdata* pd; 1919 struct cdev *dev; 1920 int inmode, outmode, inmask, outmask, mode; 1921 uint8_t ep; 1922 1923 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 1924 1925 DPRINTFN(2, "Creating device nodes\n"); 1926 1927 if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 1928 inmode = FWRITE; 1929 outmode = FREAD; 1930 } else { /* USB_MODE_HOST */ 1931 inmode = FREAD; 1932 outmode = FWRITE; 1933 } 1934 1935 inmask = 0; 1936 outmask = 0; 1937 desc = NULL; 1938 1939 /* 1940 * Collect all used endpoint numbers instead of just 1941 * generating 16 static endpoints. 1942 */ 1943 cd = usbd_get_config_descriptor(udev); 1944 while ((desc = usb_desc_foreach(cd, desc))) { 1945 /* filter out all endpoint descriptors */ 1946 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 1947 (desc->bLength >= sizeof(*ed))) { 1948 ed = (struct usb_endpoint_descriptor *)desc; 1949 1950 /* update masks */ 1951 ep = ed->bEndpointAddress; 1952 if (UE_GET_DIR(ep) == UE_DIR_OUT) 1953 outmask |= 1 << UE_GET_ADDR(ep); 1954 else 1955 inmask |= 1 << UE_GET_ADDR(ep); 1956 } 1957 } 1958 1959 /* Create all available endpoints except EP0 */ 1960 for (ep = 1; ep < 16; ep++) { 1961 mode = inmask & (1 << ep) ? inmode : 0; 1962 mode |= outmask & (1 << ep) ? outmode : 0; 1963 if (mode == 0) 1964 continue; /* no IN or OUT endpoint */ 1965 1966 dev = usb_make_dev(udev, ep, mode); 1967 pd = dev->si_drv1; 1968 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 1969 } 1970 } 1971 1972 static void 1973 usb_cdev_free(struct usb_device *udev) 1974 { 1975 struct usb_fs_privdata* pd; 1976 struct cdev* pcdev; 1977 1978 DPRINTFN(2, "Freeing device nodes\n"); 1979 1980 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 1981 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 1982 1983 pcdev = pd->cdev; 1984 pd->cdev = NULL; 1985 LIST_REMOVE(pd, pd_next); 1986 if (pcdev != NULL) 1987 destroy_dev_sched_cb(pcdev, usb_cdev_cleanup, pd); 1988 } 1989 } 1990 1991 static void 1992 usb_cdev_cleanup(void* arg) 1993 { 1994 free(arg, M_USBDEV); 1995 } 1996 #endif 1997 1998 /*------------------------------------------------------------------------* 1999 * usb_free_device 2000 * 2001 * This function is NULL safe and will free an USB device and its 2002 * children devices, if any. 2003 * 2004 * Flag values: Reserved, set to zero. 2005 *------------------------------------------------------------------------*/ 2006 void 2007 usb_free_device(struct usb_device *udev, uint8_t flag) 2008 { 2009 struct usb_bus *bus; 2010 2011 if (udev == NULL) 2012 return; /* already freed */ 2013 2014 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 2015 2016 bus = udev->bus; 2017 usb_set_device_state(udev, USB_STATE_DETACHED); 2018 2019 #if USB_HAVE_DEVCTL 2020 usb_notify_addq("DETACH", udev); 2021 #endif 2022 2023 #if USB_HAVE_UGEN 2024 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, 2025 usb_get_manufacturer(udev), device_get_nameunit(bus->bdev)); 2026 2027 /* Destroy UGEN symlink, if any */ 2028 if (udev->ugen_symlink) { 2029 usb_free_symlink(udev->ugen_symlink); 2030 udev->ugen_symlink = NULL; 2031 } 2032 #endif 2033 /* 2034 * Unregister our device first which will prevent any further 2035 * references: 2036 */ 2037 usb_bus_port_set_device(bus, udev->parent_hub ? 2038 udev->parent_hub->hub->ports + udev->port_index : NULL, 2039 NULL, USB_ROOT_HUB_ADDR); 2040 2041 #if USB_HAVE_UGEN 2042 /* wait for all pending references to go away: */ 2043 mtx_lock(&usb_ref_lock); 2044 udev->refcount--; 2045 while (udev->refcount != 0) { 2046 cv_wait(&udev->ref_cv, &usb_ref_lock); 2047 } 2048 mtx_unlock(&usb_ref_lock); 2049 2050 destroy_dev_sched_cb(udev->ctrl_dev, usb_cdev_cleanup, 2051 udev->ctrl_dev->si_drv1); 2052 #endif 2053 2054 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2055 /* stop receiving any control transfers (Device Side Mode) */ 2056 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2057 } 2058 2059 /* the following will get the device unconfigured in software */ 2060 usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 2061 2062 /* unsetup any leftover default USB transfers */ 2063 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 2064 2065 /* template unsetup, if any */ 2066 (usb_temp_unsetup_p) (udev); 2067 2068 /* 2069 * Make sure that our clear-stall messages are not queued 2070 * anywhere: 2071 */ 2072 USB_BUS_LOCK(udev->bus); 2073 usb_proc_mwait(&udev->bus->non_giant_callback_proc, 2074 &udev->cs_msg[0], &udev->cs_msg[1]); 2075 USB_BUS_UNLOCK(udev->bus); 2076 2077 sx_destroy(&udev->ctrl_sx); 2078 sx_destroy(&udev->enum_sx); 2079 sx_destroy(&udev->sr_sx); 2080 2081 cv_destroy(&udev->ctrlreq_cv); 2082 cv_destroy(&udev->ref_cv); 2083 2084 mtx_destroy(&udev->device_mtx); 2085 #if USB_HAVE_UGEN 2086 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 2087 #endif 2088 2089 /* Uninitialise device */ 2090 if (bus->methods->device_uninit != NULL) 2091 (bus->methods->device_uninit) (udev); 2092 2093 /* free device */ 2094 free(udev->serial, M_USB); 2095 free(udev->manufacturer, M_USB); 2096 free(udev->product, M_USB); 2097 free(udev, M_USB); 2098 } 2099 2100 /*------------------------------------------------------------------------* 2101 * usbd_get_iface 2102 * 2103 * This function is the safe way to get the USB interface structure 2104 * pointer by interface index. 2105 * 2106 * Return values: 2107 * NULL: Interface not present. 2108 * Else: Pointer to USB interface structure. 2109 *------------------------------------------------------------------------*/ 2110 struct usb_interface * 2111 usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 2112 { 2113 struct usb_interface *iface = udev->ifaces + iface_index; 2114 2115 if (iface_index >= udev->ifaces_max) 2116 return (NULL); 2117 return (iface); 2118 } 2119 2120 /*------------------------------------------------------------------------* 2121 * usbd_find_descriptor 2122 * 2123 * This function will lookup the first descriptor that matches the 2124 * criteria given by the arguments "type" and "subtype". Descriptors 2125 * will only be searched within the interface having the index 2126 * "iface_index". If the "id" argument points to an USB descriptor, 2127 * it will be skipped before the search is started. This allows 2128 * searching for multiple descriptors using the same criteria. Else 2129 * the search is started after the interface descriptor. 2130 * 2131 * Return values: 2132 * NULL: End of descriptors 2133 * Else: A descriptor matching the criteria 2134 *------------------------------------------------------------------------*/ 2135 void * 2136 usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 2137 uint8_t type, uint8_t type_mask, 2138 uint8_t subtype, uint8_t subtype_mask) 2139 { 2140 struct usb_descriptor *desc; 2141 struct usb_config_descriptor *cd; 2142 struct usb_interface *iface; 2143 2144 cd = usbd_get_config_descriptor(udev); 2145 if (cd == NULL) { 2146 return (NULL); 2147 } 2148 if (id == NULL) { 2149 iface = usbd_get_iface(udev, iface_index); 2150 if (iface == NULL) { 2151 return (NULL); 2152 } 2153 id = usbd_get_interface_descriptor(iface); 2154 if (id == NULL) { 2155 return (NULL); 2156 } 2157 } 2158 desc = (void *)id; 2159 2160 while ((desc = usb_desc_foreach(cd, desc))) { 2161 2162 if (desc->bDescriptorType == UDESC_INTERFACE) { 2163 break; 2164 } 2165 if (((desc->bDescriptorType & type_mask) == type) && 2166 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 2167 return (desc); 2168 } 2169 } 2170 return (NULL); 2171 } 2172 2173 /*------------------------------------------------------------------------* 2174 * usb_devinfo 2175 * 2176 * This function will dump information from the device descriptor 2177 * belonging to the USB device pointed to by "udev", to the string 2178 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2179 * including the terminating zero. 2180 *------------------------------------------------------------------------*/ 2181 void 2182 usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 2183 { 2184 struct usb_device_descriptor *udd = &udev->ddesc; 2185 uint16_t bcdDevice; 2186 uint16_t bcdUSB; 2187 2188 bcdUSB = UGETW(udd->bcdUSB); 2189 bcdDevice = UGETW(udd->bcdDevice); 2190 2191 if (udd->bDeviceClass != 0xFF) { 2192 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2193 "%x.%02x, addr %d", 2194 usb_get_manufacturer(udev), 2195 usb_get_product(udev), 2196 udd->bDeviceClass, udd->bDeviceSubClass, 2197 (bcdUSB >> 8), bcdUSB & 0xFF, 2198 (bcdDevice >> 8), bcdDevice & 0xFF, 2199 udev->address); 2200 } else { 2201 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2202 "%x.%02x, addr %d", 2203 usb_get_manufacturer(udev), 2204 usb_get_product(udev), 2205 (bcdUSB >> 8), bcdUSB & 0xFF, 2206 (bcdDevice >> 8), bcdDevice & 0xFF, 2207 udev->address); 2208 } 2209 } 2210 2211 #ifdef USB_VERBOSE 2212 /* 2213 * Descriptions of of known vendors and devices ("products"). 2214 */ 2215 struct usb_knowndev { 2216 uint16_t vendor; 2217 uint16_t product; 2218 uint32_t flags; 2219 const char *vendorname; 2220 const char *productname; 2221 }; 2222 2223 #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2224 2225 #include "usbdevs.h" 2226 #include "usbdevs_data.h" 2227 #endif /* USB_VERBOSE */ 2228 2229 static void 2230 usbd_set_device_strings(struct usb_device *udev) 2231 { 2232 struct usb_device_descriptor *udd = &udev->ddesc; 2233 #ifdef USB_VERBOSE 2234 const struct usb_knowndev *kdp; 2235 #endif 2236 char *temp_ptr; 2237 size_t temp_size; 2238 uint16_t vendor_id; 2239 uint16_t product_id; 2240 2241 temp_ptr = (char *)udev->bus->scratch[0].data; 2242 temp_size = sizeof(udev->bus->scratch[0].data); 2243 2244 vendor_id = UGETW(udd->idVendor); 2245 product_id = UGETW(udd->idProduct); 2246 2247 /* get serial number string */ 2248 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2249 udev->ddesc.iSerialNumber); 2250 udev->serial = strdup(temp_ptr, M_USB); 2251 2252 /* get manufacturer string */ 2253 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2254 udev->ddesc.iManufacturer); 2255 usb_trim_spaces(temp_ptr); 2256 if (temp_ptr[0] != '\0') 2257 udev->manufacturer = strdup(temp_ptr, M_USB); 2258 2259 /* get product string */ 2260 usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2261 udev->ddesc.iProduct); 2262 usb_trim_spaces(temp_ptr); 2263 if (temp_ptr[0] != '\0') 2264 udev->product = strdup(temp_ptr, M_USB); 2265 2266 #ifdef USB_VERBOSE 2267 if (udev->manufacturer == NULL || udev->product == NULL) { 2268 for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 2269 if (kdp->vendor == vendor_id && 2270 (kdp->product == product_id || 2271 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2272 break; 2273 } 2274 if (kdp->vendorname != NULL) { 2275 /* XXX should use pointer to knowndevs string */ 2276 if (udev->manufacturer == NULL) { 2277 udev->manufacturer = strdup(kdp->vendorname, 2278 M_USB); 2279 } 2280 if (udev->product == NULL && 2281 (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2282 udev->product = strdup(kdp->productname, 2283 M_USB); 2284 } 2285 } 2286 } 2287 #endif 2288 /* Provide default strings if none were found */ 2289 if (udev->manufacturer == NULL) { 2290 snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 2291 udev->manufacturer = strdup(temp_ptr, M_USB); 2292 } 2293 if (udev->product == NULL) { 2294 snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 2295 udev->product = strdup(temp_ptr, M_USB); 2296 } 2297 } 2298 2299 /* 2300 * Returns: 2301 * See: USB_MODE_XXX 2302 */ 2303 enum usb_hc_mode 2304 usbd_get_mode(struct usb_device *udev) 2305 { 2306 return (udev->flags.usb_mode); 2307 } 2308 2309 /* 2310 * Returns: 2311 * See: USB_SPEED_XXX 2312 */ 2313 enum usb_dev_speed 2314 usbd_get_speed(struct usb_device *udev) 2315 { 2316 return (udev->speed); 2317 } 2318 2319 uint32_t 2320 usbd_get_isoc_fps(struct usb_device *udev) 2321 { 2322 ; /* indent fix */ 2323 switch (udev->speed) { 2324 case USB_SPEED_LOW: 2325 case USB_SPEED_FULL: 2326 return (1000); 2327 default: 2328 return (8000); 2329 } 2330 } 2331 2332 struct usb_device_descriptor * 2333 usbd_get_device_descriptor(struct usb_device *udev) 2334 { 2335 if (udev == NULL) 2336 return (NULL); /* be NULL safe */ 2337 return (&udev->ddesc); 2338 } 2339 2340 struct usb_config_descriptor * 2341 usbd_get_config_descriptor(struct usb_device *udev) 2342 { 2343 if (udev == NULL) 2344 return (NULL); /* be NULL safe */ 2345 return (udev->cdesc); 2346 } 2347 2348 /*------------------------------------------------------------------------* 2349 * usb_test_quirk - test a device for a given quirk 2350 * 2351 * Return values: 2352 * 0: The USB device does not have the given quirk. 2353 * Else: The USB device has the given quirk. 2354 *------------------------------------------------------------------------*/ 2355 uint8_t 2356 usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 2357 { 2358 uint8_t found; 2359 2360 found = (usb_test_quirk_p) (&uaa->info, quirk); 2361 return (found); 2362 } 2363 2364 struct usb_interface_descriptor * 2365 usbd_get_interface_descriptor(struct usb_interface *iface) 2366 { 2367 if (iface == NULL) 2368 return (NULL); /* be NULL safe */ 2369 return (iface->idesc); 2370 } 2371 2372 uint8_t 2373 usbd_get_interface_altindex(struct usb_interface *iface) 2374 { 2375 return (iface->alt_index); 2376 } 2377 2378 uint8_t 2379 usbd_get_bus_index(struct usb_device *udev) 2380 { 2381 return ((uint8_t)device_get_unit(udev->bus->bdev)); 2382 } 2383 2384 uint8_t 2385 usbd_get_device_index(struct usb_device *udev) 2386 { 2387 return (udev->device_index); 2388 } 2389 2390 #if USB_HAVE_DEVCTL 2391 /*------------------------------------------------------------------------* 2392 * usb_notify_addq 2393 * 2394 * This function will generate events for dev. 2395 *------------------------------------------------------------------------*/ 2396 #ifndef BURN_BRIDGES 2397 static void 2398 usb_notify_addq_compat(const char *type, struct usb_device *udev) 2399 { 2400 char *data = NULL; 2401 const char *ntype; 2402 struct malloc_type *mt; 2403 const size_t buf_size = 512; 2404 2405 /* Convert notify type */ 2406 if (strcmp(type, "ATTACH") == 0) 2407 ntype = "+"; 2408 else if (strcmp(type, "DETACH") == 0) 2409 ntype = "-"; 2410 else 2411 return; 2412 2413 mtx_lock(&malloc_mtx); 2414 mt = malloc_desc2type("bus"); /* XXX M_BUS */ 2415 mtx_unlock(&malloc_mtx); 2416 if (mt == NULL) 2417 return; 2418 2419 data = malloc(buf_size, mt, M_NOWAIT); 2420 if (data == NULL) 2421 return; 2422 2423 /* String it all together. */ 2424 snprintf(data, buf_size, 2425 "%s" 2426 #if USB_HAVE_UGEN 2427 "%s " 2428 #endif 2429 "at port=%u " 2430 "vendor=0x%04x " 2431 "product=0x%04x " 2432 "devclass=0x%02x " 2433 "devsubclass=0x%02x " 2434 "sernum=\"%s\" " 2435 "release=0x%04x " 2436 #if USB_HAVE_UGEN 2437 "on %s\n" 2438 #endif 2439 "", 2440 ntype, 2441 #if USB_HAVE_UGEN 2442 udev->ugen_name, 2443 #endif 2444 udev->port_no, 2445 UGETW(udev->ddesc.idVendor), 2446 UGETW(udev->ddesc.idProduct), 2447 udev->ddesc.bDeviceClass, 2448 udev->ddesc.bDeviceSubClass, 2449 usb_get_serial(udev), 2450 UGETW(udev->ddesc.bcdDevice) 2451 #if USB_HAVE_UGEN 2452 , udev->parent_hub != NULL ? 2453 udev->parent_hub->ugen_name : 2454 device_get_nameunit(device_get_parent(udev->bus->bdev)) 2455 #endif 2456 ); 2457 2458 devctl_queue_data(data); 2459 } 2460 #endif 2461 2462 static void 2463 usb_notify_addq(const char *type, struct usb_device *udev) 2464 { 2465 struct usb_interface *iface; 2466 struct sbuf *sb; 2467 int i; 2468 2469 #ifndef BURN_BRIDGES 2470 usb_notify_addq_compat(type, udev); 2471 #endif 2472 2473 /* announce the device */ 2474 sb = sbuf_new_auto(); 2475 sbuf_printf(sb, 2476 #if USB_HAVE_UGEN 2477 "ugen=%s " 2478 #endif 2479 "vendor=0x%04x " 2480 "product=0x%04x " 2481 "devclass=0x%02x " 2482 "devsubclass=0x%02x " 2483 "sernum=\"%s\" " 2484 "release=0x%04x " 2485 "mode=%s " 2486 "port=%u " 2487 #if USB_HAVE_UGEN 2488 "parent=%s" 2489 #endif 2490 "", 2491 #if USB_HAVE_UGEN 2492 udev->ugen_name, 2493 #endif 2494 UGETW(udev->ddesc.idVendor), 2495 UGETW(udev->ddesc.idProduct), 2496 udev->ddesc.bDeviceClass, 2497 udev->ddesc.bDeviceSubClass, 2498 usb_get_serial(udev), 2499 UGETW(udev->ddesc.bcdDevice), 2500 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2501 udev->port_no 2502 #if USB_HAVE_UGEN 2503 , udev->parent_hub != NULL ? 2504 udev->parent_hub->ugen_name : 2505 device_get_nameunit(device_get_parent(udev->bus->bdev)) 2506 #endif 2507 ); 2508 sbuf_finish(sb); 2509 devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 2510 sbuf_delete(sb); 2511 2512 /* announce each interface */ 2513 for (i = 0; i < USB_IFACE_MAX; i++) { 2514 iface = usbd_get_iface(udev, i); 2515 if (iface == NULL) 2516 break; /* end of interfaces */ 2517 if (iface->idesc == NULL) 2518 continue; /* no interface descriptor */ 2519 2520 sb = sbuf_new_auto(); 2521 sbuf_printf(sb, 2522 #if USB_HAVE_UGEN 2523 "ugen=%s " 2524 #endif 2525 "vendor=0x%04x " 2526 "product=0x%04x " 2527 "devclass=0x%02x " 2528 "devsubclass=0x%02x " 2529 "sernum=\"%s\" " 2530 "release=0x%04x " 2531 "mode=%s " 2532 "interface=%d " 2533 "endpoints=%d " 2534 "intclass=0x%02x " 2535 "intsubclass=0x%02x " 2536 "intprotocol=0x%02x", 2537 #if USB_HAVE_UGEN 2538 udev->ugen_name, 2539 #endif 2540 UGETW(udev->ddesc.idVendor), 2541 UGETW(udev->ddesc.idProduct), 2542 udev->ddesc.bDeviceClass, 2543 udev->ddesc.bDeviceSubClass, 2544 usb_get_serial(udev), 2545 UGETW(udev->ddesc.bcdDevice), 2546 (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 2547 iface->idesc->bInterfaceNumber, 2548 iface->idesc->bNumEndpoints, 2549 iface->idesc->bInterfaceClass, 2550 iface->idesc->bInterfaceSubClass, 2551 iface->idesc->bInterfaceProtocol); 2552 sbuf_finish(sb); 2553 devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 2554 sbuf_delete(sb); 2555 } 2556 } 2557 #endif 2558 2559 #if USB_HAVE_UGEN 2560 /*------------------------------------------------------------------------* 2561 * usb_fifo_free_wrap 2562 * 2563 * This function will free the FIFOs. 2564 * 2565 * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2566 * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2567 * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2568 * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2569 * control endpoint FIFOs. If "iface_index" is not set to 2570 * "USB_IFACE_INDEX_ANY" the flag has no effect. 2571 *------------------------------------------------------------------------*/ 2572 static void 2573 usb_fifo_free_wrap(struct usb_device *udev, 2574 uint8_t iface_index, uint8_t flag) 2575 { 2576 struct usb_fifo *f; 2577 uint16_t i; 2578 2579 /* 2580 * Free any USB FIFOs on the given interface: 2581 */ 2582 for (i = 0; i != USB_FIFO_MAX; i++) { 2583 f = udev->fifo[i]; 2584 if (f == NULL) { 2585 continue; 2586 } 2587 /* Check if the interface index matches */ 2588 if (iface_index == f->iface_index) { 2589 if (f->methods != &usb_ugen_methods) { 2590 /* 2591 * Don't free any non-generic FIFOs in 2592 * this case. 2593 */ 2594 continue; 2595 } 2596 if ((f->dev_ep_index == 0) && 2597 (f->fs_xfer == NULL)) { 2598 /* no need to free this FIFO */ 2599 continue; 2600 } 2601 } else if (iface_index == USB_IFACE_INDEX_ANY) { 2602 if ((f->methods == &usb_ugen_methods) && 2603 (f->dev_ep_index == 0) && 2604 (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2605 (f->fs_xfer == NULL)) { 2606 /* no need to free this FIFO */ 2607 continue; 2608 } 2609 } else { 2610 /* no need to free this FIFO */ 2611 continue; 2612 } 2613 /* free this FIFO */ 2614 usb_fifo_free(f); 2615 } 2616 } 2617 #endif 2618 2619 /*------------------------------------------------------------------------* 2620 * usb_peer_can_wakeup 2621 * 2622 * Return values: 2623 * 0: Peer cannot do resume signalling. 2624 * Else: Peer can do resume signalling. 2625 *------------------------------------------------------------------------*/ 2626 uint8_t 2627 usb_peer_can_wakeup(struct usb_device *udev) 2628 { 2629 const struct usb_config_descriptor *cdp; 2630 2631 cdp = udev->cdesc; 2632 if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 2633 return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2634 } 2635 return (0); /* not supported */ 2636 } 2637 2638 void 2639 usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2640 { 2641 2642 KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2643 2644 DPRINTF("udev %p state %s -> %s\n", udev, 2645 usb_statestr(udev->state), usb_statestr(state)); 2646 udev->state = state; 2647 2648 if (udev->bus->methods->device_state_change != NULL) 2649 (udev->bus->methods->device_state_change) (udev); 2650 } 2651 2652 enum usb_dev_state 2653 usb_get_device_state(struct usb_device *udev) 2654 { 2655 if (udev == NULL) 2656 return (USB_STATE_DETACHED); 2657 return (udev->state); 2658 } 2659 2660 uint8_t 2661 usbd_device_attached(struct usb_device *udev) 2662 { 2663 return (udev->state > USB_STATE_DETACHED); 2664 } 2665 2666 /* The following function locks enumerating the given USB device. */ 2667 2668 void 2669 usbd_enum_lock(struct usb_device *udev) 2670 { 2671 sx_xlock(&udev->enum_sx); 2672 sx_xlock(&udev->sr_sx); 2673 /* 2674 * NEWBUS LOCK NOTE: We should check if any parent SX locks 2675 * are locked before locking Giant. Else the lock can be 2676 * locked multiple times. 2677 */ 2678 mtx_lock(&Giant); 2679 } 2680 2681 /* The following function unlocks enumerating the given USB device. */ 2682 2683 void 2684 usbd_enum_unlock(struct usb_device *udev) 2685 { 2686 mtx_unlock(&Giant); 2687 sx_xunlock(&udev->enum_sx); 2688 sx_xunlock(&udev->sr_sx); 2689 } 2690 2691 /* The following function locks suspend and resume. */ 2692 2693 void 2694 usbd_sr_lock(struct usb_device *udev) 2695 { 2696 sx_xlock(&udev->sr_sx); 2697 /* 2698 * NEWBUS LOCK NOTE: We should check if any parent SX locks 2699 * are locked before locking Giant. Else the lock can be 2700 * locked multiple times. 2701 */ 2702 mtx_lock(&Giant); 2703 } 2704 2705 /* The following function unlocks suspend and resume. */ 2706 2707 void 2708 usbd_sr_unlock(struct usb_device *udev) 2709 { 2710 mtx_unlock(&Giant); 2711 sx_xunlock(&udev->sr_sx); 2712 } 2713 2714 /* 2715 * The following function checks the enumerating lock for the given 2716 * USB device. 2717 */ 2718 2719 uint8_t 2720 usbd_enum_is_locked(struct usb_device *udev) 2721 { 2722 return (sx_xlocked(&udev->enum_sx)); 2723 } 2724 2725 /* 2726 * The following function is used to set the per-interface specific 2727 * plug and play information. The string referred to by the pnpinfo 2728 * argument can safely be freed after calling this function. The 2729 * pnpinfo of an interface will be reset at device detach or when 2730 * passing a NULL argument to this function. This function 2731 * returns zero on success, else a USB_ERR_XXX failure code. 2732 */ 2733 2734 usb_error_t 2735 usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 2736 { 2737 struct usb_interface *iface; 2738 2739 iface = usbd_get_iface(udev, iface_index); 2740 if (iface == NULL) 2741 return (USB_ERR_INVAL); 2742 2743 if (iface->pnpinfo != NULL) { 2744 free(iface->pnpinfo, M_USBDEV); 2745 iface->pnpinfo = NULL; 2746 } 2747 2748 if (pnpinfo == NULL || pnpinfo[0] == 0) 2749 return (0); /* success */ 2750 2751 iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 2752 if (iface->pnpinfo == NULL) 2753 return (USB_ERR_NOMEM); 2754 2755 return (0); /* success */ 2756 } 2757 2758