1 /******************************************************************************
3 * Module Name: evxface - External interfaces for ACPI events
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2006, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include <acpi/acnamesp.h>
46 #include <acpi/acevents.h>
47 #include <acpi/acinterp.h>
49 #define _COMPONENT ACPI_EVENTS
50 ACPI_MODULE_NAME("evxface")
52 /*******************************************************************************
54 * FUNCTION: acpi_install_exception_handler
56 * PARAMETERS: Handler - Pointer to the handler function for the
61 * DESCRIPTION: Saves the pointer to the handler function
63 ******************************************************************************/
64 #ifdef ACPI_FUTURE_USAGE
65 acpi_status acpi_install_exception_handler(acpi_exception_handler handler)
69 ACPI_FUNCTION_TRACE(acpi_install_exception_handler);
71 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
72 if (ACPI_FAILURE(status)) {
73 return_ACPI_STATUS(status);
76 /* Don't allow two handlers. */
78 if (acpi_gbl_exception_handler) {
79 status = AE_ALREADY_EXISTS;
83 /* Install the handler */
85 acpi_gbl_exception_handler = handler;
88 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
89 return_ACPI_STATUS(status);
92 ACPI_EXPORT_SYMBOL(acpi_install_exception_handler)
93 #endif /* ACPI_FUTURE_USAGE */
95 /*******************************************************************************
97 * FUNCTION: acpi_install_fixed_event_handler
99 * PARAMETERS: Event - Event type to enable.
100 * Handler - Pointer to the handler function for the
102 * Context - Value passed to the handler on each GPE
106 * DESCRIPTION: Saves the pointer to the handler function and then enables the
109 ******************************************************************************/
111 acpi_install_fixed_event_handler(u32 event,
112 acpi_event_handler handler, void *context)
116 ACPI_FUNCTION_TRACE(acpi_install_fixed_event_handler);
118 /* Parameter validation */
120 if (event > ACPI_EVENT_MAX) {
121 return_ACPI_STATUS(AE_BAD_PARAMETER);
124 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
125 if (ACPI_FAILURE(status)) {
126 return_ACPI_STATUS(status);
129 /* Don't allow two handlers. */
131 if (NULL != acpi_gbl_fixed_event_handlers[event].handler) {
132 status = AE_ALREADY_EXISTS;
136 /* Install the handler before enabling the event */
138 acpi_gbl_fixed_event_handlers[event].handler = handler;
139 acpi_gbl_fixed_event_handlers[event].context = context;
141 status = acpi_clear_event(event);
142 if (ACPI_SUCCESS(status))
143 status = acpi_enable_event(event, 0);
144 if (ACPI_FAILURE(status)) {
145 ACPI_WARNING((AE_INFO, "Could not enable fixed event %X",
148 /* Remove the handler */
150 acpi_gbl_fixed_event_handlers[event].handler = NULL;
151 acpi_gbl_fixed_event_handlers[event].context = NULL;
153 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
154 "Enabled fixed event %X, Handler=%p\n", event,
159 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
160 return_ACPI_STATUS(status);
163 ACPI_EXPORT_SYMBOL(acpi_install_fixed_event_handler)
165 /*******************************************************************************
167 * FUNCTION: acpi_remove_fixed_event_handler
169 * PARAMETERS: Event - Event type to disable.
170 * Handler - Address of the handler
174 * DESCRIPTION: Disables the event and unregisters the event handler.
176 ******************************************************************************/
178 acpi_remove_fixed_event_handler(u32 event, acpi_event_handler handler)
180 acpi_status status = AE_OK;
182 ACPI_FUNCTION_TRACE(acpi_remove_fixed_event_handler);
184 /* Parameter validation */
186 if (event > ACPI_EVENT_MAX) {
187 return_ACPI_STATUS(AE_BAD_PARAMETER);
190 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
191 if (ACPI_FAILURE(status)) {
192 return_ACPI_STATUS(status);
195 /* Disable the event before removing the handler */
197 status = acpi_disable_event(event, 0);
199 /* Always Remove the handler */
201 acpi_gbl_fixed_event_handlers[event].handler = NULL;
202 acpi_gbl_fixed_event_handlers[event].context = NULL;
204 if (ACPI_FAILURE(status)) {
205 ACPI_WARNING((AE_INFO,
206 "Could not write to fixed event enable register %X",
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabled fixed event %X\n",
213 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
214 return_ACPI_STATUS(status);
217 ACPI_EXPORT_SYMBOL(acpi_remove_fixed_event_handler)
219 /*******************************************************************************
221 * FUNCTION: acpi_install_notify_handler
223 * PARAMETERS: Device - The device for which notifies will be handled
224 * handler_type - The type of handler:
225 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
226 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
227 * ACPI_ALL_NOTIFY: both system and device
228 * Handler - Address of the handler
229 * Context - Value passed to the handler on each GPE
233 * DESCRIPTION: Install a handler for notifies on an ACPI device
235 ******************************************************************************/
237 acpi_install_notify_handler(acpi_handle device,
239 acpi_notify_handler handler, void *context)
241 union acpi_operand_object *obj_desc;
242 union acpi_operand_object *notify_obj;
243 struct acpi_namespace_node *node;
246 ACPI_FUNCTION_TRACE(acpi_install_notify_handler);
248 /* Parameter validation */
251 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
252 return_ACPI_STATUS(AE_BAD_PARAMETER);
255 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
256 if (ACPI_FAILURE(status)) {
257 return_ACPI_STATUS(status);
260 /* Convert and validate the device handle */
262 node = acpi_ns_map_handle_to_node(device);
264 status = AE_BAD_PARAMETER;
265 goto unlock_and_exit;
270 * Registering a notify handler on the root object indicates that the
271 * caller wishes to receive notifications for all objects. Note that
272 * only one <external> global handler can be regsitered (per notify type).
274 if (device == ACPI_ROOT_OBJECT) {
276 /* Make sure the handler is not already installed */
278 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
279 acpi_gbl_system_notify.handler) ||
280 ((handler_type & ACPI_DEVICE_NOTIFY) &&
281 acpi_gbl_device_notify.handler)) {
282 status = AE_ALREADY_EXISTS;
283 goto unlock_and_exit;
286 if (handler_type & ACPI_SYSTEM_NOTIFY) {
287 acpi_gbl_system_notify.node = node;
288 acpi_gbl_system_notify.handler = handler;
289 acpi_gbl_system_notify.context = context;
292 if (handler_type & ACPI_DEVICE_NOTIFY) {
293 acpi_gbl_device_notify.node = node;
294 acpi_gbl_device_notify.handler = handler;
295 acpi_gbl_device_notify.context = context;
298 /* Global notify handler installed */
303 * Caller will only receive notifications specific to the target object.
304 * Note that only certain object types can receive notifications.
307 /* Notifies allowed on this object? */
309 if (!acpi_ev_is_notify_object(node)) {
311 goto unlock_and_exit;
314 /* Check for an existing internal object */
316 obj_desc = acpi_ns_get_attached_object(node);
319 /* Object exists - make sure there's no handler */
321 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
322 obj_desc->common_notify.system_notify) ||
323 ((handler_type & ACPI_DEVICE_NOTIFY) &&
324 obj_desc->common_notify.device_notify)) {
325 status = AE_ALREADY_EXISTS;
326 goto unlock_and_exit;
329 /* Create a new object */
331 obj_desc = acpi_ut_create_internal_object(node->type);
333 status = AE_NO_MEMORY;
334 goto unlock_and_exit;
337 /* Attach new object to the Node */
340 acpi_ns_attach_object(device, obj_desc, node->type);
342 /* Remove local reference to the object */
344 acpi_ut_remove_reference(obj_desc);
345 if (ACPI_FAILURE(status)) {
346 goto unlock_and_exit;
350 /* Install the handler */
353 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_NOTIFY);
355 status = AE_NO_MEMORY;
356 goto unlock_and_exit;
359 notify_obj->notify.node = node;
360 notify_obj->notify.handler = handler;
361 notify_obj->notify.context = context;
363 if (handler_type & ACPI_SYSTEM_NOTIFY) {
364 obj_desc->common_notify.system_notify = notify_obj;
367 if (handler_type & ACPI_DEVICE_NOTIFY) {
368 obj_desc->common_notify.device_notify = notify_obj;
371 if (handler_type == ACPI_ALL_NOTIFY) {
373 /* Extra ref if installed in both */
375 acpi_ut_add_reference(notify_obj);
380 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
381 return_ACPI_STATUS(status);
384 ACPI_EXPORT_SYMBOL(acpi_install_notify_handler)
386 /*******************************************************************************
388 * FUNCTION: acpi_remove_notify_handler
390 * PARAMETERS: Device - The device for which notifies will be handled
391 * handler_type - The type of handler:
392 * ACPI_SYSTEM_NOTIFY: system_handler (00-7f)
393 * ACPI_DEVICE_NOTIFY: driver_handler (80-ff)
394 * ACPI_ALL_NOTIFY: both system and device
395 * Handler - Address of the handler
399 * DESCRIPTION: Remove a handler for notifies on an ACPI device
401 ******************************************************************************/
403 acpi_remove_notify_handler(acpi_handle device,
404 u32 handler_type, acpi_notify_handler handler)
406 union acpi_operand_object *notify_obj;
407 union acpi_operand_object *obj_desc;
408 struct acpi_namespace_node *node;
411 ACPI_FUNCTION_TRACE(acpi_remove_notify_handler);
413 /* Parameter validation */
416 (!handler) || (handler_type > ACPI_MAX_NOTIFY_HANDLER_TYPE)) {
417 return_ACPI_STATUS(AE_BAD_PARAMETER);
420 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
421 if (ACPI_FAILURE(status)) {
422 return_ACPI_STATUS(status);
425 /* Convert and validate the device handle */
427 node = acpi_ns_map_handle_to_node(device);
429 status = AE_BAD_PARAMETER;
430 goto unlock_and_exit;
435 if (device == ACPI_ROOT_OBJECT) {
436 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
437 "Removing notify handler for namespace root object\n"));
439 if (((handler_type & ACPI_SYSTEM_NOTIFY) &&
440 !acpi_gbl_system_notify.handler) ||
441 ((handler_type & ACPI_DEVICE_NOTIFY) &&
442 !acpi_gbl_device_notify.handler)) {
443 status = AE_NOT_EXIST;
444 goto unlock_and_exit;
447 /* Make sure all deferred tasks are completed */
449 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
450 acpi_os_wait_events_complete(NULL);
451 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
452 if (ACPI_FAILURE(status)) {
453 return_ACPI_STATUS(status);
456 if (handler_type & ACPI_SYSTEM_NOTIFY) {
457 acpi_gbl_system_notify.node = NULL;
458 acpi_gbl_system_notify.handler = NULL;
459 acpi_gbl_system_notify.context = NULL;
462 if (handler_type & ACPI_DEVICE_NOTIFY) {
463 acpi_gbl_device_notify.node = NULL;
464 acpi_gbl_device_notify.handler = NULL;
465 acpi_gbl_device_notify.context = NULL;
469 /* All Other Objects */
472 /* Notifies allowed on this object? */
474 if (!acpi_ev_is_notify_object(node)) {
476 goto unlock_and_exit;
479 /* Check for an existing internal object */
481 obj_desc = acpi_ns_get_attached_object(node);
483 status = AE_NOT_EXIST;
484 goto unlock_and_exit;
487 /* Object exists - make sure there's an existing handler */
489 if (handler_type & ACPI_SYSTEM_NOTIFY) {
490 notify_obj = obj_desc->common_notify.system_notify;
492 (notify_obj->notify.handler != handler)) {
493 status = AE_BAD_PARAMETER;
494 goto unlock_and_exit;
496 /* Make sure all deferred tasks are completed */
498 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
499 acpi_os_wait_events_complete(NULL);
500 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
501 if (ACPI_FAILURE(status)) {
502 return_ACPI_STATUS(status);
505 /* Remove the handler */
506 obj_desc->common_notify.system_notify = NULL;
507 acpi_ut_remove_reference(notify_obj);
510 if (handler_type & ACPI_DEVICE_NOTIFY) {
511 notify_obj = obj_desc->common_notify.device_notify;
513 (notify_obj->notify.handler != handler)) {
514 status = AE_BAD_PARAMETER;
515 goto unlock_and_exit;
517 /* Make sure all deferred tasks are completed */
519 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
520 acpi_os_wait_events_complete(NULL);
521 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
522 if (ACPI_FAILURE(status)) {
523 return_ACPI_STATUS(status);
526 /* Remove the handler */
527 obj_desc->common_notify.device_notify = NULL;
528 acpi_ut_remove_reference(notify_obj);
533 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
534 return_ACPI_STATUS(status);
537 ACPI_EXPORT_SYMBOL(acpi_remove_notify_handler)
539 /*******************************************************************************
541 * FUNCTION: acpi_install_gpe_handler
543 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
545 * gpe_number - The GPE number within the GPE block
546 * Type - Whether this GPE should be treated as an
547 * edge- or level-triggered interrupt.
548 * Address - Address of the handler
549 * Context - Value passed to the handler on each GPE
553 * DESCRIPTION: Install a handler for a General Purpose Event.
555 ******************************************************************************/
557 acpi_install_gpe_handler(acpi_handle gpe_device,
559 u32 type, acpi_event_handler address, void *context)
561 struct acpi_gpe_event_info *gpe_event_info;
562 struct acpi_handler_info *handler;
564 acpi_cpu_flags flags;
566 ACPI_FUNCTION_TRACE(acpi_install_gpe_handler);
568 /* Parameter validation */
570 if ((!address) || (type > ACPI_GPE_XRUPT_TYPE_MASK)) {
571 return_ACPI_STATUS(AE_BAD_PARAMETER);
574 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
575 if (ACPI_FAILURE(status)) {
576 return_ACPI_STATUS(status);
579 /* Ensure that we have a valid GPE number */
581 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
582 if (!gpe_event_info) {
583 status = AE_BAD_PARAMETER;
584 goto unlock_and_exit;
587 /* Make sure that there isn't a handler there already */
589 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
590 ACPI_GPE_DISPATCH_HANDLER) {
591 status = AE_ALREADY_EXISTS;
592 goto unlock_and_exit;
595 /* Allocate and init handler object */
597 handler = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_handler_info));
599 status = AE_NO_MEMORY;
600 goto unlock_and_exit;
603 handler->address = address;
604 handler->context = context;
605 handler->method_node = gpe_event_info->dispatch.method_node;
607 /* Disable the GPE before installing the handler */
609 status = acpi_ev_disable_gpe(gpe_event_info);
610 if (ACPI_FAILURE(status)) {
611 goto unlock_and_exit;
614 /* Install the handler */
616 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
617 gpe_event_info->dispatch.handler = handler;
619 /* Setup up dispatch flags to indicate handler (vs. method) */
621 gpe_event_info->flags &= ~(ACPI_GPE_XRUPT_TYPE_MASK | ACPI_GPE_DISPATCH_MASK); /* Clear bits */
622 gpe_event_info->flags |= (u8) (type | ACPI_GPE_DISPATCH_HANDLER);
624 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
627 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
628 return_ACPI_STATUS(status);
631 ACPI_EXPORT_SYMBOL(acpi_install_gpe_handler)
633 /*******************************************************************************
635 * FUNCTION: acpi_remove_gpe_handler
637 * PARAMETERS: gpe_device - Namespace node for the GPE (NULL for FADT
639 * gpe_number - The event to remove a handler
640 * Address - Address of the handler
644 * DESCRIPTION: Remove a handler for a General Purpose acpi_event.
646 ******************************************************************************/
648 acpi_remove_gpe_handler(acpi_handle gpe_device,
649 u32 gpe_number, acpi_event_handler address)
651 struct acpi_gpe_event_info *gpe_event_info;
652 struct acpi_handler_info *handler;
654 acpi_cpu_flags flags;
656 ACPI_FUNCTION_TRACE(acpi_remove_gpe_handler);
658 /* Parameter validation */
661 return_ACPI_STATUS(AE_BAD_PARAMETER);
664 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
665 if (ACPI_FAILURE(status)) {
666 return_ACPI_STATUS(status);
669 /* Ensure that we have a valid GPE number */
671 gpe_event_info = acpi_ev_get_gpe_event_info(gpe_device, gpe_number);
672 if (!gpe_event_info) {
673 status = AE_BAD_PARAMETER;
674 goto unlock_and_exit;
677 /* Make sure that a handler is indeed installed */
679 if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) !=
680 ACPI_GPE_DISPATCH_HANDLER) {
681 status = AE_NOT_EXIST;
682 goto unlock_and_exit;
685 /* Make sure that the installed handler is the same */
687 if (gpe_event_info->dispatch.handler->address != address) {
688 status = AE_BAD_PARAMETER;
689 goto unlock_and_exit;
692 /* Disable the GPE before removing the handler */
694 status = acpi_ev_disable_gpe(gpe_event_info);
695 if (ACPI_FAILURE(status)) {
696 goto unlock_and_exit;
699 /* Make sure all deferred tasks are completed */
701 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
702 acpi_os_wait_events_complete(NULL);
703 status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
704 if (ACPI_FAILURE(status)) {
705 return_ACPI_STATUS(status);
708 /* Remove the handler */
710 flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
711 handler = gpe_event_info->dispatch.handler;
713 /* Restore Method node (if any), set dispatch flags */
715 gpe_event_info->dispatch.method_node = handler->method_node;
716 gpe_event_info->flags &= ~ACPI_GPE_DISPATCH_MASK; /* Clear bits */
717 if (handler->method_node) {
718 gpe_event_info->flags |= ACPI_GPE_DISPATCH_METHOD;
720 acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
722 /* Now we can free the handler object */
727 (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
728 return_ACPI_STATUS(status);
731 ACPI_EXPORT_SYMBOL(acpi_remove_gpe_handler)
733 /*******************************************************************************
735 * FUNCTION: acpi_acquire_global_lock
737 * PARAMETERS: Timeout - How long the caller is willing to wait
738 * Handle - Where the handle to the lock is returned
743 * DESCRIPTION: Acquire the ACPI Global Lock
745 ******************************************************************************/
746 acpi_status acpi_acquire_global_lock(u16 timeout, u32 * handle)
751 return (AE_BAD_PARAMETER);
754 status = acpi_ex_enter_interpreter();
755 if (ACPI_FAILURE(status)) {
759 status = acpi_ev_acquire_global_lock(timeout);
760 acpi_ex_exit_interpreter();
762 if (ACPI_SUCCESS(status)) {
763 acpi_gbl_global_lock_handle++;
764 *handle = acpi_gbl_global_lock_handle;
770 ACPI_EXPORT_SYMBOL(acpi_acquire_global_lock)
772 /*******************************************************************************
774 * FUNCTION: acpi_release_global_lock
776 * PARAMETERS: Handle - Returned from acpi_acquire_global_lock
780 * DESCRIPTION: Release the ACPI Global Lock. The handle must be valid.
782 ******************************************************************************/
783 acpi_status acpi_release_global_lock(u32 handle)
787 if (handle != acpi_gbl_global_lock_handle) {
788 return (AE_NOT_ACQUIRED);
791 status = acpi_ev_release_global_lock();
795 ACPI_EXPORT_SYMBOL(acpi_release_global_lock)