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