]> git.sur5r.net Git - u-boot/blobdiff - drivers/core/device-remove.c
video: anx9804: split out registers definitions into a separate header
[u-boot] / drivers / core / device-remove.c
index bd6d4062c93723e2d4a5ff434c6d077af637ce81..3c6ab42f7d620a2aac9763f07cdbdb909072030d 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,13 @@ 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
+ * @pre_os_remove: Flag, if this functions is called in the pre-OS stage
+ * @return 0 on success, -ve on error
+ */
+static int device_chld_remove(struct udevice *dev, uint flags)
 {
        struct udevice *pos, *n;
        int ret;
@@ -42,7 +57,7 @@ int device_remove_children(struct udevice *dev)
        assert(dev);
 
        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
-               ret = device_remove(pos);
+               ret = device_remove(pos, flags);
                if (ret)
                        return ret;
        }
@@ -73,7 +88,7 @@ int device_unbind(struct udevice *dev)
                        return ret;
        }
 
-       ret = device_unbind_children(dev);
+       ret = device_chld_unbind(dev);
        if (ret)
                return ret;
 
@@ -98,6 +113,8 @@ int device_unbind(struct udevice *dev)
 
        devres_release_all(dev);
 
+       if (dev->flags & DM_FLAG_NAME_ALLOCED)
+               free((char *)dev->name);
        free(dev);
 
        return 0;
@@ -135,7 +152,16 @@ void device_free(struct udevice *dev)
        devres_release_probe(dev);
 }
 
-int device_remove(struct udevice *dev)
+static bool flags_remove(uint flags, uint drv_flags)
+{
+       if ((flags & DM_REMOVE_NORMAL) ||
+           (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
+               return true;
+
+       return false;
+}
+
+int device_remove(struct udevice *dev, uint flags)
 {
        const struct driver *drv;
        int ret;
@@ -153,11 +179,15 @@ int device_remove(struct udevice *dev)
        if (ret)
                return ret;
 
-       ret = device_remove_children(dev);
+       ret = device_chld_remove(dev, flags);
        if (ret)
                goto err;
 
-       if (drv->remove) {
+       /*
+        * Remove the device if called with the "normal" remove flag set,
+        * or if the remove flag matches any of the drivers remove flags
+        */
+       if (drv->remove && flags_remove(flags, drv->flags)) {
                ret = drv->remove(dev);
                if (ret)
                        goto err_remove;
@@ -171,10 +201,12 @@ int device_remove(struct udevice *dev)
                }
        }
 
-       device_free(dev);
+       if (flags_remove(flags, drv->flags)) {
+               device_free(dev);
 
-       dev->seq = -1;
-       dev->flags &= ~DM_FLAG_ACTIVATED;
+               dev->seq = -1;
+               dev->flags &= ~DM_FLAG_ACTIVATED;
+       }
 
        return ret;