Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Device manager |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <errno.h> |
| 13 | #include <malloc.h> |
| 14 | #include <dm/device.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/uclass.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | #include <dm/util.h> |
| 19 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 20 | int device_chld_unbind(struct udevice *dev, struct driver *drv) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 21 | { |
| 22 | struct udevice *pos, *n; |
| 23 | int ret, saved_ret = 0; |
| 24 | |
| 25 | assert(dev); |
| 26 | |
| 27 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 28 | if (drv && (pos->driver != drv)) |
| 29 | continue; |
| 30 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 31 | ret = device_unbind(pos); |
| 32 | if (ret && !saved_ret) |
| 33 | saved_ret = ret; |
| 34 | } |
| 35 | |
| 36 | return saved_ret; |
| 37 | } |
| 38 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 39 | int device_chld_remove(struct udevice *dev, struct driver *drv, |
| 40 | uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 41 | { |
| 42 | struct udevice *pos, *n; |
| 43 | int ret; |
| 44 | |
| 45 | assert(dev); |
| 46 | |
| 47 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 48 | if (drv && (pos->driver != drv)) |
| 49 | continue; |
| 50 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 51 | ret = device_remove(pos, flags); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 52 | if (ret) |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int device_unbind(struct udevice *dev) |
| 60 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 61 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 62 | int ret; |
| 63 | |
| 64 | if (!dev) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 68 | return -EINVAL; |
| 69 | |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 70 | if (!(dev->flags & DM_FLAG_BOUND)) |
| 71 | return -EINVAL; |
| 72 | |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 73 | drv = dev->driver; |
| 74 | assert(drv); |
| 75 | |
| 76 | if (drv->unbind) { |
| 77 | ret = drv->unbind(dev); |
| 78 | if (ret) |
| 79 | return ret; |
| 80 | } |
| 81 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 82 | ret = device_chld_unbind(dev, NULL); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 83 | if (ret) |
| 84 | return ret; |
| 85 | |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 86 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
| 87 | free(dev->platdata); |
| 88 | dev->platdata = NULL; |
| 89 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 90 | if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { |
| 91 | free(dev->uclass_platdata); |
| 92 | dev->uclass_platdata = NULL; |
| 93 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 94 | if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) { |
| 95 | free(dev->parent_platdata); |
| 96 | dev->parent_platdata = NULL; |
| 97 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 98 | ret = uclass_unbind_device(dev); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
| 102 | if (dev->parent) |
| 103 | list_del(&dev->sibling_node); |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 104 | |
| 105 | devres_release_all(dev); |
| 106 | |
Simon Glass | 2d4c7dc | 2016-07-04 11:58:15 -0600 | [diff] [blame] | 107 | if (dev->flags & DM_FLAG_NAME_ALLOCED) |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 108 | free((char *)dev->name); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 109 | free(dev); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * device_free() - Free memory buffers allocated by a device |
| 116 | * @dev: Device that is to be started |
| 117 | */ |
| 118 | void device_free(struct udevice *dev) |
| 119 | { |
| 120 | int size; |
| 121 | |
| 122 | if (dev->driver->priv_auto_alloc_size) { |
| 123 | free(dev->priv); |
| 124 | dev->priv = NULL; |
| 125 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 126 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; |
| 127 | if (size) { |
| 128 | free(dev->uclass_priv); |
| 129 | dev->uclass_priv = NULL; |
| 130 | } |
| 131 | if (dev->parent) { |
| 132 | size = dev->parent->driver->per_child_auto_alloc_size; |
Simon Glass | c23b428 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 133 | if (!size) { |
| 134 | size = dev->parent->uclass->uc_drv-> |
| 135 | per_child_auto_alloc_size; |
| 136 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 137 | if (size) { |
| 138 | free(dev->parent_priv); |
| 139 | dev->parent_priv = NULL; |
| 140 | } |
| 141 | } |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 142 | |
| 143 | devres_release_probe(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 146 | static bool flags_remove(uint flags, uint drv_flags) |
| 147 | { |
| 148 | if ((flags & DM_REMOVE_NORMAL) || |
| 149 | (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE)))) |
| 150 | return true; |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 155 | int device_remove(struct udevice *dev, uint flags) |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 156 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 157 | const struct driver *drv; |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 158 | int ret; |
| 159 | |
| 160 | if (!dev) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | if (!(dev->flags & DM_FLAG_ACTIVATED)) |
| 164 | return 0; |
| 165 | |
| 166 | drv = dev->driver; |
| 167 | assert(drv); |
| 168 | |
| 169 | ret = uclass_pre_remove_device(dev); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
Jean-Jacques Hiblot | 2c556a1 | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 173 | ret = device_chld_remove(dev, NULL, flags); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 174 | if (ret) |
| 175 | goto err; |
| 176 | |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 177 | /* |
| 178 | * Remove the device if called with the "normal" remove flag set, |
| 179 | * or if the remove flag matches any of the drivers remove flags |
| 180 | */ |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 181 | if (drv->remove && flags_remove(flags, drv->flags)) { |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 182 | ret = drv->remove(dev); |
| 183 | if (ret) |
| 184 | goto err_remove; |
| 185 | } |
| 186 | |
| 187 | if (dev->parent && dev->parent->driver->child_post_remove) { |
| 188 | ret = dev->parent->driver->child_post_remove(dev); |
| 189 | if (ret) { |
| 190 | dm_warn("%s: Device '%s' failed child_post_remove()", |
| 191 | __func__, dev->name); |
| 192 | } |
| 193 | } |
| 194 | |
Stefan Roese | 0745676 | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 195 | if (flags_remove(flags, drv->flags)) { |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 196 | device_free(dev); |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 197 | |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 198 | dev->seq = -1; |
| 199 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 200 | } |
Simon Glass | b38ea5a | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 201 | |
| 202 | return ret; |
| 203 | |
| 204 | err_remove: |
| 205 | /* We can't put the children back */ |
| 206 | dm_warn("%s: Device '%s' failed to remove, but children are gone\n", |
| 207 | __func__, dev->name); |
| 208 | err: |
| 209 | ret = uclass_post_probe_device(dev); |
| 210 | if (ret) { |
| 211 | dm_warn("%s: Device '%s' failed to post_probe on error path\n", |
| 212 | __func__, dev->name); |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |