kobj.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/of.h>
  3. #include <linux/slab.h>
  4. #include "of_private.h"
  5. /* true when node is initialized */
  6. static int of_node_is_initialized(struct device_node *node)
  7. {
  8. return node && node->kobj.state_initialized;
  9. }
  10. /* true when node is attached (i.e. present on sysfs) */
  11. int of_node_is_attached(struct device_node *node)
  12. {
  13. return node && node->kobj.state_in_sysfs;
  14. }
  15. #ifndef CONFIG_OF_DYNAMIC
  16. static void of_node_release(struct kobject *kobj)
  17. {
  18. /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
  19. }
  20. #endif /* CONFIG_OF_DYNAMIC */
  21. struct kobj_type of_node_ktype = {
  22. .release = of_node_release,
  23. };
  24. static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
  25. struct bin_attribute *bin_attr, char *buf,
  26. loff_t offset, size_t count)
  27. {
  28. struct property *pp = container_of(bin_attr, struct property, attr);
  29. return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
  30. }
  31. /* always return newly allocated name, caller must free after use */
  32. static const char *safe_name(struct kobject *kobj, const char *orig_name)
  33. {
  34. const char *name = orig_name;
  35. struct kernfs_node *kn;
  36. int i = 0;
  37. /* don't be a hero. After 16 tries give up */
  38. while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
  39. sysfs_put(kn);
  40. if (name != orig_name)
  41. kfree(name);
  42. name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
  43. }
  44. if (name == orig_name) {
  45. name = kstrdup(orig_name, GFP_KERNEL);
  46. } else {
  47. pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
  48. kobject_name(kobj), name);
  49. }
  50. return name;
  51. }
  52. int __of_add_property_sysfs(struct device_node *np, struct property *pp)
  53. {
  54. int rc;
  55. /* Important: Don't leak passwords */
  56. bool secure = strncmp(pp->name, "security-", 9) == 0;
  57. if (!IS_ENABLED(CONFIG_SYSFS))
  58. return 0;
  59. if (!of_kset || !of_node_is_attached(np))
  60. return 0;
  61. sysfs_bin_attr_init(&pp->attr);
  62. pp->attr.attr.name = safe_name(&np->kobj, pp->name);
  63. pp->attr.attr.mode = secure ? 0400 : 0444;
  64. pp->attr.size = secure ? 0 : pp->length;
  65. pp->attr.read = of_node_property_read;
  66. rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
  67. WARN(rc, "error adding attribute %s to node %pOF\n", pp->name, np);
  68. return rc;
  69. }
  70. void __of_sysfs_remove_bin_file(struct device_node *np, struct property *prop)
  71. {
  72. if (!IS_ENABLED(CONFIG_SYSFS))
  73. return;
  74. sysfs_remove_bin_file(&np->kobj, &prop->attr);
  75. kfree(prop->attr.attr.name);
  76. }
  77. void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
  78. {
  79. /* at early boot, bail here and defer setup to of_init() */
  80. if (of_kset && of_node_is_attached(np))
  81. __of_sysfs_remove_bin_file(np, prop);
  82. }
  83. void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
  84. struct property *oldprop)
  85. {
  86. /* At early boot, bail out and defer setup to of_init() */
  87. if (!of_kset)
  88. return;
  89. if (oldprop)
  90. __of_sysfs_remove_bin_file(np, oldprop);
  91. __of_add_property_sysfs(np, newprop);
  92. }
  93. int __of_attach_node_sysfs(struct device_node *np)
  94. {
  95. const char *name;
  96. struct kobject *parent;
  97. struct property *pp;
  98. int rc;
  99. if (!of_kset)
  100. return 0;
  101. np->kobj.kset = of_kset;
  102. if (!np->parent) {
  103. /* Nodes without parents are new top level trees */
  104. name = safe_name(&of_kset->kobj, "base");
  105. parent = NULL;
  106. } else {
  107. name = safe_name(&np->parent->kobj, kbasename(np->full_name));
  108. parent = &np->parent->kobj;
  109. }
  110. if (!name)
  111. return -ENOMEM;
  112. of_node_get(np);
  113. rc = kobject_add(&np->kobj, parent, "%s", name);
  114. kfree(name);
  115. if (rc)
  116. return rc;
  117. for_each_property_of_node(np, pp)
  118. __of_add_property_sysfs(np, pp);
  119. return 0;
  120. }
  121. void __of_detach_node_sysfs(struct device_node *np)
  122. {
  123. struct property *pp;
  124. BUG_ON(!of_node_is_initialized(np));
  125. if (!of_kset)
  126. return;
  127. /* only remove properties if on sysfs */
  128. if (of_node_is_attached(np)) {
  129. for_each_property_of_node(np, pp)
  130. __of_sysfs_remove_bin_file(np, pp);
  131. kobject_del(&np->kobj);
  132. }
  133. of_node_put(np);
  134. }