]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/acpi/acpica/nsnames.c
ACPICA: Update for some debug output. No functional change
[mirror_ubuntu-hirsute-kernel.git] / drivers / acpi / acpica / nsnames.c
CommitLineData
1da177e4
LT
1/*******************************************************************************
2 *
3 * Module Name: nsnames - Name manipulation and search
4 *
5 ******************************************************************************/
6
7/*
da6f8320 8 * Copyright (C) 2000 - 2018, Intel Corp.
1da177e4
LT
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
1da177e4 44#include <acpi/acpi.h>
e2f7a777
LB
45#include "accommon.h"
46#include "amlcode.h"
47#include "acnamesp.h"
1da177e4 48
1da177e4 49#define _COMPONENT ACPI_NAMESPACE
4be44fcd 50ACPI_MODULE_NAME("nsnames")
1da177e4 51
16ccf829
BM
52/* Local Prototypes */
53static void acpi_ns_normalize_pathname(char *original_path);
54
1da177e4
LT
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_ns_get_external_pathname
58 *
ba494bee 59 * PARAMETERS: node - Namespace node whose pathname is needed
1da177e4
LT
60 *
61 * RETURN: Pointer to storage containing the fully qualified name of
62 * the node, In external format (name segments separated by path
63 * separators.)
64 *
75c8044f
LZ
65 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
66 * for error and debug statements.
1da177e4
LT
67 *
68 ******************************************************************************/
16ccf829 69
4be44fcd 70char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
1da177e4 71{
4be44fcd 72 char *name_buffer;
1da177e4 73
b229cf92 74 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
1da177e4 75
d1e7ffe5 76 name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
4be44fcd 77 return_PTR(name_buffer);
1da177e4 78}
1da177e4 79
1da177e4
LT
80/*******************************************************************************
81 *
82 * FUNCTION: acpi_ns_get_pathname_length
83 *
ba494bee 84 * PARAMETERS: node - Namespace node
1da177e4
LT
85 *
86 * RETURN: Length of path, including prefix
87 *
88 * DESCRIPTION: Get the length of the pathname string for this node
89 *
90 ******************************************************************************/
91
4be44fcd 92acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
1da177e4 93{
4be44fcd 94 acpi_size size;
1da177e4 95
a62a7117
BM
96 /* Validate the Node */
97
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);
103 }
1da177e4 104
d1e7ffe5 105 size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
d1e7ffe5 106 return (size);
1da177e4
LT
107}
108
523db19b
LZ
109/*******************************************************************************
110 *
111 * FUNCTION: acpi_ns_handle_to_name
112 *
113 * PARAMETERS: target_handle - Handle of named object whose name is
114 * to be found
115 * buffer - Where the name is returned
116 *
117 * RETURN: Status, Buffer is filled with name if status is AE_OK
118 *
119 * DESCRIPTION: Build and return a full namespace name
120 *
121 ******************************************************************************/
122
123acpi_status
124acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
125{
126 acpi_status status;
127 struct acpi_namespace_node *node;
128 const char *node_name;
129
130 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
131
132 node = acpi_ns_validate_handle(target_handle);
133 if (!node) {
134 return_ACPI_STATUS(AE_BAD_PARAMETER);
135 }
136
137 /* Validate/Allocate/Clear caller buffer */
138
139 status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
140 if (ACPI_FAILURE(status)) {
141 return_ACPI_STATUS(status);
142 }
143
144 /* Just copy the ACPI name from the Node and zero terminate it */
145
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;
149
150 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
151 return_ACPI_STATUS(AE_OK);
152}
153
1da177e4
LT
154/*******************************************************************************
155 *
156 * FUNCTION: acpi_ns_handle_to_pathname
157 *
158 * PARAMETERS: target_handle - Handle of named object whose name is
159 * to be found
ba494bee 160 * buffer - Where the pathname is returned
d1e7ffe5
LZ
161 * no_trailing - Remove trailing '_' for each name
162 * segment
1da177e4
LT
163 *
164 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
165 *
166 * DESCRIPTION: Build and return a full namespace pathname
167 *
168 ******************************************************************************/
169
170acpi_status
4be44fcd 171acpi_ns_handle_to_pathname(acpi_handle target_handle,
f5c1e1c5 172 struct acpi_buffer *buffer, u8 no_trailing)
1da177e4 173{
4be44fcd
LB
174 acpi_status status;
175 struct acpi_namespace_node *node;
176 acpi_size required_size;
1da177e4 177
b229cf92 178 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
1da177e4 179
f24b664d 180 node = acpi_ns_validate_handle(target_handle);
1da177e4 181 if (!node) {
4be44fcd 182 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
183 }
184
185 /* Determine size required for the caller buffer */
186
d1e7ffe5
LZ
187 required_size =
188 acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
f88133d7 189 if (!required_size) {
3c7db22a 190 return_ACPI_STATUS(AE_BAD_PARAMETER);
f88133d7 191 }
1da177e4
LT
192
193 /* Validate/Allocate/Clear caller buffer */
194
4be44fcd
LB
195 status = acpi_ut_initialize_buffer(buffer, required_size);
196 if (ACPI_FAILURE(status)) {
197 return_ACPI_STATUS(status);
1da177e4
LT
198 }
199
200 /* Build the path in the caller buffer */
201
d1e7ffe5
LZ
202 (void)acpi_ns_build_normalized_path(node, buffer->pointer,
203 required_size, no_trailing);
1da177e4 204
50eca3eb 205 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
4be44fcd
LB
206 (char *)buffer->pointer, (u32) required_size));
207 return_ACPI_STATUS(AE_OK);
1da177e4 208}
d1e7ffe5
LZ
209
210/*******************************************************************************
211 *
212 * FUNCTION: acpi_ns_build_normalized_path
213 *
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
218 *
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.
222 *
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'.
229 *
230 ******************************************************************************/
231
232u32
233acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
234 char *full_path, u32 path_size, u8 no_trailing)
235{
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;
241
242 ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
243
244#define ACPI_PATH_PUT8(path, size, byte, length) \
245 do { \
246 if ((length) < (size)) \
247 { \
248 (path)[(length)] = (byte); \
249 } \
250 (length)++; \
251 } while (0)
252
253 /*
254 * Make sure the path_size is correct, so that we don't need to
255 * validate both full_path and path_size.
256 */
257 if (!full_path) {
258 path_size = 0;
259 }
260
261 if (!node) {
262 goto build_trailing_null;
263 }
264
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);
270 }
1fad8738 271
d1e7ffe5
LZ
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;
278 }
279 if (!do_no_trailing) {
280 ACPI_PATH_PUT8(full_path, path_size, c, length);
281 }
282 }
1fad8738 283
d1e7ffe5
LZ
284 next_node = next_node->parent;
285 }
1fad8738 286
d1e7ffe5
LZ
287 ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
288
289 /* Reverse the path string */
290
291 if (length <= path_size) {
292 left = full_path;
293 right = full_path + length - 1;
1fad8738 294
d1e7ffe5
LZ
295 while (left < right) {
296 c = *left;
297 *left++ = *right;
298 *right-- = c;
299 }
300 }
301
302 /* Append the trailing null */
303
304build_trailing_null:
305 ACPI_PATH_PUT8(full_path, path_size, '\0', length);
306
307#undef ACPI_PATH_PUT8
308
309 return_UINT32(length);
310}
311
312/*******************************************************************************
313 *
314 * FUNCTION: acpi_ns_get_normalized_pathname
315 *
316 * PARAMETERS: node - Namespace node whose pathname is needed
317 * no_trailing - Remove trailing '_' from each name segment
318 *
319 * RETURN: Pointer to storage containing the fully qualified name of
320 * the node, In external format (name segments separated by path
321 * separators.)
322 *
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..
326 *
327 ******************************************************************************/
328
329char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
330 u8 no_trailing)
331{
332 char *name_buffer;
333 acpi_size size;
334
335 ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
336
337 /* Calculate required buffer size based on depth below root */
338
339 size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
340 if (!size) {
341 return_PTR(NULL);
342 }
343
344 /* Allocate a buffer to be returned to caller */
345
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);
350 }
351
352 /* Build the path in the allocated buffer */
353
354 (void)acpi_ns_build_normalized_path(node, name_buffer, size,
355 no_trailing);
356
1ef63231
BM
357 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, "%s: Path \"%s\"\n",
358 ACPI_GET_FUNCTION_NAME, name_buffer));
359
d1e7ffe5
LZ
360 return_PTR(name_buffer);
361}
16ccf829
BM
362
363/*******************************************************************************
364 *
365 * FUNCTION: acpi_ns_build_prefixed_pathname
366 *
367 * PARAMETERS: prefix_scope - Scope/Path that prefixes the internal path
368 * internal_path - Name or path of the namespace node
369 *
370 * RETURN: None
371 *
372 * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
373 * 1) Path associated with the prefix_scope namespace node
374 * 2) External path representation of the Internal path
375 *
376 ******************************************************************************/
377
378char *acpi_ns_build_prefixed_pathname(union acpi_generic_state *prefix_scope,
379 const char *internal_path)
380{
381 acpi_status status;
382 char *full_path = NULL;
dea4e833 383 char *external_path = NULL;
16ccf829
BM
384 char *prefix_path = NULL;
385 u32 prefix_path_length = 0;
386
387 /* If there is a prefix, get the pathname to it */
388
389 if (prefix_scope && prefix_scope->scope.node) {
390 prefix_path =
391 acpi_ns_get_normalized_pathname(prefix_scope->scope.node,
392 TRUE);
393 if (prefix_path) {
394 prefix_path_length = strlen(prefix_path);
395 }
396 }
397
398 status = acpi_ns_externalize_name(ACPI_UINT32_MAX, internal_path,
399 NULL, &external_path);
400 if (ACPI_FAILURE(status)) {
401 goto cleanup;
402 }
403
404 /* Merge the prefix path and the path. 2 is for one dot and trailing null */
405
406 full_path =
407 ACPI_ALLOCATE_ZEROED(prefix_path_length + strlen(external_path) +
408 2);
409 if (!full_path) {
410 goto cleanup;
411 }
412
413 /* Don't merge if the External path is already fully qualified */
414
415 if (prefix_path && (*external_path != '\\') && (*external_path != '^')) {
416 strcat(full_path, prefix_path);
417 if (prefix_path[1]) {
418 strcat(full_path, ".");
419 }
420 }
421
422 acpi_ns_normalize_pathname(external_path);
423 strcat(full_path, external_path);
424
425cleanup:
426 if (prefix_path) {
427 ACPI_FREE(prefix_path);
428 }
dea4e833
EK
429 if (external_path) {
430 ACPI_FREE(external_path);
431 }
16ccf829
BM
432
433 return (full_path);
434}
435
436/*******************************************************************************
437 *
438 * FUNCTION: acpi_ns_normalize_pathname
439 *
440 * PARAMETERS: original_path - Path to be normalized, in External format
441 *
442 * RETURN: The original path is processed in-place
443 *
444 * DESCRIPTION: Remove trailing underscores from each element of a path.
445 *
446 * For example: \A___.B___.C___ becomes \A.B.C
447 *
448 ******************************************************************************/
449
450static void acpi_ns_normalize_pathname(char *original_path)
451{
452 char *input_path = original_path;
453 char *new_path_buffer;
454 char *new_path;
455 u32 i;
456
457 /* Allocate a temp buffer in which to construct the new path */
458
459 new_path_buffer = ACPI_ALLOCATE_ZEROED(strlen(input_path) + 1);
460 new_path = new_path_buffer;
461 if (!new_path_buffer) {
462 return;
463 }
464
465 /* Special characters may appear at the beginning of the path */
466
467 if (*input_path == '\\') {
468 *new_path = *input_path;
469 new_path++;
470 input_path++;
471 }
472
473 while (*input_path == '^') {
474 *new_path = *input_path;
475 new_path++;
476 input_path++;
477 }
478
479 /* Remainder of the path */
480
481 while (*input_path) {
482
483 /* Do one nameseg at a time */
484
485 for (i = 0; (i < ACPI_NAME_SIZE) && *input_path; i++) {
486 if ((i == 0) || (*input_path != '_')) { /* First char is allowed to be underscore */
487 *new_path = *input_path;
488 new_path++;
489 }
490
491 input_path++;
492 }
493
494 /* Dot means that there are more namesegs to come */
495
496 if (*input_path == '.') {
497 *new_path = *input_path;
498 new_path++;
499 input_path++;
500 }
501 }
502
503 *new_path = 0;
504 strcpy(original_path, new_path_buffer);
505 ACPI_FREE(new_path_buffer);
506}