]>
Commit | Line | Data |
---|---|---|
97e873e5 SR |
1 | /* |
2 | * Procedures for creating, accessing and interpreting the device tree. | |
3 | * | |
4 | * Paul Mackerras August 1996. | |
5 | * Copyright (C) 1996-2005 Paul Mackerras. | |
6 | * | |
7 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. | |
8 | * {engebret|bergner}@us.ibm.com | |
9 | * | |
10 | * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net | |
11 | * | |
e91edcf5 GL |
12 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
13 | * Grant Likely. | |
97e873e5 SR |
14 | * |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version | |
18 | * 2 of the License, or (at your option) any later version. | |
19 | */ | |
611cad72 | 20 | #include <linux/ctype.h> |
97e873e5 SR |
21 | #include <linux/module.h> |
22 | #include <linux/of.h> | |
581b605a | 23 | #include <linux/spinlock.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
a9f2f63a | 25 | #include <linux/proc_fs.h> |
581b605a | 26 | |
611cad72 SG |
27 | /** |
28 | * struct alias_prop - Alias property in 'aliases' node | |
29 | * @link: List node to link the structure in aliases_lookup list | |
30 | * @alias: Alias property name | |
31 | * @np: Pointer to device_node that the alias stands for | |
32 | * @id: Index value from end of alias name | |
33 | * @stem: Alias string without the index | |
34 | * | |
35 | * The structure represents one alias property of 'aliases' node as | |
36 | * an entry in aliases_lookup list. | |
37 | */ | |
38 | struct alias_prop { | |
39 | struct list_head link; | |
40 | const char *alias; | |
41 | struct device_node *np; | |
42 | int id; | |
43 | char stem[0]; | |
44 | }; | |
45 | ||
46 | static LIST_HEAD(aliases_lookup); | |
47 | ||
465aac6d RD |
48 | struct device_node *of_allnodes; |
49 | EXPORT_SYMBOL(of_allnodes); | |
fc0bdae4 | 50 | struct device_node *of_chosen; |
611cad72 SG |
51 | struct device_node *of_aliases; |
52 | ||
53 | static DEFINE_MUTEX(of_aliases_mutex); | |
1ef4d424 | 54 | |
581b605a SR |
55 | /* use when traversing tree through the allnext, child, sibling, |
56 | * or parent members of struct device_node. | |
57 | */ | |
58 | DEFINE_RWLOCK(devtree_lock); | |
97e873e5 SR |
59 | |
60 | int of_n_addr_cells(struct device_node *np) | |
61 | { | |
a9fadeef | 62 | const __be32 *ip; |
97e873e5 SR |
63 | |
64 | do { | |
65 | if (np->parent) | |
66 | np = np->parent; | |
67 | ip = of_get_property(np, "#address-cells", NULL); | |
68 | if (ip) | |
33714881 | 69 | return be32_to_cpup(ip); |
97e873e5 SR |
70 | } while (np->parent); |
71 | /* No #address-cells property for the root node */ | |
72 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; | |
73 | } | |
74 | EXPORT_SYMBOL(of_n_addr_cells); | |
75 | ||
76 | int of_n_size_cells(struct device_node *np) | |
77 | { | |
a9fadeef | 78 | const __be32 *ip; |
97e873e5 SR |
79 | |
80 | do { | |
81 | if (np->parent) | |
82 | np = np->parent; | |
83 | ip = of_get_property(np, "#size-cells", NULL); | |
84 | if (ip) | |
33714881 | 85 | return be32_to_cpup(ip); |
97e873e5 SR |
86 | } while (np->parent); |
87 | /* No #size-cells property for the root node */ | |
88 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; | |
89 | } | |
90 | EXPORT_SYMBOL(of_n_size_cells); | |
91 | ||
0f22dd39 | 92 | #if defined(CONFIG_OF_DYNAMIC) |
923f7e30 GL |
93 | /** |
94 | * of_node_get - Increment refcount of a node | |
95 | * @node: Node to inc refcount, NULL is supported to | |
96 | * simplify writing of callers | |
97 | * | |
98 | * Returns node. | |
99 | */ | |
100 | struct device_node *of_node_get(struct device_node *node) | |
101 | { | |
102 | if (node) | |
103 | kref_get(&node->kref); | |
104 | return node; | |
105 | } | |
106 | EXPORT_SYMBOL(of_node_get); | |
107 | ||
108 | static inline struct device_node *kref_to_device_node(struct kref *kref) | |
109 | { | |
110 | return container_of(kref, struct device_node, kref); | |
111 | } | |
112 | ||
113 | /** | |
114 | * of_node_release - release a dynamically allocated node | |
115 | * @kref: kref element of the node to be released | |
116 | * | |
117 | * In of_node_put() this function is passed to kref_put() | |
118 | * as the destructor. | |
119 | */ | |
120 | static void of_node_release(struct kref *kref) | |
121 | { | |
122 | struct device_node *node = kref_to_device_node(kref); | |
123 | struct property *prop = node->properties; | |
124 | ||
125 | /* We should never be releasing nodes that haven't been detached. */ | |
126 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
127 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
128 | dump_stack(); | |
129 | kref_init(&node->kref); | |
130 | return; | |
131 | } | |
132 | ||
133 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
134 | return; | |
135 | ||
136 | while (prop) { | |
137 | struct property *next = prop->next; | |
138 | kfree(prop->name); | |
139 | kfree(prop->value); | |
140 | kfree(prop); | |
141 | prop = next; | |
142 | ||
143 | if (!prop) { | |
144 | prop = node->deadprops; | |
145 | node->deadprops = NULL; | |
146 | } | |
147 | } | |
148 | kfree(node->full_name); | |
149 | kfree(node->data); | |
150 | kfree(node); | |
151 | } | |
152 | ||
153 | /** | |
154 | * of_node_put - Decrement refcount of a node | |
155 | * @node: Node to dec refcount, NULL is supported to | |
156 | * simplify writing of callers | |
157 | * | |
158 | */ | |
159 | void of_node_put(struct device_node *node) | |
160 | { | |
161 | if (node) | |
162 | kref_put(&node->kref, of_node_release); | |
163 | } | |
164 | EXPORT_SYMBOL(of_node_put); | |
0f22dd39 | 165 | #endif /* CONFIG_OF_DYNAMIC */ |
923f7e30 | 166 | |
581b605a SR |
167 | struct property *of_find_property(const struct device_node *np, |
168 | const char *name, | |
169 | int *lenp) | |
170 | { | |
171 | struct property *pp; | |
172 | ||
64e4566f TT |
173 | if (!np) |
174 | return NULL; | |
175 | ||
581b605a | 176 | read_lock(&devtree_lock); |
a3a7cab1 | 177 | for (pp = np->properties; pp; pp = pp->next) { |
581b605a | 178 | if (of_prop_cmp(pp->name, name) == 0) { |
a3a7cab1 | 179 | if (lenp) |
581b605a SR |
180 | *lenp = pp->length; |
181 | break; | |
182 | } | |
183 | } | |
184 | read_unlock(&devtree_lock); | |
185 | ||
186 | return pp; | |
187 | } | |
188 | EXPORT_SYMBOL(of_find_property); | |
189 | ||
e91edcf5 GL |
190 | /** |
191 | * of_find_all_nodes - Get next node in global list | |
192 | * @prev: Previous node or NULL to start iteration | |
193 | * of_node_put() will be called on it | |
194 | * | |
195 | * Returns a node pointer with refcount incremented, use | |
196 | * of_node_put() on it when done. | |
197 | */ | |
198 | struct device_node *of_find_all_nodes(struct device_node *prev) | |
199 | { | |
200 | struct device_node *np; | |
201 | ||
202 | read_lock(&devtree_lock); | |
465aac6d | 203 | np = prev ? prev->allnext : of_allnodes; |
e91edcf5 GL |
204 | for (; np != NULL; np = np->allnext) |
205 | if (of_node_get(np)) | |
206 | break; | |
207 | of_node_put(prev); | |
208 | read_unlock(&devtree_lock); | |
209 | return np; | |
210 | } | |
211 | EXPORT_SYMBOL(of_find_all_nodes); | |
212 | ||
97e873e5 SR |
213 | /* |
214 | * Find a property with a given name for a given node | |
215 | * and return the value. | |
216 | */ | |
217 | const void *of_get_property(const struct device_node *np, const char *name, | |
218 | int *lenp) | |
219 | { | |
220 | struct property *pp = of_find_property(np, name, lenp); | |
221 | ||
222 | return pp ? pp->value : NULL; | |
223 | } | |
224 | EXPORT_SYMBOL(of_get_property); | |
0081cbc3 SR |
225 | |
226 | /** Checks if the given "compat" string matches one of the strings in | |
227 | * the device's "compatible" property | |
228 | */ | |
229 | int of_device_is_compatible(const struct device_node *device, | |
230 | const char *compat) | |
231 | { | |
232 | const char* cp; | |
233 | int cplen, l; | |
234 | ||
235 | cp = of_get_property(device, "compatible", &cplen); | |
236 | if (cp == NULL) | |
237 | return 0; | |
238 | while (cplen > 0) { | |
239 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) | |
240 | return 1; | |
241 | l = strlen(cp) + 1; | |
242 | cp += l; | |
243 | cplen -= l; | |
244 | } | |
245 | ||
246 | return 0; | |
247 | } | |
248 | EXPORT_SYMBOL(of_device_is_compatible); | |
e679c5f4 | 249 | |
1f43cfb9 | 250 | /** |
71a157e8 | 251 | * of_machine_is_compatible - Test root of device tree for a given compatible value |
1f43cfb9 GL |
252 | * @compat: compatible string to look for in root node's compatible property. |
253 | * | |
254 | * Returns true if the root node has the given value in its | |
255 | * compatible property. | |
256 | */ | |
71a157e8 | 257 | int of_machine_is_compatible(const char *compat) |
1f43cfb9 GL |
258 | { |
259 | struct device_node *root; | |
260 | int rc = 0; | |
261 | ||
262 | root = of_find_node_by_path("/"); | |
263 | if (root) { | |
264 | rc = of_device_is_compatible(root, compat); | |
265 | of_node_put(root); | |
266 | } | |
267 | return rc; | |
268 | } | |
71a157e8 | 269 | EXPORT_SYMBOL(of_machine_is_compatible); |
1f43cfb9 | 270 | |
834d97d4 JB |
271 | /** |
272 | * of_device_is_available - check if a device is available for use | |
273 | * | |
274 | * @device: Node to check for availability | |
275 | * | |
276 | * Returns 1 if the status property is absent or set to "okay" or "ok", | |
277 | * 0 otherwise | |
278 | */ | |
279 | int of_device_is_available(const struct device_node *device) | |
280 | { | |
281 | const char *status; | |
282 | int statlen; | |
283 | ||
284 | status = of_get_property(device, "status", &statlen); | |
285 | if (status == NULL) | |
286 | return 1; | |
287 | ||
288 | if (statlen > 0) { | |
289 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) | |
290 | return 1; | |
291 | } | |
292 | ||
293 | return 0; | |
294 | } | |
295 | EXPORT_SYMBOL(of_device_is_available); | |
296 | ||
e679c5f4 SR |
297 | /** |
298 | * of_get_parent - Get a node's parent if any | |
299 | * @node: Node to get parent | |
300 | * | |
301 | * Returns a node pointer with refcount incremented, use | |
302 | * of_node_put() on it when done. | |
303 | */ | |
304 | struct device_node *of_get_parent(const struct device_node *node) | |
305 | { | |
306 | struct device_node *np; | |
307 | ||
308 | if (!node) | |
309 | return NULL; | |
310 | ||
311 | read_lock(&devtree_lock); | |
312 | np = of_node_get(node->parent); | |
313 | read_unlock(&devtree_lock); | |
314 | return np; | |
315 | } | |
316 | EXPORT_SYMBOL(of_get_parent); | |
d1cd355a | 317 | |
f4eb0107 ME |
318 | /** |
319 | * of_get_next_parent - Iterate to a node's parent | |
320 | * @node: Node to get parent of | |
321 | * | |
322 | * This is like of_get_parent() except that it drops the | |
323 | * refcount on the passed node, making it suitable for iterating | |
324 | * through a node's parents. | |
325 | * | |
326 | * Returns a node pointer with refcount incremented, use | |
327 | * of_node_put() on it when done. | |
328 | */ | |
329 | struct device_node *of_get_next_parent(struct device_node *node) | |
330 | { | |
331 | struct device_node *parent; | |
332 | ||
333 | if (!node) | |
334 | return NULL; | |
335 | ||
336 | read_lock(&devtree_lock); | |
337 | parent = of_node_get(node->parent); | |
338 | of_node_put(node); | |
339 | read_unlock(&devtree_lock); | |
340 | return parent; | |
341 | } | |
342 | ||
d1cd355a SR |
343 | /** |
344 | * of_get_next_child - Iterate a node childs | |
345 | * @node: parent node | |
346 | * @prev: previous child of the parent node, or NULL to get first | |
347 | * | |
348 | * Returns a node pointer with refcount incremented, use | |
349 | * of_node_put() on it when done. | |
350 | */ | |
351 | struct device_node *of_get_next_child(const struct device_node *node, | |
352 | struct device_node *prev) | |
353 | { | |
354 | struct device_node *next; | |
355 | ||
356 | read_lock(&devtree_lock); | |
357 | next = prev ? prev->sibling : node->child; | |
358 | for (; next; next = next->sibling) | |
359 | if (of_node_get(next)) | |
360 | break; | |
361 | of_node_put(prev); | |
362 | read_unlock(&devtree_lock); | |
363 | return next; | |
364 | } | |
365 | EXPORT_SYMBOL(of_get_next_child); | |
1ef4d424 | 366 | |
3296193d TT |
367 | /** |
368 | * of_get_next_available_child - Find the next available child node | |
369 | * @node: parent node | |
370 | * @prev: previous child of the parent node, or NULL to get first | |
371 | * | |
372 | * This function is like of_get_next_child(), except that it | |
373 | * automatically skips any disabled nodes (i.e. status = "disabled"). | |
374 | */ | |
375 | struct device_node *of_get_next_available_child(const struct device_node *node, | |
376 | struct device_node *prev) | |
377 | { | |
378 | struct device_node *next; | |
379 | ||
380 | read_lock(&devtree_lock); | |
381 | next = prev ? prev->sibling : node->child; | |
382 | for (; next; next = next->sibling) { | |
383 | if (!of_device_is_available(next)) | |
384 | continue; | |
385 | if (of_node_get(next)) | |
386 | break; | |
387 | } | |
388 | of_node_put(prev); | |
389 | read_unlock(&devtree_lock); | |
390 | return next; | |
391 | } | |
392 | EXPORT_SYMBOL(of_get_next_available_child); | |
393 | ||
9c19761a SK |
394 | /** |
395 | * of_get_child_by_name - Find the child node by name for a given parent | |
396 | * @node: parent node | |
397 | * @name: child name to look for. | |
398 | * | |
399 | * This function looks for child node for given matching name | |
400 | * | |
401 | * Returns a node pointer if found, with refcount incremented, use | |
402 | * of_node_put() on it when done. | |
403 | * Returns NULL if node is not found. | |
404 | */ | |
405 | struct device_node *of_get_child_by_name(const struct device_node *node, | |
406 | const char *name) | |
407 | { | |
408 | struct device_node *child; | |
409 | ||
410 | for_each_child_of_node(node, child) | |
411 | if (child->name && (of_node_cmp(child->name, name) == 0)) | |
412 | break; | |
413 | return child; | |
414 | } | |
415 | EXPORT_SYMBOL(of_get_child_by_name); | |
416 | ||
1ef4d424 SR |
417 | /** |
418 | * of_find_node_by_path - Find a node matching a full OF path | |
419 | * @path: The full path to match | |
420 | * | |
421 | * Returns a node pointer with refcount incremented, use | |
422 | * of_node_put() on it when done. | |
423 | */ | |
424 | struct device_node *of_find_node_by_path(const char *path) | |
425 | { | |
465aac6d | 426 | struct device_node *np = of_allnodes; |
1ef4d424 SR |
427 | |
428 | read_lock(&devtree_lock); | |
429 | for (; np; np = np->allnext) { | |
430 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) | |
431 | && of_node_get(np)) | |
432 | break; | |
433 | } | |
434 | read_unlock(&devtree_lock); | |
435 | return np; | |
436 | } | |
437 | EXPORT_SYMBOL(of_find_node_by_path); | |
438 | ||
439 | /** | |
440 | * of_find_node_by_name - Find a node by its "name" property | |
441 | * @from: The node to start searching from or NULL, the node | |
442 | * you pass will not be searched, only the next one | |
443 | * will; typically, you pass what the previous call | |
444 | * returned. of_node_put() will be called on it | |
445 | * @name: The name string to match against | |
446 | * | |
447 | * Returns a node pointer with refcount incremented, use | |
448 | * of_node_put() on it when done. | |
449 | */ | |
450 | struct device_node *of_find_node_by_name(struct device_node *from, | |
451 | const char *name) | |
452 | { | |
453 | struct device_node *np; | |
454 | ||
455 | read_lock(&devtree_lock); | |
465aac6d | 456 | np = from ? from->allnext : of_allnodes; |
1ef4d424 SR |
457 | for (; np; np = np->allnext) |
458 | if (np->name && (of_node_cmp(np->name, name) == 0) | |
459 | && of_node_get(np)) | |
460 | break; | |
461 | of_node_put(from); | |
462 | read_unlock(&devtree_lock); | |
463 | return np; | |
464 | } | |
465 | EXPORT_SYMBOL(of_find_node_by_name); | |
466 | ||
467 | /** | |
468 | * of_find_node_by_type - Find a node by its "device_type" property | |
469 | * @from: The node to start searching from, or NULL to start searching | |
470 | * the entire device tree. The node you pass will not be | |
471 | * searched, only the next one will; typically, you pass | |
472 | * what the previous call returned. of_node_put() will be | |
473 | * called on from for you. | |
474 | * @type: The type string to match against | |
475 | * | |
476 | * Returns a node pointer with refcount incremented, use | |
477 | * of_node_put() on it when done. | |
478 | */ | |
479 | struct device_node *of_find_node_by_type(struct device_node *from, | |
480 | const char *type) | |
481 | { | |
482 | struct device_node *np; | |
483 | ||
484 | read_lock(&devtree_lock); | |
465aac6d | 485 | np = from ? from->allnext : of_allnodes; |
1ef4d424 SR |
486 | for (; np; np = np->allnext) |
487 | if (np->type && (of_node_cmp(np->type, type) == 0) | |
488 | && of_node_get(np)) | |
489 | break; | |
490 | of_node_put(from); | |
491 | read_unlock(&devtree_lock); | |
492 | return np; | |
493 | } | |
494 | EXPORT_SYMBOL(of_find_node_by_type); | |
495 | ||
496 | /** | |
497 | * of_find_compatible_node - Find a node based on type and one of the | |
498 | * tokens in its "compatible" property | |
499 | * @from: The node to start searching from or NULL, the node | |
500 | * you pass will not be searched, only the next one | |
501 | * will; typically, you pass what the previous call | |
502 | * returned. of_node_put() will be called on it | |
503 | * @type: The type string to match "device_type" or NULL to ignore | |
504 | * @compatible: The string to match to one of the tokens in the device | |
505 | * "compatible" list. | |
506 | * | |
507 | * Returns a node pointer with refcount incremented, use | |
508 | * of_node_put() on it when done. | |
509 | */ | |
510 | struct device_node *of_find_compatible_node(struct device_node *from, | |
511 | const char *type, const char *compatible) | |
512 | { | |
513 | struct device_node *np; | |
514 | ||
515 | read_lock(&devtree_lock); | |
465aac6d | 516 | np = from ? from->allnext : of_allnodes; |
1ef4d424 SR |
517 | for (; np; np = np->allnext) { |
518 | if (type | |
519 | && !(np->type && (of_node_cmp(np->type, type) == 0))) | |
520 | continue; | |
521 | if (of_device_is_compatible(np, compatible) && of_node_get(np)) | |
522 | break; | |
523 | } | |
524 | of_node_put(from); | |
525 | read_unlock(&devtree_lock); | |
526 | return np; | |
527 | } | |
528 | EXPORT_SYMBOL(of_find_compatible_node); | |
283029d1 | 529 | |
1e291b14 ME |
530 | /** |
531 | * of_find_node_with_property - Find a node which has a property with | |
532 | * the given name. | |
533 | * @from: The node to start searching from or NULL, the node | |
534 | * you pass will not be searched, only the next one | |
535 | * will; typically, you pass what the previous call | |
536 | * returned. of_node_put() will be called on it | |
537 | * @prop_name: The name of the property to look for. | |
538 | * | |
539 | * Returns a node pointer with refcount incremented, use | |
540 | * of_node_put() on it when done. | |
541 | */ | |
542 | struct device_node *of_find_node_with_property(struct device_node *from, | |
543 | const char *prop_name) | |
544 | { | |
545 | struct device_node *np; | |
546 | struct property *pp; | |
547 | ||
548 | read_lock(&devtree_lock); | |
465aac6d | 549 | np = from ? from->allnext : of_allnodes; |
1e291b14 | 550 | for (; np; np = np->allnext) { |
a3a7cab1 | 551 | for (pp = np->properties; pp; pp = pp->next) { |
1e291b14 ME |
552 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
553 | of_node_get(np); | |
554 | goto out; | |
555 | } | |
556 | } | |
557 | } | |
558 | out: | |
559 | of_node_put(from); | |
560 | read_unlock(&devtree_lock); | |
561 | return np; | |
562 | } | |
563 | EXPORT_SYMBOL(of_find_node_with_property); | |
564 | ||
283029d1 GL |
565 | /** |
566 | * of_match_node - Tell if an device_node has a matching of_match structure | |
567 | * @matches: array of of device match structures to search in | |
568 | * @node: the of device structure to match against | |
569 | * | |
570 | * Low level utility function used by device matching. | |
571 | */ | |
572 | const struct of_device_id *of_match_node(const struct of_device_id *matches, | |
573 | const struct device_node *node) | |
574 | { | |
a52f07ec GL |
575 | if (!matches) |
576 | return NULL; | |
577 | ||
283029d1 GL |
578 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
579 | int match = 1; | |
580 | if (matches->name[0]) | |
581 | match &= node->name | |
582 | && !strcmp(matches->name, node->name); | |
583 | if (matches->type[0]) | |
584 | match &= node->type | |
585 | && !strcmp(matches->type, node->type); | |
bc51b0c2 LT |
586 | if (matches->compatible[0]) |
587 | match &= of_device_is_compatible(node, | |
588 | matches->compatible); | |
589 | if (match) | |
283029d1 GL |
590 | return matches; |
591 | matches++; | |
592 | } | |
593 | return NULL; | |
594 | } | |
595 | EXPORT_SYMBOL(of_match_node); | |
596 | ||
597 | /** | |
50c8af4c SW |
598 | * of_find_matching_node_and_match - Find a node based on an of_device_id |
599 | * match table. | |
283029d1 GL |
600 | * @from: The node to start searching from or NULL, the node |
601 | * you pass will not be searched, only the next one | |
602 | * will; typically, you pass what the previous call | |
603 | * returned. of_node_put() will be called on it | |
604 | * @matches: array of of device match structures to search in | |
50c8af4c | 605 | * @match Updated to point at the matches entry which matched |
283029d1 GL |
606 | * |
607 | * Returns a node pointer with refcount incremented, use | |
608 | * of_node_put() on it when done. | |
609 | */ | |
50c8af4c SW |
610 | struct device_node *of_find_matching_node_and_match(struct device_node *from, |
611 | const struct of_device_id *matches, | |
612 | const struct of_device_id **match) | |
283029d1 GL |
613 | { |
614 | struct device_node *np; | |
dc71bcf1 | 615 | const struct of_device_id *m; |
283029d1 | 616 | |
50c8af4c SW |
617 | if (match) |
618 | *match = NULL; | |
619 | ||
283029d1 | 620 | read_lock(&devtree_lock); |
465aac6d | 621 | np = from ? from->allnext : of_allnodes; |
283029d1 | 622 | for (; np; np = np->allnext) { |
dc71bcf1 TA |
623 | m = of_match_node(matches, np); |
624 | if (m && of_node_get(np)) { | |
50c8af4c | 625 | if (match) |
dc71bcf1 | 626 | *match = m; |
283029d1 | 627 | break; |
50c8af4c | 628 | } |
283029d1 GL |
629 | } |
630 | of_node_put(from); | |
631 | read_unlock(&devtree_lock); | |
632 | return np; | |
633 | } | |
80c2022e | 634 | EXPORT_SYMBOL(of_find_matching_node_and_match); |
3f07af49 | 635 | |
3f07af49 GL |
636 | /** |
637 | * of_modalias_node - Lookup appropriate modalias for a device node | |
638 | * @node: pointer to a device tree node | |
639 | * @modalias: Pointer to buffer that modalias value will be copied into | |
640 | * @len: Length of modalias value | |
641 | * | |
2ffe8c5f GL |
642 | * Based on the value of the compatible property, this routine will attempt |
643 | * to choose an appropriate modalias value for a particular device tree node. | |
644 | * It does this by stripping the manufacturer prefix (as delimited by a ',') | |
645 | * from the first entry in the compatible list property. | |
3f07af49 | 646 | * |
2ffe8c5f | 647 | * This routine returns 0 on success, <0 on failure. |
3f07af49 GL |
648 | */ |
649 | int of_modalias_node(struct device_node *node, char *modalias, int len) | |
650 | { | |
2ffe8c5f GL |
651 | const char *compatible, *p; |
652 | int cplen; | |
3f07af49 GL |
653 | |
654 | compatible = of_get_property(node, "compatible", &cplen); | |
2ffe8c5f | 655 | if (!compatible || strlen(compatible) > cplen) |
3f07af49 | 656 | return -ENODEV; |
3f07af49 | 657 | p = strchr(compatible, ','); |
2ffe8c5f | 658 | strlcpy(modalias, p ? p + 1 : compatible, len); |
3f07af49 GL |
659 | return 0; |
660 | } | |
661 | EXPORT_SYMBOL_GPL(of_modalias_node); | |
662 | ||
89751a7c JK |
663 | /** |
664 | * of_find_node_by_phandle - Find a node given a phandle | |
665 | * @handle: phandle of the node to find | |
666 | * | |
667 | * Returns a node pointer with refcount incremented, use | |
668 | * of_node_put() on it when done. | |
669 | */ | |
670 | struct device_node *of_find_node_by_phandle(phandle handle) | |
671 | { | |
672 | struct device_node *np; | |
673 | ||
674 | read_lock(&devtree_lock); | |
465aac6d | 675 | for (np = of_allnodes; np; np = np->allnext) |
89751a7c JK |
676 | if (np->phandle == handle) |
677 | break; | |
678 | of_node_get(np); | |
679 | read_unlock(&devtree_lock); | |
680 | return np; | |
681 | } | |
682 | EXPORT_SYMBOL(of_find_node_by_phandle); | |
683 | ||
be193249 VK |
684 | /** |
685 | * of_property_read_u8_array - Find and read an array of u8 from a property. | |
686 | * | |
687 | * @np: device node from which the property value is to be read. | |
688 | * @propname: name of the property to be searched. | |
689 | * @out_value: pointer to return value, modified only if return value is 0. | |
690 | * @sz: number of array elements to read | |
691 | * | |
692 | * Search for a property in a device node and read 8-bit value(s) from | |
693 | * it. Returns 0 on success, -EINVAL if the property does not exist, | |
694 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | |
695 | * property data isn't large enough. | |
696 | * | |
697 | * dts entry of array should be like: | |
698 | * property = /bits/ 8 <0x50 0x60 0x70>; | |
699 | * | |
700 | * The out_value is modified only if a valid u8 value can be decoded. | |
701 | */ | |
702 | int of_property_read_u8_array(const struct device_node *np, | |
703 | const char *propname, u8 *out_values, size_t sz) | |
704 | { | |
705 | struct property *prop = of_find_property(np, propname, NULL); | |
706 | const u8 *val; | |
707 | ||
708 | if (!prop) | |
709 | return -EINVAL; | |
710 | if (!prop->value) | |
711 | return -ENODATA; | |
712 | if ((sz * sizeof(*out_values)) > prop->length) | |
713 | return -EOVERFLOW; | |
714 | ||
715 | val = prop->value; | |
716 | while (sz--) | |
717 | *out_values++ = *val++; | |
718 | return 0; | |
719 | } | |
720 | EXPORT_SYMBOL_GPL(of_property_read_u8_array); | |
721 | ||
722 | /** | |
723 | * of_property_read_u16_array - Find and read an array of u16 from a property. | |
724 | * | |
725 | * @np: device node from which the property value is to be read. | |
726 | * @propname: name of the property to be searched. | |
727 | * @out_value: pointer to return value, modified only if return value is 0. | |
728 | * @sz: number of array elements to read | |
729 | * | |
730 | * Search for a property in a device node and read 16-bit value(s) from | |
731 | * it. Returns 0 on success, -EINVAL if the property does not exist, | |
732 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | |
733 | * property data isn't large enough. | |
734 | * | |
735 | * dts entry of array should be like: | |
736 | * property = /bits/ 16 <0x5000 0x6000 0x7000>; | |
737 | * | |
738 | * The out_value is modified only if a valid u16 value can be decoded. | |
739 | */ | |
740 | int of_property_read_u16_array(const struct device_node *np, | |
741 | const char *propname, u16 *out_values, size_t sz) | |
742 | { | |
743 | struct property *prop = of_find_property(np, propname, NULL); | |
744 | const __be16 *val; | |
745 | ||
746 | if (!prop) | |
747 | return -EINVAL; | |
748 | if (!prop->value) | |
749 | return -ENODATA; | |
750 | if ((sz * sizeof(*out_values)) > prop->length) | |
751 | return -EOVERFLOW; | |
752 | ||
753 | val = prop->value; | |
754 | while (sz--) | |
755 | *out_values++ = be16_to_cpup(val++); | |
756 | return 0; | |
757 | } | |
758 | EXPORT_SYMBOL_GPL(of_property_read_u16_array); | |
759 | ||
a3b85363 | 760 | /** |
0e373639 RH |
761 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
762 | * from a property. | |
763 | * | |
a3b85363 TA |
764 | * @np: device node from which the property value is to be read. |
765 | * @propname: name of the property to be searched. | |
766 | * @out_value: pointer to return value, modified only if return value is 0. | |
be193249 | 767 | * @sz: number of array elements to read |
a3b85363 | 768 | * |
0e373639 | 769 | * Search for a property in a device node and read 32-bit value(s) from |
a3b85363 TA |
770 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
771 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | |
772 | * property data isn't large enough. | |
773 | * | |
774 | * The out_value is modified only if a valid u32 value can be decoded. | |
775 | */ | |
aac285c6 JI |
776 | int of_property_read_u32_array(const struct device_node *np, |
777 | const char *propname, u32 *out_values, | |
778 | size_t sz) | |
a3b85363 TA |
779 | { |
780 | struct property *prop = of_find_property(np, propname, NULL); | |
0e373639 | 781 | const __be32 *val; |
a3b85363 TA |
782 | |
783 | if (!prop) | |
784 | return -EINVAL; | |
785 | if (!prop->value) | |
786 | return -ENODATA; | |
0e373639 | 787 | if ((sz * sizeof(*out_values)) > prop->length) |
a3b85363 | 788 | return -EOVERFLOW; |
0e373639 RH |
789 | |
790 | val = prop->value; | |
791 | while (sz--) | |
792 | *out_values++ = be32_to_cpup(val++); | |
a3b85363 TA |
793 | return 0; |
794 | } | |
0e373639 | 795 | EXPORT_SYMBOL_GPL(of_property_read_u32_array); |
a3b85363 | 796 | |
4cd7f7a3 JI |
797 | /** |
798 | * of_property_read_u64 - Find and read a 64 bit integer from a property | |
799 | * @np: device node from which the property value is to be read. | |
800 | * @propname: name of the property to be searched. | |
801 | * @out_value: pointer to return value, modified only if return value is 0. | |
802 | * | |
803 | * Search for a property in a device node and read a 64-bit value from | |
804 | * it. Returns 0 on success, -EINVAL if the property does not exist, | |
805 | * -ENODATA if property does not have a value, and -EOVERFLOW if the | |
806 | * property data isn't large enough. | |
807 | * | |
808 | * The out_value is modified only if a valid u64 value can be decoded. | |
809 | */ | |
810 | int of_property_read_u64(const struct device_node *np, const char *propname, | |
811 | u64 *out_value) | |
812 | { | |
813 | struct property *prop = of_find_property(np, propname, NULL); | |
814 | ||
815 | if (!prop) | |
816 | return -EINVAL; | |
817 | if (!prop->value) | |
818 | return -ENODATA; | |
819 | if (sizeof(*out_value) > prop->length) | |
820 | return -EOVERFLOW; | |
821 | *out_value = of_read_number(prop->value, 2); | |
822 | return 0; | |
823 | } | |
824 | EXPORT_SYMBOL_GPL(of_property_read_u64); | |
825 | ||
a3b85363 TA |
826 | /** |
827 | * of_property_read_string - Find and read a string from a property | |
828 | * @np: device node from which the property value is to be read. | |
829 | * @propname: name of the property to be searched. | |
830 | * @out_string: pointer to null terminated return string, modified only if | |
831 | * return value is 0. | |
832 | * | |
833 | * Search for a property in a device tree node and retrieve a null | |
834 | * terminated string value (pointer to data, not a copy). Returns 0 on | |
835 | * success, -EINVAL if the property does not exist, -ENODATA if property | |
836 | * does not have a value, and -EILSEQ if the string is not null-terminated | |
837 | * within the length of the property data. | |
838 | * | |
839 | * The out_string pointer is modified only if a valid string can be decoded. | |
840 | */ | |
aac285c6 | 841 | int of_property_read_string(struct device_node *np, const char *propname, |
f09bc831 | 842 | const char **out_string) |
a3b85363 TA |
843 | { |
844 | struct property *prop = of_find_property(np, propname, NULL); | |
845 | if (!prop) | |
846 | return -EINVAL; | |
847 | if (!prop->value) | |
848 | return -ENODATA; | |
849 | if (strnlen(prop->value, prop->length) >= prop->length) | |
850 | return -EILSEQ; | |
851 | *out_string = prop->value; | |
852 | return 0; | |
853 | } | |
854 | EXPORT_SYMBOL_GPL(of_property_read_string); | |
855 | ||
4fcd15a0 BC |
856 | /** |
857 | * of_property_read_string_index - Find and read a string from a multiple | |
858 | * strings property. | |
859 | * @np: device node from which the property value is to be read. | |
860 | * @propname: name of the property to be searched. | |
861 | * @index: index of the string in the list of strings | |
862 | * @out_string: pointer to null terminated return string, modified only if | |
863 | * return value is 0. | |
864 | * | |
865 | * Search for a property in a device tree node and retrieve a null | |
866 | * terminated string value (pointer to data, not a copy) in the list of strings | |
867 | * contained in that property. | |
868 | * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if | |
869 | * property does not have a value, and -EILSEQ if the string is not | |
870 | * null-terminated within the length of the property data. | |
871 | * | |
872 | * The out_string pointer is modified only if a valid string can be decoded. | |
873 | */ | |
874 | int of_property_read_string_index(struct device_node *np, const char *propname, | |
875 | int index, const char **output) | |
876 | { | |
877 | struct property *prop = of_find_property(np, propname, NULL); | |
878 | int i = 0; | |
879 | size_t l = 0, total = 0; | |
880 | const char *p; | |
881 | ||
882 | if (!prop) | |
883 | return -EINVAL; | |
884 | if (!prop->value) | |
885 | return -ENODATA; | |
886 | if (strnlen(prop->value, prop->length) >= prop->length) | |
887 | return -EILSEQ; | |
888 | ||
889 | p = prop->value; | |
890 | ||
891 | for (i = 0; total < prop->length; total += l, p += l) { | |
892 | l = strlen(p) + 1; | |
88af7f58 | 893 | if (i++ == index) { |
4fcd15a0 BC |
894 | *output = p; |
895 | return 0; | |
896 | } | |
897 | } | |
898 | return -ENODATA; | |
899 | } | |
900 | EXPORT_SYMBOL_GPL(of_property_read_string_index); | |
901 | ||
7aff0fe3 GL |
902 | /** |
903 | * of_property_match_string() - Find string in a list and return index | |
904 | * @np: pointer to node containing string list property | |
905 | * @propname: string list property name | |
906 | * @string: pointer to string to search for in string list | |
907 | * | |
908 | * This function searches a string list property and returns the index | |
909 | * of a specific string value. | |
910 | */ | |
911 | int of_property_match_string(struct device_node *np, const char *propname, | |
912 | const char *string) | |
913 | { | |
914 | struct property *prop = of_find_property(np, propname, NULL); | |
915 | size_t l; | |
916 | int i; | |
917 | const char *p, *end; | |
918 | ||
919 | if (!prop) | |
920 | return -EINVAL; | |
921 | if (!prop->value) | |
922 | return -ENODATA; | |
923 | ||
924 | p = prop->value; | |
925 | end = p + prop->length; | |
926 | ||
927 | for (i = 0; p < end; i++, p += l) { | |
928 | l = strlen(p) + 1; | |
929 | if (p + l > end) | |
930 | return -EILSEQ; | |
931 | pr_debug("comparing %s with %s\n", string, p); | |
932 | if (strcmp(string, p) == 0) | |
933 | return i; /* Found it; return index */ | |
934 | } | |
935 | return -ENODATA; | |
936 | } | |
937 | EXPORT_SYMBOL_GPL(of_property_match_string); | |
4fcd15a0 BC |
938 | |
939 | /** | |
940 | * of_property_count_strings - Find and return the number of strings from a | |
941 | * multiple strings property. | |
942 | * @np: device node from which the property value is to be read. | |
943 | * @propname: name of the property to be searched. | |
944 | * | |
945 | * Search for a property in a device tree node and retrieve the number of null | |
946 | * terminated string contain in it. Returns the number of strings on | |
947 | * success, -EINVAL if the property does not exist, -ENODATA if property | |
948 | * does not have a value, and -EILSEQ if the string is not null-terminated | |
949 | * within the length of the property data. | |
950 | */ | |
951 | int of_property_count_strings(struct device_node *np, const char *propname) | |
952 | { | |
953 | struct property *prop = of_find_property(np, propname, NULL); | |
954 | int i = 0; | |
955 | size_t l = 0, total = 0; | |
956 | const char *p; | |
957 | ||
958 | if (!prop) | |
959 | return -EINVAL; | |
960 | if (!prop->value) | |
961 | return -ENODATA; | |
962 | if (strnlen(prop->value, prop->length) >= prop->length) | |
963 | return -EILSEQ; | |
964 | ||
965 | p = prop->value; | |
966 | ||
88af7f58 | 967 | for (i = 0; total < prop->length; total += l, p += l, i++) |
4fcd15a0 | 968 | l = strlen(p) + 1; |
88af7f58 | 969 | |
4fcd15a0 BC |
970 | return i; |
971 | } | |
972 | EXPORT_SYMBOL_GPL(of_property_count_strings); | |
973 | ||
739649c5 GL |
974 | /** |
975 | * of_parse_phandle - Resolve a phandle property to a device_node pointer | |
976 | * @np: Pointer to device node holding phandle property | |
977 | * @phandle_name: Name of property holding a phandle value | |
978 | * @index: For properties holding a table of phandles, this is the index into | |
979 | * the table | |
980 | * | |
981 | * Returns the device_node pointer with refcount incremented. Use | |
982 | * of_node_put() on it when done. | |
983 | */ | |
b8fbdc42 ST |
984 | struct device_node *of_parse_phandle(const struct device_node *np, |
985 | const char *phandle_name, int index) | |
739649c5 | 986 | { |
9a6b2e58 | 987 | const __be32 *phandle; |
739649c5 GL |
988 | int size; |
989 | ||
990 | phandle = of_get_property(np, phandle_name, &size); | |
991 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) | |
992 | return NULL; | |
993 | ||
9a6b2e58 | 994 | return of_find_node_by_phandle(be32_to_cpup(phandle + index)); |
739649c5 GL |
995 | } |
996 | EXPORT_SYMBOL(of_parse_phandle); | |
997 | ||
64b60e09 | 998 | /** |
15c9a0ac | 999 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
64b60e09 AV |
1000 | * @np: pointer to a device tree node containing a list |
1001 | * @list_name: property name that contains a list | |
1002 | * @cells_name: property name that specifies phandles' arguments count | |
1003 | * @index: index of a phandle to parse out | |
15c9a0ac | 1004 | * @out_args: optional pointer to output arguments structure (will be filled) |
64b60e09 AV |
1005 | * |
1006 | * This function is useful to parse lists of phandles and their arguments. | |
15c9a0ac GL |
1007 | * Returns 0 on success and fills out_args, on error returns appropriate |
1008 | * errno value. | |
1009 | * | |
1010 | * Caller is responsible to call of_node_put() on the returned out_args->node | |
1011 | * pointer. | |
64b60e09 AV |
1012 | * |
1013 | * Example: | |
1014 | * | |
1015 | * phandle1: node1 { | |
1016 | * #list-cells = <2>; | |
1017 | * } | |
1018 | * | |
1019 | * phandle2: node2 { | |
1020 | * #list-cells = <1>; | |
1021 | * } | |
1022 | * | |
1023 | * node3 { | |
1024 | * list = <&phandle1 1 2 &phandle2 3>; | |
1025 | * } | |
1026 | * | |
1027 | * To get a device_node of the `node2' node you may call this: | |
15c9a0ac | 1028 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
64b60e09 | 1029 | */ |
93c667ca | 1030 | int of_parse_phandle_with_args(const struct device_node *np, const char *list_name, |
64b60e09 | 1031 | const char *cells_name, int index, |
15c9a0ac | 1032 | struct of_phandle_args *out_args) |
64b60e09 | 1033 | { |
15c9a0ac GL |
1034 | const __be32 *list, *list_end; |
1035 | int size, cur_index = 0; | |
1036 | uint32_t count = 0; | |
64b60e09 | 1037 | struct device_node *node = NULL; |
15c9a0ac | 1038 | phandle phandle; |
64b60e09 | 1039 | |
15c9a0ac | 1040 | /* Retrieve the phandle list property */ |
64b60e09 | 1041 | list = of_get_property(np, list_name, &size); |
15c9a0ac | 1042 | if (!list) |
1af4c7f1 | 1043 | return -ENOENT; |
64b60e09 AV |
1044 | list_end = list + size / sizeof(*list); |
1045 | ||
15c9a0ac | 1046 | /* Loop over the phandles until all the requested entry is found */ |
64b60e09 | 1047 | while (list < list_end) { |
15c9a0ac | 1048 | count = 0; |
64b60e09 | 1049 | |
15c9a0ac GL |
1050 | /* |
1051 | * If phandle is 0, then it is an empty entry with no | |
1052 | * arguments. Skip forward to the next entry. | |
1053 | */ | |
9a6b2e58 | 1054 | phandle = be32_to_cpup(list++); |
15c9a0ac GL |
1055 | if (phandle) { |
1056 | /* | |
1057 | * Find the provider node and parse the #*-cells | |
1058 | * property to determine the argument length | |
1059 | */ | |
1060 | node = of_find_node_by_phandle(phandle); | |
1061 | if (!node) { | |
1062 | pr_err("%s: could not find phandle\n", | |
1063 | np->full_name); | |
1064 | break; | |
1065 | } | |
1066 | if (of_property_read_u32(node, cells_name, &count)) { | |
1067 | pr_err("%s: could not get %s for %s\n", | |
1068 | np->full_name, cells_name, | |
1069 | node->full_name); | |
1070 | break; | |
1071 | } | |
64b60e09 | 1072 | |
15c9a0ac GL |
1073 | /* |
1074 | * Make sure that the arguments actually fit in the | |
1075 | * remaining property data length | |
1076 | */ | |
1077 | if (list + count > list_end) { | |
1078 | pr_err("%s: arguments longer than property\n", | |
1079 | np->full_name); | |
1080 | break; | |
1081 | } | |
64b60e09 AV |
1082 | } |
1083 | ||
15c9a0ac GL |
1084 | /* |
1085 | * All of the error cases above bail out of the loop, so at | |
1086 | * this point, the parsing is successful. If the requested | |
1087 | * index matches, then fill the out_args structure and return, | |
1088 | * or return -ENOENT for an empty entry. | |
1089 | */ | |
1090 | if (cur_index == index) { | |
1091 | if (!phandle) | |
1092 | return -ENOENT; | |
1093 | ||
1094 | if (out_args) { | |
1095 | int i; | |
1096 | if (WARN_ON(count > MAX_PHANDLE_ARGS)) | |
1097 | count = MAX_PHANDLE_ARGS; | |
1098 | out_args->np = node; | |
1099 | out_args->args_count = count; | |
1100 | for (i = 0; i < count; i++) | |
1101 | out_args->args[i] = be32_to_cpup(list++); | |
1102 | } | |
1103 | return 0; | |
64b60e09 | 1104 | } |
64b60e09 AV |
1105 | |
1106 | of_node_put(node); | |
1107 | node = NULL; | |
15c9a0ac | 1108 | list += count; |
64b60e09 AV |
1109 | cur_index++; |
1110 | } | |
1111 | ||
15c9a0ac GL |
1112 | /* Loop exited without finding a valid entry; return an error */ |
1113 | if (node) | |
1114 | of_node_put(node); | |
1115 | return -EINVAL; | |
64b60e09 | 1116 | } |
15c9a0ac | 1117 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
02af11b0 | 1118 | |
1cf3d8b3 NF |
1119 | #if defined(CONFIG_OF_DYNAMIC) |
1120 | static int of_property_notify(int action, struct device_node *np, | |
1121 | struct property *prop) | |
1122 | { | |
1123 | struct of_prop_reconfig pr; | |
1124 | ||
1125 | pr.dn = np; | |
1126 | pr.prop = prop; | |
1127 | return of_reconfig_notify(action, &pr); | |
1128 | } | |
1129 | #else | |
1130 | static int of_property_notify(int action, struct device_node *np, | |
1131 | struct property *prop) | |
1132 | { | |
1133 | return 0; | |
1134 | } | |
1135 | #endif | |
1136 | ||
02af11b0 | 1137 | /** |
79d1c712 | 1138 | * of_add_property - Add a property to a node |
02af11b0 | 1139 | */ |
79d1c712 | 1140 | int of_add_property(struct device_node *np, struct property *prop) |
02af11b0 GL |
1141 | { |
1142 | struct property **next; | |
1143 | unsigned long flags; | |
1cf3d8b3 NF |
1144 | int rc; |
1145 | ||
1146 | rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop); | |
1147 | if (rc) | |
1148 | return rc; | |
02af11b0 GL |
1149 | |
1150 | prop->next = NULL; | |
1151 | write_lock_irqsave(&devtree_lock, flags); | |
1152 | next = &np->properties; | |
1153 | while (*next) { | |
1154 | if (strcmp(prop->name, (*next)->name) == 0) { | |
1155 | /* duplicate ! don't insert it */ | |
1156 | write_unlock_irqrestore(&devtree_lock, flags); | |
1157 | return -1; | |
1158 | } | |
1159 | next = &(*next)->next; | |
1160 | } | |
1161 | *next = prop; | |
1162 | write_unlock_irqrestore(&devtree_lock, flags); | |
1163 | ||
1164 | #ifdef CONFIG_PROC_DEVICETREE | |
1165 | /* try to add to proc as well if it was initialized */ | |
1166 | if (np->pde) | |
1167 | proc_device_tree_add_prop(np->pde, prop); | |
1168 | #endif /* CONFIG_PROC_DEVICETREE */ | |
1169 | ||
1170 | return 0; | |
1171 | } | |
1172 | ||
1173 | /** | |
79d1c712 | 1174 | * of_remove_property - Remove a property from a node. |
02af11b0 GL |
1175 | * |
1176 | * Note that we don't actually remove it, since we have given out | |
1177 | * who-knows-how-many pointers to the data using get-property. | |
1178 | * Instead we just move the property to the "dead properties" | |
1179 | * list, so it won't be found any more. | |
1180 | */ | |
79d1c712 | 1181 | int of_remove_property(struct device_node *np, struct property *prop) |
02af11b0 GL |
1182 | { |
1183 | struct property **next; | |
1184 | unsigned long flags; | |
1185 | int found = 0; | |
1cf3d8b3 NF |
1186 | int rc; |
1187 | ||
1188 | rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop); | |
1189 | if (rc) | |
1190 | return rc; | |
02af11b0 GL |
1191 | |
1192 | write_lock_irqsave(&devtree_lock, flags); | |
1193 | next = &np->properties; | |
1194 | while (*next) { | |
1195 | if (*next == prop) { | |
1196 | /* found the node */ | |
1197 | *next = prop->next; | |
1198 | prop->next = np->deadprops; | |
1199 | np->deadprops = prop; | |
1200 | found = 1; | |
1201 | break; | |
1202 | } | |
1203 | next = &(*next)->next; | |
1204 | } | |
1205 | write_unlock_irqrestore(&devtree_lock, flags); | |
1206 | ||
1207 | if (!found) | |
1208 | return -ENODEV; | |
1209 | ||
1210 | #ifdef CONFIG_PROC_DEVICETREE | |
1211 | /* try to remove the proc node as well */ | |
1212 | if (np->pde) | |
1213 | proc_device_tree_remove_prop(np->pde, prop); | |
1214 | #endif /* CONFIG_PROC_DEVICETREE */ | |
1215 | ||
1216 | return 0; | |
1217 | } | |
1218 | ||
1219 | /* | |
79d1c712 | 1220 | * of_update_property - Update a property in a node, if the property does |
475d0094 | 1221 | * not exist, add it. |
02af11b0 GL |
1222 | * |
1223 | * Note that we don't actually remove it, since we have given out | |
1224 | * who-knows-how-many pointers to the data using get-property. | |
1225 | * Instead we just move the property to the "dead properties" list, | |
1226 | * and add the new property to the property list | |
1227 | */ | |
79d1c712 | 1228 | int of_update_property(struct device_node *np, struct property *newprop) |
02af11b0 | 1229 | { |
475d0094 | 1230 | struct property **next, *oldprop; |
02af11b0 | 1231 | unsigned long flags; |
1cf3d8b3 NF |
1232 | int rc, found = 0; |
1233 | ||
1234 | rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop); | |
1235 | if (rc) | |
1236 | return rc; | |
02af11b0 | 1237 | |
475d0094 DA |
1238 | if (!newprop->name) |
1239 | return -EINVAL; | |
1240 | ||
1241 | oldprop = of_find_property(np, newprop->name, NULL); | |
1242 | if (!oldprop) | |
79d1c712 | 1243 | return of_add_property(np, newprop); |
475d0094 | 1244 | |
02af11b0 GL |
1245 | write_lock_irqsave(&devtree_lock, flags); |
1246 | next = &np->properties; | |
1247 | while (*next) { | |
1248 | if (*next == oldprop) { | |
1249 | /* found the node */ | |
1250 | newprop->next = oldprop->next; | |
1251 | *next = newprop; | |
1252 | oldprop->next = np->deadprops; | |
1253 | np->deadprops = oldprop; | |
1254 | found = 1; | |
1255 | break; | |
1256 | } | |
1257 | next = &(*next)->next; | |
1258 | } | |
1259 | write_unlock_irqrestore(&devtree_lock, flags); | |
1260 | ||
1261 | if (!found) | |
1262 | return -ENODEV; | |
1263 | ||
1264 | #ifdef CONFIG_PROC_DEVICETREE | |
1265 | /* try to add to proc as well if it was initialized */ | |
1266 | if (np->pde) | |
1267 | proc_device_tree_update_prop(np->pde, newprop, oldprop); | |
1268 | #endif /* CONFIG_PROC_DEVICETREE */ | |
1269 | ||
1270 | return 0; | |
1271 | } | |
fcdeb7fe GL |
1272 | |
1273 | #if defined(CONFIG_OF_DYNAMIC) | |
1274 | /* | |
1275 | * Support for dynamic device trees. | |
1276 | * | |
1277 | * On some platforms, the device tree can be manipulated at runtime. | |
1278 | * The routines in this section support adding, removing and changing | |
1279 | * device tree nodes. | |
1280 | */ | |
1281 | ||
1cf3d8b3 NF |
1282 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); |
1283 | ||
1284 | int of_reconfig_notifier_register(struct notifier_block *nb) | |
1285 | { | |
1286 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | |
1287 | } | |
1a9bd454 | 1288 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); |
1cf3d8b3 NF |
1289 | |
1290 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | |
1291 | { | |
1292 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | |
1293 | } | |
1a9bd454 | 1294 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); |
1cf3d8b3 NF |
1295 | |
1296 | int of_reconfig_notify(unsigned long action, void *p) | |
1297 | { | |
1298 | int rc; | |
1299 | ||
1300 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); | |
1301 | return notifier_to_errno(rc); | |
1302 | } | |
1303 | ||
e81b3295 NF |
1304 | #ifdef CONFIG_PROC_DEVICETREE |
1305 | static void of_add_proc_dt_entry(struct device_node *dn) | |
1306 | { | |
1307 | struct proc_dir_entry *ent; | |
1308 | ||
1309 | ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde); | |
1310 | if (ent) | |
1311 | proc_device_tree_add_node(dn, ent); | |
1312 | } | |
1313 | #else | |
1314 | static void of_add_proc_dt_entry(struct device_node *dn) | |
1315 | { | |
1316 | return; | |
1317 | } | |
1318 | #endif | |
1319 | ||
fcdeb7fe GL |
1320 | /** |
1321 | * of_attach_node - Plug a device node into the tree and global list. | |
1322 | */ | |
1cf3d8b3 | 1323 | int of_attach_node(struct device_node *np) |
fcdeb7fe GL |
1324 | { |
1325 | unsigned long flags; | |
1cf3d8b3 NF |
1326 | int rc; |
1327 | ||
1328 | rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | |
1329 | if (rc) | |
1330 | return rc; | |
fcdeb7fe GL |
1331 | |
1332 | write_lock_irqsave(&devtree_lock, flags); | |
1333 | np->sibling = np->parent->child; | |
465aac6d | 1334 | np->allnext = of_allnodes; |
fcdeb7fe | 1335 | np->parent->child = np; |
465aac6d | 1336 | of_allnodes = np; |
fcdeb7fe | 1337 | write_unlock_irqrestore(&devtree_lock, flags); |
e81b3295 NF |
1338 | |
1339 | of_add_proc_dt_entry(np); | |
1cf3d8b3 | 1340 | return 0; |
fcdeb7fe GL |
1341 | } |
1342 | ||
e81b3295 NF |
1343 | #ifdef CONFIG_PROC_DEVICETREE |
1344 | static void of_remove_proc_dt_entry(struct device_node *dn) | |
1345 | { | |
1346 | struct device_node *parent = dn->parent; | |
1347 | struct property *prop = dn->properties; | |
1348 | ||
1349 | while (prop) { | |
1350 | remove_proc_entry(prop->name, dn->pde); | |
1351 | prop = prop->next; | |
1352 | } | |
1353 | ||
1354 | if (dn->pde) | |
1355 | remove_proc_entry(dn->pde->name, parent->pde); | |
1356 | } | |
1357 | #else | |
1358 | static void of_remove_proc_dt_entry(struct device_node *dn) | |
1359 | { | |
1360 | return; | |
1361 | } | |
1362 | #endif | |
1363 | ||
fcdeb7fe GL |
1364 | /** |
1365 | * of_detach_node - "Unplug" a node from the device tree. | |
1366 | * | |
1367 | * The caller must hold a reference to the node. The memory associated with | |
1368 | * the node is not freed until its refcount goes to zero. | |
1369 | */ | |
1cf3d8b3 | 1370 | int of_detach_node(struct device_node *np) |
fcdeb7fe GL |
1371 | { |
1372 | struct device_node *parent; | |
1373 | unsigned long flags; | |
1cf3d8b3 NF |
1374 | int rc = 0; |
1375 | ||
1376 | rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | |
1377 | if (rc) | |
1378 | return rc; | |
fcdeb7fe GL |
1379 | |
1380 | write_lock_irqsave(&devtree_lock, flags); | |
1381 | ||
e81b3295 NF |
1382 | if (of_node_check_flag(np, OF_DETACHED)) { |
1383 | /* someone already detached it */ | |
1384 | write_unlock_irqrestore(&devtree_lock, flags); | |
1cf3d8b3 | 1385 | return rc; |
e81b3295 NF |
1386 | } |
1387 | ||
fcdeb7fe | 1388 | parent = np->parent; |
e81b3295 NF |
1389 | if (!parent) { |
1390 | write_unlock_irqrestore(&devtree_lock, flags); | |
1cf3d8b3 | 1391 | return rc; |
e81b3295 | 1392 | } |
fcdeb7fe | 1393 | |
465aac6d RD |
1394 | if (of_allnodes == np) |
1395 | of_allnodes = np->allnext; | |
fcdeb7fe GL |
1396 | else { |
1397 | struct device_node *prev; | |
465aac6d | 1398 | for (prev = of_allnodes; |
fcdeb7fe GL |
1399 | prev->allnext != np; |
1400 | prev = prev->allnext) | |
1401 | ; | |
1402 | prev->allnext = np->allnext; | |
1403 | } | |
1404 | ||
1405 | if (parent->child == np) | |
1406 | parent->child = np->sibling; | |
1407 | else { | |
1408 | struct device_node *prevsib; | |
1409 | for (prevsib = np->parent->child; | |
1410 | prevsib->sibling != np; | |
1411 | prevsib = prevsib->sibling) | |
1412 | ; | |
1413 | prevsib->sibling = np->sibling; | |
1414 | } | |
1415 | ||
1416 | of_node_set_flag(np, OF_DETACHED); | |
fcdeb7fe | 1417 | write_unlock_irqrestore(&devtree_lock, flags); |
e81b3295 NF |
1418 | |
1419 | of_remove_proc_dt_entry(np); | |
1cf3d8b3 | 1420 | return rc; |
fcdeb7fe GL |
1421 | } |
1422 | #endif /* defined(CONFIG_OF_DYNAMIC) */ | |
1423 | ||
611cad72 SG |
1424 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
1425 | int id, const char *stem, int stem_len) | |
1426 | { | |
1427 | ap->np = np; | |
1428 | ap->id = id; | |
1429 | strncpy(ap->stem, stem, stem_len); | |
1430 | ap->stem[stem_len] = 0; | |
1431 | list_add_tail(&ap->link, &aliases_lookup); | |
1432 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n", | |
74a7f084 | 1433 | ap->alias, ap->stem, ap->id, of_node_full_name(np)); |
611cad72 SG |
1434 | } |
1435 | ||
1436 | /** | |
1437 | * of_alias_scan - Scan all properties of 'aliases' node | |
1438 | * | |
1439 | * The function scans all the properties of 'aliases' node and populate | |
1440 | * the the global lookup table with the properties. It returns the | |
1441 | * number of alias_prop found, or error code in error case. | |
1442 | * | |
1443 | * @dt_alloc: An allocator that provides a virtual address to memory | |
1444 | * for the resulting tree | |
1445 | */ | |
1446 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) | |
1447 | { | |
1448 | struct property *pp; | |
1449 | ||
1450 | of_chosen = of_find_node_by_path("/chosen"); | |
1451 | if (of_chosen == NULL) | |
1452 | of_chosen = of_find_node_by_path("/chosen@0"); | |
1453 | of_aliases = of_find_node_by_path("/aliases"); | |
1454 | if (!of_aliases) | |
1455 | return; | |
1456 | ||
8af0da93 | 1457 | for_each_property_of_node(of_aliases, pp) { |
611cad72 SG |
1458 | const char *start = pp->name; |
1459 | const char *end = start + strlen(start); | |
1460 | struct device_node *np; | |
1461 | struct alias_prop *ap; | |
1462 | int id, len; | |
1463 | ||
1464 | /* Skip those we do not want to proceed */ | |
1465 | if (!strcmp(pp->name, "name") || | |
1466 | !strcmp(pp->name, "phandle") || | |
1467 | !strcmp(pp->name, "linux,phandle")) | |
1468 | continue; | |
1469 | ||
1470 | np = of_find_node_by_path(pp->value); | |
1471 | if (!np) | |
1472 | continue; | |
1473 | ||
1474 | /* walk the alias backwards to extract the id and work out | |
1475 | * the 'stem' string */ | |
1476 | while (isdigit(*(end-1)) && end > start) | |
1477 | end--; | |
1478 | len = end - start; | |
1479 | ||
1480 | if (kstrtoint(end, 10, &id) < 0) | |
1481 | continue; | |
1482 | ||
1483 | /* Allocate an alias_prop with enough space for the stem */ | |
1484 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); | |
1485 | if (!ap) | |
1486 | continue; | |
1487 | ap->alias = start; | |
1488 | of_alias_add(ap, np, id, start, len); | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | /** | |
1493 | * of_alias_get_id - Get alias id for the given device_node | |
1494 | * @np: Pointer to the given device_node | |
1495 | * @stem: Alias stem of the given device_node | |
1496 | * | |
1497 | * The function travels the lookup table to get alias id for the given | |
1498 | * device_node and alias stem. It returns the alias id if find it. | |
1499 | */ | |
1500 | int of_alias_get_id(struct device_node *np, const char *stem) | |
1501 | { | |
1502 | struct alias_prop *app; | |
1503 | int id = -ENODEV; | |
1504 | ||
1505 | mutex_lock(&of_aliases_mutex); | |
1506 | list_for_each_entry(app, &aliases_lookup, link) { | |
1507 | if (strcmp(app->stem, stem) != 0) | |
1508 | continue; | |
1509 | ||
1510 | if (np == app->np) { | |
1511 | id = app->id; | |
1512 | break; | |
1513 | } | |
1514 | } | |
1515 | mutex_unlock(&of_aliases_mutex); | |
1516 | ||
1517 | return id; | |
1518 | } | |
1519 | EXPORT_SYMBOL_GPL(of_alias_get_id); | |
c541adc6 SW |
1520 | |
1521 | const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur, | |
1522 | u32 *pu) | |
1523 | { | |
1524 | const void *curv = cur; | |
1525 | ||
1526 | if (!prop) | |
1527 | return NULL; | |
1528 | ||
1529 | if (!cur) { | |
1530 | curv = prop->value; | |
1531 | goto out_val; | |
1532 | } | |
1533 | ||
1534 | curv += sizeof(*cur); | |
1535 | if (curv >= prop->value + prop->length) | |
1536 | return NULL; | |
1537 | ||
1538 | out_val: | |
1539 | *pu = be32_to_cpup(curv); | |
1540 | return curv; | |
1541 | } | |
1542 | EXPORT_SYMBOL_GPL(of_prop_next_u32); | |
1543 | ||
1544 | const char *of_prop_next_string(struct property *prop, const char *cur) | |
1545 | { | |
1546 | const void *curv = cur; | |
1547 | ||
1548 | if (!prop) | |
1549 | return NULL; | |
1550 | ||
1551 | if (!cur) | |
1552 | return prop->value; | |
1553 | ||
1554 | curv += strlen(cur) + 1; | |
1555 | if (curv >= prop->value + prop->length) | |
1556 | return NULL; | |
1557 | ||
1558 | return curv; | |
1559 | } | |
1560 | EXPORT_SYMBOL_GPL(of_prop_next_string); |