Merge git://git.denx.de/u-boot-nand-flash
[platform/kernel/u-boot.git] / drivers / core / device-remove.c
index 6b87f86..0e56b23 100644 (file)
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
 
-int device_unbind_children(struct udevice *dev)
+/**
+ * device_chld_unbind() - Unbind all device's children from the device
+ *
+ * On error, the function continues to unbind all children, and reports the
+ * first error.
+ *
+ * @dev:       The device that is to be stripped of its children
+ * @return 0 on success, -ve on error
+ */
+static int device_chld_unbind(struct udevice *dev)
 {
        struct udevice *pos, *n;
        int ret, saved_ret = 0;
@@ -34,7 +43,12 @@ int device_unbind_children(struct udevice *dev)
        return saved_ret;
 }
 
-int device_remove_children(struct udevice *dev)
+/**
+ * device_chld_remove() - Stop all device's children
+ * @dev:       The device whose children are to be removed
+ * @return 0 on success, -ve on error
+ */
+static int device_chld_remove(struct udevice *dev)
 {
        struct udevice *pos, *n;
        int ret;
@@ -61,6 +75,9 @@ int device_unbind(struct udevice *dev)
        if (dev->flags & DM_FLAG_ACTIVATED)
                return -EINVAL;
 
+       if (!(dev->flags & DM_FLAG_BOUND))
+               return -EINVAL;
+
        drv = dev->driver;
        assert(drv);
 
@@ -70,7 +87,7 @@ int device_unbind(struct udevice *dev)
                        return ret;
        }
 
-       ret = device_unbind_children(dev);
+       ret = device_chld_unbind(dev);
        if (ret)
                return ret;
 
@@ -92,6 +109,11 @@ int device_unbind(struct udevice *dev)
 
        if (dev->parent)
                list_del(&dev->sibling_node);
+
+       devres_release_all(dev);
+
+       if (dev->flags & DM_NAME_ALLOCED)
+               free((char *)dev->name);
        free(dev);
 
        return 0;
@@ -125,6 +147,8 @@ void device_free(struct udevice *dev)
                        dev->parent_priv = NULL;
                }
        }
+
+       devres_release_probe(dev);
 }
 
 int device_remove(struct udevice *dev)
@@ -145,7 +169,7 @@ int device_remove(struct udevice *dev)
        if (ret)
                return ret;
 
-       ret = device_remove_children(dev);
+       ret = device_chld_remove(dev);
        if (ret)
                goto err;