nswalk.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 *
  65. acpi_ns_get_next_node (
  66. acpi_object_type type,
  67. struct acpi_namespace_node *parent_node,
  68. struct acpi_namespace_node *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 (
  130. acpi_object_type type,
  131. acpi_handle start_node,
  132. u32 max_depth,
  133. u8 unlock_before_callback,
  134. acpi_walk_callback user_function,
  135. void *context,
  136. void **return_value)
  137. {
  138. acpi_status status;
  139. acpi_status mutex_status;
  140. struct acpi_namespace_node *child_node;
  141. struct acpi_namespace_node *parent_node;
  142. acpi_object_type child_type;
  143. u32 level;
  144. ACPI_FUNCTION_TRACE ("ns_walk_namespace");
  145. /* Special case for the namespace Root Node */
  146. if (start_node == ACPI_ROOT_OBJECT) {
  147. start_node = acpi_gbl_root_node;
  148. }
  149. /* Null child means "get first node" */
  150. parent_node = start_node;
  151. child_node = NULL;
  152. child_type = ACPI_TYPE_ANY;
  153. level = 1;
  154. /*
  155. * Traverse the tree of nodes until we bubble back up to where we
  156. * started. When Level is zero, the loop is done because we have
  157. * bubbled up to (and passed) the original parent handle (start_entry)
  158. */
  159. while (level > 0) {
  160. /* Get the next node in this scope. Null if not found */
  161. status = AE_OK;
  162. child_node = acpi_ns_get_next_node (ACPI_TYPE_ANY, parent_node, 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 = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
  178. if (ACPI_FAILURE (mutex_status)) {
  179. return_ACPI_STATUS (mutex_status);
  180. }
  181. }
  182. status = user_function (child_node, level,
  183. context, return_value);
  184. if (unlock_before_callback) {
  185. mutex_status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
  186. if (ACPI_FAILURE (mutex_status)) {
  187. return_ACPI_STATUS (mutex_status);
  188. }
  189. }
  190. switch (status) {
  191. case AE_OK:
  192. case AE_CTRL_DEPTH:
  193. /* Just keep going */
  194. break;
  195. case AE_CTRL_TERMINATE:
  196. /* Exit now, with OK status */
  197. return_ACPI_STATUS (AE_OK);
  198. default:
  199. /* All others are valid exceptions */
  200. return_ACPI_STATUS (status);
  201. }
  202. }
  203. /*
  204. * Depth first search:
  205. * Attempt to go down another level in the namespace
  206. * if we are allowed to. Don't go any further if we
  207. * have reached the caller specified maximum depth
  208. * or if the user function has specified that the
  209. * maximum depth has been reached.
  210. */
  211. if ((level < max_depth) && (status != AE_CTRL_DEPTH)) {
  212. if (acpi_ns_get_next_node (ACPI_TYPE_ANY, child_node, NULL)) {
  213. /*
  214. * There is at least one child of this
  215. * node, visit the onde
  216. */
  217. level++;
  218. parent_node = child_node;
  219. child_node = NULL;
  220. }
  221. }
  222. }
  223. else {
  224. /*
  225. * No more children of this node (acpi_ns_get_next_node
  226. * failed), go back upwards in the namespace tree to
  227. * the node's parent.
  228. */
  229. level--;
  230. child_node = parent_node;
  231. parent_node = acpi_ns_get_parent_node (parent_node);
  232. }
  233. }
  234. /* Complete walk, not terminated by user function */
  235. return_ACPI_STATUS (AE_OK);
  236. }