dynamic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. /**
  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. struct property *prop = node->properties;
  277. /* We should never be releasing nodes that haven't been detached. */
  278. if (!of_node_check_flag(node, OF_DETACHED)) {
  279. pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
  280. dump_stack();
  281. return;
  282. }
  283. if (!of_node_check_flag(node, OF_DYNAMIC))
  284. return;
  285. while (prop) {
  286. struct property *next = prop->next;
  287. kfree(prop->name);
  288. kfree(prop->value);
  289. kfree(prop);
  290. prop = next;
  291. if (!prop) {
  292. prop = node->deadprops;
  293. node->deadprops = NULL;
  294. }
  295. }
  296. kfree(node->full_name);
  297. kfree(node->data);
  298. kfree(node);
  299. }
  300. /**
  301. * __of_prop_dup - Copy a property dynamically.
  302. * @prop: Property to copy
  303. * @allocflags: Allocation flags (typically pass GFP_KERNEL)
  304. *
  305. * Copy a property by dynamically allocating the memory of both the
  306. * property structure and the property name & contents. The property's
  307. * flags have the OF_DYNAMIC bit set so that we can differentiate between
  308. * dynamically allocated properties and not.
  309. * Returns the newly allocated property or NULL on out of memory error.
  310. */
  311. struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
  312. {
  313. struct property *new;
  314. new = kzalloc(sizeof(*new), allocflags);
  315. if (!new)
  316. return NULL;
  317. /*
  318. * NOTE: There is no check for zero length value.
  319. * In case of a boolean property, this will allocate a value
  320. * of zero bytes. We do this to work around the use
  321. * of of_get_property() calls on boolean values.
  322. */
  323. new->name = kstrdup(prop->name, allocflags);
  324. new->value = kmemdup(prop->value, prop->length, allocflags);
  325. new->length = prop->length;
  326. if (!new->name || !new->value)
  327. goto err_free;
  328. /* mark the property as dynamic */
  329. of_property_set_flag(new, OF_DYNAMIC);
  330. return new;
  331. err_free:
  332. kfree(new->name);
  333. kfree(new->value);
  334. kfree(new);
  335. return NULL;
  336. }
  337. /**
  338. * __of_node_dup() - Duplicate or create an empty device node dynamically.
  339. * @fmt: Format string (plus vargs) for new full name of the device node
  340. *
  341. * Create an device tree node, either by duplicating an empty node or by allocating
  342. * an empty one suitable for further modification. The node data are
  343. * dynamically allocated and all the node flags have the OF_DYNAMIC &
  344. * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of
  345. * memory error.
  346. */
  347. struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
  348. {
  349. va_list vargs;
  350. struct device_node *node;
  351. node = kzalloc(sizeof(*node), GFP_KERNEL);
  352. if (!node)
  353. return NULL;
  354. va_start(vargs, fmt);
  355. node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
  356. va_end(vargs);
  357. if (!node->full_name) {
  358. kfree(node);
  359. return NULL;
  360. }
  361. of_node_set_flag(node, OF_DYNAMIC);
  362. of_node_set_flag(node, OF_DETACHED);
  363. of_node_init(node);
  364. /* Iterate over and duplicate all properties */
  365. if (np) {
  366. struct property *pp, *new_pp;
  367. for_each_property_of_node(np, pp) {
  368. new_pp = __of_prop_dup(pp, GFP_KERNEL);
  369. if (!new_pp)
  370. goto err_prop;
  371. if (__of_add_property(node, new_pp)) {
  372. kfree(new_pp->name);
  373. kfree(new_pp->value);
  374. kfree(new_pp);
  375. goto err_prop;
  376. }
  377. }
  378. }
  379. return node;
  380. err_prop:
  381. of_node_put(node); /* Frees the node and properties */
  382. return NULL;
  383. }
  384. static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
  385. {
  386. of_node_put(ce->np);
  387. list_del(&ce->node);
  388. kfree(ce);
  389. }
  390. #ifdef DEBUG
  391. static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  392. {
  393. switch (ce->action) {
  394. case OF_RECONFIG_ADD_PROPERTY:
  395. case OF_RECONFIG_REMOVE_PROPERTY:
  396. case OF_RECONFIG_UPDATE_PROPERTY:
  397. pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
  398. ce->np->full_name, ce->prop->name);
  399. break;
  400. case OF_RECONFIG_ATTACH_NODE:
  401. case OF_RECONFIG_DETACH_NODE:
  402. pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
  403. ce->np->full_name);
  404. break;
  405. }
  406. }
  407. #else
  408. static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
  409. {
  410. /* empty */
  411. }
  412. #endif
  413. static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
  414. struct of_changeset_entry *rce)
  415. {
  416. memcpy(rce, ce, sizeof(*rce));
  417. switch (ce->action) {
  418. case OF_RECONFIG_ATTACH_NODE:
  419. rce->action = OF_RECONFIG_DETACH_NODE;
  420. break;
  421. case OF_RECONFIG_DETACH_NODE:
  422. rce->action = OF_RECONFIG_ATTACH_NODE;
  423. break;
  424. case OF_RECONFIG_ADD_PROPERTY:
  425. rce->action = OF_RECONFIG_REMOVE_PROPERTY;
  426. break;
  427. case OF_RECONFIG_REMOVE_PROPERTY:
  428. rce->action = OF_RECONFIG_ADD_PROPERTY;
  429. break;
  430. case OF_RECONFIG_UPDATE_PROPERTY:
  431. rce->old_prop = ce->prop;
  432. rce->prop = ce->old_prop;
  433. break;
  434. }
  435. }
  436. static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
  437. {
  438. struct of_reconfig_data rd;
  439. struct of_changeset_entry ce_inverted;
  440. int ret;
  441. if (revert) {
  442. __of_changeset_entry_invert(ce, &ce_inverted);
  443. ce = &ce_inverted;
  444. }
  445. switch (ce->action) {
  446. case OF_RECONFIG_ATTACH_NODE:
  447. case OF_RECONFIG_DETACH_NODE:
  448. memset(&rd, 0, sizeof(rd));
  449. rd.dn = ce->np;
  450. ret = of_reconfig_notify(ce->action, &rd);
  451. break;
  452. case OF_RECONFIG_ADD_PROPERTY:
  453. case OF_RECONFIG_REMOVE_PROPERTY:
  454. case OF_RECONFIG_UPDATE_PROPERTY:
  455. ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
  456. break;
  457. default:
  458. pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
  459. (int)ce->action);
  460. return;
  461. }
  462. if (ret)
  463. pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
  464. }
  465. static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
  466. {
  467. struct property *old_prop, **propp;
  468. unsigned long flags;
  469. int ret = 0;
  470. __of_changeset_entry_dump(ce);
  471. raw_spin_lock_irqsave(&devtree_lock, flags);
  472. switch (ce->action) {
  473. case OF_RECONFIG_ATTACH_NODE:
  474. __of_attach_node(ce->np);
  475. break;
  476. case OF_RECONFIG_DETACH_NODE:
  477. __of_detach_node(ce->np);
  478. break;
  479. case OF_RECONFIG_ADD_PROPERTY:
  480. /* If the property is in deadprops then it must be removed */
  481. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  482. if (*propp == ce->prop) {
  483. *propp = ce->prop->next;
  484. ce->prop->next = NULL;
  485. break;
  486. }
  487. }
  488. ret = __of_add_property(ce->np, ce->prop);
  489. if (ret) {
  490. pr_err("%s: add_property failed @%s/%s\n",
  491. __func__, ce->np->full_name,
  492. ce->prop->name);
  493. break;
  494. }
  495. break;
  496. case OF_RECONFIG_REMOVE_PROPERTY:
  497. ret = __of_remove_property(ce->np, ce->prop);
  498. if (ret) {
  499. pr_err("%s: remove_property failed @%s/%s\n",
  500. __func__, ce->np->full_name,
  501. ce->prop->name);
  502. break;
  503. }
  504. break;
  505. case OF_RECONFIG_UPDATE_PROPERTY:
  506. /* If the property is in deadprops then it must be removed */
  507. for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
  508. if (*propp == ce->prop) {
  509. *propp = ce->prop->next;
  510. ce->prop->next = NULL;
  511. break;
  512. }
  513. }
  514. ret = __of_update_property(ce->np, ce->prop, &old_prop);
  515. if (ret) {
  516. pr_err("%s: update_property failed @%s/%s\n",
  517. __func__, ce->np->full_name,
  518. ce->prop->name);
  519. break;
  520. }
  521. break;
  522. default:
  523. ret = -EINVAL;
  524. }
  525. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  526. if (ret)
  527. return ret;
  528. switch (ce->action) {
  529. case OF_RECONFIG_ATTACH_NODE:
  530. __of_attach_node_sysfs(ce->np);
  531. break;
  532. case OF_RECONFIG_DETACH_NODE:
  533. __of_detach_node_sysfs(ce->np);
  534. break;
  535. case OF_RECONFIG_ADD_PROPERTY:
  536. /* ignore duplicate names */
  537. __of_add_property_sysfs(ce->np, ce->prop);
  538. break;
  539. case OF_RECONFIG_REMOVE_PROPERTY:
  540. __of_remove_property_sysfs(ce->np, ce->prop);
  541. break;
  542. case OF_RECONFIG_UPDATE_PROPERTY:
  543. __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
  544. break;
  545. }
  546. return 0;
  547. }
  548. static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
  549. {
  550. struct of_changeset_entry ce_inverted;
  551. __of_changeset_entry_invert(ce, &ce_inverted);
  552. return __of_changeset_entry_apply(&ce_inverted);
  553. }
  554. /**
  555. * of_changeset_init - Initialize a changeset for use
  556. *
  557. * @ocs: changeset pointer
  558. *
  559. * Initialize a changeset structure
  560. */
  561. void of_changeset_init(struct of_changeset *ocs)
  562. {
  563. memset(ocs, 0, sizeof(*ocs));
  564. INIT_LIST_HEAD(&ocs->entries);
  565. }
  566. /**
  567. * of_changeset_destroy - Destroy a changeset
  568. *
  569. * @ocs: changeset pointer
  570. *
  571. * Destroys a changeset. Note that if a changeset is applied,
  572. * its changes to the tree cannot be reverted.
  573. */
  574. void of_changeset_destroy(struct of_changeset *ocs)
  575. {
  576. struct of_changeset_entry *ce, *cen;
  577. list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
  578. __of_changeset_entry_destroy(ce);
  579. }
  580. /**
  581. * of_changeset_apply - Applies a changeset
  582. *
  583. * @ocs: changeset pointer
  584. *
  585. * Applies a changeset to the live tree.
  586. * Any side-effects of live tree state changes are applied here on
  587. * sucess, like creation/destruction of devices and side-effects
  588. * like creation of sysfs properties and directories.
  589. * Returns 0 on success, a negative error value in case of an error.
  590. * On error the partially applied effects are reverted.
  591. */
  592. int of_changeset_apply(struct of_changeset *ocs)
  593. {
  594. struct of_changeset_entry *ce;
  595. int ret;
  596. /* perform the rest of the work */
  597. pr_debug("of_changeset: applying...\n");
  598. list_for_each_entry(ce, &ocs->entries, node) {
  599. ret = __of_changeset_entry_apply(ce);
  600. if (ret) {
  601. pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
  602. list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
  603. __of_changeset_entry_revert(ce);
  604. return ret;
  605. }
  606. }
  607. pr_debug("of_changeset: applied, emitting notifiers.\n");
  608. /* drop the global lock while emitting notifiers */
  609. mutex_unlock(&of_mutex);
  610. list_for_each_entry(ce, &ocs->entries, node)
  611. __of_changeset_entry_notify(ce, 0);
  612. mutex_lock(&of_mutex);
  613. pr_debug("of_changeset: notifiers sent.\n");
  614. return 0;
  615. }
  616. /**
  617. * of_changeset_revert - Reverts an applied changeset
  618. *
  619. * @ocs: changeset pointer
  620. *
  621. * Reverts a changeset returning the state of the tree to what it
  622. * was before the application.
  623. * Any side-effects like creation/destruction of devices and
  624. * removal of sysfs properties and directories are applied.
  625. * Returns 0 on success, a negative error value in case of an error.
  626. */
  627. int of_changeset_revert(struct of_changeset *ocs)
  628. {
  629. struct of_changeset_entry *ce;
  630. int ret;
  631. pr_debug("of_changeset: reverting...\n");
  632. list_for_each_entry_reverse(ce, &ocs->entries, node) {
  633. ret = __of_changeset_entry_revert(ce);
  634. if (ret) {
  635. pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
  636. list_for_each_entry_continue(ce, &ocs->entries, node)
  637. __of_changeset_entry_apply(ce);
  638. return ret;
  639. }
  640. }
  641. pr_debug("of_changeset: reverted, emitting notifiers.\n");
  642. /* drop the global lock while emitting notifiers */
  643. mutex_unlock(&of_mutex);
  644. list_for_each_entry_reverse(ce, &ocs->entries, node)
  645. __of_changeset_entry_notify(ce, 1);
  646. mutex_lock(&of_mutex);
  647. pr_debug("of_changeset: notifiers sent.\n");
  648. return 0;
  649. }
  650. /**
  651. * of_changeset_action - Perform a changeset action
  652. *
  653. * @ocs: changeset pointer
  654. * @action: action to perform
  655. * @np: Pointer to device node
  656. * @prop: Pointer to property
  657. *
  658. * On action being one of:
  659. * + OF_RECONFIG_ATTACH_NODE
  660. * + OF_RECONFIG_DETACH_NODE,
  661. * + OF_RECONFIG_ADD_PROPERTY
  662. * + OF_RECONFIG_REMOVE_PROPERTY,
  663. * + OF_RECONFIG_UPDATE_PROPERTY
  664. * Returns 0 on success, a negative error value in case of an error.
  665. */
  666. int of_changeset_action(struct of_changeset *ocs, unsigned long action,
  667. struct device_node *np, struct property *prop)
  668. {
  669. struct of_changeset_entry *ce;
  670. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  671. if (!ce) {
  672. pr_err("%s: Failed to allocate\n", __func__);
  673. return -ENOMEM;
  674. }
  675. /* get a reference to the node */
  676. ce->action = action;
  677. ce->np = of_node_get(np);
  678. ce->prop = prop;
  679. if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
  680. ce->old_prop = of_find_property(np, prop->name, NULL);
  681. /* add it to the list */
  682. list_add_tail(&ce->node, &ocs->entries);
  683. return 0;
  684. }