nswalk.c 10 KB

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