]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/parser/psloop.c
ACPICA: Several fixes for internal method result stack
[linux-2.6-omap-h63xx.git] / 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
608                 /* Close this iteration of the While loop */
609
610                 walk_state->op = *op;
611                 walk_state->op_info =
612                     acpi_ps_get_opcode_info((*op)->common.aml_opcode);
613                 walk_state->opcode = (*op)->common.aml_opcode;
614
615                 status = walk_state->ascending_callback(walk_state);
616                 status = acpi_ps_next_parse_state(walk_state, *op, status);
617
618                 status2 = acpi_ps_complete_this_op(walk_state, *op);
619                 if (ACPI_FAILURE(status2)) {
620                         return_ACPI_STATUS(status2);
621                 }
622
623                 status = AE_OK;
624                 break;
625
626         case AE_CTRL_TERMINATE:
627
628                 /* Clean up */
629                 do {
630                         if (*op) {
631                                 status2 =
632                                     acpi_ps_complete_this_op(walk_state, *op);
633                                 if (ACPI_FAILURE(status2)) {
634                                         return_ACPI_STATUS(status2);
635                                 }
636
637                                 acpi_ut_delete_generic_state
638                                     (acpi_ut_pop_generic_state
639                                      (&walk_state->control_state));
640                         }
641
642                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
643                                           &walk_state->arg_types,
644                                           &walk_state->arg_count);
645
646                 } while (*op);
647
648                 return_ACPI_STATUS(AE_OK);
649
650         default:                /* All other non-AE_OK status */
651
652                 do {
653                         if (*op) {
654                                 status2 =
655                                     acpi_ps_complete_this_op(walk_state, *op);
656                                 if (ACPI_FAILURE(status2)) {
657                                         return_ACPI_STATUS(status2);
658                                 }
659                         }
660
661                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
662                                           &walk_state->arg_types,
663                                           &walk_state->arg_count);
664
665                 } while (*op);
666
667 #if 0
668                 /*
669                  * TBD: Cleanup parse ops on error
670                  */
671                 if (*op == NULL) {
672                         acpi_ps_pop_scope(parser_state, op,
673                                           &walk_state->arg_types,
674                                           &walk_state->arg_count);
675                 }
676 #endif
677                 walk_state->prev_op = NULL;
678                 walk_state->prev_arg_types = walk_state->arg_types;
679                 return_ACPI_STATUS(status);
680         }
681
682         /* This scope complete? */
683
684         if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
685                 acpi_ps_pop_scope(&(walk_state->parser_state), op,
686                                   &walk_state->arg_types,
687                                   &walk_state->arg_count);
688                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
689         } else {
690                 *op = NULL;
691         }
692
693         return_ACPI_STATUS(AE_OK);
694 }
695
696 /*******************************************************************************
697  *
698  * FUNCTION:    acpi_ps_complete_final_op
699  *
700  * PARAMETERS:  walk_state          - Current state
701  *              Op                  - Current Op
702  *              Status              - Current parse status before complete last
703  *                                    Op
704  *
705  * RETURN:      Status
706  *
707  * DESCRIPTION: Complete last Op.
708  *
709  ******************************************************************************/
710
711 static acpi_status
712 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
713                           union acpi_parse_object *op, acpi_status status)
714 {
715         acpi_status status2;
716
717         ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
718
719         /*
720          * Complete the last Op (if not completed), and clear the scope stack.
721          * It is easily possible to end an AML "package" with an unbounded number
722          * of open scopes (such as when several ASL blocks are closed with
723          * sequential closing braces). We want to terminate each one cleanly.
724          */
725         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
726                           op));
727         do {
728                 if (op) {
729                         if (walk_state->ascending_callback != NULL) {
730                                 walk_state->op = op;
731                                 walk_state->op_info =
732                                     acpi_ps_get_opcode_info(op->common.
733                                                             aml_opcode);
734                                 walk_state->opcode = op->common.aml_opcode;
735
736                                 status =
737                                     walk_state->ascending_callback(walk_state);
738                                 status =
739                                     acpi_ps_next_parse_state(walk_state, op,
740                                                              status);
741                                 if (status == AE_CTRL_PENDING) {
742                                         status =
743                                             acpi_ps_complete_op(walk_state, &op,
744                                                                 AE_OK);
745                                         if (ACPI_FAILURE(status)) {
746                                                 return_ACPI_STATUS(status);
747                                         }
748                                 }
749
750                                 if (status == AE_CTRL_TERMINATE) {
751                                         status = AE_OK;
752
753                                         /* Clean up */
754                                         do {
755                                                 if (op) {
756                                                         status2 =
757                                                             acpi_ps_complete_this_op
758                                                             (walk_state, op);
759                                                         if (ACPI_FAILURE
760                                                             (status2)) {
761                                                                 return_ACPI_STATUS
762                                                                     (status2);
763                                                         }
764                                                 }
765
766                                                 acpi_ps_pop_scope(&
767                                                                   (walk_state->
768                                                                    parser_state),
769                                                                   &op,
770                                                                   &walk_state->
771                                                                   arg_types,
772                                                                   &walk_state->
773                                                                   arg_count);
774
775                                         } while (op);
776
777                                         return_ACPI_STATUS(status);
778                                 }
779
780                                 else if (ACPI_FAILURE(status)) {
781
782                                         /* First error is most important */
783
784                                         (void)
785                                             acpi_ps_complete_this_op(walk_state,
786                                                                      op);
787                                         return_ACPI_STATUS(status);
788                                 }
789                         }
790
791                         status2 = acpi_ps_complete_this_op(walk_state, op);
792                         if (ACPI_FAILURE(status2)) {
793                                 return_ACPI_STATUS(status2);
794                         }
795                 }
796
797                 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
798                                   &walk_state->arg_types,
799                                   &walk_state->arg_count);
800
801         } while (op);
802
803         return_ACPI_STATUS(status);
804 }
805
806 /*******************************************************************************
807  *
808  * FUNCTION:    acpi_ps_parse_loop
809  *
810  * PARAMETERS:  walk_state          - Current state
811  *
812  * RETURN:      Status
813  *
814  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
815  *              a tree of ops.
816  *
817  ******************************************************************************/
818
819 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
820 {
821         acpi_status status = AE_OK;
822         union acpi_parse_object *op = NULL;     /* current op */
823         struct acpi_parse_state *parser_state;
824         u8 *aml_op_start = NULL;
825
826         ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
827
828         if (walk_state->descending_callback == NULL) {
829                 return_ACPI_STATUS(AE_BAD_PARAMETER);
830         }
831
832         parser_state = &walk_state->parser_state;
833         walk_state->arg_types = 0;
834
835 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
836
837         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
838
839                 /* We are restarting a preempted control method */
840
841                 if (acpi_ps_has_completed_scope(parser_state)) {
842                         /*
843                          * We must check if a predicate to an IF or WHILE statement
844                          * was just completed
845                          */
846                         if ((parser_state->scope->parse_scope.op) &&
847                             ((parser_state->scope->parse_scope.op->common.
848                               aml_opcode == AML_IF_OP)
849                              || (parser_state->scope->parse_scope.op->common.
850                                  aml_opcode == AML_WHILE_OP))
851                             && (walk_state->control_state)
852                             && (walk_state->control_state->common.state ==
853                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
854                                 /*
855                                  * A predicate was just completed, get the value of the
856                                  * predicate and branch based on that value
857                                  */
858                                 walk_state->op = NULL;
859                                 status =
860                                     acpi_ds_get_predicate_value(walk_state,
861                                                                 ACPI_TO_POINTER
862                                                                 (TRUE));
863                                 if (ACPI_FAILURE(status)
864                                     && ((status & AE_CODE_MASK) !=
865                                         AE_CODE_CONTROL)) {
866                                         if (status == AE_AML_NO_RETURN_VALUE) {
867                                                 ACPI_EXCEPTION((AE_INFO, status,
868                                                                 "Invoked method did not return a value"));
869
870                                         }
871
872                                         ACPI_EXCEPTION((AE_INFO, status,
873                                                         "GetPredicate Failed"));
874                                         return_ACPI_STATUS(status);
875                                 }
876
877                                 status =
878                                     acpi_ps_next_parse_state(walk_state, op,
879                                                              status);
880                         }
881
882                         acpi_ps_pop_scope(parser_state, &op,
883                                           &walk_state->arg_types,
884                                           &walk_state->arg_count);
885                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
886                                           "Popped scope, Op=%p\n", op));
887                 } else if (walk_state->prev_op) {
888
889                         /* We were in the middle of an op */
890
891                         op = walk_state->prev_op;
892                         walk_state->arg_types = walk_state->prev_arg_types;
893                 }
894         }
895 #endif
896
897         /* Iterative parsing loop, while there is more AML to process: */
898
899         while ((parser_state->aml < parser_state->aml_end) || (op)) {
900                 aml_op_start = parser_state->aml;
901                 if (!op) {
902                         status =
903                             acpi_ps_create_op(walk_state, aml_op_start, &op);
904                         if (ACPI_FAILURE(status)) {
905                                 if (status == AE_CTRL_PARSE_CONTINUE) {
906                                         continue;
907                                 }
908
909                                 if (status == AE_CTRL_PARSE_PENDING) {
910                                         status = AE_OK;
911                                 }
912
913                                 status =
914                                     acpi_ps_complete_op(walk_state, &op,
915                                                         status);
916                                 if (ACPI_FAILURE(status)) {
917                                         return_ACPI_STATUS(status);
918                                 }
919
920                                 continue;
921                         }
922
923                         op->common.aml_offset = walk_state->aml_offset;
924
925                         if (walk_state->op_info) {
926                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
927                                                   "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
928                                                   (u32) op->common.aml_opcode,
929                                                   walk_state->op_info->name, op,
930                                                   parser_state->aml,
931                                                   op->common.aml_offset));
932                         }
933                 }
934
935                 /*
936                  * Start arg_count at zero because we don't know if there are
937                  * any args yet
938                  */
939                 walk_state->arg_count = 0;
940
941                 /* Are there any arguments that must be processed? */
942
943                 if (walk_state->arg_types) {
944
945                         /* Get arguments */
946
947                         status =
948                             acpi_ps_get_arguments(walk_state, aml_op_start, op);
949                         if (ACPI_FAILURE(status)) {
950                                 status =
951                                     acpi_ps_complete_op(walk_state, &op,
952                                                         status);
953                                 if (ACPI_FAILURE(status)) {
954                                         return_ACPI_STATUS(status);
955                                 }
956
957                                 continue;
958                         }
959                 }
960
961                 /* Check for arguments that need to be processed */
962
963                 if (walk_state->arg_count) {
964                         /*
965                          * There are arguments (complex ones), push Op and
966                          * prepare for argument
967                          */
968                         status = acpi_ps_push_scope(parser_state, op,
969                                                     walk_state->arg_types,
970                                                     walk_state->arg_count);
971                         if (ACPI_FAILURE(status)) {
972                                 status =
973                                     acpi_ps_complete_op(walk_state, &op,
974                                                         status);
975                                 if (ACPI_FAILURE(status)) {
976                                         return_ACPI_STATUS(status);
977                                 }
978
979                                 continue;
980                         }
981
982                         op = NULL;
983                         continue;
984                 }
985
986                 /*
987                  * All arguments have been processed -- Op is complete,
988                  * prepare for next
989                  */
990                 walk_state->op_info =
991                     acpi_ps_get_opcode_info(op->common.aml_opcode);
992                 if (walk_state->op_info->flags & AML_NAMED) {
993                         if (acpi_gbl_depth) {
994                                 acpi_gbl_depth--;
995                         }
996
997                         if (op->common.aml_opcode == AML_REGION_OP) {
998                                 /*
999                                  * Skip parsing of control method or opregion body,
1000                                  * because we don't have enough info in the first pass
1001                                  * to parse them correctly.
1002                                  *
1003                                  * Completed parsing an op_region declaration, we now
1004                                  * know the length.
1005                                  */
1006                                 op->named.length =
1007                                     (u32) (parser_state->aml - op->named.data);
1008                         }
1009                 }
1010
1011                 if (walk_state->op_info->flags & AML_CREATE) {
1012                         /*
1013                          * Backup to beginning of create_xXXfield declaration (1 for
1014                          * Opcode)
1015                          *
1016                          * body_length is unknown until we parse the body
1017                          */
1018                         op->named.length =
1019                             (u32) (parser_state->aml - op->named.data);
1020                 }
1021
1022                 /* This op complete, notify the dispatcher */
1023
1024                 if (walk_state->ascending_callback != NULL) {
1025                         walk_state->op = op;
1026                         walk_state->opcode = op->common.aml_opcode;
1027
1028                         status = walk_state->ascending_callback(walk_state);
1029                         status =
1030                             acpi_ps_next_parse_state(walk_state, op, status);
1031                         if (status == AE_CTRL_PENDING) {
1032                                 status = AE_OK;
1033                         }
1034                 }
1035
1036                 status = acpi_ps_complete_op(walk_state, &op, status);
1037                 if (ACPI_FAILURE(status)) {
1038                         return_ACPI_STATUS(status);
1039                 }
1040
1041         }                       /* while parser_state->Aml */
1042
1043         status = acpi_ps_complete_final_op(walk_state, op, status);
1044         return_ACPI_STATUS(status);
1045 }