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