diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2011-08-03 21:10:30 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2011-08-03 21:10:30 -0400 |
commit | 27665ffa22b9b83bab226bf12c61424f7f1f8995 (patch) | |
tree | aaccc030778bb6878c46ab9ca035901af67d78e4 | |
parent | 14595f708eeb528fabcee339ed0b9e0a2ecec73f (diff) | |
parent | 750f463a749e28464151ad26938d11b07b1c43cb (diff) |
Merge branch 'devicetree/next' of git://git.secretlab.ca/git/linux-2.6
* 'devicetree/next' of git://git.secretlab.ca/git/linux-2.6:
dt: add of_alias_scan and of_alias_get_id
-rw-r--r-- | drivers/of/base.c | 130 | ||||
-rw-r--r-- | drivers/of/fdt.c | 4 | ||||
-rw-r--r-- | include/linux/of.h | 8 | ||||
-rw-r--r-- | include/linux/of_fdt.h | 1 |
4 files changed, 141 insertions, 2 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 | */ | ||
38 | struct alias_prop { | ||
39 | struct list_head link; | ||
40 | const char *alias; | ||
41 | struct device_node *np; | ||
42 | int id; | ||
43 | char stem[0]; | ||
44 | }; | ||
45 | |||
46 | static LIST_HEAD(aliases_lookup); | ||
47 | |||
26 | struct device_node *allnodes; | 48 | struct device_node *allnodes; |
27 | struct device_node *of_chosen; | 49 | struct device_node *of_chosen; |
50 | struct device_node *of_aliases; | ||
51 | |||
52 | static 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 | ||
1016 | static 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 | */ | ||
1084 | int 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 | } | ||
1120 | EXPORT_SYMBOL_GPL(of_alias_get_id); | ||
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 65200af29c52..13d6d3a96b31 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c | |||
@@ -707,10 +707,12 @@ void __init unflatten_device_tree(void) | |||
707 | __unflatten_device_tree(initial_boot_params, &allnodes, | 707 | __unflatten_device_tree(initial_boot_params, &allnodes, |
708 | early_init_dt_alloc_memory_arch); | 708 | early_init_dt_alloc_memory_arch); |
709 | 709 | ||
710 | /* Get pointer to OF "/chosen" node for use everywhere */ | 710 | /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */ |
711 | of_chosen = of_find_node_by_path("/chosen"); | 711 | of_chosen = of_find_node_by_path("/chosen"); |
712 | if (of_chosen == NULL) | 712 | if (of_chosen == NULL) |
713 | of_chosen = of_find_node_by_path("/chosen@0"); | 713 | of_chosen = of_find_node_by_path("/chosen@0"); |
714 | of_aliases = of_find_node_by_path("/aliases"); | ||
715 | of_alias_scan(); | ||
714 | } | 716 | } |
715 | 717 | ||
716 | #endif /* CONFIG_OF_EARLY_FLATTREE */ | 718 | #endif /* CONFIG_OF_EARLY_FLATTREE */ |
diff --git a/include/linux/of.h b/include/linux/of.h index 0085bb01c041..bc3dc6399547 100644 --- a/include/linux/of.h +++ b/include/linux/of.h | |||
@@ -68,6 +68,7 @@ struct device_node { | |||
68 | /* Pointer for first entry in chain of all nodes. */ | 68 | /* Pointer for first entry in chain of all nodes. */ |
69 | extern struct device_node *allnodes; | 69 | extern struct device_node *allnodes; |
70 | extern struct device_node *of_chosen; | 70 | extern struct device_node *of_chosen; |
71 | extern struct device_node *of_aliases; | ||
71 | extern rwlock_t devtree_lock; | 72 | extern rwlock_t devtree_lock; |
72 | 73 | ||
73 | static inline bool of_have_populated_dt(void) | 74 | static inline bool of_have_populated_dt(void) |
@@ -209,6 +210,9 @@ extern int of_device_is_available(const struct device_node *device); | |||
209 | extern const void *of_get_property(const struct device_node *node, | 210 | extern const void *of_get_property(const struct device_node *node, |
210 | const char *name, | 211 | const char *name, |
211 | int *lenp); | 212 | int *lenp); |
213 | #define for_each_property(pp, properties) \ | ||
214 | for (pp = properties; pp != NULL; pp = pp->next) | ||
215 | |||
212 | extern int of_n_addr_cells(struct device_node *np); | 216 | extern int of_n_addr_cells(struct device_node *np); |
213 | extern int of_n_size_cells(struct device_node *np); | 217 | extern int of_n_size_cells(struct device_node *np); |
214 | extern const struct of_device_id *of_match_node( | 218 | extern const struct of_device_id *of_match_node( |
@@ -221,6 +225,10 @@ extern int of_parse_phandles_with_args(struct device_node *np, | |||
221 | const char *list_name, const char *cells_name, int index, | 225 | const char *list_name, const char *cells_name, int index, |
222 | struct device_node **out_node, const void **out_args); | 226 | struct device_node **out_node, const void **out_args); |
223 | 227 | ||
228 | extern void *early_init_dt_alloc_memory_arch(u64 size, u64 align); | ||
229 | extern void of_alias_scan(void); | ||
230 | extern int of_alias_get_id(struct device_node *np, const char *stem); | ||
231 | |||
224 | extern int of_machine_is_compatible(const char *compat); | 232 | extern int of_machine_is_compatible(const char *compat); |
225 | 233 | ||
226 | extern int prom_add_property(struct device_node* np, struct property* prop); | 234 | extern int prom_add_property(struct device_node* np, struct property* prop); |
diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h index c84d900fbbb3..b74b74ffe0e7 100644 --- a/include/linux/of_fdt.h +++ b/include/linux/of_fdt.h | |||
@@ -97,7 +97,6 @@ extern void early_init_dt_check_for_initrd(unsigned long node); | |||
97 | extern int early_init_dt_scan_memory(unsigned long node, const char *uname, | 97 | extern int early_init_dt_scan_memory(unsigned long node, const char *uname, |
98 | int depth, void *data); | 98 | int depth, void *data); |
99 | extern void early_init_dt_add_memory_arch(u64 base, u64 size); | 99 | extern void early_init_dt_add_memory_arch(u64 base, u64 size); |
100 | extern void * early_init_dt_alloc_memory_arch(u64 size, u64 align); | ||
101 | extern u64 dt_mem_next_cell(int s, __be32 **cellp); | 100 | extern u64 dt_mem_next_cell(int s, __be32 **cellp); |
102 | 101 | ||
103 | /* | 102 | /* |