aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/parser/pswalk.c
blob: e04b1b73606b95dbe95fbcebef1b1c4ff81d8687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/******************************************************************************
 *
 * Module Name: pswalk - Parser routines to walk parsed op tree(s)
 *
 *****************************************************************************/

/*
 * Copyright (C) 2000 - 2005, R. Byron Moore
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */


#include <acpi/acpi.h>
#include <acpi/acparser.h>
#include <acpi/acdispat.h>

#define _COMPONENT          ACPI_PARSER
	 ACPI_MODULE_NAME    ("pswalk")


/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_walk_op
 *
 * PARAMETERS:  walk_state          - Current state of the walk
 *              Op                  - Current Op to be walked
 *              ascending_callback  - Procedure called when Op is complete
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Get the next Op in a walk of the parse tree.
 *
 ******************************************************************************/

acpi_status
acpi_ps_get_next_walk_op (
	struct acpi_walk_state          *walk_state,
	union acpi_parse_object         *op,
	acpi_parse_upwards              ascending_callback)
{
	union acpi_parse_object         *next;
	union acpi_parse_object         *parent;
	union acpi_parse_object         *grand_parent;
	acpi_status                     status;


	ACPI_FUNCTION_TRACE_PTR ("ps_get_next_walk_op", op);


	/* Check for a argument only if we are descending in the tree */

	if (walk_state->next_op_info != ACPI_NEXT_OP_UPWARD) {
		/* Look for an argument or child of the current op */

		next = acpi_ps_get_arg (op, 0);
		if (next) {
			/* Still going downward in tree (Op is not completed yet) */

			walk_state->prev_op     = op;
			walk_state->next_op     = next;
			walk_state->next_op_info = ACPI_NEXT_OP_DOWNWARD;

			return_ACPI_STATUS (AE_OK);
		}

		/*
		 * No more children, this Op is complete.  Save Next and Parent
		 * in case the Op object gets deleted by the callback routine
		 */
		next    = op->common.next;
		parent  = op->common.parent;

		walk_state->op    = op;
		walk_state->op_info = acpi_ps_get_opcode_info (op->common.aml_opcode);
		walk_state->opcode = op->common.aml_opcode;

		status = ascending_callback (walk_state);

		/*
		 * If we are back to the starting point, the walk is complete.
		 */
		if (op == walk_state->origin) {
			/* Reached the point of origin, the walk is complete */

			walk_state->prev_op     = op;
			walk_state->next_op     = NULL;

			return_ACPI_STATUS (status);
		}

		/*
		 * Check for a sibling to the current op.  A sibling means
		 * we are still going "downward" in the tree.
		 */
		if (next) {
			/* There is a sibling, it will be next */

			walk_state->prev_op     = op;
			walk_state->next_op     = next;
			walk_state->next_op_info = ACPI_NEXT_OP_DOWNWARD;

			/* Continue downward */

			return_ACPI_STATUS (status);
		}

		/*
		 * Drop into the loop below because we are moving upwards in
		 * the tree
		 */
	}
	else {
		/*
		 * We are resuming a walk, and we were (are) going upward in the tree.
		 * So, we want to drop into the parent loop below.
		 */
		parent = op;
	}

	/*
	 * Look for a sibling of the current Op's parent
	 * Continue moving up the tree until we find a node that has not been
	 * visited, or we get back to where we started.
	 */
	while (parent) {
		/* We are moving up the tree, therefore this parent Op is complete */

		grand_parent = parent->common.parent;
		next        = parent->common.next;

		walk_state->op    = parent;
		walk_state->op_info = acpi_ps_get_opcode_info (parent->common.aml_opcode);
		walk_state->opcode = parent->common.aml_opcode;

		status = ascending_callback (walk_state);

		/*
		 * If we are back to the starting point, the walk is complete.
		 */
		if (parent == walk_state->origin) {
			/* Reached the point of origin, the walk is complete */

			walk_state->prev_op     = parent;
			walk_state->next_op     = NULL;

			return_ACPI_STATUS (status);
		}

		/*
		 * If there is a sibling to this parent (it is not the starting point
		 * Op), then we will visit it.
		 */
		if (next) {
			/* found sibling of parent */

			walk_state->prev_op     = parent;
			walk_state->next_op     = next;
			walk_state->next_op_info = ACPI_NEXT_OP_DOWNWARD;

			return_ACPI_STATUS (status);
		}

		/* No siblings, no errors, just move up one more level in the tree */

		op                  = parent;
		parent              = grand_parent;
		walk_state->prev_op = op;
	}


	/*
	 * Got all the way to the top of the tree, we must be done!
	 * However, the code should have terminated in the loop above
	 */
	walk_state->next_op     = NULL;

	return_ACPI_STATUS (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_delete_completed_op
 *
 * PARAMETERS:  State           - Walk state
 *              Op              - Completed op
 *
 * RETURN:      AE_OK
 *
 * DESCRIPTION: Callback function for acpi_ps_get_next_walk_op(). Used during
 *              acpi_ps_delete_parse tree to delete Op objects when all sub-objects
 *              have been visited (and deleted.)
 *
 ******************************************************************************/

acpi_status
acpi_ps_delete_completed_op (
	struct acpi_walk_state          *walk_state)
{

	acpi_ps_free_op (walk_state->op);
	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_delete_parse_tree
 *
 * PARAMETERS:  subtree_root        - Root of tree (or subtree) to delete
 *
 * RETURN:      None
 *
 * DESCRIPTION: Delete a portion of or an entire parse tree.
 *
 ******************************************************************************/

void
acpi_ps_delete_parse_tree (
	union acpi_parse_object         *subtree_root)
{
	struct acpi_walk_state          *walk_state;
	struct acpi_thread_state        *thread;
	acpi_status                     status;


	ACPI_FUNCTION_TRACE_PTR ("ps_delete_parse_tree", subtree_root);


	if (!subtree_root) {
		return_VOID;
	}

	/* Create and initialize a new walk list */

	thread = acpi_ut_create_thread_state ();
	if (!thread) {
		return_VOID;
	}

	walk_state = acpi_ds_create_walk_state (0, NULL, NULL, thread);
	if (!walk_state) {
		return_VOID;
	}

	walk_state->parse_flags         = 0;
	walk_state->descending_callback = NULL;
	walk_state->ascending_callback  = NULL;

	walk_state->origin = subtree_root;
	walk_state->next_op = subtree_root;

	/* Head downward in the tree */

	walk_state->next_op_info = ACPI_NEXT_OP_DOWNWARD;

	/* Visit all nodes in the subtree */

	while (walk_state->next_op) {
		status = acpi_ps_get_next_walk_op (walk_state, walk_state->next_op,
				 acpi_ps_delete_completed_op);
		if (ACPI_FAILURE (status)) {
			break;
		}
	}

	/* We are done with this walk */

	acpi_ut_delete_generic_state (ACPI_CAST_PTR (union acpi_generic_state, thread));
	acpi_ds_delete_walk_state (walk_state);

	return_VOID;
}