base.c 20 KB

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