prom_common.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* prom_common.c: OF device tree support common code.
  2. *
  3. * Paul Mackerras August 1996.
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. *
  6. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  7. * {engebret|bergner}@us.ibm.com
  8. *
  9. * Adapted for sparc by David S. Miller davem@davemloft.net
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <asm/prom.h>
  23. #include <asm/oplib.h>
  24. #include "prom.h"
  25. struct device_node *of_find_node_by_phandle(phandle handle)
  26. {
  27. struct device_node *np;
  28. for (np = allnodes; np; np = np->allnext)
  29. if (np->node == handle)
  30. break;
  31. return np;
  32. }
  33. EXPORT_SYMBOL(of_find_node_by_phandle);
  34. int of_getintprop_default(struct device_node *np, const char *name, int def)
  35. {
  36. struct property *prop;
  37. int len;
  38. prop = of_find_property(np, name, &len);
  39. if (!prop || len != 4)
  40. return def;
  41. return *(int *) prop->value;
  42. }
  43. EXPORT_SYMBOL(of_getintprop_default);
  44. DEFINE_MUTEX(of_set_property_mutex);
  45. EXPORT_SYMBOL(of_set_property_mutex);
  46. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  47. {
  48. struct property **prevp;
  49. void *new_val;
  50. int err;
  51. new_val = kmalloc(len, GFP_KERNEL);
  52. if (!new_val)
  53. return -ENOMEM;
  54. memcpy(new_val, val, len);
  55. err = -ENODEV;
  56. write_lock(&devtree_lock);
  57. prevp = &dp->properties;
  58. while (*prevp) {
  59. struct property *prop = *prevp;
  60. if (!strcasecmp(prop->name, name)) {
  61. void *old_val = prop->value;
  62. int ret;
  63. mutex_lock(&of_set_property_mutex);
  64. ret = prom_setprop(dp->node, name, val, len);
  65. mutex_unlock(&of_set_property_mutex);
  66. err = -EINVAL;
  67. if (ret >= 0) {
  68. prop->value = new_val;
  69. prop->length = len;
  70. if (OF_IS_DYNAMIC(prop))
  71. kfree(old_val);
  72. OF_MARK_DYNAMIC(prop);
  73. err = 0;
  74. }
  75. break;
  76. }
  77. prevp = &(*prevp)->next;
  78. }
  79. write_unlock(&devtree_lock);
  80. /* XXX Upate procfs if necessary... */
  81. return err;
  82. }
  83. EXPORT_SYMBOL(of_set_property);
  84. int of_find_in_proplist(const char *list, const char *match, int len)
  85. {
  86. while (len > 0) {
  87. int l;
  88. if (!strcmp(list, match))
  89. return 1;
  90. l = strlen(list) + 1;
  91. list += l;
  92. len -= l;
  93. }
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(of_find_in_proplist);