Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3ac435d | 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 | 3ac435d | 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> |
Anatolij Gustschin | 52edfed | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 19 | #include <power-domain.h> |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 20 | |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 21 | int device_chld_unbind(struct udevice *dev, struct driver *drv) |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 22 | { |
| 23 | struct udevice *pos, *n; |
| 24 | int ret, saved_ret = 0; |
| 25 | |
| 26 | assert(dev); |
| 27 | |
| 28 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 29 | if (drv && (pos->driver != drv)) |
| 30 | continue; |
| 31 | |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 32 | ret = device_unbind(pos); |
| 33 | if (ret && !saved_ret) |
| 34 | saved_ret = ret; |
| 35 | } |
| 36 | |
| 37 | return saved_ret; |
| 38 | } |
| 39 | |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 40 | int device_chld_remove(struct udevice *dev, struct driver *drv, |
| 41 | uint flags) |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 42 | { |
| 43 | struct udevice *pos, *n; |
| 44 | int ret; |
| 45 | |
| 46 | assert(dev); |
| 47 | |
| 48 | list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) { |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 49 | if (drv && (pos->driver != drv)) |
| 50 | continue; |
| 51 | |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 52 | ret = device_remove(pos, flags); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 53 | if (ret) |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int device_unbind(struct udevice *dev) |
| 61 | { |
Simon Glass | 3479253 | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 62 | const struct driver *drv; |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 63 | int ret; |
| 64 | |
| 65 | if (!dev) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | if (dev->flags & DM_FLAG_ACTIVATED) |
| 69 | return -EINVAL; |
| 70 | |
Masahiro Yamada | aed1a4d | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 71 | if (!(dev->flags & DM_FLAG_BOUND)) |
| 72 | return -EINVAL; |
| 73 | |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 74 | drv = dev->driver; |
| 75 | assert(drv); |
| 76 | |
| 77 | if (drv->unbind) { |
| 78 | ret = drv->unbind(dev); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | } |
| 82 | |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 83 | ret = device_chld_unbind(dev, NULL); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 84 | if (ret) |
| 85 | return ret; |
| 86 | |
Simon Glass | f8a8544 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 87 | if (dev->flags & DM_FLAG_ALLOC_PDATA) { |
| 88 | free(dev->platdata); |
| 89 | dev->platdata = NULL; |
| 90 | } |
Przemyslaw Marczak | 5eaed88 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 91 | if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) { |
| 92 | free(dev->uclass_platdata); |
| 93 | dev->uclass_platdata = NULL; |
| 94 | } |
Simon Glass | cdc133b | 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 | 3ac435d | 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); |
Masahiro Yamada | 608f26c | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 105 | |
| 106 | devres_release_all(dev); |
| 107 | |
Simon Glass | fd1c2d9 | 2016-07-04 11:58:15 -0600 | [diff] [blame] | 108 | if (dev->flags & DM_FLAG_NAME_ALLOCED) |
Simon Glass | a2040fa | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 109 | free((char *)dev->name); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 110 | free(dev); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * device_free() - Free memory buffers allocated by a device |
| 117 | * @dev: Device that is to be started |
| 118 | */ |
| 119 | void device_free(struct udevice *dev) |
| 120 | { |
| 121 | int size; |
| 122 | |
| 123 | if (dev->driver->priv_auto_alloc_size) { |
| 124 | free(dev->priv); |
| 125 | dev->priv = NULL; |
| 126 | } |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 127 | size = dev->uclass->uc_drv->per_device_auto_alloc_size; |
| 128 | if (size) { |
| 129 | free(dev->uclass_priv); |
| 130 | dev->uclass_priv = NULL; |
| 131 | } |
| 132 | if (dev->parent) { |
| 133 | size = dev->parent->driver->per_child_auto_alloc_size; |
Simon Glass | dac8db2 | 2015-01-25 08:27:06 -0700 | [diff] [blame] | 134 | if (!size) { |
| 135 | size = dev->parent->uclass->uc_drv-> |
| 136 | per_child_auto_alloc_size; |
| 137 | } |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 138 | if (size) { |
| 139 | free(dev->parent_priv); |
| 140 | dev->parent_priv = NULL; |
| 141 | } |
| 142 | } |
Simon Glass | 153851d | 2019-12-29 21:19:21 -0700 | [diff] [blame] | 143 | dev->flags &= ~DM_FLAG_PLATDATA_VALID; |
Masahiro Yamada | 608f26c | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 144 | |
| 145 | devres_release_probe(dev); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Stefan Roese | 426f99f | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 148 | static bool flags_remove(uint flags, uint drv_flags) |
| 149 | { |
| 150 | if ((flags & DM_REMOVE_NORMAL) || |
| 151 | (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE)))) |
| 152 | return true; |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 157 | int device_remove(struct udevice *dev, uint flags) |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 158 | { |
Simon Glass | 3479253 | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 159 | const struct driver *drv; |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 160 | int ret; |
| 161 | |
| 162 | if (!dev) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | if (!(dev->flags & DM_FLAG_ACTIVATED)) |
| 166 | return 0; |
| 167 | |
| 168 | drv = dev->driver; |
| 169 | assert(drv); |
| 170 | |
| 171 | ret = uclass_pre_remove_device(dev); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
Jean-Jacques Hiblot | 3be9bcb | 2018-08-09 16:17:45 +0200 | [diff] [blame] | 175 | ret = device_chld_remove(dev, NULL, flags); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 176 | if (ret) |
| 177 | goto err; |
| 178 | |
Stefan Roese | bc85aa4 | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Remove the device if called with the "normal" remove flag set, |
| 181 | * or if the remove flag matches any of the drivers remove flags |
| 182 | */ |
Stefan Roese | 426f99f | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 183 | if (drv->remove && flags_remove(flags, drv->flags)) { |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 184 | ret = drv->remove(dev); |
| 185 | if (ret) |
| 186 | goto err_remove; |
| 187 | } |
| 188 | |
| 189 | if (dev->parent && dev->parent->driver->child_post_remove) { |
| 190 | ret = dev->parent->driver->child_post_remove(dev); |
| 191 | if (ret) { |
| 192 | dm_warn("%s: Device '%s' failed child_post_remove()", |
| 193 | __func__, dev->name); |
| 194 | } |
| 195 | } |
| 196 | |
Anatolij Gustschin | 5349e25 | 2020-02-17 12:36:43 +0100 | [diff] [blame^] | 197 | if (!(drv->flags & |
| 198 | (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) && |
| 199 | dev != gd->cur_serial_dev) |
Anatolij Gustschin | 52edfed | 2019-09-27 13:48:15 +0530 | [diff] [blame] | 200 | dev_power_domain_off(dev); |
| 201 | |
Stefan Roese | 426f99f | 2017-04-24 09:48:02 +0200 | [diff] [blame] | 202 | if (flags_remove(flags, drv->flags)) { |
Stefan Roese | bc85aa4 | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 203 | device_free(dev); |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 204 | |
Stefan Roese | bc85aa4 | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 205 | dev->seq = -1; |
| 206 | dev->flags &= ~DM_FLAG_ACTIVATED; |
| 207 | } |
Simon Glass | 3ac435d | 2014-11-10 17:16:47 -0700 | [diff] [blame] | 208 | |
| 209 | return ret; |
| 210 | |
| 211 | err_remove: |
| 212 | /* We can't put the children back */ |
| 213 | dm_warn("%s: Device '%s' failed to remove, but children are gone\n", |
| 214 | __func__, dev->name); |
| 215 | err: |
| 216 | ret = uclass_post_probe_device(dev); |
| 217 | if (ret) { |
| 218 | dm_warn("%s: Device '%s' failed to post_probe on error path\n", |
| 219 | __func__, dev->name); |
| 220 | } |
| 221 | |
| 222 | return ret; |
| 223 | } |