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