base.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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_property_read_u32 - Find and read a 32 bit integer from a property
  542. * @np: device node from which the property value is to be read.
  543. * @propname: name of the property to be searched.
  544. * @out_value: pointer to return value, modified only if return value is 0.
  545. *
  546. * Search for a property in a device node and read a 32-bit value from
  547. * it. Returns 0 on success, -EINVAL if the property does not exist,
  548. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  549. * property data isn't large enough.
  550. *
  551. * The out_value is modified only if a valid u32 value can be decoded.
  552. */
  553. int of_property_read_u32(struct device_node *np, char *propname, u32 *out_value)
  554. {
  555. struct property *prop = of_find_property(np, propname, NULL);
  556. if (!prop)
  557. return -EINVAL;
  558. if (!prop->value)
  559. return -ENODATA;
  560. if (sizeof(*out_value) > prop->length)
  561. return -EOVERFLOW;
  562. *out_value = be32_to_cpup(prop->value);
  563. return 0;
  564. }
  565. EXPORT_SYMBOL_GPL(of_property_read_u32);
  566. /**
  567. * of_property_read_string - Find and read a string from a property
  568. * @np: device node from which the property value is to be read.
  569. * @propname: name of the property to be searched.
  570. * @out_string: pointer to null terminated return string, modified only if
  571. * return value is 0.
  572. *
  573. * Search for a property in a device tree node and retrieve a null
  574. * terminated string value (pointer to data, not a copy). Returns 0 on
  575. * success, -EINVAL if the property does not exist, -ENODATA if property
  576. * does not have a value, and -EILSEQ if the string is not null-terminated
  577. * within the length of the property data.
  578. *
  579. * The out_string pointer is modified only if a valid string can be decoded.
  580. */
  581. int of_property_read_string(struct device_node *np, char *propname,
  582. char **out_string)
  583. {
  584. struct property *prop = of_find_property(np, propname, NULL);
  585. if (!prop)
  586. return -EINVAL;
  587. if (!prop->value)
  588. return -ENODATA;
  589. if (strnlen(prop->value, prop->length) >= prop->length)
  590. return -EILSEQ;
  591. *out_string = prop->value;
  592. return 0;
  593. }
  594. EXPORT_SYMBOL_GPL(of_property_read_string);
  595. /**
  596. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  597. * @np: Pointer to device node holding phandle property
  598. * @phandle_name: Name of property holding a phandle value
  599. * @index: For properties holding a table of phandles, this is the index into
  600. * the table
  601. *
  602. * Returns the device_node pointer with refcount incremented. Use
  603. * of_node_put() on it when done.
  604. */
  605. struct device_node *
  606. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  607. {
  608. const __be32 *phandle;
  609. int size;
  610. phandle = of_get_property(np, phandle_name, &size);
  611. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  612. return NULL;
  613. return of_find_node_by_phandle(be32_to_cpup(phandle + index));
  614. }
  615. EXPORT_SYMBOL(of_parse_phandle);
  616. /**
  617. * of_parse_phandles_with_args - Find a node pointed by phandle in a list
  618. * @np: pointer to a device tree node containing a list
  619. * @list_name: property name that contains a list
  620. * @cells_name: property name that specifies phandles' arguments count
  621. * @index: index of a phandle to parse out
  622. * @out_node: optional pointer to device_node struct pointer (will be filled)
  623. * @out_args: optional pointer to arguments pointer (will be filled)
  624. *
  625. * This function is useful to parse lists of phandles and their arguments.
  626. * Returns 0 on success and fills out_node and out_args, on error returns
  627. * appropriate errno value.
  628. *
  629. * Example:
  630. *
  631. * phandle1: node1 {
  632. * #list-cells = <2>;
  633. * }
  634. *
  635. * phandle2: node2 {
  636. * #list-cells = <1>;
  637. * }
  638. *
  639. * node3 {
  640. * list = <&phandle1 1 2 &phandle2 3>;
  641. * }
  642. *
  643. * To get a device_node of the `node2' node you may call this:
  644. * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
  645. */
  646. int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
  647. const char *cells_name, int index,
  648. struct device_node **out_node,
  649. const void **out_args)
  650. {
  651. int ret = -EINVAL;
  652. const __be32 *list;
  653. const __be32 *list_end;
  654. int size;
  655. int cur_index = 0;
  656. struct device_node *node = NULL;
  657. const void *args = NULL;
  658. list = of_get_property(np, list_name, &size);
  659. if (!list) {
  660. ret = -ENOENT;
  661. goto err0;
  662. }
  663. list_end = list + size / sizeof(*list);
  664. while (list < list_end) {
  665. const __be32 *cells;
  666. phandle phandle;
  667. phandle = be32_to_cpup(list++);
  668. args = list;
  669. /* one cell hole in the list = <>; */
  670. if (!phandle)
  671. goto next;
  672. node = of_find_node_by_phandle(phandle);
  673. if (!node) {
  674. pr_debug("%s: could not find phandle\n",
  675. np->full_name);
  676. goto err0;
  677. }
  678. cells = of_get_property(node, cells_name, &size);
  679. if (!cells || size != sizeof(*cells)) {
  680. pr_debug("%s: could not get %s for %s\n",
  681. np->full_name, cells_name, node->full_name);
  682. goto err1;
  683. }
  684. list += be32_to_cpup(cells);
  685. if (list > list_end) {
  686. pr_debug("%s: insufficient arguments length\n",
  687. np->full_name);
  688. goto err1;
  689. }
  690. next:
  691. if (cur_index == index)
  692. break;
  693. of_node_put(node);
  694. node = NULL;
  695. args = NULL;
  696. cur_index++;
  697. }
  698. if (!node) {
  699. /*
  700. * args w/o node indicates that the loop above has stopped at
  701. * the 'hole' cell. Report this differently.
  702. */
  703. if (args)
  704. ret = -EEXIST;
  705. else
  706. ret = -ENOENT;
  707. goto err0;
  708. }
  709. if (out_node)
  710. *out_node = node;
  711. if (out_args)
  712. *out_args = args;
  713. return 0;
  714. err1:
  715. of_node_put(node);
  716. err0:
  717. pr_debug("%s failed with status %d\n", __func__, ret);
  718. return ret;
  719. }
  720. EXPORT_SYMBOL(of_parse_phandles_with_args);
  721. /**
  722. * prom_add_property - Add a property to a node
  723. */
  724. int prom_add_property(struct device_node *np, struct property *prop)
  725. {
  726. struct property **next;
  727. unsigned long flags;
  728. prop->next = NULL;
  729. write_lock_irqsave(&devtree_lock, flags);
  730. next = &np->properties;
  731. while (*next) {
  732. if (strcmp(prop->name, (*next)->name) == 0) {
  733. /* duplicate ! don't insert it */
  734. write_unlock_irqrestore(&devtree_lock, flags);
  735. return -1;
  736. }
  737. next = &(*next)->next;
  738. }
  739. *next = prop;
  740. write_unlock_irqrestore(&devtree_lock, flags);
  741. #ifdef CONFIG_PROC_DEVICETREE
  742. /* try to add to proc as well if it was initialized */
  743. if (np->pde)
  744. proc_device_tree_add_prop(np->pde, prop);
  745. #endif /* CONFIG_PROC_DEVICETREE */
  746. return 0;
  747. }
  748. /**
  749. * prom_remove_property - Remove a property from a node.
  750. *
  751. * Note that we don't actually remove it, since we have given out
  752. * who-knows-how-many pointers to the data using get-property.
  753. * Instead we just move the property to the "dead properties"
  754. * list, so it won't be found any more.
  755. */
  756. int prom_remove_property(struct device_node *np, struct property *prop)
  757. {
  758. struct property **next;
  759. unsigned long flags;
  760. int found = 0;
  761. write_lock_irqsave(&devtree_lock, flags);
  762. next = &np->properties;
  763. while (*next) {
  764. if (*next == prop) {
  765. /* found the node */
  766. *next = prop->next;
  767. prop->next = np->deadprops;
  768. np->deadprops = prop;
  769. found = 1;
  770. break;
  771. }
  772. next = &(*next)->next;
  773. }
  774. write_unlock_irqrestore(&devtree_lock, flags);
  775. if (!found)
  776. return -ENODEV;
  777. #ifdef CONFIG_PROC_DEVICETREE
  778. /* try to remove the proc node as well */
  779. if (np->pde)
  780. proc_device_tree_remove_prop(np->pde, prop);
  781. #endif /* CONFIG_PROC_DEVICETREE */
  782. return 0;
  783. }
  784. /*
  785. * prom_update_property - Update a property in a node.
  786. *
  787. * Note that we don't actually remove it, since we have given out
  788. * who-knows-how-many pointers to the data using get-property.
  789. * Instead we just move the property to the "dead properties" list,
  790. * and add the new property to the property list
  791. */
  792. int prom_update_property(struct device_node *np,
  793. struct property *newprop,
  794. struct property *oldprop)
  795. {
  796. struct property **next;
  797. unsigned long flags;
  798. int found = 0;
  799. write_lock_irqsave(&devtree_lock, flags);
  800. next = &np->properties;
  801. while (*next) {
  802. if (*next == oldprop) {
  803. /* found the node */
  804. newprop->next = oldprop->next;
  805. *next = newprop;
  806. oldprop->next = np->deadprops;
  807. np->deadprops = oldprop;
  808. found = 1;
  809. break;
  810. }
  811. next = &(*next)->next;
  812. }
  813. write_unlock_irqrestore(&devtree_lock, flags);
  814. if (!found)
  815. return -ENODEV;
  816. #ifdef CONFIG_PROC_DEVICETREE
  817. /* try to add to proc as well if it was initialized */
  818. if (np->pde)
  819. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  820. #endif /* CONFIG_PROC_DEVICETREE */
  821. return 0;
  822. }
  823. #if defined(CONFIG_OF_DYNAMIC)
  824. /*
  825. * Support for dynamic device trees.
  826. *
  827. * On some platforms, the device tree can be manipulated at runtime.
  828. * The routines in this section support adding, removing and changing
  829. * device tree nodes.
  830. */
  831. /**
  832. * of_attach_node - Plug a device node into the tree and global list.
  833. */
  834. void of_attach_node(struct device_node *np)
  835. {
  836. unsigned long flags;
  837. write_lock_irqsave(&devtree_lock, flags);
  838. np->sibling = np->parent->child;
  839. np->allnext = allnodes;
  840. np->parent->child = np;
  841. allnodes = np;
  842. write_unlock_irqrestore(&devtree_lock, flags);
  843. }
  844. /**
  845. * of_detach_node - "Unplug" a node from the device tree.
  846. *
  847. * The caller must hold a reference to the node. The memory associated with
  848. * the node is not freed until its refcount goes to zero.
  849. */
  850. void of_detach_node(struct device_node *np)
  851. {
  852. struct device_node *parent;
  853. unsigned long flags;
  854. write_lock_irqsave(&devtree_lock, flags);
  855. parent = np->parent;
  856. if (!parent)
  857. goto out_unlock;
  858. if (allnodes == np)
  859. allnodes = np->allnext;
  860. else {
  861. struct device_node *prev;
  862. for (prev = allnodes;
  863. prev->allnext != np;
  864. prev = prev->allnext)
  865. ;
  866. prev->allnext = np->allnext;
  867. }
  868. if (parent->child == np)
  869. parent->child = np->sibling;
  870. else {
  871. struct device_node *prevsib;
  872. for (prevsib = np->parent->child;
  873. prevsib->sibling != np;
  874. prevsib = prevsib->sibling)
  875. ;
  876. prevsib->sibling = np->sibling;
  877. }
  878. of_node_set_flag(np, OF_DETACHED);
  879. out_unlock:
  880. write_unlock_irqrestore(&devtree_lock, flags);
  881. }
  882. #endif /* defined(CONFIG_OF_DYNAMIC) */