nswalk.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nswalk - Functions for walking the ACPI namespace
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acnamesp.h"
  12. #define _COMPONENT ACPI_NAMESPACE
  13. ACPI_MODULE_NAME("nswalk")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ns_get_next_node
  17. *
  18. * PARAMETERS: parent_node - Parent node whose children we are
  19. * getting
  20. * child_node - Previous child that was found.
  21. * The NEXT child will be returned
  22. *
  23. * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
  24. * none is found.
  25. *
  26. * DESCRIPTION: Return the next peer node within the namespace. If Handle
  27. * is valid, Scope is ignored. Otherwise, the first node
  28. * within Scope is returned.
  29. *
  30. ******************************************************************************/
  31. struct acpi_namespace_node *acpi_ns_get_next_node(struct acpi_namespace_node
  32. *parent_node,
  33. struct acpi_namespace_node
  34. *child_node)
  35. {
  36. ACPI_FUNCTION_ENTRY();
  37. if (!child_node) {
  38. /* It's really the parent's _scope_ that we want */
  39. return (parent_node->child);
  40. }
  41. /* Otherwise just return the next peer */
  42. return (child_node->peer);
  43. }
  44. /*******************************************************************************
  45. *
  46. * FUNCTION: acpi_ns_get_next_node_typed
  47. *
  48. * PARAMETERS: type - Type of node to be searched for
  49. * parent_node - Parent node whose children we are
  50. * getting
  51. * child_node - Previous child that was found.
  52. * The NEXT child will be returned
  53. *
  54. * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
  55. * none is found.
  56. *
  57. * DESCRIPTION: Return the next peer node within the namespace. If Handle
  58. * is valid, Scope is ignored. Otherwise, the first node
  59. * within Scope is returned.
  60. *
  61. ******************************************************************************/
  62. struct acpi_namespace_node *acpi_ns_get_next_node_typed(acpi_object_type type,
  63. struct
  64. acpi_namespace_node
  65. *parent_node,
  66. struct
  67. acpi_namespace_node
  68. *child_node)
  69. {
  70. struct acpi_namespace_node *next_node = NULL;
  71. ACPI_FUNCTION_ENTRY();
  72. next_node = acpi_ns_get_next_node(parent_node, child_node);
  73. /* If any type is OK, we are done */
  74. if (type == ACPI_TYPE_ANY) {
  75. /* next_node is NULL if we are at the end-of-list */
  76. return (next_node);
  77. }
  78. /* Must search for the node -- but within this scope only */
  79. while (next_node) {
  80. /* If type matches, we are done */
  81. if (next_node->type == type) {
  82. return (next_node);
  83. }
  84. /* Otherwise, move on to the next peer node */
  85. next_node = next_node->peer;
  86. }
  87. /* Not found */
  88. return (NULL);
  89. }
  90. /*******************************************************************************
  91. *
  92. * FUNCTION: acpi_ns_walk_namespace
  93. *
  94. * PARAMETERS: type - acpi_object_type to search for
  95. * start_node - Handle in namespace where search begins
  96. * max_depth - Depth to which search is to reach
  97. * flags - Whether to unlock the NS before invoking
  98. * the callback routine
  99. * descending_callback - Called during tree descent
  100. * when an object of "Type" is found
  101. * ascending_callback - Called during tree ascent
  102. * when an object of "Type" is found
  103. * context - Passed to user function(s) above
  104. * return_value - from the user_function if terminated
  105. * early. Otherwise, returns NULL.
  106. * RETURNS: Status
  107. *
  108. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  109. * starting (and ending) at the node specified by start_handle.
  110. * The callback function is called whenever a node that matches
  111. * the type parameter is found. If the callback function returns
  112. * a non-zero value, the search is terminated immediately and
  113. * this value is returned to the caller.
  114. *
  115. * The point of this procedure is to provide a generic namespace
  116. * walk routine that can be called from multiple places to
  117. * provide multiple services; the callback function(s) can be
  118. * tailored to each task, whether it is a print function,
  119. * a compare function, etc.
  120. *
  121. ******************************************************************************/
  122. acpi_status
  123. acpi_ns_walk_namespace(acpi_object_type type,
  124. acpi_handle start_node,
  125. u32 max_depth,
  126. u32 flags,
  127. acpi_walk_callback descending_callback,
  128. acpi_walk_callback ascending_callback,
  129. void *context, void **return_value)
  130. {
  131. acpi_status status;
  132. acpi_status mutex_status;
  133. struct acpi_namespace_node *child_node;
  134. struct acpi_namespace_node *parent_node;
  135. acpi_object_type child_type;
  136. u32 level;
  137. u8 node_previously_visited = FALSE;
  138. ACPI_FUNCTION_TRACE(ns_walk_namespace);
  139. /* Special case for the namespace Root Node */
  140. if (start_node == ACPI_ROOT_OBJECT) {
  141. start_node = acpi_gbl_root_node;
  142. }
  143. /* Null child means "get first node" */
  144. parent_node = start_node;
  145. child_node = acpi_ns_get_next_node(parent_node, NULL);
  146. child_type = ACPI_TYPE_ANY;
  147. level = 1;
  148. /*
  149. * Traverse the tree of nodes until we bubble back up to where we
  150. * started. When Level is zero, the loop is done because we have
  151. * bubbled up to (and passed) the original parent handle (start_entry)
  152. */
  153. while (level > 0 && child_node) {
  154. status = AE_OK;
  155. /* Found next child, get the type if we are not searching for ANY */
  156. if (type != ACPI_TYPE_ANY) {
  157. child_type = child_node->type;
  158. }
  159. /*
  160. * Ignore all temporary namespace nodes (created during control
  161. * method execution) unless told otherwise. These temporary nodes
  162. * can cause a race condition because they can be deleted during
  163. * the execution of the user function (if the namespace is
  164. * unlocked before invocation of the user function.) Only the
  165. * debugger namespace dump will examine the temporary nodes.
  166. */
  167. if ((child_node->flags & ANOBJ_TEMPORARY) &&
  168. !(flags & ACPI_NS_WALK_TEMP_NODES)) {
  169. status = AE_CTRL_DEPTH;
  170. }
  171. /* Type must match requested type */
  172. else if (child_type == type) {
  173. /*
  174. * Found a matching node, invoke the user callback function.
  175. * Unlock the namespace if flag is set.
  176. */
  177. if (flags & ACPI_NS_WALK_UNLOCK) {
  178. mutex_status =
  179. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  180. if (ACPI_FAILURE(mutex_status)) {
  181. return_ACPI_STATUS(mutex_status);
  182. }
  183. }
  184. /*
  185. * Invoke the user function, either descending, ascending,
  186. * or both.
  187. */
  188. if (!node_previously_visited) {
  189. if (descending_callback) {
  190. status =
  191. descending_callback(child_node,
  192. level, context,
  193. return_value);
  194. }
  195. } else {
  196. if (ascending_callback) {
  197. status =
  198. ascending_callback(child_node,
  199. level, context,
  200. return_value);
  201. }
  202. }
  203. if (flags & ACPI_NS_WALK_UNLOCK) {
  204. mutex_status =
  205. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  206. if (ACPI_FAILURE(mutex_status)) {
  207. return_ACPI_STATUS(mutex_status);
  208. }
  209. }
  210. switch (status) {
  211. case AE_OK:
  212. case AE_CTRL_DEPTH:
  213. /* Just keep going */
  214. break;
  215. case AE_CTRL_TERMINATE:
  216. /* Exit now, with OK status */
  217. return_ACPI_STATUS(AE_OK);
  218. default:
  219. /* All others are valid exceptions */
  220. return_ACPI_STATUS(status);
  221. }
  222. }
  223. /*
  224. * Depth first search: Attempt to go down another level in the
  225. * namespace if we are allowed to. Don't go any further if we have
  226. * reached the caller specified maximum depth or if the user
  227. * function has specified that the maximum depth has been reached.
  228. */
  229. if (!node_previously_visited &&
  230. (level < max_depth) && (status != AE_CTRL_DEPTH)) {
  231. if (child_node->child) {
  232. /* There is at least one child of this node, visit it */
  233. level++;
  234. parent_node = child_node;
  235. child_node =
  236. acpi_ns_get_next_node(parent_node, NULL);
  237. continue;
  238. }
  239. }
  240. /* No more children, re-visit this node */
  241. if (!node_previously_visited) {
  242. node_previously_visited = TRUE;
  243. continue;
  244. }
  245. /* No more children, visit peers */
  246. child_node = acpi_ns_get_next_node(parent_node, child_node);
  247. if (child_node) {
  248. node_previously_visited = FALSE;
  249. }
  250. /* No peers, re-visit parent */
  251. else {
  252. /*
  253. * No more children of this node (acpi_ns_get_next_node failed), go
  254. * back upwards in the namespace tree to the node's parent.
  255. */
  256. level--;
  257. child_node = parent_node;
  258. parent_node = parent_node->parent;
  259. node_previously_visited = TRUE;
  260. }
  261. }
  262. /* Complete walk, not terminated by user function */
  263. return_ACPI_STATUS(AE_OK);
  264. }