1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
21 * device_chld_unbind() - Unbind all device's children from the device
23 * On error, the function continues to unbind all children, and reports the
26 * @dev: The device that is to be stripped of its children
27 * @return 0 on success, -ve on error
29 static int device_chld_unbind(struct udevice *dev)
31 struct udevice *pos, *n;
32 int ret, saved_ret = 0;
36 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
37 ret = device_unbind(pos);
38 if (ret && !saved_ret)
46 * device_chld_remove() - Stop all device's children
47 * @dev: The device whose children are to be removed
48 * @pre_os_remove: Flag, if this functions is called in the pre-OS stage
49 * @return 0 on success, -ve on error
51 static int device_chld_remove(struct udevice *dev, uint flags)
53 struct udevice *pos, *n;
58 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
59 ret = device_remove(pos, flags);
67 int device_unbind(struct udevice *dev)
69 const struct driver *drv;
75 if (dev->flags & DM_FLAG_ACTIVATED)
78 if (!(dev->flags & DM_FLAG_BOUND))
85 ret = drv->unbind(dev);
90 ret = device_chld_unbind(dev);
94 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
98 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
99 free(dev->uclass_platdata);
100 dev->uclass_platdata = NULL;
102 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
103 free(dev->parent_platdata);
104 dev->parent_platdata = NULL;
106 ret = uclass_unbind_device(dev);
111 list_del(&dev->sibling_node);
113 devres_release_all(dev);
115 if (dev->flags & DM_FLAG_NAME_ALLOCED)
116 free((char *)dev->name);
123 * device_free() - Free memory buffers allocated by a device
124 * @dev: Device that is to be started
126 void device_free(struct udevice *dev)
130 if (dev->driver->priv_auto_alloc_size) {
134 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
136 free(dev->uclass_priv);
137 dev->uclass_priv = NULL;
140 size = dev->parent->driver->per_child_auto_alloc_size;
142 size = dev->parent->uclass->uc_drv->
143 per_child_auto_alloc_size;
146 free(dev->parent_priv);
147 dev->parent_priv = NULL;
151 devres_release_probe(dev);
154 static bool flags_remove(uint flags, uint drv_flags)
156 if ((flags & DM_REMOVE_NORMAL) ||
157 (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
163 int device_remove(struct udevice *dev, uint flags)
165 const struct driver *drv;
171 if (!(dev->flags & DM_FLAG_ACTIVATED))
177 ret = uclass_pre_remove_device(dev);
181 ret = device_chld_remove(dev, flags);
186 * Remove the device if called with the "normal" remove flag set,
187 * or if the remove flag matches any of the drivers remove flags
189 if (drv->remove && flags_remove(flags, drv->flags)) {
190 ret = drv->remove(dev);
195 if (dev->parent && dev->parent->driver->child_post_remove) {
196 ret = dev->parent->driver->child_post_remove(dev);
198 dm_warn("%s: Device '%s' failed child_post_remove()",
199 __func__, dev->name);
203 if (flags_remove(flags, drv->flags)) {
207 dev->flags &= ~DM_FLAG_ACTIVATED;
213 /* We can't put the children back */
214 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
215 __func__, dev->name);
217 ret = uclass_post_probe_device(dev);
219 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
220 __func__, dev->name);