nswalk.c 9.2 KB

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