Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Device manager |
| 3 | * |
| 4 | * Copyright (c) 2014 Google, Inc |
| 5 | * |
| 6 | * (C) Copyright 2012 |
| 7 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <errno.h> |
| 14 | #include <malloc.h> |
| 15 | #include <dm/device.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass.h> |
| 18 | #include <dm/uclass-internal.h> |
| 19 | #include <dm/util.h> |
| 20 | |
| 21 | /** |
| 22 | * device_chld_unbind() - Unbind all device's children from the device |
| 23 | * |
| 24 | * On error, the function continues to unbind all children, and reports the |
| 25 | * first error. |
| 26 | * |
| 27 | * @dev: The device that is to be stripped of its children |
| 28 | * @return 0 on success, -ve on error |
| 29 | */ |
| 30 | static int device_chld_unbind(struct udevice *dev) |
| 31 | { |
| 32 | struct udevice *pos, *n; |
| 33 | int ret, saved_ret = 0; |
| 34 | |
| 35 | assert(dev); |
| 36 | |
| 37 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
| 38 | ret = device_unbind(pos); |
| 39 | if (ret && !saved_ret) |
| 40 | saved_ret = ret; |
| 41 | } |
| 42 | |
| 43 | return saved_ret; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * device_chld_remove() - Stop all device's children |
| 48 | * @dev: The device whose children are to be removed |
| 49 | * @return 0 on success, -ve on error |
| 50 | */ |
| 51 | static int device_chld_remove(struct udevice *dev) |
| 52 | { |
| 53 | struct udevice *pos, *n; |
| 54 | int ret; |
| 55 | |
| 56 | assert(dev); |
| 57 | |
| 58 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
| 59 | ret = device_remove(pos); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int device_unbind(struct udevice *dev) |
| 68 | { |
| 69 | struct driver *drv; |
| 70 | int ret; |
| 71 | |
| 72 | if (!dev) |
| 73 | return -EINVAL; |
| 74 | |
| 75 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | drv = dev->driver; |
| 79 | assert(drv); |
| 80 | |
| 81 | if (drv->unbind) { |
| 82 | ret = drv->unbind(dev); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | ret = device_chld_unbind(dev); |
| 88 | if (ret) |
| 89 | return ret; |
| 90 | |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 91 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
| 92 | free(dev->platdata); |
| 93 | dev->platdata = NULL; |
| 94 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 95 | if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { |
| 96 | free(dev->parent_platdata); |
| 97 | dev->parent_platdata = NULL; |
| 98 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 99 | ret = uclass_unbind_device(dev); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | |
| 103 | if (dev->parent) |
| 104 | list_del(&dev->sibling_node); |
| 105 | free(dev); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * device_free() - Free memory buffers allocated by a device |
| 112 | * @dev: Device that is to be started |
| 113 | */ |
| 114 | void device_free(struct udevice *dev) |
| 115 | { |
| 116 | int size; |
| 117 | |
| 118 | if (dev->driver->priv_auto_alloc_size) { |
| 119 | free(dev->priv); |
| 120 | dev->priv = NULL; |
| 121 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 122 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; |
| 123 | if (size) { |
| 124 | free(dev->uclass_priv); |
| 125 | dev->uclass_priv = NULL; |
| 126 | } |
| 127 | if (dev->parent) { |
| 128 | size = dev->parent->driver->per_child_auto_alloc_size; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 129 | if (!size) { |
| 130 | size = dev->parent->uclass->uc_drv-> |
| 131 | per_child_auto_alloc_size; |
| 132 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 133 | if (size) { |
| 134 | free(dev->parent_priv); |
| 135 | dev->parent_priv = NULL; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | int device_remove(struct udevice *dev) |
| 141 | { |
| 142 | struct driver *drv; |
| 143 | int ret; |
| 144 | |
| 145 | if (!dev) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | if (!(dev->flags & DM_FLAG_ACTIVATED)) |
| 149 | return 0; |
| 150 | |
| 151 | drv = dev->driver; |
| 152 | assert(drv); |
| 153 | |
| 154 | ret = uclass_pre_remove_device(dev); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | |
| 158 | ret = device_chld_remove(dev); |
| 159 | if (ret) |
| 160 | goto err; |
| 161 | |
| 162 | if (drv->remove) { |
| 163 | ret = drv->remove(dev); |
| 164 | if (ret) |
| 165 | goto err_remove; |
| 166 | } |
| 167 | |
| 168 | if (dev->parent && dev->parent->driver->child_post_remove) { |
| 169 | ret = dev->parent->driver->child_post_remove(dev); |
| 170 | if (ret) { |
| 171 | dm_warn("%s: Device '%s' failed child_post_remove()", |
| 172 | __func__, dev->name); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | device_free(dev); |
| 177 | |
| 178 | dev->seq = -1; |
| 179 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 180 | |
| 181 | return ret; |
| 182 | |
| 183 | err_remove: |
| 184 | /* We can't put the children back */ |
| 185 | dm_warn("%s: Device '%s' failed to remove, but children are gone\n", |
| 186 | __func__, dev->name); |
| 187 | err: |
| 188 | ret = uclass_post_probe_device(dev); |
| 189 | if (ret) { |
| 190 | dm_warn("%s: Device '%s' failed to post_probe on error path\n", |
| 191 | __func__, dev->name); |
| 192 | } |
| 193 | |
| 194 | return ret; |
| 195 | } |