blob: 70779b2fc2090b98b741d10f3f37c4a8c1379bf9 [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>
Syam Sidhardhan75fc0cf2013-02-15 02:24:32 +053015#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 np->pde = de;
Jeremy Kerr8cfb3342010-02-01 21:34:14 -070024}
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static struct proc_dir_entry *proc_device_tree;
27
28/*
29 * Supply data on a read from /proc/device-tree/node/property.
30 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000031static int property_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Alexey Dobriyane22f6282009-09-06 23:31:25 +000033 struct property *pp = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Alexey Dobriyane22f6282009-09-06 23:31:25 +000035 seq_write(m, pp->value, pp->length);
36 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Alexey Dobriyane22f6282009-09-06 23:31:25 +000039static int property_proc_open(struct inode *inode, struct file *file)
40{
David Howellsc30480b2013-04-12 18:03:36 +010041 return single_open(file, property_proc_show, __PDE_DATA(inode));
Alexey Dobriyane22f6282009-09-06 23:31:25 +000042}
43
44static const struct file_operations property_proc_fops = {
45 .owner = THIS_MODULE,
46 .open = property_proc_open,
47 .read = seq_read,
48 .llseek = seq_lseek,
49 .release = single_release,
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * For a node with a name like "gc@10", we make symlinks called "gc"
54 * and "@10" to it.
55 */
56
57/*
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110058 * Add a property to a node
59 */
60static struct proc_dir_entry *
Michael Ellerman5149fa42006-03-27 14:26:26 +110061__proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp,
62 const char *name)
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110063{
64 struct proc_dir_entry *ent;
65
66 /*
67 * Unfortunately proc_register puts each new entry
68 * at the beginning of the list. So we rearrange them.
69 */
Alexey Dobriyane22f6282009-09-06 23:31:25 +000070 ent = proc_create_data(name,
71 strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR,
72 de, &property_proc_fops, pp);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110073 if (ent == NULL)
74 return NULL;
75
Michael Ellerman5149fa42006-03-27 14:26:26 +110076 if (!strncmp(name, "security-", 9))
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110077 ent->size = 0; /* don't leak number of password chars */
78 else
79 ent->size = pp->length;
80
81 return ent;
82}
83
84
85void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop)
86{
Michael Ellerman5149fa42006-03-27 14:26:26 +110087 __proc_device_tree_add_prop(pde, prop, prop->name);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110088}
89
Dave C Boutcher898b5392006-01-12 16:07:17 -060090void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
91 struct property *prop)
92{
93 remove_proc_entry(prop->name, pde);
94}
95
96void proc_device_tree_update_prop(struct proc_dir_entry *pde,
97 struct property *newprop,
98 struct property *oldprop)
99{
100 struct proc_dir_entry *ent;
101
Dong Aisheng475d0092012-07-11 15:16:37 +1000102 if (!oldprop) {
103 proc_device_tree_add_prop(pde, newprop);
104 return;
105 }
106
Dave C Boutcher898b5392006-01-12 16:07:17 -0600107 for (ent = pde->subdir; ent != NULL; ent = ent->next)
108 if (ent->data == oldprop)
109 break;
110 if (ent == NULL) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800111 pr_warn("device-tree: property \"%s\" does not exist\n",
112 oldprop->name);
Dave C Boutcher898b5392006-01-12 16:07:17 -0600113 } else {
114 ent->data = newprop;
115 ent->size = newprop->length;
116 }
117}
118
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100119/*
Michael Ellerman5149fa42006-03-27 14:26:26 +1100120 * Various dodgy firmware might give us nodes and/or properties with
121 * conflicting names. That's generally ok, except for exporting via /proc,
122 * so munge names here to ensure they're unique.
123 */
124
125static int duplicate_name(struct proc_dir_entry *de, const char *name)
126{
127 struct proc_dir_entry *ent;
128 int found = 0;
129
130 spin_lock(&proc_subdir_lock);
131
132 for (ent = de->subdir; ent != NULL; ent = ent->next) {
133 if (strcmp(ent->name, name) == 0) {
134 found = 1;
135 break;
136 }
137 }
138
139 spin_unlock(&proc_subdir_lock);
140
141 return found;
142}
143
144static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de,
145 const char *name)
146{
147 char *fixed_name;
148 int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */
149 int i = 1, size;
150
151realloc:
152 fixed_name = kmalloc(fixup_len, GFP_KERNEL);
153 if (fixed_name == NULL) {
Andrew Morton87ebdc02013-02-27 17:03:16 -0800154 pr_err("device-tree: Out of memory trying to fixup "
155 "name \"%s\"\n", name);
Michael Ellerman5149fa42006-03-27 14:26:26 +1100156 return name;
157 }
158
159retry:
160 size = snprintf(fixed_name, fixup_len, "%s#%d", name, i);
161 size++; /* account for NULL */
162
163 if (size > fixup_len) {
164 /* We ran out of space, free and reallocate. */
165 kfree(fixed_name);
166 fixup_len = size;
167 goto realloc;
168 }
169
170 if (duplicate_name(de, fixed_name)) {
171 /* Multiple duplicates. Retry with a different offset. */
172 i++;
173 goto retry;
174 }
175
Andrew Morton87ebdc02013-02-27 17:03:16 -0800176 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
177 np->full_name, fixed_name);
Michael Ellerman5149fa42006-03-27 14:26:26 +1100178
179 return fixed_name;
180}
181
182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Process a node, adding entries for its children and its properties.
184 */
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000185void proc_device_tree_add_node(struct device_node *np,
186 struct proc_dir_entry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct property *pp;
189 struct proc_dir_entry *ent;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000190 struct device_node *child;
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000191 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 set_node_proc_entry(np, de);
Benjamin Herrenschmidt5f64f732005-06-01 17:07:27 +1000194 for (child = NULL; (child = of_get_next_child(np, child));) {
Michael Ellerman5149fa42006-03-27 14:26:26 +1100195 /* Use everything after the last slash, or the full name */
Andy Shevchenkof9a00e82012-12-17 16:01:25 -0800196 p = kbasename(child->full_name);
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
Michael Ellerman9f069af2010-05-19 02:32:29 +0000211 if (strchr(p, '/'))
212 continue;
213
Michael Ellerman5149fa42006-03-27 14:26:26 +1100214 if (duplicate_name(de, p))
215 p = fixup_name(np, de, p);
216
217 ent = __proc_device_tree_add_prop(de, pp, p);
Michal Simekbcac2b12009-06-17 16:25:59 -0700218 if (ent == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/*
224 * Called on initialization to set up the /proc/device-tree subtree
225 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +0400226void __init proc_device_tree_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct device_node *root;
Anton Vorontsov6b82b3e2008-12-09 09:47:29 +0000229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 proc_device_tree = proc_mkdir("device-tree", NULL);
Michal Simekbcac2b12009-06-17 16:25:59 -0700231 if (proc_device_tree == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233 root = of_find_node_by_path("/");
Michal Simekbcac2b12009-06-17 16:25:59 -0700234 if (root == NULL) {
Paul Bolle8aaccf72011-02-14 22:34:22 +0100235 pr_debug("/proc/device-tree: can't find root\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return;
237 }
238 proc_device_tree_add_node(root, proc_device_tree);
239 of_node_put(root);
240}