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.
9 #define LOG_CATEGORY UCLASS_USB
17 #include <dm/device-internal.h>
19 #include <dm/uclass-internal.h>
21 static bool asynch_allowed;
23 struct usb_uclass_priv {
24 int companion_device_count;
27 int usb_lock_async(struct usb_device *udev, int lock)
29 struct udevice *bus = udev->controller_dev;
30 struct dm_usb_ops *ops = usb_get_ops(bus);
35 return ops->lock_async(bus, lock);
38 int usb_disable_asynch(int disable)
40 int old_value = asynch_allowed;
42 asynch_allowed = !disable;
46 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
47 int length, int interval, bool nonblock)
49 struct udevice *bus = udev->controller_dev;
50 struct dm_usb_ops *ops = usb_get_ops(bus);
55 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
59 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
60 void *buffer, int length, struct devrequest *setup)
62 struct udevice *bus = udev->controller_dev;
63 struct dm_usb_ops *ops = usb_get_ops(bus);
64 struct usb_uclass_priv *uc_priv = uclass_get_priv(bus->uclass);
70 err = ops->control(bus, udev, pipe, buffer, length, setup);
71 if (setup->request == USB_REQ_SET_FEATURE &&
72 setup->requesttype == USB_RT_PORT &&
73 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
75 /* Device handed over to companion after port reset */
76 uc_priv->companion_device_count++;
82 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
85 struct udevice *bus = udev->controller_dev;
86 struct dm_usb_ops *ops = usb_get_ops(bus);
91 return ops->bulk(bus, udev, pipe, buffer, length);
94 struct int_queue *create_int_queue(struct usb_device *udev,
95 unsigned long pipe, int queuesize, int elementsize,
96 void *buffer, int interval)
98 struct udevice *bus = udev->controller_dev;
99 struct dm_usb_ops *ops = usb_get_ops(bus);
101 if (!ops->create_int_queue)
104 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
108 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
110 struct udevice *bus = udev->controller_dev;
111 struct dm_usb_ops *ops = usb_get_ops(bus);
113 if (!ops->poll_int_queue)
116 return ops->poll_int_queue(bus, udev, queue);
119 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
121 struct udevice *bus = udev->controller_dev;
122 struct dm_usb_ops *ops = usb_get_ops(bus);
124 if (!ops->destroy_int_queue)
127 return ops->destroy_int_queue(bus, udev, queue);
130 int usb_alloc_device(struct usb_device *udev)
132 struct udevice *bus = udev->controller_dev;
133 struct dm_usb_ops *ops = usb_get_ops(bus);
135 /* This is only requird by some controllers - current XHCI */
136 if (!ops->alloc_device)
139 return ops->alloc_device(bus, udev);
142 int usb_reset_root_port(struct usb_device *udev)
144 struct udevice *bus = udev->controller_dev;
145 struct dm_usb_ops *ops = usb_get_ops(bus);
147 if (!ops->reset_root_port)
150 return ops->reset_root_port(bus, udev);
153 int usb_update_hub_device(struct usb_device *udev)
155 struct udevice *bus = udev->controller_dev;
156 struct dm_usb_ops *ops = usb_get_ops(bus);
158 if (!ops->update_hub_device)
161 return ops->update_hub_device(bus, udev);
164 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
166 struct udevice *bus = udev->controller_dev;
167 struct dm_usb_ops *ops = usb_get_ops(bus);
169 if (!ops->get_max_xfer_size)
172 return ops->get_max_xfer_size(bus, size);
180 struct usb_uclass_priv *uc_priv;
183 /* De-activate any devices that have been activated */
184 ret = uclass_get(UCLASS_USB, &uc);
188 uc_priv = uclass_get_priv(uc);
190 uclass_foreach_dev(bus, uc) {
191 ret = device_remove(bus, DM_REMOVE_NORMAL);
195 /* Locate root hub device */
196 device_find_first_child(bus, &rh);
199 * All USB devices are children of root hub.
200 * Unbinding root hub will unbind all of its children.
202 ret = device_unbind(rh);
208 #ifdef CONFIG_USB_STORAGE
211 uc_priv->companion_device_count = 0;
217 static void usb_scan_bus(struct udevice *bus, bool recurse)
219 struct usb_bus_priv *priv;
223 priv = dev_get_uclass_priv(bus);
225 assert(recurse); /* TODO: Support non-recusive */
227 printf("scanning bus %s for devices... ", bus->name);
229 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
231 printf("failed, error %d\n", ret);
232 else if (priv->next_addr == 0)
233 printf("No USB Device found\n");
235 printf("%d USB Device(s) found\n", priv->next_addr);
238 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
240 uclass_foreach_dev(bus, uc) {
241 struct udevice *dev, *next;
243 if (!device_active(bus))
245 device_foreach_child_safe(dev, next, bus) {
246 if (!device_active(dev))
254 int controllers_initialized = 0;
255 struct usb_uclass_priv *uc_priv;
256 struct usb_bus_priv *priv;
263 ret = uclass_get(UCLASS_USB, &uc);
267 uc_priv = uclass_get_priv(uc);
269 uclass_foreach_dev(bus, uc) {
270 /* init low_level USB */
271 printf("Bus %s: ", bus->name);
274 * For Sandbox, we need scan the device tree each time when we
275 * start the USB stack, in order to re-create the emulated USB
276 * devices and bind drivers for them before we actually do the
279 * For USB onboard HUB, we need to do some non-trivial init
280 * like enabling a power regulator, before enumeration.
282 if (IS_ENABLED(CONFIG_SANDBOX) ||
283 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
284 ret = dm_scan_fdt_dev(bus);
286 printf("USB device scan from fdt failed (%d)", ret);
291 ret = device_probe(bus);
292 if (ret == -ENODEV) { /* No such device. */
293 puts("Port not available.\n");
294 controllers_initialized++;
298 if (ret) { /* Other error. */
299 printf("probe failed, error %d\n", ret);
302 controllers_initialized++;
307 * lowlevel init done, now scan the bus for devices i.e. search HUBs
308 * and configure them, first scan primary controllers.
310 uclass_foreach_dev(bus, uc) {
311 if (!device_active(bus))
314 priv = dev_get_uclass_priv(bus);
315 if (!priv->companion)
316 usb_scan_bus(bus, true);
320 * Now that the primary controllers have been scanned and have handed
321 * over any devices they do not understand to their companions, scan
322 * the companions if necessary.
324 if (uc_priv->companion_device_count) {
325 uclass_foreach_dev(bus, uc) {
326 if (!device_active(bus))
329 priv = dev_get_uclass_priv(bus);
331 usb_scan_bus(bus, true);
337 /* Remove any devices that were not found on this scan */
338 remove_inactive_children(uc, bus);
340 ret = uclass_get(UCLASS_USB_HUB, &uc);
343 remove_inactive_children(uc, bus);
345 /* if we were not able to find at least one working bus, bail out */
346 if (controllers_initialized == 0)
347 printf("No working controllers found\n");
349 return usb_started ? 0 : -ENOENT;
352 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
354 struct usb_plat *plat;
358 /* Find the old device and remove it */
359 ret = uclass_find_first_device(UCLASS_USB, &dev);
362 ret = device_remove(dev, DM_REMOVE_NORMAL);
366 plat = dev_get_plat(dev);
367 plat->init_type = USB_INIT_DEVICE;
368 ret = device_probe(dev);
371 *ctlrp = dev_get_priv(dev);
376 int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
381 /* Find the old device and remove it */
382 ret = uclass_find_first_device(UCLASS_USB, &dev);
385 ret = device_remove(dev, DM_REMOVE_NORMAL);
394 /* returns 0 if no match, 1 if match */
395 static int usb_match_device(const struct usb_device_descriptor *desc,
396 const struct usb_device_id *id)
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
399 id->idVendor != desc->idVendor)
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
403 id->idProduct != desc->idProduct)
406 /* No need to test id->bcdDevice_lo != 0, since 0 is never
407 greater than any unsigned number. */
408 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
409 (id->bcdDevice_lo > desc->bcdDevice))
412 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
413 (id->bcdDevice_hi < desc->bcdDevice))
416 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
417 (id->bDeviceClass != desc->bDeviceClass))
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
421 (id->bDeviceSubClass != desc->bDeviceSubClass))
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
425 (id->bDeviceProtocol != desc->bDeviceProtocol))
431 /* returns 0 if no match, 1 if match */
432 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
433 const struct usb_interface_descriptor *int_desc,
434 const struct usb_device_id *id)
436 /* The interface class, subclass, protocol and number should never be
437 * checked for a match if the device class is Vendor Specific,
438 * unless the match record specifies the Vendor ID. */
439 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
440 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
441 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
442 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
443 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
444 USB_DEVICE_ID_MATCH_INT_NUMBER)))
447 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
448 (id->bInterfaceClass != int_desc->bInterfaceClass))
451 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
452 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
456 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
460 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
466 /* returns 0 if no match, 1 if match */
467 static int usb_match_one_id(struct usb_device_descriptor *desc,
468 struct usb_interface_descriptor *int_desc,
469 const struct usb_device_id *id)
471 if (!usb_match_device(desc, id))
474 return usb_match_one_id_intf(desc, int_desc, id);
477 static ofnode usb_get_ofnode(struct udevice *hub, int port)
482 if (!dev_has_ofnode(hub))
483 return ofnode_null();
486 * The USB controller and its USB hub are two different udevices,
487 * but the device tree has only one node for both. Thus we are
488 * assigning this node to both udevices.
489 * If port is zero, the controller scans its root hub, thus we
490 * are using the same ofnode as the controller here.
493 return dev_ofnode(hub);
495 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
496 if (ofnode_read_u32(node, "reg", ®))
503 return ofnode_null();
507 * usb_find_and_bind_driver() - Find and bind the right USB driver
509 * This only looks at certain fields in the descriptor.
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, int port,
515 struct udevice **devp)
517 struct usb_driver_entry *start, *entry;
521 ofnode node = usb_get_ofnode(parent, port);
524 debug("%s: Searching for driver\n", __func__);
525 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
526 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
527 for (entry = start; entry != start + n_ents; entry++) {
528 const struct usb_device_id *id;
530 const struct driver *drv;
531 struct usb_dev_plat *plat;
533 for (id = entry->match; id->match_flags; id++) {
534 if (!usb_match_one_id(desc, iface, id))
539 * We could pass the descriptor to the driver as
540 * plat (instead of NULL) and allow its bind()
541 * method to return -ENOENT if it doesn't support this
542 * device. That way we could continue the search to
543 * find another driver. For now this doesn't seem
544 * necesssary, so just bind the first match.
546 ret = device_bind(parent, drv, drv->name, NULL, node,
550 debug("%s: Match found: %s\n", __func__, drv->name);
551 dev->driver_data = id->driver_info;
552 plat = dev_get_parent_plat(dev);
559 /* Bind a generic driver so that the device can be used */
560 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
564 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
566 device_set_name_alloced(*devp);
569 debug("%s: No match found: %d\n", __func__, ret);
574 * usb_find_child() - Find an existing device which matches our needs
578 static int usb_find_child(struct udevice *parent,
579 struct usb_device_descriptor *desc,
580 struct usb_interface_descriptor *iface,
581 struct udevice **devp)
586 for (device_find_first_child(parent, &dev);
588 device_find_next_child(&dev)) {
589 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
591 /* If this device is already in use, skip it */
592 if (device_active(dev))
594 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
595 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
596 if (usb_match_one_id(desc, iface, &plat->id)) {
605 int usb_scan_device(struct udevice *parent, int port,
606 enum usb_device_speed speed, struct udevice **devp)
609 bool created = false;
610 struct usb_dev_plat *plat;
611 struct usb_bus_priv *priv;
612 struct usb_device *parent_udev;
614 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
615 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
618 memset(udev, '\0', sizeof(*udev));
619 udev->controller_dev = usb_get_bus(parent);
620 priv = dev_get_uclass_priv(udev->controller_dev);
623 * Somewhat nasty, this. We create a local device and use the normal
624 * USB stack to read its descriptor. Then we know what type of device
625 * to create for real.
627 * udev->dev is set to the parent, since we don't have a real device
628 * yet. The USB stack should not access udev.dev anyway, except perhaps
629 * to find the controller, and the controller will either be @parent,
630 * or some parent of @parent.
632 * Another option might be to create the device as a generic USB
633 * device, then morph it into the correct one when we know what it
634 * should be. This means that a generic USB device would morph into
635 * a network controller, or a USB flash stick, for example. However,
636 * we don't support such morphing and it isn't clear that it would
639 * Yet another option is to split out the USB stack parts of udev
640 * into something like a 'struct urb' (as Linux does) which can exist
641 * independently of any device. This feels cleaner, but calls for quite
642 * a big change to the USB stack.
644 * For now, the approach is to set up an empty udev, read its
645 * descriptor and assign it an address, then bind a real device and
646 * stash the resulting information into the device's parent
647 * platform data. Then when we probe it, usb_child_pre_probe() is called
648 * and it will pull the information out of the stash.
652 udev->devnum = priv->next_addr + 1;
654 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
655 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
656 dev_get_parent_priv(parent) : NULL;
657 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
658 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
661 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
662 debug("** usb_find_child returns %d\n", ret);
666 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
668 dev_seq(udev->controller_dev),
669 udev->devnum, port, &dev);
674 plat = dev_get_parent_plat(dev);
675 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
676 plat->devnum = udev->devnum;
679 ret = device_probe(dev);
681 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
693 * Detect if a USB device has been plugged or unplugged.
695 int usb_detect_change(void)
702 ret = uclass_get(UCLASS_USB_HUB, &uc);
706 uclass_foreach_dev(hub, uc) {
707 struct usb_device *udev;
710 if (!device_active(hub))
712 for (device_find_first_child(hub, &dev);
714 device_find_next_child(&dev)) {
715 struct usb_port_status status;
717 if (!device_active(dev))
720 udev = dev_get_parent_priv(dev);
721 if (usb_get_port_status(udev, udev->portnr, &status)
723 /* USB request failed */
726 if (le16_to_cpu(status.wPortChange) &
727 USB_PORT_STAT_C_CONNECTION)
735 static int usb_child_post_bind(struct udevice *dev)
737 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
740 if (!dev_has_ofnode(dev))
743 /* We only support matching a few things */
744 val = dev_read_u32_default(dev, "usb,device-class", -1);
746 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
747 plat->id.bDeviceClass = val;
749 val = dev_read_u32_default(dev, "usb,interface-class", -1);
751 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
752 plat->id.bInterfaceClass = val;
758 struct udevice *usb_get_bus(struct udevice *dev)
762 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
765 /* By design this cannot happen */
767 debug("USB HUB '%s' does not have a controller\n", dev->name);
773 int usb_child_pre_probe(struct udevice *dev)
775 struct usb_device *udev = dev_get_parent_priv(dev);
776 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
781 * Copy over all the values set in the on stack struct
782 * usb_device in usb_scan_device() to our final struct
783 * usb_device for this dev.
785 *udev = *(plat->udev);
786 /* And clear plat->udev as it will not be valid for long */
791 * This happens with devices which are explicitly bound
792 * instead of being discovered through usb_scan_device()
793 * such as sandbox emul devices.
796 udev->controller_dev = usb_get_bus(dev);
797 udev->devnum = plat->devnum;
800 * udev did not go through usb_scan_device(), so we need to
801 * select the config and read the config descriptors.
803 ret = usb_select_config(udev);
811 UCLASS_DRIVER(usb) = {
814 .flags = DM_UC_FLAG_SEQ_ALIAS,
815 .post_bind = dm_scan_fdt_dev,
816 .priv_auto = sizeof(struct usb_uclass_priv),
817 .per_child_auto = sizeof(struct usb_device),
818 .per_device_auto = sizeof(struct usb_bus_priv),
819 .child_post_bind = usb_child_post_bind,
820 .child_pre_probe = usb_child_pre_probe,
821 .per_child_plat_auto = sizeof(struct usb_dev_plat),
824 UCLASS_DRIVER(usb_dev_generic) = {
825 .id = UCLASS_USB_DEV_GENERIC,
826 .name = "usb_dev_generic",
829 U_BOOT_DRIVER(usb_dev_generic_drv) = {
830 .id = UCLASS_USB_DEV_GENERIC,
831 .name = "usb_dev_generic_drv",