Home | History | Annotate | Line # | Download | only in events
evxface.c revision 1.8
      1  1.1    jruoho /******************************************************************************
      2  1.1    jruoho  *
      3  1.1    jruoho  * Module Name: evxface - External interfaces for ACPI events
      4  1.1    jruoho  *
      5  1.1    jruoho  *****************************************************************************/
      6  1.1    jruoho 
      7  1.4    jruoho /*
      8  1.8  christos  * Copyright (C) 2000 - 2016, Intel Corp.
      9  1.1    jruoho  * All rights reserved.
     10  1.1    jruoho  *
     11  1.4    jruoho  * Redistribution and use in source and binary forms, with or without
     12  1.4    jruoho  * modification, are permitted provided that the following conditions
     13  1.4    jruoho  * are met:
     14  1.4    jruoho  * 1. Redistributions of source code must retain the above copyright
     15  1.4    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16  1.4    jruoho  *    without modification.
     17  1.4    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.4    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.4    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.4    jruoho  *    including a substantially similar Disclaimer requirement for further
     21  1.4    jruoho  *    binary redistribution.
     22  1.4    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.4    jruoho  *    of any contributors may be used to endorse or promote products derived
     24  1.4    jruoho  *    from this software without specific prior written permission.
     25  1.4    jruoho  *
     26  1.4    jruoho  * Alternatively, this software may be distributed under the terms of the
     27  1.4    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.4    jruoho  * Software Foundation.
     29  1.4    jruoho  *
     30  1.4    jruoho  * NO WARRANTY
     31  1.4    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.4    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.4    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.4    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.4    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.4    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.4    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.4    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.4    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.4    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.4    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42  1.4    jruoho  */
     43  1.1    jruoho 
     44  1.5  christos #define EXPORT_ACPI_INTERFACES
     45  1.1    jruoho 
     46  1.1    jruoho #include "acpi.h"
     47  1.1    jruoho #include "accommon.h"
     48  1.1    jruoho #include "acnamesp.h"
     49  1.1    jruoho #include "acevents.h"
     50  1.1    jruoho #include "acinterp.h"
     51  1.1    jruoho 
     52  1.1    jruoho #define _COMPONENT          ACPI_EVENTS
     53  1.1    jruoho         ACPI_MODULE_NAME    ("evxface")
     54  1.1    jruoho 
     55  1.7  christos #if (!ACPI_REDUCED_HARDWARE)
     56  1.7  christos 
     57  1.7  christos /* Local prototypes */
     58  1.7  christos 
     59  1.7  christos static ACPI_STATUS
     60  1.7  christos AcpiEvInstallGpeHandler (
     61  1.7  christos     ACPI_HANDLE             GpeDevice,
     62  1.7  christos     UINT32                  GpeNumber,
     63  1.7  christos     UINT32                  Type,
     64  1.7  christos     BOOLEAN                 IsRawHandler,
     65  1.7  christos     ACPI_GPE_HANDLER        Address,
     66  1.7  christos     void                    *Context);
     67  1.7  christos 
     68  1.7  christos #endif
     69  1.7  christos 
     70  1.1    jruoho 
     71  1.1    jruoho /*******************************************************************************
     72  1.1    jruoho  *
     73  1.5  christos  * FUNCTION:    AcpiInstallNotifyHandler
     74  1.5  christos  *
     75  1.5  christos  * PARAMETERS:  Device          - The device for which notifies will be handled
     76  1.5  christos  *              HandlerType     - The type of handler:
     77  1.5  christos  *                                  ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
     78  1.5  christos  *                                  ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
     79  1.5  christos  *                                  ACPI_ALL_NOTIFY:    Both System and Device
     80  1.5  christos  *              Handler         - Address of the handler
     81  1.5  christos  *              Context         - Value passed to the handler on each GPE
     82  1.5  christos  *
     83  1.5  christos  * RETURN:      Status
     84  1.5  christos  *
     85  1.5  christos  * DESCRIPTION: Install a handler for notifications on an ACPI Device,
     86  1.5  christos  *              ThermalZone, or Processor object.
     87  1.5  christos  *
     88  1.5  christos  * NOTES:       The Root namespace object may have only one handler for each
     89  1.5  christos  *              type of notify (System/Device). Device/Thermal/Processor objects
     90  1.5  christos  *              may have one device notify handler, and multiple system notify
     91  1.5  christos  *              handlers.
     92  1.5  christos  *
     93  1.5  christos  ******************************************************************************/
     94  1.5  christos 
     95  1.5  christos ACPI_STATUS
     96  1.5  christos AcpiInstallNotifyHandler (
     97  1.5  christos     ACPI_HANDLE             Device,
     98  1.5  christos     UINT32                  HandlerType,
     99  1.5  christos     ACPI_NOTIFY_HANDLER     Handler,
    100  1.5  christos     void                    *Context)
    101  1.5  christos {
    102  1.5  christos     ACPI_NAMESPACE_NODE     *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
    103  1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc;
    104  1.5  christos     ACPI_OPERAND_OBJECT     *HandlerObj;
    105  1.5  christos     ACPI_STATUS             Status;
    106  1.5  christos     UINT32                  i;
    107  1.5  christos 
    108  1.5  christos 
    109  1.5  christos     ACPI_FUNCTION_TRACE (AcpiInstallNotifyHandler);
    110  1.5  christos 
    111  1.5  christos 
    112  1.5  christos     /* Parameter validation */
    113  1.5  christos 
    114  1.5  christos     if ((!Device) || (!Handler) || (!HandlerType) ||
    115  1.5  christos         (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
    116  1.5  christos     {
    117  1.5  christos         return_ACPI_STATUS (AE_BAD_PARAMETER);
    118  1.5  christos     }
    119  1.5  christos 
    120  1.5  christos     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
    121  1.5  christos     if (ACPI_FAILURE (Status))
    122  1.5  christos     {
    123  1.5  christos         return_ACPI_STATUS (Status);
    124  1.5  christos     }
    125  1.5  christos 
    126  1.5  christos     /*
    127  1.5  christos      * Root Object:
    128  1.5  christos      * Registering a notify handler on the root object indicates that the
    129  1.5  christos      * caller wishes to receive notifications for all objects. Note that
    130  1.5  christos      * only one global handler can be registered per notify type.
    131  1.5  christos      * Ensure that a handler is not already installed.
    132  1.5  christos      */
    133  1.5  christos     if (Device == ACPI_ROOT_OBJECT)
    134  1.5  christos     {
    135  1.5  christos         for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    136  1.5  christos         {
    137  1.5  christos             if (HandlerType & (i+1))
    138  1.5  christos             {
    139  1.5  christos                 if (AcpiGbl_GlobalNotify[i].Handler)
    140  1.5  christos                 {
    141  1.5  christos                     Status = AE_ALREADY_EXISTS;
    142  1.5  christos                     goto UnlockAndExit;
    143  1.5  christos                 }
    144  1.5  christos 
    145  1.5  christos                 AcpiGbl_GlobalNotify[i].Handler = Handler;
    146  1.5  christos                 AcpiGbl_GlobalNotify[i].Context = Context;
    147  1.5  christos             }
    148  1.5  christos         }
    149  1.5  christos 
    150  1.5  christos         goto UnlockAndExit; /* Global notify handler installed, all done */
    151  1.5  christos     }
    152  1.5  christos 
    153  1.5  christos     /*
    154  1.5  christos      * All Other Objects:
    155  1.5  christos      * Caller will only receive notifications specific to the target
    156  1.5  christos      * object. Note that only certain object types are allowed to
    157  1.5  christos      * receive notifications.
    158  1.5  christos      */
    159  1.5  christos 
    160  1.5  christos     /* Are Notifies allowed on this object? */
    161  1.5  christos 
    162  1.5  christos     if (!AcpiEvIsNotifyObject (Node))
    163  1.5  christos     {
    164  1.5  christos         Status = AE_TYPE;
    165  1.5  christos         goto UnlockAndExit;
    166  1.5  christos     }
    167  1.5  christos 
    168  1.5  christos     /* Check for an existing internal object, might not exist */
    169  1.5  christos 
    170  1.5  christos     ObjDesc = AcpiNsGetAttachedObject (Node);
    171  1.5  christos     if (!ObjDesc)
    172  1.5  christos     {
    173  1.5  christos         /* Create a new object */
    174  1.5  christos 
    175  1.5  christos         ObjDesc = AcpiUtCreateInternalObject (Node->Type);
    176  1.5  christos         if (!ObjDesc)
    177  1.5  christos         {
    178  1.5  christos             Status = AE_NO_MEMORY;
    179  1.5  christos             goto UnlockAndExit;
    180  1.5  christos         }
    181  1.5  christos 
    182  1.5  christos         /* Attach new object to the Node, remove local reference */
    183  1.5  christos 
    184  1.5  christos         Status = AcpiNsAttachObject (Device, ObjDesc, Node->Type);
    185  1.5  christos         AcpiUtRemoveReference (ObjDesc);
    186  1.5  christos         if (ACPI_FAILURE (Status))
    187  1.5  christos         {
    188  1.5  christos             goto UnlockAndExit;
    189  1.5  christos         }
    190  1.5  christos     }
    191  1.5  christos 
    192  1.5  christos     /* Ensure that the handler is not already installed in the lists */
    193  1.5  christos 
    194  1.5  christos     for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    195  1.5  christos     {
    196  1.5  christos         if (HandlerType & (i+1))
    197  1.5  christos         {
    198  1.5  christos             HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
    199  1.5  christos             while (HandlerObj)
    200  1.5  christos             {
    201  1.5  christos                 if (HandlerObj->Notify.Handler == Handler)
    202  1.5  christos                 {
    203  1.5  christos                     Status = AE_ALREADY_EXISTS;
    204  1.5  christos                     goto UnlockAndExit;
    205  1.5  christos                 }
    206  1.5  christos 
    207  1.5  christos                 HandlerObj = HandlerObj->Notify.Next[i];
    208  1.5  christos             }
    209  1.5  christos         }
    210  1.5  christos     }
    211  1.5  christos 
    212  1.5  christos     /* Create and populate a new notify handler object */
    213  1.5  christos 
    214  1.5  christos     HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_NOTIFY);
    215  1.5  christos     if (!HandlerObj)
    216  1.5  christos     {
    217  1.5  christos         Status = AE_NO_MEMORY;
    218  1.5  christos         goto UnlockAndExit;
    219  1.5  christos     }
    220  1.5  christos 
    221  1.5  christos     HandlerObj->Notify.Node = Node;
    222  1.5  christos     HandlerObj->Notify.HandlerType = HandlerType;
    223  1.5  christos     HandlerObj->Notify.Handler = Handler;
    224  1.5  christos     HandlerObj->Notify.Context = Context;
    225  1.5  christos 
    226  1.5  christos     /* Install the handler at the list head(s) */
    227  1.5  christos 
    228  1.5  christos     for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    229  1.5  christos     {
    230  1.5  christos         if (HandlerType & (i+1))
    231  1.5  christos         {
    232  1.5  christos             HandlerObj->Notify.Next[i] =
    233  1.5  christos                 ObjDesc->CommonNotify.NotifyList[i];
    234  1.5  christos 
    235  1.5  christos             ObjDesc->CommonNotify.NotifyList[i] = HandlerObj;
    236  1.5  christos         }
    237  1.5  christos     }
    238  1.5  christos 
    239  1.5  christos     /* Add an extra reference if handler was installed in both lists */
    240  1.5  christos 
    241  1.5  christos     if (HandlerType == ACPI_ALL_NOTIFY)
    242  1.5  christos     {
    243  1.5  christos         AcpiUtAddReference (HandlerObj);
    244  1.5  christos     }
    245  1.5  christos 
    246  1.5  christos 
    247  1.5  christos UnlockAndExit:
    248  1.5  christos     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    249  1.5  christos     return_ACPI_STATUS (Status);
    250  1.5  christos }
    251  1.5  christos 
    252  1.5  christos ACPI_EXPORT_SYMBOL (AcpiInstallNotifyHandler)
    253  1.5  christos 
    254  1.5  christos 
    255  1.5  christos /*******************************************************************************
    256  1.5  christos  *
    257  1.5  christos  * FUNCTION:    AcpiRemoveNotifyHandler
    258  1.5  christos  *
    259  1.5  christos  * PARAMETERS:  Device          - The device for which the handler is installed
    260  1.5  christos  *              HandlerType     - The type of handler:
    261  1.5  christos  *                                  ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
    262  1.5  christos  *                                  ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
    263  1.5  christos  *                                  ACPI_ALL_NOTIFY:    Both System and Device
    264  1.5  christos  *              Handler         - Address of the handler
    265  1.5  christos  *
    266  1.5  christos  * RETURN:      Status
    267  1.5  christos  *
    268  1.5  christos  * DESCRIPTION: Remove a handler for notifies on an ACPI device
    269  1.5  christos  *
    270  1.5  christos  ******************************************************************************/
    271  1.5  christos 
    272  1.5  christos ACPI_STATUS
    273  1.5  christos AcpiRemoveNotifyHandler (
    274  1.5  christos     ACPI_HANDLE             Device,
    275  1.5  christos     UINT32                  HandlerType,
    276  1.5  christos     ACPI_NOTIFY_HANDLER     Handler)
    277  1.5  christos {
    278  1.5  christos     ACPI_NAMESPACE_NODE     *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Device);
    279  1.5  christos     ACPI_OPERAND_OBJECT     *ObjDesc;
    280  1.5  christos     ACPI_OPERAND_OBJECT     *HandlerObj;
    281  1.5  christos     ACPI_OPERAND_OBJECT     *PreviousHandlerObj;
    282  1.6  christos     ACPI_STATUS             Status = AE_OK;
    283  1.5  christos     UINT32                  i;
    284  1.5  christos 
    285  1.5  christos 
    286  1.5  christos     ACPI_FUNCTION_TRACE (AcpiRemoveNotifyHandler);
    287  1.5  christos 
    288  1.5  christos 
    289  1.5  christos     /* Parameter validation */
    290  1.5  christos 
    291  1.5  christos     if ((!Device) || (!Handler) || (!HandlerType) ||
    292  1.5  christos         (HandlerType > ACPI_MAX_NOTIFY_HANDLER_TYPE))
    293  1.5  christos     {
    294  1.5  christos         return_ACPI_STATUS (AE_BAD_PARAMETER);
    295  1.5  christos     }
    296  1.5  christos 
    297  1.5  christos     /* Root Object. Global handlers are removed here */
    298  1.5  christos 
    299  1.5  christos     if (Device == ACPI_ROOT_OBJECT)
    300  1.5  christos     {
    301  1.5  christos         for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    302  1.5  christos         {
    303  1.5  christos             if (HandlerType & (i+1))
    304  1.5  christos             {
    305  1.6  christos                 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
    306  1.6  christos                 if (ACPI_FAILURE (Status))
    307  1.6  christos                 {
    308  1.6  christos                     return_ACPI_STATUS (Status);
    309  1.6  christos                 }
    310  1.6  christos 
    311  1.5  christos                 if (!AcpiGbl_GlobalNotify[i].Handler ||
    312  1.5  christos                     (AcpiGbl_GlobalNotify[i].Handler != Handler))
    313  1.5  christos                 {
    314  1.5  christos                     Status = AE_NOT_EXIST;
    315  1.5  christos                     goto UnlockAndExit;
    316  1.5  christos                 }
    317  1.5  christos 
    318  1.5  christos                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    319  1.5  christos                     "Removing global notify handler\n"));
    320  1.5  christos 
    321  1.5  christos                 AcpiGbl_GlobalNotify[i].Handler = NULL;
    322  1.5  christos                 AcpiGbl_GlobalNotify[i].Context = NULL;
    323  1.6  christos 
    324  1.6  christos                 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    325  1.6  christos 
    326  1.6  christos                 /* Make sure all deferred notify tasks are completed */
    327  1.6  christos 
    328  1.6  christos                 AcpiOsWaitEventsComplete ();
    329  1.5  christos             }
    330  1.5  christos         }
    331  1.5  christos 
    332  1.6  christos         return_ACPI_STATUS (AE_OK);
    333  1.5  christos     }
    334  1.5  christos 
    335  1.5  christos     /* All other objects: Are Notifies allowed on this object? */
    336  1.5  christos 
    337  1.5  christos     if (!AcpiEvIsNotifyObject (Node))
    338  1.5  christos     {
    339  1.6  christos         return_ACPI_STATUS (AE_TYPE);
    340  1.5  christos     }
    341  1.5  christos 
    342  1.5  christos     /* Must have an existing internal object */
    343  1.5  christos 
    344  1.5  christos     ObjDesc = AcpiNsGetAttachedObject (Node);
    345  1.5  christos     if (!ObjDesc)
    346  1.5  christos     {
    347  1.6  christos         return_ACPI_STATUS (AE_NOT_EXIST);
    348  1.5  christos     }
    349  1.5  christos 
    350  1.5  christos     /* Internal object exists. Find the handler and remove it */
    351  1.5  christos 
    352  1.5  christos     for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    353  1.5  christos     {
    354  1.5  christos         if (HandlerType & (i+1))
    355  1.5  christos         {
    356  1.6  christos             Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
    357  1.6  christos             if (ACPI_FAILURE (Status))
    358  1.6  christos             {
    359  1.6  christos                 return_ACPI_STATUS (Status);
    360  1.6  christos             }
    361  1.6  christos 
    362  1.5  christos             HandlerObj = ObjDesc->CommonNotify.NotifyList[i];
    363  1.5  christos             PreviousHandlerObj = NULL;
    364  1.5  christos 
    365  1.5  christos             /* Attempt to find the handler in the handler list */
    366  1.5  christos 
    367  1.5  christos             while (HandlerObj &&
    368  1.5  christos                   (HandlerObj->Notify.Handler != Handler))
    369  1.5  christos             {
    370  1.5  christos                 PreviousHandlerObj = HandlerObj;
    371  1.5  christos                 HandlerObj = HandlerObj->Notify.Next[i];
    372  1.5  christos             }
    373  1.5  christos 
    374  1.5  christos             if (!HandlerObj)
    375  1.5  christos             {
    376  1.5  christos                 Status = AE_NOT_EXIST;
    377  1.5  christos                 goto UnlockAndExit;
    378  1.5  christos             }
    379  1.5  christos 
    380  1.5  christos             /* Remove the handler object from the list */
    381  1.5  christos 
    382  1.5  christos             if (PreviousHandlerObj) /* Handler is not at the list head */
    383  1.5  christos             {
    384  1.5  christos                 PreviousHandlerObj->Notify.Next[i] =
    385  1.5  christos                     HandlerObj->Notify.Next[i];
    386  1.5  christos             }
    387  1.5  christos             else /* Handler is at the list head */
    388  1.5  christos             {
    389  1.5  christos                 ObjDesc->CommonNotify.NotifyList[i] =
    390  1.5  christos                     HandlerObj->Notify.Next[i];
    391  1.5  christos             }
    392  1.5  christos 
    393  1.6  christos             (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    394  1.6  christos 
    395  1.6  christos             /* Make sure all deferred notify tasks are completed */
    396  1.6  christos 
    397  1.6  christos             AcpiOsWaitEventsComplete ();
    398  1.5  christos             AcpiUtRemoveReference (HandlerObj);
    399  1.5  christos         }
    400  1.5  christos     }
    401  1.5  christos 
    402  1.6  christos     return_ACPI_STATUS (Status);
    403  1.6  christos 
    404  1.5  christos 
    405  1.5  christos UnlockAndExit:
    406  1.5  christos     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    407  1.5  christos     return_ACPI_STATUS (Status);
    408  1.5  christos }
    409  1.5  christos 
    410  1.5  christos ACPI_EXPORT_SYMBOL (AcpiRemoveNotifyHandler)
    411  1.5  christos 
    412  1.5  christos 
    413  1.5  christos /*******************************************************************************
    414  1.5  christos  *
    415  1.1    jruoho  * FUNCTION:    AcpiInstallExceptionHandler
    416  1.1    jruoho  *
    417  1.1    jruoho  * PARAMETERS:  Handler         - Pointer to the handler function for the
    418  1.1    jruoho  *                                event
    419  1.1    jruoho  *
    420  1.1    jruoho  * RETURN:      Status
    421  1.1    jruoho  *
    422  1.1    jruoho  * DESCRIPTION: Saves the pointer to the handler function
    423  1.1    jruoho  *
    424  1.1    jruoho  ******************************************************************************/
    425  1.1    jruoho 
    426  1.1    jruoho ACPI_STATUS
    427  1.1    jruoho AcpiInstallExceptionHandler (
    428  1.1    jruoho     ACPI_EXCEPTION_HANDLER  Handler)
    429  1.1    jruoho {
    430  1.1    jruoho     ACPI_STATUS             Status;
    431  1.1    jruoho 
    432  1.1    jruoho 
    433  1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiInstallExceptionHandler);
    434  1.1    jruoho 
    435  1.1    jruoho 
    436  1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    437  1.1    jruoho     if (ACPI_FAILURE (Status))
    438  1.1    jruoho     {
    439  1.1    jruoho         return_ACPI_STATUS (Status);
    440  1.1    jruoho     }
    441  1.1    jruoho 
    442  1.5  christos     /* Don't allow two handlers. */
    443  1.5  christos 
    444  1.5  christos     if (AcpiGbl_ExceptionHandler)
    445  1.5  christos     {
    446  1.5  christos         Status = AE_ALREADY_EXISTS;
    447  1.5  christos         goto Cleanup;
    448  1.5  christos     }
    449  1.5  christos 
    450  1.5  christos     /* Install the handler */
    451  1.5  christos 
    452  1.5  christos     AcpiGbl_ExceptionHandler = Handler;
    453  1.5  christos 
    454  1.5  christos Cleanup:
    455  1.5  christos     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    456  1.5  christos     return_ACPI_STATUS (Status);
    457  1.5  christos }
    458  1.5  christos 
    459  1.5  christos ACPI_EXPORT_SYMBOL (AcpiInstallExceptionHandler)
    460  1.5  christos 
    461  1.5  christos 
    462  1.5  christos #if (!ACPI_REDUCED_HARDWARE)
    463  1.5  christos /*******************************************************************************
    464  1.5  christos  *
    465  1.5  christos  * FUNCTION:    AcpiInstallSciHandler
    466  1.5  christos  *
    467  1.5  christos  * PARAMETERS:  Address             - Address of the handler
    468  1.5  christos  *              Context             - Value passed to the handler on each SCI
    469  1.5  christos  *
    470  1.5  christos  * RETURN:      Status
    471  1.5  christos  *
    472  1.5  christos  * DESCRIPTION: Install a handler for a System Control Interrupt.
    473  1.5  christos  *
    474  1.5  christos  ******************************************************************************/
    475  1.5  christos 
    476  1.5  christos ACPI_STATUS
    477  1.5  christos AcpiInstallSciHandler (
    478  1.5  christos     ACPI_SCI_HANDLER        Address,
    479  1.5  christos     void                    *Context)
    480  1.5  christos {
    481  1.5  christos     ACPI_SCI_HANDLER_INFO   *NewSciHandler;
    482  1.5  christos     ACPI_SCI_HANDLER_INFO   *SciHandler;
    483  1.5  christos     ACPI_CPU_FLAGS          Flags;
    484  1.5  christos     ACPI_STATUS             Status;
    485  1.5  christos 
    486  1.5  christos 
    487  1.5  christos     ACPI_FUNCTION_TRACE (AcpiInstallSciHandler);
    488  1.5  christos 
    489  1.5  christos 
    490  1.5  christos     if (!Address)
    491  1.5  christos     {
    492  1.5  christos         return_ACPI_STATUS (AE_BAD_PARAMETER);
    493  1.5  christos     }
    494  1.5  christos 
    495  1.5  christos     /* Allocate and init a handler object */
    496  1.5  christos 
    497  1.5  christos     NewSciHandler = ACPI_ALLOCATE (sizeof (ACPI_SCI_HANDLER_INFO));
    498  1.5  christos     if (!NewSciHandler)
    499  1.5  christos     {
    500  1.5  christos         return_ACPI_STATUS (AE_NO_MEMORY);
    501  1.5  christos     }
    502  1.5  christos 
    503  1.5  christos     NewSciHandler->Address = Address;
    504  1.5  christos     NewSciHandler->Context = Context;
    505  1.5  christos 
    506  1.5  christos     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    507  1.5  christos     if (ACPI_FAILURE (Status))
    508  1.5  christos     {
    509  1.5  christos         goto Exit;
    510  1.5  christos     }
    511  1.5  christos 
    512  1.5  christos     /* Lock list during installation */
    513  1.5  christos 
    514  1.5  christos     Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
    515  1.5  christos     SciHandler = AcpiGbl_SciHandlerList;
    516  1.5  christos 
    517  1.5  christos     /* Ensure handler does not already exist */
    518  1.5  christos 
    519  1.5  christos     while (SciHandler)
    520  1.5  christos     {
    521  1.5  christos         if (Address == SciHandler->Address)
    522  1.5  christos         {
    523  1.5  christos             Status = AE_ALREADY_EXISTS;
    524  1.5  christos             goto UnlockAndExit;
    525  1.5  christos         }
    526  1.5  christos 
    527  1.5  christos         SciHandler = SciHandler->Next;
    528  1.5  christos     }
    529  1.5  christos 
    530  1.5  christos     /* Install the new handler into the global list (at head) */
    531  1.5  christos 
    532  1.5  christos     NewSciHandler->Next = AcpiGbl_SciHandlerList;
    533  1.5  christos     AcpiGbl_SciHandlerList = NewSciHandler;
    534  1.5  christos 
    535  1.5  christos 
    536  1.5  christos UnlockAndExit:
    537  1.5  christos 
    538  1.5  christos     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
    539  1.5  christos     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    540  1.5  christos 
    541  1.5  christos Exit:
    542  1.5  christos     if (ACPI_FAILURE (Status))
    543  1.5  christos     {
    544  1.5  christos         ACPI_FREE (NewSciHandler);
    545  1.5  christos     }
    546  1.5  christos     return_ACPI_STATUS (Status);
    547  1.5  christos }
    548  1.5  christos 
    549  1.6  christos ACPI_EXPORT_SYMBOL (AcpiInstallSciHandler)
    550  1.6  christos 
    551  1.5  christos 
    552  1.5  christos /*******************************************************************************
    553  1.5  christos  *
    554  1.5  christos  * FUNCTION:    AcpiRemoveSciHandler
    555  1.5  christos  *
    556  1.5  christos  * PARAMETERS:  Address             - Address of the handler
    557  1.5  christos  *
    558  1.5  christos  * RETURN:      Status
    559  1.5  christos  *
    560  1.5  christos  * DESCRIPTION: Remove a handler for a System Control Interrupt.
    561  1.5  christos  *
    562  1.5  christos  ******************************************************************************/
    563  1.5  christos 
    564  1.5  christos ACPI_STATUS
    565  1.5  christos AcpiRemoveSciHandler (
    566  1.5  christos     ACPI_SCI_HANDLER        Address)
    567  1.5  christos {
    568  1.5  christos     ACPI_SCI_HANDLER_INFO   *PrevSciHandler;
    569  1.5  christos     ACPI_SCI_HANDLER_INFO   *NextSciHandler;
    570  1.5  christos     ACPI_CPU_FLAGS          Flags;
    571  1.5  christos     ACPI_STATUS             Status;
    572  1.5  christos 
    573  1.5  christos 
    574  1.5  christos     ACPI_FUNCTION_TRACE (AcpiRemoveSciHandler);
    575  1.5  christos 
    576  1.5  christos 
    577  1.5  christos     if (!Address)
    578  1.5  christos     {
    579  1.5  christos         return_ACPI_STATUS (AE_BAD_PARAMETER);
    580  1.5  christos     }
    581  1.5  christos 
    582  1.5  christos     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    583  1.5  christos     if (ACPI_FAILURE (Status))
    584  1.5  christos     {
    585  1.5  christos         return_ACPI_STATUS (Status);
    586  1.5  christos     }
    587  1.5  christos 
    588  1.5  christos     /* Remove the SCI handler with lock */
    589  1.5  christos 
    590  1.5  christos     Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
    591  1.5  christos 
    592  1.5  christos     PrevSciHandler = NULL;
    593  1.5  christos     NextSciHandler = AcpiGbl_SciHandlerList;
    594  1.5  christos     while (NextSciHandler)
    595  1.5  christos     {
    596  1.5  christos         if (NextSciHandler->Address == Address)
    597  1.5  christos         {
    598  1.5  christos             /* Unlink and free the SCI handler info block */
    599  1.5  christos 
    600  1.5  christos             if (PrevSciHandler)
    601  1.5  christos             {
    602  1.5  christos                 PrevSciHandler->Next = NextSciHandler->Next;
    603  1.5  christos             }
    604  1.5  christos             else
    605  1.5  christos             {
    606  1.5  christos                 AcpiGbl_SciHandlerList = NextSciHandler->Next;
    607  1.5  christos             }
    608  1.5  christos 
    609  1.5  christos             AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
    610  1.5  christos             ACPI_FREE (NextSciHandler);
    611  1.5  christos             goto UnlockAndExit;
    612  1.5  christos         }
    613  1.1    jruoho 
    614  1.5  christos         PrevSciHandler = NextSciHandler;
    615  1.5  christos         NextSciHandler = NextSciHandler->Next;
    616  1.1    jruoho     }
    617  1.1    jruoho 
    618  1.5  christos     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
    619  1.5  christos     Status = AE_NOT_EXIST;
    620  1.1    jruoho 
    621  1.1    jruoho 
    622  1.5  christos UnlockAndExit:
    623  1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    624  1.1    jruoho     return_ACPI_STATUS (Status);
    625  1.1    jruoho }
    626  1.1    jruoho 
    627  1.6  christos ACPI_EXPORT_SYMBOL (AcpiRemoveSciHandler)
    628  1.6  christos 
    629  1.1    jruoho 
    630  1.1    jruoho /*******************************************************************************
    631  1.1    jruoho  *
    632  1.4    jruoho  * FUNCTION:    AcpiInstallGlobalEventHandler
    633  1.4    jruoho  *
    634  1.4    jruoho  * PARAMETERS:  Handler         - Pointer to the global event handler function
    635  1.4    jruoho  *              Context         - Value passed to the handler on each event
    636  1.4    jruoho  *
    637  1.4    jruoho  * RETURN:      Status
    638  1.4    jruoho  *
    639  1.4    jruoho  * DESCRIPTION: Saves the pointer to the handler function. The global handler
    640  1.4    jruoho  *              is invoked upon each incoming GPE and Fixed Event. It is
    641  1.4    jruoho  *              invoked at interrupt level at the time of the event dispatch.
    642  1.4    jruoho  *              Can be used to update event counters, etc.
    643  1.4    jruoho  *
    644  1.4    jruoho  ******************************************************************************/
    645  1.4    jruoho 
    646  1.4    jruoho ACPI_STATUS
    647  1.4    jruoho AcpiInstallGlobalEventHandler (
    648  1.4    jruoho     ACPI_GBL_EVENT_HANDLER  Handler,
    649  1.4    jruoho     void                    *Context)
    650  1.4    jruoho {
    651  1.4    jruoho     ACPI_STATUS             Status;
    652  1.4    jruoho 
    653  1.4    jruoho 
    654  1.4    jruoho     ACPI_FUNCTION_TRACE (AcpiInstallGlobalEventHandler);
    655  1.4    jruoho 
    656  1.4    jruoho 
    657  1.4    jruoho     /* Parameter validation */
    658  1.4    jruoho 
    659  1.4    jruoho     if (!Handler)
    660  1.4    jruoho     {
    661  1.4    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    662  1.4    jruoho     }
    663  1.4    jruoho 
    664  1.4    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    665  1.4    jruoho     if (ACPI_FAILURE (Status))
    666  1.4    jruoho     {
    667  1.4    jruoho         return_ACPI_STATUS (Status);
    668  1.4    jruoho     }
    669  1.4    jruoho 
    670  1.4    jruoho     /* Don't allow two handlers. */
    671  1.4    jruoho 
    672  1.4    jruoho     if (AcpiGbl_GlobalEventHandler)
    673  1.4    jruoho     {
    674  1.4    jruoho         Status = AE_ALREADY_EXISTS;
    675  1.4    jruoho         goto Cleanup;
    676  1.4    jruoho     }
    677  1.4    jruoho 
    678  1.4    jruoho     AcpiGbl_GlobalEventHandler = Handler;
    679  1.4    jruoho     AcpiGbl_GlobalEventHandlerContext = Context;
    680  1.4    jruoho 
    681  1.4    jruoho 
    682  1.4    jruoho Cleanup:
    683  1.4    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    684  1.4    jruoho     return_ACPI_STATUS (Status);
    685  1.4    jruoho }
    686  1.4    jruoho 
    687  1.4    jruoho ACPI_EXPORT_SYMBOL (AcpiInstallGlobalEventHandler)
    688  1.4    jruoho 
    689  1.4    jruoho 
    690  1.4    jruoho /*******************************************************************************
    691  1.4    jruoho  *
    692  1.1    jruoho  * FUNCTION:    AcpiInstallFixedEventHandler
    693  1.1    jruoho  *
    694  1.1    jruoho  * PARAMETERS:  Event           - Event type to enable.
    695  1.1    jruoho  *              Handler         - Pointer to the handler function for the
    696  1.1    jruoho  *                                event
    697  1.1    jruoho  *              Context         - Value passed to the handler on each GPE
    698  1.1    jruoho  *
    699  1.1    jruoho  * RETURN:      Status
    700  1.1    jruoho  *
    701  1.1    jruoho  * DESCRIPTION: Saves the pointer to the handler function and then enables the
    702  1.1    jruoho  *              event.
    703  1.1    jruoho  *
    704  1.1    jruoho  ******************************************************************************/
    705  1.1    jruoho 
    706  1.1    jruoho ACPI_STATUS
    707  1.1    jruoho AcpiInstallFixedEventHandler (
    708  1.1    jruoho     UINT32                  Event,
    709  1.1    jruoho     ACPI_EVENT_HANDLER      Handler,
    710  1.1    jruoho     void                    *Context)
    711  1.1    jruoho {
    712  1.1    jruoho     ACPI_STATUS             Status;
    713  1.1    jruoho 
    714  1.1    jruoho 
    715  1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiInstallFixedEventHandler);
    716  1.1    jruoho 
    717  1.1    jruoho 
    718  1.1    jruoho     /* Parameter validation */
    719  1.1    jruoho 
    720  1.1    jruoho     if (Event > ACPI_EVENT_MAX)
    721  1.1    jruoho     {
    722  1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    723  1.1    jruoho     }
    724  1.1    jruoho 
    725  1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    726  1.1    jruoho     if (ACPI_FAILURE (Status))
    727  1.1    jruoho     {
    728  1.1    jruoho         return_ACPI_STATUS (Status);
    729  1.1    jruoho     }
    730  1.1    jruoho 
    731  1.5  christos     /* Do not allow multiple handlers */
    732  1.1    jruoho 
    733  1.5  christos     if (AcpiGbl_FixedEventHandlers[Event].Handler)
    734  1.1    jruoho     {
    735  1.1    jruoho         Status = AE_ALREADY_EXISTS;
    736  1.1    jruoho         goto Cleanup;
    737  1.1    jruoho     }
    738  1.1    jruoho 
    739  1.1    jruoho     /* Install the handler before enabling the event */
    740  1.1    jruoho 
    741  1.1    jruoho     AcpiGbl_FixedEventHandlers[Event].Handler = Handler;
    742  1.1    jruoho     AcpiGbl_FixedEventHandlers[Event].Context = Context;
    743  1.1    jruoho 
    744  1.1    jruoho     Status = AcpiEnableEvent (Event, 0);
    745  1.1    jruoho     if (ACPI_FAILURE (Status))
    746  1.1    jruoho     {
    747  1.5  christos         ACPI_WARNING ((AE_INFO,
    748  1.5  christos             "Could not enable fixed event - %s (%u)",
    749  1.5  christos             AcpiUtGetEventName (Event), Event));
    750  1.1    jruoho 
    751  1.1    jruoho         /* Remove the handler */
    752  1.1    jruoho 
    753  1.1    jruoho         AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
    754  1.1    jruoho         AcpiGbl_FixedEventHandlers[Event].Context = NULL;
    755  1.1    jruoho     }
    756  1.1    jruoho     else
    757  1.1    jruoho     {
    758  1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    759  1.5  christos             "Enabled fixed event %s (%X), Handler=%p\n",
    760  1.5  christos             AcpiUtGetEventName (Event), Event, Handler));
    761  1.1    jruoho     }
    762  1.1    jruoho 
    763  1.1    jruoho 
    764  1.1    jruoho Cleanup:
    765  1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    766  1.1    jruoho     return_ACPI_STATUS (Status);
    767  1.1    jruoho }
    768  1.1    jruoho 
    769  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiInstallFixedEventHandler)
    770  1.1    jruoho 
    771  1.1    jruoho 
    772  1.1    jruoho /*******************************************************************************
    773  1.1    jruoho  *
    774  1.1    jruoho  * FUNCTION:    AcpiRemoveFixedEventHandler
    775  1.1    jruoho  *
    776  1.1    jruoho  * PARAMETERS:  Event           - Event type to disable.
    777  1.1    jruoho  *              Handler         - Address of the handler
    778  1.1    jruoho  *
    779  1.1    jruoho  * RETURN:      Status
    780  1.1    jruoho  *
    781  1.1    jruoho  * DESCRIPTION: Disables the event and unregisters the event handler.
    782  1.1    jruoho  *
    783  1.1    jruoho  ******************************************************************************/
    784  1.1    jruoho 
    785  1.1    jruoho ACPI_STATUS
    786  1.1    jruoho AcpiRemoveFixedEventHandler (
    787  1.1    jruoho     UINT32                  Event,
    788  1.1    jruoho     ACPI_EVENT_HANDLER      Handler)
    789  1.1    jruoho {
    790  1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    791  1.1    jruoho 
    792  1.1    jruoho 
    793  1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiRemoveFixedEventHandler);
    794  1.1    jruoho 
    795  1.1    jruoho 
    796  1.1    jruoho     /* Parameter validation */
    797  1.1    jruoho 
    798  1.1    jruoho     if (Event > ACPI_EVENT_MAX)
    799  1.1    jruoho     {
    800  1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    801  1.1    jruoho     }
    802  1.1    jruoho 
    803  1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    804  1.1    jruoho     if (ACPI_FAILURE (Status))
    805  1.1    jruoho     {
    806  1.1    jruoho         return_ACPI_STATUS (Status);
    807  1.1    jruoho     }
    808  1.1    jruoho 
    809  1.1    jruoho     /* Disable the event before removing the handler */
    810  1.1    jruoho 
    811  1.1    jruoho     Status = AcpiDisableEvent (Event, 0);
    812  1.1    jruoho 
    813  1.1    jruoho     /* Always Remove the handler */
    814  1.1    jruoho 
    815  1.1    jruoho     AcpiGbl_FixedEventHandlers[Event].Handler = NULL;
    816  1.1    jruoho     AcpiGbl_FixedEventHandlers[Event].Context = NULL;
    817  1.1    jruoho 
    818  1.1    jruoho     if (ACPI_FAILURE (Status))
    819  1.1    jruoho     {
    820  1.1    jruoho         ACPI_WARNING ((AE_INFO,
    821  1.5  christos             "Could not disable fixed event - %s (%u)",
    822  1.5  christos             AcpiUtGetEventName (Event), Event));
    823  1.1    jruoho     }
    824  1.1    jruoho     else
    825  1.1    jruoho     {
    826  1.5  christos         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
    827  1.5  christos             "Disabled fixed event - %s (%X)\n",
    828  1.5  christos             AcpiUtGetEventName (Event), Event));
    829  1.1    jruoho     }
    830  1.1    jruoho 
    831  1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    832  1.1    jruoho     return_ACPI_STATUS (Status);
    833  1.1    jruoho }
    834  1.1    jruoho 
    835  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiRemoveFixedEventHandler)
    836  1.1    jruoho 
    837  1.1    jruoho 
    838  1.1    jruoho /*******************************************************************************
    839  1.1    jruoho  *
    840  1.7  christos  * FUNCTION:    AcpiEvInstallGpeHandler
    841  1.1    jruoho  *
    842  1.1    jruoho  * PARAMETERS:  GpeDevice       - Namespace node for the GPE (NULL for FADT
    843  1.1    jruoho  *                                defined GPEs)
    844  1.1    jruoho  *              GpeNumber       - The GPE number within the GPE block
    845  1.1    jruoho  *              Type            - Whether this GPE should be treated as an
    846  1.1    jruoho  *                                edge- or level-triggered interrupt.
    847  1.7  christos  *              IsRawHandler    - Whether this GPE should be handled using
    848  1.7  christos  *                                the special GPE handler mode.
    849  1.1    jruoho  *              Address         - Address of the handler
    850  1.1    jruoho  *              Context         - Value passed to the handler on each GPE
    851  1.1    jruoho  *
    852  1.1    jruoho  * RETURN:      Status
    853  1.1    jruoho  *
    854  1.7  christos  * DESCRIPTION: Internal function to install a handler for a General Purpose
    855  1.7  christos  *              Event.
    856  1.1    jruoho  *
    857  1.1    jruoho  ******************************************************************************/
    858  1.1    jruoho 
    859  1.7  christos static ACPI_STATUS
    860  1.7  christos AcpiEvInstallGpeHandler (
    861  1.1    jruoho     ACPI_HANDLE             GpeDevice,
    862  1.1    jruoho     UINT32                  GpeNumber,
    863  1.1    jruoho     UINT32                  Type,
    864  1.7  christos     BOOLEAN                 IsRawHandler,
    865  1.4    jruoho     ACPI_GPE_HANDLER        Address,
    866  1.1    jruoho     void                    *Context)
    867  1.1    jruoho {
    868  1.1    jruoho     ACPI_GPE_EVENT_INFO     *GpeEventInfo;
    869  1.4    jruoho     ACPI_GPE_HANDLER_INFO   *Handler;
    870  1.1    jruoho     ACPI_STATUS             Status;
    871  1.1    jruoho     ACPI_CPU_FLAGS          Flags;
    872  1.1    jruoho 
    873  1.1    jruoho 
    874  1.7  christos     ACPI_FUNCTION_TRACE (EvInstallGpeHandler);
    875  1.1    jruoho 
    876  1.1    jruoho 
    877  1.1    jruoho     /* Parameter validation */
    878  1.1    jruoho 
    879  1.1    jruoho     if ((!Address) || (Type & ~ACPI_GPE_XRUPT_TYPE_MASK))
    880  1.1    jruoho     {
    881  1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    882  1.1    jruoho     }
    883  1.1    jruoho 
    884  1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
    885  1.1    jruoho     if (ACPI_FAILURE (Status))
    886  1.1    jruoho     {
    887  1.1    jruoho         return_ACPI_STATUS (Status);
    888  1.1    jruoho     }
    889  1.1    jruoho 
    890  1.4    jruoho     /* Allocate and init handler object (before lock) */
    891  1.4    jruoho 
    892  1.4    jruoho     Handler = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_GPE_HANDLER_INFO));
    893  1.4    jruoho     if (!Handler)
    894  1.4    jruoho     {
    895  1.4    jruoho         Status = AE_NO_MEMORY;
    896  1.4    jruoho         goto UnlockAndExit;
    897  1.4    jruoho     }
    898  1.4    jruoho 
    899  1.4    jruoho     Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
    900  1.4    jruoho 
    901  1.1    jruoho     /* Ensure that we have a valid GPE number */
    902  1.1    jruoho 
    903  1.1    jruoho     GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
    904  1.1    jruoho     if (!GpeEventInfo)
    905  1.1    jruoho     {
    906  1.1    jruoho         Status = AE_BAD_PARAMETER;
    907  1.4    jruoho         goto FreeAndExit;
    908  1.1    jruoho     }
    909  1.1    jruoho 
    910  1.1    jruoho     /* Make sure that there isn't a handler there already */
    911  1.1    jruoho 
    912  1.7  christos     if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
    913  1.7  christos             ACPI_GPE_DISPATCH_HANDLER) ||
    914  1.7  christos         (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) ==
    915  1.7  christos             ACPI_GPE_DISPATCH_RAW_HANDLER))
    916  1.1    jruoho     {
    917  1.1    jruoho         Status = AE_ALREADY_EXISTS;
    918  1.4    jruoho         goto FreeAndExit;
    919  1.1    jruoho     }
    920  1.1    jruoho 
    921  1.4    jruoho     Handler->Address = Address;
    922  1.4    jruoho     Handler->Context = Context;
    923  1.4    jruoho     Handler->MethodNode = GpeEventInfo->Dispatch.MethodNode;
    924  1.4    jruoho     Handler->OriginalFlags = (UINT8) (GpeEventInfo->Flags &
    925  1.4    jruoho         (ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK));
    926  1.1    jruoho 
    927  1.4    jruoho     /*
    928  1.4    jruoho      * If the GPE is associated with a method, it may have been enabled
    929  1.4    jruoho      * automatically during initialization, in which case it has to be
    930  1.4    jruoho      * disabled now to avoid spurious execution of the handler.
    931  1.4    jruoho      */
    932  1.7  christos     if (((ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==
    933  1.7  christos             ACPI_GPE_DISPATCH_METHOD) ||
    934  1.7  christos          (ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==
    935  1.7  christos             ACPI_GPE_DISPATCH_NOTIFY)) &&
    936  1.4    jruoho         GpeEventInfo->RuntimeCount)
    937  1.1    jruoho     {
    938  1.4    jruoho         Handler->OriginallyEnabled = TRUE;
    939  1.4    jruoho         (void) AcpiEvRemoveGpeReference (GpeEventInfo);
    940  1.1    jruoho 
    941  1.4    jruoho         /* Sanity check of original type against new type */
    942  1.1    jruoho 
    943  1.4    jruoho         if (Type != (UINT32) (GpeEventInfo->Flags & ACPI_GPE_XRUPT_TYPE_MASK))
    944  1.4    jruoho         {
    945  1.4    jruoho             ACPI_WARNING ((AE_INFO, "GPE type mismatch (level/edge)"));
    946  1.4    jruoho         }
    947  1.1    jruoho     }
    948  1.1    jruoho 
    949  1.1    jruoho     /* Install the handler */
    950  1.1    jruoho 
    951  1.1    jruoho     GpeEventInfo->Dispatch.Handler = Handler;
    952  1.1    jruoho 
    953  1.4    jruoho     /* Setup up dispatch flags to indicate handler (vs. method/notify) */
    954  1.1    jruoho 
    955  1.1    jruoho     GpeEventInfo->Flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
    956  1.7  christos     GpeEventInfo->Flags |= (UINT8) (Type | (IsRawHandler ?
    957  1.7  christos         ACPI_GPE_DISPATCH_RAW_HANDLER : ACPI_GPE_DISPATCH_HANDLER));
    958  1.1    jruoho 
    959  1.1    jruoho     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
    960  1.1    jruoho 
    961  1.1    jruoho 
    962  1.1    jruoho UnlockAndExit:
    963  1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
    964  1.1    jruoho     return_ACPI_STATUS (Status);
    965  1.4    jruoho 
    966  1.4    jruoho FreeAndExit:
    967  1.4    jruoho     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
    968  1.4    jruoho     ACPI_FREE (Handler);
    969  1.4    jruoho     goto UnlockAndExit;
    970  1.1    jruoho }
    971  1.1    jruoho 
    972  1.7  christos 
    973  1.7  christos /*******************************************************************************
    974  1.7  christos  *
    975  1.7  christos  * FUNCTION:    AcpiInstallGpeHandler
    976  1.7  christos  *
    977  1.7  christos  * PARAMETERS:  GpeDevice       - Namespace node for the GPE (NULL for FADT
    978  1.7  christos  *                                defined GPEs)
    979  1.7  christos  *              GpeNumber       - The GPE number within the GPE block
    980  1.7  christos  *              Type            - Whether this GPE should be treated as an
    981  1.7  christos  *                                edge- or level-triggered interrupt.
    982  1.7  christos  *              Address         - Address of the handler
    983  1.7  christos  *              Context         - Value passed to the handler on each GPE
    984  1.7  christos  *
    985  1.7  christos  * RETURN:      Status
    986  1.7  christos  *
    987  1.7  christos  * DESCRIPTION: Install a handler for a General Purpose Event.
    988  1.7  christos  *
    989  1.7  christos  ******************************************************************************/
    990  1.7  christos 
    991  1.7  christos ACPI_STATUS
    992  1.7  christos AcpiInstallGpeHandler (
    993  1.7  christos     ACPI_HANDLE             GpeDevice,
    994  1.7  christos     UINT32                  GpeNumber,
    995  1.7  christos     UINT32                  Type,
    996  1.7  christos     ACPI_GPE_HANDLER        Address,
    997  1.7  christos     void                    *Context)
    998  1.7  christos {
    999  1.7  christos     ACPI_STATUS             Status;
   1000  1.7  christos 
   1001  1.7  christos 
   1002  1.7  christos     ACPI_FUNCTION_TRACE (AcpiInstallGpeHandler);
   1003  1.7  christos 
   1004  1.7  christos 
   1005  1.8  christos     Status = AcpiEvInstallGpeHandler (GpeDevice, GpeNumber, Type,
   1006  1.8  christos         FALSE, Address, Context);
   1007  1.7  christos 
   1008  1.7  christos     return_ACPI_STATUS (Status);
   1009  1.7  christos }
   1010  1.7  christos 
   1011  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiInstallGpeHandler)
   1012  1.1    jruoho 
   1013  1.1    jruoho 
   1014  1.1    jruoho /*******************************************************************************
   1015  1.1    jruoho  *
   1016  1.7  christos  * FUNCTION:    AcpiInstallGpeRawHandler
   1017  1.7  christos  *
   1018  1.7  christos  * PARAMETERS:  GpeDevice       - Namespace node for the GPE (NULL for FADT
   1019  1.7  christos  *                                defined GPEs)
   1020  1.7  christos  *              GpeNumber       - The GPE number within the GPE block
   1021  1.7  christos  *              Type            - Whether this GPE should be treated as an
   1022  1.7  christos  *                                edge- or level-triggered interrupt.
   1023  1.7  christos  *              Address         - Address of the handler
   1024  1.7  christos  *              Context         - Value passed to the handler on each GPE
   1025  1.7  christos  *
   1026  1.7  christos  * RETURN:      Status
   1027  1.7  christos  *
   1028  1.7  christos  * DESCRIPTION: Install a handler for a General Purpose Event.
   1029  1.7  christos  *
   1030  1.7  christos  ******************************************************************************/
   1031  1.7  christos 
   1032  1.7  christos ACPI_STATUS
   1033  1.7  christos AcpiInstallGpeRawHandler (
   1034  1.7  christos     ACPI_HANDLE             GpeDevice,
   1035  1.7  christos     UINT32                  GpeNumber,
   1036  1.7  christos     UINT32                  Type,
   1037  1.7  christos     ACPI_GPE_HANDLER        Address,
   1038  1.7  christos     void                    *Context)
   1039  1.7  christos {
   1040  1.7  christos     ACPI_STATUS             Status;
   1041  1.7  christos 
   1042  1.7  christos 
   1043  1.7  christos     ACPI_FUNCTION_TRACE (AcpiInstallGpeRawHandler);
   1044  1.7  christos 
   1045  1.7  christos 
   1046  1.8  christos     Status = AcpiEvInstallGpeHandler (GpeDevice, GpeNumber, Type,
   1047  1.8  christos         TRUE, Address, Context);
   1048  1.7  christos 
   1049  1.7  christos     return_ACPI_STATUS (Status);
   1050  1.7  christos }
   1051  1.7  christos 
   1052  1.7  christos ACPI_EXPORT_SYMBOL (AcpiInstallGpeRawHandler)
   1053  1.7  christos 
   1054  1.7  christos 
   1055  1.7  christos /*******************************************************************************
   1056  1.7  christos  *
   1057  1.1    jruoho  * FUNCTION:    AcpiRemoveGpeHandler
   1058  1.1    jruoho  *
   1059  1.1    jruoho  * PARAMETERS:  GpeDevice       - Namespace node for the GPE (NULL for FADT
   1060  1.1    jruoho  *                                defined GPEs)
   1061  1.1    jruoho  *              GpeNumber       - The event to remove a handler
   1062  1.1    jruoho  *              Address         - Address of the handler
   1063  1.1    jruoho  *
   1064  1.1    jruoho  * RETURN:      Status
   1065  1.1    jruoho  *
   1066  1.1    jruoho  * DESCRIPTION: Remove a handler for a General Purpose AcpiEvent.
   1067  1.1    jruoho  *
   1068  1.1    jruoho  ******************************************************************************/
   1069  1.1    jruoho 
   1070  1.1    jruoho ACPI_STATUS
   1071  1.1    jruoho AcpiRemoveGpeHandler (
   1072  1.1    jruoho     ACPI_HANDLE             GpeDevice,
   1073  1.1    jruoho     UINT32                  GpeNumber,
   1074  1.4    jruoho     ACPI_GPE_HANDLER        Address)
   1075  1.1    jruoho {
   1076  1.1    jruoho     ACPI_GPE_EVENT_INFO     *GpeEventInfo;
   1077  1.4    jruoho     ACPI_GPE_HANDLER_INFO   *Handler;
   1078  1.1    jruoho     ACPI_STATUS             Status;
   1079  1.1    jruoho     ACPI_CPU_FLAGS          Flags;
   1080  1.1    jruoho 
   1081  1.1    jruoho 
   1082  1.1    jruoho     ACPI_FUNCTION_TRACE (AcpiRemoveGpeHandler);
   1083  1.1    jruoho 
   1084  1.1    jruoho 
   1085  1.1    jruoho     /* Parameter validation */
   1086  1.1    jruoho 
   1087  1.1    jruoho     if (!Address)
   1088  1.1    jruoho     {
   1089  1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
   1090  1.1    jruoho     }
   1091  1.1    jruoho 
   1092  1.1    jruoho     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
   1093  1.1    jruoho     if (ACPI_FAILURE (Status))
   1094  1.1    jruoho     {
   1095  1.1    jruoho         return_ACPI_STATUS (Status);
   1096  1.1    jruoho     }
   1097  1.1    jruoho 
   1098  1.4    jruoho     Flags = AcpiOsAcquireLock (AcpiGbl_GpeLock);
   1099  1.4    jruoho 
   1100  1.1    jruoho     /* Ensure that we have a valid GPE number */
   1101  1.1    jruoho 
   1102  1.1    jruoho     GpeEventInfo = AcpiEvGetGpeEventInfo (GpeDevice, GpeNumber);
   1103  1.1    jruoho     if (!GpeEventInfo)
   1104  1.1    jruoho     {
   1105  1.1    jruoho         Status = AE_BAD_PARAMETER;
   1106  1.1    jruoho         goto UnlockAndExit;
   1107  1.1    jruoho     }
   1108  1.1    jruoho 
   1109  1.1    jruoho     /* Make sure that a handler is indeed installed */
   1110  1.1    jruoho 
   1111  1.7  christos     if ((ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) !=
   1112  1.7  christos             ACPI_GPE_DISPATCH_HANDLER) &&
   1113  1.7  christos         (ACPI_GPE_DISPATCH_TYPE (GpeEventInfo->Flags) !=
   1114  1.7  christos             ACPI_GPE_DISPATCH_RAW_HANDLER))
   1115  1.1    jruoho     {
   1116  1.1    jruoho         Status = AE_NOT_EXIST;
   1117  1.1    jruoho         goto UnlockAndExit;
   1118  1.1    jruoho     }
   1119  1.1    jruoho 
   1120  1.1    jruoho     /* Make sure that the installed handler is the same */
   1121  1.1    jruoho 
   1122  1.1    jruoho     if (GpeEventInfo->Dispatch.Handler->Address != Address)
   1123  1.1    jruoho     {
   1124  1.1    jruoho         Status = AE_BAD_PARAMETER;
   1125  1.1    jruoho         goto UnlockAndExit;
   1126  1.1    jruoho     }
   1127  1.1    jruoho 
   1128  1.1    jruoho     /* Remove the handler */
   1129  1.1    jruoho 
   1130  1.1    jruoho     Handler = GpeEventInfo->Dispatch.Handler;
   1131  1.7  christos     GpeEventInfo->Dispatch.Handler = NULL;
   1132  1.1    jruoho 
   1133  1.1    jruoho     /* Restore Method node (if any), set dispatch flags */
   1134  1.1    jruoho 
   1135  1.1    jruoho     GpeEventInfo->Dispatch.MethodNode = Handler->MethodNode;
   1136  1.4    jruoho     GpeEventInfo->Flags &=
   1137  1.4    jruoho         ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK);
   1138  1.4    jruoho     GpeEventInfo->Flags |= Handler->OriginalFlags;
   1139  1.4    jruoho 
   1140  1.4    jruoho     /*
   1141  1.4    jruoho      * If the GPE was previously associated with a method and it was
   1142  1.4    jruoho      * enabled, it should be enabled at this point to restore the
   1143  1.4    jruoho      * post-initialization configuration.
   1144  1.4    jruoho      */
   1145  1.7  christos     if (((ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==
   1146  1.7  christos             ACPI_GPE_DISPATCH_METHOD) ||
   1147  1.7  christos          (ACPI_GPE_DISPATCH_TYPE (Handler->OriginalFlags) ==
   1148  1.7  christos             ACPI_GPE_DISPATCH_NOTIFY)) &&
   1149  1.4    jruoho         Handler->OriginallyEnabled)
   1150  1.1    jruoho     {
   1151  1.4    jruoho         (void) AcpiEvAddGpeReference (GpeEventInfo);
   1152  1.1    jruoho     }
   1153  1.1    jruoho 
   1154  1.6  christos     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
   1155  1.6  christos     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
   1156  1.6  christos 
   1157  1.6  christos     /* Make sure all deferred GPE tasks are completed */
   1158  1.6  christos 
   1159  1.6  christos     AcpiOsWaitEventsComplete ();
   1160  1.6  christos 
   1161  1.1    jruoho     /* Now we can free the handler object */
   1162  1.1    jruoho 
   1163  1.1    jruoho     ACPI_FREE (Handler);
   1164  1.6  christos     return_ACPI_STATUS (Status);
   1165  1.1    jruoho 
   1166  1.1    jruoho UnlockAndExit:
   1167  1.4    jruoho     AcpiOsReleaseLock (AcpiGbl_GpeLock, Flags);
   1168  1.1    jruoho     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
   1169  1.1    jruoho     return_ACPI_STATUS (Status);
   1170  1.1    jruoho }
   1171  1.1    jruoho 
   1172  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiRemoveGpeHandler)
   1173  1.1    jruoho 
   1174  1.1    jruoho 
   1175  1.1    jruoho /*******************************************************************************
   1176  1.1    jruoho  *
   1177  1.1    jruoho  * FUNCTION:    AcpiAcquireGlobalLock
   1178  1.1    jruoho  *
   1179  1.1    jruoho  * PARAMETERS:  Timeout         - How long the caller is willing to wait
   1180  1.1    jruoho  *              Handle          - Where the handle to the lock is returned
   1181  1.1    jruoho  *                                (if acquired)
   1182  1.1    jruoho  *
   1183  1.1    jruoho  * RETURN:      Status
   1184  1.1    jruoho  *
   1185  1.1    jruoho  * DESCRIPTION: Acquire the ACPI Global Lock
   1186  1.1    jruoho  *
   1187  1.1    jruoho  * Note: Allows callers with the same thread ID to acquire the global lock
   1188  1.1    jruoho  * multiple times. In other words, externally, the behavior of the global lock
   1189  1.1    jruoho  * is identical to an AML mutex. On the first acquire, a new handle is
   1190  1.1    jruoho  * returned. On any subsequent calls to acquire by the same thread, the same
   1191  1.1    jruoho  * handle is returned.
   1192  1.1    jruoho  *
   1193  1.1    jruoho  ******************************************************************************/
   1194  1.1    jruoho 
   1195  1.1    jruoho ACPI_STATUS
   1196  1.1    jruoho AcpiAcquireGlobalLock (
   1197  1.1    jruoho     UINT16                  Timeout,
   1198  1.1    jruoho     UINT32                  *Handle)
   1199  1.1    jruoho {
   1200  1.1    jruoho     ACPI_STATUS             Status;
   1201  1.1    jruoho 
   1202  1.1    jruoho 
   1203  1.1    jruoho     if (!Handle)
   1204  1.1    jruoho     {
   1205  1.1    jruoho         return (AE_BAD_PARAMETER);
   1206  1.1    jruoho     }
   1207  1.1    jruoho 
   1208  1.1    jruoho     /* Must lock interpreter to prevent race conditions */
   1209  1.1    jruoho 
   1210  1.1    jruoho     AcpiExEnterInterpreter ();
   1211  1.1    jruoho 
   1212  1.1    jruoho     Status = AcpiExAcquireMutexObject (Timeout,
   1213  1.8  christos         AcpiGbl_GlobalLockMutex, AcpiOsGetThreadId ());
   1214  1.1    jruoho 
   1215  1.1    jruoho     if (ACPI_SUCCESS (Status))
   1216  1.1    jruoho     {
   1217  1.1    jruoho         /* Return the global lock handle (updated in AcpiEvAcquireGlobalLock) */
   1218  1.1    jruoho 
   1219  1.1    jruoho         *Handle = AcpiGbl_GlobalLockHandle;
   1220  1.1    jruoho     }
   1221  1.1    jruoho 
   1222  1.1    jruoho     AcpiExExitInterpreter ();
   1223  1.1    jruoho     return (Status);
   1224  1.1    jruoho }
   1225  1.1    jruoho 
   1226  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiAcquireGlobalLock)
   1227  1.1    jruoho 
   1228  1.1    jruoho 
   1229  1.1    jruoho /*******************************************************************************
   1230  1.1    jruoho  *
   1231  1.1    jruoho  * FUNCTION:    AcpiReleaseGlobalLock
   1232  1.1    jruoho  *
   1233  1.1    jruoho  * PARAMETERS:  Handle      - Returned from AcpiAcquireGlobalLock
   1234  1.1    jruoho  *
   1235  1.1    jruoho  * RETURN:      Status
   1236  1.1    jruoho  *
   1237  1.1    jruoho  * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
   1238  1.1    jruoho  *
   1239  1.1    jruoho  ******************************************************************************/
   1240  1.1    jruoho 
   1241  1.1    jruoho ACPI_STATUS
   1242  1.1    jruoho AcpiReleaseGlobalLock (
   1243  1.1    jruoho     UINT32                  Handle)
   1244  1.1    jruoho {
   1245  1.1    jruoho     ACPI_STATUS             Status;
   1246  1.1    jruoho 
   1247  1.1    jruoho 
   1248  1.1    jruoho     if (!Handle || (Handle != AcpiGbl_GlobalLockHandle))
   1249  1.1    jruoho     {
   1250  1.1    jruoho         return (AE_NOT_ACQUIRED);
   1251  1.1    jruoho     }
   1252  1.1    jruoho 
   1253  1.1    jruoho     Status = AcpiExReleaseMutexObject (AcpiGbl_GlobalLockMutex);
   1254  1.1    jruoho     return (Status);
   1255  1.1    jruoho }
   1256  1.1    jruoho 
   1257  1.1    jruoho ACPI_EXPORT_SYMBOL (AcpiReleaseGlobalLock)
   1258  1.1    jruoho 
   1259  1.5  christos #endif /* !ACPI_REDUCED_HARDWARE */
   1260