nsxfeval.c revision 249663
1/*******************************************************************************
2 *
3 * Module Name: nsxfeval - Public interfaces to the ACPI subsystem
4 *                         ACPI Object evaluation interfaces
5 *
6 ******************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2013, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
30 *
31 * NO WARRANTY
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.
43 */
44
45
46#define __NSXFEVAL_C__
47
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>
52
53
54#define _COMPONENT          ACPI_NAMESPACE
55        ACPI_MODULE_NAME    ("nsxfeval")
56
57/* Local prototypes */
58
59static void
60AcpiNsResolveReferences (
61    ACPI_EVALUATE_INFO      *Info);
62
63
64/*******************************************************************************
65 *
66 * FUNCTION:    AcpiEvaluateObjectTyped
67 *
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
76 *
77 * RETURN:      Status
78 *
79 * DESCRIPTION: Find and evaluate the given object, passing the given
80 *              parameters if necessary. One of "Handle" or "Pathname" must
81 *              be valid (non-null)
82 *
83 ******************************************************************************/
84
85ACPI_STATUS
86AcpiEvaluateObjectTyped (
87    ACPI_HANDLE             Handle,
88    ACPI_STRING             Pathname,
89    ACPI_OBJECT_LIST        *ExternalParams,
90    ACPI_BUFFER             *ReturnBuffer,
91    ACPI_OBJECT_TYPE        ReturnType)
92{
93    ACPI_STATUS             Status;
94    BOOLEAN                 MustFree = FALSE;
95
96
97    ACPI_FUNCTION_TRACE (AcpiEvaluateObjectTyped);
98
99
100    /* Return buffer must be valid */
101
102    if (!ReturnBuffer)
103    {
104        return_ACPI_STATUS (AE_BAD_PARAMETER);
105    }
106
107    if (ReturnBuffer->Length == ACPI_ALLOCATE_BUFFER)
108    {
109        MustFree = TRUE;
110    }
111
112    /* Evaluate the object */
113
114    Status = AcpiEvaluateObject (Handle, Pathname, ExternalParams, ReturnBuffer);
115    if (ACPI_FAILURE (Status))
116    {
117        return_ACPI_STATUS (Status);
118    }
119
120    /* Type ANY means "don't care" */
121
122    if (ReturnType == ACPI_TYPE_ANY)
123    {
124        return_ACPI_STATUS (AE_OK);
125    }
126
127    if (ReturnBuffer->Length == 0)
128    {
129        /* Error because caller specifically asked for a return value */
130
131        ACPI_ERROR ((AE_INFO, "No return value"));
132        return_ACPI_STATUS (AE_NULL_OBJECT);
133    }
134
135    /* Examine the object type returned from EvaluateObject */
136
137    if (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type == ReturnType)
138    {
139        return_ACPI_STATUS (AE_OK);
140    }
141
142    /* Return object type does not match requested type */
143
144    ACPI_ERROR ((AE_INFO,
145        "Incorrect return type [%s] requested [%s]",
146        AcpiUtGetTypeName (((ACPI_OBJECT *) ReturnBuffer->Pointer)->Type),
147        AcpiUtGetTypeName (ReturnType)));
148
149    if (MustFree)
150    {
151        /* Caller used ACPI_ALLOCATE_BUFFER, free the return buffer */
152
153        AcpiOsFree (ReturnBuffer->Pointer);
154        ReturnBuffer->Pointer = NULL;
155    }
156
157    ReturnBuffer->Length = 0;
158    return_ACPI_STATUS (AE_TYPE);
159}
160
161ACPI_EXPORT_SYMBOL (AcpiEvaluateObjectTyped)
162
163
164/*******************************************************************************
165 *
166 * FUNCTION:    AcpiEvaluateObject
167 *
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.
175 *
176 * RETURN:      Status
177 *
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)
181 *
182 ******************************************************************************/
183
184ACPI_STATUS
185AcpiEvaluateObject (
186    ACPI_HANDLE             Handle,
187    ACPI_STRING             Pathname,
188    ACPI_OBJECT_LIST        *ExternalParams,
189    ACPI_BUFFER             *ReturnBuffer)
190{
191    ACPI_STATUS             Status;
192    ACPI_EVALUATE_INFO      *Info;
193    ACPI_SIZE               BufferSpaceNeeded;
194    UINT32                  i;
195
196
197    ACPI_FUNCTION_TRACE (AcpiEvaluateObject);
198
199
200    /* Allocate and initialize the evaluation information block */
201
202    Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
203    if (!Info)
204    {
205        return_ACPI_STATUS (AE_NO_MEMORY);
206    }
207
208    /* Convert and validate the device handle */
209
210    Info->PrefixNode = AcpiNsValidateHandle (Handle);
211    if (!Info->PrefixNode)
212    {
213        Status = AE_BAD_PARAMETER;
214        goto Cleanup;
215    }
216
217    /*
218     * Get the actual namespace node for the target object.
219     * Handles these cases:
220     *
221     * 1) Null node, valid pathname from root (absolute path)
222     * 2) Node and valid pathname (path relative to Node)
223     * 3) Node, Null pathname
224     */
225    if ((Pathname) &&
226        (ACPI_IS_ROOT_PREFIX (Pathname[0])))
227    {
228        /* The path is fully qualified, just evaluate by name */
229
230        Info->PrefixNode = NULL;
231    }
232    else if (!Handle)
233    {
234        /*
235         * A handle is optional iff a fully qualified pathname is specified.
236         * Since we've already handled fully qualified names above, this is
237         * an error.
238         */
239        if (!Pathname)
240        {
241            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
242                "Both Handle and Pathname are NULL"));
243        }
244        else
245        {
246            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
247                "Null Handle with relative pathname [%s]", Pathname));
248        }
249
250        Status = AE_BAD_PARAMETER;
251        goto Cleanup;
252    }
253
254    Info->RelativePathname = Pathname;
255
256    /*
257     * Convert all external objects passed as arguments to the
258     * internal version(s).
259     */
260    if (ExternalParams && ExternalParams->Count)
261    {
262        Info->ParamCount = (UINT16) ExternalParams->Count;
263
264        /* Warn on impossible argument count */
265
266        if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
267        {
268            ACPI_WARN_PREDEFINED ((AE_INFO, Pathname, ACPI_WARN_ALWAYS,
269                "Excess arguments (%u) - using only %u",
270                Info->ParamCount, ACPI_METHOD_NUM_ARGS));
271
272            Info->ParamCount = ACPI_METHOD_NUM_ARGS;
273        }
274
275        /*
276         * Allocate a new parameter block for the internal objects
277         * Add 1 to count to allow for null terminated internal list
278         */
279        Info->Parameters = ACPI_ALLOCATE_ZEROED (
280            ((ACPI_SIZE) Info->ParamCount + 1) * sizeof (void *));
281        if (!Info->Parameters)
282        {
283            Status = AE_NO_MEMORY;
284            goto Cleanup;
285        }
286
287        /* Convert each external object in the list to an internal object */
288
289        for (i = 0; i < Info->ParamCount; i++)
290        {
291            Status = AcpiUtCopyEobjectToIobject (
292                &ExternalParams->Pointer[i], &Info->Parameters[i]);
293            if (ACPI_FAILURE (Status))
294            {
295                goto Cleanup;
296            }
297        }
298
299        Info->Parameters[Info->ParamCount] = NULL;
300    }
301
302
303#if 0
304
305    /*
306     * Begin incoming argument count analysis. Check for too few args
307     * and too many args.
308     */
309
310    switch (AcpiNsGetType (Info->Node))
311    {
312    case ACPI_TYPE_METHOD:
313
314        /* Check incoming argument count against the method definition */
315
316        if (Info->ObjDesc->Method.ParamCount > Info->ParamCount)
317        {
318            ACPI_ERROR ((AE_INFO,
319                "Insufficient arguments (%u) - %u are required",
320                Info->ParamCount,
321                Info->ObjDesc->Method.ParamCount));
322
323            Status = AE_MISSING_ARGUMENTS;
324            goto Cleanup;
325        }
326
327        else if (Info->ObjDesc->Method.ParamCount < Info->ParamCount)
328        {
329            ACPI_WARNING ((AE_INFO,
330                "Excess arguments (%u) - only %u are required",
331                Info->ParamCount,
332                Info->ObjDesc->Method.ParamCount));
333
334            /* Just pass the required number of arguments */
335
336            Info->ParamCount = Info->ObjDesc->Method.ParamCount;
337        }
338
339        /*
340         * Any incoming external objects to be passed as arguments to the
341         * method must be converted to internal objects
342         */
343        if (Info->ParamCount)
344        {
345            /*
346             * Allocate a new parameter block for the internal objects
347             * Add 1 to count to allow for null terminated internal list
348             */
349            Info->Parameters = ACPI_ALLOCATE_ZEROED (
350                ((ACPI_SIZE) Info->ParamCount + 1) * sizeof (void *));
351            if (!Info->Parameters)
352            {
353                Status = AE_NO_MEMORY;
354                goto Cleanup;
355            }
356
357            /* Convert each external object in the list to an internal object */
358
359            for (i = 0; i < Info->ParamCount; i++)
360            {
361                Status = AcpiUtCopyEobjectToIobject (
362                    &ExternalParams->Pointer[i], &Info->Parameters[i]);
363                if (ACPI_FAILURE (Status))
364                {
365                    goto Cleanup;
366                }
367            }
368
369            Info->Parameters[Info->ParamCount] = NULL;
370        }
371        break;
372
373    default:
374
375        /* Warn if arguments passed to an object that is not a method */
376
377        if (Info->ParamCount)
378        {
379            ACPI_WARNING ((AE_INFO,
380                "%u arguments were passed to a non-method ACPI object",
381                Info->ParamCount));
382        }
383        break;
384    }
385
386#endif
387
388
389    /* Now we can evaluate the object */
390
391    Status = AcpiNsEvaluate (Info);
392
393    /*
394     * If we are expecting a return value, and all went well above,
395     * copy the return value to an external object.
396     */
397    if (ReturnBuffer)
398    {
399        if (!Info->ReturnObject)
400        {
401            ReturnBuffer->Length = 0;
402        }
403        else
404        {
405            if (ACPI_GET_DESCRIPTOR_TYPE (Info->ReturnObject) ==
406                ACPI_DESC_TYPE_NAMED)
407            {
408                /*
409                 * If we received a NS Node as a return object, this means that
410                 * the object we are evaluating has nothing interesting to
411                 * return (such as a mutex, etc.)  We return an error because
412                 * these types are essentially unsupported by this interface.
413                 * We don't check up front because this makes it easier to add
414                 * support for various types at a later date if necessary.
415                 */
416                Status = AE_TYPE;
417                Info->ReturnObject = NULL;   /* No need to delete a NS Node */
418                ReturnBuffer->Length = 0;
419            }
420
421            if (ACPI_SUCCESS (Status))
422            {
423                /* Dereference Index and RefOf references */
424
425                AcpiNsResolveReferences (Info);
426
427                /* Get the size of the returned object */
428
429                Status = AcpiUtGetObjectSize (Info->ReturnObject,
430                            &BufferSpaceNeeded);
431                if (ACPI_SUCCESS (Status))
432                {
433                    /* Validate/Allocate/Clear caller buffer */
434
435                    Status = AcpiUtInitializeBuffer (ReturnBuffer,
436                                BufferSpaceNeeded);
437                    if (ACPI_FAILURE (Status))
438                    {
439                        /*
440                         * Caller's buffer is too small or a new one can't
441                         * be allocated
442                         */
443                        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
444                            "Needed buffer size %X, %s\n",
445                            (UINT32) BufferSpaceNeeded,
446                            AcpiFormatException (Status)));
447                    }
448                    else
449                    {
450                        /* We have enough space for the object, build it */
451
452                        Status = AcpiUtCopyIobjectToEobject (Info->ReturnObject,
453                                    ReturnBuffer);
454                    }
455                }
456            }
457        }
458    }
459
460    if (Info->ReturnObject)
461    {
462        /*
463         * Delete the internal return object. NOTE: Interpreter must be
464         * locked to avoid race condition.
465         */
466        AcpiExEnterInterpreter ();
467
468        /* Remove one reference on the return object (should delete it) */
469
470        AcpiUtRemoveReference (Info->ReturnObject);
471        AcpiExExitInterpreter ();
472    }
473
474
475Cleanup:
476
477    /* Free the input parameter list (if we created one) */
478
479    if (Info->Parameters)
480    {
481        /* Free the allocated parameter block */
482
483        AcpiUtDeleteInternalObjectList (Info->Parameters);
484    }
485
486    ACPI_FREE (Info);
487    return_ACPI_STATUS (Status);
488}
489
490ACPI_EXPORT_SYMBOL (AcpiEvaluateObject)
491
492
493/*******************************************************************************
494 *
495 * FUNCTION:    AcpiNsResolveReferences
496 *
497 * PARAMETERS:  Info                    - Evaluation info block
498 *
499 * RETURN:      Info->ReturnObject is replaced with the dereferenced object
500 *
501 * DESCRIPTION: Dereference certain reference objects. Called before an
502 *              internal return object is converted to an external ACPI_OBJECT.
503 *
504 * Performs an automatic dereference of Index and RefOf reference objects.
505 * These reference objects are not supported by the ACPI_OBJECT, so this is a
506 * last resort effort to return something useful. Also, provides compatibility
507 * with other ACPI implementations.
508 *
509 * NOTE: does not handle references within returned package objects or nested
510 * references, but this support could be added later if found to be necessary.
511 *
512 ******************************************************************************/
513
514static void
515AcpiNsResolveReferences (
516    ACPI_EVALUATE_INFO      *Info)
517{
518    ACPI_OPERAND_OBJECT     *ObjDesc = NULL;
519    ACPI_NAMESPACE_NODE     *Node;
520
521
522    /* We are interested in reference objects only */
523
524    if ((Info->ReturnObject)->Common.Type != ACPI_TYPE_LOCAL_REFERENCE)
525    {
526        return;
527    }
528
529    /*
530     * Two types of references are supported - those created by Index and
531     * RefOf operators. A name reference (AML_NAMEPATH_OP) can be converted
532     * to an ACPI_OBJECT, so it is not dereferenced here. A DdbHandle
533     * (AML_LOAD_OP) cannot be dereferenced, nor can it be converted to
534     * an ACPI_OBJECT.
535     */
536    switch (Info->ReturnObject->Reference.Class)
537    {
538    case ACPI_REFCLASS_INDEX:
539
540        ObjDesc = *(Info->ReturnObject->Reference.Where);
541        break;
542
543    case ACPI_REFCLASS_REFOF:
544
545        Node = Info->ReturnObject->Reference.Object;
546        if (Node)
547        {
548            ObjDesc = Node->Object;
549        }
550        break;
551
552    default:
553        return;
554    }
555
556    /* Replace the existing reference object */
557
558    if (ObjDesc)
559    {
560        AcpiUtAddReference (ObjDesc);
561        AcpiUtRemoveReference (Info->ReturnObject);
562        Info->ReturnObject = ObjDesc;
563    }
564
565    return;
566}
567
568
569/*******************************************************************************
570 *
571 * FUNCTION:    AcpiWalkNamespace
572 *
573 * PARAMETERS:  Type                - ACPI_OBJECT_TYPE to search for
574 *              StartObject         - Handle in namespace where search begins
575 *              MaxDepth            - Depth to which search is to reach
576 *              PreOrderVisit       - Called during tree pre-order visit
577 *                                    when an object of "Type" is found
578 *              PostOrderVisit      - Called during tree post-order visit
579 *                                    when an object of "Type" is found
580 *              Context             - Passed to user function(s) above
581 *              ReturnValue         - Location where return value of
582 *                                    UserFunction is put if terminated early
583 *
584 * RETURNS      Return value from the UserFunction if terminated early.
585 *              Otherwise, returns NULL.
586 *
587 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
588 *              starting (and ending) at the object specified by StartHandle.
589 *              The callback function is called whenever an object that matches
590 *              the type parameter is found. If the callback function returns
591 *              a non-zero value, the search is terminated immediately and this
592 *              value is returned to the caller.
593 *
594 *              The point of this procedure is to provide a generic namespace
595 *              walk routine that can be called from multiple places to
596 *              provide multiple services; the callback function(s) can be
597 *              tailored to each task, whether it is a print function,
598 *              a compare function, etc.
599 *
600 ******************************************************************************/
601
602ACPI_STATUS
603AcpiWalkNamespace (
604    ACPI_OBJECT_TYPE        Type,
605    ACPI_HANDLE             StartObject,
606    UINT32                  MaxDepth,
607    ACPI_WALK_CALLBACK      PreOrderVisit,
608    ACPI_WALK_CALLBACK      PostOrderVisit,
609    void                    *Context,
610    void                    **ReturnValue)
611{
612    ACPI_STATUS             Status;
613
614
615    ACPI_FUNCTION_TRACE (AcpiWalkNamespace);
616
617
618    /* Parameter validation */
619
620    if ((Type > ACPI_TYPE_LOCAL_MAX) ||
621        (!MaxDepth)                  ||
622        (!PreOrderVisit && !PostOrderVisit))
623    {
624        return_ACPI_STATUS (AE_BAD_PARAMETER);
625    }
626
627    /*
628     * Need to acquire the namespace reader lock to prevent interference
629     * with any concurrent table unloads (which causes the deletion of
630     * namespace objects). We cannot allow the deletion of a namespace node
631     * while the user function is using it. The exception to this are the
632     * nodes created and deleted during control method execution -- these
633     * nodes are marked as temporary nodes and are ignored by the namespace
634     * walk. Thus, control methods can be executed while holding the
635     * namespace deletion lock (and the user function can execute control
636     * methods.)
637     */
638    Status = AcpiUtAcquireReadLock (&AcpiGbl_NamespaceRwLock);
639    if (ACPI_FAILURE (Status))
640    {
641        return_ACPI_STATUS (Status);
642    }
643
644    /*
645     * Lock the namespace around the walk. The namespace will be
646     * unlocked/locked around each call to the user function - since the user
647     * function must be allowed to make ACPICA calls itself (for example, it
648     * will typically execute control methods during device enumeration.)
649     */
650    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
651    if (ACPI_FAILURE (Status))
652    {
653        goto UnlockAndExit;
654    }
655
656    Status = AcpiNsWalkNamespace (Type, StartObject, MaxDepth,
657                ACPI_NS_WALK_UNLOCK, PreOrderVisit,
658                PostOrderVisit, Context, ReturnValue);
659
660    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
661
662UnlockAndExit:
663    (void) AcpiUtReleaseReadLock (&AcpiGbl_NamespaceRwLock);
664    return_ACPI_STATUS (Status);
665}
666
667ACPI_EXPORT_SYMBOL (AcpiWalkNamespace)
668
669
670/*******************************************************************************
671 *
672 * FUNCTION:    AcpiNsGetDeviceCallback
673 *
674 * PARAMETERS:  Callback from AcpiGetDevice
675 *
676 * RETURN:      Status
677 *
678 * DESCRIPTION: Takes callbacks from WalkNamespace and filters out all non-
679 *              present devices, or if they specified a HID, it filters based
680 *              on that.
681 *
682 ******************************************************************************/
683
684static ACPI_STATUS
685AcpiNsGetDeviceCallback (
686    ACPI_HANDLE             ObjHandle,
687    UINT32                  NestingLevel,
688    void                    *Context,
689    void                    **ReturnValue)
690{
691    ACPI_GET_DEVICES_INFO   *Info = Context;
692    ACPI_STATUS             Status;
693    ACPI_NAMESPACE_NODE     *Node;
694    UINT32                  Flags;
695    ACPI_PNP_DEVICE_ID      *Hid;
696    ACPI_PNP_DEVICE_ID_LIST *Cid;
697    UINT32                  i;
698    BOOLEAN                 Found;
699    int                     NoMatch;
700
701
702    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
703    if (ACPI_FAILURE (Status))
704    {
705        return (Status);
706    }
707
708    Node = AcpiNsValidateHandle (ObjHandle);
709    Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
710    if (ACPI_FAILURE (Status))
711    {
712        return (Status);
713    }
714
715    if (!Node)
716    {
717        return (AE_BAD_PARAMETER);
718    }
719
720    /*
721     * First, filter based on the device HID and CID.
722     *
723     * 01/2010: For this case where a specific HID is requested, we don't
724     * want to run _STA until we have an actual HID match. Thus, we will
725     * not unnecessarily execute _STA on devices for which the caller
726     * doesn't care about. Previously, _STA was executed unconditionally
727     * on all devices found here.
728     *
729     * A side-effect of this change is that now we will continue to search
730     * for a matching HID even under device trees where the parent device
731     * would have returned a _STA that indicates it is not present or
732     * not functioning (thus aborting the search on that branch).
733     */
734    if (Info->Hid != NULL)
735    {
736        Status = AcpiUtExecute_HID (Node, &Hid);
737        if (Status == AE_NOT_FOUND)
738        {
739            return (AE_OK);
740        }
741        else if (ACPI_FAILURE (Status))
742        {
743            return (AE_CTRL_DEPTH);
744        }
745
746        NoMatch = ACPI_STRCMP (Hid->String, Info->Hid);
747        ACPI_FREE (Hid);
748
749        if (NoMatch)
750        {
751            /*
752             * HID does not match, attempt match within the
753             * list of Compatible IDs (CIDs)
754             */
755            Status = AcpiUtExecute_CID (Node, &Cid);
756            if (Status == AE_NOT_FOUND)
757            {
758                return (AE_OK);
759            }
760            else if (ACPI_FAILURE (Status))
761            {
762                return (AE_CTRL_DEPTH);
763            }
764
765            /* Walk the CID list */
766
767            Found = FALSE;
768            for (i = 0; i < Cid->Count; i++)
769            {
770                if (ACPI_STRCMP (Cid->Ids[i].String, Info->Hid) == 0)
771                {
772                    /* Found a matching CID */
773
774                    Found = TRUE;
775                    break;
776                }
777            }
778
779            ACPI_FREE (Cid);
780            if (!Found)
781            {
782                return (AE_OK);
783            }
784        }
785    }
786
787    /* Run _STA to determine if device is present */
788
789    Status = AcpiUtExecute_STA (Node, &Flags);
790    if (ACPI_FAILURE (Status))
791    {
792        return (AE_CTRL_DEPTH);
793    }
794
795    if (!(Flags & ACPI_STA_DEVICE_PRESENT) &&
796        !(Flags & ACPI_STA_DEVICE_FUNCTIONING))
797    {
798        /*
799         * Don't examine the children of the device only when the
800         * device is neither present nor functional. See ACPI spec,
801         * description of _STA for more information.
802         */
803        return (AE_CTRL_DEPTH);
804    }
805
806    /* We have a valid device, invoke the user function */
807
808    Status = Info->UserFunction (ObjHandle, NestingLevel, Info->Context,
809                ReturnValue);
810    return (Status);
811}
812
813
814/*******************************************************************************
815 *
816 * FUNCTION:    AcpiGetDevices
817 *
818 * PARAMETERS:  HID                 - HID to search for. Can be NULL.
819 *              UserFunction        - Called when a matching object is found
820 *              Context             - Passed to user function
821 *              ReturnValue         - Location where return value of
822 *                                    UserFunction is put if terminated early
823 *
824 * RETURNS      Return value from the UserFunction if terminated early.
825 *              Otherwise, returns NULL.
826 *
827 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
828 *              starting (and ending) at the object specified by StartHandle.
829 *              The UserFunction is called whenever an object of type
830 *              Device is found. If the user function returns
831 *              a non-zero value, the search is terminated immediately and this
832 *              value is returned to the caller.
833 *
834 *              This is a wrapper for WalkNamespace, but the callback performs
835 *              additional filtering. Please see AcpiNsGetDeviceCallback.
836 *
837 ******************************************************************************/
838
839ACPI_STATUS
840AcpiGetDevices (
841    char                    *HID,
842    ACPI_WALK_CALLBACK      UserFunction,
843    void                    *Context,
844    void                    **ReturnValue)
845{
846    ACPI_STATUS             Status;
847    ACPI_GET_DEVICES_INFO   Info;
848
849
850    ACPI_FUNCTION_TRACE (AcpiGetDevices);
851
852
853    /* Parameter validation */
854
855    if (!UserFunction)
856    {
857        return_ACPI_STATUS (AE_BAD_PARAMETER);
858    }
859
860    /*
861     * We're going to call their callback from OUR callback, so we need
862     * to know what it is, and their context parameter.
863     */
864    Info.Hid          = HID;
865    Info.Context      = Context;
866    Info.UserFunction = UserFunction;
867
868    /*
869     * Lock the namespace around the walk.
870     * The namespace will be unlocked/locked around each call
871     * to the user function - since this function
872     * must be allowed to make Acpi calls itself.
873     */
874    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
875    if (ACPI_FAILURE (Status))
876    {
877        return_ACPI_STATUS (Status);
878    }
879
880    Status = AcpiNsWalkNamespace (ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
881                ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
882                AcpiNsGetDeviceCallback, NULL, &Info, ReturnValue);
883
884    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
885    return_ACPI_STATUS (Status);
886}
887
888ACPI_EXPORT_SYMBOL (AcpiGetDevices)
889
890
891/*******************************************************************************
892 *
893 * FUNCTION:    AcpiAttachData
894 *
895 * PARAMETERS:  ObjHandle           - Namespace node
896 *              Handler             - Handler for this attachment
897 *              Data                - Pointer to data to be attached
898 *
899 * RETURN:      Status
900 *
901 * DESCRIPTION: Attach arbitrary data and handler to a namespace node.
902 *
903 ******************************************************************************/
904
905ACPI_STATUS
906AcpiAttachData (
907    ACPI_HANDLE             ObjHandle,
908    ACPI_OBJECT_HANDLER     Handler,
909    void                    *Data)
910{
911    ACPI_NAMESPACE_NODE     *Node;
912    ACPI_STATUS             Status;
913
914
915    /* Parameter validation */
916
917    if (!ObjHandle  ||
918        !Handler    ||
919        !Data)
920    {
921        return (AE_BAD_PARAMETER);
922    }
923
924    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
925    if (ACPI_FAILURE (Status))
926    {
927        return (Status);
928    }
929
930    /* Convert and validate the handle */
931
932    Node = AcpiNsValidateHandle (ObjHandle);
933    if (!Node)
934    {
935        Status = AE_BAD_PARAMETER;
936        goto UnlockAndExit;
937    }
938
939    Status = AcpiNsAttachData (Node, Handler, Data);
940
941UnlockAndExit:
942    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
943    return (Status);
944}
945
946ACPI_EXPORT_SYMBOL (AcpiAttachData)
947
948
949/*******************************************************************************
950 *
951 * FUNCTION:    AcpiDetachData
952 *
953 * PARAMETERS:  ObjHandle           - Namespace node handle
954 *              Handler             - Handler used in call to AcpiAttachData
955 *
956 * RETURN:      Status
957 *
958 * DESCRIPTION: Remove data that was previously attached to a node.
959 *
960 ******************************************************************************/
961
962ACPI_STATUS
963AcpiDetachData (
964    ACPI_HANDLE             ObjHandle,
965    ACPI_OBJECT_HANDLER     Handler)
966{
967    ACPI_NAMESPACE_NODE     *Node;
968    ACPI_STATUS             Status;
969
970
971    /* Parameter validation */
972
973    if (!ObjHandle  ||
974        !Handler)
975    {
976        return (AE_BAD_PARAMETER);
977    }
978
979    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
980    if (ACPI_FAILURE (Status))
981    {
982        return (Status);
983    }
984
985    /* Convert and validate the handle */
986
987    Node = AcpiNsValidateHandle (ObjHandle);
988    if (!Node)
989    {
990        Status = AE_BAD_PARAMETER;
991        goto UnlockAndExit;
992    }
993
994    Status = AcpiNsDetachData (Node, Handler);
995
996UnlockAndExit:
997    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
998    return (Status);
999}
1000
1001ACPI_EXPORT_SYMBOL (AcpiDetachData)
1002
1003
1004/*******************************************************************************
1005 *
1006 * FUNCTION:    AcpiGetData
1007 *
1008 * PARAMETERS:  ObjHandle           - Namespace node
1009 *              Handler             - Handler used in call to AttachData
1010 *              Data                - Where the data is returned
1011 *
1012 * RETURN:      Status
1013 *
1014 * DESCRIPTION: Retrieve data that was previously attached to a namespace node.
1015 *
1016 ******************************************************************************/
1017
1018ACPI_STATUS
1019AcpiGetData (
1020    ACPI_HANDLE             ObjHandle,
1021    ACPI_OBJECT_HANDLER     Handler,
1022    void                    **Data)
1023{
1024    ACPI_NAMESPACE_NODE     *Node;
1025    ACPI_STATUS             Status;
1026
1027
1028    /* Parameter validation */
1029
1030    if (!ObjHandle  ||
1031        !Handler    ||
1032        !Data)
1033    {
1034        return (AE_BAD_PARAMETER);
1035    }
1036
1037    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
1038    if (ACPI_FAILURE (Status))
1039    {
1040        return (Status);
1041    }
1042
1043    /* Convert and validate the handle */
1044
1045    Node = AcpiNsValidateHandle (ObjHandle);
1046    if (!Node)
1047    {
1048        Status = AE_BAD_PARAMETER;
1049        goto UnlockAndExit;
1050    }
1051
1052    Status = AcpiNsGetAttachedData (Node, Handler, Data);
1053
1054UnlockAndExit:
1055    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
1056    return (Status);
1057}
1058
1059ACPI_EXPORT_SYMBOL (AcpiGetData)
1060