aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/base.c
diff options
context:
space:
mode:
authorShawn Guo <shawn.guo@linaro.org>2011-08-03 06:28:14 -0400
committerGrant Likely <grant.likely@secretlab.ca>2011-08-03 08:02:31 -0400
commit750f463a749e28464151ad26938d11b07b1c43cb (patch)
tree6aabb1512a4000615e93b25beccba606b62f7237 /drivers/of/base.c
parented8f37370d83e695c0a4fa5d5fc7a83ecb947526 (diff)
dt: add of_alias_scan and of_alias_get_id
The patch adds function of_alias_scan to populate a global lookup table with the properties of 'aliases' node and function of_alias_get_id for drivers to find alias id from the lookup table. Signed-off-by: Shawn Guo <shawn.guo@linaro.org> [grant.likely: add locking and rework parse loop] Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Diffstat (limited to 'drivers/of/base.c')
-rw-r--r--drivers/of/base.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 3ff22e32b602..fb28b5af733b 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -17,14 +17,39 @@
17 * as published by the Free Software Foundation; either version 17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version. 18 * 2 of the License, or (at your option) any later version.
19 */ 19 */
20#include <linux/ctype.h>
20#include <linux/module.h> 21#include <linux/module.h>
21#include <linux/of.h> 22#include <linux/of.h>
22#include <linux/spinlock.h> 23#include <linux/spinlock.h>
23#include <linux/slab.h> 24#include <linux/slab.h>
24#include <linux/proc_fs.h> 25#include <linux/proc_fs.h>
25 26
27/**
28 * struct alias_prop - Alias property in 'aliases' node
29 * @link: List node to link the structure in aliases_lookup list
30 * @alias: Alias property name
31 * @np: Pointer to device_node that the alias stands for
32 * @id: Index value from end of alias name
33 * @stem: Alias string without the index
34 *
35 * The structure represents one alias property of 'aliases' node as
36 * an entry in aliases_lookup list.
37 */
38struct alias_prop {
39 struct list_head link;
40 const char *alias;
41 struct device_node *np;
42 int id;
43 char stem[0];
44};
45
46static LIST_HEAD(aliases_lookup);
47
26struct device_node *allnodes; 48struct device_node *allnodes;
27struct device_node *of_chosen; 49struct device_node *of_chosen;
50struct device_node *of_aliases;
51
52static DEFINE_MUTEX(of_aliases_mutex);
28 53
29/* use when traversing tree through the allnext, child, sibling, 54/* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node. 55 * or parent members of struct device_node.
@@ -988,3 +1013,108 @@ out_unlock:
988} 1013}
989#endif /* defined(CONFIG_OF_DYNAMIC) */ 1014#endif /* defined(CONFIG_OF_DYNAMIC) */
990 1015
1016static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1017 int id, const char *stem, int stem_len)
1018{
1019 ap->id = id;
1020 ap->np = np;
1021 strncpy(ap->stem, stem, stem_len);
1022 ap->stem[stem_len] = 0;
1023 list_add_tail(&ap->link, &aliases_lookup);
1024 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1025 ap->alias, ap->stem, ap->id, np ? np->full_name : NULL);
1026}
1027
1028/**
1029 * of_alias_scan() - Scan all properties of 'aliases' node
1030 *
1031 * The function scans all the properties of 'aliases' node and populate
1032 * the global lookup table with the properties. It returns the
1033 * number of alias_prop found, or error code in error case.
1034 */
1035__init void of_alias_scan(void)
1036{
1037 struct property *pp;
1038
1039 if (!of_aliases)
1040 return;
1041
1042 for_each_property(pp, of_aliases->properties) {
1043 const char *start = pp->name;
1044 const char *end = start + strlen(start);
1045 struct device_node *np;
1046 struct alias_prop *ap;
1047 int id, len;
1048
1049 /* Skip those we do not want to proceed */
1050 if (!strcmp(pp->name, "name") ||
1051 !strcmp(pp->name, "phandle") ||
1052 !strcmp(pp->name, "linux,phandle"))
1053 continue;
1054
1055 np = of_find_node_by_path(pp->value);
1056 if (!np)
1057 continue;
1058
1059 /* walk alias backwards to extract the id and 'stem' string */
1060 while (isdigit(*(end-1)) && end > start)
1061 end--;
1062 len = end - start;
1063 id = strlen(end) ? simple_strtoul(end, NULL, 10) : -1;
1064
1065 /* Allocate an alias_prop with enough space for the stem */
1066 ap = early_init_dt_alloc_memory_arch(sizeof(*ap) + len + 1, 4);
1067 if (!ap)
1068 continue;
1069 ap->alias = start;
1070 of_alias_add(ap, np, id, start, len);
1071 }
1072}
1073
1074/**
1075 * of_alias_get_id() - Get alias id for the given device_node
1076 * @np: Pointer to the given device_node
1077 * @stem: Alias stem of the given device_node
1078 *
1079 * The function travels the lookup table to get alias id for the given
1080 * device_node and alias stem. It returns the alias id if find it.
1081 * If not, dynamically creates one in the lookup table and returns it,
1082 * or returns error code if fail to create.
1083 */
1084int of_alias_get_id(struct device_node *np, const char *stem)
1085{
1086 struct alias_prop *app;
1087 int id = 0;
1088 bool found = false;
1089
1090 mutex_lock(&of_aliases_mutex);
1091 list_for_each_entry(app, &aliases_lookup, link) {
1092 if (strcmp(app->stem, stem) != 0)
1093 continue;
1094
1095 if (np == app->np) {
1096 found = true;
1097 id = app->id;
1098 break;
1099 }
1100
1101 if (id <= app->id)
1102 id = app->id + 1;
1103 }
1104
1105 /* If an id is not found, then allocate a new one */
1106 if (!found) {
1107 app = kzalloc(sizeof(*app) + strlen(stem) + 1, 4);
1108 if (!app) {
1109 id = -ENODEV;
1110 goto out;
1111 }
1112 of_alias_add(app, np, id, stem, strlen(stem));
1113 }
1114
1115 out:
1116 mutex_unlock(&of_aliases_mutex);
1117
1118 return id;
1119}
1120EXPORT_SYMBOL_GPL(of_alias_get_id);