psargs.c 18.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/******************************************************************************
 *
 * Module Name: psargs - Parse AML opcode arguments
 *
 *****************************************************************************/

/*
 * Copyright (C) 2000 - 2005, R. Byron Moore
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/acparser.h>
#include <acpi/amlcode.h>
#include <acpi/acnamesp.h>

#define _COMPONENT          ACPI_PARSER
L
Len Brown 已提交
50
ACPI_MODULE_NAME("psargs")
L
Linus Torvalds 已提交
51

R
Robert Moore 已提交
52 53
/* Local prototypes */
static u32
L
Len Brown 已提交
54
acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state);
R
Robert Moore 已提交
55

L
Len Brown 已提交
56 57
static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
						       *parser_state);
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_package_length
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *
 * RETURN:      Decoded package length.  On completion, the AML pointer points
 *              past the length byte or bytes.
 *
 * DESCRIPTION: Decode and return a package length field
 *
 ******************************************************************************/

R
Robert Moore 已提交
72
static u32
L
Len Brown 已提交
73
acpi_ps_get_next_package_length(struct acpi_parse_state *parser_state)
L
Linus Torvalds 已提交
74
{
L
Len Brown 已提交
75 76
	u32 encoded_length;
	u32 length = 0;
L
Linus Torvalds 已提交
77

L
Len Brown 已提交
78
	ACPI_FUNCTION_TRACE("ps_get_next_package_length");
L
Linus Torvalds 已提交
79

L
Len Brown 已提交
80
	encoded_length = (u32) ACPI_GET8(parser_state->aml);
L
Linus Torvalds 已提交
81 82
	parser_state->aml++;

L
Len Brown 已提交
83 84
	switch (encoded_length >> 6) {	/* bits 6-7 contain encoding scheme */
	case 0:		/* 1-byte encoding (bits 0-5) */
L
Linus Torvalds 已提交
85 86 87 88

		length = (encoded_length & 0x3F);
		break;

L
Len Brown 已提交
89
	case 1:		/* 2-byte encoding (next byte + bits 0-3) */
L
Linus Torvalds 已提交
90

L
Len Brown 已提交
91 92
		length = ((ACPI_GET8(parser_state->aml) << 04) |
			  (encoded_length & 0x0F));
L
Linus Torvalds 已提交
93 94 95
		parser_state->aml++;
		break;

L
Len Brown 已提交
96
	case 2:		/* 3-byte encoding (next 2 bytes + bits 0-3) */
L
Linus Torvalds 已提交
97

L
Len Brown 已提交
98 99 100
		length = ((ACPI_GET8(parser_state->aml + 1) << 12) |
			  (ACPI_GET8(parser_state->aml) << 04) |
			  (encoded_length & 0x0F));
L
Linus Torvalds 已提交
101 102 103
		parser_state->aml += 2;
		break;

L
Len Brown 已提交
104
	case 3:		/* 4-byte encoding (next 3 bytes + bits 0-3) */
L
Linus Torvalds 已提交
105

L
Len Brown 已提交
106 107 108 109
		length = ((ACPI_GET8(parser_state->aml + 2) << 20) |
			  (ACPI_GET8(parser_state->aml + 1) << 12) |
			  (ACPI_GET8(parser_state->aml) << 04) |
			  (encoded_length & 0x0F));
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118
		parser_state->aml += 3;
		break;

	default:

		/* Can't get here, only 2 bits / 4 cases */
		break;
	}

B
Bob Moore 已提交
119
	return_UINT32(length);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_package_end
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *
 * RETURN:      Pointer to end-of-package +1
 *
 * DESCRIPTION: Get next package length and return a pointer past the end of
 *              the package.  Consumes the package length field
 *
 ******************************************************************************/

L
Len Brown 已提交
135
u8 *acpi_ps_get_next_package_end(struct acpi_parse_state *parser_state)
L
Linus Torvalds 已提交
136
{
L
Len Brown 已提交
137 138
	u8 *start = parser_state->aml;
	acpi_native_uint length;
L
Linus Torvalds 已提交
139

L
Len Brown 已提交
140
	ACPI_FUNCTION_TRACE("ps_get_next_package_end");
L
Linus Torvalds 已提交
141 142 143

	/* Function below changes parser_state->Aml */

L
Len Brown 已提交
144 145
	length =
	    (acpi_native_uint) acpi_ps_get_next_package_length(parser_state);
L
Linus Torvalds 已提交
146

L
Len Brown 已提交
147
	return_PTR(start + length);	/* end of package */
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_namestring
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *
 * RETURN:      Pointer to the start of the name string (pointer points into
 *              the AML.
 *
 * DESCRIPTION: Get next raw namestring within the AML stream.  Handles all name
 *              prefix characters.  Set parser state to point past the string.
 *              (Name is consumed from the AML.)
 *
 ******************************************************************************/

L
Len Brown 已提交
165
char *acpi_ps_get_next_namestring(struct acpi_parse_state *parser_state)
L
Linus Torvalds 已提交
166
{
L
Len Brown 已提交
167 168
	u8 *start = parser_state->aml;
	u8 *end = parser_state->aml;
L
Linus Torvalds 已提交
169

L
Len Brown 已提交
170
	ACPI_FUNCTION_TRACE("ps_get_next_namestring");
L
Linus Torvalds 已提交
171 172 173

	/* Handle multiple prefix characters */

L
Len Brown 已提交
174
	while (acpi_ps_is_prefix_char(ACPI_GET8(end))) {
L
Linus Torvalds 已提交
175 176 177 178 179 180 181
		/* Include prefix '\\' or '^' */

		end++;
	}

	/* Decode the path */

L
Len Brown 已提交
182
	switch (ACPI_GET8(end)) {
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	case 0:

		/* null_name */

		if (end == start) {
			start = NULL;
		}
		end++;
		break;

	case AML_DUAL_NAME_PREFIX:

		/* Two name segments */

		end += 1 + (2 * ACPI_NAME_SIZE);
		break;

	case AML_MULTI_NAME_PREFIX_OP:

		/* Multiple name segments, 4 chars each */

L
Len Brown 已提交
204
		end += 2 + ((acpi_size) ACPI_GET8(end + 1) * ACPI_NAME_SIZE);
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214
		break;

	default:

		/* Single name segment */

		end += ACPI_NAME_SIZE;
		break;
	}

L
Len Brown 已提交
215 216
	parser_state->aml = (u8 *) end;
	return_PTR((char *)start);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_namepath
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *              Arg                 - Where the namepath will be stored
 *              arg_count           - If the namepath points to a control method
 *                                    the method's argument is returned here.
 *              method_call         - Whether the namepath can possibly be the
 *                                    start of a method call
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Get next name (if method call, return # of required args).
 *              Names are looked up in the internal namespace to determine
 *              if the name represents a control method.  If a method
 *              is found, the number of arguments to the method is returned.
 *              This information is critical for parsing to continue correctly.
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
241 242 243
acpi_ps_get_next_namepath(struct acpi_walk_state *walk_state,
			  struct acpi_parse_state *parser_state,
			  union acpi_parse_object *arg, u8 method_call)
L
Linus Torvalds 已提交
244
{
L
Len Brown 已提交
245 246 247 248 249 250
	char *path;
	union acpi_parse_object *name_op;
	acpi_status status = AE_OK;
	union acpi_operand_object *method_desc;
	struct acpi_namespace_node *node;
	union acpi_generic_state scope_info;
L
Linus Torvalds 已提交
251

L
Len Brown 已提交
252
	ACPI_FUNCTION_TRACE("ps_get_next_namepath");
L
Linus Torvalds 已提交
253

L
Len Brown 已提交
254
	path = acpi_ps_get_next_namestring(parser_state);
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

	/* Null path case is allowed */

	if (path) {
		/*
		 * Lookup the name in the internal namespace
		 */
		scope_info.scope.node = NULL;
		node = parser_state->start_node;
		if (node) {
			scope_info.scope.node = node;
		}

		/*
		 * Lookup object.  We don't want to add anything new to the namespace
		 * here, however.  So we use MODE_EXECUTE.  Allow searching of the
		 * parent tree, but don't open a new scope -- we just want to lookup the
		 * object  (MUST BE mode EXECUTE to perform upsearch)
		 */
L
Len Brown 已提交
274 275 276 277 278
		status = acpi_ns_lookup(&scope_info, path, ACPI_TYPE_ANY,
					ACPI_IMODE_EXECUTE,
					ACPI_NS_SEARCH_PARENT |
					ACPI_NS_DONT_OPEN_SCOPE, NULL, &node);
		if (ACPI_SUCCESS(status) && method_call) {
L
Linus Torvalds 已提交
279
			if (node->type == ACPI_TYPE_METHOD) {
R
Robert Moore 已提交
280 281
				/* This name is actually a control method invocation */

L
Len Brown 已提交
282 283 284 285
				method_desc = acpi_ns_get_attached_object(node);
				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
						  "Control Method - %p Desc %p Path=%p\n",
						  node, method_desc, path));
L
Linus Torvalds 已提交
286

L
Len Brown 已提交
287
				name_op = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
L
Linus Torvalds 已提交
288
				if (!name_op) {
L
Len Brown 已提交
289
					return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
290 291 292 293
				}

				/* Change arg into a METHOD CALL and attach name to it */

L
Len Brown 已提交
294
				acpi_ps_init_op(arg, AML_INT_METHODCALL_OP);
L
Linus Torvalds 已提交
295 296 297 298 299
				name_op->common.value.name = path;

				/* Point METHODCALL/NAME to the METHOD Node */

				name_op->common.node = node;
L
Len Brown 已提交
300
				acpi_ps_append_arg(arg, name_op);
L
Linus Torvalds 已提交
301 302

				if (!method_desc) {
L
Len Brown 已提交
303 304
					ACPI_REPORT_ERROR(("ps_get_next_namepath: Control Method %p has no attached object\n", node));
					return_ACPI_STATUS(AE_AML_INTERNAL);
L
Linus Torvalds 已提交
305 306
				}

L
Len Brown 已提交
307 308 309 310 311
				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
						  "Control Method - %p Args %X\n",
						  node,
						  method_desc->method.
						  param_count));
L
Linus Torvalds 已提交
312 313 314

				/* Get the number of arguments to expect */

L
Len Brown 已提交
315 316 317
				walk_state->arg_count =
				    method_desc->method.param_count;
				return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
318 319 320 321 322 323 324 325 326
			}

			/*
			 * Else this is normal named object reference.
			 * Just init the NAMEPATH object with the pathname.
			 * (See code below)
			 */
		}

L
Len Brown 已提交
327
		if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
328 329 330 331 332
			/*
			 * 1) Any error other than NOT_FOUND is always severe
			 * 2) NOT_FOUND is only important if we are executing a method.
			 * 3) If executing a cond_ref_of opcode, NOT_FOUND is ok.
			 */
L
Len Brown 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346
			if ((((walk_state->
			       parse_flags & ACPI_PARSE_MODE_MASK) ==
			      ACPI_PARSE_EXECUTE) && (status == AE_NOT_FOUND)
			     && (walk_state->op->common.aml_opcode !=
				 AML_COND_REF_OF_OP))
			    || (status != AE_NOT_FOUND)) {
				ACPI_REPORT_NSERROR(path, status);

				acpi_os_printf
				    ("search_node %p start_node %p return_node %p\n",
				     scope_info.scope.node,
				     parser_state->start_node, node);

			} else {
L
Linus Torvalds 已提交
347 348 349
				/*
				 * We got a NOT_FOUND during table load or we encountered
				 * a cond_ref_of(x) where the target does not exist.
R
Robert Moore 已提交
350
				 * Either case is ok
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360
				 */
				status = AE_OK;
			}
		}
	}

	/*
	 * Regardless of success/failure above,
	 * Just initialize the Op with the pathname.
	 */
L
Len Brown 已提交
361
	acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
L
Linus Torvalds 已提交
362 363
	arg->common.value.name = path;

L
Len Brown 已提交
364
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_simple_arg
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *              arg_type            - The argument type (AML_*_ARG)
 *              Arg                 - Where the argument is returned
 *
 * RETURN:      None
 *
 * DESCRIPTION: Get the next simple argument (constant, string, or namestring)
 *
 ******************************************************************************/

void
L
Len Brown 已提交
382 383
acpi_ps_get_next_simple_arg(struct acpi_parse_state *parser_state,
			    u32 arg_type, union acpi_parse_object *arg)
L
Linus Torvalds 已提交
384 385
{

L
Len Brown 已提交
386
	ACPI_FUNCTION_TRACE_U32("ps_get_next_simple_arg", arg_type);
L
Linus Torvalds 已提交
387 388 389 390

	switch (arg_type) {
	case ARGP_BYTEDATA:

L
Len Brown 已提交
391 392
		acpi_ps_init_op(arg, AML_BYTE_OP);
		arg->common.value.integer = (u32) ACPI_GET8(parser_state->aml);
L
Linus Torvalds 已提交
393 394 395 396 397
		parser_state->aml++;
		break;

	case ARGP_WORDDATA:

L
Len Brown 已提交
398
		acpi_ps_init_op(arg, AML_WORD_OP);
L
Linus Torvalds 已提交
399 400 401

		/* Get 2 bytes from the AML stream */

L
Len Brown 已提交
402 403
		ACPI_MOVE_16_TO_32(&arg->common.value.integer,
				   parser_state->aml);
L
Linus Torvalds 已提交
404 405 406 407 408
		parser_state->aml += 2;
		break;

	case ARGP_DWORDDATA:

L
Len Brown 已提交
409
		acpi_ps_init_op(arg, AML_DWORD_OP);
L
Linus Torvalds 已提交
410 411 412

		/* Get 4 bytes from the AML stream */

L
Len Brown 已提交
413 414
		ACPI_MOVE_32_TO_32(&arg->common.value.integer,
				   parser_state->aml);
L
Linus Torvalds 已提交
415 416 417 418 419
		parser_state->aml += 4;
		break;

	case ARGP_QWORDDATA:

L
Len Brown 已提交
420
		acpi_ps_init_op(arg, AML_QWORD_OP);
L
Linus Torvalds 已提交
421 422 423

		/* Get 8 bytes from the AML stream */

L
Len Brown 已提交
424 425
		ACPI_MOVE_64_TO_64(&arg->common.value.integer,
				   parser_state->aml);
L
Linus Torvalds 已提交
426 427 428 429 430
		parser_state->aml += 8;
		break;

	case ARGP_CHARLIST:

L
Len Brown 已提交
431 432
		acpi_ps_init_op(arg, AML_STRING_OP);
		arg->common.value.string = (char *)parser_state->aml;
L
Linus Torvalds 已提交
433

L
Len Brown 已提交
434
		while (ACPI_GET8(parser_state->aml) != '\0') {
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442
			parser_state->aml++;
		}
		parser_state->aml++;
		break;

	case ARGP_NAME:
	case ARGP_NAMESTRING:

L
Len Brown 已提交
443 444 445
		acpi_ps_init_op(arg, AML_INT_NAMEPATH_OP);
		arg->common.value.name =
		    acpi_ps_get_next_namestring(parser_state);
L
Linus Torvalds 已提交
446 447 448 449
		break;

	default:

L
Len Brown 已提交
450
		ACPI_REPORT_ERROR(("Invalid arg_type %X\n", arg_type));
L
Linus Torvalds 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
		break;
	}

	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_field
 *
 * PARAMETERS:  parser_state        - Current parser state object
 *
 * RETURN:      A newly allocated FIELD op
 *
 * DESCRIPTION: Get next field (named_field, reserved_field, or access_field)
 *
 ******************************************************************************/

L
Len Brown 已提交
469 470
static union acpi_parse_object *acpi_ps_get_next_field(struct acpi_parse_state
						       *parser_state)
L
Linus Torvalds 已提交
471
{
L
Len Brown 已提交
472 473 474 475 476 477
	u32 aml_offset = (u32)
	    ACPI_PTR_DIFF(parser_state->aml,
			  parser_state->aml_start);
	union acpi_parse_object *field;
	u16 opcode;
	u32 name;
L
Linus Torvalds 已提交
478

L
Len Brown 已提交
479
	ACPI_FUNCTION_TRACE("ps_get_next_field");
L
Linus Torvalds 已提交
480

R
Robert Moore 已提交
481
	/* Determine field type */
L
Linus Torvalds 已提交
482

L
Len Brown 已提交
483
	switch (ACPI_GET8(parser_state->aml)) {
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	default:

		opcode = AML_INT_NAMEDFIELD_OP;
		break;

	case 0x00:

		opcode = AML_INT_RESERVEDFIELD_OP;
		parser_state->aml++;
		break;

	case 0x01:

		opcode = AML_INT_ACCESSFIELD_OP;
		parser_state->aml++;
		break;
	}

	/* Allocate a new field op */

L
Len Brown 已提交
504
	field = acpi_ps_alloc_op(opcode);
L
Linus Torvalds 已提交
505
	if (!field) {
L
Len Brown 已提交
506
		return_PTR(NULL);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517
	}

	field->common.aml_offset = aml_offset;

	/* Decode the field type */

	switch (opcode) {
	case AML_INT_NAMEDFIELD_OP:

		/* Get the 4-character name */

L
Len Brown 已提交
518 519
		ACPI_MOVE_32_TO_32(&name, parser_state->aml);
		acpi_ps_set_name(field, name);
L
Linus Torvalds 已提交
520 521 522 523
		parser_state->aml += ACPI_NAME_SIZE;

		/* Get the length which is encoded as a package length */

L
Len Brown 已提交
524 525
		field->common.value.size =
		    acpi_ps_get_next_package_length(parser_state);
L
Linus Torvalds 已提交
526 527 528 529 530 531
		break;

	case AML_INT_RESERVEDFIELD_OP:

		/* Get the length which is encoded as a package length */

L
Len Brown 已提交
532 533
		field->common.value.size =
		    acpi_ps_get_next_package_length(parser_state);
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541
		break;

	case AML_INT_ACCESSFIELD_OP:

		/*
		 * Get access_type and access_attrib and merge into the field Op
		 * access_type is first operand, access_attribute is second
		 */
L
Len Brown 已提交
542 543
		field->common.value.integer =
		    (ACPI_GET8(parser_state->aml) << 8);
L
Linus Torvalds 已提交
544
		parser_state->aml++;
L
Len Brown 已提交
545
		field->common.value.integer |= ACPI_GET8(parser_state->aml);
L
Linus Torvalds 已提交
546 547 548 549 550 551 552 553 554
		parser_state->aml++;
		break;

	default:

		/* Opcode was set in previous switch */
		break;
	}

L
Len Brown 已提交
555
	return_PTR(field);
L
Linus Torvalds 已提交
556 557 558 559 560 561
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ps_get_next_arg
 *
R
Robert Moore 已提交
562 563
 * PARAMETERS:  walk_state          - Current state
 *              parser_state        - Current parser state object
L
Linus Torvalds 已提交
564
 *              arg_type            - The argument type (AML_*_ARG)
R
Robert Moore 已提交
565
 *              return_arg          - Where the next arg is returned
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574
 *
 * RETURN:      Status, and an op object containing the next argument.
 *
 * DESCRIPTION: Get next argument (including complex list arguments that require
 *              pushing the parser stack)
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
575 576 577
acpi_ps_get_next_arg(struct acpi_walk_state *walk_state,
		     struct acpi_parse_state *parser_state,
		     u32 arg_type, union acpi_parse_object **return_arg)
L
Linus Torvalds 已提交
578
{
L
Len Brown 已提交
579 580 581 582 583
	union acpi_parse_object *arg = NULL;
	union acpi_parse_object *prev = NULL;
	union acpi_parse_object *field;
	u32 subop;
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
584

L
Len Brown 已提交
585
	ACPI_FUNCTION_TRACE_PTR("ps_get_next_arg", parser_state);
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594

	switch (arg_type) {
	case ARGP_BYTEDATA:
	case ARGP_WORDDATA:
	case ARGP_DWORDDATA:
	case ARGP_CHARLIST:
	case ARGP_NAME:
	case ARGP_NAMESTRING:

R
Robert Moore 已提交
595
		/* Constants, strings, and namestrings are all the same size */
L
Linus Torvalds 已提交
596

L
Len Brown 已提交
597
		arg = acpi_ps_alloc_op(AML_BYTE_OP);
L
Linus Torvalds 已提交
598
		if (!arg) {
L
Len Brown 已提交
599
			return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
600
		}
L
Len Brown 已提交
601
		acpi_ps_get_next_simple_arg(parser_state, arg_type, arg);
L
Linus Torvalds 已提交
602 603 604 605 606 607
		break;

	case ARGP_PKGLENGTH:

		/* Package length, nothing returned */

L
Len Brown 已提交
608 609
		parser_state->pkg_end =
		    acpi_ps_get_next_package_end(parser_state);
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617
		break;

	case ARGP_FIELDLIST:

		if (parser_state->aml < parser_state->pkg_end) {
			/* Non-empty list */

			while (parser_state->aml < parser_state->pkg_end) {
L
Len Brown 已提交
618
				field = acpi_ps_get_next_field(parser_state);
L
Linus Torvalds 已提交
619
				if (!field) {
L
Len Brown 已提交
620
					return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
621 622 623 624
				}

				if (prev) {
					prev->common.next = field;
L
Len Brown 已提交
625
				} else {
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
					arg = field;
				}
				prev = field;
			}

			/* Skip to End of byte data */

			parser_state->aml = parser_state->pkg_end;
		}
		break;

	case ARGP_BYTELIST:

		if (parser_state->aml < parser_state->pkg_end) {
			/* Non-empty list */

L
Len Brown 已提交
642
			arg = acpi_ps_alloc_op(AML_INT_BYTELIST_OP);
L
Linus Torvalds 已提交
643
			if (!arg) {
L
Len Brown 已提交
644
				return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
645 646 647 648
			}

			/* Fill in bytelist data */

R
Robert Moore 已提交
649
			arg->common.value.size = (u32)
L
Len Brown 已提交
650 651
			    ACPI_PTR_DIFF(parser_state->pkg_end,
					  parser_state->aml);
L
Linus Torvalds 已提交
652 653 654 655 656 657 658 659 660 661 662 663
			arg->named.data = parser_state->aml;

			/* Skip to End of byte data */

			parser_state->aml = parser_state->pkg_end;
		}
		break;

	case ARGP_TARGET:
	case ARGP_SUPERNAME:
	case ARGP_SIMPLENAME:

L
Len Brown 已提交
664 665 666 667
		subop = acpi_ps_peek_opcode(parser_state);
		if (subop == 0 ||
		    acpi_ps_is_leading_char(subop) ||
		    acpi_ps_is_prefix_char(subop)) {
L
Linus Torvalds 已提交
668 669
			/* null_name or name_string */

L
Len Brown 已提交
670
			arg = acpi_ps_alloc_op(AML_INT_NAMEPATH_OP);
L
Linus Torvalds 已提交
671
			if (!arg) {
L
Len Brown 已提交
672
				return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
673 674
			}

L
Len Brown 已提交
675 676 677 678
			status =
			    acpi_ps_get_next_namepath(walk_state, parser_state,
						      arg, 0);
		} else {
R
Robert Moore 已提交
679
			/* Single complex argument, nothing returned */
L
Linus Torvalds 已提交
680 681 682 683 684 685 686 687

			walk_state->arg_count = 1;
		}
		break;

	case ARGP_DATAOBJ:
	case ARGP_TERMARG:

R
Robert Moore 已提交
688
		/* Single complex argument, nothing returned */
L
Linus Torvalds 已提交
689 690 691 692 693 694 695 696 697

		walk_state->arg_count = 1;
		break;

	case ARGP_DATAOBJLIST:
	case ARGP_TERMLIST:
	case ARGP_OBJLIST:

		if (parser_state->aml < parser_state->pkg_end) {
R
Robert Moore 已提交
698
			/* Non-empty list of variable arguments, nothing returned */
L
Linus Torvalds 已提交
699 700 701 702 703 704 705

			walk_state->arg_count = ACPI_VAR_ARGS;
		}
		break;

	default:

L
Len Brown 已提交
706
		ACPI_REPORT_ERROR(("Invalid arg_type: %X\n", arg_type));
L
Linus Torvalds 已提交
707 708 709 710 711
		status = AE_AML_OPERAND_TYPE;
		break;
	}

	*return_arg = arg;
L
Len Brown 已提交
712
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
713
}