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