base.c 22 KB

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