]>
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 | */ | |
20 | #include <linux/module.h> | |
21 | #include <linux/of.h> | |
581b605a SR |
22 | #include <linux/spinlock.h> |
23 | ||
1ef4d424 SR |
24 | struct device_node *allnodes; |
25 | ||
581b605a SR |
26 | /* use when traversing tree through the allnext, child, sibling, |
27 | * or parent members of struct device_node. | |
28 | */ | |
29 | DEFINE_RWLOCK(devtree_lock); | |
97e873e5 SR |
30 | |
31 | int of_n_addr_cells(struct device_node *np) | |
32 | { | |
33 | const int *ip; | |
34 | ||
35 | do { | |
36 | if (np->parent) | |
37 | np = np->parent; | |
38 | ip = of_get_property(np, "#address-cells", NULL); | |
39 | if (ip) | |
40 | return *ip; | |
41 | } while (np->parent); | |
42 | /* No #address-cells property for the root node */ | |
43 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; | |
44 | } | |
45 | EXPORT_SYMBOL(of_n_addr_cells); | |
46 | ||
47 | int of_n_size_cells(struct device_node *np) | |
48 | { | |
49 | const int *ip; | |
50 | ||
51 | do { | |
52 | if (np->parent) | |
53 | np = np->parent; | |
54 | ip = of_get_property(np, "#size-cells", NULL); | |
55 | if (ip) | |
56 | return *ip; | |
57 | } while (np->parent); | |
58 | /* No #size-cells property for the root node */ | |
59 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; | |
60 | } | |
61 | EXPORT_SYMBOL(of_n_size_cells); | |
62 | ||
923f7e30 GL |
63 | #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */ |
64 | /** | |
65 | * of_node_get - Increment refcount of a node | |
66 | * @node: Node to inc refcount, NULL is supported to | |
67 | * simplify writing of callers | |
68 | * | |
69 | * Returns node. | |
70 | */ | |
71 | struct device_node *of_node_get(struct device_node *node) | |
72 | { | |
73 | if (node) | |
74 | kref_get(&node->kref); | |
75 | return node; | |
76 | } | |
77 | EXPORT_SYMBOL(of_node_get); | |
78 | ||
79 | static inline struct device_node *kref_to_device_node(struct kref *kref) | |
80 | { | |
81 | return container_of(kref, struct device_node, kref); | |
82 | } | |
83 | ||
84 | /** | |
85 | * of_node_release - release a dynamically allocated node | |
86 | * @kref: kref element of the node to be released | |
87 | * | |
88 | * In of_node_put() this function is passed to kref_put() | |
89 | * as the destructor. | |
90 | */ | |
91 | static void of_node_release(struct kref *kref) | |
92 | { | |
93 | struct device_node *node = kref_to_device_node(kref); | |
94 | struct property *prop = node->properties; | |
95 | ||
96 | /* We should never be releasing nodes that haven't been detached. */ | |
97 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
98 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
99 | dump_stack(); | |
100 | kref_init(&node->kref); | |
101 | return; | |
102 | } | |
103 | ||
104 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
105 | return; | |
106 | ||
107 | while (prop) { | |
108 | struct property *next = prop->next; | |
109 | kfree(prop->name); | |
110 | kfree(prop->value); | |
111 | kfree(prop); | |
112 | prop = next; | |
113 | ||
114 | if (!prop) { | |
115 | prop = node->deadprops; | |
116 | node->deadprops = NULL; | |
117 | } | |
118 | } | |
119 | kfree(node->full_name); | |
120 | kfree(node->data); | |
121 | kfree(node); | |
122 | } | |
123 | ||
124 | /** | |
125 | * of_node_put - Decrement refcount of a node | |
126 | * @node: Node to dec refcount, NULL is supported to | |
127 | * simplify writing of callers | |
128 | * | |
129 | */ | |
130 | void of_node_put(struct device_node *node) | |
131 | { | |
132 | if (node) | |
133 | kref_put(&node->kref, of_node_release); | |
134 | } | |
135 | EXPORT_SYMBOL(of_node_put); | |
136 | #endif /* !CONFIG_SPARC */ | |
137 | ||
581b605a SR |
138 | struct property *of_find_property(const struct device_node *np, |
139 | const char *name, | |
140 | int *lenp) | |
141 | { | |
142 | struct property *pp; | |
143 | ||
64e4566f TT |
144 | if (!np) |
145 | return NULL; | |
146 | ||
581b605a SR |
147 | read_lock(&devtree_lock); |
148 | for (pp = np->properties; pp != 0; pp = pp->next) { | |
149 | if (of_prop_cmp(pp->name, name) == 0) { | |
150 | if (lenp != 0) | |
151 | *lenp = pp->length; | |
152 | break; | |
153 | } | |
154 | } | |
155 | read_unlock(&devtree_lock); | |
156 | ||
157 | return pp; | |
158 | } | |
159 | EXPORT_SYMBOL(of_find_property); | |
160 | ||
e91edcf5 GL |
161 | /** |
162 | * of_find_all_nodes - Get next node in global list | |
163 | * @prev: Previous node or NULL to start iteration | |
164 | * of_node_put() will be called on it | |
165 | * | |
166 | * Returns a node pointer with refcount incremented, use | |
167 | * of_node_put() on it when done. | |
168 | */ | |
169 | struct device_node *of_find_all_nodes(struct device_node *prev) | |
170 | { | |
171 | struct device_node *np; | |
172 | ||
173 | read_lock(&devtree_lock); | |
174 | np = prev ? prev->allnext : allnodes; | |
175 | for (; np != NULL; np = np->allnext) | |
176 | if (of_node_get(np)) | |
177 | break; | |
178 | of_node_put(prev); | |
179 | read_unlock(&devtree_lock); | |
180 | return np; | |
181 | } | |
182 | EXPORT_SYMBOL(of_find_all_nodes); | |
183 | ||
97e873e5 SR |
184 | /* |
185 | * Find a property with a given name for a given node | |
186 | * and return the value. | |
187 | */ | |
188 | const void *of_get_property(const struct device_node *np, const char *name, | |
189 | int *lenp) | |
190 | { | |
191 | struct property *pp = of_find_property(np, name, lenp); | |
192 | ||
193 | return pp ? pp->value : NULL; | |
194 | } | |
195 | EXPORT_SYMBOL(of_get_property); | |
0081cbc3 SR |
196 | |
197 | /** Checks if the given "compat" string matches one of the strings in | |
198 | * the device's "compatible" property | |
199 | */ | |
200 | int of_device_is_compatible(const struct device_node *device, | |
201 | const char *compat) | |
202 | { | |
203 | const char* cp; | |
204 | int cplen, l; | |
205 | ||
206 | cp = of_get_property(device, "compatible", &cplen); | |
207 | if (cp == NULL) | |
208 | return 0; | |
209 | while (cplen > 0) { | |
210 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) | |
211 | return 1; | |
212 | l = strlen(cp) + 1; | |
213 | cp += l; | |
214 | cplen -= l; | |
215 | } | |
216 | ||
217 | return 0; | |
218 | } | |
219 | EXPORT_SYMBOL(of_device_is_compatible); | |
e679c5f4 | 220 | |
1f43cfb9 GL |
221 | /** |
222 | * machine_is_compatible - Test root of device tree for a given compatible value | |
223 | * @compat: compatible string to look for in root node's compatible property. | |
224 | * | |
225 | * Returns true if the root node has the given value in its | |
226 | * compatible property. | |
227 | */ | |
228 | int machine_is_compatible(const char *compat) | |
229 | { | |
230 | struct device_node *root; | |
231 | int rc = 0; | |
232 | ||
233 | root = of_find_node_by_path("/"); | |
234 | if (root) { | |
235 | rc = of_device_is_compatible(root, compat); | |
236 | of_node_put(root); | |
237 | } | |
238 | return rc; | |
239 | } | |
240 | EXPORT_SYMBOL(machine_is_compatible); | |
241 | ||
834d97d4 JB |
242 | /** |
243 | * of_device_is_available - check if a device is available for use | |
244 | * | |
245 | * @device: Node to check for availability | |
246 | * | |
247 | * Returns 1 if the status property is absent or set to "okay" or "ok", | |
248 | * 0 otherwise | |
249 | */ | |
250 | int of_device_is_available(const struct device_node *device) | |
251 | { | |
252 | const char *status; | |
253 | int statlen; | |
254 | ||
255 | status = of_get_property(device, "status", &statlen); | |
256 | if (status == NULL) | |
257 | return 1; | |
258 | ||
259 | if (statlen > 0) { | |
260 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) | |
261 | return 1; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | EXPORT_SYMBOL(of_device_is_available); | |
267 | ||
e679c5f4 SR |
268 | /** |
269 | * of_get_parent - Get a node's parent if any | |
270 | * @node: Node to get parent | |
271 | * | |
272 | * Returns a node pointer with refcount incremented, use | |
273 | * of_node_put() on it when done. | |
274 | */ | |
275 | struct device_node *of_get_parent(const struct device_node *node) | |
276 | { | |
277 | struct device_node *np; | |
278 | ||
279 | if (!node) | |
280 | return NULL; | |
281 | ||
282 | read_lock(&devtree_lock); | |
283 | np = of_node_get(node->parent); | |
284 | read_unlock(&devtree_lock); | |
285 | return np; | |
286 | } | |
287 | EXPORT_SYMBOL(of_get_parent); | |
d1cd355a | 288 | |
f4eb0107 ME |
289 | /** |
290 | * of_get_next_parent - Iterate to a node's parent | |
291 | * @node: Node to get parent of | |
292 | * | |
293 | * This is like of_get_parent() except that it drops the | |
294 | * refcount on the passed node, making it suitable for iterating | |
295 | * through a node's parents. | |
296 | * | |
297 | * Returns a node pointer with refcount incremented, use | |
298 | * of_node_put() on it when done. | |
299 | */ | |
300 | struct device_node *of_get_next_parent(struct device_node *node) | |
301 | { | |
302 | struct device_node *parent; | |
303 | ||
304 | if (!node) | |
305 | return NULL; | |
306 | ||
307 | read_lock(&devtree_lock); | |
308 | parent = of_node_get(node->parent); | |
309 | of_node_put(node); | |
310 | read_unlock(&devtree_lock); | |
311 | return parent; | |
312 | } | |
313 | ||
d1cd355a SR |
314 | /** |
315 | * of_get_next_child - Iterate a node childs | |
316 | * @node: parent node | |
317 | * @prev: previous child of the parent node, or NULL to get first | |
318 | * | |
319 | * Returns a node pointer with refcount incremented, use | |
320 | * of_node_put() on it when done. | |
321 | */ | |
322 | struct device_node *of_get_next_child(const struct device_node *node, | |
323 | struct device_node *prev) | |
324 | { | |
325 | struct device_node *next; | |
326 | ||
327 | read_lock(&devtree_lock); | |
328 | next = prev ? prev->sibling : node->child; | |
329 | for (; next; next = next->sibling) | |
330 | if (of_node_get(next)) | |
331 | break; | |
332 | of_node_put(prev); | |
333 | read_unlock(&devtree_lock); | |
334 | return next; | |
335 | } | |
336 | EXPORT_SYMBOL(of_get_next_child); | |
1ef4d424 SR |
337 | |
338 | /** | |
339 | * of_find_node_by_path - Find a node matching a full OF path | |
340 | * @path: The full path to match | |
341 | * | |
342 | * Returns a node pointer with refcount incremented, use | |
343 | * of_node_put() on it when done. | |
344 | */ | |
345 | struct device_node *of_find_node_by_path(const char *path) | |
346 | { | |
347 | struct device_node *np = allnodes; | |
348 | ||
349 | read_lock(&devtree_lock); | |
350 | for (; np; np = np->allnext) { | |
351 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) | |
352 | && of_node_get(np)) | |
353 | break; | |
354 | } | |
355 | read_unlock(&devtree_lock); | |
356 | return np; | |
357 | } | |
358 | EXPORT_SYMBOL(of_find_node_by_path); | |
359 | ||
360 | /** | |
361 | * of_find_node_by_name - Find a node by its "name" property | |
362 | * @from: The node to start searching from or NULL, the node | |
363 | * you pass will not be searched, only the next one | |
364 | * will; typically, you pass what the previous call | |
365 | * returned. of_node_put() will be called on it | |
366 | * @name: The name string to match against | |
367 | * | |
368 | * Returns a node pointer with refcount incremented, use | |
369 | * of_node_put() on it when done. | |
370 | */ | |
371 | struct device_node *of_find_node_by_name(struct device_node *from, | |
372 | const char *name) | |
373 | { | |
374 | struct device_node *np; | |
375 | ||
376 | read_lock(&devtree_lock); | |
377 | np = from ? from->allnext : allnodes; | |
378 | for (; np; np = np->allnext) | |
379 | if (np->name && (of_node_cmp(np->name, name) == 0) | |
380 | && of_node_get(np)) | |
381 | break; | |
382 | of_node_put(from); | |
383 | read_unlock(&devtree_lock); | |
384 | return np; | |
385 | } | |
386 | EXPORT_SYMBOL(of_find_node_by_name); | |
387 | ||
388 | /** | |
389 | * of_find_node_by_type - Find a node by its "device_type" property | |
390 | * @from: The node to start searching from, or NULL to start searching | |
391 | * the entire device tree. The node you pass will not be | |
392 | * searched, only the next one will; typically, you pass | |
393 | * what the previous call returned. of_node_put() will be | |
394 | * called on from for you. | |
395 | * @type: The type string to match against | |
396 | * | |
397 | * Returns a node pointer with refcount incremented, use | |
398 | * of_node_put() on it when done. | |
399 | */ | |
400 | struct device_node *of_find_node_by_type(struct device_node *from, | |
401 | const char *type) | |
402 | { | |
403 | struct device_node *np; | |
404 | ||
405 | read_lock(&devtree_lock); | |
406 | np = from ? from->allnext : allnodes; | |
407 | for (; np; np = np->allnext) | |
408 | if (np->type && (of_node_cmp(np->type, type) == 0) | |
409 | && of_node_get(np)) | |
410 | break; | |
411 | of_node_put(from); | |
412 | read_unlock(&devtree_lock); | |
413 | return np; | |
414 | } | |
415 | EXPORT_SYMBOL(of_find_node_by_type); | |
416 | ||
417 | /** | |
418 | * of_find_compatible_node - Find a node based on type and one of the | |
419 | * tokens in its "compatible" property | |
420 | * @from: The node to start searching from or NULL, the node | |
421 | * you pass will not be searched, only the next one | |
422 | * will; typically, you pass what the previous call | |
423 | * returned. of_node_put() will be called on it | |
424 | * @type: The type string to match "device_type" or NULL to ignore | |
425 | * @compatible: The string to match to one of the tokens in the device | |
426 | * "compatible" list. | |
427 | * | |
428 | * Returns a node pointer with refcount incremented, use | |
429 | * of_node_put() on it when done. | |
430 | */ | |
431 | struct device_node *of_find_compatible_node(struct device_node *from, | |
432 | const char *type, const char *compatible) | |
433 | { | |
434 | struct device_node *np; | |
435 | ||
436 | read_lock(&devtree_lock); | |
437 | np = from ? from->allnext : allnodes; | |
438 | for (; np; np = np->allnext) { | |
439 | if (type | |
440 | && !(np->type && (of_node_cmp(np->type, type) == 0))) | |
441 | continue; | |
442 | if (of_device_is_compatible(np, compatible) && of_node_get(np)) | |
443 | break; | |
444 | } | |
445 | of_node_put(from); | |
446 | read_unlock(&devtree_lock); | |
447 | return np; | |
448 | } | |
449 | EXPORT_SYMBOL(of_find_compatible_node); | |
283029d1 | 450 | |
1e291b14 ME |
451 | /** |
452 | * of_find_node_with_property - Find a node which has a property with | |
453 | * the given name. | |
454 | * @from: The node to start searching from or NULL, the node | |
455 | * you pass will not be searched, only the next one | |
456 | * will; typically, you pass what the previous call | |
457 | * returned. of_node_put() will be called on it | |
458 | * @prop_name: The name of the property to look for. | |
459 | * | |
460 | * Returns a node pointer with refcount incremented, use | |
461 | * of_node_put() on it when done. | |
462 | */ | |
463 | struct device_node *of_find_node_with_property(struct device_node *from, | |
464 | const char *prop_name) | |
465 | { | |
466 | struct device_node *np; | |
467 | struct property *pp; | |
468 | ||
469 | read_lock(&devtree_lock); | |
470 | np = from ? from->allnext : allnodes; | |
471 | for (; np; np = np->allnext) { | |
472 | for (pp = np->properties; pp != 0; pp = pp->next) { | |
473 | if (of_prop_cmp(pp->name, prop_name) == 0) { | |
474 | of_node_get(np); | |
475 | goto out; | |
476 | } | |
477 | } | |
478 | } | |
479 | out: | |
480 | of_node_put(from); | |
481 | read_unlock(&devtree_lock); | |
482 | return np; | |
483 | } | |
484 | EXPORT_SYMBOL(of_find_node_with_property); | |
485 | ||
283029d1 GL |
486 | /** |
487 | * of_match_node - Tell if an device_node has a matching of_match structure | |
488 | * @matches: array of of device match structures to search in | |
489 | * @node: the of device structure to match against | |
490 | * | |
491 | * Low level utility function used by device matching. | |
492 | */ | |
493 | const struct of_device_id *of_match_node(const struct of_device_id *matches, | |
494 | const struct device_node *node) | |
495 | { | |
496 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { | |
497 | int match = 1; | |
498 | if (matches->name[0]) | |
499 | match &= node->name | |
500 | && !strcmp(matches->name, node->name); | |
501 | if (matches->type[0]) | |
502 | match &= node->type | |
503 | && !strcmp(matches->type, node->type); | |
504 | if (matches->compatible[0]) | |
505 | match &= of_device_is_compatible(node, | |
506 | matches->compatible); | |
507 | if (match) | |
508 | return matches; | |
509 | matches++; | |
510 | } | |
511 | return NULL; | |
512 | } | |
513 | EXPORT_SYMBOL(of_match_node); | |
514 | ||
515 | /** | |
516 | * of_find_matching_node - Find a node based on an of_device_id match | |
517 | * table. | |
518 | * @from: The node to start searching from or NULL, the node | |
519 | * you pass will not be searched, only the next one | |
520 | * will; typically, you pass what the previous call | |
521 | * returned. of_node_put() will be called on it | |
522 | * @matches: array of of device match structures to search in | |
523 | * | |
524 | * Returns a node pointer with refcount incremented, use | |
525 | * of_node_put() on it when done. | |
526 | */ | |
527 | struct device_node *of_find_matching_node(struct device_node *from, | |
528 | const struct of_device_id *matches) | |
529 | { | |
530 | struct device_node *np; | |
531 | ||
532 | read_lock(&devtree_lock); | |
533 | np = from ? from->allnext : allnodes; | |
534 | for (; np; np = np->allnext) { | |
535 | if (of_match_node(matches, np) && of_node_get(np)) | |
536 | break; | |
537 | } | |
538 | of_node_put(from); | |
539 | read_unlock(&devtree_lock); | |
540 | return np; | |
541 | } | |
542 | EXPORT_SYMBOL(of_find_matching_node); | |
3f07af49 GL |
543 | |
544 | /** | |
545 | * of_modalias_table: Table of explicit compatible ==> modalias mappings | |
546 | * | |
547 | * This table allows particulare compatible property values to be mapped | |
548 | * to modalias strings. This is useful for busses which do not directly | |
549 | * understand the OF device tree but are populated based on data contained | |
550 | * within the device tree. SPI and I2C are the two current users of this | |
551 | * table. | |
552 | * | |
553 | * In most cases, devices do not need to be listed in this table because | |
554 | * the modalias value can be derived directly from the compatible table. | |
555 | * However, if for any reason a value cannot be derived, then this table | |
556 | * provides a method to override the implicit derivation. | |
557 | * | |
558 | * At the moment, a single table is used for all bus types because it is | |
559 | * assumed that the data size is small and that the compatible values | |
560 | * should already be distinct enough to differentiate between SPI, I2C | |
561 | * and other devices. | |
562 | */ | |
563 | struct of_modalias_table { | |
564 | char *of_device; | |
565 | char *modalias; | |
566 | }; | |
567 | static struct of_modalias_table of_modalias_table[] = { | |
4c3ed7d6 | 568 | { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" }, |
3f1c6ebf | 569 | { "mmc-spi-slot", "mmc_spi" }, |
3f07af49 GL |
570 | }; |
571 | ||
572 | /** | |
573 | * of_modalias_node - Lookup appropriate modalias for a device node | |
574 | * @node: pointer to a device tree node | |
575 | * @modalias: Pointer to buffer that modalias value will be copied into | |
576 | * @len: Length of modalias value | |
577 | * | |
578 | * Based on the value of the compatible property, this routine will determine | |
58f467ce GL |
579 | * an appropriate modalias value for a particular device tree node. Two |
580 | * separate methods are attempted to derive a modalias value. | |
3f07af49 GL |
581 | * |
582 | * First method is to lookup the compatible value in of_modalias_table. | |
58f467ce GL |
583 | * Second is to strip off the manufacturer prefix from the first |
584 | * compatible entry and use the remainder as modalias | |
3f07af49 GL |
585 | * |
586 | * This routine returns 0 on success | |
587 | */ | |
588 | int of_modalias_node(struct device_node *node, char *modalias, int len) | |
589 | { | |
590 | int i, cplen; | |
591 | const char *compatible; | |
592 | const char *p; | |
593 | ||
594 | /* 1. search for exception list entry */ | |
595 | for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) { | |
596 | compatible = of_modalias_table[i].of_device; | |
597 | if (!of_device_is_compatible(node, compatible)) | |
598 | continue; | |
599 | strlcpy(modalias, of_modalias_table[i].modalias, len); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | compatible = of_get_property(node, "compatible", &cplen); | |
604 | if (!compatible) | |
605 | return -ENODEV; | |
606 | ||
58f467ce | 607 | /* 2. take first compatible entry and strip manufacturer */ |
3f07af49 GL |
608 | p = strchr(compatible, ','); |
609 | if (!p) | |
610 | return -ENODEV; | |
611 | p++; | |
612 | strlcpy(modalias, p, len); | |
613 | return 0; | |
614 | } | |
615 | EXPORT_SYMBOL_GPL(of_modalias_node); | |
616 | ||
739649c5 GL |
617 | /** |
618 | * of_parse_phandle - Resolve a phandle property to a device_node pointer | |
619 | * @np: Pointer to device node holding phandle property | |
620 | * @phandle_name: Name of property holding a phandle value | |
621 | * @index: For properties holding a table of phandles, this is the index into | |
622 | * the table | |
623 | * | |
624 | * Returns the device_node pointer with refcount incremented. Use | |
625 | * of_node_put() on it when done. | |
626 | */ | |
627 | struct device_node * | |
628 | of_parse_phandle(struct device_node *np, const char *phandle_name, int index) | |
629 | { | |
630 | const phandle *phandle; | |
631 | int size; | |
632 | ||
633 | phandle = of_get_property(np, phandle_name, &size); | |
634 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) | |
635 | return NULL; | |
636 | ||
637 | return of_find_node_by_phandle(phandle[index]); | |
638 | } | |
639 | EXPORT_SYMBOL(of_parse_phandle); | |
640 | ||
64b60e09 AV |
641 | /** |
642 | * of_parse_phandles_with_args - Find a node pointed by phandle in a list | |
643 | * @np: pointer to a device tree node containing a list | |
644 | * @list_name: property name that contains a list | |
645 | * @cells_name: property name that specifies phandles' arguments count | |
646 | * @index: index of a phandle to parse out | |
7736a3db AV |
647 | * @out_node: optional pointer to device_node struct pointer (will be filled) |
648 | * @out_args: optional pointer to arguments pointer (will be filled) | |
64b60e09 AV |
649 | * |
650 | * This function is useful to parse lists of phandles and their arguments. | |
651 | * Returns 0 on success and fills out_node and out_args, on error returns | |
652 | * appropriate errno value. | |
653 | * | |
654 | * Example: | |
655 | * | |
656 | * phandle1: node1 { | |
657 | * #list-cells = <2>; | |
658 | * } | |
659 | * | |
660 | * phandle2: node2 { | |
661 | * #list-cells = <1>; | |
662 | * } | |
663 | * | |
664 | * node3 { | |
665 | * list = <&phandle1 1 2 &phandle2 3>; | |
666 | * } | |
667 | * | |
668 | * To get a device_node of the `node2' node you may call this: | |
669 | * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args); | |
670 | */ | |
671 | int of_parse_phandles_with_args(struct device_node *np, const char *list_name, | |
672 | const char *cells_name, int index, | |
673 | struct device_node **out_node, | |
674 | const void **out_args) | |
675 | { | |
676 | int ret = -EINVAL; | |
677 | const u32 *list; | |
678 | const u32 *list_end; | |
679 | int size; | |
680 | int cur_index = 0; | |
681 | struct device_node *node = NULL; | |
7736a3db | 682 | const void *args = NULL; |
64b60e09 AV |
683 | |
684 | list = of_get_property(np, list_name, &size); | |
685 | if (!list) { | |
686 | ret = -ENOENT; | |
687 | goto err0; | |
688 | } | |
689 | list_end = list + size / sizeof(*list); | |
690 | ||
691 | while (list < list_end) { | |
692 | const u32 *cells; | |
693 | const phandle *phandle; | |
694 | ||
c1bb7c6d AV |
695 | phandle = list++; |
696 | args = list; | |
64b60e09 AV |
697 | |
698 | /* one cell hole in the list = <>; */ | |
c1bb7c6d | 699 | if (!*phandle) |
64b60e09 | 700 | goto next; |
64b60e09 AV |
701 | |
702 | node = of_find_node_by_phandle(*phandle); | |
703 | if (!node) { | |
704 | pr_debug("%s: could not find phandle\n", | |
705 | np->full_name); | |
706 | goto err0; | |
707 | } | |
708 | ||
709 | cells = of_get_property(node, cells_name, &size); | |
710 | if (!cells || size != sizeof(*cells)) { | |
711 | pr_debug("%s: could not get %s for %s\n", | |
712 | np->full_name, cells_name, node->full_name); | |
713 | goto err1; | |
714 | } | |
715 | ||
c1bb7c6d | 716 | list += *cells; |
64b60e09 AV |
717 | if (list > list_end) { |
718 | pr_debug("%s: insufficient arguments length\n", | |
719 | np->full_name); | |
720 | goto err1; | |
721 | } | |
722 | next: | |
723 | if (cur_index == index) | |
724 | break; | |
725 | ||
726 | of_node_put(node); | |
727 | node = NULL; | |
7736a3db | 728 | args = NULL; |
64b60e09 AV |
729 | cur_index++; |
730 | } | |
731 | ||
732 | if (!node) { | |
7736a3db AV |
733 | /* |
734 | * args w/o node indicates that the loop above has stopped at | |
735 | * the 'hole' cell. Report this differently. | |
736 | */ | |
737 | if (args) | |
738 | ret = -EEXIST; | |
739 | else | |
740 | ret = -ENOENT; | |
64b60e09 AV |
741 | goto err0; |
742 | } | |
743 | ||
7736a3db AV |
744 | if (out_node) |
745 | *out_node = node; | |
746 | if (out_args) | |
747 | *out_args = args; | |
64b60e09 AV |
748 | |
749 | return 0; | |
750 | err1: | |
751 | of_node_put(node); | |
752 | err0: | |
753 | pr_debug("%s failed with status %d\n", __func__, ret); | |
754 | return ret; | |
755 | } | |
756 | EXPORT_SYMBOL(of_parse_phandles_with_args); | |
02af11b0 GL |
757 | |
758 | /** | |
759 | * prom_add_property - Add a property to a node | |
760 | */ | |
761 | int prom_add_property(struct device_node *np, struct property *prop) | |
762 | { | |
763 | struct property **next; | |
764 | unsigned long flags; | |
765 | ||
766 | prop->next = NULL; | |
767 | write_lock_irqsave(&devtree_lock, flags); | |
768 | next = &np->properties; | |
769 | while (*next) { | |
770 | if (strcmp(prop->name, (*next)->name) == 0) { | |
771 | /* duplicate ! don't insert it */ | |
772 | write_unlock_irqrestore(&devtree_lock, flags); | |
773 | return -1; | |
774 | } | |
775 | next = &(*next)->next; | |
776 | } | |
777 | *next = prop; | |
778 | write_unlock_irqrestore(&devtree_lock, flags); | |
779 | ||
780 | #ifdef CONFIG_PROC_DEVICETREE | |
781 | /* try to add to proc as well if it was initialized */ | |
782 | if (np->pde) | |
783 | proc_device_tree_add_prop(np->pde, prop); | |
784 | #endif /* CONFIG_PROC_DEVICETREE */ | |
785 | ||
786 | return 0; | |
787 | } | |
788 | ||
789 | /** | |
790 | * prom_remove_property - Remove a property from a node. | |
791 | * | |
792 | * Note that we don't actually remove it, since we have given out | |
793 | * who-knows-how-many pointers to the data using get-property. | |
794 | * Instead we just move the property to the "dead properties" | |
795 | * list, so it won't be found any more. | |
796 | */ | |
797 | int prom_remove_property(struct device_node *np, struct property *prop) | |
798 | { | |
799 | struct property **next; | |
800 | unsigned long flags; | |
801 | int found = 0; | |
802 | ||
803 | write_lock_irqsave(&devtree_lock, flags); | |
804 | next = &np->properties; | |
805 | while (*next) { | |
806 | if (*next == prop) { | |
807 | /* found the node */ | |
808 | *next = prop->next; | |
809 | prop->next = np->deadprops; | |
810 | np->deadprops = prop; | |
811 | found = 1; | |
812 | break; | |
813 | } | |
814 | next = &(*next)->next; | |
815 | } | |
816 | write_unlock_irqrestore(&devtree_lock, flags); | |
817 | ||
818 | if (!found) | |
819 | return -ENODEV; | |
820 | ||
821 | #ifdef CONFIG_PROC_DEVICETREE | |
822 | /* try to remove the proc node as well */ | |
823 | if (np->pde) | |
824 | proc_device_tree_remove_prop(np->pde, prop); | |
825 | #endif /* CONFIG_PROC_DEVICETREE */ | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | /* | |
831 | * prom_update_property - Update a property in a node. | |
832 | * | |
833 | * Note that we don't actually remove it, since we have given out | |
834 | * who-knows-how-many pointers to the data using get-property. | |
835 | * Instead we just move the property to the "dead properties" list, | |
836 | * and add the new property to the property list | |
837 | */ | |
838 | int prom_update_property(struct device_node *np, | |
839 | struct property *newprop, | |
840 | struct property *oldprop) | |
841 | { | |
842 | struct property **next; | |
843 | unsigned long flags; | |
844 | int found = 0; | |
845 | ||
846 | write_lock_irqsave(&devtree_lock, flags); | |
847 | next = &np->properties; | |
848 | while (*next) { | |
849 | if (*next == oldprop) { | |
850 | /* found the node */ | |
851 | newprop->next = oldprop->next; | |
852 | *next = newprop; | |
853 | oldprop->next = np->deadprops; | |
854 | np->deadprops = oldprop; | |
855 | found = 1; | |
856 | break; | |
857 | } | |
858 | next = &(*next)->next; | |
859 | } | |
860 | write_unlock_irqrestore(&devtree_lock, flags); | |
861 | ||
862 | if (!found) | |
863 | return -ENODEV; | |
864 | ||
865 | #ifdef CONFIG_PROC_DEVICETREE | |
866 | /* try to add to proc as well if it was initialized */ | |
867 | if (np->pde) | |
868 | proc_device_tree_update_prop(np->pde, newprop, oldprop); | |
869 | #endif /* CONFIG_PROC_DEVICETREE */ | |
870 | ||
871 | return 0; | |
872 | } |