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/stat.h>
12 #include <linux/string.h>
14 #include <linux/module.h>
16 #include <asm/uaccess.h>
19 static inline void set_node_proc_entry(struct device_node
*np
,
20 struct proc_dir_entry
*de
)
22 #ifdef HAVE_ARCH_DEVTREE_FIXUPS
27 static struct proc_dir_entry
*proc_device_tree
;
30 * Supply data on a read from /proc/device-tree/node/property.
32 static int property_proc_show(struct seq_file
*m
, void *v
)
34 struct property
*pp
= m
->private;
36 seq_write(m
, pp
->value
, pp
->length
);
40 static int property_proc_open(struct inode
*inode
, struct file
*file
)
42 return single_open(file
, property_proc_show
, PDE(inode
)->data
);
45 static const struct file_operations property_proc_fops
= {
47 .open
= property_proc_open
,
50 .release
= single_release
,
54 * For a node with a name like "gc@10", we make symlinks called "gc"
59 * Add a property to a node
61 static struct proc_dir_entry
*
62 __proc_device_tree_add_prop(struct proc_dir_entry
*de
, struct property
*pp
,
65 struct proc_dir_entry
*ent
;
68 * Unfortunately proc_register puts each new entry
69 * at the beginning of the list. So we rearrange them.
71 ent
= proc_create_data(name
,
72 strncmp(name
, "security-", 9) ? S_IRUGO
: S_IRUSR
,
73 de
, &property_proc_fops
, pp
);
77 if (!strncmp(name
, "security-", 9))
78 ent
->size
= 0; /* don't leak number of password chars */
80 ent
->size
= pp
->length
;
86 void proc_device_tree_add_prop(struct proc_dir_entry
*pde
, struct property
*prop
)
88 __proc_device_tree_add_prop(pde
, prop
, prop
->name
);
91 void proc_device_tree_remove_prop(struct proc_dir_entry
*pde
,
92 struct property
*prop
)
94 remove_proc_entry(prop
->name
, pde
);
97 void proc_device_tree_update_prop(struct proc_dir_entry
*pde
,
98 struct property
*newprop
,
99 struct property
*oldprop
)
101 struct proc_dir_entry
*ent
;
103 for (ent
= pde
->subdir
; ent
!= NULL
; ent
= ent
->next
)
104 if (ent
->data
== oldprop
)
107 printk(KERN_WARNING
"device-tree: property \"%s\" "
108 " does not exist\n", oldprop
->name
);
111 ent
->size
= newprop
->length
;
116 * Various dodgy firmware might give us nodes and/or properties with
117 * conflicting names. That's generally ok, except for exporting via /proc,
118 * so munge names here to ensure they're unique.
121 static int duplicate_name(struct proc_dir_entry
*de
, const char *name
)
123 struct proc_dir_entry
*ent
;
126 spin_lock(&proc_subdir_lock
);
128 for (ent
= de
->subdir
; ent
!= NULL
; ent
= ent
->next
) {
129 if (strcmp(ent
->name
, name
) == 0) {
135 spin_unlock(&proc_subdir_lock
);
140 static const char *fixup_name(struct device_node
*np
, struct proc_dir_entry
*de
,
144 int fixup_len
= strlen(name
) + 2 + 1; /* name + #x + \0 */
148 fixed_name
= kmalloc(fixup_len
, GFP_KERNEL
);
149 if (fixed_name
== NULL
) {
150 printk(KERN_ERR
"device-tree: Out of memory trying to fixup "
151 "name \"%s\"\n", name
);
156 size
= snprintf(fixed_name
, fixup_len
, "%s#%d", name
, i
);
157 size
++; /* account for NULL */
159 if (size
> fixup_len
) {
160 /* We ran out of space, free and reallocate. */
166 if (duplicate_name(de
, fixed_name
)) {
167 /* Multiple duplicates. Retry with a different offset. */
172 printk(KERN_WARNING
"device-tree: Duplicate name in %s, "
173 "renamed to \"%s\"\n", np
->full_name
, fixed_name
);
179 * Process a node, adding entries for its children and its properties.
181 void proc_device_tree_add_node(struct device_node
*np
,
182 struct proc_dir_entry
*de
)
185 struct proc_dir_entry
*ent
;
186 struct device_node
*child
;
189 set_node_proc_entry(np
, de
);
190 for (child
= NULL
; (child
= of_get_next_child(np
, child
));) {
191 /* Use everything after the last slash, or the full name */
192 p
= strrchr(child
->full_name
, '/');
194 p
= 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
) {
211 if (duplicate_name(de
, p
))
212 p
= fixup_name(np
, de
, p
);
214 ent
= __proc_device_tree_add_prop(de
, pp
, p
);
221 * Called on initialization to set up the /proc/device-tree subtree
223 void __init
proc_device_tree_init(void)
225 struct device_node
*root
;
227 proc_device_tree
= proc_mkdir("device-tree", NULL
);
228 if (proc_device_tree
== NULL
)
230 root
= of_find_node_by_path("/");
232 printk(KERN_ERR
"/proc/device-tree: can't find root\n");
235 proc_device_tree_add_node(root
, proc_device_tree
);