prom_common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/export.h>
  18. #include <linux/errno.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/of_pdt.h>
  23. #include <asm/prom.h>
  24. #include <asm/oplib.h>
  25. #include "prom.h"
  26. struct device_node *of_console_device;
  27. EXPORT_SYMBOL(of_console_device);
  28. char *of_console_path;
  29. EXPORT_SYMBOL(of_console_path);
  30. char *of_console_options;
  31. EXPORT_SYMBOL(of_console_options);
  32. int of_getintprop_default(struct device_node *np, const char *name, int def)
  33. {
  34. struct property *prop;
  35. int len;
  36. prop = of_find_property(np, name, &len);
  37. if (!prop || len != 4)
  38. return def;
  39. return *(int *) prop->value;
  40. }
  41. EXPORT_SYMBOL(of_getintprop_default);
  42. DEFINE_MUTEX(of_set_property_mutex);
  43. EXPORT_SYMBOL(of_set_property_mutex);
  44. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  45. {
  46. struct property **prevp;
  47. void *new_val;
  48. int err;
  49. new_val = kmemdup(val, len, GFP_KERNEL);
  50. if (!new_val)
  51. return -ENOMEM;
  52. err = -ENODEV;
  53. mutex_lock(&of_set_property_mutex);
  54. write_lock(&devtree_lock);
  55. prevp = &dp->properties;
  56. while (*prevp) {
  57. struct property *prop = *prevp;
  58. if (!strcasecmp(prop->name, name)) {
  59. void *old_val = prop->value;
  60. int ret;
  61. ret = prom_setprop(dp->phandle, name, val, len);
  62. err = -EINVAL;
  63. if (ret >= 0) {
  64. prop->value = new_val;
  65. prop->length = len;
  66. if (OF_IS_DYNAMIC(prop))
  67. kfree(old_val);
  68. OF_MARK_DYNAMIC(prop);
  69. err = 0;
  70. }
  71. break;
  72. }
  73. prevp = &(*prevp)->next;
  74. }
  75. write_unlock(&devtree_lock);
  76. mutex_unlock(&of_set_property_mutex);
  77. /* XXX Upate procfs if necessary... */
  78. return err;
  79. }
  80. EXPORT_SYMBOL(of_set_property);
  81. int of_find_in_proplist(const char *list, const char *match, int len)
  82. {
  83. while (len > 0) {
  84. int l;
  85. if (!strcmp(list, match))
  86. return 1;
  87. l = strlen(list) + 1;
  88. list += l;
  89. len -= l;
  90. }
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(of_find_in_proplist);
  94. /*
  95. * SPARC32 and SPARC64's prom_nextprop() do things differently
  96. * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf',
  97. * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an
  98. * empty string upon error.
  99. */
  100. static int __init handle_nextprop_quirks(char *buf, const char *name)
  101. {
  102. if (!name || strlen(name) == 0)
  103. return -1;
  104. #ifdef CONFIG_SPARC32
  105. strcpy(buf, name);
  106. #endif
  107. return 0;
  108. }
  109. static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
  110. {
  111. const char *name;
  112. buf[0] = '\0';
  113. name = prom_nextprop(node, prev, buf);
  114. return handle_nextprop_quirks(buf, name);
  115. }
  116. unsigned int prom_early_allocated __initdata;
  117. static struct of_pdt_ops prom_sparc_ops __initdata = {
  118. .nextprop = prom_common_nextprop,
  119. .getproplen = prom_getproplen,
  120. .getproperty = prom_getproperty,
  121. .getchild = prom_getchild,
  122. .getsibling = prom_getsibling,
  123. };
  124. void __init prom_build_devicetree(void)
  125. {
  126. of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
  127. of_console_init();
  128. pr_info("PROM: Built device tree with %u bytes of memory.\n",
  129. prom_early_allocated);
  130. }