1 /******************************************************************************
3 * Module Name: nswalk - Functions for walking the ACPI namespace
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nswalk")
53 /*******************************************************************************
55 * FUNCTION: AcpiNsGetNextNode
57 * PARAMETERS: ParentNode - Parent node whose children we are
59 * ChildNode - Previous child that was found.
60 * The NEXT child will be returned
62 * RETURN: ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
65 * DESCRIPTION: Return the next peer node within the namespace. If Handle
66 * is valid, Scope is ignored. Otherwise, the first node
67 * within Scope is returned.
69 ******************************************************************************/
73 ACPI_NAMESPACE_NODE
*ParentNode
,
74 ACPI_NAMESPACE_NODE
*ChildNode
)
76 ACPI_FUNCTION_ENTRY ();
81 /* It's really the parent's _scope_ that we want */
83 return (ParentNode
->Child
);
86 /* Otherwise just return the next peer */
88 return (ChildNode
->Peer
);
92 /*******************************************************************************
94 * FUNCTION: AcpiNsGetNextNodeTyped
96 * PARAMETERS: Type - Type of node to be searched for
97 * ParentNode - Parent node whose children we are
99 * ChildNode - Previous child that was found.
100 * The NEXT child will be returned
102 * RETURN: ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
105 * DESCRIPTION: Return the next peer node within the namespace. If Handle
106 * is valid, Scope is ignored. Otherwise, the first node
107 * within Scope is returned.
109 ******************************************************************************/
111 ACPI_NAMESPACE_NODE
*
112 AcpiNsGetNextNodeTyped (
113 ACPI_OBJECT_TYPE Type
,
114 ACPI_NAMESPACE_NODE
*ParentNode
,
115 ACPI_NAMESPACE_NODE
*ChildNode
)
117 ACPI_NAMESPACE_NODE
*NextNode
= NULL
;
120 ACPI_FUNCTION_ENTRY ();
123 NextNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
125 /* If any type is OK, we are done */
127 if (Type
== ACPI_TYPE_ANY
)
129 /* NextNode is NULL if we are at the end-of-list */
134 /* Must search for the node -- but within this scope only */
138 /* If type matches, we are done */
140 if (NextNode
->Type
== Type
)
145 /* Otherwise, move on to the next peer node */
147 NextNode
= NextNode
->Peer
;
156 /*******************************************************************************
158 * FUNCTION: AcpiNsWalkNamespace
160 * PARAMETERS: Type - ACPI_OBJECT_TYPE to search for
161 * StartNode - Handle in namespace where search begins
162 * MaxDepth - Depth to which search is to reach
163 * Flags - Whether to unlock the NS before invoking
164 * the callback routine
165 * DescendingCallback - Called during tree descent
166 * when an object of "Type" is found
167 * AscendingCallback - Called during tree ascent
168 * when an object of "Type" is found
169 * Context - Passed to user function(s) above
170 * ReturnValue - from the UserFunction if terminated
171 * early. Otherwise, returns NULL.
174 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
175 * starting (and ending) at the node specified by StartHandle.
176 * The callback function is called whenever a node that matches
177 * the type parameter is found. If the callback function returns
178 * a non-zero value, the search is terminated immediately and
179 * this value is returned to the caller.
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 callback function(s) can be
184 * tailored to each task, whether it is a print function,
185 * a compare function, etc.
187 ******************************************************************************/
190 AcpiNsWalkNamespace (
191 ACPI_OBJECT_TYPE Type
,
192 ACPI_HANDLE StartNode
,
195 ACPI_WALK_CALLBACK DescendingCallback
,
196 ACPI_WALK_CALLBACK AscendingCallback
,
201 ACPI_STATUS MutexStatus
;
202 ACPI_NAMESPACE_NODE
*ChildNode
;
203 ACPI_NAMESPACE_NODE
*ParentNode
;
204 ACPI_OBJECT_TYPE ChildType
;
206 BOOLEAN NodePreviouslyVisited
= FALSE
;
209 ACPI_FUNCTION_TRACE (NsWalkNamespace
);
212 /* Special case for the namespace Root Node */
214 if (StartNode
== ACPI_ROOT_OBJECT
)
216 StartNode
= AcpiGbl_RootNode
;
219 /* Null child means "get first node" */
221 ParentNode
= StartNode
;
222 ChildNode
= AcpiNsGetNextNode (ParentNode
, NULL
);
223 ChildType
= ACPI_TYPE_ANY
;
227 * Traverse the tree of nodes until we bubble back up to where we
228 * started. When Level is zero, the loop is done because we have
229 * bubbled up to (and passed) the original parent handle (StartEntry)
231 while (Level
> 0 && ChildNode
)
235 /* Found next child, get the type if we are not searching for ANY */
237 if (Type
!= ACPI_TYPE_ANY
)
239 ChildType
= ChildNode
->Type
;
243 * Ignore all temporary namespace nodes (created during control
244 * method execution) unless told otherwise. These temporary nodes
245 * can cause a race condition because they can be deleted during
246 * the execution of the user function (if the namespace is
247 * unlocked before invocation of the user function.) Only the
248 * debugger namespace dump will examine the temporary nodes.
250 if ((ChildNode
->Flags
& ANOBJ_TEMPORARY
) &&
251 !(Flags
& ACPI_NS_WALK_TEMP_NODES
))
253 Status
= AE_CTRL_DEPTH
;
256 /* Type must match requested type */
258 else if (ChildType
== Type
)
261 * Found a matching node, invoke the user callback function.
262 * Unlock the namespace if flag is set.
264 if (Flags
& ACPI_NS_WALK_UNLOCK
)
266 MutexStatus
= AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
267 if (ACPI_FAILURE (MutexStatus
))
269 return_ACPI_STATUS (MutexStatus
);
274 * Invoke the user function, either descending, ascending,
277 if (!NodePreviouslyVisited
)
279 if (DescendingCallback
)
281 Status
= DescendingCallback (ChildNode
, Level
,
282 Context
, ReturnValue
);
287 if (AscendingCallback
)
289 Status
= AscendingCallback (ChildNode
, Level
,
290 Context
, ReturnValue
);
294 if (Flags
& ACPI_NS_WALK_UNLOCK
)
296 MutexStatus
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
297 if (ACPI_FAILURE (MutexStatus
))
299 return_ACPI_STATUS (MutexStatus
);
308 /* Just keep going */
311 case AE_CTRL_TERMINATE
:
313 /* Exit now, with OK status */
315 return_ACPI_STATUS (AE_OK
);
319 /* All others are valid exceptions */
321 return_ACPI_STATUS (Status
);
326 * Depth first search: Attempt to go down another level in the
327 * namespace if we are allowed to. Don't go any further if we have
328 * reached the caller specified maximum depth or if the user
329 * function has specified that the maximum depth has been reached.
331 if (!NodePreviouslyVisited
&&
332 (Level
< MaxDepth
) &&
333 (Status
!= AE_CTRL_DEPTH
))
335 if (ChildNode
->Child
)
337 /* There is at least one child of this node, visit it */
340 ParentNode
= ChildNode
;
341 ChildNode
= AcpiNsGetNextNode (ParentNode
, NULL
);
346 /* No more children, re-visit this node */
348 if (!NodePreviouslyVisited
)
350 NodePreviouslyVisited
= TRUE
;
354 /* No more children, visit peers */
356 ChildNode
= AcpiNsGetNextNode (ParentNode
, ChildNode
);
359 NodePreviouslyVisited
= FALSE
;
362 /* No peers, re-visit parent */
367 * No more children of this node (AcpiNsGetNextNode failed), go
368 * back upwards in the namespace tree to the node's parent.
371 ChildNode
= ParentNode
;
372 ParentNode
= ParentNode
->Parent
;
374 NodePreviouslyVisited
= TRUE
;
378 /* Complete walk, not terminated by user function */
380 return_ACPI_STATUS (AE_OK
);