// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/******************************************************************************
*
* Module Name: evxface - External interfaces for ACPI events
*
* Copyright (C) 2000 - 2020, Intel Corp.
*
*****************************************************************************/
#define EXPORT_ACPI_INTERFACES
#include <acpi/acpi.h>
#include "accommon.h"
#include "acnamesp.h"
#include "acevents.h"
#include "acinterp.h"
#define _COMPONENT ACPI_EVENTS
ACPI_MODULE_NAME("evxface")
#if (!ACPI_REDUCED_HARDWARE)
/* Local prototypes */
static acpi_status
acpi_ev_install_gpe_handler(acpi_handle gpe_device,
u32 gpe_number,
u32 type,
u8 is_raw_handler,
acpi_gpe_handler address, void *context);
#endif
/*******************************************************************************
*
* FUNCTION: acpi_install_notify_handler
*
* PARAMETERS: device - The device for which notifies will be handled
* handler_type - The type of handler:
* ACPI_SYSTEM_NOTIFY: System Handler (00-7F)
* ACPI_DEVICE_NOTIFY: Device Handler (80-FF)
* ACPI_ALL_NOTIFY: Both System and Device
* handler - Address of the handler
* context - Value passed to the handler on each GPE
*
* RETURN: Status
*
* DESCRIPTION: Install a handler for notifications on an ACPI Device,
* thermal_zone, or Processor object.
*
* NOTES: The Root namespace object may have only one handler for each
* type of notify (System/Device). Device/Thermal/Processor objects
* may have one device notify handler, and multiple system notify
* handlers.
*
******************************************************************************/
acpi_status
acpi_install_notify_handler(acpi_handle device,
u32 handler_type,
acpi_notify_handler handler, void *context)
{
struct acpi_namespace_node *node =
ACPI_CAST_PTR(struct acpi_namespace_node, device);
union acpi_operand_object *obj_desc;
union acpi_operand_object *handler_obj;
acpi_status status;
u32 i;
ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
/* Parameter validation */
if ((!device) || (!handler) || (!handler_type) ||
(handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
return_ACPI_STATUS(AE_BAD_PARAMETER);
}
status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS(status);
}
/*
* Root Object:
* Registering a notify handler on the root object indicates that the
* caller wishes to receive notifications for all objects. Note that
* only one global handler can be registered per notify type.
* Ensure that a handler is not already installed.
*/
if (device == ACPI_ROOT_OBJECT) {
for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
if (handler_type & (i + 1)) {
if (acpi_gbl_global_notify[i].handler) {
status = AE_ALREADY_EXISTS;
goto unlock_and_exit;
}
acpi_gbl_global_notify[i].handler = handler;
acpi_gbl_global_notify[i].context = context;
}
}
goto unlock_and_exit; /* Global notify handler installed, all done */
}
/*
* All Other Objects:
* Caller will only receive notifications specific to the target
* object. Note that only certain object types are allowed to
* receive notifications.
*/
/* Are Notifies allowed on this object? */
if (!acpi_ev_is_notify_object(node)) {
status = AE_TYPE;
goto unlock_and_exit;
}
/* Check for an existing internal object, might not exist */
obj_desc = acpi_ns_get_attached_object(node);
if (!obj_desc) {
/* Create a new object */
obj_desc = acpi_ut_create_internal_object(node->type);
if (!obj_desc) {
status = AE_NO_MEMORY;
goto unlock_and_exit;
}
/* Attach new object to the Node, remove local reference */
status = acpi_ns_attach_object(device, obj_desc, node->type);
acpi_ut_remove_reference(obj_desc);
if (ACPI_FAILURE(status)) {
goto unlock_and_exit;
}
}
/* Ensure that the handler is not already installed in the lists */
for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++) {
if (handler_type & (i + 1)) {
handler_obj = obj_desc->common_notify.notify_list[i];
while (handler_obj) {
if (handler_obj->notify.handler == handler) {
status = AE_ALREADY_EXISTS;
goto unlock_and_exit;
}
handler_obj = handler_obj->notify.next[i];
}
}
}
/* Create and populate a new notify handler object */
handler_obj = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
if (!handler_obj) {
status = AE_NO_MEMORY;
goto unlock_and_exit;
}
handler_obj->notify.node = node;
handler_obj->notify.handler_type = handler_type;
handler_obj->notify.handler = handler