ACPICA 20050708 from Bob Moore <robert.moore@intel.com>
[linux-2.6/verdex.git] / drivers / acpi / namespace / nsnames.c
blobd8ce7e39795fde0a5450b7c07859c6115c568530
1 /*******************************************************************************
3 * Module Name: nsnames - Name manipulation and search
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/amlcode.h>
47 #include <acpi/acnamesp.h>
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsnames")
53 /* Local prototypes */
55 static void
56 acpi_ns_build_external_path (
57 struct acpi_namespace_node *node,
58 acpi_size size,
59 char *name_buffer);
62 /*******************************************************************************
64 * FUNCTION: acpi_ns_build_external_path
66 * PARAMETERS: Node - NS node whose pathname is needed
67 * Size - Size of the pathname
68 * *name_buffer - Where to return the pathname
70 * RETURN: Places the pathname into the name_buffer, in external format
71 * (name segments separated by path separators)
73 * DESCRIPTION: Generate a full pathaname
75 ******************************************************************************/
77 static void
78 acpi_ns_build_external_path (
79 struct acpi_namespace_node *node,
80 acpi_size size,
81 char *name_buffer)
83 acpi_size index;
84 struct acpi_namespace_node *parent_node;
87 ACPI_FUNCTION_NAME ("ns_build_external_path");
90 /* Special case for root */
92 index = size - 1;
93 if (index < ACPI_NAME_SIZE) {
94 name_buffer[0] = AML_ROOT_PREFIX;
95 name_buffer[1] = 0;
96 return;
99 /* Store terminator byte, then build name backwards */
101 parent_node = node;
102 name_buffer[index] = 0;
104 while ((index > ACPI_NAME_SIZE) && (parent_node != acpi_gbl_root_node)) {
105 index -= ACPI_NAME_SIZE;
107 /* Put the name into the buffer */
109 ACPI_MOVE_32_TO_32 ((name_buffer + index), &parent_node->name);
110 parent_node = acpi_ns_get_parent_node (parent_node);
112 /* Prefix name with the path separator */
114 index--;
115 name_buffer[index] = ACPI_PATH_SEPARATOR;
118 /* Overwrite final separator with the root prefix character */
120 name_buffer[index] = AML_ROOT_PREFIX;
122 if (index != 0) {
123 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
124 "Could not construct pathname; index=%X, size=%X, Path=%s\n",
125 (u32) index, (u32) size, &name_buffer[size]));
128 return;
132 #ifdef ACPI_DEBUG_OUTPUT
133 /*******************************************************************************
135 * FUNCTION: acpi_ns_get_external_pathname
137 * PARAMETERS: Node - Namespace node whose pathname is needed
139 * RETURN: Pointer to storage containing the fully qualified name of
140 * the node, In external format (name segments separated by path
141 * separators.)
143 * DESCRIPTION: Used for debug printing in acpi_ns_search_table().
145 ******************************************************************************/
147 char *
148 acpi_ns_get_external_pathname (
149 struct acpi_namespace_node *node)
151 char *name_buffer;
152 acpi_size size;
155 ACPI_FUNCTION_TRACE_PTR ("ns_get_external_pathname", node);
158 /* Calculate required buffer size based on depth below root */
160 size = acpi_ns_get_pathname_length (node);
162 /* Allocate a buffer to be returned to caller */
164 name_buffer = ACPI_MEM_CALLOCATE (size);
165 if (!name_buffer) {
166 ACPI_REPORT_ERROR (("ns_get_table_pathname: allocation failure\n"));
167 return_PTR (NULL);
170 /* Build the path in the allocated buffer */
172 acpi_ns_build_external_path (node, size, name_buffer);
173 return_PTR (name_buffer);
175 #endif
178 /*******************************************************************************
180 * FUNCTION: acpi_ns_get_pathname_length
182 * PARAMETERS: Node - Namespace node
184 * RETURN: Length of path, including prefix
186 * DESCRIPTION: Get the length of the pathname string for this node
188 ******************************************************************************/
190 acpi_size
191 acpi_ns_get_pathname_length (
192 struct acpi_namespace_node *node)
194 acpi_size size;
195 struct acpi_namespace_node *next_node;
198 ACPI_FUNCTION_ENTRY ();
202 * Compute length of pathname as 5 * number of name segments.
203 * Go back up the parent tree to the root
205 size = 0;
206 next_node = node;
208 while (next_node && (next_node != acpi_gbl_root_node)) {
209 size += ACPI_PATH_SEGMENT_LENGTH;
210 next_node = acpi_ns_get_parent_node (next_node);
213 if (!size) {
214 size = 1; /* Root node case */
217 return (size + 1); /* +1 for null string terminator */
221 /*******************************************************************************
223 * FUNCTION: acpi_ns_handle_to_pathname
225 * PARAMETERS: target_handle - Handle of named object whose name is
226 * to be found
227 * Buffer - Where the pathname is returned
229 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
231 * DESCRIPTION: Build and return a full namespace pathname
233 ******************************************************************************/
235 acpi_status
236 acpi_ns_handle_to_pathname (
237 acpi_handle target_handle,
238 struct acpi_buffer *buffer)
240 acpi_status status;
241 struct acpi_namespace_node *node;
242 acpi_size required_size;
245 ACPI_FUNCTION_TRACE_PTR ("ns_handle_to_pathname", target_handle);
248 node = acpi_ns_map_handle_to_node (target_handle);
249 if (!node) {
250 return_ACPI_STATUS (AE_BAD_PARAMETER);
253 /* Determine size required for the caller buffer */
255 required_size = acpi_ns_get_pathname_length (node);
257 /* Validate/Allocate/Clear caller buffer */
259 status = acpi_ut_initialize_buffer (buffer, required_size);
260 if (ACPI_FAILURE (status)) {
261 return_ACPI_STATUS (status);
264 /* Build the path in the caller buffer */
266 acpi_ns_build_external_path (node, required_size, buffer->pointer);
268 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X] \n",
269 (char *) buffer->pointer, (u32) required_size));
270 return_ACPI_STATUS (AE_OK);