1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Support for dynamic device trees.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * On some platforms, the device tree can be manipulated at runtime.
6*4882a593Smuzhiyun * The routines in this section support adding, removing and changing
7*4882a593Smuzhiyun * device tree nodes.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #define pr_fmt(fmt) "OF: " fmt
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/spinlock.h>
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/string.h>
16*4882a593Smuzhiyun #include <linux/proc_fs.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "of_private.h"
19*4882a593Smuzhiyun
kobj_to_device_node(struct kobject * kobj)20*4882a593Smuzhiyun static struct device_node *kobj_to_device_node(struct kobject *kobj)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun return container_of(kobj, struct device_node, kobj);
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun * of_node_get() - Increment refcount of a node
27*4882a593Smuzhiyun * @node: Node to inc refcount, NULL is supported to simplify writing of
28*4882a593Smuzhiyun * callers
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Returns node.
31*4882a593Smuzhiyun */
of_node_get(struct device_node * node)32*4882a593Smuzhiyun struct device_node *of_node_get(struct device_node *node)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun if (node)
35*4882a593Smuzhiyun kobject_get(&node->kobj);
36*4882a593Smuzhiyun return node;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun EXPORT_SYMBOL(of_node_get);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * of_node_put() - Decrement refcount of a node
42*4882a593Smuzhiyun * @node: Node to dec refcount, NULL is supported to simplify writing of
43*4882a593Smuzhiyun * callers
44*4882a593Smuzhiyun */
of_node_put(struct device_node * node)45*4882a593Smuzhiyun void of_node_put(struct device_node *node)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun if (node)
48*4882a593Smuzhiyun kobject_put(&node->kobj);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun EXPORT_SYMBOL(of_node_put);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
53*4882a593Smuzhiyun
of_reconfig_notifier_register(struct notifier_block * nb)54*4882a593Smuzhiyun int of_reconfig_notifier_register(struct notifier_block *nb)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun return blocking_notifier_chain_register(&of_reconfig_chain, nb);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
59*4882a593Smuzhiyun
of_reconfig_notifier_unregister(struct notifier_block * nb)60*4882a593Smuzhiyun int of_reconfig_notifier_unregister(struct notifier_block *nb)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun #ifdef DEBUG
67*4882a593Smuzhiyun const char *action_names[] = {
68*4882a593Smuzhiyun [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
69*4882a593Smuzhiyun [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
70*4882a593Smuzhiyun [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
71*4882a593Smuzhiyun [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
72*4882a593Smuzhiyun [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun
of_reconfig_notify(unsigned long action,struct of_reconfig_data * p)76*4882a593Smuzhiyun int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun int rc;
79*4882a593Smuzhiyun #ifdef DEBUG
80*4882a593Smuzhiyun struct of_reconfig_data *pr = p;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun switch (action) {
83*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
84*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
85*4882a593Smuzhiyun pr_debug("notify %-15s %pOF\n", action_names[action],
86*4882a593Smuzhiyun pr->dn);
87*4882a593Smuzhiyun break;
88*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
89*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
90*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
91*4882a593Smuzhiyun pr_debug("notify %-15s %pOF:%s\n", action_names[action],
92*4882a593Smuzhiyun pr->dn, pr->prop->name);
93*4882a593Smuzhiyun break;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun #endif
97*4882a593Smuzhiyun rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
98*4882a593Smuzhiyun return notifier_to_errno(rc);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * of_reconfig_get_state_change() - Returns new state of device
103*4882a593Smuzhiyun * @action - action of the of notifier
104*4882a593Smuzhiyun * @arg - argument of the of notifier
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * Returns the new state of a device based on the notifier used.
107*4882a593Smuzhiyun * Returns 0 on device going from enabled to disabled, 1 on device
108*4882a593Smuzhiyun * going from disabled to enabled and -1 on no change.
109*4882a593Smuzhiyun */
of_reconfig_get_state_change(unsigned long action,struct of_reconfig_data * pr)110*4882a593Smuzhiyun int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun struct property *prop, *old_prop = NULL;
113*4882a593Smuzhiyun int is_status, status_state, old_status_state, prev_state, new_state;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* figure out if a device should be created or destroyed */
116*4882a593Smuzhiyun switch (action) {
117*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
118*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
119*4882a593Smuzhiyun prop = of_find_property(pr->dn, "status", NULL);
120*4882a593Smuzhiyun break;
121*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
122*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
123*4882a593Smuzhiyun prop = pr->prop;
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
126*4882a593Smuzhiyun prop = pr->prop;
127*4882a593Smuzhiyun old_prop = pr->old_prop;
128*4882a593Smuzhiyun break;
129*4882a593Smuzhiyun default:
130*4882a593Smuzhiyun return OF_RECONFIG_NO_CHANGE;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun is_status = 0;
134*4882a593Smuzhiyun status_state = -1;
135*4882a593Smuzhiyun old_status_state = -1;
136*4882a593Smuzhiyun prev_state = -1;
137*4882a593Smuzhiyun new_state = -1;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (prop && !strcmp(prop->name, "status")) {
140*4882a593Smuzhiyun is_status = 1;
141*4882a593Smuzhiyun status_state = !strcmp(prop->value, "okay") ||
142*4882a593Smuzhiyun !strcmp(prop->value, "ok");
143*4882a593Smuzhiyun if (old_prop)
144*4882a593Smuzhiyun old_status_state = !strcmp(old_prop->value, "okay") ||
145*4882a593Smuzhiyun !strcmp(old_prop->value, "ok");
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun switch (action) {
149*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
150*4882a593Smuzhiyun prev_state = 0;
151*4882a593Smuzhiyun /* -1 & 0 status either missing or okay */
152*4882a593Smuzhiyun new_state = status_state != 0;
153*4882a593Smuzhiyun break;
154*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
155*4882a593Smuzhiyun /* -1 & 0 status either missing or okay */
156*4882a593Smuzhiyun prev_state = status_state != 0;
157*4882a593Smuzhiyun new_state = 0;
158*4882a593Smuzhiyun break;
159*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
160*4882a593Smuzhiyun if (is_status) {
161*4882a593Smuzhiyun /* no status property -> enabled (legacy) */
162*4882a593Smuzhiyun prev_state = 1;
163*4882a593Smuzhiyun new_state = status_state;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun break;
166*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
167*4882a593Smuzhiyun if (is_status) {
168*4882a593Smuzhiyun prev_state = status_state;
169*4882a593Smuzhiyun /* no status property -> enabled (legacy) */
170*4882a593Smuzhiyun new_state = 1;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun break;
173*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
174*4882a593Smuzhiyun if (is_status) {
175*4882a593Smuzhiyun prev_state = old_status_state != 0;
176*4882a593Smuzhiyun new_state = status_state != 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun break;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (prev_state == new_state)
182*4882a593Smuzhiyun return OF_RECONFIG_NO_CHANGE;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
187*4882a593Smuzhiyun
of_property_notify(int action,struct device_node * np,struct property * prop,struct property * oldprop)188*4882a593Smuzhiyun int of_property_notify(int action, struct device_node *np,
189*4882a593Smuzhiyun struct property *prop, struct property *oldprop)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun struct of_reconfig_data pr;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* only call notifiers if the node is attached */
194*4882a593Smuzhiyun if (!of_node_is_attached(np))
195*4882a593Smuzhiyun return 0;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun pr.dn = np;
198*4882a593Smuzhiyun pr.prop = prop;
199*4882a593Smuzhiyun pr.old_prop = oldprop;
200*4882a593Smuzhiyun return of_reconfig_notify(action, &pr);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
__of_attach_node(struct device_node * np)203*4882a593Smuzhiyun static void __of_attach_node(struct device_node *np)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun const __be32 *phandle;
206*4882a593Smuzhiyun int sz;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun if (!of_node_check_flag(np, OF_OVERLAY)) {
209*4882a593Smuzhiyun np->name = __of_get_property(np, "name", NULL);
210*4882a593Smuzhiyun if (!np->name)
211*4882a593Smuzhiyun np->name = "<NULL>";
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun phandle = __of_get_property(np, "phandle", &sz);
214*4882a593Smuzhiyun if (!phandle)
215*4882a593Smuzhiyun phandle = __of_get_property(np, "linux,phandle", &sz);
216*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
217*4882a593Smuzhiyun phandle = __of_get_property(np, "ibm,phandle", &sz);
218*4882a593Smuzhiyun if (phandle && (sz >= 4))
219*4882a593Smuzhiyun np->phandle = be32_to_cpup(phandle);
220*4882a593Smuzhiyun else
221*4882a593Smuzhiyun np->phandle = 0;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun np->child = NULL;
225*4882a593Smuzhiyun np->sibling = np->parent->child;
226*4882a593Smuzhiyun np->parent->child = np;
227*4882a593Smuzhiyun of_node_clear_flag(np, OF_DETACHED);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun * of_attach_node() - Plug a device node into the tree and global list.
232*4882a593Smuzhiyun */
of_attach_node(struct device_node * np)233*4882a593Smuzhiyun int of_attach_node(struct device_node *np)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun struct of_reconfig_data rd;
236*4882a593Smuzhiyun unsigned long flags;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun memset(&rd, 0, sizeof(rd));
239*4882a593Smuzhiyun rd.dn = np;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun mutex_lock(&of_mutex);
242*4882a593Smuzhiyun raw_spin_lock_irqsave(&devtree_lock, flags);
243*4882a593Smuzhiyun __of_attach_node(np);
244*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&devtree_lock, flags);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun __of_attach_node_sysfs(np);
247*4882a593Smuzhiyun mutex_unlock(&of_mutex);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return 0;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
__of_detach_node(struct device_node * np)254*4882a593Smuzhiyun void __of_detach_node(struct device_node *np)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun struct device_node *parent;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
259*4882a593Smuzhiyun return;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun parent = np->parent;
262*4882a593Smuzhiyun if (WARN_ON(!parent))
263*4882a593Smuzhiyun return;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (parent->child == np)
266*4882a593Smuzhiyun parent->child = np->sibling;
267*4882a593Smuzhiyun else {
268*4882a593Smuzhiyun struct device_node *prevsib;
269*4882a593Smuzhiyun for (prevsib = np->parent->child;
270*4882a593Smuzhiyun prevsib->sibling != np;
271*4882a593Smuzhiyun prevsib = prevsib->sibling)
272*4882a593Smuzhiyun ;
273*4882a593Smuzhiyun prevsib->sibling = np->sibling;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun of_node_set_flag(np, OF_DETACHED);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /* race with of_find_node_by_phandle() prevented by devtree_lock */
279*4882a593Smuzhiyun __of_phandle_cache_inv_entry(np->phandle);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /**
283*4882a593Smuzhiyun * of_detach_node() - "Unplug" a node from the device tree.
284*4882a593Smuzhiyun */
of_detach_node(struct device_node * np)285*4882a593Smuzhiyun int of_detach_node(struct device_node *np)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct of_reconfig_data rd;
288*4882a593Smuzhiyun unsigned long flags;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun memset(&rd, 0, sizeof(rd));
291*4882a593Smuzhiyun rd.dn = np;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun mutex_lock(&of_mutex);
294*4882a593Smuzhiyun raw_spin_lock_irqsave(&devtree_lock, flags);
295*4882a593Smuzhiyun __of_detach_node(np);
296*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&devtree_lock, flags);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun __of_detach_node_sysfs(np);
299*4882a593Smuzhiyun mutex_unlock(&of_mutex);
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_detach_node);
306*4882a593Smuzhiyun
property_list_free(struct property * prop_list)307*4882a593Smuzhiyun static void property_list_free(struct property *prop_list)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun struct property *prop, *next;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun for (prop = prop_list; prop != NULL; prop = next) {
312*4882a593Smuzhiyun next = prop->next;
313*4882a593Smuzhiyun kfree(prop->name);
314*4882a593Smuzhiyun kfree(prop->value);
315*4882a593Smuzhiyun kfree(prop);
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /**
320*4882a593Smuzhiyun * of_node_release() - release a dynamically allocated node
321*4882a593Smuzhiyun * @kref: kref element of the node to be released
322*4882a593Smuzhiyun *
323*4882a593Smuzhiyun * In of_node_put() this function is passed to kref_put() as the destructor.
324*4882a593Smuzhiyun */
of_node_release(struct kobject * kobj)325*4882a593Smuzhiyun void of_node_release(struct kobject *kobj)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct device_node *node = kobj_to_device_node(kobj);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /* We should never be releasing nodes that haven't been detached. */
330*4882a593Smuzhiyun if (!of_node_check_flag(node, OF_DETACHED)) {
331*4882a593Smuzhiyun pr_err("ERROR: Bad of_node_put() on %pOF\n", node);
332*4882a593Smuzhiyun dump_stack();
333*4882a593Smuzhiyun return;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun if (!of_node_check_flag(node, OF_DYNAMIC))
336*4882a593Smuzhiyun return;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (of_node_check_flag(node, OF_OVERLAY)) {
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun if (!of_node_check_flag(node, OF_OVERLAY_FREE_CSET)) {
341*4882a593Smuzhiyun /* premature refcount of zero, do not free memory */
342*4882a593Smuzhiyun pr_err("ERROR: memory leak before free overlay changeset, %pOF\n",
343*4882a593Smuzhiyun node);
344*4882a593Smuzhiyun return;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun * If node->properties non-empty then properties were added
349*4882a593Smuzhiyun * to this node either by different overlay that has not
350*4882a593Smuzhiyun * yet been removed, or by a non-overlay mechanism.
351*4882a593Smuzhiyun */
352*4882a593Smuzhiyun if (node->properties)
353*4882a593Smuzhiyun pr_err("ERROR: %s(), unexpected properties in %pOF\n",
354*4882a593Smuzhiyun __func__, node);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun property_list_free(node->properties);
358*4882a593Smuzhiyun property_list_free(node->deadprops);
359*4882a593Smuzhiyun fwnode_links_purge(of_fwnode_handle(node));
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun kfree(node->full_name);
362*4882a593Smuzhiyun kfree(node->data);
363*4882a593Smuzhiyun kfree(node);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun * __of_prop_dup - Copy a property dynamically.
368*4882a593Smuzhiyun * @prop: Property to copy
369*4882a593Smuzhiyun * @allocflags: Allocation flags (typically pass GFP_KERNEL)
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * Copy a property by dynamically allocating the memory of both the
372*4882a593Smuzhiyun * property structure and the property name & contents. The property's
373*4882a593Smuzhiyun * flags have the OF_DYNAMIC bit set so that we can differentiate between
374*4882a593Smuzhiyun * dynamically allocated properties and not.
375*4882a593Smuzhiyun * Returns the newly allocated property or NULL on out of memory error.
376*4882a593Smuzhiyun */
__of_prop_dup(const struct property * prop,gfp_t allocflags)377*4882a593Smuzhiyun struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun struct property *new;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun new = kzalloc(sizeof(*new), allocflags);
382*4882a593Smuzhiyun if (!new)
383*4882a593Smuzhiyun return NULL;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun /*
386*4882a593Smuzhiyun * NOTE: There is no check for zero length value.
387*4882a593Smuzhiyun * In case of a boolean property, this will allocate a value
388*4882a593Smuzhiyun * of zero bytes. We do this to work around the use
389*4882a593Smuzhiyun * of of_get_property() calls on boolean values.
390*4882a593Smuzhiyun */
391*4882a593Smuzhiyun new->name = kstrdup(prop->name, allocflags);
392*4882a593Smuzhiyun new->value = kmemdup(prop->value, prop->length, allocflags);
393*4882a593Smuzhiyun new->length = prop->length;
394*4882a593Smuzhiyun if (!new->name || !new->value)
395*4882a593Smuzhiyun goto err_free;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /* mark the property as dynamic */
398*4882a593Smuzhiyun of_property_set_flag(new, OF_DYNAMIC);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun return new;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun err_free:
403*4882a593Smuzhiyun kfree(new->name);
404*4882a593Smuzhiyun kfree(new->value);
405*4882a593Smuzhiyun kfree(new);
406*4882a593Smuzhiyun return NULL;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /**
410*4882a593Smuzhiyun * __of_node_dup() - Duplicate or create an empty device node dynamically.
411*4882a593Smuzhiyun * @np: if not NULL, contains properties to be duplicated in new node
412*4882a593Smuzhiyun * @full_name: string value to be duplicated into new node's full_name field
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * Create a device tree node, optionally duplicating the properties of
415*4882a593Smuzhiyun * another node. The node data are dynamically allocated and all the node
416*4882a593Smuzhiyun * flags have the OF_DYNAMIC & OF_DETACHED bits set.
417*4882a593Smuzhiyun *
418*4882a593Smuzhiyun * Returns the newly allocated node or NULL on out of memory error.
419*4882a593Smuzhiyun */
__of_node_dup(const struct device_node * np,const char * full_name)420*4882a593Smuzhiyun struct device_node *__of_node_dup(const struct device_node *np,
421*4882a593Smuzhiyun const char *full_name)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct device_node *node;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun node = kzalloc(sizeof(*node), GFP_KERNEL);
426*4882a593Smuzhiyun if (!node)
427*4882a593Smuzhiyun return NULL;
428*4882a593Smuzhiyun node->full_name = kstrdup(full_name, GFP_KERNEL);
429*4882a593Smuzhiyun if (!node->full_name) {
430*4882a593Smuzhiyun kfree(node);
431*4882a593Smuzhiyun return NULL;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun of_node_set_flag(node, OF_DYNAMIC);
435*4882a593Smuzhiyun of_node_set_flag(node, OF_DETACHED);
436*4882a593Smuzhiyun of_node_init(node);
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /* Iterate over and duplicate all properties */
439*4882a593Smuzhiyun if (np) {
440*4882a593Smuzhiyun struct property *pp, *new_pp;
441*4882a593Smuzhiyun for_each_property_of_node(np, pp) {
442*4882a593Smuzhiyun new_pp = __of_prop_dup(pp, GFP_KERNEL);
443*4882a593Smuzhiyun if (!new_pp)
444*4882a593Smuzhiyun goto err_prop;
445*4882a593Smuzhiyun if (__of_add_property(node, new_pp)) {
446*4882a593Smuzhiyun kfree(new_pp->name);
447*4882a593Smuzhiyun kfree(new_pp->value);
448*4882a593Smuzhiyun kfree(new_pp);
449*4882a593Smuzhiyun goto err_prop;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun return node;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun err_prop:
456*4882a593Smuzhiyun of_node_put(node); /* Frees the node and properties */
457*4882a593Smuzhiyun return NULL;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
__of_changeset_entry_destroy(struct of_changeset_entry * ce)460*4882a593Smuzhiyun static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun if (ce->action == OF_RECONFIG_ATTACH_NODE &&
463*4882a593Smuzhiyun of_node_check_flag(ce->np, OF_OVERLAY)) {
464*4882a593Smuzhiyun if (kref_read(&ce->np->kobj.kref) > 1) {
465*4882a593Smuzhiyun pr_err("ERROR: memory leak, expected refcount 1 instead of %d, of_node_get()/of_node_put() unbalanced - destroy cset entry: attach overlay node %pOF\n",
466*4882a593Smuzhiyun kref_read(&ce->np->kobj.kref), ce->np);
467*4882a593Smuzhiyun } else {
468*4882a593Smuzhiyun of_node_set_flag(ce->np, OF_OVERLAY_FREE_CSET);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun of_node_put(ce->np);
473*4882a593Smuzhiyun list_del(&ce->node);
474*4882a593Smuzhiyun kfree(ce);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun #ifdef DEBUG
__of_changeset_entry_dump(struct of_changeset_entry * ce)478*4882a593Smuzhiyun static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun switch (ce->action) {
481*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
482*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
483*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
484*4882a593Smuzhiyun pr_debug("cset<%p> %-15s %pOF/%s\n", ce, action_names[ce->action],
485*4882a593Smuzhiyun ce->np, ce->prop->name);
486*4882a593Smuzhiyun break;
487*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
488*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
489*4882a593Smuzhiyun pr_debug("cset<%p> %-15s %pOF\n", ce, action_names[ce->action],
490*4882a593Smuzhiyun ce->np);
491*4882a593Smuzhiyun break;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun #else
__of_changeset_entry_dump(struct of_changeset_entry * ce)495*4882a593Smuzhiyun static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun /* empty */
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun #endif
500*4882a593Smuzhiyun
__of_changeset_entry_invert(struct of_changeset_entry * ce,struct of_changeset_entry * rce)501*4882a593Smuzhiyun static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
502*4882a593Smuzhiyun struct of_changeset_entry *rce)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun memcpy(rce, ce, sizeof(*rce));
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun switch (ce->action) {
507*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
508*4882a593Smuzhiyun rce->action = OF_RECONFIG_DETACH_NODE;
509*4882a593Smuzhiyun break;
510*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
511*4882a593Smuzhiyun rce->action = OF_RECONFIG_ATTACH_NODE;
512*4882a593Smuzhiyun break;
513*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
514*4882a593Smuzhiyun rce->action = OF_RECONFIG_REMOVE_PROPERTY;
515*4882a593Smuzhiyun break;
516*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
517*4882a593Smuzhiyun rce->action = OF_RECONFIG_ADD_PROPERTY;
518*4882a593Smuzhiyun break;
519*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
520*4882a593Smuzhiyun rce->old_prop = ce->prop;
521*4882a593Smuzhiyun rce->prop = ce->old_prop;
522*4882a593Smuzhiyun /* update was used but original property did not exist */
523*4882a593Smuzhiyun if (!rce->prop) {
524*4882a593Smuzhiyun rce->action = OF_RECONFIG_REMOVE_PROPERTY;
525*4882a593Smuzhiyun rce->prop = ce->prop;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun break;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun
__of_changeset_entry_notify(struct of_changeset_entry * ce,bool revert)531*4882a593Smuzhiyun static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
532*4882a593Smuzhiyun bool revert)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun struct of_reconfig_data rd;
535*4882a593Smuzhiyun struct of_changeset_entry ce_inverted;
536*4882a593Smuzhiyun int ret = 0;
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (revert) {
539*4882a593Smuzhiyun __of_changeset_entry_invert(ce, &ce_inverted);
540*4882a593Smuzhiyun ce = &ce_inverted;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun switch (ce->action) {
544*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
545*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
546*4882a593Smuzhiyun memset(&rd, 0, sizeof(rd));
547*4882a593Smuzhiyun rd.dn = ce->np;
548*4882a593Smuzhiyun ret = of_reconfig_notify(ce->action, &rd);
549*4882a593Smuzhiyun break;
550*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
551*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
552*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
553*4882a593Smuzhiyun ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
554*4882a593Smuzhiyun break;
555*4882a593Smuzhiyun default:
556*4882a593Smuzhiyun pr_err("invalid devicetree changeset action: %i\n",
557*4882a593Smuzhiyun (int)ce->action);
558*4882a593Smuzhiyun ret = -EINVAL;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun if (ret)
562*4882a593Smuzhiyun pr_err("changeset notifier error @%pOF\n", ce->np);
563*4882a593Smuzhiyun return ret;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
__of_changeset_entry_apply(struct of_changeset_entry * ce)566*4882a593Smuzhiyun static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun struct property *old_prop, **propp;
569*4882a593Smuzhiyun unsigned long flags;
570*4882a593Smuzhiyun int ret = 0;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun __of_changeset_entry_dump(ce);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun raw_spin_lock_irqsave(&devtree_lock, flags);
575*4882a593Smuzhiyun switch (ce->action) {
576*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
577*4882a593Smuzhiyun __of_attach_node(ce->np);
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
580*4882a593Smuzhiyun __of_detach_node(ce->np);
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
583*4882a593Smuzhiyun /* If the property is in deadprops then it must be removed */
584*4882a593Smuzhiyun for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
585*4882a593Smuzhiyun if (*propp == ce->prop) {
586*4882a593Smuzhiyun *propp = ce->prop->next;
587*4882a593Smuzhiyun ce->prop->next = NULL;
588*4882a593Smuzhiyun break;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun ret = __of_add_property(ce->np, ce->prop);
593*4882a593Smuzhiyun if (ret) {
594*4882a593Smuzhiyun pr_err("changeset: add_property failed @%pOF/%s\n",
595*4882a593Smuzhiyun ce->np,
596*4882a593Smuzhiyun ce->prop->name);
597*4882a593Smuzhiyun break;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun break;
600*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
601*4882a593Smuzhiyun ret = __of_remove_property(ce->np, ce->prop);
602*4882a593Smuzhiyun if (ret) {
603*4882a593Smuzhiyun pr_err("changeset: remove_property failed @%pOF/%s\n",
604*4882a593Smuzhiyun ce->np,
605*4882a593Smuzhiyun ce->prop->name);
606*4882a593Smuzhiyun break;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun break;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
611*4882a593Smuzhiyun /* If the property is in deadprops then it must be removed */
612*4882a593Smuzhiyun for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
613*4882a593Smuzhiyun if (*propp == ce->prop) {
614*4882a593Smuzhiyun *propp = ce->prop->next;
615*4882a593Smuzhiyun ce->prop->next = NULL;
616*4882a593Smuzhiyun break;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun ret = __of_update_property(ce->np, ce->prop, &old_prop);
621*4882a593Smuzhiyun if (ret) {
622*4882a593Smuzhiyun pr_err("changeset: update_property failed @%pOF/%s\n",
623*4882a593Smuzhiyun ce->np,
624*4882a593Smuzhiyun ce->prop->name);
625*4882a593Smuzhiyun break;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun break;
628*4882a593Smuzhiyun default:
629*4882a593Smuzhiyun ret = -EINVAL;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&devtree_lock, flags);
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun if (ret)
634*4882a593Smuzhiyun return ret;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun switch (ce->action) {
637*4882a593Smuzhiyun case OF_RECONFIG_ATTACH_NODE:
638*4882a593Smuzhiyun __of_attach_node_sysfs(ce->np);
639*4882a593Smuzhiyun break;
640*4882a593Smuzhiyun case OF_RECONFIG_DETACH_NODE:
641*4882a593Smuzhiyun __of_detach_node_sysfs(ce->np);
642*4882a593Smuzhiyun break;
643*4882a593Smuzhiyun case OF_RECONFIG_ADD_PROPERTY:
644*4882a593Smuzhiyun /* ignore duplicate names */
645*4882a593Smuzhiyun __of_add_property_sysfs(ce->np, ce->prop);
646*4882a593Smuzhiyun break;
647*4882a593Smuzhiyun case OF_RECONFIG_REMOVE_PROPERTY:
648*4882a593Smuzhiyun __of_remove_property_sysfs(ce->np, ce->prop);
649*4882a593Smuzhiyun break;
650*4882a593Smuzhiyun case OF_RECONFIG_UPDATE_PROPERTY:
651*4882a593Smuzhiyun __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
652*4882a593Smuzhiyun break;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun return 0;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
__of_changeset_entry_revert(struct of_changeset_entry * ce)658*4882a593Smuzhiyun static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun struct of_changeset_entry ce_inverted;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun __of_changeset_entry_invert(ce, &ce_inverted);
663*4882a593Smuzhiyun return __of_changeset_entry_apply(&ce_inverted);
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun /**
667*4882a593Smuzhiyun * of_changeset_init - Initialize a changeset for use
668*4882a593Smuzhiyun *
669*4882a593Smuzhiyun * @ocs: changeset pointer
670*4882a593Smuzhiyun *
671*4882a593Smuzhiyun * Initialize a changeset structure
672*4882a593Smuzhiyun */
of_changeset_init(struct of_changeset * ocs)673*4882a593Smuzhiyun void of_changeset_init(struct of_changeset *ocs)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun memset(ocs, 0, sizeof(*ocs));
676*4882a593Smuzhiyun INIT_LIST_HEAD(&ocs->entries);
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_changeset_init);
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun /**
681*4882a593Smuzhiyun * of_changeset_destroy - Destroy a changeset
682*4882a593Smuzhiyun *
683*4882a593Smuzhiyun * @ocs: changeset pointer
684*4882a593Smuzhiyun *
685*4882a593Smuzhiyun * Destroys a changeset. Note that if a changeset is applied,
686*4882a593Smuzhiyun * its changes to the tree cannot be reverted.
687*4882a593Smuzhiyun */
of_changeset_destroy(struct of_changeset * ocs)688*4882a593Smuzhiyun void of_changeset_destroy(struct of_changeset *ocs)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun struct of_changeset_entry *ce, *cen;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
693*4882a593Smuzhiyun __of_changeset_entry_destroy(ce);
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_changeset_destroy);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun * Apply the changeset entries in @ocs.
699*4882a593Smuzhiyun * If apply fails, an attempt is made to revert the entries that were
700*4882a593Smuzhiyun * successfully applied.
701*4882a593Smuzhiyun *
702*4882a593Smuzhiyun * If multiple revert errors occur then only the final revert error is reported.
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
705*4882a593Smuzhiyun * If a revert error occurs, it is returned in *ret_revert.
706*4882a593Smuzhiyun */
__of_changeset_apply_entries(struct of_changeset * ocs,int * ret_revert)707*4882a593Smuzhiyun int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun struct of_changeset_entry *ce;
710*4882a593Smuzhiyun int ret, ret_tmp;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun pr_debug("changeset: applying...\n");
713*4882a593Smuzhiyun list_for_each_entry(ce, &ocs->entries, node) {
714*4882a593Smuzhiyun ret = __of_changeset_entry_apply(ce);
715*4882a593Smuzhiyun if (ret) {
716*4882a593Smuzhiyun pr_err("Error applying changeset (%d)\n", ret);
717*4882a593Smuzhiyun list_for_each_entry_continue_reverse(ce, &ocs->entries,
718*4882a593Smuzhiyun node) {
719*4882a593Smuzhiyun ret_tmp = __of_changeset_entry_revert(ce);
720*4882a593Smuzhiyun if (ret_tmp)
721*4882a593Smuzhiyun *ret_revert = ret_tmp;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun return ret;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun return 0;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun
730*4882a593Smuzhiyun /*
731*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
732*4882a593Smuzhiyun *
733*4882a593Smuzhiyun * If multiple changeset entry notification errors occur then only the
734*4882a593Smuzhiyun * final notification error is reported.
735*4882a593Smuzhiyun */
__of_changeset_apply_notify(struct of_changeset * ocs)736*4882a593Smuzhiyun int __of_changeset_apply_notify(struct of_changeset *ocs)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun struct of_changeset_entry *ce;
739*4882a593Smuzhiyun int ret = 0, ret_tmp;
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun pr_debug("changeset: emitting notifiers.\n");
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /* drop the global lock while emitting notifiers */
744*4882a593Smuzhiyun mutex_unlock(&of_mutex);
745*4882a593Smuzhiyun list_for_each_entry(ce, &ocs->entries, node) {
746*4882a593Smuzhiyun ret_tmp = __of_changeset_entry_notify(ce, 0);
747*4882a593Smuzhiyun if (ret_tmp)
748*4882a593Smuzhiyun ret = ret_tmp;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun mutex_lock(&of_mutex);
751*4882a593Smuzhiyun pr_debug("changeset: notifiers sent.\n");
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun return ret;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun /*
757*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
758*4882a593Smuzhiyun *
759*4882a593Smuzhiyun * If a changeset entry apply fails, an attempt is made to revert any
760*4882a593Smuzhiyun * previous entries in the changeset. If any of the reverts fails,
761*4882a593Smuzhiyun * that failure is not reported. Thus the state of the device tree
762*4882a593Smuzhiyun * is unknown if an apply error occurs.
763*4882a593Smuzhiyun */
__of_changeset_apply(struct of_changeset * ocs)764*4882a593Smuzhiyun static int __of_changeset_apply(struct of_changeset *ocs)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun int ret, ret_revert = 0;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun ret = __of_changeset_apply_entries(ocs, &ret_revert);
769*4882a593Smuzhiyun if (!ret)
770*4882a593Smuzhiyun ret = __of_changeset_apply_notify(ocs);
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun return ret;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /**
776*4882a593Smuzhiyun * of_changeset_apply - Applies a changeset
777*4882a593Smuzhiyun *
778*4882a593Smuzhiyun * @ocs: changeset pointer
779*4882a593Smuzhiyun *
780*4882a593Smuzhiyun * Applies a changeset to the live tree.
781*4882a593Smuzhiyun * Any side-effects of live tree state changes are applied here on
782*4882a593Smuzhiyun * success, like creation/destruction of devices and side-effects
783*4882a593Smuzhiyun * like creation of sysfs properties and directories.
784*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
785*4882a593Smuzhiyun * On error the partially applied effects are reverted.
786*4882a593Smuzhiyun */
of_changeset_apply(struct of_changeset * ocs)787*4882a593Smuzhiyun int of_changeset_apply(struct of_changeset *ocs)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun int ret;
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun mutex_lock(&of_mutex);
792*4882a593Smuzhiyun ret = __of_changeset_apply(ocs);
793*4882a593Smuzhiyun mutex_unlock(&of_mutex);
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun return ret;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_changeset_apply);
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun /*
800*4882a593Smuzhiyun * Revert the changeset entries in @ocs.
801*4882a593Smuzhiyun * If revert fails, an attempt is made to re-apply the entries that were
802*4882a593Smuzhiyun * successfully removed.
803*4882a593Smuzhiyun *
804*4882a593Smuzhiyun * If multiple re-apply errors occur then only the final apply error is
805*4882a593Smuzhiyun * reported.
806*4882a593Smuzhiyun *
807*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
808*4882a593Smuzhiyun * If an apply error occurs, it is returned in *ret_apply.
809*4882a593Smuzhiyun */
__of_changeset_revert_entries(struct of_changeset * ocs,int * ret_apply)810*4882a593Smuzhiyun int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun struct of_changeset_entry *ce;
813*4882a593Smuzhiyun int ret, ret_tmp;
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun pr_debug("changeset: reverting...\n");
816*4882a593Smuzhiyun list_for_each_entry_reverse(ce, &ocs->entries, node) {
817*4882a593Smuzhiyun ret = __of_changeset_entry_revert(ce);
818*4882a593Smuzhiyun if (ret) {
819*4882a593Smuzhiyun pr_err("Error reverting changeset (%d)\n", ret);
820*4882a593Smuzhiyun list_for_each_entry_continue(ce, &ocs->entries, node) {
821*4882a593Smuzhiyun ret_tmp = __of_changeset_entry_apply(ce);
822*4882a593Smuzhiyun if (ret_tmp)
823*4882a593Smuzhiyun *ret_apply = ret_tmp;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun return ret;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun return 0;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /*
833*4882a593Smuzhiyun * If multiple changeset entry notification errors occur then only the
834*4882a593Smuzhiyun * final notification error is reported.
835*4882a593Smuzhiyun */
__of_changeset_revert_notify(struct of_changeset * ocs)836*4882a593Smuzhiyun int __of_changeset_revert_notify(struct of_changeset *ocs)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun struct of_changeset_entry *ce;
839*4882a593Smuzhiyun int ret = 0, ret_tmp;
840*4882a593Smuzhiyun
841*4882a593Smuzhiyun pr_debug("changeset: emitting notifiers.\n");
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun /* drop the global lock while emitting notifiers */
844*4882a593Smuzhiyun mutex_unlock(&of_mutex);
845*4882a593Smuzhiyun list_for_each_entry_reverse(ce, &ocs->entries, node) {
846*4882a593Smuzhiyun ret_tmp = __of_changeset_entry_notify(ce, 1);
847*4882a593Smuzhiyun if (ret_tmp)
848*4882a593Smuzhiyun ret = ret_tmp;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun mutex_lock(&of_mutex);
851*4882a593Smuzhiyun pr_debug("changeset: notifiers sent.\n");
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun return ret;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
__of_changeset_revert(struct of_changeset * ocs)856*4882a593Smuzhiyun static int __of_changeset_revert(struct of_changeset *ocs)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun int ret, ret_reply;
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun ret_reply = 0;
861*4882a593Smuzhiyun ret = __of_changeset_revert_entries(ocs, &ret_reply);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun if (!ret)
864*4882a593Smuzhiyun ret = __of_changeset_revert_notify(ocs);
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun return ret;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun /**
870*4882a593Smuzhiyun * of_changeset_revert - Reverts an applied changeset
871*4882a593Smuzhiyun *
872*4882a593Smuzhiyun * @ocs: changeset pointer
873*4882a593Smuzhiyun *
874*4882a593Smuzhiyun * Reverts a changeset returning the state of the tree to what it
875*4882a593Smuzhiyun * was before the application.
876*4882a593Smuzhiyun * Any side-effects like creation/destruction of devices and
877*4882a593Smuzhiyun * removal of sysfs properties and directories are applied.
878*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
879*4882a593Smuzhiyun */
of_changeset_revert(struct of_changeset * ocs)880*4882a593Smuzhiyun int of_changeset_revert(struct of_changeset *ocs)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun int ret;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun mutex_lock(&of_mutex);
885*4882a593Smuzhiyun ret = __of_changeset_revert(ocs);
886*4882a593Smuzhiyun mutex_unlock(&of_mutex);
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun return ret;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_changeset_revert);
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun /**
893*4882a593Smuzhiyun * of_changeset_action - Add an action to the tail of the changeset list
894*4882a593Smuzhiyun *
895*4882a593Smuzhiyun * @ocs: changeset pointer
896*4882a593Smuzhiyun * @action: action to perform
897*4882a593Smuzhiyun * @np: Pointer to device node
898*4882a593Smuzhiyun * @prop: Pointer to property
899*4882a593Smuzhiyun *
900*4882a593Smuzhiyun * On action being one of:
901*4882a593Smuzhiyun * + OF_RECONFIG_ATTACH_NODE
902*4882a593Smuzhiyun * + OF_RECONFIG_DETACH_NODE,
903*4882a593Smuzhiyun * + OF_RECONFIG_ADD_PROPERTY
904*4882a593Smuzhiyun * + OF_RECONFIG_REMOVE_PROPERTY,
905*4882a593Smuzhiyun * + OF_RECONFIG_UPDATE_PROPERTY
906*4882a593Smuzhiyun * Returns 0 on success, a negative error value in case of an error.
907*4882a593Smuzhiyun */
of_changeset_action(struct of_changeset * ocs,unsigned long action,struct device_node * np,struct property * prop)908*4882a593Smuzhiyun int of_changeset_action(struct of_changeset *ocs, unsigned long action,
909*4882a593Smuzhiyun struct device_node *np, struct property *prop)
910*4882a593Smuzhiyun {
911*4882a593Smuzhiyun struct of_changeset_entry *ce;
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun ce = kzalloc(sizeof(*ce), GFP_KERNEL);
914*4882a593Smuzhiyun if (!ce)
915*4882a593Smuzhiyun return -ENOMEM;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun /* get a reference to the node */
918*4882a593Smuzhiyun ce->action = action;
919*4882a593Smuzhiyun ce->np = of_node_get(np);
920*4882a593Smuzhiyun ce->prop = prop;
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
923*4882a593Smuzhiyun ce->old_prop = of_find_property(np, prop->name, NULL);
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* add it to the list */
926*4882a593Smuzhiyun list_add_tail(&ce->node, &ocs->entries);
927*4882a593Smuzhiyun return 0;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(of_changeset_action);
930