xref: /freebsd/sys/contrib/dev/acpica/components/utilities/uteval.c (revision 6dcefcac2b043be030851e03a721607b414b666b)
1 /******************************************************************************
2  *
3  * Module Name: uteval - Object evaluation
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #define __UTEVAL_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
49 
50 
51 #define _COMPONENT          ACPI_UTILITIES
52         ACPI_MODULE_NAME    ("uteval")
53 
54 
55 /*******************************************************************************
56  *
57  * FUNCTION:    AcpiUtEvaluateObject
58  *
59  * PARAMETERS:  PrefixNode          - Starting node
60  *              Path                - Path to object from starting node
61  *              ExpectedReturnTypes - Bitmap of allowed return types
62  *              ReturnDesc          - Where a return value is stored
63  *
64  * RETURN:      Status
65  *
66  * DESCRIPTION: Evaluates a namespace object and verifies the type of the
67  *              return object. Common code that simplifies accessing objects
68  *              that have required return objects of fixed types.
69  *
70  *              NOTE: Internal function, no parameter validation
71  *
72  ******************************************************************************/
73 
74 ACPI_STATUS
75 AcpiUtEvaluateObject (
76     ACPI_NAMESPACE_NODE     *PrefixNode,
77     char                    *Path,
78     UINT32                  ExpectedReturnBtypes,
79     ACPI_OPERAND_OBJECT     **ReturnDesc)
80 {
81     ACPI_EVALUATE_INFO      *Info;
82     ACPI_STATUS             Status;
83     UINT32                  ReturnBtype;
84 
85 
86     ACPI_FUNCTION_TRACE (UtEvaluateObject);
87 
88 
89     /* Allocate the evaluation information block */
90 
91     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
92     if (!Info)
93     {
94         return_ACPI_STATUS (AE_NO_MEMORY);
95     }
96 
97     Info->PrefixNode = PrefixNode;
98     Info->RelativePathname = Path;
99 
100     /* Evaluate the object/method */
101 
102     Status = AcpiNsEvaluate (Info);
103     if (ACPI_FAILURE (Status))
104     {
105         if (Status == AE_NOT_FOUND)
106         {
107             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "[%4.4s.%s] was not found\n",
108                 AcpiUtGetNodeName (PrefixNode), Path));
109         }
110         else
111         {
112             ACPI_ERROR_METHOD ("Method execution failed",
113                 PrefixNode, Path, Status);
114         }
115 
116         goto Cleanup;
117     }
118 
119     /* Did we get a return object? */
120 
121     if (!Info->ReturnObject)
122     {
123         if (ExpectedReturnBtypes)
124         {
125             ACPI_ERROR_METHOD ("No object was returned from",
126                 PrefixNode, Path, AE_NOT_EXIST);
127 
128             Status = AE_NOT_EXIST;
129         }
130 
131         goto Cleanup;
132     }
133 
134     /* Map the return object type to the bitmapped type */
135 
136     switch ((Info->ReturnObject)->Common.Type)
137     {
138     case ACPI_TYPE_INTEGER:
139 
140         ReturnBtype = ACPI_BTYPE_INTEGER;
141         break;
142 
143     case ACPI_TYPE_BUFFER:
144 
145         ReturnBtype = ACPI_BTYPE_BUFFER;
146         break;
147 
148     case ACPI_TYPE_STRING:
149 
150         ReturnBtype = ACPI_BTYPE_STRING;
151         break;
152 
153     case ACPI_TYPE_PACKAGE:
154 
155         ReturnBtype = ACPI_BTYPE_PACKAGE;
156         break;
157 
158     default:
159 
160         ReturnBtype = 0;
161         break;
162     }
163 
164     if ((AcpiGbl_EnableInterpreterSlack) &&
165         (!ExpectedReturnBtypes))
166     {
167         /*
168          * We received a return object, but one was not expected. This can
169          * happen frequently if the "implicit return" feature is enabled.
170          * Just delete the return object and return AE_OK.
171          */
172         AcpiUtRemoveReference (Info->ReturnObject);
173         goto Cleanup;
174     }
175 
176     /* Is the return object one of the expected types? */
177 
178     if (!(ExpectedReturnBtypes & ReturnBtype))
179     {
180         ACPI_ERROR_METHOD ("Return object type is incorrect",
181             PrefixNode, Path, AE_TYPE);
182 
183         ACPI_ERROR ((AE_INFO,
184             "Type returned from %s was incorrect: %s, expected Btypes: 0x%X",
185             Path, AcpiUtGetObjectTypeName (Info->ReturnObject),
186             ExpectedReturnBtypes));
187 
188         /* On error exit, we must delete the return object */
189 
190         AcpiUtRemoveReference (Info->ReturnObject);
191         Status = AE_TYPE;
192         goto Cleanup;
193     }
194 
195     /* Object type is OK, return it */
196 
197     *ReturnDesc = Info->ReturnObject;
198 
199 Cleanup:
200     ACPI_FREE (Info);
201     return_ACPI_STATUS (Status);
202 }
203 
204 
205 /*******************************************************************************
206  *
207  * FUNCTION:    AcpiUtEvaluateNumericObject
208  *
209  * PARAMETERS:  ObjectName          - Object name to be evaluated
210  *              DeviceNode          - Node for the device
211  *              Value               - Where the value is returned
212  *
213  * RETURN:      Status
214  *
215  * DESCRIPTION: Evaluates a numeric namespace object for a selected device
216  *              and stores result in *Value.
217  *
218  *              NOTE: Internal function, no parameter validation
219  *
220  ******************************************************************************/
221 
222 ACPI_STATUS
223 AcpiUtEvaluateNumericObject (
224     char                    *ObjectName,
225     ACPI_NAMESPACE_NODE     *DeviceNode,
226     UINT64                  *Value)
227 {
228     ACPI_OPERAND_OBJECT     *ObjDesc;
229     ACPI_STATUS             Status;
230 
231 
232     ACPI_FUNCTION_TRACE (UtEvaluateNumericObject);
233 
234 
235     Status = AcpiUtEvaluateObject (DeviceNode, ObjectName,
236                 ACPI_BTYPE_INTEGER, &ObjDesc);
237     if (ACPI_FAILURE (Status))
238     {
239         return_ACPI_STATUS (Status);
240     }
241 
242     /* Get the returned Integer */
243 
244     *Value = ObjDesc->Integer.Value;
245 
246     /* On exit, we must delete the return object */
247 
248     AcpiUtRemoveReference (ObjDesc);
249     return_ACPI_STATUS (Status);
250 }
251 
252 
253 /*******************************************************************************
254  *
255  * FUNCTION:    AcpiUtExecute_STA
256  *
257  * PARAMETERS:  DeviceNode          - Node for the device
258  *              Flags               - Where the status flags are returned
259  *
260  * RETURN:      Status
261  *
262  * DESCRIPTION: Executes _STA for selected device and stores results in
263  *              *Flags.
264  *
265  *              NOTE: Internal function, no parameter validation
266  *
267  ******************************************************************************/
268 
269 ACPI_STATUS
270 AcpiUtExecute_STA (
271     ACPI_NAMESPACE_NODE     *DeviceNode,
272     UINT32                  *Flags)
273 {
274     ACPI_OPERAND_OBJECT     *ObjDesc;
275     ACPI_STATUS             Status;
276 
277 
278     ACPI_FUNCTION_TRACE (UtExecute_STA);
279 
280 
281     Status = AcpiUtEvaluateObject (DeviceNode, METHOD_NAME__STA,
282                 ACPI_BTYPE_INTEGER, &ObjDesc);
283     if (ACPI_FAILURE (Status))
284     {
285         if (AE_NOT_FOUND == Status)
286         {
287             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
288                 "_STA on %4.4s was not found, assuming device is present\n",
289                 AcpiUtGetNodeName (DeviceNode)));
290 
291             *Flags = ACPI_UINT32_MAX;
292             Status = AE_OK;
293         }
294 
295         return_ACPI_STATUS (Status);
296     }
297 
298     /* Extract the status flags */
299 
300     *Flags = (UINT32) ObjDesc->Integer.Value;
301 
302     /* On exit, we must delete the return object */
303 
304     AcpiUtRemoveReference (ObjDesc);
305     return_ACPI_STATUS (Status);
306 }
307 
308 
309 /*******************************************************************************
310  *
311  * FUNCTION:    AcpiUtExecutePowerMethods
312  *
313  * PARAMETERS:  DeviceNode          - Node for the device
314  *              MethodNames         - Array of power method names
315  *              MethodCount         - Number of methods to execute
316  *              OutValues           - Where the power method values are returned
317  *
318  * RETURN:      Status, OutValues
319  *
320  * DESCRIPTION: Executes the specified power methods for the device and returns
321  *              the result(s).
322  *
323  *              NOTE: Internal function, no parameter validation
324  *
325  ******************************************************************************/
326 
327 ACPI_STATUS
328 AcpiUtExecutePowerMethods (
329     ACPI_NAMESPACE_NODE     *DeviceNode,
330     const char              **MethodNames,
331     UINT8                   MethodCount,
332     UINT8                   *OutValues)
333 {
334     ACPI_OPERAND_OBJECT     *ObjDesc;
335     ACPI_STATUS             Status;
336     ACPI_STATUS             FinalStatus = AE_NOT_FOUND;
337     UINT32                  i;
338 
339 
340     ACPI_FUNCTION_TRACE (UtExecutePowerMethods);
341 
342 
343     for (i = 0; i < MethodCount; i++)
344     {
345         /*
346          * Execute the power method (_SxD or _SxW). The only allowable
347          * return type is an Integer.
348          */
349         Status = AcpiUtEvaluateObject (DeviceNode,
350                     ACPI_CAST_PTR (char, MethodNames[i]),
351                     ACPI_BTYPE_INTEGER, &ObjDesc);
352         if (ACPI_SUCCESS (Status))
353         {
354             OutValues[i] = (UINT8) ObjDesc->Integer.Value;
355 
356             /* Delete the return object */
357 
358             AcpiUtRemoveReference (ObjDesc);
359             FinalStatus = AE_OK;            /* At least one value is valid */
360             continue;
361         }
362 
363         OutValues[i] = ACPI_UINT8_MAX;
364         if (Status == AE_NOT_FOUND)
365         {
366             continue; /* Ignore if not found */
367         }
368 
369         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Failed %s on Device %4.4s, %s\n",
370             ACPI_CAST_PTR (char, MethodNames[i]),
371             AcpiUtGetNodeName (DeviceNode), AcpiFormatException (Status)));
372     }
373 
374     return_ACPI_STATUS (FinalStatus);
375 }
376