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 %d for devices... ", bus->seq);
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;
250 ret = uclass_get(UCLASS_USB, &uc);
256 uclass_foreach_dev(bus, uc) {
257 /* init low_level USB */
258 printf("USB%d: ", count);
261 #ifdef CONFIG_SANDBOX
263 * For Sandbox, we need scan the device tree each time when we
264 * start the USB stack, in order to re-create the emulated USB
265 * devices and bind drivers for them before we actually do the
268 ret = dm_scan_fdt_dev(bus);
270 printf("Sandbox USB device scan failed (%d)\n", ret);
275 ret = device_probe(bus);
276 if (ret == -ENODEV) { /* No such device. */
277 puts("Port not available.\n");
278 controllers_initialized++;
282 if (ret) { /* Other error. */
283 printf("probe failed, error %d\n", ret);
286 controllers_initialized++;
291 * lowlevel init done, now scan the bus for devices i.e. search HUBs
292 * and configure them, first scan primary controllers.
294 uclass_foreach_dev(bus, uc) {
295 if (!device_active(bus))
298 priv = dev_get_uclass_priv(bus);
299 if (!priv->companion)
300 usb_scan_bus(bus, true);
304 * Now that the primary controllers have been scanned and have handed
305 * over any devices they do not understand to their companions, scan
306 * the companions if necessary.
308 if (uc_priv->companion_device_count) {
309 uclass_foreach_dev(bus, uc) {
310 if (!device_active(bus))
313 priv = dev_get_uclass_priv(bus);
315 usb_scan_bus(bus, true);
321 /* Remove any devices that were not found on this scan */
322 remove_inactive_children(uc, bus);
324 ret = uclass_get(UCLASS_USB_HUB, &uc);
327 remove_inactive_children(uc, bus);
329 /* if we were not able to find at least one working bus, bail out */
331 printf("No controllers found\n");
332 else if (controllers_initialized == 0)
333 printf("USB error: all controllers failed lowlevel init\n");
335 return usb_started ? 0 : -1;
339 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
340 * to support boards which use driver model for USB but not Ethernet, and want
341 * to use USB Ethernet.
343 * The #if clause is here to ensure that remains the only case.
345 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
346 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
348 struct usb_device *udev;
351 if (!device_active(parent))
353 udev = dev_get_parent_priv(parent);
354 if (udev->devnum == devnum)
357 for (device_find_first_child(parent, &dev);
359 device_find_next_child(&dev)) {
360 udev = find_child_devnum(dev, devnum);
368 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
371 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
373 device_find_first_child(bus, &dev);
377 return find_child_devnum(dev, devnum);
381 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
383 struct usb_platdata *plat;
387 /* Find the old device and remove it */
388 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
391 ret = device_remove(dev, DM_REMOVE_NORMAL);
395 plat = dev_get_platdata(dev);
396 plat->init_type = USB_INIT_DEVICE;
397 ret = device_probe(dev);
400 *ctlrp = dev_get_priv(dev);
405 /* returns 0 if no match, 1 if match */
406 static int usb_match_device(const struct usb_device_descriptor *desc,
407 const struct usb_device_id *id)
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
410 id->idVendor != le16_to_cpu(desc->idVendor))
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
414 id->idProduct != le16_to_cpu(desc->idProduct))
417 /* No need to test id->bcdDevice_lo != 0, since 0 is never
418 greater than any unsigned number. */
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
420 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
424 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
428 (id->bDeviceClass != desc->bDeviceClass))
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
432 (id->bDeviceSubClass != desc->bDeviceSubClass))
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
436 (id->bDeviceProtocol != desc->bDeviceProtocol))
442 /* returns 0 if no match, 1 if match */
443 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
444 const struct usb_interface_descriptor *int_desc,
445 const struct usb_device_id *id)
447 /* The interface class, subclass, protocol and number should never be
448 * checked for a match if the device class is Vendor Specific,
449 * unless the match record specifies the Vendor ID. */
450 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
451 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
452 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
453 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
454 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
455 USB_DEVICE_ID_MATCH_INT_NUMBER)))
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
459 (id->bInterfaceClass != int_desc->bInterfaceClass))
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
463 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
467 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
470 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
471 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
477 /* returns 0 if no match, 1 if match */
478 static int usb_match_one_id(struct usb_device_descriptor *desc,
479 struct usb_interface_descriptor *int_desc,
480 const struct usb_device_id *id)
482 if (!usb_match_device(desc, id))
485 return usb_match_one_id_intf(desc, int_desc, id);
489 * usb_find_and_bind_driver() - Find and bind the right USB driver
491 * This only looks at certain fields in the descriptor.
493 static int usb_find_and_bind_driver(struct udevice *parent,
494 struct usb_device_descriptor *desc,
495 struct usb_interface_descriptor *iface,
496 int bus_seq, int devnum,
497 struct udevice **devp)
499 struct usb_driver_entry *start, *entry;
505 debug("%s: Searching for driver\n", __func__);
506 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
507 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
508 for (entry = start; entry != start + n_ents; entry++) {
509 const struct usb_device_id *id;
511 const struct driver *drv;
512 struct usb_dev_platdata *plat;
514 for (id = entry->match; id->match_flags; id++) {
515 if (!usb_match_one_id(desc, iface, id))
520 * We could pass the descriptor to the driver as
521 * platdata (instead of NULL) and allow its bind()
522 * method to return -ENOENT if it doesn't support this
523 * device. That way we could continue the search to
524 * find another driver. For now this doesn't seem
525 * necesssary, so just bind the first match.
527 ret = device_bind(parent, drv, drv->name, NULL, -1,
531 debug("%s: Match found: %s\n", __func__, drv->name);
532 dev->driver_data = id->driver_info;
533 plat = dev_get_parent_platdata(dev);
540 /* Bind a generic driver so that the device can be used */
541 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
545 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
548 debug("%s: No match found: %d\n", __func__, ret);
553 * usb_find_child() - Find an existing device which matches our needs
557 static int usb_find_child(struct udevice *parent,
558 struct usb_device_descriptor *desc,
559 struct usb_interface_descriptor *iface,
560 struct udevice **devp)
565 for (device_find_first_child(parent, &dev);
567 device_find_next_child(&dev)) {
568 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
570 /* If this device is already in use, skip it */
571 if (device_active(dev))
573 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
574 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
575 if (usb_match_one_id(desc, iface, &plat->id)) {
584 int usb_scan_device(struct udevice *parent, int port,
585 enum usb_device_speed speed, struct udevice **devp)
588 bool created = false;
589 struct usb_dev_platdata *plat;
590 struct usb_bus_priv *priv;
591 struct usb_device *parent_udev;
593 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
594 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
597 memset(udev, '\0', sizeof(*udev));
598 udev->controller_dev = usb_get_bus(parent);
599 priv = dev_get_uclass_priv(udev->controller_dev);
602 * Somewhat nasty, this. We create a local device and use the normal
603 * USB stack to read its descriptor. Then we know what type of device
604 * to create for real.
606 * udev->dev is set to the parent, since we don't have a real device
607 * yet. The USB stack should not access udev.dev anyway, except perhaps
608 * to find the controller, and the controller will either be @parent,
609 * or some parent of @parent.
611 * Another option might be to create the device as a generic USB
612 * device, then morph it into the correct one when we know what it
613 * should be. This means that a generic USB device would morph into
614 * a network controller, or a USB flash stick, for example. However,
615 * we don't support such morphing and it isn't clear that it would
618 * Yet another option is to split out the USB stack parts of udev
619 * into something like a 'struct urb' (as Linux does) which can exist
620 * independently of any device. This feels cleaner, but calls for quite
621 * a big change to the USB stack.
623 * For now, the approach is to set up an empty udev, read its
624 * descriptor and assign it an address, then bind a real device and
625 * stash the resulting information into the device's parent
626 * platform data. Then when we probe it, usb_child_pre_probe() is called
627 * and it will pull the information out of the stash.
631 udev->devnum = priv->next_addr + 1;
633 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
634 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
635 dev_get_parent_priv(parent) : NULL;
636 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
637 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
640 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
641 debug("** usb_find_child returns %d\n", ret);
645 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
646 udev->controller_dev->seq,
652 plat = dev_get_parent_platdata(dev);
653 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
654 plat->devnum = udev->devnum;
657 ret = device_probe(dev);
659 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
671 * Detect if a USB device has been plugged or unplugged.
673 int usb_detect_change(void)
680 ret = uclass_get(UCLASS_USB_HUB, &uc);
684 uclass_foreach_dev(hub, uc) {
685 struct usb_device *udev;
688 if (!device_active(hub))
690 for (device_find_first_child(hub, &dev);
692 device_find_next_child(&dev)) {
693 struct usb_port_status status;
695 if (!device_active(dev))
698 udev = dev_get_parent_priv(dev);
699 if (usb_get_port_status(udev, udev->portnr, &status)
701 /* USB request failed */
704 if (le16_to_cpu(status.wPortChange) &
705 USB_PORT_STAT_C_CONNECTION)
713 static int usb_child_post_bind(struct udevice *dev)
715 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
718 if (!dev_of_valid(dev))
721 /* We only support matching a few things */
722 val = dev_read_u32_default(dev, "usb,device-class", -1);
724 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
725 plat->id.bDeviceClass = val;
727 val = dev_read_u32_default(dev, "usb,interface-class", -1);
729 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
730 plat->id.bInterfaceClass = val;
736 struct udevice *usb_get_bus(struct udevice *dev)
740 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
743 /* By design this cannot happen */
745 debug("USB HUB '%s' does not have a controller\n", dev->name);
751 int usb_child_pre_probe(struct udevice *dev)
753 struct usb_device *udev = dev_get_parent_priv(dev);
754 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
759 * Copy over all the values set in the on stack struct
760 * usb_device in usb_scan_device() to our final struct
761 * usb_device for this dev.
763 *udev = *(plat->udev);
764 /* And clear plat->udev as it will not be valid for long */
769 * This happens with devices which are explicitly bound
770 * instead of being discovered through usb_scan_device()
771 * such as sandbox emul devices.
774 udev->controller_dev = usb_get_bus(dev);
775 udev->devnum = plat->devnum;
778 * udev did not go through usb_scan_device(), so we need to
779 * select the config and read the config descriptors.
781 ret = usb_select_config(udev);
789 UCLASS_DRIVER(usb) = {
792 .flags = DM_UC_FLAG_SEQ_ALIAS,
793 .post_bind = dm_scan_fdt_dev,
794 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
795 .per_child_auto_alloc_size = sizeof(struct usb_device),
796 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
797 .child_post_bind = usb_child_post_bind,
798 .child_pre_probe = usb_child_pre_probe,
799 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
802 UCLASS_DRIVER(usb_dev_generic) = {
803 .id = UCLASS_USB_DEV_GENERIC,
804 .name = "usb_dev_generic",
807 U_BOOT_DRIVER(usb_dev_generic_drv) = {
808 .id = UCLASS_USB_DEV_GENERIC,
809 .name = "usb_dev_generic_drv",