Merge /home/torvalds/linux-2.6-arm
[pandora-kernel.git] / drivers / acpi / parser / psargs.c
1 /******************************************************************************
2  *
3  * Module Name: psargs - Parse AML opcode arguments
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acnamesp.h>
49
50 #define _COMPONENT          ACPI_PARSER
51          ACPI_MODULE_NAME    ("psargs")
52
53 /* Local prototypes */
54
55 static u32
56 acpi_ps_get_next_package_length (
57         struct acpi_parse_state         *parser_state);
58
59 static union acpi_parse_object *
60 acpi_ps_get_next_field (
61         struct acpi_parse_state         *parser_state);
62
63
64 /*******************************************************************************
65  *
66  * FUNCTION:    acpi_ps_get_next_package_length
67  *
68  * PARAMETERS:  parser_state        - Current parser state object
69  *
70  * RETURN:      Decoded package length.  On completion, the AML pointer points
71  *              past the length byte or bytes.
72  *
73  * DESCRIPTION: Decode and return a package length field
74  *
75  ******************************************************************************/
76
77 static u32
78 acpi_ps_get_next_package_length (
79         struct acpi_parse_state         *parser_state)
80 {
81         u32                             encoded_length;
82         u32                             length = 0;
83
84
85         ACPI_FUNCTION_TRACE ("ps_get_next_package_length");
86
87
88         encoded_length = (u32) ACPI_GET8 (parser_state->aml);
89         parser_state->aml++;
90
91         switch (encoded_length >> 6) /* bits 6-7 contain encoding scheme */ {
92         case 0: /* 1-byte encoding (bits 0-5) */
93
94                 length = (encoded_length & 0x3F);
95                 break;
96
97
98         case 1: /* 2-byte encoding (next byte + bits 0-3) */
99
100                 length = ((ACPI_GET8 (parser_state->aml) << 04) |
101                                  (encoded_length & 0x0F));
102                 parser_state->aml++;
103                 break;
104
105
106         case 2: /* 3-byte encoding (next 2 bytes + bits 0-3) */
107
108                 length = ((ACPI_GET8 (parser_state->aml + 1) << 12) |
109                                   (ACPI_GET8 (parser_state->aml)    << 04) |
110                                   (encoded_length & 0x0F));
111                 parser_state->aml += 2;
112                 break;
113
114
115         case 3: /* 4-byte encoding (next 3 bytes + bits 0-3) */
116
117                 length = ((ACPI_GET8 (parser_state->aml + 2) << 20) |
118                                   (ACPI_GET8 (parser_state->aml + 1) << 12) |
119                                   (ACPI_GET8 (parser_state->aml)    << 04) |
120                                   (encoded_length & 0x0F));
121                 parser_state->aml += 3;
122                 break;
123
124         default:
125
126                 /* Can't get here, only 2 bits / 4 cases */
127                 break;
128         }
129
130         return_VALUE (length);
131 }
132
133
134 /*******************************************************************************
135  *
136  * FUNCTION:    acpi_ps_get_next_package_end
137  *
138  * PARAMETERS:  parser_state        - Current parser state object
139  *
140  * RETURN:      Pointer to end-of-package +1
141  *
142  * DESCRIPTION: Get next package length and return a pointer past the end of
143  *              the package.  Consumes the package length field
144  *
145  ******************************************************************************/
146
147 u8 *
148 acpi_ps_get_next_package_end (
149         struct acpi_parse_state         *parser_state)
150 {
151         u8                              *start = parser_state->aml;
152         acpi_native_uint                length;
153
154
155         ACPI_FUNCTION_TRACE ("ps_get_next_package_end");
156
157
158         /* Function below changes parser_state->Aml */
159
160         length = (acpi_native_uint) acpi_ps_get_next_package_length (parser_state);
161
162         return_PTR (start + length); /* end of package */
163 }
164
165
166 /*******************************************************************************
167  *
168  * FUNCTION:    acpi_ps_get_next_namestring
169  *
170  * PARAMETERS:  parser_state        - Current parser state object
171  *
172  * RETURN:      Pointer to the start of the name string (pointer points into
173  *              the AML.
174  *
175  * DESCRIPTION: Get next raw namestring within the AML stream.  Handles all name
176  *              prefix characters.  Set parser state to point past the string.
177  *              (Name is consumed from the AML.)
178  *
179  ******************************************************************************/
180
181 char *
182 acpi_ps_get_next_namestring (
183         struct acpi_parse_state         *parser_state)
184 {
185         u8                              *start = parser_state->aml;
186         u8                              *end = parser_state->aml;
187
188
189         ACPI_FUNCTION_TRACE ("ps_get_next_namestring");
190
191
192         /* Handle multiple prefix characters */
193
194         while (acpi_ps_is_prefix_char (ACPI_GET8 (end))) {
195                 /* Include prefix '\\' or '^' */
196
197                 end++;
198         }
199
200         /* Decode the path */
201
202         switch (ACPI_GET8 (end)) {
203         case 0:
204
205                 /* null_name */
206
207                 if (end == start) {
208                         start = NULL;
209                 }
210                 end++;
211                 break;
212
213         case AML_DUAL_NAME_PREFIX:
214
215                 /* Two name segments */
216
217                 end += 1 + (2 * ACPI_NAME_SIZE);
218                 break;
219
220         case AML_MULTI_NAME_PREFIX_OP:
221
222                 /* Multiple name segments, 4 chars each */
223
224                 end += 2 + ((acpi_size) ACPI_GET8 (end + 1) * ACPI_NAME_SIZE);
225                 break;
226
227         default:
228
229                 /* Single name segment */
230
231                 end += ACPI_NAME_SIZE;
232                 break;
233         }
234
235         parser_state->aml = (u8*) end;
236         return_PTR ((char *) start);
237 }
238
239
240 /*******************************************************************************
241  *
242  * FUNCTION:    acpi_ps_get_next_namepath
243  *
244  * PARAMETERS:  parser_state        - Current parser state object
245  *              Arg                 - Where the namepath will be stored
246  *              arg_count           - If the namepath points to a control method
247  *                                    the method's argument is returned here.
248  *              method_call         - Whether the namepath can possibly be the
249  *                                    start of a method call
250  *
251  * RETURN:      Status
252  *
253  * DESCRIPTION: Get next name (if method call, return # of required args).
254  *              Names are looked up in the internal namespace to determine
255  *              if the name represents a control method.  If a method
256  *              is found, the number of arguments to the method is returned.
257  *              This information is critical for parsing to continue correctly.
258  *
259  ******************************************************************************/
260
261 acpi_status
262 acpi_ps_get_next_namepath (
263         struct acpi_walk_state          *walk_state,
264         struct acpi_parse_state         *parser_state,
265         union acpi_parse_object         *arg,
266         u8                              method_call)
267 {
268         char                            *path;
269         union acpi_parse_object         *name_op;
270         acpi_status                     status = AE_OK;
271         union acpi_operand_object       *method_desc;
272         struct acpi_namespace_node      *node;
273         union acpi_generic_state        scope_info;
274
275
276         ACPI_FUNCTION_TRACE ("ps_get_next_namepath");
277
278
279         path = acpi_ps_get_next_namestring (parser_state);
280
281         /* Null path case is allowed */
282
283         if (path) {
284                 /*
285                  * Lookup the name in the internal namespace
286                  */
287                 scope_info.scope.node = NULL;
288                 node = parser_state->start_node;
289                 if (node) {
290                         scope_info.scope.node = node;
291                 }
292
293                 /*
294                  * Lookup object.  We don't want to add anything new to the namespace
295                  * here, however.  So we use MODE_EXECUTE.  Allow searching of the
296                  * parent tree, but don't open a new scope -- we just want to lookup the
297                  * object  (MUST BE mode EXECUTE to perform upsearch)
298                  */
299                 status = acpi_ns_lookup (&scope_info, path, ACPI_TYPE_ANY,
300                                  ACPI_IMODE_EXECUTE,
301                                  ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
302                                  NULL, &node);
303                 if (ACPI_SUCCESS (status) && method_call) {
304                         if (node->type == ACPI_TYPE_METHOD) {
305                                 /* This name is actually a control method invocation */
306
307                                 method_desc = acpi_ns_get_attached_object (node);
308                                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
309                                         "Control Method - %p Desc %p Path=%p\n",
310                                         node, method_desc, path));
311
312                                 name_op = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
313                                 if (!name_op) {
314                                         return_ACPI_STATUS (AE_NO_MEMORY);
315                                 }
316
317                                 /* Change arg into a METHOD CALL and attach name to it */
318
319                                 acpi_ps_init_op (arg, AML_INT_METHODCALL_OP);
320                                 name_op->common.value.name = path;
321
322                                 /* Point METHODCALL/NAME to the METHOD Node */
323
324                                 name_op->common.node = node;
325                                 acpi_ps_append_arg (arg, name_op);
326
327                                 if (!method_desc) {
328                                         ACPI_REPORT_ERROR ((
329                                                 "ps_get_next_namepath: Control Method %p has no attached object\n",
330                                                 node));
331                                         return_ACPI_STATUS (AE_AML_INTERNAL);
332                                 }
333
334                                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
335                                         "Control Method - %p Args %X\n",
336                                         node, method_desc->method.param_count));
337
338                                 /* Get the number of arguments to expect */
339
340                                 walk_state->arg_count = method_desc->method.param_count;
341                                 return_ACPI_STATUS (AE_OK);
342                         }
343
344                         /*
345                          * Else this is normal named object reference.
346                          * Just init the NAMEPATH object with the pathname.
347                          * (See code below)
348                          */
349                 }
350
351                 if (ACPI_FAILURE (status)) {
352                         /*
353                          * 1) Any error other than NOT_FOUND is always severe
354                          * 2) NOT_FOUND is only important if we are executing a method.
355                          * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
356                          */
357                         if ((((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) &&
358                                 (status == AE_NOT_FOUND)                                                &&
359                                 (walk_state->op->common.aml_opcode != AML_COND_REF_OF_OP)) ||
360
361                                 (status != AE_NOT_FOUND)) {
362                                 ACPI_REPORT_NSERROR (path, status);
363
364                                 acpi_os_printf ("search_node %p start_node %p return_node %p\n",
365                                         scope_info.scope.node, parser_state->start_node, node);
366
367
368                         }
369                         else {
370                                 /*
371                                  * We got a NOT_FOUND during table load or we encountered
372                                  * a cond_ref_of(x) where the target does not exist.
373                                  * Either case is ok
374                                  */
375                                 status = AE_OK;
376                         }
377                 }
378         }
379
380         /*
381          * Regardless of success/failure above,
382          * Just initialize the Op with the pathname.
383          */
384         acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
385         arg->common.value.name = path;
386
387         return_ACPI_STATUS (status);
388 }
389
390
391 /*******************************************************************************
392  *
393  * FUNCTION:    acpi_ps_get_next_simple_arg
394  *
395  * PARAMETERS:  parser_state        - Current parser state object
396  *              arg_type            - The argument type (AML_*_ARG)
397  *              Arg                 - Where the argument is returned
398  *
399  * RETURN:      None
400  *
401  * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
402  *
403  ******************************************************************************/
404
405 void
406 acpi_ps_get_next_simple_arg (
407         struct acpi_parse_state         *parser_state,
408         u32                             arg_type,
409         union acpi_parse_object         *arg)
410 {
411
412         ACPI_FUNCTION_TRACE_U32 ("ps_get_next_simple_arg", arg_type);
413
414
415         switch (arg_type) {
416         case ARGP_BYTEDATA:
417
418                 acpi_ps_init_op (arg, AML_BYTE_OP);
419                 arg->common.value.integer = (u32) ACPI_GET8 (parser_state->aml);
420                 parser_state->aml++;
421                 break;
422
423
424         case ARGP_WORDDATA:
425
426                 acpi_ps_init_op (arg, AML_WORD_OP);
427
428                 /* Get 2 bytes from the AML stream */
429
430                 ACPI_MOVE_16_TO_32 (&arg->common.value.integer, parser_state->aml);
431                 parser_state->aml += 2;
432                 break;
433
434
435         case ARGP_DWORDDATA:
436
437                 acpi_ps_init_op (arg, AML_DWORD_OP);
438
439                 /* Get 4 bytes from the AML stream */
440
441                 ACPI_MOVE_32_TO_32 (&arg->common.value.integer, parser_state->aml);
442                 parser_state->aml += 4;
443                 break;
444
445
446         case ARGP_QWORDDATA:
447
448                 acpi_ps_init_op (arg, AML_QWORD_OP);
449
450                 /* Get 8 bytes from the AML stream */
451
452                 ACPI_MOVE_64_TO_64 (&arg->common.value.integer, parser_state->aml);
453                 parser_state->aml += 8;
454                 break;
455
456
457         case ARGP_CHARLIST:
458
459                 acpi_ps_init_op (arg, AML_STRING_OP);
460                 arg->common.value.string = (char *) parser_state->aml;
461
462                 while (ACPI_GET8 (parser_state->aml) != '\0') {
463                         parser_state->aml++;
464                 }
465                 parser_state->aml++;
466                 break;
467
468
469         case ARGP_NAME:
470         case ARGP_NAMESTRING:
471
472                 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
473                 arg->common.value.name = acpi_ps_get_next_namestring (parser_state);
474                 break;
475
476
477         default:
478
479                 ACPI_REPORT_ERROR (("Invalid arg_type %X\n", arg_type));
480                 break;
481         }
482
483         return_VOID;
484 }
485
486
487 /*******************************************************************************
488  *
489  * FUNCTION:    acpi_ps_get_next_field
490  *
491  * PARAMETERS:  parser_state        - Current parser state object
492  *
493  * RETURN:      A newly allocated FIELD op
494  *
495  * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
496  *
497  ******************************************************************************/
498
499 static union acpi_parse_object *
500 acpi_ps_get_next_field (
501         struct acpi_parse_state         *parser_state)
502 {
503         u32                             aml_offset = (u32)
504                           ACPI_PTR_DIFF (parser_state->aml,
505                                            parser_state->aml_start);
506         union acpi_parse_object         *field;
507         u16                             opcode;
508         u32                             name;
509
510
511         ACPI_FUNCTION_TRACE ("ps_get_next_field");
512
513
514         /* Determine field type */
515
516         switch (ACPI_GET8 (parser_state->aml)) {
517         default:
518
519                 opcode = AML_INT_NAMEDFIELD_OP;
520                 break;
521
522         case 0x00:
523
524                 opcode = AML_INT_RESERVEDFIELD_OP;
525                 parser_state->aml++;
526                 break;
527
528         case 0x01:
529
530                 opcode = AML_INT_ACCESSFIELD_OP;
531                 parser_state->aml++;
532                 break;
533         }
534
535         /* Allocate a new field op */
536
537         field = acpi_ps_alloc_op (opcode);
538         if (!field) {
539                 return_PTR (NULL);
540         }
541
542         field->common.aml_offset = aml_offset;
543
544         /* Decode the field type */
545
546         switch (opcode) {
547         case AML_INT_NAMEDFIELD_OP:
548
549                 /* Get the 4-character name */
550
551                 ACPI_MOVE_32_TO_32 (&name, parser_state->aml);
552                 acpi_ps_set_name (field, name);
553                 parser_state->aml += ACPI_NAME_SIZE;
554
555                 /* Get the length which is encoded as a package length */
556
557                 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
558                 break;
559
560
561         case AML_INT_RESERVEDFIELD_OP:
562
563                 /* Get the length which is encoded as a package length */
564
565                 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
566                 break;
567
568
569         case AML_INT_ACCESSFIELD_OP:
570
571                 /*
572                  * Get access_type and access_attrib and merge into the field Op
573                  * access_type is first operand, access_attribute is second
574                  */
575                 field->common.value.integer = (ACPI_GET8 (parser_state->aml) << 8);
576                 parser_state->aml++;
577                 field->common.value.integer |= ACPI_GET8 (parser_state->aml);
578                 parser_state->aml++;
579                 break;
580
581         default:
582
583                 /* Opcode was set in previous switch */
584                 break;
585         }
586
587         return_PTR (field);
588 }
589
590
591 /*******************************************************************************
592  *
593  * FUNCTION:    acpi_ps_get_next_arg
594  *
595  * PARAMETERS:  walk_state          - Current state
596  *              parser_state        - Current parser state object
597  *              arg_type            - The argument type (AML_*_ARG)
598  *              return_arg          - Where the next arg is returned
599  *
600  * RETURN:      Status, and an op object containing the next argument.
601  *
602  * DESCRIPTION: Get next argument (including complex list arguments that require
603  *              pushing the parser stack)
604  *
605  ******************************************************************************/
606
607 acpi_status
608 acpi_ps_get_next_arg (
609         struct acpi_walk_state          *walk_state,
610         struct acpi_parse_state         *parser_state,
611         u32                             arg_type,
612         union acpi_parse_object         **return_arg)
613 {
614         union acpi_parse_object         *arg = NULL;
615         union acpi_parse_object         *prev = NULL;
616         union acpi_parse_object         *field;
617         u32                             subop;
618         acpi_status                     status = AE_OK;
619
620
621         ACPI_FUNCTION_TRACE_PTR ("ps_get_next_arg", parser_state);
622
623
624         switch (arg_type) {
625         case ARGP_BYTEDATA:
626         case ARGP_WORDDATA:
627         case ARGP_DWORDDATA:
628         case ARGP_CHARLIST:
629         case ARGP_NAME:
630         case ARGP_NAMESTRING:
631
632                 /* Constants, strings, and namestrings are all the same size */
633
634                 arg = acpi_ps_alloc_op (AML_BYTE_OP);
635                 if (!arg) {
636                         return_ACPI_STATUS (AE_NO_MEMORY);
637                 }
638                 acpi_ps_get_next_simple_arg (parser_state, arg_type, arg);
639                 break;
640
641
642         case ARGP_PKGLENGTH:
643
644                 /* Package length, nothing returned */
645
646                 parser_state->pkg_end = acpi_ps_get_next_package_end (parser_state);
647                 break;
648
649
650         case ARGP_FIELDLIST:
651
652                 if (parser_state->aml < parser_state->pkg_end) {
653                         /* Non-empty list */
654
655                         while (parser_state->aml < parser_state->pkg_end) {
656                                 field = acpi_ps_get_next_field (parser_state);
657                                 if (!field) {
658                                         return_ACPI_STATUS (AE_NO_MEMORY);
659                                 }
660
661                                 if (prev) {
662                                         prev->common.next = field;
663                                 }
664                                 else {
665                                         arg = field;
666                                 }
667                                 prev = field;
668                         }
669
670                         /* Skip to End of byte data */
671
672                         parser_state->aml = parser_state->pkg_end;
673                 }
674                 break;
675
676
677         case ARGP_BYTELIST:
678
679                 if (parser_state->aml < parser_state->pkg_end) {
680                         /* Non-empty list */
681
682                         arg = acpi_ps_alloc_op (AML_INT_BYTELIST_OP);
683                         if (!arg) {
684                                 return_ACPI_STATUS (AE_NO_MEMORY);
685                         }
686
687                         /* Fill in bytelist data */
688
689                         arg->common.value.size = (u32)
690                                 ACPI_PTR_DIFF (parser_state->pkg_end, parser_state->aml);
691                         arg->named.data = parser_state->aml;
692
693                         /* Skip to End of byte data */
694
695                         parser_state->aml = parser_state->pkg_end;
696                 }
697                 break;
698
699
700         case ARGP_TARGET:
701         case ARGP_SUPERNAME:
702         case ARGP_SIMPLENAME:
703
704                 subop = acpi_ps_peek_opcode (parser_state);
705                 if (subop == 0                  ||
706                         acpi_ps_is_leading_char (subop) ||
707                         acpi_ps_is_prefix_char (subop)) {
708                         /* null_name or name_string */
709
710                         arg = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
711                         if (!arg) {
712                                 return_ACPI_STATUS (AE_NO_MEMORY);
713                         }
714
715                         status = acpi_ps_get_next_namepath (walk_state, parser_state, arg, 0);
716                 }
717                 else {
718                         /* Single complex argument, nothing returned */
719
720                         walk_state->arg_count = 1;
721                 }
722                 break;
723
724
725         case ARGP_DATAOBJ:
726         case ARGP_TERMARG:
727
728                 /* Single complex argument, nothing returned */
729
730                 walk_state->arg_count = 1;
731                 break;
732
733
734         case ARGP_DATAOBJLIST:
735         case ARGP_TERMLIST:
736         case ARGP_OBJLIST:
737
738                 if (parser_state->aml < parser_state->pkg_end) {
739                         /* Non-empty list of variable arguments, nothing returned */
740
741                         walk_state->arg_count = ACPI_VAR_ARGS;
742                 }
743                 break;
744
745
746         default:
747
748                 ACPI_REPORT_ERROR (("Invalid arg_type: %X\n", arg_type));
749                 status = AE_AML_OPERAND_TYPE;
750                 break;
751         }
752
753         *return_arg = arg;
754         return_ACPI_STATUS (status);
755 }