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) 1492c03d07aSLuca 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 436*2263576cSLin Ming * pre_order_visit - Called during tree pre-order visit 437*2263576cSLin Ming * when an object of "Type" is found 438*2263576cSLin Ming * post_order_visit - Called during tree post-order visit 439*2263576cSLin Ming * when an object of "Type" is found 440*2263576cSLin Ming * Context - Passed to user function(s) above 44195b482a8SLen Brown * return_value - Location where return value of 44295b482a8SLen Brown * user_function is put if terminated early 44395b482a8SLen Brown * 44495b482a8SLen Brown * RETURNS Return value from the user_function if terminated early. 44595b482a8SLen Brown * Otherwise, returns NULL. 44695b482a8SLen Brown * 44795b482a8SLen Brown * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, 44895b482a8SLen Brown * starting (and ending) at the object specified by start_handle. 449*2263576cSLin Ming * The callback function is called whenever an object that matches 450*2263576cSLin Ming * the type parameter is found. If the callback function returns 45195b482a8SLen Brown * a non-zero value, the search is terminated immediately and this 45295b482a8SLen Brown * value is returned to the caller. 45395b482a8SLen Brown * 45495b482a8SLen Brown * The point of this procedure is to provide a generic namespace 45595b482a8SLen Brown * walk routine that can be called from multiple places to 456*2263576cSLin Ming * provide multiple services; the callback function(s) can be 457*2263576cSLin Ming * tailored to each task, whether it is a print function, 458*2263576cSLin Ming * a compare function, etc. 45995b482a8SLen Brown * 46095b482a8SLen Brown ******************************************************************************/ 46195b482a8SLen Brown 46295b482a8SLen Brown acpi_status 46395b482a8SLen Brown acpi_walk_namespace(acpi_object_type type, 46495b482a8SLen Brown acpi_handle start_object, 46595b482a8SLen Brown u32 max_depth, 466*2263576cSLin Ming acpi_walk_callback pre_order_visit, 467*2263576cSLin Ming acpi_walk_callback post_order_visit, 46895b482a8SLen Brown void *context, void **return_value) 46995b482a8SLen Brown { 47095b482a8SLen Brown acpi_status status; 47195b482a8SLen Brown 47295b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_walk_namespace); 47395b482a8SLen Brown 47495b482a8SLen Brown /* Parameter validation */ 47595b482a8SLen Brown 476*2263576cSLin Ming if ((type > ACPI_TYPE_LOCAL_MAX) || 477*2263576cSLin Ming (!max_depth) || (!pre_order_visit && !post_order_visit)) { 47895b482a8SLen Brown return_ACPI_STATUS(AE_BAD_PARAMETER); 47995b482a8SLen Brown } 48095b482a8SLen Brown 48195b482a8SLen Brown /* 4828a335a23SBob Moore * Need to acquire the namespace reader lock to prevent interference 4838a335a23SBob Moore * with any concurrent table unloads (which causes the deletion of 4848a335a23SBob Moore * namespace objects). We cannot allow the deletion of a namespace node 4858a335a23SBob Moore * while the user function is using it. The exception to this are the 4868a335a23SBob Moore * nodes created and deleted during control method execution -- these 4878a335a23SBob Moore * nodes are marked as temporary nodes and are ignored by the namespace 4888a335a23SBob Moore * walk. Thus, control methods can be executed while holding the 4898a335a23SBob Moore * namespace deletion lock (and the user function can execute control 4908a335a23SBob Moore * methods.) 4918a335a23SBob Moore */ 4928a335a23SBob Moore status = acpi_ut_acquire_read_lock(&acpi_gbl_namespace_rw_lock); 4938a335a23SBob Moore if (ACPI_FAILURE(status)) { 4948a335a23SBob Moore return status; 4958a335a23SBob Moore } 4968a335a23SBob Moore 4978a335a23SBob Moore /* 4988a335a23SBob Moore * Lock the namespace around the walk. The namespace will be 4998a335a23SBob Moore * unlocked/locked around each call to the user function - since the user 5008a335a23SBob Moore * function must be allowed to make ACPICA calls itself (for example, it 5018a335a23SBob Moore * will typically execute control methods during device enumeration.) 50295b482a8SLen Brown */ 50395b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 50495b482a8SLen Brown if (ACPI_FAILURE(status)) { 5058a335a23SBob Moore goto unlock_and_exit; 50695b482a8SLen Brown } 50795b482a8SLen Brown 50895b482a8SLen Brown status = acpi_ns_walk_namespace(type, start_object, max_depth, 509*2263576cSLin Ming ACPI_NS_WALK_UNLOCK, pre_order_visit, 510*2263576cSLin Ming post_order_visit, context, 511*2263576cSLin Ming return_value); 51295b482a8SLen Brown 51395b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 5148a335a23SBob Moore 5158a335a23SBob Moore unlock_and_exit: 5168a335a23SBob Moore (void)acpi_ut_release_read_lock(&acpi_gbl_namespace_rw_lock); 51795b482a8SLen Brown return_ACPI_STATUS(status); 51895b482a8SLen Brown } 51995b482a8SLen Brown 52095b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_walk_namespace) 52195b482a8SLen Brown 52295b482a8SLen Brown /******************************************************************************* 52395b482a8SLen Brown * 52495b482a8SLen Brown * FUNCTION: acpi_ns_get_device_callback 52595b482a8SLen Brown * 52695b482a8SLen Brown * PARAMETERS: Callback from acpi_get_device 52795b482a8SLen Brown * 52895b482a8SLen Brown * RETURN: Status 52995b482a8SLen Brown * 53095b482a8SLen Brown * DESCRIPTION: Takes callbacks from walk_namespace and filters out all non- 53195b482a8SLen Brown * present devices, or if they specified a HID, it filters based 53295b482a8SLen Brown * on that. 53395b482a8SLen Brown * 53495b482a8SLen Brown ******************************************************************************/ 53595b482a8SLen Brown static acpi_status 53695b482a8SLen Brown acpi_ns_get_device_callback(acpi_handle obj_handle, 53795b482a8SLen Brown u32 nesting_level, 53895b482a8SLen Brown void *context, void **return_value) 53995b482a8SLen Brown { 54095b482a8SLen Brown struct acpi_get_devices_info *info = context; 54195b482a8SLen Brown acpi_status status; 54295b482a8SLen Brown struct acpi_namespace_node *node; 54395b482a8SLen Brown u32 flags; 54415b8dd53SBob Moore struct acpica_device_id *hid; 54515b8dd53SBob Moore struct acpica_device_id_list *cid; 54695b482a8SLen Brown u32 i; 54715b8dd53SBob Moore u8 found; 54815b8dd53SBob Moore int no_match; 54995b482a8SLen Brown 55095b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 55195b482a8SLen Brown if (ACPI_FAILURE(status)) { 55295b482a8SLen Brown return (status); 55395b482a8SLen Brown } 55495b482a8SLen Brown 55595b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 55695b482a8SLen Brown status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 55795b482a8SLen Brown if (ACPI_FAILURE(status)) { 55895b482a8SLen Brown return (status); 55995b482a8SLen Brown } 56095b482a8SLen Brown 56195b482a8SLen Brown if (!node) { 56295b482a8SLen Brown return (AE_BAD_PARAMETER); 56395b482a8SLen Brown } 56495b482a8SLen Brown 56595b482a8SLen Brown /* Run _STA to determine if device is present */ 56695b482a8SLen Brown 56795b482a8SLen Brown status = acpi_ut_execute_STA(node, &flags); 56895b482a8SLen Brown if (ACPI_FAILURE(status)) { 56995b482a8SLen Brown return (AE_CTRL_DEPTH); 57095b482a8SLen Brown } 57195b482a8SLen Brown 57295b482a8SLen Brown if (!(flags & ACPI_STA_DEVICE_PRESENT) && 57395b482a8SLen Brown !(flags & ACPI_STA_DEVICE_FUNCTIONING)) { 57495b482a8SLen Brown /* 57595b482a8SLen Brown * Don't examine the children of the device only when the 57695b482a8SLen Brown * device is neither present nor functional. See ACPI spec, 57795b482a8SLen Brown * description of _STA for more information. 57895b482a8SLen Brown */ 57995b482a8SLen Brown return (AE_CTRL_DEPTH); 58095b482a8SLen Brown } 58195b482a8SLen Brown 58295b482a8SLen Brown /* Filter based on device HID & CID */ 58395b482a8SLen Brown 58495b482a8SLen Brown if (info->hid != NULL) { 58595b482a8SLen Brown status = acpi_ut_execute_HID(node, &hid); 58695b482a8SLen Brown if (status == AE_NOT_FOUND) { 58795b482a8SLen Brown return (AE_OK); 58895b482a8SLen Brown } else if (ACPI_FAILURE(status)) { 58995b482a8SLen Brown return (AE_CTRL_DEPTH); 59095b482a8SLen Brown } 59195b482a8SLen Brown 59215b8dd53SBob Moore no_match = ACPI_STRCMP(hid->string, info->hid); 59315b8dd53SBob Moore ACPI_FREE(hid); 59495b482a8SLen Brown 59515b8dd53SBob Moore if (no_match) { 59615b8dd53SBob Moore /* 59715b8dd53SBob Moore * HID does not match, attempt match within the 59815b8dd53SBob Moore * list of Compatible IDs (CIDs) 59915b8dd53SBob Moore */ 60095b482a8SLen Brown status = acpi_ut_execute_CID(node, &cid); 60195b482a8SLen Brown if (status == AE_NOT_FOUND) { 60295b482a8SLen Brown return (AE_OK); 60395b482a8SLen Brown } else if (ACPI_FAILURE(status)) { 60495b482a8SLen Brown return (AE_CTRL_DEPTH); 60595b482a8SLen Brown } 60695b482a8SLen Brown 60795b482a8SLen Brown /* Walk the CID list */ 60895b482a8SLen Brown 60995b482a8SLen Brown found = 0; 61095b482a8SLen Brown for (i = 0; i < cid->count; i++) { 61115b8dd53SBob Moore if (ACPI_STRCMP(cid->ids[i].string, info->hid) 61215b8dd53SBob Moore == 0) { 61395b482a8SLen Brown found = 1; 61495b482a8SLen Brown break; 61595b482a8SLen Brown } 61695b482a8SLen Brown } 61795b482a8SLen Brown ACPI_FREE(cid); 61895b482a8SLen Brown if (!found) 61995b482a8SLen Brown return (AE_OK); 62095b482a8SLen Brown } 62195b482a8SLen Brown } 62295b482a8SLen Brown 62395b482a8SLen Brown status = info->user_function(obj_handle, nesting_level, info->context, 62495b482a8SLen Brown return_value); 62595b482a8SLen Brown return (status); 62695b482a8SLen Brown } 62795b482a8SLen Brown 62895b482a8SLen Brown /******************************************************************************* 62995b482a8SLen Brown * 63095b482a8SLen Brown * FUNCTION: acpi_get_devices 63195b482a8SLen Brown * 63295b482a8SLen Brown * PARAMETERS: HID - HID to search for. Can be NULL. 63395b482a8SLen Brown * user_function - Called when a matching object is found 63495b482a8SLen Brown * Context - Passed to user function 63595b482a8SLen Brown * return_value - Location where return value of 63695b482a8SLen Brown * user_function is put if terminated early 63795b482a8SLen Brown * 63895b482a8SLen Brown * RETURNS Return value from the user_function if terminated early. 63995b482a8SLen Brown * Otherwise, returns NULL. 64095b482a8SLen Brown * 64195b482a8SLen Brown * DESCRIPTION: Performs a modified depth-first walk of the namespace tree, 64295b482a8SLen Brown * starting (and ending) at the object specified by start_handle. 64395b482a8SLen Brown * The user_function is called whenever an object of type 64495b482a8SLen Brown * Device is found. If the user function returns 64595b482a8SLen Brown * a non-zero value, the search is terminated immediately and this 64695b482a8SLen Brown * value is returned to the caller. 64795b482a8SLen Brown * 64895b482a8SLen Brown * This is a wrapper for walk_namespace, but the callback performs 64995b482a8SLen Brown * additional filtering. Please see acpi_ns_get_device_callback. 65095b482a8SLen Brown * 65195b482a8SLen Brown ******************************************************************************/ 65295b482a8SLen Brown 65395b482a8SLen Brown acpi_status 65495b482a8SLen Brown acpi_get_devices(const char *HID, 65595b482a8SLen Brown acpi_walk_callback user_function, 65695b482a8SLen Brown void *context, void **return_value) 65795b482a8SLen Brown { 65895b482a8SLen Brown acpi_status status; 65995b482a8SLen Brown struct acpi_get_devices_info info; 66095b482a8SLen Brown 66195b482a8SLen Brown ACPI_FUNCTION_TRACE(acpi_get_devices); 66295b482a8SLen Brown 66395b482a8SLen Brown /* Parameter validation */ 66495b482a8SLen Brown 66595b482a8SLen Brown if (!user_function) { 66695b482a8SLen Brown return_ACPI_STATUS(AE_BAD_PARAMETER); 66795b482a8SLen Brown } 66895b482a8SLen Brown 66995b482a8SLen Brown /* 67095b482a8SLen Brown * We're going to call their callback from OUR callback, so we need 67195b482a8SLen Brown * to know what it is, and their context parameter. 67295b482a8SLen Brown */ 67395b482a8SLen Brown info.hid = HID; 67495b482a8SLen Brown info.context = context; 67595b482a8SLen Brown info.user_function = user_function; 67695b482a8SLen Brown 67795b482a8SLen Brown /* 67895b482a8SLen Brown * Lock the namespace around the walk. 67995b482a8SLen Brown * The namespace will be unlocked/locked around each call 68095b482a8SLen Brown * to the user function - since this function 68195b482a8SLen Brown * must be allowed to make Acpi calls itself. 68295b482a8SLen Brown */ 68395b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 68495b482a8SLen Brown if (ACPI_FAILURE(status)) { 68595b482a8SLen Brown return_ACPI_STATUS(status); 68695b482a8SLen Brown } 68795b482a8SLen Brown 68895b482a8SLen Brown status = acpi_ns_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 68995b482a8SLen Brown ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK, 690*2263576cSLin Ming acpi_ns_get_device_callback, NULL, 691*2263576cSLin Ming &info, return_value); 69295b482a8SLen Brown 69395b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 69495b482a8SLen Brown return_ACPI_STATUS(status); 69595b482a8SLen Brown } 69695b482a8SLen Brown 69795b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_get_devices) 69895b482a8SLen Brown 69995b482a8SLen Brown /******************************************************************************* 70095b482a8SLen Brown * 70195b482a8SLen Brown * FUNCTION: acpi_attach_data 70295b482a8SLen Brown * 70395b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node 70495b482a8SLen Brown * Handler - Handler for this attachment 70595b482a8SLen Brown * Data - Pointer to data to be attached 70695b482a8SLen Brown * 70795b482a8SLen Brown * RETURN: Status 70895b482a8SLen Brown * 70995b482a8SLen Brown * DESCRIPTION: Attach arbitrary data and handler to a namespace node. 71095b482a8SLen Brown * 71195b482a8SLen Brown ******************************************************************************/ 71295b482a8SLen Brown acpi_status 71395b482a8SLen Brown acpi_attach_data(acpi_handle obj_handle, 71495b482a8SLen Brown acpi_object_handler handler, void *data) 71595b482a8SLen Brown { 71695b482a8SLen Brown struct acpi_namespace_node *node; 71795b482a8SLen Brown acpi_status status; 71895b482a8SLen Brown 71995b482a8SLen Brown /* Parameter validation */ 72095b482a8SLen Brown 72195b482a8SLen Brown if (!obj_handle || !handler || !data) { 72295b482a8SLen Brown return (AE_BAD_PARAMETER); 72395b482a8SLen Brown } 72495b482a8SLen Brown 72595b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 72695b482a8SLen Brown if (ACPI_FAILURE(status)) { 72795b482a8SLen Brown return (status); 72895b482a8SLen Brown } 72995b482a8SLen Brown 73095b482a8SLen Brown /* Convert and validate the handle */ 73195b482a8SLen Brown 73295b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 73395b482a8SLen Brown if (!node) { 73495b482a8SLen Brown status = AE_BAD_PARAMETER; 73595b482a8SLen Brown goto unlock_and_exit; 73695b482a8SLen Brown } 73795b482a8SLen Brown 73895b482a8SLen Brown status = acpi_ns_attach_data(node, handler, data); 73995b482a8SLen Brown 74095b482a8SLen Brown unlock_and_exit: 74195b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 74295b482a8SLen Brown return (status); 74395b482a8SLen Brown } 74495b482a8SLen Brown 74595b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_attach_data) 74695b482a8SLen Brown 74795b482a8SLen Brown /******************************************************************************* 74895b482a8SLen Brown * 74995b482a8SLen Brown * FUNCTION: acpi_detach_data 75095b482a8SLen Brown * 75195b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node handle 75295b482a8SLen Brown * Handler - Handler used in call to acpi_attach_data 75395b482a8SLen Brown * 75495b482a8SLen Brown * RETURN: Status 75595b482a8SLen Brown * 75695b482a8SLen Brown * DESCRIPTION: Remove data that was previously attached to a node. 75795b482a8SLen Brown * 75895b482a8SLen Brown ******************************************************************************/ 75995b482a8SLen Brown acpi_status 76095b482a8SLen Brown acpi_detach_data(acpi_handle obj_handle, acpi_object_handler handler) 76195b482a8SLen Brown { 76295b482a8SLen Brown struct acpi_namespace_node *node; 76395b482a8SLen Brown acpi_status status; 76495b482a8SLen Brown 76595b482a8SLen Brown /* Parameter validation */ 76695b482a8SLen Brown 76795b482a8SLen Brown if (!obj_handle || !handler) { 76895b482a8SLen Brown return (AE_BAD_PARAMETER); 76995b482a8SLen Brown } 77095b482a8SLen Brown 77195b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 77295b482a8SLen Brown if (ACPI_FAILURE(status)) { 77395b482a8SLen Brown return (status); 77495b482a8SLen Brown } 77595b482a8SLen Brown 77695b482a8SLen Brown /* Convert and validate the handle */ 77795b482a8SLen Brown 77895b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 77995b482a8SLen Brown if (!node) { 78095b482a8SLen Brown status = AE_BAD_PARAMETER; 78195b482a8SLen Brown goto unlock_and_exit; 78295b482a8SLen Brown } 78395b482a8SLen Brown 78495b482a8SLen Brown status = acpi_ns_detach_data(node, handler); 78595b482a8SLen Brown 78695b482a8SLen Brown unlock_and_exit: 78795b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 78895b482a8SLen Brown return (status); 78995b482a8SLen Brown } 79095b482a8SLen Brown 79195b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_detach_data) 79295b482a8SLen Brown 79395b482a8SLen Brown /******************************************************************************* 79495b482a8SLen Brown * 79595b482a8SLen Brown * FUNCTION: acpi_get_data 79695b482a8SLen Brown * 79795b482a8SLen Brown * PARAMETERS: obj_handle - Namespace node 79895b482a8SLen Brown * Handler - Handler used in call to attach_data 79995b482a8SLen Brown * Data - Where the data is returned 80095b482a8SLen Brown * 80195b482a8SLen Brown * RETURN: Status 80295b482a8SLen Brown * 80395b482a8SLen Brown * DESCRIPTION: Retrieve data that was previously attached to a namespace node. 80495b482a8SLen Brown * 80595b482a8SLen Brown ******************************************************************************/ 80695b482a8SLen Brown acpi_status 80795b482a8SLen Brown acpi_get_data(acpi_handle obj_handle, acpi_object_handler handler, void **data) 80895b482a8SLen Brown { 80995b482a8SLen Brown struct acpi_namespace_node *node; 81095b482a8SLen Brown acpi_status status; 81195b482a8SLen Brown 81295b482a8SLen Brown /* Parameter validation */ 81395b482a8SLen Brown 81495b482a8SLen Brown if (!obj_handle || !handler || !data) { 81595b482a8SLen Brown return (AE_BAD_PARAMETER); 81695b482a8SLen Brown } 81795b482a8SLen Brown 81895b482a8SLen Brown status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); 81995b482a8SLen Brown if (ACPI_FAILURE(status)) { 82095b482a8SLen Brown return (status); 82195b482a8SLen Brown } 82295b482a8SLen Brown 82395b482a8SLen Brown /* Convert and validate the handle */ 82495b482a8SLen Brown 82595b482a8SLen Brown node = acpi_ns_map_handle_to_node(obj_handle); 82695b482a8SLen Brown if (!node) { 82795b482a8SLen Brown status = AE_BAD_PARAMETER; 82895b482a8SLen Brown goto unlock_and_exit; 82995b482a8SLen Brown } 83095b482a8SLen Brown 83195b482a8SLen Brown status = acpi_ns_get_attached_data(node, handler, data); 83295b482a8SLen Brown 83395b482a8SLen Brown unlock_and_exit: 83495b482a8SLen Brown (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); 83595b482a8SLen Brown return (status); 83695b482a8SLen Brown } 83795b482a8SLen Brown 83895b482a8SLen Brown ACPI_EXPORT_SYMBOL(acpi_get_data) 839