xref: /minix3/minix/drivers/power/acpi/namespace/nsnames.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /*******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: nsnames - Name manipulation and search
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  ******************************************************************************/
6433d6423SLionel Sambuc 
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek  * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc  * All rights reserved.
10433d6423SLionel Sambuc  *
11*29492bb7SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek  * are met:
14*29492bb7SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek  *    notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek  *    without modification.
17*29492bb7SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek  *    including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek  *    binary redistribution.
22*29492bb7SDavid van Moolenbroek  * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek  *    of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek  *    from this software without specific prior written permission.
25433d6423SLionel Sambuc  *
26*29492bb7SDavid van Moolenbroek  * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek  * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek  * Software Foundation.
29433d6423SLionel Sambuc  *
30*29492bb7SDavid van Moolenbroek  * NO WARRANTY
31*29492bb7SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek  * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek  */
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "amlcode.h"
47433d6423SLionel Sambuc #include "acnamesp.h"
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc #define _COMPONENT          ACPI_NAMESPACE
51433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("nsnames")
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc /*******************************************************************************
55433d6423SLionel Sambuc  *
56433d6423SLionel Sambuc  * FUNCTION:    AcpiNsBuildExternalPath
57433d6423SLionel Sambuc  *
58433d6423SLionel Sambuc  * PARAMETERS:  Node            - NS node whose pathname is needed
59433d6423SLionel Sambuc  *              Size            - Size of the pathname
60433d6423SLionel Sambuc  *              *NameBuffer     - Where to return the pathname
61433d6423SLionel Sambuc  *
62433d6423SLionel Sambuc  * RETURN:      Status
63433d6423SLionel Sambuc  *              Places the pathname into the NameBuffer, in external format
64433d6423SLionel Sambuc  *              (name segments separated by path separators)
65433d6423SLionel Sambuc  *
66433d6423SLionel Sambuc  * DESCRIPTION: Generate a full pathaname
67433d6423SLionel Sambuc  *
68433d6423SLionel Sambuc  ******************************************************************************/
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc ACPI_STATUS
AcpiNsBuildExternalPath(ACPI_NAMESPACE_NODE * Node,ACPI_SIZE Size,char * NameBuffer)71433d6423SLionel Sambuc AcpiNsBuildExternalPath (
72433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *Node,
73433d6423SLionel Sambuc     ACPI_SIZE               Size,
74433d6423SLionel Sambuc     char                    *NameBuffer)
75433d6423SLionel Sambuc {
76433d6423SLionel Sambuc     ACPI_SIZE               Index;
77433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParentNode;
78433d6423SLionel Sambuc 
79433d6423SLionel Sambuc 
80433d6423SLionel Sambuc     ACPI_FUNCTION_ENTRY ();
81433d6423SLionel Sambuc 
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc     /* Special case for root */
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc     Index = Size - 1;
86433d6423SLionel Sambuc     if (Index < ACPI_NAME_SIZE)
87433d6423SLionel Sambuc     {
88433d6423SLionel Sambuc         NameBuffer[0] = AML_ROOT_PREFIX;
89433d6423SLionel Sambuc         NameBuffer[1] = 0;
90433d6423SLionel Sambuc         return (AE_OK);
91433d6423SLionel Sambuc     }
92433d6423SLionel Sambuc 
93433d6423SLionel Sambuc     /* Store terminator byte, then build name backwards */
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc     ParentNode = Node;
96433d6423SLionel Sambuc     NameBuffer[Index] = 0;
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc     while ((Index > ACPI_NAME_SIZE) && (ParentNode != AcpiGbl_RootNode))
99433d6423SLionel Sambuc     {
100433d6423SLionel Sambuc         Index -= ACPI_NAME_SIZE;
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc         /* Put the name into the buffer */
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc         ACPI_MOVE_32_TO_32 ((NameBuffer + Index), &ParentNode->Name);
105433d6423SLionel Sambuc         ParentNode = ParentNode->Parent;
106433d6423SLionel Sambuc 
107433d6423SLionel Sambuc         /* Prefix name with the path separator */
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc         Index--;
110433d6423SLionel Sambuc         NameBuffer[Index] = ACPI_PATH_SEPARATOR;
111433d6423SLionel Sambuc     }
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc     /* Overwrite final separator with the root prefix character */
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc     NameBuffer[Index] = AML_ROOT_PREFIX;
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc     if (Index != 0)
118433d6423SLionel Sambuc     {
119433d6423SLionel Sambuc         ACPI_ERROR ((AE_INFO,
120433d6423SLionel Sambuc             "Could not construct external pathname; index=%u, size=%u, Path=%s",
121433d6423SLionel Sambuc             (UINT32) Index, (UINT32) Size, &NameBuffer[Size]));
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc         return (AE_BAD_PARAMETER);
124433d6423SLionel Sambuc     }
125433d6423SLionel Sambuc 
126433d6423SLionel Sambuc     return (AE_OK);
127433d6423SLionel Sambuc }
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc 
130433d6423SLionel Sambuc /*******************************************************************************
131433d6423SLionel Sambuc  *
132433d6423SLionel Sambuc  * FUNCTION:    AcpiNsGetExternalPathname
133433d6423SLionel Sambuc  *
134433d6423SLionel Sambuc  * PARAMETERS:  Node            - Namespace node whose pathname is needed
135433d6423SLionel Sambuc  *
136433d6423SLionel Sambuc  * RETURN:      Pointer to storage containing the fully qualified name of
137433d6423SLionel Sambuc  *              the node, In external format (name segments separated by path
138433d6423SLionel Sambuc  *              separators.)
139433d6423SLionel Sambuc  *
140433d6423SLionel Sambuc  * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
141433d6423SLionel Sambuc  *              for error and debug statements.
142433d6423SLionel Sambuc  *
143433d6423SLionel Sambuc  ******************************************************************************/
144433d6423SLionel Sambuc 
145433d6423SLionel Sambuc char *
AcpiNsGetExternalPathname(ACPI_NAMESPACE_NODE * Node)146433d6423SLionel Sambuc AcpiNsGetExternalPathname (
147433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *Node)
148433d6423SLionel Sambuc {
149433d6423SLionel Sambuc     ACPI_STATUS             Status;
150433d6423SLionel Sambuc     char                    *NameBuffer;
151433d6423SLionel Sambuc     ACPI_SIZE               Size;
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc 
154433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (NsGetExternalPathname, Node);
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc     /* Calculate required buffer size based on depth below root */
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc     Size = AcpiNsGetPathnameLength (Node);
160433d6423SLionel Sambuc     if (!Size)
161433d6423SLionel Sambuc     {
162433d6423SLionel Sambuc         return_PTR (NULL);
163433d6423SLionel Sambuc     }
164433d6423SLionel Sambuc 
165433d6423SLionel Sambuc     /* Allocate a buffer to be returned to caller */
166433d6423SLionel Sambuc 
167433d6423SLionel Sambuc     NameBuffer = ACPI_ALLOCATE_ZEROED (Size);
168433d6423SLionel Sambuc     if (!NameBuffer)
169433d6423SLionel Sambuc     {
170433d6423SLionel Sambuc         ACPI_ERROR ((AE_INFO, "Could not allocate %u bytes", (UINT32) Size));
171433d6423SLionel Sambuc         return_PTR (NULL);
172433d6423SLionel Sambuc     }
173433d6423SLionel Sambuc 
174433d6423SLionel Sambuc     /* Build the path in the allocated buffer */
175433d6423SLionel Sambuc 
176433d6423SLionel Sambuc     Status = AcpiNsBuildExternalPath (Node, Size, NameBuffer);
177433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
178433d6423SLionel Sambuc     {
179433d6423SLionel Sambuc         ACPI_FREE (NameBuffer);
180433d6423SLionel Sambuc         return_PTR (NULL);
181433d6423SLionel Sambuc     }
182433d6423SLionel Sambuc 
183433d6423SLionel Sambuc     return_PTR (NameBuffer);
184433d6423SLionel Sambuc }
185433d6423SLionel Sambuc 
186433d6423SLionel Sambuc 
187433d6423SLionel Sambuc /*******************************************************************************
188433d6423SLionel Sambuc  *
189433d6423SLionel Sambuc  * FUNCTION:    AcpiNsGetPathnameLength
190433d6423SLionel Sambuc  *
191433d6423SLionel Sambuc  * PARAMETERS:  Node        - Namespace node
192433d6423SLionel Sambuc  *
193433d6423SLionel Sambuc  * RETURN:      Length of path, including prefix
194433d6423SLionel Sambuc  *
195433d6423SLionel Sambuc  * DESCRIPTION: Get the length of the pathname string for this node
196433d6423SLionel Sambuc  *
197433d6423SLionel Sambuc  ******************************************************************************/
198433d6423SLionel Sambuc 
199433d6423SLionel Sambuc ACPI_SIZE
AcpiNsGetPathnameLength(ACPI_NAMESPACE_NODE * Node)200433d6423SLionel Sambuc AcpiNsGetPathnameLength (
201433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *Node)
202433d6423SLionel Sambuc {
203433d6423SLionel Sambuc     ACPI_SIZE               Size;
204433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *NextNode;
205433d6423SLionel Sambuc 
206433d6423SLionel Sambuc 
207433d6423SLionel Sambuc     ACPI_FUNCTION_ENTRY ();
208433d6423SLionel Sambuc 
209433d6423SLionel Sambuc 
210433d6423SLionel Sambuc     /*
211433d6423SLionel Sambuc      * Compute length of pathname as 5 * number of name segments.
212433d6423SLionel Sambuc      * Go back up the parent tree to the root
213433d6423SLionel Sambuc      */
214433d6423SLionel Sambuc     Size = 0;
215433d6423SLionel Sambuc     NextNode = Node;
216433d6423SLionel Sambuc 
217433d6423SLionel Sambuc     while (NextNode && (NextNode != AcpiGbl_RootNode))
218433d6423SLionel Sambuc     {
219433d6423SLionel Sambuc         if (ACPI_GET_DESCRIPTOR_TYPE (NextNode) != ACPI_DESC_TYPE_NAMED)
220433d6423SLionel Sambuc         {
221433d6423SLionel Sambuc             ACPI_ERROR ((AE_INFO,
222433d6423SLionel Sambuc                 "Invalid Namespace Node (%p) while traversing namespace",
223433d6423SLionel Sambuc                 NextNode));
224*29492bb7SDavid van Moolenbroek             return (0);
225433d6423SLionel Sambuc         }
226433d6423SLionel Sambuc         Size += ACPI_PATH_SEGMENT_LENGTH;
227433d6423SLionel Sambuc         NextNode = NextNode->Parent;
228433d6423SLionel Sambuc     }
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc     if (!Size)
231433d6423SLionel Sambuc     {
232433d6423SLionel Sambuc         Size = 1; /* Root node case */
233433d6423SLionel Sambuc     }
234433d6423SLionel Sambuc 
235433d6423SLionel Sambuc     return (Size + 1);  /* +1 for null string terminator */
236433d6423SLionel Sambuc }
237433d6423SLionel Sambuc 
238433d6423SLionel Sambuc 
239433d6423SLionel Sambuc /*******************************************************************************
240433d6423SLionel Sambuc  *
241433d6423SLionel Sambuc  * FUNCTION:    AcpiNsHandleToPathname
242433d6423SLionel Sambuc  *
243433d6423SLionel Sambuc  * PARAMETERS:  TargetHandle            - Handle of named object whose name is
244433d6423SLionel Sambuc  *                                        to be found
245433d6423SLionel Sambuc  *              Buffer                  - Where the pathname is returned
246433d6423SLionel Sambuc  *
247433d6423SLionel Sambuc  * RETURN:      Status, Buffer is filled with pathname if status is AE_OK
248433d6423SLionel Sambuc  *
249433d6423SLionel Sambuc  * DESCRIPTION: Build and return a full namespace pathname
250433d6423SLionel Sambuc  *
251433d6423SLionel Sambuc  ******************************************************************************/
252433d6423SLionel Sambuc 
253433d6423SLionel Sambuc ACPI_STATUS
AcpiNsHandleToPathname(ACPI_HANDLE TargetHandle,ACPI_BUFFER * Buffer)254433d6423SLionel Sambuc AcpiNsHandleToPathname (
255433d6423SLionel Sambuc     ACPI_HANDLE             TargetHandle,
256433d6423SLionel Sambuc     ACPI_BUFFER             *Buffer)
257433d6423SLionel Sambuc {
258433d6423SLionel Sambuc     ACPI_STATUS             Status;
259433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *Node;
260433d6423SLionel Sambuc     ACPI_SIZE               RequiredSize;
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc 
263433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (NsHandleToPathname, TargetHandle);
264433d6423SLionel Sambuc 
265433d6423SLionel Sambuc 
266433d6423SLionel Sambuc     Node = AcpiNsValidateHandle (TargetHandle);
267433d6423SLionel Sambuc     if (!Node)
268433d6423SLionel Sambuc     {
269433d6423SLionel Sambuc         return_ACPI_STATUS (AE_BAD_PARAMETER);
270433d6423SLionel Sambuc     }
271433d6423SLionel Sambuc 
272433d6423SLionel Sambuc     /* Determine size required for the caller buffer */
273433d6423SLionel Sambuc 
274433d6423SLionel Sambuc     RequiredSize = AcpiNsGetPathnameLength (Node);
275433d6423SLionel Sambuc     if (!RequiredSize)
276433d6423SLionel Sambuc     {
277433d6423SLionel Sambuc         return_ACPI_STATUS (AE_BAD_PARAMETER);
278433d6423SLionel Sambuc     }
279433d6423SLionel Sambuc 
280433d6423SLionel Sambuc     /* Validate/Allocate/Clear caller buffer */
281433d6423SLionel Sambuc 
282433d6423SLionel Sambuc     Status = AcpiUtInitializeBuffer (Buffer, RequiredSize);
283433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
284433d6423SLionel Sambuc     {
285433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
286433d6423SLionel Sambuc     }
287433d6423SLionel Sambuc 
288433d6423SLionel Sambuc     /* Build the path in the caller buffer */
289433d6423SLionel Sambuc 
290433d6423SLionel Sambuc     Status = AcpiNsBuildExternalPath (Node, RequiredSize, Buffer->Pointer);
291433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
292433d6423SLionel Sambuc     {
293433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
294433d6423SLionel Sambuc     }
295433d6423SLionel Sambuc 
296433d6423SLionel Sambuc     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X]\n",
297433d6423SLionel Sambuc         (char *) Buffer->Pointer, (UINT32) RequiredSize));
298433d6423SLionel Sambuc     return_ACPI_STATUS (AE_OK);
299433d6423SLionel Sambuc }
300