blob: c6619d8a516451b8d98ab05e8e61c982b8c6a476 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: nswalk - Functions for walking the ACPI namespace
4 *
5 *****************************************************************************/
6
7/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acnamesp.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("nswalk")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*******************************************************************************
51 *
52 * FUNCTION: acpi_ns_get_next_node
53 *
54 * PARAMETERS: Type - Type of node to be searched for
55 * parent_node - Parent node whose children we are
Robert Moore44f6c012005-04-18 22:49:35 -040056 * getting
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * child_node - Previous child that was found.
58 * The NEXT child will be returned
59 *
60 * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
61 * none is found.
62 *
63 * DESCRIPTION: Return the next peer node within the namespace. If Handle
64 * is valid, Scope is ignored. Otherwise, the first node
65 * within Scope is returned.
66 *
67 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040068struct acpi_namespace_node *acpi_ns_get_next_node(acpi_object_type type,
69 struct acpi_namespace_node
70 *parent_node,
71 struct acpi_namespace_node
72 *child_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Len Brown4be44fc2005-08-05 00:44:28 -040074 struct acpi_namespace_node *next_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Len Brown4be44fc2005-08-05 00:44:28 -040076 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (!child_node) {
Bob Moore52fc0b02006-10-02 00:00:00 -040079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* It's really the parent's _scope_ that we want */
81
82 if (parent_node->child) {
83 next_node = parent_node->child;
84 }
85 }
86
87 else {
88 /* Start search at the NEXT node */
89
Len Brown4be44fc2005-08-05 00:44:28 -040090 next_node = acpi_ns_get_next_valid_node(child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 /* If any type is OK, we are done */
94
95 if (type == ACPI_TYPE_ANY) {
Bob Moore52fc0b02006-10-02 00:00:00 -040096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* next_node is NULL if we are at the end-of-list */
98
99 return (next_node);
100 }
101
102 /* Must search for the node -- but within this scope only */
103
104 while (next_node) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* If type matches, we are done */
107
108 if (next_node->type == type) {
109 return (next_node);
110 }
111
112 /* Otherwise, move on to the next node */
113
Len Brown4be44fc2005-08-05 00:44:28 -0400114 next_node = acpi_ns_get_next_valid_node(next_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 /* Not found */
118
119 return (NULL);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*******************************************************************************
123 *
124 * FUNCTION: acpi_ns_walk_namespace
125 *
126 * PARAMETERS: Type - acpi_object_type to search for
127 * start_node - Handle in namespace where search begins
128 * max_depth - Depth to which search is to reach
Bob Moored1fdda82007-02-02 19:48:21 +0300129 * Flags - Whether to unlock the NS before invoking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * the callback routine
131 * user_function - Called when an object of "Type" is found
132 * Context - Passed to user function
133 * return_value - from the user_function if terminated early.
134 * Otherwise, returns NULL.
135 * RETURNS: Status
136 *
137 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
138 * starting (and ending) at the node specified by start_handle.
139 * The user_function is called whenever a node that matches
140 * the type parameter is found. If the user function returns
141 * a non-zero value, the search is terminated immediately and this
142 * value is returned to the caller.
143 *
144 * The point of this procedure is to provide a generic namespace
145 * walk routine that can be called from multiple places to
146 * provide multiple services; the User Function can be tailored
147 * to each task, whether it is a print function, a compare
148 * function, etc.
149 *
150 ******************************************************************************/
151
152acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400153acpi_ns_walk_namespace(acpi_object_type type,
154 acpi_handle start_node,
155 u32 max_depth,
Bob Moored1fdda82007-02-02 19:48:21 +0300156 u32 flags,
Len Brown4be44fc2005-08-05 00:44:28 -0400157 acpi_walk_callback user_function,
158 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Len Brown4be44fc2005-08-05 00:44:28 -0400160 acpi_status status;
161 acpi_status mutex_status;
162 struct acpi_namespace_node *child_node;
163 struct acpi_namespace_node *parent_node;
164 acpi_object_type child_type;
165 u32 level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Bob Mooreb229cf92006-04-21 17:15:00 -0400167 ACPI_FUNCTION_TRACE(ns_walk_namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* Special case for the namespace Root Node */
170
171 if (start_node == ACPI_ROOT_OBJECT) {
172 start_node = acpi_gbl_root_node;
173 }
174
175 /* Null child means "get first node" */
176
177 parent_node = start_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400178 child_node = NULL;
179 child_type = ACPI_TYPE_ANY;
180 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /*
183 * Traverse the tree of nodes until we bubble back up to where we
184 * started. When Level is zero, the loop is done because we have
185 * bubbled up to (and passed) the original parent handle (start_entry)
186 */
187 while (level > 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Get the next node in this scope. Null if not found */
190
191 status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 child_node =
193 acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
194 child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (child_node) {
Bob Moore3effba32007-02-02 19:48:21 +0300196
197 /* Found node, Get the type if we are not searching for ANY */
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (type != ACPI_TYPE_ANY) {
200 child_type = child_node->type;
201 }
202
Bob Moore3effba32007-02-02 19:48:21 +0300203 /*
204 * 1) Type must match
205 * 2) Permanent namespace nodes are OK
206 * 3) Ignore temporary nodes unless told otherwise. Typically,
207 * the temporary nodes can cause a race condition where they can
208 * be deleted during the execution of the user function. Only the
209 * debugger namespace dump will examine the temporary nodes.
210 */
Bob Moored1fdda82007-02-02 19:48:21 +0300211 if ((child_type == type) &&
212 (!(child_node->flags & ANOBJ_TEMPORARY) ||
213 (child_node->flags & ANOBJ_TEMPORARY)
214 && (flags & ACPI_NS_WALK_TEMP_NODES))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
Bob Moore3effba32007-02-02 19:48:21 +0300216 * Found a matching node, invoke the user callback function.
217 * Unlock the namespace if flag is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Bob Moored1fdda82007-02-02 19:48:21 +0300219 if (flags & ACPI_NS_WALK_UNLOCK) {
Len Brown4be44fc2005-08-05 00:44:28 -0400220 mutex_status =
221 acpi_ut_release_mutex
222 (ACPI_MTX_NAMESPACE);
223 if (ACPI_FAILURE(mutex_status)) {
224 return_ACPI_STATUS
225 (mutex_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 }
228
Bob Moore3effba32007-02-02 19:48:21 +0300229 status =
230 user_function(child_node, level, context,
231 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Bob Moored1fdda82007-02-02 19:48:21 +0300233 if (flags & ACPI_NS_WALK_UNLOCK) {
Len Brown4be44fc2005-08-05 00:44:28 -0400234 mutex_status =
235 acpi_ut_acquire_mutex
236 (ACPI_MTX_NAMESPACE);
237 if (ACPI_FAILURE(mutex_status)) {
238 return_ACPI_STATUS
239 (mutex_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
242
243 switch (status) {
244 case AE_OK:
245 case AE_CTRL_DEPTH:
246
247 /* Just keep going */
248 break;
249
250 case AE_CTRL_TERMINATE:
251
252 /* Exit now, with OK status */
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 default:
257
258 /* All others are valid exceptions */
259
Len Brown4be44fc2005-08-05 00:44:28 -0400260 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262 }
263
264 /*
Bob Moore3effba32007-02-02 19:48:21 +0300265 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270 if ((level < max_depth) && (status != AE_CTRL_DEPTH)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400271 if (acpi_ns_get_next_node
272 (ACPI_TYPE_ANY, child_node, NULL)) {
Bob Moore3effba32007-02-02 19:48:21 +0300273
274 /* There is at least one child of this node, visit it */
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 level++;
277 parent_node = child_node;
278 child_node = NULL;
279 }
280 }
Len Brown4be44fc2005-08-05 00:44:28 -0400281 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /*
Bob Moore3effba32007-02-02 19:48:21 +0300283 * No more children of this node (acpi_ns_get_next_node failed), go
284 * back upwards in the namespace tree to the node's parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 */
286 level--;
287 child_node = parent_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400288 parent_node = acpi_ns_get_parent_node(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290 }
291
292 /* Complete walk, not terminated by user function */
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}