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