1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
6 * usb_match_device() modified from Linux kernel v4.0.
14 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
18 extern bool usb_started; /* flag for the started/stopped USB status */
19 static bool asynch_allowed;
21 struct usb_uclass_priv {
22 int companion_device_count;
25 int usb_disable_asynch(int disable)
27 int old_value = asynch_allowed;
29 asynch_allowed = !disable;
33 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
34 int length, int interval)
36 struct udevice *bus = udev->controller_dev;
37 struct dm_usb_ops *ops = usb_get_ops(bus);
42 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
45 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
46 void *buffer, int length, struct devrequest *setup)
48 struct udevice *bus = udev->controller_dev;
49 struct dm_usb_ops *ops = usb_get_ops(bus);
50 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
56 err = ops->control(bus, udev, pipe, buffer, length, setup);
57 if (setup->request == USB_REQ_SET_FEATURE &&
58 setup->requesttype == USB_RT_PORT &&
59 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
61 /* Device handed over to companion after port reset */
62 uc_priv->companion_device_count++;
68 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
71 struct udevice *bus = udev->controller_dev;
72 struct dm_usb_ops *ops = usb_get_ops(bus);
77 return ops->bulk(bus, udev, pipe, buffer, length);
80 struct int_queue *create_int_queue(struct usb_device *udev,
81 unsigned long pipe, int queuesize, int elementsize,
82 void *buffer, int interval)
84 struct udevice *bus = udev->controller_dev;
85 struct dm_usb_ops *ops = usb_get_ops(bus);
87 if (!ops->create_int_queue)
90 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
94 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
96 struct udevice *bus = udev->controller_dev;
97 struct dm_usb_ops *ops = usb_get_ops(bus);
99 if (!ops->poll_int_queue)
102 return ops->poll_int_queue(bus, udev, queue);
105 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
107 struct udevice *bus = udev->controller_dev;
108 struct dm_usb_ops *ops = usb_get_ops(bus);
110 if (!ops->destroy_int_queue)
113 return ops->destroy_int_queue(bus, udev, queue);
116 int usb_alloc_device(struct usb_device *udev)
118 struct udevice *bus = udev->controller_dev;
119 struct dm_usb_ops *ops = usb_get_ops(bus);
121 /* This is only requird by some controllers - current XHCI */
122 if (!ops->alloc_device)
125 return ops->alloc_device(bus, udev);
128 int usb_reset_root_port(struct usb_device *udev)
130 struct udevice *bus = udev->controller_dev;
131 struct dm_usb_ops *ops = usb_get_ops(bus);
133 if (!ops->reset_root_port)
136 return ops->reset_root_port(bus, udev);
139 int usb_update_hub_device(struct usb_device *udev)
141 struct udevice *bus = udev->controller_dev;
142 struct dm_usb_ops *ops = usb_get_ops(bus);
144 if (!ops->update_hub_device)
147 return ops->update_hub_device(bus, udev);
150 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
152 struct udevice *bus = udev->controller_dev;
153 struct dm_usb_ops *ops = usb_get_ops(bus);
155 if (!ops->get_max_xfer_size)
158 return ops->get_max_xfer_size(bus, size);
166 struct usb_uclass_priv *uc_priv;
169 /* De-activate any devices that have been activated */
170 ret = uclass_get(UCLASS_USB, &uc);
176 uclass_foreach_dev(bus, uc) {
177 ret = device_remove(bus, DM_REMOVE_NORMAL);
181 /* Locate root hub device */
182 device_find_first_child(bus, &rh);
185 * All USB devices are children of root hub.
186 * Unbinding root hub will unbind all of its children.
188 ret = device_unbind(rh);
194 #ifdef CONFIG_USB_STORAGE
197 uc_priv->companion_device_count = 0;
203 static void usb_scan_bus(struct udevice *bus, bool recurse)
205 struct usb_bus_priv *priv;
209 priv = dev_get_uclass_priv(bus);
211 assert(recurse); /* TODO: Support non-recusive */
213 printf("scanning bus %s for devices... ", bus->name);
215 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
217 printf("failed, error %d\n", ret);
218 else if (priv->next_addr == 0)
219 printf("No USB Device found\n");
221 printf("%d USB Device(s) found\n", priv->next_addr);
224 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
226 uclass_foreach_dev(bus, uc) {
227 struct udevice *dev, *next;
229 if (!device_active(bus))
231 device_foreach_child_safe(dev, next, bus) {
232 if (!device_active(dev))
240 int controllers_initialized = 0;
241 struct usb_uclass_priv *uc_priv;
242 struct usb_bus_priv *priv;
249 ret = uclass_get(UCLASS_USB, &uc);
255 uclass_foreach_dev(bus, uc) {
256 /* init low_level USB */
257 printf("Bus %s: ", bus->name);
259 #ifdef CONFIG_SANDBOX
261 * For Sandbox, we need scan the device tree each time when we
262 * start the USB stack, in order to re-create the emulated USB
263 * devices and bind drivers for them before we actually do the
266 ret = dm_scan_fdt_dev(bus);
268 printf("Sandbox USB device scan failed (%d)\n", ret);
273 ret = device_probe(bus);
274 if (ret == -ENODEV) { /* No such device. */
275 puts("Port not available.\n");
276 controllers_initialized++;
280 if (ret) { /* Other error. */
281 printf("probe failed, error %d\n", ret);
284 controllers_initialized++;
289 * lowlevel init done, now scan the bus for devices i.e. search HUBs
290 * and configure them, first scan primary controllers.
292 uclass_foreach_dev(bus, uc) {
293 if (!device_active(bus))
296 priv = dev_get_uclass_priv(bus);
297 if (!priv->companion)
298 usb_scan_bus(bus, true);
302 * Now that the primary controllers have been scanned and have handed
303 * over any devices they do not understand to their companions, scan
304 * the companions if necessary.
306 if (uc_priv->companion_device_count) {
307 uclass_foreach_dev(bus, uc) {
308 if (!device_active(bus))
311 priv = dev_get_uclass_priv(bus);
313 usb_scan_bus(bus, true);
319 /* Remove any devices that were not found on this scan */
320 remove_inactive_children(uc, bus);
322 ret = uclass_get(UCLASS_USB_HUB, &uc);
325 remove_inactive_children(uc, bus);
327 /* if we were not able to find at least one working bus, bail out */
328 if (controllers_initialized == 0)
329 printf("No working controllers found\n");
331 return usb_started ? 0 : -1;
335 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
336 * to support boards which use driver model for USB but not Ethernet, and want
337 * to use USB Ethernet.
339 * The #if clause is here to ensure that remains the only case.
341 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
342 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
344 struct usb_device *udev;
347 if (!device_active(parent))
349 udev = dev_get_parent_priv(parent);
350 if (udev->devnum == devnum)
353 for (device_find_first_child(parent, &dev);
355 device_find_next_child(&dev)) {
356 udev = find_child_devnum(dev, devnum);
364 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
367 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
369 device_find_first_child(bus, &dev);
373 return find_child_devnum(dev, devnum);
377 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
379 struct usb_platdata *plat;
383 /* Find the old device and remove it */
384 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
387 ret = device_remove(dev, DM_REMOVE_NORMAL);
391 plat = dev_get_platdata(dev);
392 plat->init_type = USB_INIT_DEVICE;
393 ret = device_probe(dev);
396 *ctlrp = dev_get_priv(dev);
401 /* returns 0 if no match, 1 if match */
402 static int usb_match_device(const struct usb_device_descriptor *desc,
403 const struct usb_device_id *id)
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
406 id->idVendor != le16_to_cpu(desc->idVendor))
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
410 id->idProduct != le16_to_cpu(desc->idProduct))
413 /* No need to test id->bcdDevice_lo != 0, since 0 is never
414 greater than any unsigned number. */
415 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
416 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
420 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
424 (id->bDeviceClass != desc->bDeviceClass))
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
428 (id->bDeviceSubClass != desc->bDeviceSubClass))
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
432 (id->bDeviceProtocol != desc->bDeviceProtocol))
438 /* returns 0 if no match, 1 if match */
439 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
440 const struct usb_interface_descriptor *int_desc,
441 const struct usb_device_id *id)
443 /* The interface class, subclass, protocol and number should never be
444 * checked for a match if the device class is Vendor Specific,
445 * unless the match record specifies the Vendor ID. */
446 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
447 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
448 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
449 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
450 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
451 USB_DEVICE_ID_MATCH_INT_NUMBER)))
454 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
455 (id->bInterfaceClass != int_desc->bInterfaceClass))
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
459 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
463 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
467 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
473 /* returns 0 if no match, 1 if match */
474 static int usb_match_one_id(struct usb_device_descriptor *desc,
475 struct usb_interface_descriptor *int_desc,
476 const struct usb_device_id *id)
478 if (!usb_match_device(desc, id))
481 return usb_match_one_id_intf(desc, int_desc, id);
485 * usb_find_and_bind_driver() - Find and bind the right USB driver
487 * This only looks at certain fields in the descriptor.
489 static int usb_find_and_bind_driver(struct udevice *parent,
490 struct usb_device_descriptor *desc,
491 struct usb_interface_descriptor *iface,
492 int bus_seq, int devnum,
493 struct udevice **devp)
495 struct usb_driver_entry *start, *entry;
501 debug("%s: Searching for driver\n", __func__);
502 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
503 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
504 for (entry = start; entry != start + n_ents; entry++) {
505 const struct usb_device_id *id;
507 const struct driver *drv;
508 struct usb_dev_platdata *plat;
510 for (id = entry->match; id->match_flags; id++) {
511 if (!usb_match_one_id(desc, iface, id))
516 * We could pass the descriptor to the driver as
517 * platdata (instead of NULL) and allow its bind()
518 * method to return -ENOENT if it doesn't support this
519 * device. That way we could continue the search to
520 * find another driver. For now this doesn't seem
521 * necesssary, so just bind the first match.
523 ret = device_bind(parent, drv, drv->name, NULL, -1,
527 debug("%s: Match found: %s\n", __func__, drv->name);
528 dev->driver_data = id->driver_info;
529 plat = dev_get_parent_platdata(dev);
536 /* Bind a generic driver so that the device can be used */
537 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
541 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
544 debug("%s: No match found: %d\n", __func__, ret);
549 * usb_find_child() - Find an existing device which matches our needs
553 static int usb_find_child(struct udevice *parent,
554 struct usb_device_descriptor *desc,
555 struct usb_interface_descriptor *iface,
556 struct udevice **devp)
561 for (device_find_first_child(parent, &dev);
563 device_find_next_child(&dev)) {
564 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
566 /* If this device is already in use, skip it */
567 if (device_active(dev))
569 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
570 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
571 if (usb_match_one_id(desc, iface, &plat->id)) {
580 int usb_scan_device(struct udevice *parent, int port,
581 enum usb_device_speed speed, struct udevice **devp)
584 bool created = false;
585 struct usb_dev_platdata *plat;
586 struct usb_bus_priv *priv;
587 struct usb_device *parent_udev;
589 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
590 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
593 memset(udev, '\0', sizeof(*udev));
594 udev->controller_dev = usb_get_bus(parent);
595 priv = dev_get_uclass_priv(udev->controller_dev);
598 * Somewhat nasty, this. We create a local device and use the normal
599 * USB stack to read its descriptor. Then we know what type of device
600 * to create for real.
602 * udev->dev is set to the parent, since we don't have a real device
603 * yet. The USB stack should not access udev.dev anyway, except perhaps
604 * to find the controller, and the controller will either be @parent,
605 * or some parent of @parent.
607 * Another option might be to create the device as a generic USB
608 * device, then morph it into the correct one when we know what it
609 * should be. This means that a generic USB device would morph into
610 * a network controller, or a USB flash stick, for example. However,
611 * we don't support such morphing and it isn't clear that it would
614 * Yet another option is to split out the USB stack parts of udev
615 * into something like a 'struct urb' (as Linux does) which can exist
616 * independently of any device. This feels cleaner, but calls for quite
617 * a big change to the USB stack.
619 * For now, the approach is to set up an empty udev, read its
620 * descriptor and assign it an address, then bind a real device and
621 * stash the resulting information into the device's parent
622 * platform data. Then when we probe it, usb_child_pre_probe() is called
623 * and it will pull the information out of the stash.
627 udev->devnum = priv->next_addr + 1;
629 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
630 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
631 dev_get_parent_priv(parent) : NULL;
632 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
633 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
636 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
637 debug("** usb_find_child returns %d\n", ret);
641 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
642 udev->controller_dev->seq,
648 plat = dev_get_parent_platdata(dev);
649 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
650 plat->devnum = udev->devnum;
653 ret = device_probe(dev);
655 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
667 * Detect if a USB device has been plugged or unplugged.
669 int usb_detect_change(void)
676 ret = uclass_get(UCLASS_USB_HUB, &uc);
680 uclass_foreach_dev(hub, uc) {
681 struct usb_device *udev;
684 if (!device_active(hub))
686 for (device_find_first_child(hub, &dev);
688 device_find_next_child(&dev)) {
689 struct usb_port_status status;
691 if (!device_active(dev))
694 udev = dev_get_parent_priv(dev);
695 if (usb_get_port_status(udev, udev->portnr, &status)
697 /* USB request failed */
700 if (le16_to_cpu(status.wPortChange) &
701 USB_PORT_STAT_C_CONNECTION)
709 static int usb_child_post_bind(struct udevice *dev)
711 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
714 if (!dev_of_valid(dev))
717 /* We only support matching a few things */
718 val = dev_read_u32_default(dev, "usb,device-class", -1);
720 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
721 plat->id.bDeviceClass = val;
723 val = dev_read_u32_default(dev, "usb,interface-class", -1);
725 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
726 plat->id.bInterfaceClass = val;
732 struct udevice *usb_get_bus(struct udevice *dev)
736 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
739 /* By design this cannot happen */
741 debug("USB HUB '%s' does not have a controller\n", dev->name);
747 int usb_child_pre_probe(struct udevice *dev)
749 struct usb_device *udev = dev_get_parent_priv(dev);
750 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
755 * Copy over all the values set in the on stack struct
756 * usb_device in usb_scan_device() to our final struct
757 * usb_device for this dev.
759 *udev = *(plat->udev);
760 /* And clear plat->udev as it will not be valid for long */
765 * This happens with devices which are explicitly bound
766 * instead of being discovered through usb_scan_device()
767 * such as sandbox emul devices.
770 udev->controller_dev = usb_get_bus(dev);
771 udev->devnum = plat->devnum;
774 * udev did not go through usb_scan_device(), so we need to
775 * select the config and read the config descriptors.
777 ret = usb_select_config(udev);
785 UCLASS_DRIVER(usb) = {
788 .flags = DM_UC_FLAG_SEQ_ALIAS,
789 .post_bind = dm_scan_fdt_dev,
790 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
791 .per_child_auto_alloc_size = sizeof(struct usb_device),
792 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
793 .child_post_bind = usb_child_post_bind,
794 .child_pre_probe = usb_child_pre_probe,
795 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
798 UCLASS_DRIVER(usb_dev_generic) = {
799 .id = UCLASS_USB_DEV_GENERIC,
800 .name = "usb_dev_generic",
803 U_BOOT_DRIVER(usb_dev_generic_drv) = {
804 .id = UCLASS_USB_DEV_GENERIC,
805 .name = "usb_dev_generic_drv",