nsutils.c 20.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/******************************************************************************
 *
 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
 *                        parents and siblings and Scope manipulation
 *
 *****************************************************************************/

/*
9
 * Copyright (C) 2000 - 2012, Intel Corp.
L
Linus Torvalds 已提交
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
 * 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>
L
Len Brown 已提交
46 47 48
#include "accommon.h"
#include "acnamesp.h"
#include "amlcode.h"
L
Linus Torvalds 已提交
49 50

#define _COMPONENT          ACPI_NAMESPACE
L
Len Brown 已提交
51
ACPI_MODULE_NAME("nsutils")
L
Linus Torvalds 已提交
52

R
Robert Moore 已提交
53
/* Local prototypes */
L
Len Brown 已提交
54
static u8 acpi_ns_valid_path_separator(char sep);
R
Robert Moore 已提交
55 56

#ifdef ACPI_OBSOLETE_FUNCTIONS
L
Len Brown 已提交
57
acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node *node_to_search);
R
Robert Moore 已提交
58 59
#endif

L
Linus Torvalds 已提交
60 61 62 63
/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_print_node_pathname
 *
64 65
 * PARAMETERS:  node            - Object
 *              message         - Prefix message
L
Linus Torvalds 已提交
66 67 68 69 70 71 72
 *
 * DESCRIPTION: Print an object's full namespace pathname
 *              Manages allocation/freeing of a pathname buffer
 *
 ******************************************************************************/

void
73 74
acpi_ns_print_node_pathname(struct acpi_namespace_node *node,
			    const char *message)
L
Linus Torvalds 已提交
75
{
L
Len Brown 已提交
76 77
	struct acpi_buffer buffer;
	acpi_status status;
L
Linus Torvalds 已提交
78 79

	if (!node) {
L
Len Brown 已提交
80
		acpi_os_printf("[NULL NAME]");
L
Linus Torvalds 已提交
81 82 83 84 85 86 87
		return;
	}

	/* Convert handle to full pathname and print it (with supplied message) */

	buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;

L
Len Brown 已提交
88 89
	status = acpi_ns_handle_to_pathname(node, &buffer);
	if (ACPI_SUCCESS(status)) {
R
Robert Moore 已提交
90
		if (message) {
L
Len Brown 已提交
91
			acpi_os_printf("%s ", message);
L
Linus Torvalds 已提交
92 93
		}

L
Len Brown 已提交
94
		acpi_os_printf("[%s] (Node %p)", (char *)buffer.pointer, node);
B
Bob Moore 已提交
95
		ACPI_FREE(buffer.pointer);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102
	}
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_valid_root_prefix
 *
103
 * PARAMETERS:  prefix          - Character to be checked
L
Linus Torvalds 已提交
104 105 106 107 108 109 110
 *
 * RETURN:      TRUE if a valid prefix
 *
 * DESCRIPTION: Check if a character is a valid ACPI Root prefix
 *
 ******************************************************************************/

L
Len Brown 已提交
111
u8 acpi_ns_valid_root_prefix(char prefix)
L
Linus Torvalds 已提交
112 113
{

114
	return ((u8)(prefix == '\\'));
L
Linus Torvalds 已提交
115 116 117 118 119 120
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_valid_path_separator
 *
121
 * PARAMETERS:  sep         - Character to be checked
L
Linus Torvalds 已提交
122 123 124 125 126 127 128
 *
 * RETURN:      TRUE if a valid path separator
 *
 * DESCRIPTION: Check if a character is a valid ACPI path separator
 *
 ******************************************************************************/

L
Len Brown 已提交
129
static u8 acpi_ns_valid_path_separator(char sep)
L
Linus Torvalds 已提交
130 131
{

132
	return ((u8)(sep == '.'));
L
Linus Torvalds 已提交
133 134 135 136 137 138
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_type
 *
139
 * PARAMETERS:  node        - Parent Node to be examined
L
Linus Torvalds 已提交
140 141 142
 *
 * RETURN:      Type field from Node whose handle is passed
 *
R
Robert Moore 已提交
143 144
 * DESCRIPTION: Return the type of a Namespace node
 *
L
Linus Torvalds 已提交
145 146
 ******************************************************************************/

L
Len Brown 已提交
147
acpi_object_type acpi_ns_get_type(struct acpi_namespace_node * node)
L
Linus Torvalds 已提交
148
{
B
Bob Moore 已提交
149
	ACPI_FUNCTION_TRACE(ns_get_type);
L
Linus Torvalds 已提交
150 151

	if (!node) {
B
Bob Moore 已提交
152
		ACPI_WARNING((AE_INFO, "Null Node parameter"));
B
Bob Moore 已提交
153
		return_UINT32(ACPI_TYPE_ANY);
L
Linus Torvalds 已提交
154 155
	}

B
Bob Moore 已提交
156
	return_UINT32((acpi_object_type) node->type);
L
Linus Torvalds 已提交
157 158 159 160 161 162
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_local
 *
163
 * PARAMETERS:  type        - A namespace object type
L
Linus Torvalds 已提交
164 165 166 167
 *
 * RETURN:      LOCAL if names must be found locally in objects of the
 *              passed type, 0 if enclosing scopes should be searched
 *
R
Robert Moore 已提交
168 169
 * DESCRIPTION: Returns scope rule for the given object type.
 *
L
Linus Torvalds 已提交
170 171
 ******************************************************************************/

L
Len Brown 已提交
172
u32 acpi_ns_local(acpi_object_type type)
L
Linus Torvalds 已提交
173
{
B
Bob Moore 已提交
174
	ACPI_FUNCTION_TRACE(ns_local);
L
Linus Torvalds 已提交
175

L
Len Brown 已提交
176
	if (!acpi_ut_valid_object_type(type)) {
B
Bob Moore 已提交
177

L
Linus Torvalds 已提交
178 179
		/* Type code out of range  */

180
		ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
B
Bob Moore 已提交
181
		return_UINT32(ACPI_NS_NORMAL);
L
Linus Torvalds 已提交
182 183
	}

B
Bob Moore 已提交
184
	return_UINT32((u32) acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
L
Linus Torvalds 已提交
185 186 187 188 189 190
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_internal_name_length
 *
191
 * PARAMETERS:  info            - Info struct initialized with the
L
Linus Torvalds 已提交
192 193
 *                                external name pointer.
 *
R
Robert Moore 已提交
194
 * RETURN:      None
L
Linus Torvalds 已提交
195 196 197 198 199 200
 *
 * DESCRIPTION: Calculate the length of the internal (AML) namestring
 *              corresponding to the external (ASL) namestring.
 *
 ******************************************************************************/

L
Len Brown 已提交
201
void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
L
Linus Torvalds 已提交
202
{
203
	const char *next_external_char;
L
Len Brown 已提交
204
	u32 i;
L
Linus Torvalds 已提交
205

L
Len Brown 已提交
206
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219

	next_external_char = info->external_name;
	info->num_carats = 0;
	info->num_segments = 0;
	info->fully_qualified = FALSE;

	/*
	 * For the internal name, the required length is 4 bytes per segment, plus
	 * 1 each for root_prefix, multi_name_prefix_op, segment count, trailing null
	 * (which is not really needed, but no there's harm in putting it there)
	 *
	 * strlen() + 1 covers the first name_seg, which has no path separator
	 */
220
	if (acpi_ns_valid_root_prefix(*next_external_char)) {
L
Linus Torvalds 已提交
221 222
		info->fully_qualified = TRUE;
		next_external_char++;
223 224 225 226 227 228

		/* Skip redundant root_prefix, like \\_SB.PCI0.SBRG.EC0 */

		while (acpi_ns_valid_root_prefix(*next_external_char)) {
			next_external_char++;
		}
L
Len Brown 已提交
229
	} else {
230 231
		/* Handle Carat prefixes */

L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245
		while (*next_external_char == '^') {
			info->num_carats++;
			next_external_char++;
		}
	}

	/*
	 * Determine the number of ACPI name "segments" by counting the number of
	 * path separators within the string. Start with one segment since the
	 * segment count is [(# separators) + 1], and zero separators is ok.
	 */
	if (*next_external_char) {
		info->num_segments = 1;
		for (i = 0; next_external_char[i]; i++) {
L
Len Brown 已提交
246
			if (acpi_ns_valid_path_separator(next_external_char[i])) {
L
Linus Torvalds 已提交
247 248 249 250 251 252
				info->num_segments++;
			}
		}
	}

	info->length = (ACPI_NAME_SIZE * info->num_segments) +
L
Len Brown 已提交
253
	    4 + info->num_carats;
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261

	info->next_external_char = next_external_char;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_build_internal_name
 *
262
 * PARAMETERS:  info            - Info struct fully initialized
L
Linus Torvalds 已提交
263 264 265 266 267 268 269 270
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Construct the internal (AML) namestring
 *              corresponding to the external (ASL) namestring.
 *
 ******************************************************************************/

L
Len Brown 已提交
271
acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
L
Linus Torvalds 已提交
272
{
L
Len Brown 已提交
273 274
	u32 num_segments = info->num_segments;
	char *internal_name = info->internal_name;
275
	const char *external_name = info->next_external_char;
L
Len Brown 已提交
276
	char *result = NULL;
277
	u32 i;
L
Linus Torvalds 已提交
278

B
Bob Moore 已提交
279
	ACPI_FUNCTION_TRACE(ns_build_internal_name);
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287

	/* Setup the correct prefixes, counts, and pointers */

	if (info->fully_qualified) {
		internal_name[0] = '\\';

		if (num_segments <= 1) {
			result = &internal_name[1];
L
Len Brown 已提交
288
		} else if (num_segments == 2) {
L
Linus Torvalds 已提交
289 290
			internal_name[1] = AML_DUAL_NAME_PREFIX;
			result = &internal_name[2];
L
Len Brown 已提交
291
		} else {
L
Linus Torvalds 已提交
292
			internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
L
Len Brown 已提交
293
			internal_name[2] = (char)num_segments;
L
Linus Torvalds 已提交
294 295
			result = &internal_name[3];
		}
L
Len Brown 已提交
296
	} else {
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309
		/*
		 * Not fully qualified.
		 * Handle Carats first, then append the name segments
		 */
		i = 0;
		if (info->num_carats) {
			for (i = 0; i < info->num_carats; i++) {
				internal_name[i] = '^';
			}
		}

		if (num_segments <= 1) {
			result = &internal_name[i];
L
Len Brown 已提交
310
		} else if (num_segments == 2) {
L
Linus Torvalds 已提交
311
			internal_name[i] = AML_DUAL_NAME_PREFIX;
312
			result = &internal_name[(acpi_size) i + 1];
L
Len Brown 已提交
313
		} else {
L
Linus Torvalds 已提交
314
			internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
315 316
			internal_name[(acpi_size) i + 1] = (char)num_segments;
			result = &internal_name[(acpi_size) i + 2];
L
Linus Torvalds 已提交
317 318 319 320 321 322 323
		}
	}

	/* Build the name (minus path separators) */

	for (; num_segments; num_segments--) {
		for (i = 0; i < ACPI_NAME_SIZE; i++) {
L
Len Brown 已提交
324 325
			if (acpi_ns_valid_path_separator(*external_name) ||
			    (*external_name == 0)) {
B
Bob Moore 已提交
326

L
Linus Torvalds 已提交
327 328 329
				/* Pad the segment with underscore(s) if segment is short */

				result[i] = '_';
L
Len Brown 已提交
330
			} else {
L
Linus Torvalds 已提交
331 332
				/* Convert the character to uppercase and save it */

L
Len Brown 已提交
333 334
				result[i] =
				    (char)ACPI_TOUPPER((int)*external_name);
L
Linus Torvalds 已提交
335 336 337 338 339 340
				external_name++;
			}
		}

		/* Now we must have a path separator, or the pathname is bad */

L
Len Brown 已提交
341 342
		if (!acpi_ns_valid_path_separator(*external_name) &&
		    (*external_name != 0)) {
343
			return_ACPI_STATUS(AE_BAD_PATHNAME);
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
		}

		/* Move on the next segment */

		external_name++;
		result += ACPI_NAME_SIZE;
	}

	/* Terminate the string */

	*result = 0;

	if (info->fully_qualified) {
L
Len Brown 已提交
357 358 359 360 361 362
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
				  "Returning [%p] (abs) \"\\%s\"\n",
				  internal_name, internal_name));
	} else {
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
				  internal_name, internal_name));
L
Linus Torvalds 已提交
363 364
	}

L
Len Brown 已提交
365
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
366 367 368 369 370 371 372
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_internalize_name
 *
 * PARAMETERS:  *external_name          - External representation of name
373
 *              **Converted name        - Where to return the resulting
L
Linus Torvalds 已提交
374 375 376 377 378 379 380 381 382
 *                                        internal represention of the name
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
 *              to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
 *
 *******************************************************************************/

383 384
acpi_status
acpi_ns_internalize_name(const char *external_name, char **converted_name)
L
Linus Torvalds 已提交
385
{
L
Len Brown 已提交
386 387 388
	char *internal_name;
	struct acpi_namestring_info info;
	acpi_status status;
L
Linus Torvalds 已提交
389

B
Bob Moore 已提交
390
	ACPI_FUNCTION_TRACE(ns_internalize_name);
L
Linus Torvalds 已提交
391

L
Len Brown 已提交
392 393
	if ((!external_name) || (*external_name == 0) || (!converted_name)) {
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
394 395 396 397 398
	}

	/* Get the length of the new internal name */

	info.external_name = external_name;
L
Len Brown 已提交
399
	acpi_ns_get_internal_name_length(&info);
L
Linus Torvalds 已提交
400 401 402

	/* We need a segment to store the internal  name */

B
Bob Moore 已提交
403
	internal_name = ACPI_ALLOCATE_ZEROED(info.length);
L
Linus Torvalds 已提交
404
	if (!internal_name) {
L
Len Brown 已提交
405
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
406 407 408 409 410
	}

	/* Build the name */

	info.internal_name = internal_name;
L
Len Brown 已提交
411 412
	status = acpi_ns_build_internal_name(&info);
	if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
413
		ACPI_FREE(internal_name);
L
Len Brown 已提交
414
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
415 416 417
	}

	*converted_name = internal_name;
L
Len Brown 已提交
418
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
419 420 421 422 423 424
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_externalize_name
 *
R
Robert Moore 已提交
425 426 427 428 429
 * PARAMETERS:  internal_name_length - Lenth of the internal name below
 *              internal_name       - Internal representation of name
 *              converted_name_length - Where the length is returned
 *              converted_name      - Where the resulting external name
 *                                    is returned
L
Linus Torvalds 已提交
430 431 432 433
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
R
Robert Moore 已提交
434
 *              to its external (printable) form (e.g. "\_PR_.CPU0")
L
Linus Torvalds 已提交
435 436 437 438
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
439
acpi_ns_externalize_name(u32 internal_name_length,
440
			 const char *internal_name,
L
Len Brown 已提交
441
			 u32 * converted_name_length, char **converted_name)
L
Linus Torvalds 已提交
442
{
443 444 445 446 447 448
	u32 names_index = 0;
	u32 num_segments = 0;
	u32 required_length;
	u32 prefix_length = 0;
	u32 i = 0;
	u32 j = 0;
L
Linus Torvalds 已提交
449

B
Bob Moore 已提交
450
	ACPI_FUNCTION_TRACE(ns_externalize_name);
L
Linus Torvalds 已提交
451

L
Len Brown 已提交
452 453
	if (!internal_name_length || !internal_name || !converted_name) {
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
454 455
	}

456 457
	/* Check for a prefix (one '\' | one or more '^') */

L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466
	switch (internal_name[0]) {
	case '\\':
		prefix_length = 1;
		break;

	case '^':
		for (i = 0; i < internal_name_length; i++) {
			if (internal_name[i] == '^') {
				prefix_length = i + 1;
L
Len Brown 已提交
467
			} else {
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
				break;
			}
		}

		if (i == internal_name_length) {
			prefix_length = i;
		}

		break;

	default:
		break;
	}

	/*
483
	 * Check for object names. Note that there could be 0-255 of these
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492
	 * 4-byte elements.
	 */
	if (prefix_length < internal_name_length) {
		switch (internal_name[prefix_length]) {
		case AML_MULTI_NAME_PREFIX_OP:

			/* <count> 4-byte names */

			names_index = prefix_length + 2;
493 494
			num_segments = (u8)
			    internal_name[(acpi_size) prefix_length + 1];
L
Linus Torvalds 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
			break;

		case AML_DUAL_NAME_PREFIX:

			/* Two 4-byte names */

			names_index = prefix_length + 1;
			num_segments = 2;
			break;

		case 0:

			/* null_name */

			names_index = 0;
			num_segments = 0;
			break;

		default:

			/* one 4-byte name */

			names_index = prefix_length;
			num_segments = 1;
			break;
		}
	}

	/*
	 * Calculate the length of converted_name, which equals the length
	 * of the prefix, length of all object names, length of any required
	 * punctuation ('.') between object names, plus the NULL terminator.
	 */
	required_length = prefix_length + (4 * num_segments) +
L
Len Brown 已提交
529
	    ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
L
Linus Torvalds 已提交
530 531

	/*
532
	 * Check to see if we're still in bounds. If not, there's a problem
L
Linus Torvalds 已提交
533 534 535
	 * with internal_name (invalid format).
	 */
	if (required_length > internal_name_length) {
B
Bob Moore 已提交
536
		ACPI_ERROR((AE_INFO, "Invalid internal name"));
L
Len Brown 已提交
537
		return_ACPI_STATUS(AE_BAD_PATHNAME);
L
Linus Torvalds 已提交
538 539
	}

540 541
	/* Build the converted_name */

B
Bob Moore 已提交
542
	*converted_name = ACPI_ALLOCATE_ZEROED(required_length);
L
Linus Torvalds 已提交
543
	if (!(*converted_name)) {
L
Len Brown 已提交
544
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558
	}

	j = 0;

	for (i = 0; i < prefix_length; i++) {
		(*converted_name)[j++] = internal_name[i];
	}

	if (num_segments > 0) {
		for (i = 0; i < num_segments; i++) {
			if (i > 0) {
				(*converted_name)[j++] = '.';
			}

559 560 561 562 563
			/* Copy and validate the 4-char name segment */

			ACPI_MOVE_NAME(&(*converted_name)[j],
				       &internal_name[names_index]);
			acpi_ut_repair_name(&(*converted_name)[j]);
564 565 566

			j += ACPI_NAME_SIZE;
			names_index += ACPI_NAME_SIZE;
L
Linus Torvalds 已提交
567 568 569 570 571 572 573
		}
	}

	if (converted_name_length) {
		*converted_name_length = (u32) required_length;
	}

L
Len Brown 已提交
574
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
575 576 577 578
}

/*******************************************************************************
 *
579
 * FUNCTION:    acpi_ns_validate_handle
L
Linus Torvalds 已提交
580
 *
581
 * PARAMETERS:  handle          - Handle to be validated and typecast to a
582
 *                                namespace node.
L
Linus Torvalds 已提交
583
 *
584
 * RETURN:      A pointer to a namespace node
L
Linus Torvalds 已提交
585
 *
586 587
 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
 *              cases for the root node.
L
Linus Torvalds 已提交
588
 *
589
 * NOTE: Real integer handles would allow for more verification
R
Robert Moore 已提交
590
 *       and keep all pointers within this subsystem - however this introduces
591 592 593
 *       more overhead and has not been necessary to this point. Drivers
 *       holding handles are typically notified before a node becomes invalid
 *       due to a table unload.
594
 *
L
Linus Torvalds 已提交
595 596
 ******************************************************************************/

597
struct acpi_namespace_node *acpi_ns_validate_handle(acpi_handle handle)
L
Linus Torvalds 已提交
598 599
{

L
Len Brown 已提交
600
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
601

602 603
	/* Parameter validation */

B
Bob Moore 已提交
604
	if ((!handle) || (handle == ACPI_ROOT_OBJECT)) {
L
Linus Torvalds 已提交
605 606 607 608 609
		return (acpi_gbl_root_node);
	}

	/* We can at least attempt to verify the handle */

L
Len Brown 已提交
610
	if (ACPI_GET_DESCRIPTOR_TYPE(handle) != ACPI_DESC_TYPE_NAMED) {
L
Linus Torvalds 已提交
611 612 613
		return (NULL);
	}

B
Bob Moore 已提交
614
	return (ACPI_CAST_PTR(struct acpi_namespace_node, handle));
L
Linus Torvalds 已提交
615 616 617 618 619 620 621 622 623 624
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_terminate
 *
 * PARAMETERS:  none
 *
 * RETURN:      none
 *
R
Robert Moore 已提交
625
 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
L
Linus Torvalds 已提交
626 627 628
 *
 ******************************************************************************/

L
Len Brown 已提交
629
void acpi_ns_terminate(void)
L
Linus Torvalds 已提交
630
{
L
Len Brown 已提交
631
	union acpi_operand_object *obj_desc;
L
Linus Torvalds 已提交
632

B
Bob Moore 已提交
633
	ACPI_FUNCTION_TRACE(ns_terminate);
L
Linus Torvalds 已提交
634 635 636 637 638 639

	/*
	 * 1) Free the entire namespace -- all nodes and objects
	 *
	 * Delete all object descriptors attached to namepsace nodes
	 */
L
Len Brown 已提交
640
	acpi_ns_delete_namespace_subtree(acpi_gbl_root_node);
L
Linus Torvalds 已提交
641 642 643

	/* Detach any objects attached to the root */

L
Len Brown 已提交
644
	obj_desc = acpi_ns_get_attached_object(acpi_gbl_root_node);
L
Linus Torvalds 已提交
645
	if (obj_desc) {
L
Len Brown 已提交
646
		acpi_ns_detach_object(acpi_gbl_root_node);
L
Linus Torvalds 已提交
647 648
	}

L
Len Brown 已提交
649
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Namespace freed\n"));
L
Linus Torvalds 已提交
650 651 652 653 654 655 656
	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_opens_scope
 *
657
 * PARAMETERS:  type        - A valid namespace type
L
Linus Torvalds 已提交
658 659 660 661 662 663
 *
 * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
 *              to the ACPI specification, else 0
 *
 ******************************************************************************/

L
Len Brown 已提交
664
u32 acpi_ns_opens_scope(acpi_object_type type)
L
Linus Torvalds 已提交
665
{
B
Bob Moore 已提交
666
	ACPI_FUNCTION_TRACE_STR(ns_opens_scope, acpi_ut_get_type_name(type));
L
Linus Torvalds 已提交
667

L
Len Brown 已提交
668
	if (!acpi_ut_valid_object_type(type)) {
B
Bob Moore 已提交
669

L
Linus Torvalds 已提交
670 671
		/* type code out of range  */

672
		ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
B
Bob Moore 已提交
673
		return_UINT32(ACPI_NS_NORMAL);
L
Linus Torvalds 已提交
674 675
	}

676
	return_UINT32(((u32)acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
L
Linus Torvalds 已提交
677 678 679 680
}

/*******************************************************************************
 *
B
Bob Moore 已提交
681
 * FUNCTION:    acpi_ns_get_node
L
Linus Torvalds 已提交
682
 *
683
 * PARAMETERS:  *pathname   - Name to be found, in external (ASL) format. The
L
Linus Torvalds 已提交
684 685
 *                            \ (backslash) and ^ (carat) prefixes, and the
 *                            . (period) to separate segments are supported.
B
Bob Moore 已提交
686
 *              prefix_node  - Root of subtree to be searched, or NS_ALL for the
687
 *                            root of the name space. If Name is fully
L
Linus Torvalds 已提交
688 689
 *                            qualified (first s8 is '\'), the passed value
 *                            of Scope will not be accessed.
690
 *              flags       - Used to indicate whether to perform upsearch or
L
Linus Torvalds 已提交
691 692 693 694
 *                            not.
 *              return_node - Where the Node is returned
 *
 * DESCRIPTION: Look up a name relative to a given scope and return the
695
 *              corresponding Node. NOTE: Scope can be null.
L
Linus Torvalds 已提交
696 697 698 699 700 701
 *
 * MUTEX:       Locks namespace
 *
 ******************************************************************************/

acpi_status
B
Bob Moore 已提交
702
acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
703
		 const char *pathname,
B
Bob Moore 已提交
704
		 u32 flags, struct acpi_namespace_node **return_node)
L
Linus Torvalds 已提交
705
{
L
Len Brown 已提交
706 707
	union acpi_generic_state scope_info;
	acpi_status status;
B
Bob Moore 已提交
708
	char *internal_path;
L
Linus Torvalds 已提交
709

710
	ACPI_FUNCTION_TRACE_PTR(ns_get_node, ACPI_CAST_PTR(char, pathname));
L
Linus Torvalds 已提交
711

B
Bob Moore 已提交
712 713 714 715 716 717 718
	if (!pathname) {
		*return_node = prefix_node;
		if (!prefix_node) {
			*return_node = acpi_gbl_root_node;
		}
		return_ACPI_STATUS(AE_OK);
	}
B
Bob Moore 已提交
719

B
Bob Moore 已提交
720
	/* Convert path to internal representation */
L
Linus Torvalds 已提交
721

B
Bob Moore 已提交
722 723 724
	status = acpi_ns_internalize_name(pathname, &internal_path);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
725 726 727 728
	}

	/* Must lock namespace during lookup */

L
Len Brown 已提交
729 730
	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
731 732 733 734 735
		goto cleanup;
	}

	/* Setup lookup scope (search starting point) */

B
Bob Moore 已提交
736
	scope_info.scope.node = prefix_node;
L
Linus Torvalds 已提交
737 738 739

	/* Lookup the name in the namespace */

B
Bob Moore 已提交
740 741 742 743
	status = acpi_ns_lookup(&scope_info, internal_path, ACPI_TYPE_ANY,
				ACPI_IMODE_EXECUTE,
				(flags | ACPI_NS_DONT_OPEN_SCOPE), NULL,
				return_node);
L
Len Brown 已提交
744
	if (ACPI_FAILURE(status)) {
745
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s, %s\n",
B
Bob Moore 已提交
746
				  pathname, acpi_format_exception(status)));
L
Linus Torvalds 已提交
747 748
	}

L
Len Brown 已提交
749
	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
L
Linus Torvalds 已提交
750

L
Len Brown 已提交
751
      cleanup:
B
Bob Moore 已提交
752
	ACPI_FREE(internal_path);
L
Len Brown 已提交
753
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
754
}