Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / acpica / nsxfname.c
blobb2915c9cceaf6fe482727d10d8287c8fae7f65bb
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: nsxfname - Public interfaces to the ACPI subsystem
5 * ACPI Namespace oriented interfaces
7 * Copyright (C) 2000 - 2018, Intel Corp.
9 *****************************************************************************/
11 #define EXPORT_ACPI_INTERFACES
13 #include <acpi/acpi.h>
14 #include "accommon.h"
15 #include "acnamesp.h"
16 #include "acparser.h"
17 #include "amlcode.h"
19 #define _COMPONENT ACPI_NAMESPACE
20 ACPI_MODULE_NAME("nsxfname")
22 /* Local prototypes */
23 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
24 struct acpi_pnp_device_id *source,
25 char *string_area);
27 /******************************************************************************
29 * FUNCTION: acpi_get_handle
31 * PARAMETERS: parent - Object to search under (search scope).
32 * pathname - Pointer to an asciiz string containing the
33 * name
34 * ret_handle - Where the return handle is returned
36 * RETURN: Status
38 * DESCRIPTION: This routine will search for a caller specified name in the
39 * name space. The caller can restrict the search region by
40 * specifying a non NULL parent. The parent value is itself a
41 * namespace handle.
43 ******************************************************************************/
45 acpi_status
46 acpi_get_handle(acpi_handle parent,
47 acpi_string pathname, acpi_handle *ret_handle)
49 acpi_status status;
50 struct acpi_namespace_node *node = NULL;
51 struct acpi_namespace_node *prefix_node = NULL;
53 ACPI_FUNCTION_ENTRY();
55 /* Parameter Validation */
57 if (!ret_handle || !pathname) {
58 return (AE_BAD_PARAMETER);
61 /* Convert a parent handle to a prefix node */
63 if (parent) {
64 prefix_node = acpi_ns_validate_handle(parent);
65 if (!prefix_node) {
66 return (AE_BAD_PARAMETER);
71 * Valid cases are:
72 * 1) Fully qualified pathname
73 * 2) Parent + Relative pathname
75 * Error for <null Parent + relative path>
77 if (ACPI_IS_ROOT_PREFIX(pathname[0])) {
79 /* Pathname is fully qualified (starts with '\') */
81 /* Special case for root-only, since we can't search for it */
83 if (!strcmp(pathname, ACPI_NS_ROOT_PATH)) {
84 *ret_handle =
85 ACPI_CAST_PTR(acpi_handle, acpi_gbl_root_node);
86 return (AE_OK);
88 } else if (!prefix_node) {
90 /* Relative path with null prefix is disallowed */
92 return (AE_BAD_PARAMETER);
95 /* Find the Node and convert to a handle */
97 status =
98 acpi_ns_get_node(prefix_node, pathname, ACPI_NS_NO_UPSEARCH, &node);
99 if (ACPI_SUCCESS(status)) {
100 *ret_handle = ACPI_CAST_PTR(acpi_handle, node);
103 return (status);
106 ACPI_EXPORT_SYMBOL(acpi_get_handle)
108 /******************************************************************************
110 * FUNCTION: acpi_get_name
112 * PARAMETERS: handle - Handle to be converted to a pathname
113 * name_type - Full pathname or single segment
114 * buffer - Buffer for returned path
116 * RETURN: Pointer to a string containing the fully qualified Name.
118 * DESCRIPTION: This routine returns the fully qualified name associated with
119 * the Handle parameter. This and the acpi_pathname_to_handle are
120 * complementary functions.
122 ******************************************************************************/
123 acpi_status
124 acpi_get_name(acpi_handle handle, u32 name_type, struct acpi_buffer *buffer)
126 acpi_status status;
128 /* Parameter validation */
130 if (name_type > ACPI_NAME_TYPE_MAX) {
131 return (AE_BAD_PARAMETER);
134 status = acpi_ut_validate_buffer(buffer);
135 if (ACPI_FAILURE(status)) {
136 return (status);
140 * Wants the single segment ACPI name.
141 * Validate handle and convert to a namespace Node
143 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
144 if (ACPI_FAILURE(status)) {
145 return (status);
148 if (name_type == ACPI_FULL_PATHNAME ||
149 name_type == ACPI_FULL_PATHNAME_NO_TRAILING) {
151 /* Get the full pathname (From the namespace root) */
153 status = acpi_ns_handle_to_pathname(handle, buffer,
154 name_type ==
155 ACPI_FULL_PATHNAME ? FALSE :
156 TRUE);
157 } else {
158 /* Get the single name */
160 status = acpi_ns_handle_to_name(handle, buffer);
163 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
164 return (status);
167 ACPI_EXPORT_SYMBOL(acpi_get_name)
169 /******************************************************************************
171 * FUNCTION: acpi_ns_copy_device_id
173 * PARAMETERS: dest - Pointer to the destination PNP_DEVICE_ID
174 * source - Pointer to the source PNP_DEVICE_ID
175 * string_area - Pointer to where to copy the dest string
177 * RETURN: Pointer to the next string area
179 * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
181 ******************************************************************************/
182 static char *acpi_ns_copy_device_id(struct acpi_pnp_device_id *dest,
183 struct acpi_pnp_device_id *source,
184 char *string_area)
186 /* Create the destination PNP_DEVICE_ID */
188 dest->string = string_area;
189 dest->length = source->length;
191 /* Copy actual string and return a pointer to the next string area */
193 memcpy(string_area, source->string, source->length);
194 return (string_area + source->length);
197 /******************************************************************************
199 * FUNCTION: acpi_get_object_info
201 * PARAMETERS: handle - Object Handle
202 * return_buffer - Where the info is returned
204 * RETURN: Status
206 * DESCRIPTION: Returns information about an object as gleaned from the
207 * namespace node and possibly by running several standard
208 * control methods (Such as in the case of a device.)
210 * For Device and Processor objects, run the Device _HID, _UID, _CID,
211 * _CLS, _ADR, _sx_w, and _sx_d methods.
213 * Note: Allocates the return buffer, must be freed by the caller.
215 * Note: This interface is intended to be used during the initial device
216 * discovery namespace traversal. Therefore, no complex methods can be
217 * executed, especially those that access operation regions. Therefore, do
218 * not add any additional methods that could cause problems in this area.
219 * Because of this reason support for the following methods has been removed:
220 * 1) _SUB method was removed (11/2015)
221 * 2) _STA method was removed (02/2018)
223 ******************************************************************************/
225 acpi_status
226 acpi_get_object_info(acpi_handle handle,
227 struct acpi_device_info **return_buffer)
229 struct acpi_namespace_node *node;
230 struct acpi_device_info *info;
231 struct acpi_pnp_device_id_list *cid_list = NULL;
232 struct acpi_pnp_device_id *hid = NULL;
233 struct acpi_pnp_device_id *uid = NULL;
234 struct acpi_pnp_device_id *cls = NULL;
235 char *next_id_string;
236 acpi_object_type type;
237 acpi_name name;
238 u8 param_count = 0;
239 u16 valid = 0;
240 u32 info_size;
241 u32 i;
242 acpi_status status;
244 /* Parameter validation */
246 if (!handle || !return_buffer) {
247 return (AE_BAD_PARAMETER);
250 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
251 if (ACPI_FAILURE(status)) {
252 return (status);
255 node = acpi_ns_validate_handle(handle);
256 if (!node) {
257 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
258 return (AE_BAD_PARAMETER);
261 /* Get the namespace node data while the namespace is locked */
263 info_size = sizeof(struct acpi_device_info);
264 type = node->type;
265 name = node->name.integer;
267 if (node->type == ACPI_TYPE_METHOD) {
268 param_count = node->object->method.param_count;
271 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
272 if (ACPI_FAILURE(status)) {
273 return (status);
276 if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
278 * Get extra info for ACPI Device/Processor objects only:
279 * Run the Device _HID, _UID, _CLS, and _CID methods.
281 * Note: none of these methods are required, so they may or may
282 * not be present for this device. The Info->Valid bitfield is used
283 * to indicate which methods were found and run successfully.
286 /* Execute the Device._HID method */
288 status = acpi_ut_execute_HID(node, &hid);
289 if (ACPI_SUCCESS(status)) {
290 info_size += hid->length;
291 valid |= ACPI_VALID_HID;
294 /* Execute the Device._UID method */
296 status = acpi_ut_execute_UID(node, &uid);
297 if (ACPI_SUCCESS(status)) {
298 info_size += uid->length;
299 valid |= ACPI_VALID_UID;
302 /* Execute the Device._CID method */
304 status = acpi_ut_execute_CID(node, &cid_list);
305 if (ACPI_SUCCESS(status)) {
307 /* Add size of CID strings and CID pointer array */
309 info_size +=
310 (cid_list->list_size -
311 sizeof(struct acpi_pnp_device_id_list));
312 valid |= ACPI_VALID_CID;
315 /* Execute the Device._CLS method */
317 status = acpi_ut_execute_CLS(node, &cls);
318 if (ACPI_SUCCESS(status)) {
319 info_size += cls->length;
320 valid |= ACPI_VALID_CLS;
325 * Now that we have the variable-length data, we can allocate the
326 * return buffer
328 info = ACPI_ALLOCATE_ZEROED(info_size);
329 if (!info) {
330 status = AE_NO_MEMORY;
331 goto cleanup;
334 /* Get the fixed-length data */
336 if ((type == ACPI_TYPE_DEVICE) || (type == ACPI_TYPE_PROCESSOR)) {
338 * Get extra info for ACPI Device/Processor objects only:
339 * Run the _ADR and, sx_w, and _sx_d methods.
341 * Notes: none of these methods are required, so they may or may
342 * not be present for this device. The Info->Valid bitfield is used
343 * to indicate which methods were found and run successfully.
346 /* Execute the Device._ADR method */
348 status = acpi_ut_evaluate_numeric_object(METHOD_NAME__ADR, node,
349 &info->address);
350 if (ACPI_SUCCESS(status)) {
351 valid |= ACPI_VALID_ADR;
354 /* Execute the Device._sx_w methods */
356 status = acpi_ut_execute_power_methods(node,
357 acpi_gbl_lowest_dstate_names,
358 ACPI_NUM_sx_w_METHODS,
359 info->lowest_dstates);
360 if (ACPI_SUCCESS(status)) {
361 valid |= ACPI_VALID_SXWS;
364 /* Execute the Device._sx_d methods */
366 status = acpi_ut_execute_power_methods(node,
367 acpi_gbl_highest_dstate_names,
368 ACPI_NUM_sx_d_METHODS,
369 info->highest_dstates);
370 if (ACPI_SUCCESS(status)) {
371 valid |= ACPI_VALID_SXDS;
376 * Create a pointer to the string area of the return buffer.
377 * Point to the end of the base struct acpi_device_info structure.
379 next_id_string = ACPI_CAST_PTR(char, info->compatible_id_list.ids);
380 if (cid_list) {
382 /* Point past the CID PNP_DEVICE_ID array */
384 next_id_string +=
385 ((acpi_size)cid_list->count *
386 sizeof(struct acpi_pnp_device_id));
390 * Copy the HID, UID, and CIDs to the return buffer. The variable-length
391 * strings are copied to the reserved area at the end of the buffer.
393 * For HID and CID, check if the ID is a PCI Root Bridge.
395 if (hid) {
396 next_id_string = acpi_ns_copy_device_id(&info->hardware_id,
397 hid, next_id_string);
399 if (acpi_ut_is_pci_root_bridge(hid->string)) {
400 info->flags |= ACPI_PCI_ROOT_BRIDGE;
404 if (uid) {
405 next_id_string = acpi_ns_copy_device_id(&info->unique_id,
406 uid, next_id_string);
409 if (cid_list) {
410 info->compatible_id_list.count = cid_list->count;
411 info->compatible_id_list.list_size = cid_list->list_size;
413 /* Copy each CID */
415 for (i = 0; i < cid_list->count; i++) {
416 next_id_string =
417 acpi_ns_copy_device_id(&info->compatible_id_list.
418 ids[i], &cid_list->ids[i],
419 next_id_string);
421 if (acpi_ut_is_pci_root_bridge(cid_list->ids[i].string)) {
422 info->flags |= ACPI_PCI_ROOT_BRIDGE;
427 if (cls) {
428 next_id_string = acpi_ns_copy_device_id(&info->class_code,
429 cls, next_id_string);
432 /* Copy the fixed-length data */
434 info->info_size = info_size;
435 info->type = type;
436 info->name = name;
437 info->param_count = param_count;
438 info->valid = valid;
440 *return_buffer = info;
441 status = AE_OK;
443 cleanup:
444 if (hid) {
445 ACPI_FREE(hid);
447 if (uid) {
448 ACPI_FREE(uid);
450 if (cid_list) {
451 ACPI_FREE(cid_list);
453 if (cls) {
454 ACPI_FREE(cls);
456 return (status);
459 ACPI_EXPORT_SYMBOL(acpi_get_object_info)
461 /******************************************************************************
463 * FUNCTION: acpi_install_method
465 * PARAMETERS: buffer - An ACPI table containing one control method
467 * RETURN: Status
469 * DESCRIPTION: Install a control method into the namespace. If the method
470 * name already exists in the namespace, it is overwritten. The
471 * input buffer must contain a valid DSDT or SSDT containing a
472 * single control method.
474 ******************************************************************************/
475 acpi_status acpi_install_method(u8 *buffer)
477 struct acpi_table_header *table =
478 ACPI_CAST_PTR(struct acpi_table_header, buffer);
479 u8 *aml_buffer;
480 u8 *aml_start;
481 char *path;
482 struct acpi_namespace_node *node;
483 union acpi_operand_object *method_obj;
484 struct acpi_parse_state parser_state;
485 u32 aml_length;
486 u16 opcode;
487 u8 method_flags;
488 acpi_status status;
490 /* Parameter validation */
492 if (!buffer) {
493 return (AE_BAD_PARAMETER);
496 /* Table must be a DSDT or SSDT */
498 if (!ACPI_COMPARE_NAME(table->signature, ACPI_SIG_DSDT) &&
499 !ACPI_COMPARE_NAME(table->signature, ACPI_SIG_SSDT)) {
500 return (AE_BAD_HEADER);
503 /* First AML opcode in the table must be a control method */
505 parser_state.aml = buffer + sizeof(struct acpi_table_header);
506 opcode = acpi_ps_peek_opcode(&parser_state);
507 if (opcode != AML_METHOD_OP) {
508 return (AE_BAD_PARAMETER);
511 /* Extract method information from the raw AML */
513 parser_state.aml += acpi_ps_get_opcode_size(opcode);
514 parser_state.pkg_end = acpi_ps_get_next_package_end(&parser_state);
515 path = acpi_ps_get_next_namestring(&parser_state);
517 method_flags = *parser_state.aml++;
518 aml_start = parser_state.aml;
519 aml_length = ACPI_PTR_DIFF(parser_state.pkg_end, aml_start);
522 * Allocate resources up-front. We don't want to have to delete a new
523 * node from the namespace if we cannot allocate memory.
525 aml_buffer = ACPI_ALLOCATE(aml_length);
526 if (!aml_buffer) {
527 return (AE_NO_MEMORY);
530 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
531 if (!method_obj) {
532 ACPI_FREE(aml_buffer);
533 return (AE_NO_MEMORY);
536 /* Lock namespace for acpi_ns_lookup, we may be creating a new node */
538 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
539 if (ACPI_FAILURE(status)) {
540 goto error_exit;
543 /* The lookup either returns an existing node or creates a new one */
545 status =
546 acpi_ns_lookup(NULL, path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
547 ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND,
548 NULL, &node);
550 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
552 if (ACPI_FAILURE(status)) { /* ns_lookup */
553 if (status != AE_ALREADY_EXISTS) {
554 goto error_exit;
557 /* Node existed previously, make sure it is a method node */
559 if (node->type != ACPI_TYPE_METHOD) {
560 status = AE_TYPE;
561 goto error_exit;
565 /* Copy the method AML to the local buffer */
567 memcpy(aml_buffer, aml_start, aml_length);
569 /* Initialize the method object with the new method's information */
571 method_obj->method.aml_start = aml_buffer;
572 method_obj->method.aml_length = aml_length;
574 method_obj->method.param_count = (u8)
575 (method_flags & AML_METHOD_ARG_COUNT);
577 if (method_flags & AML_METHOD_SERIALIZED) {
578 method_obj->method.info_flags = ACPI_METHOD_SERIALIZED;
580 method_obj->method.sync_level = (u8)
581 ((method_flags & AML_METHOD_SYNC_LEVEL) >> 4);
585 * Now that it is complete, we can attach the new method object to
586 * the method Node (detaches/deletes any existing object)
588 status = acpi_ns_attach_object(node, method_obj, ACPI_TYPE_METHOD);
591 * Flag indicates AML buffer is dynamic, must be deleted later.
592 * Must be set only after attach above.
594 node->flags |= ANOBJ_ALLOCATED_BUFFER;
596 /* Remove local reference to the method object */
598 acpi_ut_remove_reference(method_obj);
599 return (status);
601 error_exit:
603 ACPI_FREE(aml_buffer);
604 ACPI_FREE(method_obj);
605 return (status);
607 ACPI_EXPORT_SYMBOL(acpi_install_method)