blob: 586fadee0a8731f9b77f24369606ed355d94e5b5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb38ea5a2014-11-10 17:16:47 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2014 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassb38ea5a2014-11-10 17:16:47 -07009 */
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 Hiblot2c556a12018-08-09 16:17:45 +020020int device_chld_unbind(struct udevice *dev, struct driver *drv)
Simon Glassb38ea5a2014-11-10 17:16:47 -070021{
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 Hiblot2c556a12018-08-09 16:17:45 +020028 if (drv && (pos->driver != drv))
29 continue;
30
Simon Glassb38ea5a2014-11-10 17:16:47 -070031 ret = device_unbind(pos);
32 if (ret && !saved_ret)
33 saved_ret = ret;
34 }
35
36 return saved_ret;
37}
38
Jean-Jacques Hiblot2c556a12018-08-09 16:17:45 +020039int device_chld_remove(struct udevice *dev, struct driver *drv,
40 uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -070041{
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 Hiblot2c556a12018-08-09 16:17:45 +020048 if (drv && (pos->driver != drv))
49 continue;
50
Stefan Roese80b5bc92017-03-20 12:51:48 +010051 ret = device_remove(pos, flags);
Simon Glassb38ea5a2014-11-10 17:16:47 -070052 if (ret)
53 return ret;
54 }
55
56 return 0;
57}
58
59int device_unbind(struct udevice *dev)
60{
Simon Glassa626dff2015-03-25 12:21:54 -060061 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070062 int ret;
63
64 if (!dev)
65 return -EINVAL;
66
67 if (dev->flags & DM_FLAG_ACTIVATED)
68 return -EINVAL;
69
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +090070 if (!(dev->flags & DM_FLAG_BOUND))
71 return -EINVAL;
72
Simon Glassb38ea5a2014-11-10 17:16:47 -070073 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 Hiblot2c556a12018-08-09 16:17:45 +020082 ret = device_chld_unbind(dev, NULL);
Simon Glassb38ea5a2014-11-10 17:16:47 -070083 if (ret)
84 return ret;
85
Simon Glassba750492015-01-25 08:27:00 -070086 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
87 free(dev->platdata);
88 dev->platdata = NULL;
89 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020090 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
91 free(dev->uclass_platdata);
92 dev->uclass_platdata = NULL;
93 }
Simon Glass11b61732015-01-25 08:27:01 -070094 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
95 free(dev->parent_platdata);
96 dev->parent_platdata = NULL;
97 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070098 ret = uclass_unbind_device(dev);
99 if (ret)
100 return ret;
101
102 if (dev->parent)
103 list_del(&dev->sibling_node);
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900104
105 devres_release_all(dev);
106
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600107 if (dev->flags & DM_FLAG_NAME_ALLOCED)
Simon Glass7760ba22016-05-01 13:52:23 -0600108 free((char *)dev->name);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700109 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 */
118void 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 Glassb38ea5a2014-11-10 17:16:47 -0700126 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 Glassc23b4282015-01-25 08:27:06 -0700133 if (!size) {
134 size = dev->parent->uclass->uc_drv->
135 per_child_auto_alloc_size;
136 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700137 if (size) {
138 free(dev->parent_priv);
139 dev->parent_priv = NULL;
140 }
141 }
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900142
143 devres_release_probe(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700144}
145
Stefan Roese07456762017-04-24 09:48:02 +0200146static 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 Roese80b5bc92017-03-20 12:51:48 +0100155int device_remove(struct udevice *dev, uint flags)
Simon Glassb38ea5a2014-11-10 17:16:47 -0700156{
Simon Glassa626dff2015-03-25 12:21:54 -0600157 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700158 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 Hiblot2c556a12018-08-09 16:17:45 +0200173 ret = device_chld_remove(dev, NULL, flags);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700174 if (ret)
175 goto err;
176
Stefan Roese505045d2017-03-27 10:58:53 +0200177 /*
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 Roese07456762017-04-24 09:48:02 +0200181 if (drv->remove && flags_remove(flags, drv->flags)) {
Simon Glassb38ea5a2014-11-10 17:16:47 -0700182 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 Roese07456762017-04-24 09:48:02 +0200195 if (flags_remove(flags, drv->flags)) {
Stefan Roese505045d2017-03-27 10:58:53 +0200196 device_free(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700197
Stefan Roese505045d2017-03-27 10:58:53 +0200198 dev->seq = -1;
199 dev->flags &= ~DM_FLAG_ACTIVATED;
200 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700201
202 return ret;
203
204err_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);
208err:
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}