dynamic.c 20 KB

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