dynamic.c 19 KB

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