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