1 /*******************************************************************************
2  *
3  * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4  *                         ACPI Object evaluation interfaces
5  *
6  ******************************************************************************/
7 
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights. You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code. No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision. In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change. Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee. Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution. In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government. In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************
116  *
117  * Alternatively, you may choose to be licensed under the terms of the
118  * following license:
119  *
120  * Redistribution and use in source and binary forms, with or without
121  * modification, are permitted provided that the following conditions
122  * are met:
123  * 1. Redistributions of source code must retain the above copyright
124  *    notice, this list of conditions, and the following disclaimer,
125  *    without modification.
126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127  *    substantially similar to the "NO WARRANTY" disclaimer below
128  *    ("Disclaimer") and any redistribution must be conditioned upon
129  *    including a substantially similar Disclaimer requirement for further
130  *    binary redistribution.
131  * 3. Neither the names of the above-listed copyright holders nor the names
132  *    of any contributors may be used to endorse or promote products derived
133  *    from this software without specific prior written permission.
134  *
135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Alternatively, you may choose to be licensed under the terms of the
148  * GNU General Public License ("GPL") version 2 as published by the Free
149  * Software Foundation.
150  *
151  *****************************************************************************/
152 
153 #define EXPORT_ACPI_INTERFACES
154 
155 #include "acpi.h"
156 #include "accommon.h"
157 #include "acnamesp.h"
158 #include "acinterp.h"
159 
160 
161 #define _COMPONENT          ACPI_NAMESPACE
162         ACPI_MODULE_NAME    ("nsxfeval")
163 
164 /* Local prototypes */
165 
166 static void
167 AcpiNsResolveReferences (
168     ACPI_EVALUATE_INFO      *Info);
169 
170 
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiEvaluateObjectTyped
174  *
175  * PARAMETERS:  Handle              - Object handle (optional)
176  *              Pathname            - Object pathname (optional)
177  *              ExternalParams      - List of parameters to pass to method,
178  *                                    terminated by NULL. May be NULL
179  *                                    if no parameters are being passed.
180  *              ReturnBuffer        - Where to put method's return value (if
181  *                                    any). If NULL, no value is returned.
182  *              ReturnType          - Expected type of return object
183  *
184  * RETURN:      Status
185  *
186  * DESCRIPTION: Find and evaluate the given object, passing the given
187  *              parameters if necessary. One of "Handle" or "Pathname" must
188  *              be valid (non-null)
189  *
190  ******************************************************************************/
191 
192 ACPI_STATUS
193 AcpiEvaluateObjectTyped (
194     ACPI_HANDLE             Handle,
195     ACPI_STRING             Pathname,
196     ACPI_OBJECT_LIST        *ExternalParams,
197     ACPI_BUFFER             *ReturnBuffer,
198     ACPI_OBJECT_TYPE        ReturnType)
199 {
200     ACPI_STATUS             Status;
201     BOOLEAN                 FreeBufferOnError = FALSE;
202     ACPI_HANDLE             TargetHandle;
203     char                    *FullPathname;
204 
205 
206     ACPI_FUNCTION_TRACE (AcpiEvaluateObjectTyped);
207 
208 
209     /* Return buffer must be valid */
210 
211     if (!ReturnBuffer)
212     {
213         return_ACPI_STATUS (AE_BAD_PARAMETER);
214     }
215 
216     if (ReturnBuffer->Length == ACPI_ALLOCATE_BUFFER)
217     {
218         FreeBufferOnError = TRUE;
219     }
220 
221     Status = AcpiGetHandle (Handle, Pathname, &TargetHandle);
222     if (ACPI_FAILURE (Status))
223     {
224         return_ACPI_STATUS (Status);
225     }
226 
227     FullPathname = AcpiNsGetExternalPathname (TargetHandle);
228     if (!FullPathname)
229     {
230         return_ACPI_STATUS (AE_NO_MEMORY);
231     }
232 
233     /* Evaluate the object */
234 
235     Status = AcpiEvaluateObject (TargetHandle, NULL, ExternalParams,
236         ReturnBuffer);
237     if (ACPI_FAILURE (Status))
238     {
239         goto Exit;
240     }
241 
242     /* Type ANY means "don't care about return value type" */
243 
244     if (ReturnType == ACPI_TYPE_ANY)
245     {
246         goto Exit;
247     }
248 
249     if (ReturnBuffer->Length == 0)
250     {
251         /* Error because caller specifically asked for a return value */
252 
253         ACPI_ERROR ((AE_INFO, "%s did not return any object",
254             FullPathname));
255         Status = AE_NULL_OBJECT;
256         goto Exit;
257     }
258 
259     /* Examine the object type returned from EvaluateObject */
260 
261     if (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type == ReturnType)
262     {
263         goto Exit;
264     }
265 
266     /* Return object type does not match requested type */
267 
268     ACPI_ERROR ((AE_INFO,
269         "Incorrect return type from %s - received [%s], requested [%s]",
270         FullPathname,
271         AcpiUtGetTypeName (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type),
272         AcpiUtGetTypeName (ReturnType)));
273 
274     if (FreeBufferOnError)
275     {
276         /*
277          * Free a buffer created via ACPI_ALLOCATE_BUFFER.
278          * Note: We use AcpiOsFree here because AcpiOsAllocate was used
279          * to allocate the buffer. This purposefully bypasses the
280          * (optionally enabled) allocation tracking mechanism since we
281          * only want to track internal allocations.
282          */
283         AcpiOsFree (ReturnBuffer->Pointer);
284         ReturnBuffer->Pointer = NULL;
285     }
286 
287     ReturnBuffer->Length = 0;
288     Status = AE_TYPE;
289 
290 Exit:
291     ACPI_FREE (FullPathname);
292     return_ACPI_STATUS (Status);
293 }
294 
295 ACPI_EXPORT_SYMBOL (AcpiEvaluateObjectTyped)
296 
297 
298 /*******************************************************************************
299  *
300  * FUNCTION:    AcpiEvaluateObject
301  *
302  * PARAMETERS:  Handle              - Object handle (optional)
303  *              Pathname            - Object pathname (optional)
304  *              ExternalParams      - List of parameters to pass to method,
305  *                                    terminated by NULL. May be NULL
306  *                                    if no parameters are being passed.
307  *              ReturnBuffer        - Where to put method's return value (if
308  *                                    any). If NULL, no value is returned.
309  *
310  * RETURN:      Status
311  *
312  * DESCRIPTION: Find and evaluate the given object, passing the given
313  *              parameters if necessary. One of "Handle" or "Pathname" must
314  *              be valid (non-null)
315  *
316  ******************************************************************************/
317 
318 ACPI_STATUS
319 AcpiEvaluateObject (
320     ACPI_HANDLE             Handle,
321     ACPI_STRING             Pathname,
322     ACPI_OBJECT_LIST        *ExternalParams,
323     ACPI_BUFFER             *ReturnBuffer)
324 {
325     ACPI_STATUS             Status;
326     ACPI_EVALUATE_INFO      *Info;
327     ACPI_SIZE               BufferSpaceNeeded;
328     UINT32                  i;
329 
330 
331     ACPI_FUNCTION_TRACE (AcpiEvaluateObject);
332 
333 
334     /* Allocate and initialize the evaluation information block */
335 
336     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
337     if (!Info)
338     {
339         return_ACPI_STATUS (AE_NO_MEMORY);
340     }
341 
342     /* Convert and validate the device handle */
343 
344     Info->PrefixNode = AcpiNsValidateHandle (Handle);
345     if (!Info->PrefixNode)
346     {
347         Status = AE_BAD_PARAMETER;
348         goto Cleanup;
349     }
350 
351     /*
352      * Get the actual namespace node for the target object.
353      * Handles these cases:
354      *
355      * 1) Null node, valid pathname from root (absolute path)
356      * 2) Node and valid pathname (path relative to Node)
357      * 3) Node, Null pathname
358      */
359     if ((Pathname) &&
360         (ACPI_IS_ROOT_PREFIX (Pathname[0])))
361     {
362         /* The path is fully qualified, just evaluate by name */
363 
364         Info->PrefixNode = NULL;
365     }
366     else if (!Handle)
367     {
368         /*
369          * A handle is optional iff a fully qualified pathname is specified.
370          * Since we've already handled fully qualified names above, this is
371          * an error.
372          */
373         if (!Pathname)
374         {
375             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
376                 "Both Handle and Pathname are NULL"));
377         }
378         else
379         {
380             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
381                 "Null Handle with relative pathname [%s]", Pathname));
382         }
383 
384         Status = AE_BAD_PARAMETER;
385         goto Cleanup;
386     }
387 
388     Info->RelativePathname = Pathname;
389 
390     /*
391      * Convert all external objects passed as arguments to the
392      * internal version(s).
393      */
394     if (ExternalParams && ExternalParams->Count)
395     {
396         Info->ParamCount = (UINT16) ExternalParams->Count;
397 
398         /* Warn on impossible argument count */
399 
400         if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
401         {
402             ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
403                 "Excess arguments (%u) - using only %u",
404                 Info->ParamCount, ACPI_METHOD_NUM_ARGS));
405 
406             Info->ParamCount = ACPI_METHOD_NUM_ARGS;
407         }
408 
409         /*
410          * Allocate a new parameter block for the internal objects
411          * Add 1 to count to allow for null terminated internal list
412          */
413         Info->Parameters = ACPI_ALLOCATE_ZEROED (
414             ((ACPI_SIZE) Info->ParamCount + 1) * sizeof (void *));
415         if (!Info->Parameters)
416         {
417             Status = AE_NO_MEMORY;
418             goto Cleanup;
419         }
420 
421         /* Convert each external object in the list to an internal object */
422 
423         for (i = 0; i < Info->ParamCount; i++)
424         {
425             Status = AcpiUtCopyEobjectToIobject (
426                 &ExternalParams->Pointer[i], &Info->Parameters[i]);
427             if (ACPI_FAILURE (Status))
428             {
429                 goto Cleanup;
430             }
431         }
432 
433         Info->Parameters[Info->ParamCount] = NULL;
434     }
435 
436 
437 #ifdef _FUTURE_FEATURE
438 
439     /*
440      * Begin incoming argument count analysis. Check for too few args
441      * and too many args.
442      */
443     switch (AcpiNsGetType (Info->Node))
444     {
445     case ACPI_TYPE_METHOD:
446 
447         /* Check incoming argument count against the method definition */
448 
449         if (Info->ObjDesc->Method.ParamCount > Info->ParamCount)
450         {
451             ACPI_ERROR ((AE_INFO,
452                 "Insufficient arguments (%u) - %u are required",
453                 Info->ParamCount,
454                 Info->ObjDesc->Method.ParamCount));
455 
456             Status = AE_MISSING_ARGUMENTS;
457             goto Cleanup;
458         }
459 
460         else if (Info->ObjDesc->Method.ParamCount < Info->ParamCount)
461         {
462             ACPI_WARNING ((AE_INFO,
463                 "Excess arguments (%u) - only %u are required",
464                 Info->ParamCount,
465                 Info->ObjDesc->Method.ParamCount));
466 
467             /* Just pass the required number of arguments */
468 
469             Info->ParamCount = Info->ObjDesc->Method.ParamCount;
470         }
471 
472         /*
473          * Any incoming external objects to be passed as arguments to the
474          * method must be converted to internal objects
475          */
476         if (Info->ParamCount)
477         {
478             /*
479              * Allocate a new parameter block for the internal objects
480              * Add 1 to count to allow for null terminated internal list
481              */
482             Info->Parameters = ACPI_ALLOCATE_ZEROED (
483                 ((ACPI_SIZE) Info->ParamCount + 1) * sizeof (void *));
484             if (!Info->Parameters)
485             {
486                 Status = AE_NO_MEMORY;
487                 goto Cleanup;
488             }
489 
490             /* Convert each external object in the list to an internal object */
491 
492             for (i = 0; i < Info->ParamCount; i++)
493             {
494                 Status = AcpiUtCopyEobjectToIobject (
495                     &ExternalParams->Pointer[i], &Info->Parameters[i]);
496                 if (ACPI_FAILURE (Status))
497                 {
498                     goto Cleanup;
499                 }
500             }
501 
502             Info->Parameters[Info->ParamCount] = NULL;
503         }
504         break;
505 
506     default:
507 
508         /* Warn if arguments passed to an object that is not a method */
509 
510         if (Info->ParamCount)
511         {
512             ACPI_WARNING ((AE_INFO,
513                 "%u arguments were passed to a non-method ACPI object",
514                 Info->ParamCount));
515         }
516         break;
517     }
518 
519 #endif
520 
521 
522     /* Now we can evaluate the object */
523 
524     Status = AcpiNsEvaluate (Info);
525 
526     /*
527      * If we are expecting a return value, and all went well above,
528      * copy the return value to an external object.
529      */
530     if (!ReturnBuffer)
531     {
532         goto CleanupReturnObject;
533     }
534 
535     if (!Info->ReturnObject)
536     {
537         ReturnBuffer->Length = 0;
538         goto Cleanup;
539     }
540 
541     if (ACPI_GET_DESCRIPTOR_TYPE (Info->ReturnObject) ==
542         ACPI_DESC_TYPE_NAMED)
543     {
544         /*
545          * If we received a NS Node as a return object, this means that
546          * the object we are evaluating has nothing interesting to
547          * return (such as a mutex, etc.)  We return an error because
548          * these types are essentially unsupported by this interface.
549          * We don't check up front because this makes it easier to add
550          * support for various types at a later date if necessary.
551          */
552         Status = AE_TYPE;
553         Info->ReturnObject = NULL;   /* No need to delete a NS Node */
554         ReturnBuffer->Length = 0;
555     }
556 
557     if (ACPI_FAILURE (Status))
558     {
559         goto CleanupReturnObject;
560     }
561 
562     /* Dereference Index and RefOf references */
563 
564     AcpiNsResolveReferences (Info);
565 
566     /* Get the size of the returned object */
567 
568     Status = AcpiUtGetObjectSize (Info->ReturnObject,
569         &BufferSpaceNeeded);
570     if (ACPI_SUCCESS (Status))
571     {
572         /* Validate/Allocate/Clear caller buffer */
573 
574         Status = AcpiUtInitializeBuffer (ReturnBuffer,
575             BufferSpaceNeeded);
576         if (ACPI_FAILURE (Status))
577         {
578             /*
579              * Caller's buffer is too small or a new one can't
580              * be allocated
581              */
582             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
583                 "Needed buffer size %X, %s\n",
584                 (UINT32) BufferSpaceNeeded,
585                 AcpiFormatException (Status)));
586         }
587         else
588         {
589             /* We have enough space for the object, build it */
590 
591             Status = AcpiUtCopyIobjectToEobject (
592                 Info->ReturnObject, ReturnBuffer);
593         }
594     }
595 
596 CleanupReturnObject:
597 
598     if (Info->ReturnObject)
599     {
600         /*
601          * Delete the internal return object. NOTE: Interpreter must be
602          * locked to avoid race condition.
603          */
604         AcpiExEnterInterpreter ();
605 
606         /* Remove one reference on the return object (should delete it) */
607 
608         AcpiUtRemoveReference (Info->ReturnObject);
609         AcpiExExitInterpreter ();
610     }
611 
612 
613 Cleanup:
614 
615     /* Free the input parameter list (if we created one) */
616 
617     if (Info->Parameters)
618     {
619         /* Free the allocated parameter block */
620 
621         AcpiUtDeleteInternalObjectList (Info->Parameters);
622     }
623 
624     ACPI_FREE (Info);
625     return_ACPI_STATUS (Status);
626 }
627 
628 ACPI_EXPORT_SYMBOL (AcpiEvaluateObject)
629 
630 
631 /*******************************************************************************
632  *
633  * FUNCTION:    AcpiNsResolveReferences
634  *
635  * PARAMETERS:  Info                    - Evaluation info block
636  *
637  * RETURN:      Info->ReturnObject is replaced with the dereferenced object
638  *
639  * DESCRIPTION: Dereference certain reference objects. Called before an
640  *              internal return object is converted to an external ACPI_OBJECT.
641  *
642  * Performs an automatic dereference of Index and RefOf reference objects.
643  * These reference objects are not supported by the ACPI_OBJECT, so this is a
644  * last resort effort to return something useful. Also, provides compatibility
645  * with other ACPI implementations.
646  *
647  * NOTE: does not handle references within returned package objects or nested
648  * references, but this support could be added later if found to be necessary.
649  *
650  ******************************************************************************/
651 
652 static void
653 AcpiNsResolveReferences (
654     ACPI_EVALUATE_INFO      *Info)
655 {
656     ACPI_OPERAND_OBJECT     *ObjDesc = NULL;
657     ACPI_NAMESPACE_NODE     *Node;
658 
659 
660     /* We are interested in reference objects only */
661 
662     if ((Info->ReturnObject)->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
663     {
664         return;
665     }
666 
667     /*
668      * Two types of references are supported - those created by Index and
669      * RefOf operators. A name reference (AML_NAMEPATH_OP) can be converted
670      * to an ACPI_OBJECT, so it is not dereferenced here. A DdbHandle
671      * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to
672      * an ACPI_OBJECT.
673      */
674     switch (Info->ReturnObject->Reference.Class)
675     {
676     case ACPI_REFCLASS_INDEX:
677 
678         ObjDesc = *(Info->ReturnObject->Reference.Where);
679         break;
680 
681     case ACPI_REFCLASS_REFOF:
682 
683         Node = Info->ReturnObject->Reference.Object;
684         if (Node)
685         {
686             ObjDesc = Node->Object;
687         }
688         break;
689 
690     default:
691 
692         return;
693     }
694 
695     /* Replace the existing reference object */
696 
697     if (ObjDesc)
698     {
699         AcpiUtAddReference (ObjDesc);
700         AcpiUtRemoveReference (Info->ReturnObject);
701         Info->ReturnObject = ObjDesc;
702     }
703 
704     return;
705 }
706 
707 
708 /*******************************************************************************
709  *
710  * FUNCTION:    AcpiWalkNamespace
711  *
712  * PARAMETERS:  Type                - ACPI_OBJECT_TYPE to search for
713  *              StartObject         - Handle in namespace where search begins
714  *              MaxDepth            - Depth to which search is to reach
715  *              DescendingCallback  - Called during tree descent
716  *                                    when an object of "Type" is found
717  *              AscendingCallback   - Called during tree ascent
718  *                                    when an object of "Type" is found
719  *              Context             - Passed to user function(s) above
720  *              ReturnValue         - Location where return value of
721  *                                    UserFunction is put if terminated early
722  *
723  * RETURNS      Return value from the UserFunction if terminated early.
724  *              Otherwise, returns NULL.
725  *
726  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
727  *              starting (and ending) at the object specified by StartHandle.
728  *              The callback function is called whenever an object that matches
729  *              the type parameter is found. If the callback function returns
730  *              a non-zero value, the search is terminated immediately and this
731  *              value is returned to the caller.
732  *
733  *              The point of this procedure is to provide a generic namespace
734  *              walk routine that can be called from multiple places to
735  *              provide multiple services; the callback function(s) can be
736  *              tailored to each task, whether it is a print function,
737  *              a compare function, etc.
738  *
739  ******************************************************************************/
740 
741 ACPI_STATUS
742 AcpiWalkNamespace (
743     ACPI_OBJECT_TYPE        Type,
744     ACPI_HANDLE             StartObject,
745     UINT32                  MaxDepth,
746     ACPI_WALK_CALLBACK      DescendingCallback,
747     ACPI_WALK_CALLBACK      AscendingCallback,
748     void                    *Context,
749     void                    **ReturnValue)
750 {
751     ACPI_STATUS             Status;
752 
753 
754     ACPI_FUNCTION_TRACE (AcpiWalkNamespace);
755 
756 
757     /* Parameter validation */
758 
759     if ((Type > ACPI_TYPE_LOCAL_MAX) ||
760         (!MaxDepth)                  ||
761         (!DescendingCallback && !AscendingCallback))
762     {
763         return_ACPI_STATUS (AE_BAD_PARAMETER);
764     }
765 
766     /*
767      * Need to acquire the namespace reader lock to prevent interference
768      * with any concurrent table unloads (which causes the deletion of
769      * namespace objects). We cannot allow the deletion of a namespace node
770      * while the user function is using it. The exception to this are the
771      * nodes created and deleted during control method execution -- these
772      * nodes are marked as temporary nodes and are ignored by the namespace
773      * walk. Thus, control methods can be executed while holding the
774      * namespace deletion lock (and the user function can execute control
775      * methods.)
776      */
777     Status = AcpiUtAcquireReadLock (&AcpiGbl_NamespaceRwLock);
778     if (ACPI_FAILURE (Status))
779     {
780         return_ACPI_STATUS (Status);
781     }
782 
783     /*
784      * Lock the namespace around the walk. The namespace will be
785      * unlocked/locked around each call to the user function - since the user
786      * function must be allowed to make ACPICA calls itself (for example, it
787      * will typically execute control methods during device enumeration.)
788      */
789     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
790     if (ACPI_FAILURE (Status))
791     {
792         goto UnlockAndExit;
793     }
794 
795     /* Now we can validate the starting node */
796 
797     if (!AcpiNsValidateHandle (StartObject))
798     {
799         Status = AE_BAD_PARAMETER;
800         goto UnlockAndExit2;
801     }
802 
803     Status = AcpiNsWalkNamespace (Type, StartObject, MaxDepth,
804         ACPI_NS_WALK_UNLOCK, DescendingCallback,
805         AscendingCallback, Context, ReturnValue);
806 
807 UnlockAndExit2:
808     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
809 
810 UnlockAndExit:
811     (void) AcpiUtReleaseReadLock (&AcpiGbl_NamespaceRwLock);
812     return_ACPI_STATUS (Status);
813 }
814 
815 ACPI_EXPORT_SYMBOL (AcpiWalkNamespace)
816 
817 
818 /*******************************************************************************
819  *
820  * FUNCTION:    AcpiNsGetDeviceCallback
821  *
822  * PARAMETERS:  Callback from AcpiGetDevice
823  *
824  * RETURN:      Status
825  *
826  * DESCRIPTION: Takes callbacks from WalkNamespace and filters out all non-
827  *              present devices, or if they specified a HID, it filters based
828  *              on that.
829  *
830  ******************************************************************************/
831 
832 static ACPI_STATUS
833 AcpiNsGetDeviceCallback (
834     ACPI_HANDLE             ObjHandle,
835     UINT32                  NestingLevel,
836     void                    *Context,
837     void                    **ReturnValue)
838 {
839     ACPI_GET_DEVICES_INFO   *Info = Context;
840     ACPI_STATUS             Status;
841     ACPI_NAMESPACE_NODE     *Node;
842     UINT32                  Flags;
843     ACPI_PNP_DEVICE_ID      *Hid;
844     ACPI_PNP_DEVICE_ID_LIST *Cid;
845     UINT32                  i;
846     BOOLEAN                 Found;
847     int                     NoMatch;
848 
849 
850     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
851     if (ACPI_FAILURE (Status))
852     {
853         return (Status);
854     }
855 
856     Node = AcpiNsValidateHandle (ObjHandle);
857     Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
858     if (ACPI_FAILURE (Status))
859     {
860         return (Status);
861     }
862 
863     if (!Node)
864     {
865         return (AE_BAD_PARAMETER);
866     }
867 
868     /*
869      * First, filter based on the device HID and CID.
870      *
871      * 01/2010: For this case where a specific HID is requested, we don't
872      * want to run _STA until we have an actual HID match. Thus, we will
873      * not unnecessarily execute _STA on devices for which the caller
874      * doesn't care about. Previously, _STA was executed unconditionally
875      * on all devices found here.
876      *
877      * A side-effect of this change is that now we will continue to search
878      * for a matching HID even under device trees where the parent device
879      * would have returned a _STA that indicates it is not present or
880      * not functioning (thus aborting the search on that branch).
881      */
882     if (Info->Hid != NULL)
883     {
884         Status = AcpiUtExecute_HID (Node, &Hid);
885         if (Status == AE_NOT_FOUND)
886         {
887             return (AE_OK);
888         }
889         else if (ACPI_FAILURE (Status))
890         {
891             return (AE_CTRL_DEPTH);
892         }
893 
894         NoMatch = strcmp (Hid->String, Info->Hid);
895         ACPI_FREE (Hid);
896 
897         if (NoMatch)
898         {
899             /*
900              * HID does not match, attempt match within the
901              * list of Compatible IDs (CIDs)
902              */
903             Status = AcpiUtExecute_CID (Node, &Cid);
904             if (Status == AE_NOT_FOUND)
905             {
906                 return (AE_OK);
907             }
908             else if (ACPI_FAILURE (Status))
909             {
910                 return (AE_CTRL_DEPTH);
911             }
912 
913             /* Walk the CID list */
914 
915             Found = FALSE;
916             for (i = 0; i < Cid->Count; i++)
917             {
918                 if (strcmp (Cid->Ids[i].String, Info->Hid) == 0)
919                 {
920                     /* Found a matching CID */
921 
922                     Found = TRUE;
923                     break;
924                 }
925             }
926 
927             ACPI_FREE (Cid);
928             if (!Found)
929             {
930                 return (AE_OK);
931             }
932         }
933     }
934 
935     /* Run _STA to determine if device is present */
936 
937     Status = AcpiUtExecute_STA (Node, &Flags);
938     if (ACPI_FAILURE (Status))
939     {
940         return (AE_CTRL_DEPTH);
941     }
942 
943     if (!(Flags & ACPI_STA_DEVICE_PRESENT) &&
944         !(Flags & ACPI_STA_DEVICE_FUNCTIONING))
945     {
946         /*
947          * Don't examine the children of the device only when the
948          * device is neither present nor functional. See ACPI spec,
949          * description of _STA for more information.
950          */
951         return (AE_CTRL_DEPTH);
952     }
953 
954     /* We have a valid device, invoke the user function */
955 
956     Status = Info->UserFunction (ObjHandle, NestingLevel,
957         Info->Context, ReturnValue);
958     return (Status);
959 }
960 
961 
962 /*******************************************************************************
963  *
964  * FUNCTION:    AcpiGetDevices
965  *
966  * PARAMETERS:  HID                 - HID to search for. Can be NULL.
967  *              UserFunction        - Called when a matching object is found
968  *              Context             - Passed to user function
969  *              ReturnValue         - Location where return value of
970  *                                    UserFunction is put if terminated early
971  *
972  * RETURNS      Return value from the UserFunction if terminated early.
973  *              Otherwise, returns NULL.
974  *
975  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
976  *              starting (and ending) at the object specified by StartHandle.
977  *              The UserFunction is called whenever an object of type
978  *              Device is found. If the user function returns
979  *              a non-zero value, the search is terminated immediately and this
980  *              value is returned to the caller.
981  *
982  *              This is a wrapper for WalkNamespace, but the callback performs
983  *              additional filtering. Please see AcpiNsGetDeviceCallback.
984  *
985  ******************************************************************************/
986 
987 ACPI_STATUS
988 AcpiGetDevices (
989     char                    *HID,
990     ACPI_WALK_CALLBACK      UserFunction,
991     void                    *Context,
992     void                    **ReturnValue)
993 {
994     ACPI_STATUS             Status;
995     ACPI_GET_DEVICES_INFO   Info;
996 
997 
998     ACPI_FUNCTION_TRACE (AcpiGetDevices);
999 
1000 
1001     /* Parameter validation */
1002 
1003     if (!UserFunction)
1004     {
1005         return_ACPI_STATUS (AE_BAD_PARAMETER);
1006     }
1007 
1008     /*
1009      * We're going to call their callback from OUR callback, so we need
1010      * to know what it is, and their context parameter.
1011      */
1012     Info.Hid = HID;
1013     Info.Context = Context;
1014     Info.UserFunction = UserFunction;
1015 
1016     /*
1017      * Lock the namespace around the walk.
1018      * The namespace will be unlocked/locked around each call
1019      * to the user function - since this function
1020      * must be allowed to make Acpi calls itself.
1021      */
1022     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1023     if (ACPI_FAILURE (Status))
1024     {
1025         return_ACPI_STATUS (Status);
1026     }
1027 
1028     Status = AcpiNsWalkNamespace (ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1029         ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
1030         AcpiNsGetDeviceCallback, NULL, &Info, ReturnValue);
1031 
1032     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1033     return_ACPI_STATUS (Status);
1034 }
1035 
1036 ACPI_EXPORT_SYMBOL (AcpiGetDevices)
1037 
1038 
1039 /*******************************************************************************
1040  *
1041  * FUNCTION:    AcpiAttachData
1042  *
1043  * PARAMETERS:  ObjHandle           - Namespace node
1044  *              Handler             - Handler for this attachment
1045  *              Data                - Pointer to data to be attached
1046  *
1047  * RETURN:      Status
1048  *
1049  * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
1050  *
1051  ******************************************************************************/
1052 
1053 ACPI_STATUS
1054 AcpiAttachData (
1055     ACPI_HANDLE             ObjHandle,
1056     ACPI_OBJECT_HANDLER     Handler,
1057     void                    *Data)
1058 {
1059     ACPI_NAMESPACE_NODE     *Node;
1060     ACPI_STATUS             Status;
1061 
1062 
1063     /* Parameter validation */
1064 
1065     if (!ObjHandle  ||
1066         !Handler    ||
1067         !Data)
1068     {
1069         return (AE_BAD_PARAMETER);
1070     }
1071 
1072     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1073     if (ACPI_FAILURE (Status))
1074     {
1075         return (Status);
1076     }
1077 
1078     /* Convert and validate the handle */
1079 
1080     Node = AcpiNsValidateHandle (ObjHandle);
1081     if (!Node)
1082     {
1083         Status = AE_BAD_PARAMETER;
1084         goto UnlockAndExit;
1085     }
1086 
1087     Status = AcpiNsAttachData (Node, Handler, Data);
1088 
1089 UnlockAndExit:
1090     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1091     return (Status);
1092 }
1093 
1094 ACPI_EXPORT_SYMBOL (AcpiAttachData)
1095 
1096 
1097 /*******************************************************************************
1098  *
1099  * FUNCTION:    AcpiDetachData
1100  *
1101  * PARAMETERS:  ObjHandle           - Namespace node handle
1102  *              Handler             - Handler used in call to AcpiAttachData
1103  *
1104  * RETURN:      Status
1105  *
1106  * DESCRIPTION: Remove data that was previously attached to a node.
1107  *
1108  ******************************************************************************/
1109 
1110 ACPI_STATUS
1111 AcpiDetachData (
1112     ACPI_HANDLE             ObjHandle,
1113     ACPI_OBJECT_HANDLER     Handler)
1114 {
1115     ACPI_NAMESPACE_NODE     *Node;
1116     ACPI_STATUS             Status;
1117 
1118 
1119     /* Parameter validation */
1120 
1121     if (!ObjHandle  ||
1122         !Handler)
1123     {
1124         return (AE_BAD_PARAMETER);
1125     }
1126 
1127     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1128     if (ACPI_FAILURE (Status))
1129     {
1130         return (Status);
1131     }
1132 
1133     /* Convert and validate the handle */
1134 
1135     Node = AcpiNsValidateHandle (ObjHandle);
1136     if (!Node)
1137     {
1138         Status = AE_BAD_PARAMETER;
1139         goto UnlockAndExit;
1140     }
1141 
1142     Status = AcpiNsDetachData (Node, Handler);
1143 
1144 UnlockAndExit:
1145     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1146     return (Status);
1147 }
1148 
1149 ACPI_EXPORT_SYMBOL (AcpiDetachData)
1150 
1151 
1152 /*******************************************************************************
1153  *
1154  * FUNCTION:    AcpiGetData
1155  *
1156  * PARAMETERS:  ObjHandle           - Namespace node
1157  *              Handler             - Handler used in call to AttachData
1158  *              Data                - Where the data is returned
1159  *
1160  * RETURN:      Status
1161  *
1162  * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
1163  *
1164  ******************************************************************************/
1165 
1166 ACPI_STATUS
1167 AcpiGetData (
1168     ACPI_HANDLE             ObjHandle,
1169     ACPI_OBJECT_HANDLER     Handler,
1170     void                    **Data)
1171 {
1172     ACPI_NAMESPACE_NODE     *Node;
1173     ACPI_STATUS             Status;
1174 
1175 
1176     /* Parameter validation */
1177 
1178     if (!ObjHandle  ||
1179         !Handler    ||
1180         !Data)
1181     {
1182         return (AE_BAD_PARAMETER);
1183     }
1184 
1185     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1186     if (ACPI_FAILURE (Status))
1187     {
1188         return (Status);
1189     }
1190 
1191     /* Convert and validate the handle */
1192 
1193     Node = AcpiNsValidateHandle (ObjHandle);
1194     if (!Node)
1195     {
1196         Status = AE_BAD_PARAMETER;
1197         goto UnlockAndExit;
1198     }
1199 
1200     Status = AcpiNsGetAttachedData (Node, Handler, Data);
1201 
1202 UnlockAndExit:
1203     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1204     return (Status);
1205 }
1206 
1207 ACPI_EXPORT_SYMBOL (AcpiGetData)
1208