[ACPI] ACPICA 20060113
[sfrench/cifs-2.6.git] / drivers / acpi / namespace / nswalk.c
1 /******************************************************************************
2  *
3  * Module Name: nswalk - Functions for walking the ACPI namespace
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2006, R. Byron Moore
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
44 #include <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
46
47 #define _COMPONENT          ACPI_NAMESPACE
48 ACPI_MODULE_NAME("nswalk")
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
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  ******************************************************************************/
68 struct 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)
73 {
74         struct acpi_namespace_node *next_node = NULL;
75
76         ACPI_FUNCTION_ENTRY();
77
78         if (!child_node) {
79                 /* It's really the parent's _scope_ that we want */
80
81                 if (parent_node->child) {
82                         next_node = parent_node->child;
83                 }
84         }
85
86         else {
87                 /* Start search at the NEXT node */
88
89                 next_node = acpi_ns_get_next_valid_node(child_node);
90         }
91
92         /* If any type is OK, we are done */
93
94         if (type == ACPI_TYPE_ANY) {
95                 /* next_node is NULL if we are at the end-of-list */
96
97                 return (next_node);
98         }
99
100         /* Must search for the node -- but within this scope only */
101
102         while (next_node) {
103                 /* If type matches, we are done */
104
105                 if (next_node->type == type) {
106                         return (next_node);
107                 }
108
109                 /* Otherwise, move on to the next node */
110
111                 next_node = acpi_ns_get_next_valid_node(next_node);
112         }
113
114         /* Not found */
115
116         return (NULL);
117 }
118
119 /*******************************************************************************
120  *
121  * FUNCTION:    acpi_ns_walk_namespace
122  *
123  * PARAMETERS:  Type                - acpi_object_type to search for
124  *              start_node          - Handle in namespace where search begins
125  *              max_depth           - Depth to which search is to reach
126  *              unlock_before_callback- Whether to unlock the NS before invoking
127  *                                    the callback routine
128  *              user_function       - Called when an object of "Type" is found
129  *              Context             - Passed to user function
130  *              return_value        - from the user_function if terminated early.
131  *                                    Otherwise, returns NULL.
132  * RETURNS:     Status
133  *
134  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
135  *              starting (and ending) at the node specified by start_handle.
136  *              The user_function is called whenever a node that matches
137  *              the type parameter is found.  If the user function returns
138  *              a non-zero value, the search is terminated immediately and this
139  *              value is returned to the caller.
140  *
141  *              The point of this procedure is to provide a generic namespace
142  *              walk routine that can be called from multiple places to
143  *              provide multiple services;  the User Function can be tailored
144  *              to each task, whether it is a print function, a compare
145  *              function, etc.
146  *
147  ******************************************************************************/
148
149 acpi_status
150 acpi_ns_walk_namespace(acpi_object_type type,
151                        acpi_handle start_node,
152                        u32 max_depth,
153                        u8 unlock_before_callback,
154                        acpi_walk_callback user_function,
155                        void *context, void **return_value)
156 {
157         acpi_status status;
158         acpi_status mutex_status;
159         struct acpi_namespace_node *child_node;
160         struct acpi_namespace_node *parent_node;
161         acpi_object_type child_type;
162         u32 level;
163
164         ACPI_FUNCTION_TRACE("ns_walk_namespace");
165
166         /* Special case for the namespace Root Node */
167
168         if (start_node == ACPI_ROOT_OBJECT) {
169                 start_node = acpi_gbl_root_node;
170         }
171
172         /* Null child means "get first node" */
173
174         parent_node = start_node;
175         child_node = NULL;
176         child_type = ACPI_TYPE_ANY;
177         level = 1;
178
179         /*
180          * Traverse the tree of nodes until we bubble back up to where we
181          * started. When Level is zero, the loop is done because we have
182          * bubbled up to (and passed) the original parent handle (start_entry)
183          */
184         while (level > 0) {
185                 /* Get the next node in this scope.  Null if not found */
186
187                 status = AE_OK;
188                 child_node =
189                     acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
190                                           child_node);
191                 if (child_node) {
192                         /*
193                          * Found node, Get the type if we are not
194                          * searching for ANY
195                          */
196                         if (type != ACPI_TYPE_ANY) {
197                                 child_type = child_node->type;
198                         }
199
200                         if (child_type == type) {
201                                 /*
202                                  * Found a matching node, invoke the user
203                                  * callback function
204                                  */
205                                 if (unlock_before_callback) {
206                                         mutex_status =
207                                             acpi_ut_release_mutex
208                                             (ACPI_MTX_NAMESPACE);
209                                         if (ACPI_FAILURE(mutex_status)) {
210                                                 return_ACPI_STATUS
211                                                     (mutex_status);
212                                         }
213                                 }
214
215                                 status = user_function(child_node, level,
216                                                        context, return_value);
217
218                                 if (unlock_before_callback) {
219                                         mutex_status =
220                                             acpi_ut_acquire_mutex
221                                             (ACPI_MTX_NAMESPACE);
222                                         if (ACPI_FAILURE(mutex_status)) {
223                                                 return_ACPI_STATUS
224                                                     (mutex_status);
225                                         }
226                                 }
227
228                                 switch (status) {
229                                 case AE_OK:
230                                 case AE_CTRL_DEPTH:
231
232                                         /* Just keep going */
233                                         break;
234
235                                 case AE_CTRL_TERMINATE:
236
237                                         /* Exit now, with OK status */
238
239                                         return_ACPI_STATUS(AE_OK);
240
241                                 default:
242
243                                         /* All others are valid exceptions */
244
245                                         return_ACPI_STATUS(status);
246                                 }
247                         }
248
249                         /*
250                          * Depth first search:
251                          * Attempt to go down another level in the namespace
252                          * if we are allowed to.  Don't go any further if we
253                          * have reached the caller specified maximum depth
254                          * or if the user function has specified that the
255                          * maximum depth has been reached.
256                          */
257                         if ((level < max_depth) && (status != AE_CTRL_DEPTH)) {
258                                 if (acpi_ns_get_next_node
259                                     (ACPI_TYPE_ANY, child_node, NULL)) {
260                                         /*
261                                          * There is at least one child of this
262                                          * node, visit the onde
263                                          */
264                                         level++;
265                                         parent_node = child_node;
266                                         child_node = NULL;
267                                 }
268                         }
269                 } else {
270                         /*
271                          * No more children of this node (acpi_ns_get_next_node
272                          * failed), go back upwards in the namespace tree to
273                          * the node's parent.
274                          */
275                         level--;
276                         child_node = parent_node;
277                         parent_node = acpi_ns_get_parent_node(parent_node);
278                 }
279         }
280
281         /* Complete walk, not terminated by user function */
282
283         return_ACPI_STATUS(AE_OK);
284 }