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