mm/hmm.c: remove superfluous RCU protection around radix tree lookup
[linux/fpc-iii.git] / drivers / acpi / acpica / nsnames.c
blob289c15bb8c6a96acbe1ab95d1d8e2e612819bfb8
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: nsnames - Name manipulation and search
6 ******************************************************************************/
8 #include <acpi/acpi.h>
9 #include "accommon.h"
10 #include "amlcode.h"
11 #include "acnamesp.h"
13 #define _COMPONENT ACPI_NAMESPACE
14 ACPI_MODULE_NAME("nsnames")
16 /* Local Prototypes */
17 static void acpi_ns_normalize_pathname(char *original_path);
19 /*******************************************************************************
21 * FUNCTION: acpi_ns_get_external_pathname
23 * PARAMETERS: node - Namespace node whose pathname is needed
25 * RETURN: Pointer to storage containing the fully qualified name of
26 * the node, In external format (name segments separated by path
27 * separators.)
29 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
30 * for error and debug statements.
32 ******************************************************************************/
34 char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
36 char *name_buffer;
38 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
40 name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
41 return_PTR(name_buffer);
44 /*******************************************************************************
46 * FUNCTION: acpi_ns_get_pathname_length
48 * PARAMETERS: node - Namespace node
50 * RETURN: Length of path, including prefix
52 * DESCRIPTION: Get the length of the pathname string for this node
54 ******************************************************************************/
56 acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
58 acpi_size size;
60 /* Validate the Node */
62 if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
63 ACPI_ERROR((AE_INFO,
64 "Invalid/cached reference target node: %p, descriptor type %d",
65 node, ACPI_GET_DESCRIPTOR_TYPE(node)));
66 return (0);
69 size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
70 return (size);
73 /*******************************************************************************
75 * FUNCTION: acpi_ns_handle_to_name
77 * PARAMETERS: target_handle - Handle of named object whose name is
78 * to be found
79 * buffer - Where the name is returned
81 * RETURN: Status, Buffer is filled with name if status is AE_OK
83 * DESCRIPTION: Build and return a full namespace name
85 ******************************************************************************/
87 acpi_status
88 acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
90 acpi_status status;
91 struct acpi_namespace_node *node;
92 const char *node_name;
94 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
96 node = acpi_ns_validate_handle(target_handle);
97 if (!node) {
98 return_ACPI_STATUS(AE_BAD_PARAMETER);
101 /* Validate/Allocate/Clear caller buffer */
103 status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
104 if (ACPI_FAILURE(status)) {
105 return_ACPI_STATUS(status);
108 /* Just copy the ACPI name from the Node and zero terminate it */
110 node_name = acpi_ut_get_node_name(node);
111 ACPI_MOVE_NAME(buffer->pointer, node_name);
112 ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
114 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
115 return_ACPI_STATUS(AE_OK);
118 /*******************************************************************************
120 * FUNCTION: acpi_ns_handle_to_pathname
122 * PARAMETERS: target_handle - Handle of named object whose name is
123 * to be found
124 * buffer - Where the pathname is returned
125 * no_trailing - Remove trailing '_' for each name
126 * segment
128 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
130 * DESCRIPTION: Build and return a full namespace pathname
132 ******************************************************************************/
134 acpi_status
135 acpi_ns_handle_to_pathname(acpi_handle target_handle,
136 struct acpi_buffer *buffer, u8 no_trailing)
138 acpi_status status;
139 struct acpi_namespace_node *node;
140 acpi_size required_size;
142 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
144 node = acpi_ns_validate_handle(target_handle);
145 if (!node) {
146 return_ACPI_STATUS(AE_BAD_PARAMETER);
149 /* Determine size required for the caller buffer */
151 required_size =
152 acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
153 if (!required_size) {
154 return_ACPI_STATUS(AE_BAD_PARAMETER);
157 /* Validate/Allocate/Clear caller buffer */
159 status = acpi_ut_initialize_buffer(buffer, required_size);
160 if (ACPI_FAILURE(status)) {
161 return_ACPI_STATUS(status);
164 /* Build the path in the caller buffer */
166 (void)acpi_ns_build_normalized_path(node, buffer->pointer,
167 required_size, no_trailing);
169 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
170 (char *)buffer->pointer, (u32) required_size));
171 return_ACPI_STATUS(AE_OK);
174 /*******************************************************************************
176 * FUNCTION: acpi_ns_build_normalized_path
178 * PARAMETERS: node - Namespace node
179 * full_path - Where the path name is returned
180 * path_size - Size of returned path name buffer
181 * no_trailing - Remove trailing '_' from each name segment
183 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
184 * of pathname + 1) which means the 'FullPath' contains a trailing
185 * null.
187 * DESCRIPTION: Build and return a full namespace pathname.
188 * Note that if the size of 'FullPath' isn't large enough to
189 * contain the namespace node's path name, the actual required
190 * buffer length is returned, and it should be greater than
191 * 'PathSize'. So callers are able to check the returning value
192 * to determine the buffer size of 'FullPath'.
194 ******************************************************************************/
197 acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
198 char *full_path, u32 path_size, u8 no_trailing)
200 u32 length = 0, i;
201 char name[ACPI_NAME_SIZE];
202 u8 do_no_trailing;
203 char c, *left, *right;
204 struct acpi_namespace_node *next_node;
206 ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
208 #define ACPI_PATH_PUT8(path, size, byte, length) \
209 do { \
210 if ((length) < (size)) \
212 (path)[(length)] = (byte); \
214 (length)++; \
215 } while (0)
218 * Make sure the path_size is correct, so that we don't need to
219 * validate both full_path and path_size.
221 if (!full_path) {
222 path_size = 0;
225 if (!node) {
226 goto build_trailing_null;
229 next_node = node;
230 while (next_node && next_node != acpi_gbl_root_node) {
231 if (next_node != node) {
232 ACPI_PATH_PUT8(full_path, path_size,
233 AML_DUAL_NAME_PREFIX, length);
236 ACPI_MOVE_32_TO_32(name, &next_node->name);
237 do_no_trailing = no_trailing;
238 for (i = 0; i < 4; i++) {
239 c = name[4 - i - 1];
240 if (do_no_trailing && c != '_') {
241 do_no_trailing = FALSE;
243 if (!do_no_trailing) {
244 ACPI_PATH_PUT8(full_path, path_size, c, length);
248 next_node = next_node->parent;
251 ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
253 /* Reverse the path string */
255 if (length <= path_size) {
256 left = full_path;
257 right = full_path + length - 1;
259 while (left < right) {
260 c = *left;
261 *left++ = *right;
262 *right-- = c;
266 /* Append the trailing null */
268 build_trailing_null:
269 ACPI_PATH_PUT8(full_path, path_size, '\0', length);
271 #undef ACPI_PATH_PUT8
273 return_UINT32(length);
276 /*******************************************************************************
278 * FUNCTION: acpi_ns_get_normalized_pathname
280 * PARAMETERS: node - Namespace node whose pathname is needed
281 * no_trailing - Remove trailing '_' from each name segment
283 * RETURN: Pointer to storage containing the fully qualified name of
284 * the node, In external format (name segments separated by path
285 * separators.)
287 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
288 * for error and debug statements. All trailing '_' will be
289 * removed from the full pathname if 'NoTrailing' is specified..
291 ******************************************************************************/
293 char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
294 u8 no_trailing)
296 char *name_buffer;
297 acpi_size size;
299 ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
301 /* Calculate required buffer size based on depth below root */
303 size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
304 if (!size) {
305 return_PTR(NULL);
308 /* Allocate a buffer to be returned to caller */
310 name_buffer = ACPI_ALLOCATE_ZEROED(size);
311 if (!name_buffer) {
312 ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
313 return_PTR(NULL);
316 /* Build the path in the allocated buffer */
318 (void)acpi_ns_build_normalized_path(node, name_buffer, size,
319 no_trailing);
321 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, "%s: Path \"%s\"\n",
322 ACPI_GET_FUNCTION_NAME, name_buffer));
324 return_PTR(name_buffer);
327 /*******************************************************************************
329 * FUNCTION: acpi_ns_build_prefixed_pathname
331 * PARAMETERS: prefix_scope - Scope/Path that prefixes the internal path
332 * internal_path - Name or path of the namespace node
334 * RETURN: None
336 * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
337 * 1) Path associated with the prefix_scope namespace node
338 * 2) External path representation of the Internal path
340 ******************************************************************************/
342 char *acpi_ns_build_prefixed_pathname(union acpi_generic_state *prefix_scope,
343 const char *internal_path)
345 acpi_status status;
346 char *full_path = NULL;
347 char *external_path = NULL;
348 char *prefix_path = NULL;
349 u32 prefix_path_length = 0;
351 /* If there is a prefix, get the pathname to it */
353 if (prefix_scope && prefix_scope->scope.node) {
354 prefix_path =
355 acpi_ns_get_normalized_pathname(prefix_scope->scope.node,
356 TRUE);
357 if (prefix_path) {
358 prefix_path_length = strlen(prefix_path);
362 status = acpi_ns_externalize_name(ACPI_UINT32_MAX, internal_path,
363 NULL, &external_path);
364 if (ACPI_FAILURE(status)) {
365 goto cleanup;
368 /* Merge the prefix path and the path. 2 is for one dot and trailing null */
370 full_path =
371 ACPI_ALLOCATE_ZEROED(prefix_path_length + strlen(external_path) +
373 if (!full_path) {
374 goto cleanup;
377 /* Don't merge if the External path is already fully qualified */
379 if (prefix_path && (*external_path != '\\') && (*external_path != '^')) {
380 strcat(full_path, prefix_path);
381 if (prefix_path[1]) {
382 strcat(full_path, ".");
386 acpi_ns_normalize_pathname(external_path);
387 strcat(full_path, external_path);
389 cleanup:
390 if (prefix_path) {
391 ACPI_FREE(prefix_path);
393 if (external_path) {
394 ACPI_FREE(external_path);
397 return (full_path);
400 /*******************************************************************************
402 * FUNCTION: acpi_ns_normalize_pathname
404 * PARAMETERS: original_path - Path to be normalized, in External format
406 * RETURN: The original path is processed in-place
408 * DESCRIPTION: Remove trailing underscores from each element of a path.
410 * For example: \A___.B___.C___ becomes \A.B.C
412 ******************************************************************************/
414 static void acpi_ns_normalize_pathname(char *original_path)
416 char *input_path = original_path;
417 char *new_path_buffer;
418 char *new_path;
419 u32 i;
421 /* Allocate a temp buffer in which to construct the new path */
423 new_path_buffer = ACPI_ALLOCATE_ZEROED(strlen(input_path) + 1);
424 new_path = new_path_buffer;
425 if (!new_path_buffer) {
426 return;
429 /* Special characters may appear at the beginning of the path */
431 if (*input_path == '\\') {
432 *new_path = *input_path;
433 new_path++;
434 input_path++;
437 while (*input_path == '^') {
438 *new_path = *input_path;
439 new_path++;
440 input_path++;
443 /* Remainder of the path */
445 while (*input_path) {
447 /* Do one nameseg at a time */
449 for (i = 0; (i < ACPI_NAME_SIZE) && *input_path; i++) {
450 if ((i == 0) || (*input_path != '_')) { /* First char is allowed to be underscore */
451 *new_path = *input_path;
452 new_path++;
455 input_path++;
458 /* Dot means that there are more namesegs to come */
460 if (*input_path == '.') {
461 *new_path = *input_path;
462 new_path++;
463 input_path++;
467 *new_path = 0;
468 strcpy(original_path, new_path_buffer);
469 ACPI_FREE(new_path_buffer);