blob: 35539df5c75dc965800e555bd1432d8ef11c9767 [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/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
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>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("nswalk")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*******************************************************************************
52 *
53 * FUNCTION: acpi_ns_get_next_node
54 *
Bob Moore8c725bf2009-05-21 10:27:51 +080055 * PARAMETERS: parent_node - Parent node whose children we are
56 * getting
57 * 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 ******************************************************************************/
68struct acpi_namespace_node *acpi_ns_get_next_node(struct acpi_namespace_node
69 *parent_node,
70 struct acpi_namespace_node
71 *child_node)
72{
73 ACPI_FUNCTION_ENTRY();
74
75 if (!child_node) {
76
77 /* It's really the parent's _scope_ that we want */
78
79 return parent_node->child;
80 }
81
82 /*
83 * Get the next node.
84 *
85 * If we are at the end of this peer list, return NULL
86 */
87 if (child_node->flags & ANOBJ_END_OF_PEER_LIST) {
88 return NULL;
89 }
90
91 /* Otherwise just return the next peer */
92
93 return child_node->peer;
94}
95
96/*******************************************************************************
97 *
98 * FUNCTION: acpi_ns_get_next_node_typed
99 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * PARAMETERS: Type - Type of node to be searched for
101 * parent_node - Parent node whose children we are
Robert Moore44f6c012005-04-18 22:49:35 -0400102 * getting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * child_node - Previous child that was found.
104 * The NEXT child will be returned
105 *
106 * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
107 * none is found.
108 *
109 * DESCRIPTION: Return the next peer node within the namespace. If Handle
110 * is valid, Scope is ignored. Otherwise, the first node
111 * within Scope is returned.
112 *
113 ******************************************************************************/
Bob Moore8c725bf2009-05-21 10:27:51 +0800114
115struct acpi_namespace_node *acpi_ns_get_next_node_typed(acpi_object_type type,
116 struct
117 acpi_namespace_node
118 *parent_node,
119 struct
120 acpi_namespace_node
121 *child_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Len Brown4be44fc2005-08-05 00:44:28 -0400123 struct acpi_namespace_node *next_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Len Brown4be44fc2005-08-05 00:44:28 -0400125 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Bob Moore8c725bf2009-05-21 10:27:51 +0800127 next_node = acpi_ns_get_next_node(parent_node, child_node);
Bob Moore52fc0b02006-10-02 00:00:00 -0400128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* If any type is OK, we are done */
131
132 if (type == ACPI_TYPE_ANY) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* next_node is NULL if we are at the end-of-list */
135
136 return (next_node);
137 }
138
139 /* Must search for the node -- but within this scope only */
140
141 while (next_node) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 /* If type matches, we are done */
144
145 if (next_node->type == type) {
146 return (next_node);
147 }
148
149 /* Otherwise, move on to the next node */
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151 next_node = acpi_ns_get_next_valid_node(next_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
154 /* Not found */
155
156 return (NULL);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*******************************************************************************
160 *
161 * FUNCTION: acpi_ns_walk_namespace
162 *
163 * PARAMETERS: Type - acpi_object_type to search for
164 * start_node - Handle in namespace where search begins
165 * max_depth - Depth to which search is to reach
Bob Moored1fdda832007-02-02 19:48:21 +0300166 * Flags - Whether to unlock the NS before invoking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * the callback routine
168 * user_function - Called when an object of "Type" is found
169 * Context - Passed to user function
170 * return_value - from the user_function if terminated early.
171 * Otherwise, returns NULL.
172 * RETURNS: Status
173 *
174 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
175 * starting (and ending) at the node specified by start_handle.
176 * The user_function is called whenever a node that matches
177 * the type parameter is found. If the user function returns
Bob Moored4913dc2009-03-06 10:05:18 +0800178 * a non-zero value, the search is terminated immediately and
179 * this value is returned to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *
181 * The point of this procedure is to provide a generic namespace
182 * walk routine that can be called from multiple places to
183 * provide multiple services; the User Function can be tailored
184 * to each task, whether it is a print function, a compare
185 * function, etc.
186 *
187 ******************************************************************************/
188
189acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400190acpi_ns_walk_namespace(acpi_object_type type,
191 acpi_handle start_node,
192 u32 max_depth,
Bob Moored1fdda832007-02-02 19:48:21 +0300193 u32 flags,
Len Brown4be44fc2005-08-05 00:44:28 -0400194 acpi_walk_callback user_function,
195 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Len Brown4be44fc2005-08-05 00:44:28 -0400197 acpi_status status;
198 acpi_status mutex_status;
199 struct acpi_namespace_node *child_node;
200 struct acpi_namespace_node *parent_node;
201 acpi_object_type child_type;
202 u32 level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Bob Mooreb229cf92006-04-21 17:15:00 -0400204 ACPI_FUNCTION_TRACE(ns_walk_namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Special case for the namespace Root Node */
207
208 if (start_node == ACPI_ROOT_OBJECT) {
209 start_node = acpi_gbl_root_node;
210 }
211
212 /* Null child means "get first node" */
213
214 parent_node = start_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400215 child_node = NULL;
216 child_type = ACPI_TYPE_ANY;
217 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /*
220 * Traverse the tree of nodes until we bubble back up to where we
221 * started. When Level is zero, the loop is done because we have
222 * bubbled up to (and passed) the original parent handle (start_entry)
223 */
224 while (level > 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Get the next node in this scope. Null if not found */
227
228 status = AE_OK;
Bob Moore8c725bf2009-05-21 10:27:51 +0800229 child_node = acpi_ns_get_next_node(parent_node, child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (child_node) {
Bob Moore3effba32007-02-02 19:48:21 +0300231
Bob Moorec1014622007-02-02 19:48:21 +0300232 /* Found next child, get the type if we are not searching for ANY */
Bob Moore3effba32007-02-02 19:48:21 +0300233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (type != ACPI_TYPE_ANY) {
235 child_type = child_node->type;
236 }
237
Bob Moore3effba32007-02-02 19:48:21 +0300238 /*
Bob Moorec1014622007-02-02 19:48:21 +0300239 * Ignore all temporary namespace nodes (created during control
240 * method execution) unless told otherwise. These temporary nodes
Bob Moored4913dc2009-03-06 10:05:18 +0800241 * can cause a race condition because they can be deleted during
242 * the execution of the user function (if the namespace is
243 * unlocked before invocation of the user function.) Only the
244 * debugger namespace dump will examine the temporary nodes.
Bob Moore3effba32007-02-02 19:48:21 +0300245 */
Bob Moorec1014622007-02-02 19:48:21 +0300246 if ((child_node->flags & ANOBJ_TEMPORARY) &&
247 !(flags & ACPI_NS_WALK_TEMP_NODES)) {
248 status = AE_CTRL_DEPTH;
249 }
250
251 /* Type must match requested type */
252
253 else if (child_type == type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
Bob Moore3effba32007-02-02 19:48:21 +0300255 * Found a matching node, invoke the user callback function.
256 * Unlock the namespace if flag is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
Bob Moored1fdda832007-02-02 19:48:21 +0300258 if (flags & ACPI_NS_WALK_UNLOCK) {
Len Brown4be44fc2005-08-05 00:44:28 -0400259 mutex_status =
260 acpi_ut_release_mutex
261 (ACPI_MTX_NAMESPACE);
262 if (ACPI_FAILURE(mutex_status)) {
263 return_ACPI_STATUS
264 (mutex_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 }
267
Bob Moore3effba32007-02-02 19:48:21 +0300268 status =
269 user_function(child_node, level, context,
270 return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Bob Moored1fdda832007-02-02 19:48:21 +0300272 if (flags & ACPI_NS_WALK_UNLOCK) {
Len Brown4be44fc2005-08-05 00:44:28 -0400273 mutex_status =
274 acpi_ut_acquire_mutex
275 (ACPI_MTX_NAMESPACE);
276 if (ACPI_FAILURE(mutex_status)) {
277 return_ACPI_STATUS
278 (mutex_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
281
282 switch (status) {
283 case AE_OK:
284 case AE_CTRL_DEPTH:
285
286 /* Just keep going */
287 break;
288
289 case AE_CTRL_TERMINATE:
290
291 /* Exit now, with OK status */
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 default:
296
297 /* All others are valid exceptions */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 }
302
303 /*
Bob Moore3effba32007-02-02 19:48:21 +0300304 * Depth first search: Attempt to go down another level in the
305 * namespace if we are allowed to. Don't go any further if we have
306 * reached the caller specified maximum depth or if the user
307 * function has specified that the maximum depth has been reached.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
309 if ((level < max_depth) && (status != AE_CTRL_DEPTH)) {
Bob Moore8c725bf2009-05-21 10:27:51 +0800310 if (child_node->child) {
Bob Moore3effba32007-02-02 19:48:21 +0300311
312 /* There is at least one child of this node, visit it */
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 level++;
315 parent_node = child_node;
316 child_node = NULL;
317 }
318 }
Len Brown4be44fc2005-08-05 00:44:28 -0400319 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
Bob Moore3effba32007-02-02 19:48:21 +0300321 * No more children of this node (acpi_ns_get_next_node failed), go
322 * back upwards in the namespace tree to the node's parent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324 level--;
325 child_node = parent_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400326 parent_node = acpi_ns_get_parent_node(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 }
329
330 /* Complete walk, not terminated by user function */
331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}