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