Merge trunk HEAD (46152)
[reactos.git] / drivers / bus / acpi / parser / pswalk.c
1 /******************************************************************************
2 *
3 * Module Name: pswalk - Parser routines to walk parsed op tree(s)
4 * $Revision: 1.1 $
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000, 2001 R. Byron Moore
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27 #include <acpi.h>
28
29 #define _COMPONENT ACPI_PARSER
30 MODULE_NAME ("pswalk")
31
32
33 /*******************************************************************************
34 *
35 * FUNCTION: Acpi_ps_get_next_walk_op
36 *
37 * PARAMETERS: Walk_state - Current state of the walk
38 * Op - Current Op to be walked
39 * Ascending_callback - Procedure called when Op is complete
40 *
41 * RETURN: Status
42 *
43 * DESCRIPTION: Get the next Op in a walk of the parse tree.
44 *
45 ******************************************************************************/
46
47 ACPI_STATUS
48 acpi_ps_get_next_walk_op (
49 ACPI_WALK_STATE *walk_state,
50 ACPI_PARSE_OBJECT *op,
51 ACPI_PARSE_UPWARDS ascending_callback)
52 {
53 ACPI_PARSE_OBJECT *next;
54 ACPI_PARSE_OBJECT *parent;
55 ACPI_PARSE_OBJECT *grand_parent;
56 ACPI_STATUS status;
57
58
59 /* Check for a argument only if we are descending in the tree */
60
61 if (walk_state->next_op_info != NEXT_OP_UPWARD) {
62 /* Look for an argument or child of the current op */
63
64 next = acpi_ps_get_arg (op, 0);
65 if (next) {
66 /* Still going downward in tree (Op is not completed yet) */
67
68 walk_state->prev_op = op;
69 walk_state->next_op = next;
70 walk_state->next_op_info = NEXT_OP_DOWNWARD;
71
72 return (AE_OK);
73 }
74
75
76 /*
77 * No more children, this Op is complete. Save Next and Parent
78 * in case the Op object gets deleted by the callback routine
79 */
80
81 next = op->next;
82 parent = op->parent;
83
84 status = ascending_callback (walk_state, op);
85
86 /*
87 * If we are back to the starting point, the walk is complete.
88 */
89 if (op == walk_state->origin) {
90 /* Reached the point of origin, the walk is complete */
91
92 walk_state->prev_op = op;
93 walk_state->next_op = NULL;
94
95 return (status);
96 }
97
98 /*
99 * Check for a sibling to the current op. A sibling means
100 * we are still going "downward" in the tree.
101 */
102
103 if (next) {
104 /* There is a sibling, it will be next */
105
106 walk_state->prev_op = op;
107 walk_state->next_op = next;
108 walk_state->next_op_info = NEXT_OP_DOWNWARD;
109
110 /* Continue downward */
111
112 return (status);
113 }
114
115
116 /*
117 * Drop into the loop below because we are moving upwards in
118 * the tree
119 */
120 }
121
122 else {
123 /*
124 * We are resuming a walk, and we were (are) going upward in the tree.
125 * So, we want to drop into the parent loop below.
126 */
127
128 parent = op;
129 }
130
131
132 /*
133 * Look for a sibling of the current Op's parent
134 * Continue moving up the tree until we find a node that has not been
135 * visited, or we get back to where we started.
136 */
137 while (parent) {
138 /* We are moving up the tree, therefore this parent Op is complete */
139
140 grand_parent = parent->parent;
141 next = parent->next;
142
143 status = ascending_callback (walk_state, parent);
144
145 /*
146 * If we are back to the starting point, the walk is complete.
147 */
148 if (parent == walk_state->origin) {
149 /* Reached the point of origin, the walk is complete */
150
151 walk_state->prev_op = parent;
152 walk_state->next_op = NULL;
153
154 return (status);
155 }
156
157 /*
158 * If there is a sibling to this parent (it is not the starting point
159 * Op), then we will visit it.
160 */
161 if (next) {
162 /* found sibling of parent */
163
164 walk_state->prev_op = parent;
165 walk_state->next_op = next;
166 walk_state->next_op_info = NEXT_OP_DOWNWARD;
167
168 return (status);
169 }
170
171 /* No siblings, no errors, just move up one more level in the tree */
172
173 op = parent;
174 parent = grand_parent;
175 walk_state->prev_op = op;
176 }
177
178
179 /* Got all the way to the top of the tree, we must be done! */
180 /* However, the code should have terminated in the loop above */
181
182 walk_state->next_op = NULL;
183
184 return (AE_OK);
185 }
186
187
188 /*******************************************************************************
189 *
190 * FUNCTION: Acpi_ps_delete_completed_op
191 *
192 * PARAMETERS: State - Walk state
193 * Op - Completed op
194 *
195 * RETURN: AE_OK
196 *
197 * DESCRIPTION: Callback function for Acpi_ps_get_next_walk_op(). Used during
198 * Acpi_ps_delete_parse tree to delete Op objects when all sub-objects
199 * have been visited (and deleted.)
200 *
201 ******************************************************************************/
202
203 static ACPI_STATUS
204 acpi_ps_delete_completed_op (
205 ACPI_WALK_STATE *state,
206 ACPI_PARSE_OBJECT *op)
207 {
208
209 acpi_ps_free_op (op);
210 return (AE_OK);
211 }
212
213
214 /*******************************************************************************
215 *
216 * FUNCTION: Acpi_ps_delete_parse_tree
217 *
218 * PARAMETERS: Subtree_root - Root of tree (or subtree) to delete
219 *
220 * RETURN: None
221 *
222 * DESCRIPTION: Delete a portion of or an entire parse tree.
223 *
224 ******************************************************************************/
225
226 void
227 acpi_ps_delete_parse_tree (
228 ACPI_PARSE_OBJECT *subtree_root)
229 {
230 ACPI_WALK_STATE *walk_state;
231 ACPI_WALK_LIST walk_list;
232
233
234 if (!subtree_root) {
235 return;
236 }
237
238 /* Create and initialize a new walk list */
239
240 walk_list.walk_state = NULL;
241 walk_list.acquired_mutex_list.prev = NULL;
242 walk_list.acquired_mutex_list.next = NULL;
243
244 walk_state = acpi_ds_create_walk_state (TABLE_ID_DSDT, NULL, NULL, &walk_list);
245 if (!walk_state) {
246 return;
247 }
248
249 walk_state->parser_state = NULL;
250 walk_state->parse_flags = 0;
251 walk_state->descending_callback = NULL;
252 walk_state->ascending_callback = NULL;
253
254
255 walk_state->origin = subtree_root;
256 walk_state->next_op = subtree_root;
257
258
259 /* Head downward in the tree */
260
261 walk_state->next_op_info = NEXT_OP_DOWNWARD;
262
263 /* Visit all nodes in the subtree */
264
265 while (walk_state->next_op) {
266 acpi_ps_get_next_walk_op (walk_state, walk_state->next_op,
267 acpi_ps_delete_completed_op);
268 }
269
270 /* We are done with this walk */
271
272 acpi_aml_release_all_mutexes ((ACPI_OPERAND_OBJECT *) &walk_list.acquired_mutex_list);
273 acpi_ds_delete_walk_state (walk_state);
274
275 return;
276 }
277
278