blob: f8650dce74fb069c6a66a497c4933cc50e630539 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/prom.h>
16#include <asm/uaccess.h>
Al Viro3174c212009-04-07 13:19:18 -040017#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +100019static inline void set_node_proc_entry(struct device_node *np,
20 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070022#ifdef HAVE_ARCH_DEVTREE_FIXUPS
23 np->pde = de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#endif
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070025}
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct proc_dir_entry *proc_device_tree;
28
29/*
30 * Supply data on a read from /proc/device-tree/node/property.
31 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000032static int property_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Alexey Dobriyane22f6282009-09-06 23:31:25 +000034 struct property *pp = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Alexey Dobriyane22f6282009-09-06 23:31:25 +000036 seq_write(m, pp->value, pp->length);
37 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Alexey Dobriyane22f6282009-09-06 23:31:25 +000040static int property_proc_open(struct inode *inode, struct file *file)
41{
42 return single_open(file, property_proc_show, PDE(inode)->data);
43}
44
45static const struct file_operations property_proc_fops = {
46 .owner = THIS_MODULE,
47 .open = property_proc_open,
48 .read = seq_read,
49 .llseek = seq_lseek,
50 .release = single_release,
51};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * For a node with a name like "gc@10", we make symlinks called "gc"
55 * and "@10" to it.
56 */
57
58/*
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110059 * Add a property to a node
60 */
61static struct proc_dir_entry *
Michael Ellerman5149fa42006-03-27 14:26:26 +110062__proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
63 const char *name)
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110064{
65 struct proc_dir_entry *ent;
66
67 /*
68 * Unfortunately proc_register puts each new entry
69 * at the beginning of the list. So we rearrange them.
70 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000071 ent = proc_create_data(name,
72 strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
73 de, &property_proc_fops, pp);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110074 if (ent == NULL)
75 return NULL;
76
Michael Ellerman5149fa42006-03-27 14:26:26 +110077 if (!strncmp(name, "security-", 9))
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110078 ent->size = 0; /* don't leak number of password chars */
79 else
80 ent->size = pp->length;
81
82 return ent;
83}
84
85
86void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
87{
Michael Ellerman5149fa42006-03-27 14:26:26 +110088 __proc_device_tree_add_prop(pde, prop, prop->name);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110089}
90
Dave C Boutcher898b5392006-01-12 16:07:17 -060091void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
92 struct property *prop)
93{
94 remove_proc_entry(prop->name, pde);
95}
96
97void proc_device_tree_update_prop(struct proc_dir_entry *pde,
98 struct property *newprop,
99 struct property *oldprop)
100{
101 struct proc_dir_entry *ent;
102
103 for (ent = pde->subdir; ent != NULL; ent = ent->next)
104 if (ent->data == oldprop)
105 break;
106 if (ent == NULL) {
107 printk(KERN_WARNING "device-tree: property \"%s\" "
108 " does not exist\n", oldprop->name);
109 } else {
110 ent->data = newprop;
111 ent->size = newprop->length;
112 }
113}
114
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100115/*
Michael Ellerman5149fa42006-03-27 14:26:26 +1100116 * 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.
119 */
120
121static int duplicate_name(struct proc_dir_entry *de, const char *name)
122{
123 struct proc_dir_entry *ent;
124 int found = 0;
125
126 spin_lock(&proc_subdir_lock);
127
128 for (ent = de->subdir; ent != NULL; ent = ent->next) {
129 if (strcmp(ent->name, name) == 0) {
130 found = 1;
131 break;
132 }
133 }
134
135 spin_unlock(&proc_subdir_lock);
136
137 return found;
138}
139
140static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
141 const char *name)
142{
143 char *fixed_name;
144 int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
145 int i = 1, size;
146
147realloc:
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);
152 return name;
153 }
154
155retry:
156 size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
157 size++; /* account for NULL */
158
159 if (size > fixup_len) {
160 /* We ran out of space, free and reallocate. */
161 kfree(fixed_name);
162 fixup_len = size;
163 goto realloc;
164 }
165
166 if (duplicate_name(de, fixed_name)) {
167 /* Multiple duplicates. Retry with a different offset. */
168 i++;
169 goto retry;
170 }
171
172 printk(KERN_WARNING "device-tree: Duplicate name in %s, "
173 "renamed to \"%s\"\n", np->full_name, fixed_name);
174
175 return fixed_name;
176}
177
178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Process a node, adding entries for its children and its properties.
180 */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000181void proc_device_tree_add_node(struct device_node *np,
182 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct property *pp;
185 struct proc_dir_entry *ent;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000186 struct device_node *child;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000187 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 set_node_proc_entry(np, de);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000190 for (child = NULL; (child = of_get_next_child(np, child));) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100191 /* Use everything after the last slash, or the full name */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000192 p = strrchr(child->full_name, '/');
193 if (!p)
194 p = child->full_name;
195 else
196 ++p;
Michael Ellerman5149fa42006-03-27 14:26:26 +1100197
198 if (duplicate_name(de, p))
199 p = fixup_name(np, de, p);
200
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000201 ent = proc_mkdir(p, de);
Michal Simekbcac2b12009-06-17 16:25:59 -0700202 if (ent == NULL)
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000203 break;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000204 proc_device_tree_add_node(child, ent);
205 }
206 of_node_put(child);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000207
Michal Simekbcac2b12009-06-17 16:25:59 -0700208 for (pp = np->properties; pp != NULL; pp = pp->next) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100209 p = pp->name;
210
211 if (duplicate_name(de, p))
212 p = fixup_name(np, de, p);
213
214 ent = __proc_device_tree_add_prop(de, pp, p);
Michal Simekbcac2b12009-06-17 16:25:59 -0700215 if (ent == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220/*
221 * Called on initialization to set up the /proc/device-tree subtree
222 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400223void __init proc_device_tree_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct device_node *root;
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 proc_device_tree = proc_mkdir("device-tree", NULL);
Michal Simekbcac2b12009-06-17 16:25:59 -0700228 if (proc_device_tree == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return;
230 root = of_find_node_by_path("/");
Michal Simekbcac2b12009-06-17 16:25:59 -0700231 if (root == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 printk(KERN_ERR "/proc/device-tree: can't find root\n");
233 return;
234 }
235 proc_device_tree_add_node(root, proc_device_tree);
236 of_node_put(root);
237}