ecryptfs: use after free
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / of / base.c
blobe6627b2320f16a89a56d30e9a7e00b6c2c80130a
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
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.
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/spinlock.h>
24 struct device_node *allnodes;
26 /* use when traversing tree through the allnext, child, sibling,
27 * or parent members of struct device_node.
29 DEFINE_RWLOCK(devtree_lock);
31 int of_n_addr_cells(struct device_node *np)
33 const int *ip;
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;
45 EXPORT_SYMBOL(of_n_addr_cells);
47 int of_n_size_cells(struct device_node *np)
49 const int *ip;
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;
61 EXPORT_SYMBOL(of_n_size_cells);
63 struct property *of_find_property(const struct device_node *np,
64 const char *name,
65 int *lenp)
67 struct property *pp;
69 if (!np)
70 return NULL;
72 read_lock(&devtree_lock);
73 for (pp = np->properties; pp != 0; pp = pp->next) {
74 if (of_prop_cmp(pp->name, name) == 0) {
75 if (lenp != 0)
76 *lenp = pp->length;
77 break;
80 read_unlock(&devtree_lock);
82 return pp;
84 EXPORT_SYMBOL(of_find_property);
86 /**
87 * of_find_all_nodes - Get next node in global list
88 * @prev: Previous node or NULL to start iteration
89 * of_node_put() will be called on it
91 * Returns a node pointer with refcount incremented, use
92 * of_node_put() on it when done.
94 struct device_node *of_find_all_nodes(struct device_node *prev)
96 struct device_node *np;
98 read_lock(&devtree_lock);
99 np = prev ? prev->allnext : allnodes;
100 for (; np != NULL; np = np->allnext)
101 if (of_node_get(np))
102 break;
103 of_node_put(prev);
104 read_unlock(&devtree_lock);
105 return np;
107 EXPORT_SYMBOL(of_find_all_nodes);
110 * Find a property with a given name for a given node
111 * and return the value.
113 const void *of_get_property(const struct device_node *np, const char *name,
114 int *lenp)
116 struct property *pp = of_find_property(np, name, lenp);
118 return pp ? pp->value : NULL;
120 EXPORT_SYMBOL(of_get_property);
122 /** Checks if the given "compat" string matches one of the strings in
123 * the device's "compatible" property
125 int of_device_is_compatible(const struct device_node *device,
126 const char *compat)
128 const char* cp;
129 int cplen, l;
131 cp = of_get_property(device, "compatible", &cplen);
132 if (cp == NULL)
133 return 0;
134 while (cplen > 0) {
135 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
136 return 1;
137 l = strlen(cp) + 1;
138 cp += l;
139 cplen -= l;
142 return 0;
144 EXPORT_SYMBOL(of_device_is_compatible);
147 * of_device_is_available - check if a device is available for use
149 * @device: Node to check for availability
151 * Returns 1 if the status property is absent or set to "okay" or "ok",
152 * 0 otherwise
154 int of_device_is_available(const struct device_node *device)
156 const char *status;
157 int statlen;
159 status = of_get_property(device, "status", &statlen);
160 if (status == NULL)
161 return 1;
163 if (statlen > 0) {
164 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
165 return 1;
168 return 0;
170 EXPORT_SYMBOL(of_device_is_available);
173 * of_get_parent - Get a node's parent if any
174 * @node: Node to get parent
176 * Returns a node pointer with refcount incremented, use
177 * of_node_put() on it when done.
179 struct device_node *of_get_parent(const struct device_node *node)
181 struct device_node *np;
183 if (!node)
184 return NULL;
186 read_lock(&devtree_lock);
187 np = of_node_get(node->parent);
188 read_unlock(&devtree_lock);
189 return np;
191 EXPORT_SYMBOL(of_get_parent);
194 * of_get_next_parent - Iterate to a node's parent
195 * @node: Node to get parent of
197 * This is like of_get_parent() except that it drops the
198 * refcount on the passed node, making it suitable for iterating
199 * through a node's parents.
201 * Returns a node pointer with refcount incremented, use
202 * of_node_put() on it when done.
204 struct device_node *of_get_next_parent(struct device_node *node)
206 struct device_node *parent;
208 if (!node)
209 return NULL;
211 read_lock(&devtree_lock);
212 parent = of_node_get(node->parent);
213 of_node_put(node);
214 read_unlock(&devtree_lock);
215 return parent;
219 * of_get_next_child - Iterate a node childs
220 * @node: parent node
221 * @prev: previous child of the parent node, or NULL to get first
223 * Returns a node pointer with refcount incremented, use
224 * of_node_put() on it when done.
226 struct device_node *of_get_next_child(const struct device_node *node,
227 struct device_node *prev)
229 struct device_node *next;
231 read_lock(&devtree_lock);
232 next = prev ? prev->sibling : node->child;
233 for (; next; next = next->sibling)
234 if (of_node_get(next))
235 break;
236 of_node_put(prev);
237 read_unlock(&devtree_lock);
238 return next;
240 EXPORT_SYMBOL(of_get_next_child);
243 * of_find_node_by_path - Find a node matching a full OF path
244 * @path: The full path to match
246 * Returns a node pointer with refcount incremented, use
247 * of_node_put() on it when done.
249 struct device_node *of_find_node_by_path(const char *path)
251 struct device_node *np = allnodes;
253 read_lock(&devtree_lock);
254 for (; np; np = np->allnext) {
255 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
256 && of_node_get(np))
257 break;
259 read_unlock(&devtree_lock);
260 return np;
262 EXPORT_SYMBOL(of_find_node_by_path);
265 * of_find_node_by_name - Find a node by its "name" property
266 * @from: The node to start searching from or NULL, the node
267 * you pass will not be searched, only the next one
268 * will; typically, you pass what the previous call
269 * returned. of_node_put() will be called on it
270 * @name: The name string to match against
272 * Returns a node pointer with refcount incremented, use
273 * of_node_put() on it when done.
275 struct device_node *of_find_node_by_name(struct device_node *from,
276 const char *name)
278 struct device_node *np;
280 read_lock(&devtree_lock);
281 np = from ? from->allnext : allnodes;
282 for (; np; np = np->allnext)
283 if (np->name && (of_node_cmp(np->name, name) == 0)
284 && of_node_get(np))
285 break;
286 of_node_put(from);
287 read_unlock(&devtree_lock);
288 return np;
290 EXPORT_SYMBOL(of_find_node_by_name);
293 * of_find_node_by_type - Find a node by its "device_type" property
294 * @from: The node to start searching from, or NULL to start searching
295 * the entire device tree. The node you pass will not be
296 * searched, only the next one will; typically, you pass
297 * what the previous call returned. of_node_put() will be
298 * called on from for you.
299 * @type: The type string to match against
301 * Returns a node pointer with refcount incremented, use
302 * of_node_put() on it when done.
304 struct device_node *of_find_node_by_type(struct device_node *from,
305 const char *type)
307 struct device_node *np;
309 read_lock(&devtree_lock);
310 np = from ? from->allnext : allnodes;
311 for (; np; np = np->allnext)
312 if (np->type && (of_node_cmp(np->type, type) == 0)
313 && of_node_get(np))
314 break;
315 of_node_put(from);
316 read_unlock(&devtree_lock);
317 return np;
319 EXPORT_SYMBOL(of_find_node_by_type);
322 * of_find_compatible_node - Find a node based on type and one of the
323 * tokens in its "compatible" property
324 * @from: The node to start searching from or NULL, the node
325 * you pass will not be searched, only the next one
326 * will; typically, you pass what the previous call
327 * returned. of_node_put() will be called on it
328 * @type: The type string to match "device_type" or NULL to ignore
329 * @compatible: The string to match to one of the tokens in the device
330 * "compatible" list.
332 * Returns a node pointer with refcount incremented, use
333 * of_node_put() on it when done.
335 struct device_node *of_find_compatible_node(struct device_node *from,
336 const char *type, const char *compatible)
338 struct device_node *np;
340 read_lock(&devtree_lock);
341 np = from ? from->allnext : allnodes;
342 for (; np; np = np->allnext) {
343 if (type
344 && !(np->type && (of_node_cmp(np->type, type) == 0)))
345 continue;
346 if (of_device_is_compatible(np, compatible) && of_node_get(np))
347 break;
349 of_node_put(from);
350 read_unlock(&devtree_lock);
351 return np;
353 EXPORT_SYMBOL(of_find_compatible_node);
356 * of_find_node_with_property - Find a node which has a property with
357 * the given name.
358 * @from: The node to start searching from or NULL, the node
359 * you pass will not be searched, only the next one
360 * will; typically, you pass what the previous call
361 * returned. of_node_put() will be called on it
362 * @prop_name: The name of the property to look for.
364 * Returns a node pointer with refcount incremented, use
365 * of_node_put() on it when done.
367 struct device_node *of_find_node_with_property(struct device_node *from,
368 const char *prop_name)
370 struct device_node *np;
371 struct property *pp;
373 read_lock(&devtree_lock);
374 np = from ? from->allnext : allnodes;
375 for (; np; np = np->allnext) {
376 for (pp = np->properties; pp != 0; pp = pp->next) {
377 if (of_prop_cmp(pp->name, prop_name) == 0) {
378 of_node_get(np);
379 goto out;
383 out:
384 of_node_put(from);
385 read_unlock(&devtree_lock);
386 return np;
388 EXPORT_SYMBOL(of_find_node_with_property);
391 * of_match_node - Tell if an device_node has a matching of_match structure
392 * @matches: array of of device match structures to search in
393 * @node: the of device structure to match against
395 * Low level utility function used by device matching.
397 const struct of_device_id *of_match_node(const struct of_device_id *matches,
398 const struct device_node *node)
400 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
401 int match = 1;
402 if (matches->name[0])
403 match &= node->name
404 && !strcmp(matches->name, node->name);
405 if (matches->type[0])
406 match &= node->type
407 && !strcmp(matches->type, node->type);
408 if (matches->compatible[0])
409 match &= of_device_is_compatible(node,
410 matches->compatible);
411 if (match)
412 return matches;
413 matches++;
415 return NULL;
417 EXPORT_SYMBOL(of_match_node);
420 * of_find_matching_node - Find a node based on an of_device_id match
421 * table.
422 * @from: The node to start searching from or NULL, the node
423 * you pass will not be searched, only the next one
424 * will; typically, you pass what the previous call
425 * returned. of_node_put() will be called on it
426 * @matches: array of of device match structures to search in
428 * Returns a node pointer with refcount incremented, use
429 * of_node_put() on it when done.
431 struct device_node *of_find_matching_node(struct device_node *from,
432 const struct of_device_id *matches)
434 struct device_node *np;
436 read_lock(&devtree_lock);
437 np = from ? from->allnext : allnodes;
438 for (; np; np = np->allnext) {
439 if (of_match_node(matches, np) && of_node_get(np))
440 break;
442 of_node_put(from);
443 read_unlock(&devtree_lock);
444 return np;
446 EXPORT_SYMBOL(of_find_matching_node);
449 * of_modalias_table: Table of explicit compatible ==> modalias mappings
451 * This table allows particulare compatible property values to be mapped
452 * to modalias strings. This is useful for busses which do not directly
453 * understand the OF device tree but are populated based on data contained
454 * within the device tree. SPI and I2C are the two current users of this
455 * table.
457 * In most cases, devices do not need to be listed in this table because
458 * the modalias value can be derived directly from the compatible table.
459 * However, if for any reason a value cannot be derived, then this table
460 * provides a method to override the implicit derivation.
462 * At the moment, a single table is used for all bus types because it is
463 * assumed that the data size is small and that the compatible values
464 * should already be distinct enough to differentiate between SPI, I2C
465 * and other devices.
467 struct of_modalias_table {
468 char *of_device;
469 char *modalias;
471 static struct of_modalias_table of_modalias_table[] = {
472 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
473 { "mmc-spi-slot", "mmc_spi" },
477 * of_modalias_node - Lookup appropriate modalias for a device node
478 * @node: pointer to a device tree node
479 * @modalias: Pointer to buffer that modalias value will be copied into
480 * @len: Length of modalias value
482 * Based on the value of the compatible property, this routine will determine
483 * an appropriate modalias value for a particular device tree node. Two
484 * separate methods are attempted to derive a modalias value.
486 * First method is to lookup the compatible value in of_modalias_table.
487 * Second is to strip off the manufacturer prefix from the first
488 * compatible entry and use the remainder as modalias
490 * This routine returns 0 on success
492 int of_modalias_node(struct device_node *node, char *modalias, int len)
494 int i, cplen;
495 const char *compatible;
496 const char *p;
498 /* 1. search for exception list entry */
499 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
500 compatible = of_modalias_table[i].of_device;
501 if (!of_device_is_compatible(node, compatible))
502 continue;
503 strlcpy(modalias, of_modalias_table[i].modalias, len);
504 return 0;
507 compatible = of_get_property(node, "compatible", &cplen);
508 if (!compatible)
509 return -ENODEV;
511 /* 2. take first compatible entry and strip manufacturer */
512 p = strchr(compatible, ',');
513 if (!p)
514 return -ENODEV;
515 p++;
516 strlcpy(modalias, p, len);
517 return 0;
519 EXPORT_SYMBOL_GPL(of_modalias_node);
522 * of_parse_phandle - Resolve a phandle property to a device_node pointer
523 * @np: Pointer to device node holding phandle property
524 * @phandle_name: Name of property holding a phandle value
525 * @index: For properties holding a table of phandles, this is the index into
526 * the table
528 * Returns the device_node pointer with refcount incremented. Use
529 * of_node_put() on it when done.
531 struct device_node *
532 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
534 const phandle *phandle;
535 int size;
537 phandle = of_get_property(np, phandle_name, &size);
538 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
539 return NULL;
541 return of_find_node_by_phandle(phandle[index]);
543 EXPORT_SYMBOL(of_parse_phandle);
546 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
547 * @np: pointer to a device tree node containing a list
548 * @list_name: property name that contains a list
549 * @cells_name: property name that specifies phandles' arguments count
550 * @index: index of a phandle to parse out
551 * @out_node: optional pointer to device_node struct pointer (will be filled)
552 * @out_args: optional pointer to arguments pointer (will be filled)
554 * This function is useful to parse lists of phandles and their arguments.
555 * Returns 0 on success and fills out_node and out_args, on error returns
556 * appropriate errno value.
558 * Example:
560 * phandle1: node1 {
561 * #list-cells = <2>;
564 * phandle2: node2 {
565 * #list-cells = <1>;
568 * node3 {
569 * list = <&phandle1 1 2 &phandle2 3>;
572 * To get a device_node of the `node2' node you may call this:
573 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
575 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
576 const char *cells_name, int index,
577 struct device_node **out_node,
578 const void **out_args)
580 int ret = -EINVAL;
581 const u32 *list;
582 const u32 *list_end;
583 int size;
584 int cur_index = 0;
585 struct device_node *node = NULL;
586 const void *args = NULL;
588 list = of_get_property(np, list_name, &size);
589 if (!list) {
590 ret = -ENOENT;
591 goto err0;
593 list_end = list + size / sizeof(*list);
595 while (list < list_end) {
596 const u32 *cells;
597 const phandle *phandle;
599 phandle = list++;
600 args = list;
602 /* one cell hole in the list = <>; */
603 if (!*phandle)
604 goto next;
606 node = of_find_node_by_phandle(*phandle);
607 if (!node) {
608 pr_debug("%s: could not find phandle\n",
609 np->full_name);
610 goto err0;
613 cells = of_get_property(node, cells_name, &size);
614 if (!cells || size != sizeof(*cells)) {
615 pr_debug("%s: could not get %s for %s\n",
616 np->full_name, cells_name, node->full_name);
617 goto err1;
620 list += *cells;
621 if (list > list_end) {
622 pr_debug("%s: insufficient arguments length\n",
623 np->full_name);
624 goto err1;
626 next:
627 if (cur_index == index)
628 break;
630 of_node_put(node);
631 node = NULL;
632 args = NULL;
633 cur_index++;
636 if (!node) {
638 * args w/o node indicates that the loop above has stopped at
639 * the 'hole' cell. Report this differently.
641 if (args)
642 ret = -EEXIST;
643 else
644 ret = -ENOENT;
645 goto err0;
648 if (out_node)
649 *out_node = node;
650 if (out_args)
651 *out_args = args;
653 return 0;
654 err1:
655 of_node_put(node);
656 err0:
657 pr_debug("%s failed with status %d\n", __func__, ret);
658 return ret;
660 EXPORT_SYMBOL(of_parse_phandles_with_args);