0ec45110e15eecf80ee12e09d19d4dd4b01de76f
[firefly-linux-kernel-4.4.55.git] / fs / proc / proc_devtree.c
1 /*
2  * proc_devtree.c - handles /proc/device-tree
3  *
4  * Copyright 1997 Paul Mackerras
5  */
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>
13 #include <linux/of.h>
14 #include <asm/prom.h>
15 #include <asm/uaccess.h>
16 #include "internal.h"
17
18 static inline void set_node_proc_entry(struct device_node *np,
19                                        struct proc_dir_entry *de)
20 {
21 #ifdef HAVE_ARCH_DEVTREE_FIXUPS
22         np->pde = de;
23 #endif
24 }
25
26 static struct proc_dir_entry *proc_device_tree;
27
28 /*
29  * Supply data on a read from /proc/device-tree/node/property.
30  */
31 static int property_proc_show(struct seq_file *m, void *v)
32 {
33         struct property *pp = m->private;
34
35         seq_write(m, pp->value, pp->length);
36         return 0;
37 }
38
39 static int property_proc_open(struct inode *inode, struct file *file)
40 {
41         return single_open(file, property_proc_show, PDE(inode)->data);
42 }
43
44 static const struct file_operations property_proc_fops = {
45         .owner          = THIS_MODULE,
46         .open           = property_proc_open,
47         .read           = seq_read,
48         .llseek         = seq_lseek,
49         .release        = single_release,
50 };
51
52 /*
53  * For a node with a name like "gc@10", we make symlinks called "gc"
54  * and "@10" to it.
55  */
56
57 /*
58  * Add a property to a node
59  */
60 static struct proc_dir_entry *
61 __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
62                 const char *name)
63 {
64         struct proc_dir_entry *ent;
65
66         /*
67          * Unfortunately proc_register puts each new entry
68          * at the beginning of the list.  So we rearrange them.
69          */
70         ent = proc_create_data(name,
71                                strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
72                                de, &property_proc_fops, pp);
73         if (ent == NULL)
74                 return NULL;
75
76         if (!strncmp(name, "security-", 9))
77                 ent->size = 0; /* don't leak number of password chars */
78         else
79                 ent->size = pp->length;
80
81         return ent;
82 }
83
84
85 void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
86 {
87         __proc_device_tree_add_prop(pde, prop, prop->name);
88 }
89
90 void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
91                                   struct property *prop)
92 {
93         remove_proc_entry(prop->name, pde);
94 }
95
96 void proc_device_tree_update_prop(struct proc_dir_entry *pde,
97                                   struct property *newprop,
98                                   struct property *oldprop)
99 {
100         struct proc_dir_entry *ent;
101
102         for (ent = pde->subdir; ent != NULL; ent = ent->next)
103                 if (ent->data == oldprop)
104                         break;
105         if (ent == NULL) {
106                 printk(KERN_WARNING "device-tree: property \"%s\" "
107                        " does not exist\n", oldprop->name);
108         } else {
109                 ent->data = newprop;
110                 ent->size = newprop->length;
111         }
112 }
113
114 /*
115  * Various dodgy firmware might give us nodes and/or properties with
116  * conflicting names. That's generally ok, except for exporting via /proc,
117  * so munge names here to ensure they're unique.
118  */
119
120 static int duplicate_name(struct proc_dir_entry *de, const char *name)
121 {
122         struct proc_dir_entry *ent;
123         int found = 0;
124
125         spin_lock(&proc_subdir_lock);
126
127         for (ent = de->subdir; ent != NULL; ent = ent->next) {
128                 if (strcmp(ent->name, name) == 0) {
129                         found = 1;
130                         break;
131                 }
132         }
133
134         spin_unlock(&proc_subdir_lock);
135
136         return found;
137 }
138
139 static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
140                 const char *name)
141 {
142         char *fixed_name;
143         int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
144         int i = 1, size;
145
146 realloc:
147         fixed_name = kmalloc(fixup_len, GFP_KERNEL);
148         if (fixed_name == NULL) {
149                 printk(KERN_ERR "device-tree: Out of memory trying to fixup "
150                                 "name \"%s\"\n", name);
151                 return name;
152         }
153
154 retry:
155         size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
156         size++; /* account for NULL */
157
158         if (size > fixup_len) {
159                 /* We ran out of space, free and reallocate. */
160                 kfree(fixed_name);
161                 fixup_len = size;
162                 goto realloc;
163         }
164
165         if (duplicate_name(de, fixed_name)) {
166                 /* Multiple duplicates. Retry with a different offset. */
167                 i++;
168                 goto retry;
169         }
170
171         printk(KERN_WARNING "device-tree: Duplicate name in %s, "
172                         "renamed to \"%s\"\n", np->full_name, fixed_name);
173
174         return fixed_name;
175 }
176
177 /*
178  * Process a node, adding entries for its children and its properties.
179  */
180 void proc_device_tree_add_node(struct device_node *np,
181                                struct proc_dir_entry *de)
182 {
183         struct property *pp;
184         struct proc_dir_entry *ent;
185         struct device_node *child;
186         const char *p;
187
188         set_node_proc_entry(np, de);
189         for (child = NULL; (child = of_get_next_child(np, child));) {
190                 /* Use everything after the last slash, or the full name */
191                 p = strrchr(child->full_name, '/');
192                 if (!p)
193                         p = child->full_name;
194                 else
195                         ++p;
196
197                 if (duplicate_name(de, p))
198                         p = fixup_name(np, de, p);
199
200                 ent = proc_mkdir(p, de);
201                 if (ent == NULL)
202                         break;
203                 proc_device_tree_add_node(child, ent);
204         }
205         of_node_put(child);
206
207         for (pp = np->properties; pp != NULL; pp = pp->next) {
208                 p = pp->name;
209
210                 if (duplicate_name(de, p))
211                         p = fixup_name(np, de, p);
212
213                 ent = __proc_device_tree_add_prop(de, pp, p);
214                 if (ent == NULL)
215                         break;
216         }
217 }
218
219 /*
220  * Called on initialization to set up the /proc/device-tree subtree
221  */
222 void __init proc_device_tree_init(void)
223 {
224         struct device_node *root;
225
226         proc_device_tree = proc_mkdir("device-tree", NULL);
227         if (proc_device_tree == NULL)
228                 return;
229         root = of_find_node_by_path("/");
230         if (root == NULL) {
231                 printk(KERN_ERR "/proc/device-tree: can't find root\n");
232                 return;
233         }
234         proc_device_tree_add_node(root, proc_device_tree);
235         of_node_put(root);
236 }