[ACPI] ACPICA 20051117
[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 #include <acpi/acpi.h>
45 #include <acpi/acparser.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acnamesp.h>
48
49 #define _COMPONENT          ACPI_PARSER
50 ACPI_MODULE_NAME("psargs")
51
52 /* Local prototypes */
53 static u32
54 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
55
56 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
57                                                        *parser_state);
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    acpi_ps_get_next_package_length
62  *
63  * PARAMETERS:  parser_state        - Current parser state object
64  *
65  * RETURN:      Decoded package length. On completion, the AML pointer points
66  *              past the length byte or bytes.
67  *
68  * DESCRIPTION: Decode and return a package length field.
69  *              Note: Largest package length is 28 bits, from ACPI specification
70  *
71  ******************************************************************************/
72
73 static u32
74 acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
75 {
76         u8 *aml = parser_state->aml;
77         u32 package_length = 0;
78         acpi_native_uint byte_count;
79         u8 byte_zero_mask = 0x3F;       /* Default [0:5] */
80
81         ACPI_FUNCTION_TRACE("ps_get_next_package_length");
82
83         /*
84          * Byte 0 bits [6:7] contain the number of additional bytes
85          * used to encode the package length, either 0,1,2, or 3
86          */
87         byte_count = (aml[0] >> 6);
88         parser_state->aml += (byte_count + 1);
89
90         /* Get bytes 3, 2, 1 as needed */
91
92         while (byte_count) {
93                 /*
94                  * Final bit positions for the package length bytes:
95                  *      Byte3->[20:27]
96                  *      Byte2->[12:19]
97                  *      Byte1->[04:11]
98                  *      Byte0->[00:03]
99                  */
100                 package_length |= (aml[byte_count] << ((byte_count << 3) - 4));
101
102                 byte_zero_mask = 0x0F;  /* Use bits [0:3] of byte 0 */
103                 byte_count--;
104         }
105
106         /* Byte 0 is a special case, either bits [0:3] or [0:5] are used */
107
108         package_length |= (aml[0] & byte_zero_mask);
109         return_UINT32(package_length);
110 }
111
112 /*******************************************************************************
113  *
114  * FUNCTION:    acpi_ps_get_next_package_end
115  *
116  * PARAMETERS:  parser_state        - Current parser state object
117  *
118  * RETURN:      Pointer to end-of-package +1
119  *
120  * DESCRIPTION: Get next package length and return a pointer past the end of
121  *              the package.  Consumes the package length field
122  *
123  ******************************************************************************/
124
125 u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
126 {
127         u8 *start = parser_state->aml;
128         u32 package_length;
129
130         ACPI_FUNCTION_TRACE("ps_get_next_package_end");
131
132         /* Function below updates parser_state->Aml */
133
134         package_length = acpi_ps_get_next_package_length(parser_state);
135
136         return_PTR(start + package_length);     /* end of package */
137 }
138
139 /*******************************************************************************
140  *
141  * FUNCTION:    acpi_ps_get_next_namestring
142  *
143  * PARAMETERS:  parser_state        - Current parser state object
144  *
145  * RETURN:      Pointer to the start of the name string (pointer points into
146  *              the AML.
147  *
148  * DESCRIPTION: Get next raw namestring within the AML stream.  Handles all name
149  *              prefix characters.  Set parser state to point past the string.
150  *              (Name is consumed from the AML.)
151  *
152  ******************************************************************************/
153
154 char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
155 {
156         u8 *start = parser_state->aml;
157         u8 *end = parser_state->aml;
158
159         ACPI_FUNCTION_TRACE("ps_get_next_namestring");
160
161         /* Point past any namestring prefix characters (backslash or carat) */
162
163         while (acpi_ps_is_prefix_char(*end)) {
164                 end++;
165         }
166
167         /* Decode the path prefix character */
168
169         switch (*end) {
170         case 0:
171
172                 /* null_name */
173
174                 if (end == start) {
175                         start = NULL;
176                 }
177                 end++;
178                 break;
179
180         case AML_DUAL_NAME_PREFIX:
181
182                 /* Two name segments */
183
184                 end += 1 + (2 * ACPI_NAME_SIZE);
185                 break;
186
187         case AML_MULTI_NAME_PREFIX_OP:
188
189                 /* Multiple name segments, 4 chars each, count in next byte */
190
191                 end += 2 + (*(end + 1) * ACPI_NAME_SIZE);
192                 break;
193
194         default:
195
196                 /* Single name segment */
197
198                 end += ACPI_NAME_SIZE;
199                 break;
200         }
201
202         parser_state->aml = end;
203         return_PTR((char *)start);
204 }
205
206 /*******************************************************************************
207  *
208  * FUNCTION:    acpi_ps_get_next_namepath
209  *
210  * PARAMETERS:  parser_state        - Current parser state object
211  *              Arg                 - Where the namepath will be stored
212  *              arg_count           - If the namepath points to a control method
213  *                                    the method's argument is returned here.
214  *              method_call         - Whether the namepath can possibly be the
215  *                                    start of a method call
216  *
217  * RETURN:      Status
218  *
219  * DESCRIPTION: Get next name (if method call, return # of required args).
220  *              Names are looked up in the internal namespace to determine
221  *              if the name represents a control method.  If a method
222  *              is found, the number of arguments to the method is returned.
223  *              This information is critical for parsing to continue correctly.
224  *
225  ******************************************************************************/
226
227 acpi_status
228 acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
229                           struct acpi_parse_state *parser_state,
230                           union acpi_parse_object *arg, u8 method_call)
231 {
232         char *path;
233         union acpi_parse_object *name_op;
234         acpi_status status = AE_OK;
235         union acpi_operand_object *method_desc;
236         struct acpi_namespace_node *node;
237         union acpi_generic_state scope_info;
238
239         ACPI_FUNCTION_TRACE("ps_get_next_namepath");
240
241         path = acpi_ps_get_next_namestring(parser_state);
242
243         /* Null path case is allowed */
244
245         if (path) {
246                 /*
247                  * Lookup the name in the internal namespace
248                  */
249                 scope_info.scope.node = NULL;
250                 node = parser_state->start_node;
251                 if (node) {
252                         scope_info.scope.node = node;
253                 }
254
255                 /*
256                  * Lookup object.  We don't want to add anything new to the namespace
257                  * here, however.  So we use MODE_EXECUTE.  Allow searching of the
258                  * parent tree, but don't open a new scope -- we just want to lookup the
259                  * object  (MUST BE mode EXECUTE to perform upsearch)
260                  */
261                 status = acpi_ns_lookup(&scope_info, path, ACPI_TYPE_ANY,
262                                         ACPI_IMODE_EXECUTE,
263                                         ACPI_NS_SEARCH_PARENT |
264                                         ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
265                 if (ACPI_SUCCESS(status) && method_call) {
266                         if (node->type == ACPI_TYPE_METHOD) {
267                                 /* This name is actually a control method invocation */
268
269                                 method_desc = acpi_ns_get_attached_object(node);
270                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
271                                                   "Control Method - %p Desc %p Path=%p\n",
272                                                   node, method_desc, path));
273
274                                 name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
275                                 if (!name_op) {
276                                         return_ACPI_STATUS(AE_NO_MEMORY);
277                                 }
278
279                                 /* Change arg into a METHOD CALL and attach name to it */
280
281                                 acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
282                                 name_op->common.value.name = path;
283
284                                 /* Point METHODCALL/NAME to the METHOD Node */
285
286                                 name_op->common.node = node;
287                                 acpi_ps_append_arg(arg, name_op);
288
289                                 if (!method_desc) {
290                                         ACPI_REPORT_ERROR(("ps_get_next_namepath: Control Method %p has no attached object\n", node));
291                                         return_ACPI_STATUS(AE_AML_INTERNAL);
292                                 }
293
294                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
295                                                   "Control Method - %p Args %X\n",
296                                                   node,
297                                                   method_desc->method.
298                                                   param_count));
299
300                                 /* Get the number of arguments to expect */
301
302                                 walk_state->arg_count =
303                                     method_desc->method.param_count;
304                                 return_ACPI_STATUS(AE_OK);
305                         }
306
307                         /*
308                          * Else this is normal named object reference.
309                          * Just init the NAMEPATH object with the pathname.
310                          * (See code below)
311                          */
312                 }
313
314                 if (ACPI_FAILURE(status)) {
315                         /*
316                          * 1) Any error other than NOT_FOUND is always severe
317                          * 2) NOT_FOUND is only important if we are executing a method.
318                          * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
319                          */
320                         if ((((walk_state->
321                                parse_flags & ACPI_PARSE_MODE_MASK) ==
322                               ACPI_PARSE_EXECUTE) && (status == AE_NOT_FOUND)
323                              && (walk_state->op->common.aml_opcode !=
324                                  AML_COND_REF_OF_OP))
325                             || (status != AE_NOT_FOUND)) {
326                                 ACPI_REPORT_NSERROR(path, status);
327
328                                 acpi_os_printf
329                                     ("search_node %p start_node %p return_node %p\n",
330                                      scope_info.scope.node,
331                                      parser_state->start_node, node);
332                         } else {
333                                 /*
334                                  * We got a NOT_FOUND during table load or we encountered
335                                  * a cond_ref_of(x) where the target does not exist.
336                                  * Either case is ok
337                                  */
338                                 status = AE_OK;
339                         }
340                 }
341         }
342
343         /*
344          * Regardless of success/failure above,
345          * Just initialize the Op with the pathname.
346          */
347         acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
348         arg->common.value.name = path;
349
350         return_ACPI_STATUS(status);
351 }
352
353 /*******************************************************************************
354  *
355  * FUNCTION:    acpi_ps_get_next_simple_arg
356  *
357  * PARAMETERS:  parser_state        - Current parser state object
358  *              arg_type            - The argument type (AML_*_ARG)
359  *              Arg                 - Where the argument is returned
360  *
361  * RETURN:      None
362  *
363  * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
364  *
365  ******************************************************************************/
366
367 void
368 acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
369                             u32 arg_type, union acpi_parse_object *arg)
370 {
371         u32 length;
372         u16 opcode;
373         u8 *aml = parser_state->aml;
374
375         ACPI_FUNCTION_TRACE_U32("ps_get_next_simple_arg", arg_type);
376
377         switch (arg_type) {
378         case ARGP_BYTEDATA:
379
380                 /* Get 1 byte from the AML stream */
381
382                 opcode = AML_BYTE_OP;
383                 arg->common.value.integer = (acpi_integer) * aml;
384                 length = 1;
385                 break;
386
387         case ARGP_WORDDATA:
388
389                 /* Get 2 bytes from the AML stream */
390
391                 opcode = AML_WORD_OP;
392                 ACPI_MOVE_16_TO_64(&arg->common.value.integer, aml);
393                 length = 2;
394                 break;
395
396         case ARGP_DWORDDATA:
397
398                 /* Get 4 bytes from the AML stream */
399
400                 opcode = AML_DWORD_OP;
401                 ACPI_MOVE_32_TO_64(&arg->common.value.integer, aml);
402                 length = 4;
403                 break;
404
405         case ARGP_QWORDDATA:
406
407                 /* Get 8 bytes from the AML stream */
408
409                 opcode = AML_QWORD_OP;
410                 ACPI_MOVE_64_TO_64(&arg->common.value.integer, aml);
411                 length = 8;
412                 break;
413
414         case ARGP_CHARLIST:
415
416                 /* Get a pointer to the string, point past the string */
417
418                 opcode = AML_STRING_OP;
419                 arg->common.value.string = ACPI_CAST_PTR(char, aml);
420
421                 /* Find the null terminator */
422
423                 length = 0;
424                 while (aml[length]) {
425                         length++;
426                 }
427                 length++;
428                 break;
429
430         case ARGP_NAME:
431         case ARGP_NAMESTRING:
432
433                 acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
434                 arg->common.value.name =
435                     acpi_ps_get_next_namestring(parser_state);
436                 return_VOID;
437
438         default:
439
440                 ACPI_REPORT_ERROR(("Invalid arg_type %X\n", arg_type));
441                 return_VOID;
442         }
443
444         acpi_ps_init_op(arg, opcode);
445         parser_state->aml += length;
446         return_VOID;
447 }
448
449 /*******************************************************************************
450  *
451  * FUNCTION:    acpi_ps_get_next_field
452  *
453  * PARAMETERS:  parser_state        - Current parser state object
454  *
455  * RETURN:      A newly allocated FIELD op
456  *
457  * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
458  *
459  ******************************************************************************/
460
461 static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
462                                                        *parser_state)
463 {
464         u32 aml_offset = (u32)
465             ACPI_PTR_DIFF(parser_state->aml,
466                           parser_state->aml_start);
467         union acpi_parse_object *field;
468         u16 opcode;
469         u32 name;
470
471         ACPI_FUNCTION_TRACE("ps_get_next_field");
472
473         /* Determine field type */
474
475         switch (ACPI_GET8(parser_state->aml)) {
476         default:
477
478                 opcode = AML_INT_NAMEDFIELD_OP;
479                 break;
480
481         case 0x00:
482
483                 opcode = AML_INT_RESERVEDFIELD_OP;
484                 parser_state->aml++;
485                 break;
486
487         case 0x01:
488
489                 opcode = AML_INT_ACCESSFIELD_OP;
490                 parser_state->aml++;
491                 break;
492         }
493
494         /* Allocate a new field op */
495
496         field = acpi_ps_alloc_op(opcode);
497         if (!field) {
498                 return_PTR(NULL);
499         }
500
501         field->common.aml_offset = aml_offset;
502
503         /* Decode the field type */
504
505         switch (opcode) {
506         case AML_INT_NAMEDFIELD_OP:
507
508                 /* Get the 4-character name */
509
510                 ACPI_MOVE_32_TO_32(&name, parser_state->aml);
511                 acpi_ps_set_name(field, name);
512                 parser_state->aml += ACPI_NAME_SIZE;
513
514                 /* Get the length which is encoded as a package length */
515
516                 field->common.value.size =
517                     acpi_ps_get_next_package_length(parser_state);
518                 break;
519
520         case AML_INT_RESERVEDFIELD_OP:
521
522                 /* Get the length which is encoded as a package length */
523
524                 field->common.value.size =
525                     acpi_ps_get_next_package_length(parser_state);
526                 break;
527
528         case AML_INT_ACCESSFIELD_OP:
529
530                 /*
531                  * Get access_type and access_attrib and merge into the field Op
532                  * access_type is first operand, access_attribute is second
533                  */
534                 field->common.value.integer =
535                     (((u32) ACPI_GET8(parser_state->aml) << 8));
536                 parser_state->aml++;
537                 field->common.value.integer |= ACPI_GET8(parser_state->aml);
538                 parser_state->aml++;
539                 break;
540
541         default:
542
543                 /* Opcode was set in previous switch */
544                 break;
545         }
546
547         return_PTR(field);
548 }
549
550 /*******************************************************************************
551  *
552  * FUNCTION:    acpi_ps_get_next_arg
553  *
554  * PARAMETERS:  walk_state          - Current state
555  *              parser_state        - Current parser state object
556  *              arg_type            - The argument type (AML_*_ARG)
557  *              return_arg          - Where the next arg is returned
558  *
559  * RETURN:      Status, and an op object containing the next argument.
560  *
561  * DESCRIPTION: Get next argument (including complex list arguments that require
562  *              pushing the parser stack)
563  *
564  ******************************************************************************/
565
566 acpi_status
567 acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
568                      struct acpi_parse_state *parser_state,
569                      u32 arg_type, union acpi_parse_object **return_arg)
570 {
571         union acpi_parse_object *arg = NULL;
572         union acpi_parse_object *prev = NULL;
573         union acpi_parse_object *field;
574         u32 subop;
575         acpi_status status = AE_OK;
576
577         ACPI_FUNCTION_TRACE_PTR("ps_get_next_arg", parser_state);
578
579         switch (arg_type) {
580         case ARGP_BYTEDATA:
581         case ARGP_WORDDATA:
582         case ARGP_DWORDDATA:
583         case ARGP_CHARLIST:
584         case ARGP_NAME:
585         case ARGP_NAMESTRING:
586
587                 /* Constants, strings, and namestrings are all the same size */
588
589                 arg = acpi_ps_alloc_op(AML_BYTE_OP);
590                 if (!arg) {
591                         return_ACPI_STATUS(AE_NO_MEMORY);
592                 }
593                 acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
594                 break;
595
596         case ARGP_PKGLENGTH:
597
598                 /* Package length, nothing returned */
599
600                 parser_state->pkg_end =
601                     acpi_ps_get_next_package_end(parser_state);
602                 break;
603
604         case ARGP_FIELDLIST:
605
606                 if (parser_state->aml < parser_state->pkg_end) {
607                         /* Non-empty list */
608
609                         while (parser_state->aml < parser_state->pkg_end) {
610                                 field = acpi_ps_get_next_field(parser_state);
611                                 if (!field) {
612                                         return_ACPI_STATUS(AE_NO_MEMORY);
613                                 }
614
615                                 if (prev) {
616                                         prev->common.next = field;
617                                 } else {
618                                         arg = field;
619                                 }
620                                 prev = field;
621                         }
622
623                         /* Skip to End of byte data */
624
625                         parser_state->aml = parser_state->pkg_end;
626                 }
627                 break;
628
629         case ARGP_BYTELIST:
630
631                 if (parser_state->aml < parser_state->pkg_end) {
632                         /* Non-empty list */
633
634                         arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP);
635                         if (!arg) {
636                                 return_ACPI_STATUS(AE_NO_MEMORY);
637                         }
638
639                         /* Fill in bytelist data */
640
641                         arg->common.value.size = (u32)
642                             ACPI_PTR_DIFF(parser_state->pkg_end,
643                                           parser_state->aml);
644                         arg->named.data = parser_state->aml;
645
646                         /* Skip to End of byte data */
647
648                         parser_state->aml = parser_state->pkg_end;
649                 }
650                 break;
651
652         case ARGP_TARGET:
653         case ARGP_SUPERNAME:
654         case ARGP_SIMPLENAME:
655
656                 subop = acpi_ps_peek_opcode(parser_state);
657                 if (subop == 0 ||
658                     acpi_ps_is_leading_char(subop) ||
659                     acpi_ps_is_prefix_char(subop)) {
660                         /* null_name or name_string */
661
662                         arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
663                         if (!arg) {
664                                 return_ACPI_STATUS(AE_NO_MEMORY);
665                         }
666
667                         status =
668                             acpi_ps_get_next_namepath(walk_state, parser_state,
669                                                       arg, 0);
670                 } else {
671                         /* Single complex argument, nothing returned */
672
673                         walk_state->arg_count = 1;
674                 }
675                 break;
676
677         case ARGP_DATAOBJ:
678         case ARGP_TERMARG:
679
680                 /* Single complex argument, nothing returned */
681
682                 walk_state->arg_count = 1;
683                 break;
684
685         case ARGP_DATAOBJLIST:
686         case ARGP_TERMLIST:
687         case ARGP_OBJLIST:
688
689                 if (parser_state->aml < parser_state->pkg_end) {
690                         /* Non-empty list of variable arguments, nothing returned */
691
692                         walk_state->arg_count = ACPI_VAR_ARGS;
693                 }
694                 break;
695
696         default:
697
698                 ACPI_REPORT_ERROR(("Invalid arg_type: %X\n", arg_type));
699                 status = AE_AML_OPERAND_TYPE;
700                 break;
701         }
702
703         *return_arg = arg;
704         return_ACPI_STATUS(status);
705 }