nswalk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /******************************************************************************
  2. *
  3. * Module Name: nswalk - Functions for walking the ACPI namespace
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #define _COMPONENT ACPI_NAMESPACE
  46. ACPI_MODULE_NAME("nswalk")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ns_get_next_node
  50. *
  51. * PARAMETERS: parent_node - Parent node whose children we are
  52. * getting
  53. * child_node - Previous child that was found.
  54. * The NEXT child will be returned
  55. *
  56. * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
  57. * none is found.
  58. *
  59. * DESCRIPTION: Return the next peer node within the namespace. If Handle
  60. * is valid, Scope is ignored. Otherwise, the first node
  61. * within Scope is returned.
  62. *
  63. ******************************************************************************/
  64. struct acpi_namespace_node *acpi_ns_get_next_node(struct acpi_namespace_node
  65. *parent_node,
  66. struct acpi_namespace_node
  67. *child_node)
  68. {
  69. ACPI_FUNCTION_ENTRY();
  70. if (!child_node) {
  71. /* It's really the parent's _scope_ that we want */
  72. return (parent_node->child);
  73. }
  74. /* Otherwise just return the next peer */
  75. return (child_node->peer);
  76. }
  77. /*******************************************************************************
  78. *
  79. * FUNCTION: acpi_ns_get_next_node_typed
  80. *
  81. * PARAMETERS: type - Type of node to be searched for
  82. * parent_node - Parent node whose children we are
  83. * getting
  84. * child_node - Previous child that was found.
  85. * The NEXT child will be returned
  86. *
  87. * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
  88. * none is found.
  89. *
  90. * DESCRIPTION: Return the next peer node within the namespace. If Handle
  91. * is valid, Scope is ignored. Otherwise, the first node
  92. * within Scope is returned.
  93. *
  94. ******************************************************************************/
  95. struct acpi_namespace_node *acpi_ns_get_next_node_typed(acpi_object_type type,
  96. struct
  97. acpi_namespace_node
  98. *parent_node,
  99. struct
  100. acpi_namespace_node
  101. *child_node)
  102. {
  103. struct acpi_namespace_node *next_node = NULL;
  104. ACPI_FUNCTION_ENTRY();
  105. next_node = acpi_ns_get_next_node(parent_node, child_node);
  106. /* If any type is OK, we are done */
  107. if (type == ACPI_TYPE_ANY) {
  108. /* next_node is NULL if we are at the end-of-list */
  109. return (next_node);
  110. }
  111. /* Must search for the node -- but within this scope only */
  112. while (next_node) {
  113. /* If type matches, we are done */
  114. if (next_node->type == type) {
  115. return (next_node);
  116. }
  117. /* Otherwise, move on to the next peer node */
  118. next_node = next_node->peer;
  119. }
  120. /* Not found */
  121. return (NULL);
  122. }
  123. /*******************************************************************************
  124. *
  125. * FUNCTION: acpi_ns_walk_namespace
  126. *
  127. * PARAMETERS: type - acpi_object_type to search for
  128. * start_node - Handle in namespace where search begins
  129. * max_depth - Depth to which search is to reach
  130. * flags - Whether to unlock the NS before invoking
  131. * the callback routine
  132. * descending_callback - Called during tree descent
  133. * when an object of "Type" is found
  134. * ascending_callback - Called during tree ascent
  135. * when an object of "Type" is found
  136. * context - Passed to user function(s) above
  137. * return_value - from the user_function if terminated
  138. * early. Otherwise, returns NULL.
  139. * RETURNS: Status
  140. *
  141. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  142. * starting (and ending) at the node specified by start_handle.
  143. * The callback function is called whenever a node that matches
  144. * the type parameter is found. If the callback function returns
  145. * a non-zero value, the search is terminated immediately and
  146. * this value is returned to the caller.
  147. *
  148. * The point of this procedure is to provide a generic namespace
  149. * walk routine that can be called from multiple places to
  150. * provide multiple services; the callback function(s) can be
  151. * tailored to each task, whether it is a print function,
  152. * a compare function, etc.
  153. *
  154. ******************************************************************************/
  155. acpi_status
  156. acpi_ns_walk_namespace(acpi_object_type type,
  157. acpi_handle start_node,
  158. u32 max_depth,
  159. u32 flags,
  160. acpi_walk_callback descending_callback,
  161. acpi_walk_callback ascending_callback,
  162. void *context, void **return_value)
  163. {
  164. acpi_status status;
  165. acpi_status mutex_status;
  166. struct acpi_namespace_node *child_node;
  167. struct acpi_namespace_node *parent_node;
  168. acpi_object_type child_type;
  169. u32 level;
  170. u8 node_previously_visited = FALSE;
  171. ACPI_FUNCTION_TRACE(ns_walk_namespace);
  172. /* Special case for the namespace Root Node */
  173. if (start_node == ACPI_ROOT_OBJECT) {
  174. start_node = acpi_gbl_root_node;
  175. }
  176. /* Null child means "get first node" */
  177. parent_node = start_node;
  178. child_node = acpi_ns_get_next_node(parent_node, NULL);
  179. child_type = ACPI_TYPE_ANY;
  180. level = 1;
  181. /*
  182. * Traverse the tree of nodes until we bubble back up to where we
  183. * started. When Level is zero, the loop is done because we have
  184. * bubbled up to (and passed) the original parent handle (start_entry)
  185. */
  186. while (level > 0 && child_node) {
  187. status = AE_OK;
  188. /* Found next child, get the type if we are not searching for ANY */
  189. if (type != ACPI_TYPE_ANY) {
  190. child_type = child_node->type;
  191. }
  192. /*
  193. * Ignore all temporary namespace nodes (created during control
  194. * method execution) unless told otherwise. These temporary nodes
  195. * can cause a race condition because they can be deleted during
  196. * the execution of the user function (if the namespace is
  197. * unlocked before invocation of the user function.) Only the
  198. * debugger namespace dump will examine the temporary nodes.
  199. */
  200. if ((child_node->flags & ANOBJ_TEMPORARY) &&
  201. !(flags & ACPI_NS_WALK_TEMP_NODES)) {
  202. status = AE_CTRL_DEPTH;
  203. }
  204. /* Type must match requested type */
  205. else if (child_type == type) {
  206. /*
  207. * Found a matching node, invoke the user callback function.
  208. * Unlock the namespace if flag is set.
  209. */
  210. if (flags & ACPI_NS_WALK_UNLOCK) {
  211. mutex_status =
  212. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  213. if (ACPI_FAILURE(mutex_status)) {
  214. return_ACPI_STATUS(mutex_status);
  215. }
  216. }
  217. /*
  218. * Invoke the user function, either descending, ascending,
  219. * or both.
  220. */
  221. if (!node_previously_visited) {
  222. if (descending_callback) {
  223. status =
  224. descending_callback(child_node,
  225. level, context,
  226. return_value);
  227. }
  228. } else {
  229. if (ascending_callback) {
  230. status =
  231. ascending_callback(child_node,
  232. level, context,
  233. return_value);
  234. }
  235. }
  236. if (flags & ACPI_NS_WALK_UNLOCK) {
  237. mutex_status =
  238. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  239. if (ACPI_FAILURE(mutex_status)) {
  240. return_ACPI_STATUS(mutex_status);
  241. }
  242. }
  243. switch (status) {
  244. case AE_OK:
  245. case AE_CTRL_DEPTH:
  246. /* Just keep going */
  247. break;
  248. case AE_CTRL_TERMINATE:
  249. /* Exit now, with OK status */
  250. return_ACPI_STATUS(AE_OK);
  251. default:
  252. /* All others are valid exceptions */
  253. return_ACPI_STATUS(status);
  254. }
  255. }
  256. /*
  257. * Depth first search: Attempt to go down another level in the
  258. * namespace if we are allowed to. Don't go any further if we have
  259. * reached the caller specified maximum depth or if the user
  260. * function has specified that the maximum depth has been reached.
  261. */
  262. if (!node_previously_visited &&
  263. (level < max_depth) && (status != AE_CTRL_DEPTH)) {
  264. if (child_node->child) {
  265. /* There is at least one child of this node, visit it */
  266. level++;
  267. parent_node = child_node;
  268. child_node =
  269. acpi_ns_get_next_node(parent_node, NULL);
  270. continue;
  271. }
  272. }
  273. /* No more children, re-visit this node */
  274. if (!node_previously_visited) {
  275. node_previously_visited = TRUE;
  276. continue;
  277. }
  278. /* No more children, visit peers */
  279. child_node = acpi_ns_get_next_node(parent_node, child_node);
  280. if (child_node) {
  281. node_previously_visited = FALSE;
  282. }
  283. /* No peers, re-visit parent */
  284. else {
  285. /*
  286. * No more children of this node (acpi_ns_get_next_node failed), go
  287. * back upwards in the namespace tree to the node's parent.
  288. */
  289. level--;
  290. child_node = parent_node;
  291. parent_node = parent_node->parent;
  292. node_previously_visited = TRUE;
  293. }
  294. }
  295. /* Complete walk, not terminated by user function */
  296. return_ACPI_STATUS(AE_OK);
  297. }