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