Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / events / evxface.c
blob9bce5248b1b19cb29fb7657328a3159282ef8bb6
1 /******************************************************************************
3 * Module Name: evxface - External interfaces for ACPI events
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 #define __EVXFACE_C__
46 #define EXPORT_ACPI_INTERFACES
48 #include "acpi.h"
49 #include "accommon.h"
50 #include "acnamesp.h"
51 #include "acevents.h"
52 #include "acinterp.h"
54 #define _COMPONENT ACPI_EVENTS
55 ACPI_MODULE_NAME ("evxface")
58 /*******************************************************************************
60 * FUNCTION: AcpiInstallNotifyHandler
62 * PARAMETERS: Device - The device for which notifies will be handled
63 * HandlerType - The type of handler:
64 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
65 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
66 * ACPI_ALL_NOTIFY: Both System and Device
67 * Handler - Address of the handler
68 * Context - Value passed to the handler on each GPE
70 * RETURN: Status
72 * DESCRIPTION: Install a handler for notifications on an ACPI Device,
73 * ThermalZone, or Processor object.
75 * NOTES: The Root namespace object may have only one handler for each
76 * type of notify (System/Device). Device/Thermal/Processor objects
77 * may have one device notify handler, and multiple system notify
78 * handlers.
80 ******************************************************************************/
82 ACPI_STATUS
83 AcpiInstallNotifyHandler (
84 ACPI_HANDLE Device,
85 UINT32 HandlerType,
86 ACPI_NOTIFY_HANDLER Handler,
87 void *Context)
89 ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
90 ACPI_OPERAND_OBJECT *ObjDesc;
91 ACPI_OPERAND_OBJECT *HandlerObj;
92 ACPI_STATUS Status;
93 UINT32 i;
96 ACPI_FUNCTION_TRACE (AcpiInstallNotifyHandler);
99 /* Parameter validation */
101 if ((!Device) || (!Handler) || (!HandlerType) ||
102 (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
104 return_ACPI_STATUS (AE_BAD_PARAMETER);
107 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
108 if (ACPI_FAILURE (Status))
110 return_ACPI_STATUS (Status);
114 * Root Object:
115 * Registering a notify handler on the root object indicates that the
116 * caller wishes to receive notifications for all objects. Note that
117 * only one global handler can be registered per notify type.
118 * Ensure that a handler is not already installed.
120 if (Device == ACPI_ROOT_OBJECT)
122 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
124 if (HandlerType & (i+1))
126 if (AcpiGbl_GlobalNotify[i].Handler)
128 Status = AE_ALREADY_EXISTS;
129 goto UnlockAndExit;
132 AcpiGbl_GlobalNotify[i].Handler = Handler;
133 AcpiGbl_GlobalNotify[i].Context = Context;
137 goto UnlockAndExit; /* Global notify handler installed, all done */
141 * All Other Objects:
142 * Caller will only receive notifications specific to the target
143 * object. Note that only certain object types are allowed to
144 * receive notifications.
147 /* Are Notifies allowed on this object? */
149 if (!AcpiEvIsNotifyObject (Node))
151 Status = AE_TYPE;
152 goto UnlockAndExit;
155 /* Check for an existing internal object, might not exist */
157 ObjDesc = AcpiNsGetAttachedObject (Node);
158 if (!ObjDesc)
160 /* Create a new object */
162 ObjDesc = AcpiUtCreateInternalObject (Node->Type);
163 if (!ObjDesc)
165 Status = AE_NO_MEMORY;
166 goto UnlockAndExit;
169 /* Attach new object to the Node, remove local reference */
171 Status = AcpiNsAttachObject (Device, ObjDesc, Node->Type);
172 AcpiUtRemoveReference (ObjDesc);
173 if (ACPI_FAILURE (Status))
175 goto UnlockAndExit;
179 /* Ensure that the handler is not already installed in the lists */
181 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
183 if (HandlerType & (i+1))
185 HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
186 while (HandlerObj)
188 if (HandlerObj->Notify.Handler == Handler)
190 Status = AE_ALREADY_EXISTS;
191 goto UnlockAndExit;
194 HandlerObj = HandlerObj->Notify.Next[i];
199 /* Create and populate a new notify handler object */
201 HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_NOTIFY);
202 if (!HandlerObj)
204 Status = AE_NO_MEMORY;
205 goto UnlockAndExit;
208 HandlerObj->Notify.Node = Node;
209 HandlerObj->Notify.HandlerType = HandlerType;
210 HandlerObj->Notify.Handler = Handler;
211 HandlerObj->Notify.Context = Context;
213 /* Install the handler at the list head(s) */
215 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
217 if (HandlerType & (i+1))
219 HandlerObj->Notify.Next[i] =
220 ObjDesc->CommonNotify.NotifyList[i];
222 ObjDesc->CommonNotify.NotifyList[i] = HandlerObj;
226 /* Add an extra reference if handler was installed in both lists */
228 if (HandlerType == ACPI_ALL_NOTIFY)
230 AcpiUtAddReference (HandlerObj);
234 UnlockAndExit:
235 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
236 return_ACPI_STATUS (Status);
239 ACPI_EXPORT_SYMBOL (AcpiInstallNotifyHandler)
242 /*******************************************************************************
244 * FUNCTION: AcpiRemoveNotifyHandler
246 * PARAMETERS: Device - The device for which the handler is installed
247 * HandlerType - The type of handler:
248 * ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
249 * ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
250 * ACPI_ALL_NOTIFY: Both System and Device
251 * Handler - Address of the handler
253 * RETURN: Status
255 * DESCRIPTION: Remove a handler for notifies on an ACPI device
257 ******************************************************************************/
259 ACPI_STATUS
260 AcpiRemoveNotifyHandler (
261 ACPI_HANDLE Device,
262 UINT32 HandlerType,
263 ACPI_NOTIFY_HANDLER Handler)
265 ACPI_NAMESPACE_NODE *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
266 ACPI_OPERAND_OBJECT *ObjDesc;
267 ACPI_OPERAND_OBJECT *HandlerObj;
268 ACPI_OPERAND_OBJECT *PreviousHandlerObj;
269 ACPI_STATUS Status;
270 UINT32 i;
273 ACPI_FUNCTION_TRACE (AcpiRemoveNotifyHandler);
276 /* Parameter validation */
278 if ((!Device) || (!Handler) || (!HandlerType) ||
279 (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
281 return_ACPI_STATUS (AE_BAD_PARAMETER);
284 /* Make sure all deferred notify tasks are completed */
286 AcpiOsWaitEventsComplete ();
288 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
289 if (ACPI_FAILURE (Status))
291 return_ACPI_STATUS (Status);
294 /* Root Object. Global handlers are removed here */
296 if (Device == ACPI_ROOT_OBJECT)
298 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
300 if (HandlerType & (i+1))
302 if (!AcpiGbl_GlobalNotify[i].Handler ||
303 (AcpiGbl_GlobalNotify[i].Handler != Handler))
305 Status = AE_NOT_EXIST;
306 goto UnlockAndExit;
309 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
310 "Removing global notify handler\n"));
312 AcpiGbl_GlobalNotify[i].Handler = NULL;
313 AcpiGbl_GlobalNotify[i].Context = NULL;
317 goto UnlockAndExit;
320 /* All other objects: Are Notifies allowed on this object? */
322 if (!AcpiEvIsNotifyObject (Node))
324 Status = AE_TYPE;
325 goto UnlockAndExit;
328 /* Must have an existing internal object */
330 ObjDesc = AcpiNsGetAttachedObject (Node);
331 if (!ObjDesc)
333 Status = AE_NOT_EXIST;
334 goto UnlockAndExit;
337 /* Internal object exists. Find the handler and remove it */
339 for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
341 if (HandlerType & (i+1))
343 HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
344 PreviousHandlerObj = NULL;
346 /* Attempt to find the handler in the handler list */
348 while (HandlerObj &&
349 (HandlerObj->Notify.Handler != Handler))
351 PreviousHandlerObj = HandlerObj;
352 HandlerObj = HandlerObj->Notify.Next[i];
355 if (!HandlerObj)
357 Status = AE_NOT_EXIST;
358 goto UnlockAndExit;
361 /* Remove the handler object from the list */
363 if (PreviousHandlerObj) /* Handler is not at the list head */
365 PreviousHandlerObj->Notify.Next[i] =
366 HandlerObj->Notify.Next[i];
368 else /* Handler is at the list head */
370 ObjDesc->CommonNotify.NotifyList[i] =
371 HandlerObj->Notify.Next[i];
374 AcpiUtRemoveReference (HandlerObj);
379 UnlockAndExit:
380 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
381 return_ACPI_STATUS (Status);
384 ACPI_EXPORT_SYMBOL (AcpiRemoveNotifyHandler)
387 /*******************************************************************************
389 * FUNCTION: AcpiInstallExceptionHandler
391 * PARAMETERS: Handler - Pointer to the handler function for the
392 * event
394 * RETURN: Status
396 * DESCRIPTION: Saves the pointer to the handler function
398 ******************************************************************************/
400 ACPI_STATUS
401 AcpiInstallExceptionHandler (
402 ACPI_EXCEPTION_HANDLER Handler)
404 ACPI_STATUS Status;
407 ACPI_FUNCTION_TRACE (AcpiInstallExceptionHandler);
410 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
411 if (ACPI_FAILURE (Status))
413 return_ACPI_STATUS (Status);
416 /* Don't allow two handlers. */
418 if (AcpiGbl_ExceptionHandler)
420 Status = AE_ALREADY_EXISTS;
421 goto Cleanup;
424 /* Install the handler */
426 AcpiGbl_ExceptionHandler = Handler;
428 Cleanup:
429 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
430 return_ACPI_STATUS (Status);
433 ACPI_EXPORT_SYMBOL (AcpiInstallExceptionHandler)
436 #if (!ACPI_REDUCED_HARDWARE)
437 /*******************************************************************************
439 * FUNCTION: AcpiInstallSciHandler
441 * PARAMETERS: Address - Address of the handler
442 * Context - Value passed to the handler on each SCI
444 * RETURN: Status
446 * DESCRIPTION: Install a handler for a System Control Interrupt.
448 ******************************************************************************/
450 ACPI_STATUS
451 AcpiInstallSciHandler (
452 ACPI_SCI_HANDLER Address,
453 void *Context)
455 ACPI_SCI_HANDLER_INFO *NewSciHandler;
456 ACPI_SCI_HANDLER_INFO *SciHandler;
457 ACPI_CPU_FLAGS Flags;
458 ACPI_STATUS Status;
461 ACPI_FUNCTION_TRACE (AcpiInstallSciHandler);
464 if (!Address)
466 return_ACPI_STATUS (AE_BAD_PARAMETER);
469 /* Allocate and init a handler object */
471 NewSciHandler = ACPI_ALLOCATE (sizeof (ACPI_SCI_HANDLER_INFO));
472 if (!NewSciHandler)
474 return_ACPI_STATUS (AE_NO_MEMORY);
477 NewSciHandler->Address = Address;
478 NewSciHandler->Context = Context;
480 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
481 if (ACPI_FAILURE (Status))
483 goto Exit;
486 /* Lock list during installation */
488 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
489 SciHandler = AcpiGbl_SciHandlerList;
491 /* Ensure handler does not already exist */
493 while (SciHandler)
495 if (Address == SciHandler->Address)
497 Status = AE_ALREADY_EXISTS;
498 goto UnlockAndExit;
501 SciHandler = SciHandler->Next;
504 /* Install the new handler into the global list (at head) */
506 NewSciHandler->Next = AcpiGbl_SciHandlerList;
507 AcpiGbl_SciHandlerList = NewSciHandler;
510 UnlockAndExit:
512 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
513 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
515 Exit:
516 if (ACPI_FAILURE (Status))
518 ACPI_FREE (NewSciHandler);
520 return_ACPI_STATUS (Status);
524 /*******************************************************************************
526 * FUNCTION: AcpiRemoveSciHandler
528 * PARAMETERS: Address - Address of the handler
530 * RETURN: Status
532 * DESCRIPTION: Remove a handler for a System Control Interrupt.
534 ******************************************************************************/
536 ACPI_STATUS
537 AcpiRemoveSciHandler (
538 ACPI_SCI_HANDLER Address)
540 ACPI_SCI_HANDLER_INFO *PrevSciHandler;
541 ACPI_SCI_HANDLER_INFO *NextSciHandler;
542 ACPI_CPU_FLAGS Flags;
543 ACPI_STATUS Status;
546 ACPI_FUNCTION_TRACE (AcpiRemoveSciHandler);
549 if (!Address)
551 return_ACPI_STATUS (AE_BAD_PARAMETER);
554 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
555 if (ACPI_FAILURE (Status))
557 return_ACPI_STATUS (Status);
560 /* Remove the SCI handler with lock */
562 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
564 PrevSciHandler = NULL;
565 NextSciHandler = AcpiGbl_SciHandlerList;
566 while (NextSciHandler)
568 if (NextSciHandler->Address == Address)
570 /* Unlink and free the SCI handler info block */
572 if (PrevSciHandler)
574 PrevSciHandler->Next = NextSciHandler->Next;
576 else
578 AcpiGbl_SciHandlerList = NextSciHandler->Next;
581 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
582 ACPI_FREE (NextSciHandler);
583 goto UnlockAndExit;
586 PrevSciHandler = NextSciHandler;
587 NextSciHandler = NextSciHandler->Next;
590 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
591 Status = AE_NOT_EXIST;
594 UnlockAndExit:
595 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
596 return_ACPI_STATUS (Status);
600 /*******************************************************************************
602 * FUNCTION: AcpiInstallGlobalEventHandler
604 * PARAMETERS: Handler - Pointer to the global event handler function
605 * Context - Value passed to the handler on each event
607 * RETURN: Status
609 * DESCRIPTION: Saves the pointer to the handler function. The global handler
610 * is invoked upon each incoming GPE and Fixed Event. It is
611 * invoked at interrupt level at the time of the event dispatch.
612 * Can be used to update event counters, etc.
614 ******************************************************************************/
616 ACPI_STATUS
617 AcpiInstallGlobalEventHandler (
618 ACPI_GBL_EVENT_HANDLER Handler,
619 void *Context)
621 ACPI_STATUS Status;
624 ACPI_FUNCTION_TRACE (AcpiInstallGlobalEventHandler);
627 /* Parameter validation */
629 if (!Handler)
631 return_ACPI_STATUS (AE_BAD_PARAMETER);
634 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
635 if (ACPI_FAILURE (Status))
637 return_ACPI_STATUS (Status);
640 /* Don't allow two handlers. */
642 if (AcpiGbl_GlobalEventHandler)
644 Status = AE_ALREADY_EXISTS;
645 goto Cleanup;
648 AcpiGbl_GlobalEventHandler = Handler;
649 AcpiGbl_GlobalEventHandlerContext = Context;
652 Cleanup:
653 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
654 return_ACPI_STATUS (Status);
657 ACPI_EXPORT_SYMBOL (AcpiInstallGlobalEventHandler)
660 /*******************************************************************************
662 * FUNCTION: AcpiInstallFixedEventHandler
664 * PARAMETERS: Event - Event type to enable.
665 * Handler - Pointer to the handler function for the
666 * event
667 * Context - Value passed to the handler on each GPE
669 * RETURN: Status
671 * DESCRIPTION: Saves the pointer to the handler function and then enables the
672 * event.
674 ******************************************************************************/
676 ACPI_STATUS
677 AcpiInstallFixedEventHandler (
678 UINT32 Event,
679 ACPI_EVENT_HANDLER Handler,
680 void *Context)
682 ACPI_STATUS Status;
685 ACPI_FUNCTION_TRACE (AcpiInstallFixedEventHandler);
688 /* Parameter validation */
690 if (Event > ACPI_EVENT_MAX)
692 return_ACPI_STATUS (AE_BAD_PARAMETER);
695 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
696 if (ACPI_FAILURE (Status))
698 return_ACPI_STATUS (Status);
701 /* Do not allow multiple handlers */
703 if (AcpiGbl_FixedEventHandlers[Event].Handler)
705 Status = AE_ALREADY_EXISTS;
706 goto Cleanup;
709 /* Install the handler before enabling the event */
711 AcpiGbl_FixedEventHandlers[Event].Handler = Handler;
712 AcpiGbl_FixedEventHandlers[Event].Context = Context;
714 Status = AcpiEnableEvent (Event, 0);
715 if (ACPI_FAILURE (Status))
717 ACPI_WARNING ((AE_INFO,
718 "Could not enable fixed event - %s (%u)",
719 AcpiUtGetEventName (Event), Event));
721 /* Remove the handler */
723 AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
724 AcpiGbl_FixedEventHandlers[Event].Context = NULL;
726 else
728 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
729 "Enabled fixed event %s (%X), Handler=%p\n",
730 AcpiUtGetEventName (Event), Event, Handler));
734 Cleanup:
735 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
736 return_ACPI_STATUS (Status);
739 ACPI_EXPORT_SYMBOL (AcpiInstallFixedEventHandler)
742 /*******************************************************************************
744 * FUNCTION: AcpiRemoveFixedEventHandler
746 * PARAMETERS: Event - Event type to disable.
747 * Handler - Address of the handler
749 * RETURN: Status
751 * DESCRIPTION: Disables the event and unregisters the event handler.
753 ******************************************************************************/
755 ACPI_STATUS
756 AcpiRemoveFixedEventHandler (
757 UINT32 Event,
758 ACPI_EVENT_HANDLER Handler)
760 ACPI_STATUS Status = AE_OK;
763 ACPI_FUNCTION_TRACE (AcpiRemoveFixedEventHandler);
766 /* Parameter validation */
768 if (Event > ACPI_EVENT_MAX)
770 return_ACPI_STATUS (AE_BAD_PARAMETER);
773 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
774 if (ACPI_FAILURE (Status))
776 return_ACPI_STATUS (Status);
779 /* Disable the event before removing the handler */
781 Status = AcpiDisableEvent (Event, 0);
783 /* Always Remove the handler */
785 AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
786 AcpiGbl_FixedEventHandlers[Event].Context = NULL;
788 if (ACPI_FAILURE (Status))
790 ACPI_WARNING ((AE_INFO,
791 "Could not disable fixed event - %s (%u)",
792 AcpiUtGetEventName (Event), Event));
794 else
796 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
797 "Disabled fixed event - %s (%X)\n",
798 AcpiUtGetEventName (Event), Event));
801 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
802 return_ACPI_STATUS (Status);
805 ACPI_EXPORT_SYMBOL (AcpiRemoveFixedEventHandler)
808 /*******************************************************************************
810 * FUNCTION: AcpiInstallGpeHandler
812 * PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT
813 * defined GPEs)
814 * GpeNumber - The GPE number within the GPE block
815 * Type - Whether this GPE should be treated as an
816 * edge- or level-triggered interrupt.
817 * Address - Address of the handler
818 * Context - Value passed to the handler on each GPE
820 * RETURN: Status
822 * DESCRIPTION: Install a handler for a General Purpose Event.
824 ******************************************************************************/
826 ACPI_STATUS
827 AcpiInstallGpeHandler (
828 ACPI_HANDLE GpeDevice,
829 UINT32 GpeNumber,
830 UINT32 Type,
831 ACPI_GPE_HANDLER Address,
832 void *Context)
834 ACPI_GPE_EVENT_INFO *GpeEventInfo;
835 ACPI_GPE_HANDLER_INFO *Handler;
836 ACPI_STATUS Status;
837 ACPI_CPU_FLAGS Flags;
840 ACPI_FUNCTION_TRACE (AcpiInstallGpeHandler);
843 /* Parameter validation */
845 if ((!Address) || (Type & ~ACPI_GPE_XRUPT_TYPE_MASK))
847 return_ACPI_STATUS (AE_BAD_PARAMETER);
850 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
851 if (ACPI_FAILURE (Status))
853 return_ACPI_STATUS (Status);
856 /* Allocate and init handler object (before lock) */
858 Handler = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_HANDLER_INFO));
859 if (!Handler)
861 Status = AE_NO_MEMORY;
862 goto UnlockAndExit;
865 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
867 /* Ensure that we have a valid GPE number */
869 GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
870 if (!GpeEventInfo)
872 Status = AE_BAD_PARAMETER;
873 goto FreeAndExit;
876 /* Make sure that there isn't a handler there already */
878 if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) ==
879 ACPI_GPE_DISPATCH_HANDLER)
881 Status = AE_ALREADY_EXISTS;
882 goto FreeAndExit;
885 Handler->Address = Address;
886 Handler->Context = Context;
887 Handler->MethodNode = GpeEventInfo->Dispatch.MethodNode;
888 Handler->OriginalFlags = (UINT8) (GpeEventInfo->Flags &
889 (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK));
892 * If the GPE is associated with a method, it may have been enabled
893 * automatically during initialization, in which case it has to be
894 * disabled now to avoid spurious execution of the handler.
896 if (((Handler->OriginalFlags & ACPI_GPE_DISPATCH_METHOD) ||
897 (Handler->OriginalFlags & ACPI_GPE_DISPATCH_NOTIFY)) &&
898 GpeEventInfo->RuntimeCount)
900 Handler->OriginallyEnabled = TRUE;
901 (void) AcpiEvRemoveGpeReference (GpeEventInfo);
903 /* Sanity check of original type against new type */
905 if (Type != (UINT32) (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))
907 ACPI_WARNING ((AE_INFO, "GPE type mismatch (level/edge)"));
911 /* Install the handler */
913 GpeEventInfo->Dispatch.Handler = Handler;
915 /* Setup up dispatch flags to indicate handler (vs. method/notify) */
917 GpeEventInfo->Flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
918 GpeEventInfo->Flags |= (UINT8) (Type | ACPI_GPE_DISPATCH_HANDLER);
920 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
923 UnlockAndExit:
924 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
925 return_ACPI_STATUS (Status);
927 FreeAndExit:
928 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
929 ACPI_FREE (Handler);
930 goto UnlockAndExit;
933 ACPI_EXPORT_SYMBOL (AcpiInstallGpeHandler)
936 /*******************************************************************************
938 * FUNCTION: AcpiRemoveGpeHandler
940 * PARAMETERS: GpeDevice - Namespace node for the GPE (NULL for FADT
941 * defined GPEs)
942 * GpeNumber - The event to remove a handler
943 * Address - Address of the handler
945 * RETURN: Status
947 * DESCRIPTION: Remove a handler for a General Purpose AcpiEvent.
949 ******************************************************************************/
951 ACPI_STATUS
952 AcpiRemoveGpeHandler (
953 ACPI_HANDLE GpeDevice,
954 UINT32 GpeNumber,
955 ACPI_GPE_HANDLER Address)
957 ACPI_GPE_EVENT_INFO *GpeEventInfo;
958 ACPI_GPE_HANDLER_INFO *Handler;
959 ACPI_STATUS Status;
960 ACPI_CPU_FLAGS Flags;
963 ACPI_FUNCTION_TRACE (AcpiRemoveGpeHandler);
966 /* Parameter validation */
968 if (!Address)
970 return_ACPI_STATUS (AE_BAD_PARAMETER);
973 /* Make sure all deferred GPE tasks are completed */
975 AcpiOsWaitEventsComplete ();
977 Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
978 if (ACPI_FAILURE (Status))
980 return_ACPI_STATUS (Status);
983 Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
985 /* Ensure that we have a valid GPE number */
987 GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
988 if (!GpeEventInfo)
990 Status = AE_BAD_PARAMETER;
991 goto UnlockAndExit;
994 /* Make sure that a handler is indeed installed */
996 if ((GpeEventInfo->Flags & ACPI_GPE_DISPATCH_MASK) !=
997 ACPI_GPE_DISPATCH_HANDLER)
999 Status = AE_NOT_EXIST;
1000 goto UnlockAndExit;
1003 /* Make sure that the installed handler is the same */
1005 if (GpeEventInfo->Dispatch.Handler->Address != Address)
1007 Status = AE_BAD_PARAMETER;
1008 goto UnlockAndExit;
1011 /* Remove the handler */
1013 Handler = GpeEventInfo->Dispatch.Handler;
1015 /* Restore Method node (if any), set dispatch flags */
1017 GpeEventInfo->Dispatch.MethodNode = Handler->MethodNode;
1018 GpeEventInfo->Flags &=
1019 ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
1020 GpeEventInfo->Flags |= Handler->OriginalFlags;
1023 * If the GPE was previously associated with a method and it was
1024 * enabled, it should be enabled at this point to restore the
1025 * post-initialization configuration.
1027 if ((Handler->OriginalFlags & ACPI_GPE_DISPATCH_METHOD) &&
1028 Handler->OriginallyEnabled)
1030 (void) AcpiEvAddGpeReference (GpeEventInfo);
1033 /* Now we can free the handler object */
1035 ACPI_FREE (Handler);
1038 UnlockAndExit:
1039 AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
1040 (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
1041 return_ACPI_STATUS (Status);
1044 ACPI_EXPORT_SYMBOL (AcpiRemoveGpeHandler)
1047 /*******************************************************************************
1049 * FUNCTION: AcpiAcquireGlobalLock
1051 * PARAMETERS: Timeout - How long the caller is willing to wait
1052 * Handle - Where the handle to the lock is returned
1053 * (if acquired)
1055 * RETURN: Status
1057 * DESCRIPTION: Acquire the ACPI Global Lock
1059 * Note: Allows callers with the same thread ID to acquire the global lock
1060 * multiple times. In other words, externally, the behavior of the global lock
1061 * is identical to an AML mutex. On the first acquire, a new handle is
1062 * returned. On any subsequent calls to acquire by the same thread, the same
1063 * handle is returned.
1065 ******************************************************************************/
1067 ACPI_STATUS
1068 AcpiAcquireGlobalLock (
1069 UINT16 Timeout,
1070 UINT32 *Handle)
1072 ACPI_STATUS Status;
1075 if (!Handle)
1077 return (AE_BAD_PARAMETER);
1080 /* Must lock interpreter to prevent race conditions */
1082 AcpiExEnterInterpreter ();
1084 Status = AcpiExAcquireMutexObject (Timeout,
1085 AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
1087 if (ACPI_SUCCESS (Status))
1089 /* Return the global lock handle (updated in AcpiEvAcquireGlobalLock) */
1091 *Handle = AcpiGbl_GlobalLockHandle;
1094 AcpiExExitInterpreter ();
1095 return (Status);
1098 ACPI_EXPORT_SYMBOL (AcpiAcquireGlobalLock)
1101 /*******************************************************************************
1103 * FUNCTION: AcpiReleaseGlobalLock
1105 * PARAMETERS: Handle - Returned from AcpiAcquireGlobalLock
1107 * RETURN: Status
1109 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
1111 ******************************************************************************/
1113 ACPI_STATUS
1114 AcpiReleaseGlobalLock (
1115 UINT32 Handle)
1117 ACPI_STATUS Status;
1120 if (!Handle || (Handle != AcpiGbl_GlobalLockHandle))
1122 return (AE_NOT_ACQUIRED);
1125 Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
1126 return (Status);
1129 ACPI_EXPORT_SYMBOL (AcpiReleaseGlobalLock)
1131 #endif /* !ACPI_REDUCED_HARDWARE */