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