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