| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * proc_devtree.c - handles /proc/device-tree | 
 | 3 |  * | 
 | 4 |  * Copyright 1997 Paul Mackerras | 
 | 5 |  */ | 
 | 6 | #include <linux/errno.h> | 
| Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 7 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/time.h> | 
 | 9 | #include <linux/proc_fs.h> | 
| Alexey Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 10 | #include <linux/seq_file.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/stat.h> | 
 | 12 | #include <linux/string.h> | 
| Jeremy Kerr | 50ab2fe | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 13 | #include <linux/of.h> | 
| Jeremy Kerr | 7c540d9 | 2010-02-14 07:13:41 -0700 | [diff] [blame] | 14 | #include <linux/module.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/prom.h> | 
 | 17 | #include <asm/uaccess.h> | 
| Al Viro | 3174c21 | 2009-04-07 13:19:18 -0400 | [diff] [blame] | 18 | #include "internal.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 20 | static inline void set_node_proc_entry(struct device_node *np, | 
 | 21 | 				       struct proc_dir_entry *de) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { | 
| Jeremy Kerr | 8cfb334 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 23 | #ifdef HAVE_ARCH_DEVTREE_FIXUPS | 
 | 24 | 	np->pde = de; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #endif | 
| Jeremy Kerr | 8cfb334 | 2010-02-01 21:34:14 -0700 | [diff] [blame] | 26 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
 | 28 | static struct proc_dir_entry *proc_device_tree; | 
 | 29 |  | 
 | 30 | /* | 
 | 31 |  * Supply data on a read from /proc/device-tree/node/property. | 
 | 32 |  */ | 
| Alexey Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 33 | static int property_proc_show(struct seq_file *m, void *v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { | 
| Alexey Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 35 | 	struct property *pp = m->private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
| Alexey Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 37 | 	seq_write(m, pp->value, pp->length); | 
 | 38 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } | 
 | 40 |  | 
| Alexey Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 41 | static int property_proc_open(struct inode *inode, struct file *file) | 
 | 42 | { | 
 | 43 | 	return single_open(file, property_proc_show, PDE(inode)->data); | 
 | 44 | } | 
 | 45 |  | 
 | 46 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* | 
 | 55 |  * For a node with a name like "gc@10", we make symlinks called "gc" | 
 | 56 |  * and "@10" to it. | 
 | 57 |  */ | 
 | 58 |  | 
 | 59 | /* | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 60 |  * Add a property to a node | 
 | 61 |  */ | 
 | 62 | static struct proc_dir_entry * | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 63 | __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp, | 
 | 64 | 		const char *name) | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 65 | { | 
 | 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 Dobriyan | e22f628 | 2009-09-06 23:31:25 +0000 | [diff] [blame] | 72 | 	ent = proc_create_data(name, | 
 | 73 | 			       strncmp(name, "security-", 9) ? S_IRUGO : S_IRUSR, | 
 | 74 | 			       de, &property_proc_fops, pp); | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 75 | 	if (ent == NULL) | 
 | 76 | 		return NULL; | 
 | 77 |  | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 78 | 	if (!strncmp(name, "security-", 9)) | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 79 | 		ent->size = 0; /* don't leak number of password chars */ | 
 | 80 | 	else | 
 | 81 | 		ent->size = pp->length; | 
 | 82 |  | 
 | 83 | 	return ent; | 
 | 84 | } | 
 | 85 |  | 
 | 86 |  | 
 | 87 | void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop) | 
 | 88 | { | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 89 | 	__proc_device_tree_add_prop(pde, prop, prop->name); | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 90 | } | 
 | 91 |  | 
| Dave C Boutcher | 898b539 | 2006-01-12 16:07:17 -0600 | [diff] [blame] | 92 | void proc_device_tree_remove_prop(struct proc_dir_entry *pde, | 
 | 93 | 				  struct property *prop) | 
 | 94 | { | 
 | 95 | 	remove_proc_entry(prop->name, pde); | 
 | 96 | } | 
 | 97 |  | 
 | 98 | void 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 |  | 
 | 104 | 	for (ent = pde->subdir; ent != NULL; ent = ent->next) | 
 | 105 | 		if (ent->data == oldprop) | 
 | 106 | 			break; | 
 | 107 | 	if (ent == NULL) { | 
 | 108 | 		printk(KERN_WARNING "device-tree: property \"%s\" " | 
 | 109 | 		       " does not exist\n", oldprop->name); | 
 | 110 | 	} else { | 
 | 111 | 		ent->data = newprop; | 
 | 112 | 		ent->size = newprop->length; | 
 | 113 | 	} | 
 | 114 | } | 
 | 115 |  | 
| Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 116 | /* | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 117 |  * Various dodgy firmware might give us nodes and/or properties with | 
 | 118 |  * conflicting names. That's generally ok, except for exporting via /proc, | 
 | 119 |  * so munge names here to ensure they're unique. | 
 | 120 |  */ | 
 | 121 |  | 
 | 122 | static int duplicate_name(struct proc_dir_entry *de, const char *name) | 
 | 123 | { | 
 | 124 | 	struct proc_dir_entry *ent; | 
 | 125 | 	int found = 0; | 
 | 126 |  | 
 | 127 | 	spin_lock(&proc_subdir_lock); | 
 | 128 |  | 
 | 129 | 	for (ent = de->subdir; ent != NULL; ent = ent->next) { | 
 | 130 | 		if (strcmp(ent->name, name) == 0) { | 
 | 131 | 			found = 1; | 
 | 132 | 			break; | 
 | 133 | 		} | 
 | 134 | 	} | 
 | 135 |  | 
 | 136 | 	spin_unlock(&proc_subdir_lock); | 
 | 137 |  | 
 | 138 | 	return found; | 
 | 139 | } | 
 | 140 |  | 
 | 141 | static const char *fixup_name(struct device_node *np, struct proc_dir_entry *de, | 
 | 142 | 		const char *name) | 
 | 143 | { | 
 | 144 | 	char *fixed_name; | 
 | 145 | 	int fixup_len = strlen(name) + 2 + 1; /* name + #x + \0 */ | 
 | 146 | 	int i = 1, size; | 
 | 147 |  | 
 | 148 | realloc: | 
 | 149 | 	fixed_name = kmalloc(fixup_len, GFP_KERNEL); | 
 | 150 | 	if (fixed_name == NULL) { | 
 | 151 | 		printk(KERN_ERR "device-tree: Out of memory trying to fixup " | 
 | 152 | 				"name \"%s\"\n", name); | 
 | 153 | 		return name; | 
 | 154 | 	} | 
 | 155 |  | 
 | 156 | retry: | 
 | 157 | 	size = snprintf(fixed_name, fixup_len, "%s#%d", name, i); | 
 | 158 | 	size++; /* account for NULL */ | 
 | 159 |  | 
 | 160 | 	if (size > fixup_len) { | 
 | 161 | 		/* We ran out of space, free and reallocate. */ | 
 | 162 | 		kfree(fixed_name); | 
 | 163 | 		fixup_len = size; | 
 | 164 | 		goto realloc; | 
 | 165 | 	} | 
 | 166 |  | 
 | 167 | 	if (duplicate_name(de, fixed_name)) { | 
 | 168 | 		/* Multiple duplicates. Retry with a different offset. */ | 
 | 169 | 		i++; | 
 | 170 | 		goto retry; | 
 | 171 | 	} | 
 | 172 |  | 
 | 173 | 	printk(KERN_WARNING "device-tree: Duplicate name in %s, " | 
 | 174 | 			"renamed to \"%s\"\n", np->full_name, fixed_name); | 
 | 175 |  | 
 | 176 | 	return fixed_name; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  * Process a node, adding entries for its children and its properties. | 
 | 181 |  */ | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 182 | void proc_device_tree_add_node(struct device_node *np, | 
 | 183 | 			       struct proc_dir_entry *de) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { | 
 | 185 | 	struct property *pp; | 
 | 186 | 	struct proc_dir_entry *ent; | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 187 | 	struct device_node *child; | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 188 | 	const char *p; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 |  | 
 | 190 | 	set_node_proc_entry(np, de); | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 191 | 	for (child = NULL; (child = of_get_next_child(np, child));) { | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 192 | 		/* Use everything after the last slash, or the full name */ | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 193 | 		p = strrchr(child->full_name, '/'); | 
 | 194 | 		if (!p) | 
 | 195 | 			p = child->full_name; | 
 | 196 | 		else | 
 | 197 | 			++p; | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 198 |  | 
 | 199 | 		if (duplicate_name(de, p)) | 
 | 200 | 			p = fixup_name(np, de, p); | 
 | 201 |  | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 202 | 		ent = proc_mkdir(p, de); | 
| Michal Simek | bcac2b1 | 2009-06-17 16:25:59 -0700 | [diff] [blame] | 203 | 		if (ent == NULL) | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 204 | 			break; | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 205 | 		proc_device_tree_add_node(child, ent); | 
 | 206 | 	} | 
 | 207 | 	of_node_put(child); | 
| Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 208 |  | 
| Michal Simek | bcac2b1 | 2009-06-17 16:25:59 -0700 | [diff] [blame] | 209 | 	for (pp = np->properties; pp != NULL; pp = pp->next) { | 
| Michael Ellerman | 5149fa4 | 2006-03-27 14:26:26 +1100 | [diff] [blame] | 210 | 		p = pp->name; | 
 | 211 |  | 
 | 212 | 		if (duplicate_name(de, p)) | 
 | 213 | 			p = fixup_name(np, de, p); | 
 | 214 |  | 
 | 215 | 		ent = __proc_device_tree_add_prop(de, pp, p); | 
| Michal Simek | bcac2b1 | 2009-06-17 16:25:59 -0700 | [diff] [blame] | 216 | 		if (ent == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } | 
 | 220 |  | 
 | 221 | /* | 
 | 222 |  * Called on initialization to set up the /proc/device-tree subtree | 
 | 223 |  */ | 
| Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 224 | void __init proc_device_tree_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { | 
 | 226 | 	struct device_node *root; | 
| Anton Vorontsov | 6b82b3e | 2008-12-09 09:47:29 +0000 | [diff] [blame] | 227 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | 	proc_device_tree = proc_mkdir("device-tree", NULL); | 
| Michal Simek | bcac2b1 | 2009-06-17 16:25:59 -0700 | [diff] [blame] | 229 | 	if (proc_device_tree == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | 		return; | 
 | 231 | 	root = of_find_node_by_path("/"); | 
| Michal Simek | bcac2b1 | 2009-06-17 16:25:59 -0700 | [diff] [blame] | 232 | 	if (root == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | 		printk(KERN_ERR "/proc/device-tree: can't find root\n"); | 
 | 234 | 		return; | 
 | 235 | 	} | 
 | 236 | 	proc_device_tree_add_node(root, proc_device_tree); | 
 | 237 | 	of_node_put(root); | 
 | 238 | } |