uboot/drivers/core/device-remove.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Device manager
   4 *
   5 * Copyright (c) 2014 Google, Inc
   6 *
   7 * (C) Copyright 2012
   8 * Pavel Herrmann <morpheus.ibis@gmail.com>
   9 */
  10
  11#define LOG_CATEGORY    LOGC_DM
  12
  13#include <common.h>
  14#include <errno.h>
  15#include <log.h>
  16#include <malloc.h>
  17#include <dm/device.h>
  18#include <dm/device-internal.h>
  19#include <dm/uclass.h>
  20#include <dm/uclass-internal.h>
  21#include <dm/util.h>
  22#include <power-domain.h>
  23#include <asm/global_data.h>
  24
  25int device_chld_unbind(struct udevice *dev, struct driver *drv)
  26{
  27        struct udevice *pos, *n;
  28        int ret, saved_ret = 0;
  29
  30        assert(dev);
  31
  32        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  33                if (drv && (pos->driver != drv))
  34                        continue;
  35
  36                ret = device_unbind(pos);
  37                if (ret && !saved_ret) {
  38                        log_warning("device '%s' failed to unbind\n",
  39                                    pos->name);
  40                        saved_ret = ret;
  41                }
  42        }
  43
  44        return log_ret(saved_ret);
  45}
  46
  47int device_chld_remove(struct udevice *dev, struct driver *drv,
  48                       uint flags)
  49{
  50        struct udevice *pos, *n;
  51        int result = 0;
  52
  53        assert(dev);
  54
  55        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
  56                int ret;
  57
  58                if (drv && (pos->driver != drv))
  59                        continue;
  60
  61                ret = device_remove(pos, flags);
  62                if (ret == -EPROBE_DEFER)
  63                        result = ret;
  64                else if (ret && ret != -EKEYREJECTED)
  65                        return ret;
  66        }
  67
  68        return result;
  69}
  70
  71int device_unbind(struct udevice *dev)
  72{
  73        const struct driver *drv;
  74        int ret;
  75
  76        if (!dev)
  77                return log_msg_ret("dev", -EINVAL);
  78
  79        if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  80                return log_msg_ret("active", -EINVAL);
  81
  82        if (!(dev_get_flags(dev) & DM_FLAG_BOUND))
  83                return log_msg_ret("not-bound", -EINVAL);
  84
  85        drv = dev->driver;
  86        assert(drv);
  87
  88        if (drv->unbind) {
  89                ret = drv->unbind(dev);
  90                if (ret)
  91                        return log_msg_ret("unbind", ret);
  92        }
  93
  94        ret = device_chld_unbind(dev, NULL);
  95        if (ret)
  96                return log_msg_ret("child unbind", ret);
  97
  98        if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
  99                free(dev_get_plat(dev));
 100                dev_set_plat(dev, NULL);
 101        }
 102        if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
 103                free(dev_get_uclass_plat(dev));
 104                dev_set_uclass_plat(dev, NULL);
 105        }
 106        if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
 107                free(dev_get_parent_plat(dev));
 108                dev_set_parent_plat(dev, NULL);
 109        }
 110        ret = uclass_unbind_device(dev);
 111        if (ret)
 112                return log_msg_ret("uc", ret);
 113
 114        if (dev->parent)
 115                list_del(&dev->sibling_node);
 116
 117        devres_release_all(dev);
 118
 119        if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
 120                free((char *)dev->name);
 121        free(dev);
 122
 123        return 0;
 124}
 125
 126/**
 127 * device_free() - Free memory buffers allocated by a device
 128 * @dev:        Device that is to be started
 129 */
 130void device_free(struct udevice *dev)
 131{
 132        int size;
 133
 134        if (dev->driver->priv_auto) {
 135                free(dev_get_priv(dev));
 136                dev_set_priv(dev, NULL);
 137        }
 138        size = dev->uclass->uc_drv->per_device_auto;
 139        if (size) {
 140                free(dev_get_uclass_priv(dev));
 141                dev_set_uclass_priv(dev, NULL);
 142        }
 143        if (dev->parent) {
 144                size = dev->parent->driver->per_child_auto;
 145                if (!size) {
 146                        size = dev->parent->uclass->uc_drv->
 147                                        per_child_auto;
 148                }
 149                if (size) {
 150                        free(dev_get_parent_priv(dev));
 151                        dev_set_parent_priv(dev, NULL);
 152                }
 153        }
 154        dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
 155
 156        devres_release_probe(dev);
 157}
 158
 159/**
 160 * flags_remove() - Figure out whether to remove a device
 161 *
 162 * If this is called with @flags == DM_REMOVE_NON_VITAL | DM_REMOVE_ACTIVE_DMA,
 163 * then it returns 0 (=go head and remove) if the device is not matked vital
 164 * but is marked DM_REMOVE_ACTIVE_DMA.
 165 *
 166 * If this is called with @flags == DM_REMOVE_ACTIVE_DMA,
 167 * then it returns 0 (=go head and remove) if the device is marked
 168 * DM_REMOVE_ACTIVE_DMA, regardless of whether it is marked vital.
 169 *
 170 * @flags: Flags passed to device_remove()
 171 * @drv_flags: Driver flags
 172 * @return 0 if the device should be removed,
 173 * -EKEYREJECTED if @flags includes a flag in DM_REMOVE_ACTIVE_ALL but
 174 *      @drv_flags does not (indicates that this device has nothing to do for
 175 *      DMA shutdown or OS prepare)
 176 * -EPROBE_DEFER if @flags is DM_REMOVE_NON_VITAL but @drv_flags contains
 177 *      DM_FLAG_VITAL (indicates the device is vital and should not be removed)
 178 */
 179static int flags_remove(uint flags, uint drv_flags)
 180{
 181        if (!(flags & DM_REMOVE_NORMAL)) {
 182                bool vital_match;
 183                bool active_match;
 184
 185                active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
 186                        (drv_flags & flags);
 187                vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
 188                        !(drv_flags & DM_FLAG_VITAL);
 189                if (!vital_match)
 190                        return -EPROBE_DEFER;
 191                if (!active_match)
 192                        return -EKEYREJECTED;
 193        }
 194
 195        return 0;
 196}
 197
 198int device_remove(struct udevice *dev, uint flags)
 199{
 200        const struct driver *drv;
 201        int ret;
 202
 203        if (!dev)
 204                return -EINVAL;
 205
 206        if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
 207                return 0;
 208
 209        /*
 210         * If the child returns EKEYREJECTED, continue. It just means that it
 211         * didn't match the flags.
 212         */
 213        ret = device_chld_remove(dev, NULL, flags);
 214        if (ret && ret != -EKEYREJECTED)
 215                return ret;
 216
 217        /*
 218         * Remove the device if called with the "normal" remove flag set,
 219         * or if the remove flag matches any of the drivers remove flags
 220         */
 221        drv = dev->driver;
 222        assert(drv);
 223        ret = flags_remove(flags, drv->flags);
 224        if (ret) {
 225                log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
 226                          dev->name, flags, drv->flags, ret);
 227                return ret;
 228        }
 229
 230        ret = uclass_pre_remove_device(dev);
 231        if (ret)
 232                return ret;
 233
 234        if (drv->remove) {
 235                ret = drv->remove(dev);
 236                if (ret)
 237                        goto err_remove;
 238        }
 239
 240        if (dev->parent && dev->parent->driver->child_post_remove) {
 241                ret = dev->parent->driver->child_post_remove(dev);
 242                if (ret) {
 243                        dm_warn("%s: Device '%s' failed child_post_remove()",
 244                                __func__, dev->name);
 245                }
 246        }
 247
 248        if (!(flags & DM_REMOVE_NO_PD) &&
 249            !(drv->flags &
 250              (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
 251            dev != gd->cur_serial_dev)
 252                dev_power_domain_off(dev);
 253
 254        device_free(dev);
 255
 256        dev_bic_flags(dev, DM_FLAG_ACTIVATED);
 257
 258        return 0;
 259
 260err_remove:
 261        /* We can't put the children back */
 262        dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
 263                __func__, dev->name);
 264
 265        return ret;
 266}
 267