blob: 2309bf17203fb4590315ecf946cefed269264b42 [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>
13#include <asm/prom.h>
14#include <asm/uaccess.h>
Al Viro3174c212009-04-07 13:19:18 -040015#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +100017static inline void set_node_proc_entry(struct device_node *np,
18 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070020#ifdef HAVE_ARCH_DEVTREE_FIXUPS
21 np->pde = de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#endif
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070023}
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static struct proc_dir_entry *proc_device_tree;
26
27/*
28 * Supply data on a read from /proc/device-tree/node/property.
29 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000030static int property_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Alexey Dobriyane22f6282009-09-06 23:31:25 +000032 struct property *pp = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Alexey Dobriyane22f6282009-09-06 23:31:25 +000034 seq_write(m, pp->value, pp->length);
35 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Alexey Dobriyane22f6282009-09-06 23:31:25 +000038static int property_proc_open(struct inode *inode, struct file *file)
39{
40 return single_open(file, property_proc_show, PDE(inode)->data);
41}
42
43static const struct file_operations property_proc_fops = {
44 .owner = THIS_MODULE,
45 .open = property_proc_open,
46 .read = seq_read,
47 .llseek = seq_lseek,
48 .release = single_release,
49};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * For a node with a name like "gc@10", we make symlinks called "gc"
53 * and "@10" to it.
54 */
55
56/*
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110057 * Add a property to a node
58 */
59static struct proc_dir_entry *
Michael Ellerman5149fa42006-03-27 14:26:26 +110060__proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
61 const char *name)
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110062{
63 struct proc_dir_entry *ent;
64
65 /*
66 * Unfortunately proc_register puts each new entry
67 * at the beginning of the list. So we rearrange them.
68 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000069 ent = proc_create_data(name,
70 strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
71 de, &property_proc_fops, pp);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110072 if (ent == NULL)
73 return NULL;
74
Michael Ellerman5149fa42006-03-27 14:26:26 +110075 if (!strncmp(name, "security-", 9))
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110076 ent->size = 0; /* don't leak number of password chars */
77 else
78 ent->size = pp->length;
79
80 return ent;
81}
82
83
84void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
85{
Michael Ellerman5149fa42006-03-27 14:26:26 +110086 __proc_device_tree_add_prop(pde, prop, prop->name);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110087}
88
Dave C Boutcher898b5392006-01-12 16:07:17 -060089void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
90 struct property *prop)
91{
92 remove_proc_entry(prop->name, pde);
93}
94
95void proc_device_tree_update_prop(struct proc_dir_entry *pde,
96 struct property *newprop,
97 struct property *oldprop)
98{
99 struct proc_dir_entry *ent;
100
101 for (ent = pde->subdir; ent != NULL; ent = ent->next)
102 if (ent->data == oldprop)
103 break;
104 if (ent == NULL) {
105 printk(KERN_WARNING "device-tree: property \"%s\" "
106 " does not exist\n", oldprop->name);
107 } else {
108 ent->data = newprop;
109 ent->size = newprop->length;
110 }
111}
112
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100113/*
Michael Ellerman5149fa42006-03-27 14:26:26 +1100114 * Various dodgy firmware might give us nodes and/or properties with
115 * conflicting names. That's generally ok, except for exporting via /proc,
116 * so munge names here to ensure they're unique.
117 */
118
119static int duplicate_name(struct proc_dir_entry *de, const char *name)
120{
121 struct proc_dir_entry *ent;
122 int found = 0;
123
124 spin_lock(&proc_subdir_lock);
125
126 for (ent = de->subdir; ent != NULL; ent = ent->next) {
127 if (strcmp(ent->name, name) == 0) {
128 found = 1;
129 break;
130 }
131 }
132
133 spin_unlock(&proc_subdir_lock);
134
135 return found;
136}
137
138static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
139 const char *name)
140{
141 char *fixed_name;
142 int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
143 int i = 1, size;
144
145realloc:
146 fixed_name = kmalloc(fixup_len, GFP_KERNEL);
147 if (fixed_name == NULL) {
148 printk(KERN_ERR "device-tree: Out of memory trying to fixup "
149 "name \"%s\"\n", name);
150 return name;
151 }
152
153retry:
154 size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
155 size++; /* account for NULL */
156
157 if (size > fixup_len) {
158 /* We ran out of space, free and reallocate. */
159 kfree(fixed_name);
160 fixup_len = size;
161 goto realloc;
162 }
163
164 if (duplicate_name(de, fixed_name)) {
165 /* Multiple duplicates. Retry with a different offset. */
166 i++;
167 goto retry;
168 }
169
170 printk(KERN_WARNING "device-tree: Duplicate name in %s, "
171 "renamed to \"%s\"\n", np->full_name, fixed_name);
172
173 return fixed_name;
174}
175
176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * Process a node, adding entries for its children and its properties.
178 */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000179void proc_device_tree_add_node(struct device_node *np,
180 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct property *pp;
183 struct proc_dir_entry *ent;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000184 struct device_node *child;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000185 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 set_node_proc_entry(np, de);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000188 for (child = NULL; (child = of_get_next_child(np, child));) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100189 /* Use everything after the last slash, or the full name */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000190 p = strrchr(child->full_name, '/');
191 if (!p)
192 p = child->full_name;
193 else
194 ++p;
Michael Ellerman5149fa42006-03-27 14:26:26 +1100195
196 if (duplicate_name(de, p))
197 p = fixup_name(np, de, p);
198
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000199 ent = proc_mkdir(p, de);
Michal Simekbcac2b12009-06-17 16:25:59 -0700200 if (ent == NULL)
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000201 break;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000202 proc_device_tree_add_node(child, ent);
203 }
204 of_node_put(child);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000205
Michal Simekbcac2b12009-06-17 16:25:59 -0700206 for (pp = np->properties; pp != NULL; pp = pp->next) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100207 p = pp->name;
208
209 if (duplicate_name(de, p))
210 p = fixup_name(np, de, p);
211
212 ent = __proc_device_tree_add_prop(de, pp, p);
Michal Simekbcac2b12009-06-17 16:25:59 -0700213 if (ent == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/*
219 * Called on initialization to set up the /proc/device-tree subtree
220 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400221void __init proc_device_tree_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct device_node *root;
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 proc_device_tree = proc_mkdir("device-tree", NULL);
Michal Simekbcac2b12009-06-17 16:25:59 -0700226 if (proc_device_tree == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return;
228 root = of_find_node_by_path("/");
Michal Simekbcac2b12009-06-17 16:25:59 -0700229 if (root == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 printk(KERN_ERR "/proc/device-tree: can't find root\n");
231 return;
232 }
233 proc_device_tree_add_node(root, proc_device_tree);
234 of_node_put(root);
235}