Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / acpi / acpica / nsnames.c
blobc686eda7ca66f703f6bad73eb97d0227f8c9d607
1 /*******************************************************************************
3 * Module Name: nsnames - Name manipulation and search
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2018, 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 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "amlcode.h"
47 #include "acnamesp.h"
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsnames")
52 /* Local Prototypes */
53 static void acpi_ns_normalize_pathname(char *original_path);
55 /*******************************************************************************
57 * FUNCTION: acpi_ns_get_external_pathname
59 * PARAMETERS: node - Namespace node whose pathname is needed
61 * RETURN: Pointer to storage containing the fully qualified name of
62 * the node, In external format (name segments separated by path
63 * separators.)
65 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
66 * for error and debug statements.
68 ******************************************************************************/
70 char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
72 char *name_buffer;
74 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
76 name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
77 return_PTR(name_buffer);
80 /*******************************************************************************
82 * FUNCTION: acpi_ns_get_pathname_length
84 * PARAMETERS: node - Namespace node
86 * RETURN: Length of path, including prefix
88 * DESCRIPTION: Get the length of the pathname string for this node
90 ******************************************************************************/
92 acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
94 acpi_size size;
96 /* Validate the Node */
98 if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
99 ACPI_ERROR((AE_INFO,
100 "Invalid/cached reference target node: %p, descriptor type %d",
101 node, ACPI_GET_DESCRIPTOR_TYPE(node)));
102 return (0);
105 size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
106 return (size);
109 /*******************************************************************************
111 * FUNCTION: acpi_ns_handle_to_name
113 * PARAMETERS: target_handle - Handle of named object whose name is
114 * to be found
115 * buffer - Where the name is returned
117 * RETURN: Status, Buffer is filled with name if status is AE_OK
119 * DESCRIPTION: Build and return a full namespace name
121 ******************************************************************************/
123 acpi_status
124 acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
126 acpi_status status;
127 struct acpi_namespace_node *node;
128 const char *node_name;
130 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
132 node = acpi_ns_validate_handle(target_handle);
133 if (!node) {
134 return_ACPI_STATUS(AE_BAD_PARAMETER);
137 /* Validate/Allocate/Clear caller buffer */
139 status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
140 if (ACPI_FAILURE(status)) {
141 return_ACPI_STATUS(status);
144 /* Just copy the ACPI name from the Node and zero terminate it */
146 node_name = acpi_ut_get_node_name(node);
147 ACPI_MOVE_NAME(buffer->pointer, node_name);
148 ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
150 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
151 return_ACPI_STATUS(AE_OK);
154 /*******************************************************************************
156 * FUNCTION: acpi_ns_handle_to_pathname
158 * PARAMETERS: target_handle - Handle of named object whose name is
159 * to be found
160 * buffer - Where the pathname is returned
161 * no_trailing - Remove trailing '_' for each name
162 * segment
164 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
166 * DESCRIPTION: Build and return a full namespace pathname
168 ******************************************************************************/
170 acpi_status
171 acpi_ns_handle_to_pathname(acpi_handle target_handle,
172 struct acpi_buffer *buffer, u8 no_trailing)
174 acpi_status status;
175 struct acpi_namespace_node *node;
176 acpi_size required_size;
178 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
180 node = acpi_ns_validate_handle(target_handle);
181 if (!node) {
182 return_ACPI_STATUS(AE_BAD_PARAMETER);
185 /* Determine size required for the caller buffer */
187 required_size =
188 acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
189 if (!required_size) {
190 return_ACPI_STATUS(AE_BAD_PARAMETER);
193 /* Validate/Allocate/Clear caller buffer */
195 status = acpi_ut_initialize_buffer(buffer, required_size);
196 if (ACPI_FAILURE(status)) {
197 return_ACPI_STATUS(status);
200 /* Build the path in the caller buffer */
202 (void)acpi_ns_build_normalized_path(node, buffer->pointer,
203 required_size, no_trailing);
205 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
206 (char *)buffer->pointer, (u32) required_size));
207 return_ACPI_STATUS(AE_OK);
210 /*******************************************************************************
212 * FUNCTION: acpi_ns_build_normalized_path
214 * PARAMETERS: node - Namespace node
215 * full_path - Where the path name is returned
216 * path_size - Size of returned path name buffer
217 * no_trailing - Remove trailing '_' from each name segment
219 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
220 * of pathname + 1) which means the 'FullPath' contains a trailing
221 * null.
223 * DESCRIPTION: Build and return a full namespace pathname.
224 * Note that if the size of 'FullPath' isn't large enough to
225 * contain the namespace node's path name, the actual required
226 * buffer length is returned, and it should be greater than
227 * 'PathSize'. So callers are able to check the returning value
228 * to determine the buffer size of 'FullPath'.
230 ******************************************************************************/
233 acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
234 char *full_path, u32 path_size, u8 no_trailing)
236 u32 length = 0, i;
237 char name[ACPI_NAME_SIZE];
238 u8 do_no_trailing;
239 char c, *left, *right;
240 struct acpi_namespace_node *next_node;
242 ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
244 #define ACPI_PATH_PUT8(path, size, byte, length) \
245 do { \
246 if ((length) < (size)) \
248 (path)[(length)] = (byte); \
250 (length)++; \
251 } while (0)
254 * Make sure the path_size is correct, so that we don't need to
255 * validate both full_path and path_size.
257 if (!full_path) {
258 path_size = 0;
261 if (!node) {
262 goto build_trailing_null;
265 next_node = node;
266 while (next_node && next_node != acpi_gbl_root_node) {
267 if (next_node != node) {
268 ACPI_PATH_PUT8(full_path, path_size,
269 AML_DUAL_NAME_PREFIX, length);
272 ACPI_MOVE_32_TO_32(name, &next_node->name);
273 do_no_trailing = no_trailing;
274 for (i = 0; i < 4; i++) {
275 c = name[4 - i - 1];
276 if (do_no_trailing && c != '_') {
277 do_no_trailing = FALSE;
279 if (!do_no_trailing) {
280 ACPI_PATH_PUT8(full_path, path_size, c, length);
284 next_node = next_node->parent;
287 ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
289 /* Reverse the path string */
291 if (length <= path_size) {
292 left = full_path;
293 right = full_path + length - 1;
295 while (left < right) {
296 c = *left;
297 *left++ = *right;
298 *right-- = c;
302 /* Append the trailing null */
304 build_trailing_null:
305 ACPI_PATH_PUT8(full_path, path_size, '\0', length);
307 #undef ACPI_PATH_PUT8
309 return_UINT32(length);
312 /*******************************************************************************
314 * FUNCTION: acpi_ns_get_normalized_pathname
316 * PARAMETERS: node - Namespace node whose pathname is needed
317 * no_trailing - Remove trailing '_' from each name segment
319 * RETURN: Pointer to storage containing the fully qualified name of
320 * the node, In external format (name segments separated by path
321 * separators.)
323 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
324 * for error and debug statements. All trailing '_' will be
325 * removed from the full pathname if 'NoTrailing' is specified..
327 ******************************************************************************/
329 char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
330 u8 no_trailing)
332 char *name_buffer;
333 acpi_size size;
335 ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
337 /* Calculate required buffer size based on depth below root */
339 size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
340 if (!size) {
341 return_PTR(NULL);
344 /* Allocate a buffer to be returned to caller */
346 name_buffer = ACPI_ALLOCATE_ZEROED(size);
347 if (!name_buffer) {
348 ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
349 return_PTR(NULL);
352 /* Build the path in the allocated buffer */
354 (void)acpi_ns_build_normalized_path(node, name_buffer, size,
355 no_trailing);
357 return_PTR(name_buffer);
360 /*******************************************************************************
362 * FUNCTION: acpi_ns_build_prefixed_pathname
364 * PARAMETERS: prefix_scope - Scope/Path that prefixes the internal path
365 * internal_path - Name or path of the namespace node
367 * RETURN: None
369 * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
370 * 1) Path associated with the prefix_scope namespace node
371 * 2) External path representation of the Internal path
373 ******************************************************************************/
375 char *acpi_ns_build_prefixed_pathname(union acpi_generic_state *prefix_scope,
376 const char *internal_path)
378 acpi_status status;
379 char *full_path = NULL;
380 char *external_path = NULL;
381 char *prefix_path = NULL;
382 u32 prefix_path_length = 0;
384 /* If there is a prefix, get the pathname to it */
386 if (prefix_scope && prefix_scope->scope.node) {
387 prefix_path =
388 acpi_ns_get_normalized_pathname(prefix_scope->scope.node,
389 TRUE);
390 if (prefix_path) {
391 prefix_path_length = strlen(prefix_path);
395 status = acpi_ns_externalize_name(ACPI_UINT32_MAX, internal_path,
396 NULL, &external_path);
397 if (ACPI_FAILURE(status)) {
398 goto cleanup;
401 /* Merge the prefix path and the path. 2 is for one dot and trailing null */
403 full_path =
404 ACPI_ALLOCATE_ZEROED(prefix_path_length + strlen(external_path) +
406 if (!full_path) {
407 goto cleanup;
410 /* Don't merge if the External path is already fully qualified */
412 if (prefix_path && (*external_path != '\\') && (*external_path != '^')) {
413 strcat(full_path, prefix_path);
414 if (prefix_path[1]) {
415 strcat(full_path, ".");
419 acpi_ns_normalize_pathname(external_path);
420 strcat(full_path, external_path);
422 cleanup:
423 if (prefix_path) {
424 ACPI_FREE(prefix_path);
426 if (external_path) {
427 ACPI_FREE(external_path);
430 return (full_path);
433 /*******************************************************************************
435 * FUNCTION: acpi_ns_normalize_pathname
437 * PARAMETERS: original_path - Path to be normalized, in External format
439 * RETURN: The original path is processed in-place
441 * DESCRIPTION: Remove trailing underscores from each element of a path.
443 * For example: \A___.B___.C___ becomes \A.B.C
445 ******************************************************************************/
447 static void acpi_ns_normalize_pathname(char *original_path)
449 char *input_path = original_path;
450 char *new_path_buffer;
451 char *new_path;
452 u32 i;
454 /* Allocate a temp buffer in which to construct the new path */
456 new_path_buffer = ACPI_ALLOCATE_ZEROED(strlen(input_path) + 1);
457 new_path = new_path_buffer;
458 if (!new_path_buffer) {
459 return;
462 /* Special characters may appear at the beginning of the path */
464 if (*input_path == '\\') {
465 *new_path = *input_path;
466 new_path++;
467 input_path++;
470 while (*input_path == '^') {
471 *new_path = *input_path;
472 new_path++;
473 input_path++;
476 /* Remainder of the path */
478 while (*input_path) {
480 /* Do one nameseg at a time */
482 for (i = 0; (i < ACPI_NAME_SIZE) && *input_path; i++) {
483 if ((i == 0) || (*input_path != '_')) { /* First char is allowed to be underscore */
484 *new_path = *input_path;
485 new_path++;
488 input_path++;
491 /* Dot means that there are more namesegs to come */
493 if (*input_path == '.') {
494 *new_path = *input_path;
495 new_path++;
496 input_path++;
500 *new_path = 0;
501 strcpy(original_path, new_path_buffer);
502 ACPI_FREE(new_path_buffer);