David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 1 | /* prom_common.c: OF device tree support common code. |
| 2 | * |
| 3 | * Paul Mackerras August 1996. |
| 4 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 5 | * |
| 6 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 7 | * {engebret|bergner}@us.ibm.com |
| 8 | * |
| 9 | * Adapted for sparc by David S. Miller davem@davemloft.net |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <asm/prom.h> |
| 24 | #include <asm/oplib.h> |
| 25 | |
| 26 | #include "prom.h" |
| 27 | |
| 28 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 29 | { |
| 30 | struct device_node *np; |
| 31 | |
| 32 | for (np = allnodes; np; np = np->allnext) |
| 33 | if (np->node == handle) |
| 34 | break; |
| 35 | |
| 36 | return np; |
| 37 | } |
| 38 | EXPORT_SYMBOL(of_find_node_by_phandle); |
| 39 | |
| 40 | int of_getintprop_default(struct device_node *np, const char *name, int def) |
| 41 | { |
| 42 | struct property *prop; |
| 43 | int len; |
| 44 | |
| 45 | prop = of_find_property(np, name, &len); |
| 46 | if (!prop || len != 4) |
| 47 | return def; |
| 48 | |
| 49 | return *(int *) prop->value; |
| 50 | } |
| 51 | EXPORT_SYMBOL(of_getintprop_default); |
| 52 | |
| 53 | DEFINE_MUTEX(of_set_property_mutex); |
| 54 | EXPORT_SYMBOL(of_set_property_mutex); |
| 55 | |
| 56 | int of_set_property(struct device_node *dp, const char *name, void *val, int len) |
| 57 | { |
| 58 | struct property **prevp; |
| 59 | void *new_val; |
| 60 | int err; |
| 61 | |
| 62 | new_val = kmalloc(len, GFP_KERNEL); |
| 63 | if (!new_val) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | memcpy(new_val, val, len); |
| 67 | |
| 68 | err = -ENODEV; |
| 69 | |
| 70 | write_lock(&devtree_lock); |
| 71 | prevp = &dp->properties; |
| 72 | while (*prevp) { |
| 73 | struct property *prop = *prevp; |
| 74 | |
| 75 | if (!strcasecmp(prop->name, name)) { |
| 76 | void *old_val = prop->value; |
| 77 | int ret; |
| 78 | |
| 79 | mutex_lock(&of_set_property_mutex); |
| 80 | ret = prom_setprop(dp->node, name, val, len); |
| 81 | mutex_unlock(&of_set_property_mutex); |
| 82 | |
| 83 | err = -EINVAL; |
| 84 | if (ret >= 0) { |
| 85 | prop->value = new_val; |
| 86 | prop->length = len; |
| 87 | |
| 88 | if (OF_IS_DYNAMIC(prop)) |
| 89 | kfree(old_val); |
| 90 | |
| 91 | OF_MARK_DYNAMIC(prop); |
| 92 | |
| 93 | err = 0; |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | prevp = &(*prevp)->next; |
| 98 | } |
| 99 | write_unlock(&devtree_lock); |
| 100 | |
| 101 | /* XXX Upate procfs if necessary... */ |
| 102 | |
| 103 | return err; |
| 104 | } |
| 105 | EXPORT_SYMBOL(of_set_property); |
| 106 | |
| 107 | int of_find_in_proplist(const char *list, const char *match, int len) |
| 108 | { |
| 109 | while (len > 0) { |
| 110 | int l; |
| 111 | |
| 112 | if (!strcmp(list, match)) |
| 113 | return 1; |
| 114 | l = strlen(list) + 1; |
| 115 | list += l; |
| 116 | len -= l; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | EXPORT_SYMBOL(of_find_in_proplist); |
| 121 | |
David S. Miller | e5ff0fe | 2008-12-05 00:50:22 -0800 | [diff] [blame] | 122 | unsigned int prom_unique_id; |
David S. Miller | b9e5567 | 2008-12-05 01:00:46 -0800 | [diff] [blame] | 123 | |
| 124 | static struct property * __init build_one_prop(phandle node, char *prev, |
| 125 | char *special_name, |
| 126 | void *special_val, |
| 127 | int special_len) |
| 128 | { |
| 129 | static struct property *tmp = NULL; |
| 130 | struct property *p; |
| 131 | const char *name; |
| 132 | |
| 133 | if (tmp) { |
| 134 | p = tmp; |
| 135 | memset(p, 0, sizeof(*p) + 32); |
| 136 | tmp = NULL; |
| 137 | } else { |
| 138 | p = prom_early_alloc(sizeof(struct property) + 32); |
| 139 | p->unique_id = prom_unique_id++; |
| 140 | } |
| 141 | |
| 142 | p->name = (char *) (p + 1); |
| 143 | if (special_name) { |
| 144 | strcpy(p->name, special_name); |
| 145 | p->length = special_len; |
| 146 | p->value = prom_early_alloc(special_len); |
| 147 | memcpy(p->value, special_val, special_len); |
| 148 | } else { |
| 149 | #ifdef CONFIG_SPARC32 |
| 150 | if (prev == NULL) { |
| 151 | name = prom_firstprop(node, NULL); |
| 152 | } else { |
| 153 | name = prom_nextprop(node, prev, NULL); |
| 154 | } |
| 155 | #else |
| 156 | if (prev == NULL) { |
| 157 | prom_firstprop(node, p->name); |
| 158 | } else { |
| 159 | prom_nextprop(node, prev, p->name); |
| 160 | } |
| 161 | name = p->name; |
| 162 | #endif |
| 163 | if (strlen(name) == 0) { |
| 164 | tmp = p; |
| 165 | return NULL; |
| 166 | } |
| 167 | #ifdef CONFIG_SPARC32 |
| 168 | strcpy(p->name, name); |
| 169 | #endif |
| 170 | p->length = prom_getproplen(node, p->name); |
| 171 | if (p->length <= 0) { |
| 172 | p->length = 0; |
| 173 | } else { |
| 174 | int len; |
| 175 | |
| 176 | p->value = prom_early_alloc(p->length + 1); |
| 177 | len = prom_getproperty(node, p->name, p->value, |
| 178 | p->length); |
| 179 | if (len <= 0) |
| 180 | p->length = 0; |
| 181 | ((unsigned char *)p->value)[p->length] = '\0'; |
| 182 | } |
| 183 | } |
| 184 | return p; |
| 185 | } |
| 186 | |
David S. Miller | 7d9439d | 2008-12-05 01:10:18 -0800 | [diff] [blame] | 187 | static struct property * __init build_prop_list(phandle node) |
David S. Miller | b9e5567 | 2008-12-05 01:00:46 -0800 | [diff] [blame] | 188 | { |
| 189 | struct property *head, *tail; |
| 190 | |
| 191 | head = tail = build_one_prop(node, NULL, |
| 192 | ".node", &node, sizeof(node)); |
| 193 | |
| 194 | tail->next = build_one_prop(node, NULL, NULL, NULL, 0); |
| 195 | tail = tail->next; |
| 196 | while(tail) { |
| 197 | tail->next = build_one_prop(node, tail->name, |
| 198 | NULL, NULL, 0); |
| 199 | tail = tail->next; |
| 200 | } |
| 201 | |
| 202 | return head; |
| 203 | } |
David S. Miller | 7d9439d | 2008-12-05 01:10:18 -0800 | [diff] [blame] | 204 | |
| 205 | static char * __init get_one_property(phandle node, const char *name) |
| 206 | { |
| 207 | char *buf = "<NULL>"; |
| 208 | int len; |
| 209 | |
| 210 | len = prom_getproplen(node, name); |
| 211 | if (len > 0) { |
| 212 | buf = prom_early_alloc(len); |
| 213 | len = prom_getproperty(node, name, buf, len); |
| 214 | } |
| 215 | |
| 216 | return buf; |
| 217 | } |
| 218 | |
David S. Miller | 6524036 | 2008-12-05 01:21:41 -0800 | [diff] [blame^] | 219 | struct device_node * __init prom_create_node(phandle node, |
| 220 | struct device_node *parent) |
David S. Miller | 7d9439d | 2008-12-05 01:10:18 -0800 | [diff] [blame] | 221 | { |
| 222 | struct device_node *dp; |
| 223 | |
| 224 | if (!node) |
| 225 | return NULL; |
| 226 | |
| 227 | dp = prom_early_alloc(sizeof(*dp)); |
| 228 | dp->unique_id = prom_unique_id++; |
| 229 | dp->parent = parent; |
| 230 | |
| 231 | kref_init(&dp->kref); |
| 232 | |
| 233 | dp->name = get_one_property(node, "name"); |
| 234 | dp->type = get_one_property(node, "device_type"); |
| 235 | dp->node = node; |
| 236 | |
| 237 | /* Build interrupts later... */ |
| 238 | |
| 239 | dp->properties = build_prop_list(node); |
| 240 | |
| 241 | return dp; |
| 242 | } |
David S. Miller | 6524036 | 2008-12-05 01:21:41 -0800 | [diff] [blame^] | 243 | |
| 244 | static char * __init build_full_name(struct device_node *dp) |
| 245 | { |
| 246 | int len, ourlen, plen; |
| 247 | char *n; |
| 248 | |
| 249 | plen = strlen(dp->parent->full_name); |
| 250 | ourlen = strlen(dp->path_component_name); |
| 251 | len = ourlen + plen + 2; |
| 252 | |
| 253 | n = prom_early_alloc(len); |
| 254 | strcpy(n, dp->parent->full_name); |
| 255 | if (!is_root_node(dp->parent)) { |
| 256 | strcpy(n + plen, "/"); |
| 257 | plen++; |
| 258 | } |
| 259 | strcpy(n + plen, dp->path_component_name); |
| 260 | |
| 261 | return n; |
| 262 | } |
| 263 | |
| 264 | struct device_node * __init prom_build_tree(struct device_node *parent, |
| 265 | phandle node, |
| 266 | struct device_node ***nextp) |
| 267 | { |
| 268 | struct device_node *ret = NULL, *prev_sibling = NULL; |
| 269 | struct device_node *dp; |
| 270 | |
| 271 | while (1) { |
| 272 | dp = prom_create_node(node, parent); |
| 273 | if (!dp) |
| 274 | break; |
| 275 | |
| 276 | if (prev_sibling) |
| 277 | prev_sibling->sibling = dp; |
| 278 | |
| 279 | if (!ret) |
| 280 | ret = dp; |
| 281 | prev_sibling = dp; |
| 282 | |
| 283 | *(*nextp) = dp; |
| 284 | *nextp = &dp->allnext; |
| 285 | |
| 286 | dp->path_component_name = build_path_component(dp); |
| 287 | dp->full_name = build_full_name(dp); |
| 288 | |
| 289 | dp->child = prom_build_tree(dp, prom_getchild(node), nextp); |
| 290 | |
| 291 | node = prom_getsibling(node); |
| 292 | } |
| 293 | |
| 294 | return ret; |
| 295 | } |