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