dynamic.c 20 KB

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