Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / tools / examples / examples.c
blobd90715d844920aaf34dc81fa05dc8c79148406af
1 /******************************************************************************
3 * Module Name: examples - Example ACPICA code
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
45 /* Set the ACPICA application type for use in include/platform/acenv.h */
47 #ifndef WIN32
48 #define WIN32
49 #endif
51 #define ACPI_DEBUG_OUTPUT
53 /* ACPICA public headers */
55 #include "acpi.h"
57 #define _COMPONENT ACPI_EXAMPLE
58 ACPI_MODULE_NAME ("examples")
61 /******************************************************************************
63 * ACPICA Example Code
65 * This module contains examples of how the host OS should interface to the
66 * ACPICA subsystem.
68 * 1) How to use the platform/acenv.h file and how to set configuration
69 * options.
71 * 2) main - using the debug output mechanism and the error/warning output
72 * macros.
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 */
86 #include <stdio.h>
87 #include <string.h>
89 /* Local Prototypes */
91 ACPI_STATUS
92 InitializeFullAcpi (void);
94 ACPI_STATUS
95 InstallHandlers (void);
97 void
98 ExecuteOSI (void);
101 /******************************************************************************
103 * FUNCTION: main
105 * PARAMETERS: argc, argv
107 * RETURN: Status
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
115 main (
116 int argc,
117 char **argv)
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"));
138 ExecuteOSI ();
139 return (0);
143 /******************************************************************************
145 * Example ACPICA initialization code. This shows a full initialization with
146 * no early ACPI table access.
148 *****************************************************************************/
150 ACPI_STATUS
151 InitializeFullAcpi (void)
153 ACPI_STATUS Status;
156 /* Initialize the ACPICA subsystem */
158 Status = AcpiInitializeSubsystem ();
159 if (ACPI_FAILURE (Status))
161 ACPI_EXCEPTION ((AE_INFO, Status, "While initializing ACPICA"));
162 return (Status);
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"));
171 return (Status);
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"));
180 return (Status);
183 /* Install local handlers */
185 Status = InstallHandlers ();
186 if (ACPI_FAILURE (Status))
188 ACPI_EXCEPTION ((AE_INFO, Status, "While installing handlers"));
189 return (Status);
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"));
198 return (Status);
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"));
207 return (Status);
210 return (AE_OK);
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
225 * access.
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.
235 ACPI_STATUS
236 InitializeAcpiTables (void)
238 ACPI_STATUS Status;
241 /* Initialize the ACPICA Table Manager and get all ACPI tables */
243 Status = AcpiInitializeTables (TableArray, ACPI_MAX_INIT_TABLES, TRUE);
244 return (Status);
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.
253 ACPI_STATUS
254 InitializeAcpi (void)
256 ACPI_STATUS Status;
259 /* Initialize the ACPICA subsystem */
261 Status = AcpiInitializeSubsystem ();
262 if (ACPI_FAILURE (Status))
264 return (Status);
267 /* Copy the root table list to dynamic memory */
269 Status = AcpiReallocateRootTable ();
270 if (ACPI_FAILURE (Status))
272 return (Status);
275 /* Create the ACPI namespace from ACPI tables */
277 Status = AcpiLoadTables ();
278 if (ACPI_FAILURE (Status))
280 return (Status);
283 /* Install local handlers */
285 Status = InstallHandlers ();
286 if (ACPI_FAILURE (Status))
288 ACPI_EXCEPTION ((AE_INFO, Status, "While installing handlers"));
289 return (Status);
292 /* Initialize the ACPI hardware */
294 Status = AcpiEnableSubsystem (ACPI_FULL_INITIALIZATION);
295 if (ACPI_FAILURE (Status))
297 return (Status);
300 /* Complete the ACPI namespace object initialization */
302 Status = AcpiInitializeObjects (ACPI_FULL_INITIALIZATION);
303 if (ACPI_FAILURE (Status))
305 return (Status);
308 return (AE_OK);
312 /******************************************************************************
314 * Example ACPICA handler and handler installation
316 *****************************************************************************/
318 void
319 NotifyHandler (
320 ACPI_HANDLE Device,
321 UINT32 Value,
322 void *Context)
325 ACPI_INFO ((AE_INFO, "Received a notify 0x%X", Value));
329 ACPI_STATUS
330 InstallHandlers (void)
332 ACPI_STATUS Status;
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"));
342 return (Status);
345 return (AE_OK);
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 *****************************************************************************/
365 void
366 ExecuteOSI (void)
368 ACPI_STATUS Status;
369 ACPI_OBJECT_LIST ArgList;
370 ACPI_OBJECT Arg[1];
371 ACPI_BUFFER ReturnValue;
372 ACPI_OBJECT *Object;
375 ACPI_INFO ((AE_INFO, "Executing OSI method"));
377 /* Setup input argument */
379 ArgList.Count = 1;
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"));
394 return;
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",
402 ReturnValue.Length);
403 return;
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));
415 AcpiOsFree (Object);
416 return;
420 /******************************************************************************
422 * OSL support (only needed to link to the windows OSL)
424 *****************************************************************************/
426 FILE *AcpiGbl_DebugFile;
428 ACPI_PHYSICAL_ADDRESS
429 AeLocalGetRootPointer (
430 void)
433 return (0);
436 ACPI_THREAD_ID
437 AcpiOsGetThreadId (
438 void)
440 return (0xFFFF);
443 ACPI_STATUS
444 AcpiOsExecute (
445 ACPI_EXECUTE_TYPE Type,
446 ACPI_OSD_EXEC_CALLBACK Function,
447 void *Context)
449 return (AE_SUPPORT);