nswalk.c 9.2 KB

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