base.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 __be32 *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 __be32 *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. if (!matches)
  450. return NULL;
  451. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  452. int match = 1;
  453. if (matches->name[0])
  454. match &= node->name
  455. && !strcmp(matches->name, node->name);
  456. if (matches->type[0])
  457. match &= node->type
  458. && !strcmp(matches->type, node->type);
  459. if (matches->compatible[0])
  460. match &= of_device_is_compatible(node,
  461. matches->compatible);
  462. if (match)
  463. return matches;
  464. matches++;
  465. }
  466. return NULL;
  467. }
  468. EXPORT_SYMBOL(of_match_node);
  469. /**
  470. * of_find_matching_node - Find a node based on an of_device_id match
  471. * table.
  472. * @from: The node to start searching from or NULL, the node
  473. * you pass will not be searched, only the next one
  474. * will; typically, you pass what the previous call
  475. * returned. of_node_put() will be called on it
  476. * @matches: array of of device match structures to search in
  477. *
  478. * Returns a node pointer with refcount incremented, use
  479. * of_node_put() on it when done.
  480. */
  481. struct device_node *of_find_matching_node(struct device_node *from,
  482. const struct of_device_id *matches)
  483. {
  484. struct device_node *np;
  485. read_lock(&devtree_lock);
  486. np = from ? from->allnext : allnodes;
  487. for (; np; np = np->allnext) {
  488. if (of_match_node(matches, np) && of_node_get(np))
  489. break;
  490. }
  491. of_node_put(from);
  492. read_unlock(&devtree_lock);
  493. return np;
  494. }
  495. EXPORT_SYMBOL(of_find_matching_node);
  496. /**
  497. * of_modalias_node - Lookup appropriate modalias for a device node
  498. * @node: pointer to a device tree node
  499. * @modalias: Pointer to buffer that modalias value will be copied into
  500. * @len: Length of modalias value
  501. *
  502. * Based on the value of the compatible property, this routine will attempt
  503. * to choose an appropriate modalias value for a particular device tree node.
  504. * It does this by stripping the manufacturer prefix (as delimited by a ',')
  505. * from the first entry in the compatible list property.
  506. *
  507. * This routine returns 0 on success, <0 on failure.
  508. */
  509. int of_modalias_node(struct device_node *node, char *modalias, int len)
  510. {
  511. const char *compatible, *p;
  512. int cplen;
  513. compatible = of_get_property(node, "compatible", &cplen);
  514. if (!compatible || strlen(compatible) > cplen)
  515. return -ENODEV;
  516. p = strchr(compatible, ',');
  517. strlcpy(modalias, p ? p + 1 : compatible, len);
  518. return 0;
  519. }
  520. EXPORT_SYMBOL_GPL(of_modalias_node);
  521. /**
  522. * of_find_node_by_phandle - Find a node given a phandle
  523. * @handle: phandle of the node to find
  524. *
  525. * Returns a node pointer with refcount incremented, use
  526. * of_node_put() on it when done.
  527. */
  528. struct device_node *of_find_node_by_phandle(phandle handle)
  529. {
  530. struct device_node *np;
  531. read_lock(&devtree_lock);
  532. for (np = allnodes; np; np = np->allnext)
  533. if (np->phandle == handle)
  534. break;
  535. of_node_get(np);
  536. read_unlock(&devtree_lock);
  537. return np;
  538. }
  539. EXPORT_SYMBOL(of_find_node_by_phandle);
  540. /**
  541. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  542. * @np: Pointer to device node holding phandle property
  543. * @phandle_name: Name of property holding a phandle value
  544. * @index: For properties holding a table of phandles, this is the index into
  545. * the table
  546. *
  547. * Returns the device_node pointer with refcount incremented. Use
  548. * of_node_put() on it when done.
  549. */
  550. struct device_node *
  551. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  552. {
  553. const __be32 *phandle;
  554. int size;
  555. phandle = of_get_property(np, phandle_name, &size);
  556. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  557. return NULL;
  558. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  559. }
  560. EXPORT_SYMBOL(of_parse_phandle);
  561. /**
  562. * of_parse_phandles_with_args - Find a node pointed by phandle in a list
  563. * @np: pointer to a device tree node containing a list
  564. * @list_name: property name that contains a list
  565. * @cells_name: property name that specifies phandles' arguments count
  566. * @index: index of a phandle to parse out
  567. * @out_node: optional pointer to device_node struct pointer (will be filled)
  568. * @out_args: optional pointer to arguments pointer (will be filled)
  569. *
  570. * This function is useful to parse lists of phandles and their arguments.
  571. * Returns 0 on success and fills out_node and out_args, on error returns
  572. * appropriate errno value.
  573. *
  574. * Example:
  575. *
  576. * phandle1: node1 {
  577. * #list-cells = <2>;
  578. * }
  579. *
  580. * phandle2: node2 {
  581. * #list-cells = <1>;
  582. * }
  583. *
  584. * node3 {
  585. * list = <&phandle1 1 2 &phandle2 3>;
  586. * }
  587. *
  588. * To get a device_node of the `node2' node you may call this:
  589. * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
  590. */
  591. int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
  592. const char *cells_name, int index,
  593. struct device_node **out_node,
  594. const void **out_args)
  595. {
  596. int ret = -EINVAL;
  597. const __be32 *list;
  598. const __be32 *list_end;
  599. int size;
  600. int cur_index = 0;
  601. struct device_node *node = NULL;
  602. const void *args = NULL;
  603. list = of_get_property(np, list_name, &size);
  604. if (!list) {
  605. ret = -ENOENT;
  606. goto err0;
  607. }
  608. list_end = list + size / sizeof(*list);
  609. while (list < list_end) {
  610. const __be32 *cells;
  611. phandle phandle;
  612. phandle = be32_to_cpup(list++);
  613. args = list;
  614. /* one cell hole in the list = <>; */
  615. if (!phandle)
  616. goto next;
  617. node = of_find_node_by_phandle(phandle);
  618. if (!node) {
  619. pr_debug("%s: could not find phandle\n",
  620. np->full_name);
  621. goto err0;
  622. }
  623. cells = of_get_property(node, cells_name, &size);
  624. if (!cells || size != sizeof(*cells)) {
  625. pr_debug("%s: could not get %s for %s\n",
  626. np->full_name, cells_name, node->full_name);
  627. goto err1;
  628. }
  629. list += be32_to_cpup(cells);
  630. if (list > list_end) {
  631. pr_debug("%s: insufficient arguments length\n",
  632. np->full_name);
  633. goto err1;
  634. }
  635. next:
  636. if (cur_index == index)
  637. break;
  638. of_node_put(node);
  639. node = NULL;
  640. args = NULL;
  641. cur_index++;
  642. }
  643. if (!node) {
  644. /*
  645. * args w/o node indicates that the loop above has stopped at
  646. * the 'hole' cell. Report this differently.
  647. */
  648. if (args)
  649. ret = -EEXIST;
  650. else
  651. ret = -ENOENT;
  652. goto err0;
  653. }
  654. if (out_node)
  655. *out_node = node;
  656. if (out_args)
  657. *out_args = args;
  658. return 0;
  659. err1:
  660. of_node_put(node);
  661. err0:
  662. pr_debug("%s failed with status %d\n", __func__, ret);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(of_parse_phandles_with_args);
  666. /**
  667. * prom_add_property - Add a property to a node
  668. */
  669. int prom_add_property(struct device_node *np, struct property *prop)
  670. {
  671. struct property **next;
  672. unsigned long flags;
  673. prop->next = NULL;
  674. write_lock_irqsave(&devtree_lock, flags);
  675. next = &np->properties;
  676. while (*next) {
  677. if (strcmp(prop->name, (*next)->name) == 0) {
  678. /* duplicate ! don't insert it */
  679. write_unlock_irqrestore(&devtree_lock, flags);
  680. return -1;
  681. }
  682. next = &(*next)->next;
  683. }
  684. *next = prop;
  685. write_unlock_irqrestore(&devtree_lock, flags);
  686. #ifdef CONFIG_PROC_DEVICETREE
  687. /* try to add to proc as well if it was initialized */
  688. if (np->pde)
  689. proc_device_tree_add_prop(np->pde, prop);
  690. #endif /* CONFIG_PROC_DEVICETREE */
  691. return 0;
  692. }
  693. /**
  694. * prom_remove_property - Remove a property from a node.
  695. *
  696. * Note that we don't actually remove it, since we have given out
  697. * who-knows-how-many pointers to the data using get-property.
  698. * Instead we just move the property to the "dead properties"
  699. * list, so it won't be found any more.
  700. */
  701. int prom_remove_property(struct device_node *np, struct property *prop)
  702. {
  703. struct property **next;
  704. unsigned long flags;
  705. int found = 0;
  706. write_lock_irqsave(&devtree_lock, flags);
  707. next = &np->properties;
  708. while (*next) {
  709. if (*next == prop) {
  710. /* found the node */
  711. *next = prop->next;
  712. prop->next = np->deadprops;
  713. np->deadprops = prop;
  714. found = 1;
  715. break;
  716. }
  717. next = &(*next)->next;
  718. }
  719. write_unlock_irqrestore(&devtree_lock, flags);
  720. if (!found)
  721. return -ENODEV;
  722. #ifdef CONFIG_PROC_DEVICETREE
  723. /* try to remove the proc node as well */
  724. if (np->pde)
  725. proc_device_tree_remove_prop(np->pde, prop);
  726. #endif /* CONFIG_PROC_DEVICETREE */
  727. return 0;
  728. }
  729. /*
  730. * prom_update_property - Update a property in a node.
  731. *
  732. * Note that we don't actually remove it, since we have given out
  733. * who-knows-how-many pointers to the data using get-property.
  734. * Instead we just move the property to the "dead properties" list,
  735. * and add the new property to the property list
  736. */
  737. int prom_update_property(struct device_node *np,
  738. struct property *newprop,
  739. struct property *oldprop)
  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 == oldprop) {
  748. /* found the node */
  749. newprop->next = oldprop->next;
  750. *next = newprop;
  751. oldprop->next = np->deadprops;
  752. np->deadprops = oldprop;
  753. found = 1;
  754. break;
  755. }
  756. next = &(*next)->next;
  757. }
  758. write_unlock_irqrestore(&devtree_lock, flags);
  759. if (!found)
  760. return -ENODEV;
  761. #ifdef CONFIG_PROC_DEVICETREE
  762. /* try to add to proc as well if it was initialized */
  763. if (np->pde)
  764. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  765. #endif /* CONFIG_PROC_DEVICETREE */
  766. return 0;
  767. }
  768. #if defined(CONFIG_OF_DYNAMIC)
  769. /*
  770. * Support for dynamic device trees.
  771. *
  772. * On some platforms, the device tree can be manipulated at runtime.
  773. * The routines in this section support adding, removing and changing
  774. * device tree nodes.
  775. */
  776. /**
  777. * of_attach_node - Plug a device node into the tree and global list.
  778. */
  779. void of_attach_node(struct device_node *np)
  780. {
  781. unsigned long flags;
  782. write_lock_irqsave(&devtree_lock, flags);
  783. np->sibling = np->parent->child;
  784. np->allnext = allnodes;
  785. np->parent->child = np;
  786. allnodes = np;
  787. write_unlock_irqrestore(&devtree_lock, flags);
  788. }
  789. /**
  790. * of_detach_node - "Unplug" a node from the device tree.
  791. *
  792. * The caller must hold a reference to the node. The memory associated with
  793. * the node is not freed until its refcount goes to zero.
  794. */
  795. void of_detach_node(struct device_node *np)
  796. {
  797. struct device_node *parent;
  798. unsigned long flags;
  799. write_lock_irqsave(&devtree_lock, flags);
  800. parent = np->parent;
  801. if (!parent)
  802. goto out_unlock;
  803. if (allnodes == np)
  804. allnodes = np->allnext;
  805. else {
  806. struct device_node *prev;
  807. for (prev = allnodes;
  808. prev->allnext != np;
  809. prev = prev->allnext)
  810. ;
  811. prev->allnext = np->allnext;
  812. }
  813. if (parent->child == np)
  814. parent->child = np->sibling;
  815. else {
  816. struct device_node *prevsib;
  817. for (prevsib = np->parent->child;
  818. prevsib->sibling != np;
  819. prevsib = prevsib->sibling)
  820. ;
  821. prevsib->sibling = np->sibling;
  822. }
  823. of_node_set_flag(np, OF_DETACHED);
  824. out_unlock:
  825. write_unlock_irqrestore(&devtree_lock, flags);
  826. }
  827. #endif /* defined(CONFIG_OF_DYNAMIC) */