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> |
| 7 | #include <linux/time.h> |
| 8 | #include <linux/proc_fs.h> |
| 9 | #include <linux/stat.h> |
| 10 | #include <linux/string.h> |
| 11 | #include <asm/prom.h> |
| 12 | #include <asm/uaccess.h> |
| 13 | |
| 14 | #ifndef HAVE_ARCH_DEVTREE_FIXUPS |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 15 | static inline void set_node_proc_entry(struct device_node *np, |
| 16 | struct proc_dir_entry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | #endif |
| 20 | |
| 21 | static struct proc_dir_entry *proc_device_tree; |
| 22 | |
| 23 | /* |
| 24 | * Supply data on a read from /proc/device-tree/node/property. |
| 25 | */ |
| 26 | static int property_read_proc(char *page, char **start, off_t off, |
| 27 | int count, int *eof, void *data) |
| 28 | { |
| 29 | struct property *pp = data; |
| 30 | int n; |
| 31 | |
| 32 | if (off >= pp->length) { |
| 33 | *eof = 1; |
| 34 | return 0; |
| 35 | } |
| 36 | n = pp->length - off; |
| 37 | if (n > count) |
| 38 | n = count; |
| 39 | else |
| 40 | *eof = 1; |
| 41 | memcpy(page, pp->value + off, n); |
| 42 | *start = page; |
| 43 | return n; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * For a node with a name like "gc@10", we make symlinks called "gc" |
| 48 | * and "@10" to it. |
| 49 | */ |
| 50 | |
| 51 | /* |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 52 | * Add a property to a node |
| 53 | */ |
| 54 | static struct proc_dir_entry * |
| 55 | __proc_device_tree_add_prop(struct proc_dir_entry *de, struct property *pp) |
| 56 | { |
| 57 | struct proc_dir_entry *ent; |
| 58 | |
| 59 | /* |
| 60 | * Unfortunately proc_register puts each new entry |
| 61 | * at the beginning of the list. So we rearrange them. |
| 62 | */ |
| 63 | ent = create_proc_read_entry(pp->name, |
| 64 | strncmp(pp->name, "security-", 9) |
| 65 | ? S_IRUGO : S_IRUSR, de, |
| 66 | property_read_proc, pp); |
| 67 | if (ent == NULL) |
| 68 | return NULL; |
| 69 | |
| 70 | if (!strncmp(pp->name, "security-", 9)) |
| 71 | ent->size = 0; /* don't leak number of password chars */ |
| 72 | else |
| 73 | ent->size = pp->length; |
| 74 | |
| 75 | return ent; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop) |
| 80 | { |
| 81 | __proc_device_tree_add_prop(pde, prop); |
| 82 | } |
| 83 | |
Dave C Boutcher | 898b539 | 2006-01-12 16:07:17 -0600 | [diff] [blame] | 84 | void proc_device_tree_remove_prop(struct proc_dir_entry *pde, |
| 85 | struct property *prop) |
| 86 | { |
| 87 | remove_proc_entry(prop->name, pde); |
| 88 | } |
| 89 | |
| 90 | void proc_device_tree_update_prop(struct proc_dir_entry *pde, |
| 91 | struct property *newprop, |
| 92 | struct property *oldprop) |
| 93 | { |
| 94 | struct proc_dir_entry *ent; |
| 95 | |
| 96 | for (ent = pde->subdir; ent != NULL; ent = ent->next) |
| 97 | if (ent->data == oldprop) |
| 98 | break; |
| 99 | if (ent == NULL) { |
| 100 | printk(KERN_WARNING "device-tree: property \"%s\" " |
| 101 | " does not exist\n", oldprop->name); |
| 102 | } else { |
| 103 | ent->data = newprop; |
| 104 | ent->size = newprop->length; |
| 105 | } |
| 106 | } |
| 107 | |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 108 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * Process a node, adding entries for its children and its properties. |
| 110 | */ |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 111 | void proc_device_tree_add_node(struct device_node *np, |
| 112 | struct proc_dir_entry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | struct property *pp; |
| 115 | struct proc_dir_entry *ent; |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 116 | struct device_node *child; |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 117 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | set_node_proc_entry(np, de); |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 120 | for (child = NULL; (child = of_get_next_child(np, child));) { |
| 121 | p = strrchr(child->full_name, '/'); |
| 122 | if (!p) |
| 123 | p = child->full_name; |
| 124 | else |
| 125 | ++p; |
| 126 | ent = proc_mkdir(p, de); |
| 127 | if (ent == 0) |
| 128 | break; |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 129 | proc_device_tree_add_node(child, ent); |
| 130 | } |
| 131 | of_node_put(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 133 | /* |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 134 | * Yet another Apple device-tree bogosity: on some machines, |
| 135 | * they have properties & nodes with the same name. Those |
| 136 | * properties are quite unimportant for us though, thus we |
| 137 | * simply "skip" them here, but we do have to check. |
| 138 | */ |
Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 139 | spin_lock(&proc_subdir_lock); |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 140 | for (ent = de->subdir; ent != NULL; ent = ent->next) |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 141 | if (!strcmp(ent->name, pp->name)) |
| 142 | break; |
Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 143 | spin_unlock(&proc_subdir_lock); |
Benjamin Herrenschmidt | 5f64f73 | 2005-06-01 17:07:27 +1000 | [diff] [blame] | 144 | if (ent != NULL) { |
| 145 | printk(KERN_WARNING "device-tree: property \"%s\" name" |
| 146 | " conflicts with node in %s\n", pp->name, |
| 147 | np->full_name); |
| 148 | continue; |
| 149 | } |
| 150 | |
Benjamin Herrenschmidt | 183d020 | 2005-11-07 14:29:02 +1100 | [diff] [blame] | 151 | ent = __proc_device_tree_add_prop(de, pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (ent == 0) |
| 153 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Called on initialization to set up the /proc/device-tree subtree |
| 159 | */ |
| 160 | void proc_device_tree_init(void) |
| 161 | { |
| 162 | struct device_node *root; |
| 163 | if ( !have_of ) |
| 164 | return; |
| 165 | proc_device_tree = proc_mkdir("device-tree", NULL); |
| 166 | if (proc_device_tree == 0) |
| 167 | return; |
| 168 | root = of_find_node_by_path("/"); |
| 169 | if (root == 0) { |
| 170 | printk(KERN_ERR "/proc/device-tree: can't find root\n"); |
| 171 | return; |
| 172 | } |
| 173 | proc_device_tree_add_node(root, proc_device_tree); |
| 174 | of_node_put(root); |
| 175 | } |