]>
Commit | Line | Data |
---|---|---|
6afc0dc3 GL |
1 | /* |
2 | * Support for dynamic device trees. | |
3 | * | |
4 | * On some platforms, the device tree can be manipulated at runtime. | |
5 | * The routines in this section support adding, removing and changing | |
6 | * device tree nodes. | |
7 | */ | |
8 | ||
9 | #include <linux/of.h> | |
10 | #include <linux/spinlock.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/string.h> | |
13 | #include <linux/proc_fs.h> | |
14 | ||
15 | #include "of_private.h" | |
16 | ||
17 | /** | |
18 | * of_node_get() - Increment refcount of a node | |
19 | * @node: Node to inc refcount, NULL is supported to simplify writing of | |
20 | * callers | |
21 | * | |
22 | * Returns node. | |
23 | */ | |
24 | struct device_node *of_node_get(struct device_node *node) | |
25 | { | |
26 | if (node) | |
27 | kobject_get(&node->kobj); | |
28 | return node; | |
29 | } | |
30 | EXPORT_SYMBOL(of_node_get); | |
31 | ||
32 | /** | |
33 | * of_node_put() - Decrement refcount of a node | |
34 | * @node: Node to dec refcount, NULL is supported to simplify writing of | |
35 | * callers | |
36 | */ | |
37 | void of_node_put(struct device_node *node) | |
38 | { | |
39 | if (node) | |
40 | kobject_put(&node->kobj); | |
41 | } | |
42 | EXPORT_SYMBOL(of_node_put); | |
43 | ||
8a2b22a2 | 44 | void __of_detach_node_sysfs(struct device_node *np) |
6afc0dc3 GL |
45 | { |
46 | struct property *pp; | |
47 | ||
48 | BUG_ON(!of_node_is_initialized(np)); | |
8a2b22a2 GL |
49 | if (!of_kset) |
50 | return; | |
6afc0dc3 GL |
51 | |
52 | /* only remove properties if on sysfs */ | |
53 | if (of_node_is_attached(np)) { | |
54 | for_each_property_of_node(np, pp) | |
55 | sysfs_remove_bin_file(&np->kobj, &pp->attr); | |
56 | kobject_del(&np->kobj); | |
57 | } | |
58 | ||
59 | /* finally remove the kobj_init ref */ | |
60 | of_node_put(np); | |
61 | } | |
62 | ||
63 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); | |
64 | ||
65 | int of_reconfig_notifier_register(struct notifier_block *nb) | |
66 | { | |
67 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | |
68 | } | |
69 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); | |
70 | ||
71 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | |
72 | { | |
73 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | |
74 | } | |
75 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); | |
76 | ||
77 | int of_reconfig_notify(unsigned long action, void *p) | |
78 | { | |
79 | int rc; | |
80 | ||
81 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); | |
82 | return notifier_to_errno(rc); | |
83 | } | |
84 | ||
85 | int of_property_notify(int action, struct device_node *np, | |
259092a3 | 86 | struct property *prop, struct property *oldprop) |
6afc0dc3 GL |
87 | { |
88 | struct of_prop_reconfig pr; | |
89 | ||
90 | /* only call notifiers if the node is attached */ | |
91 | if (!of_node_is_attached(np)) | |
92 | return 0; | |
93 | ||
94 | pr.dn = np; | |
95 | pr.prop = prop; | |
259092a3 | 96 | pr.old_prop = oldprop; |
6afc0dc3 GL |
97 | return of_reconfig_notify(action, &pr); |
98 | } | |
99 | ||
d8c50088 PA |
100 | void __of_attach_node(struct device_node *np) |
101 | { | |
a25095d4 GL |
102 | const __be32 *phandle; |
103 | int sz; | |
104 | ||
105 | np->name = __of_get_property(np, "name", NULL) ? : "<NULL>"; | |
106 | np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>"; | |
107 | ||
108 | phandle = __of_get_property(np, "phandle", &sz); | |
109 | if (!phandle) | |
110 | phandle = __of_get_property(np, "linux,phandle", &sz); | |
111 | if (IS_ENABLED(PPC_PSERIES) && !phandle) | |
112 | phandle = __of_get_property(np, "ibm,phandle", &sz); | |
113 | np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0; | |
114 | ||
6162dbe4 | 115 | np->child = NULL; |
d8c50088 PA |
116 | np->sibling = np->parent->child; |
117 | np->allnext = np->parent->allnext; | |
118 | np->parent->allnext = np; | |
119 | np->parent->child = np; | |
120 | of_node_clear_flag(np, OF_DETACHED); | |
121 | } | |
122 | ||
6afc0dc3 GL |
123 | /** |
124 | * of_attach_node() - Plug a device node into the tree and global list. | |
125 | */ | |
126 | int of_attach_node(struct device_node *np) | |
127 | { | |
128 | unsigned long flags; | |
6afc0dc3 | 129 | |
8a2b22a2 | 130 | mutex_lock(&of_mutex); |
6afc0dc3 | 131 | raw_spin_lock_irqsave(&devtree_lock, flags); |
d8c50088 | 132 | __of_attach_node(np); |
6afc0dc3 GL |
133 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
134 | ||
8a2b22a2 GL |
135 | __of_attach_node_sysfs(np); |
136 | mutex_unlock(&of_mutex); | |
259092a3 GL |
137 | |
138 | of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | |
139 | ||
6afc0dc3 GL |
140 | return 0; |
141 | } | |
142 | ||
d8c50088 | 143 | void __of_detach_node(struct device_node *np) |
6afc0dc3 GL |
144 | { |
145 | struct device_node *parent; | |
6afc0dc3 | 146 | |
d8c50088 PA |
147 | if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) |
148 | return; | |
6afc0dc3 GL |
149 | |
150 | parent = np->parent; | |
d8c50088 PA |
151 | if (WARN_ON(!parent)) |
152 | return; | |
6afc0dc3 GL |
153 | |
154 | if (of_allnodes == np) | |
155 | of_allnodes = np->allnext; | |
156 | else { | |
157 | struct device_node *prev; | |
158 | for (prev = of_allnodes; | |
159 | prev->allnext != np; | |
160 | prev = prev->allnext) | |
161 | ; | |
162 | prev->allnext = np->allnext; | |
163 | } | |
164 | ||
165 | if (parent->child == np) | |
166 | parent->child = np->sibling; | |
167 | else { | |
168 | struct device_node *prevsib; | |
169 | for (prevsib = np->parent->child; | |
170 | prevsib->sibling != np; | |
171 | prevsib = prevsib->sibling) | |
172 | ; | |
173 | prevsib->sibling = np->sibling; | |
174 | } | |
175 | ||
176 | of_node_set_flag(np, OF_DETACHED); | |
d8c50088 PA |
177 | } |
178 | ||
179 | /** | |
180 | * of_detach_node() - "Unplug" a node from the device tree. | |
181 | * | |
182 | * The caller must hold a reference to the node. The memory associated with | |
183 | * the node is not freed until its refcount goes to zero. | |
184 | */ | |
185 | int of_detach_node(struct device_node *np) | |
186 | { | |
187 | unsigned long flags; | |
188 | int rc = 0; | |
189 | ||
8a2b22a2 | 190 | mutex_lock(&of_mutex); |
d8c50088 PA |
191 | raw_spin_lock_irqsave(&devtree_lock, flags); |
192 | __of_detach_node(np); | |
6afc0dc3 GL |
193 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
194 | ||
8a2b22a2 GL |
195 | __of_detach_node_sysfs(np); |
196 | mutex_unlock(&of_mutex); | |
259092a3 GL |
197 | |
198 | of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | |
199 | ||
6afc0dc3 GL |
200 | return rc; |
201 | } | |
202 | ||
203 | /** | |
204 | * of_node_release() - release a dynamically allocated node | |
205 | * @kref: kref element of the node to be released | |
206 | * | |
207 | * In of_node_put() this function is passed to kref_put() as the destructor. | |
208 | */ | |
209 | void of_node_release(struct kobject *kobj) | |
210 | { | |
211 | struct device_node *node = kobj_to_device_node(kobj); | |
212 | struct property *prop = node->properties; | |
213 | ||
214 | /* We should never be releasing nodes that haven't been detached. */ | |
215 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
216 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
217 | dump_stack(); | |
218 | return; | |
219 | } | |
220 | ||
221 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
222 | return; | |
223 | ||
224 | while (prop) { | |
225 | struct property *next = prop->next; | |
226 | kfree(prop->name); | |
227 | kfree(prop->value); | |
228 | kfree(prop); | |
229 | prop = next; | |
230 | ||
231 | if (!prop) { | |
232 | prop = node->deadprops; | |
233 | node->deadprops = NULL; | |
234 | } | |
235 | } | |
236 | kfree(node->full_name); | |
237 | kfree(node->data); | |
238 | kfree(node); | |
239 | } | |
69843396 PA |
240 | |
241 | /** | |
242 | * __of_prop_dup - Copy a property dynamically. | |
243 | * @prop: Property to copy | |
244 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
245 | * | |
246 | * Copy a property by dynamically allocating the memory of both the | |
247 | * property stucture and the property name & contents. The property's | |
248 | * flags have the OF_DYNAMIC bit set so that we can differentiate between | |
249 | * dynamically allocated properties and not. | |
250 | * Returns the newly allocated property or NULL on out of memory error. | |
251 | */ | |
252 | struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) | |
253 | { | |
254 | struct property *new; | |
255 | ||
256 | new = kzalloc(sizeof(*new), allocflags); | |
257 | if (!new) | |
258 | return NULL; | |
259 | ||
260 | /* | |
261 | * NOTE: There is no check for zero length value. | |
262 | * In case of a boolean property This will allocate a value | |
263 | * of zero bytes. We do this to work around the use | |
264 | * of of_get_property() calls on boolean values. | |
265 | */ | |
266 | new->name = kstrdup(prop->name, allocflags); | |
267 | new->value = kmemdup(prop->value, prop->length, allocflags); | |
268 | new->length = prop->length; | |
269 | if (!new->name || !new->value) | |
270 | goto err_free; | |
271 | ||
272 | /* mark the property as dynamic */ | |
273 | of_property_set_flag(new, OF_DYNAMIC); | |
274 | ||
275 | return new; | |
276 | ||
277 | err_free: | |
278 | kfree(new->name); | |
279 | kfree(new->value); | |
280 | kfree(new); | |
281 | return NULL; | |
282 | } | |
283 | ||
284 | /** | |
285 | * __of_node_alloc() - Create an empty device node dynamically. | |
286 | * @full_name: Full name of the new device node | |
287 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
288 | * | |
289 | * Create an empty device tree node, suitable for further modification. | |
290 | * The node data are dynamically allocated and all the node flags | |
291 | * have the OF_DYNAMIC & OF_DETACHED bits set. | |
292 | * Returns the newly allocated node or NULL on out of memory error. | |
293 | */ | |
294 | struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags) | |
295 | { | |
296 | struct device_node *node; | |
297 | ||
298 | node = kzalloc(sizeof(*node), allocflags); | |
299 | if (!node) | |
300 | return NULL; | |
301 | ||
302 | node->full_name = kstrdup(full_name, allocflags); | |
303 | of_node_set_flag(node, OF_DYNAMIC); | |
304 | of_node_set_flag(node, OF_DETACHED); | |
305 | if (!node->full_name) | |
306 | goto err_free; | |
307 | ||
308 | of_node_init(node); | |
309 | ||
310 | return node; | |
311 | ||
312 | err_free: | |
313 | kfree(node->full_name); | |
314 | kfree(node); | |
315 | return NULL; | |
316 | } |