blob: df7dd08d439121253bb180e26ad4d7e982775adf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * proc_devtree.c - handles /proc/device-tree
3 *
4 * Copyright 1997 Paul Mackerras
5 */
6#include <linux/errno.h>
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04007#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/time.h>
9#include <linux/proc_fs.h>
Alexey Dobriyane22f6282009-09-06 23:31:25 +000010#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/stat.h>
12#include <linux/string.h>
Jeremy Kerr50ab2fe2010-02-01 21:34:14 -070013#include <linux/of.h>
Jeremy Kerr7c540d92010-02-14 07:13:41 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/prom.h>
17#include <asm/uaccess.h>
Al Viro3174c212009-04-07 13:19:18 -040018#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +100020static inline void set_node_proc_entry(struct device_node *np,
21 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070023#ifdef HAVE_ARCH_DEVTREE_FIXUPS
24 np->pde = de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#endif
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070026}
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static struct proc_dir_entry *proc_device_tree;
29
30/*
31 * Supply data on a read from /proc/device-tree/node/property.
32 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000033static int property_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Alexey Dobriyane22f6282009-09-06 23:31:25 +000035 struct property *pp = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Alexey Dobriyane22f6282009-09-06 23:31:25 +000037 seq_write(m, pp->value, pp->length);
38 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Alexey Dobriyane22f6282009-09-06 23:31:25 +000041static int property_proc_open(struct inode *inode, struct file *file)
42{
43 return single_open(file, property_proc_show, PDE(inode)->data);
44}
45
46static const struct file_operations property_proc_fops = {
47 .owner = THIS_MODULE,
48 .open = property_proc_open,
49 .read = seq_read,
50 .llseek = seq_lseek,
51 .release = single_release,
52};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * For a node with a name like "gc@10", we make symlinks called "gc"
56 * and "@10" to it.
57 */
58
59/*
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110060 * Add a property to a node
61 */
62static struct proc_dir_entry *
Michael Ellerman5149fa42006-03-27 14:26:26 +110063__proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
64 const char *name)
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110065{
66 struct proc_dir_entry *ent;
67
68 /*
69 * Unfortunately proc_register puts each new entry
70 * at the beginning of the list. So we rearrange them.
71 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000072 ent = proc_create_data(name,
73 strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
74 de, &property_proc_fops, pp);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110075 if (ent == NULL)
76 return NULL;
77
Michael Ellerman5149fa42006-03-27 14:26:26 +110078 if (!strncmp(name, "security-", 9))
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110079 ent->size = 0; /* don't leak number of password chars */
80 else
81 ent->size = pp->length;
82
83 return ent;
84}
85
86
87void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
88{
Michael Ellerman5149fa42006-03-27 14:26:26 +110089 __proc_device_tree_add_prop(pde, prop, prop->name);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110090}
91
Dave C Boutcher898b5392006-01-12 16:07:17 -060092void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
93 struct property *prop)
94{
95 remove_proc_entry(prop->name, pde);
96}
97
98void proc_device_tree_update_prop(struct proc_dir_entry *pde,
99 struct property *newprop,
100 struct property *oldprop)
101{
102 struct proc_dir_entry *ent;
103
Dong Aisheng475d0092012-07-11 15:16:37 +1000104 if (!oldprop) {
105 proc_device_tree_add_prop(pde, newprop);
106 return;
107 }
108
Dave C Boutcher898b5392006-01-12 16:07:17 -0600109 for (ent = pde->subdir; ent != NULL; ent = ent->next)
110 if (ent->data == oldprop)
111 break;
112 if (ent == NULL) {
113 printk(KERN_WARNING "device-tree: property \"%s\" "
114 " does not exist\n", oldprop->name);
115 } else {
116 ent->data = newprop;
117 ent->size = newprop->length;
118 }
119}
120
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100121/*
Michael Ellerman5149fa42006-03-27 14:26:26 +1100122 * Various dodgy firmware might give us nodes and/or properties with
123 * conflicting names. That's generally ok, except for exporting via /proc,
124 * so munge names here to ensure they're unique.
125 */
126
127static int duplicate_name(struct proc_dir_entry *de, const char *name)
128{
129 struct proc_dir_entry *ent;
130 int found = 0;
131
132 spin_lock(&proc_subdir_lock);
133
134 for (ent = de->subdir; ent != NULL; ent = ent->next) {
135 if (strcmp(ent->name, name) == 0) {
136 found = 1;
137 break;
138 }
139 }
140
141 spin_unlock(&proc_subdir_lock);
142
143 return found;
144}
145
146static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
147 const char *name)
148{
149 char *fixed_name;
150 int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
151 int i = 1, size;
152
153realloc:
154 fixed_name = kmalloc(fixup_len, GFP_KERNEL);
155 if (fixed_name == NULL) {
156 printk(KERN_ERR "device-tree: Out of memory trying to fixup "
157 "name \"%s\"\n", name);
158 return name;
159 }
160
161retry:
162 size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
163 size++; /* account for NULL */
164
165 if (size > fixup_len) {
166 /* We ran out of space, free and reallocate. */
167 kfree(fixed_name);
168 fixup_len = size;
169 goto realloc;
170 }
171
172 if (duplicate_name(de, fixed_name)) {
173 /* Multiple duplicates. Retry with a different offset. */
174 i++;
175 goto retry;
176 }
177
178 printk(KERN_WARNING "device-tree: Duplicate name in %s, "
179 "renamed to \"%s\"\n", np->full_name, fixed_name);
180
181 return fixed_name;
182}
183
184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Process a node, adding entries for its children and its properties.
186 */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000187void proc_device_tree_add_node(struct device_node *np,
188 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct property *pp;
191 struct proc_dir_entry *ent;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000192 struct device_node *child;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000193 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 set_node_proc_entry(np, de);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000196 for (child = NULL; (child = of_get_next_child(np, child));) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100197 /* Use everything after the last slash, or the full name */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000198 p = strrchr(child->full_name, '/');
199 if (!p)
200 p = child->full_name;
201 else
202 ++p;
Michael Ellerman5149fa42006-03-27 14:26:26 +1100203
204 if (duplicate_name(de, p))
205 p = fixup_name(np, de, p);
206
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000207 ent = proc_mkdir(p, de);
Michal Simekbcac2b12009-06-17 16:25:59 -0700208 if (ent == NULL)
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000209 break;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000210 proc_device_tree_add_node(child, ent);
211 }
212 of_node_put(child);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000213
Michal Simekbcac2b12009-06-17 16:25:59 -0700214 for (pp = np->properties; pp != NULL; pp = pp->next) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100215 p = pp->name;
216
Michael Ellerman9f069af2010-05-19 02:32:29 +0000217 if (strchr(p, '/'))
218 continue;
219
Michael Ellerman5149fa42006-03-27 14:26:26 +1100220 if (duplicate_name(de, p))
221 p = fixup_name(np, de, p);
222
223 ent = __proc_device_tree_add_prop(de, pp, p);
Michal Simekbcac2b12009-06-17 16:25:59 -0700224 if (ent == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/*
230 * Called on initialization to set up the /proc/device-tree subtree
231 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400232void __init proc_device_tree_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct device_node *root;
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 proc_device_tree = proc_mkdir("device-tree", NULL);
Michal Simekbcac2b12009-06-17 16:25:59 -0700237 if (proc_device_tree == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return;
239 root = of_find_node_by_path("/");
Michal Simekbcac2b12009-06-17 16:25:59 -0700240 if (root == NULL) {
Paul Bolle8aaccf72011-02-14 22:34:22 +0100241 pr_debug("/proc/device-tree: can't find root\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
243 }
244 proc_device_tree_add_node(root, proc_device_tree);
245 of_node_put(root);
246}