1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Google, Inc
6 * usb_match_device() modified from Linux kernel v4.0.
15 #include <dm/device-internal.h>
17 #include <dm/uclass-internal.h>
19 extern bool usb_started; /* flag for the started/stopped USB status */
20 static bool asynch_allowed;
22 struct usb_uclass_priv {
23 int companion_device_count;
26 int usb_lock_async(struct usb_device *udev, int lock)
28 struct udevice *bus = udev->controller_dev;
29 struct dm_usb_ops *ops = usb_get_ops(bus);
34 return ops->lock_async(bus, lock);
37 int usb_disable_asynch(int disable)
39 int old_value = asynch_allowed;
41 asynch_allowed = !disable;
45 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
46 int length, int interval, bool nonblock)
48 struct udevice *bus = udev->controller_dev;
49 struct dm_usb_ops *ops = usb_get_ops(bus);
54 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
58 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
59 void *buffer, int length, struct devrequest *setup)
61 struct udevice *bus = udev->controller_dev;
62 struct dm_usb_ops *ops = usb_get_ops(bus);
63 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
69 err = ops->control(bus, udev, pipe, buffer, length, setup);
70 if (setup->request == USB_REQ_SET_FEATURE &&
71 setup->requesttype == USB_RT_PORT &&
72 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
74 /* Device handed over to companion after port reset */
75 uc_priv->companion_device_count++;
81 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
84 struct udevice *bus = udev->controller_dev;
85 struct dm_usb_ops *ops = usb_get_ops(bus);
90 return ops->bulk(bus, udev, pipe, buffer, length);
93 struct int_queue *create_int_queue(struct usb_device *udev,
94 unsigned long pipe, int queuesize, int elementsize,
95 void *buffer, int interval)
97 struct udevice *bus = udev->controller_dev;
98 struct dm_usb_ops *ops = usb_get_ops(bus);
100 if (!ops->create_int_queue)
103 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
107 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
109 struct udevice *bus = udev->controller_dev;
110 struct dm_usb_ops *ops = usb_get_ops(bus);
112 if (!ops->poll_int_queue)
115 return ops->poll_int_queue(bus, udev, queue);
118 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
120 struct udevice *bus = udev->controller_dev;
121 struct dm_usb_ops *ops = usb_get_ops(bus);
123 if (!ops->destroy_int_queue)
126 return ops->destroy_int_queue(bus, udev, queue);
129 int usb_alloc_device(struct usb_device *udev)
131 struct udevice *bus = udev->controller_dev;
132 struct dm_usb_ops *ops = usb_get_ops(bus);
134 /* This is only requird by some controllers - current XHCI */
135 if (!ops->alloc_device)
138 return ops->alloc_device(bus, udev);
141 int usb_reset_root_port(struct usb_device *udev)
143 struct udevice *bus = udev->controller_dev;
144 struct dm_usb_ops *ops = usb_get_ops(bus);
146 if (!ops->reset_root_port)
149 return ops->reset_root_port(bus, udev);
152 int usb_update_hub_device(struct usb_device *udev)
154 struct udevice *bus = udev->controller_dev;
155 struct dm_usb_ops *ops = usb_get_ops(bus);
157 if (!ops->update_hub_device)
160 return ops->update_hub_device(bus, udev);
163 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
165 struct udevice *bus = udev->controller_dev;
166 struct dm_usb_ops *ops = usb_get_ops(bus);
168 if (!ops->get_max_xfer_size)
171 return ops->get_max_xfer_size(bus, size);
179 struct usb_uclass_priv *uc_priv;
182 /* De-activate any devices that have been activated */
183 ret = uclass_get(UCLASS_USB, &uc);
189 uclass_foreach_dev(bus, uc) {
190 ret = device_remove(bus, DM_REMOVE_NORMAL);
194 /* Locate root hub device */
195 device_find_first_child(bus, &rh);
198 * All USB devices are children of root hub.
199 * Unbinding root hub will unbind all of its children.
201 ret = device_unbind(rh);
207 #ifdef CONFIG_USB_STORAGE
210 uc_priv->companion_device_count = 0;
216 static void usb_scan_bus(struct udevice *bus, bool recurse)
218 struct usb_bus_priv *priv;
222 priv = dev_get_uclass_priv(bus);
224 assert(recurse); /* TODO: Support non-recusive */
226 printf("scanning bus %s for devices... ", bus->name);
228 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
230 printf("failed, error %d\n", ret);
231 else if (priv->next_addr == 0)
232 printf("No USB Device found\n");
234 printf("%d USB Device(s) found\n", priv->next_addr);
237 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
239 uclass_foreach_dev(bus, uc) {
240 struct udevice *dev, *next;
242 if (!device_active(bus))
244 device_foreach_child_safe(dev, next, bus) {
245 if (!device_active(dev))
253 int controllers_initialized = 0;
254 struct usb_uclass_priv *uc_priv;
255 struct usb_bus_priv *priv;
262 ret = uclass_get(UCLASS_USB, &uc);
268 uclass_foreach_dev(bus, uc) {
269 /* init low_level USB */
270 printf("Bus %s: ", bus->name);
272 #ifdef CONFIG_SANDBOX
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 ret = dm_scan_fdt_dev(bus);
281 printf("Sandbox USB device scan failed (%d)\n", ret);
286 ret = device_probe(bus);
287 if (ret == -ENODEV) { /* No such device. */
288 puts("Port not available.\n");
289 controllers_initialized++;
293 if (ret) { /* Other error. */
294 printf("probe failed, error %d\n", ret);
297 controllers_initialized++;
302 * lowlevel init done, now scan the bus for devices i.e. search HUBs
303 * and configure them, first scan primary controllers.
305 uclass_foreach_dev(bus, uc) {
306 if (!device_active(bus))
309 priv = dev_get_uclass_priv(bus);
310 if (!priv->companion)
311 usb_scan_bus(bus, true);
315 * Now that the primary controllers have been scanned and have handed
316 * over any devices they do not understand to their companions, scan
317 * the companions if necessary.
319 if (uc_priv->companion_device_count) {
320 uclass_foreach_dev(bus, uc) {
321 if (!device_active(bus))
324 priv = dev_get_uclass_priv(bus);
326 usb_scan_bus(bus, true);
332 /* Remove any devices that were not found on this scan */
333 remove_inactive_children(uc, bus);
335 ret = uclass_get(UCLASS_USB_HUB, &uc);
338 remove_inactive_children(uc, bus);
340 /* if we were not able to find at least one working bus, bail out */
341 if (controllers_initialized == 0)
342 printf("No working controllers found\n");
344 return usb_started ? 0 : -1;
349 * to support boards which use driver model for USB but not Ethernet, and want
350 * to use USB Ethernet.
352 * The #if clause is here to ensure that remains the only case.
354 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
355 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
357 struct usb_device *udev;
360 if (!device_active(parent))
362 udev = dev_get_parent_priv(parent);
363 if (udev->devnum == devnum)
366 for (device_find_first_child(parent, &dev);
368 device_find_next_child(&dev)) {
369 udev = find_child_devnum(dev, devnum);
377 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
380 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
382 device_find_first_child(bus, &dev);
386 return find_child_devnum(dev, devnum);
390 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
392 struct usb_plat *plat;
396 /* Find the old device and remove it */
397 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
400 ret = device_remove(dev, DM_REMOVE_NORMAL);
404 plat = dev_get_plat(dev);
405 plat->init_type = USB_INIT_DEVICE;
406 ret = device_probe(dev);
409 *ctlrp = dev_get_priv(dev);
414 int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
419 /* Find the old device and remove it */
420 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
423 ret = device_remove(dev, DM_REMOVE_NORMAL);
432 /* returns 0 if no match, 1 if match */
433 static int usb_match_device(const struct usb_device_descriptor *desc,
434 const struct usb_device_id *id)
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
437 id->idVendor != desc->idVendor)
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
441 id->idProduct != desc->idProduct)
444 /* No need to test id->bcdDevice_lo != 0, since 0 is never
445 greater than any unsigned number. */
446 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
447 (id->bcdDevice_lo > desc->bcdDevice))
450 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
451 (id->bcdDevice_hi < desc->bcdDevice))
454 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
455 (id->bDeviceClass != desc->bDeviceClass))
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
459 (id->bDeviceSubClass != desc->bDeviceSubClass))
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
463 (id->bDeviceProtocol != desc->bDeviceProtocol))
469 /* returns 0 if no match, 1 if match */
470 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
471 const struct usb_interface_descriptor *int_desc,
472 const struct usb_device_id *id)
474 /* The interface class, subclass, protocol and number should never be
475 * checked for a match if the device class is Vendor Specific,
476 * unless the match record specifies the Vendor ID. */
477 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
478 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
479 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
480 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
481 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
482 USB_DEVICE_ID_MATCH_INT_NUMBER)))
485 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
486 (id->bInterfaceClass != int_desc->bInterfaceClass))
489 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
490 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
493 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
494 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
497 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
498 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
504 /* returns 0 if no match, 1 if match */
505 static int usb_match_one_id(struct usb_device_descriptor *desc,
506 struct usb_interface_descriptor *int_desc,
507 const struct usb_device_id *id)
509 if (!usb_match_device(desc, id))
512 return usb_match_one_id_intf(desc, int_desc, id);
515 static ofnode usb_get_ofnode(struct udevice *hub, int port)
520 if (!dev_has_of_node(hub))
521 return ofnode_null();
524 * The USB controller and its USB hub are two different udevices,
525 * but the device tree has only one node for both. Thus we are
526 * assigning this node to both udevices.
527 * If port is zero, the controller scans its root hub, thus we
528 * are using the same ofnode as the controller here.
531 return dev_ofnode(hub);
533 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
534 if (ofnode_read_u32(node, "reg", ®))
541 return ofnode_null();
545 * usb_find_and_bind_driver() - Find and bind the right USB driver
547 * This only looks at certain fields in the descriptor.
549 static int usb_find_and_bind_driver(struct udevice *parent,
550 struct usb_device_descriptor *desc,
551 struct usb_interface_descriptor *iface,
552 int bus_seq, int devnum, int port,
553 struct udevice **devp)
555 struct usb_driver_entry *start, *entry;
559 ofnode node = usb_get_ofnode(parent, port);
562 debug("%s: Searching for driver\n", __func__);
563 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
564 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
565 for (entry = start; entry != start + n_ents; entry++) {
566 const struct usb_device_id *id;
568 const struct driver *drv;
569 struct usb_dev_plat *plat;
571 for (id = entry->match; id->match_flags; id++) {
572 if (!usb_match_one_id(desc, iface, id))
577 * We could pass the descriptor to the driver as
578 * plat (instead of NULL) and allow its bind()
579 * method to return -ENOENT if it doesn't support this
580 * device. That way we could continue the search to
581 * find another driver. For now this doesn't seem
582 * necesssary, so just bind the first match.
584 ret = device_bind(parent, drv, drv->name, NULL, node,
588 debug("%s: Match found: %s\n", __func__, drv->name);
589 dev->driver_data = id->driver_info;
590 plat = dev_get_parent_plat(dev);
597 /* Bind a generic driver so that the device can be used */
598 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
602 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
605 debug("%s: No match found: %d\n", __func__, ret);
610 * usb_find_child() - Find an existing device which matches our needs
614 static int usb_find_child(struct udevice *parent,
615 struct usb_device_descriptor *desc,
616 struct usb_interface_descriptor *iface,
617 struct udevice **devp)
622 for (device_find_first_child(parent, &dev);
624 device_find_next_child(&dev)) {
625 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
627 /* If this device is already in use, skip it */
628 if (device_active(dev))
630 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
631 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
632 if (usb_match_one_id(desc, iface, &plat->id)) {
641 int usb_scan_device(struct udevice *parent, int port,
642 enum usb_device_speed speed, struct udevice **devp)
645 bool created = false;
646 struct usb_dev_plat *plat;
647 struct usb_bus_priv *priv;
648 struct usb_device *parent_udev;
650 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
651 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
654 memset(udev, '\0', sizeof(*udev));
655 udev->controller_dev = usb_get_bus(parent);
656 priv = dev_get_uclass_priv(udev->controller_dev);
659 * Somewhat nasty, this. We create a local device and use the normal
660 * USB stack to read its descriptor. Then we know what type of device
661 * to create for real.
663 * udev->dev is set to the parent, since we don't have a real device
664 * yet. The USB stack should not access udev.dev anyway, except perhaps
665 * to find the controller, and the controller will either be @parent,
666 * or some parent of @parent.
668 * Another option might be to create the device as a generic USB
669 * device, then morph it into the correct one when we know what it
670 * should be. This means that a generic USB device would morph into
671 * a network controller, or a USB flash stick, for example. However,
672 * we don't support such morphing and it isn't clear that it would
675 * Yet another option is to split out the USB stack parts of udev
676 * into something like a 'struct urb' (as Linux does) which can exist
677 * independently of any device. This feels cleaner, but calls for quite
678 * a big change to the USB stack.
680 * For now, the approach is to set up an empty udev, read its
681 * descriptor and assign it an address, then bind a real device and
682 * stash the resulting information into the device's parent
683 * platform data. Then when we probe it, usb_child_pre_probe() is called
684 * and it will pull the information out of the stash.
688 udev->devnum = priv->next_addr + 1;
690 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
691 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
692 dev_get_parent_priv(parent) : NULL;
693 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
694 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
697 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
698 debug("** usb_find_child returns %d\n", ret);
702 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
704 udev->controller_dev->seq,
705 udev->devnum, port, &dev);
710 plat = dev_get_parent_plat(dev);
711 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
712 plat->devnum = udev->devnum;
715 ret = device_probe(dev);
717 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
729 * Detect if a USB device has been plugged or unplugged.
731 int usb_detect_change(void)
738 ret = uclass_get(UCLASS_USB_HUB, &uc);
742 uclass_foreach_dev(hub, uc) {
743 struct usb_device *udev;
746 if (!device_active(hub))
748 for (device_find_first_child(hub, &dev);
750 device_find_next_child(&dev)) {
751 struct usb_port_status status;
753 if (!device_active(dev))
756 udev = dev_get_parent_priv(dev);
757 if (usb_get_port_status(udev, udev->portnr, &status)
759 /* USB request failed */
762 if (le16_to_cpu(status.wPortChange) &
763 USB_PORT_STAT_C_CONNECTION)
771 static int usb_child_post_bind(struct udevice *dev)
773 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
776 if (!dev_of_valid(dev))
779 /* We only support matching a few things */
780 val = dev_read_u32_default(dev, "usb,device-class", -1);
782 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
783 plat->id.bDeviceClass = val;
785 val = dev_read_u32_default(dev, "usb,interface-class", -1);
787 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
788 plat->id.bInterfaceClass = val;
794 struct udevice *usb_get_bus(struct udevice *dev)
798 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
801 /* By design this cannot happen */
803 debug("USB HUB '%s' does not have a controller\n", dev->name);
809 int usb_child_pre_probe(struct udevice *dev)
811 struct usb_device *udev = dev_get_parent_priv(dev);
812 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
817 * Copy over all the values set in the on stack struct
818 * usb_device in usb_scan_device() to our final struct
819 * usb_device for this dev.
821 *udev = *(plat->udev);
822 /* And clear plat->udev as it will not be valid for long */
827 * This happens with devices which are explicitly bound
828 * instead of being discovered through usb_scan_device()
829 * such as sandbox emul devices.
832 udev->controller_dev = usb_get_bus(dev);
833 udev->devnum = plat->devnum;
836 * udev did not go through usb_scan_device(), so we need to
837 * select the config and read the config descriptors.
839 ret = usb_select_config(udev);
847 UCLASS_DRIVER(usb) = {
850 .flags = DM_UC_FLAG_SEQ_ALIAS,
851 .post_bind = dm_scan_fdt_dev,
852 .priv_auto = sizeof(struct usb_uclass_priv),
853 .per_child_auto = sizeof(struct usb_device),
854 .per_device_auto = sizeof(struct usb_bus_priv),
855 .child_post_bind = usb_child_post_bind,
856 .child_pre_probe = usb_child_pre_probe,
857 .per_child_plat_auto = sizeof(struct usb_dev_plat),
860 UCLASS_DRIVER(usb_dev_generic) = {
861 .id = UCLASS_USB_DEV_GENERIC,
862 .name = "usb_dev_generic",
865 U_BOOT_DRIVER(usb_dev_generic_drv) = {
866 .id = UCLASS_USB_DEV_GENERIC,
867 .name = "usb_dev_generic_drv",