16afc0dc3SGrant Likely /* 26afc0dc3SGrant Likely * Support for dynamic device trees. 36afc0dc3SGrant Likely * 46afc0dc3SGrant Likely * On some platforms, the device tree can be manipulated at runtime. 56afc0dc3SGrant Likely * The routines in this section support adding, removing and changing 66afc0dc3SGrant Likely * device tree nodes. 76afc0dc3SGrant Likely */ 86afc0dc3SGrant Likely 96afc0dc3SGrant Likely #include <linux/of.h> 106afc0dc3SGrant Likely #include <linux/spinlock.h> 116afc0dc3SGrant Likely #include <linux/slab.h> 126afc0dc3SGrant Likely #include <linux/string.h> 136afc0dc3SGrant Likely #include <linux/proc_fs.h> 146afc0dc3SGrant Likely 156afc0dc3SGrant Likely #include "of_private.h" 166afc0dc3SGrant Likely 176afc0dc3SGrant Likely /** 186afc0dc3SGrant Likely * of_node_get() - Increment refcount of a node 196afc0dc3SGrant Likely * @node: Node to inc refcount, NULL is supported to simplify writing of 206afc0dc3SGrant Likely * callers 216afc0dc3SGrant Likely * 226afc0dc3SGrant Likely * Returns node. 236afc0dc3SGrant Likely */ 246afc0dc3SGrant Likely struct device_node *of_node_get(struct device_node *node) 256afc0dc3SGrant Likely { 266afc0dc3SGrant Likely if (node) 276afc0dc3SGrant Likely kobject_get(&node->kobj); 286afc0dc3SGrant Likely return node; 296afc0dc3SGrant Likely } 306afc0dc3SGrant Likely EXPORT_SYMBOL(of_node_get); 316afc0dc3SGrant Likely 326afc0dc3SGrant Likely /** 336afc0dc3SGrant Likely * of_node_put() - Decrement refcount of a node 346afc0dc3SGrant Likely * @node: Node to dec refcount, NULL is supported to simplify writing of 356afc0dc3SGrant Likely * callers 366afc0dc3SGrant Likely */ 376afc0dc3SGrant Likely void of_node_put(struct device_node *node) 386afc0dc3SGrant Likely { 396afc0dc3SGrant Likely if (node) 406afc0dc3SGrant Likely kobject_put(&node->kobj); 416afc0dc3SGrant Likely } 426afc0dc3SGrant Likely EXPORT_SYMBOL(of_node_put); 436afc0dc3SGrant Likely 44*8a2b22a2SGrant Likely void __of_detach_node_sysfs(struct device_node *np) 456afc0dc3SGrant Likely { 466afc0dc3SGrant Likely struct property *pp; 476afc0dc3SGrant Likely 486afc0dc3SGrant Likely BUG_ON(!of_node_is_initialized(np)); 49*8a2b22a2SGrant Likely if (!of_kset) 50*8a2b22a2SGrant Likely return; 516afc0dc3SGrant Likely 526afc0dc3SGrant Likely /* only remove properties if on sysfs */ 536afc0dc3SGrant Likely if (of_node_is_attached(np)) { 546afc0dc3SGrant Likely for_each_property_of_node(np, pp) 556afc0dc3SGrant Likely sysfs_remove_bin_file(&np->kobj, &pp->attr); 566afc0dc3SGrant Likely kobject_del(&np->kobj); 576afc0dc3SGrant Likely } 586afc0dc3SGrant Likely 596afc0dc3SGrant Likely /* finally remove the kobj_init ref */ 606afc0dc3SGrant Likely of_node_put(np); 616afc0dc3SGrant Likely } 626afc0dc3SGrant Likely 636afc0dc3SGrant Likely static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); 646afc0dc3SGrant Likely 656afc0dc3SGrant Likely int of_reconfig_notifier_register(struct notifier_block *nb) 666afc0dc3SGrant Likely { 676afc0dc3SGrant Likely return blocking_notifier_chain_register(&of_reconfig_chain, nb); 686afc0dc3SGrant Likely } 696afc0dc3SGrant Likely EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); 706afc0dc3SGrant Likely 716afc0dc3SGrant Likely int of_reconfig_notifier_unregister(struct notifier_block *nb) 726afc0dc3SGrant Likely { 736afc0dc3SGrant Likely return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); 746afc0dc3SGrant Likely } 756afc0dc3SGrant Likely EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); 766afc0dc3SGrant Likely 776afc0dc3SGrant Likely int of_reconfig_notify(unsigned long action, void *p) 786afc0dc3SGrant Likely { 796afc0dc3SGrant Likely int rc; 806afc0dc3SGrant Likely 816afc0dc3SGrant Likely rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); 826afc0dc3SGrant Likely return notifier_to_errno(rc); 836afc0dc3SGrant Likely } 846afc0dc3SGrant Likely 856afc0dc3SGrant Likely int of_property_notify(int action, struct device_node *np, 866afc0dc3SGrant Likely struct property *prop) 876afc0dc3SGrant Likely { 886afc0dc3SGrant Likely struct of_prop_reconfig pr; 896afc0dc3SGrant Likely 906afc0dc3SGrant Likely /* only call notifiers if the node is attached */ 916afc0dc3SGrant Likely if (!of_node_is_attached(np)) 926afc0dc3SGrant Likely return 0; 936afc0dc3SGrant Likely 946afc0dc3SGrant Likely pr.dn = np; 956afc0dc3SGrant Likely pr.prop = prop; 966afc0dc3SGrant Likely return of_reconfig_notify(action, &pr); 976afc0dc3SGrant Likely } 986afc0dc3SGrant Likely 99d8c50088SPantelis Antoniou void __of_attach_node(struct device_node *np) 100d8c50088SPantelis Antoniou { 101d8c50088SPantelis Antoniou np->sibling = np->parent->child; 102d8c50088SPantelis Antoniou np->allnext = np->parent->allnext; 103d8c50088SPantelis Antoniou np->parent->allnext = np; 104d8c50088SPantelis Antoniou np->parent->child = np; 105d8c50088SPantelis Antoniou of_node_clear_flag(np, OF_DETACHED); 106d8c50088SPantelis Antoniou } 107d8c50088SPantelis Antoniou 1086afc0dc3SGrant Likely /** 1096afc0dc3SGrant Likely * of_attach_node() - Plug a device node into the tree and global list. 1106afc0dc3SGrant Likely */ 1116afc0dc3SGrant Likely int of_attach_node(struct device_node *np) 1126afc0dc3SGrant Likely { 1136afc0dc3SGrant Likely unsigned long flags; 1146afc0dc3SGrant Likely int rc; 1156afc0dc3SGrant Likely 1166afc0dc3SGrant Likely rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); 1176afc0dc3SGrant Likely if (rc) 1186afc0dc3SGrant Likely return rc; 1196afc0dc3SGrant Likely 120*8a2b22a2SGrant Likely mutex_lock(&of_mutex); 1216afc0dc3SGrant Likely raw_spin_lock_irqsave(&devtree_lock, flags); 122d8c50088SPantelis Antoniou __of_attach_node(np); 1236afc0dc3SGrant Likely raw_spin_unlock_irqrestore(&devtree_lock, flags); 1246afc0dc3SGrant Likely 125*8a2b22a2SGrant Likely __of_attach_node_sysfs(np); 126*8a2b22a2SGrant Likely mutex_unlock(&of_mutex); 1276afc0dc3SGrant Likely return 0; 1286afc0dc3SGrant Likely } 1296afc0dc3SGrant Likely 130d8c50088SPantelis Antoniou void __of_detach_node(struct device_node *np) 1316afc0dc3SGrant Likely { 1326afc0dc3SGrant Likely struct device_node *parent; 1336afc0dc3SGrant Likely 134d8c50088SPantelis Antoniou if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) 135d8c50088SPantelis Antoniou return; 1366afc0dc3SGrant Likely 1376afc0dc3SGrant Likely parent = np->parent; 138d8c50088SPantelis Antoniou if (WARN_ON(!parent)) 139d8c50088SPantelis Antoniou return; 1406afc0dc3SGrant Likely 1416afc0dc3SGrant Likely if (of_allnodes == np) 1426afc0dc3SGrant Likely of_allnodes = np->allnext; 1436afc0dc3SGrant Likely else { 1446afc0dc3SGrant Likely struct device_node *prev; 1456afc0dc3SGrant Likely for (prev = of_allnodes; 1466afc0dc3SGrant Likely prev->allnext != np; 1476afc0dc3SGrant Likely prev = prev->allnext) 1486afc0dc3SGrant Likely ; 1496afc0dc3SGrant Likely prev->allnext = np->allnext; 1506afc0dc3SGrant Likely } 1516afc0dc3SGrant Likely 1526afc0dc3SGrant Likely if (parent->child == np) 1536afc0dc3SGrant Likely parent->child = np->sibling; 1546afc0dc3SGrant Likely else { 1556afc0dc3SGrant Likely struct device_node *prevsib; 1566afc0dc3SGrant Likely for (prevsib = np->parent->child; 1576afc0dc3SGrant Likely prevsib->sibling != np; 1586afc0dc3SGrant Likely prevsib = prevsib->sibling) 1596afc0dc3SGrant Likely ; 1606afc0dc3SGrant Likely prevsib->sibling = np->sibling; 1616afc0dc3SGrant Likely } 1626afc0dc3SGrant Likely 1636afc0dc3SGrant Likely of_node_set_flag(np, OF_DETACHED); 164d8c50088SPantelis Antoniou } 165d8c50088SPantelis Antoniou 166d8c50088SPantelis Antoniou /** 167d8c50088SPantelis Antoniou * of_detach_node() - "Unplug" a node from the device tree. 168d8c50088SPantelis Antoniou * 169d8c50088SPantelis Antoniou * The caller must hold a reference to the node. The memory associated with 170d8c50088SPantelis Antoniou * the node is not freed until its refcount goes to zero. 171d8c50088SPantelis Antoniou */ 172d8c50088SPantelis Antoniou int of_detach_node(struct device_node *np) 173d8c50088SPantelis Antoniou { 174d8c50088SPantelis Antoniou unsigned long flags; 175d8c50088SPantelis Antoniou int rc = 0; 176d8c50088SPantelis Antoniou 177d8c50088SPantelis Antoniou rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); 178d8c50088SPantelis Antoniou if (rc) 179d8c50088SPantelis Antoniou return rc; 180d8c50088SPantelis Antoniou 181*8a2b22a2SGrant Likely mutex_lock(&of_mutex); 182d8c50088SPantelis Antoniou raw_spin_lock_irqsave(&devtree_lock, flags); 183d8c50088SPantelis Antoniou __of_detach_node(np); 1846afc0dc3SGrant Likely raw_spin_unlock_irqrestore(&devtree_lock, flags); 1856afc0dc3SGrant Likely 186*8a2b22a2SGrant Likely __of_detach_node_sysfs(np); 187*8a2b22a2SGrant Likely mutex_unlock(&of_mutex); 1886afc0dc3SGrant Likely return rc; 1896afc0dc3SGrant Likely } 1906afc0dc3SGrant Likely 1916afc0dc3SGrant Likely /** 1926afc0dc3SGrant Likely * of_node_release() - release a dynamically allocated node 1936afc0dc3SGrant Likely * @kref: kref element of the node to be released 1946afc0dc3SGrant Likely * 1956afc0dc3SGrant Likely * In of_node_put() this function is passed to kref_put() as the destructor. 1966afc0dc3SGrant Likely */ 1976afc0dc3SGrant Likely void of_node_release(struct kobject *kobj) 1986afc0dc3SGrant Likely { 1996afc0dc3SGrant Likely struct device_node *node = kobj_to_device_node(kobj); 2006afc0dc3SGrant Likely struct property *prop = node->properties; 2016afc0dc3SGrant Likely 2026afc0dc3SGrant Likely /* We should never be releasing nodes that haven't been detached. */ 2036afc0dc3SGrant Likely if (!of_node_check_flag(node, OF_DETACHED)) { 2046afc0dc3SGrant Likely pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); 2056afc0dc3SGrant Likely dump_stack(); 2066afc0dc3SGrant Likely return; 2076afc0dc3SGrant Likely } 2086afc0dc3SGrant Likely 2096afc0dc3SGrant Likely if (!of_node_check_flag(node, OF_DYNAMIC)) 2106afc0dc3SGrant Likely return; 2116afc0dc3SGrant Likely 2126afc0dc3SGrant Likely while (prop) { 2136afc0dc3SGrant Likely struct property *next = prop->next; 2146afc0dc3SGrant Likely kfree(prop->name); 2156afc0dc3SGrant Likely kfree(prop->value); 2166afc0dc3SGrant Likely kfree(prop); 2176afc0dc3SGrant Likely prop = next; 2186afc0dc3SGrant Likely 2196afc0dc3SGrant Likely if (!prop) { 2206afc0dc3SGrant Likely prop = node->deadprops; 2216afc0dc3SGrant Likely node->deadprops = NULL; 2226afc0dc3SGrant Likely } 2236afc0dc3SGrant Likely } 2246afc0dc3SGrant Likely kfree(node->full_name); 2256afc0dc3SGrant Likely kfree(node->data); 2266afc0dc3SGrant Likely kfree(node); 2276afc0dc3SGrant Likely } 22869843396SPantelis Antoniou 22969843396SPantelis Antoniou /** 23069843396SPantelis Antoniou * __of_prop_dup - Copy a property dynamically. 23169843396SPantelis Antoniou * @prop: Property to copy 23269843396SPantelis Antoniou * @allocflags: Allocation flags (typically pass GFP_KERNEL) 23369843396SPantelis Antoniou * 23469843396SPantelis Antoniou * Copy a property by dynamically allocating the memory of both the 23569843396SPantelis Antoniou * property stucture and the property name & contents. The property's 23669843396SPantelis Antoniou * flags have the OF_DYNAMIC bit set so that we can differentiate between 23769843396SPantelis Antoniou * dynamically allocated properties and not. 23869843396SPantelis Antoniou * Returns the newly allocated property or NULL on out of memory error. 23969843396SPantelis Antoniou */ 24069843396SPantelis Antoniou struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) 24169843396SPantelis Antoniou { 24269843396SPantelis Antoniou struct property *new; 24369843396SPantelis Antoniou 24469843396SPantelis Antoniou new = kzalloc(sizeof(*new), allocflags); 24569843396SPantelis Antoniou if (!new) 24669843396SPantelis Antoniou return NULL; 24769843396SPantelis Antoniou 24869843396SPantelis Antoniou /* 24969843396SPantelis Antoniou * NOTE: There is no check for zero length value. 25069843396SPantelis Antoniou * In case of a boolean property This will allocate a value 25169843396SPantelis Antoniou * of zero bytes. We do this to work around the use 25269843396SPantelis Antoniou * of of_get_property() calls on boolean values. 25369843396SPantelis Antoniou */ 25469843396SPantelis Antoniou new->name = kstrdup(prop->name, allocflags); 25569843396SPantelis Antoniou new->value = kmemdup(prop->value, prop->length, allocflags); 25669843396SPantelis Antoniou new->length = prop->length; 25769843396SPantelis Antoniou if (!new->name || !new->value) 25869843396SPantelis Antoniou goto err_free; 25969843396SPantelis Antoniou 26069843396SPantelis Antoniou /* mark the property as dynamic */ 26169843396SPantelis Antoniou of_property_set_flag(new, OF_DYNAMIC); 26269843396SPantelis Antoniou 26369843396SPantelis Antoniou return new; 26469843396SPantelis Antoniou 26569843396SPantelis Antoniou err_free: 26669843396SPantelis Antoniou kfree(new->name); 26769843396SPantelis Antoniou kfree(new->value); 26869843396SPantelis Antoniou kfree(new); 26969843396SPantelis Antoniou return NULL; 27069843396SPantelis Antoniou } 27169843396SPantelis Antoniou 27269843396SPantelis Antoniou /** 27369843396SPantelis Antoniou * __of_node_alloc() - Create an empty device node dynamically. 27469843396SPantelis Antoniou * @full_name: Full name of the new device node 27569843396SPantelis Antoniou * @allocflags: Allocation flags (typically pass GFP_KERNEL) 27669843396SPantelis Antoniou * 27769843396SPantelis Antoniou * Create an empty device tree node, suitable for further modification. 27869843396SPantelis Antoniou * The node data are dynamically allocated and all the node flags 27969843396SPantelis Antoniou * have the OF_DYNAMIC & OF_DETACHED bits set. 28069843396SPantelis Antoniou * Returns the newly allocated node or NULL on out of memory error. 28169843396SPantelis Antoniou */ 28269843396SPantelis Antoniou struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags) 28369843396SPantelis Antoniou { 28469843396SPantelis Antoniou struct device_node *node; 28569843396SPantelis Antoniou 28669843396SPantelis Antoniou node = kzalloc(sizeof(*node), allocflags); 28769843396SPantelis Antoniou if (!node) 28869843396SPantelis Antoniou return NULL; 28969843396SPantelis Antoniou 29069843396SPantelis Antoniou node->full_name = kstrdup(full_name, allocflags); 29169843396SPantelis Antoniou of_node_set_flag(node, OF_DYNAMIC); 29269843396SPantelis Antoniou of_node_set_flag(node, OF_DETACHED); 29369843396SPantelis Antoniou if (!node->full_name) 29469843396SPantelis Antoniou goto err_free; 29569843396SPantelis Antoniou 29669843396SPantelis Antoniou of_node_init(node); 29769843396SPantelis Antoniou 29869843396SPantelis Antoniou return node; 29969843396SPantelis Antoniou 30069843396SPantelis Antoniou err_free: 30169843396SPantelis Antoniou kfree(node->full_name); 30269843396SPantelis Antoniou kfree(node); 30369843396SPantelis Antoniou return NULL; 30469843396SPantelis Antoniou } 305