u-boot-brain/drivers/core/device-remove.c
Tom Rini 83d290c56f SPDX: Convert all of our single license tags to Linux Kernel style
When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from.  So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry.  Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents.  There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>
2018-05-07 09:34:12 -04:00

225 lines
4.3 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* Device manager
*
* Copyright (c) 2014 Google, Inc
*
* (C) Copyright 2012
* Pavel Herrmann <morpheus.ibis@gmail.com>
*/
#include <common.h>
#include <errno.h>
#include <malloc.h>
#include <dm/device.h>
#include <dm/device-internal.h>
#include <dm/uclass.h>
#include <dm/uclass-internal.h>
#include <dm/util.h>
/**
* 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;
assert(dev);
list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
ret = device_unbind(pos);
if (ret && !saved_ret)
saved_ret = ret;
}
return saved_ret;
}
/**
* 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;
assert(dev);
list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
ret = device_remove(pos, flags);
if (ret)
return ret;
}
return 0;
}
int device_unbind(struct udevice *dev)
{
const struct driver *drv;
int ret;
if (!dev)
return -EINVAL;
if (dev->flags & DM_FLAG_ACTIVATED)
return -EINVAL;
if (!(dev->flags & DM_FLAG_BOUND))
return -EINVAL;
drv = dev->driver;
assert(drv);
if (drv->unbind) {
ret = drv->unbind(dev);
if (ret)
return ret;
}
ret = device_chld_unbind(dev);
if (ret)
return ret;
if (dev->flags & DM_FLAG_ALLOC_PDATA) {
free(dev->platdata);
dev->platdata = NULL;
}
if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
free(dev->uclass_platdata);
dev->uclass_platdata = NULL;
}
if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
free(dev->parent_platdata);
dev->parent_platdata = NULL;
}
ret = uclass_unbind_device(dev);
if (ret)
return ret;
if (dev->parent)
list_del(&dev->sibling_node);
devres_release_all(dev);
if (dev->flags & DM_FLAG_NAME_ALLOCED)
free((char *)dev->name);
free(dev);
return 0;
}
/**
* device_free() - Free memory buffers allocated by a device
* @dev: Device that is to be started
*/
void device_free(struct udevice *dev)
{
int size;
if (dev->driver->priv_auto_alloc_size) {
free(dev->priv);
dev->priv = NULL;
}
size = dev->uclass->uc_drv->per_device_auto_alloc_size;
if (size) {
free(dev->uclass_priv);
dev->uclass_priv = NULL;
}
if (dev->parent) {
size = dev->parent->driver->per_child_auto_alloc_size;
if (!size) {
size = dev->parent->uclass->uc_drv->
per_child_auto_alloc_size;
}
if (size) {
free(dev->parent_priv);
dev->parent_priv = NULL;
}
}
devres_release_probe(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;
if (!dev)
return -EINVAL;
if (!(dev->flags & DM_FLAG_ACTIVATED))
return 0;
drv = dev->driver;
assert(drv);
ret = uclass_pre_remove_device(dev);
if (ret)
return ret;
ret = device_chld_remove(dev, flags);
if (ret)
goto err;
/*
* 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;
}
if (dev->parent && dev->parent->driver->child_post_remove) {
ret = dev->parent->driver->child_post_remove(dev);
if (ret) {
dm_warn("%s: Device '%s' failed child_post_remove()",
__func__, dev->name);
}
}
if (flags_remove(flags, drv->flags)) {
device_free(dev);
dev->seq = -1;
dev->flags &= ~DM_FLAG_ACTIVATED;
}
return ret;
err_remove:
/* We can't put the children back */
dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
__func__, dev->name);
err:
ret = uclass_post_probe_device(dev);
if (ret) {
dm_warn("%s: Device '%s' failed to post_probe on error path\n",
__func__, dev->name);
}
return ret;
}