1 /******************************************************************************
3 * Module Name: examples - Example ACPICA code
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 /* Set the ACPICA application type for use in include/platform/acenv.h */
51 #define ACPI_DEBUG_OUTPUT
53 /* ACPICA public headers */
57 #define _COMPONENT ACPI_EXAMPLE
58 ACPI_MODULE_NAME ("examples")
61 /******************************************************************************
65 * This module contains examples of how the host OS should interface to the
68 * 1) How to use the platform/acenv.h file and how to set configuration
71 * 2) main - using the debug output mechanism and the error/warning output
74 * 3) Two examples of the ACPICA initialization sequence. The first is a
75 * initialization with no "early" ACPI table access. The second shows
76 * how to use ACPICA to obtain the tables very early during kernel
77 * initialization, even before dynamic memory is available.
79 * 4) How to invoke a control method, including argument setup and how to
80 * access the return value.
82 *****************************************************************************/
84 /* Standard Clib headers */
89 /* Local Prototypes */
92 InitializeFullAcpi (void);
95 InstallHandlers (void);
101 /******************************************************************************
105 * PARAMETERS: argc, argv
109 * DESCRIPTION: Main routine. Shows the use of the various output macros, as
110 * well as the use of the debug layer/level globals.
112 *****************************************************************************/
114 int ACPI_SYSTEM_XFACE
119 ACPI_FUNCTION_NAME (Examples
-main
);
122 ACPI_DEBUG_INITIALIZE (); /* For debug version only */
123 InitializeFullAcpi ();
125 /* Enable debug output, example debug print */
127 AcpiDbgLayer
= ACPI_EXAMPLE
;
128 AcpiDbgLevel
= ACPI_LV_INIT
;
129 ACPI_DEBUG_PRINT ((ACPI_DB_INIT
, "Example Debug output\n"));
131 /* Example warning and error output */
133 ACPI_INFO ((AE_INFO
, "ACPICA example info message"));
134 ACPI_WARNING ((AE_INFO
, "ACPICA example warning message"));
135 ACPI_ERROR ((AE_INFO
, "ACPICA example error message"));
136 ACPI_EXCEPTION ((AE_INFO
, AE_AML_OPERAND_TYPE
, "Example exception message"));
143 /******************************************************************************
145 * Example ACPICA initialization code. This shows a full initialization with
146 * no early ACPI table access.
148 *****************************************************************************/
151 InitializeFullAcpi (void)
156 /* Initialize the ACPICA subsystem */
158 Status
= AcpiInitializeSubsystem ();
159 if (ACPI_FAILURE (Status
))
161 ACPI_EXCEPTION ((AE_INFO
, Status
, "While initializing ACPICA"));
165 /* Initialize the ACPICA Table Manager and get all ACPI tables */
167 Status
= AcpiInitializeTables (NULL
, 16, FALSE
);
168 if (ACPI_FAILURE (Status
))
170 ACPI_EXCEPTION ((AE_INFO
, Status
, "While initializing Table Manager"));
174 /* Create the ACPI namespace from ACPI tables */
176 Status
= AcpiLoadTables ();
177 if (ACPI_FAILURE (Status
))
179 ACPI_EXCEPTION ((AE_INFO
, Status
, "While loading ACPI tables"));
183 /* Install local handlers */
185 Status
= InstallHandlers ();
186 if (ACPI_FAILURE (Status
))
188 ACPI_EXCEPTION ((AE_INFO
, Status
, "While installing handlers"));
192 /* Initialize the ACPI hardware */
194 Status
= AcpiEnableSubsystem (ACPI_FULL_INITIALIZATION
);
195 if (ACPI_FAILURE (Status
))
197 ACPI_EXCEPTION ((AE_INFO
, Status
, "While enabling ACPICA"));
201 /* Complete the ACPI namespace object initialization */
203 Status
= AcpiInitializeObjects (ACPI_FULL_INITIALIZATION
);
204 if (ACPI_FAILURE (Status
))
206 ACPI_EXCEPTION ((AE_INFO
, Status
, "While initializing ACPICA objects"));
214 /******************************************************************************
216 * Example ACPICA initialization code with early ACPI table access. This shows
217 * an initialization that requires early access to ACPI tables (before
218 * kernel dynamic memory is available)
220 *****************************************************************************/
223 * The purpose of this static table array is to avoid the use of kernel
224 * dynamic memory which may not be available during early ACPI table
227 #define ACPI_MAX_INIT_TABLES 16
228 static ACPI_TABLE_DESC TableArray
[ACPI_MAX_INIT_TABLES
];
232 * This function would be called early in kernel initialization. After this
233 * is called, all ACPI tables are available to the host.
236 InitializeAcpiTables (void)
241 /* Initialize the ACPICA Table Manager and get all ACPI tables */
243 Status
= AcpiInitializeTables (TableArray
, ACPI_MAX_INIT_TABLES
, TRUE
);
249 * This function would be called after the kernel is initialized and
250 * dynamic/virtual memory is available. It completes the initialization of
251 * the ACPICA subsystem.
254 InitializeAcpi (void)
259 /* Initialize the ACPICA subsystem */
261 Status
= AcpiInitializeSubsystem ();
262 if (ACPI_FAILURE (Status
))
267 /* Copy the root table list to dynamic memory */
269 Status
= AcpiReallocateRootTable ();
270 if (ACPI_FAILURE (Status
))
275 /* Create the ACPI namespace from ACPI tables */
277 Status
= AcpiLoadTables ();
278 if (ACPI_FAILURE (Status
))
283 /* Install local handlers */
285 Status
= InstallHandlers ();
286 if (ACPI_FAILURE (Status
))
288 ACPI_EXCEPTION ((AE_INFO
, Status
, "While installing handlers"));
292 /* Initialize the ACPI hardware */
294 Status
= AcpiEnableSubsystem (ACPI_FULL_INITIALIZATION
);
295 if (ACPI_FAILURE (Status
))
300 /* Complete the ACPI namespace object initialization */
302 Status
= AcpiInitializeObjects (ACPI_FULL_INITIALIZATION
);
303 if (ACPI_FAILURE (Status
))
312 /******************************************************************************
314 * Example ACPICA handler and handler installation
316 *****************************************************************************/
325 ACPI_INFO ((AE_INFO
, "Received a notify 0x%X", Value
));
330 InstallHandlers (void)
335 /* Install global notify handler */
337 Status
= AcpiInstallNotifyHandler (ACPI_ROOT_OBJECT
, ACPI_SYSTEM_NOTIFY
,
338 NotifyHandler
, NULL
);
339 if (ACPI_FAILURE (Status
))
341 ACPI_EXCEPTION ((AE_INFO
, Status
, "While installing Notify handler"));
349 /******************************************************************************
351 * Example control method execution.
353 * _OSI is a predefined method that is implemented internally within ACPICA.
355 * Shows the following elements:
357 * 1) How to setup a control method argument and argument list
358 * 2) How to setup the return value object
359 * 3) How to invoke AcpiEvaluateObject
360 * 4) How to check the returned ACPI_STATUS
361 * 5) How to analyze the return value
363 *****************************************************************************/
369 ACPI_OBJECT_LIST ArgList
;
371 ACPI_BUFFER ReturnValue
;
375 ACPI_INFO ((AE_INFO
, "Executing OSI method"));
377 /* Setup input argument */
380 ArgList
.Pointer
= Arg
;
382 Arg
[0].Type
= ACPI_TYPE_STRING
;
383 Arg
[0].String
.Pointer
= "Windows 2001";
384 Arg
[0].String
.Length
= strlen (Arg
[0].String
.Pointer
);
386 /* Ask ACPICA to allocate space for the return object */
388 ReturnValue
.Length
= ACPI_ALLOCATE_BUFFER
;
390 Status
= AcpiEvaluateObject (NULL
, "\\_OSI", &ArgList
, &ReturnValue
);
391 if (ACPI_FAILURE (Status
))
393 ACPI_EXCEPTION ((AE_INFO
, Status
, "While executing _OSI"));
397 /* Ensure that the return object is large enough */
399 if (ReturnValue
.Length
< sizeof (ACPI_OBJECT
))
401 AcpiOsPrintf ("Return value from _OSI method too small, %.8X\n",
406 /* Expect an integer return value from execution of _OSI */
408 Object
= ReturnValue
.Pointer
;
409 if (Object
->Type
!= ACPI_TYPE_INTEGER
)
411 AcpiOsPrintf ("Invalid return type from _OSI, %.2X\n", Object
->Type
);
414 ACPI_INFO ((AE_INFO
, "_OSI returned 0x%8.8X", (UINT32
) Object
->Integer
.Value
));
420 /******************************************************************************
422 * OSL support (only needed to link to the windows OSL)
424 *****************************************************************************/
426 FILE *AcpiGbl_DebugFile
;
428 ACPI_PHYSICAL_ADDRESS
429 AeLocalGetRootPointer (
445 ACPI_EXECUTE_TYPE Type
,
446 ACPI_OSD_EXEC_CALLBACK Function
,