nswalk.c revision 91116
164880Sphk/******************************************************************************
264880Sphk *
364880Sphk * Module Name: nswalk - Functions for walking the ACPI namespace
464880Sphk *              $Revision: 32 $
564880Sphk *
664880Sphk *****************************************************************************/
764880Sphk
864880Sphk/******************************************************************************
964880Sphk *
1064880Sphk * 1. Copyright Notice
1164880Sphk *
1264880Sphk * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp.
1364880Sphk * All rights reserved.
1464880Sphk *
1564880Sphk * 2. License
1664880Sphk *
1764880Sphk * 2.1. This is your license from Intel Corp. under its intellectual property
1864880Sphk * rights.  You may have additional license terms from the party that provided
1964880Sphk * you this software, covering your right to use that party's intellectual
2064880Sphk * property rights.
2164880Sphk *
2264880Sphk * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2364880Sphk * copy of the source code appearing in this file ("Covered Code") an
2464880Sphk * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2564880Sphk * base code distributed originally by Intel ("Original Intel Code") to copy,
2664880Sphk * make derivatives, distribute, use and display any portion of the Covered
2764880Sphk * Code in any form, with the right to sublicense such rights; and
2864880Sphk *
2964880Sphk * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3064880Sphk * license (with the right to sublicense), under only those claims of Intel
3164880Sphk * patents that are infringed by the Original Intel Code, to make, use, sell,
3264880Sphk * offer to sell, and import the Covered Code and derivative works thereof
3364880Sphk * solely to the minimum extent necessary to exercise the above copyright
3464880Sphk * license, and in no event shall the patent license extend to any additions
3564880Sphk * to or modifications of the Original Intel Code.  No other license or right
3664880Sphk * is granted directly or by implication, estoppel or otherwise;
3764880Sphk *
3864880Sphk * The above copyright and patent license is granted only if the following
3964880Sphk * conditions are met:
4064880Sphk *
4164880Sphk * 3. Conditions
4264880Sphk *
4364880Sphk * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4464880Sphk * Redistribution of source code of any substantial portion of the Covered
4565132Sphk * Code or modification with rights to further distribute source must include
4665132Sphk * the above Copyright Notice, the above License, this list of Conditions,
4765132Sphk * and the following Disclaimer and Export Compliance provision.  In addition,
4865132Sphk * Licensee must cause all Covered Code to which Licensee contributes to
4965132Sphk * contain a file documenting the changes Licensee made to create that Covered
5065132Sphk * Code and the date of any change.  Licensee must include in that file the
5165132Sphk * documentation of any changes made by any predecessor Licensee.  Licensee
5265132Sphk * must include a prominent statement that the modification is derived,
5365132Sphk * directly or indirectly, from Original Intel Code.
5465132Sphk *
5565132Sphk * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5665132Sphk * Redistribution of source code of any substantial portion of the Covered
5765132Sphk * Code or modification without rights to further distribute source must
5865132Sphk * include the following Disclaimer and Export Compliance provision in the
5965132Sphk * documentation and/or other materials provided with distribution.  In
6064880Sphk * addition, Licensee may not authorize further sublicense of source of any
6164880Sphk * portion of the Covered Code, and must include terms to the effect that the
6264880Sphk * license from Licensee to its licensee is limited to the intellectual
6364880Sphk * property embodied in the software Licensee provides to its licensee, and
6464880Sphk * not to intellectual property embodied in modifications its licensee may
6564880Sphk * make.
6664880Sphk *
6764880Sphk * 3.3. Redistribution of Executable. Redistribution in executable form of any
6864880Sphk * substantial portion of the Covered Code or modification must reproduce the
6964880Sphk * above Copyright Notice, and the following Disclaimer and Export Compliance
7064880Sphk * provision in the documentation and/or other materials provided with the
7164880Sphk * distribution.
7264880Sphk *
7364880Sphk * 3.4. Intel retains all right, title, and interest in and to the Original
7464880Sphk * Intel Code.
7565051Sphk *
7665051Sphk * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7764880Sphk * Intel shall be used in advertising or otherwise to promote the sale, use or
7864880Sphk * other dealings in products derived from or relating to the Covered Code
7964880Sphk * without prior written authorization from Intel.
8065051Sphk *
8165051Sphk * 4. Disclaimer and Export Compliance
8264880Sphk *
8365051Sphk * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8464880Sphk * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8564880Sphk * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8665051Sphk * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8764880Sphk * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8865051Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
8965051Sphk * PARTICULAR PURPOSE.
9065051Sphk *
9165051Sphk * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9265051Sphk * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9365051Sphk * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9465051Sphk * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9564880Sphk * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9664880Sphk * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9765051Sphk * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9865051Sphk * LIMITED REMEDY.
9965051Sphk *
10065051Sphk * 4.3. Licensee shall not export, either directly or indirectly, any of this
10165051Sphk * software or system incorporating such software without first obtaining any
10265051Sphk * required license or other approval from the U. S. Department of Commerce or
10365051Sphk * any other agency or department of the United States Government.  In the
10465051Sphk * event Licensee exports any such software from the United States or
10565051Sphk * re-exports any such software from a foreign destination, Licensee shall
10665051Sphk * ensure that the distribution and export/re-export of the software is in
10765051Sphk * compliance with all laws, regulations, orders, or other restrictions of the
10865051Sphk * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10965051Sphk * any of its subsidiaries will export/re-export any technical data, process,
11064880Sphk * software, or service, directly or indirectly, to any country for which the
11164880Sphk * United States government or any agency thereof requires an export license,
11264880Sphk * other governmental approval, or letter of assurance, without first obtaining
11365051Sphk * such license, approval or letter.
11465051Sphk *
11564880Sphk *****************************************************************************/
11664880Sphk
11764880Sphk
11864880Sphk#define __NSWALK_C__
11964880Sphk
12064880Sphk#include "acpi.h"
12164880Sphk#include "acinterp.h"
12264880Sphk#include "acnamesp.h"
12364880Sphk
12464880Sphk
12565051Sphk#define _COMPONENT          ACPI_NAMESPACE
12664880Sphk        ACPI_MODULE_NAME    ("nswalk")
12764880Sphk
12864880Sphk
12964880Sphk/*******************************************************************************
13065051Sphk *
13164880Sphk * FUNCTION:    AcpiNsGetNextNode
13264880Sphk *
13364880Sphk * PARAMETERS:  Type                - Type of node to be searched for
13464880Sphk *              ParentNode          - Parent node whose children we are
13565051Sphk *                                     getting
13664880Sphk *              ChildNode           - Previous child that was found.
13764880Sphk *                                    The NEXT child will be returned
13864880Sphk *
13964880Sphk * RETURN:      ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
14064880Sphk *                                    none is found.
14164880Sphk *
14264880Sphk * DESCRIPTION: Return the next peer node within the namespace.  If Handle
14364880Sphk *              is valid, Scope is ignored.  Otherwise, the first node
14464880Sphk *              within Scope is returned.
14564880Sphk *
14664880Sphk ******************************************************************************/
14764880Sphk
14864880SphkACPI_NAMESPACE_NODE *
14965051SphkAcpiNsGetNextNode (
15064880Sphk    ACPI_OBJECT_TYPE        Type,
15164880Sphk    ACPI_NAMESPACE_NODE     *ParentNode,
15264880Sphk    ACPI_NAMESPACE_NODE     *ChildNode)
15364880Sphk{
15464880Sphk    ACPI_NAMESPACE_NODE     *NextNode = NULL;
15564880Sphk
15664880Sphk
15764880Sphk    ACPI_FUNCTION_ENTRY ();
15865051Sphk
15965051Sphk
16065051Sphk    if (!ChildNode)
16165051Sphk    {
16264880Sphk        /* It's really the parent's _scope_ that we want */
16364880Sphk
16464880Sphk        if (ParentNode->Child)
16565051Sphk        {
16664880Sphk            NextNode = ParentNode->Child;
16764880Sphk        }
16864880Sphk    }
16964880Sphk
17064880Sphk    else
17164880Sphk    {
17264880Sphk        /* Start search at the NEXT node */
17364880Sphk
17464880Sphk        NextNode = AcpiNsGetNextValidNode (ChildNode);
17564880Sphk    }
17664880Sphk
17764880Sphk    /* If any type is OK, we are done */
17864880Sphk
17965132Sphk    if (Type == ACPI_TYPE_ANY)
18065132Sphk    {
18165132Sphk        /* NextNode is NULL if we are at the end-of-list */
18265132Sphk
18365132Sphk        return (NextNode);
18465132Sphk    }
18565132Sphk
18665132Sphk    /* Must search for the node -- but within this scope only */
18765132Sphk
18865132Sphk    while (NextNode)
18965132Sphk    {
19065051Sphk        /* If type matches, we are done */
19165051Sphk
19265051Sphk        if (NextNode->Type == Type)
19364880Sphk        {
19464880Sphk            return (NextNode);
19564880Sphk        }
19664880Sphk
19764880Sphk        /* Otherwise, move on to the next node */
19864880Sphk
19965051Sphk        NextNode = AcpiNsGetNextValidNode (NextNode);
20064880Sphk    }
20164880Sphk
20264880Sphk    /* Not found */
20364880Sphk
20464880Sphk    return (NULL);
20564880Sphk}
20664880Sphk
20764880Sphk
20864880Sphk/*******************************************************************************
20964880Sphk *
21064880Sphk * FUNCTION:    AcpiNsWalkNamespace
21164880Sphk *
21265051Sphk * PARAMETERS:  Type                - ACPI_OBJECT_TYPE to search for
21365132Sphk *              StartNode           - Handle in namespace where search begins
21465051Sphk *              MaxDepth            - Depth to which search is to reach
21564880Sphk *              UnlockBeforeCallback- Whether to unlock the NS before invoking
21664880Sphk *                                    the callback routine
21764880Sphk *              UserFunction        - Called when an object of "Type" is found
21864880Sphk *              Context             - Passed to user function
21964880Sphk *              ReturnValue         - from the UserFunction if terminated early.
22064880Sphk *                                    Otherwise, returns NULL.
22164880Sphk * RETURNS:     Status
22264880Sphk *
22364880Sphk * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
22464880Sphk *              starting (and ending) at the node specified by StartHandle.
22564880Sphk *              The UserFunction is called whenever a node that matches
22664880Sphk *              the type parameter is found.  If the user function returns
22764880Sphk *              a non-zero value, the search is terminated immediately and this
22864880Sphk *              value is returned to the caller.
22964880Sphk *
23064880Sphk *              The point of this procedure is to provide a generic namespace
23164880Sphk *              walk routine that can be called from multiple places to
23264880Sphk *              provide multiple services;  the User Function can be tailored
23364880Sphk *              to each task, whether it is a print function, a compare
23464880Sphk *              function, etc.
23564880Sphk *
23664880Sphk ******************************************************************************/
23764880Sphk
23864880SphkACPI_STATUS
23964880SphkAcpiNsWalkNamespace (
24064880Sphk    ACPI_OBJECT_TYPE        Type,
24164880Sphk    ACPI_HANDLE             StartNode,
24264880Sphk    UINT32                  MaxDepth,
24364880Sphk    BOOLEAN                 UnlockBeforeCallback,
24464880Sphk    ACPI_WALK_CALLBACK      UserFunction,
24564880Sphk    void                    *Context,
24664880Sphk    void                    **ReturnValue)
24764880Sphk{
24864880Sphk    ACPI_STATUS             Status;
24964880Sphk    ACPI_NAMESPACE_NODE     *ChildNode;
25064880Sphk    ACPI_NAMESPACE_NODE     *ParentNode;
25164880Sphk    ACPI_OBJECT_TYPE        ChildType;
25264880Sphk    UINT32                  Level;
25364880Sphk
25464880Sphk
25564880Sphk    ACPI_FUNCTION_TRACE ("NsWalkNamespace");
25664880Sphk
25764880Sphk
25864880Sphk    /* Special case for the namespace Root Node */
25964880Sphk
26064880Sphk    if (StartNode == ACPI_ROOT_OBJECT)
26164880Sphk    {
26264880Sphk        StartNode = AcpiGbl_RootNode;
26364880Sphk    }
26464880Sphk
26564880Sphk    /* Null child means "get first node" */
26664880Sphk
26764880Sphk    ParentNode  = StartNode;
26864880Sphk    ChildNode   = 0;
26964880Sphk    ChildType   = ACPI_TYPE_ANY;
27064880Sphk    Level       = 1;
27164880Sphk
272    /*
273     * Traverse the tree of nodes until we bubble back up to where we
274     * started. When Level is zero, the loop is done because we have
275     * bubbled up to (and passed) the original parent handle (StartEntry)
276     */
277    while (Level > 0)
278    {
279        /* Get the next node in this scope.  Null if not found */
280
281        Status = AE_OK;
282        ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
283        if (ChildNode)
284        {
285            /*
286             * Found node, Get the type if we are not
287             * searching for ANY
288             */
289            if (Type != ACPI_TYPE_ANY)
290            {
291                ChildType = ChildNode->Type;
292            }
293
294            if (ChildType == Type)
295            {
296                /*
297                 * Found a matching node, invoke the user
298                 * callback function
299                 */
300                if (UnlockBeforeCallback)
301                {
302                    Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
303                    if (ACPI_FAILURE (Status))
304                    {
305                        return_ACPI_STATUS (Status);
306                    }
307                }
308
309                Status = UserFunction (ChildNode, Level,
310                                        Context, ReturnValue);
311
312                if (UnlockBeforeCallback)
313                {
314                    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
315                    if (ACPI_FAILURE (Status))
316                    {
317                        return_ACPI_STATUS (Status);
318                    }
319                }
320
321                switch (Status)
322                {
323                case AE_OK:
324                case AE_CTRL_DEPTH:
325
326                    /* Just keep going */
327                    break;
328
329                case AE_CTRL_TERMINATE:
330
331                    /* Exit now, with OK status */
332
333                    return_ACPI_STATUS (AE_OK);
334
335                default:
336
337                    /* All others are valid exceptions */
338
339                    return_ACPI_STATUS (Status);
340                }
341            }
342
343            /*
344             * Depth first search:
345             * Attempt to go down another level in the namespace
346             * if we are allowed to.  Don't go any further if we
347             * have reached the caller specified maximum depth
348             * or if the user function has specified that the
349             * maximum depth has been reached.
350             */
351            if ((Level < MaxDepth) && (Status != AE_CTRL_DEPTH))
352            {
353                if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, 0))
354                {
355                    /*
356                     * There is at least one child of this
357                     * node, visit the onde
358                     */
359                    Level++;
360                    ParentNode    = ChildNode;
361                    ChildNode     = 0;
362                }
363            }
364        }
365        else
366        {
367            /*
368             * No more children of this node (AcpiNsGetNextNode
369             * failed), go back upwards in the namespace tree to
370             * the node's parent.
371             */
372            Level--;
373            ChildNode = ParentNode;
374            ParentNode = AcpiNsGetParentNode (ParentNode);
375        }
376    }
377
378    /* Complete walk, not terminated by user function */
379
380    return_ACPI_STATUS (AE_OK);
381}
382
383
384