1 /*******************************************************************************
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 * ACPI Object evaluation interfaces
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2013, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #define __NSXFEVAL_C__
48 #include <contrib/dev/acpica/include/acpi.h>
49 #include <contrib/dev/acpica/include/accommon.h>
50 #include <contrib/dev/acpica/include/acnamesp.h>
51 #include <contrib/dev/acpica/include/acinterp.h>
54 #define _COMPONENT ACPI_NAMESPACE
55 ACPI_MODULE_NAME ("nsxfeval")
57 /* Local prototypes */
60 AcpiNsResolveReferences (
61 ACPI_EVALUATE_INFO *Info);
64 /*******************************************************************************
66 * FUNCTION: AcpiEvaluateObjectTyped
68 * PARAMETERS: Handle - Object handle (optional)
69 * Pathname - Object pathname (optional)
70 * ExternalParams - List of parameters to pass to method,
71 * terminated by NULL. May be NULL
72 * if no parameters are being passed.
73 * ReturnBuffer - Where to put method's return value (if
74 * any). If NULL, no value is returned.
75 * ReturnType - Expected type of return object
79 * DESCRIPTION: Find and evaluate the given object, passing the given
80 * parameters if necessary. One of "Handle" or "Pathname" must
83 ******************************************************************************/
86 AcpiEvaluateObjectTyped (
89 ACPI_OBJECT_LIST *ExternalParams,
90 ACPI_BUFFER *ReturnBuffer,
91 ACPI_OBJECT_TYPE ReturnType)
94 BOOLEAN MustFree = FALSE;
97 ACPI_FUNCTION_TRACE (AcpiEvaluateObjectTyped);
100 /* Return buffer must be valid */
104 return_ACPI_STATUS (AE_BAD_PARAMETER);
107 if (ReturnBuffer->Length == ACPI_ALLOCATE_BUFFER)
112 /* Evaluate the object */
114 Status = AcpiEvaluateObject (Handle, Pathname, ExternalParams, ReturnBuffer);
115 if (ACPI_FAILURE (Status))
117 return_ACPI_STATUS (Status);
120 /* Type ANY means "don't care" */
122 if (ReturnType == ACPI_TYPE_ANY)
124 return_ACPI_STATUS (AE_OK);
127 if (ReturnBuffer->Length == 0)
129 /* Error because caller specifically asked for a return value */
131 ACPI_ERROR ((AE_INFO, "No return value"));
132 return_ACPI_STATUS (AE_NULL_OBJECT);
135 /* Examine the object type returned from EvaluateObject */
137 if (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type == ReturnType)
139 return_ACPI_STATUS (AE_OK);
142 /* Return object type does not match requested type */
144 ACPI_ERROR ((AE_INFO,
145 "Incorrect return type [%s] requested [%s]",
146 AcpiUtGetTypeName (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type),
147 AcpiUtGetTypeName (ReturnType)));
151 /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
153 AcpiOsFree (ReturnBuffer->Pointer);
154 ReturnBuffer->Pointer = NULL;
157 ReturnBuffer->Length = 0;
158 return_ACPI_STATUS (AE_TYPE);
161 ACPI_EXPORT_SYMBOL (AcpiEvaluateObjectTyped)
164 /*******************************************************************************
166 * FUNCTION: AcpiEvaluateObject
168 * PARAMETERS: Handle - Object handle (optional)
169 * Pathname - Object pathname (optional)
170 * ExternalParams - List of parameters to pass to method,
171 * terminated by NULL. May be NULL
172 * if no parameters are being passed.
173 * ReturnBuffer - Where to put method's return value (if
174 * any). If NULL, no value is returned.
178 * DESCRIPTION: Find and evaluate the given object, passing the given
179 * parameters if necessary. One of "Handle" or "Pathname" must
180 * be valid (non-null)
182 ******************************************************************************/
187 ACPI_STRING Pathname,
188 ACPI_OBJECT_LIST *ExternalParams,
189 ACPI_BUFFER *ReturnBuffer)
192 ACPI_EVALUATE_INFO *Info;
193 ACPI_SIZE BufferSpaceNeeded;
197 ACPI_FUNCTION_TRACE (AcpiEvaluateObject);
200 /* Allocate and initialize the evaluation information block */
202 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
205 return_ACPI_STATUS (AE_NO_MEMORY);
208 Info->Pathname = Pathname;
210 /* Convert and validate the device handle */
212 Info->PrefixNode = AcpiNsValidateHandle (Handle);
213 if (!Info->PrefixNode)
215 Status = AE_BAD_PARAMETER;
220 * If there are parameters to be passed to a control method, the external
221 * objects must all be converted to internal objects
223 if (ExternalParams && ExternalParams->Count)
226 * Allocate a new parameter block for the internal objects
227 * Add 1 to count to allow for null terminated internal list
229 Info->Parameters = ACPI_ALLOCATE_ZEROED (
230 ((ACPI_SIZE) ExternalParams->Count + 1) * sizeof (void *));
231 if (!Info->Parameters)
233 Status = AE_NO_MEMORY;
237 /* Convert each external object in the list to an internal object */
239 for (i = 0; i < ExternalParams->Count; i++)
241 Status = AcpiUtCopyEobjectToIobject (
242 &ExternalParams->Pointer[i], &Info->Parameters[i]);
243 if (ACPI_FAILURE (Status))
248 Info->Parameters[ExternalParams->Count] = NULL;
253 * 1) Fully qualified pathname
254 * 2) No handle, not fully qualified pathname (error)
258 (ACPI_IS_ROOT_PREFIX (Pathname[0])))
260 /* The path is fully qualified, just evaluate by name */
262 Info->PrefixNode = NULL;
263 Status = AcpiNsEvaluate (Info);
268 * A handle is optional iff a fully qualified pathname is specified.
269 * Since we've already handled fully qualified names above, this is
274 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
275 "Both Handle and Pathname are NULL"));
279 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
280 "Null Handle with relative pathname [%s]", Pathname));
283 Status = AE_BAD_PARAMETER;
287 /* We have a namespace a node and a possible relative path */
289 Status = AcpiNsEvaluate (Info);
293 * If we are expecting a return value, and all went well above,
294 * copy the return value to an external object.
298 if (!Info->ReturnObject)
300 ReturnBuffer->Length = 0;
304 if (ACPI_GET_DESCRIPTOR_TYPE (Info->ReturnObject) ==
305 ACPI_DESC_TYPE_NAMED)
308 * If we received a NS Node as a return object, this means that
309 * the object we are evaluating has nothing interesting to
310 * return (such as a mutex, etc.) We return an error because
311 * these types are essentially unsupported by this interface.
312 * We don't check up front because this makes it easier to add
313 * support for various types at a later date if necessary.
316 Info->ReturnObject = NULL; /* No need to delete a NS Node */
317 ReturnBuffer->Length = 0;
320 if (ACPI_SUCCESS (Status))
322 /* Dereference Index and RefOf references */
324 AcpiNsResolveReferences (Info);
326 /* Get the size of the returned object */
328 Status = AcpiUtGetObjectSize (Info->ReturnObject,
330 if (ACPI_SUCCESS (Status))
332 /* Validate/Allocate/Clear caller buffer */
334 Status = AcpiUtInitializeBuffer (ReturnBuffer,
336 if (ACPI_FAILURE (Status))
339 * Caller's buffer is too small or a new one can't
342 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
343 "Needed buffer size %X, %s\n",
344 (UINT32) BufferSpaceNeeded,
345 AcpiFormatException (Status)));
349 /* We have enough space for the object, build it */
351 Status = AcpiUtCopyIobjectToEobject (Info->ReturnObject,
359 if (Info->ReturnObject)
362 * Delete the internal return object. NOTE: Interpreter must be
363 * locked to avoid race condition.
365 AcpiExEnterInterpreter ();
367 /* Remove one reference on the return object (should delete it) */
369 AcpiUtRemoveReference (Info->ReturnObject);
370 AcpiExExitInterpreter ();
376 /* Free the input parameter list (if we created one) */
378 if (Info->Parameters)
380 /* Free the allocated parameter block */
382 AcpiUtDeleteInternalObjectList (Info->Parameters);
386 return_ACPI_STATUS (Status);
389 ACPI_EXPORT_SYMBOL (AcpiEvaluateObject)
392 /*******************************************************************************
394 * FUNCTION: AcpiNsResolveReferences
396 * PARAMETERS: Info - Evaluation info block
398 * RETURN: Info->ReturnObject is replaced with the dereferenced object
400 * DESCRIPTION: Dereference certain reference objects. Called before an
401 * internal return object is converted to an external ACPI_OBJECT.
403 * Performs an automatic dereference of Index and RefOf reference objects.
404 * These reference objects are not supported by the ACPI_OBJECT, so this is a
405 * last resort effort to return something useful. Also, provides compatibility
406 * with other ACPI implementations.
408 * NOTE: does not handle references within returned package objects or nested
409 * references, but this support could be added later if found to be necessary.
411 ******************************************************************************/
414 AcpiNsResolveReferences (
415 ACPI_EVALUATE_INFO *Info)
417 ACPI_OPERAND_OBJECT *ObjDesc = NULL;
418 ACPI_NAMESPACE_NODE *Node;
421 /* We are interested in reference objects only */
423 if ((Info->ReturnObject)->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
429 * Two types of references are supported - those created by Index and
430 * RefOf operators. A name reference (AML_NAMEPATH_OP) can be converted
431 * to an ACPI_OBJECT, so it is not dereferenced here. A DdbHandle
432 * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to
435 switch (Info->ReturnObject->Reference.Class)
437 case ACPI_REFCLASS_INDEX:
439 ObjDesc = *(Info->ReturnObject->Reference.Where);
442 case ACPI_REFCLASS_REFOF:
444 Node = Info->ReturnObject->Reference.Object;
447 ObjDesc = Node->Object;
455 /* Replace the existing reference object */
459 AcpiUtAddReference (ObjDesc);
460 AcpiUtRemoveReference (Info->ReturnObject);
461 Info->ReturnObject = ObjDesc;
468 /*******************************************************************************
470 * FUNCTION: AcpiWalkNamespace
472 * PARAMETERS: Type - ACPI_OBJECT_TYPE to search for
473 * StartObject - Handle in namespace where search begins
474 * MaxDepth - Depth to which search is to reach
475 * PreOrderVisit - Called during tree pre-order visit
476 * when an object of "Type" is found
477 * PostOrderVisit - Called during tree post-order visit
478 * when an object of "Type" is found
479 * Context - Passed to user function(s) above
480 * ReturnValue - Location where return value of
481 * UserFunction is put if terminated early
483 * RETURNS Return value from the UserFunction if terminated early.
484 * Otherwise, returns NULL.
486 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
487 * starting (and ending) at the object specified by StartHandle.
488 * The callback function is called whenever an object that matches
489 * the type parameter is found. If the callback function returns
490 * a non-zero value, the search is terminated immediately and this
491 * value is returned to the caller.
493 * The point of this procedure is to provide a generic namespace
494 * walk routine that can be called from multiple places to
495 * provide multiple services; the callback function(s) can be
496 * tailored to each task, whether it is a print function,
497 * a compare function, etc.
499 ******************************************************************************/
503 ACPI_OBJECT_TYPE Type,
504 ACPI_HANDLE StartObject,
506 ACPI_WALK_CALLBACK PreOrderVisit,
507 ACPI_WALK_CALLBACK PostOrderVisit,
514 ACPI_FUNCTION_TRACE (AcpiWalkNamespace);
517 /* Parameter validation */
519 if ((Type > ACPI_TYPE_LOCAL_MAX) ||
521 (!PreOrderVisit && !PostOrderVisit))
523 return_ACPI_STATUS (AE_BAD_PARAMETER);
527 * Need to acquire the namespace reader lock to prevent interference
528 * with any concurrent table unloads (which causes the deletion of
529 * namespace objects). We cannot allow the deletion of a namespace node
530 * while the user function is using it. The exception to this are the
531 * nodes created and deleted during control method execution -- these
532 * nodes are marked as temporary nodes and are ignored by the namespace
533 * walk. Thus, control methods can be executed while holding the
534 * namespace deletion lock (and the user function can execute control
537 Status = AcpiUtAcquireReadLock (&AcpiGbl_NamespaceRwLock);
538 if (ACPI_FAILURE (Status))
540 return_ACPI_STATUS (Status);
544 * Lock the namespace around the walk. The namespace will be
545 * unlocked/locked around each call to the user function - since the user
546 * function must be allowed to make ACPICA calls itself (for example, it
547 * will typically execute control methods during device enumeration.)
549 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
550 if (ACPI_FAILURE (Status))
555 Status = AcpiNsWalkNamespace (Type, StartObject, MaxDepth,
556 ACPI_NS_WALK_UNLOCK, PreOrderVisit,
557 PostOrderVisit, Context, ReturnValue);
559 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
562 (void) AcpiUtReleaseReadLock (&AcpiGbl_NamespaceRwLock);
563 return_ACPI_STATUS (Status);
566 ACPI_EXPORT_SYMBOL (AcpiWalkNamespace)
569 /*******************************************************************************
571 * FUNCTION: AcpiNsGetDeviceCallback
573 * PARAMETERS: Callback from AcpiGetDevice
577 * DESCRIPTION: Takes callbacks from WalkNamespace and filters out all non-
578 * present devices, or if they specified a HID, it filters based
581 ******************************************************************************/
584 AcpiNsGetDeviceCallback (
585 ACPI_HANDLE ObjHandle,
590 ACPI_GET_DEVICES_INFO *Info = Context;
592 ACPI_NAMESPACE_NODE *Node;
594 ACPI_PNP_DEVICE_ID *Hid;
595 ACPI_PNP_DEVICE_ID_LIST *Cid;
601 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
602 if (ACPI_FAILURE (Status))
607 Node = AcpiNsValidateHandle (ObjHandle);
608 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
609 if (ACPI_FAILURE (Status))
616 return (AE_BAD_PARAMETER);
620 * First, filter based on the device HID and CID.
622 * 01/2010: For this case where a specific HID is requested, we don't
623 * want to run _STA until we have an actual HID match. Thus, we will
624 * not unnecessarily execute _STA on devices for which the caller
625 * doesn't care about. Previously, _STA was executed unconditionally
626 * on all devices found here.
628 * A side-effect of this change is that now we will continue to search
629 * for a matching HID even under device trees where the parent device
630 * would have returned a _STA that indicates it is not present or
631 * not functioning (thus aborting the search on that branch).
633 if (Info->Hid != NULL)
635 Status = AcpiUtExecute_HID (Node, &Hid);
636 if (Status == AE_NOT_FOUND)
640 else if (ACPI_FAILURE (Status))
642 return (AE_CTRL_DEPTH);
645 NoMatch = ACPI_STRCMP (Hid->String, Info->Hid);
651 * HID does not match, attempt match within the
652 * list of Compatible IDs (CIDs)
654 Status = AcpiUtExecute_CID (Node, &Cid);
655 if (Status == AE_NOT_FOUND)
659 else if (ACPI_FAILURE (Status))
661 return (AE_CTRL_DEPTH);
664 /* Walk the CID list */
667 for (i = 0; i < Cid->Count; i++)
669 if (ACPI_STRCMP (Cid->Ids[i].String, Info->Hid) == 0)
671 /* Found a matching CID */
686 /* Run _STA to determine if device is present */
688 Status = AcpiUtExecute_STA (Node, &Flags);
689 if (ACPI_FAILURE (Status))
691 return (AE_CTRL_DEPTH);
694 if (!(Flags & ACPI_STA_DEVICE_PRESENT) &&
695 !(Flags & ACPI_STA_DEVICE_FUNCTIONING))
698 * Don't examine the children of the device only when the
699 * device is neither present nor functional. See ACPI spec,
700 * description of _STA for more information.
702 return (AE_CTRL_DEPTH);
705 /* We have a valid device, invoke the user function */
707 Status = Info->UserFunction (ObjHandle, NestingLevel, Info->Context,
713 /*******************************************************************************
715 * FUNCTION: AcpiGetDevices
717 * PARAMETERS: HID - HID to search for. Can be NULL.
718 * UserFunction - Called when a matching object is found
719 * Context - Passed to user function
720 * ReturnValue - Location where return value of
721 * UserFunction is put if terminated early
723 * RETURNS Return value from the UserFunction if terminated early.
724 * Otherwise, returns NULL.
726 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
727 * starting (and ending) at the object specified by StartHandle.
728 * The UserFunction is called whenever an object of type
729 * Device is found. If the user function returns
730 * a non-zero value, the search is terminated immediately and this
731 * value is returned to the caller.
733 * This is a wrapper for WalkNamespace, but the callback performs
734 * additional filtering. Please see AcpiNsGetDeviceCallback.
736 ******************************************************************************/
741 ACPI_WALK_CALLBACK UserFunction,
746 ACPI_GET_DEVICES_INFO Info;
749 ACPI_FUNCTION_TRACE (AcpiGetDevices);
752 /* Parameter validation */
756 return_ACPI_STATUS (AE_BAD_PARAMETER);
760 * We're going to call their callback from OUR callback, so we need
761 * to know what it is, and their context parameter.
764 Info.Context = Context;
765 Info.UserFunction = UserFunction;
768 * Lock the namespace around the walk.
769 * The namespace will be unlocked/locked around each call
770 * to the user function - since this function
771 * must be allowed to make Acpi calls itself.
773 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
774 if (ACPI_FAILURE (Status))
776 return_ACPI_STATUS (Status);
779 Status = AcpiNsWalkNamespace (ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
780 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
781 AcpiNsGetDeviceCallback, NULL, &Info, ReturnValue);
783 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
784 return_ACPI_STATUS (Status);
787 ACPI_EXPORT_SYMBOL (AcpiGetDevices)
790 /*******************************************************************************
792 * FUNCTION: AcpiAttachData
794 * PARAMETERS: ObjHandle - Namespace node
795 * Handler - Handler for this attachment
796 * Data - Pointer to data to be attached
800 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
802 ******************************************************************************/
806 ACPI_HANDLE ObjHandle,
807 ACPI_OBJECT_HANDLER Handler,
810 ACPI_NAMESPACE_NODE *Node;
814 /* Parameter validation */
820 return (AE_BAD_PARAMETER);
823 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
824 if (ACPI_FAILURE (Status))
829 /* Convert and validate the handle */
831 Node = AcpiNsValidateHandle (ObjHandle);
834 Status = AE_BAD_PARAMETER;
838 Status = AcpiNsAttachData (Node, Handler, Data);
841 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
845 ACPI_EXPORT_SYMBOL (AcpiAttachData)
848 /*******************************************************************************
850 * FUNCTION: AcpiDetachData
852 * PARAMETERS: ObjHandle - Namespace node handle
853 * Handler - Handler used in call to AcpiAttachData
857 * DESCRIPTION: Remove data that was previously attached to a node.
859 ******************************************************************************/
863 ACPI_HANDLE ObjHandle,
864 ACPI_OBJECT_HANDLER Handler)
866 ACPI_NAMESPACE_NODE *Node;
870 /* Parameter validation */
875 return (AE_BAD_PARAMETER);
878 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
879 if (ACPI_FAILURE (Status))
884 /* Convert and validate the handle */
886 Node = AcpiNsValidateHandle (ObjHandle);
889 Status = AE_BAD_PARAMETER;
893 Status = AcpiNsDetachData (Node, Handler);
896 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
900 ACPI_EXPORT_SYMBOL (AcpiDetachData)
903 /*******************************************************************************
905 * FUNCTION: AcpiGetData
907 * PARAMETERS: ObjHandle - Namespace node
908 * Handler - Handler used in call to AttachData
909 * Data - Where the data is returned
913 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
915 ******************************************************************************/
919 ACPI_HANDLE ObjHandle,
920 ACPI_OBJECT_HANDLER Handler,
923 ACPI_NAMESPACE_NODE *Node;
927 /* Parameter validation */
933 return (AE_BAD_PARAMETER);
936 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
937 if (ACPI_FAILURE (Status))
942 /* Convert and validate the handle */
944 Node = AcpiNsValidateHandle (ObjHandle);
947 Status = AE_BAD_PARAMETER;
951 Status = AcpiNsGetAttachedData (Node, Handler, Data);
954 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
958 ACPI_EXPORT_SYMBOL (AcpiGetData)