dynamic.c 22 KB

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