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; |