Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / acpi / namespace / nsnames.c
1 /*******************************************************************************
2  *
3  * Module Name: nsnames - Name manipulation and search
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/acnamesp.h>
48
49
50 #define _COMPONENT          ACPI_NAMESPACE
51          ACPI_MODULE_NAME    ("nsnames")
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    acpi_ns_build_external_path
57  *
58  * PARAMETERS:  Node            - NS node whose pathname is needed
59  *              Size            - Size of the pathname
60  *              *name_buffer    - Where to return the pathname
61  *
62  * RETURN:      Places the pathname into the name_buffer, in external format
63  *              (name segments separated by path separators)
64  *
65  * DESCRIPTION: Generate a full pathaname
66  *
67  ******************************************************************************/
68
69 void
70 acpi_ns_build_external_path (
71         struct acpi_namespace_node      *node,
72         acpi_size                       size,
73         char                            *name_buffer)
74 {
75         acpi_size                       index;
76         struct acpi_namespace_node      *parent_node;
77
78
79         ACPI_FUNCTION_NAME ("ns_build_external_path");
80
81
82         /* Special case for root */
83
84         index = size - 1;
85         if (index < ACPI_NAME_SIZE) {
86                 name_buffer[0] = AML_ROOT_PREFIX;
87                 name_buffer[1] = 0;
88                 return;
89         }
90
91         /* Store terminator byte, then build name backwards */
92
93         parent_node = node;
94         name_buffer[index] = 0;
95
96         while ((index > ACPI_NAME_SIZE) && (parent_node != acpi_gbl_root_node)) {
97                 index -= ACPI_NAME_SIZE;
98
99                 /* Put the name into the buffer */
100
101                 ACPI_MOVE_32_TO_32 ((name_buffer + index), &parent_node->name);
102                 parent_node = acpi_ns_get_parent_node (parent_node);
103
104                 /* Prefix name with the path separator */
105
106                 index--;
107                 name_buffer[index] = ACPI_PATH_SEPARATOR;
108         }
109
110         /* Overwrite final separator with the root prefix character */
111
112         name_buffer[index] = AML_ROOT_PREFIX;
113
114         if (index != 0) {
115                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
116                         "Could not construct pathname; index=%X, size=%X, Path=%s\n",
117                         (u32) index, (u32) size, &name_buffer[size]));
118         }
119
120         return;
121 }
122
123
124 #ifdef ACPI_DEBUG_OUTPUT
125 /*******************************************************************************
126  *
127  * FUNCTION:    acpi_ns_get_external_pathname
128  *
129  * PARAMETERS:  Node            - NS node whose pathname is needed
130  *
131  * RETURN:      Pointer to storage containing the fully qualified name of
132  *              the node, In external format (name segments separated by path
133  *              separators.)
134  *
135  * DESCRIPTION: Used for debug printing in acpi_ns_search_table().
136  *
137  ******************************************************************************/
138
139 char *
140 acpi_ns_get_external_pathname (
141         struct acpi_namespace_node      *node)
142 {
143         char                            *name_buffer;
144         acpi_size                       size;
145
146
147         ACPI_FUNCTION_TRACE_PTR ("ns_get_external_pathname", node);
148
149
150         /* Calculate required buffer size based on depth below root */
151
152         size = acpi_ns_get_pathname_length (node);
153
154         /* Allocate a buffer to be returned to caller */
155
156         name_buffer = ACPI_MEM_CALLOCATE (size);
157         if (!name_buffer) {
158                 ACPI_REPORT_ERROR (("ns_get_table_pathname: allocation failure\n"));
159                 return_PTR (NULL);
160         }
161
162         /* Build the path in the allocated buffer */
163
164         acpi_ns_build_external_path (node, size, name_buffer);
165         return_PTR (name_buffer);
166 }
167 #endif
168
169
170 /*******************************************************************************
171  *
172  * FUNCTION:    acpi_ns_get_pathname_length
173  *
174  * PARAMETERS:  Node        - Namespace node
175  *
176  * RETURN:      Length of path, including prefix
177  *
178  * DESCRIPTION: Get the length of the pathname string for this node
179  *
180  ******************************************************************************/
181
182 acpi_size
183 acpi_ns_get_pathname_length (
184         struct acpi_namespace_node      *node)
185 {
186         acpi_size                       size;
187         struct acpi_namespace_node      *next_node;
188
189
190         ACPI_FUNCTION_ENTRY ();
191
192
193         /*
194          * Compute length of pathname as 5 * number of name segments.
195          * Go back up the parent tree to the root
196          */
197         size = 0;
198         next_node = node;
199
200         while (next_node && (next_node != acpi_gbl_root_node)) {
201                 size += ACPI_PATH_SEGMENT_LENGTH;
202                 next_node = acpi_ns_get_parent_node (next_node);
203         }
204
205         if (!size) {
206                 size = 1;       /* Root node case */
207         }
208
209         return (size + 1);  /* +1 for null string terminator */
210 }
211
212
213 /*******************************************************************************
214  *
215  * FUNCTION:    acpi_ns_handle_to_pathname
216  *
217  * PARAMETERS:  target_handle           - Handle of named object whose name is
218  *                                        to be found
219  *              Buffer                  - Where the pathname is returned
220  *
221  * RETURN:      Status, Buffer is filled with pathname if status is AE_OK
222  *
223  * DESCRIPTION: Build and return a full namespace pathname
224  *
225  ******************************************************************************/
226
227 acpi_status
228 acpi_ns_handle_to_pathname (
229         acpi_handle                     target_handle,
230         struct acpi_buffer              *buffer)
231 {
232         acpi_status                     status;
233         struct acpi_namespace_node      *node;
234         acpi_size                       required_size;
235
236
237         ACPI_FUNCTION_TRACE_PTR ("ns_handle_to_pathname", target_handle);
238
239
240         node = acpi_ns_map_handle_to_node (target_handle);
241         if (!node) {
242                 return_ACPI_STATUS (AE_BAD_PARAMETER);
243         }
244
245         /* Determine size required for the caller buffer */
246
247         required_size = acpi_ns_get_pathname_length (node);
248
249         /* Validate/Allocate/Clear caller buffer */
250
251         status = acpi_ut_initialize_buffer (buffer, required_size);
252         if (ACPI_FAILURE (status)) {
253                 return_ACPI_STATUS (status);
254         }
255
256         /* Build the path in the caller buffer */
257
258         acpi_ns_build_external_path (node, required_size, buffer->pointer);
259
260         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "%s [%X] \n",
261                 (char *) buffer->pointer, (u32) required_size));
262         return_ACPI_STATUS (AE_OK);
263 }
264
265