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