1 /* 2 * (C) Copyright 2015 Google, Inc 3 * Written by Simon Glass <sjg@chromium.org> 4 * 5 * usb_match_device() modified from Linux kernel v4.0. 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <dm.h> 12 #include <errno.h> 13 #include <memalign.h> 14 #include <usb.h> 15 #include <dm/device-internal.h> 16 #include <dm/lists.h> 17 #include <dm/uclass-internal.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 extern bool usb_started; /* flag for the started/stopped USB status */ 22 static bool asynch_allowed; 23 24 struct usb_uclass_priv { 25 int companion_device_count; 26 }; 27 28 int usb_disable_asynch(int disable) 29 { 30 int old_value = asynch_allowed; 31 32 asynch_allowed = !disable; 33 return old_value; 34 } 35 36 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, 37 int length, int interval) 38 { 39 struct udevice *bus = udev->controller_dev; 40 struct dm_usb_ops *ops = usb_get_ops(bus); 41 42 if (!ops->interrupt) 43 return -ENOSYS; 44 45 return ops->interrupt(bus, udev, pipe, buffer, length, interval); 46 } 47 48 int submit_control_msg(struct usb_device *udev, unsigned long pipe, 49 void *buffer, int length, struct devrequest *setup) 50 { 51 struct udevice *bus = udev->controller_dev; 52 struct dm_usb_ops *ops = usb_get_ops(bus); 53 struct usb_uclass_priv *uc_priv = bus->uclass->priv; 54 int err; 55 56 if (!ops->control) 57 return -ENOSYS; 58 59 err = ops->control(bus, udev, pipe, buffer, length, setup); 60 if (setup->request == USB_REQ_SET_FEATURE && 61 setup->requesttype == USB_RT_PORT && 62 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) && 63 err == -ENXIO) { 64 /* Device handed over to companion after port reset */ 65 uc_priv->companion_device_count++; 66 } 67 68 return err; 69 } 70 71 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, 72 int length) 73 { 74 struct udevice *bus = udev->controller_dev; 75 struct dm_usb_ops *ops = usb_get_ops(bus); 76 77 if (!ops->bulk) 78 return -ENOSYS; 79 80 return ops->bulk(bus, udev, pipe, buffer, length); 81 } 82 83 struct int_queue *create_int_queue(struct usb_device *udev, 84 unsigned long pipe, int queuesize, int elementsize, 85 void *buffer, int interval) 86 { 87 struct udevice *bus = udev->controller_dev; 88 struct dm_usb_ops *ops = usb_get_ops(bus); 89 90 if (!ops->create_int_queue) 91 return NULL; 92 93 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize, 94 buffer, interval); 95 } 96 97 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue) 98 { 99 struct udevice *bus = udev->controller_dev; 100 struct dm_usb_ops *ops = usb_get_ops(bus); 101 102 if (!ops->poll_int_queue) 103 return NULL; 104 105 return ops->poll_int_queue(bus, udev, queue); 106 } 107 108 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue) 109 { 110 struct udevice *bus = udev->controller_dev; 111 struct dm_usb_ops *ops = usb_get_ops(bus); 112 113 if (!ops->destroy_int_queue) 114 return -ENOSYS; 115 116 return ops->destroy_int_queue(bus, udev, queue); 117 } 118 119 int usb_alloc_device(struct usb_device *udev) 120 { 121 struct udevice *bus = udev->controller_dev; 122 struct dm_usb_ops *ops = usb_get_ops(bus); 123 124 /* This is only requird by some controllers - current XHCI */ 125 if (!ops->alloc_device) 126 return 0; 127 128 return ops->alloc_device(bus, udev); 129 } 130 131 int usb_reset_root_port(struct usb_device *udev) 132 { 133 struct udevice *bus = udev->controller_dev; 134 struct dm_usb_ops *ops = usb_get_ops(bus); 135 136 if (!ops->reset_root_port) 137 return -ENOSYS; 138 139 return ops->reset_root_port(bus, udev); 140 } 141 142 int usb_update_hub_device(struct usb_device *udev) 143 { 144 struct udevice *bus = udev->controller_dev; 145 struct dm_usb_ops *ops = usb_get_ops(bus); 146 147 if (!ops->update_hub_device) 148 return -ENOSYS; 149 150 return ops->update_hub_device(bus, udev); 151 } 152 153 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size) 154 { 155 struct udevice *bus = udev->controller_dev; 156 struct dm_usb_ops *ops = usb_get_ops(bus); 157 158 if (!ops->get_max_xfer_size) 159 return -ENOSYS; 160 161 return ops->get_max_xfer_size(bus, size); 162 } 163 164 int usb_stop(void) 165 { 166 struct udevice *bus; 167 struct udevice *rh; 168 struct uclass *uc; 169 struct usb_uclass_priv *uc_priv; 170 int err = 0, ret; 171 172 /* De-activate any devices that have been activated */ 173 ret = uclass_get(UCLASS_USB, &uc); 174 if (ret) 175 return ret; 176 177 uc_priv = uc->priv; 178 179 uclass_foreach_dev(bus, uc) { 180 ret = device_remove(bus, DM_REMOVE_NORMAL); 181 if (ret && !err) 182 err = ret; 183 184 /* Locate root hub device */ 185 device_find_first_child(bus, &rh); 186 if (rh) { 187 /* 188 * All USB devices are children of root hub. 189 * Unbinding root hub will unbind all of its children. 190 */ 191 ret = device_unbind(rh); 192 if (ret && !err) 193 err = ret; 194 } 195 } 196 #ifdef CONFIG_BLK 197 ret = blk_unbind_all(IF_TYPE_USB); 198 if (ret && !err) 199 err = ret; 200 #endif 201 #ifdef CONFIG_SANDBOX 202 struct udevice *dev; 203 204 /* Reset all enulation devices */ 205 ret = uclass_get(UCLASS_USB_EMUL, &uc); 206 if (ret) 207 return ret; 208 209 uclass_foreach_dev(dev, uc) 210 usb_emul_reset(dev); 211 #endif 212 #ifdef CONFIG_USB_STORAGE 213 usb_stor_reset(); 214 #endif 215 uc_priv->companion_device_count = 0; 216 usb_started = 0; 217 218 return err; 219 } 220 221 static void usb_scan_bus(struct udevice *bus, bool recurse) 222 { 223 struct usb_bus_priv *priv; 224 struct udevice *dev; 225 int ret; 226 227 priv = dev_get_uclass_priv(bus); 228 229 assert(recurse); /* TODO: Support non-recusive */ 230 231 printf("scanning bus %d for devices... ", bus->seq); 232 debug("\n"); 233 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev); 234 if (ret) 235 printf("failed, error %d\n", ret); 236 else if (priv->next_addr == 0) 237 printf("No USB Device found\n"); 238 else 239 printf("%d USB Device(s) found\n", priv->next_addr); 240 } 241 242 static void remove_inactive_children(struct uclass *uc, struct udevice *bus) 243 { 244 uclass_foreach_dev(bus, uc) { 245 struct udevice *dev, *next; 246 247 if (!device_active(bus)) 248 continue; 249 device_foreach_child_safe(dev, next, bus) { 250 if (!device_active(dev)) 251 device_unbind(dev); 252 } 253 } 254 } 255 256 int usb_init(void) 257 { 258 int controllers_initialized = 0; 259 struct usb_uclass_priv *uc_priv; 260 struct usb_bus_priv *priv; 261 struct udevice *bus; 262 struct uclass *uc; 263 int count = 0; 264 int ret; 265 266 asynch_allowed = 1; 267 268 ret = uclass_get(UCLASS_USB, &uc); 269 if (ret) 270 return ret; 271 272 uc_priv = uc->priv; 273 274 uclass_foreach_dev(bus, uc) { 275 /* init low_level USB */ 276 printf("USB%d: ", count); 277 count++; 278 279 #ifdef CONFIG_SANDBOX 280 /* 281 * For Sandbox, we need scan the device tree each time when we 282 * start the USB stack, in order to re-create the emulated USB 283 * devices and bind drivers for them before we actually do the 284 * driver probe. 285 */ 286 ret = dm_scan_fdt_dev(bus); 287 if (ret) { 288 printf("Sandbox USB device scan failed (%d)\n", ret); 289 continue; 290 } 291 #endif 292 293 ret = device_probe(bus); 294 if (ret == -ENODEV) { /* No such device. */ 295 puts("Port not available.\n"); 296 controllers_initialized++; 297 continue; 298 } 299 300 if (ret) { /* Other error. */ 301 printf("probe failed, error %d\n", ret); 302 continue; 303 } 304 controllers_initialized++; 305 usb_started = true; 306 } 307 308 /* 309 * lowlevel init done, now scan the bus for devices i.e. search HUBs 310 * and configure them, first scan primary controllers. 311 */ 312 uclass_foreach_dev(bus, uc) { 313 if (!device_active(bus)) 314 continue; 315 316 priv = dev_get_uclass_priv(bus); 317 if (!priv->companion) 318 usb_scan_bus(bus, true); 319 } 320 321 /* 322 * Now that the primary controllers have been scanned and have handed 323 * over any devices they do not understand to their companions, scan 324 * the companions if necessary. 325 */ 326 if (uc_priv->companion_device_count) { 327 uclass_foreach_dev(bus, uc) { 328 if (!device_active(bus)) 329 continue; 330 331 priv = dev_get_uclass_priv(bus); 332 if (priv->companion) 333 usb_scan_bus(bus, true); 334 } 335 } 336 337 debug("scan end\n"); 338 339 /* Remove any devices that were not found on this scan */ 340 remove_inactive_children(uc, bus); 341 342 ret = uclass_get(UCLASS_USB_HUB, &uc); 343 if (ret) 344 return ret; 345 remove_inactive_children(uc, bus); 346 347 /* if we were not able to find at least one working bus, bail out */ 348 if (!count) 349 printf("No controllers found\n"); 350 else if (controllers_initialized == 0) 351 printf("USB error: all controllers failed lowlevel init\n"); 352 353 return usb_started ? 0 : -1; 354 } 355 356 /* 357 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed 358 * to support boards which use driver model for USB but not Ethernet, and want 359 * to use USB Ethernet. 360 * 361 * The #if clause is here to ensure that remains the only case. 362 */ 363 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER) 364 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum) 365 { 366 struct usb_device *udev; 367 struct udevice *dev; 368 369 if (!device_active(parent)) 370 return NULL; 371 udev = dev_get_parent_priv(parent); 372 if (udev->devnum == devnum) 373 return udev; 374 375 for (device_find_first_child(parent, &dev); 376 dev; 377 device_find_next_child(&dev)) { 378 udev = find_child_devnum(dev, devnum); 379 if (udev) 380 return udev; 381 } 382 383 return NULL; 384 } 385 386 struct usb_device *usb_get_dev_index(struct udevice *bus, int index) 387 { 388 struct udevice *dev; 389 int devnum = index + 1; /* Addresses are allocated from 1 on USB */ 390 391 device_find_first_child(bus, &dev); 392 if (!dev) 393 return NULL; 394 395 return find_child_devnum(dev, devnum); 396 } 397 #endif 398 399 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp) 400 { 401 struct usb_platdata *plat; 402 struct udevice *dev; 403 int ret; 404 405 /* Find the old device and remove it */ 406 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev); 407 if (ret) 408 return ret; 409 ret = device_remove(dev, DM_REMOVE_NORMAL); 410 if (ret) 411 return ret; 412 413 plat = dev_get_platdata(dev); 414 plat->init_type = USB_INIT_DEVICE; 415 ret = device_probe(dev); 416 if (ret) 417 return ret; 418 *ctlrp = dev_get_priv(dev); 419 420 return 0; 421 } 422 423 /* returns 0 if no match, 1 if match */ 424 static int usb_match_device(const struct usb_device_descriptor *desc, 425 const struct usb_device_id *id) 426 { 427 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 428 id->idVendor != le16_to_cpu(desc->idVendor)) 429 return 0; 430 431 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 432 id->idProduct != le16_to_cpu(desc->idProduct)) 433 return 0; 434 435 /* No need to test id->bcdDevice_lo != 0, since 0 is never 436 greater than any unsigned number. */ 437 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 438 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice))) 439 return 0; 440 441 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 442 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice))) 443 return 0; 444 445 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 446 (id->bDeviceClass != desc->bDeviceClass)) 447 return 0; 448 449 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 450 (id->bDeviceSubClass != desc->bDeviceSubClass)) 451 return 0; 452 453 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 454 (id->bDeviceProtocol != desc->bDeviceProtocol)) 455 return 0; 456 457 return 1; 458 } 459 460 /* returns 0 if no match, 1 if match */ 461 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc, 462 const struct usb_interface_descriptor *int_desc, 463 const struct usb_device_id *id) 464 { 465 /* The interface class, subclass, protocol and number should never be 466 * checked for a match if the device class is Vendor Specific, 467 * unless the match record specifies the Vendor ID. */ 468 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC && 469 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 470 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 471 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 472 USB_DEVICE_ID_MATCH_INT_PROTOCOL | 473 USB_DEVICE_ID_MATCH_INT_NUMBER))) 474 return 0; 475 476 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 477 (id->bInterfaceClass != int_desc->bInterfaceClass)) 478 return 0; 479 480 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 481 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass)) 482 return 0; 483 484 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 485 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol)) 486 return 0; 487 488 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) && 489 (id->bInterfaceNumber != int_desc->bInterfaceNumber)) 490 return 0; 491 492 return 1; 493 } 494 495 /* returns 0 if no match, 1 if match */ 496 static int usb_match_one_id(struct usb_device_descriptor *desc, 497 struct usb_interface_descriptor *int_desc, 498 const struct usb_device_id *id) 499 { 500 if (!usb_match_device(desc, id)) 501 return 0; 502 503 return usb_match_one_id_intf(desc, int_desc, id); 504 } 505 506 /** 507 * usb_find_and_bind_driver() - Find and bind the right USB driver 508 * 509 * This only looks at certain fields in the descriptor. 510 */ 511 static int usb_find_and_bind_driver(struct udevice *parent, 512 struct usb_device_descriptor *desc, 513 struct usb_interface_descriptor *iface, 514 int bus_seq, int devnum, 515 struct udevice **devp) 516 { 517 struct usb_driver_entry *start, *entry; 518 int n_ents; 519 int ret; 520 char name[30], *str; 521 522 *devp = NULL; 523 debug("%s: Searching for driver\n", __func__); 524 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry); 525 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry); 526 for (entry = start; entry != start + n_ents; entry++) { 527 const struct usb_device_id *id; 528 struct udevice *dev; 529 const struct driver *drv; 530 struct usb_dev_platdata *plat; 531 532 for (id = entry->match; id->match_flags; id++) { 533 if (!usb_match_one_id(desc, iface, id)) 534 continue; 535 536 drv = entry->driver; 537 /* 538 * We could pass the descriptor to the driver as 539 * platdata (instead of NULL) and allow its bind() 540 * method to return -ENOENT if it doesn't support this 541 * device. That way we could continue the search to 542 * find another driver. For now this doesn't seem 543 * necesssary, so just bind the first match. 544 */ 545 ret = device_bind(parent, drv, drv->name, NULL, -1, 546 &dev); 547 if (ret) 548 goto error; 549 debug("%s: Match found: %s\n", __func__, drv->name); 550 dev->driver_data = id->driver_info; 551 plat = dev_get_parent_platdata(dev); 552 plat->id = *id; 553 *devp = dev; 554 return 0; 555 } 556 } 557 558 /* Bind a generic driver so that the device can be used */ 559 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum); 560 str = strdup(name); 561 if (!str) 562 return -ENOMEM; 563 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp); 564 565 error: 566 debug("%s: No match found: %d\n", __func__, ret); 567 return ret; 568 } 569 570 /** 571 * usb_find_child() - Find an existing device which matches our needs 572 * 573 * 574 */ 575 static int usb_find_child(struct udevice *parent, 576 struct usb_device_descriptor *desc, 577 struct usb_interface_descriptor *iface, 578 struct udevice **devp) 579 { 580 struct udevice *dev; 581 582 *devp = NULL; 583 for (device_find_first_child(parent, &dev); 584 dev; 585 device_find_next_child(&dev)) { 586 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); 587 588 /* If this device is already in use, skip it */ 589 if (device_active(dev)) 590 continue; 591 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__, 592 dev->name, plat->id.bDeviceClass, desc->bDeviceClass); 593 if (usb_match_one_id(desc, iface, &plat->id)) { 594 *devp = dev; 595 return 0; 596 } 597 } 598 599 return -ENOENT; 600 } 601 602 int usb_scan_device(struct udevice *parent, int port, 603 enum usb_device_speed speed, struct udevice **devp) 604 { 605 struct udevice *dev; 606 bool created = false; 607 struct usb_dev_platdata *plat; 608 struct usb_bus_priv *priv; 609 struct usb_device *parent_udev; 610 int ret; 611 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1); 612 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc; 613 614 *devp = NULL; 615 memset(udev, '\0', sizeof(*udev)); 616 udev->controller_dev = usb_get_bus(parent); 617 priv = dev_get_uclass_priv(udev->controller_dev); 618 619 /* 620 * Somewhat nasty, this. We create a local device and use the normal 621 * USB stack to read its descriptor. Then we know what type of device 622 * to create for real. 623 * 624 * udev->dev is set to the parent, since we don't have a real device 625 * yet. The USB stack should not access udev.dev anyway, except perhaps 626 * to find the controller, and the controller will either be @parent, 627 * or some parent of @parent. 628 * 629 * Another option might be to create the device as a generic USB 630 * device, then morph it into the correct one when we know what it 631 * should be. This means that a generic USB device would morph into 632 * a network controller, or a USB flash stick, for example. However, 633 * we don't support such morphing and it isn't clear that it would 634 * be easy to do. 635 * 636 * Yet another option is to split out the USB stack parts of udev 637 * into something like a 'struct urb' (as Linux does) which can exist 638 * independently of any device. This feels cleaner, but calls for quite 639 * a big change to the USB stack. 640 * 641 * For now, the approach is to set up an empty udev, read its 642 * descriptor and assign it an address, then bind a real device and 643 * stash the resulting information into the device's parent 644 * platform data. Then when we probe it, usb_child_pre_probe() is called 645 * and it will pull the information out of the stash. 646 */ 647 udev->dev = parent; 648 udev->speed = speed; 649 udev->devnum = priv->next_addr + 1; 650 udev->portnr = port; 651 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr); 652 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ? 653 dev_get_parent_priv(parent) : NULL; 654 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev); 655 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret); 656 if (ret) 657 return ret; 658 ret = usb_find_child(parent, &udev->descriptor, iface, &dev); 659 debug("** usb_find_child returns %d\n", ret); 660 if (ret) { 661 if (ret != -ENOENT) 662 return ret; 663 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface, 664 udev->controller_dev->seq, 665 udev->devnum, &dev); 666 if (ret) 667 return ret; 668 created = true; 669 } 670 plat = dev_get_parent_platdata(dev); 671 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat); 672 plat->devnum = udev->devnum; 673 plat->udev = udev; 674 priv->next_addr++; 675 ret = device_probe(dev); 676 if (ret) { 677 debug("%s: Device '%s' probe failed\n", __func__, dev->name); 678 priv->next_addr--; 679 if (created) 680 device_unbind(dev); 681 return ret; 682 } 683 *devp = dev; 684 685 return 0; 686 } 687 688 /* 689 * Detect if a USB device has been plugged or unplugged. 690 */ 691 int usb_detect_change(void) 692 { 693 struct udevice *hub; 694 struct uclass *uc; 695 int change = 0; 696 int ret; 697 698 ret = uclass_get(UCLASS_USB_HUB, &uc); 699 if (ret) 700 return ret; 701 702 uclass_foreach_dev(hub, uc) { 703 struct usb_device *udev; 704 struct udevice *dev; 705 706 if (!device_active(hub)) 707 continue; 708 for (device_find_first_child(hub, &dev); 709 dev; 710 device_find_next_child(&dev)) { 711 struct usb_port_status status; 712 713 if (!device_active(dev)) 714 continue; 715 716 udev = dev_get_parent_priv(dev); 717 if (usb_get_port_status(udev, udev->portnr, &status) 718 < 0) 719 /* USB request failed */ 720 continue; 721 722 if (le16_to_cpu(status.wPortChange) & 723 USB_PORT_STAT_C_CONNECTION) 724 change++; 725 } 726 } 727 728 return change; 729 } 730 731 static int usb_child_post_bind(struct udevice *dev) 732 { 733 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); 734 int val; 735 736 if (!dev_of_valid(dev)) 737 return 0; 738 739 /* We only support matching a few things */ 740 val = dev_read_u32_default(dev, "usb,device-class", -1); 741 if (val != -1) { 742 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS; 743 plat->id.bDeviceClass = val; 744 } 745 val = dev_read_u32_default(dev, "usb,interface-class", -1); 746 if (val != -1) { 747 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; 748 plat->id.bInterfaceClass = val; 749 } 750 751 return 0; 752 } 753 754 struct udevice *usb_get_bus(struct udevice *dev) 755 { 756 struct udevice *bus; 757 758 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; ) 759 bus = bus->parent; 760 if (!bus) { 761 /* By design this cannot happen */ 762 assert(bus); 763 debug("USB HUB '%s' does not have a controller\n", dev->name); 764 } 765 766 return bus; 767 } 768 769 int usb_child_pre_probe(struct udevice *dev) 770 { 771 struct usb_device *udev = dev_get_parent_priv(dev); 772 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); 773 int ret; 774 775 if (plat->udev) { 776 /* 777 * Copy over all the values set in the on stack struct 778 * usb_device in usb_scan_device() to our final struct 779 * usb_device for this dev. 780 */ 781 *udev = *(plat->udev); 782 /* And clear plat->udev as it will not be valid for long */ 783 plat->udev = NULL; 784 udev->dev = dev; 785 } else { 786 /* 787 * This happens with devices which are explicitly bound 788 * instead of being discovered through usb_scan_device() 789 * such as sandbox emul devices. 790 */ 791 udev->dev = dev; 792 udev->controller_dev = usb_get_bus(dev); 793 udev->devnum = plat->devnum; 794 795 /* 796 * udev did not go through usb_scan_device(), so we need to 797 * select the config and read the config descriptors. 798 */ 799 ret = usb_select_config(udev); 800 if (ret) 801 return ret; 802 } 803 804 return 0; 805 } 806 807 UCLASS_DRIVER(usb) = { 808 .id = UCLASS_USB, 809 .name = "usb", 810 .flags = DM_UC_FLAG_SEQ_ALIAS, 811 .post_bind = dm_scan_fdt_dev, 812 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv), 813 .per_child_auto_alloc_size = sizeof(struct usb_device), 814 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv), 815 .child_post_bind = usb_child_post_bind, 816 .child_pre_probe = usb_child_pre_probe, 817 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), 818 }; 819 820 UCLASS_DRIVER(usb_dev_generic) = { 821 .id = UCLASS_USB_DEV_GENERIC, 822 .name = "usb_dev_generic", 823 }; 824 825 U_BOOT_DRIVER(usb_dev_generic_drv) = { 826 .id = UCLASS_USB_DEV_GENERIC, 827 .name = "usb_dev_generic_drv", 828 }; 829