lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [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 | * |
| 12 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 13 | * Grant Likely. |
| 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 | */ |
| 20 | #include <linux/ctype.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/proc_fs.h> |
| 26 | |
| 27 | /** |
| 28 | * struct alias_prop - Alias property in 'aliases' node |
| 29 | * @link: List node to link the structure in aliases_lookup list |
| 30 | * @alias: Alias property name |
| 31 | * @np: Pointer to device_node that the alias stands for |
| 32 | * @id: Index value from end of alias name |
| 33 | * @stem: Alias string without the index |
| 34 | * |
| 35 | * The structure represents one alias property of 'aliases' node as |
| 36 | * an entry in aliases_lookup list. |
| 37 | */ |
| 38 | struct alias_prop { |
| 39 | struct list_head link; |
| 40 | const char *alias; |
| 41 | struct device_node *np; |
| 42 | int id; |
| 43 | char stem[0]; |
| 44 | }; |
| 45 | |
| 46 | static LIST_HEAD(aliases_lookup); |
| 47 | |
| 48 | struct device_node *allnodes; |
| 49 | struct device_node *of_chosen; |
| 50 | struct device_node *of_aliases; |
| 51 | |
| 52 | static DEFINE_MUTEX(of_aliases_mutex); |
| 53 | |
| 54 | /* use when traversing tree through the allnext, child, sibling, |
| 55 | * or parent members of struct device_node. |
| 56 | */ |
| 57 | DEFINE_RAW_SPINLOCK(devtree_lock); |
| 58 | |
| 59 | int of_n_addr_cells(struct device_node *np) |
| 60 | { |
| 61 | const __be32 *ip; |
| 62 | |
| 63 | do { |
| 64 | if (np->parent) |
| 65 | np = np->parent; |
| 66 | ip = of_get_property(np, "#address-cells", NULL); |
| 67 | if (ip) |
| 68 | return be32_to_cpup(ip); |
| 69 | } while (np->parent); |
| 70 | /* No #address-cells property for the root node */ |
| 71 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 72 | } |
| 73 | EXPORT_SYMBOL(of_n_addr_cells); |
| 74 | |
| 75 | int of_n_size_cells(struct device_node *np) |
| 76 | { |
| 77 | const __be32 *ip; |
| 78 | |
| 79 | do { |
| 80 | if (np->parent) |
| 81 | np = np->parent; |
| 82 | ip = of_get_property(np, "#size-cells", NULL); |
| 83 | if (ip) |
| 84 | return be32_to_cpup(ip); |
| 85 | } while (np->parent); |
| 86 | /* No #size-cells property for the root node */ |
| 87 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 88 | } |
| 89 | EXPORT_SYMBOL(of_n_size_cells); |
| 90 | |
| 91 | #if defined(CONFIG_OF_DYNAMIC) |
| 92 | /** |
| 93 | * of_node_get - Increment refcount of a node |
| 94 | * @node: Node to inc refcount, NULL is supported to |
| 95 | * simplify writing of callers |
| 96 | * |
| 97 | * Returns node. |
| 98 | */ |
| 99 | struct device_node *of_node_get(struct device_node *node) |
| 100 | { |
| 101 | if (node) |
| 102 | kref_get(&node->kref); |
| 103 | return node; |
| 104 | } |
| 105 | EXPORT_SYMBOL(of_node_get); |
| 106 | |
| 107 | static inline struct device_node *kref_to_device_node(struct kref *kref) |
| 108 | { |
| 109 | return container_of(kref, struct device_node, kref); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * of_node_release - release a dynamically allocated node |
| 114 | * @kref: kref element of the node to be released |
| 115 | * |
| 116 | * In of_node_put() this function is passed to kref_put() |
| 117 | * as the destructor. |
| 118 | */ |
| 119 | static void of_node_release(struct kref *kref) |
| 120 | { |
| 121 | struct device_node *node = kref_to_device_node(kref); |
| 122 | struct property *prop = node->properties; |
| 123 | |
| 124 | /* We should never be releasing nodes that haven't been detached. */ |
| 125 | if (!of_node_check_flag(node, OF_DETACHED)) { |
| 126 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); |
| 127 | dump_stack(); |
| 128 | kref_init(&node->kref); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if (!of_node_check_flag(node, OF_DYNAMIC)) |
| 133 | return; |
| 134 | |
| 135 | while (prop) { |
| 136 | struct property *next = prop->next; |
| 137 | kfree(prop->name); |
| 138 | kfree(prop->value); |
| 139 | kfree(prop); |
| 140 | prop = next; |
| 141 | |
| 142 | if (!prop) { |
| 143 | prop = node->deadprops; |
| 144 | node->deadprops = NULL; |
| 145 | } |
| 146 | } |
| 147 | kfree(node->full_name); |
| 148 | kfree(node->data); |
| 149 | kfree(node); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * of_node_put - Decrement refcount of a node |
| 154 | * @node: Node to dec refcount, NULL is supported to |
| 155 | * simplify writing of callers |
| 156 | * |
| 157 | */ |
| 158 | void of_node_put(struct device_node *node) |
| 159 | { |
| 160 | if (node) |
| 161 | kref_put(&node->kref, of_node_release); |
| 162 | } |
| 163 | EXPORT_SYMBOL(of_node_put); |
| 164 | #endif /* CONFIG_OF_DYNAMIC */ |
| 165 | |
| 166 | static struct property *__of_find_property(const struct device_node *np, |
| 167 | const char *name, int *lenp) |
| 168 | { |
| 169 | struct property *pp; |
| 170 | |
| 171 | if (!np) |
| 172 | return NULL; |
| 173 | |
| 174 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 175 | if (of_prop_cmp(pp->name, name) == 0) { |
| 176 | if (lenp != 0) |
| 177 | *lenp = pp->length; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return pp; |
| 183 | } |
| 184 | |
| 185 | struct property *of_find_property(const struct device_node *np, |
| 186 | const char *name, |
| 187 | int *lenp) |
| 188 | { |
| 189 | struct property *pp; |
| 190 | unsigned long flags; |
| 191 | |
| 192 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 193 | pp = __of_find_property(np, name, lenp); |
| 194 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 195 | |
| 196 | return pp; |
| 197 | } |
| 198 | EXPORT_SYMBOL(of_find_property); |
| 199 | |
| 200 | /** |
| 201 | * of_find_all_nodes - Get next node in global list |
| 202 | * @prev: Previous node or NULL to start iteration |
| 203 | * of_node_put() will be called on it |
| 204 | * |
| 205 | * Returns a node pointer with refcount incremented, use |
| 206 | * of_node_put() on it when done. |
| 207 | */ |
| 208 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 209 | { |
| 210 | struct device_node *np; |
| 211 | |
| 212 | raw_spin_lock(&devtree_lock); |
| 213 | np = prev ? prev->allnext : allnodes; |
| 214 | for (; np != NULL; np = np->allnext) |
| 215 | if (of_node_get(np)) |
| 216 | break; |
| 217 | of_node_put(prev); |
| 218 | raw_spin_unlock(&devtree_lock); |
| 219 | return np; |
| 220 | } |
| 221 | EXPORT_SYMBOL(of_find_all_nodes); |
| 222 | |
| 223 | /* |
| 224 | * Find a property with a given name for a given node |
| 225 | * and return the value. |
| 226 | */ |
| 227 | static const void *__of_get_property(const struct device_node *np, |
| 228 | const char *name, int *lenp) |
| 229 | { |
| 230 | struct property *pp = __of_find_property(np, name, lenp); |
| 231 | |
| 232 | return pp ? pp->value : NULL; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Find a property with a given name for a given node |
| 237 | * and return the value. |
| 238 | */ |
| 239 | const void *of_get_property(const struct device_node *np, const char *name, |
| 240 | int *lenp) |
| 241 | { |
| 242 | struct property *pp = of_find_property(np, name, lenp); |
| 243 | |
| 244 | return pp ? pp->value : NULL; |
| 245 | } |
| 246 | EXPORT_SYMBOL(of_get_property); |
| 247 | |
| 248 | /** Checks if the given "compat" string matches one of the strings in |
| 249 | * the device's "compatible" property |
| 250 | */ |
| 251 | static int __of_device_is_compatible(const struct device_node *device, |
| 252 | const char *compat) |
| 253 | { |
| 254 | const char* cp; |
| 255 | int uninitialized_var(cplen), l; |
| 256 | |
| 257 | cp = __of_get_property(device, "compatible", &cplen); |
| 258 | if (cp == NULL) |
| 259 | return 0; |
| 260 | while (cplen > 0) { |
| 261 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) |
| 262 | return 1; |
| 263 | l = strlen(cp) + 1; |
| 264 | cp += l; |
| 265 | cplen -= l; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /** Checks if the given "compat" string matches one of the strings in |
| 272 | * the device's "compatible" property |
| 273 | */ |
| 274 | int of_device_is_compatible(const struct device_node *device, |
| 275 | const char *compat) |
| 276 | { |
| 277 | unsigned long flags; |
| 278 | int res; |
| 279 | |
| 280 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 281 | res = __of_device_is_compatible(device, compat); |
| 282 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 283 | return res; |
| 284 | } |
| 285 | EXPORT_SYMBOL(of_device_is_compatible); |
| 286 | |
| 287 | /** |
| 288 | * of_machine_is_compatible - Test root of device tree for a given compatible value |
| 289 | * @compat: compatible string to look for in root node's compatible property. |
| 290 | * |
| 291 | * Returns true if the root node has the given value in its |
| 292 | * compatible property. |
| 293 | */ |
| 294 | int of_machine_is_compatible(const char *compat) |
| 295 | { |
| 296 | struct device_node *root; |
| 297 | int rc = 0; |
| 298 | |
| 299 | root = of_find_node_by_path("/"); |
| 300 | if (root) { |
| 301 | rc = of_device_is_compatible(root, compat); |
| 302 | of_node_put(root); |
| 303 | } |
| 304 | return rc; |
| 305 | } |
| 306 | EXPORT_SYMBOL(of_machine_is_compatible); |
| 307 | |
| 308 | /** |
| 309 | * of_device_is_available - check if a device is available for use |
| 310 | * |
| 311 | * @device: Node to check for availability |
| 312 | * |
| 313 | * Returns 1 if the status property is absent or set to "okay" or "ok", |
| 314 | * 0 otherwise |
| 315 | */ |
| 316 | int of_device_is_available(const struct device_node *device) |
| 317 | { |
| 318 | const char *status; |
| 319 | int statlen; |
| 320 | |
| 321 | status = of_get_property(device, "status", &statlen); |
| 322 | if (status == NULL) |
| 323 | return 1; |
| 324 | |
| 325 | if (statlen > 0) { |
| 326 | if (!strcmp(status, "okay") || !strcmp(status, "ok")) |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | EXPORT_SYMBOL(of_device_is_available); |
| 333 | |
| 334 | /** |
| 335 | * of_get_parent - Get a node's parent if any |
| 336 | * @node: Node to get parent |
| 337 | * |
| 338 | * Returns a node pointer with refcount incremented, use |
| 339 | * of_node_put() on it when done. |
| 340 | */ |
| 341 | struct device_node *of_get_parent(const struct device_node *node) |
| 342 | { |
| 343 | struct device_node *np; |
| 344 | unsigned long flags; |
| 345 | |
| 346 | if (!node) |
| 347 | return NULL; |
| 348 | |
| 349 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 350 | np = of_node_get(node->parent); |
| 351 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 352 | return np; |
| 353 | } |
| 354 | EXPORT_SYMBOL(of_get_parent); |
| 355 | |
| 356 | /** |
| 357 | * of_get_next_parent - Iterate to a node's parent |
| 358 | * @node: Node to get parent of |
| 359 | * |
| 360 | * This is like of_get_parent() except that it drops the |
| 361 | * refcount on the passed node, making it suitable for iterating |
| 362 | * through a node's parents. |
| 363 | * |
| 364 | * Returns a node pointer with refcount incremented, use |
| 365 | * of_node_put() on it when done. |
| 366 | */ |
| 367 | struct device_node *of_get_next_parent(struct device_node *node) |
| 368 | { |
| 369 | struct device_node *parent; |
| 370 | unsigned long flags; |
| 371 | |
| 372 | if (!node) |
| 373 | return NULL; |
| 374 | |
| 375 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 376 | parent = of_node_get(node->parent); |
| 377 | of_node_put(node); |
| 378 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 379 | return parent; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * of_get_next_child - Iterate a node childs |
| 384 | * @node: parent node |
| 385 | * @prev: previous child of the parent node, or NULL to get first |
| 386 | * |
| 387 | * Returns a node pointer with refcount incremented, use |
| 388 | * of_node_put() on it when done. |
| 389 | */ |
| 390 | struct device_node *of_get_next_child(const struct device_node *node, |
| 391 | struct device_node *prev) |
| 392 | { |
| 393 | struct device_node *next; |
| 394 | unsigned long flags; |
| 395 | |
| 396 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 397 | next = prev ? prev->sibling : node->child; |
| 398 | for (; next; next = next->sibling) |
| 399 | if (of_node_get(next)) |
| 400 | break; |
| 401 | of_node_put(prev); |
| 402 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 403 | return next; |
| 404 | } |
| 405 | EXPORT_SYMBOL(of_get_next_child); |
| 406 | |
| 407 | /** |
| 408 | * of_find_node_by_path - Find a node matching a full OF path |
| 409 | * @path: The full path to match |
| 410 | * |
| 411 | * Returns a node pointer with refcount incremented, use |
| 412 | * of_node_put() on it when done. |
| 413 | */ |
| 414 | struct device_node *of_find_node_by_path(const char *path) |
| 415 | { |
| 416 | struct device_node *np = allnodes; |
| 417 | unsigned long flags; |
| 418 | |
| 419 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 420 | for (; np; np = np->allnext) { |
| 421 | if (np->full_name && (of_node_cmp(np->full_name, path) == 0) |
| 422 | && of_node_get(np)) |
| 423 | break; |
| 424 | } |
| 425 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 426 | return np; |
| 427 | } |
| 428 | EXPORT_SYMBOL(of_find_node_by_path); |
| 429 | |
| 430 | /** |
| 431 | * of_find_node_by_name - Find a node by its "name" property |
| 432 | * @from: The node to start searching from or NULL, the node |
| 433 | * you pass will not be searched, only the next one |
| 434 | * will; typically, you pass what the previous call |
| 435 | * returned. of_node_put() will be called on it |
| 436 | * @name: The name string to match against |
| 437 | * |
| 438 | * Returns a node pointer with refcount incremented, use |
| 439 | * of_node_put() on it when done. |
| 440 | */ |
| 441 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 442 | const char *name) |
| 443 | { |
| 444 | struct device_node *np; |
| 445 | unsigned long flags; |
| 446 | |
| 447 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 448 | np = from ? from->allnext : allnodes; |
| 449 | for (; np; np = np->allnext) |
| 450 | if (np->name && (of_node_cmp(np->name, name) == 0) |
| 451 | && of_node_get(np)) |
| 452 | break; |
| 453 | of_node_put(from); |
| 454 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 455 | return np; |
| 456 | } |
| 457 | EXPORT_SYMBOL(of_find_node_by_name); |
| 458 | |
| 459 | /** |
| 460 | * of_find_node_by_type - Find a node by its "device_type" property |
| 461 | * @from: The node to start searching from, or NULL to start searching |
| 462 | * the entire device tree. The node you pass will not be |
| 463 | * searched, only the next one will; typically, you pass |
| 464 | * what the previous call returned. of_node_put() will be |
| 465 | * called on from for you. |
| 466 | * @type: The type string to match against |
| 467 | * |
| 468 | * Returns a node pointer with refcount incremented, use |
| 469 | * of_node_put() on it when done. |
| 470 | */ |
| 471 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 472 | const char *type) |
| 473 | { |
| 474 | struct device_node *np; |
| 475 | unsigned long flags; |
| 476 | |
| 477 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 478 | np = from ? from->allnext : allnodes; |
| 479 | for (; np; np = np->allnext) |
| 480 | if (np->type && (of_node_cmp(np->type, type) == 0) |
| 481 | && of_node_get(np)) |
| 482 | break; |
| 483 | of_node_put(from); |
| 484 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 485 | return np; |
| 486 | } |
| 487 | EXPORT_SYMBOL(of_find_node_by_type); |
| 488 | |
| 489 | /** |
| 490 | * of_find_compatible_node - Find a node based on type and one of the |
| 491 | * tokens in its "compatible" property |
| 492 | * @from: The node to start searching from or NULL, the node |
| 493 | * you pass will not be searched, only the next one |
| 494 | * will; typically, you pass what the previous call |
| 495 | * returned. of_node_put() will be called on it |
| 496 | * @type: The type string to match "device_type" or NULL to ignore |
| 497 | * @compatible: The string to match to one of the tokens in the device |
| 498 | * "compatible" list. |
| 499 | * |
| 500 | * Returns a node pointer with refcount incremented, use |
| 501 | * of_node_put() on it when done. |
| 502 | */ |
| 503 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 504 | const char *type, const char *compatible) |
| 505 | { |
| 506 | struct device_node *np; |
| 507 | unsigned long flags; |
| 508 | |
| 509 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 510 | np = from ? from->allnext : allnodes; |
| 511 | for (; np; np = np->allnext) { |
| 512 | if (type |
| 513 | && !(np->type && (of_node_cmp(np->type, type) == 0))) |
| 514 | continue; |
| 515 | if (__of_device_is_compatible(np, compatible) && |
| 516 | of_node_get(np)) |
| 517 | break; |
| 518 | } |
| 519 | of_node_put(from); |
| 520 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 521 | return np; |
| 522 | } |
| 523 | EXPORT_SYMBOL(of_find_compatible_node); |
| 524 | |
| 525 | /** |
| 526 | * of_find_node_with_property - Find a node which has a property with |
| 527 | * the given name. |
| 528 | * @from: The node to start searching from or NULL, the node |
| 529 | * you pass will not be searched, only the next one |
| 530 | * will; typically, you pass what the previous call |
| 531 | * returned. of_node_put() will be called on it |
| 532 | * @prop_name: The name of the property to look for. |
| 533 | * |
| 534 | * Returns a node pointer with refcount incremented, use |
| 535 | * of_node_put() on it when done. |
| 536 | */ |
| 537 | struct device_node *of_find_node_with_property(struct device_node *from, |
| 538 | const char *prop_name) |
| 539 | { |
| 540 | struct device_node *np; |
| 541 | struct property *pp; |
| 542 | unsigned long flags; |
| 543 | |
| 544 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 545 | np = from ? from->allnext : allnodes; |
| 546 | for (; np; np = np->allnext) { |
| 547 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 548 | if (of_prop_cmp(pp->name, prop_name) == 0) { |
| 549 | of_node_get(np); |
| 550 | goto out; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | out: |
| 555 | of_node_put(from); |
| 556 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 557 | return np; |
| 558 | } |
| 559 | EXPORT_SYMBOL(of_find_node_with_property); |
| 560 | |
| 561 | static |
| 562 | const struct of_device_id *__of_match_node(const struct of_device_id *matches, |
| 563 | const struct device_node *node) |
| 564 | { |
| 565 | if (!matches) |
| 566 | return NULL; |
| 567 | |
| 568 | while (matches->name[0] || matches->type[0] || matches->compatible[0]) { |
| 569 | int match = 1; |
| 570 | if (matches->name[0]) |
| 571 | match &= node->name |
| 572 | && !strcmp(matches->name, node->name); |
| 573 | if (matches->type[0]) |
| 574 | match &= node->type |
| 575 | && !strcmp(matches->type, node->type); |
| 576 | if (matches->compatible[0]) |
| 577 | match &= __of_device_is_compatible(node, |
| 578 | matches->compatible); |
| 579 | if (match) |
| 580 | return matches; |
| 581 | matches++; |
| 582 | } |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * of_match_node - Tell if an device_node has a matching of_match structure |
| 588 | * @matches: array of of device match structures to search in |
| 589 | * @node: the of device structure to match against |
| 590 | * |
| 591 | * Low level utility function used by device matching. |
| 592 | */ |
| 593 | const struct of_device_id *of_match_node(const struct of_device_id *matches, |
| 594 | const struct device_node *node) |
| 595 | { |
| 596 | const struct of_device_id *match; |
| 597 | unsigned long flags; |
| 598 | |
| 599 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 600 | match = __of_match_node(matches, node); |
| 601 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 602 | return match; |
| 603 | } |
| 604 | EXPORT_SYMBOL(of_match_node); |
| 605 | |
| 606 | /** |
| 607 | * of_find_matching_node - Find a node based on an of_device_id match |
| 608 | * table. |
| 609 | * @from: The node to start searching from or NULL, the node |
| 610 | * you pass will not be searched, only the next one |
| 611 | * will; typically, you pass what the previous call |
| 612 | * returned. of_node_put() will be called on it |
| 613 | * @matches: array of of device match structures to search in |
| 614 | * |
| 615 | * Returns a node pointer with refcount incremented, use |
| 616 | * of_node_put() on it when done. |
| 617 | */ |
| 618 | struct device_node *of_find_matching_node(struct device_node *from, |
| 619 | const struct of_device_id *matches) |
| 620 | { |
| 621 | struct device_node *np; |
| 622 | unsigned long flags; |
| 623 | |
| 624 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 625 | np = from ? from->allnext : allnodes; |
| 626 | for (; np; np = np->allnext) { |
| 627 | if (__of_match_node(matches, np) && of_node_get(np)) |
| 628 | break; |
| 629 | } |
| 630 | of_node_put(from); |
| 631 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 632 | return np; |
| 633 | } |
| 634 | EXPORT_SYMBOL(of_find_matching_node); |
| 635 | |
| 636 | /** |
| 637 | * of_modalias_node - Lookup appropriate modalias for a device node |
| 638 | * @node: pointer to a device tree node |
| 639 | * @modalias: Pointer to buffer that modalias value will be copied into |
| 640 | * @len: Length of modalias value |
| 641 | * |
| 642 | * Based on the value of the compatible property, this routine will attempt |
| 643 | * to choose an appropriate modalias value for a particular device tree node. |
| 644 | * It does this by stripping the manufacturer prefix (as delimited by a ',') |
| 645 | * from the first entry in the compatible list property. |
| 646 | * |
| 647 | * This routine returns 0 on success, <0 on failure. |
| 648 | */ |
| 649 | int of_modalias_node(struct device_node *node, char *modalias, int len) |
| 650 | { |
| 651 | const char *compatible, *p; |
| 652 | int cplen; |
| 653 | |
| 654 | compatible = of_get_property(node, "compatible", &cplen); |
| 655 | if (!compatible || strlen(compatible) > cplen) |
| 656 | return -ENODEV; |
| 657 | p = strchr(compatible, ','); |
| 658 | strlcpy(modalias, p ? p + 1 : compatible, len); |
| 659 | return 0; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 662 | |
| 663 | /** |
| 664 | * of_find_node_by_phandle - Find a node given a phandle |
| 665 | * @handle: phandle of the node to find |
| 666 | * |
| 667 | * Returns a node pointer with refcount incremented, use |
| 668 | * of_node_put() on it when done. |
| 669 | */ |
| 670 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 671 | { |
| 672 | struct device_node *np; |
| 673 | |
| 674 | raw_spin_lock(&devtree_lock); |
| 675 | for (np = allnodes; np; np = np->allnext) |
| 676 | if (np->phandle == handle) |
| 677 | break; |
| 678 | of_node_get(np); |
| 679 | raw_spin_unlock(&devtree_lock); |
| 680 | return np; |
| 681 | } |
| 682 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 683 | |
| 684 | /** |
| 685 | * of_property_read_u32_array - Find and read an array of 32 bit integers |
| 686 | * from a property. |
| 687 | * |
| 688 | * @np: device node from which the property value is to be read. |
| 689 | * @propname: name of the property to be searched. |
| 690 | * @out_value: pointer to return value, modified only if return value is 0. |
| 691 | * |
| 692 | * Search for a property in a device node and read 32-bit value(s) from |
| 693 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 694 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 695 | * property data isn't large enough. |
| 696 | * |
| 697 | * The out_value is modified only if a valid u32 value can be decoded. |
| 698 | */ |
| 699 | int of_property_read_u32_array(const struct device_node *np, |
| 700 | const char *propname, u32 *out_values, |
| 701 | size_t sz) |
| 702 | { |
| 703 | struct property *prop = of_find_property(np, propname, NULL); |
| 704 | const __be32 *val; |
| 705 | |
| 706 | if (!prop) |
| 707 | return -EINVAL; |
| 708 | if (!prop->value) |
| 709 | return -ENODATA; |
| 710 | if ((sz * sizeof(*out_values)) > prop->length) |
| 711 | return -EOVERFLOW; |
| 712 | |
| 713 | val = prop->value; |
| 714 | while (sz--) |
| 715 | *out_values++ = be32_to_cpup(val++); |
| 716 | return 0; |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(of_property_read_u32_array); |
| 719 | |
| 720 | /** |
| 721 | * of_property_read_u64 - Find and read a 64 bit integer from a property |
| 722 | * @np: device node from which the property value is to be read. |
| 723 | * @propname: name of the property to be searched. |
| 724 | * @out_value: pointer to return value, modified only if return value is 0. |
| 725 | * |
| 726 | * Search for a property in a device node and read a 64-bit value from |
| 727 | * it. Returns 0 on success, -EINVAL if the property does not exist, |
| 728 | * -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 729 | * property data isn't large enough. |
| 730 | * |
| 731 | * The out_value is modified only if a valid u64 value can be decoded. |
| 732 | */ |
| 733 | int of_property_read_u64(const struct device_node *np, const char *propname, |
| 734 | u64 *out_value) |
| 735 | { |
| 736 | struct property *prop = of_find_property(np, propname, NULL); |
| 737 | |
| 738 | if (!prop) |
| 739 | return -EINVAL; |
| 740 | if (!prop->value) |
| 741 | return -ENODATA; |
| 742 | if (sizeof(*out_value) > prop->length) |
| 743 | return -EOVERFLOW; |
| 744 | *out_value = of_read_number(prop->value, 2); |
| 745 | return 0; |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(of_property_read_u64); |
| 748 | |
| 749 | /** |
| 750 | * of_property_read_string - Find and read a string from a property |
| 751 | * @np: device node from which the property value is to be read. |
| 752 | * @propname: name of the property to be searched. |
| 753 | * @out_string: pointer to null terminated return string, modified only if |
| 754 | * return value is 0. |
| 755 | * |
| 756 | * Search for a property in a device tree node and retrieve a null |
| 757 | * terminated string value (pointer to data, not a copy). Returns 0 on |
| 758 | * success, -EINVAL if the property does not exist, -ENODATA if property |
| 759 | * does not have a value, and -EILSEQ if the string is not null-terminated |
| 760 | * within the length of the property data. |
| 761 | * |
| 762 | * The out_string pointer is modified only if a valid string can be decoded. |
| 763 | */ |
| 764 | int of_property_read_string(struct device_node *np, const char *propname, |
| 765 | const char **out_string) |
| 766 | { |
| 767 | struct property *prop = of_find_property(np, propname, NULL); |
| 768 | if (!prop) |
| 769 | return -EINVAL; |
| 770 | if (!prop->value) |
| 771 | return -ENODATA; |
| 772 | if (strnlen(prop->value, prop->length) >= prop->length) |
| 773 | return -EILSEQ; |
| 774 | *out_string = prop->value; |
| 775 | return 0; |
| 776 | } |
| 777 | EXPORT_SYMBOL_GPL(of_property_read_string); |
| 778 | |
| 779 | /** |
| 780 | * of_property_match_string() - Find string in a list and return index |
| 781 | * @np: pointer to node containing string list property |
| 782 | * @propname: string list property name |
| 783 | * @string: pointer to string to search for in string list |
| 784 | * |
| 785 | * This function searches a string list property and returns the index |
| 786 | * of a specific string value. |
| 787 | */ |
| 788 | int of_property_match_string(struct device_node *np, const char *propname, |
| 789 | const char *string) |
| 790 | { |
| 791 | struct property *prop = of_find_property(np, propname, NULL); |
| 792 | size_t l; |
| 793 | int i; |
| 794 | const char *p, *end; |
| 795 | |
| 796 | if (!prop) |
| 797 | return -EINVAL; |
| 798 | if (!prop->value) |
| 799 | return -ENODATA; |
| 800 | |
| 801 | p = prop->value; |
| 802 | end = p + prop->length; |
| 803 | |
| 804 | for (i = 0; p < end; i++, p += l) { |
| 805 | l = strnlen(p, end - p) + 1; |
| 806 | if (p + l > end) |
| 807 | return -EILSEQ; |
| 808 | pr_debug("comparing %s with %s\n", string, p); |
| 809 | if (strcmp(string, p) == 0) |
| 810 | return i; /* Found it; return index */ |
| 811 | } |
| 812 | return -ENODATA; |
| 813 | } |
| 814 | EXPORT_SYMBOL_GPL(of_property_match_string); |
| 815 | |
| 816 | /** |
| 817 | * of_property_read_string_util() - Utility helper for parsing string properties |
| 818 | * @np: device node from which the property value is to be read. |
| 819 | * @propname: name of the property to be searched. |
| 820 | * @out_strs: output array of string pointers. |
| 821 | * @sz: number of array elements to read. |
| 822 | * @skip: Number of strings to skip over at beginning of list. |
| 823 | * |
| 824 | * Don't call this function directly. It is a utility helper for the |
| 825 | * of_property_read_string*() family of functions. |
| 826 | */ |
| 827 | int of_property_read_string_helper(struct device_node *np, const char *propname, |
| 828 | const char **out_strs, size_t sz, int skip) |
| 829 | { |
| 830 | struct property *prop = of_find_property(np, propname, NULL); |
| 831 | int l = 0, i = 0; |
| 832 | const char *p, *end; |
| 833 | |
| 834 | if (!prop) |
| 835 | return -EINVAL; |
| 836 | if (!prop->value) |
| 837 | return -ENODATA; |
| 838 | p = prop->value; |
| 839 | end = p + prop->length; |
| 840 | |
| 841 | for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { |
| 842 | l = strnlen(p, end - p) + 1; |
| 843 | if (p + l > end) |
| 844 | return -EILSEQ; |
| 845 | if (out_strs && i >= skip) |
| 846 | *out_strs++ = p; |
| 847 | } |
| 848 | i -= skip; |
| 849 | return i <= 0 ? -ENODATA : i; |
| 850 | } |
| 851 | EXPORT_SYMBOL_GPL(of_property_read_string_helper); |
| 852 | |
| 853 | /** |
| 854 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 855 | * @np: Pointer to device node holding phandle property |
| 856 | * @phandle_name: Name of property holding a phandle value |
| 857 | * @index: For properties holding a table of phandles, this is the index into |
| 858 | * the table |
| 859 | * |
| 860 | * Returns the device_node pointer with refcount incremented. Use |
| 861 | * of_node_put() on it when done. |
| 862 | */ |
| 863 | struct device_node * |
| 864 | of_parse_phandle(struct device_node *np, const char *phandle_name, int index) |
| 865 | { |
| 866 | const __be32 *phandle; |
| 867 | int size; |
| 868 | |
| 869 | phandle = of_get_property(np, phandle_name, &size); |
| 870 | if ((!phandle) || (size < sizeof(*phandle) * (index + 1))) |
| 871 | return NULL; |
| 872 | |
| 873 | return of_find_node_by_phandle(be32_to_cpup(phandle + index)); |
| 874 | } |
| 875 | EXPORT_SYMBOL(of_parse_phandle); |
| 876 | |
| 877 | /** |
| 878 | * of_parse_phandle_with_args() - Find a node pointed by phandle in a list |
| 879 | * @np: pointer to a device tree node containing a list |
| 880 | * @list_name: property name that contains a list |
| 881 | * @cells_name: property name that specifies phandles' arguments count |
| 882 | * @index: index of a phandle to parse out |
| 883 | * @out_args: optional pointer to output arguments structure (will be filled) |
| 884 | * |
| 885 | * This function is useful to parse lists of phandles and their arguments. |
| 886 | * Returns 0 on success and fills out_args, on error returns appropriate |
| 887 | * errno value. |
| 888 | * |
| 889 | * Caller is responsible to call of_node_put() on the returned out_args->node |
| 890 | * pointer. |
| 891 | * |
| 892 | * Example: |
| 893 | * |
| 894 | * phandle1: node1 { |
| 895 | * #list-cells = <2>; |
| 896 | * } |
| 897 | * |
| 898 | * phandle2: node2 { |
| 899 | * #list-cells = <1>; |
| 900 | * } |
| 901 | * |
| 902 | * node3 { |
| 903 | * list = <&phandle1 1 2 &phandle2 3>; |
| 904 | * } |
| 905 | * |
| 906 | * To get a device_node of the `node2' node you may call this: |
| 907 | * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args); |
| 908 | */ |
| 909 | int of_parse_phandle_with_args(struct device_node *np, const char *list_name, |
| 910 | const char *cells_name, int index, |
| 911 | struct of_phandle_args *out_args) |
| 912 | { |
| 913 | const __be32 *list, *list_end; |
| 914 | int size, cur_index = 0; |
| 915 | uint32_t count = 0; |
| 916 | struct device_node *node = NULL; |
| 917 | phandle phandle; |
| 918 | |
| 919 | /* Retrieve the phandle list property */ |
| 920 | list = of_get_property(np, list_name, &size); |
| 921 | if (!list) |
| 922 | return -EINVAL; |
| 923 | list_end = list + size / sizeof(*list); |
| 924 | |
| 925 | /* Loop over the phandles until all the requested entry is found */ |
| 926 | while (list < list_end) { |
| 927 | count = 0; |
| 928 | |
| 929 | /* |
| 930 | * If phandle is 0, then it is an empty entry with no |
| 931 | * arguments. Skip forward to the next entry. |
| 932 | */ |
| 933 | phandle = be32_to_cpup(list++); |
| 934 | if (phandle) { |
| 935 | /* |
| 936 | * Find the provider node and parse the #*-cells |
| 937 | * property to determine the argument length |
| 938 | */ |
| 939 | node = of_find_node_by_phandle(phandle); |
| 940 | if (!node) { |
| 941 | pr_err("%s: could not find phandle\n", |
| 942 | np->full_name); |
| 943 | break; |
| 944 | } |
| 945 | if (of_property_read_u32(node, cells_name, &count)) { |
| 946 | pr_err("%s: could not get %s for %s\n", |
| 947 | np->full_name, cells_name, |
| 948 | node->full_name); |
| 949 | break; |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * Make sure that the arguments actually fit in the |
| 954 | * remaining property data length |
| 955 | */ |
| 956 | if (list + count > list_end) { |
| 957 | pr_err("%s: arguments longer than property\n", |
| 958 | np->full_name); |
| 959 | break; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * All of the error cases above bail out of the loop, so at |
| 965 | * this point, the parsing is successful. If the requested |
| 966 | * index matches, then fill the out_args structure and return, |
| 967 | * or return -ENOENT for an empty entry. |
| 968 | */ |
| 969 | if (cur_index == index) { |
| 970 | if (!phandle) |
| 971 | return -ENOENT; |
| 972 | |
| 973 | if (out_args) { |
| 974 | int i; |
| 975 | if (WARN_ON(count > MAX_PHANDLE_ARGS)) |
| 976 | count = MAX_PHANDLE_ARGS; |
| 977 | out_args->np = node; |
| 978 | out_args->args_count = count; |
| 979 | for (i = 0; i < count; i++) |
| 980 | out_args->args[i] = be32_to_cpup(list++); |
| 981 | } |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | of_node_put(node); |
| 986 | node = NULL; |
| 987 | list += count; |
| 988 | cur_index++; |
| 989 | } |
| 990 | |
| 991 | /* Loop exited without finding a valid entry; return an error */ |
| 992 | if (node) |
| 993 | of_node_put(node); |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | EXPORT_SYMBOL(of_parse_phandle_with_args); |
| 997 | |
| 998 | /** |
| 999 | * prom_add_property - Add a property to a node |
| 1000 | */ |
| 1001 | int prom_add_property(struct device_node *np, struct property *prop) |
| 1002 | { |
| 1003 | struct property **next; |
| 1004 | unsigned long flags; |
| 1005 | |
| 1006 | prop->next = NULL; |
| 1007 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 1008 | next = &np->properties; |
| 1009 | while (*next) { |
| 1010 | if (strcmp(prop->name, (*next)->name) == 0) { |
| 1011 | /* duplicate ! don't insert it */ |
| 1012 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1013 | return -1; |
| 1014 | } |
| 1015 | next = &(*next)->next; |
| 1016 | } |
| 1017 | *next = prop; |
| 1018 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1019 | |
| 1020 | #ifdef CONFIG_PROC_DEVICETREE |
| 1021 | /* try to add to proc as well if it was initialized */ |
| 1022 | if (np->pde) |
| 1023 | proc_device_tree_add_prop(np->pde, prop); |
| 1024 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * prom_remove_property - Remove a property from a node. |
| 1031 | * |
| 1032 | * Note that we don't actually remove it, since we have given out |
| 1033 | * who-knows-how-many pointers to the data using get-property. |
| 1034 | * Instead we just move the property to the "dead properties" |
| 1035 | * list, so it won't be found any more. |
| 1036 | */ |
| 1037 | int prom_remove_property(struct device_node *np, struct property *prop) |
| 1038 | { |
| 1039 | struct property **next; |
| 1040 | unsigned long flags; |
| 1041 | int found = 0; |
| 1042 | |
| 1043 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 1044 | next = &np->properties; |
| 1045 | while (*next) { |
| 1046 | if (*next == prop) { |
| 1047 | /* found the node */ |
| 1048 | *next = prop->next; |
| 1049 | prop->next = np->deadprops; |
| 1050 | np->deadprops = prop; |
| 1051 | found = 1; |
| 1052 | break; |
| 1053 | } |
| 1054 | next = &(*next)->next; |
| 1055 | } |
| 1056 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1057 | |
| 1058 | if (!found) |
| 1059 | return -ENODEV; |
| 1060 | |
| 1061 | #ifdef CONFIG_PROC_DEVICETREE |
| 1062 | /* try to remove the proc node as well */ |
| 1063 | if (np->pde) |
| 1064 | proc_device_tree_remove_prop(np->pde, prop); |
| 1065 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * prom_update_property - Update a property in a node. |
| 1072 | * |
| 1073 | * Note that we don't actually remove it, since we have given out |
| 1074 | * who-knows-how-many pointers to the data using get-property. |
| 1075 | * Instead we just move the property to the "dead properties" list, |
| 1076 | * and add the new property to the property list |
| 1077 | */ |
| 1078 | int prom_update_property(struct device_node *np, |
| 1079 | struct property *newprop, |
| 1080 | struct property *oldprop) |
| 1081 | { |
| 1082 | struct property **next; |
| 1083 | unsigned long flags; |
| 1084 | int found = 0; |
| 1085 | |
| 1086 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 1087 | next = &np->properties; |
| 1088 | while (*next) { |
| 1089 | if (*next == oldprop) { |
| 1090 | /* found the node */ |
| 1091 | newprop->next = oldprop->next; |
| 1092 | *next = newprop; |
| 1093 | oldprop->next = np->deadprops; |
| 1094 | np->deadprops = oldprop; |
| 1095 | found = 1; |
| 1096 | break; |
| 1097 | } |
| 1098 | next = &(*next)->next; |
| 1099 | } |
| 1100 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1101 | |
| 1102 | if (!found) |
| 1103 | return -ENODEV; |
| 1104 | |
| 1105 | #ifdef CONFIG_PROC_DEVICETREE |
| 1106 | /* try to add to proc as well if it was initialized */ |
| 1107 | if (np->pde) |
| 1108 | proc_device_tree_update_prop(np->pde, newprop, oldprop); |
| 1109 | #endif /* CONFIG_PROC_DEVICETREE */ |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | #if defined(CONFIG_OF_DYNAMIC) |
| 1115 | /* |
| 1116 | * Support for dynamic device trees. |
| 1117 | * |
| 1118 | * On some platforms, the device tree can be manipulated at runtime. |
| 1119 | * The routines in this section support adding, removing and changing |
| 1120 | * device tree nodes. |
| 1121 | */ |
| 1122 | |
| 1123 | /** |
| 1124 | * of_attach_node - Plug a device node into the tree and global list. |
| 1125 | */ |
| 1126 | void of_attach_node(struct device_node *np) |
| 1127 | { |
| 1128 | unsigned long flags; |
| 1129 | |
| 1130 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 1131 | np->sibling = np->parent->child; |
| 1132 | np->allnext = allnodes; |
| 1133 | np->parent->child = np; |
| 1134 | allnodes = np; |
| 1135 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * of_detach_node - "Unplug" a node from the device tree. |
| 1140 | * |
| 1141 | * The caller must hold a reference to the node. The memory associated with |
| 1142 | * the node is not freed until its refcount goes to zero. |
| 1143 | */ |
| 1144 | void of_detach_node(struct device_node *np) |
| 1145 | { |
| 1146 | struct device_node *parent; |
| 1147 | unsigned long flags; |
| 1148 | |
| 1149 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 1150 | |
| 1151 | parent = np->parent; |
| 1152 | if (!parent) |
| 1153 | goto out_unlock; |
| 1154 | |
| 1155 | if (allnodes == np) |
| 1156 | allnodes = np->allnext; |
| 1157 | else { |
| 1158 | struct device_node *prev; |
| 1159 | for (prev = allnodes; |
| 1160 | prev->allnext != np; |
| 1161 | prev = prev->allnext) |
| 1162 | ; |
| 1163 | prev->allnext = np->allnext; |
| 1164 | } |
| 1165 | |
| 1166 | if (parent->child == np) |
| 1167 | parent->child = np->sibling; |
| 1168 | else { |
| 1169 | struct device_node *prevsib; |
| 1170 | for (prevsib = np->parent->child; |
| 1171 | prevsib->sibling != np; |
| 1172 | prevsib = prevsib->sibling) |
| 1173 | ; |
| 1174 | prevsib->sibling = np->sibling; |
| 1175 | } |
| 1176 | |
| 1177 | of_node_set_flag(np, OF_DETACHED); |
| 1178 | |
| 1179 | out_unlock: |
| 1180 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 1181 | } |
| 1182 | #endif /* defined(CONFIG_OF_DYNAMIC) */ |
| 1183 | |
| 1184 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 1185 | int id, const char *stem, int stem_len) |
| 1186 | { |
| 1187 | ap->np = np; |
| 1188 | ap->id = id; |
| 1189 | strncpy(ap->stem, stem, stem_len); |
| 1190 | ap->stem[stem_len] = 0; |
| 1191 | list_add_tail(&ap->link, &aliases_lookup); |
| 1192 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n", |
| 1193 | ap->alias, ap->stem, ap->id, np ? np->full_name : NULL); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * of_alias_scan - Scan all properties of 'aliases' node |
| 1198 | * |
| 1199 | * The function scans all the properties of 'aliases' node and populate |
| 1200 | * the the global lookup table with the properties. It returns the |
| 1201 | * number of alias_prop found, or error code in error case. |
| 1202 | * |
| 1203 | * @dt_alloc: An allocator that provides a virtual address to memory |
| 1204 | * for the resulting tree |
| 1205 | */ |
| 1206 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) |
| 1207 | { |
| 1208 | struct property *pp; |
| 1209 | |
| 1210 | of_chosen = of_find_node_by_path("/chosen"); |
| 1211 | if (of_chosen == NULL) |
| 1212 | of_chosen = of_find_node_by_path("/chosen@0"); |
| 1213 | of_aliases = of_find_node_by_path("/aliases"); |
| 1214 | if (!of_aliases) |
| 1215 | return; |
| 1216 | |
| 1217 | for_each_property_of_node(of_aliases, pp) { |
| 1218 | const char *start = pp->name; |
| 1219 | const char *end = start + strlen(start); |
| 1220 | struct device_node *np; |
| 1221 | struct alias_prop *ap; |
| 1222 | int id, len; |
| 1223 | |
| 1224 | /* Skip those we do not want to proceed */ |
| 1225 | if (!strcmp(pp->name, "name") || |
| 1226 | !strcmp(pp->name, "phandle") || |
| 1227 | !strcmp(pp->name, "linux,phandle")) |
| 1228 | continue; |
| 1229 | |
| 1230 | np = of_find_node_by_path(pp->value); |
| 1231 | if (!np) |
| 1232 | continue; |
| 1233 | |
| 1234 | /* walk the alias backwards to extract the id and work out |
| 1235 | * the 'stem' string */ |
| 1236 | while (isdigit(*(end-1)) && end > start) |
| 1237 | end--; |
| 1238 | len = end - start; |
| 1239 | |
| 1240 | if (kstrtoint(end, 10, &id) < 0) |
| 1241 | continue; |
| 1242 | |
| 1243 | /* Allocate an alias_prop with enough space for the stem */ |
| 1244 | ap = dt_alloc(sizeof(*ap) + len + 1, 4); |
| 1245 | if (!ap) |
| 1246 | continue; |
| 1247 | memset(ap, 0, sizeof(*ap) + len + 1); |
| 1248 | ap->alias = start; |
| 1249 | of_alias_add(ap, np, id, start, len); |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * of_alias_get_id - Get alias id for the given device_node |
| 1255 | * @np: Pointer to the given device_node |
| 1256 | * @stem: Alias stem of the given device_node |
| 1257 | * |
| 1258 | * The function travels the lookup table to get alias id for the given |
| 1259 | * device_node and alias stem. It returns the alias id if find it. |
| 1260 | */ |
| 1261 | int of_alias_get_id(struct device_node *np, const char *stem) |
| 1262 | { |
| 1263 | struct alias_prop *app; |
| 1264 | int id = -ENODEV; |
| 1265 | |
| 1266 | mutex_lock(&of_aliases_mutex); |
| 1267 | list_for_each_entry(app, &aliases_lookup, link) { |
| 1268 | if (strcmp(app->stem, stem) != 0) |
| 1269 | continue; |
| 1270 | |
| 1271 | if (np == app->np) { |
| 1272 | id = app->id; |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | mutex_unlock(&of_aliases_mutex); |
| 1277 | |
| 1278 | return id; |
| 1279 | } |
| 1280 | EXPORT_SYMBOL_GPL(of_alias_get_id); |