195b482a8SLen Brown /******************************************************************************* 295b482a8SLen Brown * 395b482a8SLen Brown * Module Name: nsxfeval - Public interfaces to the ACPI subsystem 495b482a8SLen Brown * ACPI Object evaluation interfaces 595b482a8SLen Brown * 695b482a8SLen Brown ******************************************************************************/ 795b482a8SLen Brown 895b482a8SLen Brown /* 995b482a8SLen Brown * Copyright (C) 2000 - 2008, Intel Corp. 1095b482a8SLen Brown * All rights reserved. 1195b482a8SLen Brown * 1295b482a8SLen Brown * Redistribution and use in source and binary forms, with or without 1395b482a8SLen Brown * modification, are permitted provided that the following conditions 1495b482a8SLen Brown * are met: 1595b482a8SLen Brown * 1. Redistributions of source code must retain the above copyright 1695b482a8SLen Brown * notice, this list of conditions, and the following disclaimer, 1795b482a8SLen Brown * without modification. 1895b482a8SLen Brown * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1995b482a8SLen Brown * substantially similar to the "NO WARRANTY" disclaimer below 2095b482a8SLen Brown * ("Disclaimer") and any redistribution must be conditioned upon 2195b482a8SLen Brown * including a substantially similar Disclaimer requirement for further 2295b482a8SLen Brown * binary redistribution. 2395b482a8SLen Brown * 3. Neither the names of the above-listed copyright holders nor the names 2495b482a8SLen Brown * of any contributors may be used to endorse or promote products derived 2595b482a8SLen Brown * from this software without specific prior written permission. 2695b482a8SLen Brown * 2795b482a8SLen Brown * Alternatively, this software may be distributed under the terms of the 2895b482a8SLen Brown * GNU General Public License ("GPL") version 2 as published by the Free 2995b482a8SLen Brown * Software Foundation. 3095b482a8SLen Brown * 3195b482a8SLen Brown * NO WARRANTY 3295b482a8SLen Brown * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 3395b482a8SLen Brown * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3495b482a8SLen Brown * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 3595b482a8SLen Brown * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3695b482a8SLen Brown * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3795b482a8SLen Brown * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3895b482a8SLen Brown * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3995b482a8SLen Brown * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4095b482a8SLen Brown * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 4195b482a8SLen Brown * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4295b482a8SLen Brown * POSSIBILITY OF SUCH DAMAGES. 4395b482a8SLen Brown */ 4495b482a8SLen Brown 4595b482a8SLen Brown #include <acpi/acpi.h> 46e2f7a777SLen Brown #include "accommon.h" 47e2f7a777SLen Brown #include "acnamesp.h" 48e2f7a777SLen Brown #include "acinterp.h" 4995b482a8SLen Brown 5095b482a8SLen Brown #define _COMPONENT ACPI_NAMESPACE 5195b482a8SLen Brown ACPI_MODULE_NAME("nsxfeval") 5295b482a8SLen Brown 5395b482a8SLen Brown /* Local prototypes */ 5495b482a8SLen Brown static void acpi_ns_resolve_references(struct acpi_evaluate_info *info); 5595b482a8SLen Brown 5695b482a8SLen Brown /******************************************************************************* 5795b482a8SLen Brown * 5895b482a8SLen Brown * FUNCTION: acpi_evaluate_object_typed 5995b482a8SLen Brown * 6095b482a8SLen Brown * PARAMETERS: Handle - Object handle (optional) 6195b482a8SLen Brown * Pathname - Object pathname (optional) 6295b482a8SLen Brown * external_params - List of parameters to pass to method, 6395b482a8SLen Brown * terminated by NULL. May be NULL 6495b482a8SLen Brown * if no parameters are being passed. 6595b482a8SLen Brown * return_buffer - Where to put method's return value (if 6695b482a8SLen Brown * any). If NULL, no value is returned. 6795b482a8SLen Brown * return_type - Expected type of return object 6895b482a8SLen Brown * 6995b482a8SLen Brown * RETURN: Status 7095b482a8SLen Brown * 7195b482a8SLen Brown * DESCRIPTION: Find and evaluate the given object, passing the given 7295b482a8SLen Brown * parameters if necessary. One of "Handle" or "Pathname" must 7395b482a8SLen Brown * be valid (non-null) 7495b482a8SLen Brown * 7595b482a8SLen Brown ******************************************************************************/ 7695b482a8SLen Brown 7795b482a8SLen Brown acpi_status 7895b482a8SLen Brown acpi_evaluate_object_typed(acpi_handle handle, 7995b482a8SLen Brown acpi_string pathname, 8095b482a8SLen Brown struct acpi_object_list *external_params, 8195b482a8SLen Brown struct acpi_buffer *return_buffer, 8295b482a8SLen Brown acpi_object_type return_type) 8395b482a8SLen Brown { 8495b482a8SLen Brown acpi_status status; 8595b482a8SLen Brown u8 must_free = FALSE; 8695b482a8SLen Brown 8795b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_evaluate_object_typed); 8895b482a8SLen Brown 8995b482a8SLen Brown /* Return buffer must be valid */ 9095b482a8SLen Brown 9195b482a8SLen Brown if (!return_buffer) { 9295b482a8SLen Brown return_ACPI_STATUS(AE_BAD_PARAMETER); 9395b482a8SLen Brown } 9495b482a8SLen Brown 9595b482a8SLen Brown if (return_buffer->length == ACPI_ALLOCATE_BUFFER) { 9695b482a8SLen Brown must_free = TRUE; 9795b482a8SLen Brown } 9895b482a8SLen Brown 9995b482a8SLen Brown /* Evaluate the object */ 10095b482a8SLen Brown 10195b482a8SLen Brown status = 10295b482a8SLen Brown acpi_evaluate_object(handle, pathname, external_params, 10395b482a8SLen Brown return_buffer); 10495b482a8SLen Brown if (ACPI_FAILURE(status)) { 10595b482a8SLen Brown return_ACPI_STATUS(status); 10695b482a8SLen Brown } 10795b482a8SLen Brown 10895b482a8SLen Brown /* Type ANY means "don't care" */ 10995b482a8SLen Brown 11095b482a8SLen Brown if (return_type == ACPI_TYPE_ANY) { 11195b482a8SLen Brown return_ACPI_STATUS(AE_OK); 11295b482a8SLen Brown } 11395b482a8SLen Brown 11495b482a8SLen Brown if (return_buffer->length == 0) { 11595b482a8SLen Brown 11695b482a8SLen Brown /* Error because caller specifically asked for a return value */ 11795b482a8SLen Brown 11895b482a8SLen Brown ACPI_ERROR((AE_INFO, "No return value")); 11995b482a8SLen Brown return_ACPI_STATUS(AE_NULL_OBJECT); 12095b482a8SLen Brown } 12195b482a8SLen Brown 12295b482a8SLen Brown /* Examine the object type returned from evaluate_object */ 12395b482a8SLen Brown 12495b482a8SLen Brown if (((union acpi_object *)return_buffer->pointer)->type == return_type) { 12595b482a8SLen Brown return_ACPI_STATUS(AE_OK); 12695b482a8SLen Brown } 12795b482a8SLen Brown 12895b482a8SLen Brown /* Return object type does not match requested type */ 12995b482a8SLen Brown 13095b482a8SLen Brown ACPI_ERROR((AE_INFO, 13195b482a8SLen Brown "Incorrect return type [%s] requested [%s]", 13295b482a8SLen Brown acpi_ut_get_type_name(((union acpi_object *)return_buffer-> 13395b482a8SLen Brown pointer)->type), 13495b482a8SLen Brown acpi_ut_get_type_name(return_type))); 13595b482a8SLen Brown 13695b482a8SLen Brown if (must_free) { 13795b482a8SLen Brown 13895b482a8SLen Brown /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */ 13995b482a8SLen Brown 14095b482a8SLen Brown ACPI_FREE(return_buffer->pointer); 14195b482a8SLen Brown return_buffer->pointer = NULL; 14295b482a8SLen Brown } 14395b482a8SLen Brown 14495b482a8SLen Brown return_buffer->length = 0; 14595b482a8SLen Brown return_ACPI_STATUS(AE_TYPE); 14695b482a8SLen Brown } 14795b482a8SLen Brown 14895b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_evaluate_object_typed) 149*2c03d07aSLuca Tettamanti 15095b482a8SLen Brown /******************************************************************************* 15195b482a8SLen Brown * 15295b482a8SLen Brown * FUNCTION: acpi_evaluate_object 15395b482a8SLen Brown * 15495b482a8SLen Brown * PARAMETERS: Handle - Object handle (optional) 15595b482a8SLen Brown * Pathname - Object pathname (optional) 15695b482a8SLen Brown * external_params - List of parameters to pass to method, 15795b482a8SLen Brown * terminated by NULL. May be NULL 15895b482a8SLen Brown * if no parameters are being passed. 15995b482a8SLen Brown * return_buffer - Where to put method's return value (if 16095b482a8SLen Brown * any). If NULL, no value is returned. 16195b482a8SLen Brown * 16295b482a8SLen Brown * RETURN: Status 16395b482a8SLen Brown * 16495b482a8SLen Brown * DESCRIPTION: Find and evaluate the given object, passing the given 16595b482a8SLen Brown * parameters if necessary. One of "Handle" or "Pathname" must 16695b482a8SLen Brown * be valid (non-null) 16795b482a8SLen Brown * 16895b482a8SLen Brown ******************************************************************************/ 16995b482a8SLen Brown acpi_status 17095b482a8SLen Brown acpi_evaluate_object(acpi_handle handle, 17195b482a8SLen Brown acpi_string pathname, 17295b482a8SLen Brown struct acpi_object_list *external_params, 17395b482a8SLen Brown struct acpi_buffer *return_buffer) 17495b482a8SLen Brown { 17595b482a8SLen Brown acpi_status status; 17695b482a8SLen Brown struct acpi_evaluate_info *info; 17795b482a8SLen Brown acpi_size buffer_space_needed; 17895b482a8SLen Brown u32 i; 17995b482a8SLen Brown 18095b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_evaluate_object); 18195b482a8SLen Brown 18295b482a8SLen Brown /* Allocate and initialize the evaluation information block */ 18395b482a8SLen Brown 18495b482a8SLen Brown info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info)); 18595b482a8SLen Brown if (!info) { 18695b482a8SLen Brown return_ACPI_STATUS(AE_NO_MEMORY); 18795b482a8SLen Brown } 18895b482a8SLen Brown 18995b482a8SLen Brown info->pathname = pathname; 19095b482a8SLen Brown 19195b482a8SLen Brown /* Convert and validate the device handle */ 19295b482a8SLen Brown 19395b482a8SLen Brown info->prefix_node = acpi_ns_map_handle_to_node(handle); 19495b482a8SLen Brown if (!info->prefix_node) { 19595b482a8SLen Brown status = AE_BAD_PARAMETER; 19695b482a8SLen Brown goto cleanup; 19795b482a8SLen Brown } 19895b482a8SLen Brown 19995b482a8SLen Brown /* 20095b482a8SLen Brown * If there are parameters to be passed to a control method, the external 20195b482a8SLen Brown * objects must all be converted to internal objects 20295b482a8SLen Brown */ 20395b482a8SLen Brown if (external_params && external_params->count) { 20495b482a8SLen Brown /* 20595b482a8SLen Brown * Allocate a new parameter block for the internal objects 20695b482a8SLen Brown * Add 1 to count to allow for null terminated internal list 20795b482a8SLen Brown */ 20895b482a8SLen Brown info->parameters = ACPI_ALLOCATE_ZEROED(((acpi_size) 20995b482a8SLen Brown external_params-> 21095b482a8SLen Brown count + 21195b482a8SLen Brown 1) * sizeof(void *)); 21295b482a8SLen Brown if (!info->parameters) { 21395b482a8SLen Brown status = AE_NO_MEMORY; 21495b482a8SLen Brown goto cleanup; 21595b482a8SLen Brown } 21695b482a8SLen Brown 21795b482a8SLen Brown /* Convert each external object in the list to an internal object */ 21895b482a8SLen Brown 21995b482a8SLen Brown for (i = 0; i < external_params->count; i++) { 22095b482a8SLen Brown status = 22195b482a8SLen Brown acpi_ut_copy_eobject_to_iobject(&external_params-> 22295b482a8SLen Brown pointer[i], 22395b482a8SLen Brown &info-> 22495b482a8SLen Brown parameters[i]); 22595b482a8SLen Brown if (ACPI_FAILURE(status)) { 22695b482a8SLen Brown goto cleanup; 22795b482a8SLen Brown } 22895b482a8SLen Brown } 22995b482a8SLen Brown info->parameters[external_params->count] = NULL; 23095b482a8SLen Brown } 23195b482a8SLen Brown 23295b482a8SLen Brown /* 23395b482a8SLen Brown * Three major cases: 23495b482a8SLen Brown * 1) Fully qualified pathname 23595b482a8SLen Brown * 2) No handle, not fully qualified pathname (error) 23695b482a8SLen Brown * 3) Valid handle 23795b482a8SLen Brown */ 23895b482a8SLen Brown if ((pathname) && (acpi_ns_valid_root_prefix(pathname[0]))) { 23995b482a8SLen Brown 24095b482a8SLen Brown /* The path is fully qualified, just evaluate by name */ 24195b482a8SLen Brown 24295b482a8SLen Brown info->prefix_node = NULL; 24395b482a8SLen Brown status = acpi_ns_evaluate(info); 24495b482a8SLen Brown } else if (!handle) { 24595b482a8SLen Brown /* 24695b482a8SLen Brown * A handle is optional iff a fully qualified pathname is specified. 24795b482a8SLen Brown * Since we've already handled fully qualified names above, this is 24895b482a8SLen Brown * an error 24995b482a8SLen Brown */ 25095b482a8SLen Brown if (!pathname) { 25195b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 25295b482a8SLen Brown "Both Handle and Pathname are NULL")); 25395b482a8SLen Brown } else { 25495b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 25595b482a8SLen Brown "Null Handle with relative pathname [%s]", 25695b482a8SLen Brown pathname)); 25795b482a8SLen Brown } 25895b482a8SLen Brown 25995b482a8SLen Brown status = AE_BAD_PARAMETER; 26095b482a8SLen Brown } else { 26195b482a8SLen Brown /* We have a namespace a node and a possible relative path */ 26295b482a8SLen Brown 26395b482a8SLen Brown status = acpi_ns_evaluate(info); 26495b482a8SLen Brown } 26595b482a8SLen Brown 26695b482a8SLen Brown /* 26795b482a8SLen Brown * If we are expecting a return value, and all went well above, 26895b482a8SLen Brown * copy the return value to an external object. 26995b482a8SLen Brown */ 27095b482a8SLen Brown if (return_buffer) { 27195b482a8SLen Brown if (!info->return_object) { 27295b482a8SLen Brown return_buffer->length = 0; 27395b482a8SLen Brown } else { 27495b482a8SLen Brown if (ACPI_GET_DESCRIPTOR_TYPE(info->return_object) == 27595b482a8SLen Brown ACPI_DESC_TYPE_NAMED) { 27695b482a8SLen Brown /* 27795b482a8SLen Brown * If we received a NS Node as a return object, this means that 27895b482a8SLen Brown * the object we are evaluating has nothing interesting to 27995b482a8SLen Brown * return (such as a mutex, etc.) We return an error because 28095b482a8SLen Brown * these types are essentially unsupported by this interface. 28195b482a8SLen Brown * We don't check up front because this makes it easier to add 28295b482a8SLen Brown * support for various types at a later date if necessary. 28395b482a8SLen Brown */ 28495b482a8SLen Brown status = AE_TYPE; 28595b482a8SLen Brown info->return_object = NULL; /* No need to delete a NS Node */ 28695b482a8SLen Brown return_buffer->length = 0; 28795b482a8SLen Brown } 28895b482a8SLen Brown 28995b482a8SLen Brown if (ACPI_SUCCESS(status)) { 29095b482a8SLen Brown 29195b482a8SLen Brown /* Dereference Index and ref_of references */ 29295b482a8SLen Brown 29395b482a8SLen Brown acpi_ns_resolve_references(info); 29495b482a8SLen Brown 29595b482a8SLen Brown /* Get the size of the returned object */ 29695b482a8SLen Brown 29795b482a8SLen Brown status = 29895b482a8SLen Brown acpi_ut_get_object_size(info->return_object, 29995b482a8SLen Brown &buffer_space_needed); 30095b482a8SLen Brown if (ACPI_SUCCESS(status)) { 30195b482a8SLen Brown 30295b482a8SLen Brown /* Validate/Allocate/Clear caller buffer */ 30395b482a8SLen Brown 30495b482a8SLen Brown status = 30595b482a8SLen Brown acpi_ut_initialize_buffer 30695b482a8SLen Brown (return_buffer, 30795b482a8SLen Brown buffer_space_needed); 30895b482a8SLen Brown if (ACPI_FAILURE(status)) { 30995b482a8SLen Brown /* 31095b482a8SLen Brown * Caller's buffer is too small or a new one can't 31195b482a8SLen Brown * be allocated 31295b482a8SLen Brown */ 31395b482a8SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 31495b482a8SLen Brown "Needed buffer size %X, %s\n", 31595b482a8SLen Brown (u32) 31695b482a8SLen Brown buffer_space_needed, 31795b482a8SLen Brown acpi_format_exception 31895b482a8SLen Brown (status))); 31995b482a8SLen Brown } else { 32095b482a8SLen Brown /* We have enough space for the object, build it */ 32195b482a8SLen Brown 32295b482a8SLen Brown status = 32395b482a8SLen Brown acpi_ut_copy_iobject_to_eobject 32495b482a8SLen Brown (info->return_object, 32595b482a8SLen Brown return_buffer); 32695b482a8SLen Brown } 32795b482a8SLen Brown } 32895b482a8SLen Brown } 32995b482a8SLen Brown } 33095b482a8SLen Brown } 33195b482a8SLen Brown 33295b482a8SLen Brown if (info->return_object) { 33395b482a8SLen Brown /* 33495b482a8SLen Brown * Delete the internal return object. NOTE: Interpreter must be 33595b482a8SLen Brown * locked to avoid race condition. 33695b482a8SLen Brown */ 33795b482a8SLen Brown acpi_ex_enter_interpreter(); 33895b482a8SLen Brown 33995b482a8SLen Brown /* Remove one reference on the return object (should delete it) */ 34095b482a8SLen Brown 34195b482a8SLen Brown acpi_ut_remove_reference(info->return_object); 34295b482a8SLen Brown acpi_ex_exit_interpreter(); 34395b482a8SLen Brown } 34495b482a8SLen Brown 34595b482a8SLen Brown cleanup: 34695b482a8SLen Brown 34795b482a8SLen Brown /* Free the input parameter list (if we created one) */ 34895b482a8SLen Brown 34995b482a8SLen Brown if (info->parameters) { 35095b482a8SLen Brown 35195b482a8SLen Brown /* Free the allocated parameter block */ 35295b482a8SLen Brown 35395b482a8SLen Brown acpi_ut_delete_internal_object_list(info->parameters); 35495b482a8SLen Brown } 35595b482a8SLen Brown 35695b482a8SLen Brown ACPI_FREE(info); 35795b482a8SLen Brown return_ACPI_STATUS(status); 35895b482a8SLen Brown } 35995b482a8SLen Brown 36095b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_evaluate_object) 36195b482a8SLen Brown 36295b482a8SLen Brown /******************************************************************************* 36395b482a8SLen Brown * 36495b482a8SLen Brown * FUNCTION: acpi_ns_resolve_references 36595b482a8SLen Brown * 36695b482a8SLen Brown * PARAMETERS: Info - Evaluation info block 36795b482a8SLen Brown * 36895b482a8SLen Brown * RETURN: Info->return_object is replaced with the dereferenced object 36995b482a8SLen Brown * 37095b482a8SLen Brown * DESCRIPTION: Dereference certain reference objects. Called before an 37195b482a8SLen Brown * internal return object is converted to an external union acpi_object. 37295b482a8SLen Brown * 37395b482a8SLen Brown * Performs an automatic dereference of Index and ref_of reference objects. 37495b482a8SLen Brown * These reference objects are not supported by the union acpi_object, so this is a 37595b482a8SLen Brown * last resort effort to return something useful. Also, provides compatibility 37695b482a8SLen Brown * with other ACPI implementations. 37795b482a8SLen Brown * 37895b482a8SLen Brown * NOTE: does not handle references within returned package objects or nested 37995b482a8SLen Brown * references, but this support could be added later if found to be necessary. 38095b482a8SLen Brown * 38195b482a8SLen Brown ******************************************************************************/ 38295b482a8SLen Brown static void acpi_ns_resolve_references(struct acpi_evaluate_info *info) 38395b482a8SLen Brown { 38495b482a8SLen Brown union acpi_operand_object *obj_desc = NULL; 38595b482a8SLen Brown struct acpi_namespace_node *node; 38695b482a8SLen Brown 38795b482a8SLen Brown /* We are interested in reference objects only */ 38895b482a8SLen Brown 3893371c19cSBob Moore if ((info->return_object)->common.type != ACPI_TYPE_LOCAL_REFERENCE) { 39095b482a8SLen Brown return; 39195b482a8SLen Brown } 39295b482a8SLen Brown 39395b482a8SLen Brown /* 39495b482a8SLen Brown * Two types of references are supported - those created by Index and 39595b482a8SLen Brown * ref_of operators. A name reference (AML_NAMEPATH_OP) can be converted 39695b482a8SLen Brown * to an union acpi_object, so it is not dereferenced here. A ddb_handle 39795b482a8SLen Brown * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to 39895b482a8SLen Brown * an union acpi_object. 39995b482a8SLen Brown */ 40095b482a8SLen Brown switch (info->return_object->reference.class) { 40195b482a8SLen Brown case ACPI_REFCLASS_INDEX: 40295b482a8SLen Brown 40395b482a8SLen Brown obj_desc = *(info->return_object->reference.where); 40495b482a8SLen Brown break; 40595b482a8SLen Brown 40695b482a8SLen Brown case ACPI_REFCLASS_REFOF: 40795b482a8SLen Brown 40895b482a8SLen Brown node = info->return_object->reference.object; 40995b482a8SLen Brown if (node) { 41095b482a8SLen Brown obj_desc = node->object; 41195b482a8SLen Brown } 41295b482a8SLen Brown break; 41395b482a8SLen Brown 41495b482a8SLen Brown default: 41595b482a8SLen Brown return; 41695b482a8SLen Brown } 41795b482a8SLen Brown 41895b482a8SLen Brown /* Replace the existing reference object */ 41995b482a8SLen Brown 42095b482a8SLen Brown if (obj_desc) { 42195b482a8SLen Brown acpi_ut_add_reference(obj_desc); 42295b482a8SLen Brown acpi_ut_remove_reference(info->return_object); 42395b482a8SLen Brown info->return_object = obj_desc; 42495b482a8SLen Brown } 42595b482a8SLen Brown 42695b482a8SLen Brown return; 42795b482a8SLen Brown } 42895b482a8SLen Brown 42995b482a8SLen Brown /******************************************************************************* 43095b482a8SLen Brown * 43195b482a8SLen Brown * FUNCTION: acpi_walk_namespace 43295b482a8SLen Brown * 43395b482a8SLen Brown * PARAMETERS: Type - acpi_object_type to search for 43495b482a8SLen Brown * start_object - Handle in namespace where search begins 43595b482a8SLen Brown * max_depth - Depth to which search is to reach 43695b482a8SLen Brown * user_function - Called when an object of "Type" is found 43795b482a8SLen Brown * Context - Passed to user function 43895b482a8SLen Brown * return_value - Location where return value of 43995b482a8SLen Brown * user_function is put if terminated early 44095b482a8SLen Brown * 44195b482a8SLen Brown * RETURNS Return value from the user_function if terminated early. 44295b482a8SLen Brown * Otherwise, returns NULL. 44395b482a8SLen Brown * 44495b482a8SLen Brown * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, 44595b482a8SLen Brown * starting (and ending) at the object specified by start_handle. 44695b482a8SLen Brown * The user_function is called whenever an object that matches 44795b482a8SLen Brown * the type parameter is found. If the user function returns 44895b482a8SLen Brown * a non-zero value, the search is terminated immediately and this 44995b482a8SLen Brown * value is returned to the caller. 45095b482a8SLen Brown * 45195b482a8SLen Brown * The point of this procedure is to provide a generic namespace 45295b482a8SLen Brown * walk routine that can be called from multiple places to 45395b482a8SLen Brown * provide multiple services; the User Function can be tailored 45495b482a8SLen Brown * to each task, whether it is a print function, a compare 45595b482a8SLen Brown * function, etc. 45695b482a8SLen Brown * 45795b482a8SLen Brown ******************************************************************************/ 45895b482a8SLen Brown 45995b482a8SLen Brown acpi_status 46095b482a8SLen Brown acpi_walk_namespace(acpi_object_type type, 46195b482a8SLen Brown acpi_handle start_object, 46295b482a8SLen Brown u32 max_depth, 46395b482a8SLen Brown acpi_walk_callback user_function, 46495b482a8SLen Brown void *context, void **return_value) 46595b482a8SLen Brown { 46695b482a8SLen Brown acpi_status status; 46795b482a8SLen Brown 46895b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_walk_namespace); 46995b482a8SLen Brown 47095b482a8SLen Brown /* Parameter validation */ 47195b482a8SLen Brown 47295b482a8SLen Brown if ((type > ACPI_TYPE_LOCAL_MAX) || (!max_depth) || (!user_function)) { 47395b482a8SLen Brown return_ACPI_STATUS(AE_BAD_PARAMETER); 47495b482a8SLen Brown } 47595b482a8SLen Brown 47695b482a8SLen Brown /* 4778a335a23SBob Moore * Need to acquire the namespace reader lock to prevent interference 4788a335a23SBob Moore * with any concurrent table unloads (which causes the deletion of 4798a335a23SBob Moore * namespace objects). We cannot allow the deletion of a namespace node 4808a335a23SBob Moore * while the user function is using it. The exception to this are the 4818a335a23SBob Moore * nodes created and deleted during control method execution -- these 4828a335a23SBob Moore * nodes are marked as temporary nodes and are ignored by the namespace 4838a335a23SBob Moore * walk. Thus, control methods can be executed while holding the 4848a335a23SBob Moore * namespace deletion lock (and the user function can execute control 4858a335a23SBob Moore * methods.) 4868a335a23SBob Moore */ 4878a335a23SBob Moore status = acpi_ut_acquire_read_lock(&acpi_gbl_namespace_rw_lock); 4888a335a23SBob Moore if (ACPI_FAILURE(status)) { 4898a335a23SBob Moore return status; 4908a335a23SBob Moore } 4918a335a23SBob Moore 4928a335a23SBob Moore /* 4938a335a23SBob Moore * Lock the namespace around the walk. The namespace will be 4948a335a23SBob Moore * unlocked/locked around each call to the user function - since the user 4958a335a23SBob Moore * function must be allowed to make ACPICA calls itself (for example, it 4968a335a23SBob Moore * will typically execute control methods during device enumeration.) 49795b482a8SLen Brown */ 49895b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 49995b482a8SLen Brown if (ACPI_FAILURE(status)) { 5008a335a23SBob Moore goto unlock_and_exit; 50195b482a8SLen Brown } 50295b482a8SLen Brown 50395b482a8SLen Brown status = acpi_ns_walk_namespace(type, start_object, max_depth, 5048a335a23SBob Moore ACPI_NS_WALK_UNLOCK, user_function, 5058a335a23SBob Moore context, return_value); 50695b482a8SLen Brown 50795b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 5088a335a23SBob Moore 5098a335a23SBob Moore unlock_and_exit: 5108a335a23SBob Moore (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock); 51195b482a8SLen Brown return_ACPI_STATUS(status); 51295b482a8SLen Brown } 51395b482a8SLen Brown 51495b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_walk_namespace) 51595b482a8SLen Brown 51695b482a8SLen Brown /******************************************************************************* 51795b482a8SLen Brown * 51895b482a8SLen Brown * FUNCTION: acpi_ns_get_device_callback 51995b482a8SLen Brown * 52095b482a8SLen Brown * PARAMETERS: Callback from acpi_get_device 52195b482a8SLen Brown * 52295b482a8SLen Brown * RETURN: Status 52395b482a8SLen Brown * 52495b482a8SLen Brown * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non- 52595b482a8SLen Brown * present devices, or if they specified a HID, it filters based 52695b482a8SLen Brown * on that. 52795b482a8SLen Brown * 52895b482a8SLen Brown ******************************************************************************/ 52995b482a8SLen Brown static acpi_status 53095b482a8SLen Brown acpi_ns_get_device_callback(acpi_handle obj_handle, 53195b482a8SLen Brown u32 nesting_level, 53295b482a8SLen Brown void *context, void **return_value) 53395b482a8SLen Brown { 53495b482a8SLen Brown struct acpi_get_devices_info *info = context; 53595b482a8SLen Brown acpi_status status; 53695b482a8SLen Brown struct acpi_namespace_node *node; 53795b482a8SLen Brown u32 flags; 53895b482a8SLen Brown struct acpica_device_id hid; 53995b482a8SLen Brown struct acpi_compatible_id_list *cid; 54095b482a8SLen Brown u32 i; 54195b482a8SLen Brown int found; 54295b482a8SLen Brown 54395b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 54495b482a8SLen Brown if (ACPI_FAILURE(status)) { 54595b482a8SLen Brown return (status); 54695b482a8SLen Brown } 54795b482a8SLen Brown 54895b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 54995b482a8SLen Brown status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 55095b482a8SLen Brown if (ACPI_FAILURE(status)) { 55195b482a8SLen Brown return (status); 55295b482a8SLen Brown } 55395b482a8SLen Brown 55495b482a8SLen Brown if (!node) { 55595b482a8SLen Brown return (AE_BAD_PARAMETER); 55695b482a8SLen Brown } 55795b482a8SLen Brown 55895b482a8SLen Brown /* Run _STA to determine if device is present */ 55995b482a8SLen Brown 56095b482a8SLen Brown status = acpi_ut_execute_STA(node, &flags); 56195b482a8SLen Brown if (ACPI_FAILURE(status)) { 56295b482a8SLen Brown return (AE_CTRL_DEPTH); 56395b482a8SLen Brown } 56495b482a8SLen Brown 56595b482a8SLen Brown if (!(flags & ACPI_STA_DEVICE_PRESENT) && 56695b482a8SLen Brown !(flags & ACPI_STA_DEVICE_FUNCTIONING)) { 56795b482a8SLen Brown /* 56895b482a8SLen Brown * Don't examine the children of the device only when the 56995b482a8SLen Brown * device is neither present nor functional. See ACPI spec, 57095b482a8SLen Brown * description of _STA for more information. 57195b482a8SLen Brown */ 57295b482a8SLen Brown return (AE_CTRL_DEPTH); 57395b482a8SLen Brown } 57495b482a8SLen Brown 57595b482a8SLen Brown /* Filter based on device HID & CID */ 57695b482a8SLen Brown 57795b482a8SLen Brown if (info->hid != NULL) { 57895b482a8SLen Brown status = acpi_ut_execute_HID(node, &hid); 57995b482a8SLen Brown if (status == AE_NOT_FOUND) { 58095b482a8SLen Brown return (AE_OK); 58195b482a8SLen Brown } else if (ACPI_FAILURE(status)) { 58295b482a8SLen Brown return (AE_CTRL_DEPTH); 58395b482a8SLen Brown } 58495b482a8SLen Brown 58595b482a8SLen Brown if (ACPI_STRNCMP(hid.value, info->hid, sizeof(hid.value)) != 0) { 58695b482a8SLen Brown 58795b482a8SLen Brown /* Get the list of Compatible IDs */ 58895b482a8SLen Brown 58995b482a8SLen Brown status = acpi_ut_execute_CID(node, &cid); 59095b482a8SLen Brown if (status == AE_NOT_FOUND) { 59195b482a8SLen Brown return (AE_OK); 59295b482a8SLen Brown } else if (ACPI_FAILURE(status)) { 59395b482a8SLen Brown return (AE_CTRL_DEPTH); 59495b482a8SLen Brown } 59595b482a8SLen Brown 59695b482a8SLen Brown /* Walk the CID list */ 59795b482a8SLen Brown 59895b482a8SLen Brown found = 0; 59995b482a8SLen Brown for (i = 0; i < cid->count; i++) { 60095b482a8SLen Brown if (ACPI_STRNCMP(cid->id[i].value, info->hid, 60195b482a8SLen Brown sizeof(struct 60295b482a8SLen Brown acpi_compatible_id)) == 60395b482a8SLen Brown 0) { 60495b482a8SLen Brown found = 1; 60595b482a8SLen Brown break; 60695b482a8SLen Brown } 60795b482a8SLen Brown } 60895b482a8SLen Brown ACPI_FREE(cid); 60995b482a8SLen Brown if (!found) 61095b482a8SLen Brown return (AE_OK); 61195b482a8SLen Brown } 61295b482a8SLen Brown } 61395b482a8SLen Brown 61495b482a8SLen Brown status = info->user_function(obj_handle, nesting_level, info->context, 61595b482a8SLen Brown return_value); 61695b482a8SLen Brown return (status); 61795b482a8SLen Brown } 61895b482a8SLen Brown 61995b482a8SLen Brown /******************************************************************************* 62095b482a8SLen Brown * 62195b482a8SLen Brown * FUNCTION: acpi_get_devices 62295b482a8SLen Brown * 62395b482a8SLen Brown * PARAMETERS: HID - HID to search for. Can be NULL. 62495b482a8SLen Brown * user_function - Called when a matching object is found 62595b482a8SLen Brown * Context - Passed to user function 62695b482a8SLen Brown * return_value - Location where return value of 62795b482a8SLen Brown * user_function is put if terminated early 62895b482a8SLen Brown * 62995b482a8SLen Brown * RETURNS Return value from the user_function if terminated early. 63095b482a8SLen Brown * Otherwise, returns NULL. 63195b482a8SLen Brown * 63295b482a8SLen Brown * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, 63395b482a8SLen Brown * starting (and ending) at the object specified by start_handle. 63495b482a8SLen Brown * The user_function is called whenever an object of type 63595b482a8SLen Brown * Device is found. If the user function returns 63695b482a8SLen Brown * a non-zero value, the search is terminated immediately and this 63795b482a8SLen Brown * value is returned to the caller. 63895b482a8SLen Brown * 63995b482a8SLen Brown * This is a wrapper for walk_namespace, but the callback performs 64095b482a8SLen Brown * additional filtering. Please see acpi_ns_get_device_callback. 64195b482a8SLen Brown * 64295b482a8SLen Brown ******************************************************************************/ 64395b482a8SLen Brown 64495b482a8SLen Brown acpi_status 64595b482a8SLen Brown acpi_get_devices(const char *HID, 64695b482a8SLen Brown acpi_walk_callback user_function, 64795b482a8SLen Brown void *context, void **return_value) 64895b482a8SLen Brown { 64995b482a8SLen Brown acpi_status status; 65095b482a8SLen Brown struct acpi_get_devices_info info; 65195b482a8SLen Brown 65295b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_get_devices); 65395b482a8SLen Brown 65495b482a8SLen Brown /* Parameter validation */ 65595b482a8SLen Brown 65695b482a8SLen Brown if (!user_function) { 65795b482a8SLen Brown return_ACPI_STATUS(AE_BAD_PARAMETER); 65895b482a8SLen Brown } 65995b482a8SLen Brown 66095b482a8SLen Brown /* 66195b482a8SLen Brown * We're going to call their callback from OUR callback, so we need 66295b482a8SLen Brown * to know what it is, and their context parameter. 66395b482a8SLen Brown */ 66495b482a8SLen Brown info.hid = HID; 66595b482a8SLen Brown info.context = context; 66695b482a8SLen Brown info.user_function = user_function; 66795b482a8SLen Brown 66895b482a8SLen Brown /* 66995b482a8SLen Brown * Lock the namespace around the walk. 67095b482a8SLen Brown * The namespace will be unlocked/locked around each call 67195b482a8SLen Brown * to the user function - since this function 67295b482a8SLen Brown * must be allowed to make Acpi calls itself. 67395b482a8SLen Brown */ 67495b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 67595b482a8SLen Brown if (ACPI_FAILURE(status)) { 67695b482a8SLen Brown return_ACPI_STATUS(status); 67795b482a8SLen Brown } 67895b482a8SLen Brown 67995b482a8SLen Brown status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 68095b482a8SLen Brown ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK, 68195b482a8SLen Brown acpi_ns_get_device_callback, &info, 68295b482a8SLen Brown return_value); 68395b482a8SLen Brown 68495b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 68595b482a8SLen Brown return_ACPI_STATUS(status); 68695b482a8SLen Brown } 68795b482a8SLen Brown 68895b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_get_devices) 68995b482a8SLen Brown 69095b482a8SLen Brown /******************************************************************************* 69195b482a8SLen Brown * 69295b482a8SLen Brown * FUNCTION: acpi_attach_data 69395b482a8SLen Brown * 69495b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node 69595b482a8SLen Brown * Handler - Handler for this attachment 69695b482a8SLen Brown * Data - Pointer to data to be attached 69795b482a8SLen Brown * 69895b482a8SLen Brown * RETURN: Status 69995b482a8SLen Brown * 70095b482a8SLen Brown * DESCRIPTION: Attach arbitrary data and handler to a namespace node. 70195b482a8SLen Brown * 70295b482a8SLen Brown ******************************************************************************/ 70395b482a8SLen Brown acpi_status 70495b482a8SLen Brown acpi_attach_data(acpi_handle obj_handle, 70595b482a8SLen Brown acpi_object_handler handler, void *data) 70695b482a8SLen Brown { 70795b482a8SLen Brown struct acpi_namespace_node *node; 70895b482a8SLen Brown acpi_status status; 70995b482a8SLen Brown 71095b482a8SLen Brown /* Parameter validation */ 71195b482a8SLen Brown 71295b482a8SLen Brown if (!obj_handle || !handler || !data) { 71395b482a8SLen Brown return (AE_BAD_PARAMETER); 71495b482a8SLen Brown } 71595b482a8SLen Brown 71695b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 71795b482a8SLen Brown if (ACPI_FAILURE(status)) { 71895b482a8SLen Brown return (status); 71995b482a8SLen Brown } 72095b482a8SLen Brown 72195b482a8SLen Brown /* Convert and validate the handle */ 72295b482a8SLen Brown 72395b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 72495b482a8SLen Brown if (!node) { 72595b482a8SLen Brown status = AE_BAD_PARAMETER; 72695b482a8SLen Brown goto unlock_and_exit; 72795b482a8SLen Brown } 72895b482a8SLen Brown 72995b482a8SLen Brown status = acpi_ns_attach_data(node, handler, data); 73095b482a8SLen Brown 73195b482a8SLen Brown unlock_and_exit: 73295b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 73395b482a8SLen Brown return (status); 73495b482a8SLen Brown } 73595b482a8SLen Brown 73695b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_attach_data) 73795b482a8SLen Brown 73895b482a8SLen Brown /******************************************************************************* 73995b482a8SLen Brown * 74095b482a8SLen Brown * FUNCTION: acpi_detach_data 74195b482a8SLen Brown * 74295b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node handle 74395b482a8SLen Brown * Handler - Handler used in call to acpi_attach_data 74495b482a8SLen Brown * 74595b482a8SLen Brown * RETURN: Status 74695b482a8SLen Brown * 74795b482a8SLen Brown * DESCRIPTION: Remove data that was previously attached to a node. 74895b482a8SLen Brown * 74995b482a8SLen Brown ******************************************************************************/ 75095b482a8SLen Brown acpi_status 75195b482a8SLen Brown acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler) 75295b482a8SLen Brown { 75395b482a8SLen Brown struct acpi_namespace_node *node; 75495b482a8SLen Brown acpi_status status; 75595b482a8SLen Brown 75695b482a8SLen Brown /* Parameter validation */ 75795b482a8SLen Brown 75895b482a8SLen Brown if (!obj_handle || !handler) { 75995b482a8SLen Brown return (AE_BAD_PARAMETER); 76095b482a8SLen Brown } 76195b482a8SLen Brown 76295b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 76395b482a8SLen Brown if (ACPI_FAILURE(status)) { 76495b482a8SLen Brown return (status); 76595b482a8SLen Brown } 76695b482a8SLen Brown 76795b482a8SLen Brown /* Convert and validate the handle */ 76895b482a8SLen Brown 76995b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 77095b482a8SLen Brown if (!node) { 77195b482a8SLen Brown status = AE_BAD_PARAMETER; 77295b482a8SLen Brown goto unlock_and_exit; 77395b482a8SLen Brown } 77495b482a8SLen Brown 77595b482a8SLen Brown status = acpi_ns_detach_data(node, handler); 77695b482a8SLen Brown 77795b482a8SLen Brown unlock_and_exit: 77895b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 77995b482a8SLen Brown return (status); 78095b482a8SLen Brown } 78195b482a8SLen Brown 78295b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_detach_data) 78395b482a8SLen Brown 78495b482a8SLen Brown /******************************************************************************* 78595b482a8SLen Brown * 78695b482a8SLen Brown * FUNCTION: acpi_get_data 78795b482a8SLen Brown * 78895b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node 78995b482a8SLen Brown * Handler - Handler used in call to attach_data 79095b482a8SLen Brown * Data - Where the data is returned 79195b482a8SLen Brown * 79295b482a8SLen Brown * RETURN: Status 79395b482a8SLen Brown * 79495b482a8SLen Brown * DESCRIPTION: Retrieve data that was previously attached to a namespace node. 79595b482a8SLen Brown * 79695b482a8SLen Brown ******************************************************************************/ 79795b482a8SLen Brown acpi_status 79895b482a8SLen Brown acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data) 79995b482a8SLen Brown { 80095b482a8SLen Brown struct acpi_namespace_node *node; 80195b482a8SLen Brown acpi_status status; 80295b482a8SLen Brown 80395b482a8SLen Brown /* Parameter validation */ 80495b482a8SLen Brown 80595b482a8SLen Brown if (!obj_handle || !handler || !data) { 80695b482a8SLen Brown return (AE_BAD_PARAMETER); 80795b482a8SLen Brown } 80895b482a8SLen Brown 80995b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 81095b482a8SLen Brown if (ACPI_FAILURE(status)) { 81195b482a8SLen Brown return (status); 81295b482a8SLen Brown } 81395b482a8SLen Brown 81495b482a8SLen Brown /* Convert and validate the handle */ 81595b482a8SLen Brown 81695b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 81795b482a8SLen Brown if (!node) { 81895b482a8SLen Brown status = AE_BAD_PARAMETER; 81995b482a8SLen Brown goto unlock_and_exit; 82095b482a8SLen Brown } 82195b482a8SLen Brown 82295b482a8SLen Brown status = acpi_ns_get_attached_data(node, handler, data); 82395b482a8SLen Brown 82495b482a8SLen Brown unlock_and_exit: 82595b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 82695b482a8SLen Brown return (status); 82795b482a8SLen Brown } 82895b482a8SLen Brown 82995b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_get_data) 830