[SPARC]: Check kzalloc() return value in SUN4D irq/iommu init.
[powerpc.git] / arch / sparc / kernel / prom.c
index 63b2b9b..2cc302b 100644 (file)
 
 static struct device_node *allnodes;
 
+/* use when traversing tree through the allnext, child, sibling,
+ * or parent members of struct device_node.
+ */
+static DEFINE_RWLOCK(devtree_lock);
+
 int of_device_is_compatible(struct device_node *device, const char *compat)
 {
        const char* cp;
@@ -185,6 +190,84 @@ int of_getintprop_default(struct device_node *np, const char *name, int def)
 }
 EXPORT_SYMBOL(of_getintprop_default);
 
+int of_n_addr_cells(struct device_node *np)
+{
+       int* ip;
+       do {
+               if (np->parent)
+                       np = np->parent;
+               ip = of_get_property(np, "#address-cells", NULL);
+               if (ip != NULL)
+                       return *ip;
+       } while (np->parent);
+       /* No #address-cells property for the root node, default to 2 */
+       return 2;
+}
+EXPORT_SYMBOL(of_n_addr_cells);
+
+int of_n_size_cells(struct device_node *np)
+{
+       int* ip;
+       do {
+               if (np->parent)
+                       np = np->parent;
+               ip = of_get_property(np, "#size-cells", NULL);
+               if (ip != NULL)
+                       return *ip;
+       } while (np->parent);
+       /* No #size-cells property for the root node, default to 1 */
+       return 1;
+}
+EXPORT_SYMBOL(of_n_size_cells);
+
+int of_set_property(struct device_node *dp, const char *name, void *val, int len)
+{
+       struct property **prevp;
+       void *new_val;
+       int err;
+
+       new_val = kmalloc(len, GFP_KERNEL);
+       if (!new_val)
+               return -ENOMEM;
+
+       memcpy(new_val, val, len);
+
+       err = -ENODEV;
+
+       write_lock(&devtree_lock);
+       prevp = &dp->properties;
+       while (*prevp) {
+               struct property *prop = *prevp;
+
+               if (!strcmp(prop->name, name)) {
+                       void *old_val = prop->value;
+                       int ret;
+
+                       ret = prom_setprop(dp->node, (char *) name, val, len);
+                       err = -EINVAL;
+                       if (ret >= 0) {
+                               prop->value = new_val;
+                               prop->length = len;
+
+                               if (OF_IS_DYNAMIC(prop))
+                                       kfree(old_val);
+
+                               OF_MARK_DYNAMIC(prop);
+
+                               err = 0;
+                       }
+                       break;
+               }
+               prevp = &(*prevp)->next;
+       }
+       write_unlock(&devtree_lock);
+
+       /* XXX Upate procfs if necessary... */
+
+       return err;
+}
+EXPORT_SYMBOL(of_set_property);
+
 static unsigned int prom_early_allocated;
 
 static void * __init prom_early_alloc(unsigned long size)
@@ -354,35 +437,52 @@ static char * __init build_full_name(struct device_node *dp)
        return n;
 }
 
-static struct property * __init build_one_prop(phandle node, char *prev)
+static unsigned int unique_id;
+
+static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
 {
        static struct property *tmp = NULL;
        struct property *p;
        int len;
+       const char *name;
 
        if (tmp) {
                p = tmp;
                memset(p, 0, sizeof(*p) + 32);
                tmp = NULL;
-       } else
+       } else {
                p = prom_early_alloc(sizeof(struct property) + 32);
+               p->unique_id = unique_id++;
+       }
 
        p->name = (char *) (p + 1);
-       if (prev == NULL) {
-               prom_firstprop(node, p->name);
-       } else {
-               prom_nextprop(node, prev, p->name);
-       }
-       if (strlen(p->name) == 0) {
-               tmp = p;
-               return NULL;
-       }
-       p->length = prom_getproplen(node, p->name);
-       if (p->length <= 0) {
-               p->length = 0;
+       if (special_name) {
+               strcpy(p->name, special_name);
+               p->length = special_len;
+               p->value = prom_early_alloc(special_len);
+               memcpy(p->value, special_val, special_len);
        } else {
-               p->value = prom_early_alloc(p->length);
-               len = prom_getproperty(node, p->name, p->value, p->length);
+               if (prev == NULL) {
+                       name = prom_firstprop(node, NULL);
+               } else {
+                       name = prom_nextprop(node, prev, NULL);
+               }
+               if (strlen(name) == 0) {
+                       tmp = p;
+                       return NULL;
+               }
+               strcpy(p->name, name);
+               p->length = prom_getproplen(node, p->name);
+               if (p->length <= 0) {
+                       p->length = 0;
+               } else {
+                       p->value = prom_early_alloc(p->length + 1);
+                       len = prom_getproperty(node, p->name, p->value,
+                                              p->length);
+                       if (len <= 0)
+                               p->length = 0;
+                       ((unsigned char *)p->value)[p->length] = '\0';
+               }
        }
        return p;
 }
@@ -391,9 +491,14 @@ static struct property * __init build_prop_list(phandle node)
 {
        struct property *head, *tail;
 
-       head = tail = build_one_prop(node, NULL);
+       head = tail = build_one_prop(node, NULL,
+                                    ".node", &node, sizeof(node));
+
+       tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
+       tail = tail->next;
        while(tail) {
-               tail->next = build_one_prop(node, tail->name);
+               tail->next = build_one_prop(node, tail->name,
+                                           NULL, NULL, 0);
                tail = tail->next;
        }
 
@@ -422,6 +527,7 @@ static struct device_node * __init create_node(phandle node)
                return NULL;
 
        dp = prom_early_alloc(sizeof(*dp));
+       dp->unique_id = unique_id++;
 
        kref_init(&dp->kref);