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