| 1 | |
| 2 | #include <linux/of.h>
|
| 3 | #include <linux/slab.h>
|
| 4 |
|
| 5 | #include <kunit/visibility.h>
|
| 6 |
|
| 7 | #include "of_private.h"
|
| 8 |
|
| 9 |
|
| 10 | static int of_node_is_initialized(const struct device_node *node)
|
| 11 | {
|
| 12 | return node && node->kobj.state_initialized;
|
| 13 | }
|
| 14 |
|
| 15 |
|
| 16 | int of_node_is_attached(const struct device_node *node)
|
| 17 | {
|
| 18 | return node && node->kobj.state_in_sysfs;
|
| 19 | }
|
| 20 |
|
| 21 |
|
| 22 | #ifndef CONFIG_OF_DYNAMIC
|
| 23 | static void of_node_release(struct kobject *kobj)
|
| 24 | {
|
| 25 |
|
| 26 | }
|
| 27 | #endif
|
| 28 |
|
| 29 | const struct kobj_type of_node_ktype = {
|
| 30 | .release = of_node_release,
|
| 31 | };
|
| 32 | EXPORT_SYMBOL_IF_KUNIT(of_node_ktype);
|
| 33 |
|
| 34 | static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
|
| 35 | const struct bin_attribute *bin_attr, char *buf,
|
| 36 | loff_t offset, size_t count)
|
| 37 | {
|
| 38 | struct property *pp = container_of(bin_attr, struct property, attr);
|
| 39 | return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
|
| 40 | }
|
| 41 |
|
| 42 |
|
| 43 | static const char *safe_name(const struct kobject *kobj, const char *orig_name)
|
| 44 | {
|
| 45 | const char *name = orig_name;
|
| 46 | struct kernfs_node *kn;
|
| 47 | int i = 0;
|
| 48 |
|
| 49 |
|
| 50 | while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
|
| 51 | sysfs_put(kn);
|
| 52 | if (name != orig_name)
|
| 53 | kfree(name);
|
| 54 | name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
|
| 55 | }
|
| 56 |
|
| 57 | if (name == orig_name) {
|
| 58 | name = kstrdup(orig_name, GFP_KERNEL);
|
| 59 | } else {
|
| 60 | pr_warn("Duplicate name in %s, renamed to \"%s\"\n",
|
| 61 | kobject_name(kobj), name);
|
| 62 | }
|
| 63 | return name;
|
| 64 | }
|
| 65 |
|
| 66 | int __of_add_property_sysfs(struct device_node *np, struct property *pp)
|
| 67 | {
|
| 68 | int rc;
|
| 69 |
|
| 70 |
|
| 71 | bool secure = strncmp(pp->name, "security-", 9) == 0;
|
| 72 |
|
| 73 | if (!IS_ENABLED(CONFIG_SYSFS))
|
| 74 | return 0;
|
| 75 |
|
| 76 | if (!of_kset || !of_node_is_attached(np))
|
| 77 | return 0;
|
| 78 |
|
| 79 | sysfs_bin_attr_init(&pp->attr);
|
| 80 | pp->attr.attr.name = safe_name(&np->kobj, pp->name);
|
| 81 | pp->attr.attr.mode = secure ? 0400 : 0444;
|
| 82 | pp->attr.size = secure ? 0 : pp->length;
|
| 83 | pp->attr.read = of_node_property_read;
|
| 84 |
|
| 85 | rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
|
| 86 | WARN(rc, "error adding attribute %s to node %pOF\n", pp->name, np);
|
| 87 | return rc;
|
| 88 | }
|
| 89 |
|
| 90 | void __of_sysfs_remove_bin_file(struct device_node *np, const struct property *prop)
|
| 91 | {
|
| 92 | if (!IS_ENABLED(CONFIG_SYSFS))
|
| 93 | return;
|
| 94 |
|
| 95 | sysfs_remove_bin_file(&np->kobj, &prop->attr);
|
| 96 | kfree(prop->attr.attr.name);
|
| 97 | }
|
| 98 |
|
| 99 | void __of_remove_property_sysfs(struct device_node *np, const struct property *prop)
|
| 100 | {
|
| 101 |
|
| 102 | if (of_kset && of_node_is_attached(np))
|
| 103 | __of_sysfs_remove_bin_file(np, prop);
|
| 104 | }
|
| 105 |
|
| 106 | void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
|
| 107 | const struct property *oldprop)
|
| 108 | {
|
| 109 |
|
| 110 | if (!of_kset)
|
| 111 | return;
|
| 112 |
|
| 113 | if (oldprop)
|
| 114 | __of_sysfs_remove_bin_file(np, oldprop);
|
| 115 | __of_add_property_sysfs(np, newprop);
|
| 116 | }
|
| 117 |
|
| 118 | int __of_attach_node_sysfs(struct device_node *np)
|
| 119 | {
|
| 120 | const char *name;
|
| 121 | struct kobject *parent;
|
| 122 | struct property *pp;
|
| 123 | int rc;
|
| 124 |
|
| 125 | if (!IS_ENABLED(CONFIG_SYSFS) || !of_kset)
|
| 126 | return 0;
|
| 127 |
|
| 128 | np->kobj.kset = of_kset;
|
| 129 | if (!np->parent) {
|
| 130 |
|
| 131 | name = safe_name(&of_kset->kobj, "base");
|
| 132 | parent = NULL;
|
| 133 | } else {
|
| 134 | name = safe_name(&np->parent->kobj, kbasename(np->full_name));
|
| 135 | parent = &np->parent->kobj;
|
| 136 | }
|
| 137 | if (!name)
|
| 138 | return -ENOMEM;
|
| 139 |
|
| 140 | rc = kobject_add(&np->kobj, parent, "%s", name);
|
| 141 | kfree(name);
|
| 142 | if (rc)
|
| 143 | return rc;
|
| 144 |
|
| 145 | for_each_property_of_node(np, pp)
|
| 146 | __of_add_property_sysfs(np, pp);
|
| 147 |
|
| 148 | of_node_get(np);
|
| 149 | return 0;
|
| 150 | }
|
| 151 |
|
| 152 | void __of_detach_node_sysfs(struct device_node *np)
|
| 153 | {
|
| 154 | struct property *pp;
|
| 155 |
|
| 156 | BUG_ON(!of_node_is_initialized(np));
|
| 157 | if (!of_kset)
|
| 158 | return;
|
| 159 |
|
| 160 |
|
| 161 | if (of_node_is_attached(np)) {
|
| 162 | for_each_property_of_node(np, pp)
|
| 163 | __of_sysfs_remove_bin_file(np, pp);
|
| 164 | kobject_del(&np->kobj);
|
| 165 | }
|
| 166 |
|
| 167 | of_node_put(np);
|
| 168 | }
|