blob: 6b87f865e40377fc1a2e179fa4f4d394d8124abf [file] [log] [blame]
Simon Glassb38ea5a2014-11-10 17:16:47 -07001/*
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
Hans de Goedea608ba62015-07-01 20:52:58 +020021int device_unbind_children(struct udevice *dev)
Simon Glassb38ea5a2014-11-10 17:16:47 -070022{
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) {
29 ret = device_unbind(pos);
30 if (ret && !saved_ret)
31 saved_ret = ret;
32 }
33
34 return saved_ret;
35}
36
Hans de Goedea608ba62015-07-01 20:52:58 +020037int device_remove_children(struct udevice *dev)
Simon Glassb38ea5a2014-11-10 17:16:47 -070038{
39 struct udevice *pos, *n;
40 int ret;
41
42 assert(dev);
43
44 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
45 ret = device_remove(pos);
46 if (ret)
47 return ret;
48 }
49
50 return 0;
51}
52
53int device_unbind(struct udevice *dev)
54{
Simon Glassa626dff2015-03-25 12:21:54 -060055 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -070056 int ret;
57
58 if (!dev)
59 return -EINVAL;
60
61 if (dev->flags & DM_FLAG_ACTIVATED)
62 return -EINVAL;
63
64 drv = dev->driver;
65 assert(drv);
66
67 if (drv->unbind) {
68 ret = drv->unbind(dev);
69 if (ret)
70 return ret;
71 }
72
Hans de Goedea608ba62015-07-01 20:52:58 +020073 ret = device_unbind_children(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -070074 if (ret)
75 return ret;
76
Simon Glassba750492015-01-25 08:27:00 -070077 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
78 free(dev->platdata);
79 dev->platdata = NULL;
80 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020081 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
82 free(dev->uclass_platdata);
83 dev->uclass_platdata = NULL;
84 }
Simon Glass11b61732015-01-25 08:27:01 -070085 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
86 free(dev->parent_platdata);
87 dev->parent_platdata = NULL;
88 }
Simon Glassb38ea5a2014-11-10 17:16:47 -070089 ret = uclass_unbind_device(dev);
90 if (ret)
91 return ret;
92
93 if (dev->parent)
94 list_del(&dev->sibling_node);
95 free(dev);
96
97 return 0;
98}
99
100/**
101 * device_free() - Free memory buffers allocated by a device
102 * @dev: Device that is to be started
103 */
104void device_free(struct udevice *dev)
105{
106 int size;
107
108 if (dev->driver->priv_auto_alloc_size) {
109 free(dev->priv);
110 dev->priv = NULL;
111 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700112 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
113 if (size) {
114 free(dev->uclass_priv);
115 dev->uclass_priv = NULL;
116 }
117 if (dev->parent) {
118 size = dev->parent->driver->per_child_auto_alloc_size;
Simon Glassc23b4282015-01-25 08:27:06 -0700119 if (!size) {
120 size = dev->parent->uclass->uc_drv->
121 per_child_auto_alloc_size;
122 }
Simon Glassb38ea5a2014-11-10 17:16:47 -0700123 if (size) {
124 free(dev->parent_priv);
125 dev->parent_priv = NULL;
126 }
127 }
128}
129
130int device_remove(struct udevice *dev)
131{
Simon Glassa626dff2015-03-25 12:21:54 -0600132 const struct driver *drv;
Simon Glassb38ea5a2014-11-10 17:16:47 -0700133 int ret;
134
135 if (!dev)
136 return -EINVAL;
137
138 if (!(dev->flags & DM_FLAG_ACTIVATED))
139 return 0;
140
141 drv = dev->driver;
142 assert(drv);
143
144 ret = uclass_pre_remove_device(dev);
145 if (ret)
146 return ret;
147
Hans de Goedea608ba62015-07-01 20:52:58 +0200148 ret = device_remove_children(dev);
Simon Glassb38ea5a2014-11-10 17:16:47 -0700149 if (ret)
150 goto err;
151
152 if (drv->remove) {
153 ret = drv->remove(dev);
154 if (ret)
155 goto err_remove;
156 }
157
158 if (dev->parent && dev->parent->driver->child_post_remove) {
159 ret = dev->parent->driver->child_post_remove(dev);
160 if (ret) {
161 dm_warn("%s: Device '%s' failed child_post_remove()",
162 __func__, dev->name);
163 }
164 }
165
166 device_free(dev);
167
168 dev->seq = -1;
169 dev->flags &= ~DM_FLAG_ACTIVATED;
170
171 return ret;
172
173err_remove:
174 /* We can't put the children back */
175 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
176 __func__, dev->name);
177err:
178 ret = uclass_post_probe_device(dev);
179 if (ret) {
180 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
181 __func__, dev->name);
182 }
183
184 return ret;
185}