Merge master.kernel.org:/pub/scm/linux/kernel/git/chrisw/lsm-2.6
[linux-2.6] / drivers / acpi / parser / psloop.c
1 /******************************************************************************
2  *
3  * Module Name: psloop - Main AML parse loop
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, 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 /*
45  * Parse the AML and build an operation tree as most interpreters,
46  * like Perl, do.  Parsing is done by hand rather than with a YACC
47  * generated parser to tightly constrain stack and dynamic memory
48  * usage.  At the same time, parsing is kept flexible and the code
49  * fairly compact by parsing based on a list of AML opcode
50  * templates in aml_op_info[]
51  */
52
53 #include <acpi/acpi.h>
54 #include <acpi/acparser.h>
55 #include <acpi/acdispat.h>
56 #include <acpi/amlcode.h>
57
58 #define _COMPONENT          ACPI_PARSER
59 ACPI_MODULE_NAME("psloop")
60
61 static u32 acpi_gbl_depth = 0;
62
63 /*******************************************************************************
64  *
65  * FUNCTION:    acpi_ps_parse_loop
66  *
67  * PARAMETERS:  walk_state          - Current state
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
72  *              a tree of ops.
73  *
74  ******************************************************************************/
75
76 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
77 {
78         acpi_status status = AE_OK;
79         acpi_status status2;
80         union acpi_parse_object *op = NULL;     /* current op */
81         union acpi_parse_object *arg = NULL;
82         union acpi_parse_object *pre_op = NULL;
83         struct acpi_parse_state *parser_state;
84         u8 *aml_op_start = NULL;
85
86         ACPI_FUNCTION_TRACE_PTR("ps_parse_loop", walk_state);
87
88         if (walk_state->descending_callback == NULL) {
89                 return_ACPI_STATUS(AE_BAD_PARAMETER);
90         }
91
92         parser_state = &walk_state->parser_state;
93         walk_state->arg_types = 0;
94
95 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
96
97         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
98                 /* We are restarting a preempted control method */
99
100                 if (acpi_ps_has_completed_scope(parser_state)) {
101                         /*
102                          * We must check if a predicate to an IF or WHILE statement
103                          * was just completed
104                          */
105                         if ((parser_state->scope->parse_scope.op) &&
106                             ((parser_state->scope->parse_scope.op->common.
107                               aml_opcode == AML_IF_OP)
108                              || (parser_state->scope->parse_scope.op->common.
109                                  aml_opcode == AML_WHILE_OP))
110                             && (walk_state->control_state)
111                             && (walk_state->control_state->common.state ==
112                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
113                                 /*
114                                  * A predicate was just completed, get the value of the
115                                  * predicate and branch based on that value
116                                  */
117                                 walk_state->op = NULL;
118                                 status =
119                                     acpi_ds_get_predicate_value(walk_state,
120                                                                 ACPI_TO_POINTER
121                                                                 (TRUE));
122                                 if (ACPI_FAILURE(status)
123                                     && ((status & AE_CODE_MASK) !=
124                                         AE_CODE_CONTROL)) {
125                                         if (status == AE_AML_NO_RETURN_VALUE) {
126                                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
127                                                                   "Invoked method did not return a value, %s\n",
128                                                                   acpi_format_exception
129                                                                   (status)));
130
131                                         }
132                                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
133                                                           "get_predicate Failed, %s\n",
134                                                           acpi_format_exception
135                                                           (status)));
136                                         return_ACPI_STATUS(status);
137                                 }
138
139                                 status =
140                                     acpi_ps_next_parse_state(walk_state, op,
141                                                              status);
142                         }
143
144                         acpi_ps_pop_scope(parser_state, &op,
145                                           &walk_state->arg_types,
146                                           &walk_state->arg_count);
147                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
148                                           "Popped scope, Op=%p\n", op));
149                 } else if (walk_state->prev_op) {
150                         /* We were in the middle of an op */
151
152                         op = walk_state->prev_op;
153                         walk_state->arg_types = walk_state->prev_arg_types;
154                 }
155         }
156 #endif
157
158         /* Iterative parsing loop, while there is more AML to process: */
159
160         while ((parser_state->aml < parser_state->aml_end) || (op)) {
161                 aml_op_start = parser_state->aml;
162                 if (!op) {
163                         /* Get the next opcode from the AML stream */
164
165                         walk_state->aml_offset =
166                             (u32) ACPI_PTR_DIFF(parser_state->aml,
167                                                 parser_state->aml_start);
168                         walk_state->opcode = acpi_ps_peek_opcode(parser_state);
169
170                         /*
171                          * First cut to determine what we have found:
172                          * 1) A valid AML opcode
173                          * 2) A name string
174                          * 3) An unknown/invalid opcode
175                          */
176                         walk_state->op_info =
177                             acpi_ps_get_opcode_info(walk_state->opcode);
178                         switch (walk_state->op_info->class) {
179                         case AML_CLASS_ASCII:
180                         case AML_CLASS_PREFIX:
181                                 /*
182                                  * Starts with a valid prefix or ASCII char, this is a name
183                                  * string.  Convert the bare name string to a namepath.
184                                  */
185                                 walk_state->opcode = AML_INT_NAMEPATH_OP;
186                                 walk_state->arg_types = ARGP_NAMESTRING;
187                                 break;
188
189                         case AML_CLASS_UNKNOWN:
190
191                                 /* The opcode is unrecognized.  Just skip unknown opcodes */
192
193                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
194                                                   "Found unknown opcode %X at AML address %p offset %X, ignoring\n",
195                                                   walk_state->opcode,
196                                                   parser_state->aml,
197                                                   walk_state->aml_offset));
198
199                                 ACPI_DUMP_BUFFER(parser_state->aml, 128);
200
201                                 /* Assume one-byte bad opcode */
202
203                                 parser_state->aml++;
204                                 continue;
205
206                         default:
207
208                                 /* Found opcode info, this is a normal opcode */
209
210                                 parser_state->aml +=
211                                     acpi_ps_get_opcode_size(walk_state->opcode);
212                                 walk_state->arg_types =
213                                     walk_state->op_info->parse_args;
214                                 break;
215                         }
216
217                         /* Create Op structure and append to parent's argument list */
218
219                         if (walk_state->op_info->flags & AML_NAMED) {
220                                 /* Allocate a new pre_op if necessary */
221
222                                 if (!pre_op) {
223                                         pre_op =
224                                             acpi_ps_alloc_op(walk_state->
225                                                              opcode);
226                                         if (!pre_op) {
227                                                 status = AE_NO_MEMORY;
228                                                 goto close_this_op;
229                                         }
230                                 }
231
232                                 pre_op->common.value.arg = NULL;
233                                 pre_op->common.aml_opcode = walk_state->opcode;
234
235                                 /*
236                                  * Get and append arguments until we find the node that contains
237                                  * the name (the type ARGP_NAME).
238                                  */
239                                 while (GET_CURRENT_ARG_TYPE
240                                        (walk_state->arg_types)
241                                        &&
242                                        (GET_CURRENT_ARG_TYPE
243                                         (walk_state->arg_types) != ARGP_NAME)) {
244                                         status =
245                                             acpi_ps_get_next_arg(walk_state,
246                                                                  parser_state,
247                                                                  GET_CURRENT_ARG_TYPE
248                                                                  (walk_state->
249                                                                   arg_types),
250                                                                  &arg);
251                                         if (ACPI_FAILURE(status)) {
252                                                 goto close_this_op;
253                                         }
254
255                                         acpi_ps_append_arg(pre_op, arg);
256                                         INCREMENT_ARG_LIST(walk_state->
257                                                            arg_types);
258                                 }
259
260                                 /*
261                                  * Make sure that we found a NAME and didn't run out of
262                                  * arguments
263                                  */
264                                 if (!GET_CURRENT_ARG_TYPE
265                                     (walk_state->arg_types)) {
266                                         status = AE_AML_NO_OPERAND;
267                                         goto close_this_op;
268                                 }
269
270                                 /* We know that this arg is a name, move to next arg */
271
272                                 INCREMENT_ARG_LIST(walk_state->arg_types);
273
274                                 /*
275                                  * Find the object.  This will either insert the object into
276                                  * the namespace or simply look it up
277                                  */
278                                 walk_state->op = NULL;
279
280                                 status =
281                                     walk_state->descending_callback(walk_state,
282                                                                     &op);
283                                 if (ACPI_FAILURE(status)) {
284                                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
285                                                           "During name lookup/catalog, %s\n",
286                                                           acpi_format_exception
287                                                           (status)));
288                                         goto close_this_op;
289                                 }
290
291                                 if (!op) {
292                                         continue;
293                                 }
294
295                                 status =
296                                     acpi_ps_next_parse_state(walk_state, op,
297                                                              status);
298                                 if (status == AE_CTRL_PENDING) {
299                                         status = AE_OK;
300                                         goto close_this_op;
301                                 }
302
303                                 if (ACPI_FAILURE(status)) {
304                                         goto close_this_op;
305                                 }
306
307                                 acpi_ps_append_arg(op,
308                                                    pre_op->common.value.arg);
309                                 acpi_gbl_depth++;
310
311                                 if (op->common.aml_opcode == AML_REGION_OP) {
312                                         /*
313                                          * Defer final parsing of an operation_region body,
314                                          * because we don't have enough info in the first pass
315                                          * to parse it correctly (i.e., there may be method
316                                          * calls within the term_arg elements of the body.)
317                                          *
318                                          * However, we must continue parsing because
319                                          * the opregion is not a standalone package --
320                                          * we don't know where the end is at this point.
321                                          *
322                                          * (Length is unknown until parse of the body complete)
323                                          */
324                                         op->named.data = aml_op_start;
325                                         op->named.length = 0;
326                                 }
327                         } else {
328                                 /* Not a named opcode, just allocate Op and append to parent */
329
330                                 walk_state->op_info =
331                                     acpi_ps_get_opcode_info(walk_state->opcode);
332                                 op = acpi_ps_alloc_op(walk_state->opcode);
333                                 if (!op) {
334                                         status = AE_NO_MEMORY;
335                                         goto close_this_op;
336                                 }
337
338                                 if (walk_state->op_info->flags & AML_CREATE) {
339                                         /*
340                                          * Backup to beginning of create_xXXfield declaration
341                                          * body_length is unknown until we parse the body
342                                          */
343                                         op->named.data = aml_op_start;
344                                         op->named.length = 0;
345                                 }
346
347                                 acpi_ps_append_arg(acpi_ps_get_parent_scope
348                                                    (parser_state), op);
349
350                                 if ((walk_state->descending_callback != NULL)) {
351                                         /*
352                                          * Find the object. This will either insert the object into
353                                          * the namespace or simply look it up
354                                          */
355                                         walk_state->op = op;
356
357                                         status =
358                                             walk_state->
359                                             descending_callback(walk_state,
360                                                                 &op);
361                                         status =
362                                             acpi_ps_next_parse_state(walk_state,
363                                                                      op,
364                                                                      status);
365                                         if (status == AE_CTRL_PENDING) {
366                                                 status = AE_OK;
367                                                 goto close_this_op;
368                                         }
369
370                                         if (ACPI_FAILURE(status)) {
371                                                 goto close_this_op;
372                                         }
373                                 }
374                         }
375
376                         op->common.aml_offset = walk_state->aml_offset;
377
378                         if (walk_state->op_info) {
379                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
380                                                   "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
381                                                   (u32) op->common.aml_opcode,
382                                                   walk_state->op_info->name, op,
383                                                   parser_state->aml,
384                                                   op->common.aml_offset));
385                         }
386                 }
387
388                 /*
389                  * Start arg_count at zero because we don't know if there are
390                  * any args yet
391                  */
392                 walk_state->arg_count = 0;
393
394                 /* Are there any arguments that must be processed? */
395
396                 if (walk_state->arg_types) {
397                         /* Get arguments */
398
399                         switch (op->common.aml_opcode) {
400                         case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
401                         case AML_WORD_OP:       /* AML_WORDDATA_ARG */
402                         case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
403                         case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
404                         case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
405
406                                 /* Fill in constant or string argument directly */
407
408                                 acpi_ps_get_next_simple_arg(parser_state,
409                                                             GET_CURRENT_ARG_TYPE
410                                                             (walk_state->
411                                                              arg_types), op);
412                                 break;
413
414                         case AML_INT_NAMEPATH_OP:       /* AML_NAMESTRING_ARG */
415
416                                 status =
417                                     acpi_ps_get_next_namepath(walk_state,
418                                                               parser_state, op,
419                                                               1);
420                                 if (ACPI_FAILURE(status)) {
421                                         goto close_this_op;
422                                 }
423
424                                 walk_state->arg_types = 0;
425                                 break;
426
427                         default:
428                                 /*
429                                  * Op is not a constant or string, append each argument
430                                  * to the Op
431                                  */
432                                 while (GET_CURRENT_ARG_TYPE
433                                        (walk_state->arg_types)
434                                        && !walk_state->arg_count) {
435                                         walk_state->aml_offset = (u32)
436                                             ACPI_PTR_DIFF(parser_state->aml,
437                                                           parser_state->
438                                                           aml_start);
439
440                                         status =
441                                             acpi_ps_get_next_arg(walk_state,
442                                                                  parser_state,
443                                                                  GET_CURRENT_ARG_TYPE
444                                                                  (walk_state->
445                                                                   arg_types),
446                                                                  &arg);
447                                         if (ACPI_FAILURE(status)) {
448                                                 goto close_this_op;
449                                         }
450
451                                         if (arg) {
452                                                 arg->common.aml_offset =
453                                                     walk_state->aml_offset;
454                                                 acpi_ps_append_arg(op, arg);
455                                         }
456                                         INCREMENT_ARG_LIST(walk_state->
457                                                            arg_types);
458                                 }
459
460                                 /* Special processing for certain opcodes */
461
462                                 /* TBD (remove): Temporary mechanism to disable this code if needed */
463
464 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
465
466                                 if ((walk_state->pass_number <=
467                                      ACPI_IMODE_LOAD_PASS1)
468                                     &&
469                                     ((walk_state->
470                                       parse_flags & ACPI_PARSE_DISASSEMBLE) ==
471                                      0)) {
472                                         /*
473                                          * We want to skip If/Else/While constructs during Pass1
474                                          * because we want to actually conditionally execute the
475                                          * code during Pass2.
476                                          *
477                                          * Except for disassembly, where we always want to
478                                          * walk the If/Else/While packages
479                                          */
480                                         switch (op->common.aml_opcode) {
481                                         case AML_IF_OP:
482                                         case AML_ELSE_OP:
483                                         case AML_WHILE_OP:
484
485                                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
486                                                                   "Pass1: Skipping an If/Else/While body\n"));
487
488                                                 /* Skip body of if/else/while in pass 1 */
489
490                                                 parser_state->aml =
491                                                     parser_state->pkg_end;
492                                                 walk_state->arg_count = 0;
493                                                 break;
494
495                                         default:
496                                                 break;
497                                         }
498                                 }
499 #endif
500                                 switch (op->common.aml_opcode) {
501                                 case AML_METHOD_OP:
502
503                                         /*
504                                          * Skip parsing of control method
505                                          * because we don't have enough info in the first pass
506                                          * to parse it correctly.
507                                          *
508                                          * Save the length and address of the body
509                                          */
510                                         op->named.data = parser_state->aml;
511                                         op->named.length =
512                                             (u32) (parser_state->pkg_end -
513                                                    parser_state->aml);
514
515                                         /* Skip body of method */
516
517                                         parser_state->aml =
518                                             parser_state->pkg_end;
519                                         walk_state->arg_count = 0;
520                                         break;
521
522                                 case AML_BUFFER_OP:
523                                 case AML_PACKAGE_OP:
524                                 case AML_VAR_PACKAGE_OP:
525
526                                         if ((op->common.parent) &&
527                                             (op->common.parent->common.
528                                              aml_opcode == AML_NAME_OP)
529                                             && (walk_state->pass_number <=
530                                                 ACPI_IMODE_LOAD_PASS2)) {
531                                                 /*
532                                                  * Skip parsing of Buffers and Packages
533                                                  * because we don't have enough info in the first pass
534                                                  * to parse them correctly.
535                                                  */
536                                                 op->named.data = aml_op_start;
537                                                 op->named.length =
538                                                     (u32) (parser_state->
539                                                            pkg_end -
540                                                            aml_op_start);
541
542                                                 /* Skip body */
543
544                                                 parser_state->aml =
545                                                     parser_state->pkg_end;
546                                                 walk_state->arg_count = 0;
547                                         }
548                                         break;
549
550                                 case AML_WHILE_OP:
551
552                                         if (walk_state->control_state) {
553                                                 walk_state->control_state->
554                                                     control.package_end =
555                                                     parser_state->pkg_end;
556                                         }
557                                         break;
558
559                                 default:
560
561                                         /* No action for all other opcodes */
562                                         break;
563                                 }
564                                 break;
565                         }
566                 }
567
568                 /* Check for arguments that need to be processed */
569
570                 if (walk_state->arg_count) {
571                         /*
572                          * There are arguments (complex ones), push Op and
573                          * prepare for argument
574                          */
575                         status = acpi_ps_push_scope(parser_state, op,
576                                                     walk_state->arg_types,
577                                                     walk_state->arg_count);
578                         if (ACPI_FAILURE(status)) {
579                                 goto close_this_op;
580                         }
581                         op = NULL;
582                         continue;
583                 }
584
585                 /*
586                  * All arguments have been processed -- Op is complete,
587                  * prepare for next
588                  */
589                 walk_state->op_info =
590                     acpi_ps_get_opcode_info(op->common.aml_opcode);
591                 if (walk_state->op_info->flags & AML_NAMED) {
592                         if (acpi_gbl_depth) {
593                                 acpi_gbl_depth--;
594                         }
595
596                         if (op->common.aml_opcode == AML_REGION_OP) {
597                                 /*
598                                  * Skip parsing of control method or opregion body,
599                                  * because we don't have enough info in the first pass
600                                  * to parse them correctly.
601                                  *
602                                  * Completed parsing an op_region declaration, we now
603                                  * know the length.
604                                  */
605                                 op->named.length =
606                                     (u32) (parser_state->aml - op->named.data);
607                         }
608                 }
609
610                 if (walk_state->op_info->flags & AML_CREATE) {
611                         /*
612                          * Backup to beginning of create_xXXfield declaration (1 for
613                          * Opcode)
614                          *
615                          * body_length is unknown until we parse the body
616                          */
617                         op->named.length =
618                             (u32) (parser_state->aml - op->named.data);
619                 }
620
621                 /* This op complete, notify the dispatcher */
622
623                 if (walk_state->ascending_callback != NULL) {
624                         walk_state->op = op;
625                         walk_state->opcode = op->common.aml_opcode;
626
627                         status = walk_state->ascending_callback(walk_state);
628                         status =
629                             acpi_ps_next_parse_state(walk_state, op, status);
630                         if (status == AE_CTRL_PENDING) {
631                                 status = AE_OK;
632                                 goto close_this_op;
633                         }
634                 }
635
636               close_this_op:
637                 /*
638                  * Finished one argument of the containing scope
639                  */
640                 parser_state->scope->parse_scope.arg_count--;
641
642                 /* Finished with pre_op */
643
644                 if (pre_op) {
645                         acpi_ps_free_op(pre_op);
646                         pre_op = NULL;
647                 }
648
649                 /* Close this Op (will result in parse subtree deletion) */
650
651                 status2 = acpi_ps_complete_this_op(walk_state, op);
652                 if (ACPI_FAILURE(status2)) {
653                         return_ACPI_STATUS(status2);
654                 }
655                 op = NULL;
656
657                 switch (status) {
658                 case AE_OK:
659                         break;
660
661                 case AE_CTRL_TRANSFER:
662
663                         /* We are about to transfer to a called method. */
664
665                         walk_state->prev_op = op;
666                         walk_state->prev_arg_types = walk_state->arg_types;
667                         return_ACPI_STATUS(status);
668
669                 case AE_CTRL_END:
670
671                         acpi_ps_pop_scope(parser_state, &op,
672                                           &walk_state->arg_types,
673                                           &walk_state->arg_count);
674
675                         if (op) {
676                                 walk_state->op = op;
677                                 walk_state->op_info =
678                                     acpi_ps_get_opcode_info(op->common.
679                                                             aml_opcode);
680                                 walk_state->opcode = op->common.aml_opcode;
681
682                                 status =
683                                     walk_state->ascending_callback(walk_state);
684                                 status =
685                                     acpi_ps_next_parse_state(walk_state, op,
686                                                              status);
687
688                                 status2 =
689                                     acpi_ps_complete_this_op(walk_state, op);
690                                 if (ACPI_FAILURE(status2)) {
691                                         return_ACPI_STATUS(status2);
692                                 }
693                                 op = NULL;
694                         }
695                         status = AE_OK;
696                         break;
697
698                 case AE_CTRL_BREAK:
699                 case AE_CTRL_CONTINUE:
700
701                         /* Pop off scopes until we find the While */
702
703                         while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
704                                 acpi_ps_pop_scope(parser_state, &op,
705                                                   &walk_state->arg_types,
706                                                   &walk_state->arg_count);
707                         }
708
709                         /* Close this iteration of the While loop */
710
711                         walk_state->op = op;
712                         walk_state->op_info =
713                             acpi_ps_get_opcode_info(op->common.aml_opcode);
714                         walk_state->opcode = op->common.aml_opcode;
715
716                         status = walk_state->ascending_callback(walk_state);
717                         status =
718                             acpi_ps_next_parse_state(walk_state, op, status);
719
720                         status2 = acpi_ps_complete_this_op(walk_state, op);
721                         if (ACPI_FAILURE(status2)) {
722                                 return_ACPI_STATUS(status2);
723                         }
724                         op = NULL;
725
726                         status = AE_OK;
727                         break;
728
729                 case AE_CTRL_TERMINATE:
730
731                         status = AE_OK;
732
733                         /* Clean up */
734                         do {
735                                 if (op) {
736                                         status2 =
737                                             acpi_ps_complete_this_op(walk_state,
738                                                                      op);
739                                         if (ACPI_FAILURE(status2)) {
740                                                 return_ACPI_STATUS(status2);
741                                         }
742                                 }
743                                 acpi_ps_pop_scope(parser_state, &op,
744                                                   &walk_state->arg_types,
745                                                   &walk_state->arg_count);
746
747                         } while (op);
748
749                         return_ACPI_STATUS(status);
750
751                 default:        /* All other non-AE_OK status */
752
753                         do {
754                                 if (op) {
755                                         status2 =
756                                             acpi_ps_complete_this_op(walk_state,
757                                                                      op);
758                                         if (ACPI_FAILURE(status2)) {
759                                                 return_ACPI_STATUS(status2);
760                                         }
761                                 }
762                                 acpi_ps_pop_scope(parser_state, &op,
763                                                   &walk_state->arg_types,
764                                                   &walk_state->arg_count);
765
766                         } while (op);
767
768                         /*
769                          * TBD: Cleanup parse ops on error
770                          */
771 #if 0
772                         if (op == NULL) {
773                                 acpi_ps_pop_scope(parser_state, &op,
774                                                   &walk_state->arg_types,
775                                                   &walk_state->arg_count);
776                         }
777 #endif
778                         walk_state->prev_op = op;
779                         walk_state->prev_arg_types = walk_state->arg_types;
780                         return_ACPI_STATUS(status);
781                 }
782
783                 /* This scope complete? */
784
785                 if (acpi_ps_has_completed_scope(parser_state)) {
786                         acpi_ps_pop_scope(parser_state, &op,
787                                           &walk_state->arg_types,
788                                           &walk_state->arg_count);
789                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
790                                           "Popped scope, Op=%p\n", op));
791                 } else {
792                         op = NULL;
793                 }
794
795         }                       /* while parser_state->Aml */
796
797         /*
798          * Complete the last Op (if not completed), and clear the scope stack.
799          * It is easily possible to end an AML "package" with an unbounded number
800          * of open scopes (such as when several ASL blocks are closed with
801          * sequential closing braces).  We want to terminate each one cleanly.
802          */
803         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
804                           op));
805         do {
806                 if (op) {
807                         if (walk_state->ascending_callback != NULL) {
808                                 walk_state->op = op;
809                                 walk_state->op_info =
810                                     acpi_ps_get_opcode_info(op->common.
811                                                             aml_opcode);
812                                 walk_state->opcode = op->common.aml_opcode;
813
814                                 status =
815                                     walk_state->ascending_callback(walk_state);
816                                 status =
817                                     acpi_ps_next_parse_state(walk_state, op,
818                                                              status);
819                                 if (status == AE_CTRL_PENDING) {
820                                         status = AE_OK;
821                                         goto close_this_op;
822                                 }
823
824                                 if (status == AE_CTRL_TERMINATE) {
825                                         status = AE_OK;
826
827                                         /* Clean up */
828                                         do {
829                                                 if (op) {
830                                                         status2 =
831                                                             acpi_ps_complete_this_op
832                                                             (walk_state, op);
833                                                         if (ACPI_FAILURE
834                                                             (status2)) {
835                                                                 return_ACPI_STATUS
836                                                                     (status2);
837                                                         }
838                                                 }
839
840                                                 acpi_ps_pop_scope(parser_state,
841                                                                   &op,
842                                                                   &walk_state->
843                                                                   arg_types,
844                                                                   &walk_state->
845                                                                   arg_count);
846
847                                         } while (op);
848
849                                         return_ACPI_STATUS(status);
850                                 }
851
852                                 else if (ACPI_FAILURE(status)) {
853                                         /* First error is most important */
854
855                                         (void)
856                                             acpi_ps_complete_this_op(walk_state,
857                                                                      op);
858                                         return_ACPI_STATUS(status);
859                                 }
860                         }
861
862                         status2 = acpi_ps_complete_this_op(walk_state, op);
863                         if (ACPI_FAILURE(status2)) {
864                                 return_ACPI_STATUS(status2);
865                         }
866                 }
867
868                 acpi_ps_pop_scope(parser_state, &op, &walk_state->arg_types,
869                                   &walk_state->arg_count);
870
871         } while (op);
872
873         return_ACPI_STATUS(status);
874 }