base.c 25 KB

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