diff options
Diffstat (limited to 'drivers/of/base.c')
| -rw-r--r-- | drivers/of/base.c | 318 |
1 files changed, 312 insertions, 6 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c index e6627b2320f1..cb96888d1427 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c | |||
| @@ -20,8 +20,10 @@ | |||
| 20 | #include <linux/module.h> | 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> | 21 | #include <linux/of.h> |
| 22 | #include <linux/spinlock.h> | 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/proc_fs.h> | ||
| 23 | 24 | ||
| 24 | struct device_node *allnodes; | 25 | struct device_node *allnodes; |
| 26 | struct device_node *of_chosen; | ||
| 25 | 27 | ||
| 26 | /* use when traversing tree through the allnext, child, sibling, | 28 | /* use when traversing tree through the allnext, child, sibling, |
| 27 | * or parent members of struct device_node. | 29 | * or parent members of struct device_node. |
| @@ -37,7 +39,7 @@ int of_n_addr_cells(struct device_node *np) | |||
| 37 | np = np->parent; | 39 | np = np->parent; |
| 38 | ip = of_get_property(np, "#address-cells", NULL); | 40 | ip = of_get_property(np, "#address-cells", NULL); |
| 39 | if (ip) | 41 | if (ip) |
| 40 | return *ip; | 42 | return be32_to_cpup(ip); |
| 41 | } while (np->parent); | 43 | } while (np->parent); |
| 42 | /* No #address-cells property for the root node */ | 44 | /* No #address-cells property for the root node */ |
| 43 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; | 45 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| @@ -53,13 +55,88 @@ int of_n_size_cells(struct device_node *np) | |||
| 53 | np = np->parent; | 55 | np = np->parent; |
| 54 | ip = of_get_property(np, "#size-cells", NULL); | 56 | ip = of_get_property(np, "#size-cells", NULL); |
| 55 | if (ip) | 57 | if (ip) |
| 56 | return *ip; | 58 | return be32_to_cpup(ip); |
| 57 | } while (np->parent); | 59 | } while (np->parent); |
| 58 | /* No #size-cells property for the root node */ | 60 | /* No #size-cells property for the root node */ |
| 59 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; | 61 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 60 | } | 62 | } |
| 61 | EXPORT_SYMBOL(of_n_size_cells); | 63 | EXPORT_SYMBOL(of_n_size_cells); |
| 62 | 64 | ||
| 65 | #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */ | ||
| 66 | /** | ||
| 67 | * of_node_get - Increment refcount of a node | ||
| 68 | * @node: Node to inc refcount, NULL is supported to | ||
| 69 | * simplify writing of callers | ||
| 70 | * | ||
| 71 | * Returns node. | ||
| 72 | */ | ||
| 73 | struct device_node *of_node_get(struct device_node *node) | ||
| 74 | { | ||
| 75 | if (node) | ||
| 76 | kref_get(&node->kref); | ||
| 77 | return node; | ||
| 78 | } | ||
| 79 | EXPORT_SYMBOL(of_node_get); | ||
| 80 | |||
| 81 | static inline struct device_node *kref_to_device_node(struct kref *kref) | ||
| 82 | { | ||
| 83 | return container_of(kref, struct device_node, kref); | ||
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * of_node_release - release a dynamically allocated node | ||
| 88 | * @kref: kref element of the node to be released | ||
| 89 | * | ||
| 90 | * In of_node_put() this function is passed to kref_put() | ||
| 91 | * as the destructor. | ||
| 92 | */ | ||
| 93 | static void of_node_release(struct kref *kref) | ||
| 94 | { | ||
| 95 | struct device_node *node = kref_to_device_node(kref); | ||
| 96 | struct property *prop = node->properties; | ||
| 97 | |||
| 98 | /* We should never be releasing nodes that haven't been detached. */ | ||
| 99 | if (!of_node_check_flag(node, OF_DETACHED)) { | ||
| 100 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | ||
| 101 | dump_stack(); | ||
| 102 | kref_init(&node->kref); | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | if (!of_node_check_flag(node, OF_DYNAMIC)) | ||
| 107 | return; | ||
| 108 | |||
| 109 | while (prop) { | ||
| 110 | struct property *next = prop->next; | ||
| 111 | kfree(prop->name); | ||
| 112 | kfree(prop->value); | ||
| 113 | kfree(prop); | ||
| 114 | prop = next; | ||
| 115 | |||
| 116 | if (!prop) { | ||
| 117 | prop = node->deadprops; | ||
| 118 | node->deadprops = NULL; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | kfree(node->full_name); | ||
| 122 | kfree(node->data); | ||
| 123 | kfree(node); | ||
| 124 | } | ||
| 125 | |||
| 126 | /** | ||
| 127 | * of_node_put - Decrement refcount of a node | ||
| 128 | * @node: Node to dec refcount, NULL is supported to | ||
| 129 | * simplify writing of callers | ||
| 130 | * | ||
| 131 | */ | ||
| 132 | void of_node_put(struct device_node *node) | ||
| 133 | { | ||
| 134 | if (node) | ||
| 135 | kref_put(&node->kref, of_node_release); | ||
| 136 | } | ||
| 137 | EXPORT_SYMBOL(of_node_put); | ||
| 138 | #endif /* !CONFIG_SPARC */ | ||
| 139 | |||
| 63 | struct property *of_find_property(const struct device_node *np, | 140 | struct property *of_find_property(const struct device_node *np, |
| 64 | const char *name, | 141 | const char *name, |
| 65 | int *lenp) | 142 | int *lenp) |
| @@ -144,6 +221,27 @@ int of_device_is_compatible(const struct device_node *device, | |||
| 144 | EXPORT_SYMBOL(of_device_is_compatible); | 221 | EXPORT_SYMBOL(of_device_is_compatible); |
| 145 | 222 | ||
| 146 | /** | 223 | /** |
| 224 | * of_machine_is_compatible - Test root of device tree for a given compatible value | ||
| 225 | * @compat: compatible string to look for in root node's compatible property. | ||
| 226 | * | ||
| 227 | * Returns true if the root node has the given value in its | ||
| 228 | * compatible property. | ||
| 229 | */ | ||
| 230 | int of_machine_is_compatible(const char *compat) | ||
| 231 | { | ||
| 232 | struct device_node *root; | ||
| 233 | int rc = 0; | ||
| 234 | |||
| 235 | root = of_find_node_by_path("/"); | ||
| 236 | if (root) { | ||
| 237 | rc = of_device_is_compatible(root, compat); | ||
| 238 | of_node_put(root); | ||
| 239 | } | ||
| 240 | return rc; | ||
| 241 | } | ||
| 242 | EXPORT_SYMBOL(of_machine_is_compatible); | ||
| 243 | |||
| 244 | /** | ||
| 147 | * of_device_is_available - check if a device is available for use | 245 | * of_device_is_available - check if a device is available for use |
| 148 | * | 246 | * |
| 149 | * @device: Node to check for availability | 247 | * @device: Node to check for availability |
| @@ -519,6 +617,27 @@ int of_modalias_node(struct device_node *node, char *modalias, int len) | |||
| 519 | EXPORT_SYMBOL_GPL(of_modalias_node); | 617 | EXPORT_SYMBOL_GPL(of_modalias_node); |
| 520 | 618 | ||
| 521 | /** | 619 | /** |
| 620 | * of_find_node_by_phandle - Find a node given a phandle | ||
| 621 | * @handle: phandle of the node to find | ||
| 622 | * | ||
| 623 | * Returns a node pointer with refcount incremented, use | ||
| 624 | * of_node_put() on it when done. | ||
| 625 | */ | ||
| 626 | struct device_node *of_find_node_by_phandle(phandle handle) | ||
| 627 | { | ||
| 628 | struct device_node *np; | ||
| 629 | |||
| 630 | read_lock(&devtree_lock); | ||
| 631 | for (np = allnodes; np; np = np->allnext) | ||
| 632 | if (np->phandle == handle) | ||
| 633 | break; | ||
| 634 | of_node_get(np); | ||
| 635 | read_unlock(&devtree_lock); | ||
| 636 | return np; | ||
| 637 | } | ||
| 638 | EXPORT_SYMBOL(of_find_node_by_phandle); | ||
| 639 | |||
| 640 | /** | ||
| 522 | * of_parse_phandle - Resolve a phandle property to a device_node pointer | 641 | * of_parse_phandle - Resolve a phandle property to a device_node pointer |
| 523 | * @np: Pointer to device node holding phandle property | 642 | * @np: Pointer to device node holding phandle property |
| 524 | * @phandle_name: Name of property holding a phandle value | 643 | * @phandle_name: Name of property holding a phandle value |
| @@ -578,8 +697,8 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name, | |||
| 578 | const void **out_args) | 697 | const void **out_args) |
| 579 | { | 698 | { |
| 580 | int ret = -EINVAL; | 699 | int ret = -EINVAL; |
| 581 | const u32 *list; | 700 | const __be32 *list; |
| 582 | const u32 *list_end; | 701 | const __be32 *list_end; |
| 583 | int size; | 702 | int size; |
| 584 | int cur_index = 0; | 703 | int cur_index = 0; |
| 585 | struct device_node *node = NULL; | 704 | struct device_node *node = NULL; |
| @@ -593,7 +712,7 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name, | |||
| 593 | list_end = list + size / sizeof(*list); | 712 | list_end = list + size / sizeof(*list); |
| 594 | 713 | ||
| 595 | while (list < list_end) { | 714 | while (list < list_end) { |
| 596 | const u32 *cells; | 715 | const __be32 *cells; |
| 597 | const phandle *phandle; | 716 | const phandle *phandle; |
| 598 | 717 | ||
| 599 | phandle = list++; | 718 | phandle = list++; |
| @@ -617,7 +736,7 @@ int of_parse_phandles_with_args(struct device_node *np, const char *list_name, | |||
| 617 | goto err1; | 736 | goto err1; |
| 618 | } | 737 | } |
| 619 | 738 | ||
| 620 | list += *cells; | 739 | list += be32_to_cpup(cells); |
| 621 | if (list > list_end) { | 740 | if (list > list_end) { |
| 622 | pr_debug("%s: insufficient arguments length\n", | 741 | pr_debug("%s: insufficient arguments length\n", |
| 623 | np->full_name); | 742 | np->full_name); |
| @@ -658,3 +777,190 @@ err0: | |||
| 658 | return ret; | 777 | return ret; |
| 659 | } | 778 | } |
| 660 | EXPORT_SYMBOL(of_parse_phandles_with_args); | 779 | EXPORT_SYMBOL(of_parse_phandles_with_args); |
| 780 | |||
| 781 | /** | ||
| 782 | * prom_add_property - Add a property to a node | ||
| 783 | */ | ||
| 784 | int prom_add_property(struct device_node *np, struct property *prop) | ||
| 785 | { | ||
| 786 | struct property **next; | ||
| 787 | unsigned long flags; | ||
| 788 | |||
| 789 | prop->next = NULL; | ||
| 790 | write_lock_irqsave(&devtree_lock, flags); | ||
| 791 | next = &np->properties; | ||
| 792 | while (*next) { | ||
| 793 | if (strcmp(prop->name, (*next)->name) == 0) { | ||
| 794 | /* duplicate ! don't insert it */ | ||
| 795 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 796 | return -1; | ||
| 797 | } | ||
| 798 | next = &(*next)->next; | ||
| 799 | } | ||
| 800 | *next = prop; | ||
| 801 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 802 | |||
| 803 | #ifdef CONFIG_PROC_DEVICETREE | ||
| 804 | /* try to add to proc as well if it was initialized */ | ||
| 805 | if (np->pde) | ||
| 806 | proc_device_tree_add_prop(np->pde, prop); | ||
| 807 | #endif /* CONFIG_PROC_DEVICETREE */ | ||
| 808 | |||
| 809 | return 0; | ||
| 810 | } | ||
| 811 | |||
| 812 | /** | ||
| 813 | * prom_remove_property - Remove a property from a node. | ||
| 814 | * | ||
| 815 | * Note that we don't actually remove it, since we have given out | ||
| 816 | * who-knows-how-many pointers to the data using get-property. | ||
| 817 | * Instead we just move the property to the "dead properties" | ||
| 818 | * list, so it won't be found any more. | ||
| 819 | */ | ||
| 820 | int prom_remove_property(struct device_node *np, struct property *prop) | ||
| 821 | { | ||
| 822 | struct property **next; | ||
| 823 | unsigned long flags; | ||
| 824 | int found = 0; | ||
| 825 | |||
| 826 | write_lock_irqsave(&devtree_lock, flags); | ||
| 827 | next = &np->properties; | ||
| 828 | while (*next) { | ||
| 829 | if (*next == prop) { | ||
| 830 | /* found the node */ | ||
| 831 | *next = prop->next; | ||
| 832 | prop->next = np->deadprops; | ||
| 833 | np->deadprops = prop; | ||
| 834 | found = 1; | ||
| 835 | break; | ||
| 836 | } | ||
| 837 | next = &(*next)->next; | ||
| 838 | } | ||
| 839 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 840 | |||
| 841 | if (!found) | ||
| 842 | return -ENODEV; | ||
| 843 | |||
| 844 | #ifdef CONFIG_PROC_DEVICETREE | ||
| 845 | /* try to remove the proc node as well */ | ||
| 846 | if (np->pde) | ||
| 847 | proc_device_tree_remove_prop(np->pde, prop); | ||
| 848 | #endif /* CONFIG_PROC_DEVICETREE */ | ||
| 849 | |||
| 850 | return 0; | ||
| 851 | } | ||
| 852 | |||
| 853 | /* | ||
| 854 | * prom_update_property - Update a property in a node. | ||
| 855 | * | ||
| 856 | * Note that we don't actually remove it, since we have given out | ||
| 857 | * who-knows-how-many pointers to the data using get-property. | ||
| 858 | * Instead we just move the property to the "dead properties" list, | ||
| 859 | * and add the new property to the property list | ||
| 860 | */ | ||
| 861 | int prom_update_property(struct device_node *np, | ||
| 862 | struct property *newprop, | ||
| 863 | struct property *oldprop) | ||
| 864 | { | ||
| 865 | struct property **next; | ||
| 866 | unsigned long flags; | ||
| 867 | int found = 0; | ||
| 868 | |||
| 869 | write_lock_irqsave(&devtree_lock, flags); | ||
| 870 | next = &np->properties; | ||
| 871 | while (*next) { | ||
| 872 | if (*next == oldprop) { | ||
| 873 | /* found the node */ | ||
| 874 | newprop->next = oldprop->next; | ||
| 875 | *next = newprop; | ||
| 876 | oldprop->next = np->deadprops; | ||
| 877 | np->deadprops = oldprop; | ||
| 878 | found = 1; | ||
| 879 | break; | ||
| 880 | } | ||
| 881 | next = &(*next)->next; | ||
| 882 | } | ||
| 883 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 884 | |||
| 885 | if (!found) | ||
| 886 | return -ENODEV; | ||
| 887 | |||
| 888 | #ifdef CONFIG_PROC_DEVICETREE | ||
| 889 | /* try to add to proc as well if it was initialized */ | ||
| 890 | if (np->pde) | ||
| 891 | proc_device_tree_update_prop(np->pde, newprop, oldprop); | ||
| 892 | #endif /* CONFIG_PROC_DEVICETREE */ | ||
| 893 | |||
| 894 | return 0; | ||
| 895 | } | ||
| 896 | |||
| 897 | #if defined(CONFIG_OF_DYNAMIC) | ||
| 898 | /* | ||
| 899 | * Support for dynamic device trees. | ||
| 900 | * | ||
| 901 | * On some platforms, the device tree can be manipulated at runtime. | ||
| 902 | * The routines in this section support adding, removing and changing | ||
| 903 | * device tree nodes. | ||
| 904 | */ | ||
| 905 | |||
| 906 | /** | ||
| 907 | * of_attach_node - Plug a device node into the tree and global list. | ||
| 908 | */ | ||
| 909 | void of_attach_node(struct device_node *np) | ||
| 910 | { | ||
| 911 | unsigned long flags; | ||
| 912 | |||
| 913 | write_lock_irqsave(&devtree_lock, flags); | ||
| 914 | np->sibling = np->parent->child; | ||
| 915 | np->allnext = allnodes; | ||
| 916 | np->parent->child = np; | ||
| 917 | allnodes = np; | ||
| 918 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 919 | } | ||
| 920 | |||
| 921 | /** | ||
| 922 | * of_detach_node - "Unplug" a node from the device tree. | ||
| 923 | * | ||
| 924 | * The caller must hold a reference to the node. The memory associated with | ||
| 925 | * the node is not freed until its refcount goes to zero. | ||
| 926 | */ | ||
| 927 | void of_detach_node(struct device_node *np) | ||
| 928 | { | ||
| 929 | struct device_node *parent; | ||
| 930 | unsigned long flags; | ||
| 931 | |||
| 932 | write_lock_irqsave(&devtree_lock, flags); | ||
| 933 | |||
| 934 | parent = np->parent; | ||
| 935 | if (!parent) | ||
| 936 | goto out_unlock; | ||
| 937 | |||
| 938 | if (allnodes == np) | ||
| 939 | allnodes = np->allnext; | ||
| 940 | else { | ||
| 941 | struct device_node *prev; | ||
| 942 | for (prev = allnodes; | ||
| 943 | prev->allnext != np; | ||
| 944 | prev = prev->allnext) | ||
| 945 | ; | ||
| 946 | prev->allnext = np->allnext; | ||
| 947 | } | ||
| 948 | |||
| 949 | if (parent->child == np) | ||
| 950 | parent->child = np->sibling; | ||
| 951 | else { | ||
| 952 | struct device_node *prevsib; | ||
| 953 | for (prevsib = np->parent->child; | ||
| 954 | prevsib->sibling != np; | ||
| 955 | prevsib = prevsib->sibling) | ||
| 956 | ; | ||
| 957 | prevsib->sibling = np->sibling; | ||
| 958 | } | ||
| 959 | |||
| 960 | of_node_set_flag(np, OF_DETACHED); | ||
| 961 | |||
| 962 | out_unlock: | ||
| 963 | write_unlock_irqrestore(&devtree_lock, flags); | ||
| 964 | } | ||
| 965 | #endif /* defined(CONFIG_OF_DYNAMIC) */ | ||
| 966 | |||
