Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / acpi / parser / psargs.c
blobb5d98895f6a8601ec024184750b7d390fbd8f9da
1 /******************************************************************************
3 * Module Name: psargs - Parse AML opcode arguments
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_PARSER
51 ACPI_MODULE_NAME ("psargs")
54 /*******************************************************************************
56 * FUNCTION: acpi_ps_get_next_package_length
58 * PARAMETERS: parser_state - Current parser state object
60 * RETURN: Decoded package length. On completion, the AML pointer points
61 * past the length byte or bytes.
63 * DESCRIPTION: Decode and return a package length field
65 ******************************************************************************/
67 u32
68 acpi_ps_get_next_package_length (
69 struct acpi_parse_state *parser_state)
71 u32 encoded_length;
72 u32 length = 0;
75 ACPI_FUNCTION_TRACE ("ps_get_next_package_length");
78 encoded_length = (u32) ACPI_GET8 (parser_state->aml);
79 parser_state->aml++;
82 switch (encoded_length >> 6) /* bits 6-7 contain encoding scheme */ {
83 case 0: /* 1-byte encoding (bits 0-5) */
85 length = (encoded_length & 0x3F);
86 break;
89 case 1: /* 2-byte encoding (next byte + bits 0-3) */
91 length = ((ACPI_GET8 (parser_state->aml) << 04) |
92 (encoded_length & 0x0F));
93 parser_state->aml++;
94 break;
97 case 2: /* 3-byte encoding (next 2 bytes + bits 0-3) */
99 length = ((ACPI_GET8 (parser_state->aml + 1) << 12) |
100 (ACPI_GET8 (parser_state->aml) << 04) |
101 (encoded_length & 0x0F));
102 parser_state->aml += 2;
103 break;
106 case 3: /* 4-byte encoding (next 3 bytes + bits 0-3) */
108 length = ((ACPI_GET8 (parser_state->aml + 2) << 20) |
109 (ACPI_GET8 (parser_state->aml + 1) << 12) |
110 (ACPI_GET8 (parser_state->aml) << 04) |
111 (encoded_length & 0x0F));
112 parser_state->aml += 3;
113 break;
115 default:
117 /* Can't get here, only 2 bits / 4 cases */
118 break;
121 return_VALUE (length);
125 /*******************************************************************************
127 * FUNCTION: acpi_ps_get_next_package_end
129 * PARAMETERS: parser_state - Current parser state object
131 * RETURN: Pointer to end-of-package +1
133 * DESCRIPTION: Get next package length and return a pointer past the end of
134 * the package. Consumes the package length field
136 ******************************************************************************/
138 u8 *
139 acpi_ps_get_next_package_end (
140 struct acpi_parse_state *parser_state)
142 u8 *start = parser_state->aml;
143 acpi_native_uint length;
146 ACPI_FUNCTION_TRACE ("ps_get_next_package_end");
149 /* Function below changes parser_state->Aml */
151 length = (acpi_native_uint) acpi_ps_get_next_package_length (parser_state);
153 return_PTR (start + length); /* end of package */
157 /*******************************************************************************
159 * FUNCTION: acpi_ps_get_next_namestring
161 * PARAMETERS: parser_state - Current parser state object
163 * RETURN: Pointer to the start of the name string (pointer points into
164 * the AML.
166 * DESCRIPTION: Get next raw namestring within the AML stream. Handles all name
167 * prefix characters. Set parser state to point past the string.
168 * (Name is consumed from the AML.)
170 ******************************************************************************/
172 char *
173 acpi_ps_get_next_namestring (
174 struct acpi_parse_state *parser_state)
176 u8 *start = parser_state->aml;
177 u8 *end = parser_state->aml;
180 ACPI_FUNCTION_TRACE ("ps_get_next_namestring");
183 /* Handle multiple prefix characters */
185 while (acpi_ps_is_prefix_char (ACPI_GET8 (end))) {
186 /* Include prefix '\\' or '^' */
188 end++;
191 /* Decode the path */
193 switch (ACPI_GET8 (end)) {
194 case 0:
196 /* null_name */
198 if (end == start) {
199 start = NULL;
201 end++;
202 break;
204 case AML_DUAL_NAME_PREFIX:
206 /* Two name segments */
208 end += 1 + (2 * ACPI_NAME_SIZE);
209 break;
211 case AML_MULTI_NAME_PREFIX_OP:
213 /* Multiple name segments, 4 chars each */
215 end += 2 + ((acpi_size) ACPI_GET8 (end + 1) * ACPI_NAME_SIZE);
216 break;
218 default:
220 /* Single name segment */
222 end += ACPI_NAME_SIZE;
223 break;
226 parser_state->aml = (u8*) end;
227 return_PTR ((char *) start);
231 /*******************************************************************************
233 * FUNCTION: acpi_ps_get_next_namepath
235 * PARAMETERS: parser_state - Current parser state object
236 * Arg - Where the namepath will be stored
237 * arg_count - If the namepath points to a control method
238 * the method's argument is returned here.
239 * method_call - Whether the namepath can possibly be the
240 * start of a method call
242 * RETURN: Status
244 * DESCRIPTION: Get next name (if method call, return # of required args).
245 * Names are looked up in the internal namespace to determine
246 * if the name represents a control method. If a method
247 * is found, the number of arguments to the method is returned.
248 * This information is critical for parsing to continue correctly.
250 ******************************************************************************/
252 acpi_status
253 acpi_ps_get_next_namepath (
254 struct acpi_walk_state *walk_state,
255 struct acpi_parse_state *parser_state,
256 union acpi_parse_object *arg,
257 u8 method_call)
259 char *path;
260 union acpi_parse_object *name_op;
261 acpi_status status = AE_OK;
262 union acpi_operand_object *method_desc;
263 struct acpi_namespace_node *node;
264 union acpi_generic_state scope_info;
267 ACPI_FUNCTION_TRACE ("ps_get_next_namepath");
270 path = acpi_ps_get_next_namestring (parser_state);
272 /* Null path case is allowed */
274 if (path) {
276 * Lookup the name in the internal namespace
278 scope_info.scope.node = NULL;
279 node = parser_state->start_node;
280 if (node) {
281 scope_info.scope.node = node;
285 * Lookup object. We don't want to add anything new to the namespace
286 * here, however. So we use MODE_EXECUTE. Allow searching of the
287 * parent tree, but don't open a new scope -- we just want to lookup the
288 * object (MUST BE mode EXECUTE to perform upsearch)
290 status = acpi_ns_lookup (&scope_info, path, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
291 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
292 if (ACPI_SUCCESS (status) && method_call) {
293 if (node->type == ACPI_TYPE_METHOD) {
295 * This name is actually a control method invocation
297 method_desc = acpi_ns_get_attached_object (node);
298 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
299 "Control Method - %p Desc %p Path=%p\n",
300 node, method_desc, path));
302 name_op = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
303 if (!name_op) {
304 return_ACPI_STATUS (AE_NO_MEMORY);
307 /* Change arg into a METHOD CALL and attach name to it */
309 acpi_ps_init_op (arg, AML_INT_METHODCALL_OP);
310 name_op->common.value.name = path;
312 /* Point METHODCALL/NAME to the METHOD Node */
314 name_op->common.node = node;
315 acpi_ps_append_arg (arg, name_op);
317 if (!method_desc) {
318 ACPI_REPORT_ERROR ((
319 "ps_get_next_namepath: Control Method %p has no attached object\n",
320 node));
321 return_ACPI_STATUS (AE_AML_INTERNAL);
324 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
325 "Control Method - %p Args %X\n",
326 node, method_desc->method.param_count));
328 /* Get the number of arguments to expect */
330 walk_state->arg_count = method_desc->method.param_count;
331 return_ACPI_STATUS (AE_OK);
335 * Else this is normal named object reference.
336 * Just init the NAMEPATH object with the pathname.
337 * (See code below)
341 if (ACPI_FAILURE (status)) {
343 * 1) Any error other than NOT_FOUND is always severe
344 * 2) NOT_FOUND is only important if we are executing a method.
345 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
347 if ((((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) == ACPI_PARSE_EXECUTE) &&
348 (status == AE_NOT_FOUND) &&
349 (walk_state->op->common.aml_opcode != AML_COND_REF_OF_OP)) ||
351 (status != AE_NOT_FOUND)) {
352 ACPI_REPORT_NSERROR (path, status);
354 acpi_os_printf ("search_node %p start_node %p return_node %p\n",
355 scope_info.scope.node, parser_state->start_node, node);
359 else {
361 * We got a NOT_FOUND during table load or we encountered
362 * a cond_ref_of(x) where the target does not exist.
363 * -- either case is ok
365 status = AE_OK;
371 * Regardless of success/failure above,
372 * Just initialize the Op with the pathname.
374 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
375 arg->common.value.name = path;
377 return_ACPI_STATUS (status);
381 /*******************************************************************************
383 * FUNCTION: acpi_ps_get_next_simple_arg
385 * PARAMETERS: parser_state - Current parser state object
386 * arg_type - The argument type (AML_*_ARG)
387 * Arg - Where the argument is returned
389 * RETURN: None
391 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
393 ******************************************************************************/
395 void
396 acpi_ps_get_next_simple_arg (
397 struct acpi_parse_state *parser_state,
398 u32 arg_type,
399 union acpi_parse_object *arg)
402 ACPI_FUNCTION_TRACE_U32 ("ps_get_next_simple_arg", arg_type);
405 switch (arg_type) {
406 case ARGP_BYTEDATA:
408 acpi_ps_init_op (arg, AML_BYTE_OP);
409 arg->common.value.integer = (u32) ACPI_GET8 (parser_state->aml);
410 parser_state->aml++;
411 break;
414 case ARGP_WORDDATA:
416 acpi_ps_init_op (arg, AML_WORD_OP);
418 /* Get 2 bytes from the AML stream */
420 ACPI_MOVE_16_TO_32 (&arg->common.value.integer, parser_state->aml);
421 parser_state->aml += 2;
422 break;
425 case ARGP_DWORDDATA:
427 acpi_ps_init_op (arg, AML_DWORD_OP);
429 /* Get 4 bytes from the AML stream */
431 ACPI_MOVE_32_TO_32 (&arg->common.value.integer, parser_state->aml);
432 parser_state->aml += 4;
433 break;
436 case ARGP_QWORDDATA:
438 acpi_ps_init_op (arg, AML_QWORD_OP);
440 /* Get 8 bytes from the AML stream */
442 ACPI_MOVE_64_TO_64 (&arg->common.value.integer, parser_state->aml);
443 parser_state->aml += 8;
444 break;
447 case ARGP_CHARLIST:
449 acpi_ps_init_op (arg, AML_STRING_OP);
450 arg->common.value.string = (char *) parser_state->aml;
452 while (ACPI_GET8 (parser_state->aml) != '\0') {
453 parser_state->aml++;
455 parser_state->aml++;
456 break;
459 case ARGP_NAME:
460 case ARGP_NAMESTRING:
462 acpi_ps_init_op (arg, AML_INT_NAMEPATH_OP);
463 arg->common.value.name = acpi_ps_get_next_namestring (parser_state);
464 break;
467 default:
469 ACPI_REPORT_ERROR (("Invalid arg_type %X\n", arg_type));
470 break;
473 return_VOID;
477 /*******************************************************************************
479 * FUNCTION: acpi_ps_get_next_field
481 * PARAMETERS: parser_state - Current parser state object
483 * RETURN: A newly allocated FIELD op
485 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
487 ******************************************************************************/
489 union acpi_parse_object *
490 acpi_ps_get_next_field (
491 struct acpi_parse_state *parser_state)
493 u32 aml_offset = (u32) ACPI_PTR_DIFF (parser_state->aml,
494 parser_state->aml_start);
495 union acpi_parse_object *field;
496 u16 opcode;
497 u32 name;
500 ACPI_FUNCTION_TRACE ("ps_get_next_field");
503 /* determine field type */
505 switch (ACPI_GET8 (parser_state->aml)) {
506 default:
508 opcode = AML_INT_NAMEDFIELD_OP;
509 break;
511 case 0x00:
513 opcode = AML_INT_RESERVEDFIELD_OP;
514 parser_state->aml++;
515 break;
517 case 0x01:
519 opcode = AML_INT_ACCESSFIELD_OP;
520 parser_state->aml++;
521 break;
525 /* Allocate a new field op */
527 field = acpi_ps_alloc_op (opcode);
528 if (!field) {
529 return_PTR (NULL);
532 field->common.aml_offset = aml_offset;
534 /* Decode the field type */
536 switch (opcode) {
537 case AML_INT_NAMEDFIELD_OP:
539 /* Get the 4-character name */
541 ACPI_MOVE_32_TO_32 (&name, parser_state->aml);
542 acpi_ps_set_name (field, name);
543 parser_state->aml += ACPI_NAME_SIZE;
545 /* Get the length which is encoded as a package length */
547 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
548 break;
551 case AML_INT_RESERVEDFIELD_OP:
553 /* Get the length which is encoded as a package length */
555 field->common.value.size = acpi_ps_get_next_package_length (parser_state);
556 break;
559 case AML_INT_ACCESSFIELD_OP:
562 * Get access_type and access_attrib and merge into the field Op
563 * access_type is first operand, access_attribute is second
565 field->common.value.integer = (ACPI_GET8 (parser_state->aml) << 8);
566 parser_state->aml++;
567 field->common.value.integer |= ACPI_GET8 (parser_state->aml);
568 parser_state->aml++;
569 break;
571 default:
573 /* Opcode was set in previous switch */
574 break;
577 return_PTR (field);
581 /*******************************************************************************
583 * FUNCTION: acpi_ps_get_next_arg
585 * PARAMETERS: parser_state - Current parser state object
586 * arg_type - The argument type (AML_*_ARG)
587 * arg_count - If the argument points to a control method
588 * the method's argument is returned here.
590 * RETURN: Status, and an op object containing the next argument.
592 * DESCRIPTION: Get next argument (including complex list arguments that require
593 * pushing the parser stack)
595 ******************************************************************************/
597 acpi_status
598 acpi_ps_get_next_arg (
599 struct acpi_walk_state *walk_state,
600 struct acpi_parse_state *parser_state,
601 u32 arg_type,
602 union acpi_parse_object **return_arg)
604 union acpi_parse_object *arg = NULL;
605 union acpi_parse_object *prev = NULL;
606 union acpi_parse_object *field;
607 u32 subop;
608 acpi_status status = AE_OK;
611 ACPI_FUNCTION_TRACE_PTR ("ps_get_next_arg", parser_state);
614 switch (arg_type) {
615 case ARGP_BYTEDATA:
616 case ARGP_WORDDATA:
617 case ARGP_DWORDDATA:
618 case ARGP_CHARLIST:
619 case ARGP_NAME:
620 case ARGP_NAMESTRING:
622 /* constants, strings, and namestrings are all the same size */
624 arg = acpi_ps_alloc_op (AML_BYTE_OP);
625 if (!arg) {
626 return_ACPI_STATUS (AE_NO_MEMORY);
628 acpi_ps_get_next_simple_arg (parser_state, arg_type, arg);
629 break;
632 case ARGP_PKGLENGTH:
634 /* Package length, nothing returned */
636 parser_state->pkg_end = acpi_ps_get_next_package_end (parser_state);
637 break;
640 case ARGP_FIELDLIST:
642 if (parser_state->aml < parser_state->pkg_end) {
643 /* Non-empty list */
645 while (parser_state->aml < parser_state->pkg_end) {
646 field = acpi_ps_get_next_field (parser_state);
647 if (!field) {
648 return_ACPI_STATUS (AE_NO_MEMORY);
651 if (prev) {
652 prev->common.next = field;
654 else {
655 arg = field;
658 prev = field;
661 /* Skip to End of byte data */
663 parser_state->aml = parser_state->pkg_end;
665 break;
668 case ARGP_BYTELIST:
670 if (parser_state->aml < parser_state->pkg_end) {
671 /* Non-empty list */
673 arg = acpi_ps_alloc_op (AML_INT_BYTELIST_OP);
674 if (!arg) {
675 return_ACPI_STATUS (AE_NO_MEMORY);
678 /* Fill in bytelist data */
680 arg->common.value.size = (u32) ACPI_PTR_DIFF (parser_state->pkg_end,
681 parser_state->aml);
682 arg->named.data = parser_state->aml;
684 /* Skip to End of byte data */
686 parser_state->aml = parser_state->pkg_end;
688 break;
691 case ARGP_TARGET:
692 case ARGP_SUPERNAME:
693 case ARGP_SIMPLENAME:
695 subop = acpi_ps_peek_opcode (parser_state);
696 if (subop == 0 ||
697 acpi_ps_is_leading_char (subop) ||
698 acpi_ps_is_prefix_char (subop)) {
699 /* null_name or name_string */
701 arg = acpi_ps_alloc_op (AML_INT_NAMEPATH_OP);
702 if (!arg) {
703 return_ACPI_STATUS (AE_NO_MEMORY);
706 status = acpi_ps_get_next_namepath (walk_state, parser_state, arg, 0);
708 else {
709 /* single complex argument, nothing returned */
711 walk_state->arg_count = 1;
713 break;
716 case ARGP_DATAOBJ:
717 case ARGP_TERMARG:
719 /* single complex argument, nothing returned */
721 walk_state->arg_count = 1;
722 break;
725 case ARGP_DATAOBJLIST:
726 case ARGP_TERMLIST:
727 case ARGP_OBJLIST:
729 if (parser_state->aml < parser_state->pkg_end) {
730 /* non-empty list of variable arguments, nothing returned */
732 walk_state->arg_count = ACPI_VAR_ARGS;
734 break;
737 default:
739 ACPI_REPORT_ERROR (("Invalid arg_type: %X\n", arg_type));
740 status = AE_AML_OPERAND_TYPE;
741 break;
744 *return_arg = arg;
745 return_ACPI_STATUS (status);