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, bool nonblock)
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,
46 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
47 void *buffer, int length, struct devrequest *setup)
49 struct udevice *bus = udev->controller_dev;
50 struct dm_usb_ops *ops = usb_get_ops(bus);
51 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
57 err = ops->control(bus, udev, pipe, buffer, length, setup);
58 if (setup->request == USB_REQ_SET_FEATURE &&
59 setup->requesttype == USB_RT_PORT &&
60 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
62 /* Device handed over to companion after port reset */
63 uc_priv->companion_device_count++;
69 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
72 struct udevice *bus = udev->controller_dev;
73 struct dm_usb_ops *ops = usb_get_ops(bus);
78 return ops->bulk(bus, udev, pipe, buffer, length);
81 struct int_queue *create_int_queue(struct usb_device *udev,
82 unsigned long pipe, int queuesize, int elementsize,
83 void *buffer, int interval)
85 struct udevice *bus = udev->controller_dev;
86 struct dm_usb_ops *ops = usb_get_ops(bus);
88 if (!ops->create_int_queue)
91 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
95 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
97 struct udevice *bus = udev->controller_dev;
98 struct dm_usb_ops *ops = usb_get_ops(bus);
100 if (!ops->poll_int_queue)
103 return ops->poll_int_queue(bus, udev, queue);
106 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
108 struct udevice *bus = udev->controller_dev;
109 struct dm_usb_ops *ops = usb_get_ops(bus);
111 if (!ops->destroy_int_queue)
114 return ops->destroy_int_queue(bus, udev, queue);
117 int usb_alloc_device(struct usb_device *udev)
119 struct udevice *bus = udev->controller_dev;
120 struct dm_usb_ops *ops = usb_get_ops(bus);
122 /* This is only requird by some controllers - current XHCI */
123 if (!ops->alloc_device)
126 return ops->alloc_device(bus, udev);
129 int usb_reset_root_port(struct usb_device *udev)
131 struct udevice *bus = udev->controller_dev;
132 struct dm_usb_ops *ops = usb_get_ops(bus);
134 if (!ops->reset_root_port)
137 return ops->reset_root_port(bus, udev);
140 int usb_update_hub_device(struct usb_device *udev)
142 struct udevice *bus = udev->controller_dev;
143 struct dm_usb_ops *ops = usb_get_ops(bus);
145 if (!ops->update_hub_device)
148 return ops->update_hub_device(bus, udev);
151 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
153 struct udevice *bus = udev->controller_dev;
154 struct dm_usb_ops *ops = usb_get_ops(bus);
156 if (!ops->get_max_xfer_size)
159 return ops->get_max_xfer_size(bus, size);
167 struct usb_uclass_priv *uc_priv;
170 /* De-activate any devices that have been activated */
171 ret = uclass_get(UCLASS_USB, &uc);
177 uclass_foreach_dev(bus, uc) {
178 ret = device_remove(bus, DM_REMOVE_NORMAL);
182 /* Locate root hub device */
183 device_find_first_child(bus, &rh);
186 * All USB devices are children of root hub.
187 * Unbinding root hub will unbind all of its children.
189 ret = device_unbind(rh);
195 #ifdef CONFIG_USB_STORAGE
198 uc_priv->companion_device_count = 0;
204 static void usb_scan_bus(struct udevice *bus, bool recurse)
206 struct usb_bus_priv *priv;
210 priv = dev_get_uclass_priv(bus);
212 assert(recurse); /* TODO: Support non-recusive */
214 printf("scanning bus %s for devices... ", bus->name);
216 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
218 printf("failed, error %d\n", ret);
219 else if (priv->next_addr == 0)
220 printf("No USB Device found\n");
222 printf("%d USB Device(s) found\n", priv->next_addr);
225 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
227 uclass_foreach_dev(bus, uc) {
228 struct udevice *dev, *next;
230 if (!device_active(bus))
232 device_foreach_child_safe(dev, next, bus) {
233 if (!device_active(dev))
241 int controllers_initialized = 0;
242 struct usb_uclass_priv *uc_priv;
243 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("Bus %s: ", bus->name);
260 #ifdef CONFIG_SANDBOX
262 * For Sandbox, we need scan the device tree each time when we
263 * start the USB stack, in order to re-create the emulated USB
264 * devices and bind drivers for them before we actually do the
267 ret = dm_scan_fdt_dev(bus);
269 printf("Sandbox USB device scan failed (%d)\n", ret);
274 ret = device_probe(bus);
275 if (ret == -ENODEV) { /* No such device. */
276 puts("Port not available.\n");
277 controllers_initialized++;
281 if (ret) { /* Other error. */
282 printf("probe failed, error %d\n", ret);
285 controllers_initialized++;
290 * lowlevel init done, now scan the bus for devices i.e. search HUBs
291 * and configure them, first scan primary controllers.
293 uclass_foreach_dev(bus, uc) {
294 if (!device_active(bus))
297 priv = dev_get_uclass_priv(bus);
298 if (!priv->companion)
299 usb_scan_bus(bus, true);
303 * Now that the primary controllers have been scanned and have handed
304 * over any devices they do not understand to their companions, scan
305 * the companions if necessary.
307 if (uc_priv->companion_device_count) {
308 uclass_foreach_dev(bus, uc) {
309 if (!device_active(bus))
312 priv = dev_get_uclass_priv(bus);
314 usb_scan_bus(bus, true);
320 /* Remove any devices that were not found on this scan */
321 remove_inactive_children(uc, bus);
323 ret = uclass_get(UCLASS_USB_HUB, &uc);
326 remove_inactive_children(uc, bus);
328 /* if we were not able to find at least one working bus, bail out */
329 if (controllers_initialized == 0)
330 printf("No working controllers found\n");
332 return usb_started ? 0 : -1;
336 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
337 * to support boards which use driver model for USB but not Ethernet, and want
338 * to use USB Ethernet.
340 * The #if clause is here to ensure that remains the only case.
342 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
343 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
345 struct usb_device *udev;
348 if (!device_active(parent))
350 udev = dev_get_parent_priv(parent);
351 if (udev->devnum == devnum)
354 for (device_find_first_child(parent, &dev);
356 device_find_next_child(&dev)) {
357 udev = find_child_devnum(dev, devnum);
365 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
368 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
370 device_find_first_child(bus, &dev);
374 return find_child_devnum(dev, devnum);
378 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
380 struct usb_platdata *plat;
384 /* Find the old device and remove it */
385 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
388 ret = device_remove(dev, DM_REMOVE_NORMAL);
392 plat = dev_get_platdata(dev);
393 plat->init_type = USB_INIT_DEVICE;
394 ret = device_probe(dev);
397 *ctlrp = dev_get_priv(dev);
402 /* returns 0 if no match, 1 if match */
403 static int usb_match_device(const struct usb_device_descriptor *desc,
404 const struct usb_device_id *id)
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
407 id->idVendor != le16_to_cpu(desc->idVendor))
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
411 id->idProduct != le16_to_cpu(desc->idProduct))
414 /* No need to test id->bcdDevice_lo != 0, since 0 is never
415 greater than any unsigned number. */
416 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
417 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
421 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
425 (id->bDeviceClass != desc->bDeviceClass))
428 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
429 (id->bDeviceSubClass != desc->bDeviceSubClass))
432 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
433 (id->bDeviceProtocol != desc->bDeviceProtocol))
439 /* returns 0 if no match, 1 if match */
440 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
441 const struct usb_interface_descriptor *int_desc,
442 const struct usb_device_id *id)
444 /* The interface class, subclass, protocol and number should never be
445 * checked for a match if the device class is Vendor Specific,
446 * unless the match record specifies the Vendor ID. */
447 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
448 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
449 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
450 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
451 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
452 USB_DEVICE_ID_MATCH_INT_NUMBER)))
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
456 (id->bInterfaceClass != int_desc->bInterfaceClass))
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
460 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
463 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
464 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
467 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
468 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
474 /* returns 0 if no match, 1 if match */
475 static int usb_match_one_id(struct usb_device_descriptor *desc,
476 struct usb_interface_descriptor *int_desc,
477 const struct usb_device_id *id)
479 if (!usb_match_device(desc, id))
482 return usb_match_one_id_intf(desc, int_desc, id);
486 * usb_find_and_bind_driver() - Find and bind the right USB driver
488 * This only looks at certain fields in the descriptor.
490 static int usb_find_and_bind_driver(struct udevice *parent,
491 struct usb_device_descriptor *desc,
492 struct usb_interface_descriptor *iface,
493 int bus_seq, int devnum,
494 struct udevice **devp)
496 struct usb_driver_entry *start, *entry;
502 debug("%s: Searching for driver\n", __func__);
503 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
504 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
505 for (entry = start; entry != start + n_ents; entry++) {
506 const struct usb_device_id *id;
508 const struct driver *drv;
509 struct usb_dev_platdata *plat;
511 for (id = entry->match; id->match_flags; id++) {
512 if (!usb_match_one_id(desc, iface, id))
517 * We could pass the descriptor to the driver as
518 * platdata (instead of NULL) and allow its bind()
519 * method to return -ENOENT if it doesn't support this
520 * device. That way we could continue the search to
521 * find another driver. For now this doesn't seem
522 * necesssary, so just bind the first match.
524 ret = device_bind(parent, drv, drv->name, NULL, -1,
528 debug("%s: Match found: %s\n", __func__, drv->name);
529 dev->driver_data = id->driver_info;
530 plat = dev_get_parent_platdata(dev);
537 /* Bind a generic driver so that the device can be used */
538 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
542 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
545 debug("%s: No match found: %d\n", __func__, ret);
550 * usb_find_child() - Find an existing device which matches our needs
554 static int usb_find_child(struct udevice *parent,
555 struct usb_device_descriptor *desc,
556 struct usb_interface_descriptor *iface,
557 struct udevice **devp)
562 for (device_find_first_child(parent, &dev);
564 device_find_next_child(&dev)) {
565 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
567 /* If this device is already in use, skip it */
568 if (device_active(dev))
570 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
571 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
572 if (usb_match_one_id(desc, iface, &plat->id)) {
581 int usb_scan_device(struct udevice *parent, int port,
582 enum usb_device_speed speed, struct udevice **devp)
585 bool created = false;
586 struct usb_dev_platdata *plat;
587 struct usb_bus_priv *priv;
588 struct usb_device *parent_udev;
590 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
591 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
594 memset(udev, '\0', sizeof(*udev));
595 udev->controller_dev = usb_get_bus(parent);
596 priv = dev_get_uclass_priv(udev->controller_dev);
599 * Somewhat nasty, this. We create a local device and use the normal
600 * USB stack to read its descriptor. Then we know what type of device
601 * to create for real.
603 * udev->dev is set to the parent, since we don't have a real device
604 * yet. The USB stack should not access udev.dev anyway, except perhaps
605 * to find the controller, and the controller will either be @parent,
606 * or some parent of @parent.
608 * Another option might be to create the device as a generic USB
609 * device, then morph it into the correct one when we know what it
610 * should be. This means that a generic USB device would morph into
611 * a network controller, or a USB flash stick, for example. However,
612 * we don't support such morphing and it isn't clear that it would
615 * Yet another option is to split out the USB stack parts of udev
616 * into something like a 'struct urb' (as Linux does) which can exist
617 * independently of any device. This feels cleaner, but calls for quite
618 * a big change to the USB stack.
620 * For now, the approach is to set up an empty udev, read its
621 * descriptor and assign it an address, then bind a real device and
622 * stash the resulting information into the device's parent
623 * platform data. Then when we probe it, usb_child_pre_probe() is called
624 * and it will pull the information out of the stash.
628 udev->devnum = priv->next_addr + 1;
630 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
631 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
632 dev_get_parent_priv(parent) : NULL;
633 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
634 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
637 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
638 debug("** usb_find_child returns %d\n", ret);
642 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
643 udev->controller_dev->seq,
649 plat = dev_get_parent_platdata(dev);
650 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
651 plat->devnum = udev->devnum;
654 ret = device_probe(dev);
656 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
668 * Detect if a USB device has been plugged or unplugged.
670 int usb_detect_change(void)
677 ret = uclass_get(UCLASS_USB_HUB, &uc);
681 uclass_foreach_dev(hub, uc) {
682 struct usb_device *udev;
685 if (!device_active(hub))
687 for (device_find_first_child(hub, &dev);
689 device_find_next_child(&dev)) {
690 struct usb_port_status status;
692 if (!device_active(dev))
695 udev = dev_get_parent_priv(dev);
696 if (usb_get_port_status(udev, udev->portnr, &status)
698 /* USB request failed */
701 if (le16_to_cpu(status.wPortChange) &
702 USB_PORT_STAT_C_CONNECTION)
710 static int usb_child_post_bind(struct udevice *dev)
712 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
715 if (!dev_of_valid(dev))
718 /* We only support matching a few things */
719 val = dev_read_u32_default(dev, "usb,device-class", -1);
721 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
722 plat->id.bDeviceClass = val;
724 val = dev_read_u32_default(dev, "usb,interface-class", -1);
726 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
727 plat->id.bInterfaceClass = val;
733 struct udevice *usb_get_bus(struct udevice *dev)
737 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
740 /* By design this cannot happen */
742 debug("USB HUB '%s' does not have a controller\n", dev->name);
748 int usb_child_pre_probe(struct udevice *dev)
750 struct usb_device *udev = dev_get_parent_priv(dev);
751 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
756 * Copy over all the values set in the on stack struct
757 * usb_device in usb_scan_device() to our final struct
758 * usb_device for this dev.
760 *udev = *(plat->udev);
761 /* And clear plat->udev as it will not be valid for long */
766 * This happens with devices which are explicitly bound
767 * instead of being discovered through usb_scan_device()
768 * such as sandbox emul devices.
771 udev->controller_dev = usb_get_bus(dev);
772 udev->devnum = plat->devnum;
775 * udev did not go through usb_scan_device(), so we need to
776 * select the config and read the config descriptors.
778 ret = usb_select_config(udev);
786 UCLASS_DRIVER(usb) = {
789 .flags = DM_UC_FLAG_SEQ_ALIAS,
790 .post_bind = dm_scan_fdt_dev,
791 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
792 .per_child_auto_alloc_size = sizeof(struct usb_device),
793 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
794 .child_post_bind = usb_child_post_bind,
795 .child_pre_probe = usb_child_pre_probe,
796 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
799 UCLASS_DRIVER(usb_dev_generic) = {
800 .id = UCLASS_USB_DEV_GENERIC,
801 .name = "usb_dev_generic",
804 U_BOOT_DRIVER(usb_dev_generic_drv) = {
805 .id = UCLASS_USB_DEV_GENERIC,
806 .name = "usb_dev_generic_drv",