base.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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/module.h>
  21. #include <linux/of.h>
  22. #include <linux/spinlock.h>
  23. struct device_node *allnodes;
  24. /* use when traversing tree through the allnext, child, sibling,
  25. * or parent members of struct device_node.
  26. */
  27. DEFINE_RWLOCK(devtree_lock);
  28. int of_n_addr_cells(struct device_node *np)
  29. {
  30. const int *ip;
  31. do {
  32. if (np->parent)
  33. np = np->parent;
  34. ip = of_get_property(np, "#address-cells", NULL);
  35. if (ip)
  36. return *ip;
  37. } while (np->parent);
  38. /* No #address-cells property for the root node */
  39. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  40. }
  41. EXPORT_SYMBOL(of_n_addr_cells);
  42. int of_n_size_cells(struct device_node *np)
  43. {
  44. const int *ip;
  45. do {
  46. if (np->parent)
  47. np = np->parent;
  48. ip = of_get_property(np, "#size-cells", NULL);
  49. if (ip)
  50. return *ip;
  51. } while (np->parent);
  52. /* No #size-cells property for the root node */
  53. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  54. }
  55. EXPORT_SYMBOL(of_n_size_cells);
  56. #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
  57. /**
  58. * of_node_get - Increment refcount of a node
  59. * @node: Node to inc refcount, NULL is supported to
  60. * simplify writing of callers
  61. *
  62. * Returns node.
  63. */
  64. struct device_node *of_node_get(struct device_node *node)
  65. {
  66. if (node)
  67. kref_get(&node->kref);
  68. return node;
  69. }
  70. EXPORT_SYMBOL(of_node_get);
  71. static inline struct device_node *kref_to_device_node(struct kref *kref)
  72. {
  73. return container_of(kref, struct device_node, kref);
  74. }
  75. /**
  76. * of_node_release - release a dynamically allocated node
  77. * @kref: kref element of the node to be released
  78. *
  79. * In of_node_put() this function is passed to kref_put()
  80. * as the destructor.
  81. */
  82. static void of_node_release(struct kref *kref)
  83. {
  84. struct device_node *node = kref_to_device_node(kref);
  85. struct property *prop = node->properties;
  86. /* We should never be releasing nodes that haven't been detached. */
  87. if (!of_node_check_flag(node, OF_DETACHED)) {
  88. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  89. dump_stack();
  90. kref_init(&node->kref);
  91. return;
  92. }
  93. if (!of_node_check_flag(node, OF_DYNAMIC))
  94. return;
  95. while (prop) {
  96. struct property *next = prop->next;
  97. kfree(prop->name);
  98. kfree(prop->value);
  99. kfree(prop);
  100. prop = next;
  101. if (!prop) {
  102. prop = node->deadprops;
  103. node->deadprops = NULL;
  104. }
  105. }
  106. kfree(node->full_name);
  107. kfree(node->data);
  108. kfree(node);
  109. }
  110. /**
  111. * of_node_put - Decrement refcount of a node
  112. * @node: Node to dec refcount, NULL is supported to
  113. * simplify writing of callers
  114. *
  115. */
  116. void of_node_put(struct device_node *node)
  117. {
  118. if (node)
  119. kref_put(&node->kref, of_node_release);
  120. }
  121. EXPORT_SYMBOL(of_node_put);
  122. #endif /* !CONFIG_SPARC */
  123. struct property *of_find_property(const struct device_node *np,
  124. const char *name,
  125. int *lenp)
  126. {
  127. struct property *pp;
  128. if (!np)
  129. return NULL;
  130. read_lock(&devtree_lock);
  131. for (pp = np->properties; pp != 0; pp = pp->next) {
  132. if (of_prop_cmp(pp->name, name) == 0) {
  133. if (lenp != 0)
  134. *lenp = pp->length;
  135. break;
  136. }
  137. }
  138. read_unlock(&devtree_lock);
  139. return pp;
  140. }
  141. EXPORT_SYMBOL(of_find_property);
  142. /**
  143. * of_find_all_nodes - Get next node in global list
  144. * @prev: Previous node or NULL to start iteration
  145. * of_node_put() will be called on it
  146. *
  147. * Returns a node pointer with refcount incremented, use
  148. * of_node_put() on it when done.
  149. */
  150. struct device_node *of_find_all_nodes(struct device_node *prev)
  151. {
  152. struct device_node *np;
  153. read_lock(&devtree_lock);
  154. np = prev ? prev->allnext : allnodes;
  155. for (; np != NULL; np = np->allnext)
  156. if (of_node_get(np))
  157. break;
  158. of_node_put(prev);
  159. read_unlock(&devtree_lock);
  160. return np;
  161. }
  162. EXPORT_SYMBOL(of_find_all_nodes);
  163. /*
  164. * Find a property with a given name for a given node
  165. * and return the value.
  166. */
  167. const void *of_get_property(const struct device_node *np, const char *name,
  168. int *lenp)
  169. {
  170. struct property *pp = of_find_property(np, name, lenp);
  171. return pp ? pp->value : NULL;
  172. }
  173. EXPORT_SYMBOL(of_get_property);
  174. /** Checks if the given "compat" string matches one of the strings in
  175. * the device's "compatible" property
  176. */
  177. int of_device_is_compatible(const struct device_node *device,
  178. const char *compat)
  179. {
  180. const char* cp;
  181. int cplen, l;
  182. cp = of_get_property(device, "compatible", &cplen);
  183. if (cp == NULL)
  184. return 0;
  185. while (cplen > 0) {
  186. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  187. return 1;
  188. l = strlen(cp) + 1;
  189. cp += l;
  190. cplen -= l;
  191. }
  192. return 0;
  193. }
  194. EXPORT_SYMBOL(of_device_is_compatible);
  195. /**
  196. * of_machine_is_compatible - Test root of device tree for a given compatible value
  197. * @compat: compatible string to look for in root node's compatible property.
  198. *
  199. * Returns true if the root node has the given value in its
  200. * compatible property.
  201. */
  202. int of_machine_is_compatible(const char *compat)
  203. {
  204. struct device_node *root;
  205. int rc = 0;
  206. root = of_find_node_by_path("/");
  207. if (root) {
  208. rc = of_device_is_compatible(root, compat);
  209. of_node_put(root);
  210. }
  211. return rc;
  212. }
  213. EXPORT_SYMBOL(of_machine_is_compatible);
  214. /**
  215. * of_device_is_available - check if a device is available for use
  216. *
  217. * @device: Node to check for availability
  218. *
  219. * Returns 1 if the status property is absent or set to "okay" or "ok",
  220. * 0 otherwise
  221. */
  222. int of_device_is_available(const struct device_node *device)
  223. {
  224. const char *status;
  225. int statlen;
  226. status = of_get_property(device, "status", &statlen);
  227. if (status == NULL)
  228. return 1;
  229. if (statlen > 0) {
  230. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. EXPORT_SYMBOL(of_device_is_available);
  236. /**
  237. * of_get_parent - Get a node's parent if any
  238. * @node: Node to get parent
  239. *
  240. * Returns a node pointer with refcount incremented, use
  241. * of_node_put() on it when done.
  242. */
  243. struct device_node *of_get_parent(const struct device_node *node)
  244. {
  245. struct device_node *np;
  246. if (!node)
  247. return NULL;
  248. read_lock(&devtree_lock);
  249. np = of_node_get(node->parent);
  250. read_unlock(&devtree_lock);
  251. return np;
  252. }
  253. EXPORT_SYMBOL(of_get_parent);
  254. /**
  255. * of_get_next_parent - Iterate to a node's parent
  256. * @node: Node to get parent of
  257. *
  258. * This is like of_get_parent() except that it drops the
  259. * refcount on the passed node, making it suitable for iterating
  260. * through a node's parents.
  261. *
  262. * Returns a node pointer with refcount incremented, use
  263. * of_node_put() on it when done.
  264. */
  265. struct device_node *of_get_next_parent(struct device_node *node)
  266. {
  267. struct device_node *parent;
  268. if (!node)
  269. return NULL;
  270. read_lock(&devtree_lock);
  271. parent = of_node_get(node->parent);
  272. of_node_put(node);
  273. read_unlock(&devtree_lock);
  274. return parent;
  275. }
  276. /**
  277. * of_get_next_child - Iterate a node childs
  278. * @node: parent node
  279. * @prev: previous child of the parent node, or NULL to get first
  280. *
  281. * Returns a node pointer with refcount incremented, use
  282. * of_node_put() on it when done.
  283. */
  284. struct device_node *of_get_next_child(const struct device_node *node,
  285. struct device_node *prev)
  286. {
  287. struct device_node *next;
  288. read_lock(&devtree_lock);
  289. next = prev ? prev->sibling : node->child;
  290. for (; next; next = next->sibling)
  291. if (of_node_get(next))
  292. break;
  293. of_node_put(prev);
  294. read_unlock(&devtree_lock);
  295. return next;
  296. }
  297. EXPORT_SYMBOL(of_get_next_child);
  298. /**
  299. * of_find_node_by_path - Find a node matching a full OF path
  300. * @path: The full path to match
  301. *
  302. * Returns a node pointer with refcount incremented, use
  303. * of_node_put() on it when done.
  304. */
  305. struct device_node *of_find_node_by_path(const char *path)
  306. {
  307. struct device_node *np = allnodes;
  308. read_lock(&devtree_lock);
  309. for (; np; np = np->allnext) {
  310. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  311. && of_node_get(np))
  312. break;
  313. }
  314. read_unlock(&devtree_lock);
  315. return np;
  316. }
  317. EXPORT_SYMBOL(of_find_node_by_path);
  318. /**
  319. * of_find_node_by_name - Find a node by its "name" property
  320. * @from: The node to start searching from or NULL, the node
  321. * you pass will not be searched, only the next one
  322. * will; typically, you pass what the previous call
  323. * returned. of_node_put() will be called on it
  324. * @name: The name string to match against
  325. *
  326. * Returns a node pointer with refcount incremented, use
  327. * of_node_put() on it when done.
  328. */
  329. struct device_node *of_find_node_by_name(struct device_node *from,
  330. const char *name)
  331. {
  332. struct device_node *np;
  333. read_lock(&devtree_lock);
  334. np = from ? from->allnext : allnodes;
  335. for (; np; np = np->allnext)
  336. if (np->name && (of_node_cmp(np->name, name) == 0)
  337. && of_node_get(np))
  338. break;
  339. of_node_put(from);
  340. read_unlock(&devtree_lock);
  341. return np;
  342. }
  343. EXPORT_SYMBOL(of_find_node_by_name);
  344. /**
  345. * of_find_node_by_type - Find a node by its "device_type" property
  346. * @from: The node to start searching from, or NULL to start searching
  347. * the entire device tree. The node you pass will not be
  348. * searched, only the next one will; typically, you pass
  349. * what the previous call returned. of_node_put() will be
  350. * called on from for you.
  351. * @type: The type string to match against
  352. *
  353. * Returns a node pointer with refcount incremented, use
  354. * of_node_put() on it when done.
  355. */
  356. struct device_node *of_find_node_by_type(struct device_node *from,
  357. const char *type)
  358. {
  359. struct device_node *np;
  360. read_lock(&devtree_lock);
  361. np = from ? from->allnext : allnodes;
  362. for (; np; np = np->allnext)
  363. if (np->type && (of_node_cmp(np->type, type) == 0)
  364. && of_node_get(np))
  365. break;
  366. of_node_put(from);
  367. read_unlock(&devtree_lock);
  368. return np;
  369. }
  370. EXPORT_SYMBOL(of_find_node_by_type);
  371. /**
  372. * of_find_compatible_node - Find a node based on type and one of the
  373. * tokens in its "compatible" property
  374. * @from: The node to start searching from or NULL, the node
  375. * you pass will not be searched, only the next one
  376. * will; typically, you pass what the previous call
  377. * returned. of_node_put() will be called on it
  378. * @type: The type string to match "device_type" or NULL to ignore
  379. * @compatible: The string to match to one of the tokens in the device
  380. * "compatible" list.
  381. *
  382. * Returns a node pointer with refcount incremented, use
  383. * of_node_put() on it when done.
  384. */
  385. struct device_node *of_find_compatible_node(struct device_node *from,
  386. const char *type, const char *compatible)
  387. {
  388. struct device_node *np;
  389. read_lock(&devtree_lock);
  390. np = from ? from->allnext : allnodes;
  391. for (; np; np = np->allnext) {
  392. if (type
  393. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  394. continue;
  395. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  396. break;
  397. }
  398. of_node_put(from);
  399. read_unlock(&devtree_lock);
  400. return np;
  401. }
  402. EXPORT_SYMBOL(of_find_compatible_node);
  403. /**
  404. * of_find_node_with_property - Find a node which has a property with
  405. * the given name.
  406. * @from: The node to start searching from or NULL, the node
  407. * you pass will not be searched, only the next one
  408. * will; typically, you pass what the previous call
  409. * returned. of_node_put() will be called on it
  410. * @prop_name: The name of the property to look for.
  411. *
  412. * Returns a node pointer with refcount incremented, use
  413. * of_node_put() on it when done.
  414. */
  415. struct device_node *of_find_node_with_property(struct device_node *from,
  416. const char *prop_name)
  417. {
  418. struct device_node *np;
  419. struct property *pp;
  420. read_lock(&devtree_lock);
  421. np = from ? from->allnext : allnodes;
  422. for (; np; np = np->allnext) {
  423. for (pp = np->properties; pp != 0; pp = pp->next) {
  424. if (of_prop_cmp(pp->name, prop_name) == 0) {
  425. of_node_get(np);
  426. goto out;
  427. }
  428. }
  429. }
  430. out:
  431. of_node_put(from);
  432. read_unlock(&devtree_lock);
  433. return np;
  434. }
  435. EXPORT_SYMBOL(of_find_node_with_property);
  436. /**
  437. * of_match_node - Tell if an device_node has a matching of_match structure
  438. * @matches: array of of device match structures to search in
  439. * @node: the of device structure to match against
  440. *
  441. * Low level utility function used by device matching.
  442. */
  443. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  444. const struct device_node *node)
  445. {
  446. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  447. int match = 1;
  448. if (matches->name[0])
  449. match &= node->name
  450. && !strcmp(matches->name, node->name);
  451. if (matches->type[0])
  452. match &= node->type
  453. && !strcmp(matches->type, node->type);
  454. if (matches->compatible[0])
  455. match &= of_device_is_compatible(node,
  456. matches->compatible);
  457. if (match)
  458. return matches;
  459. matches++;
  460. }
  461. return NULL;
  462. }
  463. EXPORT_SYMBOL(of_match_node);
  464. /**
  465. * of_find_matching_node - Find a node based on an of_device_id match
  466. * table.
  467. * @from: The node to start searching from or NULL, the node
  468. * you pass will not be searched, only the next one
  469. * will; typically, you pass what the previous call
  470. * returned. of_node_put() will be called on it
  471. * @matches: array of of device match structures to search in
  472. *
  473. * Returns a node pointer with refcount incremented, use
  474. * of_node_put() on it when done.
  475. */
  476. struct device_node *of_find_matching_node(struct device_node *from,
  477. const struct of_device_id *matches)
  478. {
  479. struct device_node *np;
  480. read_lock(&devtree_lock);
  481. np = from ? from->allnext : allnodes;
  482. for (; np; np = np->allnext) {
  483. if (of_match_node(matches, np) && of_node_get(np))
  484. break;
  485. }
  486. of_node_put(from);
  487. read_unlock(&devtree_lock);
  488. return np;
  489. }
  490. EXPORT_SYMBOL(of_find_matching_node);
  491. /**
  492. * of_modalias_table: Table of explicit compatible ==> modalias mappings
  493. *
  494. * This table allows particulare compatible property values to be mapped
  495. * to modalias strings. This is useful for busses which do not directly
  496. * understand the OF device tree but are populated based on data contained
  497. * within the device tree. SPI and I2C are the two current users of this
  498. * table.
  499. *
  500. * In most cases, devices do not need to be listed in this table because
  501. * the modalias value can be derived directly from the compatible table.
  502. * However, if for any reason a value cannot be derived, then this table
  503. * provides a method to override the implicit derivation.
  504. *
  505. * At the moment, a single table is used for all bus types because it is
  506. * assumed that the data size is small and that the compatible values
  507. * should already be distinct enough to differentiate between SPI, I2C
  508. * and other devices.
  509. */
  510. struct of_modalias_table {
  511. char *of_device;
  512. char *modalias;
  513. };
  514. static struct of_modalias_table of_modalias_table[] = {
  515. { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
  516. { "mmc-spi-slot", "mmc_spi" },
  517. };
  518. /**
  519. * of_modalias_node - Lookup appropriate modalias for a device node
  520. * @node: pointer to a device tree node
  521. * @modalias: Pointer to buffer that modalias value will be copied into
  522. * @len: Length of modalias value
  523. *
  524. * Based on the value of the compatible property, this routine will determine
  525. * an appropriate modalias value for a particular device tree node. Two
  526. * separate methods are attempted to derive a modalias value.
  527. *
  528. * First method is to lookup the compatible value in of_modalias_table.
  529. * Second is to strip off the manufacturer prefix from the first
  530. * compatible entry and use the remainder as modalias
  531. *
  532. * This routine returns 0 on success
  533. */
  534. int of_modalias_node(struct device_node *node, char *modalias, int len)
  535. {
  536. int i, cplen;
  537. const char *compatible;
  538. const char *p;
  539. /* 1. search for exception list entry */
  540. for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
  541. compatible = of_modalias_table[i].of_device;
  542. if (!of_device_is_compatible(node, compatible))
  543. continue;
  544. strlcpy(modalias, of_modalias_table[i].modalias, len);
  545. return 0;
  546. }
  547. compatible = of_get_property(node, "compatible", &cplen);
  548. if (!compatible)
  549. return -ENODEV;
  550. /* 2. take first compatible entry and strip manufacturer */
  551. p = strchr(compatible, ',');
  552. if (!p)
  553. return -ENODEV;
  554. p++;
  555. strlcpy(modalias, p, len);
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(of_modalias_node);
  559. /**
  560. * of_find_node_by_phandle - Find a node given a phandle
  561. * @handle: phandle of the node to find
  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_phandle(phandle handle)
  567. {
  568. struct device_node *np;
  569. read_lock(&devtree_lock);
  570. for (np = allnodes; np; np = np->allnext)
  571. if (np->phandle == handle)
  572. break;
  573. of_node_get(np);
  574. read_unlock(&devtree_lock);
  575. return np;
  576. }
  577. EXPORT_SYMBOL(of_find_node_by_phandle);
  578. /**
  579. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  580. * @np: Pointer to device node holding phandle property
  581. * @phandle_name: Name of property holding a phandle value
  582. * @index: For properties holding a table of phandles, this is the index into
  583. * the table
  584. *
  585. * Returns the device_node pointer with refcount incremented. Use
  586. * of_node_put() on it when done.
  587. */
  588. struct device_node *
  589. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  590. {
  591. const phandle *phandle;
  592. int size;
  593. phandle = of_get_property(np, phandle_name, &size);
  594. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  595. return NULL;
  596. return of_find_node_by_phandle(phandle[index]);
  597. }
  598. EXPORT_SYMBOL(of_parse_phandle);
  599. /**
  600. * of_parse_phandles_with_args - Find a node pointed by phandle in a list
  601. * @np: pointer to a device tree node containing a list
  602. * @list_name: property name that contains a list
  603. * @cells_name: property name that specifies phandles' arguments count
  604. * @index: index of a phandle to parse out
  605. * @out_node: optional pointer to device_node struct pointer (will be filled)
  606. * @out_args: optional pointer to arguments pointer (will be filled)
  607. *
  608. * This function is useful to parse lists of phandles and their arguments.
  609. * Returns 0 on success and fills out_node and out_args, on error returns
  610. * appropriate errno value.
  611. *
  612. * Example:
  613. *
  614. * phandle1: node1 {
  615. * #list-cells = <2>;
  616. * }
  617. *
  618. * phandle2: node2 {
  619. * #list-cells = <1>;
  620. * }
  621. *
  622. * node3 {
  623. * list = <&phandle1 1 2 &phandle2 3>;
  624. * }
  625. *
  626. * To get a device_node of the `node2' node you may call this:
  627. * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
  628. */
  629. int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
  630. const char *cells_name, int index,
  631. struct device_node **out_node,
  632. const void **out_args)
  633. {
  634. int ret = -EINVAL;
  635. const u32 *list;
  636. const u32 *list_end;
  637. int size;
  638. int cur_index = 0;
  639. struct device_node *node = NULL;
  640. const void *args = NULL;
  641. list = of_get_property(np, list_name, &size);
  642. if (!list) {
  643. ret = -ENOENT;
  644. goto err0;
  645. }
  646. list_end = list + size / sizeof(*list);
  647. while (list < list_end) {
  648. const u32 *cells;
  649. const phandle *phandle;
  650. phandle = list++;
  651. args = list;
  652. /* one cell hole in the list = <>; */
  653. if (!*phandle)
  654. goto next;
  655. node = of_find_node_by_phandle(*phandle);
  656. if (!node) {
  657. pr_debug("%s: could not find phandle\n",
  658. np->full_name);
  659. goto err0;
  660. }
  661. cells = of_get_property(node, cells_name, &size);
  662. if (!cells || size != sizeof(*cells)) {
  663. pr_debug("%s: could not get %s for %s\n",
  664. np->full_name, cells_name, node->full_name);
  665. goto err1;
  666. }
  667. list += *cells;
  668. if (list > list_end) {
  669. pr_debug("%s: insufficient arguments length\n",
  670. np->full_name);
  671. goto err1;
  672. }
  673. next:
  674. if (cur_index == index)
  675. break;
  676. of_node_put(node);
  677. node = NULL;
  678. args = NULL;
  679. cur_index++;
  680. }
  681. if (!node) {
  682. /*
  683. * args w/o node indicates that the loop above has stopped at
  684. * the 'hole' cell. Report this differently.
  685. */
  686. if (args)
  687. ret = -EEXIST;
  688. else
  689. ret = -ENOENT;
  690. goto err0;
  691. }
  692. if (out_node)
  693. *out_node = node;
  694. if (out_args)
  695. *out_args = args;
  696. return 0;
  697. err1:
  698. of_node_put(node);
  699. err0:
  700. pr_debug("%s failed with status %d\n", __func__, ret);
  701. return ret;
  702. }
  703. EXPORT_SYMBOL(of_parse_phandles_with_args);
  704. /**
  705. * prom_add_property - Add a property to a node
  706. */
  707. int prom_add_property(struct device_node *np, struct property *prop)
  708. {
  709. struct property **next;
  710. unsigned long flags;
  711. prop->next = NULL;
  712. write_lock_irqsave(&devtree_lock, flags);
  713. next = &np->properties;
  714. while (*next) {
  715. if (strcmp(prop->name, (*next)->name) == 0) {
  716. /* duplicate ! don't insert it */
  717. write_unlock_irqrestore(&devtree_lock, flags);
  718. return -1;
  719. }
  720. next = &(*next)->next;
  721. }
  722. *next = prop;
  723. write_unlock_irqrestore(&devtree_lock, flags);
  724. #ifdef CONFIG_PROC_DEVICETREE
  725. /* try to add to proc as well if it was initialized */
  726. if (np->pde)
  727. proc_device_tree_add_prop(np->pde, prop);
  728. #endif /* CONFIG_PROC_DEVICETREE */
  729. return 0;
  730. }
  731. /**
  732. * prom_remove_property - Remove a property from a node.
  733. *
  734. * Note that we don't actually remove it, since we have given out
  735. * who-knows-how-many pointers to the data using get-property.
  736. * Instead we just move the property to the "dead properties"
  737. * list, so it won't be found any more.
  738. */
  739. int prom_remove_property(struct device_node *np, struct property *prop)
  740. {
  741. struct property **next;
  742. unsigned long flags;
  743. int found = 0;
  744. write_lock_irqsave(&devtree_lock, flags);
  745. next = &np->properties;
  746. while (*next) {
  747. if (*next == prop) {
  748. /* found the node */
  749. *next = prop->next;
  750. prop->next = np->deadprops;
  751. np->deadprops = prop;
  752. found = 1;
  753. break;
  754. }
  755. next = &(*next)->next;
  756. }
  757. write_unlock_irqrestore(&devtree_lock, flags);
  758. if (!found)
  759. return -ENODEV;
  760. #ifdef CONFIG_PROC_DEVICETREE
  761. /* try to remove the proc node as well */
  762. if (np->pde)
  763. proc_device_tree_remove_prop(np->pde, prop);
  764. #endif /* CONFIG_PROC_DEVICETREE */
  765. return 0;
  766. }
  767. /*
  768. * prom_update_property - Update a property in a node.
  769. *
  770. * Note that we don't actually remove it, since we have given out
  771. * who-knows-how-many pointers to the data using get-property.
  772. * Instead we just move the property to the "dead properties" list,
  773. * and add the new property to the property list
  774. */
  775. int prom_update_property(struct device_node *np,
  776. struct property *newprop,
  777. struct property *oldprop)
  778. {
  779. struct property **next;
  780. unsigned long flags;
  781. int found = 0;
  782. write_lock_irqsave(&devtree_lock, flags);
  783. next = &np->properties;
  784. while (*next) {
  785. if (*next == oldprop) {
  786. /* found the node */
  787. newprop->next = oldprop->next;
  788. *next = newprop;
  789. oldprop->next = np->deadprops;
  790. np->deadprops = oldprop;
  791. found = 1;
  792. break;
  793. }
  794. next = &(*next)->next;
  795. }
  796. write_unlock_irqrestore(&devtree_lock, flags);
  797. if (!found)
  798. return -ENODEV;
  799. #ifdef CONFIG_PROC_DEVICETREE
  800. /* try to add to proc as well if it was initialized */
  801. if (np->pde)
  802. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  803. #endif /* CONFIG_PROC_DEVICETREE */
  804. return 0;
  805. }
  806. #if defined(CONFIG_OF_DYNAMIC)
  807. /*
  808. * Support for dynamic device trees.
  809. *
  810. * On some platforms, the device tree can be manipulated at runtime.
  811. * The routines in this section support adding, removing and changing
  812. * device tree nodes.
  813. */
  814. /**
  815. * of_attach_node - Plug a device node into the tree and global list.
  816. */
  817. void of_attach_node(struct device_node *np)
  818. {
  819. unsigned long flags;
  820. write_lock_irqsave(&devtree_lock, flags);
  821. np->sibling = np->parent->child;
  822. np->allnext = allnodes;
  823. np->parent->child = np;
  824. allnodes = np;
  825. write_unlock_irqrestore(&devtree_lock, flags);
  826. }
  827. /**
  828. * of_detach_node - "Unplug" a node from the device tree.
  829. *
  830. * The caller must hold a reference to the node. The memory associated with
  831. * the node is not freed until its refcount goes to zero.
  832. */
  833. void of_detach_node(struct device_node *np)
  834. {
  835. struct device_node *parent;
  836. unsigned long flags;
  837. write_lock_irqsave(&devtree_lock, flags);
  838. parent = np->parent;
  839. if (!parent)
  840. goto out_unlock;
  841. if (allnodes == np)
  842. allnodes = np->allnext;
  843. else {
  844. struct device_node *prev;
  845. for (prev = allnodes;
  846. prev->allnext != np;
  847. prev = prev->allnext)
  848. ;
  849. prev->allnext = np->allnext;
  850. }
  851. if (parent->child == np)
  852. parent->child = np->sibling;
  853. else {
  854. struct device_node *prevsib;
  855. for (prevsib = np->parent->child;
  856. prevsib->sibling != np;
  857. prevsib = prevsib->sibling)
  858. ;
  859. prevsib->sibling = np->sibling;
  860. }
  861. of_node_set_flag(np, OF_DETACHED);
  862. out_unlock:
  863. write_unlock_irqrestore(&devtree_lock, flags);
  864. }
  865. #endif /* defined(CONFIG_OF_DYNAMIC) */