2 * proc_devtree.c - handles /proc/device-tree
4 * Copyright 1997 Paul Mackerras
6 #include <linux/errno.h>
7 #include <linux/init.h>
8 #include <linux/time.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_file.h>
11 #include <linux/printk.h>
12 #include <linux/stat.h>
13 #include <linux/string.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <asm/uaccess.h>
20 static inline void set_node_proc_entry(struct device_node
*np
,
21 struct proc_dir_entry
*de
)
26 static struct proc_dir_entry
*proc_device_tree
;
29 * Supply data on a read from /proc/device-tree/node/property.
31 static int property_proc_show(struct seq_file
*m
, void *v
)
33 struct property
*pp
= m
->private;
35 seq_write(m
, pp
->value
, pp
->length
);
39 static int property_proc_open(struct inode
*inode
, struct file
*file
)
41 return single_open(file
, property_proc_show
, __PDE_DATA(inode
));
44 static const struct file_operations property_proc_fops
= {
46 .open
= property_proc_open
,
49 .release
= single_release
,
53 * For a node with a name like "gc@10", we make symlinks called "gc"
58 * Add a property to a node
60 static struct proc_dir_entry
*
61 __proc_device_tree_add_prop(struct proc_dir_entry
*de
, struct property
*pp
,
64 struct proc_dir_entry
*ent
;
67 * Unfortunately proc_register puts each new entry
68 * at the beginning of the list. So we rearrange them.
70 ent
= proc_create_data(name
,
71 strncmp(name
, "security-", 9) ? S_IRUGO
: S_IRUSR
,
72 de
, &property_proc_fops
, pp
);
76 if (!strncmp(name
, "security-", 9))
77 proc_set_size(ent
, 0); /* don't leak number of password chars */
79 proc_set_size(ent
, pp
->length
);
85 void proc_device_tree_add_prop(struct proc_dir_entry
*pde
, struct property
*prop
)
87 __proc_device_tree_add_prop(pde
, prop
, prop
->name
);
90 void proc_device_tree_remove_prop(struct proc_dir_entry
*pde
,
91 struct property
*prop
)
93 remove_proc_entry(prop
->name
, pde
);
96 void proc_device_tree_update_prop(struct proc_dir_entry
*pde
,
97 struct property
*newprop
,
98 struct property
*oldprop
)
100 struct proc_dir_entry
*ent
;
103 proc_device_tree_add_prop(pde
, newprop
);
107 for (ent
= pde
->subdir
; ent
!= NULL
; ent
= ent
->next
)
108 if (ent
->data
== oldprop
)
111 pr_warn("device-tree: property \"%s\" does not exist\n",
115 ent
->size
= newprop
->length
;
120 * Various dodgy firmware might give us nodes and/or properties with
121 * conflicting names. That's generally ok, except for exporting via /proc,
122 * so munge names here to ensure they're unique.
125 static int duplicate_name(struct proc_dir_entry
*de
, const char *name
)
127 struct proc_dir_entry
*ent
;
130 spin_lock(&proc_subdir_lock
);
132 for (ent
= de
->subdir
; ent
!= NULL
; ent
= ent
->next
) {
133 if (strcmp(ent
->name
, name
) == 0) {
139 spin_unlock(&proc_subdir_lock
);
144 static const char *fixup_name(struct device_node
*np
, struct proc_dir_entry
*de
,
148 int fixup_len
= strlen(name
) + 2 + 1; /* name + #x + \0 */
152 fixed_name
= kmalloc(fixup_len
, GFP_KERNEL
);
153 if (fixed_name
== NULL
) {
154 pr_err("device-tree: Out of memory trying to fixup "
155 "name \"%s\"\n", name
);
160 size
= snprintf(fixed_name
, fixup_len
, "%s#%d", name
, i
);
161 size
++; /* account for NULL */
163 if (size
> fixup_len
) {
164 /* We ran out of space, free and reallocate. */
170 if (duplicate_name(de
, fixed_name
)) {
171 /* Multiple duplicates. Retry with a different offset. */
176 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
177 np
->full_name
, fixed_name
);
183 * Process a node, adding entries for its children and its properties.
185 void proc_device_tree_add_node(struct device_node
*np
,
186 struct proc_dir_entry
*de
)
189 struct proc_dir_entry
*ent
;
190 struct device_node
*child
;
193 set_node_proc_entry(np
, de
);
194 for (child
= NULL
; (child
= of_get_next_child(np
, child
));) {
195 /* Use everything after the last slash, or the full name */
196 p
= kbasename(child
->full_name
);
198 if (duplicate_name(de
, p
))
199 p
= fixup_name(np
, de
, p
);
201 ent
= proc_mkdir(p
, de
);
204 proc_device_tree_add_node(child
, ent
);
208 for (pp
= np
->properties
; pp
!= NULL
; pp
= pp
->next
) {
214 if (duplicate_name(de
, p
))
215 p
= fixup_name(np
, de
, p
);
217 ent
= __proc_device_tree_add_prop(de
, pp
, p
);
224 * Called on initialization to set up the /proc/device-tree subtree
226 void __init
proc_device_tree_init(void)
228 struct device_node
*root
;
230 proc_device_tree
= proc_mkdir("device-tree", NULL
);
231 if (proc_device_tree
== NULL
)
233 root
= of_find_node_by_path("/");
235 remove_proc_entry("device-tree", NULL
);
236 pr_debug("/proc/device-tree: can't find root\n");
239 proc_device_tree_add_node(root
, proc_device_tree
);