nsutils.c 26.9 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
 *
 *****************************************************************************/

/*
L
Len Brown 已提交
9
 * Copyright (C) 2000 - 2008, 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 49
#include "accommon.h"
#include "acnamesp.h"
#include "amlcode.h"
#include "actables.h"
L
Linus Torvalds 已提交
50 51

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

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

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

L
Linus Torvalds 已提交
61 62 63 64 65 66
/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_report_error
 *
 * PARAMETERS:  module_name         - Caller's module name (for error output)
 *              line_number         - Caller's line number (for error output)
R
Robert Moore 已提交
67 68
 *              internal_name       - Name or path of the namespace node
 *              lookup_status       - Exception code from NS lookup
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76
 *
 * RETURN:      None
 *
 * DESCRIPTION: Print warning message with full pathname
 *
 ******************************************************************************/

void
77
acpi_ns_report_error(const char *module_name,
L
Len Brown 已提交
78
		     u32 line_number,
79
		     const char *internal_name, acpi_status lookup_status)
L
Linus Torvalds 已提交
80
{
L
Len Brown 已提交
81
	acpi_status status;
B
Bob Moore 已提交
82
	u32 bad_name;
L
Len Brown 已提交
83
	char *name = NULL;
L
Linus Torvalds 已提交
84

B
Bob Moore 已提交
85
	acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
L
Linus Torvalds 已提交
86 87

	if (lookup_status == AE_BAD_CHARACTER) {
B
Bob Moore 已提交
88

L
Linus Torvalds 已提交
89 90
		/* There is a non-ascii character in the name */

B
Bob Moore 已提交
91 92
		ACPI_MOVE_32_TO_32(&bad_name, internal_name);
		acpi_os_printf("[0x%4.4X] (NON-ASCII)", bad_name);
L
Len Brown 已提交
93
	} else {
L
Linus Torvalds 已提交
94 95
		/* Convert path to external format */

L
Len Brown 已提交
96 97
		status = acpi_ns_externalize_name(ACPI_UINT32_MAX,
						  internal_name, NULL, &name);
L
Linus Torvalds 已提交
98 99 100

		/* Print target name */

L
Len Brown 已提交
101 102 103 104
		if (ACPI_SUCCESS(status)) {
			acpi_os_printf("[%s]", name);
		} else {
			acpi_os_printf("[COULD NOT EXTERNALIZE NAME]");
L
Linus Torvalds 已提交
105 106 107
		}

		if (name) {
B
Bob Moore 已提交
108
			ACPI_FREE(name);
L
Linus Torvalds 已提交
109 110 111
		}
	}

B
Bob Moore 已提交
112
	acpi_os_printf(" Namespace lookup failure, %s\n",
L
Len Brown 已提交
113
		       acpi_format_exception(lookup_status));
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_report_method_error
 *
 * PARAMETERS:  module_name         - Caller's module name (for error output)
 *              line_number         - Caller's line number (for error output)
 *              Message             - Error message to use on failure
R
Robert Moore 已提交
123
 *              prefix_node         - Prefix relative to the path
B
Bob Moore 已提交
124
 *              Path                - Path to the node (optional)
R
Robert Moore 已提交
125
 *              method_status       - Execution status
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133
 *
 * RETURN:      None
 *
 * DESCRIPTION: Print warning message with full pathname
 *
 ******************************************************************************/

void
134
acpi_ns_report_method_error(const char *module_name,
L
Len Brown 已提交
135
			    u32 line_number,
136
			    const char *message,
L
Len Brown 已提交
137
			    struct acpi_namespace_node *prefix_node,
138
			    const char *path, acpi_status method_status)
L
Linus Torvalds 已提交
139
{
L
Len Brown 已提交
140 141
	acpi_status status;
	struct acpi_namespace_node *node = prefix_node;
L
Linus Torvalds 已提交
142

B
Bob Moore 已提交
143
	acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
B
Bob Moore 已提交
144

L
Linus Torvalds 已提交
145
	if (path) {
B
Bob Moore 已提交
146 147 148
		status =
		    acpi_ns_get_node(prefix_node, path, ACPI_NS_NO_UPSEARCH,
				     &node);
L
Len Brown 已提交
149
		if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
150
			acpi_os_printf("[Could not get node by pathname]");
L
Linus Torvalds 已提交
151 152 153
		}
	}

L
Len Brown 已提交
154 155
	acpi_ns_print_node_pathname(node, message);
	acpi_os_printf(", %s\n", acpi_format_exception(method_status));
L
Linus Torvalds 已提交
156 157 158 159 160 161
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_print_node_pathname
 *
R
Robert Moore 已提交
162 163
 * PARAMETERS:  Node            - Object
 *              Message         - Prefix message
L
Linus Torvalds 已提交
164 165 166 167 168 169 170
 *
 * DESCRIPTION: Print an object's full namespace pathname
 *              Manages allocation/freeing of a pathname buffer
 *
 ******************************************************************************/

void
171 172
acpi_ns_print_node_pathname(struct acpi_namespace_node *node,
			    const char *message)
L
Linus Torvalds 已提交
173
{
L
Len Brown 已提交
174 175
	struct acpi_buffer buffer;
	acpi_status status;
L
Linus Torvalds 已提交
176 177

	if (!node) {
L
Len Brown 已提交
178
		acpi_os_printf("[NULL NAME]");
L
Linus Torvalds 已提交
179 180 181 182 183 184 185
		return;
	}

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

	buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;

L
Len Brown 已提交
186 187
	status = acpi_ns_handle_to_pathname(node, &buffer);
	if (ACPI_SUCCESS(status)) {
R
Robert Moore 已提交
188
		if (message) {
L
Len Brown 已提交
189
			acpi_os_printf("%s ", message);
L
Linus Torvalds 已提交
190 191
		}

L
Len Brown 已提交
192
		acpi_os_printf("[%s] (Node %p)", (char *)buffer.pointer, node);
B
Bob Moore 已提交
193
		ACPI_FREE(buffer.pointer);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	}
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_valid_root_prefix
 *
 * PARAMETERS:  Prefix          - Character to be checked
 *
 * RETURN:      TRUE if a valid prefix
 *
 * DESCRIPTION: Check if a character is a valid ACPI Root prefix
 *
 ******************************************************************************/

L
Len Brown 已提交
209
u8 acpi_ns_valid_root_prefix(char prefix)
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218
{

	return ((u8) (prefix == '\\'));
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_valid_path_separator
 *
R
Robert Moore 已提交
219
 * PARAMETERS:  Sep         - Character to be checked
L
Linus Torvalds 已提交
220 221 222 223 224 225 226
 *
 * RETURN:      TRUE if a valid path separator
 *
 * DESCRIPTION: Check if a character is a valid ACPI path separator
 *
 ******************************************************************************/

L
Len Brown 已提交
227
static u8 acpi_ns_valid_path_separator(char sep)
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236
{

	return ((u8) (sep == '.'));
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_type
 *
R
Robert Moore 已提交
237
 * PARAMETERS:  Node        - Parent Node to be examined
L
Linus Torvalds 已提交
238 239 240
 *
 * RETURN:      Type field from Node whose handle is passed
 *
R
Robert Moore 已提交
241 242
 * DESCRIPTION: Return the type of a Namespace node
 *
L
Linus Torvalds 已提交
243 244
 ******************************************************************************/

L
Len Brown 已提交
245
acpi_object_type acpi_ns_get_type(struct acpi_namespace_node * node)
L
Linus Torvalds 已提交
246
{
B
Bob Moore 已提交
247
	ACPI_FUNCTION_TRACE(ns_get_type);
L
Linus Torvalds 已提交
248 249

	if (!node) {
B
Bob Moore 已提交
250
		ACPI_WARNING((AE_INFO, "Null Node parameter"));
B
Bob Moore 已提交
251
		return_UINT32(ACPI_TYPE_ANY);
L
Linus Torvalds 已提交
252 253
	}

B
Bob Moore 已提交
254
	return_UINT32((acpi_object_type) node->type);
L
Linus Torvalds 已提交
255 256 257 258 259 260
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_local
 *
R
Robert Moore 已提交
261
 * PARAMETERS:  Type        - A namespace object type
L
Linus Torvalds 已提交
262 263 264 265
 *
 * RETURN:      LOCAL if names must be found locally in objects of the
 *              passed type, 0 if enclosing scopes should be searched
 *
R
Robert Moore 已提交
266 267
 * DESCRIPTION: Returns scope rule for the given object type.
 *
L
Linus Torvalds 已提交
268 269
 ******************************************************************************/

L
Len Brown 已提交
270
u32 acpi_ns_local(acpi_object_type type)
L
Linus Torvalds 已提交
271
{
B
Bob Moore 已提交
272
	ACPI_FUNCTION_TRACE(ns_local);
L
Linus Torvalds 已提交
273

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

L
Linus Torvalds 已提交
276 277
		/* Type code out of range  */

B
Bob Moore 已提交
278
		ACPI_WARNING((AE_INFO, "Invalid Object Type %X", type));
B
Bob Moore 已提交
279
		return_UINT32(ACPI_NS_NORMAL);
L
Linus Torvalds 已提交
280 281
	}

B
Bob Moore 已提交
282
	return_UINT32((u32) acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_internal_name_length
 *
 * PARAMETERS:  Info            - Info struct initialized with the
 *                                external name pointer.
 *
R
Robert Moore 已提交
292
 * RETURN:      None
L
Linus Torvalds 已提交
293 294 295 296 297 298
 *
 * DESCRIPTION: Calculate the length of the internal (AML) namestring
 *              corresponding to the external (ASL) namestring.
 *
 ******************************************************************************/

L
Len Brown 已提交
299
void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
L
Linus Torvalds 已提交
300
{
301
	const char *next_external_char;
L
Len Brown 已提交
302
	u32 i;
L
Linus Torvalds 已提交
303

L
Len Brown 已提交
304
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317

	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
	 */
318
	if (acpi_ns_valid_root_prefix(*next_external_char)) {
L
Linus Torvalds 已提交
319 320
		info->fully_qualified = TRUE;
		next_external_char++;
321 322 323 324 325 326

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

		while (acpi_ns_valid_root_prefix(*next_external_char)) {
			next_external_char++;
		}
L
Len Brown 已提交
327
	} else {
328 329
		/* Handle Carat prefixes */

L
Linus Torvalds 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343
		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 已提交
344
			if (acpi_ns_valid_path_separator(next_external_char[i])) {
L
Linus Torvalds 已提交
345 346 347 348 349 350
				info->num_segments++;
			}
		}
	}

	info->length = (ACPI_NAME_SIZE * info->num_segments) +
L
Len Brown 已提交
351
	    4 + info->num_carats;
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

	info->next_external_char = next_external_char;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_build_internal_name
 *
 * PARAMETERS:  Info            - Info struct fully initialized
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Construct the internal (AML) namestring
 *              corresponding to the external (ASL) namestring.
 *
 ******************************************************************************/

L
Len Brown 已提交
369
acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
L
Linus Torvalds 已提交
370
{
L
Len Brown 已提交
371 372
	u32 num_segments = info->num_segments;
	char *internal_name = info->internal_name;
373
	const char *external_name = info->next_external_char;
L
Len Brown 已提交
374
	char *result = NULL;
375
	u32 i;
L
Linus Torvalds 已提交
376

B
Bob Moore 已提交
377
	ACPI_FUNCTION_TRACE(ns_build_internal_name);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385

	/* 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 已提交
386
		} else if (num_segments == 2) {
L
Linus Torvalds 已提交
387 388
			internal_name[1] = AML_DUAL_NAME_PREFIX;
			result = &internal_name[2];
L
Len Brown 已提交
389
		} else {
L
Linus Torvalds 已提交
390
			internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
L
Len Brown 已提交
391
			internal_name[2] = (char)num_segments;
L
Linus Torvalds 已提交
392 393
			result = &internal_name[3];
		}
L
Len Brown 已提交
394
	} else {
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407
		/*
		 * 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 已提交
408
		} else if (num_segments == 2) {
L
Linus Torvalds 已提交
409
			internal_name[i] = AML_DUAL_NAME_PREFIX;
410
			result = &internal_name[(acpi_size) i + 1];
L
Len Brown 已提交
411
		} else {
L
Linus Torvalds 已提交
412
			internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
413 414
			internal_name[(acpi_size) i + 1] = (char)num_segments;
			result = &internal_name[(acpi_size) i + 2];
L
Linus Torvalds 已提交
415 416 417 418 419 420 421
		}
	}

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

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

L
Linus Torvalds 已提交
425 426 427
				/* Pad the segment with underscore(s) if segment is short */

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

L
Len Brown 已提交
431 432
				result[i] =
				    (char)ACPI_TOUPPER((int)*external_name);
L
Linus Torvalds 已提交
433 434 435 436 437 438
				external_name++;
			}
		}

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

L
Len Brown 已提交
439 440 441
		if (!acpi_ns_valid_path_separator(*external_name) &&
		    (*external_name != 0)) {
			return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454
		}

		/* Move on the next segment */

		external_name++;
		result += ACPI_NAME_SIZE;
	}

	/* Terminate the string */

	*result = 0;

	if (info->fully_qualified) {
L
Len Brown 已提交
455 456 457 458 459 460
		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 已提交
461 462
	}

L
Len Brown 已提交
463
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_internalize_name
 *
 * PARAMETERS:  *external_name          - External representation of name
 *              **Converted Name        - Where to return the resulting
 *                                        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)
 *
 *******************************************************************************/

481 482
acpi_status
acpi_ns_internalize_name(const char *external_name, char **converted_name)
L
Linus Torvalds 已提交
483
{
L
Len Brown 已提交
484 485 486
	char *internal_name;
	struct acpi_namestring_info info;
	acpi_status status;
L
Linus Torvalds 已提交
487

B
Bob Moore 已提交
488
	ACPI_FUNCTION_TRACE(ns_internalize_name);
L
Linus Torvalds 已提交
489

L
Len Brown 已提交
490 491
	if ((!external_name) || (*external_name == 0) || (!converted_name)) {
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
492 493 494 495 496
	}

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

	info.external_name = external_name;
L
Len Brown 已提交
497
	acpi_ns_get_internal_name_length(&info);
L
Linus Torvalds 已提交
498 499 500

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

B
Bob Moore 已提交
501
	internal_name = ACPI_ALLOCATE_ZEROED(info.length);
L
Linus Torvalds 已提交
502
	if (!internal_name) {
L
Len Brown 已提交
503
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
504 505 506 507 508
	}

	/* Build the name */

	info.internal_name = internal_name;
L
Len Brown 已提交
509 510
	status = acpi_ns_build_internal_name(&info);
	if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
511
		ACPI_FREE(internal_name);
L
Len Brown 已提交
512
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
513 514 515
	}

	*converted_name = internal_name;
L
Len Brown 已提交
516
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
517 518 519 520 521 522
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_externalize_name
 *
R
Robert Moore 已提交
523 524 525 526 527
 * 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 已提交
528 529 530 531
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
R
Robert Moore 已提交
532
 *              to its external (printable) form (e.g. "\_PR_.CPU0")
L
Linus Torvalds 已提交
533 534 535 536
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
537
acpi_ns_externalize_name(u32 internal_name_length,
538
			 const char *internal_name,
L
Len Brown 已提交
539
			 u32 * converted_name_length, char **converted_name)
L
Linus Torvalds 已提交
540
{
541 542 543 544 545 546
	u32 names_index = 0;
	u32 num_segments = 0;
	u32 required_length;
	u32 prefix_length = 0;
	u32 i = 0;
	u32 j = 0;
L
Linus Torvalds 已提交
547

B
Bob Moore 已提交
548
	ACPI_FUNCTION_TRACE(ns_externalize_name);
L
Linus Torvalds 已提交
549

L
Len Brown 已提交
550 551
	if (!internal_name_length || !internal_name || !converted_name) {
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
552 553
	}

554 555
	/* Check for a prefix (one '\' | one or more '^') */

L
Linus Torvalds 已提交
556 557 558 559 560 561 562 563 564
	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 已提交
565
			} else {
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
				break;
			}
		}

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

		break;

	default:
		break;
	}

	/*
581
	 * Check for object names. Note that there could be 0-255 of these
L
Linus Torvalds 已提交
582 583 584 585 586 587 588 589 590
	 * 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;
591 592
			num_segments = (u8)
			    internal_name[(acpi_size) prefix_length + 1];
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
			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 已提交
627
	    ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
L
Linus Torvalds 已提交
628 629 630 631 632 633

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

638 639
	/* Build the converted_name */

B
Bob Moore 已提交
640
	*converted_name = ACPI_ALLOCATE_ZEROED(required_length);
L
Linus Torvalds 已提交
641
	if (!(*converted_name)) {
L
Len Brown 已提交
642
		return_ACPI_STATUS(AE_NO_MEMORY);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	}

	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++] = '.';
			}

			(*converted_name)[j++] = internal_name[names_index++];
			(*converted_name)[j++] = internal_name[names_index++];
			(*converted_name)[j++] = internal_name[names_index++];
			(*converted_name)[j++] = internal_name[names_index++];
		}
	}

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

L
Len Brown 已提交
668
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
669 670 671 672 673 674 675 676 677 678 679 680
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_map_handle_to_node
 *
 * PARAMETERS:  Handle          - Handle to be converted to an Node
 *
 * RETURN:      A Name table entry pointer
 *
 * DESCRIPTION: Convert a namespace handle to a real Node
 *
R
Robert Moore 已提交
681 682 683
 * Note: Real integer handles would allow for more verification
 *       and keep all pointers within this subsystem - however this introduces
 *       more (and perhaps unnecessary) overhead.
L
Linus Torvalds 已提交
684
 *
685 686 687
 * The current implemenation is basically a placeholder until such time comes
 * that it is needed.
 *
L
Linus Torvalds 已提交
688 689
 ******************************************************************************/

L
Len Brown 已提交
690
struct acpi_namespace_node *acpi_ns_map_handle_to_node(acpi_handle handle)
L
Linus Torvalds 已提交
691 692
{

L
Len Brown 已提交
693
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
694

695 696
	/* Parameter validation */

B
Bob Moore 已提交
697
	if ((!handle) || (handle == ACPI_ROOT_OBJECT)) {
L
Linus Torvalds 已提交
698 699 700 701 702
		return (acpi_gbl_root_node);
	}

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

L
Len Brown 已提交
703
	if (ACPI_GET_DESCRIPTOR_TYPE(handle) != ACPI_DESC_TYPE_NAMED) {
L
Linus Torvalds 已提交
704 705 706
		return (NULL);
	}

B
Bob Moore 已提交
707
	return (ACPI_CAST_PTR(struct acpi_namespace_node, handle));
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_convert_entry_to_handle
 *
 * PARAMETERS:  Node          - Node to be converted to a Handle
 *
 * RETURN:      A user handle
 *
 * DESCRIPTION: Convert a real Node to a namespace handle
 *
 ******************************************************************************/

L
Len Brown 已提交
722
acpi_handle acpi_ns_convert_entry_to_handle(struct acpi_namespace_node *node)
L
Linus Torvalds 已提交
723 724 725 726 727 728 729
{

	/*
	 * Simple implementation for now;
	 */
	return ((acpi_handle) node);

R
Robert Moore 已提交
730
/* Example future implementation ---------------------
L
Linus Torvalds 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

	if (!Node)
	{
		return (NULL);
	}

	if (Node == acpi_gbl_root_node)
	{
		return (ACPI_ROOT_OBJECT);
	}

	return ((acpi_handle) Node);
------------------------------------------------------*/
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_terminate
 *
 * PARAMETERS:  none
 *
 * RETURN:      none
 *
R
Robert Moore 已提交
754
 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
L
Linus Torvalds 已提交
755 756 757
 *
 ******************************************************************************/

L
Len Brown 已提交
758
void acpi_ns_terminate(void)
L
Linus Torvalds 已提交
759
{
L
Len Brown 已提交
760
	union acpi_operand_object *obj_desc;
L
Linus Torvalds 已提交
761

B
Bob Moore 已提交
762
	ACPI_FUNCTION_TRACE(ns_terminate);
L
Linus Torvalds 已提交
763 764 765 766 767 768

	/*
	 * 1) Free the entire namespace -- all nodes and objects
	 *
	 * Delete all object descriptors attached to namepsace nodes
	 */
L
Len Brown 已提交
769
	acpi_ns_delete_namespace_subtree(acpi_gbl_root_node);
L
Linus Torvalds 已提交
770 771 772

	/* Detach any objects attached to the root */

L
Len Brown 已提交
773
	obj_desc = acpi_ns_get_attached_object(acpi_gbl_root_node);
L
Linus Torvalds 已提交
774
	if (obj_desc) {
L
Len Brown 已提交
775
		acpi_ns_detach_object(acpi_gbl_root_node);
L
Linus Torvalds 已提交
776 777
	}

L
Len Brown 已提交
778
	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Namespace freed\n"));
L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792
	return_VOID;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_opens_scope
 *
 * PARAMETERS:  Type        - A valid namespace type
 *
 * RETURN:      NEWSCOPE if the passed type "opens a name scope" according
 *              to the ACPI specification, else 0
 *
 ******************************************************************************/

L
Len Brown 已提交
793
u32 acpi_ns_opens_scope(acpi_object_type type)
L
Linus Torvalds 已提交
794
{
B
Bob Moore 已提交
795
	ACPI_FUNCTION_TRACE_STR(ns_opens_scope, acpi_ut_get_type_name(type));
L
Linus Torvalds 已提交
796

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

L
Linus Torvalds 已提交
799 800
		/* type code out of range  */

B
Bob Moore 已提交
801
		ACPI_WARNING((AE_INFO, "Invalid Object Type %X", type));
B
Bob Moore 已提交
802
		return_UINT32(ACPI_NS_NORMAL);
L
Linus Torvalds 已提交
803 804
	}

B
Bob Moore 已提交
805
	return_UINT32(((u32) acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
L
Linus Torvalds 已提交
806 807 808 809
}

/*******************************************************************************
 *
B
Bob Moore 已提交
810
 * FUNCTION:    acpi_ns_get_node
L
Linus Torvalds 已提交
811 812 813 814
 *
 * PARAMETERS:  *Pathname   - Name to be found, in external (ASL) format. The
 *                            \ (backslash) and ^ (carat) prefixes, and the
 *                            . (period) to separate segments are supported.
B
Bob Moore 已提交
815
 *              prefix_node  - Root of subtree to be searched, or NS_ALL for the
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
 *                            root of the name space.  If Name is fully
 *                            qualified (first s8 is '\'), the passed value
 *                            of Scope will not be accessed.
 *              Flags       - Used to indicate whether to perform upsearch or
 *                            not.
 *              return_node - Where the Node is returned
 *
 * DESCRIPTION: Look up a name relative to a given scope and return the
 *              corresponding Node.  NOTE: Scope can be null.
 *
 * MUTEX:       Locks namespace
 *
 ******************************************************************************/

acpi_status
B
Bob Moore 已提交
831
acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
832
		 const char *pathname,
B
Bob Moore 已提交
833
		 u32 flags, struct acpi_namespace_node **return_node)
L
Linus Torvalds 已提交
834
{
L
Len Brown 已提交
835 836
	union acpi_generic_state scope_info;
	acpi_status status;
B
Bob Moore 已提交
837
	char *internal_path;
L
Linus Torvalds 已提交
838

B
Bob Moore 已提交
839
	ACPI_FUNCTION_TRACE_PTR(ns_get_node, pathname);
L
Linus Torvalds 已提交
840

B
Bob Moore 已提交
841 842 843 844 845 846 847
	if (!pathname) {
		*return_node = prefix_node;
		if (!prefix_node) {
			*return_node = acpi_gbl_root_node;
		}
		return_ACPI_STATUS(AE_OK);
	}
B
Bob Moore 已提交
848

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

B
Bob Moore 已提交
851 852 853
	status = acpi_ns_internalize_name(pathname, &internal_path);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
854 855 856 857
	}

	/* Must lock namespace during lookup */

L
Len Brown 已提交
858 859
	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
	if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
860 861 862 863 864
		goto cleanup;
	}

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

B
Bob Moore 已提交
865
	scope_info.scope.node = prefix_node;
L
Linus Torvalds 已提交
866 867 868

	/* Lookup the name in the namespace */

B
Bob Moore 已提交
869 870 871 872
	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 已提交
873
	if (ACPI_FAILURE(status)) {
874
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s, %s\n",
B
Bob Moore 已提交
875
				  pathname, acpi_format_exception(status)));
L
Linus Torvalds 已提交
876 877
	}

L
Len Brown 已提交
878
	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
L
Linus Torvalds 已提交
879

L
Len Brown 已提交
880
      cleanup:
B
Bob Moore 已提交
881
	ACPI_FREE(internal_path);
L
Len Brown 已提交
882
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_parent_node
 *
 * PARAMETERS:  Node       - Current table entry
 *
 * RETURN:      Parent entry of the given entry
 *
 * DESCRIPTION: Obtain the parent entry for a given entry in the namespace.
 *
 ******************************************************************************/

L
Len Brown 已提交
897 898
struct acpi_namespace_node *acpi_ns_get_parent_node(struct acpi_namespace_node
						    *node)
L
Linus Torvalds 已提交
899
{
L
Len Brown 已提交
900
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

	if (!node) {
		return (NULL);
	}

	/*
	 * Walk to the end of this peer list. The last entry is marked with a flag
	 * and the peer pointer is really a pointer back to the parent. This saves
	 * putting a parent back pointer in each and every named object!
	 */
	while (!(node->flags & ANOBJ_END_OF_PEER_LIST)) {
		node = node->peer;
	}

	return (node->peer);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_get_next_valid_node
 *
 * PARAMETERS:  Node       - Current table entry
 *
 * RETURN:      Next valid Node in the linked node list. NULL if no more valid
 *              nodes.
 *
 * DESCRIPTION: Find the next valid node within a name table.
 *              Useful for implementing NULL-end-of-list loops.
 *
 ******************************************************************************/

L
Len Brown 已提交
932 933 934
struct acpi_namespace_node *acpi_ns_get_next_valid_node(struct
							acpi_namespace_node
							*node)
L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947
{

	/* If we are at the end of this peer list, return NULL */

	if (node->flags & ANOBJ_END_OF_PEER_LIST) {
		return NULL;
	}

	/* Otherwise just return the next peer */

	return (node->peer);
}

R
Robert Moore 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
#ifdef ACPI_OBSOLETE_FUNCTIONS
/*******************************************************************************
 *
 * FUNCTION:    acpi_ns_find_parent_name
 *
 * PARAMETERS:  *child_node            - Named Obj whose name is to be found
 *
 * RETURN:      The ACPI name
 *
 * DESCRIPTION: Search for the given obj in its parent scope and return the
 *              name segment, or "????" if the parent name can't be found
 *              (which "should not happen").
 *
 ******************************************************************************/

L
Len Brown 已提交
963
acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node * child_node)
R
Robert Moore 已提交
964
{
L
Len Brown 已提交
965
	struct acpi_namespace_node *parent_node;
R
Robert Moore 已提交
966

B
Bob Moore 已提交
967
	ACPI_FUNCTION_TRACE(ns_find_parent_name);
R
Robert Moore 已提交
968 969

	if (child_node) {
B
Bob Moore 已提交
970

R
Robert Moore 已提交
971 972
		/* Valid entry.  Get the parent Node */

L
Len Brown 已提交
973
		parent_node = acpi_ns_get_parent_node(child_node);
R
Robert Moore 已提交
974
		if (parent_node) {
L
Len Brown 已提交
975 976 977 978 979 980
			ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
					  "Parent of %p [%4.4s] is %p [%4.4s]\n",
					  child_node,
					  acpi_ut_get_node_name(child_node),
					  parent_node,
					  acpi_ut_get_node_name(parent_node)));
R
Robert Moore 已提交
981 982

			if (parent_node->name.integer) {
L
Len Brown 已提交
983 984
				return_VALUE((acpi_name) parent_node->name.
					     integer);
R
Robert Moore 已提交
985 986 987
			}
		}

L
Len Brown 已提交
988 989 990 991
		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
				  "Unable to find parent of %p (%4.4s)\n",
				  child_node,
				  acpi_ut_get_node_name(child_node)));
R
Robert Moore 已提交
992 993
	}

L
Len Brown 已提交
994
	return_VALUE(ACPI_UNKNOWN_NAME);
R
Robert Moore 已提交
995 996
}
#endif