Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Consolidate of_find_node_by routines

This consolidates the routines of_find_node_by_path, of_find_node_by_name,
of_find_node_by_type and of_find_compatible_device. Again, the comparison
of strings are done differently by Sparc and PowerPC and also these add
read_locks around the iterations.

Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
Acked-by: Paul Mackerras <paulus@samba.org>
Acked-by: David S. Miller <davem@davemloft.net>

+121 -234
+1 -114
arch/powerpc/kernel/prom.c
··· 78 78 struct boot_param_header *initial_boot_params; 79 79 #endif 80 80 81 - static struct device_node *allnodes = NULL; 81 + extern struct device_node *allnodes; /* temporary while merging */ 82 82 83 83 extern rwlock_t devtree_lock; /* temporary while merging */ 84 84 ··· 1082 1082 * this isn't dealt with yet. 1083 1083 * 1084 1084 *******/ 1085 - 1086 - /** 1087 - * of_find_node_by_name - Find a node by its "name" property 1088 - * @from: The node to start searching from or NULL, the node 1089 - * you pass will not be searched, only the next one 1090 - * will; typically, you pass what the previous call 1091 - * returned. of_node_put() will be called on it 1092 - * @name: The name string to match against 1093 - * 1094 - * Returns a node pointer with refcount incremented, use 1095 - * of_node_put() on it when done. 1096 - */ 1097 - struct device_node *of_find_node_by_name(struct device_node *from, 1098 - const char *name) 1099 - { 1100 - struct device_node *np; 1101 - 1102 - read_lock(&devtree_lock); 1103 - np = from ? from->allnext : allnodes; 1104 - for (; np != NULL; np = np->allnext) 1105 - if (np->name != NULL && strcasecmp(np->name, name) == 0 1106 - && of_node_get(np)) 1107 - break; 1108 - of_node_put(from); 1109 - read_unlock(&devtree_lock); 1110 - return np; 1111 - } 1112 - EXPORT_SYMBOL(of_find_node_by_name); 1113 - 1114 - /** 1115 - * of_find_node_by_type - Find a node by its "device_type" property 1116 - * @from: The node to start searching from, or NULL to start searching 1117 - * the entire device tree. The node you pass will not be 1118 - * searched, only the next one will; typically, you pass 1119 - * what the previous call returned. of_node_put() will be 1120 - * called on from for you. 1121 - * @type: The type string to match against 1122 - * 1123 - * Returns a node pointer with refcount incremented, use 1124 - * of_node_put() on it when done. 1125 - */ 1126 - struct device_node *of_find_node_by_type(struct device_node *from, 1127 - const char *type) 1128 - { 1129 - struct device_node *np; 1130 - 1131 - read_lock(&devtree_lock); 1132 - np = from ? from->allnext : allnodes; 1133 - for (; np != 0; np = np->allnext) 1134 - if (np->type != 0 && strcasecmp(np->type, type) == 0 1135 - && of_node_get(np)) 1136 - break; 1137 - of_node_put(from); 1138 - read_unlock(&devtree_lock); 1139 - return np; 1140 - } 1141 - EXPORT_SYMBOL(of_find_node_by_type); 1142 - 1143 - /** 1144 - * of_find_compatible_node - Find a node based on type and one of the 1145 - * tokens in its "compatible" property 1146 - * @from: The node to start searching from or NULL, the node 1147 - * you pass will not be searched, only the next one 1148 - * will; typically, you pass what the previous call 1149 - * returned. of_node_put() will be called on it 1150 - * @type: The type string to match "device_type" or NULL to ignore 1151 - * @compatible: The string to match to one of the tokens in the device 1152 - * "compatible" list. 1153 - * 1154 - * Returns a node pointer with refcount incremented, use 1155 - * of_node_put() on it when done. 1156 - */ 1157 - struct device_node *of_find_compatible_node(struct device_node *from, 1158 - const char *type, const char *compatible) 1159 - { 1160 - struct device_node *np; 1161 - 1162 - read_lock(&devtree_lock); 1163 - np = from ? from->allnext : allnodes; 1164 - for (; np != 0; np = np->allnext) { 1165 - if (type != NULL 1166 - && !(np->type != 0 && strcasecmp(np->type, type) == 0)) 1167 - continue; 1168 - if (of_device_is_compatible(np, compatible) && of_node_get(np)) 1169 - break; 1170 - } 1171 - of_node_put(from); 1172 - read_unlock(&devtree_lock); 1173 - return np; 1174 - } 1175 - EXPORT_SYMBOL(of_find_compatible_node); 1176 - 1177 - /** 1178 - * of_find_node_by_path - Find a node matching a full OF path 1179 - * @path: The full path to match 1180 - * 1181 - * Returns a node pointer with refcount incremented, use 1182 - * of_node_put() on it when done. 1183 - */ 1184 - struct device_node *of_find_node_by_path(const char *path) 1185 - { 1186 - struct device_node *np = allnodes; 1187 - 1188 - read_lock(&devtree_lock); 1189 - for (; np != 0; np = np->allnext) { 1190 - if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0 1191 - && of_node_get(np)) 1192 - break; 1193 - } 1194 - read_unlock(&devtree_lock); 1195 - return np; 1196 - } 1197 - EXPORT_SYMBOL(of_find_node_by_path); 1198 1085 1199 1086 /** 1200 1087 * of_find_node_by_phandle - Find a node given a phandle
+1 -60
arch/sparc/kernel/prom.c
··· 25 25 #include <asm/prom.h> 26 26 #include <asm/oplib.h> 27 27 28 - static struct device_node *allnodes; 28 + extern struct device_node *allnodes; /* temporary while merging */ 29 29 30 30 extern rwlock_t devtree_lock; /* temporary while merging */ 31 - 32 - struct device_node *of_find_node_by_path(const char *path) 33 - { 34 - struct device_node *np = allnodes; 35 - 36 - for (; np != 0; np = np->allnext) { 37 - if (np->full_name != 0 && strcmp(np->full_name, path) == 0) 38 - break; 39 - } 40 - 41 - return np; 42 - } 43 - EXPORT_SYMBOL(of_find_node_by_path); 44 31 45 32 struct device_node *of_find_node_by_phandle(phandle handle) 46 33 { ··· 40 53 return np; 41 54 } 42 55 EXPORT_SYMBOL(of_find_node_by_phandle); 43 - 44 - struct device_node *of_find_node_by_name(struct device_node *from, 45 - const char *name) 46 - { 47 - struct device_node *np; 48 - 49 - np = from ? from->allnext : allnodes; 50 - for (; np != NULL; np = np->allnext) 51 - if (np->name != NULL && strcmp(np->name, name) == 0) 52 - break; 53 - 54 - return np; 55 - } 56 - EXPORT_SYMBOL(of_find_node_by_name); 57 - 58 - struct device_node *of_find_node_by_type(struct device_node *from, 59 - const char *type) 60 - { 61 - struct device_node *np; 62 - 63 - np = from ? from->allnext : allnodes; 64 - for (; np != 0; np = np->allnext) 65 - if (np->type != 0 && strcmp(np->type, type) == 0) 66 - break; 67 - 68 - return np; 69 - } 70 - EXPORT_SYMBOL(of_find_node_by_type); 71 - 72 - struct device_node *of_find_compatible_node(struct device_node *from, 73 - const char *type, const char *compatible) 74 - { 75 - struct device_node *np; 76 - 77 - np = from ? from->allnext : allnodes; 78 - for (; np != 0; np = np->allnext) { 79 - if (type != NULL 80 - && !(np->type != 0 && strcmp(np->type, type) == 0)) 81 - continue; 82 - if (of_device_is_compatible(np, compatible)) 83 - break; 84 - } 85 - 86 - return np; 87 - } 88 - EXPORT_SYMBOL(of_find_compatible_node); 89 56 90 57 int of_getintprop_default(struct device_node *np, const char *name, int def) 91 58 {
+1 -60
arch/sparc64/kernel/prom.c
··· 30 30 #include <asm/upa.h> 31 31 #include <asm/smp.h> 32 32 33 - static struct device_node *allnodes; 33 + extern struct device_node *allnodes; /* temporary while merging */ 34 34 35 35 extern rwlock_t devtree_lock; /* temporary while merging */ 36 - 37 - struct device_node *of_find_node_by_path(const char *path) 38 - { 39 - struct device_node *np = allnodes; 40 - 41 - for (; np != 0; np = np->allnext) { 42 - if (np->full_name != 0 && strcmp(np->full_name, path) == 0) 43 - break; 44 - } 45 - 46 - return np; 47 - } 48 - EXPORT_SYMBOL(of_find_node_by_path); 49 36 50 37 struct device_node *of_find_node_by_phandle(phandle handle) 51 38 { ··· 45 58 return np; 46 59 } 47 60 EXPORT_SYMBOL(of_find_node_by_phandle); 48 - 49 - struct device_node *of_find_node_by_name(struct device_node *from, 50 - const char *name) 51 - { 52 - struct device_node *np; 53 - 54 - np = from ? from->allnext : allnodes; 55 - for (; np != NULL; np = np->allnext) 56 - if (np->name != NULL && strcmp(np->name, name) == 0) 57 - break; 58 - 59 - return np; 60 - } 61 - EXPORT_SYMBOL(of_find_node_by_name); 62 - 63 - struct device_node *of_find_node_by_type(struct device_node *from, 64 - const char *type) 65 - { 66 - struct device_node *np; 67 - 68 - np = from ? from->allnext : allnodes; 69 - for (; np != 0; np = np->allnext) 70 - if (np->type != 0 && strcmp(np->type, type) == 0) 71 - break; 72 - 73 - return np; 74 - } 75 - EXPORT_SYMBOL(of_find_node_by_type); 76 - 77 - struct device_node *of_find_compatible_node(struct device_node *from, 78 - const char *type, const char *compatible) 79 - { 80 - struct device_node *np; 81 - 82 - np = from ? from->allnext : allnodes; 83 - for (; np != 0; np = np->allnext) { 84 - if (type != NULL 85 - && !(np->type != 0 && strcmp(np->type, type) == 0)) 86 - continue; 87 - if (of_device_is_compatible(np, compatible)) 88 - break; 89 - } 90 - 91 - return np; 92 - } 93 - EXPORT_SYMBOL(of_find_compatible_node); 94 61 95 62 int of_getintprop_default(struct device_node *np, const char *name, int def) 96 63 {
+115
drivers/of/base.c
··· 20 20 #include <linux/of.h> 21 21 #include <linux/spinlock.h> 22 22 23 + struct device_node *allnodes; 24 + 23 25 /* use when traversing tree through the allnext, child, sibling, 24 26 * or parent members of struct device_node. 25 27 */ ··· 160 158 return next; 161 159 } 162 160 EXPORT_SYMBOL(of_get_next_child); 161 + 162 + /** 163 + * of_find_node_by_path - Find a node matching a full OF path 164 + * @path: The full path to match 165 + * 166 + * Returns a node pointer with refcount incremented, use 167 + * of_node_put() on it when done. 168 + */ 169 + struct device_node *of_find_node_by_path(const char *path) 170 + { 171 + struct device_node *np = allnodes; 172 + 173 + read_lock(&devtree_lock); 174 + for (; np; np = np->allnext) { 175 + if (np->full_name && (of_node_cmp(np->full_name, path) == 0) 176 + && of_node_get(np)) 177 + break; 178 + } 179 + read_unlock(&devtree_lock); 180 + return np; 181 + } 182 + EXPORT_SYMBOL(of_find_node_by_path); 183 + 184 + /** 185 + * of_find_node_by_name - Find a node by its "name" property 186 + * @from: The node to start searching from or NULL, the node 187 + * you pass will not be searched, only the next one 188 + * will; typically, you pass what the previous call 189 + * returned. of_node_put() will be called on it 190 + * @name: The name string to match against 191 + * 192 + * Returns a node pointer with refcount incremented, use 193 + * of_node_put() on it when done. 194 + */ 195 + struct device_node *of_find_node_by_name(struct device_node *from, 196 + const char *name) 197 + { 198 + struct device_node *np; 199 + 200 + read_lock(&devtree_lock); 201 + np = from ? from->allnext : allnodes; 202 + for (; np; np = np->allnext) 203 + if (np->name && (of_node_cmp(np->name, name) == 0) 204 + && of_node_get(np)) 205 + break; 206 + of_node_put(from); 207 + read_unlock(&devtree_lock); 208 + return np; 209 + } 210 + EXPORT_SYMBOL(of_find_node_by_name); 211 + 212 + /** 213 + * of_find_node_by_type - Find a node by its "device_type" property 214 + * @from: The node to start searching from, or NULL to start searching 215 + * the entire device tree. The node you pass will not be 216 + * searched, only the next one will; typically, you pass 217 + * what the previous call returned. of_node_put() will be 218 + * called on from for you. 219 + * @type: The type string to match against 220 + * 221 + * Returns a node pointer with refcount incremented, use 222 + * of_node_put() on it when done. 223 + */ 224 + struct device_node *of_find_node_by_type(struct device_node *from, 225 + const char *type) 226 + { 227 + struct device_node *np; 228 + 229 + read_lock(&devtree_lock); 230 + np = from ? from->allnext : allnodes; 231 + for (; np; np = np->allnext) 232 + if (np->type && (of_node_cmp(np->type, type) == 0) 233 + && of_node_get(np)) 234 + break; 235 + of_node_put(from); 236 + read_unlock(&devtree_lock); 237 + return np; 238 + } 239 + EXPORT_SYMBOL(of_find_node_by_type); 240 + 241 + /** 242 + * of_find_compatible_node - Find a node based on type and one of the 243 + * tokens in its "compatible" property 244 + * @from: The node to start searching from or NULL, the node 245 + * you pass will not be searched, only the next one 246 + * will; typically, you pass what the previous call 247 + * returned. of_node_put() will be called on it 248 + * @type: The type string to match "device_type" or NULL to ignore 249 + * @compatible: The string to match to one of the tokens in the device 250 + * "compatible" list. 251 + * 252 + * Returns a node pointer with refcount incremented, use 253 + * of_node_put() on it when done. 254 + */ 255 + struct device_node *of_find_compatible_node(struct device_node *from, 256 + const char *type, const char *compatible) 257 + { 258 + struct device_node *np; 259 + 260 + read_lock(&devtree_lock); 261 + np = from ? from->allnext : allnodes; 262 + for (; np; np = np->allnext) { 263 + if (type 264 + && !(np->type && (of_node_cmp(np->type, type) == 0))) 265 + continue; 266 + if (of_device_is_compatible(np, compatible) && of_node_get(np)) 267 + break; 268 + } 269 + of_node_put(from); 270 + read_unlock(&devtree_lock); 271 + return np; 272 + } 273 + EXPORT_SYMBOL(of_find_compatible_node);
+1
include/asm-powerpc/prom.h
··· 26 26 27 27 #define of_compat_cmp(s1, s2, l) strncasecmp((s1), (s2), (l)) 28 28 #define of_prop_cmp(s1, s2) strcmp((s1), (s2)) 29 + #define of_node_cmp(s1, s2) strcasecmp((s1), (s2)) 29 30 30 31 /* Definitions used by the flattened device tree */ 31 32 #define OF_DT_HEADER 0xd00dfeed /* marker */
+1
include/asm-sparc/prom.h
··· 25 25 26 26 #define of_compat_cmp(s1, s2, l) strncmp((s1), (s2), (l)) 27 27 #define of_prop_cmp(s1, s2) strcasecmp((s1), (s2)) 28 + #define of_node_cmp(s1, s2) strcmp((s1), (s2)) 28 29 29 30 typedef u32 phandle; 30 31 typedef u32 ihandle;
+1
include/asm-sparc64/prom.h
··· 25 25 26 26 #define of_compat_cmp(s1, s2, l) strncmp((s1), (s2), (l)) 27 27 #define of_prop_cmp(s1, s2) strcasecmp((s1), (s2)) 28 + #define of_node_cmp(s1, s2) strcmp((s1), (s2)) 28 29 29 30 typedef u32 phandle; 30 31 typedef u32 ihandle;