base.c 24 KB

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