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