usb_device.c revision 189599
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 189599 2009-03-09 20:08:08Z thompsa $ */ 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 <dev/usb/usb_defs.h> 28#include <dev/usb/usb_mfunc.h> 29#include <dev/usb/usb_error.h> 30#include <dev/usb/usb.h> 31#include <dev/usb/usb_ioctl.h> 32#include "usbdevs.h" 33 34#define USB_DEBUG_VAR usb2_debug 35 36#include <dev/usb/usb_core.h> 37#include <dev/usb/usb_debug.h> 38#include <dev/usb/usb_process.h> 39#include <dev/usb/usb_device.h> 40#include <dev/usb/usb_busdma.h> 41#include <dev/usb/usb_transfer.h> 42#include <dev/usb/usb_parse.h> 43#include <dev/usb/usb_request.h> 44#include <dev/usb/usb_dynamic.h> 45#include <dev/usb/usb_hub.h> 46#include <dev/usb/usb_util.h> 47#include <dev/usb/usb_mbuf.h> 48#include <dev/usb/usb_dev.h> 49#include <dev/usb/usb_msctest.h> 50#include <dev/usb/usb_generic.h> 51 52#include <dev/usb/quirk/usb_quirk.h> 53 54#include <dev/usb/usb_controller.h> 55#include <dev/usb/usb_bus.h> 56 57/* function prototypes */ 58 59static void usb2_fill_pipe_data(struct usb2_device *, uint8_t, 60 struct usb2_endpoint_descriptor *, struct usb2_pipe *); 61static void usb2_free_pipe_data(struct usb2_device *, uint8_t, uint8_t); 62static void usb2_free_iface_data(struct usb2_device *); 63static void usb2_detach_device_sub(struct usb2_device *, device_t *, 64 uint8_t); 65static uint8_t usb2_probe_and_attach_sub(struct usb2_device *, 66 struct usb2_attach_arg *); 67static void usb2_init_attach_arg(struct usb2_device *, 68 struct usb2_attach_arg *); 69static void usb2_suspend_resume_sub(struct usb2_device *, device_t, 70 uint8_t); 71static void usb2_clear_stall_proc(struct usb2_proc_msg *_pm); 72static void usb2_check_strings(struct usb2_device *); 73static usb2_error_t usb2_fill_iface_data(struct usb2_device *, uint8_t, 74 uint8_t); 75static void usb2_notify_addq(const char *type, struct usb2_device *); 76 77#if USB_HAVE_UGEN 78static void usb2_fifo_free_wrap(struct usb2_device *, uint8_t, uint8_t); 79static struct cdev *usb2_make_dev(struct usb2_device *, int, int); 80static void usb2_cdev_create(struct usb2_device *); 81static void usb2_cdev_free(struct usb2_device *); 82static void usb2_cdev_cleanup(void *); 83#endif 84 85/* This variable is global to allow easy access to it: */ 86 87int usb2_template = 0; 88 89SYSCTL_INT(_hw_usb2, OID_AUTO, template, CTLFLAG_RW, 90 &usb2_template, 0, "Selected USB device side template"); 91 92 93/*------------------------------------------------------------------------* 94 * usb2_get_pipe_by_addr 95 * 96 * This function searches for an USB pipe by endpoint address and 97 * direction. 98 * 99 * Returns: 100 * NULL: Failure 101 * Else: Success 102 *------------------------------------------------------------------------*/ 103struct usb2_pipe * 104usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val) 105{ 106 struct usb2_pipe *pipe = udev->pipes; 107 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 108 enum { 109 EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 110 }; 111 112 /* 113 * According to the USB specification not all bits are used 114 * for the endpoint address. Keep defined bits only: 115 */ 116 ea_val &= EA_MASK; 117 118 /* 119 * Iterate accross all the USB pipes searching for a match 120 * based on the endpoint address: 121 */ 122 for (; pipe != pipe_end; pipe++) { 123 124 if (pipe->edesc == NULL) { 125 continue; 126 } 127 /* do the mask and check the value */ 128 if ((pipe->edesc->bEndpointAddress & EA_MASK) == ea_val) { 129 goto found; 130 } 131 } 132 133 /* 134 * The default pipe is always present and is checked separately: 135 */ 136 if ((udev->default_pipe.edesc) && 137 ((udev->default_pipe.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 138 pipe = &udev->default_pipe; 139 goto found; 140 } 141 return (NULL); 142 143found: 144 return (pipe); 145} 146 147/*------------------------------------------------------------------------* 148 * usb2_get_pipe 149 * 150 * This function searches for an USB pipe based on the information 151 * given by the passed "struct usb2_config" pointer. 152 * 153 * Return values: 154 * NULL: No match. 155 * Else: Pointer to "struct usb2_pipe". 156 *------------------------------------------------------------------------*/ 157struct usb2_pipe * 158usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index, 159 const struct usb2_config *setup) 160{ 161 struct usb2_pipe *pipe = udev->pipes; 162 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 163 uint8_t index = setup->ep_index; 164 uint8_t ea_mask; 165 uint8_t ea_val; 166 uint8_t type_mask; 167 uint8_t type_val; 168 169 DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 170 "type=0x%x dir=0x%x index=%d\n", 171 udev, iface_index, setup->endpoint, 172 setup->type, setup->direction, setup->ep_index); 173 174 /* setup expected endpoint direction mask and value */ 175 176 if (setup->direction == UE_DIR_ANY) { 177 /* match any endpoint direction */ 178 ea_mask = 0; 179 ea_val = 0; 180 } else { 181 /* match the given endpoint direction */ 182 ea_mask = (UE_DIR_IN | UE_DIR_OUT); 183 ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 184 } 185 186 /* setup expected endpoint address */ 187 188 if (setup->endpoint == UE_ADDR_ANY) { 189 /* match any endpoint address */ 190 } else { 191 /* match the given endpoint address */ 192 ea_mask |= UE_ADDR; 193 ea_val |= (setup->endpoint & UE_ADDR); 194 } 195 196 /* setup expected endpoint type */ 197 198 if (setup->type == UE_BULK_INTR) { 199 /* this will match BULK and INTERRUPT endpoints */ 200 type_mask = 2; 201 type_val = 2; 202 } else if (setup->type == UE_TYPE_ANY) { 203 /* match any endpoint type */ 204 type_mask = 0; 205 type_val = 0; 206 } else { 207 /* match the given endpoint type */ 208 type_mask = UE_XFERTYPE; 209 type_val = (setup->type & UE_XFERTYPE); 210 } 211 212 /* 213 * Iterate accross all the USB pipes searching for a match 214 * based on the endpoint address. Note that we are searching 215 * the pipes from the beginning of the "udev->pipes" array. 216 */ 217 for (; pipe != pipe_end; pipe++) { 218 219 if ((pipe->edesc == NULL) || 220 (pipe->iface_index != iface_index)) { 221 continue; 222 } 223 /* do the masks and check the values */ 224 225 if (((pipe->edesc->bEndpointAddress & ea_mask) == ea_val) && 226 ((pipe->edesc->bmAttributes & type_mask) == type_val)) { 227 if (!index--) { 228 goto found; 229 } 230 } 231 } 232 233 /* 234 * Match against default pipe last, so that "any pipe", "any 235 * address" and "any direction" returns the first pipe of the 236 * interface. "iface_index" and "direction" is ignored: 237 */ 238 if ((udev->default_pipe.edesc) && 239 ((udev->default_pipe.edesc->bEndpointAddress & ea_mask) == ea_val) && 240 ((udev->default_pipe.edesc->bmAttributes & type_mask) == type_val) && 241 (!index)) { 242 pipe = &udev->default_pipe; 243 goto found; 244 } 245 return (NULL); 246 247found: 248 return (pipe); 249} 250 251/*------------------------------------------------------------------------* 252 * usb2_interface_count 253 * 254 * This function stores the number of USB interfaces excluding 255 * alternate settings, which the USB config descriptor reports into 256 * the unsigned 8-bit integer pointed to by "count". 257 * 258 * Returns: 259 * 0: Success 260 * Else: Failure 261 *------------------------------------------------------------------------*/ 262usb2_error_t 263usb2_interface_count(struct usb2_device *udev, uint8_t *count) 264{ 265 if (udev->cdesc == NULL) { 266 *count = 0; 267 return (USB_ERR_NOT_CONFIGURED); 268 } 269 *count = udev->cdesc->bNumInterface; 270 return (USB_ERR_NORMAL_COMPLETION); 271} 272 273 274/*------------------------------------------------------------------------* 275 * usb2_fill_pipe_data 276 * 277 * This function will initialise the USB pipe structure pointed to by 278 * the "pipe" argument. 279 *------------------------------------------------------------------------*/ 280static void 281usb2_fill_pipe_data(struct usb2_device *udev, uint8_t iface_index, 282 struct usb2_endpoint_descriptor *edesc, struct usb2_pipe *pipe) 283{ 284 285 bzero(pipe, sizeof(*pipe)); 286 287 (udev->bus->methods->pipe_init) (udev, edesc, pipe); 288 289 if (pipe->methods == NULL) { 290 /* the pipe is invalid: just return */ 291 return; 292 } 293 /* initialise USB pipe structure */ 294 pipe->edesc = edesc; 295 pipe->iface_index = iface_index; 296 TAILQ_INIT(&pipe->pipe_q.head); 297 pipe->pipe_q.command = &usb2_pipe_start; 298 299 /* clear stall, if any */ 300 if (udev->bus->methods->clear_stall) { 301 USB_BUS_LOCK(udev->bus); 302 (udev->bus->methods->clear_stall) (udev, pipe); 303 USB_BUS_UNLOCK(udev->bus); 304 } 305} 306 307/*------------------------------------------------------------------------* 308 * usb2_free_pipe_data 309 * 310 * This function will free USB pipe data for the given interface 311 * index. Hence we do not have any dynamic allocations we simply clear 312 * "pipe->edesc" to indicate that the USB pipe structure can be 313 * reused. The pipes belonging to the given interface should not be in 314 * use when this function is called and no check is performed to 315 * prevent this. 316 *------------------------------------------------------------------------*/ 317static void 318usb2_free_pipe_data(struct usb2_device *udev, 319 uint8_t iface_index, uint8_t iface_mask) 320{ 321 struct usb2_pipe *pipe = udev->pipes; 322 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 323 324 while (pipe != pipe_end) { 325 if ((pipe->iface_index & iface_mask) == iface_index) { 326 /* free pipe */ 327 pipe->edesc = NULL; 328 } 329 pipe++; 330 } 331} 332 333/*------------------------------------------------------------------------* 334 * usb2_pipe_foreach 335 * 336 * This function will iterate all the USB endpoints except the control 337 * endpoint. This function is NULL safe. 338 * 339 * Return values: 340 * NULL: End of USB pipes 341 * Else: Pointer to next USB pipe 342 *------------------------------------------------------------------------*/ 343struct usb2_pipe * 344usb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe) 345{ 346 struct usb2_pipe *pipe_end = udev->pipes + USB_EP_MAX; 347 348 /* be NULL safe */ 349 if (udev == NULL) 350 return (NULL); 351 352 /* get next pipe */ 353 if (pipe == NULL) 354 pipe = udev->pipes; 355 else 356 pipe++; 357 358 /* find next allocated pipe */ 359 while (pipe != pipe_end) { 360 if (pipe->edesc != NULL) 361 return (pipe); 362 pipe++; 363 } 364 return (NULL); 365} 366 367/*------------------------------------------------------------------------* 368 * usb2_fill_iface_data 369 * 370 * This function will fill in interface data and allocate USB pipes 371 * for all the endpoints that belong to the given interface. This 372 * function is typically called when setting the configuration or when 373 * setting an alternate interface. 374 *------------------------------------------------------------------------*/ 375static usb2_error_t 376usb2_fill_iface_data(struct usb2_device *udev, 377 uint8_t iface_index, uint8_t alt_index) 378{ 379 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 380 struct usb2_pipe *pipe; 381 struct usb2_pipe *pipe_end; 382 struct usb2_interface_descriptor *id; 383 struct usb2_endpoint_descriptor *ed = NULL; 384 struct usb2_descriptor *desc; 385 uint8_t nendpt; 386 387 if (iface == NULL) { 388 return (USB_ERR_INVAL); 389 } 390 DPRINTFN(5, "iface_index=%d alt_index=%d\n", 391 iface_index, alt_index); 392 393 sx_assert(udev->default_sx + 1, SA_LOCKED); 394 395 pipe = udev->pipes; 396 pipe_end = udev->pipes + USB_EP_MAX; 397 398 /* 399 * Check if any USB pipes on the given USB interface are in 400 * use: 401 */ 402 while (pipe != pipe_end) { 403 if ((pipe->edesc != NULL) && 404 (pipe->iface_index == iface_index) && 405 (pipe->refcount != 0)) { 406 return (USB_ERR_IN_USE); 407 } 408 pipe++; 409 } 410 411 pipe = &udev->pipes[0]; 412 413 id = usb2_find_idesc(udev->cdesc, iface_index, alt_index); 414 if (id == NULL) { 415 return (USB_ERR_INVAL); 416 } 417 /* 418 * Free old pipes after we know that an interface descriptor exists, 419 * if any. 420 */ 421 usb2_free_pipe_data(udev, iface_index, 0 - 1); 422 423 /* Setup USB interface structure */ 424 iface->idesc = id; 425 iface->alt_index = alt_index; 426 iface->parent_iface_index = USB_IFACE_INDEX_ANY; 427 428 nendpt = id->bNumEndpoints; 429 DPRINTFN(5, "found idesc nendpt=%d\n", nendpt); 430 431 desc = (void *)id; 432 433 while (nendpt--) { 434 DPRINTFN(11, "endpt=%d\n", nendpt); 435 436 while ((desc = usb2_desc_foreach(udev->cdesc, desc))) { 437 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 438 (desc->bLength >= sizeof(*ed))) { 439 goto found; 440 } 441 if (desc->bDescriptorType == UDESC_INTERFACE) { 442 break; 443 } 444 } 445 goto error; 446 447found: 448 ed = (void *)desc; 449 450 /* find a free pipe */ 451 while (pipe != pipe_end) { 452 if (pipe->edesc == NULL) { 453 /* pipe is free */ 454 usb2_fill_pipe_data(udev, iface_index, ed, pipe); 455 break; 456 } 457 pipe++; 458 } 459 } 460 return (USB_ERR_NORMAL_COMPLETION); 461 462error: 463 /* passed end, or bad desc */ 464 DPRINTFN(0, "%s: bad descriptor(s), addr=%d!\n", 465 __FUNCTION__, udev->address); 466 467 /* free old pipes if any */ 468 usb2_free_pipe_data(udev, iface_index, 0 - 1); 469 return (USB_ERR_INVAL); 470} 471 472/*------------------------------------------------------------------------* 473 * usb2_free_iface_data 474 * 475 * This function will free all USB interfaces and USB pipes belonging 476 * to an USB device. 477 *------------------------------------------------------------------------*/ 478static void 479usb2_free_iface_data(struct usb2_device *udev) 480{ 481 struct usb2_interface *iface = udev->ifaces; 482 struct usb2_interface *iface_end = udev->ifaces + USB_IFACE_MAX; 483 484 /* mtx_assert() */ 485 486 /* free Linux compat device, if any */ 487 if (udev->linux_dev) { 488 usb_linux_free_device(udev->linux_dev); 489 udev->linux_dev = NULL; 490 } 491 /* free all pipes, if any */ 492 usb2_free_pipe_data(udev, 0, 0); 493 494 /* free all interfaces, if any */ 495 while (iface != iface_end) { 496 iface->idesc = NULL; 497 iface->alt_index = 0; 498 iface->parent_iface_index = USB_IFACE_INDEX_ANY; 499 iface++; 500 } 501 502 /* free "cdesc" after "ifaces", if any */ 503 if (udev->cdesc) { 504 free(udev->cdesc, M_USB); 505 udev->cdesc = NULL; 506 } 507 /* set unconfigured state */ 508 udev->curr_config_no = USB_UNCONFIG_NO; 509 udev->curr_config_index = USB_UNCONFIG_INDEX; 510} 511 512/*------------------------------------------------------------------------* 513 * usb2_set_config_index 514 * 515 * This function selects configuration by index, independent of the 516 * actual configuration number. This function should not be used by 517 * USB drivers. 518 * 519 * Returns: 520 * 0: Success 521 * Else: Failure 522 *------------------------------------------------------------------------*/ 523usb2_error_t 524usb2_set_config_index(struct usb2_device *udev, uint8_t index) 525{ 526 struct usb2_status ds; 527 struct usb2_hub_descriptor hd; 528 struct usb2_config_descriptor *cdp; 529 uint16_t power; 530 uint16_t max_power; 531 uint8_t nifc; 532 uint8_t selfpowered; 533 uint8_t do_unlock; 534 usb2_error_t err; 535 536 DPRINTFN(6, "udev=%p index=%d\n", udev, index); 537 538 /* automatic locking */ 539 if (sx_xlocked(udev->default_sx + 1)) { 540 do_unlock = 0; 541 } else { 542 do_unlock = 1; 543 sx_xlock(udev->default_sx + 1); 544 } 545 546 /* detach all interface drivers */ 547 usb2_detach_device(udev, USB_IFACE_INDEX_ANY, 1); 548 549#if USB_HAVE_UGEN 550 /* free all FIFOs except control endpoint FIFOs */ 551 usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 0); 552 553 /* free all configuration data structures */ 554 usb2_cdev_free(udev); 555#endif 556 usb2_free_iface_data(udev); 557 558 if (index == USB_UNCONFIG_INDEX) { 559 /* 560 * Leave unallocated when unconfiguring the 561 * device. "usb2_free_iface_data()" will also reset 562 * the current config number and index. 563 */ 564 err = usb2_req_set_config(udev, NULL, USB_UNCONFIG_NO); 565 goto done; 566 } 567 /* get the full config descriptor */ 568 err = usb2_req_get_config_desc_full(udev, 569 NULL, &cdp, M_USB, index); 570 if (err) { 571 goto done; 572 } 573 /* set the new config descriptor */ 574 575 udev->cdesc = cdp; 576 577 if (cdp->bNumInterface > USB_IFACE_MAX) { 578 DPRINTFN(0, "too many interfaces: %d\n", cdp->bNumInterface); 579 cdp->bNumInterface = USB_IFACE_MAX; 580 } 581 /* Figure out if the device is self or bus powered. */ 582 selfpowered = 0; 583 if ((!udev->flags.uq_bus_powered) && 584 (cdp->bmAttributes & UC_SELF_POWERED) && 585 (udev->flags.usb2_mode == USB_MODE_HOST)) { 586 /* May be self powered. */ 587 if (cdp->bmAttributes & UC_BUS_POWERED) { 588 /* Must ask device. */ 589 if (udev->flags.uq_power_claim) { 590 /* 591 * HUB claims to be self powered, but isn't. 592 * It seems that the power status can be 593 * determined by the HUB characteristics. 594 */ 595 err = usb2_req_get_hub_descriptor 596 (udev, NULL, &hd, 1); 597 if (err) { 598 DPRINTFN(0, "could not read " 599 "HUB descriptor: %s\n", 600 usb2_errstr(err)); 601 602 } else if (UGETW(hd.wHubCharacteristics) & 603 UHD_PWR_INDIVIDUAL) { 604 selfpowered = 1; 605 } 606 DPRINTF("characteristics=0x%04x\n", 607 UGETW(hd.wHubCharacteristics)); 608 } else { 609 err = usb2_req_get_device_status 610 (udev, NULL, &ds); 611 if (err) { 612 DPRINTFN(0, "could not read " 613 "device status: %s\n", 614 usb2_errstr(err)); 615 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 616 selfpowered = 1; 617 } 618 DPRINTF("status=0x%04x \n", 619 UGETW(ds.wStatus)); 620 } 621 } else 622 selfpowered = 1; 623 } 624 DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 625 "selfpowered=%d, power=%d\n", 626 udev, cdp, 627 cdp->bConfigurationValue, udev->address, cdp->bmAttributes, 628 selfpowered, cdp->bMaxPower * 2); 629 630 /* Check if we have enough power. */ 631 power = cdp->bMaxPower * 2; 632 633 if (udev->parent_hub) { 634 max_power = udev->parent_hub->hub->portpower; 635 } else { 636 max_power = USB_MAX_POWER; 637 } 638 639 if (power > max_power) { 640 DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 641 err = USB_ERR_NO_POWER; 642 goto done; 643 } 644 /* Only update "self_powered" in USB Host Mode */ 645 if (udev->flags.usb2_mode == USB_MODE_HOST) { 646 udev->flags.self_powered = selfpowered; 647 } 648 udev->power = power; 649 udev->curr_config_no = cdp->bConfigurationValue; 650 udev->curr_config_index = index; 651 652 /* Set the actual configuration value. */ 653 err = usb2_req_set_config(udev, NULL, cdp->bConfigurationValue); 654 if (err) { 655 goto done; 656 } 657 /* Allocate and fill interface data. */ 658 nifc = cdp->bNumInterface; 659 while (nifc--) { 660 err = usb2_fill_iface_data(udev, nifc, 0); 661 if (err) { 662 goto done; 663 } 664 } 665#if USB_HAVE_UGEN 666 /* create device nodes for each endpoint */ 667 usb2_cdev_create(udev); 668#endif 669 670done: 671 DPRINTF("error=%s\n", usb2_errstr(err)); 672 if (err) { 673#if USB_HAVE_UGEN 674 usb2_cdev_free(udev); 675#endif 676 usb2_free_iface_data(udev); 677 } 678 if (do_unlock) { 679 sx_unlock(udev->default_sx + 1); 680 } 681 return (err); 682} 683 684/*------------------------------------------------------------------------* 685 * usb2_set_alt_interface_index 686 * 687 * This function will select an alternate interface index for the 688 * given interface index. The interface should not be in use when this 689 * function is called. That means there should not be any open USB 690 * transfers. Else an error is returned. If the alternate setting is 691 * already set this function will simply return success. This function 692 * is called in Host mode and Device mode! 693 * 694 * Returns: 695 * 0: Success 696 * Else: Failure 697 *------------------------------------------------------------------------*/ 698usb2_error_t 699usb2_set_alt_interface_index(struct usb2_device *udev, 700 uint8_t iface_index, uint8_t alt_index) 701{ 702 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 703 usb2_error_t err; 704 uint8_t do_unlock; 705 706 /* automatic locking */ 707 if (sx_xlocked(udev->default_sx + 1)) { 708 do_unlock = 0; 709 } else { 710 do_unlock = 1; 711 sx_xlock(udev->default_sx + 1); 712 } 713 if (iface == NULL) { 714 err = USB_ERR_INVAL; 715 goto done; 716 } 717 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 718 usb2_detach_device(udev, iface_index, 1); 719 } else { 720 if (iface->alt_index == alt_index) { 721 /* 722 * Optimise away duplicate setting of 723 * alternate setting in USB Host Mode! 724 */ 725 err = 0; 726 goto done; 727 } 728 } 729#if USB_HAVE_UGEN 730 /* 731 * Free all generic FIFOs for this interface, except control 732 * endpoint FIFOs: 733 */ 734 usb2_fifo_free_wrap(udev, iface_index, 0); 735#endif 736 737 err = usb2_fill_iface_data(udev, iface_index, alt_index); 738 if (err) { 739 goto done; 740 } 741 err = usb2_req_set_alt_interface_no(udev, NULL, iface_index, 742 iface->idesc->bAlternateSetting); 743 744done: 745 if (do_unlock) { 746 sx_unlock(udev->default_sx + 1); 747 } 748 return (err); 749} 750 751/*------------------------------------------------------------------------* 752 * usb2_set_endpoint_stall 753 * 754 * This function is used to make a BULK or INTERRUPT endpoint 755 * send STALL tokens. 756 * 757 * Returns: 758 * 0: Success 759 * Else: Failure 760 *------------------------------------------------------------------------*/ 761usb2_error_t 762usb2_set_endpoint_stall(struct usb2_device *udev, struct usb2_pipe *pipe, 763 uint8_t do_stall) 764{ 765 struct usb2_xfer *xfer; 766 uint8_t et; 767 uint8_t was_stalled; 768 769 if (pipe == NULL) { 770 /* nothing to do */ 771 DPRINTF("Cannot find endpoint\n"); 772 /* 773 * Pretend that the clear or set stall request is 774 * successful else some USB host stacks can do 775 * strange things, especially when a control endpoint 776 * stalls. 777 */ 778 return (0); 779 } 780 et = (pipe->edesc->bmAttributes & UE_XFERTYPE); 781 782 if ((et != UE_BULK) && 783 (et != UE_INTERRUPT)) { 784 /* 785 * Should not stall control 786 * nor isochronous endpoints. 787 */ 788 DPRINTF("Invalid endpoint\n"); 789 return (0); 790 } 791 USB_BUS_LOCK(udev->bus); 792 793 /* store current stall state */ 794 was_stalled = pipe->is_stalled; 795 796 /* check for no change */ 797 if (was_stalled && do_stall) { 798 /* if the pipe is already stalled do nothing */ 799 USB_BUS_UNLOCK(udev->bus); 800 DPRINTF("No change\n"); 801 return (0); 802 } 803 /* set stalled state */ 804 pipe->is_stalled = 1; 805 806 if (do_stall || (!was_stalled)) { 807 if (!was_stalled) { 808 /* lookup the current USB transfer, if any */ 809 xfer = pipe->pipe_q.curr; 810 } else { 811 xfer = NULL; 812 } 813 814 /* 815 * If "xfer" is non-NULL the "set_stall" method will 816 * complete the USB transfer like in case of a timeout 817 * setting the error code "USB_ERR_STALLED". 818 */ 819 (udev->bus->methods->set_stall) (udev, xfer, pipe); 820 } 821 if (!do_stall) { 822 pipe->toggle_next = 0; /* reset data toggle */ 823 pipe->is_stalled = 0; /* clear stalled state */ 824 825 (udev->bus->methods->clear_stall) (udev, pipe); 826 827 /* start up the current or next transfer, if any */ 828 usb2_command_wrapper(&pipe->pipe_q, pipe->pipe_q.curr); 829 } 830 USB_BUS_UNLOCK(udev->bus); 831 return (0); 832} 833 834/*------------------------------------------------------------------------* 835 * usb2_reset_iface_endpoints - used in USB device side mode 836 *------------------------------------------------------------------------*/ 837usb2_error_t 838usb2_reset_iface_endpoints(struct usb2_device *udev, uint8_t iface_index) 839{ 840 struct usb2_pipe *pipe; 841 struct usb2_pipe *pipe_end; 842 usb2_error_t err; 843 844 pipe = udev->pipes; 845 pipe_end = udev->pipes + USB_EP_MAX; 846 847 for (; pipe != pipe_end; pipe++) { 848 849 if ((pipe->edesc == NULL) || 850 (pipe->iface_index != iface_index)) { 851 continue; 852 } 853 /* simulate a clear stall from the peer */ 854 err = usb2_set_endpoint_stall(udev, pipe, 0); 855 if (err) { 856 /* just ignore */ 857 } 858 } 859 return (0); 860} 861 862/*------------------------------------------------------------------------* 863 * usb2_detach_device_sub 864 * 865 * This function will try to detach an USB device. If it fails a panic 866 * will result. 867 *------------------------------------------------------------------------*/ 868static void 869usb2_detach_device_sub(struct usb2_device *udev, device_t *ppdev, 870 uint8_t free_subdev) 871{ 872 device_t dev; 873 int err; 874 875 if (!free_subdev) { 876 877 *ppdev = NULL; 878 879 } else if (*ppdev) { 880 881 /* 882 * NOTE: It is important to clear "*ppdev" before deleting 883 * the child due to some device methods being called late 884 * during the delete process ! 885 */ 886 dev = *ppdev; 887 *ppdev = NULL; 888 889 device_printf(dev, "at %s, port %d, addr %d " 890 "(disconnected)\n", 891 device_get_nameunit(udev->parent_dev), 892 udev->port_no, udev->address); 893 894 if (device_is_attached(dev)) { 895 if (udev->flags.suspended) { 896 err = DEVICE_RESUME(dev); 897 if (err) { 898 device_printf(dev, "Resume failed!\n"); 899 } 900 } 901 if (device_detach(dev)) { 902 goto error; 903 } 904 } 905 if (device_delete_child(udev->parent_dev, dev)) { 906 goto error; 907 } 908 } 909 return; 910 911error: 912 /* Detach is not allowed to fail in the USB world */ 913 panic("An USB driver would not detach!\n"); 914} 915 916/*------------------------------------------------------------------------* 917 * usb2_detach_device 918 * 919 * The following function will detach the matching interfaces. 920 * This function is NULL safe. 921 *------------------------------------------------------------------------*/ 922void 923usb2_detach_device(struct usb2_device *udev, uint8_t iface_index, 924 uint8_t free_subdev) 925{ 926 struct usb2_interface *iface; 927 uint8_t i; 928 uint8_t do_unlock; 929 930 if (udev == NULL) { 931 /* nothing to do */ 932 return; 933 } 934 DPRINTFN(4, "udev=%p\n", udev); 935 936 /* automatic locking */ 937 if (sx_xlocked(udev->default_sx + 1)) { 938 do_unlock = 0; 939 } else { 940 do_unlock = 1; 941 sx_xlock(udev->default_sx + 1); 942 } 943 944 /* 945 * First detach the child to give the child's detach routine a 946 * chance to detach the sub-devices in the correct order. 947 * Then delete the child using "device_delete_child()" which 948 * will detach all sub-devices from the bottom and upwards! 949 */ 950 if (iface_index != USB_IFACE_INDEX_ANY) { 951 i = iface_index; 952 iface_index = i + 1; 953 } else { 954 i = 0; 955 iface_index = USB_IFACE_MAX; 956 } 957 958 /* do the detach */ 959 960 for (; i != iface_index; i++) { 961 962 iface = usb2_get_iface(udev, i); 963 if (iface == NULL) { 964 /* looks like the end of the USB interfaces */ 965 break; 966 } 967 usb2_detach_device_sub(udev, &iface->subdev, free_subdev); 968 } 969 970 if (do_unlock) { 971 sx_unlock(udev->default_sx + 1); 972 } 973} 974 975/*------------------------------------------------------------------------* 976 * usb2_probe_and_attach_sub 977 * 978 * Returns: 979 * 0: Success 980 * Else: Failure 981 *------------------------------------------------------------------------*/ 982static uint8_t 983usb2_probe_and_attach_sub(struct usb2_device *udev, 984 struct usb2_attach_arg *uaa) 985{ 986 struct usb2_interface *iface; 987 device_t dev; 988 int err; 989 990 iface = uaa->iface; 991 if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 992 /* leave interface alone */ 993 return (0); 994 } 995 dev = iface->subdev; 996 if (dev) { 997 998 /* clean up after module unload */ 999 1000 if (device_is_attached(dev)) { 1001 /* already a device there */ 1002 return (0); 1003 } 1004 /* clear "iface->subdev" as early as possible */ 1005 1006 iface->subdev = NULL; 1007 1008 if (device_delete_child(udev->parent_dev, dev)) { 1009 1010 /* 1011 * Panic here, else one can get a double call 1012 * to device_detach(). USB devices should 1013 * never fail on detach! 1014 */ 1015 panic("device_delete_child() failed!\n"); 1016 } 1017 } 1018 if (uaa->temp_dev == NULL) { 1019 1020 /* create a new child */ 1021 uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 1022 if (uaa->temp_dev == NULL) { 1023 device_printf(udev->parent_dev, 1024 "Device creation failed!\n"); 1025 return (1); /* failure */ 1026 } 1027 device_set_ivars(uaa->temp_dev, uaa); 1028 device_quiet(uaa->temp_dev); 1029 } 1030 /* 1031 * Set "subdev" before probe and attach so that "devd" gets 1032 * the information it needs. 1033 */ 1034 iface->subdev = uaa->temp_dev; 1035 1036 if (device_probe_and_attach(iface->subdev) == 0) { 1037 /* 1038 * The USB attach arguments are only available during probe 1039 * and attach ! 1040 */ 1041 uaa->temp_dev = NULL; 1042 device_set_ivars(iface->subdev, NULL); 1043 1044 if (udev->flags.suspended) { 1045 err = DEVICE_SUSPEND(iface->subdev); 1046 device_printf(iface->subdev, "Suspend failed\n"); 1047 } 1048 return (0); /* success */ 1049 } else { 1050 /* No USB driver found */ 1051 iface->subdev = NULL; 1052 } 1053 return (1); /* failure */ 1054} 1055 1056/*------------------------------------------------------------------------* 1057 * usb2_set_parent_iface 1058 * 1059 * Using this function will lock the alternate interface setting on an 1060 * interface. It is typically used for multi interface drivers. In USB 1061 * device side mode it is assumed that the alternate interfaces all 1062 * have the same endpoint descriptors. The default parent index value 1063 * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 1064 * locked. 1065 *------------------------------------------------------------------------*/ 1066void 1067usb2_set_parent_iface(struct usb2_device *udev, uint8_t iface_index, 1068 uint8_t parent_index) 1069{ 1070 struct usb2_interface *iface; 1071 1072 iface = usb2_get_iface(udev, iface_index); 1073 if (iface) { 1074 iface->parent_iface_index = parent_index; 1075 } 1076} 1077 1078static void 1079usb2_init_attach_arg(struct usb2_device *udev, 1080 struct usb2_attach_arg *uaa) 1081{ 1082 bzero(uaa, sizeof(*uaa)); 1083 1084 uaa->device = udev; 1085 uaa->usb2_mode = udev->flags.usb2_mode; 1086 uaa->port = udev->port_no; 1087 1088 uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 1089 uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 1090 uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 1091 uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 1092 uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 1093 uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 1094 uaa->info.bConfigIndex = udev->curr_config_index; 1095 uaa->info.bConfigNum = udev->curr_config_no; 1096} 1097 1098/*------------------------------------------------------------------------* 1099 * usb2_probe_and_attach 1100 * 1101 * This function is called from "uhub_explore_sub()", 1102 * "usb2_handle_set_config()" and "usb2_handle_request()". 1103 * 1104 * Returns: 1105 * 0: Success 1106 * Else: A control transfer failed 1107 *------------------------------------------------------------------------*/ 1108usb2_error_t 1109usb2_probe_and_attach(struct usb2_device *udev, uint8_t iface_index) 1110{ 1111 struct usb2_attach_arg uaa; 1112 struct usb2_interface *iface; 1113 uint8_t i; 1114 uint8_t j; 1115 uint8_t do_unlock; 1116 1117 if (udev == NULL) { 1118 DPRINTF("udev == NULL\n"); 1119 return (USB_ERR_INVAL); 1120 } 1121 /* automatic locking */ 1122 if (sx_xlocked(udev->default_sx + 1)) { 1123 do_unlock = 0; 1124 } else { 1125 do_unlock = 1; 1126 sx_xlock(udev->default_sx + 1); 1127 } 1128 1129 if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 1130 /* do nothing - no configuration has been set */ 1131 goto done; 1132 } 1133 /* setup USB attach arguments */ 1134 1135 usb2_init_attach_arg(udev, &uaa); 1136 1137 /* Check if only one interface should be probed: */ 1138 if (iface_index != USB_IFACE_INDEX_ANY) { 1139 i = iface_index; 1140 j = i + 1; 1141 } else { 1142 i = 0; 1143 j = USB_IFACE_MAX; 1144 } 1145 1146 /* Do the probe and attach */ 1147 for (; i != j; i++) { 1148 1149 iface = usb2_get_iface(udev, i); 1150 if (iface == NULL) { 1151 /* 1152 * Looks like the end of the USB 1153 * interfaces ! 1154 */ 1155 DPRINTFN(2, "end of interfaces " 1156 "at %u\n", i); 1157 break; 1158 } 1159 if (iface->idesc == NULL) { 1160 /* no interface descriptor */ 1161 continue; 1162 } 1163 uaa.iface = iface; 1164 1165 uaa.info.bInterfaceClass = 1166 iface->idesc->bInterfaceClass; 1167 uaa.info.bInterfaceSubClass = 1168 iface->idesc->bInterfaceSubClass; 1169 uaa.info.bInterfaceProtocol = 1170 iface->idesc->bInterfaceProtocol; 1171 uaa.info.bIfaceIndex = i; 1172 uaa.info.bIfaceNum = 1173 iface->idesc->bInterfaceNumber; 1174 uaa.use_generic = 0; 1175 1176 DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 1177 uaa.info.bInterfaceClass, 1178 uaa.info.bInterfaceSubClass, 1179 uaa.info.bInterfaceProtocol, 1180 uaa.info.bIfaceIndex, 1181 uaa.info.bIfaceNum); 1182 1183 /* try specific interface drivers first */ 1184 1185 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1186 /* ignore */ 1187 } 1188 /* try generic interface drivers last */ 1189 1190 uaa.use_generic = 1; 1191 1192 if (usb2_probe_and_attach_sub(udev, &uaa)) { 1193 /* ignore */ 1194 } 1195 } 1196 1197 if (uaa.temp_dev) { 1198 /* remove the last created child; it is unused */ 1199 1200 if (device_delete_child(udev->parent_dev, uaa.temp_dev)) { 1201 DPRINTFN(0, "device delete child failed!\n"); 1202 } 1203 } 1204done: 1205 if (do_unlock) { 1206 sx_unlock(udev->default_sx + 1); 1207 } 1208 return (0); 1209} 1210 1211/*------------------------------------------------------------------------* 1212 * usb2_suspend_resume_sub 1213 * 1214 * This function is called when the suspend or resume methods should 1215 * be executed on an USB device. 1216 *------------------------------------------------------------------------*/ 1217static void 1218usb2_suspend_resume_sub(struct usb2_device *udev, device_t dev, uint8_t do_suspend) 1219{ 1220 int err; 1221 1222 if (dev == NULL) { 1223 return; 1224 } 1225 if (!device_is_attached(dev)) { 1226 return; 1227 } 1228 if (do_suspend) { 1229 err = DEVICE_SUSPEND(dev); 1230 } else { 1231 err = DEVICE_RESUME(dev); 1232 } 1233 if (err) { 1234 device_printf(dev, "%s failed!\n", 1235 do_suspend ? "Suspend" : "Resume"); 1236 } 1237} 1238 1239/*------------------------------------------------------------------------* 1240 * usb2_suspend_resume 1241 * 1242 * The following function will suspend or resume the USB device. 1243 * 1244 * Returns: 1245 * 0: Success 1246 * Else: Failure 1247 *------------------------------------------------------------------------*/ 1248usb2_error_t 1249usb2_suspend_resume(struct usb2_device *udev, uint8_t do_suspend) 1250{ 1251 struct usb2_interface *iface; 1252 uint8_t i; 1253 1254 if (udev == NULL) { 1255 /* nothing to do */ 1256 return (0); 1257 } 1258 DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 1259 1260 sx_assert(udev->default_sx + 1, SA_LOCKED); 1261 1262 USB_BUS_LOCK(udev->bus); 1263 /* filter the suspend events */ 1264 if (udev->flags.suspended == do_suspend) { 1265 USB_BUS_UNLOCK(udev->bus); 1266 /* nothing to do */ 1267 return (0); 1268 } 1269 udev->flags.suspended = do_suspend; 1270 USB_BUS_UNLOCK(udev->bus); 1271 1272 /* do the suspend or resume */ 1273 1274 for (i = 0; i != USB_IFACE_MAX; i++) { 1275 1276 iface = usb2_get_iface(udev, i); 1277 if (iface == NULL) { 1278 /* looks like the end of the USB interfaces */ 1279 break; 1280 } 1281 usb2_suspend_resume_sub(udev, iface->subdev, do_suspend); 1282 } 1283 return (0); 1284} 1285 1286/*------------------------------------------------------------------------* 1287 * usb2_clear_stall_proc 1288 * 1289 * This function performs generic USB clear stall operations. 1290 *------------------------------------------------------------------------*/ 1291static void 1292usb2_clear_stall_proc(struct usb2_proc_msg *_pm) 1293{ 1294 struct usb2_clear_stall_msg *pm = (void *)_pm; 1295 struct usb2_device *udev = pm->udev; 1296 1297 /* Change lock */ 1298 USB_BUS_UNLOCK(udev->bus); 1299 mtx_lock(udev->default_mtx); 1300 1301 /* Start clear stall callback */ 1302 usb2_transfer_start(udev->default_xfer[1]); 1303 1304 /* Change lock */ 1305 mtx_unlock(udev->default_mtx); 1306 USB_BUS_LOCK(udev->bus); 1307} 1308 1309/*------------------------------------------------------------------------* 1310 * usb2_alloc_device 1311 * 1312 * This function allocates a new USB device. This function is called 1313 * when a new device has been put in the powered state, but not yet in 1314 * the addressed state. Get initial descriptor, set the address, get 1315 * full descriptor and get strings. 1316 * 1317 * Return values: 1318 * 0: Failure 1319 * Else: Success 1320 *------------------------------------------------------------------------*/ 1321struct usb2_device * 1322usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus, 1323 struct usb2_device *parent_hub, uint8_t depth, 1324 uint8_t port_index, uint8_t port_no, uint8_t speed, uint8_t usb2_mode) 1325{ 1326 struct usb2_attach_arg uaa; 1327 struct usb2_device *udev; 1328 struct usb2_device *adev; 1329 struct usb2_device *hub; 1330 uint8_t *scratch_ptr; 1331 uint32_t scratch_size; 1332 usb2_error_t err; 1333 uint8_t device_index; 1334 1335 DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1336 "port_index=%u, port_no=%u, speed=%u, usb2_mode=%u\n", 1337 parent_dev, bus, parent_hub, depth, port_index, port_no, 1338 speed, usb2_mode); 1339 1340 /* 1341 * Find an unused device index. In USB Host mode this is the 1342 * same as the device address. 1343 * 1344 * Device index zero is not used and device index 1 should 1345 * always be the root hub. 1346 */ 1347 for (device_index = USB_ROOT_HUB_ADDR; 1348 (device_index != bus->devices_max) && 1349 (bus->devices[device_index] != NULL); 1350 device_index++) /* nop */; 1351 1352 if (device_index == bus->devices_max) { 1353 device_printf(bus->bdev, 1354 "No free USB device index for new device!\n"); 1355 return (NULL); 1356 } 1357 1358 if (depth > 0x10) { 1359 device_printf(bus->bdev, 1360 "Invalid device depth!\n"); 1361 return (NULL); 1362 } 1363 udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1364 if (udev == NULL) { 1365 return (NULL); 1366 } 1367 /* initialise our SX-lock */ 1368 sx_init(udev->default_sx, "0123456789ABCDEF - USB device SX lock" + depth); 1369 1370 /* initialise our SX-lock */ 1371 sx_init(udev->default_sx + 1, "0123456789ABCDEF - USB config SX lock" + depth); 1372 1373 usb2_cv_init(udev->default_cv, "WCTRL"); 1374 usb2_cv_init(udev->default_cv + 1, "UGONE"); 1375 1376 LIST_INIT(&udev->pd_list); 1377 1378 /* initialise our mutex */ 1379 mtx_init(udev->default_mtx, "USB device mutex", NULL, MTX_DEF); 1380 1381 /* initialise generic clear stall */ 1382 udev->cs_msg[0].hdr.pm_callback = &usb2_clear_stall_proc; 1383 udev->cs_msg[0].udev = udev; 1384 udev->cs_msg[1].hdr.pm_callback = &usb2_clear_stall_proc; 1385 udev->cs_msg[1].udev = udev; 1386 1387 /* initialise some USB device fields */ 1388 udev->parent_hub = parent_hub; 1389 udev->parent_dev = parent_dev; 1390 udev->port_index = port_index; 1391 udev->port_no = port_no; 1392 udev->depth = depth; 1393 udev->bus = bus; 1394 udev->address = USB_START_ADDR; /* default value */ 1395 udev->plugtime = (uint32_t)ticks; 1396 /* 1397 * We need to force the power mode to "on" because there are plenty 1398 * of USB devices out there that do not work very well with 1399 * automatic suspend and resume! 1400 */ 1401 udev->power_mode = USB_POWER_MODE_ON; 1402 udev->pwr_save.last_xfer_time = ticks; 1403 1404 /* we are not ready yet */ 1405 udev->refcount = 1; 1406 1407 /* set up default endpoint descriptor */ 1408 udev->default_ep_desc.bLength = sizeof(udev->default_ep_desc); 1409 udev->default_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1410 udev->default_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1411 udev->default_ep_desc.bmAttributes = UE_CONTROL; 1412 udev->default_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 1413 udev->default_ep_desc.wMaxPacketSize[1] = 0; 1414 udev->default_ep_desc.bInterval = 0; 1415 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1416 1417 udev->speed = speed; 1418 udev->flags.usb2_mode = usb2_mode; 1419 1420 /* speed combination should be checked by the parent HUB */ 1421 1422 hub = udev->parent_hub; 1423 1424 /* search for our High Speed USB HUB, if any */ 1425 1426 adev = udev; 1427 hub = udev->parent_hub; 1428 1429 while (hub) { 1430 if (hub->speed == USB_SPEED_HIGH) { 1431 udev->hs_hub_addr = hub->address; 1432 udev->hs_port_no = adev->port_no; 1433 break; 1434 } 1435 adev = hub; 1436 hub = hub->parent_hub; 1437 } 1438 1439 /* init the default pipe */ 1440 usb2_fill_pipe_data(udev, 0, 1441 &udev->default_ep_desc, 1442 &udev->default_pipe); 1443 1444 /* set device index */ 1445 udev->device_index = device_index; 1446 1447 /* Create ugen name */ 1448 snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1449 USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1450 device_index); 1451#if USB_HAVE_UGEN 1452 /* Create the control endpoint device */ 1453 udev->default_dev = usb2_make_dev(udev, 0, FREAD|FWRITE); 1454 1455 /* Create a link from /dev/ugenX.X to the default endpoint */ 1456 make_dev_alias(udev->default_dev, udev->ugen_name); 1457#endif 1458 1459 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1460 1461 err = usb2_req_set_address(udev, NULL, device_index); 1462 1463 /* This is the new USB device address from now on */ 1464 1465 udev->address = device_index; 1466 1467 /* 1468 * We ignore any set-address errors, hence there are 1469 * buggy USB devices out there that actually receive 1470 * the SETUP PID, but manage to set the address before 1471 * the STATUS stage is ACK'ed. If the device responds 1472 * to the subsequent get-descriptor at the new 1473 * address, then we know that the set-address command 1474 * was successful. 1475 */ 1476 if (err) { 1477 DPRINTFN(0, "set address %d failed " 1478 "(ignored)\n", udev->address); 1479 } 1480 /* allow device time to set new address */ 1481 usb2_pause_mtx(NULL, 1482 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1483 } else { 1484 /* We are not self powered */ 1485 udev->flags.self_powered = 0; 1486 1487 /* Set unconfigured state */ 1488 udev->curr_config_no = USB_UNCONFIG_NO; 1489 udev->curr_config_index = USB_UNCONFIG_INDEX; 1490 1491 /* Setup USB descriptors */ 1492 err = (usb2_temp_setup_by_index_p) (udev, usb2_template); 1493 if (err) { 1494 DPRINTFN(0, "setting up USB template failed maybe the USB " 1495 "template module has not been loaded\n"); 1496 goto done; 1497 } 1498 } 1499 1500 /* 1501 * Get the first 8 bytes of the device descriptor ! 1502 * 1503 * NOTE: "usb2_do_request" will check the device descriptor 1504 * next time we do a request to see if the maximum packet size 1505 * changed! The 8 first bytes of the device descriptor 1506 * contains the maximum packet size to use on control endpoint 1507 * 0. If this value is different from "USB_MAX_IPACKET" a new 1508 * USB control request will be setup! 1509 */ 1510 err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc, 1511 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1512 if (err) { 1513 DPRINTFN(0, "getting device descriptor " 1514 "at addr %d failed!\n", udev->address); 1515 /* XXX try to re-enumerate the device */ 1516 err = usb2_req_re_enumerate(udev, NULL); 1517 if (err) { 1518 goto done; 1519 } 1520 } 1521 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1522 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1523 udev->address, UGETW(udev->ddesc.bcdUSB), 1524 udev->ddesc.bDeviceClass, 1525 udev->ddesc.bDeviceSubClass, 1526 udev->ddesc.bDeviceProtocol, 1527 udev->ddesc.bMaxPacketSize, 1528 udev->ddesc.bLength, 1529 udev->speed); 1530 1531 /* get the full device descriptor */ 1532 err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc); 1533 if (err) { 1534 DPRINTF("addr=%d, getting full desc failed\n", 1535 udev->address); 1536 goto done; 1537 } 1538 /* 1539 * Setup temporary USB attach args so that we can figure out some 1540 * basic quirks for this device. 1541 */ 1542 usb2_init_attach_arg(udev, &uaa); 1543 1544 if (usb2_test_quirk(&uaa, UQ_BUS_POWERED)) { 1545 udev->flags.uq_bus_powered = 1; 1546 } 1547 if (usb2_test_quirk(&uaa, UQ_POWER_CLAIM)) { 1548 udev->flags.uq_power_claim = 1; 1549 } 1550 if (usb2_test_quirk(&uaa, UQ_NO_STRINGS)) { 1551 udev->flags.no_strings = 1; 1552 } 1553 /* 1554 * Workaround for buggy USB devices. 1555 * 1556 * It appears that some string-less USB chips will crash and 1557 * disappear if any attempts are made to read any string 1558 * descriptors. 1559 * 1560 * Try to detect such chips by checking the strings in the USB 1561 * device descriptor. If no strings are present there we 1562 * simply disable all USB strings. 1563 */ 1564 scratch_ptr = udev->bus->scratch[0].data; 1565 scratch_size = sizeof(udev->bus->scratch[0].data); 1566 1567 if (udev->ddesc.iManufacturer || 1568 udev->ddesc.iProduct || 1569 udev->ddesc.iSerialNumber) { 1570 /* read out the language ID string */ 1571 err = usb2_req_get_string_desc(udev, NULL, 1572 (char *)scratch_ptr, 4, scratch_size, 1573 USB_LANGUAGE_TABLE); 1574 } else { 1575 err = USB_ERR_INVAL; 1576 } 1577 1578 if (err || (scratch_ptr[0] < 4)) { 1579 udev->flags.no_strings = 1; 1580 } else { 1581 /* pick the first language as the default */ 1582 udev->langid = UGETW(scratch_ptr + 2); 1583 } 1584 1585 /* assume 100mA bus powered for now. Changed when configured. */ 1586 udev->power = USB_MIN_POWER; 1587 1588 /* get serial number string */ 1589 err = usb2_req_get_string_any 1590 (udev, NULL, (char *)scratch_ptr, 1591 scratch_size, udev->ddesc.iSerialNumber); 1592 1593 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1594 1595 /* get manufacturer string */ 1596 err = usb2_req_get_string_any 1597 (udev, NULL, (char *)scratch_ptr, 1598 scratch_size, udev->ddesc.iManufacturer); 1599 1600 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1601 1602 /* get product string */ 1603 err = usb2_req_get_string_any 1604 (udev, NULL, (char *)scratch_ptr, 1605 scratch_size, udev->ddesc.iProduct); 1606 1607 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1608 1609 /* finish up all the strings */ 1610 usb2_check_strings(udev); 1611 1612 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1613 uint8_t config_index; 1614 uint8_t config_quirk; 1615 uint8_t set_config_failed = 0; 1616 1617 /* 1618 * Most USB devices should attach to config index 0 by 1619 * default 1620 */ 1621 if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 1622 config_index = 0; 1623 config_quirk = 1; 1624 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 1625 config_index = 1; 1626 config_quirk = 1; 1627 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 1628 config_index = 2; 1629 config_quirk = 1; 1630 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 1631 config_index = 3; 1632 config_quirk = 1; 1633 } else if (usb2_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 1634 config_index = 4; 1635 config_quirk = 1; 1636 } else { 1637 config_index = 0; 1638 config_quirk = 0; 1639 } 1640 1641repeat_set_config: 1642 1643 DPRINTF("setting config %u\n", config_index); 1644 1645 /* get the USB device configured */ 1646 sx_xlock(udev->default_sx + 1); 1647 err = usb2_set_config_index(udev, config_index); 1648 sx_unlock(udev->default_sx + 1); 1649 if (err) { 1650 if (udev->ddesc.bNumConfigurations != 0) { 1651 if (!set_config_failed) { 1652 set_config_failed = 1; 1653 /* XXX try to re-enumerate the device */ 1654 err = usb2_req_re_enumerate( 1655 udev, NULL); 1656 if (err == 0) 1657 goto repeat_set_config; 1658 } 1659 DPRINTFN(0, "Failure selecting " 1660 "configuration index %u: %s, port %u, " 1661 "addr %u (ignored)\n", 1662 config_index, usb2_errstr(err), udev->port_no, 1663 udev->address); 1664 } 1665 /* 1666 * Some USB devices do not have any 1667 * configurations. Ignore any set config 1668 * failures! 1669 */ 1670 err = 0; 1671 } else if (config_quirk) { 1672 /* user quirk selects configuration index */ 1673 } else if ((config_index + 1) < udev->ddesc.bNumConfigurations) { 1674 1675 if ((udev->cdesc->bNumInterface < 2) && 1676 (usb2_get_no_endpoints(udev->cdesc) == 0)) { 1677 DPRINTFN(0, "Found no endpoints " 1678 "(trying next config)!\n"); 1679 config_index++; 1680 goto repeat_set_config; 1681 } 1682 if (config_index == 0) { 1683 /* 1684 * Try to figure out if we have an 1685 * auto-install disk there: 1686 */ 1687 if (usb2_test_autoinstall(udev, 0, 0) == 0) { 1688 DPRINTFN(0, "Found possible auto-install " 1689 "disk (trying next config)\n"); 1690 config_index++; 1691 goto repeat_set_config; 1692 } 1693 } 1694 } else if (usb2_test_huawei_autoinst_p(udev, &uaa) == 0) { 1695 DPRINTFN(0, "Found Huawei auto-install disk!\n"); 1696 err = USB_ERR_STALLED; /* fake an error */ 1697 } 1698 } else { 1699 err = 0; /* set success */ 1700 } 1701 1702 DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 1703 udev->address, udev, udev->parent_hub); 1704 1705 /* register our device - we are ready */ 1706 usb2_bus_port_set_device(bus, parent_hub ? 1707 parent_hub->hub->ports + port_index : NULL, udev, device_index); 1708 1709 /* Link and announce the ugen device name */ 1710#if USB_HAVE_UGEN 1711 udev->ugen_symlink = usb2_alloc_symlink(udev->ugen_name); 1712#endif 1713 printf("%s: <%s> at %s\n", udev->ugen_name, udev->manufacturer, 1714 device_get_nameunit(udev->bus->bdev)); 1715 1716 usb2_notify_addq("+", udev); 1717done: 1718 if (err) { 1719 /* free device */ 1720 usb2_free_device(udev); 1721 udev = NULL; 1722 } 1723 return (udev); 1724} 1725 1726#if USB_HAVE_UGEN 1727static struct cdev * 1728usb2_make_dev(struct usb2_device *udev, int ep, int mode) 1729{ 1730 struct usb2_fs_privdata* pd; 1731 char devname[20]; 1732 1733 /* Store information to locate ourselves again later */ 1734 pd = malloc(sizeof(struct usb2_fs_privdata), M_USBDEV, 1735 M_WAITOK | M_ZERO); 1736 pd->bus_index = device_get_unit(udev->bus->bdev); 1737 pd->dev_index = udev->device_index; 1738 pd->ep_addr = ep; 1739 pd->mode = mode; 1740 1741 /* Now, create the device itself */ 1742 snprintf(devname, sizeof(devname), "%u.%u.%u", 1743 pd->bus_index, pd->dev_index, pd->ep_addr); 1744 pd->cdev = make_dev(&usb2_devsw, 0, UID_ROOT, 1745 GID_OPERATOR, 0600, USB_DEVICE_DIR "/%s", devname); 1746 pd->cdev->si_drv1 = pd; 1747 1748 return (pd->cdev); 1749} 1750 1751static void 1752usb2_cdev_create(struct usb2_device *udev) 1753{ 1754 struct usb2_config_descriptor *cd; 1755 struct usb2_endpoint_descriptor *ed; 1756 struct usb2_descriptor *desc; 1757 struct usb2_fs_privdata* pd; 1758 struct cdev *dev; 1759 int inmode, outmode, inmask, outmask, mode; 1760 uint8_t ep; 1761 1762 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 1763 1764 DPRINTFN(2, "Creating device nodes\n"); 1765 1766 if (usb2_get_mode(udev) == USB_MODE_DEVICE) { 1767 inmode = FWRITE; 1768 outmode = FREAD; 1769 } else { /* USB_MODE_HOST */ 1770 inmode = FREAD; 1771 outmode = FWRITE; 1772 } 1773 1774 inmask = 0; 1775 outmask = 0; 1776 desc = NULL; 1777 1778 /* 1779 * Collect all used endpoint numbers instead of just 1780 * generating 16 static endpoints. 1781 */ 1782 cd = usb2_get_config_descriptor(udev); 1783 while ((desc = usb2_desc_foreach(cd, desc))) { 1784 /* filter out all endpoint descriptors */ 1785 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 1786 (desc->bLength >= sizeof(*ed))) { 1787 ed = (struct usb2_endpoint_descriptor *)desc; 1788 1789 /* update masks */ 1790 ep = ed->bEndpointAddress; 1791 if (UE_GET_DIR(ep) == UE_DIR_OUT) 1792 outmask |= 1 << UE_GET_ADDR(ep); 1793 else 1794 inmask |= 1 << UE_GET_ADDR(ep); 1795 } 1796 } 1797 1798 /* Create all available endpoints except EP0 */ 1799 for (ep = 1; ep < 16; ep++) { 1800 mode = inmask & (1 << ep) ? inmode : 0; 1801 mode |= outmask & (1 << ep) ? outmode : 0; 1802 if (mode == 0) 1803 continue; /* no IN or OUT endpoint */ 1804 1805 dev = usb2_make_dev(udev, ep, mode); 1806 pd = dev->si_drv1; 1807 LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 1808 } 1809} 1810 1811static void 1812usb2_cdev_free(struct usb2_device *udev) 1813{ 1814 struct usb2_fs_privdata* pd; 1815 1816 DPRINTFN(2, "Freeing device nodes\n"); 1817 1818 while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 1819 KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 1820 KASSERT(pd->ep_addr > 0, ("freeing EP0")); 1821 1822 destroy_dev_sched_cb(pd->cdev, usb2_cdev_cleanup, pd); 1823 pd->cdev = NULL; 1824 LIST_REMOVE(pd, pd_next); 1825 } 1826} 1827 1828static void 1829usb2_cdev_cleanup(void* arg) 1830{ 1831 free(arg, M_USBDEV); 1832} 1833#endif 1834 1835/*------------------------------------------------------------------------* 1836 * usb2_free_device 1837 * 1838 * This function is NULL safe and will free an USB device. 1839 *------------------------------------------------------------------------*/ 1840void 1841usb2_free_device(struct usb2_device *udev) 1842{ 1843 struct usb2_bus *bus = udev->bus;; 1844 1845 DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 1846 1847 usb2_notify_addq("-", udev); 1848 1849 printf("%s: <%s> at %s (disconnected)\n", udev->ugen_name, 1850 udev->manufacturer, device_get_nameunit(bus->bdev)); 1851 1852 /* Destroy UGEN symlink, if any */ 1853 if (udev->ugen_symlink) { 1854#if USB_HAVE_UGEN 1855 usb2_free_symlink(udev->ugen_symlink); 1856#endif 1857 udev->ugen_symlink = NULL; 1858 } 1859 /* 1860 * Unregister our device first which will prevent any further 1861 * references: 1862 */ 1863 usb2_bus_port_set_device(bus, udev->parent_hub ? 1864 udev->parent_hub->hub->ports + udev->port_index : NULL, 1865 NULL, USB_ROOT_HUB_ADDR); 1866 1867#if USB_HAVE_UGEN 1868 /* wait for all pending references to go away: */ 1869 1870 mtx_lock(&usb2_ref_lock); 1871 udev->refcount--; 1872 while (udev->refcount != 0) { 1873 usb2_cv_wait(udev->default_cv + 1, &usb2_ref_lock); 1874 } 1875 mtx_unlock(&usb2_ref_lock); 1876#endif 1877 1878 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 1879 /* stop receiving any control transfers (Device Side Mode) */ 1880 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1881 } 1882#if USB_HAVE_UGEN 1883 /* free all FIFOs */ 1884 usb2_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, 1); 1885 1886 /* 1887 * Free all interface related data and FIFOs, if any. 1888 */ 1889 usb2_cdev_free(udev); 1890#endif 1891 usb2_free_iface_data(udev); 1892#if USB_HAVE_UGEN 1893 destroy_dev_sched_cb(udev->default_dev, usb2_cdev_cleanup, 1894 udev->default_dev->si_drv1); 1895#endif 1896 1897 /* unsetup any leftover default USB transfers */ 1898 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 1899 1900 /* template unsetup, if any */ 1901 (usb2_temp_unsetup_p) (udev); 1902 1903 /* 1904 * Make sure that our clear-stall messages are not queued 1905 * anywhere: 1906 */ 1907 USB_BUS_LOCK(udev->bus); 1908 usb2_proc_mwait(&udev->bus->non_giant_callback_proc, 1909 &udev->cs_msg[0], &udev->cs_msg[1]); 1910 USB_BUS_UNLOCK(udev->bus); 1911 1912 sx_destroy(udev->default_sx); 1913 sx_destroy(udev->default_sx + 1); 1914 1915 usb2_cv_destroy(udev->default_cv); 1916 usb2_cv_destroy(udev->default_cv + 1); 1917 1918 mtx_destroy(udev->default_mtx); 1919 KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 1920 1921 /* free device */ 1922 free(udev, M_USB); 1923} 1924 1925/*------------------------------------------------------------------------* 1926 * usb2_get_iface 1927 * 1928 * This function is the safe way to get the USB interface structure 1929 * pointer by interface index. 1930 * 1931 * Return values: 1932 * NULL: Interface not present. 1933 * Else: Pointer to USB interface structure. 1934 *------------------------------------------------------------------------*/ 1935struct usb2_interface * 1936usb2_get_iface(struct usb2_device *udev, uint8_t iface_index) 1937{ 1938 struct usb2_interface *iface = udev->ifaces + iface_index; 1939 1940 if ((iface < udev->ifaces) || 1941 (iface_index >= USB_IFACE_MAX) || 1942 (udev->cdesc == NULL) || 1943 (iface_index >= udev->cdesc->bNumInterface)) { 1944 return (NULL); 1945 } 1946 return (iface); 1947} 1948 1949/*------------------------------------------------------------------------* 1950 * usb2_find_descriptor 1951 * 1952 * This function will lookup the first descriptor that matches the 1953 * criteria given by the arguments "type" and "subtype". Descriptors 1954 * will only be searched within the interface having the index 1955 * "iface_index". If the "id" argument points to an USB descriptor, 1956 * it will be skipped before the search is started. This allows 1957 * searching for multiple descriptors using the same criteria. Else 1958 * the search is started after the interface descriptor. 1959 * 1960 * Return values: 1961 * NULL: End of descriptors 1962 * Else: A descriptor matching the criteria 1963 *------------------------------------------------------------------------*/ 1964void * 1965usb2_find_descriptor(struct usb2_device *udev, void *id, uint8_t iface_index, 1966 uint8_t type, uint8_t type_mask, 1967 uint8_t subtype, uint8_t subtype_mask) 1968{ 1969 struct usb2_descriptor *desc; 1970 struct usb2_config_descriptor *cd; 1971 struct usb2_interface *iface; 1972 1973 cd = usb2_get_config_descriptor(udev); 1974 if (cd == NULL) { 1975 return (NULL); 1976 } 1977 if (id == NULL) { 1978 iface = usb2_get_iface(udev, iface_index); 1979 if (iface == NULL) { 1980 return (NULL); 1981 } 1982 id = usb2_get_interface_descriptor(iface); 1983 if (id == NULL) { 1984 return (NULL); 1985 } 1986 } 1987 desc = (void *)id; 1988 1989 while ((desc = usb2_desc_foreach(cd, desc))) { 1990 1991 if (desc->bDescriptorType == UDESC_INTERFACE) { 1992 break; 1993 } 1994 if (((desc->bDescriptorType & type_mask) == type) && 1995 ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 1996 return (desc); 1997 } 1998 } 1999 return (NULL); 2000} 2001 2002/*------------------------------------------------------------------------* 2003 * usb2_devinfo 2004 * 2005 * This function will dump information from the device descriptor 2006 * belonging to the USB device pointed to by "udev", to the string 2007 * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 2008 * including the terminating zero. 2009 *------------------------------------------------------------------------*/ 2010void 2011usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len) 2012{ 2013 struct usb2_device_descriptor *udd = &udev->ddesc; 2014 uint16_t bcdDevice; 2015 uint16_t bcdUSB; 2016 2017 bcdUSB = UGETW(udd->bcdUSB); 2018 bcdDevice = UGETW(udd->bcdDevice); 2019 2020 if (udd->bDeviceClass != 0xFF) { 2021 snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 2022 "%x.%02x, addr %d", udev->manufacturer, udev->product, 2023 udd->bDeviceClass, udd->bDeviceSubClass, 2024 (bcdUSB >> 8), bcdUSB & 0xFF, 2025 (bcdDevice >> 8), bcdDevice & 0xFF, 2026 udev->address); 2027 } else { 2028 snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 2029 "%x.%02x, addr %d", udev->manufacturer, udev->product, 2030 (bcdUSB >> 8), bcdUSB & 0xFF, 2031 (bcdDevice >> 8), bcdDevice & 0xFF, 2032 udev->address); 2033 } 2034} 2035 2036#if USB_VERBOSE 2037/* 2038 * Descriptions of of known vendors and devices ("products"). 2039 */ 2040struct usb_knowndev { 2041 uint16_t vendor; 2042 uint16_t product; 2043 uint32_t flags; 2044 const char *vendorname; 2045 const char *productname; 2046}; 2047 2048#define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 2049 2050#include "usbdevs.h" 2051#include "usbdevs_data.h" 2052#endif /* USB_VERBOSE */ 2053 2054/*------------------------------------------------------------------------* 2055 * usb2_check_strings 2056 * 2057 * This function checks the manufacturer and product strings and will 2058 * fill in defaults for missing strings. 2059 *------------------------------------------------------------------------*/ 2060static void 2061usb2_check_strings(struct usb2_device *udev) 2062{ 2063 struct usb2_device_descriptor *udd = &udev->ddesc; 2064 const char *vendor; 2065 const char *product; 2066 2067#if USB_VERBOSE 2068 const struct usb_knowndev *kdp; 2069 2070#endif 2071 uint16_t vendor_id; 2072 uint16_t product_id; 2073 2074 usb2_trim_spaces(udev->manufacturer); 2075 usb2_trim_spaces(udev->product); 2076 2077 if (udev->manufacturer[0]) { 2078 vendor = udev->manufacturer; 2079 } else { 2080 vendor = NULL; 2081 } 2082 2083 if (udev->product[0]) { 2084 product = udev->product; 2085 } else { 2086 product = NULL; 2087 } 2088 2089 vendor_id = UGETW(udd->idVendor); 2090 product_id = UGETW(udd->idProduct); 2091 2092#if USB_VERBOSE 2093 if (vendor == NULL || product == NULL) { 2094 2095 for (kdp = usb_knowndevs; 2096 kdp->vendorname != NULL; 2097 kdp++) { 2098 if (kdp->vendor == vendor_id && 2099 (kdp->product == product_id || 2100 (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 2101 break; 2102 } 2103 if (kdp->vendorname != NULL) { 2104 if (vendor == NULL) 2105 vendor = kdp->vendorname; 2106 if (product == NULL) 2107 product = (kdp->flags & USB_KNOWNDEV_NOPROD) == 0 ? 2108 kdp->productname : NULL; 2109 } 2110 } 2111#endif 2112 if (vendor && *vendor) { 2113 if (udev->manufacturer != vendor) { 2114 strlcpy(udev->manufacturer, vendor, 2115 sizeof(udev->manufacturer)); 2116 } 2117 } else { 2118 snprintf(udev->manufacturer, 2119 sizeof(udev->manufacturer), "vendor 0x%04x", vendor_id); 2120 } 2121 2122 if (product && *product) { 2123 if (udev->product != product) { 2124 strlcpy(udev->product, product, 2125 sizeof(udev->product)); 2126 } 2127 } else { 2128 snprintf(udev->product, 2129 sizeof(udev->product), "product 0x%04x", product_id); 2130 } 2131} 2132 2133/* 2134 * Returns: 2135 * See: USB_MODE_XXX 2136 */ 2137uint8_t 2138usb2_get_mode(struct usb2_device *udev) 2139{ 2140 return (udev->flags.usb2_mode); 2141} 2142 2143/* 2144 * Returns: 2145 * See: USB_SPEED_XXX 2146 */ 2147uint8_t 2148usb2_get_speed(struct usb2_device *udev) 2149{ 2150 return (udev->speed); 2151} 2152 2153uint32_t 2154usb2_get_isoc_fps(struct usb2_device *udev) 2155{ 2156 ; /* indent fix */ 2157 switch (udev->speed) { 2158 case USB_SPEED_LOW: 2159 case USB_SPEED_FULL: 2160 return (1000); 2161 default: 2162 return (8000); 2163 } 2164} 2165 2166struct usb2_device_descriptor * 2167usb2_get_device_descriptor(struct usb2_device *udev) 2168{ 2169 if (udev == NULL) 2170 return (NULL); /* be NULL safe */ 2171 return (&udev->ddesc); 2172} 2173 2174struct usb2_config_descriptor * 2175usb2_get_config_descriptor(struct usb2_device *udev) 2176{ 2177 if (udev == NULL) 2178 return (NULL); /* be NULL safe */ 2179 return (udev->cdesc); 2180} 2181 2182/*------------------------------------------------------------------------* 2183 * usb2_test_quirk - test a device for a given quirk 2184 * 2185 * Return values: 2186 * 0: The USB device does not have the given quirk. 2187 * Else: The USB device has the given quirk. 2188 *------------------------------------------------------------------------*/ 2189uint8_t 2190usb2_test_quirk(const struct usb2_attach_arg *uaa, uint16_t quirk) 2191{ 2192 uint8_t found; 2193 2194 found = (usb2_test_quirk_p) (&uaa->info, quirk); 2195 return (found); 2196} 2197 2198struct usb2_interface_descriptor * 2199usb2_get_interface_descriptor(struct usb2_interface *iface) 2200{ 2201 if (iface == NULL) 2202 return (NULL); /* be NULL safe */ 2203 return (iface->idesc); 2204} 2205 2206uint8_t 2207usb2_get_interface_altindex(struct usb2_interface *iface) 2208{ 2209 return (iface->alt_index); 2210} 2211 2212uint8_t 2213usb2_get_bus_index(struct usb2_device *udev) 2214{ 2215 return ((uint8_t)device_get_unit(udev->bus->bdev)); 2216} 2217 2218uint8_t 2219usb2_get_device_index(struct usb2_device *udev) 2220{ 2221 return (udev->device_index); 2222} 2223 2224/*------------------------------------------------------------------------* 2225 * usb2_notify_addq 2226 * 2227 * This function will generate events for dev. 2228 *------------------------------------------------------------------------*/ 2229static void 2230usb2_notify_addq(const char *type, struct usb2_device *udev) 2231{ 2232 char *data = NULL; 2233 struct malloc_type *mt; 2234 2235 mtx_lock(&malloc_mtx); 2236 mt = malloc_desc2type("bus"); /* XXX M_BUS */ 2237 mtx_unlock(&malloc_mtx); 2238 if (mt == NULL) 2239 return; 2240 2241 data = malloc(512, mt, M_NOWAIT); 2242 if (data == NULL) 2243 return; 2244 2245 /* String it all together. */ 2246 if (udev->parent_hub) { 2247 snprintf(data, 1024, 2248 "%s" 2249 "%s " 2250 "vendor=0x%04x " 2251 "product=0x%04x " 2252 "devclass=0x%02x " 2253 "devsubclass=0x%02x " 2254 "sernum=\"%s\" " 2255 "at " 2256 "port=%u " 2257 "on " 2258 "%s\n", 2259 type, 2260 udev->ugen_name, 2261 UGETW(udev->ddesc.idVendor), 2262 UGETW(udev->ddesc.idProduct), 2263 udev->ddesc.bDeviceClass, 2264 udev->ddesc.bDeviceSubClass, 2265 udev->serial, 2266 udev->port_no, 2267 udev->parent_hub->ugen_name); 2268 } else { 2269 snprintf(data, 1024, 2270 "%s" 2271 "%s " 2272 "vendor=0x%04x " 2273 "product=0x%04x " 2274 "devclass=0x%02x " 2275 "devsubclass=0x%02x " 2276 "sernum=\"%s\" " 2277 "at port=%u " 2278 "on " 2279 "%s\n", 2280 type, 2281 udev->ugen_name, 2282 UGETW(udev->ddesc.idVendor), 2283 UGETW(udev->ddesc.idProduct), 2284 udev->ddesc.bDeviceClass, 2285 udev->ddesc.bDeviceSubClass, 2286 udev->serial, 2287 udev->port_no, 2288 device_get_nameunit(device_get_parent(udev->bus->bdev))); 2289 } 2290 devctl_queue_data(data); 2291} 2292 2293#if USB_HAVE_UGEN 2294/*------------------------------------------------------------------------* 2295 * usb2_fifo_free_wrap 2296 * 2297 * This function will free the FIFOs. 2298 * 2299 * Flag values, if "iface_index" is equal to "USB_IFACE_INDEX_ANY". 2300 * 0: Free all FIFOs except generic control endpoints. 2301 * 1: Free all FIFOs. 2302 * 2303 * Flag values, if "iface_index" is not equal to "USB_IFACE_INDEX_ANY". 2304 * Not used. 2305 *------------------------------------------------------------------------*/ 2306static void 2307usb2_fifo_free_wrap(struct usb2_device *udev, 2308 uint8_t iface_index, uint8_t flag) 2309{ 2310 struct usb2_fifo *f; 2311 uint16_t i; 2312 2313 /* 2314 * Free any USB FIFOs on the given interface: 2315 */ 2316 for (i = 0; i != USB_FIFO_MAX; i++) { 2317 f = udev->fifo[i]; 2318 if (f == NULL) { 2319 continue; 2320 } 2321 /* Check if the interface index matches */ 2322 if (iface_index == f->iface_index) { 2323 if (f->methods != &usb2_ugen_methods) { 2324 /* 2325 * Don't free any non-generic FIFOs in 2326 * this case. 2327 */ 2328 continue; 2329 } 2330 if ((f->dev_ep_index == 0) && 2331 (f->fs_xfer == NULL)) { 2332 /* no need to free this FIFO */ 2333 continue; 2334 } 2335 } else if (iface_index == USB_IFACE_INDEX_ANY) { 2336 if ((f->methods == &usb2_ugen_methods) && 2337 (f->dev_ep_index == 0) && (flag == 0) && 2338 (f->fs_xfer == NULL)) { 2339 /* no need to free this FIFO */ 2340 continue; 2341 } 2342 } else { 2343 /* no need to free this FIFO */ 2344 continue; 2345 } 2346 /* free this FIFO */ 2347 usb2_fifo_free(f); 2348 } 2349} 2350#endif 2351 2352/*------------------------------------------------------------------------* 2353 * usb2_peer_can_wakeup 2354 * 2355 * Return values: 2356 * 0: Peer cannot do resume signalling. 2357 * Else: Peer can do resume signalling. 2358 *------------------------------------------------------------------------*/ 2359uint8_t 2360usb2_peer_can_wakeup(struct usb2_device *udev) 2361{ 2362 const struct usb2_config_descriptor *cdp; 2363 2364 cdp = udev->cdesc; 2365 if ((cdp != NULL) && (udev->flags.usb2_mode == USB_MODE_HOST)) { 2366 return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 2367 } 2368 return (0); /* not supported */ 2369} 2370