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