1
2
3
4
5
6
7
8
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 device_foreach_child_safe(pos, n, dev) {
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 device_foreach_child_safe(pos, n, dev) {
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 ret = uclass_pre_unbind_device(dev);
99 if (ret)
100 return log_msg_ret("uc", ret);
101 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
102 free(dev_get_plat(dev));
103 dev_set_plat(dev, NULL);
104 }
105 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
106 free(dev_get_uclass_plat(dev));
107 dev_set_uclass_plat(dev, NULL);
108 }
109 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
110 free(dev_get_parent_plat(dev));
111 dev_set_parent_plat(dev, NULL);
112 }
113 ret = uclass_unbind_device(dev);
114 if (ret)
115 return log_msg_ret("uc", ret);
116
117 if (dev->parent)
118 list_del(&dev->sibling_node);
119
120 devres_release_all(dev);
121
122 if (dev_get_flags(dev) & DM_FLAG_NAME_ALLOCED)
123 free((char *)dev->name);
124 free(dev);
125
126 return 0;
127}
128
129
130
131
132
133void device_free(struct udevice *dev)
134{
135 int size;
136
137 if (dev->driver->priv_auto) {
138 free(dev_get_priv(dev));
139 dev_set_priv(dev, NULL);
140 }
141 size = dev->uclass->uc_drv->per_device_auto;
142 if (size) {
143 free(dev_get_uclass_priv(dev));
144 dev_set_uclass_priv(dev, NULL);
145 }
146 if (dev->parent) {
147 size = dev->parent->driver->per_child_auto;
148 if (!size)
149 size = dev->parent->uclass->uc_drv->per_child_auto;
150 if (size) {
151 free(dev_get_parent_priv(dev));
152 dev_set_parent_priv(dev, NULL);
153 }
154 }
155 dev_bic_flags(dev, DM_FLAG_PLATDATA_VALID);
156
157 devres_release_probe(dev);
158}
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180static int flags_remove(uint flags, uint drv_flags)
181{
182 if (!(flags & DM_REMOVE_NORMAL)) {
183 bool vital_match;
184 bool active_match;
185
186 active_match = !(flags & DM_REMOVE_ACTIVE_ALL) ||
187 (drv_flags & flags);
188 vital_match = !(flags & DM_REMOVE_NON_VITAL) ||
189 !(drv_flags & DM_FLAG_VITAL);
190 if (!vital_match)
191 return -EPROBE_DEFER;
192 if (!active_match)
193 return -EKEYREJECTED;
194 }
195
196 return 0;
197}
198
199int device_remove(struct udevice *dev, uint flags)
200{
201 const struct driver *drv;
202 int ret;
203
204 if (!dev)
205 return -EINVAL;
206
207 if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
208 return 0;
209
210 ret = device_notify(dev, EVT_DM_PRE_REMOVE);
211 if (ret)
212 return ret;
213
214
215
216
217
218 ret = device_chld_remove(dev, NULL, flags);
219 if (ret && ret != -EKEYREJECTED)
220 return ret;
221
222
223
224
225
226 drv = dev->driver;
227 assert(drv);
228 ret = flags_remove(flags, drv->flags);
229 if (ret) {
230 log_debug("%s: When removing: flags=%x, drv->flags=%x, err=%d\n",
231 dev->name, flags, drv->flags, ret);
232 return ret;
233 }
234
235 ret = uclass_pre_remove_device(dev);
236 if (ret)
237 return ret;
238
239 if (drv->remove) {
240 ret = drv->remove(dev);
241 if (ret)
242 goto err_remove;
243 }
244
245 if (dev->parent && dev->parent->driver->child_post_remove) {
246 ret = dev->parent->driver->child_post_remove(dev);
247 if (ret) {
248 dm_warn("%s: Device '%s' failed child_post_remove()",
249 __func__, dev->name);
250 }
251 }
252
253 if (!(flags & DM_REMOVE_NO_PD) &&
254 !(drv->flags &
255 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_LEAVE_PD_ON)) &&
256 dev != gd->cur_serial_dev)
257 dev_power_domain_off(dev);
258
259 device_free(dev);
260
261 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
262
263 ret = device_notify(dev, EVT_DM_POST_REMOVE);
264 if (ret)
265 goto err_remove;
266
267 return 0;
268
269err_remove:
270
271 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
272 __func__, dev->name);
273
274 return ret;
275}
276