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