nswalk.c 8.7 KB

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