nswalk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. * pre_order_visit - Called during tree pre-order visit
  141. * when an object of "Type" is found
  142. * post_order_visit - Called during tree post-order visit
  143. * when an object of "Type" is found
  144. * Context - Passed to user function(s) above
  145. * return_value - from the user_function if terminated
  146. * early. Otherwise, returns NULL.
  147. * RETURNS: Status
  148. *
  149. * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
  150. * starting (and ending) at the node specified by start_handle.
  151. * The callback function is called whenever a node that matches
  152. * the type parameter is found. If the callback function returns
  153. * a non-zero value, the search is terminated immediately and
  154. * this value is returned to the caller.
  155. *
  156. * The point of this procedure is to provide a generic namespace
  157. * walk routine that can be called from multiple places to
  158. * provide multiple services; the callback function(s) can be
  159. * tailored to each task, whether it is a print function,
  160. * a compare function, etc.
  161. *
  162. ******************************************************************************/
  163. acpi_status
  164. acpi_ns_walk_namespace(acpi_object_type type,
  165. acpi_handle start_node,
  166. u32 max_depth,
  167. u32 flags,
  168. acpi_walk_callback pre_order_visit,
  169. acpi_walk_callback post_order_visit,
  170. void *context, void **return_value)
  171. {
  172. acpi_status status;
  173. acpi_status mutex_status;
  174. struct acpi_namespace_node *child_node;
  175. struct acpi_namespace_node *parent_node;
  176. acpi_object_type child_type;
  177. u32 level;
  178. u8 node_previously_visited = FALSE;
  179. ACPI_FUNCTION_TRACE(ns_walk_namespace);
  180. /* Special case for the namespace Root Node */
  181. if (start_node == ACPI_ROOT_OBJECT) {
  182. start_node = acpi_gbl_root_node;
  183. }
  184. /* Null child means "get first node" */
  185. parent_node = start_node;
  186. child_node = acpi_ns_get_next_node(parent_node, NULL);
  187. child_type = ACPI_TYPE_ANY;
  188. level = 1;
  189. /*
  190. * Traverse the tree of nodes until we bubble back up to where we
  191. * started. When Level is zero, the loop is done because we have
  192. * bubbled up to (and passed) the original parent handle (start_entry)
  193. */
  194. while (level > 0 && child_node) {
  195. status = AE_OK;
  196. /* Found next child, get the type if we are not searching for ANY */
  197. if (type != ACPI_TYPE_ANY) {
  198. child_type = child_node->type;
  199. }
  200. /*
  201. * Ignore all temporary namespace nodes (created during control
  202. * method execution) unless told otherwise. These temporary nodes
  203. * can cause a race condition because they can be deleted during
  204. * the execution of the user function (if the namespace is
  205. * unlocked before invocation of the user function.) Only the
  206. * debugger namespace dump will examine the temporary nodes.
  207. */
  208. if ((child_node->flags & ANOBJ_TEMPORARY) &&
  209. !(flags & ACPI_NS_WALK_TEMP_NODES)) {
  210. status = AE_CTRL_DEPTH;
  211. }
  212. /* Type must match requested type */
  213. else if (child_type == type) {
  214. /*
  215. * Found a matching node, invoke the user callback function.
  216. * Unlock the namespace if flag is set.
  217. */
  218. if (flags & ACPI_NS_WALK_UNLOCK) {
  219. mutex_status =
  220. acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  221. if (ACPI_FAILURE(mutex_status)) {
  222. return_ACPI_STATUS(mutex_status);
  223. }
  224. }
  225. /*
  226. * Invoke the user function, either pre-order or post-order
  227. * or both.
  228. */
  229. if (!node_previously_visited) {
  230. if (pre_order_visit) {
  231. status =
  232. pre_order_visit(child_node, level,
  233. context,
  234. return_value);
  235. }
  236. } else {
  237. if (post_order_visit) {
  238. status =
  239. post_order_visit(child_node, level,
  240. context,
  241. return_value);
  242. }
  243. }
  244. if (flags & ACPI_NS_WALK_UNLOCK) {
  245. mutex_status =
  246. acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  247. if (ACPI_FAILURE(mutex_status)) {
  248. return_ACPI_STATUS(mutex_status);
  249. }
  250. }
  251. switch (status) {
  252. case AE_OK:
  253. case AE_CTRL_DEPTH:
  254. /* Just keep going */
  255. break;
  256. case AE_CTRL_TERMINATE:
  257. /* Exit now, with OK status */
  258. return_ACPI_STATUS(AE_OK);
  259. default:
  260. /* All others are valid exceptions */
  261. return_ACPI_STATUS(status);
  262. }
  263. }
  264. /*
  265. * Depth first search: Attempt to go down another level in the
  266. * namespace if we are allowed to. Don't go any further if we have
  267. * reached the caller specified maximum depth or if the user
  268. * function has specified that the maximum depth has been reached.
  269. */
  270. if (!node_previously_visited &&
  271. (level < max_depth) && (status != AE_CTRL_DEPTH)) {
  272. if (child_node->child) {
  273. /* There is at least one child of this node, visit it */
  274. level++;
  275. parent_node = child_node;
  276. child_node =
  277. acpi_ns_get_next_node(parent_node, NULL);
  278. continue;
  279. }
  280. }
  281. /* No more children, re-visit this node */
  282. if (!node_previously_visited) {
  283. node_previously_visited = TRUE;
  284. continue;
  285. }
  286. /* No more children, visit peers */
  287. child_node = acpi_ns_get_next_node(parent_node, child_node);
  288. if (child_node) {
  289. node_previously_visited = FALSE;
  290. }
  291. /* No peers, re-visit parent */
  292. else {
  293. /*
  294. * No more children of this node (acpi_ns_get_next_node failed), go
  295. * back upwards in the namespace tree to the node's parent.
  296. */
  297. level--;
  298. child_node = parent_node;
  299. parent_node = acpi_ns_get_parent_node(parent_node);
  300. node_previously_visited = TRUE;
  301. }
  302. }
  303. /* Complete walk, not terminated by user function */
  304. return_ACPI_STATUS(AE_OK);
  305. }