blob: 30b590f5bd356f088e0df909c0adcf44aae3f801 [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>
Andrew Morton87ebdc02013-02-27 17:03:16 -080011#include <linux/printk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/stat.h>
13#include <linux/string.h>
Jeremy Kerr50ab2fe2010-02-01 21:34:14 -070014#include <linux/of.h>
Jeremy Kerr7c540d92010-02-14 07:13:41 -070015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/prom.h>
18#include <asm/uaccess.h>
Al Viro3174c212009-04-07 13:19:18 -040019#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +100021static inline void set_node_proc_entry(struct device_node *np,
22 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070024#ifdef HAVE_ARCH_DEVTREE_FIXUPS
25 np->pde = de;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#endif
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070027}
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static struct proc_dir_entry *proc_device_tree;
30
31/*
32 * Supply data on a read from /proc/device-tree/node/property.
33 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000034static int property_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Alexey Dobriyane22f6282009-09-06 23:31:25 +000036 struct property *pp = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alexey Dobriyane22f6282009-09-06 23:31:25 +000038 seq_write(m, pp->value, pp->length);
39 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Alexey Dobriyane22f6282009-09-06 23:31:25 +000042static int property_proc_open(struct inode *inode, struct file *file)
43{
44 return single_open(file, property_proc_show, PDE(inode)->data);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * For a node with a name like "gc@10", we make symlinks called "gc"
57 * and "@10" to it.
58 */
59
60/*
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110061 * Add a property to a node
62 */
63static struct proc_dir_entry *
Michael Ellerman5149fa42006-03-27 14:26:26 +110064__proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
65 const char *name)
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110066{
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 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000073 ent = proc_create_data(name,
74 strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
75 de, &property_proc_fops, pp);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110076 if (ent == NULL)
77 return NULL;
78
Michael Ellerman5149fa42006-03-27 14:26:26 +110079 if (!strncmp(name, "security-", 9))
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110080 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{
Michael Ellerman5149fa42006-03-27 14:26:26 +110090 __proc_device_tree_add_prop(pde, prop, prop->name);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110091}
92
Dave C Boutcher898b5392006-01-12 16:07:17 -060093void 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
Dong Aisheng475d0092012-07-11 15:16:37 +1000105 if (!oldprop) {
106 proc_device_tree_add_prop(pde, newprop);
107 return;
108 }
109
Dave C Boutcher898b5392006-01-12 16:07:17 -0600110 for (ent = pde->subdir; ent != NULL; ent = ent->next)
111 if (ent->data == oldprop)
112 break;
113 if (ent == NULL) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800114 pr_warn("device-tree: property \"%s\" does not exist\n",
115 oldprop->name);
Dave C Boutcher898b5392006-01-12 16:07:17 -0600116 } else {
117 ent->data = newprop;
118 ent->size = newprop->length;
119 }
120}
121
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100122/*
Michael Ellerman5149fa42006-03-27 14:26:26 +1100123 * 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) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800157 pr_err("device-tree: Out of memory trying to fixup "
158 "name \"%s\"\n", name);
Michael Ellerman5149fa42006-03-27 14:26:26 +1100159 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
Andrew Morton87ebdc02013-02-27 17:03:16 -0800179 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
180 np->full_name, fixed_name);
Michael Ellerman5149fa42006-03-27 14:26:26 +1100181
182 return fixed_name;
183}
184
185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * Process a node, adding entries for its children and its properties.
187 */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000188void proc_device_tree_add_node(struct device_node *np,
189 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct property *pp;
192 struct proc_dir_entry *ent;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000193 struct device_node *child;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000194 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 set_node_proc_entry(np, de);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000197 for (child = NULL; (child = of_get_next_child(np, child));) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100198 /* Use everything after the last slash, or the full name */
Andy Shevchenkof9a00e82012-12-17 16:01:25 -0800199 p = kbasename(child->full_name);
Michael Ellerman5149fa42006-03-27 14:26:26 +1100200
201 if (duplicate_name(de, p))
202 p = fixup_name(np, de, p);
203
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000204 ent = proc_mkdir(p, de);
Michal Simekbcac2b12009-06-17 16:25:59 -0700205 if (ent == NULL)
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000206 break;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000207 proc_device_tree_add_node(child, ent);
208 }
209 of_node_put(child);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000210
Michal Simekbcac2b12009-06-17 16:25:59 -0700211 for (pp = np->properties; pp != NULL; pp = pp->next) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100212 p = pp->name;
213
Michael Ellerman9f069af2010-05-19 02:32:29 +0000214 if (strchr(p, '/'))
215 continue;
216
Michael Ellerman5149fa42006-03-27 14:26:26 +1100217 if (duplicate_name(de, p))
218 p = fixup_name(np, de, p);
219
220 ent = __proc_device_tree_add_prop(de, pp, p);
Michal Simekbcac2b12009-06-17 16:25:59 -0700221 if (ent == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/*
227 * Called on initialization to set up the /proc/device-tree subtree
228 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400229void __init proc_device_tree_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct device_node *root;
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 proc_device_tree = proc_mkdir("device-tree", NULL);
Michal Simekbcac2b12009-06-17 16:25:59 -0700234 if (proc_device_tree == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return;
236 root = of_find_node_by_path("/");
Michal Simekbcac2b12009-06-17 16:25:59 -0700237 if (root == NULL) {
Paul Bolle8aaccf72011-02-14 22:34:22 +0100238 pr_debug("/proc/device-tree: can't find root\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return;
240 }
241 proc_device_tree_add_node(root, proc_device_tree);
242 of_node_put(root);
243}