]>
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, | |
86 | struct property *prop) | |
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; | |
96 | return of_reconfig_notify(action, &pr); | |
97 | } | |
98 | ||
d8c50088 PA |
99 | void __of_attach_node(struct device_node *np) |
100 | { | |
101 | np->sibling = np->parent->child; | |
102 | np->allnext = np->parent->allnext; | |
103 | np->parent->allnext = np; | |
104 | np->parent->child = np; | |
105 | of_node_clear_flag(np, OF_DETACHED); | |
106 | } | |
107 | ||
6afc0dc3 GL |
108 | /** |
109 | * of_attach_node() - Plug a device node into the tree and global list. | |
110 | */ | |
111 | int of_attach_node(struct device_node *np) | |
112 | { | |
113 | unsigned long flags; | |
114 | int rc; | |
115 | ||
116 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | |
117 | if (rc) | |
118 | return rc; | |
119 | ||
8a2b22a2 | 120 | mutex_lock(&of_mutex); |
6afc0dc3 | 121 | raw_spin_lock_irqsave(&devtree_lock, flags); |
d8c50088 | 122 | __of_attach_node(np); |
6afc0dc3 GL |
123 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
124 | ||
8a2b22a2 GL |
125 | __of_attach_node_sysfs(np); |
126 | mutex_unlock(&of_mutex); | |
6afc0dc3 GL |
127 | return 0; |
128 | } | |
129 | ||
d8c50088 | 130 | void __of_detach_node(struct device_node *np) |
6afc0dc3 GL |
131 | { |
132 | struct device_node *parent; | |
6afc0dc3 | 133 | |
d8c50088 PA |
134 | if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) |
135 | return; | |
6afc0dc3 GL |
136 | |
137 | parent = np->parent; | |
d8c50088 PA |
138 | if (WARN_ON(!parent)) |
139 | return; | |
6afc0dc3 GL |
140 | |
141 | if (of_allnodes == np) | |
142 | of_allnodes = np->allnext; | |
143 | else { | |
144 | struct device_node *prev; | |
145 | for (prev = of_allnodes; | |
146 | prev->allnext != np; | |
147 | prev = prev->allnext) | |
148 | ; | |
149 | prev->allnext = np->allnext; | |
150 | } | |
151 | ||
152 | if (parent->child == np) | |
153 | parent->child = np->sibling; | |
154 | else { | |
155 | struct device_node *prevsib; | |
156 | for (prevsib = np->parent->child; | |
157 | prevsib->sibling != np; | |
158 | prevsib = prevsib->sibling) | |
159 | ; | |
160 | prevsib->sibling = np->sibling; | |
161 | } | |
162 | ||
163 | of_node_set_flag(np, OF_DETACHED); | |
d8c50088 PA |
164 | } |
165 | ||
166 | /** | |
167 | * of_detach_node() - "Unplug" a node from the device tree. | |
168 | * | |
169 | * The caller must hold a reference to the node. The memory associated with | |
170 | * the node is not freed until its refcount goes to zero. | |
171 | */ | |
172 | int of_detach_node(struct device_node *np) | |
173 | { | |
174 | unsigned long flags; | |
175 | int rc = 0; | |
176 | ||
177 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | |
178 | if (rc) | |
179 | return rc; | |
180 | ||
8a2b22a2 | 181 | mutex_lock(&of_mutex); |
d8c50088 PA |
182 | raw_spin_lock_irqsave(&devtree_lock, flags); |
183 | __of_detach_node(np); | |
6afc0dc3 GL |
184 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
185 | ||
8a2b22a2 GL |
186 | __of_detach_node_sysfs(np); |
187 | mutex_unlock(&of_mutex); | |
6afc0dc3 GL |
188 | return rc; |
189 | } | |
190 | ||
191 | /** | |
192 | * of_node_release() - release a dynamically allocated node | |
193 | * @kref: kref element of the node to be released | |
194 | * | |
195 | * In of_node_put() this function is passed to kref_put() as the destructor. | |
196 | */ | |
197 | void of_node_release(struct kobject *kobj) | |
198 | { | |
199 | struct device_node *node = kobj_to_device_node(kobj); | |
200 | struct property *prop = node->properties; | |
201 | ||
202 | /* We should never be releasing nodes that haven't been detached. */ | |
203 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
204 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
205 | dump_stack(); | |
206 | return; | |
207 | } | |
208 | ||
209 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
210 | return; | |
211 | ||
212 | while (prop) { | |
213 | struct property *next = prop->next; | |
214 | kfree(prop->name); | |
215 | kfree(prop->value); | |
216 | kfree(prop); | |
217 | prop = next; | |
218 | ||
219 | if (!prop) { | |
220 | prop = node->deadprops; | |
221 | node->deadprops = NULL; | |
222 | } | |
223 | } | |
224 | kfree(node->full_name); | |
225 | kfree(node->data); | |
226 | kfree(node); | |
227 | } | |
69843396 PA |
228 | |
229 | /** | |
230 | * __of_prop_dup - Copy a property dynamically. | |
231 | * @prop: Property to copy | |
232 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
233 | * | |
234 | * Copy a property by dynamically allocating the memory of both the | |
235 | * property stucture and the property name & contents. The property's | |
236 | * flags have the OF_DYNAMIC bit set so that we can differentiate between | |
237 | * dynamically allocated properties and not. | |
238 | * Returns the newly allocated property or NULL on out of memory error. | |
239 | */ | |
240 | struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) | |
241 | { | |
242 | struct property *new; | |
243 | ||
244 | new = kzalloc(sizeof(*new), allocflags); | |
245 | if (!new) | |
246 | return NULL; | |
247 | ||
248 | /* | |
249 | * NOTE: There is no check for zero length value. | |
250 | * In case of a boolean property This will allocate a value | |
251 | * of zero bytes. We do this to work around the use | |
252 | * of of_get_property() calls on boolean values. | |
253 | */ | |
254 | new->name = kstrdup(prop->name, allocflags); | |
255 | new->value = kmemdup(prop->value, prop->length, allocflags); | |
256 | new->length = prop->length; | |
257 | if (!new->name || !new->value) | |
258 | goto err_free; | |
259 | ||
260 | /* mark the property as dynamic */ | |
261 | of_property_set_flag(new, OF_DYNAMIC); | |
262 | ||
263 | return new; | |
264 | ||
265 | err_free: | |
266 | kfree(new->name); | |
267 | kfree(new->value); | |
268 | kfree(new); | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | /** | |
273 | * __of_node_alloc() - Create an empty device node dynamically. | |
274 | * @full_name: Full name of the new device node | |
275 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
276 | * | |
277 | * Create an empty device tree node, suitable for further modification. | |
278 | * The node data are dynamically allocated and all the node flags | |
279 | * have the OF_DYNAMIC & OF_DETACHED bits set. | |
280 | * Returns the newly allocated node or NULL on out of memory error. | |
281 | */ | |
282 | struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags) | |
283 | { | |
284 | struct device_node *node; | |
285 | ||
286 | node = kzalloc(sizeof(*node), allocflags); | |
287 | if (!node) | |
288 | return NULL; | |
289 | ||
290 | node->full_name = kstrdup(full_name, allocflags); | |
291 | of_node_set_flag(node, OF_DYNAMIC); | |
292 | of_node_set_flag(node, OF_DETACHED); | |
293 | if (!node->full_name) | |
294 | goto err_free; | |
295 | ||
296 | of_node_init(node); | |
297 | ||
298 | return node; | |
299 | ||
300 | err_free: | |
301 | kfree(node->full_name); | |
302 | kfree(node); | |
303 | return NULL; | |
304 | } |