Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / namespace / nsnames.c
blob4b32b30ce89dcfad10b0f00fa72ae0d8f4079eb9
1 /*******************************************************************************
3 * Module Name: nsnames - Name manipulation and search
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
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.
44 #define __NSNAMES_C__
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "amlcode.h"
49 #include "acnamesp.h"
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsnames")
56 /*******************************************************************************
58 * FUNCTION: AcpiNsBuildExternalPath
60 * PARAMETERS: Node - NS node whose pathname is needed
61 * Size - Size of the pathname
62 * *NameBuffer - Where to return the pathname
64 * RETURN: Status
65 * Places the pathname into the NameBuffer, in external format
66 * (name segments separated by path separators)
68 * DESCRIPTION: Generate a full pathaname
70 ******************************************************************************/
72 ACPI_STATUS
73 AcpiNsBuildExternalPath (
74 ACPI_NAMESPACE_NODE *Node,
75 ACPI_SIZE Size,
76 char *NameBuffer)
78 ACPI_SIZE Index;
79 ACPI_NAMESPACE_NODE *ParentNode;
82 ACPI_FUNCTION_ENTRY ();
85 /* Special case for root */
87 Index = Size - 1;
88 if (Index < ACPI_NAME_SIZE)
90 NameBuffer[0] = AML_ROOT_PREFIX;
91 NameBuffer[1] = 0;
92 return (AE_OK);
95 /* Store terminator byte, then build name backwards */
97 ParentNode = Node;
98 NameBuffer[Index] = 0;
100 while ((Index > ACPI_NAME_SIZE) && (ParentNode != AcpiGbl_RootNode))
102 Index -= ACPI_NAME_SIZE;
104 /* Put the name into the buffer */
106 ACPI_MOVE_32_TO_32 ((NameBuffer + Index), &ParentNode->Name);
107 ParentNode = ParentNode->Parent;
109 /* Prefix name with the path separator */
111 Index--;
112 NameBuffer[Index] = ACPI_PATH_SEPARATOR;
115 /* Overwrite final separator with the root prefix character */
117 NameBuffer[Index] = AML_ROOT_PREFIX;
119 if (Index != 0)
121 ACPI_ERROR ((AE_INFO,
122 "Could not construct external pathname; index=%u, size=%u, Path=%s",
123 (UINT32) Index, (UINT32) Size, &NameBuffer[Size]));
125 return (AE_BAD_PARAMETER);
128 return (AE_OK);
132 /*******************************************************************************
134 * FUNCTION: AcpiNsGetExternalPathname
136 * PARAMETERS: Node - Namespace node whose pathname is needed
138 * RETURN: Pointer to storage containing the fully qualified name of
139 * the node, In external format (name segments separated by path
140 * separators.)
142 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
143 * for error and debug statements.
145 ******************************************************************************/
147 char *
148 AcpiNsGetExternalPathname (
149 ACPI_NAMESPACE_NODE *Node)
151 ACPI_STATUS Status;
152 char *NameBuffer;
153 ACPI_SIZE Size;
156 ACPI_FUNCTION_TRACE_PTR (NsGetExternalPathname, Node);
159 /* Calculate required buffer size based on depth below root */
161 Size = AcpiNsGetPathnameLength (Node);
162 if (!Size)
164 return_PTR (NULL);
167 /* Allocate a buffer to be returned to caller */
169 NameBuffer = ACPI_ALLOCATE_ZEROED (Size);
170 if (!NameBuffer)
172 ACPI_ERROR ((AE_INFO, "Could not allocate %u bytes", (UINT32) Size));
173 return_PTR (NULL);
176 /* Build the path in the allocated buffer */
178 Status = AcpiNsBuildExternalPath (Node, Size, NameBuffer);
179 if (ACPI_FAILURE (Status))
181 ACPI_FREE (NameBuffer);
182 return_PTR (NULL);
185 return_PTR (NameBuffer);
189 /*******************************************************************************
191 * FUNCTION: AcpiNsGetPathnameLength
193 * PARAMETERS: Node - Namespace node
195 * RETURN: Length of path, including prefix
197 * DESCRIPTION: Get the length of the pathname string for this node
199 ******************************************************************************/
201 ACPI_SIZE
202 AcpiNsGetPathnameLength (
203 ACPI_NAMESPACE_NODE *Node)
205 ACPI_SIZE Size;
206 ACPI_NAMESPACE_NODE *NextNode;
209 ACPI_FUNCTION_ENTRY ();
213 * Compute length of pathname as 5 * number of name segments.
214 * Go back up the parent tree to the root
216 Size = 0;
217 NextNode = Node;
219 while (NextNode && (NextNode != AcpiGbl_RootNode))
221 if (ACPI_GET_DESCRIPTOR_TYPE (NextNode) != ACPI_DESC_TYPE_NAMED)
223 ACPI_ERROR ((AE_INFO,
224 "Invalid Namespace Node (%p) while traversing namespace",
225 NextNode));
226 return (0);
228 Size += ACPI_PATH_SEGMENT_LENGTH;
229 NextNode = NextNode->Parent;
232 if (!Size)
234 Size = 1; /* Root node case */
237 return (Size + 1); /* +1 for null string terminator */
241 /*******************************************************************************
243 * FUNCTION: AcpiNsHandleToPathname
245 * PARAMETERS: TargetHandle - Handle of named object whose name is
246 * to be found
247 * Buffer - Where the pathname is returned
249 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
251 * DESCRIPTION: Build and return a full namespace pathname
253 ******************************************************************************/
255 ACPI_STATUS
256 AcpiNsHandleToPathname (
257 ACPI_HANDLE TargetHandle,
258 ACPI_BUFFER *Buffer)
260 ACPI_STATUS Status;
261 ACPI_NAMESPACE_NODE *Node;
262 ACPI_SIZE RequiredSize;
265 ACPI_FUNCTION_TRACE_PTR (NsHandleToPathname, TargetHandle);
268 Node = AcpiNsValidateHandle (TargetHandle);
269 if (!Node)
271 return_ACPI_STATUS (AE_BAD_PARAMETER);
274 /* Determine size required for the caller buffer */
276 RequiredSize = AcpiNsGetPathnameLength (Node);
277 if (!RequiredSize)
279 return_ACPI_STATUS (AE_BAD_PARAMETER);
282 /* Validate/Allocate/Clear caller buffer */
284 Status = AcpiUtInitializeBuffer (Buffer, RequiredSize);
285 if (ACPI_FAILURE (Status))
287 return_ACPI_STATUS (Status);
290 /* Build the path in the caller buffer */
292 Status = AcpiNsBuildExternalPath (Node, RequiredSize, Buffer->Pointer);
293 if (ACPI_FAILURE (Status))
295 return_ACPI_STATUS (Status);
298 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X]\n",
299 (char *) Buffer->Pointer, (UINT32) RequiredSize));
300 return_ACPI_STATUS (AE_OK);