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