base.c 24 KB

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