dynamic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. * @np: if not NULL, contains properties to be duplicated in new node
  330. * @full_name: string value to be duplicated into new node's full_name field
  331. *
  332. * Create a device tree node, optionally duplicating the properties of
  333. * another node. The node data are dynamically allocated and all the node
  334. * flags have the OF_DYNAMIC & OF_DETACHED bits set.
  335. *
  336. * Returns the newly allocated node or NULL on out of memory error.
  337. */
  338. struct device_node *__of_node_dup(const struct device_node *np,
  339. const char *full_name)
  340. {
  341. struct device_node *node;
  342. node = kzalloc(sizeof(*node), GFP_KERNEL);
  343. if (!node)
  344. return NULL;
  345. node->full_name = kstrdup(full_name, GFP_KERNEL);
  346. if (!node->full_name) {
  347. kfree(node);
  348. return NULL;
  349. }
  350. of_node_set_flag(node, OF_DYNAMIC);
  351. of_node_set_flag(node, OF_DETACHED);
  352. of_node_init(node);
  353. /* Iterate over and duplicate all properties */
  354. if (np) {
  355. struct property *pp, *new_pp;
  356. for_each_property_of_node(np, pp) {
  357. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  358. if (!new_pp)
  359. goto err_prop;
  360. if (__of_add_property(node, new_pp)) {
  361. kfree(new_pp->name);
  362. kfree(new_pp->value);
  363. kfree(new_pp);
  364. goto err_prop;
  365. }
  366. }
  367. }
  368. return node;
  369. err_prop:
  370. of_node_put(node); /* Frees the node and properties */
  371. return NULL;
  372. }
  373. static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
  374. {
  375. of_node_put(ce->np);
  376. list_del(&ce->node);
  377. kfree(ce);
  378. }
  379. #ifdef DEBUG
  380. static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  381. {
  382. switch (ce->action) {
  383. case OF_RECONFIG_ADD_PROPERTY:
  384. case OF_RECONFIG_REMOVE_PROPERTY:
  385. case OF_RECONFIG_UPDATE_PROPERTY:
  386. pr_debug("cset<%p> %-15s %pOF/%s\n", ce, action_names[ce->action],
  387. ce->np, ce->prop->name);
  388. break;
  389. case OF_RECONFIG_ATTACH_NODE:
  390. case OF_RECONFIG_DETACH_NODE:
  391. pr_debug("cset<%p> %-15s %pOF\n", ce, action_names[ce->action],
  392. ce->np);
  393. break;
  394. }
  395. }
  396. #else
  397. static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  398. {
  399. /* empty */
  400. }
  401. #endif
  402. static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
  403. struct of_changeset_entry *rce)
  404. {
  405. memcpy(rce, ce, sizeof(*rce));
  406. switch (ce->action) {
  407. case OF_RECONFIG_ATTACH_NODE:
  408. rce->action = OF_RECONFIG_DETACH_NODE;
  409. break;
  410. case OF_RECONFIG_DETACH_NODE:
  411. rce->action = OF_RECONFIG_ATTACH_NODE;
  412. break;
  413. case OF_RECONFIG_ADD_PROPERTY:
  414. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  415. break;
  416. case OF_RECONFIG_REMOVE_PROPERTY:
  417. rce->action = OF_RECONFIG_ADD_PROPERTY;
  418. break;
  419. case OF_RECONFIG_UPDATE_PROPERTY:
  420. rce->old_prop = ce->prop;
  421. rce->prop = ce->old_prop;
  422. /* update was used but original property did not exist */
  423. if (!rce->prop) {
  424. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  425. rce->prop = ce->prop;
  426. }
  427. break;
  428. }
  429. }
  430. static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
  431. bool revert)
  432. {
  433. struct of_reconfig_data rd;
  434. struct of_changeset_entry ce_inverted;
  435. int ret = 0;
  436. if (revert) {
  437. __of_changeset_entry_invert(ce, &ce_inverted);
  438. ce = &ce_inverted;
  439. }
  440. switch (ce->action) {
  441. case OF_RECONFIG_ATTACH_NODE:
  442. case OF_RECONFIG_DETACH_NODE:
  443. memset(&rd, 0, sizeof(rd));
  444. rd.dn = ce->np;
  445. ret = of_reconfig_notify(ce->action, &rd);
  446. break;
  447. case OF_RECONFIG_ADD_PROPERTY:
  448. case OF_RECONFIG_REMOVE_PROPERTY:
  449. case OF_RECONFIG_UPDATE_PROPERTY:
  450. ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
  451. break;
  452. default:
  453. pr_err("invalid devicetree changeset action: %i\n",
  454. (int)ce->action);
  455. ret = -EINVAL;
  456. }
  457. if (ret)
  458. pr_err("changeset notifier error @%pOF\n", ce->np);
  459. return ret;
  460. }
  461. static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
  462. {
  463. struct property *old_prop, **propp;
  464. unsigned long flags;
  465. int ret = 0;
  466. __of_changeset_entry_dump(ce);
  467. raw_spin_lock_irqsave(&devtree_lock, flags);
  468. switch (ce->action) {
  469. case OF_RECONFIG_ATTACH_NODE:
  470. __of_attach_node(ce->np);
  471. break;
  472. case OF_RECONFIG_DETACH_NODE:
  473. __of_detach_node(ce->np);
  474. break;
  475. case OF_RECONFIG_ADD_PROPERTY:
  476. /* If the property is in deadprops then it must be removed */
  477. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  478. if (*propp == ce->prop) {
  479. *propp = ce->prop->next;
  480. ce->prop->next = NULL;
  481. break;
  482. }
  483. }
  484. ret = __of_add_property(ce->np, ce->prop);
  485. if (ret) {
  486. pr_err("changeset: add_property failed @%pOF/%s\n",
  487. ce->np,
  488. ce->prop->name);
  489. break;
  490. }
  491. break;
  492. case OF_RECONFIG_REMOVE_PROPERTY:
  493. ret = __of_remove_property(ce->np, ce->prop);
  494. if (ret) {
  495. pr_err("changeset: remove_property failed @%pOF/%s\n",
  496. ce->np,
  497. ce->prop->name);
  498. break;
  499. }
  500. break;
  501. case OF_RECONFIG_UPDATE_PROPERTY:
  502. /* If the property is in deadprops then it must be removed */
  503. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  504. if (*propp == ce->prop) {
  505. *propp = ce->prop->next;
  506. ce->prop->next = NULL;
  507. break;
  508. }
  509. }
  510. ret = __of_update_property(ce->np, ce->prop, &old_prop);
  511. if (ret) {
  512. pr_err("changeset: update_property failed @%pOF/%s\n",
  513. ce->np,
  514. ce->prop->name);
  515. break;
  516. }
  517. break;
  518. default:
  519. ret = -EINVAL;
  520. }
  521. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  522. if (ret)
  523. return ret;
  524. switch (ce->action) {
  525. case OF_RECONFIG_ATTACH_NODE:
  526. __of_attach_node_sysfs(ce->np);
  527. break;
  528. case OF_RECONFIG_DETACH_NODE:
  529. __of_detach_node_sysfs(ce->np);
  530. break;
  531. case OF_RECONFIG_ADD_PROPERTY:
  532. /* ignore duplicate names */
  533. __of_add_property_sysfs(ce->np, ce->prop);
  534. break;
  535. case OF_RECONFIG_REMOVE_PROPERTY:
  536. __of_remove_property_sysfs(ce->np, ce->prop);
  537. break;
  538. case OF_RECONFIG_UPDATE_PROPERTY:
  539. __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
  540. break;
  541. }
  542. return 0;
  543. }
  544. static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
  545. {
  546. struct of_changeset_entry ce_inverted;
  547. __of_changeset_entry_invert(ce, &ce_inverted);
  548. return __of_changeset_entry_apply(&ce_inverted);
  549. }
  550. /**
  551. * of_changeset_init - Initialize a changeset for use
  552. *
  553. * @ocs: changeset pointer
  554. *
  555. * Initialize a changeset structure
  556. */
  557. void of_changeset_init(struct of_changeset *ocs)
  558. {
  559. memset(ocs, 0, sizeof(*ocs));
  560. INIT_LIST_HEAD(&ocs->entries);
  561. }
  562. EXPORT_SYMBOL_GPL(of_changeset_init);
  563. /**
  564. * of_changeset_destroy - Destroy a changeset
  565. *
  566. * @ocs: changeset pointer
  567. *
  568. * Destroys a changeset. Note that if a changeset is applied,
  569. * its changes to the tree cannot be reverted.
  570. */
  571. void of_changeset_destroy(struct of_changeset *ocs)
  572. {
  573. struct of_changeset_entry *ce, *cen;
  574. list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
  575. __of_changeset_entry_destroy(ce);
  576. }
  577. EXPORT_SYMBOL_GPL(of_changeset_destroy);
  578. /*
  579. * Apply the changeset entries in @ocs.
  580. * If apply fails, an attempt is made to revert the entries that were
  581. * successfully applied.
  582. *
  583. * If multiple revert errors occur then only the final revert error is reported.
  584. *
  585. * Returns 0 on success, a negative error value in case of an error.
  586. * If a revert error occurs, it is returned in *ret_revert.
  587. */
  588. int __of_changeset_apply_entries(struct of_changeset *ocs, int *ret_revert)
  589. {
  590. struct of_changeset_entry *ce;
  591. int ret, ret_tmp;
  592. pr_debug("changeset: applying...\n");
  593. list_for_each_entry(ce, &ocs->entries, node) {
  594. ret = __of_changeset_entry_apply(ce);
  595. if (ret) {
  596. pr_err("Error applying changeset (%d)\n", ret);
  597. list_for_each_entry_continue_reverse(ce, &ocs->entries,
  598. node) {
  599. ret_tmp = __of_changeset_entry_revert(ce);
  600. if (ret_tmp)
  601. *ret_revert = ret_tmp;
  602. }
  603. return ret;
  604. }
  605. }
  606. return 0;
  607. }
  608. /*
  609. * Returns 0 on success, a negative error value in case of an error.
  610. *
  611. * If multiple changeset entry notification errors occur then only the
  612. * final notification error is reported.
  613. */
  614. int __of_changeset_apply_notify(struct of_changeset *ocs)
  615. {
  616. struct of_changeset_entry *ce;
  617. int ret = 0, ret_tmp;
  618. pr_debug("changeset: emitting notifiers.\n");
  619. /* drop the global lock while emitting notifiers */
  620. mutex_unlock(&of_mutex);
  621. list_for_each_entry(ce, &ocs->entries, node) {
  622. ret_tmp = __of_changeset_entry_notify(ce, 0);
  623. if (ret_tmp)
  624. ret = ret_tmp;
  625. }
  626. mutex_lock(&of_mutex);
  627. pr_debug("changeset: notifiers sent.\n");
  628. return ret;
  629. }
  630. /*
  631. * Returns 0 on success, a negative error value in case of an error.
  632. *
  633. * If a changeset entry apply fails, an attempt is made to revert any
  634. * previous entries in the changeset. If any of the reverts fails,
  635. * that failure is not reported. Thus the state of the device tree
  636. * is unknown if an apply error occurs.
  637. */
  638. static int __of_changeset_apply(struct of_changeset *ocs)
  639. {
  640. int ret, ret_revert = 0;
  641. ret = __of_changeset_apply_entries(ocs, &ret_revert);
  642. if (!ret)
  643. ret = __of_changeset_apply_notify(ocs);
  644. return ret;
  645. }
  646. /**
  647. * of_changeset_apply - Applies a changeset
  648. *
  649. * @ocs: changeset pointer
  650. *
  651. * Applies a changeset to the live tree.
  652. * Any side-effects of live tree state changes are applied here on
  653. * success, like creation/destruction of devices and side-effects
  654. * like creation of sysfs properties and directories.
  655. * Returns 0 on success, a negative error value in case of an error.
  656. * On error the partially applied effects are reverted.
  657. */
  658. int of_changeset_apply(struct of_changeset *ocs)
  659. {
  660. int ret;
  661. mutex_lock(&of_mutex);
  662. ret = __of_changeset_apply(ocs);
  663. mutex_unlock(&of_mutex);
  664. return ret;
  665. }
  666. EXPORT_SYMBOL_GPL(of_changeset_apply);
  667. /*
  668. * Revert the changeset entries in @ocs.
  669. * If revert fails, an attempt is made to re-apply the entries that were
  670. * successfully removed.
  671. *
  672. * If multiple re-apply errors occur then only the final apply error is
  673. * reported.
  674. *
  675. * Returns 0 on success, a negative error value in case of an error.
  676. * If an apply error occurs, it is returned in *ret_apply.
  677. */
  678. int __of_changeset_revert_entries(struct of_changeset *ocs, int *ret_apply)
  679. {
  680. struct of_changeset_entry *ce;
  681. int ret, ret_tmp;
  682. pr_debug("changeset: reverting...\n");
  683. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  684. ret = __of_changeset_entry_revert(ce);
  685. if (ret) {
  686. pr_err("Error reverting changeset (%d)\n", ret);
  687. list_for_each_entry_continue(ce, &ocs->entries, node) {
  688. ret_tmp = __of_changeset_entry_apply(ce);
  689. if (ret_tmp)
  690. *ret_apply = ret_tmp;
  691. }
  692. return ret;
  693. }
  694. }
  695. return 0;
  696. }
  697. /*
  698. * If multiple changeset entry notification errors occur then only the
  699. * final notification error is reported.
  700. */
  701. int __of_changeset_revert_notify(struct of_changeset *ocs)
  702. {
  703. struct of_changeset_entry *ce;
  704. int ret = 0, ret_tmp;
  705. pr_debug("changeset: emitting notifiers.\n");
  706. /* drop the global lock while emitting notifiers */
  707. mutex_unlock(&of_mutex);
  708. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  709. ret_tmp = __of_changeset_entry_notify(ce, 1);
  710. if (ret_tmp)
  711. ret = ret_tmp;
  712. }
  713. mutex_lock(&of_mutex);
  714. pr_debug("changeset: notifiers sent.\n");
  715. return ret;
  716. }
  717. static int __of_changeset_revert(struct of_changeset *ocs)
  718. {
  719. int ret, ret_reply;
  720. ret_reply = 0;
  721. ret = __of_changeset_revert_entries(ocs, &ret_reply);
  722. if (!ret)
  723. ret = __of_changeset_revert_notify(ocs);
  724. return ret;
  725. }
  726. /**
  727. * of_changeset_revert - Reverts an applied changeset
  728. *
  729. * @ocs: changeset pointer
  730. *
  731. * Reverts a changeset returning the state of the tree to what it
  732. * was before the application.
  733. * Any side-effects like creation/destruction of devices and
  734. * removal of sysfs properties and directories are applied.
  735. * Returns 0 on success, a negative error value in case of an error.
  736. */
  737. int of_changeset_revert(struct of_changeset *ocs)
  738. {
  739. int ret;
  740. mutex_lock(&of_mutex);
  741. ret = __of_changeset_revert(ocs);
  742. mutex_unlock(&of_mutex);
  743. return ret;
  744. }
  745. EXPORT_SYMBOL_GPL(of_changeset_revert);
  746. /**
  747. * of_changeset_action - Add an action to the tail of the changeset list
  748. *
  749. * @ocs: changeset pointer
  750. * @action: action to perform
  751. * @np: Pointer to device node
  752. * @prop: Pointer to property
  753. *
  754. * On action being one of:
  755. * + OF_RECONFIG_ATTACH_NODE
  756. * + OF_RECONFIG_DETACH_NODE,
  757. * + OF_RECONFIG_ADD_PROPERTY
  758. * + OF_RECONFIG_REMOVE_PROPERTY,
  759. * + OF_RECONFIG_UPDATE_PROPERTY
  760. * Returns 0 on success, a negative error value in case of an error.
  761. */
  762. int of_changeset_action(struct of_changeset *ocs, unsigned long action,
  763. struct device_node *np, struct property *prop)
  764. {
  765. struct of_changeset_entry *ce;
  766. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  767. if (!ce)
  768. return -ENOMEM;
  769. /* get a reference to the node */
  770. ce->action = action;
  771. ce->np = of_node_get(np);
  772. ce->prop = prop;
  773. if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
  774. ce->old_prop = of_find_property(np, prop->name, NULL);
  775. /* add it to the list */
  776. list_add_tail(&ce->node, &ocs->entries);
  777. return 0;
  778. }
  779. EXPORT_SYMBOL_GPL(of_changeset_action);