Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /****************************************************************************** |
2 | * | |
3 | * Module Name: nswalk - Functions for walking the ACPI namespace | |
4 | * | |
5 | *****************************************************************************/ | |
6 | ||
7 | /* | |
75a44ce0 | 8 | * Copyright (C) 2000 - 2008, Intel Corp. |
1da177e4 LT |
9 | * 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 | ||
1da177e4 LT |
44 | #include <acpi/acpi.h> |
45 | #include <acpi/acnamesp.h> | |
46 | ||
1da177e4 | 47 | #define _COMPONENT ACPI_NAMESPACE |
4be44fcd | 48 | ACPI_MODULE_NAME("nswalk") |
1da177e4 LT |
49 | |
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 | |
44f6c012 | 56 | * getting |
1da177e4 LT |
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 | ******************************************************************************/ | |
fd350943 LB |
68 | struct acpi_namespace_node *acpi_ns_get_next_node(acpi_object_type type, struct acpi_namespace_node |
69 | *parent_node, struct acpi_namespace_node | |
4be44fcd | 70 | *child_node) |
1da177e4 | 71 | { |
4be44fcd | 72 | struct acpi_namespace_node *next_node = NULL; |
1da177e4 | 73 | |
4be44fcd | 74 | ACPI_FUNCTION_ENTRY(); |
1da177e4 LT |
75 | |
76 | if (!child_node) { | |
52fc0b02 | 77 | |
1da177e4 LT |
78 | /* It's really the parent's _scope_ that we want */ |
79 | ||
a69c77c7 | 80 | next_node = parent_node->child; |
1da177e4 LT |
81 | } |
82 | ||
83 | else { | |
84 | /* Start search at the NEXT node */ | |
85 | ||
4be44fcd | 86 | next_node = acpi_ns_get_next_valid_node(child_node); |
1da177e4 LT |
87 | } |
88 | ||
89 | /* If any type is OK, we are done */ | |
90 | ||
91 | if (type == ACPI_TYPE_ANY) { | |
52fc0b02 | 92 | |
1da177e4 LT |
93 | /* next_node is NULL if we are at the end-of-list */ |
94 | ||
95 | return (next_node); | |
96 | } | |
97 | ||
98 | /* Must search for the node -- but within this scope only */ | |
99 | ||
100 | while (next_node) { | |
52fc0b02 | 101 | |
1da177e4 LT |
102 | /* If type matches, we are done */ |
103 | ||
104 | if (next_node->type == type) { | |
105 | return (next_node); | |
106 | } | |
107 | ||
108 | /* Otherwise, move on to the next node */ | |
109 | ||
4be44fcd | 110 | next_node = acpi_ns_get_next_valid_node(next_node); |
1da177e4 LT |
111 | } |
112 | ||
113 | /* Not found */ | |
114 | ||
115 | return (NULL); | |
116 | } | |
117 | ||
1da177e4 LT |
118 | /******************************************************************************* |
119 | * | |
120 | * FUNCTION: acpi_ns_walk_namespace | |
121 | * | |
122 | * PARAMETERS: Type - acpi_object_type to search for | |
123 | * start_node - Handle in namespace where search begins | |
124 | * max_depth - Depth to which search is to reach | |
d1fdda83 | 125 | * Flags - Whether to unlock the NS before invoking |
1da177e4 LT |
126 | * the callback routine |
127 | * user_function - Called when an object of "Type" is found | |
128 | * Context - Passed to user function | |
129 | * return_value - from the user_function if terminated early. | |
130 | * Otherwise, returns NULL. | |
131 | * RETURNS: Status | |
132 | * | |
133 | * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, | |
134 | * starting (and ending) at the node specified by start_handle. | |
135 | * The user_function is called whenever a node that matches | |
136 | * the type parameter is found. If the user function returns | |
137 | * a non-zero value, the search is terminated immediately and this | |
138 | * value is returned to the caller. | |
139 | * | |
140 | * The point of this procedure is to provide a generic namespace | |
141 | * walk routine that can be called from multiple places to | |
142 | * provide multiple services; the User Function can be tailored | |
143 | * to each task, whether it is a print function, a compare | |
144 | * function, etc. | |
145 | * | |
146 | ******************************************************************************/ | |
147 | ||
148 | acpi_status | |
4be44fcd LB |
149 | acpi_ns_walk_namespace(acpi_object_type type, |
150 | acpi_handle start_node, | |
151 | u32 max_depth, | |
d1fdda83 | 152 | u32 flags, |
4be44fcd LB |
153 | acpi_walk_callback user_function, |
154 | void *context, void **return_value) | |
1da177e4 | 155 | { |
4be44fcd LB |
156 | acpi_status status; |
157 | acpi_status mutex_status; | |
158 | struct acpi_namespace_node *child_node; | |
159 | struct acpi_namespace_node *parent_node; | |
160 | acpi_object_type child_type; | |
161 | u32 level; | |
1da177e4 | 162 | |
b229cf92 | 163 | ACPI_FUNCTION_TRACE(ns_walk_namespace); |
1da177e4 LT |
164 | |
165 | /* Special case for the namespace Root Node */ | |
166 | ||
167 | if (start_node == ACPI_ROOT_OBJECT) { | |
168 | start_node = acpi_gbl_root_node; | |
169 | } | |
170 | ||
171 | /* Null child means "get first node" */ | |
172 | ||
173 | parent_node = start_node; | |
4be44fcd LB |
174 | child_node = NULL; |
175 | child_type = ACPI_TYPE_ANY; | |
176 | level = 1; | |
1da177e4 LT |
177 | |
178 | /* | |
179 | * Traverse the tree of nodes until we bubble back up to where we | |
180 | * started. When Level is zero, the loop is done because we have | |
181 | * bubbled up to (and passed) the original parent handle (start_entry) | |
182 | */ | |
183 | while (level > 0) { | |
52fc0b02 | 184 | |
1da177e4 LT |
185 | /* Get the next node in this scope. Null if not found */ |
186 | ||
187 | status = AE_OK; | |
4be44fcd LB |
188 | child_node = |
189 | acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node, | |
190 | child_node); | |
1da177e4 | 191 | if (child_node) { |
3effba32 | 192 | |
c1014629 | 193 | /* Found next child, get the type if we are not searching for ANY */ |
3effba32 | 194 | |
1da177e4 LT |
195 | if (type != ACPI_TYPE_ANY) { |
196 | child_type = child_node->type; | |
197 | } | |
198 | ||
3effba32 | 199 | /* |
c1014629 BM |
200 | * Ignore all temporary namespace nodes (created during control |
201 | * method execution) unless told otherwise. These temporary nodes | |
202 | * can cause a race condition because they can be deleted during the | |
203 | * execution of the user function (if the namespace is unlocked before | |
204 | * invocation of the user function.) Only the debugger namespace dump | |
205 | * will examine the temporary nodes. | |
3effba32 | 206 | */ |
c1014629 BM |
207 | if ((child_node->flags & ANOBJ_TEMPORARY) && |
208 | !(flags & ACPI_NS_WALK_TEMP_NODES)) { | |
209 | status = AE_CTRL_DEPTH; | |
210 | } | |
211 | ||
212 | /* Type must match requested type */ | |
213 | ||
214 | else if (child_type == type) { | |
1da177e4 | 215 | /* |
3effba32 BM |
216 | * Found a matching node, invoke the user callback function. |
217 | * Unlock the namespace if flag is set. | |
1da177e4 | 218 | */ |
d1fdda83 | 219 | if (flags & ACPI_NS_WALK_UNLOCK) { |
4be44fcd LB |
220 | mutex_status = |
221 | acpi_ut_release_mutex | |
222 | (ACPI_MTX_NAMESPACE); | |
223 | if (ACPI_FAILURE(mutex_status)) { | |
224 | return_ACPI_STATUS | |
225 | (mutex_status); | |
1da177e4 LT |
226 | } |
227 | } | |
228 | ||
3effba32 BM |
229 | status = |
230 | user_function(child_node, level, context, | |
231 | return_value); | |
1da177e4 | 232 | |
d1fdda83 | 233 | if (flags & ACPI_NS_WALK_UNLOCK) { |
4be44fcd LB |
234 | mutex_status = |
235 | acpi_ut_acquire_mutex | |
236 | (ACPI_MTX_NAMESPACE); | |
237 | if (ACPI_FAILURE(mutex_status)) { | |
238 | return_ACPI_STATUS | |
239 | (mutex_status); | |
1da177e4 LT |
240 | } |
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 | ||
4be44fcd | 254 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
255 | |
256 | default: | |
257 | ||
258 | /* All others are valid exceptions */ | |
259 | ||
4be44fcd | 260 | return_ACPI_STATUS(status); |
1da177e4 LT |
261 | } |
262 | } | |
263 | ||
264 | /* | |
3effba32 BM |
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. | |
1da177e4 LT |
269 | */ |
270 | if ((level < max_depth) && (status != AE_CTRL_DEPTH)) { | |
4be44fcd LB |
271 | if (acpi_ns_get_next_node |
272 | (ACPI_TYPE_ANY, child_node, NULL)) { | |
3effba32 BM |
273 | |
274 | /* There is at least one child of this node, visit it */ | |
275 | ||
1da177e4 LT |
276 | level++; |
277 | parent_node = child_node; | |
278 | child_node = NULL; | |
279 | } | |
280 | } | |
4be44fcd | 281 | } else { |
1da177e4 | 282 | /* |
3effba32 BM |
283 | * 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. | |
1da177e4 LT |
285 | */ |
286 | level--; | |
287 | child_node = parent_node; | |
4be44fcd | 288 | parent_node = acpi_ns_get_parent_node(parent_node); |
1da177e4 LT |
289 | } |
290 | } | |
291 | ||
292 | /* Complete walk, not terminated by user function */ | |
293 | ||
4be44fcd | 294 | return_ACPI_STATUS(AE_OK); |
1da177e4 | 295 | } |